Skip to content

Commit 54a19be

Browse files
Merge pull request #3190 from AayushSabharwal/as/discrete-cb-array
fix: fix discrete callback condition with array variable
2 parents c30dc61 + 094d736 commit 54a19be

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

src/systems/callbacks.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ end
550550
"""
551551
compile_condition(cb::SymbolicDiscreteCallback, sys, dvs, ps; expression, kwargs...)
552552
553-
Returns a function `condition(u,p,t)` returning the `condition(cb)`.
553+
Returns a function `condition(u,t,integrator)` returning the `condition(cb)`.
554554
555555
Notes
556556
@@ -571,7 +571,8 @@ function compile_condition(cb::SymbolicDiscreteCallback, sys, dvs, ps;
571571
end
572572
expr = build_function(
573573
condit, u, t, p...; expression = Val{true},
574-
wrap_code = condition_header(sys) .∘ wrap_array_vars(sys, condit; dvs, ps) .∘
574+
wrap_code = condition_header(sys) .∘
575+
wrap_array_vars(sys, condit; dvs, ps, inputs = true) .∘
575576
wrap_parameter_dependencies(sys, !(condit isa AbstractArray)),
576577
kwargs...)
577578
if expression == Val{true}

test/dde.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pmul = [1.0,
7474

7575
prob = SDDEProblem(hayes_modelf, hayes_modelg, [1.0], h, tspan, pmul;
7676
constant_lags = (pmul[1],));
77-
sol = solve(prob, RKMil())
77+
sol = solve(prob, RKMil(), seed = 100)
7878

7979
@variables x(..)
8080
@parameters a=-4.0 b=-2.0 c=10.0 α=-1.3 β=-1.2 γ=1.1
@@ -87,7 +87,7 @@ eqs = [D(x(t)) ~ a * x(t) + b * x(t - τ) + c + (α * x(t) + γ) * η]
8787
@test equations(sys) == [D(x(t)) ~ a * x(t) + b * x(t - τ) + c]
8888
@test isequal(ModelingToolkit.get_noiseeqs(sys), [α * x(t) + γ])
8989
prob_mtk = SDDEProblem(sys, [x(t) => 1.0 + t], tspan; constant_lags = (τ,));
90-
@test_nowarn sol_mtk = solve(prob_mtk, RKMil())
90+
@test_nowarn sol_mtk = solve(prob_mtk, RKMil(), seed = 100)
9191

9292
prob_sa = SDDEProblem(
9393
sys, [x(t) => 1.0 + t], tspan; constant_lags = (τ,), u0_constructor = SVector{1})

test/symbolic_events.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,3 +1075,24 @@ end
10751075
prob = ODEProblem(pend, [x => 1], (0.0, 3.0), guesses = [y => x])
10761076
@test all((0.0; atol = 1e-9), solve(prob, Rodas5())[[x, y]][end])
10771077
end
1078+
1079+
@testset "Issue#3154 Array variable in discrete condition" begin
1080+
@mtkmodel DECAY begin
1081+
@parameters begin
1082+
unrelated[1:2] = zeros(2)
1083+
k = 0.0
1084+
end
1085+
@variables begin
1086+
x(t) = 10.0
1087+
end
1088+
@equations begin
1089+
D(x) ~ -k * x
1090+
end
1091+
@discrete_events begin
1092+
(t == 1.0) => [k ~ 1.0]
1093+
end
1094+
end
1095+
@mtkbuild decay = DECAY()
1096+
prob = ODEProblem(decay, [], (0.0, 10.0), [])
1097+
@test_nowarn solve(prob, Tsit5(), tstops = [1.0])
1098+
end

0 commit comments

Comments
 (0)