Skip to content

Error when defining an ODEProblem via ModelingToolkit #78

Closed
@valentinsulzer

Description

@valentinsulzer

If a de.ODEProblem is setup via MTK's ODESystem interface, it produces a MethodError (MethodError: no method matching one(::Tuple{Operation,Float64}))

Setup

from julia import Main, ModelingToolkit
from diffeqpy import de

Main.eval("""
using ModelingToolkit, DifferentialEquations
""")
Main.eval(
    """
begin
# v -> x1
@variables t, x1(t)
@derivatives D'~t

eqs = [
D(x1) ~ -2 * x1,
]
sys = ODESystem(eqs, t)

u0 = [
x1 => 1.0,
]
end
"""
)

Approach 1: solve using Main.eval (works)

Main.eval(
"""
begin
prob = ODEProblem(sys, u0, (0.0,10.0))
sol = solve(prob)
end
"""
)

Approach 2: define ODEProblem using Main.eval then solve using diffeqpy (works)

Main.eval("prob = ODEProblem(sys, u0, (0.0,10.0))")
de.solve(Main.prob)

Approach 3: define ODEProblem using diffeqpy (fails)

prob = de.ODEProblem(Main.sys, Main.u0, (0.0, 10.0))
de.solve(prob)

or

Main.prob_de = de.ODEProblem(Main.sys, Main.u0, (0.0, 10.0))
Main.eval("solve(prob_de)")

Both fail with

RuntimeError: Julia exception: MethodError: no method matching one(::Tuple{Operation,Float64})
Closest candidates are:
  one(!Matched::Type{Missing}) at missing.jl:103
  one(!Matched::Type{ModelingToolkit.TermCombination}) at /Users/vsulzer/.julia/packages/ModelingToolkit/4JKjD/src/linearity.jl:57
  one(!Matched::Missing) at missing.jl:100
  ...
