How to (properly) use the solution of a simulation in ModelingToolkit

Hi there.

I am starting to use the amazing ModellingToolkit, but I am afraid I am missing something important. So I apologize if my questions is silly.

Suppose I have a model consisting of two differential equations, two dependent variables x1 and x2 and an independent variable t. For example:

 
 @parameters t a1 a2;
 @variables x1(t) x2(t);

 D = Differential(t);
 D2 = Differential(t)^2;

 f(t) = 0.1*sin(2*pi*t);
 T1 = a1*f(t)
 T2 = a2*f(t)

 eq = [  D2(x1)    ~ T1 - D(x2),
            D2(x2)    ~ T2 - D(x1) ];
 sys = ODESystem(eq);
 sys = ode_order_lowering(sys);

 tspan = (0.0,10.0);
 p =[a1   =>  1.0,
     a2   =>  2.0 ];

 u0 =[ D(x1)    => 0.0, 
       x1       => 0.0, 
       D(x2)    => 0.0, 
       x2       => 0.0, 
        ];

 prob = ODEProblem(sys,u0,tspan,p,jac=true)
 sol = solve(prob);

I can plot the solution x1(t) even without knowing the position of this variable in pos(t), since there is a Plot recipe

 plot(sol,vars=(x1));

My question is. Suppose I want to make some mathematical operations on x1(t). For example, evaluate the integral of (x1(t)/(a+b))^2 over the time span.

How can I “recover” x1(t) from sol and use it along with the symbolic operations? For example,

expr = substitute( (x1/(a+b))^2, p) 

will substitute the values of a and b, as expected. But if I try to use it with QuadGK, for example, it will give me an error, since x1(t) is not linked to sol(t).

I guess I am missing something important about it and I really want to understand how to properly use the solution provided by DifferentialEquations here.

Thank you.

sol[x1]?

1 Like

Thank you @ChrisRackauckas

Is it possible to interpolate, like in sol(t)? I would like to call something like sol(t)[x1] or sol[x1](t)..

Not yet. It will be sol(t;idxs=x1) when it’s ready.

1 Like

OK.

Thank you very much!