From 9e195c59e12b7c1997e69e5b2d96e27df6e7dc1a Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Wed, 15 Nov 2023 15:11:56 -0500 Subject: [PATCH 1/4] `finalize` callbacks see also https://github.com/SciML/OrdinaryDiffEq.jl/pull/2061 --- src/common_interface/solve.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/common_interface/solve.jl b/src/common_interface/solve.jl index c178640..6f51b8e 100644 --- a/src/common_interface/solve.jl +++ b/src/common_interface/solve.jl @@ -1441,6 +1441,7 @@ function DiffEqBase.solve!(integrator::AbstractSundialsIntegrator; early_free = handle_tstop!(integrator) end + DiffEqBase.finalize!(integrator.opts.callback, integrator.u, integrator.t, integrator) tend = integrator.t if integrator.opts.save_end && (isempty(integrator.sol.t) || integrator.sol.t[end] != tend) From 51c19e4ce3b36d4f8b60e6a39c885a1defb405d8 Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Wed, 15 Nov 2023 15:13:26 -0500 Subject: [PATCH 2/4] add test --- test/common_interface/callbacks.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/common_interface/callbacks.jl b/test/common_interface/callbacks.jl index 4f080ca..4f5c993 100644 --- a/test/common_interface/callbacks.jl +++ b/test/common_interface/callbacks.jl @@ -54,9 +54,11 @@ function condition2(u, t, integrator) get_du(integrator)[1] > 0 end affect2!(integrator) = terminate!(integrator) -cb = DiscreteCallback(condition2, affect2!) +times_finalize_called = 0 +cb = DiscreteCallback(condition2, affect2!, finalize=(args...)->times_finalize_called+=1) sol = solve(prob, CVODE_BDF(); callback = cb) @test sol.t[end] < 3.5 +@test times_finalize_called = 1 condition3(u, t, integrator) = u[2] affect3!(integrator) = terminate!(integrator) From 139e5a9d1c4d1da7601c99cc9d1bca502ca79508 Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Wed, 15 Nov 2023 15:43:38 -0500 Subject: [PATCH 3/4] typo --- test/common_interface/callbacks.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/common_interface/callbacks.jl b/test/common_interface/callbacks.jl index 4f5c993..ff540e8 100644 --- a/test/common_interface/callbacks.jl +++ b/test/common_interface/callbacks.jl @@ -55,7 +55,7 @@ function condition2(u, t, integrator) end affect2!(integrator) = terminate!(integrator) times_finalize_called = 0 -cb = DiscreteCallback(condition2, affect2!, finalize=(args...)->times_finalize_called+=1) +cb = DiscreteCallback(condition2, affect2!, finalize=(args...)->global times_finalize_called+=1) sol = solve(prob, CVODE_BDF(); callback = cb) @test sol.t[end] < 3.5 @test times_finalize_called = 1 From 41605e44d58ef75f66312ceb777180fb05ec1442 Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Wed, 15 Nov 2023 15:53:51 -0500 Subject: [PATCH 4/4] typo 2 --- test/common_interface/callbacks.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/common_interface/callbacks.jl b/test/common_interface/callbacks.jl index ff540e8..a2e6a37 100644 --- a/test/common_interface/callbacks.jl +++ b/test/common_interface/callbacks.jl @@ -58,7 +58,7 @@ times_finalize_called = 0 cb = DiscreteCallback(condition2, affect2!, finalize=(args...)->global times_finalize_called+=1) sol = solve(prob, CVODE_BDF(); callback = cb) @test sol.t[end] < 3.5 -@test times_finalize_called = 1 +@test times_finalize_called == 1 condition3(u, t, integrator) = u[2] affect3!(integrator) = terminate!(integrator)