Skip to content

Cannot initialize non-1-indexed array variables #3659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
hersle opened this issue May 23, 2025 · 2 comments
Open

Cannot initialize non-1-indexed array variables #3659

hersle opened this issue May 23, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@hersle
Copy link
Contributor

hersle commented May 23, 2025

Trivial example:

using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
@variables x(t)[0:1]
@named M = ODESystem([D(x[0]) ~ 0, x[1] ~ 1], t)
M = structural_simplify(M)
prob = ODEProblem(M, [x[0] => 1.0], (0.0, 1.0), [])
ERROR: BoundsError: attempt to access SymbolicUtils.BasicSymbolic{AbstractVector{Real}} at index [0]
Stacktrace:
  [1] getindex(x::SymbolicUtils.BasicSymbolic{AbstractVector{Real}}, idx::Int64)
    @ Symbolics ~/.julia/packages/Symbolics/kQzvO/src/array-lib.jl:32
  [2] add_fallbacks!(varmap::Dict{Any, Any}, vars::Vector{SymbolicUtils.BasicSymbolic{Real}}, fallbacks::Dict{Any, Any}; toterm::typeof(ModelingToolkit.default_toterm))
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/problem_utils.jl:163
  [3] add_fallbacks!
    @ ~/.julia/dev/ModelingToolkit/src/systems/problem_utils.jl:117 [inlined]
  [4] build_operating_point!(sys::ODESystem, u0map::Dict{Any, Any}, pmap::Dict{Any, Any}, defs::Dict{Any, Any}, cmap::Vector{Equation}, dvs::Vector{SymbolicUtils.BasicSymbolic{Real}}, ps::Vector{Any})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/problem_utils.jl:542
  [5] MTKParameters(sys::ODESystem, p::Dict{Any, Any}, u0::Dict{Any, Any}; tofloat::Bool, t0::Nothing, substitution_limit::Int64, floatT::Type, p_constructor::Function)
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/parameter_buffer.jl:53
  [6] MTKParameters
    @ ~/.julia/packages/ModelingToolkit/udKhl/src/systems/parameter_buffer.jl:29 [inlined]
  [7] process_SciMLProblem(constructor::Type, sys::ODESystem, u0map::Vector{…}, pmap::Vector{…}; build_initializeprob::Bool, implicit_dae::Bool, t::Float64, guesses::Dict{…}, warn_initialize_determined::Bool, initialization_eqs::Vector{…}, eval_expression::Bool, eval_module::Module, fully_determined::Nothing, check_initialization_units::Bool, tofloat::Bool, u0_constructor::typeof(identity), p_constructor::typeof(identity), du0map::Nothing, check_length::Bool, symbolic_u0::Bool, warn_cyclic_dependency::Bool, circular_dependency_max_cycle_length::Int64, circular_dependency_max_cycles::Int64, substitution_limit::Int64, use_scc::Bool, force_initialization_time_independent::Bool, algebraic_only::Bool, allow_incomplete::Bool, is_initializeprob::Bool, kwargs::@Kwargs{})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/problem_utils.jl:1345
  [8] (ODEProblem{…})(sys::ODESystem, u0map::Vector{…}, tspan::Tuple{…}, parammap::Vector{…}; allow_cost::Bool, callback::Nothing, check_length::Bool, warn_initialize_determined::Bool, eval_expression::Bool, eval_module::Module, kwargs::@Kwargs{})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:814
  ...
@hersle hersle added the bug Something isn't working label May 23, 2025
@hersle
Copy link
Contributor Author

hersle commented May 23, 2025

I think this happens because this val is a

ModelingToolkit.StructuralTransformations.change_origin(CartesianIndex(0,), SymbolicUtils.BasicSymbolic{Real}[(x(t))[0], (x(t))[1]])

This wraps the 0-based array inside a registered 1-indexed array symbolic function.
So indexing it at 0 fails.

@hersle
Copy link
Contributor Author

hersle commented May 23, 2025

Is this hack what is supposed to handle the change_origin wrapping?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant