ModelingToolkit stepping through simulation manually

Hi! I created a system with modelingtoolkit, which I’m now trying to expose to python through PythonCall.jl in order to simulate the system and learn a controller using reinforcement learning.

For that to work as I imagined, I basically need periodic callbacks, but in a way that yields control back to the caller after every simulation step / callback.

I figured something like this works:

  1. Initialize the ODEProblem

And then for every step (called from python):

  1. remake the ODEProblem with states from previous step, tspan + dt, new control params
  2. solve the problem, returns results

But this requires manual bookkeeping for states and parameters, which I’m having some trouble with:

How can I get the default initial state and parameter maps of the system, adjust some values, and transform them into the correctly ordered array?

Is there a better way to achieve this?

I wish I could just use periodic callbacks, but afaik there is no way to yield control to the caller using those.

Thanks, this is great. Is there a way to get solutions for variables that get optimized away when doing structural_simplify with the integrator interface?

The same symbolic indexing interface applies to it. integrator[x], getu(integrator, x). See

1 Like