@@ -336,8 +336,17 @@ sys = structural_simplify(model)
336
336
@test isempty (ModelingToolkit. continuous_events (sys))
337
337
338
338
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
+
339
348
@parameters k t1 t2
340
- @variables t A (t)
349
+ @variables t A (t) B (t)
341
350
342
351
cond1 = (t == t1)
343
352
affect1 = [A ~ A + 1 ]
@@ -352,21 +361,37 @@ let
352
361
u0 = [A => 1.0 ]
353
362
p = [k => 0.0 , t1 => 1.0 , t2 => 2.0 ]
354
363
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
360
373
361
374
# same as above - but with set-time event syntax
362
375
cb1‵ = [1.0 ] => affect1 # needs to be a Vector for the event to happen only once
363
376
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 ])
372
397
end
0 commit comments