ModelingToolkit.jl icon indicating copy to clipboard operation
ModelingToolkit.jl copied to clipboard

Tutorial for Spring-Mass-System resulting in Stack Overflow Error

Open Tyde opened this issue 3 years ago • 7 comments

I've tried to learn ModelingToolkit with the official Docs and followed this tutorial: https://mtk.sciml.ai/stable/tutorials/spring_mass/

When I execute the line sol = solve(prob, Rosenbrock23()) it results in the following Error

ERROR: LoadError: StackOverflowError:
Stacktrace:
  [1] recursive_unitless_bottom_eltype(a::Type{Any}) (repeats 65088 times)
    @ RecursiveArrayTools D:\julia\packages\RecursiveArrayTools\F84UB\src\utils.jl:96
  [2] recursive_unitless_bottom_eltype(a::Type{Vector{Term{Float64, Nothing}}})
    @ RecursiveArrayTools D:\julia\packages\RecursiveArrayTools\F84UB\src\utils.jl:97
  [3] recursive_unitless_bottom_eltype(a::Vector{Term{Float64, Nothing}})
    @ RecursiveArrayTools D:\julia\packages\RecursiveArrayTools\F84UB\src\utils.jl:95
  [4] __init(prob::ODEProblem{Vector{Term{Float64, Nothing}}, Tuple{Float64, Float64}, true, Vector{Float64}, ODEFunction{true, ModelingToolkit.var"#f#253"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x4d93cbef, 0x86acfa5b, 0xfafef8e8, 0x225e5d28, 0x71dcb80d)}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x521ca928, 0xb4640c53, 0x1fa99273, 0x301f0973, 0x0f8e1ab7)}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Vector{Symbol}, Symbol, ModelingToolkit.var"#239#generated_observed#260"{Bool, ODESystem, Dict{Any, Any}}, Nothing}, Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}, alg::Rosenbrock23{4, true, DefaultLinSolve, Val{:forward}}, timeseries_init::Tuple{}, ts_init::Tuple{}, ks_init::Tuple{}, recompile::Type{Val{true}}; saveat::Tuple{}, tstops::Tuple{}, d_discontinuities::Tuple{}, save_idxs::Nothing, save_everystep::Bool, save_on::Bool, save_start::Bool, save_end::Nothing, callback::Nothing, dense::Bool, calck::Bool, dt::Float64, dtmin::Nothing, dtmax::Float64, force_dtmin::Bool, adaptive::Bool, gamma::Rational{Int64}, abstol::Nothing, reltol::Nothing, qmin::Rational{Int64}, qmax::Int64, qsteady_min::Int64, qsteady_max::Rational{Int64}, beta1::Nothing, beta2::Nothing, qoldinit::Rational{Int64}, controller::Nothing, fullnormalize::Bool, failfactor::Int64, maxiters::Int64, internalnorm::typeof(DiffEqBase.ODE_DEFAULT_NORM), internalopnorm::typeof(opnorm), isoutofdomain::typeof(DiffEqBase.ODE_DEFAULT_ISOUTOFDOMAIN), unstable_check::typeof(DiffEqBase.ODE_DEFAULT_UNSTABLE_CHECK), verbose::Bool, timeseries_errors::Bool, dense_errors::Bool, advance_to_tstop::Bool, stop_at_next_tstop::Bool, initialize_save::Bool, progress::Bool, progress_steps::Int64, progress_name::String, progress_message::typeof(DiffEqBase.ODE_DEFAULT_PROG_MESSAGE), userdata::Nothing, allow_extrapolation::Bool, initialize_integrator::Bool, alias_u0::Bool, alias_du0::Bool, initializealg::OrdinaryDiffEq.DefaultInit, kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ OrdinaryDiffEq D:\julia\packages\OrdinaryDiffEq\JsAS0\src\solve.jl:150
  [5] __init(prob::ODEProblem{Vector{Term{Float64, Nothing}}, Tuple{Float64, Float64}, true, Vector{Float64}, ODEFunction{true, ModelingToolkit.var"#f#253"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x4d93cbef, 0x86acfa5b, 0xfafef8e8, 0x225e5d28, 0x71dcb80d)}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x521ca928, 0xb4640c53, 0x1fa99273, 0x301f0973, 0x0f8e1ab7)}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Vector{Symbol}, Symbol, ModelingToolkit.var"#239#generated_observed#260"{Bool, ODESystem, Dict{Any, Any}}, Nothing}, Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}, alg::Rosenbrock23{4, true, DefaultLinSolve, Val{:forward}}, timeseries_init::Tuple{}, ts_init::Tuple{}, ks_init::Tuple{}, recompile::Type{Val{true}}) (repeats 5 times)
    @ OrdinaryDiffEq D:\julia\packages\OrdinaryDiffEq\JsAS0\src\solve.jl:67
  [6] __solve(::ODEProblem{Vector{Term{Float64, Nothing}}, Tuple{Float64, Float64}, true, Vector{Float64}, ODEFunction{true, ModelingToolkit.var"#f#253"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x4d93cbef, 0x86acfa5b, 0xfafef8e8, 0x225e5d28, 0x71dcb80d)}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x521ca928, 0xb4640c53, 0x1fa99273, 0x301f0973, 0x0f8e1ab7)}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Vector{Symbol}, Symbol, ModelingToolkit.var"#239#generated_observed#260"{Bool, ODESystem, Dict{Any, Any}}, Nothing}, Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}, ::Rosenbrock23{4, true, DefaultLinSolve, Val{:forward}}; kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ OrdinaryDiffEq D:\julia\packages\OrdinaryDiffEq\JsAS0\src\solve.jl:4
  [7] __solve(::ODEProblem{Vector{Term{Float64, Nothing}}, Tuple{Float64, Float64}, true, Vector{Float64}, ODEFunction{true, ModelingToolkit.var"#f#253"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x4d93cbef, 0x86acfa5b, 0xfafef8e8, 0x225e5d28, 0x71dcb80d)}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x521ca928, 0xb4640c53, 0x1fa99273, 0x301f0973, 0x0f8e1ab7)}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Vector{Symbol}, Symbol, ModelingToolkit.var"#239#generated_observed#260"{Bool, ODESystem, Dict{Any, Any}}, Nothing}, Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}, ::Rosenbrock23{4, true, DefaultLinSolve, Val{:forward}})
    @ OrdinaryDiffEq D:\julia\packages\OrdinaryDiffEq\JsAS0\src\solve.jl:4
  [8] solve_call(_prob::ODEProblem{Vector{Term{Float64, Nothing}}, Tuple{Float64, Float64}, true, Vector{Float64}, ODEFunction{true, ModelingToolkit.var"#f#253"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x4d93cbef, 0x86acfa5b, 0xfafef8e8, 0x225e5d28, 0x71dcb80d)}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x521ca928, 0xb4640c53, 0x1fa99273, 0x301f0973, 0x0f8e1ab7)}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Vector{Symbol}, Symbol, ModelingToolkit.var"#239#generated_observed#260"{Bool, ODESystem, Dict{Any, Any}}, Nothing}, Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}, args::Rosenbrock23{4, true, DefaultLinSolve, Val{:forward}}; merge_callbacks::Bool, kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ DiffEqBase D:\julia\packages\DiffEqBase\b1nST\src\solve.jl:61
  [9] solve_call(_prob::ODEProblem{Vector{Term{Float64, Nothing}}, Tuple{Float64, Float64}, true, Vector{Float64}, ODEFunction{true, ModelingToolkit.var"#f#253"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x4d93cbef, 0x86acfa5b, 0xfafef8e8, 0x225e5d28, 0x71dcb80d)}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x521ca928, 0xb4640c53, 0x1fa99273, 0x301f0973, 0x0f8e1ab7)}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Vector{Symbol}, Symbol, ModelingToolkit.var"#239#generated_observed#260"{Bool, ODESystem, Dict{Any, Any}}, Nothing}, Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}, args::Rosenbrock23{4, true, DefaultLinSolve, Val{:forward}})
    @ DiffEqBase D:\julia\packages\DiffEqBase\b1nST\src\solve.jl:48
 [10] solve_up(prob::ODEProblem{Vector{Term{Float64, Nothing}}, Tuple{Float64, Float64}, true, Vector{Float64}, ODEFunction{true, ModelingToolkit.var"#f#253"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Vector{Symbol}, Symbol, ModelingToolkit.var"#239#generated_observed#260"{Bool, ODESystem, Dict{Any, Any}}, Nothing}, Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}, sensealg::Nothing, u0::Vector{Term{Float64, Nothing}}, p::Vector{Float64}, args::Rosenbrock23{0, true, DefaultLinSolve, Val{:forward}}; kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ DiffEqBase D:\julia\packages\DiffEqBase\b1nST\src\solve.jl:87
 [11] solve_up(prob::ODEProblem{Vector{Term{Float64, Nothing}}, Tuple{Float64, Float64}, true, Vector{Float64}, ODEFunction{true, ModelingToolkit.var"#f#253"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x4d93cbef, 0x86acfa5b, 0xfafef8e8, 0x225e5d28, 0x71dcb80d)}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x521ca928, 0xb4640c53, 0x1fa99273, 0x301f0973, 0x0f8e1ab7)}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Vector{Symbol}, Symbol, ModelingToolkit.var"#239#generated_observed#260"{Bool, ODESystem, Dict{Any, Any}}, Nothing}, Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}, sensealg::Nothing, u0::Vector{Term{Float64, Nothing}}, p::Vector{Float64}, args::Rosenbrock23{0, true, DefaultLinSolve, Val{:forward}})
    @ DiffEqBase D:\julia\packages\DiffEqBase\b1nST\src\solve.jl:78
 [12] solve(prob::ODEProblem{Vector{Term{Float64, Nothing}}, Tuple{Float64, Float64}, true, Vector{Float64}, ODEFunction{true, ModelingToolkit.var"#f#253"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x4d93cbef, 0x86acfa5b, 0xfafef8e8, 0x225e5d28, 0x71dcb80d)}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x521ca928, 0xb4640c53, 0x1fa99273, 0x301f0973, 0x0f8e1ab7)}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Vector{Symbol}, Symbol, ModelingToolkit.var"#239#generated_observed#260"{Bool, ODESystem, Dict{Any, Any}}, Nothing}, Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}, args::Rosenbrock23{0, true, DefaultLinSolve, Val{:forward}}; sensealg::Nothing, u0::Nothing, p::Nothing, kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ DiffEqBase D:\julia\packages\DiffEqBase\b1nST\src\solve.jl:73

