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