Skip to content

Commit e735b4f

Browse files
fix: handle unscalarized defaults for scalarized observed variables
1 parent eb6bc15 commit e735b4f

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/systems/problem_utils.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,9 @@ entry for `eq.lhs`, insert the reverse mapping if `eq.rhs` is not a number.
275275
"""
276276
function add_observed_equations!(varmap::AbstractDict, eqs)
277277
for eq in eqs
278-
if haskey(varmap, eq.lhs)
278+
if var_in_varlist(eq.lhs, keys(varmap), nothing)
279279
eq.rhs isa Number && continue
280-
haskey(varmap, eq.rhs) && continue
280+
var_in_varlist(eq.rhs, keys(varmap), nothing) && continue
281281
!iscall(eq.rhs) || issym(operation(eq.rhs)) || continue
282282
varmap[eq.rhs] = eq.lhs
283283
else

test/initial_values.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using ModelingToolkit
22
using ModelingToolkit: t_nounits as t, D_nounits as D, get_u0
3+
using OrdinaryDiffEq
34
using SymbolicIndexingInterface: getu
45

56
@variables x(t)[1:3]=[1.0, 2.0, 3.0] y(t) z(t)[1:2]
@@ -183,3 +184,18 @@ end
183184
@test_throws ModelingToolkit.MissingParametersError ODEProblem(
184185
sys, [x => ones(2)], (0.0, 1.0))
185186
end
187+
188+
@testset "Unscalarized default for scalarized observed variable" begin
189+
@parameters p[1:4] = rand(4)
190+
@variables x(t)[1:4] y(t)[1:2]
191+
eqs = [
192+
D(x) ~ x,
193+
y[1] ~ x[3],
194+
y[2] ~ x[4]
195+
]
196+
@mtkbuild sys = ODESystem(eqs, t; defaults = [x => vcat(ones(2), y), y => x[1:2] ./ 2])
197+
prob = ODEProblem(sys, [], (0.0, 1.0))
198+
sol = solve(prob)
199+
@test SciMLBase.successful_retcode(sol)
200+
@test sol.u[1] [1.0, 1.0, 0.5, 0.5]
201+
end

0 commit comments

Comments
 (0)