# Saving only after discrete callback

**URL:** <https://discourse.julialang.org/t/saving-only-after-discrete-callback/120790>\
**Category:** General Usage\
**Tags:** plotting, diffeqcallbacks\
**Created:** [October 1, 2024, 4:03pm UTC](https://discourse.julialang.org/t/saving-only-after-discrete-callback/120790 "2024-10-01T16:03:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pulk\_jain](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pulk_jain/32/204900_2.png) [@pulk\_jain](https://discourse.julialang.org/u/pulk_jain)\
**Post date:** [October 1, 2024, 4:03pm UTC](https://discourse.julialang.org/t/saving-only-after-discrete-callback/120790/1 "2024-10-01T16:03:37Z")

</div>

Discrete callback is saving both before and after the callback even when `save_positions = (false,true)`.  
For example:

```julia
function dynamics(u,p,t) 
    du = [2.0,3.0,1.0]
    return du
end

prob = ODEProblem(dynamics, [0.0,0.0,0.0], (0,10))
cond_true(u,t,integrator) = true
affect!(integrator) = (integrator.u[1] = integrator.t)
cb = DiscreteCallback(cond_true, affect!, save_positions = (false, true))
testsol = solve(prob, Tsit5(); callback=cb)
plot(testsol)

```

![plot1](https://global.discourse-cdn.com/julialang/original/3X/f/9/f96bbf6fd31c9267f7500941ef2bc1e05de75063.png)

Here, u[1] should have been equal to u[3]. I am seeing two saves at each time point.  
If I manually scan for `sol.t[i]==sol.t[i+1]`, I could separate out the saves after callback but is there some better way to do this?  
My understanding is that solver is saving each successful step and then checking callback conditions, and then saving that. Is it possible to somehow tell the solver to update (not append) the solution if the new time point is equal to previous time point?

PS: I made this post in a thread about similar discussion without realizing it was a very old discussion. Sorry about that.

---

<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:** [October 21, 2024, 10:20am UTC](https://discourse.julialang.org/t/saving-only-after-discrete-callback/120790/2 "2024-10-21T10:20:50Z")

</div>

Can you open an issue on DifferentialEquations.jl? @Oscar_Smith can help look into it.

---

<div class="post-metadata">

**Author:** ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)\
**Post date:** [October 21, 2024, 11:45am UTC](https://discourse.julialang.org/t/saving-only-after-discrete-callback/120790/3 "2024-10-21T11:45:16Z")

</div>

FWIW, we have an open issue on this in JumpProcesses too: [`save_positions` not working with callbacks · Issue #327 · SciML/JumpProcesses.jl · GitHub](https://github.com/SciML/JumpProcesses.jl/issues/327)

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 21, 2024, 11:52am UTC](https://discourse.julialang.org/t/saving-only-after-discrete-callback/120790/4 "2024-10-21T11:52:27Z")

</div>

how are discrete callbacks supposed to work with saving? does a false as the 1st argument mean that they are supposed to suppress the normal step save?

---

<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:** [October 21, 2024, 12:05pm UTC](https://discourse.julialang.org/t/saving-only-after-discrete-callback/120790/5 "2024-10-21T12:05:26Z")

</div>

It’s supposed to just save after the callback is applied in that case.
