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

**URL:** https://discourse.julialang.org/t/how-to-get-the-state-of-a-dynamic-system-during-integration/78016
**Category:** General Usage
**Tags:** diffeqcallbacks
**Created:** [March 17, 2022, 9:48am UTC](https://discourse.julialang.org/t/how-to-get-the-state-of-a-dynamic-system-during-integration/78016 "2022-03-17T09:48:59Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![egor](https://avatars.discourse-cdn.com/v4/letter/e/6de8d8/32.png) [@egor](https://discourse.julialang.org/u/egor)
#### Post date: [March 17, 2022, 9:48am UTC](https://discourse.julialang.org/t/how-to-get-the-state-of-a-dynamic-system-during-integration/78016/1 "2022-03-17T09:48:59Z")

</div>

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:

```julia
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:

```julia
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?

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [March 17, 2022, 9:53am UTC](https://discourse.julialang.org/t/how-to-get-the-state-of-a-dynamic-system-during-integration/78016/2 "2022-03-17T09:53:07Z")

</div>

I’d try

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

```

What happens?

---

<div class="post-metadata">

### Author: ![egor](https://avatars.discourse-cdn.com/v4/letter/e/6de8d8/32.png) [@egor](https://discourse.julialang.org/u/egor)
#### Post date: [March 17, 2022, 9:56am UTC](https://discourse.julialang.org/t/how-to-get-the-state-of-a-dynamic-system-during-integration/78016/3 "2022-03-17T09:56:06Z")

</div>

Does not work, qq also remains zero

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [March 17, 2022, 9:57am UTC](https://discourse.julialang.org/t/how-to-get-the-state-of-a-dynamic-system-during-integration/78016/4 "2022-03-17T09:57:13Z")

</div>

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

---

<div class="post-metadata">

### Author: ![egor](https://avatars.discourse-cdn.com/v4/letter/e/6de8d8/32.png) [@egor](https://discourse.julialang.org/u/egor)
#### Post date: [March 17, 2022, 9:59am UTC](https://discourse.julialang.org/t/how-to-get-the-state-of-a-dynamic-system-during-integration/78016/5 "2022-03-17T09:59:16Z")

</div>

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

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

```

---

<div class="post-metadata">

### Author: ![egor](https://avatars.discourse-cdn.com/v4/letter/e/6de8d8/32.png) [@egor](https://discourse.julialang.org/u/egor)
#### Post date: [March 17, 2022, 10:00am UTC](https://discourse.julialang.org/t/how-to-get-the-state-of-a-dynamic-system-during-integration/78016/6 "2022-03-17T10:00:06Z")

</div>

> [@goerch](#):
>
> If not your callback might not get called?

I didn’t quite understand what you mean

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [March 17, 2022, 10:04am UTC](https://discourse.julialang.org/t/how-to-get-the-state-of-a-dynamic-system-during-integration/78016/7 "2022-03-17T10:04:36Z")

</div>

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`.

---

<div class="post-metadata">

### Author: ![egor](https://avatars.discourse-cdn.com/v4/letter/e/6de8d8/32.png) [@egor](https://discourse.julialang.org/u/egor)
#### Post date: [March 17, 2022, 10:09am UTC](https://discourse.julialang.org/t/how-to-get-the-state-of-a-dynamic-system-during-integration/78016/8 "2022-03-17T10:09:03Z")

</div>

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

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [March 17, 2022, 10:13am UTC](https://discourse.julialang.org/t/how-to-get-the-state-of-a-dynamic-system-during-integration/78016/9 "2022-03-17T10:13:28Z")

</div>

Following the [documentation](https://juliadynamics.github.io/DynamicalSystems.jl/v1.3/advanced/#using-callbacks-with-integrators) I’d try

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

```

---

<div class="post-metadata">

### Author: ![egor](https://avatars.discourse-cdn.com/v4/letter/e/6de8d8/32.png) [@egor](https://discourse.julialang.org/u/egor)
#### Post date: [March 17, 2022, 10:21am UTC](https://discourse.julialang.org/t/how-to-get-the-state-of-a-dynamic-system-during-integration/78016/10 "2022-03-17T10:21:48Z")

</div>

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

```julia
┌ 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:

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

```

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

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [March 17, 2022, 10:35am UTC](https://discourse.julialang.org/t/how-to-get-the-state-of-a-dynamic-system-during-integration/78016/11 "2022-03-17T10:35:15Z")

</div>

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

```julia
condition(u,t,integrator) = true

```

?

---

<div class="post-metadata">

### Author: ![egor](https://avatars.discourse-cdn.com/v4/letter/e/6de8d8/32.png) [@egor](https://discourse.julialang.org/u/egor)
#### Post date: [March 17, 2022, 10:37am UTC](https://discourse.julialang.org/t/how-to-get-the-state-of-a-dynamic-system-during-integration/78016/12 "2022-03-17T10:37:17Z")

</div>

Nothing happens, everything works the same

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [March 17, 2022, 10:45am UTC](https://discourse.julialang.org/t/how-to-get-the-state-of-a-dynamic-system-during-integration/78016/13 "2022-03-17T10:45:37Z")

</div>

OK, I tried the [example](https://github.com/JuliaDynamics/DynamicalSystems.jl/blob/687de2779fa3b791e045bdc5ca0147654c8eab4b/docs/src/ds/integrators.md#using-callbacks-with-integrators) instead and got for

```julia
@show t, n

```

the result

```julia
(Float64[], Float64[])

```

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

---

<div class="post-metadata">

### Author: ![egor](https://avatars.discourse-cdn.com/v4/letter/e/6de8d8/32.png) [@egor](https://discourse.julialang.org/u/egor)
#### Post date: [March 17, 2022, 10:56am UTC](https://discourse.julialang.org/t/how-to-get-the-state-of-a-dynamic-system-during-integration/78016/14 "2022-03-17T10:56:54Z")

</div>

And I tried the [example](https://diffeq.sciml.ai/stable/features/callback_library/#Example-2) and everything works correctly. But for my case:

```julia
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:

```julia
NTuple{6, BigFloat}[]
Float64[]

```

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [March 17, 2022, 11:00am UTC](https://discourse.julialang.org/t/how-to-get-the-state-of-a-dynamic-system-during-integration/78016/15 "2022-03-17T11:00:13Z")

</div>

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

---

<div class="post-metadata">

### Author: ![egor](https://avatars.discourse-cdn.com/v4/letter/e/6de8d8/32.png) [@egor](https://discourse.julialang.org/u/egor)
#### Post date: [March 17, 2022, 11:04am UTC](https://discourse.julialang.org/t/how-to-get-the-state-of-a-dynamic-system-during-integration/78016/16 "2022-03-17T11:04:44Z")

</div>

> [@goerch](#):
>
> OK, I tried the [example](https://github.com/JuliaDynamics/DynamicalSystems.jl/blob/687de2779fa3b791e045bdc5ca0147654c8eab4b/docs/src/ds/integrators.md#using-callbacks-with-integrators) instead and got for

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