Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse_expr_to_symbolic can't see function scope variables #1402

Open
orebas opened this issue Jan 14, 2025 · 1 comment
Open

parse_expr_to_symbolic can't see function scope variables #1402

orebas opened this issue Jan 14, 2025 · 1 comment

Comments

@orebas
Copy link

orebas commented Jan 14, 2025

This seems to be a difficulty in pointing parse_expr_to_symbolic to the right scope. In the below MWE, parse_expr_to_symbolic() silently "misses" the @variables declared in the function test(). I believe it creates new, non-time-dependent variables in Main scope as well.

I guess this make sense if one understands the difference between module scope and function scope, but is a potential source for errors and is probably not the desired behavior for most users. The documentation has a nice warning about this, so thanks for that.

using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D

#GLOBAL SCOPE

@variables x(t) y(t) a b
str_one = "a + b*b + x + y*y"
println(Symbolics.parse_expr_to_symbolic(Meta.parse(str_one), @__MODULE__))

#function scope
function test()
	@variables u(t) v(t) c d
	str_two = "c + d*d + u + v*v"
	println(Symbolics.parse_expr_to_symbolic(Meta.parse(str_two), @__MODULE__))
	println(Symbolics.parse_expr_to_symbolic(Meta.parse(str_one), @__MODULE__))
end
test()

OUTPUT:

a + x(t) + b^2 + y(t)^2
c + u + d^2 + v^2
a + x(t) + b^2 + y(t)^2
@orebas
Copy link
Author

orebas commented Jan 14, 2025

I am trying to find a way around this using a "temporary submodule" or something, but I'm not really sure what Julia allows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant