Skip to content

Commit dcddee3

Browse files
test: update tests to use ImperativeAffect
1 parent e1c17a9 commit dcddee3

File tree

5 files changed

+49
-51
lines changed

5 files changed

+49
-51
lines changed

test/index_cache.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,17 @@ mutable struct ParamTest
9898
end
9999
(pt::ParamTest)(x) = pt.y - x
100100
@testset "Issue#3215: Callable discrete parameter" begin
101-
function update_affect!(integ, u, p, ctx)
102-
integ.p[p.p_1].y = integ.t
101+
function update_affect!(mod, obs, ctx, integ)
102+
p_1 = mod.p_1
103+
p_1.y = integ.t
104+
return (; p_1)
103105
end
104106

105107
tp1 = typeof(ParamTest(1))
106108
@parameters (p_1::tp1)(..) = ParamTest(1)
107109
@variables x(ModelingToolkit.t_nounits) = 0
108110

109-
event1 = [1.0, 2, 3] => (update_affect!, [], [p_1], [p_1], nothing)
111+
event1 = [1.0, 2, 3] => (f = update_affect!, modified = (; p_1))
110112

111113
@named sys = System([
112114
ModelingToolkit.D_nounits(x) ~ p_1(x)

test/initializationsystem.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,13 +1367,13 @@ end
13671367

13681368
@testset "Issue#3342" begin
13691369
@variables x(t) y(t)
1370-
stop!(integrator, _, _, _) = terminate!(integrator)
1370+
stop!(mod, obs, ctx, integrator) = (terminate!(integrator); return (;))
13711371
@named sys = System([D(x) ~ 1.0
13721372
D(y) ~ 1.0], t; initialization_eqs = [
13731373
y ~ 0.0
13741374
],
13751375
continuous_events = [
1376-
[y ~ 0.5] => (stop!, [y], [], [], nothing)
1376+
[y ~ 0.5] => (; f = stop!)
13771377
])
13781378
sys = structural_simplify(sys)
13791379
prob0 = ODEProblem(sys, [x => NaN], (0.0, 1.0), [])

test/jumpsystem.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,12 +516,12 @@ end
516516
@test all(abs.(Xv .- Xact) .<= 0.05 .* Xv)
517517
@test all(abs.(Yv .- Yact) .<= 0.05 .* Yv)
518518

519-
function affect!(integ, u, p, ctx)
519+
function affect!(mod, obs, ctx, integ)
520520
savevalues!(integ, true)
521521
terminate!(integ)
522-
nothing
522+
(;)
523523
end
524-
cevents = [t ~ 0.2] => (affect!, [], [], [], nothing)
524+
cevents = [t ~ 0.2] => (; f = affect!)
525525
@named jsys = JumpSystem([maj, crj, vrj, eqs[1]], t, [X, Y], [α, β];
526526
continuous_events = cevents)
527527
jsys = complete(jsys)

test/parameter_dependencies.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ using NonlinearSolve
1414
@parameters p1(t)=1.0 p2
1515
@variables x(t)
1616
cb1 = SymbolicContinuousCallback([x ~ 2.0] => [p1 ~ 2.0], discrete_parameters = [p1]) # triggers at t=-2+√6
17-
function affect1!(integ, u, p, ctx)
18-
integ.ps[p[1]] = integ.ps[p[2]]
17+
function affect1!(mod, obs, ctx, integ)
18+
return (; p1 = obs.p2)
1919
end
20-
cb2 = [x ~ 4.0] => (affect1!, [], [p1, p2], [p1]) # triggers at t=-2+√7
20+
cb2 = [x ~ 4.0] => (f = affect1!, observed = (; p2), modified = (; p1)) # triggers at t=-2+√7
2121
cb3 = SymbolicDiscreteCallback([1.0] => [p1 ~ 5.0], discrete_parameters = [p1])
2222

2323
@mtkbuild sys = System(

test/symbolic_events.jl

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -514,11 +514,10 @@ end
514514
testsol(ssys3, SDEProblem, RI5, u0, p, tspan; tstops = [1.0], paramtotest = k)
515515

516516
# mixing with a func affect
517-
function affect!(integrator, u, p, ctx)
518-
integrator.ps[p.k] = 1.0
519-
nothing
517+
function affect!(mod, obs, ctx, integ)
518+
return (; k = 1.0)
520519
end
521-
cb2‵‵ = [2.0] => (affect!, [], [k], [k], nothing)
520+
cb2‵‵ = [2.0] => (f = affect!, modified = (; k))
522521
@named osys4 = System(eqs, t, [A], [k, t1], discrete_events = [cb1, cb2‵‵])
523522
@named ssys4 = SDESystem(eqs, [0.0], t, [A], [k, t1],
524523
discrete_events = [cb1, cb2‵‵])
@@ -527,7 +526,7 @@ end
527526
testsol(ssys4, SDEProblem, RI5, u0, p, tspan; tstops = [1.0], paramtotest = k)
528527

529528
# mixing with symbolic condition in the func affect
530-
cb2‵‵‵ = (t == t2) => (affect!, [], [k], [k], nothing)
529+
cb2‵‵‵ = (t == t2) => (f = affect!, modified = (; k))
531530
@named osys5 = System(eqs, t, [A], [k, t1, t2], discrete_events = [cb1, cb2‵‵‵])
532531
@named ssys5 = SDESystem(eqs, [0.0], t, [A], [k, t1, t2],
533532
discrete_events = [cb1, cb2‵‵‵])
@@ -605,17 +604,15 @@ end
605604
testsol(jsys3, u0, p, tspan; tstops = [1.0], rng, paramtotest = k)
606605

607606
# mixing with a func affect
608-
function affect!(integrator, u, p, ctx)
609-
integrator.ps[p.k] = 1.0
610-
reset_aggregated_jumps!(integrator)
611-
nothing
607+
function affect!(mod, obs, ctx, integrator)
608+
return (; k = 1.0)
612609
end
613-
cb2‵‵ = [2.0] => (affect!, [], [k], [k], nothing)
610+
cb2‵‵ = [2.0] => (f = affect!, modified = (; k))
614611
@named jsys4 = JumpSystem(eqs, t, [A], [k, t1], discrete_events = [cb1, cb2‵‵])
615612
testsol(jsys4, u0, p, tspan; tstops = [1.0], rng, paramtotest = k)
616613

617614
# mixing with symbolic condition in the func affect
618-
cb2‵‵‵ = (t == t2) => (affect!, [], [k], [k], nothing)
615+
cb2‵‵‵ = (t == t2) => (f = affect!, modified = (; k))
619616
@named jsys5 = JumpSystem(eqs, t, [A], [k, t1, t2], discrete_events = [cb1, cb2‵‵‵])
620617
testsol(jsys5, u0, p, tspan; tstops = [1.0, 2.0], rng, paramtotest = k)
621618
@named jsys6 = JumpSystem(eqs, t, [A], [k, t1, t2], discrete_events = [cb2‵‵‵, cb1])
@@ -649,13 +646,16 @@ end
649646
# baseline affect (pos + neg + left root find)
650647
@variables c1(t)=1.0 c2(t)=1.0 # c1 = cos(t), c2 = cos(3t)
651648
eqs = [D(c1) ~ -sin(t); D(c2) ~ -3 * sin(3 * t)]
652-
record_crossings(i, u, _, c) = push!(c, i.t => i.u[u.v])
649+
function record_crossings(mod, obs, ctx, integ)
650+
push!(ctx, integ.t => obs.v)
651+
return (;)
652+
end
653653
cr1 = []
654654
cr2 = []
655655
evt1 = ModelingToolkit.SymbolicContinuousCallback(
656-
[c1 ~ 0], (record_crossings, [c1 => :v], [], [], cr1))
656+
[c1 ~ 0], (f = record_crossings, observed = (; v = c1), ctx = cr1))
657657
evt2 = ModelingToolkit.SymbolicContinuousCallback(
658-
[c2 ~ 0], (record_crossings, [c2 => :v], [], [], cr2))
658+
[c2 ~ 0], (f = record_crossings, observed = (; v = c2), ctx = cr2))
659659
@named trigsys = System(eqs, t; continuous_events = [evt1, evt2])
660660
trigsys_ss = structural_simplify(trigsys)
661661
prob = ODEProblem(trigsys_ss, [], (0.0, 2π))
@@ -673,11 +673,11 @@ end
673673
cr1n = []
674674
cr2n = []
675675
evt1 = ModelingToolkit.SymbolicContinuousCallback(
676-
[c1 ~ 0], (record_crossings, [c1 => :v], [], [], cr1p);
677-
affect_neg = (record_crossings, [c1 => :v], [], [], cr1n))
676+
[c1 ~ 0], (f = record_crossings, observed = (; v = c1), ctx = cr1p);
677+
affect_neg = (f = record_crossings, observed = (; v = c1), ctx = cr1n))
678678
evt2 = ModelingToolkit.SymbolicContinuousCallback(
679-
[c2 ~ 0], (record_crossings, [c2 => :v], [], [], cr2p);
680-
affect_neg = (record_crossings, [c2 => :v], [], [], cr2n))
679+
[c2 ~ 0], (f = record_crossings, observed = (; v = c2), ctx = cr2p);
680+
affect_neg = (f = record_crossings, observed = (; v = c2), ctx = cr2n))
681681
@named trigsys = System(eqs, t; continuous_events = [evt1, evt2])
682682
trigsys_ss = structural_simplify(trigsys)
683683
prob = ODEProblem(trigsys_ss, [], (0.0, 2π))
@@ -699,9 +699,9 @@ end
699699
cr1p = []
700700
cr2p = []
701701
evt1 = ModelingToolkit.SymbolicContinuousCallback(
702-
[c1 ~ 0], (record_crossings, [c1 => :v], [], [], cr1p); affect_neg = nothing)
702+
[c1 ~ 0], (f = record_crossings, observed = (; v = c1), ctx = cr1p); affect_neg = nothing)
703703
evt2 = ModelingToolkit.SymbolicContinuousCallback(
704-
[c2 ~ 0], (record_crossings, [c2 => :v], [], [], cr2p); affect_neg = nothing)
704+
[c2 ~ 0], (f = record_crossings, observed = (; v = c2), ctx = cr2p); affect_neg = nothing)
705705
@named trigsys = System(eqs, t; continuous_events = [evt1, evt2])
706706
trigsys_ss = structural_simplify(trigsys)
707707
prob = ODEProblem(trigsys_ss, [], (0.0, 2π))
@@ -717,10 +717,10 @@ end
717717
cr1n = []
718718
cr2n = []
719719
evt1 = ModelingToolkit.SymbolicContinuousCallback(
720-
[c1 ~ 0], (record_crossings, [c1 => :v], [], [], cr1p); affect_neg = nothing)
720+
[c1 ~ 0], (f = record_crossings, observed = (; v = c1), ctx = cr1p); affect_neg = nothing)
721721
evt2 = ModelingToolkit.SymbolicContinuousCallback(
722-
[c2 ~ 0], (record_crossings, [c2 => :v], [], [], cr2p);
723-
affect_neg = (record_crossings, [c2 => :v], [], [], cr2n))
722+
[c2 ~ 0], (f = record_crossings, observed = (; v = c2), ctx = cr2p);
723+
affect_neg = (f = record_crossings, observed = (; v = c2), ctx = cr2n))
724724
@named trigsys = System(eqs, t; continuous_events = [evt1, evt2])
725725
trigsys_ss = structural_simplify(trigsys)
726726
prob = ODEProblem(trigsys_ss, [], (0.0, 2π))
@@ -740,10 +740,10 @@ end
740740
cr1 = []
741741
cr2 = []
742742
evt1 = ModelingToolkit.SymbolicContinuousCallback(
743-
[c1 ~ 0], (record_crossings, [c1 => :v], [], [], cr1);
743+
[c1 ~ 0], (f = record_crossings, observed = (; v = c1), ctx = cr1);
744744
rootfind = SciMLBase.RightRootFind)
745745
evt2 = ModelingToolkit.SymbolicContinuousCallback(
746-
[c2 ~ 0], (record_crossings, [c2 => :v], [], [], cr2);
746+
[c2 ~ 0], (f = record_crossings, observed = (; v = c2), ctx = cr2);
747747
rootfind = SciMLBase.RightRootFind)
748748
@named trigsys = System(eqs, t; continuous_events = [evt1, evt2])
749749
trigsys_ss = structural_simplify(trigsys)
@@ -760,10 +760,10 @@ end
760760
cr1 = []
761761
cr2 = []
762762
evt1 = ModelingToolkit.SymbolicContinuousCallback(
763-
[c1 ~ 0], (record_crossings, [c1 => :v], [], [], cr1);
763+
[c1 ~ 0], (f = record_crossings, observed = (; v = c1), ctx = cr1);
764764
rootfind = SciMLBase.LeftRootFind)
765765
evt2 = ModelingToolkit.SymbolicContinuousCallback(
766-
[c2 ~ 0], (record_crossings, [c2 => :v], [], [], cr2);
766+
[c2 ~ 0], (f = record_crossings, observed = (; v = c2), ctx = cr2);
767767
rootfind = SciMLBase.RightRootFind)
768768
@named trigsys = System(eqs, t; continuous_events = [evt1, evt2])
769769
trigsys_ss = structural_simplify(trigsys)
@@ -778,10 +778,10 @@ end
778778
cr1 = []
779779
cr2 = []
780780
evt1 = ModelingToolkit.SymbolicContinuousCallback(
781-
[c1 ~ 0], (record_crossings, [c1 => :v], [], [], cr1);
781+
[c1 ~ 0], (f = record_crossings, observed = (; v = c1), ctx = cr1);
782782
rootfind = SciMLBase.LeftRootFind)
783783
evt2 = ModelingToolkit.SymbolicContinuousCallback(
784-
[c2 ~ 0], (record_crossings, [c2 => :v], [], [], cr2);
784+
[c2 ~ 0], (f = record_crossings, observed = (; v = c2), ctx = cr2);
785785
rootfind = SciMLBase.RightRootFind)
786786
@named trigsys = System(eqs, t; continuous_events = [evt2, evt1])
787787
trigsys_ss = structural_simplify(trigsys)
@@ -879,10 +879,10 @@ end
879879
@variables x(t)
880880
@parameters a(t) b(t) c(t)
881881
cb1 = SymbolicContinuousCallback([x ~ 1.0] => [a ~ -Pre(a)], discrete_parameters = [a])
882-
function save_affect!(integ, u, p, ctx)
883-
integ.ps[p.b] = 5.0
882+
function save_affect!(mod, obs, ctx, integ)
883+
return (; b = 5.0)
884884
end
885-
cb2 = [x ~ 0.5] => (save_affect!, [], [b], [b], nothing)
885+
cb2 = [x ~ 0.5] => (f = save_affect!, modified = (; b))
886886
cb3 = SymbolicDiscreteCallback(1.0 => [c ~ t], discrete_parameters = [c])
887887

