Skip to content

fix: Throw better warning when a guess is missing #3456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: add better warning for missing guess
  • Loading branch information
vyudu committed Mar 17, 2025
commit 14e810de1a18b35c515873c4af19bd10e708c730
2 changes: 1 addition & 1 deletion src/systems/diffeqs/abstractodesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1504,5 +1504,5 @@ function InitializationProblem{iip, specialize}(sys::AbstractSystem,
else
NonlinearLeastSquaresProblem
end
TProb(isys, u0map, parammap; kwargs..., build_initializeprob = false)
TProb(isys, u0map, parammap; kwargs..., build_initializeprob = false, is_initializeprob = true)
end
31 changes: 26 additions & 5 deletions src/systems/problem_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,22 @@ function Base.showerror(io::IO, err::UnexpectedSymbolicValueInVarmap)
""")
end

struct MissingGuessError <: Exception
sym::Any
val::Any
end

function Base.showerror(io::IO, err::MissingGuessError)
println(io,
"""
The problem cannot be initialized without providing an additional numeric \
guess to serve as a starting point for solving for the initial state. Please \
provide another numeric value to `guesses` in the problem constructor.

This error was thrown because symbolic value $(err.val) was found for variable $(err.sym).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only one what?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the message is a bit confusing since one could interpret it as asking for more than one guess for a single variable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay, will change

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this have a list of variables that need guesses? Not just one?

""")
end

"""
$(TYPEDSIGNATURES)

Expand All @@ -342,10 +358,11 @@ Keyword arguments:
[`missingvars`](@ref) to perform the check.
- `allow_symbolic` allows the returned array to contain symbolic values. If this is `true`,
`promotetoconcrete` is set to `false`.
- `is_initializeprob`: Whether the parent problem is an initialization problem.
"""
function better_varmap_to_vars(varmap::AbstractDict, vars::Vector;
tofloat = true, use_union = true, container_type = Array,
toterm = default_toterm, promotetoconcrete = nothing, check = true, allow_symbolic = false)
toterm = default_toterm, promotetoconcrete = nothing, check = true, allow_symbolic = false, is_initializeprob = false)
isempty(vars) && return nothing

if check
Expand All @@ -356,7 +373,11 @@ function better_varmap_to_vars(varmap::AbstractDict, vars::Vector;
if !allow_symbolic
for (sym, val) in zip(vars, vals)
symbolic_type(val) == NotSymbolic() && continue
throw(UnexpectedSymbolicValueInVarmap(sym, val))
if is_initializeprob
throw(MissingGuessError(sym, val))
else
throw(UnexpectedSymbolicValueInVarmap(sym, val))
end
end
end

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

u0 = better_varmap_to_vars(
op, dvs; tofloat = true, use_union = false,
container_type = u0Type, allow_symbolic = symbolic_u0)
container_type = u0Type, allow_symbolic = symbolic_u0, is_initializeprob)

if u0 !== nothing
u0 = u0_constructor(u0)
Expand Down
2 changes: 1 addition & 1 deletion src/variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ end

function Base.showerror(io::IO, e::MissingVariablesError)
println(io, MISSING_VARIABLES_MESSAGE)
println(io, e.vars)
println(io, join(e.vars, ", "))
end

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