Within my ODE function additional quantities are calculated which I would like to save to extract them later for e.g. inspection or plotting.

There is the approach with the SavingCallback; however, the save_func(u, t, integrator) only has access to the states.

Another option might be to use other callbacks like ContinousCallback.

My idea was to extend the differential equations with additional states and setting the corresponding differential equations to zero. It works I guess, but there might be better ways? One shortcoming is that I then have to supply initial values for the additional quantities. Any other ideas?

I’m using a PeriodicCallback for control simulation and just implemented your du[4] = 0.0 idea. In my controller, on every tick, I update the state variable with integrator.u[4] = var_of_interest. Seems to work great.

It is not a matrix though, but a vector, see the example which is taken from the documentation. Somewhere in the DifferentialEquations.jl pkg it changes to a matrix while solving.

If I remove the u.f1 = 1. in the ode function, it works fine.

I’ve come across the same problem using DEDataVector, same error as @moesphere. Except I also see the error when using Tsit5().

Inside my ode function I calculate an intermediate vector quantity which is used in computing the RHS of the ODE. I want to save this vector at each output time. Seems simple but struggling to figure out how to do it!

@TimKnab using ModelingToolkit, you can add observational equations and use them from the solution and they will be factored out of the solver process for full performance. Example: