@@ -3,7 +3,7 @@ using SciMLStructures: canonicalize, Discrete
3
3
using ModelingToolkit: SymbolicContinuousCallback,
4
4
SymbolicContinuousCallbacks,
5
5
SymbolicDiscreteCallback,
6
- SymbolicDiscreteCallbacks,
6
+ SymbolicDiscreteCallbacks,
7
7
get_callback,
8
8
t_nounits as t,
9
9
D_nounits as D,
340
340
D (v) ~ - 9.8 ], t, continuous_events = root_eqs => affect)
341
341
342
342
@test only (continuous_events (ball)) ==
343
- SymbolicContinuousCallback (Equation[x ~ 0 ], Equation[v ~ - Pre (v)])
343
+ SymbolicContinuousCallback (Equation[x ~ 0 ], Equation[v ~ - Pre (v)])
344
344
ball = structural_simplify (ball)
345
345
346
346
@test length (ModelingToolkit. continuous_events (ball)) == 1
@@ -373,13 +373,13 @@ end
373
373
cb = get_callback (prob)
374
374
@test cb isa ModelingToolkit. DiffEqCallbacks. VectorContinuousCallback
375
375
@test getfield (ball, :continuous_events )[1 ] ==
376
- SymbolicContinuousCallback (Equation[x ~ 0 ], Equation[vx ~ - Pre (vx)])
376
+ SymbolicContinuousCallback (Equation[x ~ 0 ], Equation[vx ~ - Pre (vx)])
377
377
@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)])
379
379
cond = cb. condition
380
380
out = [0.0 , 0.0 , 0.0 ]
381
- p0 = 0.
382
- t0 = 0.
381
+ p0 = 0.0
382
+ t0 = 0.0
383
383
cond. f_iip (out, [0 , 0 , 0 , 0 ], p0, t0)
384
384
@test out ≈ [0 , 1.5 , - 1.5 ]
385
385
@@ -396,10 +396,11 @@ end
396
396
# in this test, there are two variables affected by a single event.
397
397
events = [[x ~ 0 ] => [vx ~ - Pre (vx), vy ~ - Pre (vy)]]
398
398
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)
403
404
404
405
ball_nosplit = structural_simplify (ball)
405
406
ball = structural_simplify (ball)
@@ -479,12 +480,13 @@ end
479
480
end
480
481
481
482
@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 ,
483
485
kwargs... )
484
486
prob = probtype (complete (sys), u0, tspan, p; kwargs... )
485
487
sol = solve (prob, solver (); tstops = tstops, abstol = 1e-10 , reltol = 1e-10 )
486
488
@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 ])
488
490
@test isapprox (sol (4.0 )[1 ], 2 * exp (- 2.0 ); rtol = 1e-6 )
489
491
sol
490
492
end
503
505
∂ₜ = D
504
506
eqs = [∂ₜ (A) ~ - k * A]
505
507
@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])
508
509
u0 = [A => 1.0 ]
509
510
p = [k => 0.0 , t1 => 1.0 , t2 => 2.0 ]
510
511
tspan = (0.0 , 4.0 )
@@ -518,10 +519,12 @@ end
518
519
@named ssys1 = SDESystem (eqs, [0.0 ], t, [A, B], [k, t1, t2],
519
520
discrete_events = [cb1a, cb2])
520
521
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)
522
524
@test sol (1.0000001 , idxs = B) == 2.0
523
525
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)
525
528
@test sol (1.0000001 , idxs = B) == 2.0
526
529
527
530
# same as above - but with set-time event syntax
589
592
jprob = JumpProblem (jsys, dprob, Direct (); kwargs... )
590
593
sol = solve (jprob, SSAStepper (); tstops = tstops)
591
594
@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 ])
593
596
@test sol (40.0 )[1 ] == 0
594
597
sol
595
598
end
@@ -1282,53 +1285,56 @@ end
1282
1285
eqs = [D (D (x)) ~ λ * x
1283
1286
D (D (y)) ~ λ * y - g
1284
1287
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 ]
1286
1289
@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 ])
1288
1291
sol = solve (prob, FBDF ())
1289
1292
@test ≈ (sol (5.000001 , idxs = x) - sol (4.999999 , idxs = x), 0.1 , rtol = 1e-4 )
1290
1293
@test ≈ (sol (5.000001 , idxs = x)^ 2 + sol (5.000001 , idxs = y)^ 2 , 1 , rtol = 1e-4 )
1291
1294
1292
1295
# 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 ]
1294
1297
@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 ])
1296
1299
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 )
1298
1302
@test ≈ (sol (5.000001 , idxs = x)^ 2 + sol (5.000001 , idxs = y)^ 2 , 1 , rtol = 1e-4 )
1299
1303
1300
1304
# Impossible affect errors
1301
- c_evt = [t ~ 5. ] => [x ~ Pre (x) + 2 ]
1305
+ c_evt = [t ~ 5.0 ] => [x ~ Pre (x) + 2 ]
1302
1306
@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
+
1306
1310
# Changing both variables and parameters in the same affect.
1307
1311
@parameters g (t)
1308
1312
eqs = [D (D (x)) ~ λ * x
1309
1313
D (D (y)) ~ λ * y - g
1310
1314
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)
1312
1317
@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 ])
1314
1319
sol = solve (prob, FBDF ())
1315
1320
@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 )
1317
1322
1318
1323
# Proper re-initialization after parameter change
1319
1324
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)
1321
1327
@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 ])
1323
1329
sol = solve (prob, FBDF ())
1324
- @test sol. ps[g] ≈ [2. , 3. ]
1330
+ @test sol. ps[g] ≈ [2.0 , 3.0 ]
1325
1331
@test ≈ (sol (5.00000001 , idxs = x) - sol (4.9999999 , idxs = x), 1 ; rtol = 1e-4 )
1326
1332
@test ≈ (sol (5.00000001 , idxs = y), 9 - sol (5.00000001 , idxs = x), rtol = 1e-4 )
1327
1333
1328
1334
# Parameters that don't appear in affects should not be mutated.
1329
1335
c_evt = [t ~ 5.0 ] => [x ~ Pre (x) + 1 ]
1330
1336
@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 ])
1332
1338
sol = solve (prob, FBDF ())
1333
1339
@test prob. ps[g] == sol. ps[g]
1334
1340
end
0 commit comments