xref: /honee/src/setupts.c (revision c2cb7fc84be766567c7db5f0745e989dc7918e77)
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"
12a515125bSLeila Ghaffari 
13a515125bSLeila Ghaffari // Compute mass matrix for explicit scheme
142b916ea7SJeremy L Thompson PetscErrorCode ComputeLumpedMassMatrix(Ceed ceed, DM dm, CeedData ceed_data, Vec M) {
15a515125bSLeila Ghaffari   Vec           M_loc;
16a515125bSLeila Ghaffari   CeedQFunction qf_mass;
17a515125bSLeila Ghaffari   CeedOperator  op_mass;
18a515125bSLeila Ghaffari   CeedVector    m_ceed, ones_vec;
19a515125bSLeila Ghaffari   CeedInt       num_comp_q, q_data_size;
20a515125bSLeila Ghaffari   PetscFunctionBeginUser;
21a515125bSLeila Ghaffari 
22a515125bSLeila Ghaffari   // CEED Restriction
23a515125bSLeila Ghaffari   CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_q, &num_comp_q);
24a515125bSLeila Ghaffari   CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_qd_i, &q_data_size);
25a515125bSLeila Ghaffari   CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &m_ceed, NULL);
26a515125bSLeila Ghaffari   CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &ones_vec, NULL);
27a515125bSLeila Ghaffari   CeedVectorSetValue(ones_vec, 1.0);
28a515125bSLeila Ghaffari 
29a515125bSLeila Ghaffari   // CEED QFunction
309f59f36eSJames Wright   PetscCall(CreateMassQFunction(ceed, num_comp_q, q_data_size, &qf_mass));
31a515125bSLeila Ghaffari 
32a515125bSLeila Ghaffari   // CEED Operator
33a515125bSLeila Ghaffari   CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass);
349f59f36eSJames Wright   CeedOperatorSetField(op_mass, "u", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE);
352b916ea7SJeremy L Thompson   CeedOperatorSetField(op_mass, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data->q_data);
362b916ea7SJeremy L Thompson   CeedOperatorSetField(op_mass, "v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE);
37a515125bSLeila Ghaffari 
38a515125bSLeila Ghaffari   // Place PETSc vector in CEED vector
39a515125bSLeila Ghaffari   CeedScalar  *m;
40a515125bSLeila Ghaffari   PetscMemType m_mem_type;
412b916ea7SJeremy L Thompson   PetscCall(DMGetLocalVector(dm, &M_loc));
422b916ea7SJeremy L Thompson   PetscCall(VecGetArrayAndMemType(M_loc, (PetscScalar **)&m, &m_mem_type));
43a515125bSLeila Ghaffari   CeedVectorSetArray(m_ceed, MemTypeP2C(m_mem_type), CEED_USE_POINTER, m);
44a515125bSLeila Ghaffari 
45a515125bSLeila Ghaffari   // Apply CEED Operator
46a515125bSLeila Ghaffari   CeedOperatorApply(op_mass, ones_vec, m_ceed, CEED_REQUEST_IMMEDIATE);
47a515125bSLeila Ghaffari 
48a515125bSLeila Ghaffari   // Restore vectors
49a515125bSLeila Ghaffari   CeedVectorTakeArray(m_ceed, MemTypeP2C(m_mem_type), NULL);
502b916ea7SJeremy L Thompson   PetscCall(VecRestoreArrayReadAndMemType(M_loc, (const PetscScalar **)&m));
51a515125bSLeila Ghaffari 
52a515125bSLeila Ghaffari   // Local-to-Global
532b916ea7SJeremy L Thompson   PetscCall(VecZeroEntries(M));
542b916ea7SJeremy L Thompson   PetscCall(DMLocalToGlobal(dm, M_loc, ADD_VALUES, M));
552b916ea7SJeremy L Thompson   PetscCall(DMRestoreLocalVector(dm, &M_loc));
56a515125bSLeila Ghaffari 
57a515125bSLeila Ghaffari   // Invert diagonally lumped mass vector for RHS function
582b916ea7SJeremy L Thompson   PetscCall(VecReciprocal(M));
59a515125bSLeila Ghaffari 
60a515125bSLeila Ghaffari   // Cleanup
61a515125bSLeila Ghaffari   CeedVectorDestroy(&ones_vec);
62a515125bSLeila Ghaffari   CeedVectorDestroy(&m_ceed);
63a515125bSLeila Ghaffari   CeedQFunctionDestroy(&qf_mass);
64a515125bSLeila Ghaffari   CeedOperatorDestroy(&op_mass);
65a515125bSLeila Ghaffari 
66a515125bSLeila Ghaffari   PetscFunctionReturn(0);
67a515125bSLeila Ghaffari }
68a515125bSLeila Ghaffari 
69c996854bSJames Wright // Insert Boundary values if it's a new time
70c996854bSJames Wright PetscErrorCode UpdateBoundaryValues(User user, Vec Q_loc, PetscReal t) {
71c996854bSJames Wright   PetscFunctionBeginUser;
72c996854bSJames Wright   if (user->time_bc_set != t) {
73c996854bSJames Wright     PetscCall(DMPlexInsertBoundaryValues(user->dm, PETSC_TRUE, Q_loc, t, NULL, NULL, NULL));
74c996854bSJames Wright     user->time_bc_set = t;
75c996854bSJames Wright   }
76c996854bSJames Wright   PetscFunctionReturn(0);
77c996854bSJames Wright }
78c996854bSJames Wright 
79*c2cb7fc8SJames Wright PetscErrorCode UpdateContextLabel(PetscScalar *previous_value, PetscScalar update_value, CeedOperator op, CeedContextFieldLabel label) {
80*c2cb7fc8SJames Wright   PetscFunctionBeginUser;
81*c2cb7fc8SJames Wright 
82*c2cb7fc8SJames Wright   if (*previous_value != update_value) {
83*c2cb7fc8SJames Wright     if (label) {
84*c2cb7fc8SJames Wright       CeedOperatorContextSetDouble(op, label, &update_value);
85*c2cb7fc8SJames Wright     }
86*c2cb7fc8SJames Wright     *previous_value = update_value;
87*c2cb7fc8SJames Wright   }
88*c2cb7fc8SJames Wright   PetscFunctionReturn(0);
89*c2cb7fc8SJames Wright }
90*c2cb7fc8SJames Wright 
91a515125bSLeila Ghaffari // RHS (Explicit time-stepper) function setup
92a515125bSLeila Ghaffari //   This is the RHS of the ODE, given as u_t = G(t,u)
93a515125bSLeila Ghaffari //   This function takes in a state vector Q and writes into G
94a515125bSLeila Ghaffari PetscErrorCode RHS_NS(TS ts, PetscReal t, Vec Q, Vec G, void *user_data) {
95a515125bSLeila Ghaffari   User         user = *(User *)user_data;
96*c2cb7fc8SJames Wright   PetscScalar *q, *g, dt;
97e2f84137SJeremy L Thompson   Vec          Q_loc = user->Q_loc, G_loc;
98a515125bSLeila Ghaffari   PetscMemType q_mem_type, g_mem_type;
99a515125bSLeila Ghaffari   PetscFunctionBeginUser;
100a515125bSLeila Ghaffari 
101e2f84137SJeremy L Thompson   // Get local vector
1022b916ea7SJeremy L Thompson   PetscCall(DMGetLocalVector(user->dm, &G_loc));
103e2f84137SJeremy L Thompson 
104e2f84137SJeremy L Thompson   // Update time dependent data
105c996854bSJames Wright   PetscCall(UpdateBoundaryValues(user, Q_loc, t));
106*c2cb7fc8SJames Wright   PetscCall(UpdateContextLabel(&user->time, t, user->op_rhs, user->phys->solution_time_label));
1072b916ea7SJeremy L Thompson   PetscCall(TSGetTimeStep(ts, &dt));
108*c2cb7fc8SJames Wright   PetscCall(UpdateContextLabel(&user->dt, dt, user->op_rhs, user->phys->timestep_size_label));
109a515125bSLeila Ghaffari 
110a515125bSLeila Ghaffari   // Global-to-local
1112b916ea7SJeremy L Thompson   PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc));
112a515125bSLeila Ghaffari 
113a515125bSLeila Ghaffari   // Place PETSc vectors in CEED vectors
1142b916ea7SJeremy L Thompson   PetscCall(VecGetArrayReadAndMemType(Q_loc, (const PetscScalar **)&q, &q_mem_type));
1152b916ea7SJeremy L Thompson   PetscCall(VecGetArrayAndMemType(G_loc, &g, &g_mem_type));
116a515125bSLeila Ghaffari   CeedVectorSetArray(user->q_ceed, MemTypeP2C(q_mem_type), CEED_USE_POINTER, q);
117a515125bSLeila Ghaffari   CeedVectorSetArray(user->g_ceed, MemTypeP2C(g_mem_type), CEED_USE_POINTER, g);
118a515125bSLeila Ghaffari 
119a515125bSLeila Ghaffari   // Apply CEED operator
1202b916ea7SJeremy L Thompson   CeedOperatorApply(user->op_rhs, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE);
121a515125bSLeila Ghaffari 
122a515125bSLeila Ghaffari   // Restore vectors
123a515125bSLeila Ghaffari   CeedVectorTakeArray(user->q_ceed, MemTypeP2C(q_mem_type), NULL);
124a515125bSLeila Ghaffari   CeedVectorTakeArray(user->g_ceed, MemTypeP2C(g_mem_type), NULL);
1252b916ea7SJeremy L Thompson   PetscCall(VecRestoreArrayReadAndMemType(Q_loc, (const PetscScalar **)&q));
1262b916ea7SJeremy L Thompson   PetscCall(VecRestoreArrayAndMemType(G_loc, &g));
127a515125bSLeila Ghaffari 
128a515125bSLeila Ghaffari   // Local-to-Global
1292b916ea7SJeremy L Thompson   PetscCall(VecZeroEntries(G));
1302b916ea7SJeremy L Thompson   PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G));
131a515125bSLeila Ghaffari 
132a515125bSLeila Ghaffari   // Inverse of the lumped mass matrix (M is Minv)
1332b916ea7SJeremy L Thompson   PetscCall(VecPointwiseMult(G, G, user->M));
134a515125bSLeila Ghaffari 
135a515125bSLeila Ghaffari   // Restore vectors
1362b916ea7SJeremy L Thompson   PetscCall(DMRestoreLocalVector(user->dm, &G_loc));
137a515125bSLeila Ghaffari 
138a515125bSLeila Ghaffari   PetscFunctionReturn(0);
139a515125bSLeila Ghaffari }
140a515125bSLeila Ghaffari 
141a515125bSLeila Ghaffari // Implicit time-stepper function setup
1422b916ea7SJeremy L Thompson PetscErrorCode IFunction_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, Vec G, void *user_data) {
143a515125bSLeila Ghaffari   User               user = *(User *)user_data;
144a515125bSLeila Ghaffari   const PetscScalar *q, *q_dot;
145*c2cb7fc8SJames Wright   PetscScalar       *g, dt;
146e2f84137SJeremy L Thompson   Vec                Q_loc = user->Q_loc, Q_dot_loc = user->Q_dot_loc, G_loc;
147a515125bSLeila Ghaffari   PetscMemType       q_mem_type, q_dot_mem_type, g_mem_type;
148a515125bSLeila Ghaffari   PetscFunctionBeginUser;
149a515125bSLeila Ghaffari 
150e2f84137SJeremy L Thompson   // Get local vectors
1512b916ea7SJeremy L Thompson   PetscCall(DMGetLocalVector(user->dm, &G_loc));
152e2f84137SJeremy L Thompson 
153e2f84137SJeremy L Thompson   // Update time dependent data
154c996854bSJames Wright   PetscCall(UpdateBoundaryValues(user, Q_loc, t));
155*c2cb7fc8SJames Wright   PetscCall(UpdateContextLabel(&user->time, t, user->op_ifunction, user->phys->solution_time_label));
1562b916ea7SJeremy L Thompson   PetscCall(TSGetTimeStep(ts, &dt));
157*c2cb7fc8SJames Wright   PetscCall(UpdateContextLabel(&user->dt, dt, user->op_ifunction, user->phys->timestep_size_label));
158a515125bSLeila Ghaffari 
159a515125bSLeila Ghaffari   // Global-to-local
16006108310SJames Wright   PetscCall(DMGlobalToLocalBegin(user->dm, Q, INSERT_VALUES, Q_loc));
16106108310SJames Wright   PetscCall(DMGlobalToLocalBegin(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc));
16206108310SJames Wright   PetscCall(DMGlobalToLocalEnd(user->dm, Q, INSERT_VALUES, Q_loc));
16306108310SJames Wright   PetscCall(DMGlobalToLocalEnd(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc));
164a515125bSLeila Ghaffari 
165a515125bSLeila Ghaffari   // Place PETSc vectors in CEED vectors
1662b916ea7SJeremy L Thompson   PetscCall(VecGetArrayReadAndMemType(Q_loc, &q, &q_mem_type));
1672b916ea7SJeremy L Thompson   PetscCall(VecGetArrayReadAndMemType(Q_dot_loc, &q_dot, &q_dot_mem_type));
1682b916ea7SJeremy L Thompson   PetscCall(VecGetArrayAndMemType(G_loc, &g, &g_mem_type));
1692b916ea7SJeremy L Thompson   CeedVectorSetArray(user->q_ceed, MemTypeP2C(q_mem_type), CEED_USE_POINTER, (PetscScalar *)q);
1702b916ea7SJeremy L Thompson   CeedVectorSetArray(user->q_dot_ceed, MemTypeP2C(q_dot_mem_type), CEED_USE_POINTER, (PetscScalar *)q_dot);
171a515125bSLeila Ghaffari   CeedVectorSetArray(user->g_ceed, MemTypeP2C(g_mem_type), CEED_USE_POINTER, g);
172a515125bSLeila Ghaffari 
173a515125bSLeila Ghaffari   // Apply CEED operator
1742b916ea7SJeremy L Thompson   CeedOperatorApply(user->op_ifunction, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE);
175a515125bSLeila Ghaffari 
176a515125bSLeila Ghaffari   // Restore vectors
177a515125bSLeila Ghaffari   CeedVectorTakeArray(user->q_ceed, MemTypeP2C(q_mem_type), NULL);
178a515125bSLeila Ghaffari   CeedVectorTakeArray(user->q_dot_ceed, MemTypeP2C(q_dot_mem_type), NULL);
179a515125bSLeila Ghaffari   CeedVectorTakeArray(user->g_ceed, MemTypeP2C(g_mem_type), NULL);
1802b916ea7SJeremy L Thompson   PetscCall(VecRestoreArrayReadAndMemType(Q_loc, &q));
1812b916ea7SJeremy L Thompson   PetscCall(VecRestoreArrayReadAndMemType(Q_dot_loc, &q_dot));
1822b916ea7SJeremy L Thompson   PetscCall(VecRestoreArrayAndMemType(G_loc, &g));
183a515125bSLeila Ghaffari 
184a515125bSLeila Ghaffari   // Local-to-Global
1852b916ea7SJeremy L Thompson   PetscCall(VecZeroEntries(G));
1862b916ea7SJeremy L Thompson   PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G));
187a515125bSLeila Ghaffari 
188a515125bSLeila Ghaffari   // Restore vectors
1892b916ea7SJeremy L Thompson   PetscCall(DMRestoreLocalVector(user->dm, &G_loc));
190a515125bSLeila Ghaffari 
191a515125bSLeila Ghaffari   PetscFunctionReturn(0);
192a515125bSLeila Ghaffari }
193a515125bSLeila Ghaffari 
194f0b65372SJed Brown static PetscErrorCode MatMult_NS_IJacobian(Mat J, Vec Q, Vec G) {
195f0b65372SJed Brown   User               user;
196f0b65372SJed Brown   const PetscScalar *q;
197f0b65372SJed Brown   PetscScalar       *g;
198f0b65372SJed Brown   PetscMemType       q_mem_type, g_mem_type;
199f0b65372SJed Brown   PetscFunctionBeginUser;
2002b916ea7SJeremy L Thompson   PetscCall(MatShellGetContext(J, &user));
201e2f84137SJeremy L Thompson   Vec Q_loc = user->Q_dot_loc,  // Note - Q_dot_loc has zero BCs
202e2f84137SJeremy L Thompson       G_loc;
203e2f84137SJeremy L Thompson 
204f0b65372SJed Brown   // Get local vectors
2052b916ea7SJeremy L Thompson   PetscCall(DMGetLocalVector(user->dm, &G_loc));
206f0b65372SJed Brown 
207f0b65372SJed Brown   // Global-to-local
2082b916ea7SJeremy L Thompson   PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc));
209f0b65372SJed Brown 
210f0b65372SJed Brown   // Place PETSc vectors in CEED vectors
2112b916ea7SJeremy L Thompson   PetscCall(VecGetArrayReadAndMemType(Q_loc, &q, &q_mem_type));
2122b916ea7SJeremy L Thompson   PetscCall(VecGetArrayAndMemType(G_loc, &g, &g_mem_type));
2132b916ea7SJeremy L Thompson   CeedVectorSetArray(user->q_ceed, MemTypeP2C(q_mem_type), CEED_USE_POINTER, (PetscScalar *)q);
214f0b65372SJed Brown   CeedVectorSetArray(user->g_ceed, MemTypeP2C(g_mem_type), CEED_USE_POINTER, g);
215f0b65372SJed Brown 
216f0b65372SJed Brown   // Apply CEED operator
2172b916ea7SJeremy L Thompson   CeedOperatorApply(user->op_ijacobian, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE);
218f0b65372SJed Brown 
219f0b65372SJed Brown   // Restore vectors
220f0b65372SJed Brown   CeedVectorTakeArray(user->q_ceed, MemTypeP2C(q_mem_type), NULL);
221f0b65372SJed Brown   CeedVectorTakeArray(user->g_ceed, MemTypeP2C(g_mem_type), NULL);
2222b916ea7SJeremy L Thompson   PetscCall(VecRestoreArrayReadAndMemType(Q_loc, &q));
2232b916ea7SJeremy L Thompson   PetscCall(VecRestoreArrayAndMemType(G_loc, &g));
224f0b65372SJed Brown 
225f0b65372SJed Brown   // Local-to-Global
2262b916ea7SJeremy L Thompson   PetscCall(VecZeroEntries(G));
2272b916ea7SJeremy L Thompson   PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G));
228f0b65372SJed Brown 
229f0b65372SJed Brown   // Restore vectors
2302b916ea7SJeremy L Thompson   PetscCall(DMRestoreLocalVector(user->dm, &G_loc));
231f0b65372SJed Brown   PetscFunctionReturn(0);
232f0b65372SJed Brown }
233f0b65372SJed Brown 
234f0b65372SJed Brown PetscErrorCode MatGetDiagonal_NS_IJacobian(Mat A, Vec D) {
235f0b65372SJed Brown   User         user;
236f0b65372SJed Brown   Vec          D_loc;
237f0b65372SJed Brown   PetscScalar *d;
238f0b65372SJed Brown   PetscMemType mem_type;
239f0b65372SJed Brown 
240f0b65372SJed Brown   PetscFunctionBeginUser;
241f0b65372SJed Brown   MatShellGetContext(A, &user);
242f0b65372SJed Brown   PetscCall(DMGetLocalVector(user->dm, &D_loc));
243f0b65372SJed Brown   PetscCall(VecGetArrayAndMemType(D_loc, &d, &mem_type));
244f0b65372SJed Brown   CeedVectorSetArray(user->g_ceed, MemTypeP2C(mem_type), CEED_USE_POINTER, d);
2452b916ea7SJeremy L Thompson   CeedOperatorLinearAssembleDiagonal(user->op_ijacobian, user->g_ceed, CEED_REQUEST_IMMEDIATE);
246f0b65372SJed Brown   CeedVectorTakeArray(user->g_ceed, MemTypeP2C(mem_type), NULL);
247f0b65372SJed Brown   PetscCall(VecRestoreArrayAndMemType(D_loc, &d));
248f0b65372SJed Brown   PetscCall(VecZeroEntries(D));
249f0b65372SJed Brown   PetscCall(DMLocalToGlobal(user->dm, D_loc, ADD_VALUES, D));
250f0b65372SJed Brown   PetscCall(DMRestoreLocalVector(user->dm, &D_loc));
251f0b65372SJed Brown   VecViewFromOptions(D, NULL, "-diag_vec_view");
252f0b65372SJed Brown   PetscFunctionReturn(0);
253f0b65372SJed Brown }
254f0b65372SJed Brown 
2552b916ea7SJeremy L Thompson static PetscErrorCode FormPreallocation(User user, PetscBool pbdiagonal, Mat J, CeedVector *coo_values) {
256b107fddaSJed Brown   PetscCount ncoo;
257b107fddaSJed Brown   PetscInt  *rows, *cols;
258b107fddaSJed Brown 
259b107fddaSJed Brown   PetscFunctionBeginUser;
260b107fddaSJed Brown   if (pbdiagonal) {
261b107fddaSJed Brown     CeedSize l_size;
262b107fddaSJed Brown     CeedOperatorGetActiveVectorLengths(user->op_ijacobian, &l_size, NULL);
263b107fddaSJed Brown     ncoo = l_size * 5;
264b107fddaSJed Brown     rows = malloc(ncoo * sizeof(rows[0]));
265b107fddaSJed Brown     cols = malloc(ncoo * sizeof(cols[0]));
266b107fddaSJed Brown     for (PetscCount n = 0; n < l_size / 5; n++) {
267b107fddaSJed Brown       for (PetscInt i = 0; i < 5; i++) {
268b107fddaSJed Brown         for (PetscInt j = 0; j < 5; j++) {
269b107fddaSJed Brown           rows[(n * 5 + i) * 5 + j] = n * 5 + i;
270b107fddaSJed Brown           cols[(n * 5 + i) * 5 + j] = n * 5 + j;
271b107fddaSJed Brown         }
272b107fddaSJed Brown       }
273b107fddaSJed Brown     }
274b107fddaSJed Brown   } else {
2752b916ea7SJeremy L Thompson     PetscCall(CeedOperatorLinearAssembleSymbolic(user->op_ijacobian, &ncoo, &rows, &cols));
276b107fddaSJed Brown   }
277b107fddaSJed Brown   PetscCall(MatSetPreallocationCOOLocal(J, ncoo, rows, cols));
278b107fddaSJed Brown   free(rows);
279b107fddaSJed Brown   free(cols);
280b107fddaSJed Brown   CeedVectorCreate(user->ceed, ncoo, coo_values);
281b107fddaSJed Brown   PetscFunctionReturn(0);
282b107fddaSJed Brown }
283b107fddaSJed Brown 
2842b916ea7SJeremy L Thompson static PetscErrorCode FormSetValues(User user, PetscBool pbdiagonal, Mat J, CeedVector coo_values) {
285b107fddaSJed Brown   CeedMemType        mem_type = CEED_MEM_HOST;
286b107fddaSJed Brown   const PetscScalar *values;
287b107fddaSJed Brown   MatType            mat_type;
288b107fddaSJed Brown 
289b107fddaSJed Brown   PetscFunctionBeginUser;
290b107fddaSJed Brown   PetscCall(MatGetType(J, &mat_type));
2912b916ea7SJeremy L Thompson   if (strstr(mat_type, "kokkos") || strstr(mat_type, "cusparse")) mem_type = CEED_MEM_DEVICE;
292b107fddaSJed Brown   if (user->app_ctx->pmat_pbdiagonal) {
2932b916ea7SJeremy L Thompson     CeedOperatorLinearAssemblePointBlockDiagonal(user->op_ijacobian, coo_values, CEED_REQUEST_IMMEDIATE);
294b107fddaSJed Brown   } else {
295b107fddaSJed Brown     CeedOperatorLinearAssemble(user->op_ijacobian, coo_values);
296b107fddaSJed Brown   }
297b107fddaSJed Brown   CeedVectorGetArrayRead(coo_values, mem_type, &values);
298b107fddaSJed Brown   PetscCall(MatSetValuesCOO(J, values, INSERT_VALUES));
299b107fddaSJed Brown   CeedVectorRestoreArrayRead(coo_values, &values);
300b107fddaSJed Brown   PetscFunctionReturn(0);
301b107fddaSJed Brown }
302b107fddaSJed Brown 
3032b916ea7SJeremy L Thompson PetscErrorCode FormIJacobian_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, PetscReal shift, Mat J, Mat J_pre, void *user_data) {
304f0b65372SJed Brown   User      user = *(User *)user_data;
30504855949SJed Brown   PetscBool J_is_shell, J_is_mffd, J_pre_is_shell;
306f0b65372SJed Brown   PetscFunctionBeginUser;
3072b916ea7SJeremy L Thompson   if (user->phys->ijacobian_time_shift_label) CeedOperatorContextSetDouble(user->op_ijacobian, user->phys->ijacobian_time_shift_label, &shift);
30804855949SJed Brown   PetscCall(PetscObjectTypeCompare((PetscObject)J, MATMFFD, &J_is_mffd));
309f0b65372SJed Brown   PetscCall(PetscObjectTypeCompare((PetscObject)J, MATSHELL, &J_is_shell));
3102b916ea7SJeremy L Thompson   PetscCall(PetscObjectTypeCompare((PetscObject)J_pre, MATSHELL, &J_pre_is_shell));
311f0b65372SJed Brown   if (!user->matrices_set_up) {
312f0b65372SJed Brown     if (J_is_shell) {
313f0b65372SJed Brown       PetscCall(MatShellSetContext(J, user));
3142b916ea7SJeremy L Thompson       PetscCall(MatShellSetOperation(J, MATOP_MULT, (void (*)(void))MatMult_NS_IJacobian));
3152b916ea7SJeremy L Thompson       PetscCall(MatShellSetOperation(J, MATOP_GET_DIAGONAL, (void (*)(void))MatGetDiagonal_NS_IJacobian));
316f0b65372SJed Brown       PetscCall(MatSetUp(J));
317f0b65372SJed Brown     }
318f0b65372SJed Brown     if (!J_pre_is_shell) {
3192b916ea7SJeremy L Thompson       PetscCall(FormPreallocation(user, user->app_ctx->pmat_pbdiagonal, J_pre, &user->coo_values_pmat));
320b107fddaSJed Brown     }
32104855949SJed Brown     if (J != J_pre && !J_is_shell && !J_is_mffd) {
322b107fddaSJed Brown       PetscCall(FormPreallocation(user, PETSC_FALSE, J, &user->coo_values_amat));
323b107fddaSJed Brown     }
324f0b65372SJed Brown     user->matrices_set_up = true;
325f0b65372SJed Brown   }
326f0b65372SJed Brown   if (!J_pre_is_shell) {
3272b916ea7SJeremy L Thompson     PetscCall(FormSetValues(user, user->app_ctx->pmat_pbdiagonal, J_pre, user->coo_values_pmat));
328f0b65372SJed Brown   }
32904855949SJed Brown   if (user->coo_values_amat) {
33004855949SJed Brown     PetscCall(FormSetValues(user, PETSC_FALSE, J, user->coo_values_amat));
33104855949SJed Brown   } else if (J_is_mffd) {
33204855949SJed Brown     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
33304855949SJed Brown     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
33404855949SJed Brown   }
335f0b65372SJed Brown   PetscFunctionReturn(0);
336f0b65372SJed Brown }
337f0b65372SJed Brown 
3382b916ea7SJeremy L Thompson PetscErrorCode WriteOutput(User user, Vec Q, PetscInt step_no, PetscScalar time) {
339a515125bSLeila Ghaffari   Vec         Q_loc;
340a515125bSLeila Ghaffari   char        file_path[PETSC_MAX_PATH_LEN];
341a515125bSLeila Ghaffari   PetscViewer viewer;
342a515125bSLeila Ghaffari   PetscFunctionBeginUser;
343a515125bSLeila Ghaffari 
344852e5969SJed Brown   if (user->app_ctx->checkpoint_vtk) {
345a515125bSLeila Ghaffari     // Set up output
3467538d537SJames Wright     PetscCall(DMGetLocalVector(user->dm, &Q_loc));
3477538d537SJames Wright     PetscCall(PetscObjectSetName((PetscObject)Q_loc, "StateVec"));
3487538d537SJames Wright     PetscCall(VecZeroEntries(Q_loc));
3497538d537SJames Wright     PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc));
350a515125bSLeila Ghaffari 
351a515125bSLeila Ghaffari     // Output
352852e5969SJed Brown     PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no));
3537538d537SJames Wright 
3542b916ea7SJeremy L Thompson     PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q), file_path, FILE_MODE_WRITE, &viewer));
3557538d537SJames Wright     PetscCall(VecView(Q_loc, viewer));
3567538d537SJames Wright     PetscCall(PetscViewerDestroy(&viewer));
357a515125bSLeila Ghaffari     if (user->dm_viz) {
358a515125bSLeila Ghaffari       Vec         Q_refined, Q_refined_loc;
359a515125bSLeila Ghaffari       char        file_path_refined[PETSC_MAX_PATH_LEN];
360a515125bSLeila Ghaffari       PetscViewer viewer_refined;
361a515125bSLeila Ghaffari 
3627538d537SJames Wright       PetscCall(DMGetGlobalVector(user->dm_viz, &Q_refined));
3637538d537SJames Wright       PetscCall(DMGetLocalVector(user->dm_viz, &Q_refined_loc));
3647538d537SJames Wright       PetscCall(PetscObjectSetName((PetscObject)Q_refined_loc, "Refined"));
3657538d537SJames Wright 
3667538d537SJames Wright       PetscCall(MatInterpolate(user->interp_viz, Q, Q_refined));
3677538d537SJames Wright       PetscCall(VecZeroEntries(Q_refined_loc));
3682b916ea7SJeremy L Thompson       PetscCall(DMGlobalToLocal(user->dm_viz, Q_refined, INSERT_VALUES, Q_refined_loc));
3697538d537SJames Wright 
370852e5969SJed Brown       PetscCall(
371852e5969SJed Brown           PetscSNPrintf(file_path_refined, sizeof file_path_refined, "%s/nsrefined-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no));
3727538d537SJames Wright 
3732b916ea7SJeremy L Thompson       PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q_refined), file_path_refined, FILE_MODE_WRITE, &viewer_refined));
3747538d537SJames Wright       PetscCall(VecView(Q_refined_loc, viewer_refined));
3757538d537SJames Wright       PetscCall(DMRestoreLocalVector(user->dm_viz, &Q_refined_loc));
3767538d537SJames Wright       PetscCall(DMRestoreGlobalVector(user->dm_viz, &Q_refined));
3777538d537SJames Wright       PetscCall(PetscViewerDestroy(&viewer_refined));
378a515125bSLeila Ghaffari     }
3797538d537SJames Wright     PetscCall(DMRestoreLocalVector(user->dm, &Q_loc));
380852e5969SJed Brown   }
381a515125bSLeila Ghaffari 
382a515125bSLeila Ghaffari   // Save data in a binary file for continuation of simulations
38391a36801SJames Wright   if (user->app_ctx->add_stepnum2bin) {
384852e5969SJed Brown     PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution-%" PetscInt_FMT ".bin", user->app_ctx->output_dir, step_no));
38591a36801SJames Wright   } else {
3862b916ea7SJeremy L Thompson     PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution.bin", user->app_ctx->output_dir));
38791a36801SJames Wright   }
3882b916ea7SJeremy L Thompson   PetscCall(PetscViewerBinaryOpen(user->comm, file_path, FILE_MODE_WRITE, &viewer));
3897538d537SJames Wright 
3909293eaa1SJed Brown   PetscInt token = FLUIDS_FILE_TOKEN;
3919293eaa1SJed Brown   PetscCall(PetscViewerBinaryWrite(viewer, &token, 1, PETSC_INT));
3929293eaa1SJed Brown   PetscCall(PetscViewerBinaryWrite(viewer, &step_no, 1, PETSC_INT));
3939293eaa1SJed Brown   time /= user->units->second;  // Dimensionalize time back
3949293eaa1SJed Brown   PetscCall(PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL));
3957538d537SJames Wright   PetscCall(VecView(Q, viewer));
3967538d537SJames Wright   PetscCall(PetscViewerDestroy(&viewer));
3977538d537SJames Wright   PetscFunctionReturn(0);
3987538d537SJames Wright }
3997538d537SJames Wright 
4007538d537SJames Wright // User provided TS Monitor
4012b916ea7SJeremy L Thompson PetscErrorCode TSMonitor_NS(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) {
4027538d537SJames Wright   User user = ctx;
4037538d537SJames Wright   PetscFunctionBeginUser;
4047538d537SJames Wright 
405852e5969SJed Brown   // Print every 'checkpoint_interval' steps
406c539088bSJames Wright   if (user->app_ctx->checkpoint_interval <= 0 || step_no % user->app_ctx->checkpoint_interval != 0 ||
407c539088bSJames Wright       (user->app_ctx->cont_steps == step_no && step_no != 0))
408c539088bSJames Wright     PetscFunctionReturn(0);
4097538d537SJames Wright 
4107538d537SJames Wright   PetscCall(WriteOutput(user, Q, step_no, time));
411a515125bSLeila Ghaffari 
412a515125bSLeila Ghaffari   PetscFunctionReturn(0);
413a515125bSLeila Ghaffari }
414a515125bSLeila Ghaffari 
415a515125bSLeila Ghaffari // TS: Create, setup, and solve
4162b916ea7SJeremy L Thompson PetscErrorCode TSSolve_NS(DM dm, User user, AppCtx app_ctx, Physics phys, Vec *Q, PetscScalar *f_time, TS *ts) {
417a515125bSLeila Ghaffari   MPI_Comm    comm = user->comm;
418a515125bSLeila Ghaffari   TSAdapt     adapt;
419a515125bSLeila Ghaffari   PetscScalar final_time;
420a515125bSLeila Ghaffari   PetscFunctionBeginUser;
421a515125bSLeila Ghaffari 
4222b916ea7SJeremy L Thompson   PetscCall(TSCreate(comm, ts));
4232b916ea7SJeremy L Thompson   PetscCall(TSSetDM(*ts, dm));
424a515125bSLeila Ghaffari   if (phys->implicit) {
4252b916ea7SJeremy L Thompson     PetscCall(TSSetType(*ts, TSBDF));
426a515125bSLeila Ghaffari     if (user->op_ifunction) {
4272b916ea7SJeremy L Thompson       PetscCall(TSSetIFunction(*ts, NULL, IFunction_NS, &user));
428a515125bSLeila Ghaffari     } else {  // Implicit integrators can fall back to using an RHSFunction
4292b916ea7SJeremy L Thompson       PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user));
430a515125bSLeila Ghaffari     }
431f0b65372SJed Brown     if (user->op_ijacobian) {
4322b916ea7SJeremy L Thompson       PetscCall(DMTSSetIJacobian(dm, FormIJacobian_NS, &user));
433b107fddaSJed Brown       if (app_ctx->amat_type) {
434b107fddaSJed Brown         Mat Pmat, Amat;
4352b916ea7SJeremy L Thompson         PetscCall(DMCreateMatrix(dm, &Pmat));
4362b916ea7SJeremy L Thompson         PetscCall(DMSetMatType(dm, app_ctx->amat_type));
4372b916ea7SJeremy L Thompson         PetscCall(DMCreateMatrix(dm, &Amat));
4382b916ea7SJeremy L Thompson         PetscCall(TSSetIJacobian(*ts, Amat, Pmat, NULL, NULL));
4392b916ea7SJeremy L Thompson         PetscCall(MatDestroy(&Amat));
4402b916ea7SJeremy L Thompson         PetscCall(MatDestroy(&Pmat));
441b107fddaSJed Brown       }
442f0b65372SJed Brown     }
443a515125bSLeila Ghaffari   } else {
4442b916ea7SJeremy L Thompson     if (!user->op_rhs) SETERRQ(comm, PETSC_ERR_ARG_NULL, "Problem does not provide RHSFunction");
4452b916ea7SJeremy L Thompson     PetscCall(TSSetType(*ts, TSRK));
4462b916ea7SJeremy L Thompson     PetscCall(TSRKSetType(*ts, TSRK5F));
4472b916ea7SJeremy L Thompson     PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user));
448a515125bSLeila Ghaffari   }
4492b916ea7SJeremy L Thompson   PetscCall(TSSetMaxTime(*ts, 500. * user->units->second));
4502b916ea7SJeremy L Thompson   PetscCall(TSSetExactFinalTime(*ts, TS_EXACTFINALTIME_STEPOVER));
451f0784ed3SJed Brown   PetscCall(TSSetErrorIfStepFails(*ts, PETSC_FALSE));
4522b916ea7SJeremy L Thompson   PetscCall(TSSetTimeStep(*ts, 1.e-2 * user->units->second));
4530e1e9333SJames Wright   if (app_ctx->test_type != TESTTYPE_NONE) {
4542b916ea7SJeremy L Thompson     PetscCall(TSSetMaxSteps(*ts, 10));
4552b916ea7SJeremy L Thompson   }
4562b916ea7SJeremy L Thompson   PetscCall(TSGetAdapt(*ts, &adapt));
4572b916ea7SJeremy L Thompson   PetscCall(TSAdaptSetStepLimits(adapt, 1.e-12 * user->units->second, 1.e2 * user->units->second));
4582b916ea7SJeremy L Thompson   PetscCall(TSSetFromOptions(*ts));
459c996854bSJames Wright   user->time = user->time_bc_set = -1.0;  // require all BCs and ctx to be updated
460e2f84137SJeremy L Thompson   user->dt                       = -1.0;
461a515125bSLeila Ghaffari   if (!app_ctx->cont_steps) {  // print initial condition
4620e1e9333SJames Wright     if (app_ctx->test_type == TESTTYPE_NONE) {
4632b916ea7SJeremy L Thompson       PetscCall(TSMonitor_NS(*ts, 0, 0., *Q, user));
464a515125bSLeila Ghaffari     }
465a515125bSLeila Ghaffari   } else {  // continue from time of last output
466a515125bSLeila Ghaffari     PetscInt    count;
467a515125bSLeila Ghaffari     PetscViewer viewer;
4682b916ea7SJeremy L Thompson 
4699293eaa1SJed Brown     if (app_ctx->cont_time <= 0) {  // Legacy files did not include step number and time
4702b916ea7SJeremy L Thompson       PetscCall(PetscViewerBinaryOpen(comm, app_ctx->cont_time_file, FILE_MODE_READ, &viewer));
4719293eaa1SJed Brown       PetscCall(PetscViewerBinaryRead(viewer, &app_ctx->cont_time, 1, &count, PETSC_REAL));
4722b916ea7SJeremy L Thompson       PetscCall(PetscViewerDestroy(&viewer));
4739293eaa1SJed Brown       PetscCheck(app_ctx->cont_steps != -1, comm, PETSC_ERR_ARG_INCOMP,
4749293eaa1SJed Brown                  "-continue step number not specified, but checkpoint file does not contain a step number (likely written by older code version)");
4759293eaa1SJed Brown     }
4769293eaa1SJed Brown     PetscCall(TSSetTime(*ts, app_ctx->cont_time * user->units->second));
47774a6f4ddSJed Brown     PetscCall(TSSetStepNumber(*ts, app_ctx->cont_steps));
478a515125bSLeila Ghaffari   }
4790e1e9333SJames Wright   if (app_ctx->test_type == TESTTYPE_NONE) {
4802b916ea7SJeremy L Thompson     PetscCall(TSMonitorSet(*ts, TSMonitor_NS, user, NULL));
4810e1e9333SJames Wright   }
482c931fa59SJames Wright   if (app_ctx->turb_spanstats_enable) {
483b0488d1fSJames Wright     PetscCall(TSMonitorSet(*ts, TSMonitor_Statistics, user, NULL));
484b8daee98SJames Wright     CeedScalar previous_time = app_ctx->cont_time * user->units->second;
485b8daee98SJames Wright     CeedOperatorContextSetDouble(user->spanstats.op_stats_collect, user->spanstats.previous_time_label, &previous_time);
486b0488d1fSJames Wright   }
487a515125bSLeila Ghaffari 
488a515125bSLeila Ghaffari   // Solve
48974a6f4ddSJed Brown   PetscReal start_time;
49074a6f4ddSJed Brown   PetscInt  start_step;
4912b916ea7SJeremy L Thompson   PetscCall(TSGetTime(*ts, &start_time));
49274a6f4ddSJed Brown   PetscCall(TSGetStepNumber(*ts, &start_step));
49391982731SJeremy L Thompson 
49491982731SJeremy L Thompson   PetscPreLoadBegin(PETSC_FALSE, "Fluids Solve");
49591982731SJeremy L Thompson   PetscCall(TSSetTime(*ts, start_time));
49674a6f4ddSJed Brown   PetscCall(TSSetStepNumber(*ts, start_step));
49791982731SJeremy L Thompson   if (PetscPreLoadingOn) {
49891982731SJeremy L Thompson     // LCOV_EXCL_START
49991982731SJeremy L Thompson     SNES      snes;
50091982731SJeremy L Thompson     Vec       Q_preload;
50191982731SJeremy L Thompson     PetscReal rtol;
50291982731SJeremy L Thompson     PetscCall(VecDuplicate(*Q, &Q_preload));
50391982731SJeremy L Thompson     PetscCall(VecCopy(*Q, Q_preload));
50491982731SJeremy L Thompson     PetscCall(TSGetSNES(*ts, &snes));
50591982731SJeremy L Thompson     PetscCall(SNESGetTolerances(snes, NULL, &rtol, NULL, NULL, NULL));
5062b916ea7SJeremy L Thompson     PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, .99, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT));
50791982731SJeremy L Thompson     PetscCall(TSSetSolution(*ts, *Q));
50891982731SJeremy L Thompson     PetscCall(TSStep(*ts));
5092b916ea7SJeremy L Thompson     PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, rtol, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT));
51091982731SJeremy L Thompson     PetscCall(VecDestroy(&Q_preload));
51191982731SJeremy L Thompson     // LCOV_EXCL_STOP
51291982731SJeremy L Thompson   } else {
5132b916ea7SJeremy L Thompson     PetscCall(PetscBarrier((PetscObject)*ts));
5142b916ea7SJeremy L Thompson     PetscCall(TSSolve(*ts, *Q));
51591982731SJeremy L Thompson   }
51691982731SJeremy L Thompson   PetscPreLoadEnd();
51791982731SJeremy L Thompson 
51891982731SJeremy L Thompson   PetscCall(TSGetSolveTime(*ts, &final_time));
519a515125bSLeila Ghaffari   *f_time = final_time;
52091982731SJeremy L Thompson 
5210e1e9333SJames Wright   if (app_ctx->test_type == TESTTYPE_NONE) {
5227538d537SJames Wright     PetscInt step_no;
5237538d537SJames Wright     PetscCall(TSGetStepNumber(*ts, &step_no));
524b0488d1fSJames Wright     if (user->app_ctx->checkpoint_interval > 0 || user->app_ctx->checkpoint_interval == -1) {
5257538d537SJames Wright       PetscCall(WriteOutput(user, *Q, step_no, final_time));
5267538d537SJames Wright     }
5277538d537SJames Wright 
52891982731SJeremy L Thompson     PetscLogEvent stage_id;
52991982731SJeremy L Thompson     PetscStageLog stage_log;
53091982731SJeremy L Thompson 
53191982731SJeremy L Thompson     PetscCall(PetscLogStageGetId("Fluids Solve", &stage_id));
53291982731SJeremy L Thompson     PetscCall(PetscLogGetStageLog(&stage_log));
5332b916ea7SJeremy L Thompson     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Time taken for solution (sec): %g\n", stage_log->stageInfo[stage_id].perfInfo.time));
534a515125bSLeila Ghaffari   }
535a515125bSLeila Ghaffari   PetscFunctionReturn(0);
536a515125bSLeila Ghaffari }
537