Skip to content

Commit b1eeb83

Browse files
committed
add more disc event tests
1 parent 3eb74c1 commit b1eeb83

File tree

1 file changed

+39
-14
lines changed

1 file changed

+39
-14
lines changed

test/root_equations.jl

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,17 @@ sys = structural_simplify(model)
336336
@test isempty(ModelingToolkit.continuous_events(sys))
337337

338338
let
339+
function testsol(osys, u0, p, tspan; tstops = Float64[], kwargs...)
340+
oprob = ODEProblem(osys, u0, tspan, p; kwargs...)
341+
sol = solve(oprob, Tsit5(); tstops = tstops, abstol = 1e-10, reltol = 1e-10)
342+
@test isapprox(sol(1.0000000001)[1] - sol(0.999999999)[1], 1.0; rtol = 1e-6)
343+
@test oprob.p[1] == 1.0
344+
@test isapprox(sol(4.0)[1], 2 * exp(-2.0))
345+
sol
346+
end
347+
339348
@parameters k t1 t2
340-
@variables t A(t)
349+
@variables t A(t) B(t)
341350

342351
cond1 = (t == t1)
343352
affect1 = [A ~ A + 1]
@@ -352,21 +361,37 @@ let
352361
u0 = [A => 1.0]
353362
p = [k => 0.0, t1 => 1.0, t2 => 2.0]
354363
tspan = (0.0, 4.0)
355-
oprob = ODEProblem(osys, u0, tspan, p)
356-
sol = solve(oprob, Tsit5(), tstops = [1.0, 2.0]; abstol = 1e-10, reltol = 1e-10)
357-
@test isapprox(sol(1.0000000001)[1] - sol(0.999999999)[1], 1.0; rtol = 1e-6)
358-
@test oprob.p[1] == 1.0
359-
@test isapprox(sol(4.0)[1], 2 * exp(-2.0))
364+
testsol(osys, u0, p, tspan; tstops = [1.0, 2.0])
365+
366+
cond1a = (t == t1)
367+
affect1a = [A ~ A + 1, B ~ A]
368+
cb1a = cond1a => affect1a
369+
@named osys1 = ODESystem(eqs, t, [A, B], [k, t1, t2], discrete_events = [cb1a, cb2])
370+
u0′ = [A => 1.0, B => 0.0]
371+
sol = testsol(osys1, u0′, p, tspan; tstops = [1.0, 2.0], check_length = false)
372+
@test sol(1.0000001, idxs = B) == 2.0
360373

361374
# same as above - but with set-time event syntax
362375
cb1‵ = [1.0] => affect1 # needs to be a Vector for the event to happen only once
363376
cb2‵ = [2.0] => affect2
364-
365-
@named osys‵ = ODESystem(eqs, t, [A], [k, t1, t2], discrete_events = [cb1‵, cb2‵])
366-
oprob‵ = ODEProblem(osys‵, u0, tspan, p)
367-
sol‵ = solve(oprob‵, Tsit5(); abstol = 1e-10, reltol = 1e-10)
368-
369-
@test isapprox(sol‵(1.0000000001)[1] - sol‵(0.999999999)[1], 1.0; rtol = 1e-6)
370-
@test oprob‵.p[1] == 1.0
371-
@test isapprox(sol‵(4.0)[1], 2 * exp(-2.0))
377+
@named osys‵ = ODESystem(eqs, t, [A], [k], discrete_events = [cb1‵, cb2‵])
378+
testsol(osys‵, u0, p, tspan)
379+
380+
# mixing discrete affects
381+
@named osys3 = ODESystem(eqs, t, [A], [k, t1, t2], discrete_events = [cb1, cb2‵])
382+
testsol(osys3, u0, p, tspan; tstops = [1.0])
383+
384+
# mixing with a func affect
385+
function affect!(integrator, u, p, ctx)
386+
integrator.p[p.k] = 1.0
387+
nothing
388+
end
389+
cb2‵‵ = [2.0] => (affect!, [], [k], nothing)
390+
@named osys4 = ODESystem(eqs, t, [A], [k, t1], discrete_events = [cb1, cb2‵‵])
391+
oprob4 = ODEProblem(osys4, u0, tspan, p)
392+
testsol(osys4, u0, p, tspan; tstops = [1.0])
393+
# mixing with symbolic condition in the func affect
394+
cb2‵‵‵ = (t == t2) => (affect!, [], [k], nothing)
395+
@named osys5 = ODESystem(eqs, t, [A], [k, t1, t2], discrete_events = [cb1, cb2‵‵‵])
396+
testsol(osys5, u0, p, tspan; tstops = [1.0, 2.0])
372397
end

0 commit comments

Comments
 (0)