diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index c94166103b..2393cd96bc 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -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) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 5f37e524e1..77f3193f63 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -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`. @@ -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