xref: /honee/src/misc.c (revision e7754af56b6ae9480484d8c0a8eb1ad4f286face)
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 /// Miscellaneous utility functions
10a515125bSLeila Ghaffari 
11a515125bSLeila Ghaffari #include "../navierstokes.h"
129f59f36eSJames Wright #include "../qfunctions/mass.h"
13a515125bSLeila Ghaffari 
142b916ea7SJeremy L Thompson PetscErrorCode ICs_FixMultiplicity(DM dm, CeedData ceed_data, User user, Vec Q_loc, Vec Q, CeedScalar time) {
15a515125bSLeila Ghaffari   PetscFunctionBeginUser;
16a515125bSLeila Ghaffari 
17a515125bSLeila Ghaffari   // ---------------------------------------------------------------------------
18b7f03f12SJed Brown   // Update time for evaluation
19a515125bSLeila Ghaffari   // ---------------------------------------------------------------------------
20a5f46a7bSJeremy L Thompson   if (user->phys->ics_time_label) CeedOperatorSetContextDouble(ceed_data->op_ics, user->phys->ics_time_label, &time);
21a515125bSLeila Ghaffari 
22a515125bSLeila Ghaffari   // ---------------------------------------------------------------------------
23a515125bSLeila Ghaffari   // ICs
24a515125bSLeila Ghaffari   // ---------------------------------------------------------------------------
25a515125bSLeila Ghaffari   // -- CEED Restriction
26a515125bSLeila Ghaffari   CeedVector q0_ceed;
27a515125bSLeila Ghaffari   CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &q0_ceed, NULL);
28a515125bSLeila Ghaffari 
29a515125bSLeila Ghaffari   // -- Place PETSc vector in CEED vector
30a515125bSLeila Ghaffari   PetscMemType q0_mem_type;
31fd969b44SJames Wright   PetscCall(VecP2C(Q_loc, &q0_mem_type, q0_ceed));
32a515125bSLeila Ghaffari 
33a515125bSLeila Ghaffari   // -- Apply CEED Operator
342b916ea7SJeremy L Thompson   CeedOperatorApply(ceed_data->op_ics, ceed_data->x_coord, q0_ceed, CEED_REQUEST_IMMEDIATE);
35a515125bSLeila Ghaffari 
36a515125bSLeila Ghaffari   // -- Restore vectors
37fd969b44SJames Wright   PetscCall(VecC2P(q0_ceed, q0_mem_type, Q_loc));
38a515125bSLeila Ghaffari 
39a515125bSLeila Ghaffari   // -- Local-to-Global
402b916ea7SJeremy L Thompson   PetscCall(VecZeroEntries(Q));
412b916ea7SJeremy L Thompson   PetscCall(DMLocalToGlobal(dm, Q_loc, ADD_VALUES, Q));
42a515125bSLeila Ghaffari 
43a515125bSLeila Ghaffari   // ---------------------------------------------------------------------------
44a515125bSLeila Ghaffari   // Fix multiplicity for output of ICs
45a515125bSLeila Ghaffari   // ---------------------------------------------------------------------------
46a515125bSLeila Ghaffari   // -- CEED Restriction
47a515125bSLeila Ghaffari   CeedVector mult_vec;
48a515125bSLeila Ghaffari   CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &mult_vec, NULL);
49a515125bSLeila Ghaffari 
50a515125bSLeila Ghaffari   // -- Place PETSc vector in CEED vector
51a515125bSLeila Ghaffari   PetscMemType m_mem_type;
52a515125bSLeila Ghaffari   Vec          multiplicity_loc;
532b916ea7SJeremy L Thompson   PetscCall(DMGetLocalVector(dm, &multiplicity_loc));
54fd969b44SJames Wright   PetscCall(VecP2C(multiplicity_loc, &m_mem_type, mult_vec));
55a515125bSLeila Ghaffari 
56a515125bSLeila Ghaffari   // -- Get multiplicity
57a515125bSLeila Ghaffari   CeedElemRestrictionGetMultiplicity(ceed_data->elem_restr_q, mult_vec);
58a515125bSLeila Ghaffari 
59a515125bSLeila Ghaffari   // -- Restore vectors
60fd969b44SJames Wright   PetscCall(VecC2P(mult_vec, m_mem_type, multiplicity_loc));
61a515125bSLeila Ghaffari 
62a515125bSLeila Ghaffari   // -- Local-to-Global
63a515125bSLeila Ghaffari   Vec multiplicity;
642b916ea7SJeremy L Thompson   PetscCall(DMGetGlobalVector(dm, &multiplicity));
652b916ea7SJeremy L Thompson   PetscCall(VecZeroEntries(multiplicity));
662b916ea7SJeremy L Thompson   PetscCall(DMLocalToGlobal(dm, multiplicity_loc, ADD_VALUES, multiplicity));
67a515125bSLeila Ghaffari 
68a515125bSLeila Ghaffari   // -- Fix multiplicity
692b916ea7SJeremy L Thompson   PetscCall(VecPointwiseDivide(Q, Q, multiplicity));
702b916ea7SJeremy L Thompson   PetscCall(VecPointwiseDivide(Q_loc, Q_loc, multiplicity_loc));
71a515125bSLeila Ghaffari 
72a515125bSLeila Ghaffari   // -- Restore vectors
732b916ea7SJeremy L Thompson   PetscCall(DMRestoreLocalVector(dm, &multiplicity_loc));
742b916ea7SJeremy L Thompson   PetscCall(DMRestoreGlobalVector(dm, &multiplicity));
75a515125bSLeila Ghaffari 
76a515125bSLeila Ghaffari   // Cleanup
77a515125bSLeila Ghaffari   CeedVectorDestroy(&mult_vec);
78a515125bSLeila Ghaffari   CeedVectorDestroy(&q0_ceed);
79a515125bSLeila Ghaffari 
80a515125bSLeila Ghaffari   PetscFunctionReturn(0);
81a515125bSLeila Ghaffari }
82a515125bSLeila Ghaffari 
832b916ea7SJeremy L Thompson PetscErrorCode DMPlexInsertBoundaryValues_NS(DM dm, PetscBool insert_essential, Vec Q_loc, PetscReal time, Vec face_geom_FVM, Vec cell_geom_FVM,
842b916ea7SJeremy L Thompson                                              Vec grad_FVM) {
859d437337SJames Wright   Vec Qbc, boundary_mask;
86a515125bSLeila Ghaffari   PetscFunctionBegin;
87a515125bSLeila Ghaffari 
889d437337SJames Wright   // Mask (zero) Dirichlet entries
899d437337SJames Wright   PetscCall(DMGetNamedLocalVector(dm, "boundary mask", &boundary_mask));
909d437337SJames Wright   PetscCall(VecPointwiseMult(Q_loc, Q_loc, boundary_mask));
919d437337SJames Wright   PetscCall(DMRestoreNamedLocalVector(dm, "boundary mask", &boundary_mask));
929d437337SJames Wright 
932b916ea7SJeremy L Thompson   PetscCall(DMGetNamedLocalVector(dm, "Qbc", &Qbc));
942b916ea7SJeremy L Thompson   PetscCall(VecAXPY(Q_loc, 1., Qbc));
952b916ea7SJeremy L Thompson   PetscCall(DMRestoreNamedLocalVector(dm, "Qbc", &Qbc));
96a515125bSLeila Ghaffari 
97a515125bSLeila Ghaffari   PetscFunctionReturn(0);
98a515125bSLeila Ghaffari }
99a515125bSLeila Ghaffari 
100*e7754af5SKenneth E. Jansen // @brief Load vector from binary file, possibly with embedded solution time and step number
101*e7754af5SKenneth E. Jansen PetscErrorCode LoadFluidsBinaryVec(MPI_Comm comm, PetscViewer viewer, Vec Q, PetscReal *time, PetscInt *step_number) {
102*e7754af5SKenneth E. Jansen   PetscInt  token, file_step_number;
103*e7754af5SKenneth E. Jansen   PetscReal file_time;
104*e7754af5SKenneth E. Jansen   PetscFunctionBeginUser;
105*e7754af5SKenneth E. Jansen 
106*e7754af5SKenneth E. Jansen   // Attempt
107*e7754af5SKenneth E. Jansen   PetscCall(PetscViewerBinaryRead(viewer, &token, 1, NULL, PETSC_INT));
108*e7754af5SKenneth E. Jansen   if (token == FLUIDS_FILE_TOKEN) {  // New style format; we're reading a file with step number and time in the header
109*e7754af5SKenneth E. Jansen     PetscCall(PetscViewerBinaryRead(viewer, &file_step_number, 1, NULL, PETSC_INT));
110*e7754af5SKenneth E. Jansen     PetscCall(PetscViewerBinaryRead(viewer, &file_time, 1, NULL, PETSC_REAL));
111*e7754af5SKenneth E. Jansen     if (time) *time = file_time;
112*e7754af5SKenneth E. Jansen     if (step_number) *step_number = file_step_number;
113*e7754af5SKenneth E. Jansen   } else if (token == VEC_FILE_CLASSID) {  // Legacy format of just the vector, encoded as [VEC_FILE_CLASSID, length, ]
114*e7754af5SKenneth E. Jansen     PetscInt length, N;
115*e7754af5SKenneth E. Jansen     PetscCall(PetscViewerBinaryRead(viewer, &length, 1, NULL, PETSC_INT));
116*e7754af5SKenneth E. Jansen     PetscCall(VecGetSize(Q, &N));
117*e7754af5SKenneth E. Jansen     PetscCheck(length == N, comm, PETSC_ERR_ARG_INCOMP, "File Vec has length %" PetscInt_FMT " but DM has global Vec size %" PetscInt_FMT, length, N);
118*e7754af5SKenneth E. Jansen     PetscCall(PetscViewerBinarySetSkipHeader(viewer, PETSC_TRUE));
119*e7754af5SKenneth E. Jansen   } else SETERRQ(comm, PETSC_ERR_FILE_UNEXPECTED, "Not a fluids header token or a PETSc Vec in file");
120*e7754af5SKenneth E. Jansen 
121*e7754af5SKenneth E. Jansen   // Load Q from existent solution
122*e7754af5SKenneth E. Jansen   PetscCall(VecLoad(Q, viewer));
123*e7754af5SKenneth E. Jansen 
124*e7754af5SKenneth E. Jansen   PetscFunctionReturn(0);
125*e7754af5SKenneth E. Jansen }
126*e7754af5SKenneth E. Jansen 
127a515125bSLeila Ghaffari // Compare reference solution values with current test run for CI
128a515125bSLeila Ghaffari PetscErrorCode RegressionTests_NS(AppCtx app_ctx, Vec Q) {
129a515125bSLeila Ghaffari   Vec         Qref;
130a515125bSLeila Ghaffari   PetscViewer viewer;
131a515125bSLeila Ghaffari   PetscReal   error, Qrefnorm;
132*e7754af5SKenneth E. Jansen   MPI_Comm    comm = PetscObjectComm((PetscObject)Q);
133a515125bSLeila Ghaffari   PetscFunctionBegin;
134a515125bSLeila Ghaffari 
135a515125bSLeila Ghaffari   // Read reference file
1362b916ea7SJeremy L Thompson   PetscCall(VecDuplicate(Q, &Qref));
137*e7754af5SKenneth E. Jansen   PetscCall(PetscViewerBinaryOpen(comm, app_ctx->test_file_path, FILE_MODE_READ, &viewer));
138*e7754af5SKenneth E. Jansen   PetscCall(LoadFluidsBinaryVec(comm, viewer, Qref, NULL, NULL));
139a515125bSLeila Ghaffari 
140a515125bSLeila Ghaffari   // Compute error with respect to reference solution
1412b916ea7SJeremy L Thompson   PetscCall(VecAXPY(Q, -1.0, Qref));
1422b916ea7SJeremy L Thompson   PetscCall(VecNorm(Qref, NORM_MAX, &Qrefnorm));
1432b916ea7SJeremy L Thompson   PetscCall(VecScale(Q, 1. / Qrefnorm));
1442b916ea7SJeremy L Thompson   PetscCall(VecNorm(Q, NORM_MAX, &error));
145a515125bSLeila Ghaffari 
146a515125bSLeila Ghaffari   // Check error
147a515125bSLeila Ghaffari   if (error > app_ctx->test_tol) {
1482b916ea7SJeremy L Thompson     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test failed with error norm %g\n", (double)error));
149a515125bSLeila Ghaffari   }
150a515125bSLeila Ghaffari 
151a515125bSLeila Ghaffari   // Cleanup
1522b916ea7SJeremy L Thompson   PetscCall(PetscViewerDestroy(&viewer));
1532b916ea7SJeremy L Thompson   PetscCall(VecDestroy(&Qref));
154a515125bSLeila Ghaffari 
155a515125bSLeila Ghaffari   PetscFunctionReturn(0);
156a515125bSLeila Ghaffari }
157a515125bSLeila Ghaffari 
158a515125bSLeila Ghaffari // Get error for problems with exact solutions
1592b916ea7SJeremy L Thompson PetscErrorCode GetError_NS(CeedData ceed_data, DM dm, User user, Vec Q, PetscScalar final_time) {
160a515125bSLeila Ghaffari   PetscInt  loc_nodes;
161a515125bSLeila Ghaffari   Vec       Q_exact, Q_exact_loc;
162a515125bSLeila Ghaffari   PetscReal rel_error, norm_error, norm_exact;
163a515125bSLeila Ghaffari   PetscFunctionBegin;
164a515125bSLeila Ghaffari 
165a515125bSLeila Ghaffari   // Get exact solution at final time
1662b916ea7SJeremy L Thompson   PetscCall(DMCreateGlobalVector(dm, &Q_exact));
1672b916ea7SJeremy L Thompson   PetscCall(DMGetLocalVector(dm, &Q_exact_loc));
1682b916ea7SJeremy L Thompson   PetscCall(VecGetSize(Q_exact_loc, &loc_nodes));
1692b916ea7SJeremy L Thompson   PetscCall(ICs_FixMultiplicity(dm, ceed_data, user, Q_exact_loc, Q_exact, final_time));
170a515125bSLeila Ghaffari 
171a515125bSLeila Ghaffari   // Get |exact solution - obtained solution|
1722b916ea7SJeremy L Thompson   PetscCall(VecNorm(Q_exact, NORM_1, &norm_exact));
1732b916ea7SJeremy L Thompson   PetscCall(VecAXPY(Q, -1.0, Q_exact));
1742b916ea7SJeremy L Thompson   PetscCall(VecNorm(Q, NORM_1, &norm_error));
175a515125bSLeila Ghaffari 
176a515125bSLeila Ghaffari   // Compute relative error
177a515125bSLeila Ghaffari   rel_error = norm_error / norm_exact;
178a515125bSLeila Ghaffari 
179a515125bSLeila Ghaffari   // Output relative error
1802b916ea7SJeremy L Thompson   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Relative Error: %g\n", (double)rel_error));
181a515125bSLeila Ghaffari   // Cleanup
1822b916ea7SJeremy L Thompson   PetscCall(DMRestoreLocalVector(dm, &Q_exact_loc));
1832b916ea7SJeremy L Thompson   PetscCall(VecDestroy(&Q_exact));
184a515125bSLeila Ghaffari 
185a515125bSLeila Ghaffari   PetscFunctionReturn(0);
186a515125bSLeila Ghaffari }
187a515125bSLeila Ghaffari 
188a515125bSLeila Ghaffari // Post-processing
1892b916ea7SJeremy L Thompson PetscErrorCode PostProcess_NS(TS ts, CeedData ceed_data, DM dm, ProblemData *problem, User user, Vec Q, PetscScalar final_time) {
190a515125bSLeila Ghaffari   PetscInt          steps;
191f0784ed3SJed Brown   TSConvergedReason reason;
192a515125bSLeila Ghaffari   PetscFunctionBegin;
193a515125bSLeila Ghaffari 
194a515125bSLeila Ghaffari   // Print relative error
1950e1e9333SJames Wright   if (problem->non_zero_time && user->app_ctx->test_type == TESTTYPE_NONE) {
1962b916ea7SJeremy L Thompson     PetscCall(GetError_NS(ceed_data, dm, user, Q, final_time));
197a515125bSLeila Ghaffari   }
198a515125bSLeila Ghaffari 
199a515125bSLeila Ghaffari   // Print final time and number of steps
2002b916ea7SJeremy L Thompson   PetscCall(TSGetStepNumber(ts, &steps));
201f0784ed3SJed Brown   PetscCall(TSGetConvergedReason(ts, &reason));
2020e1e9333SJames Wright   if (user->app_ctx->test_type == TESTTYPE_NONE) {
203f0784ed3SJed Brown     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Time integrator %s on time step %" PetscInt_FMT " with final time %g\n", TSConvergedReasons[reason],
204f0784ed3SJed Brown                           steps, (double)final_time));
205a515125bSLeila Ghaffari   }
206a515125bSLeila Ghaffari 
207a515125bSLeila Ghaffari   // Output numerical values from command line
2082b916ea7SJeremy L Thompson   PetscCall(VecViewFromOptions(Q, NULL, "-vec_view"));
209a515125bSLeila Ghaffari 
210a515125bSLeila Ghaffari   // Compare reference solution values with current test run for CI
2110e1e9333SJames Wright   if (user->app_ctx->test_type == TESTTYPE_SOLVER) {
2122b916ea7SJeremy L Thompson     PetscCall(RegressionTests_NS(user->app_ctx, Q));
213a515125bSLeila Ghaffari   }
214a515125bSLeila Ghaffari 
215a515125bSLeila Ghaffari   PetscFunctionReturn(0);
216a515125bSLeila Ghaffari }
217a515125bSLeila Ghaffari 
2189293eaa1SJed Brown const PetscInt FLUIDS_FILE_TOKEN = 0xceedf00;
2199293eaa1SJed Brown 
220a515125bSLeila Ghaffari // Gather initial Q values in case of continuation of simulation
221a515125bSLeila Ghaffari PetscErrorCode SetupICsFromBinary(MPI_Comm comm, AppCtx app_ctx, Vec Q) {
222a515125bSLeila Ghaffari   PetscViewer viewer;
2232b916ea7SJeremy L Thompson 
224a515125bSLeila Ghaffari   PetscFunctionBegin;
225a515125bSLeila Ghaffari 
2262b916ea7SJeremy L Thompson   PetscCall(PetscViewerBinaryOpen(comm, app_ctx->cont_file, FILE_MODE_READ, &viewer));
227*e7754af5SKenneth E. Jansen   PetscCall(LoadFluidsBinaryVec(comm, viewer, Q, &app_ctx->cont_time, &app_ctx->cont_steps));
2282b916ea7SJeremy L Thompson   PetscCall(PetscViewerDestroy(&viewer));
229a515125bSLeila Ghaffari 
230a515125bSLeila Ghaffari   PetscFunctionReturn(0);
231a515125bSLeila Ghaffari }
232a515125bSLeila Ghaffari 
233a515125bSLeila Ghaffari // Record boundary values from initial condition
234a515125bSLeila Ghaffari PetscErrorCode SetBCsFromICs_NS(DM dm, Vec Q, Vec Q_loc) {
2359d437337SJames Wright   Vec Qbc, boundary_mask;
236a515125bSLeila Ghaffari   PetscFunctionBegin;
237a515125bSLeila Ghaffari 
2382b916ea7SJeremy L Thompson   PetscCall(DMGetNamedLocalVector(dm, "Qbc", &Qbc));
2392b916ea7SJeremy L Thompson   PetscCall(VecCopy(Q_loc, Qbc));
2402b916ea7SJeremy L Thompson   PetscCall(VecZeroEntries(Q_loc));
2412b916ea7SJeremy L Thompson   PetscCall(DMGlobalToLocal(dm, Q, INSERT_VALUES, Q_loc));
2422b916ea7SJeremy L Thompson   PetscCall(VecAXPY(Qbc, -1., Q_loc));
2432b916ea7SJeremy L Thompson   PetscCall(DMRestoreNamedLocalVector(dm, "Qbc", &Qbc));
2442b916ea7SJeremy L Thompson   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBoundaryValues_C", DMPlexInsertBoundaryValues_NS));
245a515125bSLeila Ghaffari 
2469d437337SJames Wright   PetscCall(DMGetNamedLocalVector(dm, "boundary mask", &boundary_mask));
2479d437337SJames Wright   PetscCall(DMGetGlobalVector(dm, &Q));
2489d437337SJames Wright   PetscCall(VecZeroEntries(boundary_mask));
2499d437337SJames Wright   PetscCall(VecSet(Q, 1.0));
2509d437337SJames Wright   PetscCall(DMGlobalToLocal(dm, Q, INSERT_VALUES, boundary_mask));
2519d437337SJames Wright   PetscCall(DMRestoreNamedLocalVector(dm, "boundary mask", &boundary_mask));
2529d437337SJames Wright 
253a515125bSLeila Ghaffari   PetscFunctionReturn(0);
254a515125bSLeila Ghaffari }
25515a3537eSJed Brown 
25615a3537eSJed Brown // Free a plain data context that was allocated using PETSc; returning libCEED error codes
25715a3537eSJed Brown int FreeContextPetsc(void *data) {
2582b916ea7SJeremy L Thompson   if (PetscFree(data)) return CeedError(NULL, CEED_ERROR_ACCESS, "PetscFree failed");
25915a3537eSJed Brown   return CEED_ERROR_SUCCESS;
26015a3537eSJed Brown }
2619f59f36eSJames Wright 
2629f59f36eSJames Wright // Return mass qfunction specification for number of components N
2639f59f36eSJames Wright PetscErrorCode CreateMassQFunction(Ceed ceed, CeedInt N, CeedInt q_data_size, CeedQFunction *qf) {
2649f59f36eSJames Wright   CeedQFunctionUser qfunction_ptr;
2659f59f36eSJames Wright   const char       *qfunction_loc;
2669f59f36eSJames Wright   PetscFunctionBeginUser;
2679f59f36eSJames Wright 
2689f59f36eSJames Wright   switch (N) {
2699f59f36eSJames Wright     case 1:
2709f59f36eSJames Wright       qfunction_ptr = Mass_1;
2719f59f36eSJames Wright       qfunction_loc = Mass_1_loc;
2729f59f36eSJames Wright       break;
2739f59f36eSJames Wright     case 5:
2749f59f36eSJames Wright       qfunction_ptr = Mass_5;
2759f59f36eSJames Wright       qfunction_loc = Mass_5_loc;
2769f59f36eSJames Wright       break;
2779f59f36eSJames Wright     case 9:
2789f59f36eSJames Wright       qfunction_ptr = Mass_9;
2799f59f36eSJames Wright       qfunction_loc = Mass_9_loc;
2809f59f36eSJames Wright       break;
2819f59f36eSJames Wright     case 22:
2829f59f36eSJames Wright       qfunction_ptr = Mass_22;
2839f59f36eSJames Wright       qfunction_loc = Mass_22_loc;
2849f59f36eSJames Wright       break;
2859f59f36eSJames Wright     default:
2869f59f36eSJames Wright       SETERRQ(PETSC_COMM_WORLD, -1, "Could not find mass qfunction of size %d", N);
2879f59f36eSJames Wright   }
2889f59f36eSJames Wright   CeedQFunctionCreateInterior(ceed, 1, qfunction_ptr, qfunction_loc, qf);
2899f59f36eSJames Wright 
2909f59f36eSJames Wright   CeedQFunctionAddInput(*qf, "u", N, CEED_EVAL_INTERP);
2919f59f36eSJames Wright   CeedQFunctionAddInput(*qf, "qdata", q_data_size, CEED_EVAL_NONE);
2929f59f36eSJames Wright   CeedQFunctionAddOutput(*qf, "v", N, CEED_EVAL_INTERP);
2939f59f36eSJames Wright   PetscFunctionReturn(0);
2949f59f36eSJames Wright }
295e5e81594SJames Wright 
296e5e81594SJames Wright /* @brief L^2 Projection of a source FEM function to a target FEM space
297e5e81594SJames Wright  *
298e5e81594SJames Wright  * To solve system using a lumped mass matrix, pass a KSP object with ksp_type=preonly, pc_type=jacobi, pc_jacobi_type=rowsum.
299e5e81594SJames Wright  *
300e5e81594SJames Wright  * @param[in]  source_vec    Global Vec of the source FEM function. NULL indicates using rhs_matop_ctx->X_loc
301e5e81594SJames Wright  * @param[out] target_vec    Global Vec of the target (result) FEM function. NULL indicates using rhs_matop_ctx->Y_loc
302e5e81594SJames Wright  * @param[in]  rhs_matop_ctx MatopApplyContext for performing the RHS evaluation
303e5e81594SJames Wright  * @param[in]  ksp           KSP for solving the consistent projection problem
304e5e81594SJames Wright  */
305e5e81594SJames Wright PetscErrorCode ComputeL2Projection(Vec source_vec, Vec target_vec, MatopApplyContext rhs_matop_ctx, KSP ksp) {
306e5e81594SJames Wright   PetscFunctionBeginUser;
307e5e81594SJames Wright 
308e5e81594SJames Wright   PetscCall(ApplyLocal_Ceed(source_vec, target_vec, rhs_matop_ctx));
309e5e81594SJames Wright   PetscCall(KSPSolve(ksp, target_vec, target_vec));
310e5e81594SJames Wright 
311e5e81594SJames Wright   PetscFunctionReturn(0);
312e5e81594SJames Wright }
313