Skip to content

Commit a1287da

Browse files
committed
Avoid a writeback if the user affect returns nothing
1 parent 51acbfb commit a1287da

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/systems/imperative_affect.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,11 @@ function compile_user_affect(affect::ImperativeAffect, cb, sys, dvs, ps; kwargs.
216216
upd_vals = user_affect(upd_component_array, obs_component_array, ctx, integ)
217217

218218
# write the new values back to the integrator
219-
_generated_writeback(integ, upd_funs, upd_vals)
220-
221-
for idx in save_idxs
222-
SciMLBase.save_discretes!(integ, idx)
219+
if !isnothing(upd_vals)
220+
_generated_writeback(integ, upd_funs, upd_vals)
221+
for idx in save_idxs
222+
SciMLBase.save_discretes!(integ, idx)
223+
end
223224
end
224225
end
225226
end

test/symbolic_events.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,3 +1484,27 @@ end
14841484
prob = ODEProblem(sys, [], (0.0, 1.0))
14851485
sol = solve(prob, Tsit5())
14861486
end
1487+
1488+
@testset "ImperativeAffect skips writing back when nothing is returned" begin
1489+
@mtkmodel ImperativeAffectTupleMWE begin
1490+
@parameters begin
1491+
y(t) = 1.0
1492+
end
1493+
@variables begin
1494+
x(t) = 0.0
1495+
end
1496+
@equations begin
1497+
D(x) ~ y
1498+
end
1499+
@continuous_events begin
1500+
(x ~ 0.5) => ModelingToolkit.ImperativeAffect(
1501+
observed = (; mypars = (x, 2 * x)), modified = (; y)) do m, o, c, i
1502+
return nothing
1503+
end
1504+
end
1505+
end
1506+
@mtkbuild sys = ImperativeAffectTupleMWE()
1507+
prob = ODEProblem(sys, [], (0.0, 1.0))
1508+
sol = solve(prob, Tsit5())
1509+
@test length(sol[sys.y]) == 1
1510+
end

0 commit comments

Comments
 (0)