xref: /libCEED/examples/fluids/src/setupts.c (revision 4de8550ab4506b5c5bd81d0c3febcb71b9614dca) !
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"
1277841947SLeila Ghaffari #include "../qfunctions/mass.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
3177841947SLeila Ghaffari   CeedQFunctionCreateInterior(ceed, 1, Mass, Mass_loc, &qf_mass);
3277841947SLeila Ghaffari   CeedQFunctionAddInput(qf_mass, "q", num_comp_q, CEED_EVAL_INTERP);
33a61c78d6SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "qdata", q_data_size, CEED_EVAL_NONE);
3477841947SLeila Ghaffari   CeedQFunctionAddOutput(qf_mass, "v", num_comp_q, CEED_EVAL_INTERP);
3577841947SLeila Ghaffari 
3677841947SLeila Ghaffari   // CEED Operator
3777841947SLeila Ghaffari   CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass);
382b730f8bSJeremy L Thompson   CeedOperatorSetField(op_mass, "q", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE);
392b730f8bSJeremy L Thompson   CeedOperatorSetField(op_mass, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data->q_data);
402b730f8bSJeremy L Thompson   CeedOperatorSetField(op_mass, "v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE);
4177841947SLeila Ghaffari 
4277841947SLeila Ghaffari   // Place PETSc vector in CEED vector
4377841947SLeila Ghaffari   CeedScalar  *m;
4477841947SLeila Ghaffari   PetscMemType m_mem_type;
452b730f8bSJeremy L Thompson   PetscCall(DMGetLocalVector(dm, &M_loc));
462b730f8bSJeremy L Thompson   PetscCall(VecGetArrayAndMemType(M_loc, (PetscScalar **)&m, &m_mem_type));
4777841947SLeila Ghaffari   CeedVectorSetArray(m_ceed, MemTypeP2C(m_mem_type), CEED_USE_POINTER, m);
4877841947SLeila Ghaffari 
4977841947SLeila Ghaffari   // Apply CEED Operator
5077841947SLeila Ghaffari   CeedOperatorApply(op_mass, ones_vec, m_ceed, CEED_REQUEST_IMMEDIATE);
5177841947SLeila Ghaffari 
5277841947SLeila Ghaffari   // Restore vectors
5377841947SLeila Ghaffari   CeedVectorTakeArray(m_ceed, MemTypeP2C(m_mem_type), NULL);
542b730f8bSJeremy L Thompson   PetscCall(VecRestoreArrayReadAndMemType(M_loc, (const PetscScalar **)&m));
5577841947SLeila Ghaffari 
5677841947SLeila Ghaffari   // Local-to-Global
572b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(M));
582b730f8bSJeremy L Thompson   PetscCall(DMLocalToGlobal(dm, M_loc, ADD_VALUES, M));
592b730f8bSJeremy L Thompson   PetscCall(DMRestoreLocalVector(dm, &M_loc));
6077841947SLeila Ghaffari 
6177841947SLeila Ghaffari   // Invert diagonally lumped mass vector for RHS function
622b730f8bSJeremy L Thompson   PetscCall(VecReciprocal(M));
6377841947SLeila Ghaffari 
6477841947SLeila Ghaffari   // Cleanup
6577841947SLeila Ghaffari   CeedVectorDestroy(&ones_vec);
6677841947SLeila Ghaffari   CeedVectorDestroy(&m_ceed);
6777841947SLeila Ghaffari   CeedQFunctionDestroy(&qf_mass);
6877841947SLeila Ghaffari   CeedOperatorDestroy(&op_mass);
6977841947SLeila Ghaffari 
7077841947SLeila Ghaffari   PetscFunctionReturn(0);
7177841947SLeila Ghaffari }
7277841947SLeila Ghaffari 
7377841947SLeila Ghaffari // RHS (Explicit time-stepper) function setup
7477841947SLeila Ghaffari //   This is the RHS of the ODE, given as u_t = G(t,u)
7577841947SLeila Ghaffari //   This function takes in a state vector Q and writes into G
7677841947SLeila Ghaffari PetscErrorCode RHS_NS(TS ts, PetscReal t, Vec Q, Vec G, void *user_data) {
7777841947SLeila Ghaffari   User         user = *(User *)user_data;
7877841947SLeila Ghaffari   PetscScalar *q, *g;
795e82a6e1SJeremy L Thompson   Vec          Q_loc = user->Q_loc, G_loc;
8077841947SLeila Ghaffari   PetscMemType q_mem_type, g_mem_type;
8177841947SLeila Ghaffari   PetscFunctionBeginUser;
8277841947SLeila Ghaffari 
835e82a6e1SJeremy L Thompson   // Get local vector
842b730f8bSJeremy L Thompson   PetscCall(DMGetLocalVector(user->dm, &G_loc));
855e82a6e1SJeremy L Thompson 
865e82a6e1SJeremy L Thompson   // Update time dependent data
875e82a6e1SJeremy L Thompson   if (user->time != t) {
882b730f8bSJeremy L Thompson     PetscCall(DMPlexInsertBoundaryValues(user->dm, PETSC_TRUE, Q_loc, t, NULL, NULL, NULL));
895e82a6e1SJeremy L Thompson     if (user->phys->solution_time_label) {
9011436a05SJames Wright       CeedOperatorContextSetDouble(user->op_rhs, user->phys->solution_time_label, &t);
915e82a6e1SJeremy L Thompson     }
925e82a6e1SJeremy L Thompson     user->time = t;
935e82a6e1SJeremy L Thompson   }
9488626eedSJames Wright   if (user->phys->timestep_size_label) {
9588626eedSJames Wright     PetscScalar dt;
962b730f8bSJeremy L Thompson     PetscCall(TSGetTimeStep(ts, &dt));
975e82a6e1SJeremy L Thompson     if (user->dt != dt) {
982b730f8bSJeremy L Thompson       CeedOperatorContextSetDouble(user->op_rhs, user->phys->timestep_size_label, &dt);
995e82a6e1SJeremy L Thompson       user->dt = dt;
1005e82a6e1SJeremy L Thompson     }
10188626eedSJames Wright   }
10277841947SLeila Ghaffari 
10377841947SLeila Ghaffari   // Global-to-local
1042b730f8bSJeremy L Thompson   PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc));
10577841947SLeila Ghaffari 
10677841947SLeila Ghaffari   // Place PETSc vectors in CEED vectors
1072b730f8bSJeremy L Thompson   PetscCall(VecGetArrayReadAndMemType(Q_loc, (const PetscScalar **)&q, &q_mem_type));
1082b730f8bSJeremy L Thompson   PetscCall(VecGetArrayAndMemType(G_loc, &g, &g_mem_type));
10977841947SLeila Ghaffari   CeedVectorSetArray(user->q_ceed, MemTypeP2C(q_mem_type), CEED_USE_POINTER, q);
11077841947SLeila Ghaffari   CeedVectorSetArray(user->g_ceed, MemTypeP2C(g_mem_type), CEED_USE_POINTER, g);
11177841947SLeila Ghaffari 
11277841947SLeila Ghaffari   // Apply CEED operator
1132b730f8bSJeremy L Thompson   CeedOperatorApply(user->op_rhs, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE);
11477841947SLeila Ghaffari 
11577841947SLeila Ghaffari   // Restore vectors
11677841947SLeila Ghaffari   CeedVectorTakeArray(user->q_ceed, MemTypeP2C(q_mem_type), NULL);
11777841947SLeila Ghaffari   CeedVectorTakeArray(user->g_ceed, MemTypeP2C(g_mem_type), NULL);
1182b730f8bSJeremy L Thompson   PetscCall(VecRestoreArrayReadAndMemType(Q_loc, (const PetscScalar **)&q));
1192b730f8bSJeremy L Thompson   PetscCall(VecRestoreArrayAndMemType(G_loc, &g));
12077841947SLeila Ghaffari 
12177841947SLeila Ghaffari   // Local-to-Global
1222b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(G));
1232b730f8bSJeremy L Thompson   PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G));
12477841947SLeila Ghaffari 
12577841947SLeila Ghaffari   // Inverse of the lumped mass matrix (M is Minv)
1262b730f8bSJeremy L Thompson   PetscCall(VecPointwiseMult(G, G, user->M));
12777841947SLeila Ghaffari 
12877841947SLeila Ghaffari   // Restore vectors
1292b730f8bSJeremy L Thompson   PetscCall(DMRestoreLocalVector(user->dm, &G_loc));
13077841947SLeila Ghaffari 
13177841947SLeila Ghaffari   PetscFunctionReturn(0);
13277841947SLeila Ghaffari }
13377841947SLeila Ghaffari 
13477841947SLeila Ghaffari // Implicit time-stepper function setup
1352b730f8bSJeremy L Thompson PetscErrorCode IFunction_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, Vec G, void *user_data) {
13677841947SLeila Ghaffari   User               user = *(User *)user_data;
13777841947SLeila Ghaffari   const PetscScalar *q, *q_dot;
13877841947SLeila Ghaffari   PetscScalar       *g;
1395e82a6e1SJeremy L Thompson   Vec                Q_loc = user->Q_loc, Q_dot_loc = user->Q_dot_loc, G_loc;
14077841947SLeila Ghaffari   PetscMemType       q_mem_type, q_dot_mem_type, g_mem_type;
14177841947SLeila Ghaffari   PetscFunctionBeginUser;
14277841947SLeila Ghaffari 
1435e82a6e1SJeremy L Thompson   // Get local vectors
1442b730f8bSJeremy L Thompson   PetscCall(DMGetLocalVector(user->dm, &G_loc));
1455e82a6e1SJeremy L Thompson 
1465e82a6e1SJeremy L Thompson   // Update time dependent data
1475e82a6e1SJeremy L Thompson   if (user->time != t) {
1482b730f8bSJeremy L Thompson     PetscCall(DMPlexInsertBoundaryValues(user->dm, PETSC_TRUE, Q_loc, t, NULL, NULL, NULL));
1495e82a6e1SJeremy L Thompson     if (user->phys->solution_time_label) {
1502b730f8bSJeremy L Thompson       CeedOperatorContextSetDouble(user->op_ifunction, user->phys->solution_time_label, &t);
1515e82a6e1SJeremy L Thompson     }
1525e82a6e1SJeremy L Thompson     user->time = t;
1535e82a6e1SJeremy L Thompson   }
15488626eedSJames Wright   if (user->phys->timestep_size_label) {
15588626eedSJames Wright     PetscScalar dt;
1562b730f8bSJeremy L Thompson     PetscCall(TSGetTimeStep(ts, &dt));
1575e82a6e1SJeremy L Thompson     if (user->dt != dt) {
1582b730f8bSJeremy L Thompson       CeedOperatorContextSetDouble(user->op_ifunction, user->phys->timestep_size_label, &dt);
1595e82a6e1SJeremy L Thompson       user->dt = dt;
1605e82a6e1SJeremy L Thompson     }
16188626eedSJames Wright   }
16277841947SLeila Ghaffari 
16377841947SLeila Ghaffari   // Global-to-local
1642b730f8bSJeremy L Thompson   PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc));
1652b730f8bSJeremy L Thompson   PetscCall(DMGlobalToLocal(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc));
16677841947SLeila Ghaffari 
16777841947SLeila Ghaffari   // Place PETSc vectors in CEED vectors
1682b730f8bSJeremy L Thompson   PetscCall(VecGetArrayReadAndMemType(Q_loc, &q, &q_mem_type));
1692b730f8bSJeremy L Thompson   PetscCall(VecGetArrayReadAndMemType(Q_dot_loc, &q_dot, &q_dot_mem_type));
1702b730f8bSJeremy L Thompson   PetscCall(VecGetArrayAndMemType(G_loc, &g, &g_mem_type));
1712b730f8bSJeremy L Thompson   CeedVectorSetArray(user->q_ceed, MemTypeP2C(q_mem_type), CEED_USE_POINTER, (PetscScalar *)q);
1722b730f8bSJeremy L Thompson   CeedVectorSetArray(user->q_dot_ceed, MemTypeP2C(q_dot_mem_type), CEED_USE_POINTER, (PetscScalar *)q_dot);
17377841947SLeila Ghaffari   CeedVectorSetArray(user->g_ceed, MemTypeP2C(g_mem_type), CEED_USE_POINTER, g);
17477841947SLeila Ghaffari 
17577841947SLeila Ghaffari   // Apply CEED operator
1762b730f8bSJeremy L Thompson   CeedOperatorApply(user->op_ifunction, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE);
17777841947SLeila Ghaffari 
17877841947SLeila Ghaffari   // Restore vectors
17977841947SLeila Ghaffari   CeedVectorTakeArray(user->q_ceed, MemTypeP2C(q_mem_type), NULL);
18077841947SLeila Ghaffari   CeedVectorTakeArray(user->q_dot_ceed, MemTypeP2C(q_dot_mem_type), NULL);
18177841947SLeila Ghaffari   CeedVectorTakeArray(user->g_ceed, MemTypeP2C(g_mem_type), NULL);
1822b730f8bSJeremy L Thompson   PetscCall(VecRestoreArrayReadAndMemType(Q_loc, &q));
1832b730f8bSJeremy L Thompson   PetscCall(VecRestoreArrayReadAndMemType(Q_dot_loc, &q_dot));
1842b730f8bSJeremy L Thompson   PetscCall(VecRestoreArrayAndMemType(G_loc, &g));
18577841947SLeila Ghaffari 
18677841947SLeila Ghaffari   // Local-to-Global
1872b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(G));
1882b730f8bSJeremy L Thompson   PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G));
18977841947SLeila Ghaffari 
19077841947SLeila Ghaffari   // Restore vectors
1912b730f8bSJeremy L Thompson   PetscCall(DMRestoreLocalVector(user->dm, &G_loc));
19277841947SLeila Ghaffari 
19377841947SLeila Ghaffari   PetscFunctionReturn(0);
19477841947SLeila Ghaffari }
19577841947SLeila Ghaffari 
196e334ad8fSJed Brown static PetscErrorCode MatMult_NS_IJacobian(Mat J, Vec Q, Vec G) {
197e334ad8fSJed Brown   User               user;
198e334ad8fSJed Brown   const PetscScalar *q;
199e334ad8fSJed Brown   PetscScalar       *g;
200e334ad8fSJed Brown   PetscMemType       q_mem_type, g_mem_type;
201e334ad8fSJed Brown   PetscFunctionBeginUser;
2022b730f8bSJeremy L Thompson   PetscCall(MatShellGetContext(J, &user));
2035e82a6e1SJeremy L Thompson   Vec Q_loc = user->Q_dot_loc,  // Note - Q_dot_loc has zero BCs
2045e82a6e1SJeremy L Thompson       G_loc;
2055e82a6e1SJeremy L Thompson 
206e334ad8fSJed Brown   // Get local vectors
2072b730f8bSJeremy L Thompson   PetscCall(DMGetLocalVector(user->dm, &G_loc));
208e334ad8fSJed Brown 
209e334ad8fSJed Brown   // Global-to-local
2102b730f8bSJeremy L Thompson   PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc));
211e334ad8fSJed Brown 
212e334ad8fSJed Brown   // Place PETSc vectors in CEED vectors
2132b730f8bSJeremy L Thompson   PetscCall(VecGetArrayReadAndMemType(Q_loc, &q, &q_mem_type));
2142b730f8bSJeremy L Thompson   PetscCall(VecGetArrayAndMemType(G_loc, &g, &g_mem_type));
2152b730f8bSJeremy L Thompson   CeedVectorSetArray(user->q_ceed, MemTypeP2C(q_mem_type), CEED_USE_POINTER, (PetscScalar *)q);
216e334ad8fSJed Brown   CeedVectorSetArray(user->g_ceed, MemTypeP2C(g_mem_type), CEED_USE_POINTER, g);
217e334ad8fSJed Brown 
218e334ad8fSJed Brown   // Apply CEED operator
2192b730f8bSJeremy L Thompson   CeedOperatorApply(user->op_ijacobian, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE);
220e334ad8fSJed Brown 
221e334ad8fSJed Brown   // Restore vectors
222e334ad8fSJed Brown   CeedVectorTakeArray(user->q_ceed, MemTypeP2C(q_mem_type), NULL);
223e334ad8fSJed Brown   CeedVectorTakeArray(user->g_ceed, MemTypeP2C(g_mem_type), NULL);
2242b730f8bSJeremy L Thompson   PetscCall(VecRestoreArrayReadAndMemType(Q_loc, &q));
2252b730f8bSJeremy L Thompson   PetscCall(VecRestoreArrayAndMemType(G_loc, &g));
226e334ad8fSJed Brown 
227e334ad8fSJed Brown   // Local-to-Global
2282b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(G));
2292b730f8bSJeremy L Thompson   PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G));
230e334ad8fSJed Brown 
231e334ad8fSJed Brown   // Restore vectors
2322b730f8bSJeremy L Thompson   PetscCall(DMRestoreLocalVector(user->dm, &G_loc));
233e334ad8fSJed Brown   PetscFunctionReturn(0);
234e334ad8fSJed Brown }
235e334ad8fSJed Brown 
236e334ad8fSJed Brown PetscErrorCode MatGetDiagonal_NS_IJacobian(Mat A, Vec D) {
237e334ad8fSJed Brown   User         user;
238e334ad8fSJed Brown   Vec          D_loc;
239e334ad8fSJed Brown   PetscScalar *d;
240e334ad8fSJed Brown   PetscMemType mem_type;
241e334ad8fSJed Brown 
242e334ad8fSJed Brown   PetscFunctionBeginUser;
243e334ad8fSJed Brown   MatShellGetContext(A, &user);
244e334ad8fSJed Brown   PetscCall(DMGetLocalVector(user->dm, &D_loc));
245e334ad8fSJed Brown   PetscCall(VecGetArrayAndMemType(D_loc, &d, &mem_type));
246e334ad8fSJed Brown   CeedVectorSetArray(user->g_ceed, MemTypeP2C(mem_type), CEED_USE_POINTER, d);
2472b730f8bSJeremy L Thompson   CeedOperatorLinearAssembleDiagonal(user->op_ijacobian, user->g_ceed, CEED_REQUEST_IMMEDIATE);
248e334ad8fSJed Brown   CeedVectorTakeArray(user->g_ceed, MemTypeP2C(mem_type), NULL);
249e334ad8fSJed Brown   PetscCall(VecRestoreArrayAndMemType(D_loc, &d));
250e334ad8fSJed Brown   PetscCall(VecZeroEntries(D));
251e334ad8fSJed Brown   PetscCall(DMLocalToGlobal(user->dm, D_loc, ADD_VALUES, D));
252e334ad8fSJed Brown   PetscCall(DMRestoreLocalVector(user->dm, &D_loc));
253e334ad8fSJed Brown   VecViewFromOptions(D, NULL, "-diag_vec_view");
254e334ad8fSJed Brown   PetscFunctionReturn(0);
255e334ad8fSJed Brown }
256e334ad8fSJed Brown 
2572b730f8bSJeremy L Thompson static PetscErrorCode FormPreallocation(User user, PetscBool pbdiagonal, Mat J, CeedVector *coo_values) {
258544be873SJed Brown   PetscCount ncoo;
259544be873SJed Brown   PetscInt  *rows, *cols;
260544be873SJed Brown 
261544be873SJed Brown   PetscFunctionBeginUser;
262544be873SJed Brown   if (pbdiagonal) {
263544be873SJed Brown     CeedSize l_size;
264544be873SJed Brown     CeedOperatorGetActiveVectorLengths(user->op_ijacobian, &l_size, NULL);
265544be873SJed Brown     ncoo = l_size * 5;
266544be873SJed Brown     rows = malloc(ncoo * sizeof(rows[0]));
267544be873SJed Brown     cols = malloc(ncoo * sizeof(cols[0]));
268544be873SJed Brown     for (PetscCount n = 0; n < l_size / 5; n++) {
269544be873SJed Brown       for (PetscInt i = 0; i < 5; i++) {
270544be873SJed Brown         for (PetscInt j = 0; j < 5; j++) {
271544be873SJed Brown           rows[(n * 5 + i) * 5 + j] = n * 5 + i;
272544be873SJed Brown           cols[(n * 5 + i) * 5 + j] = n * 5 + j;
273544be873SJed Brown         }
274544be873SJed Brown       }
275544be873SJed Brown     }
276544be873SJed Brown   } else {
2772b730f8bSJeremy L Thompson     PetscCall(CeedOperatorLinearAssembleSymbolic(user->op_ijacobian, &ncoo, &rows, &cols));
278544be873SJed Brown   }
279544be873SJed Brown   PetscCall(MatSetPreallocationCOOLocal(J, ncoo, rows, cols));
280544be873SJed Brown   free(rows);
281544be873SJed Brown   free(cols);
282544be873SJed Brown   CeedVectorCreate(user->ceed, ncoo, coo_values);
283544be873SJed Brown   PetscFunctionReturn(0);
284544be873SJed Brown }
285544be873SJed Brown 
2862b730f8bSJeremy L Thompson static PetscErrorCode FormSetValues(User user, PetscBool pbdiagonal, Mat J, CeedVector coo_values) {
287544be873SJed Brown   CeedMemType        mem_type = CEED_MEM_HOST;
288544be873SJed Brown   const PetscScalar *values;
289544be873SJed Brown   MatType            mat_type;
290544be873SJed Brown 
291544be873SJed Brown   PetscFunctionBeginUser;
292544be873SJed Brown   PetscCall(MatGetType(J, &mat_type));
2932b730f8bSJeremy L Thompson   if (strstr(mat_type, "kokkos") || strstr(mat_type, "cusparse")) mem_type = CEED_MEM_DEVICE;
294544be873SJed Brown   if (user->app_ctx->pmat_pbdiagonal) {
2952b730f8bSJeremy L Thompson     CeedOperatorLinearAssemblePointBlockDiagonal(user->op_ijacobian, coo_values, CEED_REQUEST_IMMEDIATE);
296544be873SJed Brown   } else {
297544be873SJed Brown     CeedOperatorLinearAssemble(user->op_ijacobian, coo_values);
298544be873SJed Brown   }
299544be873SJed Brown   CeedVectorGetArrayRead(coo_values, mem_type, &values);
300544be873SJed Brown   PetscCall(MatSetValuesCOO(J, values, INSERT_VALUES));
301544be873SJed Brown   CeedVectorRestoreArrayRead(coo_values, &values);
302544be873SJed Brown   PetscFunctionReturn(0);
303544be873SJed Brown }
304544be873SJed Brown 
3052b730f8bSJeremy L Thompson PetscErrorCode FormIJacobian_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, PetscReal shift, Mat J, Mat J_pre, void *user_data) {
306e334ad8fSJed Brown   User      user = *(User *)user_data;
307d6bf345cSJed Brown   PetscBool J_is_shell, J_is_mffd, J_pre_is_shell;
308e334ad8fSJed Brown   PetscFunctionBeginUser;
3092b730f8bSJeremy L Thompson   if (user->phys->ijacobian_time_shift_label) CeedOperatorContextSetDouble(user->op_ijacobian, user->phys->ijacobian_time_shift_label, &shift);
310d6bf345cSJed Brown   PetscCall(PetscObjectTypeCompare((PetscObject)J, MATMFFD, &J_is_mffd));
311e334ad8fSJed Brown   PetscCall(PetscObjectTypeCompare((PetscObject)J, MATSHELL, &J_is_shell));
3122b730f8bSJeremy L Thompson   PetscCall(PetscObjectTypeCompare((PetscObject)J_pre, MATSHELL, &J_pre_is_shell));
313e334ad8fSJed Brown   if (!user->matrices_set_up) {
314e334ad8fSJed Brown     if (J_is_shell) {
315e334ad8fSJed Brown       PetscCall(MatShellSetContext(J, user));
3162b730f8bSJeremy L Thompson       PetscCall(MatShellSetOperation(J, MATOP_MULT, (void (*)(void))MatMult_NS_IJacobian));
3172b730f8bSJeremy L Thompson       PetscCall(MatShellSetOperation(J, MATOP_GET_DIAGONAL, (void (*)(void))MatGetDiagonal_NS_IJacobian));
318e334ad8fSJed Brown       PetscCall(MatSetUp(J));
319e334ad8fSJed Brown     }
320e334ad8fSJed Brown     if (!J_pre_is_shell) {
3212b730f8bSJeremy L Thompson       PetscCall(FormPreallocation(user, user->app_ctx->pmat_pbdiagonal, J_pre, &user->coo_values_pmat));
322544be873SJed Brown     }
323d6bf345cSJed Brown     if (J != J_pre && !J_is_shell && !J_is_mffd) {
324544be873SJed Brown       PetscCall(FormPreallocation(user, PETSC_FALSE, J, &user->coo_values_amat));
325544be873SJed Brown     }
326e334ad8fSJed Brown     user->matrices_set_up = true;
327e334ad8fSJed Brown   }
328e334ad8fSJed Brown   if (!J_pre_is_shell) {
3292b730f8bSJeremy L Thompson     PetscCall(FormSetValues(user, user->app_ctx->pmat_pbdiagonal, J_pre, user->coo_values_pmat));
330e334ad8fSJed Brown   }
331d6bf345cSJed Brown   if (user->coo_values_amat) {
332d6bf345cSJed Brown     PetscCall(FormSetValues(user, PETSC_FALSE, J, user->coo_values_amat));
333d6bf345cSJed Brown   } else if (J_is_mffd) {
334d6bf345cSJed Brown     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
335d6bf345cSJed Brown     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
336d6bf345cSJed Brown   }
337e334ad8fSJed Brown   PetscFunctionReturn(0);
338e334ad8fSJed Brown }
339e334ad8fSJed Brown 
3402b730f8bSJeremy L Thompson PetscErrorCode WriteOutput(User user, Vec Q, PetscInt step_no, PetscScalar time) {
34177841947SLeila Ghaffari   Vec         Q_loc;
34277841947SLeila Ghaffari   char        file_path[PETSC_MAX_PATH_LEN];
34377841947SLeila Ghaffari   PetscViewer viewer;
34477841947SLeila Ghaffari   PetscFunctionBeginUser;
34577841947SLeila Ghaffari 
34637cbb16aSJed Brown   if (user->app_ctx->checkpoint_vtk) {
34777841947SLeila Ghaffari     // Set up output
348f14660a4SJames Wright     PetscCall(DMGetLocalVector(user->dm, &Q_loc));
349f14660a4SJames Wright     PetscCall(PetscObjectSetName((PetscObject)Q_loc, "StateVec"));
350f14660a4SJames Wright     PetscCall(VecZeroEntries(Q_loc));
351f14660a4SJames Wright     PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc));
35277841947SLeila Ghaffari 
35377841947SLeila Ghaffari     // Output
35437cbb16aSJed Brown     PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no));
355f14660a4SJames Wright 
3562b730f8bSJeremy L Thompson     PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q), file_path, FILE_MODE_WRITE, &viewer));
357f14660a4SJames Wright     PetscCall(VecView(Q_loc, viewer));
358f14660a4SJames Wright     PetscCall(PetscViewerDestroy(&viewer));
35977841947SLeila Ghaffari     if (user->dm_viz) {
36077841947SLeila Ghaffari       Vec         Q_refined, Q_refined_loc;
36177841947SLeila Ghaffari       char        file_path_refined[PETSC_MAX_PATH_LEN];
36277841947SLeila Ghaffari       PetscViewer viewer_refined;
36377841947SLeila Ghaffari 
364f14660a4SJames Wright       PetscCall(DMGetGlobalVector(user->dm_viz, &Q_refined));
365f14660a4SJames Wright       PetscCall(DMGetLocalVector(user->dm_viz, &Q_refined_loc));
366f14660a4SJames Wright       PetscCall(PetscObjectSetName((PetscObject)Q_refined_loc, "Refined"));
367f14660a4SJames Wright 
368f14660a4SJames Wright       PetscCall(MatInterpolate(user->interp_viz, Q, Q_refined));
369f14660a4SJames Wright       PetscCall(VecZeroEntries(Q_refined_loc));
3702b730f8bSJeremy L Thompson       PetscCall(DMGlobalToLocal(user->dm_viz, Q_refined, INSERT_VALUES, Q_refined_loc));
371f14660a4SJames Wright 
37237cbb16aSJed Brown       PetscCall(
37337cbb16aSJed Brown           PetscSNPrintf(file_path_refined, sizeof file_path_refined, "%s/nsrefined-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no));
374f14660a4SJames Wright 
3752b730f8bSJeremy L Thompson       PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q_refined), file_path_refined, FILE_MODE_WRITE, &viewer_refined));
376f14660a4SJames Wright       PetscCall(VecView(Q_refined_loc, viewer_refined));
377f14660a4SJames Wright       PetscCall(DMRestoreLocalVector(user->dm_viz, &Q_refined_loc));
378f14660a4SJames Wright       PetscCall(DMRestoreGlobalVector(user->dm_viz, &Q_refined));
379f14660a4SJames Wright       PetscCall(PetscViewerDestroy(&viewer_refined));
38077841947SLeila Ghaffari     }
381f14660a4SJames Wright     PetscCall(DMRestoreLocalVector(user->dm, &Q_loc));
38237cbb16aSJed Brown   }
38377841947SLeila Ghaffari 
38477841947SLeila Ghaffari   // Save data in a binary file for continuation of simulations
38569293791SJames Wright   if (user->app_ctx->add_stepnum2bin) {
38637cbb16aSJed Brown     PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution-%" PetscInt_FMT ".bin", user->app_ctx->output_dir, step_no));
38769293791SJames Wright   } else {
3882b730f8bSJeremy L Thompson     PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution.bin", user->app_ctx->output_dir));
38969293791SJames Wright   }
3902b730f8bSJeremy L Thompson   PetscCall(PetscViewerBinaryOpen(user->comm, file_path, FILE_MODE_WRITE, &viewer));
391f14660a4SJames Wright 
392*4de8550aSJed Brown   PetscInt token = FLUIDS_FILE_TOKEN;
393*4de8550aSJed Brown   PetscCall(PetscViewerBinaryWrite(viewer, &token, 1, PETSC_INT));
394*4de8550aSJed Brown   PetscCall(PetscViewerBinaryWrite(viewer, &step_no, 1, PETSC_INT));
395*4de8550aSJed Brown   time /= user->units->second;  // Dimensionalize time back
396*4de8550aSJed Brown   PetscCall(PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL));
397f14660a4SJames Wright   PetscCall(VecView(Q, viewer));
398f14660a4SJames Wright   PetscCall(PetscViewerDestroy(&viewer));
399f14660a4SJames Wright   PetscFunctionReturn(0);
400f14660a4SJames Wright }
401f14660a4SJames Wright 
402f14660a4SJames Wright // User provided TS Monitor
4032b730f8bSJeremy L Thompson PetscErrorCode TSMonitor_NS(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) {
404f14660a4SJames Wright   User user = ctx;
405f14660a4SJames Wright   PetscFunctionBeginUser;
406f14660a4SJames Wright 
40737cbb16aSJed Brown   // Print every 'checkpoint_interval' steps
408894de27cSJames Wright   if (user->app_ctx->checkpoint_interval <= 0 || step_no % user->app_ctx->checkpoint_interval != 0 ||
409894de27cSJames Wright       (user->app_ctx->cont_steps == step_no && step_no != 0))
410894de27cSJames Wright     PetscFunctionReturn(0);
411f14660a4SJames Wright 
412f14660a4SJames Wright   PetscCall(WriteOutput(user, Q, step_no, time));
41377841947SLeila Ghaffari 
41477841947SLeila Ghaffari   PetscFunctionReturn(0);
41577841947SLeila Ghaffari }
41677841947SLeila Ghaffari 
41777841947SLeila Ghaffari // TS: Create, setup, and solve
4182b730f8bSJeremy L Thompson PetscErrorCode TSSolve_NS(DM dm, User user, AppCtx app_ctx, Physics phys, Vec *Q, PetscScalar *f_time, TS *ts) {
41977841947SLeila Ghaffari   MPI_Comm    comm = user->comm;
42077841947SLeila Ghaffari   TSAdapt     adapt;
42177841947SLeila Ghaffari   PetscScalar final_time;
42277841947SLeila Ghaffari   PetscFunctionBeginUser;
42377841947SLeila Ghaffari 
4242b730f8bSJeremy L Thompson   PetscCall(TSCreate(comm, ts));
4252b730f8bSJeremy L Thompson   PetscCall(TSSetDM(*ts, dm));
42677841947SLeila Ghaffari   if (phys->implicit) {
4272b730f8bSJeremy L Thompson     PetscCall(TSSetType(*ts, TSBDF));
42877841947SLeila Ghaffari     if (user->op_ifunction) {
4292b730f8bSJeremy L Thompson       PetscCall(TSSetIFunction(*ts, NULL, IFunction_NS, &user));
43077841947SLeila Ghaffari     } else {  // Implicit integrators can fall back to using an RHSFunction
4312b730f8bSJeremy L Thompson       PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user));
43277841947SLeila Ghaffari     }
433e334ad8fSJed Brown     if (user->op_ijacobian) {
4342b730f8bSJeremy L Thompson       PetscCall(DMTSSetIJacobian(dm, FormIJacobian_NS, &user));
435544be873SJed Brown       if (app_ctx->amat_type) {
436544be873SJed Brown         Mat Pmat, Amat;
4372b730f8bSJeremy L Thompson         PetscCall(DMCreateMatrix(dm, &Pmat));
4382b730f8bSJeremy L Thompson         PetscCall(DMSetMatType(dm, app_ctx->amat_type));
4392b730f8bSJeremy L Thompson         PetscCall(DMCreateMatrix(dm, &Amat));
4402b730f8bSJeremy L Thompson         PetscCall(TSSetIJacobian(*ts, Amat, Pmat, NULL, NULL));
4412b730f8bSJeremy L Thompson         PetscCall(MatDestroy(&Amat));
4422b730f8bSJeremy L Thompson         PetscCall(MatDestroy(&Pmat));
443544be873SJed Brown       }
444e334ad8fSJed Brown     }
44577841947SLeila Ghaffari   } else {
4462b730f8bSJeremy L Thompson     if (!user->op_rhs) SETERRQ(comm, PETSC_ERR_ARG_NULL, "Problem does not provide RHSFunction");
4472b730f8bSJeremy L Thompson     PetscCall(TSSetType(*ts, TSRK));
4482b730f8bSJeremy L Thompson     PetscCall(TSRKSetType(*ts, TSRK5F));
4492b730f8bSJeremy L Thompson     PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user));
45077841947SLeila Ghaffari   }
4512b730f8bSJeremy L Thompson   PetscCall(TSSetMaxTime(*ts, 500. * user->units->second));
4522b730f8bSJeremy L Thompson   PetscCall(TSSetExactFinalTime(*ts, TS_EXACTFINALTIME_STEPOVER));
4532b730f8bSJeremy L Thompson   PetscCall(TSSetTimeStep(*ts, 1.e-2 * user->units->second));
4542b730f8bSJeremy L Thompson   if (app_ctx->test_mode) {
4552b730f8bSJeremy L Thompson     PetscCall(TSSetMaxSteps(*ts, 10));
4562b730f8bSJeremy L Thompson   }
4572b730f8bSJeremy L Thompson   PetscCall(TSGetAdapt(*ts, &adapt));
4582b730f8bSJeremy L Thompson   PetscCall(TSAdaptSetStepLimits(adapt, 1.e-12 * user->units->second, 1.e2 * user->units->second));
4592b730f8bSJeremy L Thompson   PetscCall(TSSetFromOptions(*ts));
4605e82a6e1SJeremy L Thompson   user->time = -1.0;  // require all BCs and ctx to be updated
4615e82a6e1SJeremy L Thompson   user->dt   = -1.0;
46277841947SLeila Ghaffari   if (!app_ctx->cont_steps) {  // print initial condition
46377841947SLeila Ghaffari     if (!app_ctx->test_mode) {
4642b730f8bSJeremy L Thompson       PetscCall(TSMonitor_NS(*ts, 0, 0., *Q, user));
46577841947SLeila Ghaffari     }
46677841947SLeila Ghaffari   } else {  // continue from time of last output
46777841947SLeila Ghaffari     PetscInt    count;
46877841947SLeila Ghaffari     PetscViewer viewer;
4692b730f8bSJeremy L Thompson 
470*4de8550aSJed Brown     if (app_ctx->cont_time <= 0) {  // Legacy files did not include step number and time
4712b730f8bSJeremy L Thompson       PetscCall(PetscViewerBinaryOpen(comm, app_ctx->cont_time_file, FILE_MODE_READ, &viewer));
472*4de8550aSJed Brown       PetscCall(PetscViewerBinaryRead(viewer, &app_ctx->cont_time, 1, &count, PETSC_REAL));
4732b730f8bSJeremy L Thompson       PetscCall(PetscViewerDestroy(&viewer));
474*4de8550aSJed Brown       PetscCheck(app_ctx->cont_steps != -1, comm, PETSC_ERR_ARG_INCOMP,
475*4de8550aSJed Brown                  "-continue step number not specified, but checkpoint file does not contain a step number (likely written by older code version)");
476*4de8550aSJed Brown     }
477*4de8550aSJed Brown     PetscCall(TSSetTime(*ts, app_ctx->cont_time * user->units->second));
478d8e0aecdSJed Brown     PetscCall(TSSetStepNumber(*ts, app_ctx->cont_steps));
47977841947SLeila Ghaffari   }
48077841947SLeila Ghaffari   if (!app_ctx->test_mode) {
4812b730f8bSJeremy L Thompson     PetscCall(TSMonitorSet(*ts, TSMonitor_NS, user, NULL));
48277841947SLeila Ghaffari   }
48377841947SLeila Ghaffari 
48477841947SLeila Ghaffari   // Solve
485d8e0aecdSJed Brown   PetscReal start_time;
486d8e0aecdSJed Brown   PetscInt  start_step;
4872b730f8bSJeremy L Thompson   PetscCall(TSGetTime(*ts, &start_time));
488d8e0aecdSJed Brown   PetscCall(TSGetStepNumber(*ts, &start_step));
4897b39487dSJeremy L Thompson 
4907b39487dSJeremy L Thompson   PetscPreLoadBegin(PETSC_FALSE, "Fluids Solve");
4917b39487dSJeremy L Thompson   PetscCall(TSSetTime(*ts, start_time));
492d8e0aecdSJed Brown   PetscCall(TSSetStepNumber(*ts, start_step));
4937b39487dSJeremy L Thompson   if (PetscPreLoadingOn) {
4947b39487dSJeremy L Thompson     // LCOV_EXCL_START
4957b39487dSJeremy L Thompson     SNES      snes;
4967b39487dSJeremy L Thompson     Vec       Q_preload;
4977b39487dSJeremy L Thompson     PetscReal rtol;
4987b39487dSJeremy L Thompson     PetscCall(VecDuplicate(*Q, &Q_preload));
4997b39487dSJeremy L Thompson     PetscCall(VecCopy(*Q, Q_preload));
5007b39487dSJeremy L Thompson     PetscCall(TSGetSNES(*ts, &snes));
5017b39487dSJeremy L Thompson     PetscCall(SNESGetTolerances(snes, NULL, &rtol, NULL, NULL, NULL));
5022b730f8bSJeremy L Thompson     PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, .99, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT));
5037b39487dSJeremy L Thompson     PetscCall(TSSetSolution(*ts, *Q));
5047b39487dSJeremy L Thompson     PetscCall(TSStep(*ts));
5052b730f8bSJeremy L Thompson     PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, rtol, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT));
5067b39487dSJeremy L Thompson     PetscCall(VecDestroy(&Q_preload));
5077b39487dSJeremy L Thompson     // LCOV_EXCL_STOP
5087b39487dSJeremy L Thompson   } else {
5092b730f8bSJeremy L Thompson     PetscCall(PetscBarrier((PetscObject)*ts));
5102b730f8bSJeremy L Thompson     PetscCall(TSSolve(*ts, *Q));
5117b39487dSJeremy L Thompson   }
5127b39487dSJeremy L Thompson   PetscPreLoadEnd();
5137b39487dSJeremy L Thompson 
5147b39487dSJeremy L Thompson   PetscCall(TSGetSolveTime(*ts, &final_time));
51577841947SLeila Ghaffari   *f_time = final_time;
5167b39487dSJeremy L Thompson 
51777841947SLeila Ghaffari   if (!app_ctx->test_mode) {
51837cbb16aSJed Brown     if (user->app_ctx->checkpoint_interval > 0 || user->app_ctx->checkpoint_interval == -1) {
519f14660a4SJames Wright       PetscInt step_no;
520f14660a4SJames Wright       PetscCall(TSGetStepNumber(*ts, &step_no));
521f14660a4SJames Wright       PetscCall(WriteOutput(user, *Q, step_no, final_time));
522f14660a4SJames Wright     }
523f14660a4SJames Wright 
5247b39487dSJeremy L Thompson     PetscLogEvent stage_id;
5257b39487dSJeremy L Thompson     PetscStageLog stage_log;
5267b39487dSJeremy L Thompson 
5277b39487dSJeremy L Thompson     PetscCall(PetscLogStageGetId("Fluids Solve", &stage_id));
5287b39487dSJeremy L Thompson     PetscCall(PetscLogGetStageLog(&stage_log));
5292b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Time taken for solution (sec): %g\n", stage_log->stageInfo[stage_id].perfInfo.time));
53077841947SLeila Ghaffari   }
53177841947SLeila Ghaffari   PetscFunctionReturn(0);
53277841947SLeila Ghaffari }
533