I am trying to get gradients in a heat equation solved with DifferentialEquations.jl:
using DiffEqOperators, OrdinaryDiffEq, Plots, Zygote, DiffEqSensitivity
nknots = 10
h = 1.0 / (nknots + 1)
knots = range(h, step=h, length=nknots)
ord_deriv = 2
ord_approx = 2
const Δ = CenteredDifference(ord_deriv, ord_approx, h, nknots - 2)
τ = 0.1
ξ = 0.5
t0 = 0.0
tlimit = 1.0 / ξ
t1 = 2.0 * tlimit
vlimit = 1.0
p = [1.0, 2.0]
function heat!(res, du, u, p, t)
res[1] = u[1] - t
res[2:end - 1] = p[1] * p[2] * du[2:end - 1] - 1.0 * Δ * u
res[end] = u[end] - u[end - 1]
end
u0 = zeros(nknots)
du0 = zeros(nknots)
prob = DAEProblem{true}(heat!, du0, u0, (t0, t1), p) # , differential_vars=diff_var)
sol = solve(prob, DABDF2(), abstol=1e-3, reltol=1e-3)
function sum_of_solution(p)
_prob = remake(prob, p=p)
sum(solve(_prob, DABDF2(), rtol=1e-6, atol=1e-6, saveat=0.01, sensealg=ReverseDiffAdjoint()))
end
println(sum_of_solution(p))
dp1 = Zygote.gradient(sum_of_solution, p)
but I am getting the error
ERROR: LoadError: MethodError: Cannot `convert` an object of type Vector{ReverseDiff.TrackedReal{Float64, Float64, ReverseDiff.TrackedArray{Float64, Float64, 1, Vector{Float64}, Vector{Float64}}}} to an object of type ReverseDiff.TrackedArray{Float64, Float64, 1, Vector{Float64}, Vector{Float64}}
Closest candidates are:
convert(::Type{T}, ::LinearAlgebra.Factorization) where T<:AbstractArray at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/factorization.jl:58
convert(::Type{T}, ::T) where T<:ReverseDiff.TrackedArray at /Users/salazardetro1/.julia/packages/ReverseDiff/E4Tzn/src/tracked.jl:270
convert(::Type{T}, ::T) where T<:AbstractArray at abstractarray.jl:14
Is this because ReverseDiff does not support Vectors? What are my options to get the gradients? Thanks.