Stacktrace:
 [1] oneunit(::Tuple{Operation,Float64}) at ./number.jl:299
 [2] _broadcast_getindex_evalf at ./broadcast.jl:648 [inlined]
 [3] _broadcast_getindex at ./broadcast.jl:621 [inlined]
 [4] _getindex at ./broadcast.jl:644 [inlined]
 [5] _broadcast_getindex at ./broadcast.jl:620 [inlined]
 [6] _getindex at ./broadcast.jl:645 [inlined]
 [7] _broadcast_getindex at ./broadcast.jl:620 [inlined]
 [8] getindex at ./broadcast.jl:575 [inlined]
 [9] copy at ./broadcast.jl:876 [inlined]
 [10] materialize at ./broadcast.jl:837 [inlined]
 [11] __init(::ODEProblem{Array{Tuple{Operation,Float64},1},Tuple{Float64,Float64},true,DiffEqBase.NullParameters,ODEFunction{true,DiffEqBase.EvalFunc{ModelingToolkit.var"#f#168"{ModelingToolkit.var"#474#475",ModelingToolkit.var"#476#477"}},LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Array{Symbol,1},Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem}, ::CompositeAlgorithm{Tuple{Vern9,Rodas5{0,false,DefaultLinSolve,DataType}},AutoSwitch{Vern9,Rodas5{0,false,DefaultLinSolve,DataType},Rational{Int64},Int64}}, ::Tuple{}, ::Tuple{}, ::Tuple{}, ::Type{Val{true}}; saveat::Tuple{}, tstops::Tuple{}, d_discontinuities::Tuple{}, save_idxs::Nothing, save_everystep::Bool, save_on::Bool, save_start::Bool, save_end::Bool, 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::Int64, qoldinit::Rational{Int64}, fullnormalize::Bool, failfactor::Int64, beta1::Nothing, beta2::Nothing, maxiters::Int64, internalnorm::typeof(DiffEqBase.ODE_DEFAULT_NORM), internalopnorm::typeof(LinearAlgebra.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{Symbol,Bool,Tuple{Symbol,Symbol},NamedTuple{(:default_set, :second_time),Tuple{Bool,Bool}}}) at /Users/vsulzer/.julia/packages/OrdinaryDiffEq/VPJBD/src/solve.jl:144
 [12] __solve(::ODEProblem{Array{Tuple{Operation,Float64},1},Tuple{Float64,Float64},true,DiffEqBase.NullParameters,ODEFunction{true,DiffEqBase.EvalFunc{ModelingToolkit.var"#f#168"{ModelingToolkit.var"#474#475",ModelingToolkit.var"#476#477"}},LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Array{Symbol,1},Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem}, ::CompositeAlgorithm{Tuple{Vern9,Rodas5{0,false,DefaultLinSolve,DataType}},AutoSwitch{Vern9,Rodas5{0,false,DefaultLinSolve,DataType},Rational{Int64},Int64}}; kwargs::Base.Iterators.Pairs{Symbol,Bool,Tuple{Symbol,Symbol},NamedTuple{(:default_set, :second_time),Tuple{Bool,Bool}}}) at /Users/vsulzer/.julia/packages/OrdinaryDiffEq/VPJBD/src/solve.jl:4
 [13] __solve(::ODEProblem{Array{Tuple{Operation,Float64},1},Tuple{Float64,Float64},true,DiffEqBase.NullParameters,ODEFunction{true,DiffEqBase.EvalFunc{ModelingToolkit.var"#f#168"{ModelingToolkit.var"#474#475",ModelingToolkit.var"#476#477"}},LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Array{Symbol,1},Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem}, ::Nothing; default_set::Bool, kwargs::Base.Iterators.Pairs{Symbol,Bool,Tuple{Symbol},NamedTuple{(:second_time,),Tuple{Bool}}}) at /Users/vsulzer/.julia/packages/DifferentialEquations/fpohE/src/default_solve.jl:7
 [14] #__solve#471 at /Users/vsulzer/.julia/packages/DiffEqBase/T5smF/src/solve.jl:258 [inlined]
 [15] __solve(::ODEProblem{Array{Tuple{Operation,Float64},1},Tuple{Float64,Float64},true,DiffEqBase.NullParameters,ODEFunction{true,DiffEqBase.EvalFunc{ModelingToolkit.var"#f#168"{ModelingToolkit.var"#474#475",ModelingToolkit.var"#476#477"}},LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Array{Symbol,1},Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem}) at /Users/vsulzer/.julia/packages/DiffEqBase/T5smF/src/solve.jl:245
 [16] #invokelatest#1 at ./essentials.jl:710 [inlined]
 [17] invokelatest at ./essentials.jl:709 [inlined]
 [18] solve_call(::ODEProblem{Array{Tuple{Operation,Float64},1},Tuple{Float64,Float64},true,DiffEqBase.NullParameters,ODEFunction{true,DiffEqBase.EvalFunc{ModelingToolkit.var"#f#168"{ModelingToolkit.var"#474#475",ModelingToolkit.var"#476#477"}},LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Array{Symbol,1},Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem}; merge_callbacks::Bool, kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at /Users/vsulzer/.julia/packages/DiffEqBase/T5smF/src/solve.jl:90
 [19] solve_call at /Users/vsulzer/.julia/packages/DiffEqBase/T5smF/src/solve.jl:65 [inlined]
 [20] #solve_up#461 at /Users/vsulzer/.julia/packages/DiffEqBase/T5smF/src/solve.jl:117 [inlined]
 [21] solve_up at /Users/vsulzer/.julia/packages/DiffEqBase/T5smF/src/solve.jl:107 [inlined]
 [22] #solve#460 at /Users/vsulzer/.julia/packages/DiffEqBase/T5smF/src/solve.jl:102 [inlined]
 [23] solve at /Users/vsulzer/.julia/packages/DiffEqBase/T5smF/src/solve.jl:100 [inlined]
 [24] (::PyCall.var"#65#71"{PyCall.var"#65#66#72"{typeof(solve)}})(::ODEProblem{Array{Tuple{Operation,Float64},1},Tuple{Float64,Float64},true,DiffEqBase.NullParameters,ODEFunction{true,DiffEqBase.EvalFunc{ModelingToolkit.var"#f#168"{ModelingToolkit.var"#474#475",ModelingToolkit.var"#476#477"}},LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Array{Symbol,1},Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem}; kws::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at /Users/vsulzer/.julia/packages/PyCall/zqDXB/src/callback.jl:103
 [25] #65 at /Users/vsulzer/.julia/packages/PyCall/zqDXB/src/callback.jl:103 [inlined]
 [26] #_#60 at /Users/vsulzer/.julia/packages/PyCall/zqDXB/src/callback.jl:61 [inlined]
 [27] (::PyCall.FuncWrapper{Tuple{Vararg{PyAny,N} where N},PyCall.var"#65#71"{PyCall.var"#65#66#72"{typeof(solve)}}})(::ODEProblem{Array{Tuple{Operation,Float64},1},Tuple{Float64,Float64},true,DiffEqBase.NullParameters,ODEFunction{true,DiffEqBase.EvalFunc{ModelingToolkit.var"#f#168"{ModelingToolkit.var"#474#475",ModelingToolkit.var"#476#477"}},LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Array{Symbol,1},Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem}) at /Users/vsulzer/.julia/packages/PyCall/zqDXB/src/callback.jl:61
 [28] #invokelatest#1 at ./essentials.jl:710 [inlined]
 [29] invokelatest(::Any, ::Any) at ./essentials.jl:709
 [30] _pyjlwrap_call(::PyCall.FuncWrapper{Tuple{Vararg{PyAny,N} where N},PyCall.var"#65#71"{PyCall.var"#65#66#72"{typeof(solve)}}}, ::Ptr{PyCall.PyObject_struct}, ::Ptr{PyCall.PyObject_struct}) at /Users/vsulzer/.julia/packages/PyCall/zqDXB/src/callback.jl:28
 [31] pyjlwrap_call(::Ptr{PyCall.PyObject_struct}, ::Ptr{PyCall.PyObject_struct}, ::Ptr{PyCall.PyObject_struct}) at /Users/vsulzer/.julia/packages/PyCall/zqDXB/src/callback.jl:49

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions