Save and plot parameter history with ModelingToolkit.jl

Parameters are currently not saved over time in the solution, the way states are.
This was previously discussed in Update parameters in ModelingToolkit using callbacks.

Chris mentioned there that one option would be to manually record the parameters in callbacks. I’ve done this now and wanted to share my snippet:

Perhaps it is useful for others, but I’d also appreciate feedback on the approach.
It would be nice if such functionality is eventually supported by ModelingToolkit itself.

The Plots.jl plotting recipes on the solution object are convenient, but don’t track parameter history, and therefore can be wrong for observed variables that depend on them. I wanted to use the parameter history to get similar interpolating functions for plotting, and I preferred to use Makie.

Since structural_simplify can move states to either observed or parameters, I look in all 3 in interpolator(sys, integrator, param_hist, sym)::Function.

To demonstrate I applied this approach to the basic ODE example: Composing Ordinary Differential Equations · ModelingToolkit.jl, where I update τ twice. You can see the updates at t = 4 and 8 in the observed RHS plot.



This is nice, thanks for sharing.

1 Like