Skip to content

Commit

Permalink
Merge pull request #2061 from oscardssmith/patch-1
Browse files Browse the repository at this point in the history
`finalize` callbacks.
  • Loading branch information
ChrisRackauckas authored Nov 18, 2023
2 parents 9107d4e + 4a534a8 commit da70b26
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/integrators/integrator_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions test/integrators/discrete_callback_dual_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit da70b26

Please sign in to comment.