How to know variable order in the result after solving an ODE

Hi everyone, I tried to solve the ODE system with three sets of variables (V, theta, and delta) each set has 3 variables. After solving I got the array as a result. How could I know which value belongs to which variables?

Hi @Ethan_Tran!
Could you post a reproducible example so that we can help you understand it?

@variables t  (delta(t))[1:3] (V(t))[1:3] (theta(t))[1:3] (Id(t))[1:3] (Iq(t))[1:3]
D = Differential(t)
eqs = [
    0 ~ Ed[1] - V[1]*sin(delta[1] - theta[1]) + Iq[1]*0.0969,
    0 ~ Eq[1] - V[1]*cos(delta[1] - theta[1]) - Id[1]*0.0608,
    0 ~ Ed[2] - V[2]*sin(delta[2] - theta[2]) + Iq[2]*0.1969,
    0 ~ Eq[2] - V[2]*cos(delta[2] - theta[2]) - Id[2]*0.1198,
    0 ~ Ed[3] - V[3]*sin(delta[3] - theta[3]) + Iq[3]*0.25,
    0 ~ Eq[3] - V[3]*cos(delta[3] - theta[3]) - Id[3]*0.1813,
@named sys = ODESystem(eqs)
three_machine_ode = structural_simplify(sys)
tspan = (0.0,10.0)
h = 0.01
u0 = [
    delta[1] => 3.58*pi/180,
    delta[2] => 61.1*pi/180,
    delta[3] => 54.2*pi/180,
    V[1] => 1.0,
    V[2] => 1.0,
    V[3] => 1.0,
    theta[1] => 0.0,
    theta[2] => 0.0,
    theta[3] => 0.0,
    Id[1] => 0.302,
    Id[2] => 1.29,
    Id[3] => 0.562,
    Iq[1] => 0.671,
    Iq[2] => 0.931,
    Iq[3] => 0.619
prob = ODEProblem(sys,u0,tspan)
sol = solve(prob,dt = h, adaptive = false)

This is just a part of the equations in my model. After solving, the result is the array of values. I would like to know how I figure out which vector of values belongs to delta1, delta2, delta3, theta1, theta2,theta3 … Thank you for your time.

Can you try something like dump(sol) to see if your solution has named attributes? Or alternately, explore it in the VSCode Julia panel? I’m not an expert of ODEs + Symbolics but that’s what I would try to recover the individual variables

Thank you for your information, I am a newbie in Julia, so I have not tried dump(sol) before. I will try it.

I have another question how could I save data into a CSV file with the given header?

Can you please tell us if “dump(sol)” works for you.

I have tried but I think it does not work for me.

I don’t know the answer but I can suggest a way to find out. Look at the solution at time zero, which should equal the initial conditions, e.g. delta1=3.58*pi/180. The initial delta states should this be identifiable, and that may be enough to deduce the rest. You could also simulate with distinct initial values for V[1:3] and theta[1:3], such as 1, 2, 3, etc. it doesn’t matter if they are nonsensical, it’s just to figure out the ordering.

I would guess the order is serial delta[1], delta[2], etc. because it’s hard to imagine it could be done as delta[1], V[1], theta[1], because there’s no expectation every variable is parallel 1:3. So serial makes the most sense.

Of course, the real answer is probably in the documentation, but the simple test is helpful for making a mental model for what’s going on.

Thank you for your help. Actually, the model has a lot of sets of variables with the initial condition being zero. The code I posted is just a part of the model.

This shows how to access solution data using the symbolic variables instead of integer indexes. It is written for symbolic variables in Catalyst, but the guidance should be the same for solving via ODESystems:

FAQs · Catalyst.jl?

Yes, the idea is not to assign 0 to all the initial values, but to intentionally make them distinct so you can figure out the ordering. Just set them to 1, 2, 3, in the serial ordering and then do a simulation and look at sol(0) to see the initial values in order of state. If the actual ordering is parallel then you might see 1, 4, 7, etc. or something else. You also don’t need to do this for every single state, because you can probably figure out the pattern from the first three.

See states(sys) for the variable order in the solution. You can also index the solution using symbols: sol[var_name]


Thank you for your information