SciMLBase.jl icon indicating copy to clipboard operation
SciMLBase.jl copied to clipboard

make solution indexing work with top level system namespacing

Open isaacsas opened this issue 3 years ago • 7 comments

If I have a ModelingToolkit ODESystem or such, say sys, with a variable x, then

@variables t, x(t)
... # solve an ODESystem with x
sol[x]

works nicely, however,

sol[sys.x]

doesn't appear to work.

Is it possible to make this work too? This would be convenient for users who don't have the variable x already loaded, say because they are using a generated system.

A similar comment would apply to plotting.

isaacsas avatar Oct 15 '21 16:10 isaacsas

sol[@nonamespace sys.x] to get rid of the top level namespace.

ChrisRackauckas avatar Oct 15 '21 20:10 ChrisRackauckas

Yeah, but that is a lot less convenient than just having it work with the top level namespace too. Especially in plotting if you are trying to select three or four variables. It’s just a small UI improvement, but would give one less thing a new user / less experienced user would have to worry about.

isaacsas avatar Oct 15 '21 20:10 isaacsas

Yeah, I thought @YingboMa was going to change the namespace default in MTK v6 but I don't know what happened to that.

ChrisRackauckas avatar Oct 16 '21 13:10 ChrisRackauckas

The plan was just to keep the default behavior and use at unpack.

YingboMa avatar Oct 16 '21 21:10 YingboMa

@unpack x = sys.x
sol[x]

ChrisRackauckas avatar Oct 17 '21 14:10 ChrisRackauckas

That's definitely better, though still an extra thing for a user to remember and one more line to add to basic examples for accessing solutions or plotting.

Just to look at some different use cases; what are the recommended approaches to get a variable for use in plotting, or in a solution object, in different contexts (i.e. depending on what a user has in scope)?

# top level variable in sys
@unpack x = sys
sol[x]

# sys.sys2 variable x with sys2 in scope
sol[sys2.x]

# what if I want sol[sys.sys2.x] when only sys is in scope, without overwriting x above?
@nonamespace sys2 = sys.sys2
sol[sys2.x] 

# what if sys is a flattened system, and I want the equivalent of sys.sys2.x?
@unpack sys2₊x = sys  # so a user needs to write unicode?
sol[sys2₊x]

Is there one approach that works across systems, systems with subsystems, and flattened systems?

Along those lines, using @unpack is great for getting a few variables, but what if I want to bring every variable or parameter into scope given a system (say to define an initial condition or parameter mapping)?

Sorry for my questions; I'm just trying to understand what is the simplest way to do these various operations, with an eye towards requiring minimal knowledge on a user's part.

isaacsas avatar Oct 17 '21 17:10 isaacsas

Also, feel free to move to MT if this makes more sense there. The scoping seemed a part of SciMLBase, hence why I opened this here, but maybe this is more MT specific at this point.

isaacsas avatar Oct 17 '21 17:10 isaacsas