Skip to content

Commit d3120e7

Browse files
Merge pull request #3332 from AayushSabharwal/as/fix-add-fallbacks
fix: fix `add_fallbacks!`
2 parents 40a85df + d7f908e commit d3120e7

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/systems/problem_utils.jl

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,18 @@ function symbols_to_symbolics!(sys::AbstractSystem, varmap::AbstractDict)
8888
end
8989
end
9090

91+
"""
92+
$(TYPEDSIGNATURES)
93+
94+
Utility function to get the value `val` corresponding to key `var` in `varmap`, and
95+
return `getindex(val, idx)` if it exists or `nothing` otherwise.
96+
"""
97+
function get_and_getindex(varmap, var, idx)
98+
val = get(varmap, var, nothing)
99+
val === nothing && return nothing
100+
return val[idx]
101+
end
102+
91103
"""
92104
$(TYPEDSIGNATURES)
93105
@@ -115,8 +127,9 @@ function add_fallbacks!(
115127
val = map(eachindex(var)) do idx
116128
# @something is lazy and saves from writing a massive if-elseif-else
117129
@something(get(varmap, var[idx], nothing),
118-
get(varmap, ttvar[idx], nothing), get(fallbacks, var, nothing)[idx],
119-
get(fallbacks, ttvar, nothing)[idx], get(fallbacks, var[idx], nothing),
130+
get(varmap, ttvar[idx], nothing), get_and_getindex(fallbacks, var, idx),
131+
get_and_getindex(fallbacks, ttvar, idx), get(
132+
fallbacks, var[idx], nothing),
120133
get(fallbacks, ttvar[idx], nothing), Some(nothing))
121134
end
122135
# only push the missing entries

test/initial_values.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,11 @@ end
174174
@test_throws ModelingToolkit.UnexpectedSymbolicValueInVarmap ODEProblem(
175175
sys, [x => 1, y => 2], (0.0, 1.0), [p => 2q, q => 3p])
176176
end
177+
178+
@testset "`add_fallbacks!` checks scalarized array parameters correctly" begin
179+
@variables x(t)[1:2]
180+
@parameters p[1:2, 1:2]
181+
@mtkbuild sys = ODESystem(D(x) ~ p * x, t)
182+
# used to throw a `MethodError` complaining about `getindex(::Nothing, ::CartesianIndex{2})`
183+
@test_throws ModelingToolkit.MissingParametersError ODEProblem(sys, [x => ones(2)], (0.0, 1.0))
184+
end

0 commit comments

Comments
 (0)