Skip to content

Better error messages for unlisted subsystems #1747

Open
@baggepinnen

Description

@baggepinnen

The following is a correct specification of composition, where sys1 is provided to systems=[sys1] when creating sys2:

@variables t
D = Differential(t)
@variables x(t)=0
eqs1 = [D(x) ~ -x]
@named sys1 = ODESystem(eqs1, t)

eqs2 = [D(x) ~ -x + sys1.x]
@named sys2 = ODESystem(eqs2, t; systems=[sys1])

tspan = (0.0, 1.0)
prob = ODEProblem(sys2, [], tspan)
solve(prob, Rodas5())

When building large components with several sub components, it's easy to forget to list one of the subsystems, and get a very strange error message

eqs2 = [D(x) ~ -x + sys1.x]
@named sys2 = ODESystem(eqs2, t; systems=[])

tspan = (0.0, 1.0)
prob = ODEProblem(sys2, [], tspan)
ERROR: ArgumentError: Equations (1), states (2), and initial conditions (2) are of different lengths. To allow a different number of equations than states use kwarg check_length=false.
Stacktrace:
 [1] check_eqs_u0(eqs::Vector{Equation}, dvs::Vector{Term{Real, Base.ImmutableDict{DataType, Any}}}, u0::Vector{Float64}; check_length::Bool, kwargs::Base.Pairs{Symbol, Bool, Tuple{Symbol}, NamedTuple{(:has_difference,), Tuple{Bool}}})
   @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/abstractsystem.jl:1332
 [2] 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, kwargs::Base.Pairs{Symbol, Bool, Tuple{Symbol, Symbol}, NamedTuple{(:has_difference, :check_length), Tuple{Bool, Bool}}})
   @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:553
 [3] (ODEProblem{true})(sys::ODESystem, u0map::Vector{Any}, tspan::Tuple{Float64, Float64}, parammap::SciMLBase.NullParameters; callback::Nothing, check_length::Bool, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:635
 [4] (ODEProblem{true})(sys::ODESystem, u0map::Vector{Any}, tspan::Tuple{Float64, Float64}, parammap::SciMLBase.NullParameters) (repeats 2 times)
   @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:630
 [5] ODEProblem(::ODESystem, ::Vector{Any}, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:627
 [6] ODEProblem(::ODESystem, ::Vector{Any}, ::Vararg{Any})
   @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:626
 [7] top-level scope
   @ ~/.julia/dev/ControlSystemsMTK/test/runtests.jl:37

It would be nice if we could give a better error message here

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions