SciML / diffeqpy

Solving differential equations in Python using DifferentialEquations.jl and the SciML Scientific Machine Learning organization
MIT License
531 stars 39 forks source link

MethodError: no method matching Sundials.NVector(::Vector{Real}) #111

Closed yagzo closed 11 months ago

yagzo commented 1 year ago

Hello,

I am testing the following example code according to this documentation https://sciml.ai/news/2018/04/30/Jupyter/

from diffeqpy import de

def f(du,u,p,t): resid1 = - 0.04u[0] + 1e4u[1]u[2] - du[0] resid2 = + 0.04u[0] - 3e7*u[1]*2 - 1e4u[1]*u[2] - du[1] resid3 = u[0] + u[1] + u[2] - 1.0 return [resid1,resid2,resid3]

u0 = [1.0, 0, 0] du0 = [-0.04, 0.04, 0.0] tspan = (0.0,100000.0) differential_vars = [True,True,False] prob = de.DAEProblem(f,du0,u0,tspan,differential_vars=differential_vars) sol = de.solve(prob)

Python: version 3.11.1 (built by pyenv) Julia: Julia Version 1.8.5 Commit 17cfb8e65ea (2023-01-08 06:45 UTC) Platform Info: OS: Linux (x86_64-linux-gnu) CPU: 8 × Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz WORD_SIZE: 64 LIBM: libopenlibm LLVM: libLLVM-13.0.1 (ORCJIT, skylake) Threads: 1 on 8 virtual cores OS: WSL2

This is the error message that I've got. I have no clue why this happened, can you please help me?

