Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/systems/callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,16 @@ function AffectSystem(affect::Vector{Equation}; discrete_parameters = Any[],

dvs = OrderedSet()
params = OrderedSet()
dp_set = OrderedSet(discrete_parameters)
_varsbuf = Set()
for eq in affect
if !haspre(eq) && !(symbolic_type(eq.rhs) === NotSymbolic() ||
symbolic_type(eq.lhs) === NotSymbolic())
@warn "Affect equation $eq has no `Pre` operator. As such it will be interpreted as an algebraic equation to be satisfied after the callback. If you intended to use the value of a variable x before the affect, use Pre(x). Errors may be thrown if there is no `Pre` and the algebraic equation is unsatisfiable, such as X ~ X + 1."
end
if ModelingToolkit.isparameter(eq.lhs) && (eq.lhs ∉ dp_set)
error("Detected explicit update to parameter $(eq.lhs), but it has not been passed in as a `discrete_parameters` to the callback constructor, for example: \n\n(SymbolicDiscreteCallback(cond => aff, discrete_parameters = [$(eq.lhs)])\n\nAs a result, the parameter update will fail.")
end
collect_vars!(dvs, params, eq, iv; op = Pre)
empty!(_varsbuf)
vars!(_varsbuf, eq; op = Pre)
Expand Down
9 changes: 5 additions & 4 deletions test/symbolic_events.jl
Original file line number Diff line number Diff line change
Expand Up @@ -874,10 +874,9 @@ end

# Test Simulation
@mtkcompile sys = TestSystem()

# Test Simulation
prob = ODEProblem(sys, [], (0.0, 150.0))
sol = solve(prob)

# This is singular at the second event, but the derivatives are zero so it's
# constant after that point anyway. Just make sure it hits the last event and
# had the correct `u`.
Expand Down Expand Up @@ -1374,18 +1373,20 @@ end
@parameters p2(t) = 1.0
@variables x(t) = 0.0
@variables x2(t)
event = [0.5] => [p2 ~ Pre(t)]
event = SymbolicDiscreteCallback([0.5] => [p2 ~ Pre(t)], discrete_parameters = p2)
event_broken = [0.5] => [p2 ~ Pre(t)]

eq = [
D(x) ~ p2,
x2 ~ p_1(x)
]
@test_throws ErrorException @mtkcompile sys = System(eq, t, [x, x2], [p_1, p2], discrete_events = [event_broken])
@mtkcompile sys = System(eq, t, [x, x2], [p_1, p2], discrete_events = [event])

prob = ODEProblem(sys, [], (0.0, 1.0))
sol = solve(prob)
@test SciMLBase.successful_retcode(sol)
@test sol[x, end]≈1.0 atol=1e-6
@test sol[x, end] ≈ 0.75 atol=1e-6
end

@testset "Symbolic affects are compiled in `complete`" begin
Expand Down
Loading