Skip to content

Commit 75b5457

Browse files
feat: evaluate in better_varmap_to_vars, allow passing use_union
1 parent a5080e1 commit 75b5457

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/systems/problem_utils.jl

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,26 @@ function Base.showerror(io::IO, err::MissingGuessError)
328328
In order to resolve this, please provide additional numeric guesses so that the chain can be resolved to assign numeric values to each variable. """)
329329
end
330330

331+
const MISSING_VARIABLES_MESSAGE = """
332+
Initial condition underdefined. Some are missing from the variable map.
333+
Please provide a default (`u0`), initialization equation, or guess
334+
for the following variables:
335+
"""
336+
337+
struct MissingVariablesError <: Exception
338+
vars::Any
339+
end
340+
341+
function Base.showerror(io::IO, e::MissingVariablesError)
342+
println(io, MISSING_VARIABLES_MESSAGE)
343+
println(io, join(e.vars, ", "))
344+
end
345+
331346
"""
332347
$(TYPEDSIGNATURES)
333348
334349
Return an array of values where the `i`th element corresponds to the value of `vars[i]`
335-
in `varmap`. Does not perform symbolic substitution in the values of `varmap`.
350+
in `varmap`. Will mutate `varmap` by symbolically substituting it into itself.
336351
337352
Keyword arguments:
338353
- `container_type`: The type of the returned container.
@@ -348,11 +363,13 @@ Keyword arguments:
348363
- `check`: Whether to check if all of `vars` are keys of `varmap`.
349364
- `is_initializeprob`: Whether an initialization problem is being constructed. Used for
350365
better error messages.
366+
- `substitution_limit`: The maximum number of times to recursively substitute `varmap` into
367+
itself to get a numeric value for each variable in `vars`.
351368
"""
352369
function better_varmap_to_vars(varmap::AbstractDict, vars::Vector;
353370
tofloat = true, use_union = false, container_type = Array, buffer_eltype = Nothing,
354371
toterm = default_toterm, check = true, allow_symbolic = false,
355-
is_initializeprob = false)
372+
is_initializeprob = false, substitution_limit = 100)
356373
isempty(vars) && return nothing
357374

358375
varmap = recursive_unwrap(varmap)
@@ -369,6 +386,7 @@ function better_varmap_to_vars(varmap::AbstractDict, vars::Vector;
369386
end
370387
end
371388
end
389+
evaluate_varmap!(varmap, vars; limit = substitution_limit)
372390
vals = map(x -> varmap[x], vars)
373391
if !allow_symbolic
374392
missingsyms = Any[]

0 commit comments

Comments
 (0)