This error appears for me also if I just use the copy paste example.

Is that an issue with my configuration or a bug?

Tyde avatar Nov 19 '21 10:11 Tyde

Here's a small example that results in the same error

using ModelingToolkit, OrdinaryDiffEq
@variables t
function UnitDelay(dt; name)
    @variables u(t)=0 [input=true] y(t)=0 [output=true]
    Dₜ = Difference(t; dt=dt, update=true)
    eqs = [
        Dₜ(y) ~ u
    ]
    DiscreteSystem(eqs, t, name=name)
end

@named int = UnitDelay(1)
@named iosys = DiscreteSystem([int.u~1], t, systems=[int])
# sys = structural_simplify(iosys) # broken
prob = DiscreteProblem(iosys, Pair[int.u=>t], (0.0, 10.0))
sol = solve(prob, FunctionMap())

baggepinnen avatar Nov 25 '21 10:11 baggepinnen

u0 isn't turning into an Array{Float64} for some reason.

ChrisRackauckas avatar Nov 25 '21 12:11 ChrisRackauckas

My example is flawed, the Pair[int.u=>t] doesn't make sense, the problem in the OP should still be a problem though

baggepinnen avatar Nov 25 '21 12:11 baggepinnen

Yes, and the problem in the OP is that a::Vector{Term{Float64, Nothing}} the initial condition never transformed from symbolic to numerical. So this is something @yingboma might want to look into, and we might want to capture with a test. Though I think from what I've seen before this may have just been a case of old versions.

