# Confusion on DifferentialEquations.jl. How many saved timesteps

**URL:** <https://discourse.julialang.org/t/confusion-on-differentialequations-jl-how-many-saved-timesteps/70409>\
**Category:** Modelling & Simulations\
**Tags:** diffeq\
**Created:** [October 26, 2021, 2:28pm UTC](https://discourse.julialang.org/t/confusion-on-differentialequations-jl-how-many-saved-timesteps/70409 "2021-10-26T14:28:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DerOzean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/derozean/32/28809_2.png) [@DerOzean](https://discourse.julialang.org/u/DerOzean)\
**Post date:** [October 26, 2021, 2:28pm UTC](https://discourse.julialang.org/t/confusion-on-differentialequations-jl-how-many-saved-timesteps/70409/1 "2021-10-26T14:28:53Z")

</div>

Hello everyone,

I’m working on a dynamic food web and solving some differential equations. As solver I’m using the DP5 monte carlo method.

After successful solving the problem I get for every time steps 4 time the same solution. Is this due to the integration method or do I missing something important here?

> #define the Problem  
> prob = ODEProblem(func!,Pop\_0,tspan, p)
> 
> #interaget over T  
> solution = solve(  
> prob,  
> DP5(),  
> alg\_hints=[:stiff],  
> callback=cbs,  
> reltol=1e-5, abstol=1e-5  
> )

My resulting array looks like:

> retcode: Success  
> Interpolation: specialized 4th order “free” interpolation  
> t: 74509-element Vector{Float64}:  
> 0.0  
> 0.06151151478470839  
> 0.06151151478470839  
> 0.06151151478470839  
> 0.06151151478470839  
> 0.10007934446598787  
> 0.10007934446598787  
> 0.10007934446598787  
> 0.10007934446598787  
> 0.1414257567694495  
> 0.1414257567694495  
> 0.1414257567694495  
> 0.1414257567694495  
> …  
> u: 74509-element Vector{Vector{Float32}}:  
> [0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7 … 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 3.458987, 5.472233]  
> [0.69995475, 0.7004576, 0.6996806, 0.6987875, 0.7000644, 0.700556, 0.7007521, 0.70014256, 0.7002482, 0.7001211 … 0.40155038, 0.3999803, 0.39996803, 0.39976335, 0.40271047, 0.39978436, 0.39998668, 0.39997968, 3.4524565, 5.5260825]  
> [0.69995475, 0.7004576, 0.6996806, 0.6987875, 0.7000644, 0.700556, 0.7007521, 0.70014256, 0.7002482, 0.7001211 … 0.40155038, 0.3999803, 0.39996803, 0.39976335, 0.40271047, 0.39978436, 0.39998668, 0.39997968, 3.4524565, 5.5260825]  
> [0.69995475, 0.7004576, 0.6996806, 0.6987875, 0.7000644, 0.700556, 0.7007521, 0.70014256, 0.7002482, 0.7001211 … 0.40155038, 0.3999803, 0.39996803, 0.39976335, 0.40271047, 0.39978436, 0.39998668, 0.39997968, 3.4524565, 5.5260825]  
> [0.69995475, 0.7004576, 0.6996806, 0.6987875, 0.7000644, 0.700556, 0.7007521, 0.70014256, 0.7002482, 0.7001211 … 0.40155038, 0.3999803, 0.39996803, 0.39976335, 0.40271047, 0.39978436, 0.39998668, 0.39997968, 3.4524565, 5.5260825]  
> …

Thanks for the Help,  
DerOzean

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [October 26, 2021, 3:39pm UTC](https://discourse.julialang.org/t/confusion-on-differentialequations-jl-how-many-saved-timesteps/70409/2 "2021-10-26T15:39:05Z")

</div>

What callbacks are you using? It’s possible that several of your callbacks are triggering solution-saving. As always, it helps to have a [minimal working example](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757) that can be easily copy-pasted into the REPL by your interlocutors for troubleshooting.

---

<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 26, 2021, 4:00pm UTC](https://discourse.julialang.org/t/confusion-on-differentialequations-jl-how-many-saved-timesteps/70409/3 "2021-10-26T16:00:53Z")

</div>

It’s definitely from the callbacks because application of discontinuities means there’s no unique value at a given time, and so the results at a time are ordered by the callback applications. Did you not intend to save those changes? If so, then you want to do `save_positions = (false,false)` as described in the callback documentation.

---

<div class="post-metadata">

**Author:** ![DerOzean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/derozean/32/28809_2.png) [@DerOzean](https://discourse.julialang.org/u/DerOzean)\
**Post date:** [October 27, 2021, 10:37am UTC](https://discourse.julialang.org/t/confusion-on-differentialequations-jl-how-many-saved-timesteps/70409/4 "2021-10-27T10:37:59Z")

</div>

Sorry, I just thought it would be a known feature that I don’t understand.

Minimal working example: Lotka\_Voltera on three spezies:

> #parameters  
> a = 0.981  
> b = 1  
> c = 10  
> alpha\_1 = 0.2  
> alpha\_2 = 1  
> k\_1 = 0.05  
> k\_2 = 0  
> w\_p = 0.006
> 
> end\_T = 500  
> #Seeting starting Values  
> Pop\_0 = Array{Float64}(undef, 3) #array for storage
> 
> Pop\_0[1] = 4.7  
> Pop\_0[2] = 4.2  
> Pop\_0[3] = 0.1
> 
> function func!(du,u,p,t)  
> a, b, c, alpha\_1, alpha\_2, k\_1, k\_2, w\_p = p  
> du[1] = a_u[1] - alpha\_1 \* (u[1]u[2])/(1+(k\_1u[1]))  
> du[2] = -b_u[2] + alpha\_1 \* (u[1]_u[2])/(1+(k\_1_u[1])) - alpha\_2 \* (u[2]_u[3])/(1+(k\_2_u[2]))  
> du[3] = -c\*(u[3] - w\_p) + alpha\_2\*(u[2]_u[3])/(1+(k\_2_u[2]))  
> end  
> #calbacks  
> cb = PositiveDomain()
> 
> #define the Problem  
> tspan = (0.0,end\_T)  
> p = (a, b, c, alpha\_1, alpha\_2, k\_1, k\_2, w\_p)  
> prob = ODEProblem(func!,Pop\_0,tspan,p)  
> sol = solve(prob,DP5(), alg\_hints=[:stiff], callback=cb, reltol=1e-5, abstol=1e-5)

This should reproduce the mutltiple saved timesteps

---

<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 27, 2021, 10:54am UTC](https://discourse.julialang.org/t/confusion-on-differentialequations-jl-how-many-saved-timesteps/70409/5 "2021-10-27T10:54:17Z")

</div>

> [@DerOzean](#):
>
> cb = PositiveDomain()

`cb = PositiveDomain(save=false)`? But you probably want to let the PositiveDomain callback do the saving so you get the post-projection values.

---

<div class="post-metadata">

**Author:** ![DerOzean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/derozean/32/28809_2.png) [@DerOzean](https://discourse.julialang.org/u/DerOzean)\
**Post date:** [October 27, 2021, 11:00am UTC](https://discourse.julialang.org/t/confusion-on-differentialequations-jl-how-many-saved-timesteps/70409/6 "2021-10-27T11:00:42Z")

</div>

Thanks,

that solves my question.  
I misunderstood the `save\_positions flag in the documentation.

Regards,  
DerOzean
