Skip to content

Commit c1e4dfd

Browse files
refactor: make better_varmap_to_vars more predictable
1 parent 8109604 commit c1e4dfd

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

src/systems/problem_utils.jl

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -335,28 +335,39 @@ Return an array of values where the `i`th element corresponds to the value of `v
335335
in `varmap`. Does not perform symbolic substitution in the values of `varmap`.
336336
337337
Keyword arguments:
338-
- `tofloat`: Convert values to floating point numbers using `float`.
339-
- `container_type`: The type of container to use for the values.
340-
- `toterm`: The `toterm` method to use for converting symbolics.
341-
- `promotetoconcrete`: whether the promote to a concrete buffer (respecting
342-
`tofloat`). Defaults to `container_type <: AbstractArray`.
343-
- `check`: Error if any variables in `vars` do not have a mapping in `varmap`. Uses
344-
[`missingvars`](@ref) to perform the check.
345-
- `allow_symbolic` allows the returned array to contain symbolic values. If this is `true`,
346-
`promotetoconcrete` is set to `false`.
347-
- `is_initializeprob, guesses`: Used to determine whether the system is missing guesses.
338+
- `container_type`: The type of the returned container.
339+
- `allow_symbolic`: Whether the returned container of values can have symbolic expressions.
340+
- `buffer_eltype`: The `eltype` of the returned container if `!allow_symbolic`. If
341+
`Nothing`, automatically promotes the values in the container to a common `eltype`.
342+
- `tofloat`: Whether to promote values to floating point numbers if
343+
`buffer_eltype == Nothing`.
344+
- `use_union`: Whether to allow using a `Union` as the `eltype` if
345+
`buffer_eltype == Nothing`.
346+
- `toterm`: The `toterm` function for canonicalizing keys of `varmap`. A value of `nothing`
347+
disables this process.
348+
- `check`: Whether to check if all of `vars` are keys of `varmap`.
349+
- `is_initializeprob`: Whether an initialization problem is being constructed. Used for
350+
better error messages.
348351
"""
349352
function better_varmap_to_vars(varmap::AbstractDict, vars::Vector;
350-
tofloat = true, container_type = Array, floatT = Nothing,
351-
toterm = default_toterm, promotetoconcrete = nothing, check = true,
352-
allow_symbolic = false, is_initializeprob = false)
353+
tofloat = true, use_union = false, container_type = Array, buffer_eltype = Nothing,
354+
toterm = default_toterm, check = true, allow_symbolic = false,
355+
is_initializeprob = false)
353356
isempty(vars) && return nothing
354357

355358
varmap = recursive_unwrap(varmap)
356-
add_toterms!(varmap; toterm)
359+
if toterm !== nothing
360+
add_toterms!(varmap; toterm)
361+
end
357362
if check
358363
missing_vars = missingvars(varmap, vars; toterm)
359-
isempty(missing_vars) || throw(MissingVariablesError(missing_vars))
364+
if !isempty(missing_vars)
365+
if is_initializeprob
366+
throw(MissingGuessError(collect(missing_vars), collect(missing_vars)))
367+
else
368+
throw(MissingVariablesError(missing_vars))
369+
end
370+
end
360371
end
361372
vals = map(x -> varmap[x], vars)
362373
if !allow_symbolic
@@ -372,20 +383,17 @@ function better_varmap_to_vars(varmap::AbstractDict, vars::Vector;
372383
is_initializeprob ? throw(MissingGuessError(missingsyms, missingvals)) :
373384
throw(UnexpectedSymbolicValueInVarmap(missingsyms[1], missingvals[1]))
374385
end
375-
if tofloat && !(floatT == Nothing)
376-
vals = floatT.(vals)
386+
if buffer_eltype == Nothing
387+
vals = promote_to_concrete(vals; tofloat, use_union)
388+
else
389+
vals = buffer_eltype.(vals)
377390
end
378391
end
379392

380-
if container_type <: Union{AbstractDict, Tuple, Nothing, SciMLBase.NullParameters}
393+
if container_type <: Union{AbstractDict, Nothing, SciMLBase.NullParameters}
381394
container_type = Array
382395
end
383396

384-
promotetoconcrete === nothing && (promotetoconcrete = container_type <: AbstractArray)
385-
if promotetoconcrete && !allow_symbolic
386-
vals = promote_to_concrete(vals; tofloat = tofloat, use_union = false)
387-
end
388-
389397
if isempty(vals)
390398
return nothing
391399
elseif container_type <: Tuple

0 commit comments

Comments
 (0)