xref: /honee/src/setupts.c (revision f9028c3cd083ddea6b4696730e3541a4c86420e3)
1727da7e7SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2727da7e7SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3a515125bSLeila Ghaffari //
4727da7e7SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5a515125bSLeila Ghaffari //
6727da7e7SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7a515125bSLeila Ghaffari 
8a515125bSLeila Ghaffari /// @file
9a515125bSLeila Ghaffari /// Time-stepping functions for Navier-Stokes example using PETSc
10a515125bSLeila Ghaffari 
11e419654dSJeremy L Thompson #include <ceed.h>
12e419654dSJeremy L Thompson #include <petscdmplex.h>
13e419654dSJeremy L Thompson #include <petscts.h>
14e419654dSJeremy L Thompson 
15a515125bSLeila Ghaffari #include "../navierstokes.h"
16c5e9980aSAdeleke O. Bankole #include "../qfunctions/newtonian_state.h"
17a515125bSLeila Ghaffari 
18a515125bSLeila Ghaffari // Compute mass matrix for explicit scheme
192b916ea7SJeremy L Thompson PetscErrorCode ComputeLumpedMassMatrix(Ceed ceed, DM dm, CeedData ceed_data, Vec M) {
20a515125bSLeila Ghaffari   Vec           M_loc;
21a515125bSLeila Ghaffari   CeedQFunction qf_mass;
22a515125bSLeila Ghaffari   CeedOperator  op_mass;
23a515125bSLeila Ghaffari   CeedVector    m_ceed, ones_vec;
24a515125bSLeila Ghaffari   CeedInt       num_comp_q, q_data_size;
25a515125bSLeila Ghaffari   PetscFunctionBeginUser;
26a515125bSLeila Ghaffari 
27a515125bSLeila Ghaffari   // CEED Restriction
28a515125bSLeila Ghaffari   CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_q, &num_comp_q);
29a515125bSLeila Ghaffari   CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_qd_i, &q_data_size);
30a515125bSLeila Ghaffari   CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &m_ceed, NULL);
31a515125bSLeila Ghaffari   CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &ones_vec, NULL);
32a515125bSLeila Ghaffari   CeedVectorSetValue(ones_vec, 1.0);
33a515125bSLeila Ghaffari 
34a515125bSLeila Ghaffari   // CEED QFunction
359f59f36eSJames Wright   PetscCall(CreateMassQFunction(ceed, num_comp_q, q_data_size, &qf_mass));
36a515125bSLeila Ghaffari 
37a515125bSLeila Ghaffari   // CEED Operator
38a515125bSLeila Ghaffari   CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass);
399f59f36eSJames Wright   CeedOperatorSetField(op_mass, "u", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE);
402b916ea7SJeremy L Thompson   CeedOperatorSetField(op_mass, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data->q_data);
412b916ea7SJeremy L Thompson   CeedOperatorSetField(op_mass, "v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE);
42a515125bSLeila Ghaffari 
43a515125bSLeila Ghaffari   // Place PETSc vector in CEED vector
44a515125bSLeila Ghaffari   PetscMemType m_mem_type;
452b916ea7SJeremy L Thompson   PetscCall(DMGetLocalVector(dm, &M_loc));
46fd969b44SJames Wright   PetscCall(VecP2C(M_loc, &m_mem_type, m_ceed));
47a515125bSLeila Ghaffari 
48a515125bSLeila Ghaffari   // Apply CEED Operator
49a515125bSLeila Ghaffari   CeedOperatorApply(op_mass, ones_vec, m_ceed, CEED_REQUEST_IMMEDIATE);
50a515125bSLeila Ghaffari 
51a515125bSLeila Ghaffari   // Restore vectors
52fd969b44SJames Wright   PetscCall(VecC2P(m_ceed, m_mem_type, M_loc));
53a515125bSLeila Ghaffari 
54a515125bSLeila Ghaffari   // Local-to-Global
552b916ea7SJeremy L Thompson   PetscCall(VecZeroEntries(M));
562b916ea7SJeremy L Thompson   PetscCall(DMLocalToGlobal(dm, M_loc, ADD_VALUES, M));
572b916ea7SJeremy L Thompson   PetscCall(DMRestoreLocalVector(dm, &M_loc));
58a515125bSLeila Ghaffari 
59a515125bSLeila Ghaffari   // Invert diagonally lumped mass vector for RHS function
602b916ea7SJeremy L Thompson   PetscCall(VecReciprocal(M));
61a515125bSLeila Ghaffari 
62a515125bSLeila Ghaffari   // Cleanup
63a515125bSLeila Ghaffari   CeedVectorDestroy(&ones_vec);
64a515125bSLeila Ghaffari   CeedVectorDestroy(&m_ceed);
65a515125bSLeila Ghaffari   CeedQFunctionDestroy(&qf_mass);
66a515125bSLeila Ghaffari   CeedOperatorDestroy(&op_mass);
67a515125bSLeila Ghaffari 
68a515125bSLeila Ghaffari   PetscFunctionReturn(0);
69a515125bSLeila Ghaffari }
70a515125bSLeila Ghaffari 
71c996854bSJames Wright // Insert Boundary values if it's a new time
72c996854bSJames Wright PetscErrorCode UpdateBoundaryValues(User user, Vec Q_loc, PetscReal t) {
73c996854bSJames Wright   PetscFunctionBeginUser;
74c996854bSJames Wright   if (user->time_bc_set != t) {
75c996854bSJames Wright     PetscCall(DMPlexInsertBoundaryValues(user->dm, PETSC_TRUE, Q_loc, t, NULL, NULL, NULL));
76c996854bSJames Wright     user->time_bc_set = t;
77c996854bSJames Wright   }
78c996854bSJames Wright   PetscFunctionReturn(0);
79c996854bSJames Wright }
80c996854bSJames Wright 
8191f639d2SJames Wright // @brief Update the context label value to new value if necessary.
8291f639d2SJames Wright // @note This only supports labels with scalar label values (ie. not arrays)
8391f639d2SJames Wright PetscErrorCode UpdateContextLabel(MPI_Comm comm, PetscScalar update_value, CeedOperator op, CeedContextFieldLabel label) {
8491f639d2SJames Wright   PetscScalar label_value;
85c2cb7fc8SJames Wright 
8691f639d2SJames Wright   PetscFunctionBeginUser;
8791f639d2SJames Wright   PetscCheck(label, comm, PETSC_ERR_ARG_BADPTR, "Label should be non-NULL");
8891f639d2SJames Wright 
8991f639d2SJames Wright   {
9091f639d2SJames Wright     size_t             num_elements;
9191f639d2SJames Wright     const PetscScalar *label_values;
9291f639d2SJames Wright     CeedOperatorGetContextDoubleRead(op, label, &num_elements, &label_values);
9391f639d2SJames Wright     PetscCheck(num_elements == 1, comm, PETSC_ERR_SUP, "%s does not support labels with more than 1 value. Label has %zu values", __func__,
9491f639d2SJames Wright                num_elements);
9591f639d2SJames Wright     label_value = *label_values;
9691f639d2SJames Wright     CeedOperatorRestoreContextDoubleRead(op, label, &label_values);
97c2cb7fc8SJames Wright   }
9891f639d2SJames Wright 
9991f639d2SJames Wright   if (label_value != update_value) {
10091f639d2SJames Wright     CeedOperatorSetContextDouble(op, label, &update_value);
101c2cb7fc8SJames Wright   }
102c2cb7fc8SJames Wright   PetscFunctionReturn(0);
103c2cb7fc8SJames Wright }
104c2cb7fc8SJames Wright 
105a515125bSLeila Ghaffari // RHS (Explicit time-stepper) function setup
106a515125bSLeila Ghaffari //   This is the RHS of the ODE, given as u_t = G(t,u)
107a515125bSLeila Ghaffari //   This function takes in a state vector Q and writes into G
108a515125bSLeila Ghaffari PetscErrorCode RHS_NS(TS ts, PetscReal t, Vec Q, Vec G, void *user_data) {
109a515125bSLeila Ghaffari   User         user = *(User *)user_data;
11091f639d2SJames Wright   MPI_Comm     comm = PetscObjectComm((PetscObject)ts);
111fd969b44SJames Wright   PetscScalar  dt;
112e2f84137SJeremy L Thompson   Vec          Q_loc = user->Q_loc, G_loc;
113a515125bSLeila Ghaffari   PetscMemType q_mem_type, g_mem_type;
114a515125bSLeila Ghaffari   PetscFunctionBeginUser;
115a515125bSLeila Ghaffari 
116e2f84137SJeremy L Thompson   // Get local vector
1172b916ea7SJeremy L Thompson   PetscCall(DMGetLocalVector(user->dm, &G_loc));
118e2f84137SJeremy L Thompson 
119e2f84137SJeremy L Thompson   // Update time dependent data
120c996854bSJames Wright   PetscCall(UpdateBoundaryValues(user, Q_loc, t));
12191f639d2SJames Wright   if (user->phys->solution_time_label) PetscCall(UpdateContextLabel(comm, t, user->op_rhs, user->phys->solution_time_label));
1222b916ea7SJeremy L Thompson   PetscCall(TSGetTimeStep(ts, &dt));
12391f639d2SJames Wright   if (user->phys->timestep_size_label) PetscCall(UpdateContextLabel(comm, dt, user->op_rhs, user->phys->timestep_size_label));
124a515125bSLeila Ghaffari 
125a515125bSLeila Ghaffari   // Global-to-local
1262b916ea7SJeremy L Thompson   PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc));
127a515125bSLeila Ghaffari 
128a515125bSLeila Ghaffari   // Place PETSc vectors in CEED vectors
129fd969b44SJames Wright   PetscCall(VecReadP2C(Q_loc, &q_mem_type, user->q_ceed));
130fd969b44SJames Wright   PetscCall(VecP2C(G_loc, &g_mem_type, user->g_ceed));
131a515125bSLeila Ghaffari 
132a515125bSLeila Ghaffari   // Apply CEED operator
1332b916ea7SJeremy L Thompson   CeedOperatorApply(user->op_rhs, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE);
134a515125bSLeila Ghaffari 
135a515125bSLeila Ghaffari   // Restore vectors
136fd969b44SJames Wright   PetscCall(VecReadC2P(user->q_ceed, q_mem_type, Q_loc));
137fd969b44SJames Wright   PetscCall(VecC2P(user->g_ceed, g_mem_type, G_loc));
138a515125bSLeila Ghaffari 
139a515125bSLeila Ghaffari   // Local-to-Global
1402b916ea7SJeremy L Thompson   PetscCall(VecZeroEntries(G));
1412b916ea7SJeremy L Thompson   PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G));
142a515125bSLeila Ghaffari 
143a515125bSLeila Ghaffari   // Inverse of the lumped mass matrix (M is Minv)
1442b916ea7SJeremy L Thompson   PetscCall(VecPointwiseMult(G, G, user->M));
145a515125bSLeila Ghaffari 
146a515125bSLeila Ghaffari   // Restore vectors
1472b916ea7SJeremy L Thompson   PetscCall(DMRestoreLocalVector(user->dm, &G_loc));
148a515125bSLeila Ghaffari 
149a515125bSLeila Ghaffari   PetscFunctionReturn(0);
150a515125bSLeila Ghaffari }
151a515125bSLeila Ghaffari 
152c5e9980aSAdeleke O. Bankole // Surface forces function setup
153c5e9980aSAdeleke O. Bankole static PetscErrorCode Surface_Forces_NS(DM dm, Vec G_loc, PetscInt num_walls, const PetscInt walls[], PetscScalar *reaction_force) {
154c5e9980aSAdeleke O. Bankole   DMLabel            face_label;
155c5e9980aSAdeleke O. Bankole   const PetscScalar *g;
1562004e3acSAdeleke O. Bankole   PetscInt           dof, dim = 3;
157c5e9980aSAdeleke O. Bankole   MPI_Comm           comm;
1582004e3acSAdeleke O. Bankole   PetscSection       s;
159c5e9980aSAdeleke O. Bankole 
160c5e9980aSAdeleke O. Bankole   PetscFunctionBeginUser;
161c5e9980aSAdeleke O. Bankole   PetscCall(PetscArrayzero(reaction_force, num_walls * dim));
162c5e9980aSAdeleke O. Bankole   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
163c5e9980aSAdeleke O. Bankole   PetscCall(DMGetLabel(dm, "Face Sets", &face_label));
164c5e9980aSAdeleke O. Bankole   PetscCall(VecGetArrayRead(G_loc, &g));
165c5e9980aSAdeleke O. Bankole   for (PetscInt w = 0; w < num_walls; w++) {
166c5e9980aSAdeleke O. Bankole     const PetscInt wall = walls[w];
167c5e9980aSAdeleke O. Bankole     IS             wall_is;
1682004e3acSAdeleke O. Bankole     PetscCall(DMGetLocalSection(dm, &s));
169c5e9980aSAdeleke O. Bankole     PetscCall(DMLabelGetStratumIS(face_label, wall, &wall_is));
170c5e9980aSAdeleke O. Bankole     if (wall_is) {  // There exist such points on this process
171c5e9980aSAdeleke O. Bankole       PetscInt        num_points;
1722004e3acSAdeleke O. Bankole       PetscInt        num_comp = 0;
173c5e9980aSAdeleke O. Bankole       const PetscInt *points;
1742004e3acSAdeleke O. Bankole       PetscCall(PetscSectionGetFieldComponents(s, 0, &num_comp));
175c5e9980aSAdeleke O. Bankole       PetscCall(ISGetSize(wall_is, &num_points));
176c5e9980aSAdeleke O. Bankole       PetscCall(ISGetIndices(wall_is, &points));
177c5e9980aSAdeleke O. Bankole       for (PetscInt i = 0; i < num_points; i++) {
178c5e9980aSAdeleke O. Bankole         const PetscInt           p = points[i];
179c5e9980aSAdeleke O. Bankole         const StateConservative *r;
180c5e9980aSAdeleke O. Bankole         PetscCall(DMPlexPointLocalRead(dm, p, g, &r));
1812004e3acSAdeleke O. Bankole         PetscCall(PetscSectionGetDof(s, p, &dof));
1822004e3acSAdeleke O. Bankole         for (PetscInt node = 0; node < dof / num_comp; node++) {
183c5e9980aSAdeleke O. Bankole           for (PetscInt j = 0; j < 3; j++) {
1842004e3acSAdeleke O. Bankole             reaction_force[w * dim + j] -= r[node].momentum[j];
1852004e3acSAdeleke O. Bankole           }
186c5e9980aSAdeleke O. Bankole         }
187c5e9980aSAdeleke O. Bankole       }
188c5e9980aSAdeleke O. Bankole       PetscCall(ISRestoreIndices(wall_is, &points));
189c5e9980aSAdeleke O. Bankole     }
190c5e9980aSAdeleke O. Bankole     PetscCall(ISDestroy(&wall_is));
191c5e9980aSAdeleke O. Bankole   }
192c5e9980aSAdeleke O. Bankole   PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, reaction_force, dim * num_walls, MPIU_SCALAR, MPI_SUM, comm));
193c5e9980aSAdeleke O. Bankole   //  Restore Vectors
194c5e9980aSAdeleke O. Bankole   PetscCall(VecRestoreArrayRead(G_loc, &g));
195c5e9980aSAdeleke O. Bankole 
196c5e9980aSAdeleke O. Bankole   PetscFunctionReturn(0);
197c5e9980aSAdeleke O. Bankole }
198c5e9980aSAdeleke O. Bankole 
199a515125bSLeila Ghaffari // Implicit time-stepper function setup
2002b916ea7SJeremy L Thompson PetscErrorCode IFunction_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, Vec G, void *user_data) {
201a515125bSLeila Ghaffari   User         user = *(User *)user_data;
20291f639d2SJames Wright   MPI_Comm     comm = PetscObjectComm((PetscObject)ts);
203fd969b44SJames Wright   PetscScalar  dt;
204e2f84137SJeremy L Thompson   Vec          Q_loc = user->Q_loc, Q_dot_loc = user->Q_dot_loc, G_loc;
205a515125bSLeila Ghaffari   PetscMemType q_mem_type, q_dot_mem_type, g_mem_type;
206a515125bSLeila Ghaffari   PetscFunctionBeginUser;
207a515125bSLeila Ghaffari 
208e2f84137SJeremy L Thompson   // Get local vectors
209c5e9980aSAdeleke O. Bankole   PetscCall(DMGetNamedLocalVector(user->dm, "ResidualLocal", &G_loc));
210e2f84137SJeremy L Thompson 
211e2f84137SJeremy L Thompson   // Update time dependent data
212c996854bSJames Wright   PetscCall(UpdateBoundaryValues(user, Q_loc, t));
21391f639d2SJames Wright   if (user->phys->solution_time_label) PetscCall(UpdateContextLabel(comm, t, user->op_ifunction, user->phys->solution_time_label));
2142b916ea7SJeremy L Thompson   PetscCall(TSGetTimeStep(ts, &dt));
21591f639d2SJames Wright   if (user->phys->timestep_size_label) PetscCall(UpdateContextLabel(comm, dt, user->op_ifunction, user->phys->timestep_size_label));
216a515125bSLeila Ghaffari 
217a515125bSLeila Ghaffari   // Global-to-local
21806108310SJames Wright   PetscCall(DMGlobalToLocalBegin(user->dm, Q, INSERT_VALUES, Q_loc));
21906108310SJames Wright   PetscCall(DMGlobalToLocalBegin(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc));
22006108310SJames Wright   PetscCall(DMGlobalToLocalEnd(user->dm, Q, INSERT_VALUES, Q_loc));
22106108310SJames Wright   PetscCall(DMGlobalToLocalEnd(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc));
222a515125bSLeila Ghaffari 
223a515125bSLeila Ghaffari   // Place PETSc vectors in CEED vectors
224fd969b44SJames Wright   PetscCall(VecReadP2C(Q_loc, &q_mem_type, user->q_ceed));
225fd969b44SJames Wright   PetscCall(VecReadP2C(Q_dot_loc, &q_dot_mem_type, user->q_dot_ceed));
226fd969b44SJames Wright   PetscCall(VecP2C(G_loc, &g_mem_type, user->g_ceed));
227a515125bSLeila Ghaffari 
228a515125bSLeila Ghaffari   // Apply CEED operator
2292b916ea7SJeremy L Thompson   CeedOperatorApply(user->op_ifunction, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE);
230a515125bSLeila Ghaffari 
231a515125bSLeila Ghaffari   // Restore vectors
232fd969b44SJames Wright   PetscCall(VecReadC2P(user->q_ceed, q_mem_type, Q_loc));
233fd969b44SJames Wright   PetscCall(VecReadC2P(user->q_dot_ceed, q_dot_mem_type, Q_dot_loc));
234fd969b44SJames Wright   PetscCall(VecC2P(user->g_ceed, g_mem_type, G_loc));
235a515125bSLeila Ghaffari 
23601ab89c1SJames Wright   if (user->app_ctx->sgs_model_type == SGS_MODEL_DATA_DRIVEN) {
23701ab89c1SJames Wright     PetscCall(SGS_DD_ModelApplyIFunction(user, Q_loc, G_loc));
23801ab89c1SJames Wright   }
2399c678832SJames Wright 
240a515125bSLeila Ghaffari   // Local-to-Global
2412b916ea7SJeremy L Thompson   PetscCall(VecZeroEntries(G));
2422b916ea7SJeremy L Thompson   PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G));
243a515125bSLeila Ghaffari 
244a515125bSLeila Ghaffari   // Restore vectors
245c5e9980aSAdeleke O. Bankole   PetscCall(DMRestoreNamedLocalVector(user->dm, "ResidualLocal", &G_loc));
246a515125bSLeila Ghaffari 
247a515125bSLeila Ghaffari   PetscFunctionReturn(0);
248a515125bSLeila Ghaffari }
249a515125bSLeila Ghaffari 
2502b916ea7SJeremy L Thompson static PetscErrorCode FormPreallocation(User user, PetscBool pbdiagonal, Mat J, CeedVector *coo_values) {
251b107fddaSJed Brown   PetscCount ncoo;
252b107fddaSJed Brown   PetscInt  *rows, *cols;
253b107fddaSJed Brown 
254b107fddaSJed Brown   PetscFunctionBeginUser;
255b107fddaSJed Brown   if (pbdiagonal) {
256b107fddaSJed Brown     CeedSize l_size;
257b107fddaSJed Brown     CeedOperatorGetActiveVectorLengths(user->op_ijacobian, &l_size, NULL);
258b107fddaSJed Brown     ncoo = l_size * 5;
259b107fddaSJed Brown     rows = malloc(ncoo * sizeof(rows[0]));
260b107fddaSJed Brown     cols = malloc(ncoo * sizeof(cols[0]));
261b107fddaSJed Brown     for (PetscCount n = 0; n < l_size / 5; n++) {
262b107fddaSJed Brown       for (PetscInt i = 0; i < 5; i++) {
263b107fddaSJed Brown         for (PetscInt j = 0; j < 5; j++) {
264b107fddaSJed Brown           rows[(n * 5 + i) * 5 + j] = n * 5 + i;
265b107fddaSJed Brown           cols[(n * 5 + i) * 5 + j] = n * 5 + j;
266b107fddaSJed Brown         }
267b107fddaSJed Brown       }
268b107fddaSJed Brown     }
269b107fddaSJed Brown   } else {
2702b916ea7SJeremy L Thompson     PetscCall(CeedOperatorLinearAssembleSymbolic(user->op_ijacobian, &ncoo, &rows, &cols));
271b107fddaSJed Brown   }
272b107fddaSJed Brown   PetscCall(MatSetPreallocationCOOLocal(J, ncoo, rows, cols));
273b107fddaSJed Brown   free(rows);
274b107fddaSJed Brown   free(cols);
275b107fddaSJed Brown   CeedVectorCreate(user->ceed, ncoo, coo_values);
276b107fddaSJed Brown   PetscFunctionReturn(0);
277b107fddaSJed Brown }
278b107fddaSJed Brown 
2792b916ea7SJeremy L Thompson static PetscErrorCode FormSetValues(User user, PetscBool pbdiagonal, Mat J, CeedVector coo_values) {
280b107fddaSJed Brown   CeedMemType        mem_type = CEED_MEM_HOST;
281b107fddaSJed Brown   const PetscScalar *values;
282b107fddaSJed Brown   MatType            mat_type;
283b107fddaSJed Brown 
284b107fddaSJed Brown   PetscFunctionBeginUser;
285b107fddaSJed Brown   PetscCall(MatGetType(J, &mat_type));
2862b916ea7SJeremy L Thompson   if (strstr(mat_type, "kokkos") || strstr(mat_type, "cusparse")) mem_type = CEED_MEM_DEVICE;
287b107fddaSJed Brown   if (user->app_ctx->pmat_pbdiagonal) {
2882b916ea7SJeremy L Thompson     CeedOperatorLinearAssemblePointBlockDiagonal(user->op_ijacobian, coo_values, CEED_REQUEST_IMMEDIATE);
289b107fddaSJed Brown   } else {
290b107fddaSJed Brown     CeedOperatorLinearAssemble(user->op_ijacobian, coo_values);
291b107fddaSJed Brown   }
292b107fddaSJed Brown   CeedVectorGetArrayRead(coo_values, mem_type, &values);
293b107fddaSJed Brown   PetscCall(MatSetValuesCOO(J, values, INSERT_VALUES));
294b107fddaSJed Brown   CeedVectorRestoreArrayRead(coo_values, &values);
295b107fddaSJed Brown   PetscFunctionReturn(0);
296b107fddaSJed Brown }
297b107fddaSJed Brown 
2982b916ea7SJeremy L Thompson PetscErrorCode FormIJacobian_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, PetscReal shift, Mat J, Mat J_pre, void *user_data) {
299f0b65372SJed Brown   User      user = *(User *)user_data;
30004855949SJed Brown   PetscBool J_is_shell, J_is_mffd, J_pre_is_shell;
301f0b65372SJed Brown   PetscFunctionBeginUser;
302a5f46a7bSJeremy L Thompson   if (user->phys->ijacobian_time_shift_label) CeedOperatorSetContextDouble(user->op_ijacobian, user->phys->ijacobian_time_shift_label, &shift);
30304855949SJed Brown   PetscCall(PetscObjectTypeCompare((PetscObject)J, MATMFFD, &J_is_mffd));
304f0b65372SJed Brown   PetscCall(PetscObjectTypeCompare((PetscObject)J, MATSHELL, &J_is_shell));
3052b916ea7SJeremy L Thompson   PetscCall(PetscObjectTypeCompare((PetscObject)J_pre, MATSHELL, &J_pre_is_shell));
306f0b65372SJed Brown   if (!user->matrices_set_up) {
307f0b65372SJed Brown     if (J_is_shell) {
308*f9028c3cSJames Wright       OperatorApplyContext op_ijacobian_ctx;
309*f9028c3cSJames Wright       OperatorApplyContextCreate(user->dm, user->dm, user->ceed, user->op_ijacobian, user->q_ceed, user->g_ceed, user->Q_dot_loc, NULL,
310*f9028c3cSJames Wright                                  &op_ijacobian_ctx);
311*f9028c3cSJames Wright       PetscCall(MatShellSetContext(J, op_ijacobian_ctx));
312*f9028c3cSJames Wright       PetscCall(MatShellSetContextDestroy(J, (PetscErrorCode(*)(void *))OperatorApplyContextDestroy));
313*f9028c3cSJames Wright       PetscCall(MatShellSetOperation(J, MATOP_MULT, (void (*)(void))MatMult_Ceed));
314*f9028c3cSJames Wright       PetscCall(MatShellSetOperation(J, MATOP_GET_DIAGONAL, (void (*)(void))MatGetDiag_Ceed));
315f0b65372SJed Brown       PetscCall(MatSetUp(J));
316f0b65372SJed Brown     }
317f0b65372SJed Brown     if (!J_pre_is_shell) {
3182b916ea7SJeremy L Thompson       PetscCall(FormPreallocation(user, user->app_ctx->pmat_pbdiagonal, J_pre, &user->coo_values_pmat));
319b107fddaSJed Brown     }
32004855949SJed Brown     if (J != J_pre && !J_is_shell && !J_is_mffd) {
321b107fddaSJed Brown       PetscCall(FormPreallocation(user, PETSC_FALSE, J, &user->coo_values_amat));
322b107fddaSJed Brown     }
323f0b65372SJed Brown     user->matrices_set_up = true;
324f0b65372SJed Brown   }
325f0b65372SJed Brown   if (!J_pre_is_shell) {
3262b916ea7SJeremy L Thompson     PetscCall(FormSetValues(user, user->app_ctx->pmat_pbdiagonal, J_pre, user->coo_values_pmat));
327f0b65372SJed Brown   }
32804855949SJed Brown   if (user->coo_values_amat) {
32904855949SJed Brown     PetscCall(FormSetValues(user, PETSC_FALSE, J, user->coo_values_amat));
33004855949SJed Brown   } else if (J_is_mffd) {
33104855949SJed Brown     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
33204855949SJed Brown     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
33304855949SJed Brown   }
334f0b65372SJed Brown   PetscFunctionReturn(0);
335f0b65372SJed Brown }
336f0b65372SJed Brown 
3372b916ea7SJeremy L Thompson PetscErrorCode WriteOutput(User user, Vec Q, PetscInt step_no, PetscScalar time) {
338a515125bSLeila Ghaffari   Vec         Q_loc;
339a515125bSLeila Ghaffari   char        file_path[PETSC_MAX_PATH_LEN];
340a515125bSLeila Ghaffari   PetscViewer viewer;
341a515125bSLeila Ghaffari   PetscFunctionBeginUser;
342a515125bSLeila Ghaffari 
343852e5969SJed Brown   if (user->app_ctx->checkpoint_vtk) {
344a515125bSLeila Ghaffari     // Set up output
3457538d537SJames Wright     PetscCall(DMGetLocalVector(user->dm, &Q_loc));
3467538d537SJames Wright     PetscCall(PetscObjectSetName((PetscObject)Q_loc, "StateVec"));
3477538d537SJames Wright     PetscCall(VecZeroEntries(Q_loc));
3487538d537SJames Wright     PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc));
349a515125bSLeila Ghaffari 
350a515125bSLeila Ghaffari     // Output
351852e5969SJed Brown     PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no));
3527538d537SJames Wright 
3532b916ea7SJeremy L Thompson     PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q), file_path, FILE_MODE_WRITE, &viewer));
3547538d537SJames Wright     PetscCall(VecView(Q_loc, viewer));
3557538d537SJames Wright     PetscCall(PetscViewerDestroy(&viewer));
356a515125bSLeila Ghaffari     if (user->dm_viz) {
357a515125bSLeila Ghaffari       Vec         Q_refined, Q_refined_loc;
358a515125bSLeila Ghaffari       char        file_path_refined[PETSC_MAX_PATH_LEN];
359a515125bSLeila Ghaffari       PetscViewer viewer_refined;
360a515125bSLeila Ghaffari 
3617538d537SJames Wright       PetscCall(DMGetGlobalVector(user->dm_viz, &Q_refined));
3627538d537SJames Wright       PetscCall(DMGetLocalVector(user->dm_viz, &Q_refined_loc));
3637538d537SJames Wright       PetscCall(PetscObjectSetName((PetscObject)Q_refined_loc, "Refined"));
3647538d537SJames Wright 
3657538d537SJames Wright       PetscCall(MatInterpolate(user->interp_viz, Q, Q_refined));
3667538d537SJames Wright       PetscCall(VecZeroEntries(Q_refined_loc));
3672b916ea7SJeremy L Thompson       PetscCall(DMGlobalToLocal(user->dm_viz, Q_refined, INSERT_VALUES, Q_refined_loc));
3687538d537SJames Wright 
369852e5969SJed Brown       PetscCall(
370852e5969SJed Brown           PetscSNPrintf(file_path_refined, sizeof file_path_refined, "%s/nsrefined-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no));
3717538d537SJames Wright 
3722b916ea7SJeremy L Thompson       PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q_refined), file_path_refined, FILE_MODE_WRITE, &viewer_refined));
3737538d537SJames Wright       PetscCall(VecView(Q_refined_loc, viewer_refined));
3747538d537SJames Wright       PetscCall(DMRestoreLocalVector(user->dm_viz, &Q_refined_loc));
3757538d537SJames Wright       PetscCall(DMRestoreGlobalVector(user->dm_viz, &Q_refined));
3767538d537SJames Wright       PetscCall(PetscViewerDestroy(&viewer_refined));
377a515125bSLeila Ghaffari     }
3787538d537SJames Wright     PetscCall(DMRestoreLocalVector(user->dm, &Q_loc));
379852e5969SJed Brown   }
380a515125bSLeila Ghaffari 
381a515125bSLeila Ghaffari   // Save data in a binary file for continuation of simulations
38291a36801SJames Wright   if (user->app_ctx->add_stepnum2bin) {
383852e5969SJed Brown     PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution-%" PetscInt_FMT ".bin", user->app_ctx->output_dir, step_no));
38491a36801SJames Wright   } else {
3852b916ea7SJeremy L Thompson     PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution.bin", user->app_ctx->output_dir));
38691a36801SJames Wright   }
3872b916ea7SJeremy L Thompson   PetscCall(PetscViewerBinaryOpen(user->comm, file_path, FILE_MODE_WRITE, &viewer));
3887538d537SJames Wright 
3899293eaa1SJed Brown   PetscInt token = FLUIDS_FILE_TOKEN;
3909293eaa1SJed Brown   PetscCall(PetscViewerBinaryWrite(viewer, &token, 1, PETSC_INT));
3919293eaa1SJed Brown   PetscCall(PetscViewerBinaryWrite(viewer, &step_no, 1, PETSC_INT));
3929293eaa1SJed Brown   time /= user->units->second;  // Dimensionalize time back
3939293eaa1SJed Brown   PetscCall(PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL));
3947538d537SJames Wright   PetscCall(VecView(Q, viewer));
3957538d537SJames Wright   PetscCall(PetscViewerDestroy(&viewer));
3967538d537SJames Wright   PetscFunctionReturn(0);
3977538d537SJames Wright }
3987538d537SJames Wright 
399c5e9980aSAdeleke O. Bankole // CSV Monitor
400c5e9980aSAdeleke O. Bankole PetscErrorCode TSMonitor_WallForce(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) {
401c5e9980aSAdeleke O. Bankole   User              user = ctx;
402c5e9980aSAdeleke O. Bankole   Vec               G_loc;
403c5e9980aSAdeleke O. Bankole   PetscInt          num_wall = user->app_ctx->wall_forces.num_wall, dim = 3;
404c5e9980aSAdeleke O. Bankole   const PetscInt   *walls  = user->app_ctx->wall_forces.walls;
405c5e9980aSAdeleke O. Bankole   PetscViewer       viewer = user->app_ctx->wall_forces.viewer;
406c5e9980aSAdeleke O. Bankole   PetscViewerFormat format = user->app_ctx->wall_forces.viewer_format;
407c5e9980aSAdeleke O. Bankole   PetscScalar      *reaction_force;
408c5e9980aSAdeleke O. Bankole   PetscBool         iascii;
409c5e9980aSAdeleke O. Bankole 
410c5e9980aSAdeleke O. Bankole   PetscFunctionBeginUser;
411c5e9980aSAdeleke O. Bankole   if (!viewer) PetscFunctionReturn(0);
412c5e9980aSAdeleke O. Bankole   PetscCall(DMGetNamedLocalVector(user->dm, "ResidualLocal", &G_loc));
413c5e9980aSAdeleke O. Bankole   PetscCall(PetscMalloc1(num_wall * dim, &reaction_force));
414c5e9980aSAdeleke O. Bankole   PetscCall(Surface_Forces_NS(user->dm, G_loc, num_wall, walls, reaction_force));
415c5e9980aSAdeleke O. Bankole   PetscCall(DMRestoreNamedLocalVector(user->dm, "ResidualLocal", &G_loc));
416c5e9980aSAdeleke O. Bankole 
417c5e9980aSAdeleke O. Bankole   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
418c5e9980aSAdeleke O. Bankole 
419c5e9980aSAdeleke O. Bankole   if (iascii) {
420c5e9980aSAdeleke O. Bankole     if (format == PETSC_VIEWER_ASCII_CSV && !user->app_ctx->wall_forces.header_written) {
421c5e9980aSAdeleke O. Bankole       PetscCall(PetscViewerASCIIPrintf(viewer, "Step,Time,Wall,ForceX,ForceY,ForceZ\n"));
422c5e9980aSAdeleke O. Bankole       user->app_ctx->wall_forces.header_written = PETSC_TRUE;
423c5e9980aSAdeleke O. Bankole     }
424c5e9980aSAdeleke O. Bankole     for (PetscInt w = 0; w < num_wall; w++) {
425c5e9980aSAdeleke O. Bankole       PetscInt wall = walls[w];
426c5e9980aSAdeleke O. Bankole       if (format == PETSC_VIEWER_ASCII_CSV) {
427c5e9980aSAdeleke O. Bankole         PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT ",%g,%" PetscInt_FMT ",%g,%g,%g\n", step_no, time, wall,
428c5e9980aSAdeleke O. Bankole                                          reaction_force[w * dim + 0], reaction_force[w * dim + 1], reaction_force[w * dim + 2]));
429c5e9980aSAdeleke O. Bankole 
430c5e9980aSAdeleke O. Bankole       } else {
431c5e9980aSAdeleke O. Bankole         PetscCall(PetscViewerASCIIPrintf(viewer, "Wall %" PetscInt_FMT " Forces: Force_x = %12g, Force_y = %12g, Force_z = %12g\n", wall,
432c5e9980aSAdeleke O. Bankole                                          reaction_force[w * dim + 0], reaction_force[w * dim + 1], reaction_force[w * dim + 2]));
433c5e9980aSAdeleke O. Bankole       }
434c5e9980aSAdeleke O. Bankole     }
435c5e9980aSAdeleke O. Bankole   }
436c5e9980aSAdeleke O. Bankole   PetscCall(PetscFree(reaction_force));
437c5e9980aSAdeleke O. Bankole   PetscFunctionReturn(0);
438c5e9980aSAdeleke O. Bankole }
439c5e9980aSAdeleke O. Bankole 
4407538d537SJames Wright // User provided TS Monitor
4412b916ea7SJeremy L Thompson PetscErrorCode TSMonitor_NS(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) {
4427538d537SJames Wright   User user = ctx;
4437538d537SJames Wright   PetscFunctionBeginUser;
4447538d537SJames Wright 
445852e5969SJed Brown   // Print every 'checkpoint_interval' steps
446c539088bSJames Wright   if (user->app_ctx->checkpoint_interval <= 0 || step_no % user->app_ctx->checkpoint_interval != 0 ||
447e419654dSJeremy L Thompson       (user->app_ctx->cont_steps == step_no && step_no != 0)) {
448c539088bSJames Wright     PetscFunctionReturn(0);
449e419654dSJeremy L Thompson   }
4507538d537SJames Wright 
4517538d537SJames Wright   PetscCall(WriteOutput(user, Q, step_no, time));
452a515125bSLeila Ghaffari 
453a515125bSLeila Ghaffari   PetscFunctionReturn(0);
454a515125bSLeila Ghaffari }
455a515125bSLeila Ghaffari 
456a515125bSLeila Ghaffari // TS: Create, setup, and solve
4572b916ea7SJeremy L Thompson PetscErrorCode TSSolve_NS(DM dm, User user, AppCtx app_ctx, Physics phys, Vec *Q, PetscScalar *f_time, TS *ts) {
458a515125bSLeila Ghaffari   MPI_Comm    comm = user->comm;
459a515125bSLeila Ghaffari   TSAdapt     adapt;
460a515125bSLeila Ghaffari   PetscScalar final_time;
461a515125bSLeila Ghaffari   PetscFunctionBeginUser;
462a515125bSLeila Ghaffari 
4632b916ea7SJeremy L Thompson   PetscCall(TSCreate(comm, ts));
4642b916ea7SJeremy L Thompson   PetscCall(TSSetDM(*ts, dm));
465a515125bSLeila Ghaffari   if (phys->implicit) {
4662b916ea7SJeremy L Thompson     PetscCall(TSSetType(*ts, TSBDF));
467a515125bSLeila Ghaffari     if (user->op_ifunction) {
4682b916ea7SJeremy L Thompson       PetscCall(TSSetIFunction(*ts, NULL, IFunction_NS, &user));
469a515125bSLeila Ghaffari     } else {  // Implicit integrators can fall back to using an RHSFunction
4702b916ea7SJeremy L Thompson       PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user));
471a515125bSLeila Ghaffari     }
472f0b65372SJed Brown     if (user->op_ijacobian) {
4732b916ea7SJeremy L Thompson       PetscCall(DMTSSetIJacobian(dm, FormIJacobian_NS, &user));
474b107fddaSJed Brown       if (app_ctx->amat_type) {
475b107fddaSJed Brown         Mat Pmat, Amat;
4762b916ea7SJeremy L Thompson         PetscCall(DMCreateMatrix(dm, &Pmat));
4772b916ea7SJeremy L Thompson         PetscCall(DMSetMatType(dm, app_ctx->amat_type));
4782b916ea7SJeremy L Thompson         PetscCall(DMCreateMatrix(dm, &Amat));
4792b916ea7SJeremy L Thompson         PetscCall(TSSetIJacobian(*ts, Amat, Pmat, NULL, NULL));
4802b916ea7SJeremy L Thompson         PetscCall(MatDestroy(&Amat));
4812b916ea7SJeremy L Thompson         PetscCall(MatDestroy(&Pmat));
482b107fddaSJed Brown       }
483f0b65372SJed Brown     }
484a515125bSLeila Ghaffari   } else {
4855d28dccaSJames Wright     PetscCheck(user->op_rhs, comm, PETSC_ERR_ARG_NULL, "Problem does not provide RHSFunction");
4862b916ea7SJeremy L Thompson     PetscCall(TSSetType(*ts, TSRK));
4872b916ea7SJeremy L Thompson     PetscCall(TSRKSetType(*ts, TSRK5F));
4882b916ea7SJeremy L Thompson     PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user));
489a515125bSLeila Ghaffari   }
4902b916ea7SJeremy L Thompson   PetscCall(TSSetMaxTime(*ts, 500. * user->units->second));
4912b916ea7SJeremy L Thompson   PetscCall(TSSetExactFinalTime(*ts, TS_EXACTFINALTIME_STEPOVER));
49222387d3aSJames Wright   if (app_ctx->test_type == TESTTYPE_NONE) PetscCall(TSSetErrorIfStepFails(*ts, PETSC_FALSE));
4932b916ea7SJeremy L Thompson   PetscCall(TSSetTimeStep(*ts, 1.e-2 * user->units->second));
4940e1e9333SJames Wright   if (app_ctx->test_type != TESTTYPE_NONE) {
4952b916ea7SJeremy L Thompson     PetscCall(TSSetMaxSteps(*ts, 10));
4962b916ea7SJeremy L Thompson   }
4972b916ea7SJeremy L Thompson   PetscCall(TSGetAdapt(*ts, &adapt));
4982b916ea7SJeremy L Thompson   PetscCall(TSAdaptSetStepLimits(adapt, 1.e-12 * user->units->second, 1.e2 * user->units->second));
4992b916ea7SJeremy L Thompson   PetscCall(TSSetFromOptions(*ts));
50091f639d2SJames Wright   user->time_bc_set = -1.0;    // require all BCs be updated
501a515125bSLeila Ghaffari   if (!app_ctx->cont_steps) {  // print initial condition
5020e1e9333SJames Wright     if (app_ctx->test_type == TESTTYPE_NONE) {
5032b916ea7SJeremy L Thompson       PetscCall(TSMonitor_NS(*ts, 0, 0., *Q, user));
504a515125bSLeila Ghaffari     }
505a515125bSLeila Ghaffari   } else {  // continue from time of last output
506a515125bSLeila Ghaffari     PetscInt    count;
507a515125bSLeila Ghaffari     PetscViewer viewer;
5082b916ea7SJeremy L Thompson 
5099293eaa1SJed Brown     if (app_ctx->cont_time <= 0) {  // Legacy files did not include step number and time
5102b916ea7SJeremy L Thompson       PetscCall(PetscViewerBinaryOpen(comm, app_ctx->cont_time_file, FILE_MODE_READ, &viewer));
5119293eaa1SJed Brown       PetscCall(PetscViewerBinaryRead(viewer, &app_ctx->cont_time, 1, &count, PETSC_REAL));
5122b916ea7SJeremy L Thompson       PetscCall(PetscViewerDestroy(&viewer));
5139293eaa1SJed Brown       PetscCheck(app_ctx->cont_steps != -1, comm, PETSC_ERR_ARG_INCOMP,
5149293eaa1SJed Brown                  "-continue step number not specified, but checkpoint file does not contain a step number (likely written by older code version)");
5159293eaa1SJed Brown     }
5169293eaa1SJed Brown     PetscCall(TSSetTime(*ts, app_ctx->cont_time * user->units->second));
51774a6f4ddSJed Brown     PetscCall(TSSetStepNumber(*ts, app_ctx->cont_steps));
518a515125bSLeila Ghaffari   }
5190e1e9333SJames Wright   if (app_ctx->test_type == TESTTYPE_NONE) {
5202b916ea7SJeremy L Thompson     PetscCall(TSMonitorSet(*ts, TSMonitor_NS, user, NULL));
5210e1e9333SJames Wright   }
522c5e9980aSAdeleke O. Bankole   if (app_ctx->wall_forces.viewer) {
523c5e9980aSAdeleke O. Bankole     PetscCall(TSMonitorSet(*ts, TSMonitor_WallForce, user, NULL));
524c5e9980aSAdeleke O. Bankole   }
525c931fa59SJames Wright   if (app_ctx->turb_spanstats_enable) {
526b0488d1fSJames Wright     PetscCall(TSMonitorSet(*ts, TSMonitor_Statistics, user, NULL));
527b8daee98SJames Wright     CeedScalar previous_time = app_ctx->cont_time * user->units->second;
5285cfe26e6SJames Wright     CeedOperatorSetContextDouble(user->spanstats.op_stats_collect_ctx->op, user->spanstats.previous_time_label, &previous_time);
529b0488d1fSJames Wright   }
530a515125bSLeila Ghaffari 
531a515125bSLeila Ghaffari   // Solve
53274a6f4ddSJed Brown   PetscReal start_time;
53374a6f4ddSJed Brown   PetscInt  start_step;
5342b916ea7SJeremy L Thompson   PetscCall(TSGetTime(*ts, &start_time));
53574a6f4ddSJed Brown   PetscCall(TSGetStepNumber(*ts, &start_step));
53691982731SJeremy L Thompson 
53791982731SJeremy L Thompson   PetscPreLoadBegin(PETSC_FALSE, "Fluids Solve");
53891982731SJeremy L Thompson   PetscCall(TSSetTime(*ts, start_time));
53974a6f4ddSJed Brown   PetscCall(TSSetStepNumber(*ts, start_step));
54091982731SJeremy L Thompson   if (PetscPreLoadingOn) {
54191982731SJeremy L Thompson     // LCOV_EXCL_START
54291982731SJeremy L Thompson     SNES      snes;
54391982731SJeremy L Thompson     Vec       Q_preload;
54491982731SJeremy L Thompson     PetscReal rtol;
54591982731SJeremy L Thompson     PetscCall(VecDuplicate(*Q, &Q_preload));
54691982731SJeremy L Thompson     PetscCall(VecCopy(*Q, Q_preload));
54791982731SJeremy L Thompson     PetscCall(TSGetSNES(*ts, &snes));
54891982731SJeremy L Thompson     PetscCall(SNESGetTolerances(snes, NULL, &rtol, NULL, NULL, NULL));
5492b916ea7SJeremy L Thompson     PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, .99, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT));
55091982731SJeremy L Thompson     PetscCall(TSSetSolution(*ts, *Q));
55191982731SJeremy L Thompson     PetscCall(TSStep(*ts));
5522b916ea7SJeremy L Thompson     PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, rtol, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT));
55391982731SJeremy L Thompson     PetscCall(VecDestroy(&Q_preload));
55491982731SJeremy L Thompson     // LCOV_EXCL_STOP
55591982731SJeremy L Thompson   } else {
5562b916ea7SJeremy L Thompson     PetscCall(PetscBarrier((PetscObject)*ts));
5572b916ea7SJeremy L Thompson     PetscCall(TSSolve(*ts, *Q));
55891982731SJeremy L Thompson   }
55991982731SJeremy L Thompson   PetscPreLoadEnd();
56091982731SJeremy L Thompson 
56191982731SJeremy L Thompson   PetscCall(TSGetSolveTime(*ts, &final_time));
562a515125bSLeila Ghaffari   *f_time = final_time;
56391982731SJeremy L Thompson 
5640e1e9333SJames Wright   if (app_ctx->test_type == TESTTYPE_NONE) {
5657538d537SJames Wright     PetscInt step_no;
5667538d537SJames Wright     PetscCall(TSGetStepNumber(*ts, &step_no));
567b0488d1fSJames Wright     if (user->app_ctx->checkpoint_interval > 0 || user->app_ctx->checkpoint_interval == -1) {
5687538d537SJames Wright       PetscCall(WriteOutput(user, *Q, step_no, final_time));
5697538d537SJames Wright     }
5707538d537SJames Wright 
57191982731SJeremy L Thompson     PetscLogEvent stage_id;
57291982731SJeremy L Thompson     PetscStageLog stage_log;
57391982731SJeremy L Thompson 
57491982731SJeremy L Thompson     PetscCall(PetscLogStageGetId("Fluids Solve", &stage_id));
57591982731SJeremy L Thompson     PetscCall(PetscLogGetStageLog(&stage_log));
5762b916ea7SJeremy L Thompson     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Time taken for solution (sec): %g\n", stage_log->stageInfo[stage_id].perfInfo.time));
577a515125bSLeila Ghaffari   }
578a515125bSLeila Ghaffari   PetscFunctionReturn(0);
579a515125bSLeila Ghaffari }
580