Skip to content

Commit 14e810d

Browse files
committed
fix: add better warning for missing guess
1 parent 0ad773d commit 14e810d

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

src/systems/diffeqs/abstractodesystem.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1504,5 +1504,5 @@ function InitializationProblem{iip, specialize}(sys::AbstractSystem,
15041504
else
15051505
NonlinearLeastSquaresProblem
15061506
end
1507-
TProb(isys, u0map, parammap; kwargs..., build_initializeprob = false)
1507+
TProb(isys, u0map, parammap; kwargs..., build_initializeprob = false, is_initializeprob = true)
15081508
end

src/systems/problem_utils.jl

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,22 @@ function Base.showerror(io::IO, err::UnexpectedSymbolicValueInVarmap)
325325
""")
326326
end
327327

328+
struct MissingGuessError <: Exception
329+
sym::Any
330+
val::Any
331+
end
332+
333+
function Base.showerror(io::IO, err::MissingGuessError)
334+
println(io,
335+
"""
336+
The problem cannot be initialized without providing an additional numeric \
337+
guess to serve as a starting point for solving for the initial state. Please \
338+
provide another numeric value to `guesses` in the problem constructor.
339+
340+
This error was thrown because symbolic value $(err.val) was found for variable $(err.sym).
341+
""")
342+
end
343+
328344
"""
329345
$(TYPEDSIGNATURES)
330346
@@ -342,10 +358,11 @@ Keyword arguments:
342358
[`missingvars`](@ref) to perform the check.
343359
- `allow_symbolic` allows the returned array to contain symbolic values. If this is `true`,
344360
`promotetoconcrete` is set to `false`.
361+
- `is_initializeprob`: Whether the parent problem is an initialization problem.
345362
"""
346363
function better_varmap_to_vars(varmap::AbstractDict, vars::Vector;
347364
tofloat = true, use_union = true, container_type = Array,
348-
toterm = default_toterm, promotetoconcrete = nothing, check = true, allow_symbolic = false)
365+
toterm = default_toterm, promotetoconcrete = nothing, check = true, allow_symbolic = false, is_initializeprob = false)
349366
isempty(vars) && return nothing
350367

351368
if check
@@ -356,7 +373,11 @@ function better_varmap_to_vars(varmap::AbstractDict, vars::Vector;
356373
if !allow_symbolic
357374
for (sym, val) in zip(vars, vals)
358375
symbolic_type(val) == NotSymbolic() && continue
359-
throw(UnexpectedSymbolicValueInVarmap(sym, val))
376+
if is_initializeprob
377+
throw(MissingGuessError(sym, val))
378+
else
379+
throw(UnexpectedSymbolicValueInVarmap(sym, val))
380+
end
360381
end
361382
end
362383

@@ -704,7 +725,7 @@ Keyword arguments:
704725
- `fully_determined`: Override whether the initialization system is fully determined.
705726
- `check_initialization_units`: Enable or disable unit checks when constructing the
706727
initialization problem.
707-
- `tofloat`, `use_union`: Passed to [`better_varmap_to_vars`](@ref) for building `u0` (and
728+
- `tofloat`, `use_union`, `is_initializeprob`: Passed to [`better_varmap_to_vars`](@ref) for building `u0` (and
708729
possibly `p`).
709730
- `u0_constructor`: A function to apply to the `u0` value returned from `better_varmap_to_vars`
710731
to construct the final `u0` value.
@@ -742,7 +763,7 @@ function process_SciMLProblem(
742763
circular_dependency_max_cycles = 10,
743764
substitution_limit = 100, use_scc = true,
744765
force_initialization_time_independent = false, algebraic_only = false,
745-
allow_incomplete = false, kwargs...)
766+
allow_incomplete = false, is_initializeprob = false, kwargs...)
746767
dvs = unknowns(sys)
747768
ps = parameters(sys; initial_parameters = true)
748769
iv = has_iv(sys) ? get_iv(sys) : nothing
@@ -815,7 +836,7 @@ function process_SciMLProblem(
815836

816837
u0 = better_varmap_to_vars(
817838
op, dvs; tofloat = true, use_union = false,
818-
container_type = u0Type, allow_symbolic = symbolic_u0)
839+
container_type = u0Type, allow_symbolic = symbolic_u0, is_initializeprob)
819840

820841
if u0 !== nothing
821842
u0 = u0_constructor(u0)

src/variables.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ end
219219

220220
function Base.showerror(io::IO, e::MissingVariablesError)
221221
println(io, MISSING_VARIABLES_MESSAGE)
222-
println(io, e.vars)
222+
println(io, join(e.vars, ", "))
223223
end
224224

225225
function _varmap_to_vars(varmap::Dict, varlist; defaults = Dict(), check = false,

0 commit comments

Comments
 (0)