Confusion on DifferentialEquations.jl. How many saved timesteps

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

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 that can be easily copy-pasted into the REPL by your interlocutors for troubleshooting.

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.

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] = au[1] - alpha_1 * (u[1]u[2])/(1+(k_1u[1]))
du[2] = -b
u[2] + alpha_1 * (u[1]u[2])/(1+(k_1u[1])) - alpha_2 * (u[2]u[3])/(1+(k_2u[2]))
du[3] = -c*(u[3] - w_p) + alpha_2*(u[2]u[3])/(1+(k_2u[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

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

Thanks,

that solves my question.
I misunderstood the `save_positions flag in the documentation.

Regards,
DerOzean