888888
@mtkbuild sys = System(D(x) ~ cos(t), t, [x], [a, b, c];
@@ -1067,8 +1067,7 @@ end
10671067
@testset "Initialization" begin
10681068
@variables x(t)
10691069
seen = false
1070-
f = ModelingToolkit.FunctionalAffect(
1071-
f = (i, u, p, c) -> seen = true, sts = [], pars = [], discretes = [])
1070+
f = ModelingToolkit.ImperativeAffect(f = (m, o, ctx, int) -> (seen = true; return (;)))
10721071
cb1 = ModelingToolkit.SymbolicContinuousCallback(
10731072
[x ~ 0], nothing, initialize = [x ~ 1.5], finalize = f)
10741073
@mtkbuild sys = System(D(x) ~ -1, t, [x], []; continuous_events = [cb1])
@@ -1080,16 +1079,13 @@ end
10801079

10811080
@variables x(t)
10821081
seen = false
1083-
f = ModelingToolkit.FunctionalAffect(
1084-
f = (i, u, p, c) -> seen = true, sts = [], pars = [], discretes = [])
1082+
f = ModelingToolkit.ImperativeAffect(f = (m, o, ctx, int) -> (seen = true; return (;)))
10851083
cb1 = ModelingToolkit.SymbolicContinuousCallback(
10861084
[x ~ 0], nothing, initialize = [x ~ 1.5], finalize = f)
10871085
inited = false
10881086
finaled = false
1089-
a = ModelingToolkit.FunctionalAffect(
1090-
f = (i, u, p, c) -> inited = true, sts = [], pars = [], discretes = [])
1091-
b = ModelingToolkit.FunctionalAffect(
1092-
f = (i, u, p, c) -> finaled = true, sts = [], pars = [], discretes = [])
1087+
a = ModelingToolkit.ImperativeAffect(f = (m, o, ctx, int) -> (inited = true; return (;)))
1088+
b = ModelingToolkit.ImperativeAffect(f = (m, o, ctx, int) -> (finaled = true; return (;)))
10931089
cb2 = ModelingToolkit.SymbolicContinuousCallback(
10941090
[x ~ 0.1], nothing, initialize = a, finalize = b)
10951091
@mtkbuild sys = System(D(x) ~ -1, t, [x], []; continuous_events = [cb1, cb2])

0 commit comments

Comments
 (0)