How to use ParentScope with arrays

This used to work in ModelingToolkit 8.65.0 but stopped in 8.66.0. Was it ever supposed to work? Is there a way to move array variables up the hierarchy?

@parameters a[1:2]=[1, 2] b=4 c=1
@variables t x(t)=ParentScope(a[1]) y(t)=ParentScope(b)
D = Differential(t)
@named level0 = ODESystem([D(x)~ParentScope(a[2]),
                           D(y)~ParentScope(c)], t, [x, y], [])
level1 = ODESystem(Equation[], t, [], [a..., b, c]; name=:level1) ∘ level0
level1 = structural_simplify(level1)
prob = ODEProblem(level1, [], (0, 1))

The first signs of trouble appear here, level0₊a[1] should not exist because of ParentScope, but the initial condition for y is scoped correctly.

julia> level1 = structural_simplify(level1)
Model level1 with 2 equations
States (2):
  level0₊x(t) [defaults to level0₊a[1]]
  level0₊y(t) [defaults to b]
Parameters (4):
  a[1] [defaults to 1]
  a[2] [defaults to 2]
  b [defaults to 4]
  c [defaults to 1]
Incidence matrix:2×4 SparseArrays.SparseMatrixCSC{Num, Int64} with 2 stored entries:
 ⋅  ⋅  ×  ⋅
 ⋅  ⋅  ⋅  ×

The equations show the same pattern

julia> equations(level1)
2-element Vector{Equation}:
 Differential(t)(level0₊x(t)) ~ level0₊a[2]
 Differential(t)(level0₊y(t)) ~ c

And constructing a problem fails.

julia> prob = ODEProblem(level1, [], (0, 1))
ERROR: ArgumentError: Any[level0₊a[1]] are either missing from the variable map or missing from the system's states/parameters list.
Stacktrace:
  [1] throw_missingvars_in_sys(vars::Vector{Any})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/dkLCE/src/utils.jl:642
  [2] promote_to_concrete(vs::Vector{Any}; tofloat::Bool, use_union::Bool)
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/dkLCE/src/utils.jl:654
  [3] varmap_to_vars(varmap::Vector{Any}, varlist::Vector{Any}; defaults::Dict{Any, Any}, check::Bool, toterm::Function, promotetoconcrete::Nothing, tofloat::Bool, use_union::Bool)
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/dkLCE/src/variables.jl:92
  [4] get_u0_p(sys::ODESystem, u0map::Vector{Any}, parammap::SciMLBase.NullParameters; use_union::Bool, tofloat::Bool, symbolic_u0::Bool)
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/dkLCE/src/systems/diffeqs/abstractodesystem.jl:700
  [5] get_u0_p
    @ ~/.julia/packages/ModelingToolkit/dkLCE/src/systems/diffeqs/abstractodesystem.jl:684 [inlined]
  [6] process_DEProblem(constructor::Type, sys::ODESystem, u0map::Vector{Any}, parammap::SciMLBase.NullParameters; implicit_dae::Bool, du0map::Nothing, version::Nothing, tgrad::Bool, jac::Bool, checkbounds::Bool, sparse::Bool, simplify::Bool, linenumbers::Bool, parallel::Symbolics.SerialForm, eval_expression::Bool, use_union::Bool, tofloat::Bool, symbolic_u0::Bool, kwargs::Base.Pairs{Symbol, Integer, Tuple{Symbol, Symbol, Symbol}, NamedTuple{(:t, :has_difference, :check_length), Tuple{Int64, Bool, Bool}}})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/dkLCE/src/systems/diffeqs/abstractodesystem.jl:724
  [7] (ODEProblem{true, SciMLBase.AutoSpecialize})(sys::ODESystem, u0map::Vector{Any}, tspan::Tuple{Int64, Int64}, parammap::SciMLBase.NullParameters; callback::Nothing, check_length::Bool, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/dkLCE/src/systems/diffeqs/abstractodesystem.jl:834
  [8] ODEProblem
    @ ~/.julia/packages/ModelingToolkit/dkLCE/src/systems/diffeqs/abstractodesystem.jl:827 [inlined]
  [9] (ODEProblem{true, SciMLBase.AutoSpecialize})(sys::ODESystem, u0map::Vector{Any}, tspan::Tuple{Int64, Int64})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/dkLCE/src/systems/diffeqs/abstractodesystem.jl:827
 [10] (ODEProblem{true})(::ODESystem, ::Vector{Any}, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/dkLCE/src/systems/diffeqs/abstractodesystem.jl:814
 [11] (ODEProblem{true})(::ODESystem, ::Vector{Any}, ::Vararg{Any})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/dkLCE/src/systems/diffeqs/abstractodesystem.jl:813
 [12] ODEProblem(::ODESystem, ::Vector{Any}, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/dkLCE/src/systems/diffeqs/abstractodesystem.jl:810
 [13] ODEProblem(::ODESystem, ::Vector{Any}, ::Vararg{Any})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/dkLCE/src/systems/diffeqs/abstractodesystem.jl:809
 [14] top-level scope
    @ REPL[135]:1

Open an issue. I don’t think I had a good example in the tests with array variables when doing the last set of scoping changes, so this would be a good think to fix and ensure is tested.

Issue opened