xref: /libCEED/examples/fluids/src/setupts.c (revision c798d55a05db7b418ed6055883f6fe79853e9384)
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 
1177841947SLeila Ghaffari #include "../navierstokes.h"
12ca69d878SAdeleke O. Bankole #include "../qfunctions/newtonian_state.h"
1377841947SLeila Ghaffari 
1477841947SLeila Ghaffari // Compute mass matrix for explicit scheme
152b730f8bSJeremy L Thompson PetscErrorCode ComputeLumpedMassMatrix(Ceed ceed, DM dm, CeedData ceed_data, Vec M) {
1677841947SLeila Ghaffari   Vec           M_loc;
1777841947SLeila Ghaffari   CeedQFunction qf_mass;
1877841947SLeila Ghaffari   CeedOperator  op_mass;
1977841947SLeila Ghaffari   CeedVector    m_ceed, ones_vec;
2077841947SLeila Ghaffari   CeedInt       num_comp_q, q_data_size;
2177841947SLeila Ghaffari   PetscFunctionBeginUser;
2277841947SLeila Ghaffari 
2377841947SLeila Ghaffari   // CEED Restriction
2477841947SLeila Ghaffari   CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_q, &num_comp_q);
2577841947SLeila Ghaffari   CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_qd_i, &q_data_size);
2677841947SLeila Ghaffari   CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &m_ceed, NULL);
2777841947SLeila Ghaffari   CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &ones_vec, NULL);
2877841947SLeila Ghaffari   CeedVectorSetValue(ones_vec, 1.0);
2977841947SLeila Ghaffari 
3077841947SLeila Ghaffari   // CEED QFunction
31ef080ff9SJames Wright   PetscCall(CreateMassQFunction(ceed, num_comp_q, q_data_size, &qf_mass));
3277841947SLeila Ghaffari 
3377841947SLeila Ghaffari   // CEED Operator
3477841947SLeila Ghaffari   CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass);
35ef080ff9SJames Wright   CeedOperatorSetField(op_mass, "u", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE);
362b730f8bSJeremy L Thompson   CeedOperatorSetField(op_mass, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data->q_data);
372b730f8bSJeremy L Thompson   CeedOperatorSetField(op_mass, "v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE);
3877841947SLeila Ghaffari 
3977841947SLeila Ghaffari   // Place PETSc vector in CEED vector
4077841947SLeila Ghaffari   PetscMemType m_mem_type;
412b730f8bSJeremy L Thompson   PetscCall(DMGetLocalVector(dm, &M_loc));
42*c798d55aSJames Wright   PetscCall(VecP2C(M_loc, &m_mem_type, m_ceed));
4377841947SLeila Ghaffari 
4477841947SLeila Ghaffari   // Apply CEED Operator
4577841947SLeila Ghaffari   CeedOperatorApply(op_mass, ones_vec, m_ceed, CEED_REQUEST_IMMEDIATE);
4677841947SLeila Ghaffari 
4777841947SLeila Ghaffari   // Restore vectors
48*c798d55aSJames Wright   PetscCall(VecC2P(m_ceed, m_mem_type, M_loc));
4977841947SLeila Ghaffari 
5077841947SLeila Ghaffari   // Local-to-Global
512b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(M));
522b730f8bSJeremy L Thompson   PetscCall(DMLocalToGlobal(dm, M_loc, ADD_VALUES, M));
532b730f8bSJeremy L Thompson   PetscCall(DMRestoreLocalVector(dm, &M_loc));
5477841947SLeila Ghaffari 
5577841947SLeila Ghaffari   // Invert diagonally lumped mass vector for RHS function
562b730f8bSJeremy L Thompson   PetscCall(VecReciprocal(M));
5777841947SLeila Ghaffari 
5877841947SLeila Ghaffari   // Cleanup
5977841947SLeila Ghaffari   CeedVectorDestroy(&ones_vec);
6077841947SLeila Ghaffari   CeedVectorDestroy(&m_ceed);
6177841947SLeila Ghaffari   CeedQFunctionDestroy(&qf_mass);
6277841947SLeila Ghaffari   CeedOperatorDestroy(&op_mass);
6377841947SLeila Ghaffari 
6477841947SLeila Ghaffari   PetscFunctionReturn(0);
6577841947SLeila Ghaffari }
6677841947SLeila Ghaffari 
67a0b9a424SJames Wright // Insert Boundary values if it's a new time
68a0b9a424SJames Wright PetscErrorCode UpdateBoundaryValues(User user, Vec Q_loc, PetscReal t) {
69a0b9a424SJames Wright   PetscFunctionBeginUser;
70a0b9a424SJames Wright   if (user->time_bc_set != t) {
71a0b9a424SJames Wright     PetscCall(DMPlexInsertBoundaryValues(user->dm, PETSC_TRUE, Q_loc, t, NULL, NULL, NULL));
72a0b9a424SJames Wright     user->time_bc_set = t;
73a0b9a424SJames Wright   }
74a0b9a424SJames Wright   PetscFunctionReturn(0);
75a0b9a424SJames Wright }
76a0b9a424SJames Wright 
77e42c1df6SJames Wright PetscErrorCode UpdateContextLabel(PetscScalar *previous_value, PetscScalar update_value, CeedOperator op, CeedContextFieldLabel label) {
78e42c1df6SJames Wright   PetscFunctionBeginUser;
79e42c1df6SJames Wright 
80e42c1df6SJames Wright   if (*previous_value != update_value) {
81e42c1df6SJames Wright     if (label) {
82e42c1df6SJames Wright       CeedOperatorContextSetDouble(op, label, &update_value);
83e42c1df6SJames Wright     }
84e42c1df6SJames Wright     *previous_value = update_value;
85e42c1df6SJames Wright   }
86e42c1df6SJames Wright   PetscFunctionReturn(0);
87e42c1df6SJames Wright }
88e42c1df6SJames Wright 
8977841947SLeila Ghaffari // RHS (Explicit time-stepper) function setup
9077841947SLeila Ghaffari //   This is the RHS of the ODE, given as u_t = G(t,u)
9177841947SLeila Ghaffari //   This function takes in a state vector Q and writes into G
9277841947SLeila Ghaffari PetscErrorCode RHS_NS(TS ts, PetscReal t, Vec Q, Vec G, void *user_data) {
9377841947SLeila Ghaffari   User         user = *(User *)user_data;
94*c798d55aSJames Wright   PetscScalar  dt;
955e82a6e1SJeremy L Thompson   Vec          Q_loc = user->Q_loc, G_loc;
9677841947SLeila Ghaffari   PetscMemType q_mem_type, g_mem_type;
9777841947SLeila Ghaffari   PetscFunctionBeginUser;
9877841947SLeila Ghaffari 
995e82a6e1SJeremy L Thompson   // Get local vector
1002b730f8bSJeremy L Thompson   PetscCall(DMGetLocalVector(user->dm, &G_loc));
1015e82a6e1SJeremy L Thompson 
1025e82a6e1SJeremy L Thompson   // Update time dependent data
103a0b9a424SJames Wright   PetscCall(UpdateBoundaryValues(user, Q_loc, t));
104e42c1df6SJames Wright   PetscCall(UpdateContextLabel(&user->time, t, user->op_rhs, user->phys->solution_time_label));
1052b730f8bSJeremy L Thompson   PetscCall(TSGetTimeStep(ts, &dt));
106e42c1df6SJames Wright   PetscCall(UpdateContextLabel(&user->dt, dt, user->op_rhs, user->phys->timestep_size_label));
10777841947SLeila Ghaffari 
10877841947SLeila Ghaffari   // Global-to-local
1092b730f8bSJeremy L Thompson   PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc));
11077841947SLeila Ghaffari 
11177841947SLeila Ghaffari   // Place PETSc vectors in CEED vectors
112*c798d55aSJames Wright   PetscCall(VecReadP2C(Q_loc, &q_mem_type, user->q_ceed));
113*c798d55aSJames Wright   PetscCall(VecP2C(G_loc, &g_mem_type, user->g_ceed));
11477841947SLeila Ghaffari 
11577841947SLeila Ghaffari   // Apply CEED operator
1162b730f8bSJeremy L Thompson   CeedOperatorApply(user->op_rhs, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE);
11777841947SLeila Ghaffari 
11877841947SLeila Ghaffari   // Restore vectors
119*c798d55aSJames Wright   PetscCall(VecReadC2P(user->q_ceed, q_mem_type, Q_loc));
120*c798d55aSJames Wright   PetscCall(VecC2P(user->g_ceed, g_mem_type, G_loc));
12177841947SLeila Ghaffari 
12277841947SLeila Ghaffari   // Local-to-Global
1232b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(G));
1242b730f8bSJeremy L Thompson   PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G));
12577841947SLeila Ghaffari 
12677841947SLeila Ghaffari   // Inverse of the lumped mass matrix (M is Minv)
1272b730f8bSJeremy L Thompson   PetscCall(VecPointwiseMult(G, G, user->M));
12877841947SLeila Ghaffari 
12977841947SLeila Ghaffari   // Restore vectors
1302b730f8bSJeremy L Thompson   PetscCall(DMRestoreLocalVector(user->dm, &G_loc));
13177841947SLeila Ghaffari 
13277841947SLeila Ghaffari   PetscFunctionReturn(0);
13377841947SLeila Ghaffari }
13477841947SLeila Ghaffari 
135ca69d878SAdeleke O. Bankole // Surface forces function setup
136ca69d878SAdeleke O. Bankole static PetscErrorCode Surface_Forces_NS(DM dm, Vec G_loc, PetscInt num_walls, const PetscInt walls[], PetscScalar *reaction_force) {
137ca69d878SAdeleke O. Bankole   DMLabel            face_label;
138ca69d878SAdeleke O. Bankole   const PetscScalar *g;
139ca69d878SAdeleke O. Bankole   PetscInt           dim = 3;
140ca69d878SAdeleke O. Bankole   MPI_Comm           comm;
141ca69d878SAdeleke O. Bankole 
142ca69d878SAdeleke O. Bankole   PetscFunctionBeginUser;
143ca69d878SAdeleke O. Bankole   PetscCall(PetscArrayzero(reaction_force, num_walls * dim));
144ca69d878SAdeleke O. Bankole   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
145ca69d878SAdeleke O. Bankole   PetscCall(DMGetLabel(dm, "Face Sets", &face_label));
146ca69d878SAdeleke O. Bankole   PetscCall(VecGetArrayRead(G_loc, &g));
147ca69d878SAdeleke O. Bankole   for (PetscInt w = 0; w < num_walls; w++) {
148ca69d878SAdeleke O. Bankole     const PetscInt wall = walls[w];
149ca69d878SAdeleke O. Bankole     IS             wall_is;
150ca69d878SAdeleke O. Bankole     PetscCall(DMLabelGetStratumIS(face_label, wall, &wall_is));
151ca69d878SAdeleke O. Bankole     if (wall_is) {  // There exist such points on this process
152ca69d878SAdeleke O. Bankole       PetscInt        num_points;
153ca69d878SAdeleke O. Bankole       const PetscInt *points;
154ca69d878SAdeleke O. Bankole       PetscCall(ISGetSize(wall_is, &num_points));
155ca69d878SAdeleke O. Bankole       PetscCall(ISGetIndices(wall_is, &points));
156ca69d878SAdeleke O. Bankole       for (PetscInt i = 0; i < num_points; i++) {
157ca69d878SAdeleke O. Bankole         const PetscInt           p = points[i];
158ca69d878SAdeleke O. Bankole         const StateConservative *r;
159ca69d878SAdeleke O. Bankole         PetscCall(DMPlexPointLocalRead(dm, p, g, &r));
160ca69d878SAdeleke O. Bankole         if (!r) continue;
161ca69d878SAdeleke O. Bankole         for (PetscInt j = 0; j < 3; j++) {
162ca69d878SAdeleke O. Bankole           reaction_force[w * dim + j] -= r->momentum[j];
163ca69d878SAdeleke O. Bankole         }
164ca69d878SAdeleke O. Bankole       }
165ca69d878SAdeleke O. Bankole       PetscCall(ISRestoreIndices(wall_is, &points));
166ca69d878SAdeleke O. Bankole     }
167ca69d878SAdeleke O. Bankole     PetscCall(ISDestroy(&wall_is));
168ca69d878SAdeleke O. Bankole   }
169ca69d878SAdeleke O. Bankole   PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, reaction_force, dim * num_walls, MPIU_SCALAR, MPI_SUM, comm));
170ca69d878SAdeleke O. Bankole   //  Restore Vectors
171ca69d878SAdeleke O. Bankole   PetscCall(VecRestoreArrayRead(G_loc, &g));
172ca69d878SAdeleke O. Bankole 
173ca69d878SAdeleke O. Bankole   PetscFunctionReturn(0);
174ca69d878SAdeleke O. Bankole }
175ca69d878SAdeleke O. Bankole 
17677841947SLeila Ghaffari // Implicit time-stepper function setup
1772b730f8bSJeremy L Thompson PetscErrorCode IFunction_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, Vec G, void *user_data) {
17877841947SLeila Ghaffari   User         user = *(User *)user_data;
179*c798d55aSJames Wright   PetscScalar  dt;
1805e82a6e1SJeremy L Thompson   Vec          Q_loc = user->Q_loc, Q_dot_loc = user->Q_dot_loc, G_loc;
18177841947SLeila Ghaffari   PetscMemType q_mem_type, q_dot_mem_type, g_mem_type;
18277841947SLeila Ghaffari   PetscFunctionBeginUser;
18377841947SLeila Ghaffari 
1845e82a6e1SJeremy L Thompson   // Get local vectors
185ca69d878SAdeleke O. Bankole   PetscCall(DMGetNamedLocalVector(user->dm, "ResidualLocal", &G_loc));
1865e82a6e1SJeremy L Thompson 
1875e82a6e1SJeremy L Thompson   // Update time dependent data
188a0b9a424SJames Wright   PetscCall(UpdateBoundaryValues(user, Q_loc, t));
189e42c1df6SJames Wright   PetscCall(UpdateContextLabel(&user->time, t, user->op_ifunction, user->phys->solution_time_label));
1902b730f8bSJeremy L Thompson   PetscCall(TSGetTimeStep(ts, &dt));
191e42c1df6SJames Wright   PetscCall(UpdateContextLabel(&user->dt, dt, user->op_ifunction, user->phys->timestep_size_label));
19277841947SLeila Ghaffari 
19377841947SLeila Ghaffari   // Global-to-local
194878eb0e7SJames Wright   PetscCall(DMGlobalToLocalBegin(user->dm, Q, INSERT_VALUES, Q_loc));
195878eb0e7SJames Wright   PetscCall(DMGlobalToLocalBegin(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc));
196878eb0e7SJames Wright   PetscCall(DMGlobalToLocalEnd(user->dm, Q, INSERT_VALUES, Q_loc));
197878eb0e7SJames Wright   PetscCall(DMGlobalToLocalEnd(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc));
19877841947SLeila Ghaffari 
19977841947SLeila Ghaffari   // Place PETSc vectors in CEED vectors
200*c798d55aSJames Wright   PetscCall(VecReadP2C(Q_loc, &q_mem_type, user->q_ceed));
201*c798d55aSJames Wright   PetscCall(VecReadP2C(Q_dot_loc, &q_dot_mem_type, user->q_dot_ceed));
202*c798d55aSJames Wright   PetscCall(VecP2C(G_loc, &g_mem_type, user->g_ceed));
20377841947SLeila Ghaffari 
20477841947SLeila Ghaffari   // Apply CEED operator
2052b730f8bSJeremy L Thompson   CeedOperatorApply(user->op_ifunction, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE);
20677841947SLeila Ghaffari 
20777841947SLeila Ghaffari   // Restore vectors
208*c798d55aSJames Wright   PetscCall(VecReadC2P(user->q_ceed, q_mem_type, Q_loc));
209*c798d55aSJames Wright   PetscCall(VecReadC2P(user->q_dot_ceed, q_dot_mem_type, Q_dot_loc));
210*c798d55aSJames Wright   PetscCall(VecC2P(user->g_ceed, g_mem_type, G_loc));
21177841947SLeila Ghaffari 
21277841947SLeila Ghaffari   // Local-to-Global
2132b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(G));
2142b730f8bSJeremy L Thompson   PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G));
21577841947SLeila Ghaffari 
21677841947SLeila Ghaffari   // Restore vectors
217ca69d878SAdeleke O. Bankole   PetscCall(DMRestoreNamedLocalVector(user->dm, "ResidualLocal", &G_loc));
21877841947SLeila Ghaffari 
21977841947SLeila Ghaffari   PetscFunctionReturn(0);
22077841947SLeila Ghaffari }
22177841947SLeila Ghaffari 
222e334ad8fSJed Brown static PetscErrorCode MatMult_NS_IJacobian(Mat J, Vec Q, Vec G) {
223e334ad8fSJed Brown   User               user;
224e334ad8fSJed Brown   const PetscScalar *q;
225e334ad8fSJed Brown   PetscScalar       *g;
226e334ad8fSJed Brown   PetscMemType       q_mem_type, g_mem_type;
227e334ad8fSJed Brown   PetscFunctionBeginUser;
2282b730f8bSJeremy L Thompson   PetscCall(MatShellGetContext(J, &user));
2295e82a6e1SJeremy L Thompson   Vec Q_loc = user->Q_dot_loc,  // Note - Q_dot_loc has zero BCs
2305e82a6e1SJeremy L Thompson       G_loc;
2315e82a6e1SJeremy L Thompson 
232e334ad8fSJed Brown   // Get local vectors
2332b730f8bSJeremy L Thompson   PetscCall(DMGetLocalVector(user->dm, &G_loc));
234e334ad8fSJed Brown 
235e334ad8fSJed Brown   // Global-to-local
2362b730f8bSJeremy L Thompson   PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc));
237e334ad8fSJed Brown 
238e334ad8fSJed Brown   // Place PETSc vectors in CEED vectors
2392b730f8bSJeremy L Thompson   PetscCall(VecGetArrayReadAndMemType(Q_loc, &q, &q_mem_type));
2402b730f8bSJeremy L Thompson   PetscCall(VecGetArrayAndMemType(G_loc, &g, &g_mem_type));
2412b730f8bSJeremy L Thompson   CeedVectorSetArray(user->q_ceed, MemTypeP2C(q_mem_type), CEED_USE_POINTER, (PetscScalar *)q);
242e334ad8fSJed Brown   CeedVectorSetArray(user->g_ceed, MemTypeP2C(g_mem_type), CEED_USE_POINTER, g);
243e334ad8fSJed Brown 
244e334ad8fSJed Brown   // Apply CEED operator
2452b730f8bSJeremy L Thompson   CeedOperatorApply(user->op_ijacobian, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE);
246e334ad8fSJed Brown 
247e334ad8fSJed Brown   // Restore vectors
248e334ad8fSJed Brown   CeedVectorTakeArray(user->q_ceed, MemTypeP2C(q_mem_type), NULL);
249e334ad8fSJed Brown   CeedVectorTakeArray(user->g_ceed, MemTypeP2C(g_mem_type), NULL);
2502b730f8bSJeremy L Thompson   PetscCall(VecRestoreArrayReadAndMemType(Q_loc, &q));
2512b730f8bSJeremy L Thompson   PetscCall(VecRestoreArrayAndMemType(G_loc, &g));
252e334ad8fSJed Brown 
253e334ad8fSJed Brown   // Local-to-Global
2542b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(G));
2552b730f8bSJeremy L Thompson   PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G));
256e334ad8fSJed Brown 
257e334ad8fSJed Brown   // Restore vectors
2582b730f8bSJeremy L Thompson   PetscCall(DMRestoreLocalVector(user->dm, &G_loc));
259e334ad8fSJed Brown   PetscFunctionReturn(0);
260e334ad8fSJed Brown }
261e334ad8fSJed Brown 
262e334ad8fSJed Brown PetscErrorCode MatGetDiagonal_NS_IJacobian(Mat A, Vec D) {
263e334ad8fSJed Brown   User         user;
264e334ad8fSJed Brown   Vec          D_loc;
265e334ad8fSJed Brown   PetscScalar *d;
266e334ad8fSJed Brown   PetscMemType mem_type;
267e334ad8fSJed Brown 
268e334ad8fSJed Brown   PetscFunctionBeginUser;
269e334ad8fSJed Brown   MatShellGetContext(A, &user);
270e334ad8fSJed Brown   PetscCall(DMGetLocalVector(user->dm, &D_loc));
271e334ad8fSJed Brown   PetscCall(VecGetArrayAndMemType(D_loc, &d, &mem_type));
272e334ad8fSJed Brown   CeedVectorSetArray(user->g_ceed, MemTypeP2C(mem_type), CEED_USE_POINTER, d);
2732b730f8bSJeremy L Thompson   CeedOperatorLinearAssembleDiagonal(user->op_ijacobian, user->g_ceed, CEED_REQUEST_IMMEDIATE);
274e334ad8fSJed Brown   CeedVectorTakeArray(user->g_ceed, MemTypeP2C(mem_type), NULL);
275e334ad8fSJed Brown   PetscCall(VecRestoreArrayAndMemType(D_loc, &d));
276e334ad8fSJed Brown   PetscCall(VecZeroEntries(D));
277e334ad8fSJed Brown   PetscCall(DMLocalToGlobal(user->dm, D_loc, ADD_VALUES, D));
278e334ad8fSJed Brown   PetscCall(DMRestoreLocalVector(user->dm, &D_loc));
279e334ad8fSJed Brown   VecViewFromOptions(D, NULL, "-diag_vec_view");
280e334ad8fSJed Brown   PetscFunctionReturn(0);
281e334ad8fSJed Brown }
282e334ad8fSJed Brown 
2832b730f8bSJeremy L Thompson static PetscErrorCode FormPreallocation(User user, PetscBool pbdiagonal, Mat J, CeedVector *coo_values) {
284544be873SJed Brown   PetscCount ncoo;
285544be873SJed Brown   PetscInt  *rows, *cols;
286544be873SJed Brown 
287544be873SJed Brown   PetscFunctionBeginUser;
288544be873SJed Brown   if (pbdiagonal) {
289544be873SJed Brown     CeedSize l_size;
290544be873SJed Brown     CeedOperatorGetActiveVectorLengths(user->op_ijacobian, &l_size, NULL);
291544be873SJed Brown     ncoo = l_size * 5;
292544be873SJed Brown     rows = malloc(ncoo * sizeof(rows[0]));
293544be873SJed Brown     cols = malloc(ncoo * sizeof(cols[0]));
294544be873SJed Brown     for (PetscCount n = 0; n < l_size / 5; n++) {
295544be873SJed Brown       for (PetscInt i = 0; i < 5; i++) {
296544be873SJed Brown         for (PetscInt j = 0; j < 5; j++) {
297544be873SJed Brown           rows[(n * 5 + i) * 5 + j] = n * 5 + i;
298544be873SJed Brown           cols[(n * 5 + i) * 5 + j] = n * 5 + j;
299544be873SJed Brown         }
300544be873SJed Brown       }
301544be873SJed Brown     }
302544be873SJed Brown   } else {
3032b730f8bSJeremy L Thompson     PetscCall(CeedOperatorLinearAssembleSymbolic(user->op_ijacobian, &ncoo, &rows, &cols));
304544be873SJed Brown   }
305544be873SJed Brown   PetscCall(MatSetPreallocationCOOLocal(J, ncoo, rows, cols));
306544be873SJed Brown   free(rows);
307544be873SJed Brown   free(cols);
308544be873SJed Brown   CeedVectorCreate(user->ceed, ncoo, coo_values);
309544be873SJed Brown   PetscFunctionReturn(0);
310544be873SJed Brown }
311544be873SJed Brown 
3122b730f8bSJeremy L Thompson static PetscErrorCode FormSetValues(User user, PetscBool pbdiagonal, Mat J, CeedVector coo_values) {
313544be873SJed Brown   CeedMemType        mem_type = CEED_MEM_HOST;
314544be873SJed Brown   const PetscScalar *values;
315544be873SJed Brown   MatType            mat_type;
316544be873SJed Brown 
317544be873SJed Brown   PetscFunctionBeginUser;
318544be873SJed Brown   PetscCall(MatGetType(J, &mat_type));
3192b730f8bSJeremy L Thompson   if (strstr(mat_type, "kokkos") || strstr(mat_type, "cusparse")) mem_type = CEED_MEM_DEVICE;
320544be873SJed Brown   if (user->app_ctx->pmat_pbdiagonal) {
3212b730f8bSJeremy L Thompson     CeedOperatorLinearAssemblePointBlockDiagonal(user->op_ijacobian, coo_values, CEED_REQUEST_IMMEDIATE);
322544be873SJed Brown   } else {
323544be873SJed Brown     CeedOperatorLinearAssemble(user->op_ijacobian, coo_values);
324544be873SJed Brown   }
325544be873SJed Brown   CeedVectorGetArrayRead(coo_values, mem_type, &values);
326544be873SJed Brown   PetscCall(MatSetValuesCOO(J, values, INSERT_VALUES));
327544be873SJed Brown   CeedVectorRestoreArrayRead(coo_values, &values);
328544be873SJed Brown   PetscFunctionReturn(0);
329544be873SJed Brown }
330544be873SJed Brown 
3312b730f8bSJeremy L Thompson PetscErrorCode FormIJacobian_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, PetscReal shift, Mat J, Mat J_pre, void *user_data) {
332e334ad8fSJed Brown   User      user = *(User *)user_data;
333d6bf345cSJed Brown   PetscBool J_is_shell, J_is_mffd, J_pre_is_shell;
334e334ad8fSJed Brown   PetscFunctionBeginUser;
3352b730f8bSJeremy L Thompson   if (user->phys->ijacobian_time_shift_label) CeedOperatorContextSetDouble(user->op_ijacobian, user->phys->ijacobian_time_shift_label, &shift);
336d6bf345cSJed Brown   PetscCall(PetscObjectTypeCompare((PetscObject)J, MATMFFD, &J_is_mffd));
337e334ad8fSJed Brown   PetscCall(PetscObjectTypeCompare((PetscObject)J, MATSHELL, &J_is_shell));
3382b730f8bSJeremy L Thompson   PetscCall(PetscObjectTypeCompare((PetscObject)J_pre, MATSHELL, &J_pre_is_shell));
339e334ad8fSJed Brown   if (!user->matrices_set_up) {
340e334ad8fSJed Brown     if (J_is_shell) {
341e334ad8fSJed Brown       PetscCall(MatShellSetContext(J, user));
3422b730f8bSJeremy L Thompson       PetscCall(MatShellSetOperation(J, MATOP_MULT, (void (*)(void))MatMult_NS_IJacobian));
3432b730f8bSJeremy L Thompson       PetscCall(MatShellSetOperation(J, MATOP_GET_DIAGONAL, (void (*)(void))MatGetDiagonal_NS_IJacobian));
344e334ad8fSJed Brown       PetscCall(MatSetUp(J));
345e334ad8fSJed Brown     }
346e334ad8fSJed Brown     if (!J_pre_is_shell) {
3472b730f8bSJeremy L Thompson       PetscCall(FormPreallocation(user, user->app_ctx->pmat_pbdiagonal, J_pre, &user->coo_values_pmat));
348544be873SJed Brown     }
349d6bf345cSJed Brown     if (J != J_pre && !J_is_shell && !J_is_mffd) {
350544be873SJed Brown       PetscCall(FormPreallocation(user, PETSC_FALSE, J, &user->coo_values_amat));
351544be873SJed Brown     }
352e334ad8fSJed Brown     user->matrices_set_up = true;
353e334ad8fSJed Brown   }
354e334ad8fSJed Brown   if (!J_pre_is_shell) {
3552b730f8bSJeremy L Thompson     PetscCall(FormSetValues(user, user->app_ctx->pmat_pbdiagonal, J_pre, user->coo_values_pmat));
356e334ad8fSJed Brown   }
357d6bf345cSJed Brown   if (user->coo_values_amat) {
358d6bf345cSJed Brown     PetscCall(FormSetValues(user, PETSC_FALSE, J, user->coo_values_amat));
359d6bf345cSJed Brown   } else if (J_is_mffd) {
360d6bf345cSJed Brown     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
361d6bf345cSJed Brown     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
362d6bf345cSJed Brown   }
363e334ad8fSJed Brown   PetscFunctionReturn(0);
364e334ad8fSJed Brown }
365e334ad8fSJed Brown 
3662b730f8bSJeremy L Thompson PetscErrorCode WriteOutput(User user, Vec Q, PetscInt step_no, PetscScalar time) {
36777841947SLeila Ghaffari   Vec         Q_loc;
36877841947SLeila Ghaffari   char        file_path[PETSC_MAX_PATH_LEN];
36977841947SLeila Ghaffari   PetscViewer viewer;
37077841947SLeila Ghaffari   PetscFunctionBeginUser;
37177841947SLeila Ghaffari 
37237cbb16aSJed Brown   if (user->app_ctx->checkpoint_vtk) {
37377841947SLeila Ghaffari     // Set up output
374f14660a4SJames Wright     PetscCall(DMGetLocalVector(user->dm, &Q_loc));
375f14660a4SJames Wright     PetscCall(PetscObjectSetName((PetscObject)Q_loc, "StateVec"));
376f14660a4SJames Wright     PetscCall(VecZeroEntries(Q_loc));
377f14660a4SJames Wright     PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc));
37877841947SLeila Ghaffari 
37977841947SLeila Ghaffari     // Output
38037cbb16aSJed Brown     PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no));
381f14660a4SJames Wright 
3822b730f8bSJeremy L Thompson     PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q), file_path, FILE_MODE_WRITE, &viewer));
383f14660a4SJames Wright     PetscCall(VecView(Q_loc, viewer));
384f14660a4SJames Wright     PetscCall(PetscViewerDestroy(&viewer));
38577841947SLeila Ghaffari     if (user->dm_viz) {
38677841947SLeila Ghaffari       Vec         Q_refined, Q_refined_loc;
38777841947SLeila Ghaffari       char        file_path_refined[PETSC_MAX_PATH_LEN];
38877841947SLeila Ghaffari       PetscViewer viewer_refined;
38977841947SLeila Ghaffari 
390f14660a4SJames Wright       PetscCall(DMGetGlobalVector(user->dm_viz, &Q_refined));
391f14660a4SJames Wright       PetscCall(DMGetLocalVector(user->dm_viz, &Q_refined_loc));
392f14660a4SJames Wright       PetscCall(PetscObjectSetName((PetscObject)Q_refined_loc, "Refined"));
393f14660a4SJames Wright 
394f14660a4SJames Wright       PetscCall(MatInterpolate(user->interp_viz, Q, Q_refined));
395f14660a4SJames Wright       PetscCall(VecZeroEntries(Q_refined_loc));
3962b730f8bSJeremy L Thompson       PetscCall(DMGlobalToLocal(user->dm_viz, Q_refined, INSERT_VALUES, Q_refined_loc));
397f14660a4SJames Wright 
39837cbb16aSJed Brown       PetscCall(
39937cbb16aSJed Brown           PetscSNPrintf(file_path_refined, sizeof file_path_refined, "%s/nsrefined-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no));
400f14660a4SJames Wright 
4012b730f8bSJeremy L Thompson       PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q_refined), file_path_refined, FILE_MODE_WRITE, &viewer_refined));
402f14660a4SJames Wright       PetscCall(VecView(Q_refined_loc, viewer_refined));
403f14660a4SJames Wright       PetscCall(DMRestoreLocalVector(user->dm_viz, &Q_refined_loc));
404f14660a4SJames Wright       PetscCall(DMRestoreGlobalVector(user->dm_viz, &Q_refined));
405f14660a4SJames Wright       PetscCall(PetscViewerDestroy(&viewer_refined));
40677841947SLeila Ghaffari     }
407f14660a4SJames Wright     PetscCall(DMRestoreLocalVector(user->dm, &Q_loc));
40837cbb16aSJed Brown   }
40977841947SLeila Ghaffari 
41077841947SLeila Ghaffari   // Save data in a binary file for continuation of simulations
41169293791SJames Wright   if (user->app_ctx->add_stepnum2bin) {
41237cbb16aSJed Brown     PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution-%" PetscInt_FMT ".bin", user->app_ctx->output_dir, step_no));
41369293791SJames Wright   } else {
4142b730f8bSJeremy L Thompson     PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution.bin", user->app_ctx->output_dir));
41569293791SJames Wright   }
4162b730f8bSJeremy L Thompson   PetscCall(PetscViewerBinaryOpen(user->comm, file_path, FILE_MODE_WRITE, &viewer));
417f14660a4SJames Wright 
4184de8550aSJed Brown   PetscInt token = FLUIDS_FILE_TOKEN;
4194de8550aSJed Brown   PetscCall(PetscViewerBinaryWrite(viewer, &token, 1, PETSC_INT));
4204de8550aSJed Brown   PetscCall(PetscViewerBinaryWrite(viewer, &step_no, 1, PETSC_INT));
4214de8550aSJed Brown   time /= user->units->second;  // Dimensionalize time back
4224de8550aSJed Brown   PetscCall(PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL));
423f14660a4SJames Wright   PetscCall(VecView(Q, viewer));
424f14660a4SJames Wright   PetscCall(PetscViewerDestroy(&viewer));
425f14660a4SJames Wright   PetscFunctionReturn(0);
426f14660a4SJames Wright }
427f14660a4SJames Wright 
428ca69d878SAdeleke O. Bankole // CSV Monitor
429ca69d878SAdeleke O. Bankole PetscErrorCode TSMonitor_WallForce(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) {
430ca69d878SAdeleke O. Bankole   User              user = ctx;
431ca69d878SAdeleke O. Bankole   Vec               G_loc;
432ca69d878SAdeleke O. Bankole   PetscInt          num_wall = user->app_ctx->wall_forces.num_wall, dim = 3;
433ca69d878SAdeleke O. Bankole   const PetscInt   *walls  = user->app_ctx->wall_forces.walls;
434ca69d878SAdeleke O. Bankole   PetscViewer       viewer = user->app_ctx->wall_forces.viewer;
435ca69d878SAdeleke O. Bankole   PetscViewerFormat format = user->app_ctx->wall_forces.viewer_format;
436ca69d878SAdeleke O. Bankole   PetscScalar      *reaction_force;
437ca69d878SAdeleke O. Bankole   PetscBool         iascii;
438ca69d878SAdeleke O. Bankole 
439ca69d878SAdeleke O. Bankole   PetscFunctionBeginUser;
440ca69d878SAdeleke O. Bankole   if (!viewer) PetscFunctionReturn(0);
441ca69d878SAdeleke O. Bankole   PetscCall(DMGetNamedLocalVector(user->dm, "ResidualLocal", &G_loc));
442ca69d878SAdeleke O. Bankole   PetscCall(PetscMalloc1(num_wall * dim, &reaction_force));
443ca69d878SAdeleke O. Bankole   PetscCall(Surface_Forces_NS(user->dm, G_loc, num_wall, walls, reaction_force));
444ca69d878SAdeleke O. Bankole   PetscCall(DMRestoreNamedLocalVector(user->dm, "ResidualLocal", &G_loc));
445ca69d878SAdeleke O. Bankole 
446ca69d878SAdeleke O. Bankole   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
447ca69d878SAdeleke O. Bankole 
448ca69d878SAdeleke O. Bankole   if (iascii) {
449ca69d878SAdeleke O. Bankole     if (format == PETSC_VIEWER_ASCII_CSV && !user->app_ctx->wall_forces.header_written) {
450ca69d878SAdeleke O. Bankole       PetscCall(PetscViewerASCIIPrintf(viewer, "Step,Time,Wall,ForceX,ForceY,ForceZ\n"));
451ca69d878SAdeleke O. Bankole       user->app_ctx->wall_forces.header_written = PETSC_TRUE;
452ca69d878SAdeleke O. Bankole     }
453ca69d878SAdeleke O. Bankole     for (PetscInt w = 0; w < num_wall; w++) {
454ca69d878SAdeleke O. Bankole       PetscInt wall = walls[w];
455ca69d878SAdeleke O. Bankole       if (format == PETSC_VIEWER_ASCII_CSV) {
456ca69d878SAdeleke O. Bankole         PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT ",%g,%" PetscInt_FMT ",%g,%g,%g\n", step_no, time, wall,
457ca69d878SAdeleke O. Bankole                                          reaction_force[w * dim + 0], reaction_force[w * dim + 1], reaction_force[w * dim + 2]));
458ca69d878SAdeleke O. Bankole 
459ca69d878SAdeleke O. Bankole       } else {
460ca69d878SAdeleke O. Bankole         PetscCall(PetscViewerASCIIPrintf(viewer, "Wall %" PetscInt_FMT " Forces: Force_x = %12g, Force_y = %12g, Force_z = %12g\n", wall,
461ca69d878SAdeleke O. Bankole                                          reaction_force[w * dim + 0], reaction_force[w * dim + 1], reaction_force[w * dim + 2]));
462ca69d878SAdeleke O. Bankole       }
463ca69d878SAdeleke O. Bankole     }
464ca69d878SAdeleke O. Bankole   }
465ca69d878SAdeleke O. Bankole   PetscCall(PetscFree(reaction_force));
466ca69d878SAdeleke O. Bankole   PetscFunctionReturn(0);
467ca69d878SAdeleke O. Bankole }
468ca69d878SAdeleke O. Bankole 
469f14660a4SJames Wright // User provided TS Monitor
4702b730f8bSJeremy L Thompson PetscErrorCode TSMonitor_NS(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) {
471f14660a4SJames Wright   User user = ctx;
472f14660a4SJames Wright   PetscFunctionBeginUser;
473f14660a4SJames Wright 
47437cbb16aSJed Brown   // Print every 'checkpoint_interval' steps
475894de27cSJames Wright   if (user->app_ctx->checkpoint_interval <= 0 || step_no % user->app_ctx->checkpoint_interval != 0 ||
476894de27cSJames Wright       (user->app_ctx->cont_steps == step_no && step_no != 0))
477894de27cSJames Wright     PetscFunctionReturn(0);
478f14660a4SJames Wright 
479f14660a4SJames Wright   PetscCall(WriteOutput(user, Q, step_no, time));
48077841947SLeila Ghaffari 
48177841947SLeila Ghaffari   PetscFunctionReturn(0);
48277841947SLeila Ghaffari }
48377841947SLeila Ghaffari 
48477841947SLeila Ghaffari // TS: Create, setup, and solve
4852b730f8bSJeremy L Thompson PetscErrorCode TSSolve_NS(DM dm, User user, AppCtx app_ctx, Physics phys, Vec *Q, PetscScalar *f_time, TS *ts) {
48677841947SLeila Ghaffari   MPI_Comm    comm = user->comm;
48777841947SLeila Ghaffari   TSAdapt     adapt;
48877841947SLeila Ghaffari   PetscScalar final_time;
48977841947SLeila Ghaffari   PetscFunctionBeginUser;
49077841947SLeila Ghaffari 
4912b730f8bSJeremy L Thompson   PetscCall(TSCreate(comm, ts));
4922b730f8bSJeremy L Thompson   PetscCall(TSSetDM(*ts, dm));
49377841947SLeila Ghaffari   if (phys->implicit) {
4942b730f8bSJeremy L Thompson     PetscCall(TSSetType(*ts, TSBDF));
49577841947SLeila Ghaffari     if (user->op_ifunction) {
4962b730f8bSJeremy L Thompson       PetscCall(TSSetIFunction(*ts, NULL, IFunction_NS, &user));
49777841947SLeila Ghaffari     } else {  // Implicit integrators can fall back to using an RHSFunction
4982b730f8bSJeremy L Thompson       PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user));
49977841947SLeila Ghaffari     }
500e334ad8fSJed Brown     if (user->op_ijacobian) {
5012b730f8bSJeremy L Thompson       PetscCall(DMTSSetIJacobian(dm, FormIJacobian_NS, &user));
502544be873SJed Brown       if (app_ctx->amat_type) {
503544be873SJed Brown         Mat Pmat, Amat;
5042b730f8bSJeremy L Thompson         PetscCall(DMCreateMatrix(dm, &Pmat));
5052b730f8bSJeremy L Thompson         PetscCall(DMSetMatType(dm, app_ctx->amat_type));
5062b730f8bSJeremy L Thompson         PetscCall(DMCreateMatrix(dm, &Amat));
5072b730f8bSJeremy L Thompson         PetscCall(TSSetIJacobian(*ts, Amat, Pmat, NULL, NULL));
5082b730f8bSJeremy L Thompson         PetscCall(MatDestroy(&Amat));
5092b730f8bSJeremy L Thompson         PetscCall(MatDestroy(&Pmat));
510544be873SJed Brown       }
511e334ad8fSJed Brown     }
51277841947SLeila Ghaffari   } else {
5132b730f8bSJeremy L Thompson     if (!user->op_rhs) SETERRQ(comm, PETSC_ERR_ARG_NULL, "Problem does not provide RHSFunction");
5142b730f8bSJeremy L Thompson     PetscCall(TSSetType(*ts, TSRK));
5152b730f8bSJeremy L Thompson     PetscCall(TSRKSetType(*ts, TSRK5F));
5162b730f8bSJeremy L Thompson     PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user));
51777841947SLeila Ghaffari   }
5182b730f8bSJeremy L Thompson   PetscCall(TSSetMaxTime(*ts, 500. * user->units->second));
5192b730f8bSJeremy L Thompson   PetscCall(TSSetExactFinalTime(*ts, TS_EXACTFINALTIME_STEPOVER));
520cf7a0454SJed Brown   PetscCall(TSSetErrorIfStepFails(*ts, PETSC_FALSE));
5212b730f8bSJeremy L Thompson   PetscCall(TSSetTimeStep(*ts, 1.e-2 * user->units->second));
5228fb33541SJames Wright   if (app_ctx->test_type != TESTTYPE_NONE) {
5232b730f8bSJeremy L Thompson     PetscCall(TSSetMaxSteps(*ts, 10));
5242b730f8bSJeremy L Thompson   }
5252b730f8bSJeremy L Thompson   PetscCall(TSGetAdapt(*ts, &adapt));
5262b730f8bSJeremy L Thompson   PetscCall(TSAdaptSetStepLimits(adapt, 1.e-12 * user->units->second, 1.e2 * user->units->second));
5272b730f8bSJeremy L Thompson   PetscCall(TSSetFromOptions(*ts));
528a0b9a424SJames Wright   user->time = user->time_bc_set = -1.0;  // require all BCs and ctx to be updated
5295e82a6e1SJeremy L Thompson   user->dt                       = -1.0;
53077841947SLeila Ghaffari   if (!app_ctx->cont_steps) {  // print initial condition
5318fb33541SJames Wright     if (app_ctx->test_type == TESTTYPE_NONE) {
5322b730f8bSJeremy L Thompson       PetscCall(TSMonitor_NS(*ts, 0, 0., *Q, user));
53377841947SLeila Ghaffari     }
53477841947SLeila Ghaffari   } else {  // continue from time of last output
53577841947SLeila Ghaffari     PetscInt    count;
53677841947SLeila Ghaffari     PetscViewer viewer;
5372b730f8bSJeremy L Thompson 
5384de8550aSJed Brown     if (app_ctx->cont_time <= 0) {  // Legacy files did not include step number and time
5392b730f8bSJeremy L Thompson       PetscCall(PetscViewerBinaryOpen(comm, app_ctx->cont_time_file, FILE_MODE_READ, &viewer));
5404de8550aSJed Brown       PetscCall(PetscViewerBinaryRead(viewer, &app_ctx->cont_time, 1, &count, PETSC_REAL));
5412b730f8bSJeremy L Thompson       PetscCall(PetscViewerDestroy(&viewer));
5424de8550aSJed Brown       PetscCheck(app_ctx->cont_steps != -1, comm, PETSC_ERR_ARG_INCOMP,
5434de8550aSJed Brown                  "-continue step number not specified, but checkpoint file does not contain a step number (likely written by older code version)");
5444de8550aSJed Brown     }
5454de8550aSJed Brown     PetscCall(TSSetTime(*ts, app_ctx->cont_time * user->units->second));
546d8e0aecdSJed Brown     PetscCall(TSSetStepNumber(*ts, app_ctx->cont_steps));
54777841947SLeila Ghaffari   }
5488fb33541SJames Wright   if (app_ctx->test_type == TESTTYPE_NONE) {
5492b730f8bSJeremy L Thompson     PetscCall(TSMonitorSet(*ts, TSMonitor_NS, user, NULL));
5508fb33541SJames Wright   }
551ca69d878SAdeleke O. Bankole   if (app_ctx->wall_forces.viewer) {
552ca69d878SAdeleke O. Bankole     PetscCall(TSMonitorSet(*ts, TSMonitor_WallForce, user, NULL));
553ca69d878SAdeleke O. Bankole   }
554b7d66439SJames Wright   if (app_ctx->turb_spanstats_enable) {
555a175e481SJames Wright     PetscCall(TSMonitorSet(*ts, TSMonitor_Statistics, user, NULL));
556495b9769SJames Wright     CeedScalar previous_time = app_ctx->cont_time * user->units->second;
557495b9769SJames Wright     CeedOperatorContextSetDouble(user->spanstats.op_stats_collect, user->spanstats.previous_time_label, &previous_time);
558a175e481SJames Wright   }
55977841947SLeila Ghaffari 
56077841947SLeila Ghaffari   // Solve
561d8e0aecdSJed Brown   PetscReal start_time;
562d8e0aecdSJed Brown   PetscInt  start_step;
5632b730f8bSJeremy L Thompson   PetscCall(TSGetTime(*ts, &start_time));
564d8e0aecdSJed Brown   PetscCall(TSGetStepNumber(*ts, &start_step));
5657b39487dSJeremy L Thompson 
5667b39487dSJeremy L Thompson   PetscPreLoadBegin(PETSC_FALSE, "Fluids Solve");
5677b39487dSJeremy L Thompson   PetscCall(TSSetTime(*ts, start_time));
568d8e0aecdSJed Brown   PetscCall(TSSetStepNumber(*ts, start_step));
5697b39487dSJeremy L Thompson   if (PetscPreLoadingOn) {
5707b39487dSJeremy L Thompson     // LCOV_EXCL_START
5717b39487dSJeremy L Thompson     SNES      snes;
5727b39487dSJeremy L Thompson     Vec       Q_preload;
5737b39487dSJeremy L Thompson     PetscReal rtol;
5747b39487dSJeremy L Thompson     PetscCall(VecDuplicate(*Q, &Q_preload));
5757b39487dSJeremy L Thompson     PetscCall(VecCopy(*Q, Q_preload));
5767b39487dSJeremy L Thompson     PetscCall(TSGetSNES(*ts, &snes));
5777b39487dSJeremy L Thompson     PetscCall(SNESGetTolerances(snes, NULL, &rtol, NULL, NULL, NULL));
5782b730f8bSJeremy L Thompson     PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, .99, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT));
5797b39487dSJeremy L Thompson     PetscCall(TSSetSolution(*ts, *Q));
5807b39487dSJeremy L Thompson     PetscCall(TSStep(*ts));
5812b730f8bSJeremy L Thompson     PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, rtol, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT));
5827b39487dSJeremy L Thompson     PetscCall(VecDestroy(&Q_preload));
5837b39487dSJeremy L Thompson     // LCOV_EXCL_STOP
5847b39487dSJeremy L Thompson   } else {
5852b730f8bSJeremy L Thompson     PetscCall(PetscBarrier((PetscObject)*ts));
5862b730f8bSJeremy L Thompson     PetscCall(TSSolve(*ts, *Q));
5877b39487dSJeremy L Thompson   }
5887b39487dSJeremy L Thompson   PetscPreLoadEnd();
5897b39487dSJeremy L Thompson 
5907b39487dSJeremy L Thompson   PetscCall(TSGetSolveTime(*ts, &final_time));
59177841947SLeila Ghaffari   *f_time = final_time;
5927b39487dSJeremy L Thompson 
5938fb33541SJames Wright   if (app_ctx->test_type == TESTTYPE_NONE) {
594f14660a4SJames Wright     PetscInt step_no;
595f14660a4SJames Wright     PetscCall(TSGetStepNumber(*ts, &step_no));
596a175e481SJames Wright     if (user->app_ctx->checkpoint_interval > 0 || user->app_ctx->checkpoint_interval == -1) {
597f14660a4SJames Wright       PetscCall(WriteOutput(user, *Q, step_no, final_time));
598f14660a4SJames Wright     }
599f14660a4SJames Wright 
6007b39487dSJeremy L Thompson     PetscLogEvent stage_id;
6017b39487dSJeremy L Thompson     PetscStageLog stage_log;
6027b39487dSJeremy L Thompson 
6037b39487dSJeremy L Thompson     PetscCall(PetscLogStageGetId("Fluids Solve", &stage_id));
6047b39487dSJeremy L Thompson     PetscCall(PetscLogGetStageLog(&stage_log));
6052b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Time taken for solution (sec): %g\n", stage_log->stageInfo[stage_id].perfInfo.time));
60677841947SLeila Ghaffari   }
60777841947SLeila Ghaffari   PetscFunctionReturn(0);
60877841947SLeila Ghaffari }
609