Open
Description
using ModelingToolkit, OrdinaryDiffEq, ForwardDiff, DataInterpolations
using ModelingToolkit: t_nounits as t, D_nounits as D
splval(spl::LinearInterpolation, t) = spl(t)
@register_symbolic splval(spl::LinearInterpolation, t)
vars = @variables x(t)
pars = @parameters P spl::LinearInterpolation
@named M = ODESystem([
D(x) ~ 0
], t, vars, pars; initialization_eqs = [
x ~ P + splval(spl, t)
])
M = structural_simplify(M)
prob = ODEProblem(M, [], (0.0, 1.0), [P => NaN, spl => LinearInterpolation([0.0, 0.0], [0.0, 1.0])])
function x_at_1(P)
prob′ = remake(prob; p = [M.P => P])
sol′ = solve(prob′, Tsit5())
return sol′(1.0, idxs = M.x)
end
x_at_1(1.0) # works
ForwardDiff.derivative(x_at_1, 1.0) # fails
fails with
ERROR: MethodError: no method matching splval(::ForwardDiff.Dual{ForwardDiff.Tag{…}, Float64, 1}, ::ForwardDiff.Dual{ForwardDiff.Tag{…}, Float64, 1})
The function `splval` exists, but no method is defined for this combination of argument types.
Closest candidates are:
splval(::SymbolicUtils.Symbolic{<:LinearInterpolation}, ::Real)
@ Main ~/.julia/packages/Symbolics/285ea/src/wrapper-types.jl:158
splval(::LinearInterpolation, ::Any)
@ Main REPL[1]:4
splval(::SymbolicUtils.Symbolic{<:LinearInterpolation}, ::Num)
@ Main ~/.julia/packages/Symbolics/285ea/src/wrapper-types.jl:158
...
Stacktrace:
[1] macro expansion
@ ~/.julia/packages/SymbolicUtils/0GKgW/src/code.jl:411 [inlined]
[2] macro expansion
@ ~/.julia/packages/Symbolics/285ea/src/build_function.jl:137 [inlined]
[3] macro expansion
@ ~/.julia/packages/RuntimeGeneratedFunctions/RrXEW/src/RuntimeGeneratedFunctions.jl:161 [inlined]
[4] macro expansion
@ ./none:0 [inlined]
[5] generated_callfunc
@ ./none:0 [inlined]
[6] (::RuntimeGeneratedFunctions.RuntimeGeneratedFunction{…})(::Nothing, ::MTKParameters{…})
@ RuntimeGeneratedFunctions ~/.julia/packages/RuntimeGeneratedFunctions/RrXEW/src/RuntimeGeneratedFunctions.jl:148
[7] macro expansion
@ ~/.julia/dev/ModelingToolkit/src/systems/codegen_utils.jl:0 [inlined]
[8] _generated_call(::ModelingToolkit.GeneratedFunctionWrapper{…}, ::Nothing, ::MTKParameters{…})
@ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/codegen_utils.jl:262
[9] (::ModelingToolkit.GeneratedFunctionWrapper{…})(::Nothing, ::Vararg{…})
@ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/codegen_utils.jl:259
[10] (::SymbolicIndexingInterface.TimeIndependentObservedFunction{…})(::SymbolicIndexingInterface.NotTimeseries, prob::NonlinearProblem{…})
@ SymbolicIndexingInterface ~/.julia/packages/SymbolicIndexingInterface/vXqJR/src/state_indexing.jl:142
[11] (::SymbolicIndexingInterface.TimeIndependentObservedFunction{…})(prob::NonlinearProblem{…})
@ SymbolicIndexingInterface ~/.julia/packages/SymbolicIndexingInterface/vXqJR/src/value_provider_interface.jl:166
[12] maybe_build_initialization_problem(sys::ODESystem, op::Dict{…}, u0map::Dict{…}, pmap::Dict{…}, t::Float64, defs::Dict{…}, guesses::Dict{…}, missing_unknowns::Set{…}; implicit_dae::Bool, u0_constructor::Function, floatT::Type, initialization_eqs::Vector{…}, use_scc::Bool, kwargs::@Kwargs{…})
@ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/problem_utils.jl:951
[13] remake_initialization_data(sys::ODESystem, odefn::Function, u0::Missing, t0::Float64, p::Vector{…}, newu0::Vector{…}, newp::MTKParameters{…})
@ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/nonlinear/initializesystem.jl:585
[14] remake(prob::ODEProblem{…}; f::Missing, u0::Missing, tspan::Missing, p::Vector{…}, kwargs::Missing, interpret_symbolicmap::Bool, build_initializeprob::Type, use_defaults::Bool, lazy_initialization::Nothing, _kwargs::@Kwargs{})
@ SciMLBase ~/.julia/packages/SciMLBase/m1Jrs/src/remake.jl:237
[15] x_at_1(P::ForwardDiff.Dual{ForwardDiff.Tag{typeof(x_at_1), Float64}, Float64, 1})
@ Main ./REPL[1]:18
[16] derivative(f::typeof(x_at_1), x::Float64)
@ ForwardDiff ~/.julia/packages/ForwardDiff/UBbGT/src/derivative.jl:14
Is this fixable? Or is there a better way to accomplish the same?