ChrisRackauckas avatar Nov 25 '21 12:11 ChrisRackauckas

Just installed Julia 1.7.1. on Windows10 and Ubuntu and all the libraries and this still seem to be the case.

A fix for the case is to specify the u0 manually like this:

u0 = [mass.v[1] => 0.0,
      mass.v[2] => 0.0,
      mass.pos[1] => 1.0,
      mass.pos[2] => -1.0,
     ]
prob = ODEProblem(sys, u0, (0., 3.))

sibanc avatar Jan 08 '22 15:01 sibanc

Specifying the initial condition as vectors does not work. It does not scalarize?

u0 = [mass.v => [0.0, 0.0],
     mass.pos => [1.0, -1.0]
    ]

sibanc avatar Jan 08 '22 16:01 sibanc

I'm seeing a possibly related issue where I get this error:

MethodError: Cannot `convert` an object of type SymbolicUtils.Term{Float64, Nothing} to an object of type Float64

Closest candidates are:

convert(::Type{T}, !Matched::AbstractChar) where T<:Number at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/char.jl:185

convert(::Type{T}, !Matched::Base.TwicePrecision) where T<:Number at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/twiceprecision.jl:262

convert(::Type{T}, !Matched::ChainRulesCore.AbstractZero) where T<:Number at ~/.julia/packages/ChainRulesCore/oBjCg/src/tangent_types/abstract_zero.jl:27

...

setindex!(::Vector{Float64}, ::SymbolicUtils.Term{Float64, Nothing}, ::Int64)@array.jl:903
var"#_varmap_to_vars#9"(::Dict{Any, Any}, ::Bool, ::typeof(Symbolics.diff2term), ::typeof(ModelingToolkit._varmap_to_vars), ::Dict{Any, Any}, ::Vector{SymbolicUtils.Term{Real, Base.ImmutableDict{DataType, Any}}})@variables.jl:95
var"#varmap_to_vars#8"(::Dict{Any, Any}, ::Bool, ::Function, ::typeof(ModelingToolkit.varmap_to_vars), ::Vector{Any}, ::Vector{SymbolicUtils.Term{Real, Base.ImmutableDict{DataType, Any}}})@variables.jl:56
var"#process_DEProblem#321"(::Bool, ::Nothing, ::Nothing, ::Bool, ::Bool, ::Bool, ::Bool, ::Bool, ::Bool, ::Symbolics.SerialForm, ::Bool, ::Base.Pairs{Symbol, Bool, Tuple{Symbol}, NamedTuple{(:has_difference,), Tuple{Bool}}}, ::typeof(ModelingToolkit.process_DEProblem), ::Type, ::ModelingToolkit.ODESystem, ::Vector{Any}, ::SciMLBase.NullParameters)@abstractodesystem.jl:581
var"#_#327"(::Nothing, ::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, ::Type{SciMLBase.ODEProblem{true}}, ::ModelingToolkit.ODESystem, ::Vector{Any}, ::Tuple{Float64, Float64}, ::SciMLBase.NullParameters)@abstractodesystem.jl:671
[email protected]:670[inlined]
#ODEProblem#[email protected]:649[inlined]
[email protected]:649[inlined]

at the line when I was trying to define the problem: prob = ODEProblem(sys, [], (0., 3.))

but doing what @sibanc suggested above seemed to fix it. The vector version also does not work for me.

Config details: Julia 1.7.1 on macOS 11.6 running in Pluto (I also get the issue in VS Code). All code directly is the copy/paste spring/mass example. Output of Pkg.status() is:

  [961ee093] ModelingToolkit v8.3.2
  [91a5bcdd] Plots v1.25.6
  [c3e4b0f8] Pluto v0.17.6
  [0c5d862f] Symbolics v4.3.0
  [37e2e46d] LinearAlgebra

rkurchin avatar Jan 21 '22 21:01 rkurchin

Fixed

ChrisRackauckas avatar Feb 22 '24 19:02 ChrisRackauckas