# DifferentialEquations.jl saving data without redundant calculation of control inputs

**URL:** <https://discourse.julialang.org/t/differentialequations-jl-saving-data-without-redundant-calculation-of-control-inputs/62559>\
**Category:** General Usage\
**Tags:** diffeq\
**Created:** [June 8, 2021, 4:27am UTC](https://discourse.julialang.org/t/differentialequations-jl-saving-data-without-redundant-calculation-of-control-inputs/62559 "2021-06-08T04:27:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [June 8, 2021, 4:27am UTC](https://discourse.julialang.org/t/differentialequations-jl-saving-data-without-redundant-calculation-of-control-inputs/62559/1 "2021-06-08T04:27:07Z")

</div>

I’m trying to save some data, which is also calculated within given ODE.

For example, with a linear system, I usually write a function

```julia
A = Matrix(I, n, n)
B = Matrix(I, n, m)
function dynamics!(dx, x, p, t; u)
    dx .= A*x + B*u
end

```

and apply [helper functions](https://jonniedie.github.io/ComponentArrays.jl/stable/examples/adaptive_control/#Helper-Functions-1) provided in [ComponentArrays.jl](https://github.com/jonniedie/ComponentArrays.jl) as

```julia
prob = ODEProblem(apply_inputs(dynamics!; u=(x, p, t) -> -K*x), x0)

```

with given gain matrix `K`.  
Then, when saving input data, I usually use [SavingCallback](https://diffeq.sciml.ai/stable/features/callback_library/#saving_callback) in [DifferentialEquations.jl](https://github.com/SciML/DifferentialEquations.jl) with `affect!` like

```julia
function affect!(x, t, integrator)
    u = -K*x
    (; x=x, u=u)
end

```

I think that in this case I have to calculate `u` twice at each instant when saving data.

1. Is it correct?
2. If so, how can I avoid redundant calculation?

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [June 8, 2021, 1:12pm UTC](https://discourse.julialang.org/t/differentialequations-jl-saving-data-without-redundant-calculation-of-control-inputs/62559/2 "2021-06-08T13:12:11Z")

</div>

Check out [SimulationLogs.jl](https://github.com/jonniedie/SimulationLogs.jl). You could change your `dynamics!` function to:

```julia
function dynamics!(dx, x, p, t; u)
    @log u
    dx .= A*x + B*u
end

```

Then access the control vector at each time point from the solution as

```julia
solution_log = get_log(sol)
solution_log.u

```

You can also interpolate at different time points with

```julia
solution_log_interpolated = get_log(sol, time_points)

```

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.

---

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [June 8, 2021, 2:12pm UTC](https://discourse.julialang.org/t/differentialequations-jl-saving-data-without-redundant-calculation-of-control-inputs/62559/3 "2021-06-08T14:12:38Z")

</div>

Thanks for sharing a nice package!

Can I export some dynamical equations with `@log` from a custom package?  
For example,

```julia
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 🙂 ).

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [June 8, 2021, 2:30pm UTC](https://discourse.julialang.org/t/differentialequations-jl-saving-data-without-redundant-calculation-of-control-inputs/62559/4 "2021-06-08T14:30:29Z")

</div>

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:

```julia
function predefined_dynamics!(dx, x, p, t)
    @log k = 1
    @log u = -k*x
    dx .= A*x + B*u
end

```

or

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

---

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [June 8, 2021, 2:42pm UTC](https://discourse.julialang.org/t/differentialequations-jl-saving-data-without-redundant-calculation-of-control-inputs/62559/5 "2021-06-08T14:42:00Z")

</div>

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.

Am I right? Or, do you have a good idea for this?

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [June 8, 2021, 3:55pm UTC](https://discourse.julialang.org/t/differentialequations-jl-saving-data-without-redundant-calculation-of-control-inputs/62559/6 "2021-06-08T15:55:46Z")

</div>

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.
