xref: /libCEED/examples/fluids/src/setupts.c (revision 51b00d91281914488d9f9f96015afab384fcf51f)
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   Vec           M_loc;
2177841947SLeila Ghaffari   CeedQFunction qf_mass;
2277841947SLeila Ghaffari   CeedOperator  op_mass;
2377841947SLeila Ghaffari   CeedVector    m_ceed, ones_vec;
2477841947SLeila Ghaffari   CeedInt       num_comp_q, q_data_size;
2577841947SLeila Ghaffari   PetscFunctionBeginUser;
2677841947SLeila Ghaffari 
2777841947SLeila Ghaffari   // CEED Restriction
2877841947SLeila Ghaffari   CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_q, &num_comp_q);
2977841947SLeila Ghaffari   CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_qd_i, &q_data_size);
3077841947SLeila Ghaffari   CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &m_ceed, NULL);
3177841947SLeila Ghaffari   CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &ones_vec, NULL);
3277841947SLeila Ghaffari   CeedVectorSetValue(ones_vec, 1.0);
3377841947SLeila Ghaffari 
3477841947SLeila Ghaffari   // CEED QFunction
35ef080ff9SJames Wright   PetscCall(CreateMassQFunction(ceed, num_comp_q, q_data_size, &qf_mass));
3677841947SLeila Ghaffari 
3777841947SLeila Ghaffari   // CEED Operator
3877841947SLeila Ghaffari   CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass);
39ef080ff9SJames Wright   CeedOperatorSetField(op_mass, "u", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE);
402b730f8bSJeremy L Thompson   CeedOperatorSetField(op_mass, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data->q_data);
412b730f8bSJeremy L Thompson   CeedOperatorSetField(op_mass, "v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE);
4277841947SLeila Ghaffari 
4377841947SLeila Ghaffari   // Place PETSc vector in CEED vector
4477841947SLeila Ghaffari   PetscMemType m_mem_type;
452b730f8bSJeremy L Thompson   PetscCall(DMGetLocalVector(dm, &M_loc));
46c798d55aSJames Wright   PetscCall(VecP2C(M_loc, &m_mem_type, m_ceed));
4777841947SLeila Ghaffari 
4877841947SLeila Ghaffari   // Apply CEED Operator
4977841947SLeila Ghaffari   CeedOperatorApply(op_mass, ones_vec, m_ceed, CEED_REQUEST_IMMEDIATE);
5077841947SLeila Ghaffari 
5177841947SLeila Ghaffari   // Restore vectors
52c798d55aSJames Wright   PetscCall(VecC2P(m_ceed, m_mem_type, M_loc));
5377841947SLeila Ghaffari 
5477841947SLeila Ghaffari   // Local-to-Global
552b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(M));
562b730f8bSJeremy L Thompson   PetscCall(DMLocalToGlobal(dm, M_loc, ADD_VALUES, M));
572b730f8bSJeremy L Thompson   PetscCall(DMRestoreLocalVector(dm, &M_loc));
5877841947SLeila Ghaffari 
5977841947SLeila Ghaffari   // Invert diagonally lumped mass vector for RHS function
602b730f8bSJeremy L Thompson   PetscCall(VecReciprocal(M));
6177841947SLeila Ghaffari 
6277841947SLeila Ghaffari   // Cleanup
6377841947SLeila Ghaffari   CeedVectorDestroy(&ones_vec);
6477841947SLeila Ghaffari   CeedVectorDestroy(&m_ceed);
6577841947SLeila Ghaffari   CeedQFunctionDestroy(&qf_mass);
6677841947SLeila Ghaffari   CeedOperatorDestroy(&op_mass);
6777841947SLeila Ghaffari 
6877841947SLeila Ghaffari   PetscFunctionReturn(0);
6977841947SLeila Ghaffari }
7077841947SLeila Ghaffari 
71a0b9a424SJames Wright // Insert Boundary values if it's a new time
72a0b9a424SJames Wright PetscErrorCode UpdateBoundaryValues(User user, Vec Q_loc, PetscReal t) {
73a0b9a424SJames Wright   PetscFunctionBeginUser;
74a0b9a424SJames Wright   if (user->time_bc_set != t) {
75a0b9a424SJames Wright     PetscCall(DMPlexInsertBoundaryValues(user->dm, PETSC_TRUE, Q_loc, t, NULL, NULL, NULL));
76a0b9a424SJames Wright     user->time_bc_set = t;
77a0b9a424SJames Wright   }
78a0b9a424SJames Wright   PetscFunctionReturn(0);
79a0b9a424SJames Wright }
80a0b9a424SJames Wright 
8115637395SJames Wright // @brief Update the context label value to new value if necessary.
8215637395SJames Wright // @note This only supports labels with scalar label values (ie. not arrays)
8315637395SJames Wright PetscErrorCode UpdateContextLabel(MPI_Comm comm, PetscScalar update_value, CeedOperator op, CeedContextFieldLabel label) {
8415637395SJames Wright   PetscScalar label_value;
85e42c1df6SJames Wright 
8615637395SJames Wright   PetscFunctionBeginUser;
8715637395SJames Wright   PetscCheck(label, comm, PETSC_ERR_ARG_BADPTR, "Label should be non-NULL");
8815637395SJames Wright 
8915637395SJames Wright   {
9015637395SJames Wright     size_t             num_elements;
9115637395SJames Wright     const PetscScalar *label_values;
9215637395SJames Wright     CeedOperatorGetContextDoubleRead(op, label, &num_elements, &label_values);
9315637395SJames Wright     PetscCheck(num_elements == 1, comm, PETSC_ERR_SUP, "%s does not support labels with more than 1 value. Label has %zu values", __func__,
9415637395SJames Wright                num_elements);
9515637395SJames Wright     label_value = *label_values;
9615637395SJames Wright     CeedOperatorRestoreContextDoubleRead(op, label, &label_values);
97e42c1df6SJames Wright   }
9815637395SJames Wright 
9915637395SJames Wright   if (label_value != update_value) {
10015637395SJames Wright     CeedOperatorSetContextDouble(op, label, &update_value);
101e42c1df6SJames Wright   }
102e42c1df6SJames Wright   PetscFunctionReturn(0);
103e42c1df6SJames Wright }
104e42c1df6SJames Wright 
10577841947SLeila Ghaffari // RHS (Explicit time-stepper) function setup
10677841947SLeila Ghaffari //   This is the RHS of the ODE, given as u_t = G(t,u)
10777841947SLeila Ghaffari //   This function takes in a state vector Q and writes into G
10877841947SLeila Ghaffari PetscErrorCode RHS_NS(TS ts, PetscReal t, Vec Q, Vec G, void *user_data) {
10977841947SLeila Ghaffari   User         user = *(User *)user_data;
11015637395SJames Wright   MPI_Comm     comm = PetscObjectComm((PetscObject)ts);
111c798d55aSJames Wright   PetscScalar  dt;
1125e82a6e1SJeremy L Thompson   Vec          Q_loc = user->Q_loc, G_loc;
11377841947SLeila Ghaffari   PetscMemType q_mem_type, g_mem_type;
11477841947SLeila Ghaffari   PetscFunctionBeginUser;
11577841947SLeila Ghaffari 
1165e82a6e1SJeremy L Thompson   // Get local vector
1172b730f8bSJeremy L Thompson   PetscCall(DMGetLocalVector(user->dm, &G_loc));
1185e82a6e1SJeremy L Thompson 
1195e82a6e1SJeremy L Thompson   // Update time dependent data
120a0b9a424SJames Wright   PetscCall(UpdateBoundaryValues(user, Q_loc, t));
12115637395SJames Wright   if (user->phys->solution_time_label) PetscCall(UpdateContextLabel(comm, t, user->op_rhs, user->phys->solution_time_label));
1222b730f8bSJeremy L Thompson   PetscCall(TSGetTimeStep(ts, &dt));
12315637395SJames Wright   if (user->phys->timestep_size_label) PetscCall(UpdateContextLabel(comm, dt, user->op_rhs, user->phys->timestep_size_label));
12477841947SLeila Ghaffari 
12577841947SLeila Ghaffari   // Global-to-local
1262b730f8bSJeremy L Thompson   PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc));
12777841947SLeila Ghaffari 
12877841947SLeila Ghaffari   // Place PETSc vectors in CEED vectors
129c798d55aSJames Wright   PetscCall(VecReadP2C(Q_loc, &q_mem_type, user->q_ceed));
130c798d55aSJames Wright   PetscCall(VecP2C(G_loc, &g_mem_type, user->g_ceed));
13177841947SLeila Ghaffari 
13277841947SLeila Ghaffari   // Apply CEED operator
1332b730f8bSJeremy L Thompson   CeedOperatorApply(user->op_rhs, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE);
13477841947SLeila Ghaffari 
13577841947SLeila Ghaffari   // Restore vectors
136c798d55aSJames Wright   PetscCall(VecReadC2P(user->q_ceed, q_mem_type, Q_loc));
137c798d55aSJames Wright   PetscCall(VecC2P(user->g_ceed, g_mem_type, G_loc));
13877841947SLeila Ghaffari 
13977841947SLeila Ghaffari   // Local-to-Global
1402b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(G));
1412b730f8bSJeremy L Thompson   PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G));
14277841947SLeila Ghaffari 
14377841947SLeila Ghaffari   // Inverse of the lumped mass matrix (M is Minv)
1442b730f8bSJeremy L Thompson   PetscCall(VecPointwiseMult(G, G, user->M));
14577841947SLeila Ghaffari 
14677841947SLeila Ghaffari   // Restore vectors
1472b730f8bSJeremy L Thompson   PetscCall(DMRestoreLocalVector(user->dm, &G_loc));
14877841947SLeila Ghaffari 
14977841947SLeila Ghaffari   PetscFunctionReturn(0);
15077841947SLeila Ghaffari }
15177841947SLeila Ghaffari 
152ca69d878SAdeleke O. Bankole // Surface forces function setup
153ca69d878SAdeleke O. Bankole static PetscErrorCode Surface_Forces_NS(DM dm, Vec G_loc, PetscInt num_walls, const PetscInt walls[], PetscScalar *reaction_force) {
154ca69d878SAdeleke O. Bankole   DMLabel            face_label;
155ca69d878SAdeleke O. Bankole   const PetscScalar *g;
156d6734f85SAdeleke O. Bankole   PetscInt           dof, dim = 3;
157ca69d878SAdeleke O. Bankole   MPI_Comm           comm;
158d6734f85SAdeleke O. Bankole   PetscSection       s;
159ca69d878SAdeleke O. Bankole 
160ca69d878SAdeleke O. Bankole   PetscFunctionBeginUser;
161ca69d878SAdeleke O. Bankole   PetscCall(PetscArrayzero(reaction_force, num_walls * dim));
162ca69d878SAdeleke O. Bankole   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
163ca69d878SAdeleke O. Bankole   PetscCall(DMGetLabel(dm, "Face Sets", &face_label));
164ca69d878SAdeleke O. Bankole   PetscCall(VecGetArrayRead(G_loc, &g));
165ca69d878SAdeleke O. Bankole   for (PetscInt w = 0; w < num_walls; w++) {
166ca69d878SAdeleke O. Bankole     const PetscInt wall = walls[w];
167ca69d878SAdeleke O. Bankole     IS             wall_is;
168d6734f85SAdeleke O. Bankole     PetscCall(DMGetLocalSection(dm, &s));
169ca69d878SAdeleke O. Bankole     PetscCall(DMLabelGetStratumIS(face_label, wall, &wall_is));
170ca69d878SAdeleke O. Bankole     if (wall_is) {  // There exist such points on this process
171ca69d878SAdeleke O. Bankole       PetscInt        num_points;
172d6734f85SAdeleke O. Bankole       PetscInt        num_comp = 0;
173ca69d878SAdeleke O. Bankole       const PetscInt *points;
174d6734f85SAdeleke O. Bankole       PetscCall(PetscSectionGetFieldComponents(s, 0, &num_comp));
175ca69d878SAdeleke O. Bankole       PetscCall(ISGetSize(wall_is, &num_points));
176ca69d878SAdeleke O. Bankole       PetscCall(ISGetIndices(wall_is, &points));
177ca69d878SAdeleke O. Bankole       for (PetscInt i = 0; i < num_points; i++) {
178ca69d878SAdeleke O. Bankole         const PetscInt           p = points[i];
179ca69d878SAdeleke O. Bankole         const StateConservative *r;
180ca69d878SAdeleke O. Bankole         PetscCall(DMPlexPointLocalRead(dm, p, g, &r));
181d6734f85SAdeleke O. Bankole         PetscCall(PetscSectionGetDof(s, p, &dof));
182d6734f85SAdeleke O. Bankole         for (PetscInt node = 0; node < dof / num_comp; node++) {
183ca69d878SAdeleke O. Bankole           for (PetscInt j = 0; j < 3; j++) {
184d6734f85SAdeleke O. Bankole             reaction_force[w * dim + j] -= r[node].momentum[j];
185d6734f85SAdeleke O. Bankole           }
186ca69d878SAdeleke O. Bankole         }
187ca69d878SAdeleke O. Bankole       }
188ca69d878SAdeleke O. Bankole       PetscCall(ISRestoreIndices(wall_is, &points));
189ca69d878SAdeleke O. Bankole     }
190ca69d878SAdeleke O. Bankole     PetscCall(ISDestroy(&wall_is));
191ca69d878SAdeleke O. Bankole   }
192ca69d878SAdeleke O. Bankole   PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, reaction_force, dim * num_walls, MPIU_SCALAR, MPI_SUM, comm));
193ca69d878SAdeleke O. Bankole   //  Restore Vectors
194ca69d878SAdeleke O. Bankole   PetscCall(VecRestoreArrayRead(G_loc, &g));
195ca69d878SAdeleke O. Bankole 
196ca69d878SAdeleke O. Bankole   PetscFunctionReturn(0);
197ca69d878SAdeleke O. Bankole }
198ca69d878SAdeleke O. Bankole 
19977841947SLeila Ghaffari // Implicit time-stepper function setup
2002b730f8bSJeremy L Thompson PetscErrorCode IFunction_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, Vec G, void *user_data) {
20177841947SLeila Ghaffari   User         user = *(User *)user_data;
20215637395SJames Wright   MPI_Comm     comm = PetscObjectComm((PetscObject)ts);
203c798d55aSJames Wright   PetscScalar  dt;
2045e82a6e1SJeremy L Thompson   Vec          Q_loc = user->Q_loc, Q_dot_loc = user->Q_dot_loc, G_loc;
20577841947SLeila Ghaffari   PetscMemType q_mem_type, q_dot_mem_type, g_mem_type;
20677841947SLeila Ghaffari   PetscFunctionBeginUser;
20777841947SLeila Ghaffari 
2085e82a6e1SJeremy L Thompson   // Get local vectors
209ca69d878SAdeleke O. Bankole   PetscCall(DMGetNamedLocalVector(user->dm, "ResidualLocal", &G_loc));
2105e82a6e1SJeremy L Thompson 
2115e82a6e1SJeremy L Thompson   // Update time dependent data
212a0b9a424SJames Wright   PetscCall(UpdateBoundaryValues(user, Q_loc, t));
21315637395SJames Wright   if (user->phys->solution_time_label) PetscCall(UpdateContextLabel(comm, t, user->op_ifunction, user->phys->solution_time_label));
2142b730f8bSJeremy L Thompson   PetscCall(TSGetTimeStep(ts, &dt));
21515637395SJames Wright   if (user->phys->timestep_size_label) PetscCall(UpdateContextLabel(comm, dt, user->op_ifunction, user->phys->timestep_size_label));
21677841947SLeila Ghaffari 
21777841947SLeila Ghaffari   // Global-to-local
218878eb0e7SJames Wright   PetscCall(DMGlobalToLocalBegin(user->dm, Q, INSERT_VALUES, Q_loc));
219878eb0e7SJames Wright   PetscCall(DMGlobalToLocalBegin(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc));
220878eb0e7SJames Wright   PetscCall(DMGlobalToLocalEnd(user->dm, Q, INSERT_VALUES, Q_loc));
221878eb0e7SJames Wright   PetscCall(DMGlobalToLocalEnd(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc));
22277841947SLeila Ghaffari 
22377841947SLeila Ghaffari   // Place PETSc vectors in CEED vectors
224c798d55aSJames Wright   PetscCall(VecReadP2C(Q_loc, &q_mem_type, user->q_ceed));
225c798d55aSJames Wright   PetscCall(VecReadP2C(Q_dot_loc, &q_dot_mem_type, user->q_dot_ceed));
226c798d55aSJames Wright   PetscCall(VecP2C(G_loc, &g_mem_type, user->g_ceed));
22777841947SLeila Ghaffari 
22877841947SLeila Ghaffari   // Apply CEED operator
2292b730f8bSJeremy L Thompson   CeedOperatorApply(user->op_ifunction, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE);
23077841947SLeila Ghaffari 
23177841947SLeila Ghaffari   // Restore vectors
232c798d55aSJames Wright   PetscCall(VecReadC2P(user->q_ceed, q_mem_type, Q_loc));
233c798d55aSJames Wright   PetscCall(VecReadC2P(user->q_dot_ceed, q_dot_mem_type, Q_dot_loc));
234c798d55aSJames Wright   PetscCall(VecC2P(user->g_ceed, g_mem_type, G_loc));
23577841947SLeila Ghaffari 
23619ffbc25SJames Wright   if (user->app_ctx->sgs_model_type == SGS_MODEL_DATA_DRIVEN) {
23719ffbc25SJames Wright     PetscCall(SGS_DD_ModelApplyIFunction(user, Q_loc, G_loc));
23819ffbc25SJames Wright   }
239be34b3b7SJames Wright 
24077841947SLeila Ghaffari   // Local-to-Global
2412b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(G));
2422b730f8bSJeremy L Thompson   PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G));
24377841947SLeila Ghaffari 
24477841947SLeila Ghaffari   // Restore vectors
245ca69d878SAdeleke O. Bankole   PetscCall(DMRestoreNamedLocalVector(user->dm, "ResidualLocal", &G_loc));
24677841947SLeila Ghaffari 
24777841947SLeila Ghaffari   PetscFunctionReturn(0);
24877841947SLeila Ghaffari }
24977841947SLeila Ghaffari 
250e334ad8fSJed Brown static PetscErrorCode MatMult_NS_IJacobian(Mat J, Vec Q, Vec G) {
251e334ad8fSJed Brown   User               user;
252e334ad8fSJed Brown   const PetscScalar *q;
253e334ad8fSJed Brown   PetscScalar       *g;
254e334ad8fSJed Brown   PetscMemType       q_mem_type, g_mem_type;
255e334ad8fSJed Brown   PetscFunctionBeginUser;
2562b730f8bSJeremy L Thompson   PetscCall(MatShellGetContext(J, &user));
2575e82a6e1SJeremy L Thompson   Vec Q_loc = user->Q_dot_loc,  // Note - Q_dot_loc has zero BCs
2585e82a6e1SJeremy L Thompson       G_loc;
2595e82a6e1SJeremy L Thompson 
260e334ad8fSJed Brown   // Get local vectors
2612b730f8bSJeremy L Thompson   PetscCall(DMGetLocalVector(user->dm, &G_loc));
262e334ad8fSJed Brown 
263e334ad8fSJed Brown   // Global-to-local
2642b730f8bSJeremy L Thompson   PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc));
265e334ad8fSJed Brown 
266e334ad8fSJed Brown   // Place PETSc vectors in CEED vectors
2672b730f8bSJeremy L Thompson   PetscCall(VecGetArrayReadAndMemType(Q_loc, &q, &q_mem_type));
2682b730f8bSJeremy L Thompson   PetscCall(VecGetArrayAndMemType(G_loc, &g, &g_mem_type));
2692b730f8bSJeremy L Thompson   CeedVectorSetArray(user->q_ceed, MemTypeP2C(q_mem_type), CEED_USE_POINTER, (PetscScalar *)q);
270e334ad8fSJed Brown   CeedVectorSetArray(user->g_ceed, MemTypeP2C(g_mem_type), CEED_USE_POINTER, g);
271e334ad8fSJed Brown 
272e334ad8fSJed Brown   // Apply CEED operator
2732b730f8bSJeremy L Thompson   CeedOperatorApply(user->op_ijacobian, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE);
274e334ad8fSJed Brown 
275e334ad8fSJed Brown   // Restore vectors
276e334ad8fSJed Brown   CeedVectorTakeArray(user->q_ceed, MemTypeP2C(q_mem_type), NULL);
277e334ad8fSJed Brown   CeedVectorTakeArray(user->g_ceed, MemTypeP2C(g_mem_type), NULL);
2782b730f8bSJeremy L Thompson   PetscCall(VecRestoreArrayReadAndMemType(Q_loc, &q));
2792b730f8bSJeremy L Thompson   PetscCall(VecRestoreArrayAndMemType(G_loc, &g));
280e334ad8fSJed Brown 
281e334ad8fSJed Brown   // Local-to-Global
2822b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(G));
2832b730f8bSJeremy L Thompson   PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G));
284e334ad8fSJed Brown 
285e334ad8fSJed Brown   // Restore vectors
2862b730f8bSJeremy L Thompson   PetscCall(DMRestoreLocalVector(user->dm, &G_loc));
287e334ad8fSJed Brown   PetscFunctionReturn(0);
288e334ad8fSJed Brown }
289e334ad8fSJed Brown 
290e334ad8fSJed Brown PetscErrorCode MatGetDiagonal_NS_IJacobian(Mat A, Vec D) {
291e334ad8fSJed Brown   User         user;
292e334ad8fSJed Brown   Vec          D_loc;
293e334ad8fSJed Brown   PetscScalar *d;
294e334ad8fSJed Brown   PetscMemType mem_type;
295e334ad8fSJed Brown 
296e334ad8fSJed Brown   PetscFunctionBeginUser;
297e334ad8fSJed Brown   MatShellGetContext(A, &user);
298e334ad8fSJed Brown   PetscCall(DMGetLocalVector(user->dm, &D_loc));
299e334ad8fSJed Brown   PetscCall(VecGetArrayAndMemType(D_loc, &d, &mem_type));
300e334ad8fSJed Brown   CeedVectorSetArray(user->g_ceed, MemTypeP2C(mem_type), CEED_USE_POINTER, d);
3012b730f8bSJeremy L Thompson   CeedOperatorLinearAssembleDiagonal(user->op_ijacobian, user->g_ceed, CEED_REQUEST_IMMEDIATE);
302e334ad8fSJed Brown   CeedVectorTakeArray(user->g_ceed, MemTypeP2C(mem_type), NULL);
303e334ad8fSJed Brown   PetscCall(VecRestoreArrayAndMemType(D_loc, &d));
304e334ad8fSJed Brown   PetscCall(VecZeroEntries(D));
305e334ad8fSJed Brown   PetscCall(DMLocalToGlobal(user->dm, D_loc, ADD_VALUES, D));
306e334ad8fSJed Brown   PetscCall(DMRestoreLocalVector(user->dm, &D_loc));
307e334ad8fSJed Brown   VecViewFromOptions(D, NULL, "-diag_vec_view");
308e334ad8fSJed Brown   PetscFunctionReturn(0);
309e334ad8fSJed Brown }
310e334ad8fSJed Brown 
3112b730f8bSJeremy L Thompson static PetscErrorCode FormPreallocation(User user, PetscBool pbdiagonal, Mat J, CeedVector *coo_values) {
312544be873SJed Brown   PetscCount ncoo;
313544be873SJed Brown   PetscInt  *rows, *cols;
314544be873SJed Brown 
315544be873SJed Brown   PetscFunctionBeginUser;
316544be873SJed Brown   if (pbdiagonal) {
317544be873SJed Brown     CeedSize l_size;
318544be873SJed Brown     CeedOperatorGetActiveVectorLengths(user->op_ijacobian, &l_size, NULL);
319544be873SJed Brown     ncoo = l_size * 5;
320544be873SJed Brown     rows = malloc(ncoo * sizeof(rows[0]));
321544be873SJed Brown     cols = malloc(ncoo * sizeof(cols[0]));
322544be873SJed Brown     for (PetscCount n = 0; n < l_size / 5; n++) {
323544be873SJed Brown       for (PetscInt i = 0; i < 5; i++) {
324544be873SJed Brown         for (PetscInt j = 0; j < 5; j++) {
325544be873SJed Brown           rows[(n * 5 + i) * 5 + j] = n * 5 + i;
326544be873SJed Brown           cols[(n * 5 + i) * 5 + j] = n * 5 + j;
327544be873SJed Brown         }
328544be873SJed Brown       }
329544be873SJed Brown     }
330544be873SJed Brown   } else {
3312b730f8bSJeremy L Thompson     PetscCall(CeedOperatorLinearAssembleSymbolic(user->op_ijacobian, &ncoo, &rows, &cols));
332544be873SJed Brown   }
333544be873SJed Brown   PetscCall(MatSetPreallocationCOOLocal(J, ncoo, rows, cols));
334544be873SJed Brown   free(rows);
335544be873SJed Brown   free(cols);
336544be873SJed Brown   CeedVectorCreate(user->ceed, ncoo, coo_values);
337544be873SJed Brown   PetscFunctionReturn(0);
338544be873SJed Brown }
339544be873SJed Brown 
3402b730f8bSJeremy L Thompson static PetscErrorCode FormSetValues(User user, PetscBool pbdiagonal, Mat J, CeedVector coo_values) {
341544be873SJed Brown   CeedMemType        mem_type = CEED_MEM_HOST;
342544be873SJed Brown   const PetscScalar *values;
343544be873SJed Brown   MatType            mat_type;
344544be873SJed Brown 
345544be873SJed Brown   PetscFunctionBeginUser;
346544be873SJed Brown   PetscCall(MatGetType(J, &mat_type));
3472b730f8bSJeremy L Thompson   if (strstr(mat_type, "kokkos") || strstr(mat_type, "cusparse")) mem_type = CEED_MEM_DEVICE;
348544be873SJed Brown   if (user->app_ctx->pmat_pbdiagonal) {
3492b730f8bSJeremy L Thompson     CeedOperatorLinearAssemblePointBlockDiagonal(user->op_ijacobian, coo_values, CEED_REQUEST_IMMEDIATE);
350544be873SJed Brown   } else {
351544be873SJed Brown     CeedOperatorLinearAssemble(user->op_ijacobian, coo_values);
352544be873SJed Brown   }
353544be873SJed Brown   CeedVectorGetArrayRead(coo_values, mem_type, &values);
354544be873SJed Brown   PetscCall(MatSetValuesCOO(J, values, INSERT_VALUES));
355544be873SJed Brown   CeedVectorRestoreArrayRead(coo_values, &values);
356544be873SJed Brown   PetscFunctionReturn(0);
357544be873SJed Brown }
358544be873SJed Brown 
3592b730f8bSJeremy L Thompson PetscErrorCode FormIJacobian_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, PetscReal shift, Mat J, Mat J_pre, void *user_data) {
360e334ad8fSJed Brown   User      user = *(User *)user_data;
361d6bf345cSJed Brown   PetscBool J_is_shell, J_is_mffd, J_pre_is_shell;
362e334ad8fSJed Brown   PetscFunctionBeginUser;
36317b0d5c6SJeremy L Thompson   if (user->phys->ijacobian_time_shift_label) CeedOperatorSetContextDouble(user->op_ijacobian, user->phys->ijacobian_time_shift_label, &shift);
364d6bf345cSJed Brown   PetscCall(PetscObjectTypeCompare((PetscObject)J, MATMFFD, &J_is_mffd));
365e334ad8fSJed Brown   PetscCall(PetscObjectTypeCompare((PetscObject)J, MATSHELL, &J_is_shell));
3662b730f8bSJeremy L Thompson   PetscCall(PetscObjectTypeCompare((PetscObject)J_pre, MATSHELL, &J_pre_is_shell));
367e334ad8fSJed Brown   if (!user->matrices_set_up) {
368e334ad8fSJed Brown     if (J_is_shell) {
369e334ad8fSJed Brown       PetscCall(MatShellSetContext(J, user));
3702b730f8bSJeremy L Thompson       PetscCall(MatShellSetOperation(J, MATOP_MULT, (void (*)(void))MatMult_NS_IJacobian));
3712b730f8bSJeremy L Thompson       PetscCall(MatShellSetOperation(J, MATOP_GET_DIAGONAL, (void (*)(void))MatGetDiagonal_NS_IJacobian));
372e334ad8fSJed Brown       PetscCall(MatSetUp(J));
373e334ad8fSJed Brown     }
374e334ad8fSJed Brown     if (!J_pre_is_shell) {
3752b730f8bSJeremy L Thompson       PetscCall(FormPreallocation(user, user->app_ctx->pmat_pbdiagonal, J_pre, &user->coo_values_pmat));
376544be873SJed Brown     }
377d6bf345cSJed Brown     if (J != J_pre && !J_is_shell && !J_is_mffd) {
378544be873SJed Brown       PetscCall(FormPreallocation(user, PETSC_FALSE, J, &user->coo_values_amat));
379544be873SJed Brown     }
380e334ad8fSJed Brown     user->matrices_set_up = true;
381e334ad8fSJed Brown   }
382e334ad8fSJed Brown   if (!J_pre_is_shell) {
3832b730f8bSJeremy L Thompson     PetscCall(FormSetValues(user, user->app_ctx->pmat_pbdiagonal, J_pre, user->coo_values_pmat));
384e334ad8fSJed Brown   }
385d6bf345cSJed Brown   if (user->coo_values_amat) {
386d6bf345cSJed Brown     PetscCall(FormSetValues(user, PETSC_FALSE, J, user->coo_values_amat));
387d6bf345cSJed Brown   } else if (J_is_mffd) {
388d6bf345cSJed Brown     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
389d6bf345cSJed Brown     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
390d6bf345cSJed Brown   }
391e334ad8fSJed Brown   PetscFunctionReturn(0);
392e334ad8fSJed Brown }
393e334ad8fSJed Brown 
3942b730f8bSJeremy L Thompson PetscErrorCode WriteOutput(User user, Vec Q, PetscInt step_no, PetscScalar time) {
39577841947SLeila Ghaffari   Vec         Q_loc;
39677841947SLeila Ghaffari   char        file_path[PETSC_MAX_PATH_LEN];
39777841947SLeila Ghaffari   PetscViewer viewer;
39877841947SLeila Ghaffari   PetscFunctionBeginUser;
39977841947SLeila Ghaffari 
40037cbb16aSJed Brown   if (user->app_ctx->checkpoint_vtk) {
40177841947SLeila Ghaffari     // Set up output
402f14660a4SJames Wright     PetscCall(DMGetLocalVector(user->dm, &Q_loc));
403f14660a4SJames Wright     PetscCall(PetscObjectSetName((PetscObject)Q_loc, "StateVec"));
404f14660a4SJames Wright     PetscCall(VecZeroEntries(Q_loc));
405f14660a4SJames Wright     PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc));
40677841947SLeila Ghaffari 
40777841947SLeila Ghaffari     // Output
40837cbb16aSJed Brown     PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no));
409f14660a4SJames Wright 
4102b730f8bSJeremy L Thompson     PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q), file_path, FILE_MODE_WRITE, &viewer));
411f14660a4SJames Wright     PetscCall(VecView(Q_loc, viewer));
412f14660a4SJames Wright     PetscCall(PetscViewerDestroy(&viewer));
41377841947SLeila Ghaffari     if (user->dm_viz) {
41477841947SLeila Ghaffari       Vec         Q_refined, Q_refined_loc;
41577841947SLeila Ghaffari       char        file_path_refined[PETSC_MAX_PATH_LEN];
41677841947SLeila Ghaffari       PetscViewer viewer_refined;
41777841947SLeila Ghaffari 
418f14660a4SJames Wright       PetscCall(DMGetGlobalVector(user->dm_viz, &Q_refined));
419f14660a4SJames Wright       PetscCall(DMGetLocalVector(user->dm_viz, &Q_refined_loc));
420f14660a4SJames Wright       PetscCall(PetscObjectSetName((PetscObject)Q_refined_loc, "Refined"));
421f14660a4SJames Wright 
422f14660a4SJames Wright       PetscCall(MatInterpolate(user->interp_viz, Q, Q_refined));
423f14660a4SJames Wright       PetscCall(VecZeroEntries(Q_refined_loc));
4242b730f8bSJeremy L Thompson       PetscCall(DMGlobalToLocal(user->dm_viz, Q_refined, INSERT_VALUES, Q_refined_loc));
425f14660a4SJames Wright 
42637cbb16aSJed Brown       PetscCall(
42737cbb16aSJed Brown           PetscSNPrintf(file_path_refined, sizeof file_path_refined, "%s/nsrefined-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no));
428f14660a4SJames Wright 
4292b730f8bSJeremy L Thompson       PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q_refined), file_path_refined, FILE_MODE_WRITE, &viewer_refined));
430f14660a4SJames Wright       PetscCall(VecView(Q_refined_loc, viewer_refined));
431f14660a4SJames Wright       PetscCall(DMRestoreLocalVector(user->dm_viz, &Q_refined_loc));
432f14660a4SJames Wright       PetscCall(DMRestoreGlobalVector(user->dm_viz, &Q_refined));
433f14660a4SJames Wright       PetscCall(PetscViewerDestroy(&viewer_refined));
43477841947SLeila Ghaffari     }
435f14660a4SJames Wright     PetscCall(DMRestoreLocalVector(user->dm, &Q_loc));
43637cbb16aSJed Brown   }
43777841947SLeila Ghaffari 
43877841947SLeila Ghaffari   // Save data in a binary file for continuation of simulations
43969293791SJames Wright   if (user->app_ctx->add_stepnum2bin) {
44037cbb16aSJed Brown     PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution-%" PetscInt_FMT ".bin", user->app_ctx->output_dir, step_no));
44169293791SJames Wright   } else {
4422b730f8bSJeremy L Thompson     PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution.bin", user->app_ctx->output_dir));
44369293791SJames Wright   }
4442b730f8bSJeremy L Thompson   PetscCall(PetscViewerBinaryOpen(user->comm, file_path, FILE_MODE_WRITE, &viewer));
445f14660a4SJames Wright 
4464de8550aSJed Brown   PetscInt token = FLUIDS_FILE_TOKEN;
4474de8550aSJed Brown   PetscCall(PetscViewerBinaryWrite(viewer, &token, 1, PETSC_INT));
4484de8550aSJed Brown   PetscCall(PetscViewerBinaryWrite(viewer, &step_no, 1, PETSC_INT));
4494de8550aSJed Brown   time /= user->units->second;  // Dimensionalize time back
4504de8550aSJed Brown   PetscCall(PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL));
451f14660a4SJames Wright   PetscCall(VecView(Q, viewer));
452f14660a4SJames Wright   PetscCall(PetscViewerDestroy(&viewer));
453f14660a4SJames Wright   PetscFunctionReturn(0);
454f14660a4SJames Wright }
455f14660a4SJames Wright 
456ca69d878SAdeleke O. Bankole // CSV Monitor
457ca69d878SAdeleke O. Bankole PetscErrorCode TSMonitor_WallForce(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) {
458ca69d878SAdeleke O. Bankole   User              user = ctx;
459ca69d878SAdeleke O. Bankole   Vec               G_loc;
460ca69d878SAdeleke O. Bankole   PetscInt          num_wall = user->app_ctx->wall_forces.num_wall, dim = 3;
461ca69d878SAdeleke O. Bankole   const PetscInt   *walls  = user->app_ctx->wall_forces.walls;
462ca69d878SAdeleke O. Bankole   PetscViewer       viewer = user->app_ctx->wall_forces.viewer;
463ca69d878SAdeleke O. Bankole   PetscViewerFormat format = user->app_ctx->wall_forces.viewer_format;
464ca69d878SAdeleke O. Bankole   PetscScalar      *reaction_force;
465ca69d878SAdeleke O. Bankole   PetscBool         iascii;
466ca69d878SAdeleke O. Bankole 
467ca69d878SAdeleke O. Bankole   PetscFunctionBeginUser;
468ca69d878SAdeleke O. Bankole   if (!viewer) PetscFunctionReturn(0);
469ca69d878SAdeleke O. Bankole   PetscCall(DMGetNamedLocalVector(user->dm, "ResidualLocal", &G_loc));
470ca69d878SAdeleke O. Bankole   PetscCall(PetscMalloc1(num_wall * dim, &reaction_force));
471ca69d878SAdeleke O. Bankole   PetscCall(Surface_Forces_NS(user->dm, G_loc, num_wall, walls, reaction_force));
472ca69d878SAdeleke O. Bankole   PetscCall(DMRestoreNamedLocalVector(user->dm, "ResidualLocal", &G_loc));
473ca69d878SAdeleke O. Bankole 
474ca69d878SAdeleke O. Bankole   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
475ca69d878SAdeleke O. Bankole 
476ca69d878SAdeleke O. Bankole   if (iascii) {
477ca69d878SAdeleke O. Bankole     if (format == PETSC_VIEWER_ASCII_CSV && !user->app_ctx->wall_forces.header_written) {
478ca69d878SAdeleke O. Bankole       PetscCall(PetscViewerASCIIPrintf(viewer, "Step,Time,Wall,ForceX,ForceY,ForceZ\n"));
479ca69d878SAdeleke O. Bankole       user->app_ctx->wall_forces.header_written = PETSC_TRUE;
480ca69d878SAdeleke O. Bankole     }
481ca69d878SAdeleke O. Bankole     for (PetscInt w = 0; w < num_wall; w++) {
482ca69d878SAdeleke O. Bankole       PetscInt wall = walls[w];
483ca69d878SAdeleke O. Bankole       if (format == PETSC_VIEWER_ASCII_CSV) {
484ca69d878SAdeleke O. Bankole         PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT ",%g,%" PetscInt_FMT ",%g,%g,%g\n", step_no, time, wall,
485ca69d878SAdeleke O. Bankole                                          reaction_force[w * dim + 0], reaction_force[w * dim + 1], reaction_force[w * dim + 2]));
486ca69d878SAdeleke O. Bankole 
487ca69d878SAdeleke O. Bankole       } else {
488ca69d878SAdeleke O. Bankole         PetscCall(PetscViewerASCIIPrintf(viewer, "Wall %" PetscInt_FMT " Forces: Force_x = %12g, Force_y = %12g, Force_z = %12g\n", wall,
489ca69d878SAdeleke O. Bankole                                          reaction_force[w * dim + 0], reaction_force[w * dim + 1], reaction_force[w * dim + 2]));
490ca69d878SAdeleke O. Bankole       }
491ca69d878SAdeleke O. Bankole     }
492ca69d878SAdeleke O. Bankole   }
493ca69d878SAdeleke O. Bankole   PetscCall(PetscFree(reaction_force));
494ca69d878SAdeleke O. Bankole   PetscFunctionReturn(0);
495ca69d878SAdeleke O. Bankole }
496ca69d878SAdeleke O. Bankole 
497f14660a4SJames Wright // User provided TS Monitor
4982b730f8bSJeremy L Thompson PetscErrorCode TSMonitor_NS(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) {
499f14660a4SJames Wright   User user = ctx;
500f14660a4SJames Wright   PetscFunctionBeginUser;
501f14660a4SJames Wright 
50237cbb16aSJed Brown   // Print every 'checkpoint_interval' steps
503894de27cSJames Wright   if (user->app_ctx->checkpoint_interval <= 0 || step_no % user->app_ctx->checkpoint_interval != 0 ||
50449aac155SJeremy L Thompson       (user->app_ctx->cont_steps == step_no && step_no != 0)) {
505894de27cSJames Wright     PetscFunctionReturn(0);
50649aac155SJeremy L Thompson   }
507f14660a4SJames Wright 
508f14660a4SJames Wright   PetscCall(WriteOutput(user, Q, step_no, time));
50977841947SLeila Ghaffari 
51077841947SLeila Ghaffari   PetscFunctionReturn(0);
51177841947SLeila Ghaffari }
51277841947SLeila Ghaffari 
51377841947SLeila Ghaffari // TS: Create, setup, and solve
5142b730f8bSJeremy L Thompson PetscErrorCode TSSolve_NS(DM dm, User user, AppCtx app_ctx, Physics phys, Vec *Q, PetscScalar *f_time, TS *ts) {
51577841947SLeila Ghaffari   MPI_Comm    comm = user->comm;
51677841947SLeila Ghaffari   TSAdapt     adapt;
51777841947SLeila Ghaffari   PetscScalar final_time;
51877841947SLeila Ghaffari   PetscFunctionBeginUser;
51977841947SLeila Ghaffari 
5202b730f8bSJeremy L Thompson   PetscCall(TSCreate(comm, ts));
5212b730f8bSJeremy L Thompson   PetscCall(TSSetDM(*ts, dm));
52277841947SLeila Ghaffari   if (phys->implicit) {
5232b730f8bSJeremy L Thompson     PetscCall(TSSetType(*ts, TSBDF));
52477841947SLeila Ghaffari     if (user->op_ifunction) {
5252b730f8bSJeremy L Thompson       PetscCall(TSSetIFunction(*ts, NULL, IFunction_NS, &user));
52677841947SLeila Ghaffari     } else {  // Implicit integrators can fall back to using an RHSFunction
5272b730f8bSJeremy L Thompson       PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user));
52877841947SLeila Ghaffari     }
529e334ad8fSJed Brown     if (user->op_ijacobian) {
5302b730f8bSJeremy L Thompson       PetscCall(DMTSSetIJacobian(dm, FormIJacobian_NS, &user));
531544be873SJed Brown       if (app_ctx->amat_type) {
532544be873SJed Brown         Mat Pmat, Amat;
5332b730f8bSJeremy L Thompson         PetscCall(DMCreateMatrix(dm, &Pmat));
5342b730f8bSJeremy L Thompson         PetscCall(DMSetMatType(dm, app_ctx->amat_type));
5352b730f8bSJeremy L Thompson         PetscCall(DMCreateMatrix(dm, &Amat));
5362b730f8bSJeremy L Thompson         PetscCall(TSSetIJacobian(*ts, Amat, Pmat, NULL, NULL));
5372b730f8bSJeremy L Thompson         PetscCall(MatDestroy(&Amat));
5382b730f8bSJeremy L Thompson         PetscCall(MatDestroy(&Pmat));
539544be873SJed Brown       }
540e334ad8fSJed Brown     }
54177841947SLeila Ghaffari   } else {
5422b730f8bSJeremy L Thompson     if (!user->op_rhs) SETERRQ(comm, PETSC_ERR_ARG_NULL, "Problem does not provide RHSFunction");
5432b730f8bSJeremy L Thompson     PetscCall(TSSetType(*ts, TSRK));
5442b730f8bSJeremy L Thompson     PetscCall(TSRKSetType(*ts, TSRK5F));
5452b730f8bSJeremy L Thompson     PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user));
54677841947SLeila Ghaffari   }
5472b730f8bSJeremy L Thompson   PetscCall(TSSetMaxTime(*ts, 500. * user->units->second));
5482b730f8bSJeremy L Thompson   PetscCall(TSSetExactFinalTime(*ts, TS_EXACTFINALTIME_STEPOVER));
549*51b00d91SJames Wright   if (app_ctx->test_type == TESTTYPE_NONE) PetscCall(TSSetErrorIfStepFails(*ts, PETSC_FALSE));
5502b730f8bSJeremy L Thompson   PetscCall(TSSetTimeStep(*ts, 1.e-2 * user->units->second));
5518fb33541SJames Wright   if (app_ctx->test_type != TESTTYPE_NONE) {
5522b730f8bSJeremy L Thompson     PetscCall(TSSetMaxSteps(*ts, 10));
5532b730f8bSJeremy L Thompson   }
5542b730f8bSJeremy L Thompson   PetscCall(TSGetAdapt(*ts, &adapt));
5552b730f8bSJeremy L Thompson   PetscCall(TSAdaptSetStepLimits(adapt, 1.e-12 * user->units->second, 1.e2 * user->units->second));
5562b730f8bSJeremy L Thompson   PetscCall(TSSetFromOptions(*ts));
55715637395SJames Wright   user->time_bc_set = -1.0;    // require all BCs be updated
55877841947SLeila Ghaffari   if (!app_ctx->cont_steps) {  // print initial condition
5598fb33541SJames Wright     if (app_ctx->test_type == TESTTYPE_NONE) {
5602b730f8bSJeremy L Thompson       PetscCall(TSMonitor_NS(*ts, 0, 0., *Q, user));
56177841947SLeila Ghaffari     }
56277841947SLeila Ghaffari   } else {  // continue from time of last output
56377841947SLeila Ghaffari     PetscInt    count;
56477841947SLeila Ghaffari     PetscViewer viewer;
5652b730f8bSJeremy L Thompson 
5664de8550aSJed Brown     if (app_ctx->cont_time <= 0) {  // Legacy files did not include step number and time
5672b730f8bSJeremy L Thompson       PetscCall(PetscViewerBinaryOpen(comm, app_ctx->cont_time_file, FILE_MODE_READ, &viewer));
5684de8550aSJed Brown       PetscCall(PetscViewerBinaryRead(viewer, &app_ctx->cont_time, 1, &count, PETSC_REAL));
5692b730f8bSJeremy L Thompson       PetscCall(PetscViewerDestroy(&viewer));
5704de8550aSJed Brown       PetscCheck(app_ctx->cont_steps != -1, comm, PETSC_ERR_ARG_INCOMP,
5714de8550aSJed Brown                  "-continue step number not specified, but checkpoint file does not contain a step number (likely written by older code version)");
5724de8550aSJed Brown     }
5734de8550aSJed Brown     PetscCall(TSSetTime(*ts, app_ctx->cont_time * user->units->second));
574d8e0aecdSJed Brown     PetscCall(TSSetStepNumber(*ts, app_ctx->cont_steps));
57577841947SLeila Ghaffari   }
5768fb33541SJames Wright   if (app_ctx->test_type == TESTTYPE_NONE) {
5772b730f8bSJeremy L Thompson     PetscCall(TSMonitorSet(*ts, TSMonitor_NS, user, NULL));
5788fb33541SJames Wright   }
579ca69d878SAdeleke O. Bankole   if (app_ctx->wall_forces.viewer) {
580ca69d878SAdeleke O. Bankole     PetscCall(TSMonitorSet(*ts, TSMonitor_WallForce, user, NULL));
581ca69d878SAdeleke O. Bankole   }
582b7d66439SJames Wright   if (app_ctx->turb_spanstats_enable) {
583a175e481SJames Wright     PetscCall(TSMonitorSet(*ts, TSMonitor_Statistics, user, NULL));
584495b9769SJames Wright     CeedScalar previous_time = app_ctx->cont_time * user->units->second;
58517b0d5c6SJeremy L Thompson     CeedOperatorSetContextDouble(user->spanstats.op_stats_collect, user->spanstats.previous_time_label, &previous_time);
586a175e481SJames Wright   }
58777841947SLeila Ghaffari 
58877841947SLeila Ghaffari   // Solve
589d8e0aecdSJed Brown   PetscReal start_time;
590d8e0aecdSJed Brown   PetscInt  start_step;
5912b730f8bSJeremy L Thompson   PetscCall(TSGetTime(*ts, &start_time));
592d8e0aecdSJed Brown   PetscCall(TSGetStepNumber(*ts, &start_step));
5937b39487dSJeremy L Thompson 
5947b39487dSJeremy L Thompson   PetscPreLoadBegin(PETSC_FALSE, "Fluids Solve");
5957b39487dSJeremy L Thompson   PetscCall(TSSetTime(*ts, start_time));
596d8e0aecdSJed Brown   PetscCall(TSSetStepNumber(*ts, start_step));
5977b39487dSJeremy L Thompson   if (PetscPreLoadingOn) {
5987b39487dSJeremy L Thompson     // LCOV_EXCL_START
5997b39487dSJeremy L Thompson     SNES      snes;
6007b39487dSJeremy L Thompson     Vec       Q_preload;
6017b39487dSJeremy L Thompson     PetscReal rtol;
6027b39487dSJeremy L Thompson     PetscCall(VecDuplicate(*Q, &Q_preload));
6037b39487dSJeremy L Thompson     PetscCall(VecCopy(*Q, Q_preload));
6047b39487dSJeremy L Thompson     PetscCall(TSGetSNES(*ts, &snes));
6057b39487dSJeremy L Thompson     PetscCall(SNESGetTolerances(snes, NULL, &rtol, NULL, NULL, NULL));
6062b730f8bSJeremy L Thompson     PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, .99, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT));
6077b39487dSJeremy L Thompson     PetscCall(TSSetSolution(*ts, *Q));
6087b39487dSJeremy L Thompson     PetscCall(TSStep(*ts));
6092b730f8bSJeremy L Thompson     PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, rtol, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT));
6107b39487dSJeremy L Thompson     PetscCall(VecDestroy(&Q_preload));
6117b39487dSJeremy L Thompson     // LCOV_EXCL_STOP
6127b39487dSJeremy L Thompson   } else {
6132b730f8bSJeremy L Thompson     PetscCall(PetscBarrier((PetscObject)*ts));
6142b730f8bSJeremy L Thompson     PetscCall(TSSolve(*ts, *Q));
6157b39487dSJeremy L Thompson   }
6167b39487dSJeremy L Thompson   PetscPreLoadEnd();
6177b39487dSJeremy L Thompson 
6187b39487dSJeremy L Thompson   PetscCall(TSGetSolveTime(*ts, &final_time));
61977841947SLeila Ghaffari   *f_time = final_time;
6207b39487dSJeremy L Thompson 
6218fb33541SJames Wright   if (app_ctx->test_type == TESTTYPE_NONE) {
622f14660a4SJames Wright     PetscInt step_no;
623f14660a4SJames Wright     PetscCall(TSGetStepNumber(*ts, &step_no));
624a175e481SJames Wright     if (user->app_ctx->checkpoint_interval > 0 || user->app_ctx->checkpoint_interval == -1) {
625f14660a4SJames Wright       PetscCall(WriteOutput(user, *Q, step_no, final_time));
626f14660a4SJames Wright     }
627f14660a4SJames Wright 
6287b39487dSJeremy L Thompson     PetscLogEvent stage_id;
6297b39487dSJeremy L Thompson     PetscStageLog stage_log;
6307b39487dSJeremy L Thompson 
6317b39487dSJeremy L Thompson     PetscCall(PetscLogStageGetId("Fluids Solve", &stage_id));
6327b39487dSJeremy L Thompson     PetscCall(PetscLogGetStageLog(&stage_log));
6332b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Time taken for solution (sec): %g\n", stage_log->stageInfo[stage_id].perfInfo.time));
63477841947SLeila Ghaffari   }
63577841947SLeila Ghaffari   PetscFunctionReturn(0);
63677841947SLeila Ghaffari }
637