xref: /honee/src/setupts.c (revision fd969b44c9d340fb30c009d929beb19edf2ac3d7)
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 
11a515125bSLeila Ghaffari #include "../navierstokes.h"
12c5e9980aSAdeleke O. Bankole #include "../qfunctions/newtonian_state.h"
13a515125bSLeila Ghaffari 
14a515125bSLeila Ghaffari // Compute mass matrix for explicit scheme
152b916ea7SJeremy L Thompson PetscErrorCode ComputeLumpedMassMatrix(Ceed ceed, DM dm, CeedData ceed_data, Vec M) {
16a515125bSLeila Ghaffari   Vec           M_loc;
17a515125bSLeila Ghaffari   CeedQFunction qf_mass;
18a515125bSLeila Ghaffari   CeedOperator  op_mass;
19a515125bSLeila Ghaffari   CeedVector    m_ceed, ones_vec;
20a515125bSLeila Ghaffari   CeedInt       num_comp_q, q_data_size;
21a515125bSLeila Ghaffari   PetscFunctionBeginUser;
22a515125bSLeila Ghaffari 
23a515125bSLeila Ghaffari   // CEED Restriction
24a515125bSLeila Ghaffari   CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_q, &num_comp_q);
25a515125bSLeila Ghaffari   CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_qd_i, &q_data_size);
26a515125bSLeila Ghaffari   CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &m_ceed, NULL);
27a515125bSLeila Ghaffari   CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &ones_vec, NULL);
28a515125bSLeila Ghaffari   CeedVectorSetValue(ones_vec, 1.0);
29a515125bSLeila Ghaffari 
30a515125bSLeila Ghaffari   // CEED QFunction
319f59f36eSJames Wright   PetscCall(CreateMassQFunction(ceed, num_comp_q, q_data_size, &qf_mass));
32a515125bSLeila Ghaffari 
33a515125bSLeila Ghaffari   // CEED Operator
34a515125bSLeila Ghaffari   CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass);
359f59f36eSJames Wright   CeedOperatorSetField(op_mass, "u", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE);
362b916ea7SJeremy L Thompson   CeedOperatorSetField(op_mass, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data->q_data);
372b916ea7SJeremy L Thompson   CeedOperatorSetField(op_mass, "v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE);
38a515125bSLeila Ghaffari 
39a515125bSLeila Ghaffari   // Place PETSc vector in CEED vector
40a515125bSLeila Ghaffari   PetscMemType m_mem_type;
412b916ea7SJeremy L Thompson   PetscCall(DMGetLocalVector(dm, &M_loc));
42*fd969b44SJames Wright   PetscCall(VecP2C(M_loc, &m_mem_type, m_ceed));
43a515125bSLeila Ghaffari 
44a515125bSLeila Ghaffari   // Apply CEED Operator
45a515125bSLeila Ghaffari   CeedOperatorApply(op_mass, ones_vec, m_ceed, CEED_REQUEST_IMMEDIATE);
46a515125bSLeila Ghaffari 
47a515125bSLeila Ghaffari   // Restore vectors
48*fd969b44SJames Wright   PetscCall(VecC2P(m_ceed, m_mem_type, M_loc));
49a515125bSLeila Ghaffari 
50a515125bSLeila Ghaffari   // Local-to-Global
512b916ea7SJeremy L Thompson   PetscCall(VecZeroEntries(M));
522b916ea7SJeremy L Thompson   PetscCall(DMLocalToGlobal(dm, M_loc, ADD_VALUES, M));
532b916ea7SJeremy L Thompson   PetscCall(DMRestoreLocalVector(dm, &M_loc));
54a515125bSLeila Ghaffari 
55a515125bSLeila Ghaffari   // Invert diagonally lumped mass vector for RHS function
562b916ea7SJeremy L Thompson   PetscCall(VecReciprocal(M));
57a515125bSLeila Ghaffari 
58a515125bSLeila Ghaffari   // Cleanup
59a515125bSLeila Ghaffari   CeedVectorDestroy(&ones_vec);
60a515125bSLeila Ghaffari   CeedVectorDestroy(&m_ceed);
61a515125bSLeila Ghaffari   CeedQFunctionDestroy(&qf_mass);
62a515125bSLeila Ghaffari   CeedOperatorDestroy(&op_mass);
63a515125bSLeila Ghaffari 
64a515125bSLeila Ghaffari   PetscFunctionReturn(0);
65a515125bSLeila Ghaffari }
66a515125bSLeila Ghaffari 
67c996854bSJames Wright // Insert Boundary values if it's a new time
68c996854bSJames Wright PetscErrorCode UpdateBoundaryValues(User user, Vec Q_loc, PetscReal t) {
69c996854bSJames Wright   PetscFunctionBeginUser;
70c996854bSJames Wright   if (user->time_bc_set != t) {
71c996854bSJames Wright     PetscCall(DMPlexInsertBoundaryValues(user->dm, PETSC_TRUE, Q_loc, t, NULL, NULL, NULL));
72c996854bSJames Wright     user->time_bc_set = t;
73c996854bSJames Wright   }
74c996854bSJames Wright   PetscFunctionReturn(0);
75c996854bSJames Wright }
76c996854bSJames Wright 
77c2cb7fc8SJames Wright PetscErrorCode UpdateContextLabel(PetscScalar *previous_value, PetscScalar update_value, CeedOperator op, CeedContextFieldLabel label) {
78c2cb7fc8SJames Wright   PetscFunctionBeginUser;
79c2cb7fc8SJames Wright 
80c2cb7fc8SJames Wright   if (*previous_value != update_value) {
81c2cb7fc8SJames Wright     if (label) {
82c2cb7fc8SJames Wright       CeedOperatorContextSetDouble(op, label, &update_value);
83c2cb7fc8SJames Wright     }
84c2cb7fc8SJames Wright     *previous_value = update_value;
85c2cb7fc8SJames Wright   }
86c2cb7fc8SJames Wright   PetscFunctionReturn(0);
87c2cb7fc8SJames Wright }
88c2cb7fc8SJames Wright 
89a515125bSLeila Ghaffari // RHS (Explicit time-stepper) function setup
90a515125bSLeila Ghaffari //   This is the RHS of the ODE, given as u_t = G(t,u)
91a515125bSLeila Ghaffari //   This function takes in a state vector Q and writes into G
92a515125bSLeila Ghaffari PetscErrorCode RHS_NS(TS ts, PetscReal t, Vec Q, Vec G, void *user_data) {
93a515125bSLeila Ghaffari   User         user = *(User *)user_data;
94*fd969b44SJames Wright   PetscScalar  dt;
95e2f84137SJeremy L Thompson   Vec          Q_loc = user->Q_loc, G_loc;
96a515125bSLeila Ghaffari   PetscMemType q_mem_type, g_mem_type;
97a515125bSLeila Ghaffari   PetscFunctionBeginUser;
98a515125bSLeila Ghaffari 
99e2f84137SJeremy L Thompson   // Get local vector
1002b916ea7SJeremy L Thompson   PetscCall(DMGetLocalVector(user->dm, &G_loc));
101e2f84137SJeremy L Thompson 
102e2f84137SJeremy L Thompson   // Update time dependent data
103c996854bSJames Wright   PetscCall(UpdateBoundaryValues(user, Q_loc, t));
104c2cb7fc8SJames Wright   PetscCall(UpdateContextLabel(&user->time, t, user->op_rhs, user->phys->solution_time_label));
1052b916ea7SJeremy L Thompson   PetscCall(TSGetTimeStep(ts, &dt));
106c2cb7fc8SJames Wright   PetscCall(UpdateContextLabel(&user->dt, dt, user->op_rhs, user->phys->timestep_size_label));
107a515125bSLeila Ghaffari 
108a515125bSLeila Ghaffari   // Global-to-local
1092b916ea7SJeremy L Thompson   PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc));
110a515125bSLeila Ghaffari 
111a515125bSLeila Ghaffari   // Place PETSc vectors in CEED vectors
112*fd969b44SJames Wright   PetscCall(VecReadP2C(Q_loc, &q_mem_type, user->q_ceed));
113*fd969b44SJames Wright   PetscCall(VecP2C(G_loc, &g_mem_type, user->g_ceed));
114a515125bSLeila Ghaffari 
115a515125bSLeila Ghaffari   // Apply CEED operator
1162b916ea7SJeremy L Thompson   CeedOperatorApply(user->op_rhs, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE);
117a515125bSLeila Ghaffari 
118a515125bSLeila Ghaffari   // Restore vectors
119*fd969b44SJames Wright   PetscCall(VecReadC2P(user->q_ceed, q_mem_type, Q_loc));
120*fd969b44SJames Wright   PetscCall(VecC2P(user->g_ceed, g_mem_type, G_loc));
121a515125bSLeila Ghaffari 
122a515125bSLeila Ghaffari   // Local-to-Global
1232b916ea7SJeremy L Thompson   PetscCall(VecZeroEntries(G));
1242b916ea7SJeremy L Thompson   PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G));
125a515125bSLeila Ghaffari 
126a515125bSLeila Ghaffari   // Inverse of the lumped mass matrix (M is Minv)
1272b916ea7SJeremy L Thompson   PetscCall(VecPointwiseMult(G, G, user->M));
128a515125bSLeila Ghaffari 
129a515125bSLeila Ghaffari   // Restore vectors
1302b916ea7SJeremy L Thompson   PetscCall(DMRestoreLocalVector(user->dm, &G_loc));
131a515125bSLeila Ghaffari 
132a515125bSLeila Ghaffari   PetscFunctionReturn(0);
133a515125bSLeila Ghaffari }
134a515125bSLeila Ghaffari 
135c5e9980aSAdeleke O. Bankole // Surface forces function setup
136c5e9980aSAdeleke O. Bankole static PetscErrorCode Surface_Forces_NS(DM dm, Vec G_loc, PetscInt num_walls, const PetscInt walls[], PetscScalar *reaction_force) {
137c5e9980aSAdeleke O. Bankole   DMLabel            face_label;
138c5e9980aSAdeleke O. Bankole   const PetscScalar *g;
139c5e9980aSAdeleke O. Bankole   PetscInt           dim = 3;
140c5e9980aSAdeleke O. Bankole   MPI_Comm           comm;
141c5e9980aSAdeleke O. Bankole 
142c5e9980aSAdeleke O. Bankole   PetscFunctionBeginUser;
143c5e9980aSAdeleke O. Bankole   PetscCall(PetscArrayzero(reaction_force, num_walls * dim));
144c5e9980aSAdeleke O. Bankole   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
145c5e9980aSAdeleke O. Bankole   PetscCall(DMGetLabel(dm, "Face Sets", &face_label));
146c5e9980aSAdeleke O. Bankole   PetscCall(VecGetArrayRead(G_loc, &g));
147c5e9980aSAdeleke O. Bankole   for (PetscInt w = 0; w < num_walls; w++) {
148c5e9980aSAdeleke O. Bankole     const PetscInt wall = walls[w];
149c5e9980aSAdeleke O. Bankole     IS             wall_is;
150c5e9980aSAdeleke O. Bankole     PetscCall(DMLabelGetStratumIS(face_label, wall, &wall_is));
151c5e9980aSAdeleke O. Bankole     if (wall_is) {  // There exist such points on this process
152c5e9980aSAdeleke O. Bankole       PetscInt        num_points;
153c5e9980aSAdeleke O. Bankole       const PetscInt *points;
154c5e9980aSAdeleke O. Bankole       PetscCall(ISGetSize(wall_is, &num_points));
155c5e9980aSAdeleke O. Bankole       PetscCall(ISGetIndices(wall_is, &points));
156c5e9980aSAdeleke O. Bankole       for (PetscInt i = 0; i < num_points; i++) {
157c5e9980aSAdeleke O. Bankole         const PetscInt           p = points[i];
158c5e9980aSAdeleke O. Bankole         const StateConservative *r;
159c5e9980aSAdeleke O. Bankole         PetscCall(DMPlexPointLocalRead(dm, p, g, &r));
160c5e9980aSAdeleke O. Bankole         if (!r) continue;
161c5e9980aSAdeleke O. Bankole         for (PetscInt j = 0; j < 3; j++) {
162c5e9980aSAdeleke O. Bankole           reaction_force[w * dim + j] -= r->momentum[j];
163c5e9980aSAdeleke O. Bankole         }
164c5e9980aSAdeleke O. Bankole       }
165c5e9980aSAdeleke O. Bankole       PetscCall(ISRestoreIndices(wall_is, &points));
166c5e9980aSAdeleke O. Bankole     }
167c5e9980aSAdeleke O. Bankole     PetscCall(ISDestroy(&wall_is));
168c5e9980aSAdeleke O. Bankole   }
169c5e9980aSAdeleke O. Bankole   PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, reaction_force, dim * num_walls, MPIU_SCALAR, MPI_SUM, comm));
170c5e9980aSAdeleke O. Bankole   //  Restore Vectors
171c5e9980aSAdeleke O. Bankole   PetscCall(VecRestoreArrayRead(G_loc, &g));
172c5e9980aSAdeleke O. Bankole 
173c5e9980aSAdeleke O. Bankole   PetscFunctionReturn(0);
174c5e9980aSAdeleke O. Bankole }
175c5e9980aSAdeleke O. Bankole 
176a515125bSLeila Ghaffari // Implicit time-stepper function setup
1772b916ea7SJeremy L Thompson PetscErrorCode IFunction_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, Vec G, void *user_data) {
178a515125bSLeila Ghaffari   User         user = *(User *)user_data;
179*fd969b44SJames Wright   PetscScalar  dt;
180e2f84137SJeremy L Thompson   Vec          Q_loc = user->Q_loc, Q_dot_loc = user->Q_dot_loc, G_loc;
181a515125bSLeila Ghaffari   PetscMemType q_mem_type, q_dot_mem_type, g_mem_type;
182a515125bSLeila Ghaffari   PetscFunctionBeginUser;
183a515125bSLeila Ghaffari 
184e2f84137SJeremy L Thompson   // Get local vectors
185c5e9980aSAdeleke O. Bankole   PetscCall(DMGetNamedLocalVector(user->dm, "ResidualLocal", &G_loc));
186e2f84137SJeremy L Thompson 
187e2f84137SJeremy L Thompson   // Update time dependent data
188c996854bSJames Wright   PetscCall(UpdateBoundaryValues(user, Q_loc, t));
189c2cb7fc8SJames Wright   PetscCall(UpdateContextLabel(&user->time, t, user->op_ifunction, user->phys->solution_time_label));
1902b916ea7SJeremy L Thompson   PetscCall(TSGetTimeStep(ts, &dt));
191c2cb7fc8SJames Wright   PetscCall(UpdateContextLabel(&user->dt, dt, user->op_ifunction, user->phys->timestep_size_label));
192a515125bSLeila Ghaffari 
193a515125bSLeila Ghaffari   // Global-to-local
19406108310SJames Wright   PetscCall(DMGlobalToLocalBegin(user->dm, Q, INSERT_VALUES, Q_loc));
19506108310SJames Wright   PetscCall(DMGlobalToLocalBegin(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc));
19606108310SJames Wright   PetscCall(DMGlobalToLocalEnd(user->dm, Q, INSERT_VALUES, Q_loc));
19706108310SJames Wright   PetscCall(DMGlobalToLocalEnd(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc));
198a515125bSLeila Ghaffari 
199a515125bSLeila Ghaffari   // Place PETSc vectors in CEED vectors
200*fd969b44SJames Wright   PetscCall(VecReadP2C(Q_loc, &q_mem_type, user->q_ceed));
201*fd969b44SJames Wright   PetscCall(VecReadP2C(Q_dot_loc, &q_dot_mem_type, user->q_dot_ceed));
202*fd969b44SJames Wright   PetscCall(VecP2C(G_loc, &g_mem_type, user->g_ceed));
203a515125bSLeila Ghaffari 
204a515125bSLeila Ghaffari   // Apply CEED operator
2052b916ea7SJeremy L Thompson   CeedOperatorApply(user->op_ifunction, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE);
206a515125bSLeila Ghaffari 
207a515125bSLeila Ghaffari   // Restore vectors
208*fd969b44SJames Wright   PetscCall(VecReadC2P(user->q_ceed, q_mem_type, Q_loc));
209*fd969b44SJames Wright   PetscCall(VecReadC2P(user->q_dot_ceed, q_dot_mem_type, Q_dot_loc));
210*fd969b44SJames Wright   PetscCall(VecC2P(user->g_ceed, g_mem_type, G_loc));
211a515125bSLeila Ghaffari 
212a515125bSLeila Ghaffari   // Local-to-Global
2132b916ea7SJeremy L Thompson   PetscCall(VecZeroEntries(G));
2142b916ea7SJeremy L Thompson   PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G));
215a515125bSLeila Ghaffari 
216a515125bSLeila Ghaffari   // Restore vectors
217c5e9980aSAdeleke O. Bankole   PetscCall(DMRestoreNamedLocalVector(user->dm, "ResidualLocal", &G_loc));
218a515125bSLeila Ghaffari 
219a515125bSLeila Ghaffari   PetscFunctionReturn(0);
220a515125bSLeila Ghaffari }
221a515125bSLeila Ghaffari 
222f0b65372SJed Brown static PetscErrorCode MatMult_NS_IJacobian(Mat J, Vec Q, Vec G) {
223f0b65372SJed Brown   User               user;
224f0b65372SJed Brown   const PetscScalar *q;
225f0b65372SJed Brown   PetscScalar       *g;
226f0b65372SJed Brown   PetscMemType       q_mem_type, g_mem_type;
227f0b65372SJed Brown   PetscFunctionBeginUser;
2282b916ea7SJeremy L Thompson   PetscCall(MatShellGetContext(J, &user));
229e2f84137SJeremy L Thompson   Vec Q_loc = user->Q_dot_loc,  // Note - Q_dot_loc has zero BCs
230e2f84137SJeremy L Thompson       G_loc;
231e2f84137SJeremy L Thompson 
232f0b65372SJed Brown   // Get local vectors
2332b916ea7SJeremy L Thompson   PetscCall(DMGetLocalVector(user->dm, &G_loc));
234f0b65372SJed Brown 
235f0b65372SJed Brown   // Global-to-local
2362b916ea7SJeremy L Thompson   PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc));
237f0b65372SJed Brown 
238f0b65372SJed Brown   // Place PETSc vectors in CEED vectors
2392b916ea7SJeremy L Thompson   PetscCall(VecGetArrayReadAndMemType(Q_loc, &q, &q_mem_type));
2402b916ea7SJeremy L Thompson   PetscCall(VecGetArrayAndMemType(G_loc, &g, &g_mem_type));
2412b916ea7SJeremy L Thompson   CeedVectorSetArray(user->q_ceed, MemTypeP2C(q_mem_type), CEED_USE_POINTER, (PetscScalar *)q);
242f0b65372SJed Brown   CeedVectorSetArray(user->g_ceed, MemTypeP2C(g_mem_type), CEED_USE_POINTER, g);
243f0b65372SJed Brown 
244f0b65372SJed Brown   // Apply CEED operator
2452b916ea7SJeremy L Thompson   CeedOperatorApply(user->op_ijacobian, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE);
246f0b65372SJed Brown 
247f0b65372SJed Brown   // Restore vectors
248f0b65372SJed Brown   CeedVectorTakeArray(user->q_ceed, MemTypeP2C(q_mem_type), NULL);
249f0b65372SJed Brown   CeedVectorTakeArray(user->g_ceed, MemTypeP2C(g_mem_type), NULL);
2502b916ea7SJeremy L Thompson   PetscCall(VecRestoreArrayReadAndMemType(Q_loc, &q));
2512b916ea7SJeremy L Thompson   PetscCall(VecRestoreArrayAndMemType(G_loc, &g));
252f0b65372SJed Brown 
253f0b65372SJed Brown   // Local-to-Global
2542b916ea7SJeremy L Thompson   PetscCall(VecZeroEntries(G));
2552b916ea7SJeremy L Thompson   PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G));
256f0b65372SJed Brown 
257f0b65372SJed Brown   // Restore vectors
2582b916ea7SJeremy L Thompson   PetscCall(DMRestoreLocalVector(user->dm, &G_loc));
259f0b65372SJed Brown   PetscFunctionReturn(0);
260f0b65372SJed Brown }
261f0b65372SJed Brown 
262f0b65372SJed Brown PetscErrorCode MatGetDiagonal_NS_IJacobian(Mat A, Vec D) {
263f0b65372SJed Brown   User         user;
264f0b65372SJed Brown   Vec          D_loc;
265f0b65372SJed Brown   PetscScalar *d;
266f0b65372SJed Brown   PetscMemType mem_type;
267f0b65372SJed Brown 
268f0b65372SJed Brown   PetscFunctionBeginUser;
269f0b65372SJed Brown   MatShellGetContext(A, &user);
270f0b65372SJed Brown   PetscCall(DMGetLocalVector(user->dm, &D_loc));
271f0b65372SJed Brown   PetscCall(VecGetArrayAndMemType(D_loc, &d, &mem_type));
272f0b65372SJed Brown   CeedVectorSetArray(user->g_ceed, MemTypeP2C(mem_type), CEED_USE_POINTER, d);
2732b916ea7SJeremy L Thompson   CeedOperatorLinearAssembleDiagonal(user->op_ijacobian, user->g_ceed, CEED_REQUEST_IMMEDIATE);
274f0b65372SJed Brown   CeedVectorTakeArray(user->g_ceed, MemTypeP2C(mem_type), NULL);
275f0b65372SJed Brown   PetscCall(VecRestoreArrayAndMemType(D_loc, &d));
276f0b65372SJed Brown   PetscCall(VecZeroEntries(D));
277f0b65372SJed Brown   PetscCall(DMLocalToGlobal(user->dm, D_loc, ADD_VALUES, D));
278f0b65372SJed Brown   PetscCall(DMRestoreLocalVector(user->dm, &D_loc));
279f0b65372SJed Brown   VecViewFromOptions(D, NULL, "-diag_vec_view");
280f0b65372SJed Brown   PetscFunctionReturn(0);
281f0b65372SJed Brown }
282f0b65372SJed Brown 
2832b916ea7SJeremy L Thompson static PetscErrorCode FormPreallocation(User user, PetscBool pbdiagonal, Mat J, CeedVector *coo_values) {
284b107fddaSJed Brown   PetscCount ncoo;
285b107fddaSJed Brown   PetscInt  *rows, *cols;
286b107fddaSJed Brown 
287b107fddaSJed Brown   PetscFunctionBeginUser;
288b107fddaSJed Brown   if (pbdiagonal) {
289b107fddaSJed Brown     CeedSize l_size;
290b107fddaSJed Brown     CeedOperatorGetActiveVectorLengths(user->op_ijacobian, &l_size, NULL);
291b107fddaSJed Brown     ncoo = l_size * 5;
292b107fddaSJed Brown     rows = malloc(ncoo * sizeof(rows[0]));
293b107fddaSJed Brown     cols = malloc(ncoo * sizeof(cols[0]));
294b107fddaSJed Brown     for (PetscCount n = 0; n < l_size / 5; n++) {
295b107fddaSJed Brown       for (PetscInt i = 0; i < 5; i++) {
296b107fddaSJed Brown         for (PetscInt j = 0; j < 5; j++) {
297b107fddaSJed Brown           rows[(n * 5 + i) * 5 + j] = n * 5 + i;
298b107fddaSJed Brown           cols[(n * 5 + i) * 5 + j] = n * 5 + j;
299b107fddaSJed Brown         }
300b107fddaSJed Brown       }
301b107fddaSJed Brown     }
302b107fddaSJed Brown   } else {
3032b916ea7SJeremy L Thompson     PetscCall(CeedOperatorLinearAssembleSymbolic(user->op_ijacobian, &ncoo, &rows, &cols));
304b107fddaSJed Brown   }
305b107fddaSJed Brown   PetscCall(MatSetPreallocationCOOLocal(J, ncoo, rows, cols));
306b107fddaSJed Brown   free(rows);
307b107fddaSJed Brown   free(cols);
308b107fddaSJed Brown   CeedVectorCreate(user->ceed, ncoo, coo_values);
309b107fddaSJed Brown   PetscFunctionReturn(0);
310b107fddaSJed Brown }
311b107fddaSJed Brown 
3122b916ea7SJeremy L Thompson static PetscErrorCode FormSetValues(User user, PetscBool pbdiagonal, Mat J, CeedVector coo_values) {
313b107fddaSJed Brown   CeedMemType        mem_type = CEED_MEM_HOST;
314b107fddaSJed Brown   const PetscScalar *values;
315b107fddaSJed Brown   MatType            mat_type;
316b107fddaSJed Brown 
317b107fddaSJed Brown   PetscFunctionBeginUser;
318b107fddaSJed Brown   PetscCall(MatGetType(J, &mat_type));
3192b916ea7SJeremy L Thompson   if (strstr(mat_type, "kokkos") || strstr(mat_type, "cusparse")) mem_type = CEED_MEM_DEVICE;
320b107fddaSJed Brown   if (user->app_ctx->pmat_pbdiagonal) {
3212b916ea7SJeremy L Thompson     CeedOperatorLinearAssemblePointBlockDiagonal(user->op_ijacobian, coo_values, CEED_REQUEST_IMMEDIATE);
322b107fddaSJed Brown   } else {
323b107fddaSJed Brown     CeedOperatorLinearAssemble(user->op_ijacobian, coo_values);
324b107fddaSJed Brown   }
325b107fddaSJed Brown   CeedVectorGetArrayRead(coo_values, mem_type, &values);
326b107fddaSJed Brown   PetscCall(MatSetValuesCOO(J, values, INSERT_VALUES));
327b107fddaSJed Brown   CeedVectorRestoreArrayRead(coo_values, &values);
328b107fddaSJed Brown   PetscFunctionReturn(0);
329b107fddaSJed Brown }
330b107fddaSJed Brown 
3312b916ea7SJeremy L Thompson PetscErrorCode FormIJacobian_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, PetscReal shift, Mat J, Mat J_pre, void *user_data) {
332f0b65372SJed Brown   User      user = *(User *)user_data;
33304855949SJed Brown   PetscBool J_is_shell, J_is_mffd, J_pre_is_shell;
334f0b65372SJed Brown   PetscFunctionBeginUser;
3352b916ea7SJeremy L Thompson   if (user->phys->ijacobian_time_shift_label) CeedOperatorContextSetDouble(user->op_ijacobian, user->phys->ijacobian_time_shift_label, &shift);
33604855949SJed Brown   PetscCall(PetscObjectTypeCompare((PetscObject)J, MATMFFD, &J_is_mffd));
337f0b65372SJed Brown   PetscCall(PetscObjectTypeCompare((PetscObject)J, MATSHELL, &J_is_shell));
3382b916ea7SJeremy L Thompson   PetscCall(PetscObjectTypeCompare((PetscObject)J_pre, MATSHELL, &J_pre_is_shell));
339f0b65372SJed Brown   if (!user->matrices_set_up) {
340f0b65372SJed Brown     if (J_is_shell) {
341f0b65372SJed Brown       PetscCall(MatShellSetContext(J, user));
3422b916ea7SJeremy L Thompson       PetscCall(MatShellSetOperation(J, MATOP_MULT, (void (*)(void))MatMult_NS_IJacobian));
3432b916ea7SJeremy L Thompson       PetscCall(MatShellSetOperation(J, MATOP_GET_DIAGONAL, (void (*)(void))MatGetDiagonal_NS_IJacobian));
344f0b65372SJed Brown       PetscCall(MatSetUp(J));
345f0b65372SJed Brown     }
346f0b65372SJed Brown     if (!J_pre_is_shell) {
3472b916ea7SJeremy L Thompson       PetscCall(FormPreallocation(user, user->app_ctx->pmat_pbdiagonal, J_pre, &user->coo_values_pmat));
348b107fddaSJed Brown     }
34904855949SJed Brown     if (J != J_pre && !J_is_shell && !J_is_mffd) {
350b107fddaSJed Brown       PetscCall(FormPreallocation(user, PETSC_FALSE, J, &user->coo_values_amat));
351b107fddaSJed Brown     }
352f0b65372SJed Brown     user->matrices_set_up = true;
353f0b65372SJed Brown   }
354f0b65372SJed Brown   if (!J_pre_is_shell) {
3552b916ea7SJeremy L Thompson     PetscCall(FormSetValues(user, user->app_ctx->pmat_pbdiagonal, J_pre, user->coo_values_pmat));
356f0b65372SJed Brown   }
35704855949SJed Brown   if (user->coo_values_amat) {
35804855949SJed Brown     PetscCall(FormSetValues(user, PETSC_FALSE, J, user->coo_values_amat));
35904855949SJed Brown   } else if (J_is_mffd) {
36004855949SJed Brown     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
36104855949SJed Brown     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
36204855949SJed Brown   }
363f0b65372SJed Brown   PetscFunctionReturn(0);
364f0b65372SJed Brown }
365f0b65372SJed Brown 
3662b916ea7SJeremy L Thompson PetscErrorCode WriteOutput(User user, Vec Q, PetscInt step_no, PetscScalar time) {
367a515125bSLeila Ghaffari   Vec         Q_loc;
368a515125bSLeila Ghaffari   char        file_path[PETSC_MAX_PATH_LEN];
369a515125bSLeila Ghaffari   PetscViewer viewer;
370a515125bSLeila Ghaffari   PetscFunctionBeginUser;
371a515125bSLeila Ghaffari 
372852e5969SJed Brown   if (user->app_ctx->checkpoint_vtk) {
373a515125bSLeila Ghaffari     // Set up output
3747538d537SJames Wright     PetscCall(DMGetLocalVector(user->dm, &Q_loc));
3757538d537SJames Wright     PetscCall(PetscObjectSetName((PetscObject)Q_loc, "StateVec"));
3767538d537SJames Wright     PetscCall(VecZeroEntries(Q_loc));
3777538d537SJames Wright     PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc));
378a515125bSLeila Ghaffari 
379a515125bSLeila Ghaffari     // Output
380852e5969SJed Brown     PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no));
3817538d537SJames Wright 
3822b916ea7SJeremy L Thompson     PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q), file_path, FILE_MODE_WRITE, &viewer));
3837538d537SJames Wright     PetscCall(VecView(Q_loc, viewer));
3847538d537SJames Wright     PetscCall(PetscViewerDestroy(&viewer));
385a515125bSLeila Ghaffari     if (user->dm_viz) {
386a515125bSLeila Ghaffari       Vec         Q_refined, Q_refined_loc;
387a515125bSLeila Ghaffari       char        file_path_refined[PETSC_MAX_PATH_LEN];
388a515125bSLeila Ghaffari       PetscViewer viewer_refined;
389a515125bSLeila Ghaffari 
3907538d537SJames Wright       PetscCall(DMGetGlobalVector(user->dm_viz, &Q_refined));
3917538d537SJames Wright       PetscCall(DMGetLocalVector(user->dm_viz, &Q_refined_loc));
3927538d537SJames Wright       PetscCall(PetscObjectSetName((PetscObject)Q_refined_loc, "Refined"));
3937538d537SJames Wright 
3947538d537SJames Wright       PetscCall(MatInterpolate(user->interp_viz, Q, Q_refined));
3957538d537SJames Wright       PetscCall(VecZeroEntries(Q_refined_loc));
3962b916ea7SJeremy L Thompson       PetscCall(DMGlobalToLocal(user->dm_viz, Q_refined, INSERT_VALUES, Q_refined_loc));
3977538d537SJames Wright 
398852e5969SJed Brown       PetscCall(
399852e5969SJed Brown           PetscSNPrintf(file_path_refined, sizeof file_path_refined, "%s/nsrefined-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no));
4007538d537SJames Wright 
4012b916ea7SJeremy L Thompson       PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q_refined), file_path_refined, FILE_MODE_WRITE, &viewer_refined));
4027538d537SJames Wright       PetscCall(VecView(Q_refined_loc, viewer_refined));
4037538d537SJames Wright       PetscCall(DMRestoreLocalVector(user->dm_viz, &Q_refined_loc));
4047538d537SJames Wright       PetscCall(DMRestoreGlobalVector(user->dm_viz, &Q_refined));
4057538d537SJames Wright       PetscCall(PetscViewerDestroy(&viewer_refined));
406a515125bSLeila Ghaffari     }
4077538d537SJames Wright     PetscCall(DMRestoreLocalVector(user->dm, &Q_loc));
408852e5969SJed Brown   }
409a515125bSLeila Ghaffari 
410a515125bSLeila Ghaffari   // Save data in a binary file for continuation of simulations
41191a36801SJames Wright   if (user->app_ctx->add_stepnum2bin) {
412852e5969SJed Brown     PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution-%" PetscInt_FMT ".bin", user->app_ctx->output_dir, step_no));
41391a36801SJames Wright   } else {
4142b916ea7SJeremy L Thompson     PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution.bin", user->app_ctx->output_dir));
41591a36801SJames Wright   }
4162b916ea7SJeremy L Thompson   PetscCall(PetscViewerBinaryOpen(user->comm, file_path, FILE_MODE_WRITE, &viewer));
4177538d537SJames Wright 
4189293eaa1SJed Brown   PetscInt token = FLUIDS_FILE_TOKEN;
4199293eaa1SJed Brown   PetscCall(PetscViewerBinaryWrite(viewer, &token, 1, PETSC_INT));
4209293eaa1SJed Brown   PetscCall(PetscViewerBinaryWrite(viewer, &step_no, 1, PETSC_INT));
4219293eaa1SJed Brown   time /= user->units->second;  // Dimensionalize time back
4229293eaa1SJed Brown   PetscCall(PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL));
4237538d537SJames Wright   PetscCall(VecView(Q, viewer));
4247538d537SJames Wright   PetscCall(PetscViewerDestroy(&viewer));
4257538d537SJames Wright   PetscFunctionReturn(0);
4267538d537SJames Wright }
4277538d537SJames Wright 
428c5e9980aSAdeleke O. Bankole // CSV Monitor
429c5e9980aSAdeleke O. Bankole PetscErrorCode TSMonitor_WallForce(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) {
430c5e9980aSAdeleke O. Bankole   User              user = ctx;
431c5e9980aSAdeleke O. Bankole   Vec               G_loc;
432c5e9980aSAdeleke O. Bankole   PetscInt          num_wall = user->app_ctx->wall_forces.num_wall, dim = 3;
433c5e9980aSAdeleke O. Bankole   const PetscInt   *walls  = user->app_ctx->wall_forces.walls;
434c5e9980aSAdeleke O. Bankole   PetscViewer       viewer = user->app_ctx->wall_forces.viewer;
435c5e9980aSAdeleke O. Bankole   PetscViewerFormat format = user->app_ctx->wall_forces.viewer_format;
436c5e9980aSAdeleke O. Bankole   PetscScalar      *reaction_force;
437c5e9980aSAdeleke O. Bankole   PetscBool         iascii;
438c5e9980aSAdeleke O. Bankole 
439c5e9980aSAdeleke O. Bankole   PetscFunctionBeginUser;
440c5e9980aSAdeleke O. Bankole   if (!viewer) PetscFunctionReturn(0);
441c5e9980aSAdeleke O. Bankole   PetscCall(DMGetNamedLocalVector(user->dm, "ResidualLocal", &G_loc));
442c5e9980aSAdeleke O. Bankole   PetscCall(PetscMalloc1(num_wall * dim, &reaction_force));
443c5e9980aSAdeleke O. Bankole   PetscCall(Surface_Forces_NS(user->dm, G_loc, num_wall, walls, reaction_force));
444c5e9980aSAdeleke O. Bankole   PetscCall(DMRestoreNamedLocalVector(user->dm, "ResidualLocal", &G_loc));
445c5e9980aSAdeleke O. Bankole 
446c5e9980aSAdeleke O. Bankole   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
447c5e9980aSAdeleke O. Bankole 
448c5e9980aSAdeleke O. Bankole   if (iascii) {
449c5e9980aSAdeleke O. Bankole     if (format == PETSC_VIEWER_ASCII_CSV && !user->app_ctx->wall_forces.header_written) {
450c5e9980aSAdeleke O. Bankole       PetscCall(PetscViewerASCIIPrintf(viewer, "Step,Time,Wall,ForceX,ForceY,ForceZ\n"));
451c5e9980aSAdeleke O. Bankole       user->app_ctx->wall_forces.header_written = PETSC_TRUE;
452c5e9980aSAdeleke O. Bankole     }
453c5e9980aSAdeleke O. Bankole     for (PetscInt w = 0; w < num_wall; w++) {
454c5e9980aSAdeleke O. Bankole       PetscInt wall = walls[w];
455c5e9980aSAdeleke O. Bankole       if (format == PETSC_VIEWER_ASCII_CSV) {
456c5e9980aSAdeleke O. Bankole         PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT ",%g,%" PetscInt_FMT ",%g,%g,%g\n", step_no, time, wall,
457c5e9980aSAdeleke O. Bankole                                          reaction_force[w * dim + 0], reaction_force[w * dim + 1], reaction_force[w * dim + 2]));
458c5e9980aSAdeleke O. Bankole 
459c5e9980aSAdeleke O. Bankole       } else {
460c5e9980aSAdeleke O. Bankole         PetscCall(PetscViewerASCIIPrintf(viewer, "Wall %" PetscInt_FMT " Forces: Force_x = %12g, Force_y = %12g, Force_z = %12g\n", wall,
461c5e9980aSAdeleke O. Bankole                                          reaction_force[w * dim + 0], reaction_force[w * dim + 1], reaction_force[w * dim + 2]));
462c5e9980aSAdeleke O. Bankole       }
463c5e9980aSAdeleke O. Bankole     }
464c5e9980aSAdeleke O. Bankole   }
465c5e9980aSAdeleke O. Bankole   PetscCall(PetscFree(reaction_force));
466c5e9980aSAdeleke O. Bankole   PetscFunctionReturn(0);
467c5e9980aSAdeleke O. Bankole }
468c5e9980aSAdeleke O. Bankole 
4697538d537SJames Wright // User provided TS Monitor
4702b916ea7SJeremy L Thompson PetscErrorCode TSMonitor_NS(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) {
4717538d537SJames Wright   User user = ctx;
4727538d537SJames Wright   PetscFunctionBeginUser;
4737538d537SJames Wright 
474852e5969SJed Brown   // Print every 'checkpoint_interval' steps
475c539088bSJames Wright   if (user->app_ctx->checkpoint_interval <= 0 || step_no % user->app_ctx->checkpoint_interval != 0 ||
476c539088bSJames Wright       (user->app_ctx->cont_steps == step_no && step_no != 0))
477c539088bSJames Wright     PetscFunctionReturn(0);
4787538d537SJames Wright 
4797538d537SJames Wright   PetscCall(WriteOutput(user, Q, step_no, time));
480a515125bSLeila Ghaffari 
481a515125bSLeila Ghaffari   PetscFunctionReturn(0);
482a515125bSLeila Ghaffari }
483a515125bSLeila Ghaffari 
484a515125bSLeila Ghaffari // TS: Create, setup, and solve
4852b916ea7SJeremy L Thompson PetscErrorCode TSSolve_NS(DM dm, User user, AppCtx app_ctx, Physics phys, Vec *Q, PetscScalar *f_time, TS *ts) {
486a515125bSLeila Ghaffari   MPI_Comm    comm = user->comm;
487a515125bSLeila Ghaffari   TSAdapt     adapt;
488a515125bSLeila Ghaffari   PetscScalar final_time;
489a515125bSLeila Ghaffari   PetscFunctionBeginUser;
490a515125bSLeila Ghaffari 
4912b916ea7SJeremy L Thompson   PetscCall(TSCreate(comm, ts));
4922b916ea7SJeremy L Thompson   PetscCall(TSSetDM(*ts, dm));
493a515125bSLeila Ghaffari   if (phys->implicit) {
4942b916ea7SJeremy L Thompson     PetscCall(TSSetType(*ts, TSBDF));
495a515125bSLeila Ghaffari     if (user->op_ifunction) {
4962b916ea7SJeremy L Thompson       PetscCall(TSSetIFunction(*ts, NULL, IFunction_NS, &user));
497a515125bSLeila Ghaffari     } else {  // Implicit integrators can fall back to using an RHSFunction
4982b916ea7SJeremy L Thompson       PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user));
499a515125bSLeila Ghaffari     }
500f0b65372SJed Brown     if (user->op_ijacobian) {
5012b916ea7SJeremy L Thompson       PetscCall(DMTSSetIJacobian(dm, FormIJacobian_NS, &user));
502b107fddaSJed Brown       if (app_ctx->amat_type) {
503b107fddaSJed Brown         Mat Pmat, Amat;
5042b916ea7SJeremy L Thompson         PetscCall(DMCreateMatrix(dm, &Pmat));
5052b916ea7SJeremy L Thompson         PetscCall(DMSetMatType(dm, app_ctx->amat_type));
5062b916ea7SJeremy L Thompson         PetscCall(DMCreateMatrix(dm, &Amat));
5072b916ea7SJeremy L Thompson         PetscCall(TSSetIJacobian(*ts, Amat, Pmat, NULL, NULL));
5082b916ea7SJeremy L Thompson         PetscCall(MatDestroy(&Amat));
5092b916ea7SJeremy L Thompson         PetscCall(MatDestroy(&Pmat));
510b107fddaSJed Brown       }
511f0b65372SJed Brown     }
512a515125bSLeila Ghaffari   } else {
5132b916ea7SJeremy L Thompson     if (!user->op_rhs) SETERRQ(comm, PETSC_ERR_ARG_NULL, "Problem does not provide RHSFunction");
5142b916ea7SJeremy L Thompson     PetscCall(TSSetType(*ts, TSRK));
5152b916ea7SJeremy L Thompson     PetscCall(TSRKSetType(*ts, TSRK5F));
5162b916ea7SJeremy L Thompson     PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user));
517a515125bSLeila Ghaffari   }
5182b916ea7SJeremy L Thompson   PetscCall(TSSetMaxTime(*ts, 500. * user->units->second));
5192b916ea7SJeremy L Thompson   PetscCall(TSSetExactFinalTime(*ts, TS_EXACTFINALTIME_STEPOVER));
520f0784ed3SJed Brown   PetscCall(TSSetErrorIfStepFails(*ts, PETSC_FALSE));
5212b916ea7SJeremy L Thompson   PetscCall(TSSetTimeStep(*ts, 1.e-2 * user->units->second));
5220e1e9333SJames Wright   if (app_ctx->test_type != TESTTYPE_NONE) {
5232b916ea7SJeremy L Thompson     PetscCall(TSSetMaxSteps(*ts, 10));
5242b916ea7SJeremy L Thompson   }
5252b916ea7SJeremy L Thompson   PetscCall(TSGetAdapt(*ts, &adapt));
5262b916ea7SJeremy L Thompson   PetscCall(TSAdaptSetStepLimits(adapt, 1.e-12 * user->units->second, 1.e2 * user->units->second));
5272b916ea7SJeremy L Thompson   PetscCall(TSSetFromOptions(*ts));
528c996854bSJames Wright   user->time = user->time_bc_set = -1.0;  // require all BCs and ctx to be updated
529e2f84137SJeremy L Thompson   user->dt                       = -1.0;
530a515125bSLeila Ghaffari   if (!app_ctx->cont_steps) {  // print initial condition
5310e1e9333SJames Wright     if (app_ctx->test_type == TESTTYPE_NONE) {
5322b916ea7SJeremy L Thompson       PetscCall(TSMonitor_NS(*ts, 0, 0., *Q, user));
533a515125bSLeila Ghaffari     }
534a515125bSLeila Ghaffari   } else {  // continue from time of last output
535a515125bSLeila Ghaffari     PetscInt    count;
536a515125bSLeila Ghaffari     PetscViewer viewer;
5372b916ea7SJeremy L Thompson 
5389293eaa1SJed Brown     if (app_ctx->cont_time <= 0) {  // Legacy files did not include step number and time
5392b916ea7SJeremy L Thompson       PetscCall(PetscViewerBinaryOpen(comm, app_ctx->cont_time_file, FILE_MODE_READ, &viewer));
5409293eaa1SJed Brown       PetscCall(PetscViewerBinaryRead(viewer, &app_ctx->cont_time, 1, &count, PETSC_REAL));
5412b916ea7SJeremy L Thompson       PetscCall(PetscViewerDestroy(&viewer));
5429293eaa1SJed Brown       PetscCheck(app_ctx->cont_steps != -1, comm, PETSC_ERR_ARG_INCOMP,
5439293eaa1SJed Brown                  "-continue step number not specified, but checkpoint file does not contain a step number (likely written by older code version)");
5449293eaa1SJed Brown     }
5459293eaa1SJed Brown     PetscCall(TSSetTime(*ts, app_ctx->cont_time * user->units->second));
54674a6f4ddSJed Brown     PetscCall(TSSetStepNumber(*ts, app_ctx->cont_steps));
547a515125bSLeila Ghaffari   }
5480e1e9333SJames Wright   if (app_ctx->test_type == TESTTYPE_NONE) {
5492b916ea7SJeremy L Thompson     PetscCall(TSMonitorSet(*ts, TSMonitor_NS, user, NULL));
5500e1e9333SJames Wright   }
551c5e9980aSAdeleke O. Bankole   if (app_ctx->wall_forces.viewer) {
552c5e9980aSAdeleke O. Bankole     PetscCall(TSMonitorSet(*ts, TSMonitor_WallForce, user, NULL));
553c5e9980aSAdeleke O. Bankole   }
554c931fa59SJames Wright   if (app_ctx->turb_spanstats_enable) {
555b0488d1fSJames Wright     PetscCall(TSMonitorSet(*ts, TSMonitor_Statistics, user, NULL));
556b8daee98SJames Wright     CeedScalar previous_time = app_ctx->cont_time * user->units->second;
557b8daee98SJames Wright     CeedOperatorContextSetDouble(user->spanstats.op_stats_collect, user->spanstats.previous_time_label, &previous_time);
558b0488d1fSJames Wright   }
559a515125bSLeila Ghaffari 
560a515125bSLeila Ghaffari   // Solve
56174a6f4ddSJed Brown   PetscReal start_time;
56274a6f4ddSJed Brown   PetscInt  start_step;
5632b916ea7SJeremy L Thompson   PetscCall(TSGetTime(*ts, &start_time));
56474a6f4ddSJed Brown   PetscCall(TSGetStepNumber(*ts, &start_step));
56591982731SJeremy L Thompson 
56691982731SJeremy L Thompson   PetscPreLoadBegin(PETSC_FALSE, "Fluids Solve");
56791982731SJeremy L Thompson   PetscCall(TSSetTime(*ts, start_time));
56874a6f4ddSJed Brown   PetscCall(TSSetStepNumber(*ts, start_step));
56991982731SJeremy L Thompson   if (PetscPreLoadingOn) {
57091982731SJeremy L Thompson     // LCOV_EXCL_START
57191982731SJeremy L Thompson     SNES      snes;
57291982731SJeremy L Thompson     Vec       Q_preload;
57391982731SJeremy L Thompson     PetscReal rtol;
57491982731SJeremy L Thompson     PetscCall(VecDuplicate(*Q, &Q_preload));
57591982731SJeremy L Thompson     PetscCall(VecCopy(*Q, Q_preload));
57691982731SJeremy L Thompson     PetscCall(TSGetSNES(*ts, &snes));
57791982731SJeremy L Thompson     PetscCall(SNESGetTolerances(snes, NULL, &rtol, NULL, NULL, NULL));
5782b916ea7SJeremy L Thompson     PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, .99, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT));
57991982731SJeremy L Thompson     PetscCall(TSSetSolution(*ts, *Q));
58091982731SJeremy L Thompson     PetscCall(TSStep(*ts));
5812b916ea7SJeremy L Thompson     PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, rtol, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT));
58291982731SJeremy L Thompson     PetscCall(VecDestroy(&Q_preload));
58391982731SJeremy L Thompson     // LCOV_EXCL_STOP
58491982731SJeremy L Thompson   } else {
5852b916ea7SJeremy L Thompson     PetscCall(PetscBarrier((PetscObject)*ts));
5862b916ea7SJeremy L Thompson     PetscCall(TSSolve(*ts, *Q));
58791982731SJeremy L Thompson   }
58891982731SJeremy L Thompson   PetscPreLoadEnd();
58991982731SJeremy L Thompson 
59091982731SJeremy L Thompson   PetscCall(TSGetSolveTime(*ts, &final_time));
591a515125bSLeila Ghaffari   *f_time = final_time;
59291982731SJeremy L Thompson 
5930e1e9333SJames Wright   if (app_ctx->test_type == TESTTYPE_NONE) {
5947538d537SJames Wright     PetscInt step_no;
5957538d537SJames Wright     PetscCall(TSGetStepNumber(*ts, &step_no));
596b0488d1fSJames Wright     if (user->app_ctx->checkpoint_interval > 0 || user->app_ctx->checkpoint_interval == -1) {
5977538d537SJames Wright       PetscCall(WriteOutput(user, *Q, step_no, final_time));
5987538d537SJames Wright     }
5997538d537SJames Wright 
60091982731SJeremy L Thompson     PetscLogEvent stage_id;
60191982731SJeremy L Thompson     PetscStageLog stage_log;
60291982731SJeremy L Thompson 
60391982731SJeremy L Thompson     PetscCall(PetscLogStageGetId("Fluids Solve", &stage_id));
60491982731SJeremy L Thompson     PetscCall(PetscLogGetStageLog(&stage_log));
6052b916ea7SJeremy L Thompson     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Time taken for solution (sec): %g\n", stage_log->stageInfo[stage_id].perfInfo.time));
606a515125bSLeila Ghaffari   }
607a515125bSLeila Ghaffari   PetscFunctionReturn(0);
608a515125bSLeila Ghaffari }
609