Skip to content

Commit f884651

Browse files
committed
print all missing values
1 parent 6ddc1a7 commit f884651

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

src/systems/problem_utils.jl

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -326,23 +326,24 @@ function Base.showerror(io::IO, err::UnexpectedSymbolicValueInVarmap)
326326
end
327327

328328
struct MissingGuessError <: Exception
329-
sym::Any
330-
val::Any
329+
syms::Vector{Any}
330+
vals::Vector{Any}
331331
end
332332

333333
function Base.showerror(io::IO, err::MissingGuessError)
334334
println(io,
335335
"""
336336
Unable to resolve numeric guesses for all of the variables in the system. \
337-
This may be because your guesses are cyclic.
337+
This may be because your guesses are cyclic. In order for the problem to be \
338+
initialized, all of the variables must have a numeric value to serve as a \
339+
starting point for the nonlinear solve.
338340
339-
In order for the problem to be initialized, all of the variables must have \
340-
a numeric value to serve as a starting point for the nonlinear solve. \
341-
Please provide an additional numeric guess to `guesses` in \
342-
the problem constructor.
343-
344-
This error was thrown because symbolic value $(err.val) was found for variable $(err.sym).
341+
Symbolic values were found for the following variables/parameters in the map; \
342+
please provide additional numeric guesses so they can resolve to numbers:
345343
""")
344+
for (sym, val) in zip(err.syms, err.vals)
345+
println(sym, " => ", val)
346+
end
346347
end
347348

348349
"""
@@ -375,13 +376,17 @@ function better_varmap_to_vars(varmap::AbstractDict, vars::Vector;
375376
end
376377
vals = map(x -> varmap[x], vars)
377378
if !allow_symbolic
379+
missingsyms = Any[]
380+
missingvals = Any[]
378381
for (sym, val) in zip(vars, vals)
379382
symbolic_type(val) == NotSymbolic() && continue
380-
if is_initializeprob
381-
throw(MissingGuessError(sym, val))
382-
else
383-
throw(UnexpectedSymbolicValueInVarmap(sym, val))
384-
end
383+
push!(missingsyms, sym)
384+
push!(missingvals, val)
385+
end
386+
387+
if !isempty(missingsyms)
388+
is_initializeprob ? throw(MissingGuessError(missingsyms, missingvals)) :
389+
throw(UnexpectedSymbolicValueInVarmap(missingsyms[1], missingvals[1]))
385390
end
386391
end
387392

test/initial_values.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,10 @@ end
210210

211211
@test_throws ModelingToolkit.MissingGuessError ODEProblem(pend, [x => 1], (0, 1), [g => 1], guesses = [y => λ, λ => y + 1])
212212
ODEProblem(pend, [x => 1], (0, 1), [g => 1], guesses = [y => λ, λ => 0.5])
213+
214+
# Throw multiple if multiple are missing
215+
@variables a(t) b(t) c(t) d(t) e(t)
216+
eqs = [D(a) ~ b, D(b) ~ c, D(c) ~ d, D(d) ~ e, D(e) ~ 1]
217+
@mtkbuild sys = ODESystem(eqs, t)
218+
@test_throws ["a(t) => ", "c(t) => "] ODEProblem(sys, [e => 2, a => b, b => a + 1, c => d, d => c + 1], (0, 1))
213219
end

0 commit comments

Comments
 (0)