Skip to content

Commit 5a19c6a

Browse files
fix: fix observed for DiscreteSystem, add test
1 parent 88bcd3d commit 5a19c6a

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/systems/discrete_system/discrete_system.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ function SciMLBase.DiscreteFunction{iip, specialize}(
303303
obs = get!(dict, value(obsvar)) do
304304
build_explicit_observed_function(sys, obsvar)
305305
end
306-
obs(u, p, t)
306+
p isa MTKParameters ? obs(u, p..., t) : obs(u, p, t)
307307
end
308308
end
309309

test/discrete_system.jl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,26 +64,28 @@ sol_map = solve(prob_map, FunctionMap());
6464

6565
# Using defaults constructor
6666
@parameters c=10.0 nsteps=400 δt=0.1 β=0.05 γ=0.25
67-
@variables S(t)=990.0 I(t)=10.0 R(t)=0.0
67+
@variables S(t)=990.0 I(t)=10.0 R(t)=0.0 R2(t)
6868

6969
infection2 = rate_to_proportion* c * I(k - 1) / (S(k - 1) + I(k - 1) + R(k - 1)), δt) *
7070
S(k - 1)
7171
recovery2 = rate_to_proportion(γ, δt) * I(k - 1)
7272

7373
eqs2 = [S ~ S(k - 1) - infection2,
7474
I ~ I(k - 1) + infection2 - recovery2,
75-
R ~ R(k - 1) + recovery2]
75+
R ~ R(k - 1) + recovery2,
76+
R2 ~ R]
7677

7778
@mtkbuild sys = DiscreteSystem(
78-
eqs2, t, [S, I, R], [c, nsteps, δt, β, γ]; controls = [β, γ], tspan)
79+
eqs2, t, [S, I, R, R2], [c, nsteps, δt, β, γ]; controls = [β, γ], tspan)
7980
@test ModelingToolkit.defaults(sys) != Dict()
8081

8182
prob_map2 = DiscreteProblem(sys)
82-
sol_map2 = solve(prob_map, FunctionMap());
83+
sol_map2 = solve(prob_map2, FunctionMap());
8384

84-
@test sol_map.u == sol_map2.u
85+
@test sol_map.u sol_map2.u
8586
@test sol_map.prob.p == sol_map2.prob.p
86-
87+
@test_throws Any sol_map2[R2]
88+
@test sol_map2[R2(k + 1)][begin:(end - 1)] == sol_map2[R][(begin + 1):end]
8789
# Direct Implementation
8890

8991
function sir_map!(u_diff, u, p, t)

0 commit comments

Comments
 (0)