Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 15 additions & 2 deletions src/systems/problem_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ function symbols_to_symbolics!(sys::AbstractSystem, varmap::AbstractDict)
end
end

"""
$(TYPEDSIGNATURES)

Utility function to get the value `val` corresponding to key `var` in `varmap`, and
return `getindex(val, idx)` if it exists or `nothing` otherwise.
"""
function get_and_getindex(varmap, var, idx)
val = get(varmap, var, nothing)
val === nothing && return nothing
return val[idx]
end

"""
$(TYPEDSIGNATURES)

Expand Down Expand Up @@ -115,8 +127,9 @@ function add_fallbacks!(
val = map(eachindex(var)) do idx
# @something is lazy and saves from writing a massive if-elseif-else
@something(get(varmap, var[idx], nothing),
get(varmap, ttvar[idx], nothing), get(fallbacks, var, nothing)[idx],
get(fallbacks, ttvar, nothing)[idx], get(fallbacks, var[idx], nothing),
get(varmap, ttvar[idx], nothing), get_and_getindex(fallbacks, var, idx),
get_and_getindex(fallbacks, ttvar, idx), get(
fallbacks, var[idx], nothing),
get(fallbacks, ttvar[idx], nothing), Some(nothing))
end
# only push the missing entries
Expand Down
8 changes: 8 additions & 0 deletions test/initial_values.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,11 @@ end
@test_throws ModelingToolkit.UnexpectedSymbolicValueInVarmap ODEProblem(
sys, [x => 1, y => 2], (0.0, 1.0), [p => 2q, q => 3p])
end

@testset "`add_fallbacks!` checks scalarized array parameters correctly" begin
@variables x(t)[1:2]
@parameters p[1:2, 1:2]
@mtkbuild sys = ODESystem(D(x) ~ p * x, t)
# used to throw a `MethodError` complaining about `getindex(::Nothing, ::CartesianIndex{2})`
@test_throws ModelingToolkit.MissingParametersError ODEProblem(sys, [x => ones(2)], (0.0, 1.0))
end
Loading