Traceback (most recent call last): sol = de.solve(prob) ^^^^^^^^^^^^^^ RuntimeError: <PyCall.jlwrap (in a Julia function called from Python) JULIA: MethodError: no method matching Sundials.NVector(::Vector{Real}) Closest candidates are: Sundials.NVector(!Matched::Ptr{Sundials._generic_N_Vector}) at ~/.julia/packages/Sundials/639C2/src/nvector_wrapper.jl:24 Sundials.NVector(!Matched::Vector{Float64}) at ~/.julia/packages/Sundials/639C2/src/nvector_wrapper.jl:16 Stacktrace: [1] init(prob::SciMLBase.DAEProblem{Vector{Real}, Vector{Float64}, Tuple{Float64, Float64}, false, SciMLBase.NullParameters, SciMLBase.DAEFunction{false, SciMLBase.FullSpecialize, PyObject, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing, Nothing}, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Vector{Bool}}, alg::Sundials.IDA{:Dense, Nothing, Nothing}, timeseries::Vector{Any}, ts::Vector{Any}, ks::Vector{Any}; verbose::Bool, dt::Nothing, dtmax::Float64, save_on::Bool, save_start::Bool, callback::Nothing, abstol::Float64, reltol::Float64, saveat::Vector{Float64}, tstops::Vector{Float64}, maxiters::Int64, timeseries_errors::Bool, dense_errors::Bool, save_everystep::Bool, save_idxs::Nothing, dense::Bool, save_timeseries::Nothing, save_end::Bool, progress::Bool, progress_name::String, progress_message::typeof(DiffEqBase.ODE_DEFAULT_PROG_MESSAGE), advance_to_tstop::Bool, stop_at_next_tstop::Bool, userdata::Nothing, kwargs::Base.Pairs{Symbol, Bool, Tuple{Symbol, Symbol}, NamedTuple{(:default_set, :second_time), Tuple{Bool, Bool}}}) @ Sundials ~/.julia/packages/Sundials/639C2/src/common_interface/solve.jl:1114 [2] solve(prob::SciMLBase.DAEProblem{Vector{Real}, Vector{Float64}, Tuple{Float64, Float64}, false, SciMLBase.NullParameters, SciMLBase.DAEFunction{false, SciMLBase.FullSpecialize, PyObject, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing, Nothing}, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Vector{Bool}}, alg::Sundials.IDA{:Dense, Nothing, Nothing}, timeseries::Vector{Any}, ts::Vector{Any}, ks::Vector{Any}, recompile::Type{Val{true}}; calculate_error::Bool, kwargs::Base.Pairs{Symbol, Bool, Tuple{Symbol, Symbol}, NamedTuple{(:default_set, :second_time), Tuple{Bool, Bool}}}) @ Sundials ~/.julia/packages/Sundials/639C2/src/common_interface/solve.jl:15 [3] solve(::SciMLBase.DAEProblem{Vector{Real}, Vector{Float64}, Tuple{Float64, Float64}, false, SciMLBase.NullParameters, SciMLBase.DAEFunction{false, SciMLBase.FullSpecialize, PyObject, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing, Nothing}, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Vector{Bool}}, ::Nothing; default_set::Bool, kwargs::Base.Pairs{Symbol, Bool, Tuple{Symbol}, NamedTuple{(:second_time,), Tuple{Bool}}}) @ DifferentialEquations ~/.julia/packages/DifferentialEquations/yS3VA/src/default_solve.jl:9 [4] solve(::SciMLBase.DAEProblem{Vector{Real}, Vector{Float64}, Tuple{Float64, Float64}, false, SciMLBase.NullParameters, SciMLBase.DAEFunction{false, SciMLBase.FullSpecialize, PyObject, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing, Nothing}, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Vector{Bool}}; default_set::Bool, second_time::Bool, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}) @ DiffEqBase ~/.julia/packages/DiffEqBase/qc2JZ/src/solve.jl:1181 [5] __solve(::SciMLBase.DAEProblem{Vector{Real}, Vector{Float64}, Tuple{Float64, Float64}, false, SciMLBase.NullParameters, SciMLBase.DAEFunction{false, SciMLBase.FullSpecialize, PyObject, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing, Nothing}, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Vector{Bool}}) @ DiffEqBase ~/.julia/packages/DiffEqBase/qc2JZ/src/solve.jl:1174 [6] solve_call(::SciMLBase.DAEProblem{Vector{Real}, Vector{Float64}, Tuple{Float64, Float64}, false, SciMLBase.NullParameters, SciMLBase.DAEFunction{false, SciMLBase.FullSpecialize, PyObject, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing, Nothing}, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Vector{Bool}}; merge_callbacks::Bool, kwargshandle::DiffEqBase.KeywordArgError, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}) @ DiffEqBase ~/.julia/packages/DiffEqBase/qc2JZ/src/solve.jl:494 [7] solve_call(::SciMLBase.DAEProblem{Vector{Real}, Vector{Float64}, Tuple{Float64, Float64}, false, SciMLBase.NullParameters, SciMLBase.DAEFunction{false, SciMLBase.FullSpecialize, PyObject, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing, Nothing}, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Vector{Bool}}) @ DiffEqBase ~/.julia/packages/DiffEqBase/qc2JZ/src/solve.jl:464 [8] solve_up(::SciMLBase.DAEProblem{Vector{Real}, Vector{Float64}, Tuple{Float64, Float64}, false, SciMLBase.NullParameters, SciMLBase.DAEFunction{false, SciMLBase.FullSpecialize, PyObject, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing, Nothing}, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Vector{Bool}}, ::Nothing, ::Vector{Real}, ::SciMLBase.NullParameters; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}) @ DiffEqBase ~/.julia/packages/DiffEqBase/qc2JZ/src/solve.jl:919 [9] solve_up(::SciMLBase.DAEProblem{Vector{Real}, Vector{Float64}, Tuple{Float64, Float64}, false, SciMLBase.NullParameters, SciMLBase.DAEFunction{false, SciMLBase.FullSpecialize, PyObject, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing, Nothing}, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Vector{Bool}}, ::Nothing, ::Vector{Real}, ::SciMLBase.NullParameters) @ DiffEqBase ~/.julia/packages/DiffEqBase/qc2JZ/src/solve.jl:888 [10] solve(::SciMLBase.DAEProblem{Vector{Real}, Vector{Float64}, Tuple{Float64, Float64}, false, SciMLBase.NullParameters, SciMLBase.DAEFunction{false, SciMLBase.FullSpecialize, PyObject, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing, Nothing}, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Vector{Bool}}; sensealg::Nothing, u0::Nothing, p::Nothing, wrap::Val{true}, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}) @ DiffEqBase ~/.julia/packages/DiffEqBase/qc2JZ/src/solve.jl:825 [11] solve(::SciMLBase.DAEProblem{Vector{Real}, Vector{Float64}, Tuple{Float64, Float64}, false, SciMLBase.NullParameters, SciMLBase.DAEFunction{false, SciMLBase.FullSpecialize, PyObject, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing, Nothing}, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Vector{Bool}}) @ DiffEqBase ~/.julia/packages/DiffEqBase/qc2JZ/src/solve.jl:815 [12] (::PyCall.var"#64#70"{PyCall.var"#64#65#71"{typeof(CommonSolve.solve)}})(args::SciMLBase.DAEProblem{Vector{Real}, Vector{Float64}, Tuple{Float64, Float64}, false, SciMLBase.NullParameters, SciMLBase.DAEFunction{false, SciMLBase.FullSpecialize, PyObject, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing, Nothing}, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Vector{Bool}}; kws::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}) @ PyCall ~/.julia/packages/PyCall/twYvK/src/callback.jl:98 [13] (::PyCall.var"#64#70"{PyCall.var"#64#65#71"{typeof(CommonSolve.solve)}})(args::SciMLBase.DAEProblem{Vector{Real}, Vector{Float64}, Tuple{Float64, Float64}, false, SciMLBase.NullParameters, SciMLBase.DAEFunction{false, SciMLBase.FullSpecialize, PyObject, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing, Nothing}, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Vector{Bool}}) @ PyCall ~/.julia/packages/PyCall/twYvK/src/callback.jl:98 [14] (::PyCall.FuncWrapper{Tuple{Vararg{PyAny}}, PyCall.var"#64#70"{PyCall.var"#64#65#71"{typeof(CommonSolve.solve)}}})(args::SciMLBase.DAEProblem{Vector{Real}, Vector{Float64}, Tuple{Float64, Float64}, false, SciMLBase.NullParameters, SciMLBase.DAEFunction{false, SciMLBase.FullSpecialize, PyObject, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing, Nothing}, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Vector{Bool}}; kws::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}) @ PyCall ~/.julia/packages/PyCall/twYvK/src/callback.jl:56 [15] (::PyCall.FuncWrapper{Tuple{Vararg{PyAny}}, PyCall.var"#64#70"{PyCall.var"#64#65#71"{typeof(CommonSolve.solve)}}})(args::SciMLBase.DAEProblem{Vector{Real}, Vector{Float64}, Tuple{Float64, Float64}, false, SciMLBase.NullParameters, SciMLBase.DAEFunction{false, SciMLBase.FullSpecialize, PyObject, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing, Nothing}, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Vector{Bool}}) @ PyCall ~/.julia/packages/PyCall/twYvK/src/callback.jl:56 [16] invokelatest(::Any, ::Any, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}) @ Base ./essentials.jl:729 [17] invokelatest(::Any, ::Any, ::Vararg{Any}) @ Base ./essentials.jl:726 [18] _pyjlwrapcall(f::PyCall.FuncWrapper{Tuple{Vararg{PyAny}}, PyCall.var"#64#70"{PyCall.var"#64#65#71"{typeof(CommonSolve.solve)}}}, args::Ptr{PyCall.PyObjectstruct}, kw::Ptr{PyCall.PyObject_struct}) @ PyCall ~/.julia/packages/PyCall/twYvK/src/callback.jl:28 [19] pyjlwrapcall(self::Ptr{PyCall.PyObjectstruct}, args::Ptr{PyCall.PyObjectstruct}, kw::Ptr{PyCall.PyObject_struct}) @ PyCall ~/.julia/packages/PyCall/twYvK/src/callback.jl:44>

