# Save additional quantities using DifferentialEquations.jl pkg

**URL:** <https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011>\
**Category:** Modelling & Simulations\
**Tags:** diffeq\
**Created:** [January 31, 2020, 8:35am UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011 "2020-01-31T08:35:12Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![moesphere](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moesphere/32/3348_2.png) [@moesphere](https://discourse.julialang.org/u/moesphere)\
**Post date:** [January 31, 2020, 8:35am UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011/1 "2020-01-31T08:35:12Z")

</div>

Within my ODE function additional quantities are calculated which I would like to save to extract them later for e.g. inspection or plotting.

There is the approach with the `SavingCallback`; however, the `save_func(u, t, integrator)` only has access to the states.

Another option might be to use other callbacks like `ContinousCallback`.

My idea was to extend the differential equations with additional states and setting the corresponding differential equations to zero. It works I guess, but there might be better ways? One shortcoming is that I then have to supply initial values for the additional quantities. Any other ideas?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 31, 2020, 8:44am UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011/2 "2020-01-31T08:44:17Z")

</div>

What about using a DEDataArray style?

---

<div class="post-metadata">

**Author:** ![moesphere](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moesphere/32/3348_2.png) [@moesphere](https://discourse.julialang.org/u/moesphere)\
**Post date:** [January 31, 2020, 8:47am UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011/3 "2020-01-31T08:47:26Z")

</div>

Thanks for the fast answer!

I am using that for input on the right hand side of the differential equations, e.g. control variables.

But why not using it on the LHS as well for saving values like `u.f1 = 1.0`.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 31, 2020, 3:04pm UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011/4 "2020-01-31T15:04:23Z")

</div>

