Skip to content

Commit d114048

Browse files
fix: error when all parameters are not initialized
1 parent c1b5d83 commit d114048

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/systems/parameter_buffer.jl

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,32 @@ function MTKParameters(
4343
p = merge(defs, p)
4444
p = merge(Dict(unwrap(k) => v for (k, v) in p),
4545
Dict(default_toterm(unwrap(k)) => v for (k, v) in p))
46-
p = Dict(k => fixpoint_sub(v, p) for (k, v) in p)
46+
p = Dict(unwrap(k) => fixpoint_sub(v, p) for (k, v) in p)
4747
for (sym, _) in p
4848
if istree(sym) && operation(sym) === getindex &&
4949
first(arguments(sym)) in all_ps
5050
error("Scalarized parameter values ($sym) are not supported. Instead of `[p[1] => 1.0, p[2] => 2.0]` use `[p => [1.0, 2.0]]`")
5151
end
5252
end
5353

54+
missing_params = Set()
55+
for idxmap in (ic.tunable_idx, ic.discrete_idx, ic.constant_idx, ic.nonnumeric_idx)
56+
for sym in keys(idxmap)
57+
sym isa Symbol && continue
58+
haskey(p, sym) && continue
59+
hasname(sym) && haskey(p, getname(sym)) && continue
60+
ttsym = default_toterm(sym)
61+
haskey(p, ttsym) && continue
62+
hasname(ttsym) && haskey(p, getname(ttsym)) && continue
63+
64+
istree(sym) && operation(sym) === getindex && haskey(p, arguments(sym)[1]) &&
65+
continue
66+
push!(missing_params, sym)
67+
end
68+
end
69+
70+
isempty(missing_params) || throw(MissingVariablesError(collect(missing_params)))
71+
5472
tunable_buffer = Tuple(Vector{temp.type}(undef, temp.length)
5573
for temp in ic.tunable_buffer_sizes)
5674
disc_buffer = Tuple(Vector{temp.type}(undef, temp.length)

test/mtkparameters.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,16 @@ ps = MTKParameters(sys, [p => 1.0, q => 2.0, r => 3.0])
121121
newps = remake_buffer(sys, ps, Dict(p => 1.0f0))
122122
@test newps.tunable[1] isa Vector{Float32}
123123
@test newps.tunable[1] == [1.0f0, 2.0f0, 3.0f0]
124+
125+
# Issue#2624
126+
@parameters p d
127+
@variables X(t)
128+
eqs = [D(X) ~ p - d * X]
129+
@mtkbuild sys = ODESystem(eqs, t)
130+
131+
u0 = [X => 1.0]
132+
tspan = (0.0, 100.0)
133+
ps = [p => 1.0] # Value for `d` is missing
134+
135+
@test_throws ModelingToolkit.MissingVariablesError ODEProblem(sys, u0, tspan, ps)
136+
@test_nowarn ODEProblem(sys, u0, tspan, [ps..., d => 1.0])

0 commit comments

Comments
 (0)