diff --git a/src/integrators/integrator_utils.jl b/src/integrators/integrator_utils.jl index 6d04cc7762..4bc032d1a6 100644 --- a/src/integrators/integrator_utils.jl +++ b/src/integrators/integrator_utils.jl @@ -143,6 +143,7 @@ end postamble!(integrator::ODEIntegrator) = _postamble!(integrator) function _postamble!(integrator) + DiffEqBase.finalize!(integrator.opts.callback, integrator.u, integrator.t, integrator) solution_endpoint_match_cur_integrator!(integrator) resize!(integrator.sol.t, integrator.saveiter) resize!(integrator.sol.u, integrator.saveiter) diff --git a/test/integrators/discrete_callback_dual_test.jl b/test/integrators/discrete_callback_dual_test.jl index d37ae7b916..2e87eae966 100644 --- a/test/integrators/discrete_callback_dual_test.jl +++ b/test/integrators/discrete_callback_dual_test.jl @@ -7,11 +7,11 @@ using OrdinaryDiffEq, Test, ForwardDiff u0 = 1.0 tspan = (0.0, 1.0) p = 1.0 - +times_finalize_called = 0 function stopping_cb(tstop) condition = (u, t, integrator) -> t == tstop affect! = integrator -> (println("Stopped!"); integrator.p = zero(integrator.p)) - DiscreteCallback(condition, affect!) + DiscreteCallback(condition, affect!, finalize=(args...)->global times_finalize_called+=1) end function test_fun(tstop) @@ -22,6 +22,10 @@ function test_fun(tstop) end @test ForwardDiff.derivative(test_fun, 0.5) ≈ exp(0.5) * u0 # Analytical solution: exp(tstop)*u0 +@test times_finalize_called == 1 # test that finalize callback ran exactly once +test_fun(.5) +@test times_finalize_called == 2 # test that finalize callback ran again + function test_fun(tstop) DualT = typeof(tstop)