# Track all variables within Julia DifferentialEquations.jl

**URL:** <https://discourse.julialang.org/t/track-all-variables-within-julia-differentialequations-jl/86100>\
**Category:** New to Julia\
**Tags:** question, package, performance, differentialequation\
**Created:** [August 21, 2022, 7:15pm UTC](https://discourse.julialang.org/t/track-all-variables-within-julia-differentialequations-jl/86100 "2022-08-21T19:15:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![t-anton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/t-anton/32/38981_2.png) [@t-anton](https://discourse.julialang.org/u/t-anton)\
**Post date:** [August 21, 2022, 7:15pm UTC](https://discourse.julialang.org/t/track-all-variables-within-julia-differentialequations-jl/86100/1 "2022-08-21T19:15:26Z")

</div>

Hi All,

I want to track all variables within Julia’s DifferentialEquation’s efficiently (easy and fast).  
My current solution, on a test function, is:

```julia
function test2(du,u,p,t)
    
    global i
    global Out_values
    global sampletimes
    
    a,b,c = p
    
    d=a^0.1*(t+1)
    e=u[1]/a
    f=u[2]/d
    
    if t in sampletimes
        
        Out_values[1,i] = d
        Out_values[2,i] = e
        Out_values[3,i] = f
        i=i+1
    
    end
    
    du[1] = a*u[1]
    du[2] = d*u[2]
    du[3] = b*u[2] - c*u[3]
end

sampletimes = tspan[1]:0.3:tspan[2]
Out_values = Array{Float64}(undef, 3, 2*11)
i=1

prob = ODEProblem(test2,u0,tspan,p)
sol = solve(prob,saveat=0.3,tstops=sampletimes)

```

However, there are two problems:

1. Somehow I get duplicated results (not sure why) with slightly different results, and
2. I can’t track variables if I decide to use all results, i.e. `sol = solve(prob)`.

Any help is appreciated.

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [August 22, 2022, 2:07pm UTC](https://discourse.julialang.org/t/track-all-variables-within-julia-differentialequations-jl/86100/2 "2022-08-22T14:07:34Z")

</div>

1. You could use this: [Callback Library · DifferentialEquations.jl](https://diffeq.sciml.ai/stable/features/callback_library/#saving_callback)  
It kind of does the same as you do, but it works with adaptive step-sizes and avoids using globals.

2. A second approach could also be to compute the additional values afterwards. For example you could define `d_values = @. p.a^(0.1)*(sol.t)`, `e_values = @. sol[1,:] ./ p.a` and `f_values = @. sol[2,:] ./ p.d`… If you don’t have too many values to track, this might be easier.

To answer the questions:

- You get duplicated results because solvers (especially implicit solvers) evaluate the function many times, to approximate the Jacobian, or for adaptive stepsizes. These additional evaluations are tracked in your approach. The callback library takes care of these things!
- I don’t understand the second problem fully, but with the saving callbacks you could call the `solve` function with any arguments (i.e. also without the `saveat`). With the second approach it directly uses the `sol` array, so it shouldbe compatible in all cases.

---

<div class="post-metadata">

**Author:** ![DrPapa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drpapa/32/6835_2.png) [@DrPapa](https://discourse.julialang.org/u/DrPapa)\
**Post date:** [August 22, 2022, 2:46pm UTC](https://discourse.julialang.org/t/track-all-variables-within-julia-differentialequations-jl/86100/3 "2022-08-22T14:46:23Z")

</div>

I don’t have experience with it, but this might fit the bill: [https://github.com/jonniedie/SimulationLogs.jl](https://github.com/jonniedie/SimulationLogs.jl)

---

<div class="post-metadata">

**Author:** ![t-anton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/t-anton/32/38981_2.png) [@t-anton](https://discourse.julialang.org/u/t-anton)\
**Post date:** [August 22, 2022, 3:44pm UTC](https://discourse.julialang.org/t/track-all-variables-within-julia-differentialequations-jl/86100/4 "2022-08-22T15:44:47Z")

</div>

@SteffenPL, thanks for the answer. Is there a tutorial or an example of a similar problem where callbacks are applied?

---

<div class="post-metadata">

**Author:** ![t-anton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/t-anton/32/38981_2.png) [@t-anton](https://discourse.julialang.org/u/t-anton)\
**Post date:** [August 22, 2022, 3:47pm UTC](https://discourse.julialang.org/t/track-all-variables-within-julia-differentialequations-jl/86100/5 "2022-08-22T15:47:53Z")

</div>

@DrPapa, thank you very much for answering my question. I took a look at the package, and it seems it serves the purpose. I will investigate more later and check how it performs against the callback library that @SteffenPL proposed.

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [August 22, 2022, 4:01pm UTC](https://discourse.julialang.org/t/track-all-variables-within-julia-differentialequations-jl/86100/6 "2022-08-22T16:01:25Z")

</div>

> @SteffenPL, thanks for the answer. Is there a tutorial or an example of a similar problem where callbacks are applied?

It’s for example in the documentation when you scroll down.

For you case it looks like this:

```julia

function saving_callback(u, t, integrator)
    a, b, c = integrator.p
    d=a^0.1*(t+1)
    e=u[1]/a
    f=u[2]/d
    return (d, e, f)
end

saved_values = SavedValues(Float64, Float64, Float64)
cb = SavingCallback(saving_callback, saved_values)

sol = solve(prob, callback=cb)

```

now `saved_values.saveval` contains the values corresponding to the times `saved_values.t`.

---

<div class="post-metadata">

**Author:** ![t-anton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/t-anton/32/38981_2.png) [@t-anton](https://discourse.julialang.org/u/t-anton)\
**Post date:** [August 22, 2022, 9:54pm UTC](https://discourse.julialang.org/t/track-all-variables-within-julia-differentialequations-jl/86100/7 "2022-08-22T21:54:29Z")

</div>

Thank you very much, @SteffenPL. This helps me a lot to understand the callback. Best regards!
