ModelingToolkit: how to get variable index of ODE solution as interpolating function

Since the ordering of symbols is not guaranteed, the solution of ode’s from ModelingToolkit is usually accessed by name. However, sol[foo] only gives a vector (against sol.t). How to find the index of some variable foo in the solutions’s interpolating function interface sol(t)? example from doc:

using ModelingToolkit, OrdinaryDiffEq

@parameters t σ ρ β
@variables x(t) y(t) z(t)
D = Differential(t)

eqs = [D(D(x)) ~ σ*(y-x),
       D(y) ~ x*(ρ-z)-y,
       D(z) ~ x*y - β*z]

sys = ODESystem(eqs)
sys = ode_order_lowering(sys)

u0 = [D(x) => 2.0,
      x => 1.0,
      y => 0.0,
      z => 0.0]

p  = [σ => 28.0,
      ρ => 10.0,
      β => 8/3]

tspan = (0.0,100.0)
prob = ODEProblem(sys,u0,tspan,p,jac=true)
sol = solve(prob,Tsit5())

how do I get the index of, say, the index of x from e.g. sol(1.2)? Looks like the faq’s index finding applies only to parameters and not on the ODESolution object.


Edit: looks like sys.states gives a vector of Term, one may compare against that with
[ for s in sys.states]. It appears to be the same index given in sol(t)


1 Like