xref: /honee/src/diff_flux_projection.c (revision b85c1465ed3825a918e697345c07d6c515cbe7de)
1 // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2 // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
3 /// @file
4 /// Functions for setting up and projecting the divergence of the diffusive flux
5 
6 #include "../qfunctions/diff_flux_projection.h"
7 
8 #include <petscdmplex.h>
9 
10 #include <navierstokes.h>
11 
12 /**
13   @brief Create `DivDiffFluxProjectionData` for solution DM in `honee`
14 
15   @param[in]  honee               `Honee` context
16   @param[in]  num_diff_flux_comps Number of components that makes up the diffusive flux (e.g. 1 for scalar advection-diffusion)
17   @param[out] pdiff_flux_proj     The `DivDiffFluxProjectionData` object created, or `NULL` if not created
18 **/
19 PetscErrorCode DivDiffFluxProjectionCreate(Honee honee, PetscInt num_diff_flux_comps, DivDiffFluxProjectionData *pdiff_flux_proj) {
20   PetscInt                  label_value = 0, height = 0, dm_field = 0, dim, degree = honee->app_ctx->degree, q_extra = honee->app_ctx->q_extra;
21   DMLabel                   domain_label = NULL;
22   DivDiffFluxProjectionData diff_flux_proj;
23   NodalProjectionData       projection;
24 
25   PetscFunctionBeginUser;
26   if (honee->app_ctx->divFdiffproj_method == DIV_DIFF_FLUX_PROJ_NONE) {
27     *pdiff_flux_proj = NULL;
28     PetscFunctionReturn(PETSC_SUCCESS);
29   }
30   PetscCall(PetscNew(pdiff_flux_proj));
31   diff_flux_proj = *pdiff_flux_proj;
32   PetscCall(PetscNew(&honee->diff_flux_proj->projection));
33   projection                          = honee->diff_flux_proj->projection;
34   diff_flux_proj->method              = honee->app_ctx->divFdiffproj_method;
35   diff_flux_proj->num_diff_flux_comps = num_diff_flux_comps;
36 
37   PetscCall(DMClone(honee->dm, &projection->dm));
38   PetscCall(DMSetMatrixPreallocateSkip(projection->dm, PETSC_TRUE));
39   PetscCall(DMGetDimension(projection->dm, &dim));
40   switch (diff_flux_proj->method) {
41     case DIV_DIFF_FLUX_PROJ_DIRECT: {
42       projection->num_comp = diff_flux_proj->num_diff_flux_comps;
43       PetscCall(PetscObjectSetName((PetscObject)projection->dm, "DivDiffFluxProj"));
44       PetscCall(DMSetupByOrder_FEM(PETSC_TRUE, PETSC_TRUE, degree, 1, q_extra, 1, &projection->num_comp, projection->dm));
45 
46       PetscCall(DMPlexCeedElemRestrictionCreate(honee->ceed, projection->dm, domain_label, label_value, height, dm_field,
47                                                 &diff_flux_proj->elem_restr_div_diff_flux));
48       PetscCallCeed(honee->ceed,
49                     CeedElemRestrictionCreateVector(diff_flux_proj->elem_restr_div_diff_flux, &diff_flux_proj->div_diff_flux_ceed, NULL));
50       PetscCall(CreateBasisFromPlex(honee->ceed, projection->dm, domain_label, label_value, height, dm_field, &diff_flux_proj->basis_div_diff_flux));
51       diff_flux_proj->eval_mode_div_diff_flux = CEED_EVAL_INTERP;
52 
53       {  // Create face labels on projection->dm for boundary integrals
54         DMLabel  face_sets_label;
55         PetscInt num_face_set_values, *face_set_values;
56 
57         PetscCall(DMGetLabel(honee->dm, "Face Sets", &face_sets_label));
58         PetscCall(DMLabelCreateGlobalValueArray(honee->dm, face_sets_label, &num_face_set_values, &face_set_values));
59         for (PetscInt f = 0; f < num_face_set_values; f++) {
60           DMLabel face_orientation_label;
61           char   *face_orientation_label_name;
62 
63           PetscCall(DMPlexCreateFaceLabel(honee->dm, face_set_values[f], &face_orientation_label_name));
64           PetscCall(DMGetLabel(honee->dm, face_orientation_label_name, &face_orientation_label));
65           PetscCall(DMAddLabel(projection->dm, face_orientation_label));
66           PetscCall(PetscFree(face_orientation_label_name));
67         }
68         PetscCall(PetscFree(face_set_values));
69       }
70     } break;
71     case DIV_DIFF_FLUX_PROJ_INDIRECT: {
72       projection->num_comp = diff_flux_proj->num_diff_flux_comps * dim;
73       PetscCall(PetscObjectSetName((PetscObject)projection->dm, "DiffFluxProj"));
74       PetscCall(DMSetupByOrder_FEM(PETSC_TRUE, PETSC_TRUE, degree, 1, q_extra, 1, &projection->num_comp, projection->dm));
75 
76       PetscCall(DMPlexCeedElemRestrictionQDataCreate(honee->ceed, projection->dm, domain_label, label_value, height,
77                                                      diff_flux_proj->num_diff_flux_comps, &diff_flux_proj->elem_restr_div_diff_flux));
78       PetscCallCeed(honee->ceed,
79                     CeedElemRestrictionCreateVector(diff_flux_proj->elem_restr_div_diff_flux, &diff_flux_proj->div_diff_flux_ceed, NULL));
80       diff_flux_proj->basis_div_diff_flux     = CEED_BASIS_NONE;
81       diff_flux_proj->eval_mode_div_diff_flux = CEED_EVAL_NONE;
82     } break;
83     case DIV_DIFF_FLUX_PROJ_NONE:
84       SETERRQ(PetscObjectComm((PetscObject)honee->dm), PETSC_ERR_ARG_WRONG, "Should not reach here with div_diff_flux_projection_method %s",
85               DivDiffFluxProjectionMethods[honee->app_ctx->divFdiffproj_method]);
86       break;
87   }
88   PetscFunctionReturn(PETSC_SUCCESS);
89 };
90 
91 /**
92   @brief Return the objects required for the Divergence of Diffusive flux to be read by a `CeedOperator`
93 
94   @param[in]  diff_flux_proj Projection object
95   @param[out] elem_restr     Element restriction for the divergence of diffusive flux, or `NULL`
96   @param[out] basis          Basis for the divergence of diffusive flux, or `NULL`
97   @param[out] vector         Vector for the divergence of diffusive flux, or `NULL`
98   @param[out] eval_mode      Eval mode for the divergence of diffusive flux, or `NULL`
99 **/
100 PetscErrorCode DivDiffFluxProjectionGetOperatorFieldData(DivDiffFluxProjectionData diff_flux_proj, CeedElemRestriction *elem_restr, CeedBasis *basis,
101                                                          CeedVector *vector, CeedEvalMode *eval_mode) {
102   Ceed ceed = CeedVectorReturnCeed(diff_flux_proj->div_diff_flux_ceed);
103 
104   PetscFunctionBeginUser;
105   if (elem_restr) PetscCallCeed(ceed, CeedElemRestrictionReferenceCopy(diff_flux_proj->elem_restr_div_diff_flux, elem_restr));
106   if (basis) PetscCallCeed(ceed, CeedBasisReferenceCopy(diff_flux_proj->basis_div_diff_flux, basis));
107   if (vector) PetscCallCeed(ceed, CeedVectorReferenceCopy(diff_flux_proj->div_diff_flux_ceed, vector));
108   if (eval_mode) *eval_mode = diff_flux_proj->eval_mode_div_diff_flux;
109   PetscFunctionReturn(PETSC_SUCCESS);
110 }
111 
112 /**
113   @brief Setup direct projection of divergence of diffusive flux
114 
115   @param[in]     honee          `Honee` context
116   @param[in,out] diff_flux_proj Flux projection object to setup
117 **/
118 static PetscErrorCode DivDiffFluxProjectionSetup_Direct(Honee honee, DivDiffFluxProjectionData diff_flux_proj) {
119   Ceed                ceed       = honee->ceed;
120   NodalProjectionData projection = diff_flux_proj->projection;
121   MPI_Comm            comm       = PetscObjectComm((PetscObject)projection->dm);
122 
123   PetscFunctionBeginUser;
124   {  // Create Projection RHS OperatorApplyContext
125     CeedOperator op_rhs;
126 
127     PetscCheck(diff_flux_proj->CreateRHSOperator_Direct, comm, PETSC_ERR_ARG_WRONGSTATE,
128                "Must define CreateRHSOperator_Direct to use indirect div_diff_flux projection");
129     PetscCall(diff_flux_proj->CreateRHSOperator_Direct(honee, diff_flux_proj, &op_rhs));
130     PetscCall(DMCreateLocalVector(projection->dm, &diff_flux_proj->DivDiffFlux_loc));
131     diff_flux_proj->ceed_vec_has_array = PETSC_FALSE;
132     PetscCall(OperatorApplyContextCreate(honee->dm, projection->dm, ceed, op_rhs, NULL, NULL, NULL, diff_flux_proj->DivDiffFlux_loc,
133                                          &projection->l2_rhs_ctx));
134     PetscCallCeed(ceed, CeedOperatorDestroy(&op_rhs));
135   }
136 
137   {  // -- Build Mass operator
138     CeedQFunction       qf_mass;
139     CeedOperator        op_mass;
140     CeedBasis           basis_div_diff_flux             = NULL;
141     CeedElemRestriction elem_restr_div_diff_flux_volume = NULL, elem_restr_qd;
142     CeedVector          q_data;
143     CeedInt             q_data_size;
144     PetscInt            label_value  = 0;
145     DMLabel             domain_label = NULL;
146 
147     PetscCall(DivDiffFluxProjectionGetOperatorFieldData(diff_flux_proj, &elem_restr_div_diff_flux_volume, &basis_div_diff_flux, NULL, NULL));
148     PetscCall(QDataGet(ceed, projection->dm, domain_label, label_value, honee->elem_restr_x, honee->basis_x, honee->x_coord, &elem_restr_qd, &q_data,
149                        &q_data_size));
150 
151     PetscCall(CreateMassQFunction(ceed, projection->num_comp, q_data_size, &qf_mass));
152     PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass));
153     PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "u", elem_restr_div_diff_flux_volume, basis_div_diff_flux, CEED_VECTOR_ACTIVE));
154     PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "qdata", elem_restr_qd, CEED_BASIS_NONE, q_data));
155     PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "v", elem_restr_div_diff_flux_volume, basis_div_diff_flux, CEED_VECTOR_ACTIVE));
156 
157     {  // -- Setup KSP for L^2 projection
158       Mat mat_mass;
159 
160       PetscCall(MatCreateCeed(projection->dm, projection->dm, op_mass, NULL, &mat_mass));
161 
162       PetscCall(KSPCreate(comm, &projection->ksp));
163       PetscCall(KSPSetOptionsPrefix(projection->ksp, "div_diff_flux_projection_"));
164       {  // lumped by default
165         PC pc;
166         PetscCall(KSPGetPC(projection->ksp, &pc));
167         PetscCall(PCSetType(pc, PCJACOBI));
168         PetscCall(PCJacobiSetType(pc, PC_JACOBI_ROWSUM));
169         PetscCall(KSPSetType(projection->ksp, KSPPREONLY));
170       }
171       PetscCall(KSPSetFromOptions_WithMatCeed(projection->ksp, mat_mass));
172       PetscCall(MatDestroy(&mat_mass));
173     }
174 
175     PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_div_diff_flux_volume));
176     PetscCallCeed(ceed, CeedBasisDestroy(&basis_div_diff_flux));
177     PetscCallCeed(ceed, CeedVectorDestroy(&q_data));
178     PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_qd));
179     PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_mass));
180     PetscCallCeed(ceed, CeedOperatorDestroy(&op_mass));
181   }
182   PetscFunctionReturn(PETSC_SUCCESS);
183 }
184 
185 /**
186   @brief Setup indirect projection of divergence of diffusive flux
187 
188   @param[in]     honee          `Honee` context
189   @param[in,out] diff_flux_proj Flux projection object to setup
190 **/
191 static PetscErrorCode DivDiffFluxProjectionSetup_Indirect(Honee honee, DivDiffFluxProjectionData diff_flux_proj) {
192   Ceed                ceed       = honee->ceed;
193   NodalProjectionData projection = diff_flux_proj->projection;
194   CeedBasis           basis_diff_flux;
195   CeedElemRestriction elem_restr_diff_flux, elem_restr_qd;
196   CeedVector          q_data;
197   CeedInt             q_data_size;
198   MPI_Comm            comm = PetscObjectComm((PetscObject)projection->dm);
199 
200   PetscFunctionBeginUser;
201   {
202     PetscInt label_value = 0, height = 0, dm_field = 0;
203     DMLabel  domain_label = NULL;
204 
205     PetscCall(DMPlexCeedElemRestrictionCreate(ceed, projection->dm, domain_label, label_value, height, dm_field, &elem_restr_diff_flux));
206     PetscCall(CreateBasisFromPlex(ceed, projection->dm, domain_label, label_value, height, dm_field, &basis_diff_flux));
207     PetscCall(QDataGet(ceed, projection->dm, domain_label, label_value, honee->elem_restr_x, honee->basis_x, honee->x_coord, &elem_restr_qd, &q_data,
208                        &q_data_size));
209   }
210 
211   {
212     CeedOperator op_rhs;
213 
214     PetscCheck(diff_flux_proj->CreateRHSOperator_Indirect, comm, PETSC_ERR_ARG_WRONGSTATE,
215                "Must define CreateRHSOperator_Indirect to use indirect div_diff_flux projection");
216     PetscCall(diff_flux_proj->CreateRHSOperator_Indirect(honee, diff_flux_proj, &op_rhs));
217     PetscCall(OperatorApplyContextCreate(honee->dm, projection->dm, ceed, op_rhs, NULL, NULL, NULL, NULL, &projection->l2_rhs_ctx));
218     PetscCallCeed(ceed, CeedOperatorDestroy(&op_rhs));
219   }
220 
221   {  // -- Build Mass operator
222     CeedQFunction qf_mass;
223     CeedOperator  op_mass;
224 
225     PetscCall(CreateMassQFunction(ceed, projection->num_comp, q_data_size, &qf_mass));
226     PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass));
227     PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "u", elem_restr_diff_flux, basis_diff_flux, CEED_VECTOR_ACTIVE));
228     PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "qdata", elem_restr_qd, CEED_BASIS_NONE, q_data));
229     PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "v", elem_restr_diff_flux, basis_diff_flux, CEED_VECTOR_ACTIVE));
230 
231     {  // -- Setup KSP for L^2 projection
232       Mat mat_mass;
233 
234       PetscCall(MatCreateCeed(projection->dm, projection->dm, op_mass, NULL, &mat_mass));
235 
236       PetscCall(KSPCreate(comm, &projection->ksp));
237       PetscCall(KSPSetOptionsPrefix(projection->ksp, "div_diff_flux_projection_"));
238       {  // lumped by default
239         PC pc;
240         PetscCall(KSPGetPC(projection->ksp, &pc));
241         PetscCall(PCSetType(pc, PCJACOBI));
242         PetscCall(PCJacobiSetType(pc, PC_JACOBI_ROWSUM));
243         PetscCall(KSPSetType(projection->ksp, KSPPREONLY));
244       }
245       PetscCall(KSPSetFromOptions_WithMatCeed(projection->ksp, mat_mass));
246       PetscCall(MatDestroy(&mat_mass));
247     }
248     PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_mass));
249     PetscCallCeed(ceed, CeedOperatorDestroy(&op_mass));
250   }
251 
252   {  // Create OperatorApplyContext to calculate divergence at quadrature points
253     CeedQFunction       qf_calc_divergence = NULL;
254     CeedOperator        op_calc_divergence;
255     CeedElemRestriction elem_restr_div_diff_flux = NULL;
256     PetscInt            dim;
257 
258     PetscCall(DMGetDimension(projection->dm, &dim));
259     PetscCall(DivDiffFluxProjectionGetOperatorFieldData(diff_flux_proj, &elem_restr_div_diff_flux, NULL, NULL, NULL));
260 
261     switch (dim) {
262       case 2:
263         switch (diff_flux_proj->num_diff_flux_comps) {
264           case 1:
265             PetscCallCeed(ceed,
266                           CeedQFunctionCreateInterior(ceed, 1, ComputeDivDiffusiveFlux2D_1, ComputeDivDiffusiveFlux2D_1_loc, &qf_calc_divergence));
267             break;
268         }
269         break;
270       case 3:
271         switch (diff_flux_proj->num_diff_flux_comps) {
272           case 1:
273             PetscCallCeed(ceed,
274                           CeedQFunctionCreateInterior(ceed, 1, ComputeDivDiffusiveFlux3D_1, ComputeDivDiffusiveFlux3D_1_loc, &qf_calc_divergence));
275             break;
276           case 4:
277             PetscCallCeed(ceed,
278                           CeedQFunctionCreateInterior(ceed, 1, ComputeDivDiffusiveFlux3D_4, ComputeDivDiffusiveFlux3D_4_loc, &qf_calc_divergence));
279             break;
280         }
281         break;
282     }
283     PetscCheck(qf_calc_divergence, comm, PETSC_ERR_SUP,
284                "QFunction for calculating divergence of diffusive flux does not exist for"
285                " %" PetscInt_FMT " dimensional grid and %" PetscInt_FMT
286                " number of components.\nA new qfunction can be easily added; see source code for pattern.",
287                dim, diff_flux_proj->num_diff_flux_comps);
288 
289     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_calc_divergence, "Grad F_diff", projection->num_comp * dim, CEED_EVAL_GRAD));
290     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_calc_divergence, "qdata", q_data_size, CEED_EVAL_NONE));
291     PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_calc_divergence, "Div F_diff", diff_flux_proj->num_diff_flux_comps, CEED_EVAL_NONE));
292 
293     PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_calc_divergence, NULL, NULL, &op_calc_divergence));
294     PetscCallCeed(ceed, CeedOperatorSetField(op_calc_divergence, "Grad F_diff", elem_restr_diff_flux, basis_diff_flux, CEED_VECTOR_ACTIVE));
295     PetscCallCeed(ceed, CeedOperatorSetField(op_calc_divergence, "qdata", elem_restr_qd, CEED_BASIS_NONE, q_data));
296     PetscCallCeed(
297         ceed, CeedOperatorSetField(op_calc_divergence, "Div F_diff", elem_restr_div_diff_flux, CEED_BASIS_NONE, diff_flux_proj->div_diff_flux_ceed));
298 
299     PetscCall(
300         OperatorApplyContextCreate(projection->dm, NULL, ceed, op_calc_divergence, NULL, NULL, NULL, NULL, &diff_flux_proj->calc_div_diff_flux));
301 
302     PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_div_diff_flux));
303     PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_calc_divergence));
304     PetscCallCeed(ceed, CeedOperatorDestroy(&op_calc_divergence));
305   }
306   PetscCallCeed(ceed, CeedBasisDestroy(&basis_diff_flux));
307   PetscCallCeed(ceed, CeedVectorDestroy(&q_data));
308   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_qd));
309   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_diff_flux));
310   PetscFunctionReturn(PETSC_SUCCESS);
311 }
312 
313 /**
314   @brief Setup projection of divergence of diffusive flux
315 
316   @param[in]     honee          `Honee` context
317   @param[in,out] diff_flux_proj Flux projection object to setup
318 **/
319 PetscErrorCode DivDiffFluxProjectionSetup(Honee honee, DivDiffFluxProjectionData diff_flux_proj) {
320   PetscFunctionBeginUser;
321   switch (honee->app_ctx->divFdiffproj_method) {
322     case DIV_DIFF_FLUX_PROJ_DIRECT:
323       PetscCall(DivDiffFluxProjectionSetup_Direct(honee, diff_flux_proj));
324       break;
325     case DIV_DIFF_FLUX_PROJ_INDIRECT:
326       PetscCall(DivDiffFluxProjectionSetup_Indirect(honee, diff_flux_proj));
327       break;
328     case DIV_DIFF_FLUX_PROJ_NONE:
329       SETERRQ(PetscObjectComm((PetscObject)honee->dm), PETSC_ERR_ARG_WRONG, "Should not reach here with div_diff_flux_projection_method %s",
330               DivDiffFluxProjectionMethods[honee->app_ctx->divFdiffproj_method]);
331       break;
332   }
333   PetscFunctionReturn(PETSC_SUCCESS);
334 }
335 
336 /**
337   @brief Project the divergence of diffusive flux
338 
339   This implicitly sets the `CeedVector` input (`div_diff_flux_ceed`) to the divergence of diffusive flux.
340 
341   @param[in]  diff_flux_proj `NodalProjectionData` for the projection
342   @param[in]  Q_loc          Localized solution vector
343 **/
344 PetscErrorCode DivDiffFluxProjectionApply(DivDiffFluxProjectionData diff_flux_proj, Vec Q_loc) {
345   NodalProjectionData projection = diff_flux_proj->projection;
346 
347   PetscFunctionBeginUser;
348   PetscCall(PetscLogEventBegin(FLUIDS_DivDiffFluxProjection, Q_loc, 0, 0, 0));
349   switch (diff_flux_proj->method) {
350     case DIV_DIFF_FLUX_PROJ_DIRECT: {
351       Vec DivDiffFlux;
352 
353       PetscCall(DMGetGlobalVector(projection->dm, &DivDiffFlux));
354       if (diff_flux_proj->ceed_vec_has_array) {
355         PetscCall(VecReadCeedToPetsc(diff_flux_proj->div_diff_flux_ceed, diff_flux_proj->DivDiffFlux_memtype, diff_flux_proj->DivDiffFlux_loc));
356         diff_flux_proj->ceed_vec_has_array = PETSC_FALSE;
357       }
358       PetscCall(ApplyCeedOperatorLocalToGlobal(Q_loc, DivDiffFlux, projection->l2_rhs_ctx));
359       PetscCall(VecViewFromOptions(DivDiffFlux, NULL, "-div_diff_flux_projection_rhs_view"));
360 
361       PetscCall(KSPSolve(projection->ksp, DivDiffFlux, DivDiffFlux));
362       PetscCall(VecViewFromOptions(DivDiffFlux, NULL, "-div_diff_flux_projection_view"));
363 
364       PetscCall(DMGlobalToLocal(projection->dm, DivDiffFlux, INSERT_VALUES, diff_flux_proj->DivDiffFlux_loc));
365       PetscCall(VecReadPetscToCeed(diff_flux_proj->DivDiffFlux_loc, &diff_flux_proj->DivDiffFlux_memtype, diff_flux_proj->div_diff_flux_ceed));
366       diff_flux_proj->ceed_vec_has_array = PETSC_TRUE;
367 
368       PetscCall(DMRestoreGlobalVector(projection->dm, &DivDiffFlux));
369       break;
370     }
371     case DIV_DIFF_FLUX_PROJ_INDIRECT: {
372       Vec DiffFlux;
373 
374       PetscCall(DMGetGlobalVector(projection->dm, &DiffFlux));
375       PetscCall(ApplyCeedOperatorLocalToGlobal(Q_loc, DiffFlux, projection->l2_rhs_ctx));
376       PetscCall(VecViewFromOptions(DiffFlux, NULL, "-div_diff_flux_projection_rhs_view"));
377 
378       PetscCall(KSPSolve(projection->ksp, DiffFlux, DiffFlux));
379       PetscCall(VecViewFromOptions(DiffFlux, NULL, "-div_diff_flux_projection_view"));
380 
381       PetscCall(ApplyCeedOperatorGlobalToLocal(DiffFlux, NULL, diff_flux_proj->calc_div_diff_flux));
382       PetscCall(DMRestoreGlobalVector(projection->dm, &DiffFlux));
383     } break;
384     case DIV_DIFF_FLUX_PROJ_NONE:
385       SETERRQ(PetscObjectComm((PetscObject)projection->dm), PETSC_ERR_ARG_WRONG, "Should not reach here with div_diff_flux_projection_method %s",
386               DivDiffFluxProjectionMethods[diff_flux_proj->method]);
387       break;
388   }
389   PetscCall(PetscLogEventEnd(FLUIDS_DivDiffFluxProjection, Q_loc, 0, 0, 0));
390   PetscFunctionReturn(PETSC_SUCCESS);
391 }
392 
393 /**
394   @brief Destroy `DivDiffFluxProjectionData` object
395 
396   @param[in,out] diff_flux_proj Object to destroy
397 **/
398 PetscErrorCode DivDiffFluxProjectionDataDestroy(DivDiffFluxProjectionData diff_flux_proj) {
399   PetscFunctionBeginUser;
400   if (diff_flux_proj == NULL) PetscFunctionReturn(PETSC_SUCCESS);
401   Ceed ceed = CeedVectorReturnCeed(diff_flux_proj->div_diff_flux_ceed);
402 
403   PetscCall(NodalProjectionDataDestroy(diff_flux_proj->projection));
404   PetscCall(OperatorApplyContextDestroy(diff_flux_proj->calc_div_diff_flux));
405   if (diff_flux_proj->ceed_vec_has_array) {
406     PetscCall(VecReadCeedToPetsc(diff_flux_proj->div_diff_flux_ceed, diff_flux_proj->DivDiffFlux_memtype, diff_flux_proj->DivDiffFlux_loc));
407     diff_flux_proj->ceed_vec_has_array = PETSC_FALSE;
408   }
409   PetscCallCeed(ceed, CeedVectorDestroy(&diff_flux_proj->div_diff_flux_ceed));
410   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&diff_flux_proj->elem_restr_div_diff_flux));
411   PetscCallCeed(ceed, CeedBasisDestroy(&diff_flux_proj->basis_div_diff_flux));
412   PetscCall(VecDestroy(&diff_flux_proj->DivDiffFlux_loc));
413   PetscCall(PetscFree(diff_flux_proj));
414   PetscFunctionReturn(PETSC_SUCCESS);
415 }
416