When I solve the same ODE with callback to stop simulation early and without callback, I get different partials at the same time not even close. I believe that adding callback introduces dual times and, at the event time, the partials of time become nonzero that cause jump in the partials of the states at the event time. For example, below is non-dimensional mass-spring in vertical axis. Check s1
to s6
for the jump. Is this expected or is there a bug?
using OrdinaryDiffEq, ForwardDiff, LinearAlgebra
# val d/dy0 d/dvy0 d/dgy
y = ForwardDiff.Dual(0.6, 1.0, 0.0, 0.0 )
vy = ForwardDiff.Dual(0.0, 0.0, 1.0, 0.0 )
gy = ForwardDiff.Dual(0.005, 0.0, 0.0, 1.0 )
u0 = [y;vy]
tspan = (0.0, 2.0)
function st!(du, u, p, t)
@inbounds begin
du[1] = u[2]
du[2] = (1-u[1]) - p
end
return nothing
end
cnd(u, t, integ) = u[1] - 1
CC = ContinuousCallback(cnd, terminate!, affect_neg! = nothing, abstol=0, reltol=0)
prb = ODEProblem(st!, u0, tspan, gy)
sl1 = solve(prb, Feagin14(), abstol=1e-18, reltol=1e-18)
sl2 = solve(prb, Feagin14(), abstol=1e-18, reltol=1e-18, callback=CC)
s1 = sl1(sl2.t[end].value)
s2 = sl2[end]
s3 = sl1(sl2.t[end-1].value)
s4 = sl2[end-1]
s5 = sl1(sl2.t[end-2].value)
s6 = sl2[end-2]
s7 = sl1(sl2.t[end-3].value)
s8 = sl2[end-3]
s9 = sl1(sl2.t[end-4].value)
s10= sl2[end-4]
julia> s1 = sl1(sl2.t[end].value)
2-element Array{ForwardDiff.Dual{Nothing,Float64,3},1}:
Dual{Nothing}(0.9999999997450032,-0.012658227202540293,0.9999198739688338,-1.0126582272025406)
Dual{Nothing}(0.3949683502176894,-0.9999198739688338,-0.012658227202540293,-0.9999198739688332)
julia> s2 = sl2[end]
2-element Array{ForwardDiff.Dual{Nothing,Float64,3},1}:
Dual{Nothing}(1.0,-2.382985700754451e-16,3.982129662242418e-15,2.3278389821175142e-15)
Dual{Nothing}(0.39496832262015696,-1.0000799869548667,-4.797279698585894e-6,-1.0127344323578877)
julia> s3 = sl1(sl2.t[end-1].value)
2-element Array{ForwardDiff.Dual{Nothing,Float64,3},1}:
Dual{Nothing}(0.9999999997450032,-0.012658227202540293,0.9999198739688338,-1.0126582272025406)
Dual{Nothing}(0.3949683502176894,-0.9999198739688338,-0.012658227202540293,-0.9999198739688332)
julia> s4 = sl2[end-1]
2-element Array{ForwardDiff.Dual{Nothing,Float64,3},1}:
Dual{Nothing}(1.0,-2.382985700754451e-16,3.982129662242418e-15,2.3278389821175142e-15)
Dual{Nothing}(0.39496832262015696,-1.0000799869548667,-4.797279698585894e-6,-1.0127344323578877)
julia> s5 = sl1(sl2.t[end-2].value)
2-element Array{ForwardDiff.Dual{Nothing,Float64,3},1}:
Dual{Nothing}(0.9748648857268446,0.05097497284343077,0.9986998325015783,-0.9490250271565693)
Dual{Nothing}(0.3944864338381234,-0.9986998325015783,0.05097497284343077,-0.9986998325015778)
julia> s6 = sl2[end-2]
2-element Array{ForwardDiff.Dual{Nothing,Float64,3},1}:
Dual{Nothing}(0.9748648842437063,0.05097497659821093,0.998699930790432,-0.9490250234017891)
Dual{Nothing}(0.3944864726622205,-0.998699930790432,0.05097497659821093,-0.998699930790432)