Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remake does not handle dual type promotion with dummy derivatives #3336

Open
hersle opened this issue Jan 17, 2025 · 4 comments
Open

Remake does not handle dual type promotion with dummy derivatives #3336

hersle opened this issue Jan 17, 2025 · 4 comments
Labels
bug Something isn't working

Comments

@hersle
Copy link
Contributor

hersle commented Jan 17, 2025

Here is a simple throw-ball-straight-up-into-the-air example (2nd order ODE)

using ModelingToolkit, OrdinaryDiffEq, NonlinearSolve
using ModelingToolkit: t_nounits as t, D_nounits as D

# Throw ball straight up into the air
@variables y(t)
@named sys = ODESystem([
    D(D(y)) ~ -9.81
], t; initialization_eqs = [
    y^2 ~ 0 # initialize y = 0 in a way that builds an initialization problem
])
sys = structural_simplify(sys)

# Find initial throw velocity that reaches exactly 100 m after 1 s
dprob0 = ODEProblem(sys, [D(y) => NaN], (0.0, 1.0), []; guesses = [y => 0.0])
nprob = NonlinearProblem((ics, _) -> begin
    dprob = remake(dprob0, u0 = Dict(D(y) => ics[1]))
    dsol = solve(dprob, Tsit5())
    return [dsol[y][end] - 100.0]
end, [1.0])
nsol = solve(nprob, NewtonRaphson())

This crashes with

ERROR: MethodError: no method matching Float64(::ForwardDiff.Dual{ForwardDiff.Tag{DifferentiationInterface.FixTail{…}, Float64}, Float64, 1})       
The type `Float64` exists, but no method is defined for this combination of argument types when trying to construct it.

Closest candidates are:
  (::Type{T})(::Real, ::RoundingMode) where T<:AbstractFloat
   @ Base rounding.jl:265
  (::Type{T})(::T) where T<:Number
   @ Core boot.jl:900
  Float64(::Float16)
   @ Base float.jl:342
  ...

Stacktrace:
  [1] #1117
    @ C:\Users\herma\.julia\dev\ModelingToolkit\src\systems\diffeqs\abstractodesystem.jl:1386 [inlined]
  [2] (::ModelingToolkit.var"#1117#1120")(::Pair{Any, Any})
    @ ModelingToolkit .\none:0
  [3] iterate
    @ .\generator.jl:48 [inlined]
  [4] grow_to!(dest::Dict{Any, Any}, itr::Base.Generator{Dict{Any, Any}, ModelingToolkit.var"#1117#1120"})
    @ Base .\abstractdict.jl:587
  [5] dict_with_eltype
    @ .\abstractdict.jl:641 [inlined]
  [6] Dict
    @ .\dict.jl:117 [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 C:\Users\herma\.julia\dev\ModelingToolkit\src\systems\diffeqs\abstractodesystem.jl:1385
  [8] InitializationProblem
    @ C:\Users\herma\.julia\dev\ModelingToolkit\src\systems\diffeqs\abstractodesystem.jl:1287 [inlined]
  [9] #_#1112
    @ C:\Users\herma\.julia\dev\ModelingToolkit\src\systems\diffeqs\abstractodesystem.jl:1265 [inlined]
 [10] InitializationProblem
    @ C:\Users\herma\.julia\dev\ModelingToolkit\src\systems\diffeqs\abstractodesystem.jl:1264 [inlined]
 [11] #InitializationProblem#1110
    @ C:\Users\herma\.julia\dev\ModelingToolkit\src\systems\diffeqs\abstractodesystem.jl:1253 [inlined]
 [12] InitializationProblem
    @ C:\Users\herma\.julia\dev\ModelingToolkit\src\systems\diffeqs\abstractodesystem.jl:1252 [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 C:\Users\herma\.julia\dev\ModelingToolkit\src\systems\problem_utils.jl:546
 [14] remake_initialization_data(sys::ODESystem, odefn::Function, u0::Dict{…}, t0::Float64, p::Missing, newu0::Vector{…}, newp::MTKParameters{…})   
    @ ModelingToolkit C:\Users\herma\.julia\dev\ModelingToolkit\src\systems\nonlinear\initializesystem.jl:343
 [15] remake(prob::ODEProblem{…}; f::Missing, u0::Dict{…}, tspan::Missing, p::Missing, kwargs::Missing, interpret_symbolicmap::Bool, build_initializeprob::Bool, use_defaults::Bool, lazy_initialization::Nothing, _kwargs::@Kwargs{})
    @ SciMLBase C:\Users\herma\.julia\packages\SciMLBase\XzPx0\src\remake.jl:230
  ...
@hersle hersle added the bug Something isn't working label Jan 17, 2025
@hersle
Copy link
Contributor Author

hersle commented Jan 17, 2025

I think the issue is here:

  • fullmap contains Differential(t)(y(t)) (without dummy derivative)
  • unknowns(isys) and/or observed(isys) contains y_t(t) (with dummy derivative)
  • These two are not found to match, so u0T does not promote to the type of D(y)

@hersle

This comment has been minimized.

@hersle

This comment has been minimized.

@hersle
Copy link
Contributor Author

hersle commented Jan 17, 2025

The above PR seems to fix this particular example.

I will open another issue for my other issue, which seems unrelated to dummy derivatives upon further investigation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant