@@ -3,7 +3,7 @@ using SciMLStructures: canonicalize, Discrete
33using ModelingToolkit: SymbolicContinuousCallback,
44 SymbolicContinuousCallbacks,
55 SymbolicDiscreteCallback,
6- SymbolicDiscreteCallbacks,
6+ SymbolicDiscreteCallbacks,
77 get_callback,
88 t_nounits as t,
99 D_nounits as D,
340340 D (v) ~ - 9.8 ], t, continuous_events = root_eqs => affect)
341341
342342 @test only (continuous_events (ball)) ==
343- SymbolicContinuousCallback (Equation[x ~ 0 ], Equation[v ~ - Pre (v)])
343+ SymbolicContinuousCallback (Equation[x ~ 0 ], Equation[v ~ - Pre (v)])
344344 ball = structural_simplify (ball)
345345
346346 @test length (ModelingToolkit. continuous_events (ball)) == 1
@@ -373,13 +373,13 @@ end
373373 cb = get_callback (prob)
374374 @test cb isa ModelingToolkit. DiffEqCallbacks. VectorContinuousCallback
375375 @test getfield (ball, :continuous_events )[1 ] ==
376- SymbolicContinuousCallback (Equation[x ~ 0 ], Equation[vx ~ - Pre (vx)])
376+ SymbolicContinuousCallback (Equation[x ~ 0 ], Equation[vx ~ - Pre (vx)])
377377 @test getfield (ball, :continuous_events )[2 ] ==
378- SymbolicContinuousCallback (Equation[y ~ - 1.5 , y ~ 1.5 ], Equation[vy ~ - Pre (vy)])
378+ SymbolicContinuousCallback (Equation[y ~ - 1.5 , y ~ 1.5 ], Equation[vy ~ - Pre (vy)])
379379 cond = cb. condition
380380 out = [0.0 , 0.0 , 0.0 ]
381- p0 = 0.
382- t0 = 0.
381+ p0 = 0.0
382+ t0 = 0.0
383383 cond. f_iip (out, [0 , 0 , 0 , 0 ], p0, t0)
384384 @test out ≈ [0 , 1.5 , - 1.5 ]
385385
@@ -396,10 +396,11 @@ end
396396 # in this test, there are two variables affected by a single event.
397397 events = [[x ~ 0 ] => [vx ~ - Pre (vx), vy ~ - Pre (vy)]]
398398
399- @named ball = ODESystem ([D (x) ~ vx
400- D (y) ~ vy
401- D (vx) ~ - 1
402- D (vy) ~ 0 ], t; continuous_events = events)
399+ @named ball = ODESystem (
400+ [D (x) ~ vx
401+ D (y) ~ vy
402+ D (vx) ~ - 1
403+ D (vy) ~ 0 ], t; continuous_events = events)
403404
404405 ball_nosplit = structural_simplify (ball)
405406 ball = structural_simplify (ball)
@@ -479,12 +480,13 @@ end
479480end
480481
481482@testset " SDE/ODESystem Discrete Callbacks" begin
482- function testsol (sys, probtype, solver, u0, p, tspan; tstops = Float64[], paramtotest = nothing ,
483+ function testsol (
484+ sys, probtype, solver, u0, p, tspan; tstops = Float64[], paramtotest = nothing ,
483485 kwargs... )
484486 prob = probtype (complete (sys), u0, tspan, p; kwargs... )
485487 sol = solve (prob, solver (); tstops = tstops, abstol = 1e-10 , reltol = 1e-10 )
486488 @test isapprox (sol (1.0000000001 )[1 ] - sol (0.999999999 )[1 ], 1.0 ; rtol = 1e-6 )
487- paramtotest === nothing || (@test sol. ps[paramtotest] == [0. , 1. ])
489+ paramtotest === nothing || (@test sol. ps[paramtotest] == [0.0 , 1.0 ])
488490 @test isapprox (sol (4.0 )[1 ], 2 * exp (- 2.0 ); rtol = 1e-6 )
489491 sol
490492 end
503505 ∂ₜ = D
504506 eqs = [∂ₜ (A) ~ - k * A]
505507 @named osys = ODESystem (eqs, t, [A], [k, t1, t2], discrete_events = [cb1, cb2])
506- @named ssys = SDESystem (eqs, [0.0 ], t, [A], [k, t1, t2],
507- discrete_events = [cb1, cb2])
508+ @named ssys = SDESystem (eqs, [0.0 ], t, [A], [k, t1, t2], discrete_events = [cb1, cb2])
508509 u0 = [A => 1.0 ]
509510 p = [k => 0.0 , t1 => 1.0 , t2 => 2.0 ]
510511 tspan = (0.0 , 4.0 )
@@ -518,10 +519,12 @@ end
518519 @named ssys1 = SDESystem (eqs, [0.0 ], t, [A, B], [k, t1, t2],
519520 discrete_events = [cb1a, cb2])
520521 u0′ = [A => 1.0 , B => 0.0 ]
521- sol = testsol (osys1, ODEProblem, Tsit5, u0′, p, tspan; tstops = [1.0 , 2.0 ], check_length = false , paramtotest = k)
522+ sol = testsol (osys1, ODEProblem, Tsit5, u0′, p, tspan;
523+ tstops = [1.0 , 2.0 ], check_length = false , paramtotest = k)
522524 @test sol (1.0000001 , idxs = B) == 2.0
523525
524- sol = testsol (ssys1, SDEProblem, RI5, u0′, p, tspan; tstops = [1.0 , 2.0 ], check_length = false , paramtotest = k)
526+ sol = testsol (ssys1, SDEProblem, RI5, u0′, p, tspan; tstops = [1.0 , 2.0 ],
527+ check_length = false , paramtotest = k)
525528 @test sol (1.0000001 , idxs = B) == 2.0
526529
527530 # same as above - but with set-time event syntax
589592 jprob = JumpProblem (jsys, dprob, Direct (); kwargs... )
590593 sol = solve (jprob, SSAStepper (); tstops = tstops)
591594 @test (sol (1.000000000001 )[1 ] - sol (0.99999999999 )[1 ]) == 1
592- paramtotest === nothing || (@test sol. ps[paramtotest] == [0. , 1.0 ])
595+ paramtotest === nothing || (@test sol. ps[paramtotest] == [0.0 , 1.0 ])
593596 @test sol (40.0 )[1 ] == 0
594597 sol
595598 end
@@ -1282,53 +1285,56 @@ end
12821285 eqs = [D (D (x)) ~ λ * x
12831286 D (D (y)) ~ λ * y - g
12841287 x^ 2 + y^ 2 ~ 1 ]
1285- c_evt = [t ~ 5. ] => [x ~ Pre (x) + 0.1 ]
1288+ c_evt = [t ~ 5.0 ] => [x ~ Pre (x) + 0.1 ]
12861289 @mtkbuild pend = ODESystem (eqs, t, continuous_events = c_evt)
1287- prob = ODEProblem (pend, [x => - 1 , y => 0 ], (0. , 10. ), [g => 1 ], guesses = [λ => 1 ])
1290+ prob = ODEProblem (pend, [x => - 1 , y => 0 ], (0.0 , 10.0 ), [g => 1 ], guesses = [λ => 1 ])
12881291 sol = solve (prob, FBDF ())
12891292 @test ≈ (sol (5.000001 , idxs = x) - sol (4.999999 , idxs = x), 0.1 , rtol = 1e-4 )
12901293 @test ≈ (sol (5.000001 , idxs = x)^ 2 + sol (5.000001 , idxs = y)^ 2 , 1 , rtol = 1e-4 )
12911294
12921295 # Implicit affect with Pre
1293- c_evt = [t ~ 5. ] => [x ~ Pre (x) + y^ 2 ]
1296+ c_evt = [t ~ 5.0 ] => [x ~ Pre (x) + y^ 2 ]
12941297 @mtkbuild pend = ODESystem (eqs, t, continuous_events = c_evt)
1295- prob = ODEProblem (pend, [x => 1 , y => 0 ], (0. , 10. ), [g => 1 ], guesses = [λ => 1 ])
1298+ prob = ODEProblem (pend, [x => 1 , y => 0 ], (0.0 , 10.0 ), [g => 1 ], guesses = [λ => 1 ])
12961299 sol = solve (prob, FBDF ())
1297- @test ≈ (sol (5.000001 , idxs = y)^ 2 + sol (4.999999 , idxs = x), sol (5.000001 , idxs = x), rtol = 1e-4 )
1300+ @test ≈ (sol (5.000001 , idxs = y)^ 2 + sol (4.999999 , idxs = x),
1301+ sol (5.000001 , idxs = x), rtol = 1e-4 )
12981302 @test ≈ (sol (5.000001 , idxs = x)^ 2 + sol (5.000001 , idxs = y)^ 2 , 1 , rtol = 1e-4 )
12991303
13001304 # Impossible affect errors
1301- c_evt = [t ~ 5. ] => [x ~ Pre (x) + 2 ]
1305+ c_evt = [t ~ 5.0 ] => [x ~ Pre (x) + 2 ]
13021306 @mtkbuild pend = ODESystem (eqs, t, continuous_events = c_evt)
1303- prob = ODEProblem (pend, [x => 1 , y => 0 ], (0. , 10. ), [g => 1 ], guesses = [λ => 1 ])
1304- @test_throws UnsolvableCallbackError sol = solve (prob, FBDF ())
1305-
1307+ prob = ODEProblem (pend, [x => 1 , y => 0 ], (0.0 , 10.0 ), [g => 1 ], guesses = [λ => 1 ])
1308+ @test_throws UnsolvableCallbackError sol= solve (prob, FBDF ())
1309+
13061310 # Changing both variables and parameters in the same affect.
13071311 @parameters g (t)
13081312 eqs = [D (D (x)) ~ λ * x
13091313 D (D (y)) ~ λ * y - g
13101314 x^ 2 + y^ 2 ~ 1 ]
1311- c_evt = SymbolicContinuousCallback ([t ~ 5.0 ], [x ~ Pre (x) + 0.1 , g ~ Pre (g) + 1 ], discrete_parameters = [g], iv = t)
1315+ c_evt = SymbolicContinuousCallback (
1316+ [t ~ 5.0 ], [x ~ Pre (x) + 0.1 , g ~ Pre (g) + 1 ], discrete_parameters = [g], iv = t)
13121317 @mtkbuild pend = ODESystem (eqs, t, continuous_events = c_evt)
1313- prob = ODEProblem (pend, [x => 1 , y => 0 ], (0. , 10. ), [g => 1 ], guesses = [λ => 1 ])
1318+ prob = ODEProblem (pend, [x => 1 , y => 0 ], (0.0 , 10.0 ), [g => 1 ], guesses = [λ => 1 ])
13141319 sol = solve (prob, FBDF ())
13151320 @test sol. ps[g] ≈ [1 , 2 ]
1316- @test ≈ (sol (5.0000001 , idxs = x) - sol (4.999999 , idxs = x), .1 , rtol = 1e-4 )
1321+ @test ≈ (sol (5.0000001 , idxs = x) - sol (4.999999 , idxs = x), 0 .1 , rtol = 1e-4 )
13171322
13181323 # Proper re-initialization after parameter change
13191324 eqs = [y ~ g^ 2 - x, D (x) ~ x]
1320- c_evt = SymbolicContinuousCallback ([t ~ 5.0 ], [x ~ Pre (x) + 1 , g ~ Pre (g) + 1 ], discrete_parameters = [g], iv = t)
1325+ c_evt = SymbolicContinuousCallback (
1326+ [t ~ 5.0 ], [x ~ Pre (x) + 1 , g ~ Pre (g) + 1 ], discrete_parameters = [g], iv = t)
13211327 @mtkbuild sys = ODESystem (eqs, t, continuous_events = c_evt)
1322- prob = ODEProblem (sys, [x => 1.0 ], (0. , 10. ), [g => 2 ])
1328+ prob = ODEProblem (sys, [x => 1.0 ], (0.0 , 10.0 ), [g => 2 ])
13231329 sol = solve (prob, FBDF ())
1324- @test sol. ps[g] ≈ [2. , 3. ]
1330+ @test sol. ps[g] ≈ [2.0 , 3.0 ]
13251331 @test ≈ (sol (5.00000001 , idxs = x) - sol (4.9999999 , idxs = x), 1 ; rtol = 1e-4 )
13261332 @test ≈ (sol (5.00000001 , idxs = y), 9 - sol (5.00000001 , idxs = x), rtol = 1e-4 )
13271333
13281334 # Parameters that don't appear in affects should not be mutated.
13291335 c_evt = [t ~ 5.0 ] => [x ~ Pre (x) + 1 ]
13301336 @mtkbuild sys = ODESystem (eqs, t, continuous_events = c_evt)
1331- prob = ODEProblem (sys, [x => 0.5 ], (0. , 10. ), [g => 2 ], guesses = [y => 0 ])
1337+ prob = ODEProblem (sys, [x => 0.5 ], (0.0 , 10.0 ), [g => 2 ], guesses = [y => 0 ])
13321338 sol = solve (prob, FBDF ())
13331339 @test prob. ps[g] == sol. ps[g]
13341340end
0 commit comments