How to get system state during event handling?

Hello everyone, I’m going through an example from the documentation about event handling during dynamic system integration.

using DifferentialEquations
function f(du,u,p,t)
    du[1] = -u[1]
end
u0 = [10.0]
const V = 1
prob = ODEProblem(f,u0,(0.0,10.0))
sol = solve(prob,Tsit5())
Plots.plot(sol)

At a certain step (t = 4.0), I want to get the state of the system, for this I wrote the following code:

check = 0
condition(u,t,integrator) = t==4
function affect!(integrator)
    check = integrator.u[1]
end
cb = DiscreteCallback(condition,affect!)

sol = solve(prob,Tsit5(),callback=cb, tstops=[4.0])
Plots.plot(sol)

I can not understand why the value is not written to the “check” variable? If instead of “check = integrator.u[1]” I write “print(integrator.u[1])”, then it works, and the value I need is displayed on the screen. And how to remember it in some kind of variable, so that later it can be used somewhere else?

Shot in the dark: probably a missing

    global check = integrator.u[1]

?

Good question, I’d expect some kind of parameter for user data, too.

1 Like