Skip to content

Fix DiffCacheAllocatorAPIWrapper conversion errors#4455

Merged
AayushSabharwal merged 1 commit intoSciML:masterfrom
SebastianM-C:smc/dcapiw
Apr 16, 2026
Merged

Fix DiffCacheAllocatorAPIWrapper conversion errors#4455
AayushSabharwal merged 1 commit intoSciML:masterfrom
SebastianM-C:smc/dcapiw

Conversation

@SebastianM-C
Copy link
Copy Markdown
Member

@SebastianM-C SebastianM-C commented Apr 15, 2026

Fixes

ERROR: MethodError: Cannot `convert` an object of type ModelingToolkitBase.DiffCacheAllocatorAPIWrapper{Float64} to an object of type PreallocationTools.DiffCache{Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(costfn), Float64}, Float64, 2}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(costfn), Float64}, Float64, 2}}}
The function `convert` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  (::Type{PreallocationTools.DiffCache{T, S}} where {T<:AbstractArray, S<:AbstractArray})(::Any, ::Any, ::Any, ::Any)
   @ PreallocationTools ~/.julia/packages/PreallocationTools/K2Foo/src/PreallocationTools.jl:82
  convert(::Type{T}, ::T) where T
   @ Base Base_compiler.jl:133

Stacktrace:
  [1] ModelingToolkitBase.DiffCacheAllocatorAPIWrapper{…}(cache::ModelingToolkitBase.DiffCacheAllocatorAPIWrapper{…})
    @ ModelingToolkitBase ~/.julia/packages/ModelingToolkitBase/jyweh/src/systems/diffeqs/basic_transformations.jl:1164
  [2] _broadcast_getindex_evalf
    @ ./broadcast.jl:699 [inlined]
  [3] _broadcast_getindex
    @ ./broadcast.jl:672 [inlined]
  [4] _getindex
    @ ./broadcast.jl:620 [inlined]
  [5] getindex
    @ ./broadcast.jl:616 [inlined]
  [6] copy
    @ ./broadcast.jl:933 [inlined]
  [7] materialize
    @ ./broadcast.jl:894 [inlined]
  [8] macro expansion
    @ ~/.julia/packages/Setfield/ZezIj/src/sugar.jl:198 [inlined]
  [9] (::ModelingToolkitBase.var"#_getter#742"{…})(valp::NonlinearProblem{…}, initprob::ODEProblem{…})
    @ ModelingToolkitBase ~/.julia/packages/ModelingToolkitBase/jyweh/src/systems/problem_utils.jl:851
 [10] (::ModelingToolkitBase.var"#initprobpmap_split#746"{…})(prob::ODEProblem{…}, initsol::NonlinearProblem{…})
    @ ModelingToolkitBase ~/.julia/packages/ModelingToolkitBase/jyweh/src/systems/problem_utils.jl:974
 [11] get_initial_values(prob::ODEProblem{…}, valp::ODEProblem{…}, f::ODEFunction{…}, alg::SciMLBase.OverrideInit{…}, iip::Val{…}; nlsolve_alg::Nothing, abstol::Nothing, reltol::Nothing, kwargs::@Kwargs{})
    @ SciMLBase ~/.julia/packages/SciMLBase/Ag5zT/src/initialization.jl:325
 [12] get_initial_values
    @ ~/.julia/packages/SciMLBase/Ag5zT/src/initialization.jl:267 [inlined]
 [13] maybe_eager_initialize_problem(prob::ODEProblem{…}, initialization_data::SciMLBase.OverrideInitData{…}, lazy_initialization::Nothing)
    @ SciMLBase ~/.julia/packages/SciMLBase/Ag5zT/src/remake.jl:1576
 [14] remake(prob::ODEProblem{…}; f::Missing, u0::Missing, tspan::Missing, p::MTKParameters{…}, kwargs::Missing, interpret_symbolicmap::Bool, build_initializeprob::Type, use_defaults::Bool, lazy_initialization::Nothing, _kwargs::@Kwargs{})
    @ SciMLBase ~/.julia/packages/SciMLBase/Ag5zT/src/remake.jl:392
 [15] costfn(theta::Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(costfn), Float64}, Float64, 2}})
    @ Main ./REPL[19]:3

by adding an explicit constructor that does the conversion

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Additional context

Add any other context about the problem here.

When the constructor is called with a different type,
the convert method needs to be explicitely called
@SebastianM-C SebastianM-C changed the title Fix DiffCacheAllocatorAPIWrapper conversion errors Fix DiffCacheAllocatorAPIWrapper conversion errors Apr 15, 2026
@AayushSabharwal AayushSabharwal merged commit fd113b3 into SciML:master Apr 16, 2026
48 of 77 checks passed
@SebastianM-C SebastianM-C deleted the smc/dcapiw branch April 16, 2026 08:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants