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

Constructing an ODEProblem with a non-numeric parameter throws a Float conversion error.

Open jedforrest opened this issue 2 years ago • 1 comments

Constructing an ODEProblem with a non-numeric parameter value (i.e. a function) throws a Float conversion error.

I'm using Julia 1.7.2 and ModelingToolkit v8.11.5. This code worked on a previous version of MTK which was (I believe) v8.7.

MWE:

@parameters t, u(t)
@variables x(t)
D = Differential(t)

eqs = [D(x) ~ u]

F(t) = 1 + t

@named sys = ODESystem(eqs)
prob = ODEProblem(sys, [0], (0.0, 2.0), [u => F])

ERROR: MethodError: no method matching AbstractFloat(::Type{typeof(F)})
Closest candidates are:
  (::Type{T})(::SymbolicUtils.Symbolic) where T<:Union{AbstractFloat, Integer, Complex{<:Integer}, Complex{<:AbstractFloat}} at C:\Users\jedfo\.julia\packages\Symbolics\jplLc\src\Symbolics.jl:137
  (::Type{T})(::AbstractChar) where T<:Union{AbstractChar, Number} at C:\Users\jedfo\AppData\Local\Programs\Julia-1.7.2\share\julia\base\char.jl:50
  (::Type{T})(::Base.TwicePrecision) where T<:Number at C:\Users\jedfo\AppData\Local\Programs\Julia-1.7.2\share\julia\base\twiceprecision.jl:255  ...
Stacktrace:
 [1] float(x::Type)
   @ Base .\float.jl:269
 [2] promote_to_concrete(vs::Vector{typeof(F)}; tofloat::Bool, use_union::Bool)
   @ ModelingToolkit C:\Users\jedfo\.julia\packages\ModelingToolkit\qKYfn\src\utils.jl:480
 [3] varmap_to_vars(varmap::Vector{Pair{Num, typeof(F)}}, varlist::Vector{Term{Real, Base.ImmutableDict{DataType, Any}}}; defaults::Dict{Any, Any}, check::Bool, toterm::Function, promotetoconcrete::Nothing, tofloat::Bool, use_union::Bool)
   @ ModelingToolkit C:\Users\jedfo\.julia\packages\ModelingToolkit\qKYfn\src\variables.jl:64
 [4] process_DEProblem(constructor::Type, sys::ODESystem, u0map::Vector{Int64}, parammap::Vector{Pair{Num, typeof(F)}}; 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 C:\Users\jedfo\.julia\packages\ModelingToolkit\qKYfn\src\systems\diffeqs\abstractodesystem.jl:614
 [5] (ODEProblem{true})(sys::ODESystem, u0map::Vector{Int64}, tspan::Tuple{Float64, Float64}, parammap::Vector{Pair{Num, typeof(F)}}; callback::Nothing, check_length::Bool, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ ModelingToolkit C:\Users\jedfo\.julia\packages\ModelingToolkit\qKYfn\src\systems\diffeqs\abstractodesystem.jl:705
 [6] (ODEProblem{true})(sys::ODESystem, u0map::Vector{Int64}, tspan::Tuple{Float64, Float64}, parammap::Vector{Pair{Num, typeof(F)}})
   @ ModelingToolkit C:\Users\jedfo\.julia\packages\ModelingToolkit\qKYfn\src\systems\diffeqs\abstractodesystem.jl:704
 [7] ODEProblem(::ODESystem, ::Vector{Int64}, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ ModelingToolkit C:\Users\jedfo\.julia\packages\ModelingToolkit\qKYfn\src\systems\diffeqs\abstractodesystem.jl:682
 [8] ODEProblem(::ODESystem, ::Vector{Int64}, ::Vararg{Any})
   @ ModelingToolkit C:\Users\jedfo\.julia\packages\ModelingToolkit\qKYfn\src\systems\diffeqs\abstractodesystem.jl:682
 [9] top-level scope
   @ c:\Users\jedfo\.julia\dev\BondGraphs\test\simulation_tests.jl:254 

jedforrest avatar May 23 '22 04:05 jedforrest

Although you can get the problem creation to work with use_union=true, it will error in the run-time generated function.

ValentinKaisermayer avatar May 23 '22 08:05 ValentinKaisermayer

Different parameter types is now handled in v9

ChrisRackauckas avatar Feb 22 '24 18:02 ChrisRackauckas