ModelingToolkit.jl
ModelingToolkit.jl copied to clipboard
Handling of wrongly namespaced variables
An incredibly common error that makes the user experience bad for both new and experienced users of MTK is the fact that one can not use
sys.x
to, e.g., set initial conditions
u0 = [sys.x => 0]
and instead have to use something like
@unpack x = sys
u0 = [x => 0]
The error message is often not enough guide for the user:
@variables t
D = Differential(t)
@variables x(t)
eqs = [D(x) ~ -x]
@named sys = ODESystem(eqs, t)
u0 = [sys.x => 1.0]
tspan = (0.0, 1.0)
ODEProblem(sys, u0, tspan)
ERROR: ArgumentError: SymbolicUtils.Symbolic{Real}[x(t)] are missing from the variable map.
We should at least come up with a way to provide better error messages, but even better would be to just let the user use sys.x
since, from the user's perspective, it is indeed sys.x
the user wants to provide an initial condition for (evidenced by how often this error is encountered by actual users).
I make this error several times per day in different contexts while working with MTK, and I've helped several others with exactly this problem.
I definitely brought this up before, though I can't find the issue. It seems like this is now fixed in both solution indexing, i.e. sol(t, idxs=[sys.x])
and plot indexing, i.e. plot(sol, idxs=[sys.x])
, so feels like for consistency this should also be allowed in specifying parameter and initial condition mappings.
In Catalyst I went and added the ability to specify these mappings via Symbol
s that we then convert via getproperty
to the symbolic variables, specifically due to this issue. But that is somewhat of a hack I guess.
complete
and v9 handles this.