Missing from the variable map error

I’m trying to construct an ODEProblem from a ODESystem with the following code. I can’t seem to debug this error.

using ModelingToolkit
using LinearAlgebra
using StaticArrays

@parameters t μ 
@variables x(t) y(t) z(t) ẋ(t) ẏ(t) ż(t)
δ = Differential(t)
r = @SVector [x,y,z]
v = @SVector [ẋ,ẏ,ż]

eqs = vcat(
    δ.(r) .~ v,
    δ.(v) .~ -μ .* (r ./ norm(r)^3)
)

@named R2BP = ODESystem(eqs, t, vcat(r,v), [μ])

x₀ = Dict(
    R2BP.x => 1e4,
    R2BP.y => 0.0,
    R2BP.z => 1e4,
    R2BP.ẋ => 1e3,
    R2BP.ẏ => 0.0,
    R2BP.ż => -2e3
)

ts = (0.0, 1e4)

μ = Dict(R2BP.μ => 5e6)

ODEProblem(R2BP, x₀, ts, μ)
ERROR: ArgumentError: Term{Real, Nothing}[x(t), y(t), z(t), ẋ(t), ẏ(t), ż(t)] are missing from the variable map.
Stacktrace:
  [1] throw_missingvars(vars::Vector{Term{Real, Nothing}})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/MuQfD/src/variables.jl:67
  [2] _varmap_to_vars(varmap::Dict{Term{Real, Nothing}, Float64}, varlist::Vector{Term{Real, Nothing}}; defaults::Dict{Any, Any}, check::Bool, toterm::typeof(Symbolics.diff2term))
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/MuQfD/src/variables.jl:59
  [3] varmap_to_vars(varmap::Dict{Term{Real, Nothing}, Float64}, varlist::Vector{Term{Real, Nothing}}; defaults::Dict{Any, Any}, check::Bool, toterm::Function)
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/MuQfD/src/variables.jl:34
  [4] process_DEProblem(constructor::Type, sys::ODESystem, u0map::Dict{Term{Real, Nothing}, Float64}, parammap::Dict{Sym{Real, Base.ImmutableDict{DataType, Any}}, Float64}; 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, kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/MuQfD/src/systems/diffeqs/abstractodesystem.jl:433
  [5] process_DEProblem
    @ ~/.julia/packages/ModelingToolkit/MuQfD/src/systems/diffeqs/abstractodesystem.jl:409 [inlined]
  [6] (ODEProblem{true, tType, isinplace, P, F, K, PT} where {tType, isinplace, P, F, K, PT})(sys::ODESystem, u0map::Dict{Term{Real, Nothing}, Float64}, tspan::Tuple{Float64, Float64}, parammap::Dict{Sym{Real, Base.ImmutableDict{DataType, Any}}, Float64}; kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/MuQfD/src/systems/diffeqs/abstractodesystem.jl:522
  [7] (ODEProblem{true, tType, isinplace, P, F, K, PT} where {tType, isinplace, P, F, K, PT})(sys::ODESystem, u0map::Dict{Term{Real, Nothing}, Float64}, tspan::Tuple{Float64, Float64}, parammap::Dict{Sym{Real, Base.ImmutableDict{DataType, Any}}, Float64})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/MuQfD/src/systems/diffeqs/abstractodesystem.jl:522
  [8] ODEProblem(::ODESystem, ::Dict{Term{Real, Nothing}, Float64}, ::Vararg{Any, N} where N; kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/MuQfD/src/systems/diffeqs/abstractodesystem.jl:501
  [9] ODEProblem(::ODESystem, ::Dict{Term{Real, Nothing}, Float64}, ::Vararg{Any, N} where N)
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/MuQfD/src/systems/diffeqs/abstractodesystem.jl:501
 [10] top-level scope
    @ REPL[6]:1
1 Like

Update. This code works if I remove the R2BP. in front of each state variable and parameter. But then how can I create an ODEProblem from the ODESystem if the ODESystem is defined in another package?

This is described in this issue and others.

You can use the @nonamespace macro in front of your initial condition and parameter declarations.

@nonamespace x₀ = Dict(
    R2BP.x => 1e4,
    R2BP.y => 0.0,
    R2BP.z => 1e4,
    R2BP.ẋ => 1e3,
    R2BP.ẏ => 0.0,
    R2BP.ż => -2e3
)

@nonamespace μ = Dict(R2BP.μ => 5e6)
2 Likes

Yup this is the right answer for now. But given how non-intuitive it is, there’s a breaking change coming to switch the assumption of implicit namespacing. Just noting that it’s coming rather soon.

1 Like

I know I have read the proper way to do this now. It should probably be linked here.