Skip to content

Commit 4a6f2f6

Browse files
committedMar 20, 2025
fix: most tests passing
1 parent e62ad7a commit 4a6f2f6

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed
 

‎src/systems/callbacks.jl

+7-5
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,8 @@ function generate_discrete_callbacks(sys::AbstractSystem, dvs = unknowns(sys), p
685685
[generate_callback(db, sys; kwargs...) for db in dbs]
686686
end
687687

688+
const EMPTY_FUNCTION = (args...) -> ()
689+
688690
"""
689691
Codegen a DifferentialEquations callback. A (set of) continuous callback with multiple equations becomes a VectorContinuousCallback.
690692
Continuous callbacks with only one equation will become a ContinuousCallback.
@@ -705,9 +707,9 @@ function generate_callback(cbs::Vector{SymbolicContinuousCallback}, sys; kwargs.
705707
inits = []
706708
finals = []
707709
for cb in cbs
708-
affect = compile_affect(cb.affect, cb, sys, default = nothing)
710+
affect = compile_affect(cb.affect, cb, sys, default = EMPTY_FUNCTION)
709711
push!(affects, affect)
710-
affect_neg = (cb.affect_neg == cb.affect) ? affect : compile_affect(cb.affect_neg, cb, sys, default = nothing)
712+
affect_neg = (cb.affect_neg === cb.affect) ? affect : compile_affect(cb.affect_neg, cb, sys, default = EMPTY_FUNCTION)
711713
push!(affect_negs, affect_neg)
712714
push!(inits, compile_affect(cb.initialize, cb, sys; default = nothing, is_init = true))
713715
push!(finals, compile_affect(cb.finalize, cb, sys; default = nothing))
@@ -741,11 +743,11 @@ function generate_callback(cb, sys; kwargs...)
741743
ps = parameters(sys; initial_parameters = true)
742744

743745
trigger = is_timed ? conditions(cb) : compile_condition(cb, sys, dvs, ps; kwargs...)
744-
affect = compile_affect(cb.affect, cb, sys, default = (args...) -> ())
746+
affect = compile_affect(cb.affect, cb, sys, default = EMPTY_FUNCTION)
745747
affect_neg = if is_discrete(cb)
746748
nothing
747749
else
748-
(cb.affect == cb.affect_neg) ? affect : compile_affect(cb.affect_neg, cb, sys, default = nothing)
750+
(cb.affect === cb.affect_neg) ? affect : compile_affect(cb.affect_neg, cb, sys, default = EMPTY_FUNCTION)
749751
end
750752
init = compile_affect(cb.initialize, cb, sys, default = SciMLBase.INITIALIZE_DEFAULT, is_init = true)
751753
final = compile_affect(cb.finalize, cb, sys, default = SciMLBase.FINALIZE_DEFAULT)
@@ -860,7 +862,7 @@ function compile_equational_affect(aff::AffectSystem, sys; kwargs...)
860862
push!(pmap, pre_p => pval)
861863
end
862864
guesses = Pair[u => integrator[aff_map[u]] for u in unknowns(affsys)]
863-
affprob = ImplicitDiscreteProblem(affsys, Pair[], (0, 1), pmap; guesses, build_initializeprob = false)
865+
affprob = ImplicitDiscreteProblem(affsys, Pair[], (integrator.t, integrator.t), pmap; guesses, build_initializeprob = false)
864866

865867
affsol = init(affprob, SimpleIDSolve())
866868
for u in dvs_to_modify

‎test/symbolic_events.jl

+4-5
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,6 @@ end
644644
dprob = DiscreteProblem(jsys, u0, tspan, p)
645645
jprob = JumpProblem(jsys, dprob, Direct(); kwargs...)
646646
sol = solve(jprob, SSAStepper(); tstops = tstops)
647-
@show sol
648647
@test (sol(1.000000000001)[1] - sol(0.99999999999)[1]) == 1
649648
paramtotest === nothing || (@test sol.ps[paramtotest] == 1.0)
650649
@test sol(40.0)[1] == 0
@@ -1153,7 +1152,7 @@ end
11531152
f = ModelingToolkit.FunctionalAffect(
11541153
f = (i, u, p, c) -> seen = true, sts = [], pars = [], discretes = [])
11551154
cb1 = ModelingToolkit.SymbolicContinuousCallback(
1156-
[x ~ 0], Equation[], initialize = [x ~ 1.5], finalize = f)
1155+
[x ~ 0], nothing, initialize = [x ~ 1.5], finalize = f)
11571156
@mtkbuild sys = ODESystem(D(x) ~ -1, t, [x], []; continuous_events = [cb1])
11581157
prob = ODEProblem(sys, [x => 1.0], (0.0, 2), [])
11591158
sol = solve(prob, Tsit5(); dtmax = 0.01)
@@ -1166,15 +1165,15 @@ end
11661165
f = ModelingToolkit.FunctionalAffect(
11671166
f = (i, u, p, c) -> seen = true, sts = [], pars = [], discretes = [])
11681167
cb1 = ModelingToolkit.SymbolicContinuousCallback(
1169-
[x ~ 0], Equation[], initialize = [x ~ 1.5], finalize = f)
1168+
[x ~ 0], nothing, initialize = [x ~ 1.5], finalize = f)
11701169
inited = false
11711170
finaled = false
11721171
a = ModelingToolkit.FunctionalAffect(
11731172
f = (i, u, p, c) -> inited = true, sts = [], pars = [], discretes = [])
11741173
b = ModelingToolkit.FunctionalAffect(
11751174
f = (i, u, p, c) -> finaled = true, sts = [], pars = [], discretes = [])
11761175
cb2 = ModelingToolkit.SymbolicContinuousCallback(
1177-
[x ~ 0.1], Equation[], initialize = a, finalize = b)
1176+
[x ~ 0.1], nothing, initialize = a, finalize = b)
11781177
@mtkbuild sys = ODESystem(D(x) ~ -1, t, [x], []; continuous_events = [cb1, cb2])
11791178
prob = ODEProblem(sys, [x => 1.0], (0.0, 2), [])
11801179
sol = solve(prob, Tsit5())
@@ -1238,7 +1237,7 @@ end
12381237
@variables x(t) [irreducible = true] y(t) [irreducible = true]
12391238
eqs = [x ~ y, D(x) ~ -1]
12401239
cb = [x ~ 0.0] => [x ~ 0, y ~ 1]
1241-
@test_throws ErrorException @mtkbuild pend = ODESystem(eqs, t; continuous_events = [cb])
1240+
@test_throws Exception @mtkbuild pend = ODESystem(eqs, t; continuous_events = [cb])
12421241

12431242
cb = [x ~ 0.0] => [y ~ 1]
12441243
@mtkbuild pend = ODESystem(eqs, t; continuous_events = [cb])

0 commit comments

Comments
 (0)