# ModelingToolkit stepping through simulation manually

**URL:** <https://discourse.julialang.org/t/modelingtoolkit-stepping-through-simulation-manually/111736>\
**Category:** Modelling & Simulations\
**Tags:** modelingtoolkit\
**Created:** [March 17, 2024, 10:45pm UTC](https://discourse.julialang.org/t/modelingtoolkit-stepping-through-simulation-manually/111736 "2024-03-17T22:45:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rg2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rg2/32/207810_2.png) [@rg2](https://discourse.julialang.org/u/rg2)\
**Post date:** [March 17, 2024, 10:45pm UTC](https://discourse.julialang.org/t/modelingtoolkit-stepping-through-simulation-manually/111736/1 "2024-03-17T22:45:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 18, 2024, 12:03am UTC](https://discourse.julialang.org/t/modelingtoolkit-stepping-through-simulation-manually/111736/2 "2024-03-18T00:03:22Z")

</div>

> **[Integrator Interface · DifferentialEquations.jl](https://docs.sciml.ai/DiffEqDocs/stable/basics/integrator/)**
>
> Documentation for DifferentialEquations.jl.

---

<div class="post-metadata">

**Author:** ![rg2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rg2/32/207810_2.png) [@rg2](https://discourse.julialang.org/u/rg2)\
**Post date:** [March 18, 2024, 11:25pm UTC](https://discourse.julialang.org/t/modelingtoolkit-stepping-through-simulation-manually/111736/3 "2024-03-18T23:25:10Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 19, 2024, 2:19am UTC](https://discourse.julialang.org/t/modelingtoolkit-stepping-through-simulation-manually/111736/4 "2024-03-19T02:19:55Z")

</div>

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

> **[Home · SymbolicIndexingInterface.jl](https://docs.sciml.ai/SymbolicIndexingInterface/stable/)**
>
> Documentation for SymbolicIndexingInterface.jl.
