Skip to content

Commit

Permalink
Fix finalization
Browse files Browse the repository at this point in the history
  • Loading branch information
BenChung committed Oct 29, 2024
1 parent c592db1 commit 219c405
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/systems/callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ function initialize_affects(cbs::Vector{SymbolicContinuousCallback})
mapreduce(initialize_affects, vcat, cbs, init = Equation[])
end

finalize_affects(cb::SymbolicContinuousCallback) = cb.initialize
finalize_affects(cb::SymbolicContinuousCallback) = cb.finalize
function finalize_affects(cbs::Vector{SymbolicContinuousCallback})
mapreduce(finalize_affects, vcat, cbs, init = Equation[])
end
Expand Down
14 changes: 14 additions & 0 deletions test/symbolic_events.jl
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,20 @@ end
@test sol[c] == [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
end


@testset "Initialization" begin
@variables x(t)
seen = false
f = ModelingToolkit.FunctionalAffect(f=(i,u,p,c)->seen=true, sts=[], pars=[], discretes=[])
cb1 = ModelingToolkit.SymbolicContinuousCallback([x ~ 0], Equation[], initialize=[x~1.5], finalize=f)
@mtkbuild sys = ODESystem(D(x) ~ -1, t, [x], []; continuous_events = [cb1])
prob = ODEProblem(sys, [x => 1.0], (0.0, 2), [])
sol = solve(prob, Tsit5(); dtmax=0.01)
@test sol[x][1] 1.0
@test sol[x][2] 1.5 # the initialize affect has been applied
@test seen == true
end

@testset "Bump" begin
@variables x(t) [irreducible = true] y(t) [irreducible = true]
eqs = [x ~ y, D(x) ~ -1]
Expand Down

0 comments on commit 219c405

Please sign in to comment.