Skip to content

Commit 0e626e1

Browse files
efaulhabersvchb
andauthored
Fix update_callback_used when SaveSolutionCallback comes first (#924)
* Fix `update_callback_used` when `SaveSolutionCallback` comes first * Fix `PostProcessCallback` as well * Change wording --------- Co-authored-by: Sven Berger <[email protected]>
1 parent d193c71 commit 0e626e1

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/callbacks/post_process.jl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ function initialize_postprocess_callback!(cb::PostprocessCallback, u, t, integra
214214
cb.git_hash[] = compute_git_hash()
215215

216216
# Apply the callback
217-
cb(integrator)
217+
cb(integrator; from_initialize=true)
218218

219219
return cb
220220
end
@@ -227,10 +227,19 @@ function (pp::PostprocessCallback)(u, t, integrator)
227227
end
228228

229229
# `affect!`
230-
function (pp::PostprocessCallback)(integrator)
230+
function (pp::PostprocessCallback)(integrator; from_initialize=false)
231231
@trixi_timeit timer() "apply postprocess cb" begin
232-
dv_ode, du_ode = get_du(integrator).x
233232
vu_ode = integrator.u
233+
if from_initialize
234+
# Avoid calling `get_du` here, since it will call the RHS function
235+
# if it is called before the first time step.
236+
# This would cause problems with `semi.update_callback_used`,
237+
# which might not yet be set to `true` at this point if the `UpdateCallback`
238+
# comes AFTER the `PostprocessCallback` in the `CallbackSet`.
239+
dv_ode, du_ode = zero(vu_ode).x
240+
else
241+
dv_ode, du_ode = get_du(integrator).x
242+
end
234243
v_ode, u_ode = vu_ode.x
235244
semi = integrator.p
236245
t = integrator.t

src/callbacks/solution_saving.jl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ function initialize_save_cb!(solution_callback::SolutionSavingCallback, u, t, in
135135

136136
# Save initial solution
137137
if solution_callback.save_initial_solution
138-
solution_callback(integrator)
138+
solution_callback(integrator; from_initialize=true)
139139
end
140140

141141
return nothing
@@ -150,12 +150,21 @@ function (solution_callback::SolutionSavingCallback)(u, t, integrator)
150150
end
151151

152152
# `affect!`
153-
function (solution_callback::SolutionSavingCallback)(integrator)
153+
function (solution_callback::SolutionSavingCallback)(integrator; from_initialize=false)
154154
(; interval, output_directory, custom_quantities, git_hash, verbose,
155155
prefix, latest_saved_iter, max_coordinates) = solution_callback
156156

157-
dvdu_ode = get_du(integrator)
158157
vu_ode = integrator.u
158+
if from_initialize
159+
# Avoid calling `get_du` here, since it will call the RHS function
160+
# if it is called before the first time step.
161+
# This would cause problems with `semi.update_callback_used`,
162+
# which might not yet be set to `true` at this point if the `UpdateCallback`
163+
# comes AFTER the `SolutionSavingCallback` in the `CallbackSet`.
164+
dvdu_ode = zero(vu_ode)
165+
else
166+
dvdu_ode = get_du(integrator)
167+
end
159168
semi = integrator.p
160169
iter = get_iter(interval, integrator)
161170

0 commit comments

Comments
 (0)