How to obtain numerical values of observed variables after structural simplification?

Hi there. I am quite new to Julia and sorry if this question is too silly, but here it goes.

I have used the modelingtoolkitize function to convert my discretized PDE to symbolic form and then used the strucutural_simplify function followed by the ODAEProblem to convert it into an ODE. I have observed an incredible speedup in the solution time (25x) and I am very impressed!

In the original PDE discretization I have 248 variables. After structural_simplify and alias_elimination, the solve() gives me back 246 “state variables”. I can “access” the other 2 variables via sys.observed, however, they are symbolic equations and they are the most important variables in my system.

Here is a picture of the Equation type after sys.observed command.

I have read the entire documentation and could not find a way to go from the “Equation” type to calculating these two variables X_62(t) and x_124(t) over time. Could someone give me a hint on this?

I can’t find the documentation for this, but the solution struct returned from the call to solve can be indexed by variable:

sol(t, idxs=[X_62, x_124])

Hey! I really appreciate your help and suggestion.

However, it seems that this method can only query variables returned from the solve() call. The problem is that x_62 and x_124 are not returned in the solve call when I use structural_simplify. They are separated from the other 246 variables (states) and are stored in the sys.observed as symbolic equations.

No, it can query observed variables.

Dear Chris,

First, thank you for your answer. I have been following your work and have to say thank you for all high-quality material you make available in the internet.

Despite your invaluable comments, I am not able to query observed variables in my problem.

Aditionally, I tried to print the state variables available after running states(sys) and it returns the following symbols which has length 246 (the same dimension of the vector returned by solve() method).
Just reminding that my original problem - PDE discretization - had 248 variables.

If i try states(sys)[124] it will give me back the state X_128(t). For some reason, it is sorting the states in an order different from the index. And if i try solution(solution.t, idxs = [124]) it gives me the variable x_128.

I do not know why but two unexpected behaviors are happening:
solve() is returning 246 variables and hiding the other 2.
Moreover, it is sorting the solution vector in an awkward manner (x_1(t) is the last value).

Thank you for the support

See the first tutorial.

https://mtk.sciml.ai/dev/tutorials/ode_modeling/

As @contradict mentioned, the solution only solves for a subset of the system, but using the symbols of the observed variables, such as sol[x_128], will retrieve their values. You can use the symbol in place of an index for this feature and it will compute their outputs on demand. Likewise, if you built this from a symbolic vector, sol[x] should work and return the timeseries for the full x, or similarly sol[x,i] etc. using the symbol x itself.

Dear Chris,

I managed to get the variables values with your tip! Thank you for your patience and attention. It is taking me a while to grasp symbolic variables with differentialequations.jl library!

No worries, it’s not you. The symbolic stuff is pretty new so it’s just starting to become standard.