# DiffEq: SavingCallback, save befor and after other callback

**URL:** <https://discourse.julialang.org/t/diffeq-savingcallback-save-befor-and-after-other-callback/59263>\
**Category:** Specific Domains\
**Tags:** diffeq\
**Created:** [April 14, 2021, 10:30am UTC](https://discourse.julialang.org/t/diffeq-savingcallback-save-befor-and-after-other-callback/59263 "2021-04-14T10:30:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hexaeder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hexaeder/32/24403_2.png) [@hexaeder](https://discourse.julialang.org/u/hexaeder)\
**Post date:** [April 14, 2021, 10:30am UTC](https://discourse.julialang.org/t/diffeq-savingcallback-save-befor-and-after-other-callback/59263/1 "2021-04-14T10:30:03Z")

</div>

I use a callback to alter the parameters `p` of my system. I also use a `SavingCallback` to save some function `f(u,p)` at every timestep.  
The parameter changing callback has the option `save_positions=(true,true)` and saves the system state before and after the discontinuity.  
The `SavingCallback` however only saves `f(u,p)` after the discontinuity. Is there a way to get around this?

---

<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:** [April 14, 2021, 10:49am UTC](https://discourse.julialang.org/t/diffeq-savingcallback-save-befor-and-after-other-callback/59263/2 "2021-04-14T10:49:34Z")

</div>

> [@hexaeder](#):
>
> The `SavingCallback` however only saves `f(u,p)` after the discontinuity. Is there a way to get around this?

`save_positions = (true,true)`.

---

<div class="post-metadata">

**Author:** ![hexaeder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hexaeder/32/24403_2.png) [@hexaeder](https://discourse.julialang.org/u/hexaeder)\
**Post date:** [April 14, 2021, 10:57am UTC](https://discourse.julialang.org/t/diffeq-savingcallback-save-befor-and-after-other-callback/59263/3 "2021-04-14T10:57:41Z")

</div>

What do you mean by this? There is no such kw argument for `SavingCallback`. It is set to `(false, false)` in the [underlying `DiscreteCallback`](https://github.com/SciML/DiffEqCallbacks.jl/blob/ea4768444af84e123ab142e3b705ba79b8949bd9/src/saving.jl#L112) but this seems correct. I want to save `f(u,p)` to the `SavedValues` bevor and after the other callback. I don’t want to add more steps to the solution object…

---

<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:** [April 14, 2021, 11:05am UTC](https://discourse.julialang.org/t/diffeq-savingcallback-save-befor-and-after-other-callback/59263/4 "2021-04-14T11:05:37Z")

</div>

Oh I see, yes, you can’t do that in the way it’s setup. You’d need to add such saving behavior to the other callback. This is worth an issue in DifferentialEquations.jl. I hope to improve some of the saving options in the near future.

---

<div class="post-metadata">

**Author:** ![hexaeder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hexaeder/32/24403_2.png) [@hexaeder](https://discourse.julialang.org/u/hexaeder)\
**Post date:** [April 14, 2021, 11:15am UTC](https://discourse.julialang.org/t/diffeq-savingcallback-save-befor-and-after-other-callback/59263/5 "2021-04-14T11:15:31Z")

</div>

For a bit more context:  
My system has some internal state `f(u,p)` which I use in a `VectorContinousCallback` in order to change `p` of the system. This change of `p` introduces discontionuities to `f`. I’d like to check `f` as a function of time after I’ve solved the system. Right know I’m trying to to this with aseparat `SavingCallback`.

Alternatively i could save `f` inside the condition function of the continous callback. This seems a bit hacky though… is there any problem with this?

Thanks for the reply anyway, I’ll open an issue then. I think this is also somewhat related to the whole observed variables thing because `f` is basicially an important state of my system for callbacks, analysis and so on… but not a real state in a diffeq solver sense.

---

<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:** [April 14, 2021, 11:16am UTC](https://discourse.julialang.org/t/diffeq-savingcallback-save-befor-and-after-other-callback/59263/6 "2021-04-14T11:16:12Z")

</div>

> [@hexaeder](#):
>
> Alternatively i could save `f` inside the condition function of the continous callback. This seems a bit hacky though… is there any problem with this?

That’s what I would suggest for now.

---

<div class="post-metadata">

**Author:** ![david-pl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/david-pl/32/6224_2.png) [@david-pl](https://discourse.julialang.org/u/david-pl)\
**Post date:** [April 14, 2021, 11:48am UTC](https://discourse.julialang.org/t/diffeq-savingcallback-save-befor-and-after-other-callback/59263/7 "2021-04-14T11:48:58Z")

</div>

> [@ChrisRackauckas](#):
>
> This is worth an issue in DifferentialEquations.jl

FYI, there already is an issue on this:

> <https://github.com/SciML/DiffEqCallbacks.jl/issues/30>
>
> Event handling callbacks (\`DiscreteCallback\`, \`ContinuousCallback\`) allow us to …save \`u\` before and after the \`affect!\` with \`save\_positions=(true,true)\`. It would be a nice option for \`SavingCallback\` to save values before and after the \`affect!\` of event handling callback when those callbacks are used together in a \`CallbackSet\`.

---

<div class="post-metadata">

**Author:** ![ivborissov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ivborissov/32/2841_2.png) [@ivborissov](https://discourse.julialang.org/u/ivborissov)\
**Post date:** [April 29, 2021, 11:21am UTC](https://discourse.julialang.org/t/diffeq-savingcallback-save-befor-and-after-other-callback/59263/8 "2021-04-29T11:21:03Z")

</div>

There are certain problems with triggering `SavingCallback` from other callback functions. I have tried to illustrate them here [SavingCallback used together with event handling callbacks · Issue #30 · SciML/DiffEqCallbacks.jl · GitHub](https://github.com/SciML/DiffEqCallbacks.jl/issues/30#issuecomment-828788228) . Maybe together we can come up with a working solution
