How to get the state of a dynamic system during integration?

Hello everyone, I am calculating Lyapunov exponents for my system using lyapunovspectrum from DynamicalSystems. Can I somehow get the state of the system during integration? For another system, I wrote this code:

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

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

As a result, in the check variable, I get the state value I need. I tried using the same on my system when calculating lyapunov exponents with diffeq:

qq = 0
condition(u,t,integrator) = t==4
function affect!(integrator)
    global qq = integrator.u[1]
end
cb = DiscreteCallback(condition,affect!)
test = lyapunovspectrum(ds,10; Δt = 0.001, diffeq=(callback=cb, tstops=[4.0]))

But nothing is written to the variable qq, can I somehow fix this?

I’d try

@show integrator.u[1]
global qq = integrator.u[1]
@show qq

What happens?

Does not work, qq also remains zero

Does it show something in the REPL? If not your callback might not get called?

It does not show anything in REPL, it displays the vector of Lyapunov exponents as usual:

6-element Vector{BigFloat}:
 -0.084004423986244
  0.53101598407193
 -0.3037323622757
 -4.1028902584876
 -1.4216653152434
 -5.3503278350181

I didn’t quite understand what you mean

I meant this is not a problem of the global variable, but affect!(integrator) never gets called by lyapunovspectrum in the first place. That could be either not implemented or a problem with your parameterization of lyapunovspectrum.

Now I understand, thanks. Do you know if there is any other way to get the state of the system in this case?

Following the documentation I’d try

lyapunovspectrum(ds,10; Δt = 0.001, diffeq=(tstops=[4.0]), callback=cb)

It turns out such an error and again nothing is written to qq.

┌ Warning: Direct propagation of keyword arguments to DifferentialEquations.jl is deprecated.
│ From now on pass any DiffEq-related keywords as a `NamedTuple` using the
│ explicit keyword `diffeq` instead.
└ @ ChaosTools C:\Users\semenjuta.e\.julia\packages\ChaosTools\Xe6aI\src\chaosdetection\lyapunovs.jl:79

If I write like this:

lyapunovspectrum(ds,10; Δt = 0.001, diffeq=(tstops=[4.0], callback=cb))

then there are no errors, but in qq it still remains zero

OK, I tried to check the example: it is (obviously) not complete. So one more remark: what happens if you use

condition(u,t,integrator) = true

?

Nothing happens, everything works the same

OK, I tried the example instead and got for

@show t, n

the result

(Float64[], Float64[])

This doesn’t look right to me. Maybe file an issue?

And I tried the example and everything works correctly. But for my case:

saved_values = SavedValues(Float64, Tuple{BigFloat,BigFloat, BigFloat, BigFloat, BigFloat, BigFloat})
cb = SavingCallback((u,t,integrator)->tr(u), saved_values)

test1 = lyapunovspectrum(ds,10; Δt = 0.001, callback=cb)

println(saved_values.saveval)
println(saved_values.t)

the result is wrong again:

NTuple{6, BigFloat}[]
Float64[]

You tried an example from DifferentialEquations.jl. The link I gave refers to DynamicalSystems.jl of which lyapunovspectrum is part of.

In this example, I have the same error as you.