Note that “logging” actually works by going back and re-calculating values, so there is some redundant calculation there. But it’s generally a pretty small price to pay compared to the running time of the simulation.
Can I export some dynamical equations with @log from a custom package?
For example,
using MyPackage: predefined_dynamics! # with @log k = 1
using SimulationLogs
prob = ODEProblem(predefined_dynamics!, x0, tspan)
sol = solve(prob)
out = get_log(sol)
@show out.k # should be an array filled with 1
Sorry for a weird question if I understood incorrectly (I’m not familiar with that package ).
Yep, that should work as long as MyPackage is using SimulationLogs and has the @log k = 1 defined somewhere where it will be called by predefined_dynamics!. For example:
function predefined_dynamics!(dx, x, p, t)
@log k = 1
@log u = -k*x
dx .= A*x + B*u
end
or
function unity_gain(x, p, t)
@log k = 1
return k
end
function predefined_dynamics!(dx, x, p, t)
k = unity_gain(x, p, t)
@log u = -k*x
dx .= A*x + B*u
end
It wouldn’t work, though, if k is defined statically somewhere in MyPackage with a @log in front of it, because then it wouldn’t be called again by get_log.
Awesome!
But as it re-calculate values after simulation, some stochastic behaviour would be hard to be logged.
As a remedy, it would be possible to update (stochastic) parameters and log the parameters.
To reproduce the same result, rng of the stochastic parameter update callback should be reset.
Ah, I see. Unfortunately, since your dynamics function is being called by the solver multiple times per simulation time step, the random values generated while logging will get out of sync with those generated during the initial simulation. I’m not sure what the right solution is here.