xref: /libCEED/examples/fluids/src/setupts.c (revision a424bcd0dd58ea8f8b80ddde5af211268f0b358a)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
377841947SLeila Ghaffari //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
577841947SLeila Ghaffari //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
777841947SLeila Ghaffari 
877841947SLeila Ghaffari /// @file
977841947SLeila Ghaffari /// Time-stepping functions for Navier-Stokes example using PETSc
1077841947SLeila Ghaffari 
1149aac155SJeremy L Thompson #include <ceed.h>
1249aac155SJeremy L Thompson #include <petscdmplex.h>
1349aac155SJeremy L Thompson #include <petscts.h>
1449aac155SJeremy L Thompson 
1577841947SLeila Ghaffari #include "../navierstokes.h"
16ca69d878SAdeleke O. Bankole #include "../qfunctions/newtonian_state.h"
1777841947SLeila Ghaffari 
1877841947SLeila Ghaffari // Compute mass matrix for explicit scheme
192b730f8bSJeremy L Thompson PetscErrorCode ComputeLumpedMassMatrix(Ceed ceed, DM dm, CeedData ceed_data, Vec M) {
2077841947SLeila Ghaffari   CeedQFunction        qf_mass;
2177841947SLeila Ghaffari   CeedOperator         op_mass;
223221f4d3SJames Wright   OperatorApplyContext op_mass_ctx;
233221f4d3SJames Wright   Vec                  Ones_loc;
2477841947SLeila Ghaffari   CeedInt              num_comp_q, q_data_size;
2577841947SLeila Ghaffari   PetscFunctionBeginUser;
2677841947SLeila Ghaffari 
2777841947SLeila Ghaffari   // CEED Restriction
28*a424bcd0SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_q, &num_comp_q));
29*a424bcd0SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_qd_i, &q_data_size));
3077841947SLeila Ghaffari 
3177841947SLeila Ghaffari   // CEED QFunction
32ef080ff9SJames Wright   PetscCall(CreateMassQFunction(ceed, num_comp_q, q_data_size, &qf_mass));
3377841947SLeila Ghaffari 
3477841947SLeila Ghaffari   // CEED Operator
35*a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass));
36*a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "u", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
37*a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data->q_data));
38*a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
3977841947SLeila Ghaffari 
403221f4d3SJames Wright   PetscCall(OperatorApplyContextCreate(NULL, dm, ceed, op_mass, NULL, NULL, NULL, NULL, &op_mass_ctx));
4177841947SLeila Ghaffari 
423221f4d3SJames Wright   PetscCall(DMGetLocalVector(dm, &Ones_loc));
433221f4d3SJames Wright   PetscCall(VecSet(Ones_loc, 1));
443221f4d3SJames Wright   PetscCall(ApplyCeedOperatorLocalToGlobal(Ones_loc, M, op_mass_ctx));
4577841947SLeila Ghaffari 
4677841947SLeila Ghaffari   // Invert diagonally lumped mass vector for RHS function
472b730f8bSJeremy L Thompson   PetscCall(VecReciprocal(M));
4877841947SLeila Ghaffari 
4977841947SLeila Ghaffari   // Cleanup
503221f4d3SJames Wright   PetscCall(OperatorApplyContextDestroy(op_mass_ctx));
513221f4d3SJames Wright   PetscCall(DMRestoreLocalVector(dm, &Ones_loc));
52*a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_mass));
53*a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorDestroy(&op_mass));
5477841947SLeila Ghaffari 
55ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
5677841947SLeila Ghaffari }
5777841947SLeila Ghaffari 
58a0b9a424SJames Wright // Insert Boundary values if it's a new time
59a0b9a424SJames Wright PetscErrorCode UpdateBoundaryValues(User user, Vec Q_loc, PetscReal t) {
60a0b9a424SJames Wright   PetscFunctionBeginUser;
61a0b9a424SJames Wright   if (user->time_bc_set != t) {
62a0b9a424SJames Wright     PetscCall(DMPlexInsertBoundaryValues(user->dm, PETSC_TRUE, Q_loc, t, NULL, NULL, NULL));
63a0b9a424SJames Wright     user->time_bc_set = t;
64a0b9a424SJames Wright   }
65ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
66a0b9a424SJames Wright }
67a0b9a424SJames Wright 
6815637395SJames Wright // @brief Update the context label value to new value if necessary.
6915637395SJames Wright // @note This only supports labels with scalar label values (ie. not arrays)
70*a424bcd0SJames Wright PetscErrorCode UpdateContextLabel(Ceed ceed, MPI_Comm comm, PetscScalar update_value, CeedOperator op, CeedContextFieldLabel label) {
7115637395SJames Wright   PetscScalar label_value;
72e42c1df6SJames Wright 
7315637395SJames Wright   PetscFunctionBeginUser;
7415637395SJames Wright   PetscCheck(label, comm, PETSC_ERR_ARG_BADPTR, "Label should be non-NULL");
7515637395SJames Wright 
7615637395SJames Wright   {
7715637395SJames Wright     size_t             num_elements;
7815637395SJames Wright     const PetscScalar *label_values;
79*a424bcd0SJames Wright     PetscCallCeed(ceed, CeedOperatorGetContextDoubleRead(op, label, &num_elements, &label_values));
8015637395SJames Wright     PetscCheck(num_elements == 1, comm, PETSC_ERR_SUP, "%s does not support labels with more than 1 value. Label has %zu values", __func__,
8115637395SJames Wright                num_elements);
8215637395SJames Wright     label_value = *label_values;
83*a424bcd0SJames Wright     PetscCallCeed(ceed, CeedOperatorRestoreContextDoubleRead(op, label, &label_values));
84e42c1df6SJames Wright   }
8515637395SJames Wright 
8615637395SJames Wright   if (label_value != update_value) {
87*a424bcd0SJames Wright     PetscCallCeed(ceed, CeedOperatorSetContextDouble(op, label, &update_value));
88e42c1df6SJames Wright   }
89ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
90e42c1df6SJames Wright }
91e42c1df6SJames Wright 
9277841947SLeila Ghaffari // RHS (Explicit time-stepper) function setup
9377841947SLeila Ghaffari //   This is the RHS of the ODE, given as u_t = G(t,u)
9477841947SLeila Ghaffari //   This function takes in a state vector Q and writes into G
9577841947SLeila Ghaffari PetscErrorCode RHS_NS(TS ts, PetscReal t, Vec Q, Vec G, void *user_data) {
9677841947SLeila Ghaffari   User        user = *(User *)user_data;
9715637395SJames Wright   MPI_Comm    comm = PetscObjectComm((PetscObject)ts);
98c798d55aSJames Wright   PetscScalar dt;
999ad5e8e4SJames Wright   Vec         Q_loc = user->Q_loc;
10077841947SLeila Ghaffari   PetscFunctionBeginUser;
10177841947SLeila Ghaffari 
1025e82a6e1SJeremy L Thompson   // Update time dependent data
103a0b9a424SJames Wright   PetscCall(UpdateBoundaryValues(user, Q_loc, t));
104*a424bcd0SJames Wright   if (user->phys->solution_time_label) PetscCall(UpdateContextLabel(user->ceed, comm, t, user->op_rhs_ctx->op, user->phys->solution_time_label));
1052b730f8bSJeremy L Thompson   PetscCall(TSGetTimeStep(ts, &dt));
106*a424bcd0SJames Wright   if (user->phys->timestep_size_label) PetscCall(UpdateContextLabel(user->ceed, comm, dt, user->op_rhs_ctx->op, user->phys->timestep_size_label));
10777841947SLeila Ghaffari 
1089ad5e8e4SJames Wright   PetscCall(ApplyCeedOperatorGlobalToGlobal(Q, G, user->op_rhs_ctx));
10977841947SLeila Ghaffari 
11077841947SLeila Ghaffari   // Inverse of the lumped mass matrix (M is Minv)
111186595e6SJames Wright   PetscCall(VecPointwiseMult(G, G, user->M_inv));
11277841947SLeila Ghaffari 
113ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
11477841947SLeila Ghaffari }
11577841947SLeila Ghaffari 
116ca69d878SAdeleke O. Bankole // Surface forces function setup
117ca69d878SAdeleke O. Bankole static PetscErrorCode Surface_Forces_NS(DM dm, Vec G_loc, PetscInt num_walls, const PetscInt walls[], PetscScalar *reaction_force) {
118ca69d878SAdeleke O. Bankole   DMLabel            face_label;
119ca69d878SAdeleke O. Bankole   const PetscScalar *g;
120d6734f85SAdeleke O. Bankole   PetscInt           dof, dim = 3;
121ca69d878SAdeleke O. Bankole   MPI_Comm           comm;
122d6734f85SAdeleke O. Bankole   PetscSection       s;
123ca69d878SAdeleke O. Bankole 
124ca69d878SAdeleke O. Bankole   PetscFunctionBeginUser;
125ca69d878SAdeleke O. Bankole   PetscCall(PetscArrayzero(reaction_force, num_walls * dim));
126ca69d878SAdeleke O. Bankole   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
127ca69d878SAdeleke O. Bankole   PetscCall(DMGetLabel(dm, "Face Sets", &face_label));
128ca69d878SAdeleke O. Bankole   PetscCall(VecGetArrayRead(G_loc, &g));
129ca69d878SAdeleke O. Bankole   for (PetscInt w = 0; w < num_walls; w++) {
130ca69d878SAdeleke O. Bankole     const PetscInt wall = walls[w];
131ca69d878SAdeleke O. Bankole     IS             wall_is;
132d6734f85SAdeleke O. Bankole     PetscCall(DMGetLocalSection(dm, &s));
133ca69d878SAdeleke O. Bankole     PetscCall(DMLabelGetStratumIS(face_label, wall, &wall_is));
134ca69d878SAdeleke O. Bankole     if (wall_is) {  // There exist such points on this process
135ca69d878SAdeleke O. Bankole       PetscInt        num_points;
136d6734f85SAdeleke O. Bankole       PetscInt        num_comp = 0;
137ca69d878SAdeleke O. Bankole       const PetscInt *points;
138d6734f85SAdeleke O. Bankole       PetscCall(PetscSectionGetFieldComponents(s, 0, &num_comp));
139ca69d878SAdeleke O. Bankole       PetscCall(ISGetSize(wall_is, &num_points));
140ca69d878SAdeleke O. Bankole       PetscCall(ISGetIndices(wall_is, &points));
141ca69d878SAdeleke O. Bankole       for (PetscInt i = 0; i < num_points; i++) {
142ca69d878SAdeleke O. Bankole         const PetscInt           p = points[i];
143ca69d878SAdeleke O. Bankole         const StateConservative *r;
144ca69d878SAdeleke O. Bankole         PetscCall(DMPlexPointLocalRead(dm, p, g, &r));
145d6734f85SAdeleke O. Bankole         PetscCall(PetscSectionGetDof(s, p, &dof));
146d6734f85SAdeleke O. Bankole         for (PetscInt node = 0; node < dof / num_comp; node++) {
147ca69d878SAdeleke O. Bankole           for (PetscInt j = 0; j < 3; j++) {
148d6734f85SAdeleke O. Bankole             reaction_force[w * dim + j] -= r[node].momentum[j];
149d6734f85SAdeleke O. Bankole           }
150ca69d878SAdeleke O. Bankole         }
151ca69d878SAdeleke O. Bankole       }
152ca69d878SAdeleke O. Bankole       PetscCall(ISRestoreIndices(wall_is, &points));
153ca69d878SAdeleke O. Bankole     }
154ca69d878SAdeleke O. Bankole     PetscCall(ISDestroy(&wall_is));
155ca69d878SAdeleke O. Bankole   }
156ca69d878SAdeleke O. Bankole   PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, reaction_force, dim * num_walls, MPIU_SCALAR, MPI_SUM, comm));
157ca69d878SAdeleke O. Bankole   //  Restore Vectors
158ca69d878SAdeleke O. Bankole   PetscCall(VecRestoreArrayRead(G_loc, &g));
159ca69d878SAdeleke O. Bankole 
160ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
161ca69d878SAdeleke O. Bankole }
162ca69d878SAdeleke O. Bankole 
16377841947SLeila Ghaffari // Implicit time-stepper function setup
1642b730f8bSJeremy L Thompson PetscErrorCode IFunction_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, Vec G, void *user_data) {
16577841947SLeila Ghaffari   User         user = *(User *)user_data;
16615637395SJames Wright   MPI_Comm     comm = PetscObjectComm((PetscObject)ts);
167c798d55aSJames Wright   PetscScalar  dt;
1685e82a6e1SJeremy L Thompson   Vec          Q_loc = user->Q_loc, Q_dot_loc = user->Q_dot_loc, G_loc;
16977841947SLeila Ghaffari   PetscMemType q_mem_type, q_dot_mem_type, g_mem_type;
17077841947SLeila Ghaffari   PetscFunctionBeginUser;
17177841947SLeila Ghaffari 
1725e82a6e1SJeremy L Thompson   // Get local vectors
173ca69d878SAdeleke O. Bankole   PetscCall(DMGetNamedLocalVector(user->dm, "ResidualLocal", &G_loc));
1745e82a6e1SJeremy L Thompson 
1755e82a6e1SJeremy L Thompson   // Update time dependent data
176a0b9a424SJames Wright   PetscCall(UpdateBoundaryValues(user, Q_loc, t));
177*a424bcd0SJames Wright   if (user->phys->solution_time_label) PetscCall(UpdateContextLabel(user->ceed, comm, t, user->op_ifunction, user->phys->solution_time_label));
1782b730f8bSJeremy L Thompson   PetscCall(TSGetTimeStep(ts, &dt));
179*a424bcd0SJames Wright   if (user->phys->timestep_size_label) PetscCall(UpdateContextLabel(user->ceed, comm, dt, user->op_ifunction, user->phys->timestep_size_label));
18077841947SLeila Ghaffari 
18177841947SLeila Ghaffari   // Global-to-local
182878eb0e7SJames Wright   PetscCall(DMGlobalToLocalBegin(user->dm, Q, INSERT_VALUES, Q_loc));
183878eb0e7SJames Wright   PetscCall(DMGlobalToLocalBegin(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc));
184878eb0e7SJames Wright   PetscCall(DMGlobalToLocalEnd(user->dm, Q, INSERT_VALUES, Q_loc));
185878eb0e7SJames Wright   PetscCall(DMGlobalToLocalEnd(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc));
18677841947SLeila Ghaffari 
18777841947SLeila Ghaffari   // Place PETSc vectors in CEED vectors
188c798d55aSJames Wright   PetscCall(VecReadP2C(Q_loc, &q_mem_type, user->q_ceed));
189c798d55aSJames Wright   PetscCall(VecReadP2C(Q_dot_loc, &q_dot_mem_type, user->q_dot_ceed));
190c798d55aSJames Wright   PetscCall(VecP2C(G_loc, &g_mem_type, user->g_ceed));
19177841947SLeila Ghaffari 
19277841947SLeila Ghaffari   // Apply CEED operator
19375d1798cSJames Wright   PetscCall(PetscLogEventBegin(FLUIDS_CeedOperatorApply, Q, G, 0, 0));
19475d1798cSJames Wright   PetscCall(PetscLogGpuTimeBegin());
195*a424bcd0SJames Wright   PetscCallCeed(user->ceed, CeedOperatorApply(user->op_ifunction, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE));
19675d1798cSJames Wright   PetscCall(PetscLogGpuTimeEnd());
19775d1798cSJames Wright   PetscCall(PetscLogEventEnd(FLUIDS_CeedOperatorApply, Q, G, 0, 0));
19877841947SLeila Ghaffari 
19977841947SLeila Ghaffari   // Restore vectors
200c798d55aSJames Wright   PetscCall(VecReadC2P(user->q_ceed, q_mem_type, Q_loc));
201c798d55aSJames Wright   PetscCall(VecReadC2P(user->q_dot_ceed, q_dot_mem_type, Q_dot_loc));
202c798d55aSJames Wright   PetscCall(VecC2P(user->g_ceed, g_mem_type, G_loc));
20377841947SLeila Ghaffari 
20419ffbc25SJames Wright   if (user->app_ctx->sgs_model_type == SGS_MODEL_DATA_DRIVEN) {
20519ffbc25SJames Wright     PetscCall(SGS_DD_ModelApplyIFunction(user, Q_loc, G_loc));
20619ffbc25SJames Wright   }
207be34b3b7SJames Wright 
20877841947SLeila Ghaffari   // Local-to-Global
2092b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(G));
2102b730f8bSJeremy L Thompson   PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G));
21177841947SLeila Ghaffari 
21277841947SLeila Ghaffari   // Restore vectors
213ca69d878SAdeleke O. Bankole   PetscCall(DMRestoreNamedLocalVector(user->dm, "ResidualLocal", &G_loc));
21477841947SLeila Ghaffari 
215ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
21677841947SLeila Ghaffari }
21777841947SLeila Ghaffari 
2182b730f8bSJeremy L Thompson static PetscErrorCode FormPreallocation(User user, PetscBool pbdiagonal, Mat J, CeedVector *coo_values) {
219544be873SJed Brown   PetscCount ncoo;
220457e73b2SJames Wright   PetscInt  *rows_petsc, *cols_petsc;
221544be873SJed Brown 
222544be873SJed Brown   PetscFunctionBeginUser;
223544be873SJed Brown   if (pbdiagonal) {
224544be873SJed Brown     CeedSize l_size;
225*a424bcd0SJames Wright     PetscCallCeed(user->ceed, CeedOperatorGetActiveVectorLengths(user->op_ijacobian, &l_size, NULL));
226544be873SJed Brown     ncoo       = l_size * 5;
227457e73b2SJames Wright     rows_petsc = malloc(ncoo * sizeof(rows_petsc));
228457e73b2SJames Wright     cols_petsc = malloc(ncoo * sizeof(cols_petsc));
229544be873SJed Brown     for (PetscCount n = 0; n < l_size / 5; n++) {
230544be873SJed Brown       for (PetscInt i = 0; i < 5; i++) {
231544be873SJed Brown         for (PetscInt j = 0; j < 5; j++) {
232457e73b2SJames Wright           rows_petsc[(n * 5 + i) * 5 + j] = n * 5 + i;
233457e73b2SJames Wright           cols_petsc[(n * 5 + i) * 5 + j] = n * 5 + j;
234544be873SJed Brown         }
235544be873SJed Brown       }
236544be873SJed Brown     }
237544be873SJed Brown   } else {
238457e73b2SJames Wright     CeedInt *rows_ceed, *cols_ceed;
239*a424bcd0SJames Wright     PetscCallCeed(user->ceed, CeedOperatorLinearAssembleSymbolic(user->op_ijacobian, &ncoo, &rows_ceed, &cols_ceed));
240457e73b2SJames Wright     PetscCall(IntArrayC2P(ncoo, &rows_ceed, &rows_petsc));
241457e73b2SJames Wright     PetscCall(IntArrayC2P(ncoo, &cols_ceed, &cols_petsc));
242544be873SJed Brown   }
243457e73b2SJames Wright   PetscCall(MatSetPreallocationCOOLocal(J, ncoo, rows_petsc, cols_petsc));
244457e73b2SJames Wright   free(rows_petsc);
245457e73b2SJames Wright   free(cols_petsc);
246*a424bcd0SJames Wright   PetscCallCeed(user->ceed, CeedVectorCreate(user->ceed, ncoo, coo_values));
247ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
248544be873SJed Brown }
249544be873SJed Brown 
2502b730f8bSJeremy L Thompson static PetscErrorCode FormSetValues(User user, PetscBool pbdiagonal, Mat J, CeedVector coo_values) {
251544be873SJed Brown   CeedMemType        mem_type = CEED_MEM_HOST;
252544be873SJed Brown   const PetscScalar *values;
253544be873SJed Brown   MatType            mat_type;
254544be873SJed Brown 
255544be873SJed Brown   PetscFunctionBeginUser;
256544be873SJed Brown   PetscCall(MatGetType(J, &mat_type));
2572b730f8bSJeremy L Thompson   if (strstr(mat_type, "kokkos") || strstr(mat_type, "cusparse")) mem_type = CEED_MEM_DEVICE;
258fbb546ddSJames Wright   if (pbdiagonal) {
25975d1798cSJames Wright     PetscCall(PetscLogEventBegin(FLUIDS_CeedOperatorAssemblePointBlockDiagonal, J, 0, 0, 0));
26075d1798cSJames Wright     PetscCall(PetscLogGpuTimeBegin());
261*a424bcd0SJames Wright     PetscCallCeed(user->ceed, CeedOperatorLinearAssemblePointBlockDiagonal(user->op_ijacobian, coo_values, CEED_REQUEST_IMMEDIATE));
26275d1798cSJames Wright     PetscCall(PetscLogGpuTimeEnd());
26375d1798cSJames Wright     PetscCall(PetscLogEventEnd(FLUIDS_CeedOperatorAssemblePointBlockDiagonal, J, 0, 0, 0));
264544be873SJed Brown   } else {
26575d1798cSJames Wright     PetscCall(PetscLogEventBegin(FLUIDS_CeedOperatorAssemble, J, 0, 0, 0));
26675d1798cSJames Wright     PetscCall(PetscLogGpuTimeBegin());
267*a424bcd0SJames Wright     PetscCallCeed(user->ceed, CeedOperatorLinearAssemble(user->op_ijacobian, coo_values));
26875d1798cSJames Wright     PetscCall(PetscLogGpuTimeEnd());
26975d1798cSJames Wright     PetscCall(PetscLogEventEnd(FLUIDS_CeedOperatorAssemble, J, 0, 0, 0));
270544be873SJed Brown   }
271*a424bcd0SJames Wright   PetscCallCeed(user->ceed, CeedVectorGetArrayRead(coo_values, mem_type, &values));
272544be873SJed Brown   PetscCall(MatSetValuesCOO(J, values, INSERT_VALUES));
273*a424bcd0SJames Wright   PetscCallCeed(user->ceed, CeedVectorRestoreArrayRead(coo_values, &values));
274ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
275544be873SJed Brown }
276544be873SJed Brown 
2772b730f8bSJeremy L Thompson PetscErrorCode FormIJacobian_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, PetscReal shift, Mat J, Mat J_pre, void *user_data) {
278e334ad8fSJed Brown   User      user = *(User *)user_data;
279*a424bcd0SJames Wright   Ceed      ceed = user->ceed;
280d6bf345cSJed Brown   PetscBool J_is_shell, J_is_mffd, J_pre_is_shell;
281e334ad8fSJed Brown   PetscFunctionBeginUser;
282*a424bcd0SJames Wright   if (user->phys->ijacobian_time_shift_label)
283*a424bcd0SJames Wright     PetscCallCeed(ceed, CeedOperatorSetContextDouble(user->op_ijacobian, user->phys->ijacobian_time_shift_label, &shift));
284d6bf345cSJed Brown   PetscCall(PetscObjectTypeCompare((PetscObject)J, MATMFFD, &J_is_mffd));
285e334ad8fSJed Brown   PetscCall(PetscObjectTypeCompare((PetscObject)J, MATSHELL, &J_is_shell));
2862b730f8bSJeremy L Thompson   PetscCall(PetscObjectTypeCompare((PetscObject)J_pre, MATSHELL, &J_pre_is_shell));
287e334ad8fSJed Brown   if (!user->matrices_set_up) {
288e334ad8fSJed Brown     if (J_is_shell) {
289b07979f9SJames Wright       OperatorApplyContext op_ijacobian_ctx;
290b07979f9SJames Wright       OperatorApplyContextCreate(user->dm, user->dm, user->ceed, user->op_ijacobian, user->q_ceed, user->g_ceed, user->Q_dot_loc, NULL,
291b07979f9SJames Wright                                  &op_ijacobian_ctx);
292b07979f9SJames Wright       PetscCall(MatShellSetContext(J, op_ijacobian_ctx));
293b07979f9SJames Wright       PetscCall(MatShellSetContextDestroy(J, (PetscErrorCode(*)(void *))OperatorApplyContextDestroy));
294b07979f9SJames Wright       PetscCall(MatShellSetOperation(J, MATOP_MULT, (void (*)(void))MatMult_Ceed));
295b07979f9SJames Wright       PetscCall(MatShellSetOperation(J, MATOP_GET_DIAGONAL, (void (*)(void))MatGetDiag_Ceed));
296e334ad8fSJed Brown       PetscCall(MatSetUp(J));
297e334ad8fSJed Brown     }
298e334ad8fSJed Brown     if (!J_pre_is_shell) {
2992b730f8bSJeremy L Thompson       PetscCall(FormPreallocation(user, user->app_ctx->pmat_pbdiagonal, J_pre, &user->coo_values_pmat));
300544be873SJed Brown     }
301d6bf345cSJed Brown     if (J != J_pre && !J_is_shell && !J_is_mffd) {
302544be873SJed Brown       PetscCall(FormPreallocation(user, PETSC_FALSE, J, &user->coo_values_amat));
303544be873SJed Brown     }
304e334ad8fSJed Brown     user->matrices_set_up = true;
305e334ad8fSJed Brown   }
306e334ad8fSJed Brown   if (!J_pre_is_shell) {
3072b730f8bSJeremy L Thompson     PetscCall(FormSetValues(user, user->app_ctx->pmat_pbdiagonal, J_pre, user->coo_values_pmat));
308e334ad8fSJed Brown   }
309d6bf345cSJed Brown   if (user->coo_values_amat) {
310d6bf345cSJed Brown     PetscCall(FormSetValues(user, PETSC_FALSE, J, user->coo_values_amat));
311d6bf345cSJed Brown   } else if (J_is_mffd) {
312d6bf345cSJed Brown     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
313d6bf345cSJed Brown     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
314d6bf345cSJed Brown   }
315ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
316e334ad8fSJed Brown }
317e334ad8fSJed Brown 
3182b730f8bSJeremy L Thompson PetscErrorCode WriteOutput(User user, Vec Q, PetscInt step_no, PetscScalar time) {
31977841947SLeila Ghaffari   Vec         Q_loc;
32077841947SLeila Ghaffari   char        file_path[PETSC_MAX_PATH_LEN];
32177841947SLeila Ghaffari   PetscViewer viewer;
32277841947SLeila Ghaffari   PetscFunctionBeginUser;
32377841947SLeila Ghaffari 
32437cbb16aSJed Brown   if (user->app_ctx->checkpoint_vtk) {
32577841947SLeila Ghaffari     // Set up output
326f14660a4SJames Wright     PetscCall(DMGetLocalVector(user->dm, &Q_loc));
327f14660a4SJames Wright     PetscCall(PetscObjectSetName((PetscObject)Q_loc, "StateVec"));
328f14660a4SJames Wright     PetscCall(VecZeroEntries(Q_loc));
329f14660a4SJames Wright     PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc));
33077841947SLeila Ghaffari 
33177841947SLeila Ghaffari     // Output
33237cbb16aSJed Brown     PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no));
333f14660a4SJames Wright 
3342b730f8bSJeremy L Thompson     PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q), file_path, FILE_MODE_WRITE, &viewer));
335f14660a4SJames Wright     PetscCall(VecView(Q_loc, viewer));
336f14660a4SJames Wright     PetscCall(PetscViewerDestroy(&viewer));
33777841947SLeila Ghaffari     if (user->dm_viz) {
33877841947SLeila Ghaffari       Vec         Q_refined, Q_refined_loc;
33977841947SLeila Ghaffari       char        file_path_refined[PETSC_MAX_PATH_LEN];
34077841947SLeila Ghaffari       PetscViewer viewer_refined;
34177841947SLeila Ghaffari 
342f14660a4SJames Wright       PetscCall(DMGetGlobalVector(user->dm_viz, &Q_refined));
343f14660a4SJames Wright       PetscCall(DMGetLocalVector(user->dm_viz, &Q_refined_loc));
344f14660a4SJames Wright       PetscCall(PetscObjectSetName((PetscObject)Q_refined_loc, "Refined"));
345f14660a4SJames Wright 
346f14660a4SJames Wright       PetscCall(MatInterpolate(user->interp_viz, Q, Q_refined));
347f14660a4SJames Wright       PetscCall(VecZeroEntries(Q_refined_loc));
3482b730f8bSJeremy L Thompson       PetscCall(DMGlobalToLocal(user->dm_viz, Q_refined, INSERT_VALUES, Q_refined_loc));
349f14660a4SJames Wright 
35037cbb16aSJed Brown       PetscCall(
35137cbb16aSJed Brown           PetscSNPrintf(file_path_refined, sizeof file_path_refined, "%s/nsrefined-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no));
352f14660a4SJames Wright 
3532b730f8bSJeremy L Thompson       PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q_refined), file_path_refined, FILE_MODE_WRITE, &viewer_refined));
354f14660a4SJames Wright       PetscCall(VecView(Q_refined_loc, viewer_refined));
355f14660a4SJames Wright       PetscCall(DMRestoreLocalVector(user->dm_viz, &Q_refined_loc));
356f14660a4SJames Wright       PetscCall(DMRestoreGlobalVector(user->dm_viz, &Q_refined));
357f14660a4SJames Wright       PetscCall(PetscViewerDestroy(&viewer_refined));
35877841947SLeila Ghaffari     }
359f14660a4SJames Wright     PetscCall(DMRestoreLocalVector(user->dm, &Q_loc));
36037cbb16aSJed Brown   }
36177841947SLeila Ghaffari 
36277841947SLeila Ghaffari   // Save data in a binary file for continuation of simulations
36369293791SJames Wright   if (user->app_ctx->add_stepnum2bin) {
36437cbb16aSJed Brown     PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution-%" PetscInt_FMT ".bin", user->app_ctx->output_dir, step_no));
36569293791SJames Wright   } else {
3662b730f8bSJeremy L Thompson     PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution.bin", user->app_ctx->output_dir));
36769293791SJames Wright   }
3682b730f8bSJeremy L Thompson   PetscCall(PetscViewerBinaryOpen(user->comm, file_path, FILE_MODE_WRITE, &viewer));
369f14660a4SJames Wright 
3700de6a49fSJames Wright   PetscInt32 token = PetscDefined(USE_64BIT_INDICES) ? FLUIDS_FILE_TOKEN_64 : FLUIDS_FILE_TOKEN_32;
3710de6a49fSJames Wright   PetscCall(PetscViewerBinaryWrite(viewer, &token, 1, PETSC_INT32));
3724de8550aSJed Brown   PetscCall(PetscViewerBinaryWrite(viewer, &step_no, 1, PETSC_INT));
3734de8550aSJed Brown   time /= user->units->second;  // Dimensionalize time back
3744de8550aSJed Brown   PetscCall(PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL));
375f14660a4SJames Wright   PetscCall(VecView(Q, viewer));
376f14660a4SJames Wright   PetscCall(PetscViewerDestroy(&viewer));
377ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
378f14660a4SJames Wright }
379f14660a4SJames Wright 
380ca69d878SAdeleke O. Bankole // CSV Monitor
381ca69d878SAdeleke O. Bankole PetscErrorCode TSMonitor_WallForce(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) {
382ca69d878SAdeleke O. Bankole   User              user = ctx;
383ca69d878SAdeleke O. Bankole   Vec               G_loc;
384ca69d878SAdeleke O. Bankole   PetscInt          num_wall = user->app_ctx->wall_forces.num_wall, dim = 3;
385ca69d878SAdeleke O. Bankole   const PetscInt   *walls  = user->app_ctx->wall_forces.walls;
386ca69d878SAdeleke O. Bankole   PetscViewer       viewer = user->app_ctx->wall_forces.viewer;
387ca69d878SAdeleke O. Bankole   PetscViewerFormat format = user->app_ctx->wall_forces.viewer_format;
388ca69d878SAdeleke O. Bankole   PetscScalar      *reaction_force;
389ca69d878SAdeleke O. Bankole   PetscBool         iascii;
390ca69d878SAdeleke O. Bankole 
391ca69d878SAdeleke O. Bankole   PetscFunctionBeginUser;
392ee4ca9cbSJames Wright   if (!viewer) PetscFunctionReturn(PETSC_SUCCESS);
393ca69d878SAdeleke O. Bankole   PetscCall(DMGetNamedLocalVector(user->dm, "ResidualLocal", &G_loc));
394ca69d878SAdeleke O. Bankole   PetscCall(PetscMalloc1(num_wall * dim, &reaction_force));
395ca69d878SAdeleke O. Bankole   PetscCall(Surface_Forces_NS(user->dm, G_loc, num_wall, walls, reaction_force));
396ca69d878SAdeleke O. Bankole   PetscCall(DMRestoreNamedLocalVector(user->dm, "ResidualLocal", &G_loc));
397ca69d878SAdeleke O. Bankole 
398ca69d878SAdeleke O. Bankole   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
399ca69d878SAdeleke O. Bankole 
400ca69d878SAdeleke O. Bankole   if (iascii) {
401ca69d878SAdeleke O. Bankole     if (format == PETSC_VIEWER_ASCII_CSV && !user->app_ctx->wall_forces.header_written) {
402ca69d878SAdeleke O. Bankole       PetscCall(PetscViewerASCIIPrintf(viewer, "Step,Time,Wall,ForceX,ForceY,ForceZ\n"));
403ca69d878SAdeleke O. Bankole       user->app_ctx->wall_forces.header_written = PETSC_TRUE;
404ca69d878SAdeleke O. Bankole     }
405ca69d878SAdeleke O. Bankole     for (PetscInt w = 0; w < num_wall; w++) {
406ca69d878SAdeleke O. Bankole       PetscInt wall = walls[w];
407ca69d878SAdeleke O. Bankole       if (format == PETSC_VIEWER_ASCII_CSV) {
408ca69d878SAdeleke O. Bankole         PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT ",%g,%" PetscInt_FMT ",%g,%g,%g\n", step_no, time, wall,
409ca69d878SAdeleke O. Bankole                                          reaction_force[w * dim + 0], reaction_force[w * dim + 1], reaction_force[w * dim + 2]));
410ca69d878SAdeleke O. Bankole 
411ca69d878SAdeleke O. Bankole       } else {
412ca69d878SAdeleke O. Bankole         PetscCall(PetscViewerASCIIPrintf(viewer, "Wall %" PetscInt_FMT " Forces: Force_x = %12g, Force_y = %12g, Force_z = %12g\n", wall,
413ca69d878SAdeleke O. Bankole                                          reaction_force[w * dim + 0], reaction_force[w * dim + 1], reaction_force[w * dim + 2]));
414ca69d878SAdeleke O. Bankole       }
415ca69d878SAdeleke O. Bankole     }
416ca69d878SAdeleke O. Bankole   }
417ca69d878SAdeleke O. Bankole   PetscCall(PetscFree(reaction_force));
418ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
419ca69d878SAdeleke O. Bankole }
420ca69d878SAdeleke O. Bankole 
421f14660a4SJames Wright // User provided TS Monitor
4222b730f8bSJeremy L Thompson PetscErrorCode TSMonitor_NS(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) {
423f14660a4SJames Wright   User user = ctx;
424f14660a4SJames Wright   PetscFunctionBeginUser;
425f14660a4SJames Wright 
42637cbb16aSJed Brown   // Print every 'checkpoint_interval' steps
427894de27cSJames Wright   if (user->app_ctx->checkpoint_interval <= 0 || step_no % user->app_ctx->checkpoint_interval != 0 ||
42849aac155SJeremy L Thompson       (user->app_ctx->cont_steps == step_no && step_no != 0)) {
429ee4ca9cbSJames Wright     PetscFunctionReturn(PETSC_SUCCESS);
43049aac155SJeremy L Thompson   }
431f14660a4SJames Wright 
432f14660a4SJames Wright   PetscCall(WriteOutput(user, Q, step_no, time));
43377841947SLeila Ghaffari 
434ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
43577841947SLeila Ghaffari }
43677841947SLeila Ghaffari 
43777841947SLeila Ghaffari // TS: Create, setup, and solve
4382b730f8bSJeremy L Thompson PetscErrorCode TSSolve_NS(DM dm, User user, AppCtx app_ctx, Physics phys, Vec *Q, PetscScalar *f_time, TS *ts) {
43977841947SLeila Ghaffari   MPI_Comm    comm = user->comm;
44077841947SLeila Ghaffari   TSAdapt     adapt;
44177841947SLeila Ghaffari   PetscScalar final_time;
44277841947SLeila Ghaffari   PetscFunctionBeginUser;
44377841947SLeila Ghaffari 
4442b730f8bSJeremy L Thompson   PetscCall(TSCreate(comm, ts));
4452b730f8bSJeremy L Thompson   PetscCall(TSSetDM(*ts, dm));
44677841947SLeila Ghaffari   if (phys->implicit) {
4472b730f8bSJeremy L Thompson     PetscCall(TSSetType(*ts, TSBDF));
44877841947SLeila Ghaffari     if (user->op_ifunction) {
4492b730f8bSJeremy L Thompson       PetscCall(TSSetIFunction(*ts, NULL, IFunction_NS, &user));
45077841947SLeila Ghaffari     } else {  // Implicit integrators can fall back to using an RHSFunction
4512b730f8bSJeremy L Thompson       PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user));
45277841947SLeila Ghaffari     }
453e334ad8fSJed Brown     if (user->op_ijacobian) {
4542b730f8bSJeremy L Thompson       PetscCall(DMTSSetIJacobian(dm, FormIJacobian_NS, &user));
455544be873SJed Brown       if (app_ctx->amat_type) {
456544be873SJed Brown         Mat Pmat, Amat;
4572b730f8bSJeremy L Thompson         PetscCall(DMCreateMatrix(dm, &Pmat));
4582b730f8bSJeremy L Thompson         PetscCall(DMSetMatType(dm, app_ctx->amat_type));
4592b730f8bSJeremy L Thompson         PetscCall(DMCreateMatrix(dm, &Amat));
4602b730f8bSJeremy L Thompson         PetscCall(TSSetIJacobian(*ts, Amat, Pmat, NULL, NULL));
4612b730f8bSJeremy L Thompson         PetscCall(MatDestroy(&Amat));
4622b730f8bSJeremy L Thompson         PetscCall(MatDestroy(&Pmat));
463544be873SJed Brown       }
464e334ad8fSJed Brown     }
46577841947SLeila Ghaffari   } else {
4669ad5e8e4SJames Wright     PetscCheck(user->op_rhs_ctx, comm, PETSC_ERR_ARG_NULL, "Problem does not provide RHSFunction");
4672b730f8bSJeremy L Thompson     PetscCall(TSSetType(*ts, TSRK));
4682b730f8bSJeremy L Thompson     PetscCall(TSRKSetType(*ts, TSRK5F));
4692b730f8bSJeremy L Thompson     PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user));
47077841947SLeila Ghaffari   }
4712b730f8bSJeremy L Thompson   PetscCall(TSSetMaxTime(*ts, 500. * user->units->second));
4722b730f8bSJeremy L Thompson   PetscCall(TSSetExactFinalTime(*ts, TS_EXACTFINALTIME_STEPOVER));
47351b00d91SJames Wright   if (app_ctx->test_type == TESTTYPE_NONE) PetscCall(TSSetErrorIfStepFails(*ts, PETSC_FALSE));
4742b730f8bSJeremy L Thompson   PetscCall(TSSetTimeStep(*ts, 1.e-2 * user->units->second));
4758fb33541SJames Wright   if (app_ctx->test_type != TESTTYPE_NONE) {
4762b730f8bSJeremy L Thompson     PetscCall(TSSetMaxSteps(*ts, 10));
4772b730f8bSJeremy L Thompson   }
4782b730f8bSJeremy L Thompson   PetscCall(TSGetAdapt(*ts, &adapt));
4792b730f8bSJeremy L Thompson   PetscCall(TSAdaptSetStepLimits(adapt, 1.e-12 * user->units->second, 1.e2 * user->units->second));
4802b730f8bSJeremy L Thompson   PetscCall(TSSetFromOptions(*ts));
48115637395SJames Wright   user->time_bc_set = -1.0;   // require all BCs be updated
482d55646a4SJames Wright   if (app_ctx->cont_steps) {  // continue from previous timestep data
48377841947SLeila Ghaffari     PetscInt    count;
48477841947SLeila Ghaffari     PetscViewer viewer;
4852b730f8bSJeremy L Thompson 
4864de8550aSJed Brown     if (app_ctx->cont_time <= 0) {  // Legacy files did not include step number and time
4872b730f8bSJeremy L Thompson       PetscCall(PetscViewerBinaryOpen(comm, app_ctx->cont_time_file, FILE_MODE_READ, &viewer));
4884de8550aSJed Brown       PetscCall(PetscViewerBinaryRead(viewer, &app_ctx->cont_time, 1, &count, PETSC_REAL));
4892b730f8bSJeremy L Thompson       PetscCall(PetscViewerDestroy(&viewer));
4904de8550aSJed Brown       PetscCheck(app_ctx->cont_steps != -1, comm, PETSC_ERR_ARG_INCOMP,
4914de8550aSJed Brown                  "-continue step number not specified, but checkpoint file does not contain a step number (likely written by older code version)");
4924de8550aSJed Brown     }
4934de8550aSJed Brown     PetscCall(TSSetTime(*ts, app_ctx->cont_time * user->units->second));
494d8e0aecdSJed Brown     PetscCall(TSSetStepNumber(*ts, app_ctx->cont_steps));
49577841947SLeila Ghaffari   }
4968fb33541SJames Wright   if (app_ctx->test_type == TESTTYPE_NONE) {
4972b730f8bSJeremy L Thompson     PetscCall(TSMonitorSet(*ts, TSMonitor_NS, user, NULL));
4988fb33541SJames Wright   }
499ca69d878SAdeleke O. Bankole   if (app_ctx->wall_forces.viewer) {
500ca69d878SAdeleke O. Bankole     PetscCall(TSMonitorSet(*ts, TSMonitor_WallForce, user, NULL));
501ca69d878SAdeleke O. Bankole   }
502b7d66439SJames Wright   if (app_ctx->turb_spanstats_enable) {
503f5452247SJames Wright     PetscCall(TSMonitorSet(*ts, TSMonitor_TurbulenceStatistics, user, NULL));
504495b9769SJames Wright     CeedScalar previous_time = app_ctx->cont_time * user->units->second;
505*a424bcd0SJames Wright     PetscCallCeed(user->ceed,
506*a424bcd0SJames Wright                   CeedOperatorSetContextDouble(user->spanstats.op_stats_collect_ctx->op, user->spanstats.previous_time_label, &previous_time));
507a175e481SJames Wright   }
5084e9802d1SJames Wright   if (app_ctx->diff_filter_monitor) PetscCall(TSMonitorSet(*ts, TSMonitor_DifferentialFilter, user, NULL));
50977841947SLeila Ghaffari 
51077841947SLeila Ghaffari   // Solve
511d8e0aecdSJed Brown   PetscReal start_time;
512d8e0aecdSJed Brown   PetscInt  start_step;
5132b730f8bSJeremy L Thompson   PetscCall(TSGetTime(*ts, &start_time));
514d8e0aecdSJed Brown   PetscCall(TSGetStepNumber(*ts, &start_step));
5157b39487dSJeremy L Thompson 
5167b39487dSJeremy L Thompson   PetscPreLoadBegin(PETSC_FALSE, "Fluids Solve");
5177b39487dSJeremy L Thompson   PetscCall(TSSetTime(*ts, start_time));
518d8e0aecdSJed Brown   PetscCall(TSSetStepNumber(*ts, start_step));
5197b39487dSJeremy L Thompson   if (PetscPreLoadingOn) {
5207b39487dSJeremy L Thompson     // LCOV_EXCL_START
5217b39487dSJeremy L Thompson     SNES      snes;
5227b39487dSJeremy L Thompson     Vec       Q_preload;
5237b39487dSJeremy L Thompson     PetscReal rtol;
5247b39487dSJeremy L Thompson     PetscCall(VecDuplicate(*Q, &Q_preload));
5257b39487dSJeremy L Thompson     PetscCall(VecCopy(*Q, Q_preload));
5267b39487dSJeremy L Thompson     PetscCall(TSGetSNES(*ts, &snes));
5277b39487dSJeremy L Thompson     PetscCall(SNESGetTolerances(snes, NULL, &rtol, NULL, NULL, NULL));
5282b730f8bSJeremy L Thompson     PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, .99, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT));
5297b39487dSJeremy L Thompson     PetscCall(TSSetSolution(*ts, *Q));
5307b39487dSJeremy L Thompson     PetscCall(TSStep(*ts));
5312b730f8bSJeremy L Thompson     PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, rtol, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT));
5327b39487dSJeremy L Thompson     PetscCall(VecDestroy(&Q_preload));
5337b39487dSJeremy L Thompson     // LCOV_EXCL_STOP
5347b39487dSJeremy L Thompson   } else {
5352b730f8bSJeremy L Thompson     PetscCall(PetscBarrier((PetscObject)*ts));
5362b730f8bSJeremy L Thompson     PetscCall(TSSolve(*ts, *Q));
5377b39487dSJeremy L Thompson   }
5387b39487dSJeremy L Thompson   PetscPreLoadEnd();
5397b39487dSJeremy L Thompson 
5407b39487dSJeremy L Thompson   PetscCall(TSGetSolveTime(*ts, &final_time));
54177841947SLeila Ghaffari   *f_time = final_time;
5427b39487dSJeremy L Thompson 
5438fb33541SJames Wright   if (app_ctx->test_type == TESTTYPE_NONE) {
544f14660a4SJames Wright     PetscInt step_no;
545f14660a4SJames Wright     PetscCall(TSGetStepNumber(*ts, &step_no));
546a175e481SJames Wright     if (user->app_ctx->checkpoint_interval > 0 || user->app_ctx->checkpoint_interval == -1) {
547f14660a4SJames Wright       PetscCall(WriteOutput(user, *Q, step_no, final_time));
548f14660a4SJames Wright     }
549f14660a4SJames Wright 
55075d1798cSJames Wright     PetscLogStage stage_id;
5517b39487dSJeremy L Thompson     PetscStageLog stage_log;
5527b39487dSJeremy L Thompson 
5537b39487dSJeremy L Thompson     PetscCall(PetscLogStageGetId("Fluids Solve", &stage_id));
5547b39487dSJeremy L Thompson     PetscCall(PetscLogGetStageLog(&stage_log));
5552b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Time taken for solution (sec): %g\n", stage_log->stageInfo[stage_id].perfInfo.time));
55677841947SLeila Ghaffari   }
557ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
55877841947SLeila Ghaffari }
559