xref: /honee/src/setupts.c (revision b4fd18dfeb7fe20bc2ce09e18a422e556e44809a)
1 // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2 // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
3 
4 /// @file
5 /// Time-stepping functions for Navier-Stokes example using PETSc
6 
7 #include <ceed.h>
8 #include <petscdmplex.h>
9 #include <petscts.h>
10 
11 #include <navierstokes.h>
12 #include "../qfunctions/newtonian_state.h"
13 
14 // @brief Insert Boundary values if it's a new time
15 PetscErrorCode UpdateBoundaryValues(Honee honee, Vec Q_loc, PetscReal t) {
16   PetscFunctionBeginUser;
17   if (honee->time_bc_set != t) {
18     PetscCall(DMPlexInsertBoundaryValues(honee->dm, PETSC_TRUE, Q_loc, t, NULL, NULL, NULL));
19     honee->time_bc_set = t;
20   }
21   PetscFunctionReturn(PETSC_SUCCESS);
22 }
23 
24 // RHS (Explicit time-stepper) function setup
25 //   This is the RHS of the ODE, given as u_t = G(t,u)
26 //   This function takes in a state vector Q and writes into G
27 PetscErrorCode RHS_NS(TS ts, PetscReal t, Vec Q, Vec G, void *user_data) {
28   Honee        honee = *(Honee *)user_data;
29   Ceed         ceed  = honee->ceed;
30   PetscScalar  dt;
31   Vec          Q_loc = honee->Q_loc;
32   PetscMemType q_mem_type;
33 
34   PetscFunctionBeginUser;
35   // Update time dependent data
36   PetscCall(UpdateBoundaryValues(honee, Q_loc, t));
37   if (honee->phys->solution_time_label)
38     PetscCallCeed(ceed, CeedOperatorSetContextDouble(honee->op_rhs_ctx->op, honee->phys->solution_time_label, &t));
39   PetscCall(TSGetTimeStep(ts, &dt));
40   if (honee->phys->timestep_size_label)
41     PetscCallCeed(ceed, CeedOperatorSetContextDouble(honee->op_rhs_ctx->op, honee->phys->timestep_size_label, &dt));
42 
43   PetscCall(DMGlobalToLocal(honee->dm, Q, INSERT_VALUES, Q_loc));
44   if (honee->app_ctx->divFdiffproj_method != DIV_DIFF_FLUX_PROJ_NONE) PetscCall(DivDiffFluxProjectionApply(honee->diff_flux_proj, Q_loc));
45   PetscCall(ApplyCeedOperatorLocalToGlobal(Q_loc, G, honee->op_rhs_ctx));
46 
47   PetscCall(VecReadPetscToCeed(Q_loc, &q_mem_type, honee->q_ceed));
48 
49   // Inverse of the mass matrix
50   PetscCall(KSPSolve(honee->mass_ksp, G, G));
51 
52   PetscCall(VecReadCeedToPetsc(honee->q_ceed, q_mem_type, Q_loc));
53   PetscFunctionReturn(PETSC_SUCCESS);
54 }
55 
56 // Surface forces function setup
57 static PetscErrorCode Surface_Forces_NS(DM dm, Vec G_loc, PetscInt num_walls, const PetscInt walls[], PetscScalar *reaction_force) {
58   DMLabel            face_label;
59   const PetscScalar *g_array;
60   PetscInt           dim  = 3;
61   MPI_Comm           comm = PetscObjectComm((PetscObject)dm);
62   PetscSection       section;
63 
64   PetscFunctionBeginUser;
65   PetscCall(DMGetLabel(dm, "Face Sets", &face_label));
66   PetscCall(VecGetArrayRead(G_loc, &g_array));
67   for (PetscInt w = 0; w < num_walls; w++) {
68     const PetscInt wall = walls[w], *points;
69     IS             wall_is;
70     PetscInt       num_points, num_comp = 0;
71 
72     PetscCall(DMLabelGetStratumIS(face_label, wall, &wall_is));
73     if (!wall_is) continue;  // No wall points on this process, skip
74 
75     PetscCall(DMGetLocalSection(dm, &section));
76     PetscCall(PetscSectionGetFieldComponents(section, 0, &num_comp));
77     PetscCall(ISGetSize(wall_is, &num_points));
78     PetscCall(ISGetIndices(wall_is, &points));
79     for (PetscInt i = 0; i < num_points; i++) {
80       const PetscInt           p = points[i];
81       const StateConservative *r;
82       PetscInt                 dof;
83 
84       PetscCall(DMPlexPointLocalRead(dm, p, g_array, &r));
85       PetscCall(PetscSectionGetDof(section, p, &dof));
86       for (PetscInt node = 0; node < dof / num_comp; node++) {
87         for (PetscInt j = 0; j < dim; j++) {
88           reaction_force[w * dim + j] -= r[node].momentum[j];
89         }
90       }
91     }
92     PetscCall(ISRestoreIndices(wall_is, &points));
93     PetscCall(ISDestroy(&wall_is));
94   }
95   PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, reaction_force, dim * num_walls, MPIU_SCALAR, MPI_SUM, comm));
96   PetscCall(VecRestoreArrayRead(G_loc, &g_array));
97   PetscFunctionReturn(PETSC_SUCCESS);
98 }
99 
100 // Implicit time-stepper function setup
101 PetscErrorCode IFunction_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, Vec G, void *user_data) {
102   Honee        honee = *(Honee *)user_data;
103   Ceed         ceed  = honee->ceed;
104   PetscScalar  dt;
105   Vec          Q_loc = honee->Q_loc, Q_dot_loc = honee->Q_dot_loc, G_loc;
106   PetscMemType q_mem_type, q_dot_mem_type, g_mem_type;
107 
108   PetscFunctionBeginUser;
109   // Get local vectors
110   PetscCall(DMGetNamedLocalVector(honee->dm, "ResidualLocal", &G_loc));
111 
112   // Update time dependent data
113   PetscCall(UpdateBoundaryValues(honee, Q_loc, t));
114   if (honee->phys->solution_time_label) PetscCallCeed(ceed, CeedOperatorSetContextDouble(honee->op_ifunction, honee->phys->solution_time_label, &t));
115   PetscCall(TSGetTimeStep(ts, &dt));
116   if (honee->phys->timestep_size_label) PetscCallCeed(ceed, CeedOperatorSetContextDouble(honee->op_ifunction, honee->phys->timestep_size_label, &dt));
117 
118   // Global-to-local
119   PetscCall(DMGlobalToLocalBegin(honee->dm, Q, INSERT_VALUES, Q_loc));
120   PetscCall(DMGlobalToLocalBegin(honee->dm, Q_dot, INSERT_VALUES, Q_dot_loc));
121   PetscCall(DMGlobalToLocalEnd(honee->dm, Q, INSERT_VALUES, Q_loc));
122   if (honee->app_ctx->divFdiffproj_method != DIV_DIFF_FLUX_PROJ_NONE) PetscCall(DivDiffFluxProjectionApply(honee->diff_flux_proj, Q_loc));
123   PetscCall(DMGlobalToLocalEnd(honee->dm, Q_dot, INSERT_VALUES, Q_dot_loc));
124 
125   // Place PETSc vectors in CEED vectors
126   PetscCall(VecReadPetscToCeed(Q_loc, &q_mem_type, honee->q_ceed));
127   PetscCall(VecReadPetscToCeed(Q_dot_loc, &q_dot_mem_type, honee->q_dot_ceed));
128   PetscCall(VecPetscToCeed(G_loc, &g_mem_type, honee->g_ceed));
129 
130   // Apply CEED operator
131   PetscCall(PetscLogEventBegin(FLUIDS_CeedOperatorApply, Q, G, 0, 0));
132   PetscCall(PetscLogGpuTimeBegin());
133   PetscCallCeed(honee->ceed, CeedOperatorApply(honee->op_ifunction, honee->q_ceed, honee->g_ceed, CEED_REQUEST_IMMEDIATE));
134   PetscCall(PetscLogGpuTimeEnd());
135   PetscCall(PetscLogEventEnd(FLUIDS_CeedOperatorApply, Q, G, 0, 0));
136 
137   // Restore vectors
138   PetscCall(VecReadCeedToPetsc(honee->q_ceed, q_mem_type, Q_loc));
139   PetscCall(VecReadCeedToPetsc(honee->q_dot_ceed, q_dot_mem_type, Q_dot_loc));
140   PetscCall(VecCeedToPetsc(honee->g_ceed, g_mem_type, G_loc));
141 
142   if (honee->app_ctx->sgs_model_type == SGS_MODEL_DATA_DRIVEN) {
143     PetscCall(SgsDDApplyIFunction(honee, Q_loc, G_loc));
144   }
145 
146   // Local-to-Global
147   PetscCall(VecZeroEntries(G));
148   PetscCall(DMLocalToGlobal(honee->dm, G_loc, ADD_VALUES, G));
149 
150   // Restore vectors
151   PetscCall(DMRestoreNamedLocalVector(honee->dm, "ResidualLocal", &G_loc));
152   PetscFunctionReturn(PETSC_SUCCESS);
153 }
154 
155 PetscErrorCode FormIJacobian_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, PetscReal shift, Mat J, Mat J_pre, void *user_data) {
156   Honee     honee = *(Honee *)user_data;
157   PetscBool J_is_matceed, J_is_mffd, J_pre_is_matceed, J_pre_is_mffd;
158 
159   PetscFunctionBeginUser;
160   PetscCall(PetscObjectTypeCompare((PetscObject)J, MATMFFD, &J_is_mffd));
161   PetscCall(PetscObjectTypeCompare((PetscObject)J, MATCEED, &J_is_matceed));
162   PetscCall(PetscObjectTypeCompare((PetscObject)J_pre, MATMFFD, &J_pre_is_mffd));
163   PetscCall(PetscObjectTypeCompare((PetscObject)J_pre, MATCEED, &J_pre_is_matceed));
164 
165   PetscCall(MatCeedSetContextReal(honee->mat_ijacobian, "ijacobian time shift", shift));
166 
167   if (J_is_matceed || J_is_mffd) {
168     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
169     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
170   } else PetscCall(MatCeedAssembleCOO(honee->mat_ijacobian, J));
171 
172   if (J_pre_is_matceed && J != J_pre) {
173     PetscCall(MatAssemblyBegin(J_pre, MAT_FINAL_ASSEMBLY));
174     PetscCall(MatAssemblyEnd(J_pre, MAT_FINAL_ASSEMBLY));
175   } else if (!J_pre_is_matceed && !J_pre_is_mffd && J != J_pre) {
176     PetscCall(MatCeedAssembleCOO(honee->mat_ijacobian, J_pre));
177   }
178   PetscFunctionReturn(PETSC_SUCCESS);
179 }
180 
181 PetscErrorCode WriteOutput(Honee honee, Vec Q, PetscInt step_no, PetscScalar time) {
182   Vec         Q_loc;
183   char        file_path[PETSC_MAX_PATH_LEN];
184   PetscViewer viewer;
185 
186   PetscFunctionBeginUser;
187   if (honee->app_ctx->checkpoint_vtk) {
188     // Set up output
189     PetscCall(DMGetLocalVector(honee->dm, &Q_loc));
190     PetscCall(PetscObjectSetName((PetscObject)Q_loc, "StateVec"));
191     PetscCall(VecZeroEntries(Q_loc));
192     PetscCall(DMGlobalToLocal(honee->dm, Q, INSERT_VALUES, Q_loc));
193 
194     // Output
195     PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-%03" PetscInt_FMT ".vtu", honee->app_ctx->output_dir, step_no));
196 
197     PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q), file_path, FILE_MODE_WRITE, &viewer));
198     PetscCall(VecView(Q_loc, viewer));
199     PetscCall(PetscViewerDestroy(&viewer));
200     if (honee->dm_viz) {
201       Vec         Q_refined, Q_refined_loc;
202       char        file_path_refined[PETSC_MAX_PATH_LEN];
203       PetscViewer viewer_refined;
204 
205       PetscCall(DMGetGlobalVector(honee->dm_viz, &Q_refined));
206       PetscCall(DMGetLocalVector(honee->dm_viz, &Q_refined_loc));
207       PetscCall(PetscObjectSetName((PetscObject)Q_refined_loc, "Refined"));
208 
209       PetscCall(MatInterpolate(honee->interp_viz, Q, Q_refined));
210       PetscCall(VecZeroEntries(Q_refined_loc));
211       PetscCall(DMGlobalToLocal(honee->dm_viz, Q_refined, INSERT_VALUES, Q_refined_loc));
212 
213       PetscCall(
214           PetscSNPrintf(file_path_refined, sizeof file_path_refined, "%s/nsrefined-%03" PetscInt_FMT ".vtu", honee->app_ctx->output_dir, step_no));
215 
216       PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q_refined), file_path_refined, FILE_MODE_WRITE, &viewer_refined));
217       PetscCall(VecView(Q_refined_loc, viewer_refined));
218       PetscCall(DMRestoreLocalVector(honee->dm_viz, &Q_refined_loc));
219       PetscCall(DMRestoreGlobalVector(honee->dm_viz, &Q_refined));
220       PetscCall(PetscViewerDestroy(&viewer_refined));
221     }
222     PetscCall(DMRestoreLocalVector(honee->dm, &Q_loc));
223   }
224 
225   // Save data in a binary file for continuation of simulations
226   if (honee->app_ctx->add_stepnum2bin) {
227     PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution-%" PetscInt_FMT ".bin", honee->app_ctx->output_dir, step_no));
228   } else {
229     PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution.bin", honee->app_ctx->output_dir));
230   }
231   PetscCall(PetscViewerBinaryOpen(honee->comm, file_path, FILE_MODE_WRITE, &viewer));
232 
233   time /= honee->units->second;  // Dimensionalize time back
234   PetscCall(HoneeWriteBinaryVec(viewer, Q, time, step_no));
235   PetscCall(PetscViewerDestroy(&viewer));
236   PetscFunctionReturn(PETSC_SUCCESS);
237 }
238 
239 // CSV Monitor
240 PetscErrorCode TSMonitor_WallForce(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) {
241   Honee             honee = ctx;
242   Vec               G_loc;
243   PetscInt          num_wall = honee->app_ctx->wall_forces.num_wall, dim = 3;
244   const PetscInt   *walls  = honee->app_ctx->wall_forces.walls;
245   PetscViewer       viewer = honee->app_ctx->wall_forces.viewer;
246   PetscViewerFormat format = honee->app_ctx->wall_forces.viewer_format;
247   PetscScalar      *reaction_force;
248   PetscBool         is_ascii;
249 
250   PetscFunctionBeginUser;
251   if (!viewer) PetscFunctionReturn(PETSC_SUCCESS);
252   PetscCall(DMGetNamedLocalVector(honee->dm, "ResidualLocal", &G_loc));
253   PetscCall(PetscCalloc1(num_wall * dim, &reaction_force));
254   PetscCall(Surface_Forces_NS(honee->dm, G_loc, num_wall, walls, reaction_force));
255   PetscCall(DMRestoreNamedLocalVector(honee->dm, "ResidualLocal", &G_loc));
256 
257   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &is_ascii));
258 
259   if (is_ascii) {
260     if (format == PETSC_VIEWER_ASCII_CSV && !honee->app_ctx->wall_forces.header_written) {
261       PetscCall(PetscViewerASCIIPrintf(viewer, "Step,Time,Wall,ForceX,ForceY,ForceZ\n"));
262       honee->app_ctx->wall_forces.header_written = PETSC_TRUE;
263     }
264     for (PetscInt w = 0; w < num_wall; w++) {
265       PetscInt wall = walls[w];
266       if (format == PETSC_VIEWER_ASCII_CSV) {
267         PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT ",%g,%" PetscInt_FMT ",%g,%g,%g\n", step_no, time, wall,
268                                          reaction_force[w * dim + 0], reaction_force[w * dim + 1], reaction_force[w * dim + 2]));
269 
270       } else {
271         PetscCall(PetscViewerASCIIPrintf(viewer, "Wall %" PetscInt_FMT " Forces: Force_x = %12g, Force_y = %12g, Force_z = %12g\n", wall,
272                                          reaction_force[w * dim + 0], reaction_force[w * dim + 1], reaction_force[w * dim + 2]));
273       }
274     }
275   }
276   PetscCall(PetscFree(reaction_force));
277   PetscFunctionReturn(PETSC_SUCCESS);
278 }
279 
280 // User provided TS Monitor
281 PetscErrorCode TSMonitor_NS(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) {
282   Honee honee = ctx;
283 
284   PetscFunctionBeginUser;
285   // Print every 'checkpoint_interval' steps
286   if (honee->app_ctx->checkpoint_interval <= 0 || step_no % honee->app_ctx->checkpoint_interval != 0 ||
287       (honee->app_ctx->cont_steps == step_no && step_no != 0)) {
288     PetscFunctionReturn(PETSC_SUCCESS);
289   }
290 
291   PetscCall(WriteOutput(honee, Q, step_no, time));
292   PetscFunctionReturn(PETSC_SUCCESS);
293 }
294 
295 // TS: Create, setup, and solve
296 PetscErrorCode TSSolve_NS(DM dm, Honee honee, AppCtx app_ctx, Physics phys, ProblemData problem, Vec *Q, PetscScalar *f_time, TS *ts) {
297   MPI_Comm    comm = honee->comm;
298   TSAdapt     adapt;
299   PetscScalar final_time;
300 
301   PetscFunctionBeginUser;
302   PetscCall(TSCreate(comm, ts));
303   PetscCall(TSSetDM(*ts, dm));
304   PetscCall(TSSetApplicationContext(*ts, honee));
305   if (phys->implicit) {
306     PetscCall(TSSetType(*ts, TSBDF));
307     if (honee->op_ifunction) {
308       PetscCall(TSSetIFunction(*ts, NULL, IFunction_NS, &honee));
309     } else {  // Implicit integrators can fall back to using an RHSFunction
310       PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &honee));
311     }
312     if (honee->mat_ijacobian) {
313       PetscCall(DMTSSetIJacobian(dm, FormIJacobian_NS, &honee));
314     }
315   } else {
316     PetscCheck(honee->op_rhs_ctx, comm, PETSC_ERR_ARG_NULL, "Problem does not provide RHSFunction");
317     PetscCall(TSSetType(*ts, TSRK));
318     PetscCall(TSRKSetType(*ts, TSRK5F));
319     PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &honee));
320   }
321   PetscCall(TSSetMaxTime(*ts, 500. * honee->units->second));
322   PetscCall(TSSetExactFinalTime(*ts, TS_EXACTFINALTIME_STEPOVER));
323   if (app_ctx->test_type == TESTTYPE_NONE) PetscCall(TSSetErrorIfStepFails(*ts, PETSC_FALSE));
324   PetscCall(TSSetTimeStep(*ts, 1.e-2 * honee->units->second));
325   PetscCall(TSGetAdapt(*ts, &adapt));
326   PetscCall(TSAdaptSetStepLimits(adapt, 1.e-12 * honee->units->second, 1.e2 * honee->units->second));
327   PetscCall(TSSetFromOptions(*ts));
328   if (honee->mat_ijacobian) {
329     if (app_ctx->amat_type && !strcmp(app_ctx->amat_type, MATSHELL)) {
330       SNES snes;
331       KSP  ksp;
332       Mat  Pmat, Amat;
333 
334       PetscCall(TSGetSNES(*ts, &snes));
335       PetscCall(SNESGetKSP(snes, &ksp));
336       PetscCall(CreateSolveOperatorsFromMatCeed(ksp, honee->mat_ijacobian, PETSC_FALSE, &Amat, &Pmat));
337       PetscCall(TSSetIJacobian(*ts, honee->mat_ijacobian, Pmat, NULL, NULL));
338       PetscCall(MatDestroy(&Amat));
339       PetscCall(MatDestroy(&Pmat));
340     }
341   }
342   honee->time_bc_set = -1.0;  // require all BCs be updated
343   if (app_ctx->cont_steps) {  // continue from previous timestep data
344     PetscInt    count;
345     PetscViewer viewer;
346 
347     if (app_ctx->cont_time <= 0) {  // Legacy files did not include step number and time
348       PetscCall(PetscViewerBinaryOpen(comm, app_ctx->cont_time_file, FILE_MODE_READ, &viewer));
349       PetscCall(PetscViewerBinaryRead(viewer, &app_ctx->cont_time, 1, &count, PETSC_REAL));
350       PetscCall(PetscViewerDestroy(&viewer));
351       PetscCheck(app_ctx->cont_steps != -1, comm, PETSC_ERR_ARG_INCOMP,
352                  "-continue step number not specified, but checkpoint file does not contain a step number (likely written by older code version)");
353     }
354     PetscCall(TSSetTime(*ts, app_ctx->cont_time * honee->units->second));
355     PetscCall(TSSetStepNumber(*ts, app_ctx->cont_steps));
356   }
357   if (app_ctx->test_type == TESTTYPE_NONE) {
358     PetscCall(TSMonitorSet(*ts, TSMonitor_NS, honee, NULL));
359   }
360   if (app_ctx->wall_forces.viewer) {
361     PetscCall(TSMonitorSet(*ts, TSMonitor_WallForce, honee, NULL));
362   }
363   if (app_ctx->turb_spanstats_enable) {
364     PetscCall(TSMonitorSet(*ts, TSMonitor_TurbulenceStatistics, honee, NULL));
365     CeedScalar previous_time = app_ctx->cont_time * honee->units->second;
366     PetscCallCeed(honee->ceed,
367                   CeedOperatorSetContextDouble(honee->spanstats.op_stats_collect_ctx->op, honee->spanstats.previous_time_label, &previous_time));
368   }
369   if (app_ctx->diff_filter_monitor) PetscCall(TSMonitorSet(*ts, TSMonitor_DifferentialFilter, honee, NULL));
370 
371   if (app_ctx->sgs_train_enable) {
372     PetscCall(TSMonitorSet(*ts, TSMonitor_SGS_DD_Training, honee, NULL));
373     PetscCall(TSSetPostStep(*ts, TSPostStep_SGS_DD_Training));
374   }
375 
376   if (app_ctx->test_type == TESTTYPE_NONE) PetscCall(PrintRunInfo(honee, honee->phys, problem, *ts));
377   // Solve
378   PetscReal start_time;
379   PetscInt  start_step;
380   PetscCall(TSGetTime(*ts, &start_time));
381   PetscCall(TSGetStepNumber(*ts, &start_step));
382 
383   PetscCall(PetscLogDefaultBegin());  // So we can use PetscLogStageGetPerfInfo without -log_view
384   PetscPreLoadBegin(PETSC_FALSE, "Fluids Solve");
385   PetscCall(TSSetTime(*ts, start_time));
386   PetscCall(TSSetStepNumber(*ts, start_step));
387   if (PetscPreLoadingOn) {
388     // LCOV_EXCL_START
389     SNES      snes;
390     Vec       Q_preload;
391     PetscReal rtol;
392     PetscCall(VecDuplicate(*Q, &Q_preload));
393     PetscCall(VecCopy(*Q, Q_preload));
394     PetscCall(TSGetSNES(*ts, &snes));
395     PetscCall(SNESGetTolerances(snes, NULL, &rtol, NULL, NULL, NULL));
396     PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, .99, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT));
397     PetscCall(TSSetSolution(*ts, Q_preload));
398     PetscCall(TSStep(*ts));
399     PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, rtol, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT));
400     PetscCall(VecDestroy(&Q_preload));
401     // LCOV_EXCL_STOP
402   } else {
403     PetscCall(PetscBarrier((PetscObject)*ts));
404     PetscCall(TSSolve(*ts, *Q));
405   }
406   PetscPreLoadEnd();
407 
408   PetscCall(TSGetSolveTime(*ts, &final_time));
409   *f_time = final_time;
410 
411   if (app_ctx->test_type == TESTTYPE_NONE) {
412     PetscInt step_no;
413     PetscCall(TSGetStepNumber(*ts, &step_no));
414     if (honee->app_ctx->checkpoint_interval > 0 || honee->app_ctx->checkpoint_interval == -1) {
415       PetscCall(WriteOutput(honee, *Q, step_no, final_time));
416     }
417 
418     PetscLogStage      stage_id;
419     PetscEventPerfInfo stage_perf;
420 
421     PetscCall(PetscLogStageGetId("Fluids Solve", &stage_id));
422     PetscCall(PetscLogStageGetPerfInfo(stage_id, &stage_perf));
423     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Time taken for solution (sec): %g\n", stage_perf.time));
424   }
425   PetscFunctionReturn(PETSC_SUCCESS);
426 }
427