> [@moesphere](#):
>
> But why not using it on the LHS as well for saving values like `u.f1 = 1.0` .

It may not update all internal vectors.

---

<div class="post-metadata">

**Author:** ![ranocha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ranocha/32/35588_2.png) [@ranocha](https://discourse.julialang.org/u/ranocha)\
**Post date:** [January 31, 2020, 4:31pm UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011/5 "2020-01-31T16:31:28Z")

</div>

You can also get access to your ODE function in the `SavingCallback` as `integrator.f.f` (if I remember correctly).

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [January 31, 2020, 7:45pm UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011/6 "2020-01-31T19:45:39Z")

</div>

I’m using a PeriodicCallback for control simulation and just implemented your `du[4] = 0.0` idea. In my controller, on every tick, I update the state variable with `integrator.u[4] = var_of_interest`. Seems to work great.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [February 1, 2020, 10:57am UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011/7 "2020-02-01T10:57:52Z")

</div>

Could you pose the model as a DAE and use a DAE solver?

---

<div class="post-metadata">

**Author:** ![moesphere](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moesphere/32/3348_2.png) [@moesphere](https://discourse.julialang.org/u/moesphere)\
**Post date:** [February 13, 2020, 4:34pm UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011/8 "2020-02-13T16:34:13Z")

</div>

Considering the `DEDataArray` approach:

The minimal example shown below throws an error as an object of type `Array{T,2}` for the field `x` of `SimType` needs to be assigned somewhere in the code.

It works if the inner constructor of `SimType` vectorizes the `x` argument, i.e. `SimType(x,f1) = new{eltype(x)}(vec(x),f1)`.

```julia
using DifferentialEquations
mutable struct SimType{T} <: DEDataVector{T}
    x::Array{T,1}
    f1::T
end

function f(du,u,p,t)
    du[1] = -0.5*u[1]
    du[2] = -0.5*u[2]
    u.f1 = 1.
end

u0 = SimType([10.0;10.0], 0.0)
prob = ODEProblem(f,u0,(0.0,10.0))

sol = solve(prob)

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 13, 2020, 4:42pm UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011/9 "2020-02-13T16:42:31Z")

</div>

If you want to use a matrix, you should type it as a matrix, i.e.

```julia
mutable struct SimType{T} <: DEDataMatrix{T}
    x::Array{T,2}
    f1::T
end

```

It has the same semantics as `Array`

---

<div class="post-metadata">

**Author:** ![moesphere](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moesphere/32/3348_2.png) [@moesphere](https://discourse.julialang.org/u/moesphere)\
**Post date:** [February 13, 2020, 4:44pm UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011/10 "2020-02-13T16:44:48Z")

</div>

It is not a matrix though, but a vector, see the example which is taken from the documentation. Somewhere in the `DifferentialEquations.jl` pkg it changes to a matrix while solving.

If I remove the `u.f1 = 1.` in the ode function, it works fine.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 13, 2020, 4:46pm UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011/11 "2020-02-13T16:46:53Z")

</div>

> [@moesphere](#):
>
> t is not a matrix though, but a vector, see the example which is taken from the documentation. Somewhere in the `DifferentialEquations.jl` pkg it changes to a matrix while solving.
> 
> If I remove the `u.f1 = 1.` in the ode function, it works fine.

It makes a matrix for the stiff ODE solver. Does choosing `Tsit5()` work?

---

<div class="post-metadata">

**Author:** ![moesphere](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moesphere/32/3348_2.png) [@moesphere](https://discourse.julialang.org/u/moesphere)\
**Post date:** [February 13, 2020, 4:49pm UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011/12 "2020-02-13T16:49:18Z")

</div>

Yes, that works.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 13, 2020, 5:32pm UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011/13 "2020-02-13T17:32:02Z")

</div>

What does `false .* u .* u'` generate when `u` is a `SimType`?

---

<div class="post-metadata">

**Author:** ![moesphere](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moesphere/32/3348_2.png) [@moesphere](https://discourse.julialang.org/u/moesphere)\
**Post date:** [February 14, 2020, 7:57am UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011/14 "2020-02-14T07:57:48Z")

</div>

`ERROR: MethodError: no method matching SimType(::Array{Float64,2}, ::Float64)`

Same error when you try to solve the ode with a stiff solver.

---

<div class="post-metadata">

**Author:** ![moesphere](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moesphere/32/3348_2.png) [@moesphere](https://discourse.julialang.org/u/moesphere)\
**Post date:** [February 28, 2020, 2:58pm UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011/15 "2020-02-28T14:58:24Z")

</div>

Is there a plan to resolve this in the near future?

---

<div class="post-metadata">

**Author:** ![acse-ogb119](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acse-ogb119/32/20981_2.png) [@acse-ogb119](https://discourse.julialang.org/u/acse-ogb119)\
**Post date:** [February 4, 2021, 7:45pm UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011/16 "2021-02-04T19:45:12Z")

</div>

I’ve come across the same problem using DEDataVector, same error as @moesphere. Except I also see the error when using `Tsit5()`.

Inside my ode function I calculate an intermediate vector quantity which is used in computing the RHS of the ODE. I want to save this vector at each output time. Seems simple but struggling to figure out how to do it!

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 4, 2021, 10:41pm UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011/17 "2021-02-04T22:41:31Z")

</div>

> [@acse-ogb119](#):
>
> Inside my ode function I calculate an intermediate vector quantity which is used in computing the RHS of the ODE. I want to save this vector at each output time. Seems simple but struggling to figure out how to do it!

We have a new interface for this coming out soon.

---

<div class="post-metadata">

**Author:** ![TimKnab](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timknab/32/22256_2.png) [@TimKnab](https://discourse.julialang.org/u/TimKnab)\
**Post date:** [February 24, 2021, 11:56am UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011/18 "2021-02-24T11:56:48Z")

</div>

Any update on when the new interface will be released? This feature would be really useful!

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 25, 2021, 12:16am UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011/19 "2021-02-25T00:16:29Z")

</div>

It exists but needs better docs. This week and the next are ergonomics and docs.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 1, 2021, 7:46pm UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011/20 "2021-03-01T19:46:29Z")

</div>

@TimKnab using ModelingToolkit, you can add observational equations and use them from the solution and they will be factored out of the solver process for full performance. Example:

[https://mtk.sciml.ai/dev/tutorials/acausal\_components/](https://mtk.sciml.ai/dev/tutorials/acausal_components/)

[Next page](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011.md?page=2)
