Skip to content

Commit df84447

Browse files
committed
Add more complete test for ODESystem with difference operator
1 parent 40502fa commit df84447

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/systems/diffeqs/abstractodesystem.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ function generate_difference_cb(sys::ODESystem, dvs = states(sys), ps = paramete
117117
rhss = [
118118
begin
119119
ind = findfirst(eq -> isdifference(eq.lhs) && isequal(arguments(eq.lhs)[1], s), eqs)
120-
ind === nothing ? Val{0} : eqs[ind].rhs
120+
ind === nothing ? 0 : eqs[ind].rhs
121121
end
122122
for s in dvs ]
123123

test/odesystem.jl

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -398,20 +398,37 @@ eqs = [
398398
δ(y) ~ -c*y + d*x*y,
399399
D(x) ~ y
400400
]
401-
402401
de = ODESystem(eqs,t,[x,y],[a,b,c,d])
403-
404402
@test generate_difference_cb(de) isa ModelingToolkit.DiffEqCallbacks.DiscreteCallback
405403

406-
prob = ODEProblem(ODEFunction{false}(de),[1.0,1.0],(0.0,1.0),[1.5,1.0,3.0,1.0])
404+
# doesn't work with ODEFunction
405+
# prob = ODEProblem(ODEFunction{false}(de),[1.0,1.0],(0.0,1.0),[1.5,1.0,3.0,1.0])
407406

408407
prob = ODEProblem(de,[1.0,1.0],(0.0,1.0),[1.5,1.0,3.0,1.0], check_length=false)
409-
410408
@test prob.kwargs[:difference_cb] isa ModelingToolkit.DiffEqCallbacks.DiscreteCallback
411409

412-
sol = solve(prob, Tsit5(); cb=prob.kwargs[:difference_cb], tstops=prob.tspan[1]:0.1:prob.tspan[2])
410+
sol = solve(prob, Tsit5(); callback=prob.kwargs[:difference_cb], tstops=prob.tspan[1]:0.1:prob.tspan[2])
411+
# using Plots
412+
# plot(sol)
413+
414+
# Direct implementation
415+
416+
function lotka(du,u,p,t)
417+
x = u[1]
418+
y = u[2]
419+
du[1] = p[1]*x - p[2]*x*y
420+
du[2] = -p[3]*y + p[4]*x*y
421+
end
422+
423+
prob2 = ODEProblem(lotka,[1.0,1.0],(0.0,1.0),[1.5,1.0,3.0,1.0])
424+
function periodic_difference_affect!(int)
425+
int.u += [int.u[2], 0]
426+
end
427+
428+
difference_cb = ModelingToolkit.PeriodicCallback(periodic_difference_affect!, 0.1)
429+
430+
sol2 = solve(prob2, Tsit5(); callback=difference_cb, tstops=collect(prob.tspan[1]:0.1:prob.tspan[2])[2:end]
431+
)
432+
# plot(sol2)
413433

414-
#=
415-
using Plots
416-
plot(sol)
417-
=#
434+
@test sol(0:0.01:1) sol2(0:0.01:1)

0 commit comments

Comments
 (0)