Skip to content

Commit 361e59a

Browse files
Merge pull request #4772 from SciML/as/index-with-var
fix: handle variable discover of `x[i]` where `i` is a variable
2 parents 77941f8 + 540689d commit 361e59a

3 files changed

Lines changed: 16 additions & 3 deletions

File tree

lib/ModelingToolkitBase/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ StochasticDiffEq = "6.82.0, 7"
188188
Sundials = "6"
189189
SymbolicIndexingInterface = "0.3.39"
190190
SymbolicUtils = "4.35.3"
191-
Symbolics = "7.31"
191+
Symbolics = "7.32.1"
192192
TaskLocalValues = "0.1.3"
193193
Test = "1"
194194
Tracker = "0.2.30"

lib/ModelingToolkitBase/src/utils.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -954,8 +954,15 @@ function collect_var!(unknowns::OrderedSet{SymbolicT}, parameters::OrderedSet{Sy
954954
)
955955
end
956956
arr, isarr = split_indexed_var(var)
957-
if isarr && SU.is_array_shape(SU.shape(var))
958-
# `var` is indexed, and it is an array, so it must be a slice. Replace it with `arr`.
957+
if isarr && (
958+
# `var` is indexed, and it is an array, so it must be a slice. Replace it with `arr`.
959+
SU.is_array_shape(SU.shape(var)) ||
960+
# `var` is of the form `x[i]` where `i` is also a variable/expression
961+
any(!SU.isconst, Iterators.drop(arguments(var), 1))
962+
)
963+
for arg in Iterators.drop(arguments(var), 1)
964+
collect_vars!(unknowns, parameters, arg, iv)
965+
end
959966
var = arr
960967
end
961968
check_scope_depth(getmetadata(arr, SymScope, LocalScope())::AllScopes, depth) || return nothing

lib/ModelingToolkitBase/test/system_building.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,9 @@ ModelingToolkitBase.validate_operator(::MyOp, args...; kws...) = nothing
7272
@named sys = System([D(x) ~ x + SU.term(MyOp(), [0.0]; type = Real, shape = SU.ShapeVecT())], t)
7373
@test issetequal(unknowns(sys), [x])
7474
end
75+
76+
@testset "Variable discovery `x[i]` where `i` is a variable" begin
77+
@variables x(t)[1:2] i(t)::Int
78+
@named sys = System([x[i] ~ 0], t)
79+
@test issetequal(unknowns(sys), [x, i])
80+
end

0 commit comments

Comments
 (0)