Skip to content

Commit 65b819a

Browse files
authored
Merge pull request #1622 from SciML/myb/varmap
Better error message
2 parents 989cfb8 + ca48b2c commit 65b819a

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/utils.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,10 @@ function mergedefaults(defaults, varmap, vars)
545545
end
546546
end
547547

548+
@noinline function throw_missingvars_in_sys(vars)
549+
throw(ArgumentError("$vars are either missing from the variable map or missing from the system's states/parameters list."))
550+
end
551+
548552
function promote_to_concrete(vs; tofloat = true, use_union = false)
549553
if isempty(vs)
550554
return vs
@@ -553,6 +557,8 @@ function promote_to_concrete(vs; tofloat = true, use_union = false)
553557
if Base.isconcretetype(T) && (!tofloat || T === float(T)) # nothing to do
554558
vs
555559
else
560+
sym_vs = filter(x -> SymbolicUtils.issym(x) || SymbolicUtils.istree(x), vs)
561+
isempty(sym_vs) || throw_missingvars_in_sys(sym_vs)
556562
C = typeof(first(vs))
557563
I = Int8
558564
has_int = false

test/components.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,33 @@ prob = ODAEProblem(sys, u0, (0, 10.0))
4646
sol = solve(prob, Rodas4())
4747
check_rc_sol(sol)
4848

49+
# https://discourse.julialang.org/t/using-optimization-parameters-in-modelingtoolkit/82099
50+
let
51+
@parameters param_r1 param_c1
52+
@named resistor = Resistor(R = param_r1)
53+
@named capacitor = Capacitor(C = param_c1)
54+
@named source = ConstantVoltage(V = 1.0)
55+
@named ground = Ground()
56+
57+
rc_eqs = [connect(source.p, resistor.p)
58+
connect(resistor.n, capacitor.p)
59+
connect(capacitor.n, source.n)
60+
connect(capacitor.n, ground.g)]
61+
62+
@named _rc_model = ODESystem(rc_eqs, t)
63+
@named rc_model = compose(_rc_model,
64+
[resistor, capacitor, source, ground])
65+
sys = structural_simplify(rc_model)
66+
u0 = [
67+
capacitor.v => 0.0,
68+
]
69+
70+
params = [param_r1 => 1.0, param_c1 => 1.0]
71+
tspan = (0.0, 10.0)
72+
73+
@test_throws Any prob=ODAEProblem(sys, u0, tspan, params)
74+
end
75+
4976
let
5077
# 1478
5178
@named resistor2 = Resistor(R = R)

0 commit comments

Comments
 (0)