ChrisRackauckas commented 11 months ago
from diffeqpy import de

def f(du,u,p,t):
  du[1] = - 0.04u[0] + 1e4u[1]u[2] - du[0]
  du[2] = + 0.04u[0] - 3e7u[1]**2 - 1e4u[1]*u[2] - du[1]
  du[3] = u[0] + u[1] + u[2] - 1.0
  return

u0 = [1.0, 0, 0]
du0 = [-0.04, 0.04, 0.0]
tspan = (0.0,100000.0)
differential_vars = [True,True,False]
prob = de.DAEProblem(f,du0,u0,tspan,differential_vars=differential_vars)
sol = de.solve(prob)

should be it. @LilithHafner did your changes cover the DAEProblems as well?

LilithHafner commented 11 months ago

Yes, f, u0, and du0 all pass through the appropriate conversions. However, kwargs does not.

I think the issue is that u0 is to a Vector{Real} because it contains non-homogonous number types. We should promote.

>>> u0 = [1.0, 0, 0]
>>> de.SciMLBase.prepare_initial_state(u0)
Julia:
3-element Vector{Real}:
 1.0
 0
 0
>>> u0 = [1.0, 0.0, 0.0]
>>> de.SciMLBase.prepare_initial_state(u0)
Julia:
3-element Vector{Float64}:
 1.0
 0.0
 0.0
ChrisRackauckas commented 11 months ago
from diffeqpy import de

def f(resid,du,u,p,t):
    resid[0] = - 0.04*u[0] + 1e4*u[1]*u[2] - du[0]
    resid[1] = + 0.04*u[0] - 3e7*u[1]**2 - 1e4*u[1]*u[2] - du[1]
    resid[2] = u[0] + u[1] + u[2] - 1.0
    return

u0 = [1.0, 0, 0]
du0 = [-0.04, 0.04, 0.0]
tspan = (0.0,100000.0)
differential_vars = [True,True,False]
prob = de.DAEProblem(f,du0,u0,tspan,differential_vars=differential_vars)
sol = de.solve(prob)

that works now