Skip to content

Commit e822248

Browse files
Merge pull request #3992 from TorkelE/add_missing_event_error
Prevents erroneous creation of discrete events (vector of conditions)
2 parents 185ee8c + 7bfdf3b commit e822248

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/systems/callbacks.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,11 @@ function SymbolicDiscreteCallback(
438438
condition::Union{Symbolic{Bool}, Number, Vector{<:Number}}, affect = nothing;
439439
initialize = nothing, finalize = nothing,
440440
reinitializealg = nothing, kwargs...)
441-
c = is_timed_condition(condition) ? condition : value(scalarize(condition))
441+
# Manual error check (to prevent events like `[X < 5.0] => [X ~ Pre(X) + 10.0]` from being created).
442+
(condition isa Vector) && (eltype(condition) <: Num) &&
443+
error("Vectors of symbolic conditions are not allowed for `SymbolicDiscreteCallback`.")
442444

445+
c = is_timed_condition(condition) ? condition : value(scalarize(condition))
443446
if isnothing(reinitializealg)
444447
if any(a -> a isa ImperativeAffect,
445448
[affect, initialize, finalize])

test/symbolic_events.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,3 +1440,18 @@ end
14401440
@mtkcompile sys = MWE()
14411441
@test_nowarn ODEProblem(sys, [], (0.0, 1.0))
14421442
end
1443+
1444+
@testset "Test erroneously created events yields errors" begin
1445+
@parameters p(t) d
1446+
@variables X(t)
1447+
@test_throws "Vectors of symbolic conditions are not allowed" SymbolicDiscreteCallback([X <
1448+
5.0] => [X ~
1449+
Pre(X) +
1450+
10.0])
1451+
@test_throws "Vectors of symbolic conditions are not allowed" SymbolicDiscreteCallback([
1452+
X < 5.0, X > 10.0] => [X ~ Pre(X) + 10.0])
1453+
@test_throws "MethodError: no method" SymbolicContinuousCallback((X <
1454+
5.0) => [X ~
1455+
Pre(X) +
1456+
10.0])
1457+
end

0 commit comments

Comments
 (0)