You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When a problem is defined with units, remake cannot change the initial conditions or parameters.
Expected behavior
It should be possible to modify problems with units, hopefully by using units in the new values.
Minimal Reproducible Example 👇
using ModelingToolkit, OrdinaryDiffEq
using ModelingToolkit: t_unitful as t, D_unitful as D
using Unitful
@variablesbeginx(t), [unit=u"m"]
y(t), [unit=u"m"]
end@parametersbegin
total, [unit=u"m", guess=1.0]
τ=1.0, [unit=u"s^-1"]
end@mtkbuild sys =ODESystem([D(x) ~-τ * x, total ~ x + y], t;
defaults = [total =>missing], guesses = [total =>1.0])
prob =ODEProblem(sys, [x =>1.0, y =>2.0], (0.0, 1.0))
The nicest thing would be if this worked, with something like ustrip(getunit(variable), newvalue) used to convert the input value to a plain old floating point number:
julia> prob2 =remake(prob; u0 = [y =>1.0u"m"], p = [total =>4.0u"m"])
ERROR: MethodError: no method matching (Quantity{Float64})(::Float64)
The type `Quantity{Float64}` exists, but no method is defined for this combination of argument types when trying to construct it.
Closest candidates are:
(::Type{T})(::T) where T<:Number
@ Core boot.jl:900
(::Type{<:Unitful.AbstractQuantity})(::Union{Dates.Day, Dates.Hour, Dates.Microsecond, Dates.Millisecond, Dates.Minute, Dates.Nanosecond, Dates.Second, Dates.Week})
@ Unitful ~/.julia/packages/Unitful/dHMk1/src/dates.jl:60
(::Type{<:Unitful.AbstractQuantity})(::Dates.CompoundPeriod)
@ Unitful ~/.julia/packages/Unitful/dHMk1/src/dates.jl:135...
Stacktrace:
[1] remake_buffer(sys::ODEProblem{…}, oldbuffer::Vector{…}, idxs::Base.KeySet{…}, vals::Base.ValueIterator{…})
@ SymbolicIndexingInterface ~/.julia/packages/SymbolicIndexingInterface/rkfQB/src/remake.jl:47
[2] _updated_u0_p_symmap(prob::ODEProblem{…}, u0::Dict{…}, ::Val{…}, p::Dict{…}, ::Val{…}, t0::Float64)
@ SciMLBase ~/.julia/packages/SciMLBase/0ZQSg/src/remake.jl:1059
[3] _updated_u0_p_internal(prob::ODEProblem{…}, u0::Vector{…}, p::Vector{…}, t0::Float64; interpret_symbolicmap::Bool, use_defaults::Bool)
@ SciMLBase ~/.julia/packages/SciMLBase/0ZQSg/src/remake.jl:930
[4] _updated_u0_p_internal
@ ~/.julia/packages/SciMLBase/0ZQSg/src/remake.jl:915 [inlined]
[5] #updated_u0_p#780
@ ~/.julia/packages/SciMLBase/0ZQSg/src/remake.jl:1102 [inlined]
[6] updated_u0_p
@ ~/.julia/packages/SciMLBase/0ZQSg/src/remake.jl:1081 [inlined]
[7] remake(prob::ODEProblem{…}; f::Missing, u0::Vector{…}, tspan::Missing, p::Vector{…}, kwargs::Missing, interpret_symbolicmap::Bool, build_initializeprob::Bool, use_defaults::Bool, lazy_initialization::Nothing, _kwargs::@Kwargs{})
@ SciMLBase ~/.julia/packages/SciMLBase/0ZQSg/src/remake.jl:222
[8] top-level scope
@ REPL[12]:1
Some type information was truncated. Use `show(err)` to see complete types.
Also acceptable but less friendly would be assuming the user knew what they were doing and accepting floats.
julia> prob2 =remake(prob; u0 = [y =>1.0], p = [total =>4.0])
┌ Warning:in eq. #1right, in sum 1.0 - x(t), units [, m] do not match.
└ @ ModelingToolkit.UnitfulUnitCheck /home/russel/.julia/packages/ModelingToolkit/klLLV/src/systems/validation.jl:153
┌ Warning:in eq. #2right, in sum 1.0 - y(t), units [, m] do not match.
└ @ ModelingToolkit.UnitfulUnitCheck /home/russel/.julia/packages/ModelingToolkit/klLLV/src/systems/validation.jl:153
┌ Warning:in eq. #3right, in sum 4.0 - total, units [, m] do not match.
└ @ ModelingToolkit.UnitfulUnitCheck /home/russel/.julia/packages/ModelingToolkit/klLLV/src/systems/validation.jl:153
ERROR: ModelingToolkit.ValidationError("Some equations had invalid units. See warnings for details.")
Stacktrace:
[1] check_units
@ ~/.julia/packages/ModelingToolkit/klLLV/src/systems/validation.jl:278 [inlined]
[2] NonlinearSystem(tag::UInt64, eqs::Vector{…}, unknowns::Vector{…}, ps::Vector{…}, var_to_name::Dict{…}, observed::Vector{…}, jac::Base.RefValue{…}, name::Symbol, description::String, systems::Vector{…}, defaults::Dict{…}, connector_type::Nothing, parameter_dependencies::Vector{…}, metadata::ModelingToolkit.InitializationSystemMetadata, gui_metadata::Nothing, tearing_state::Nothing, substitutions::Nothing, complete::Bool, index_cache::Nothing, parent::Nothing, isscheduled::Bool; checks::Bool)
@ ModelingToolkit ~/.julia/packages/ModelingToolkit/klLLV/src/systems/nonlinear/nonlinearsystem.jl:108
[3] NonlinearSystem
@ ~/.julia/packages/ModelingToolkit/klLLV/src/systems/nonlinear/nonlinearsystem.jl:98 [inlined]
[4] NonlinearSystem(eqs::Vector{…}, unknowns::Vector{…}, ps::Vector{…}; observed::Vector{…}, name::Symbol, description::String, default_u0::Dict{…}, default_p::Dict{…}, defaults::Dict{…}, systems::Vector{…}, connector_type::Nothing, continuous_events::Nothing, discrete_events::Nothing, checks::Bool, parameter_dependencies::Vector{…}, metadata::ModelingToolkit.InitializationSystemMetadata, gui_metadata::Nothing)
@ ModelingToolkit ~/.julia/packages/ModelingToolkit/klLLV/src/systems/nonlinear/nonlinearsystem.jl:166
[5] hash
@ ~/.julia/packages/Symbolics/YbNrd/src/num.jl:174
[6] generate_initializesystem
@ ~/.julia/packages/ModelingToolkit/klLLV/src/systems/nonlinear/initializesystem.jl:6 [inlined]
[7] ModelingToolkit.InitializationProblem{…}(sys::ODESystem, t::Float64, u0map::Dict{…}, parammap::Dict{…}; guesses::Dict{…}, check_length::Bool, warn_initialize_determined::Bool, initialization_eqs::Vector{…}, fully_determined::Nothing, check_units::Bool, use_scc::Bool, kwargs::@Kwargs{})
@ ModelingToolkit ~/.julia/packages/ModelingToolkit/klLLV/src/systems/diffeqs/abstractodesystem.jl:1317
[8] InitializationProblem
@ ~/.julia/packages/ModelingToolkit/klLLV/src/systems/diffeqs/abstractodesystem.jl:1295 [inlined]
[9] #_#1065
@ ~/.julia/packages/ModelingToolkit/klLLV/src/systems/diffeqs/abstractodesystem.jl:1273 [inlined]
[10] InitializationProblem
@ ~/.julia/packages/ModelingToolkit/klLLV/src/systems/diffeqs/abstractodesystem.jl:1272 [inlined]
[11] #InitializationProblem#1063
@ ~/.julia/packages/ModelingToolkit/klLLV/src/systems/diffeqs/abstractodesystem.jl:1261 [inlined]
[12] InitializationProblem
@ ~/.julia/packages/ModelingToolkit/klLLV/src/systems/diffeqs/abstractodesystem.jl:1260 [inlined]
[13] maybe_build_initialization_problem(sys::ODESystem, op::Dict{…}, u0map::Dict{…}, pmap::Dict{…}, t::Float64, defs::Dict{…}, guesses::Dict{…}, missing_unknowns::Set{…}; implicit_dae::Bool, kwargs::@Kwargs{…})
@ ModelingToolkit ~/.julia/packages/ModelingToolkit/klLLV/src/systems/problem_utils.jl:545
[14] remake_initialization_data(sys::ODESystem, odefn::Function, u0::Vector{…}, t0::Float64, p::Vector{…}, newu0::Vector{…}, newp::MTKParameters{…})
@ ModelingToolkit ~/.julia/packages/ModelingToolkit/klLLV/src/systems/nonlinear/initializesystem.jl:336
[15] remake(prob::ODEProblem{…}; f::Missing, u0::Vector{…}, tspan::Missing, p::Vector{…}, kwargs::Missing, interpret_symbolicmap::Bool, build_initializeprob::Bool, use_defaults::Bool, lazy_initialization::Nothing, _kwargs::@Kwargs{})
@ SciMLBase ~/.julia/packages/SciMLBase/0ZQSg/src/remake.jl:230
[16] top-level scope
@ REPL[13]:1
Some type information was truncated. Use `show(err)` to see complete types.
Environment (please complete the following information):
Output of using Pkg; Pkg.status()
(RemakeUnits) pkg> st
Status `~/Desktop/RemakeUnits/Project.toml`
[961ee093] ModelingToolkit v9.59.0
[1dea7af3] OrdinaryDiffEq v6.90.1
[1986cc42] Unitful v1.21.1
Output of using Pkg; Pkg.status(; mode = PKGMODE_MANIFEST)
Describe the bug 🐞
When a problem is defined with units,
remake
cannot change the initial conditions or parameters.Expected behavior
It should be possible to modify problems with units, hopefully by using units in the new values.
Minimal Reproducible Example 👇
The nicest thing would be if this worked, with something like
ustrip(getunit(variable), newvalue)
used to convert the input value to a plain old floating point number:Also acceptable but less friendly would be assuming the user knew what they were doing and accepting floats.
Environment (please complete the following information):
using Pkg; Pkg.status()
using Pkg; Pkg.status(; mode = PKGMODE_MANIFEST)
versioninfo()
Additional context
The text was updated successfully, but these errors were encountered: