xref: /honee/src/differential_filter.c (revision e3663b908338ee76924aed100340d66e13adb0df)
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 performing differential filtering
5 
6 #include "../qfunctions/differential_filter.h"
7 #include <ceed.h>
8 
9 #include <petscdmplex.h>
10 
11 #include <navierstokes.h>
12 
13 // @brief Create RHS and LHS operators for differential filtering
14 PetscErrorCode DifferentialFilterCreateOperators(Ceed ceed, Honee honee, CeedQFunctionContext diff_filter_qfctx) {
15   DiffFilterData      diff_filter = honee->diff_filter;
16   DM                  dm_filter   = diff_filter->dm_filter;
17   CeedInt             num_comp_q, q_data_size, num_comp_x;
18   PetscInt            dim;
19   CeedVector          q_data;
20   CeedElemRestriction elem_restr_qd;
21   DMLabel             domain_label = NULL;
22   PetscInt            label_value = 0, height = 0;
23 
24   PetscFunctionBeginUser;
25   PetscCall(DMGetDimension(honee->dm, &dim));
26   PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(honee->elem_restr_x, &num_comp_x));
27   PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(honee->elem_restr_q, &num_comp_q));
28 
29   PetscCall(QDataGet(ceed, dm_filter, domain_label, label_value, honee->elem_restr_x, honee->basis_x, honee->x_coord, &elem_restr_qd, &q_data,
30                      &q_data_size));
31 
32   {  // -- Create RHS MatopApplyContext
33     CeedQFunction qf_rhs;
34     CeedOperator  op_rhs;
35     switch (honee->phys->state_var) {
36       case STATEVAR_PRIMITIVE:
37         PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, DifferentialFilter_RHS_Prim, DifferentialFilter_RHS_Prim_loc, &qf_rhs));
38         break;
39       case STATEVAR_CONSERVATIVE:
40         PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, DifferentialFilter_RHS_Conserv, DifferentialFilter_RHS_Conserv_loc, &qf_rhs));
41         break;
42       case STATEVAR_ENTROPY:
43         PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, DifferentialFilter_RHS_Entropy, DifferentialFilter_RHS_Entropy_loc, &qf_rhs));
44         break;
45     }
46     if (diff_filter->do_mms_test) {
47       PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_rhs));
48       PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, DifferentialFilter_MMS_RHS, DifferentialFilter_MMS_RHS_loc, &qf_rhs));
49     }
50 
51     PetscCallCeed(ceed, CeedQFunctionSetContext(qf_rhs, diff_filter_qfctx));
52     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs, "q", num_comp_q, CEED_EVAL_INTERP));
53     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs, "qdata", q_data_size, CEED_EVAL_NONE));
54     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs, "x", num_comp_x, CEED_EVAL_INTERP));
55     for (PetscInt i = 0; i < diff_filter->num_filtered_fields; i++) {
56       char field_name[PETSC_MAX_PATH_LEN];
57 
58       PetscCall(PetscSNPrintf(field_name, PETSC_MAX_PATH_LEN, "v%" PetscInt_FMT, i));
59       PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_rhs, field_name, diff_filter->num_field_components[i], CEED_EVAL_INTERP));
60     }
61 
62     PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_rhs, NULL, NULL, &op_rhs));
63     PetscCallCeed(ceed, CeedOperatorSetField(op_rhs, "q", honee->elem_restr_q, honee->basis_q, CEED_VECTOR_ACTIVE));
64     PetscCallCeed(ceed, CeedOperatorSetField(op_rhs, "qdata", elem_restr_qd, CEED_BASIS_NONE, q_data));
65     PetscCallCeed(ceed, CeedOperatorSetField(op_rhs, "x", honee->elem_restr_x, honee->basis_x, honee->x_coord));
66     for (PetscInt dm_field = 0; dm_field < diff_filter->num_filtered_fields; dm_field++) {
67       char                field_name[PETSC_MAX_PATH_LEN];
68       CeedElemRestriction elem_restr_filter;
69       CeedBasis           basis_filter;
70 
71       PetscCall(DMPlexCeedElemRestrictionCreate(ceed, dm_filter, domain_label, label_value, height, dm_field, &elem_restr_filter));
72       PetscCall(CreateBasisFromPlex(ceed, dm_filter, domain_label, label_value, height, dm_field, &basis_filter));
73 
74       PetscCall(PetscSNPrintf(field_name, PETSC_MAX_PATH_LEN, "v%" PetscInt_FMT, dm_field));
75       PetscCallCeed(ceed, CeedOperatorSetField(op_rhs, field_name, elem_restr_filter, basis_filter, CEED_VECTOR_ACTIVE));
76 
77       PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_filter));
78       PetscCallCeed(ceed, CeedBasisDestroy(&basis_filter));
79     }
80 
81     PetscCall(OperatorApplyContextCreate(honee->dm, dm_filter, ceed, op_rhs, NULL, NULL, honee->Q_loc, NULL, &diff_filter->op_rhs_ctx));
82 
83     PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_rhs));
84     PetscCallCeed(ceed, CeedOperatorDestroy(&op_rhs));
85   }
86 
87   {  // Setup LHS Operator and KSP for the differential filtering solve
88     CeedOperator        op_lhs;
89     Mat                 mat_lhs;
90     PetscInt            dim, num_comp_grid_aniso;
91     CeedElemRestriction elem_restr_grid_aniso;
92     CeedVector          grid_aniso_ceed;
93 
94     PetscCall(DMGetDimension(honee->dm, &dim));
95     PetscCall(GridAnisotropyTensorCalculateCollocatedVector(ceed, honee, &elem_restr_grid_aniso, &grid_aniso_ceed, &num_comp_grid_aniso));
96 
97     PetscCallCeed(ceed, CeedCompositeOperatorCreate(ceed, &op_lhs));
98     for (PetscInt i = 0; i < diff_filter->num_filtered_fields; i++) {
99       CeedQFunction       qf_lhs;
100       PetscInt            num_comp_filter = diff_filter->num_field_components[i];
101       CeedOperator        op_lhs_sub;
102       CeedElemRestriction elem_restr_filter;
103       CeedBasis           basis_filter;
104 
105       switch (num_comp_filter) {
106         case 1:
107           PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, DifferentialFilter_LHS_1, DifferentialFilter_LHS_1_loc, &qf_lhs));
108           break;
109         case 5:
110           PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, DifferentialFilter_LHS_5, DifferentialFilter_LHS_5_loc, &qf_lhs));
111           break;
112         case 6:
113           PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, DifferentialFilter_LHS_6, DifferentialFilter_LHS_6_loc, &qf_lhs));
114           break;
115         case 11:
116           PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, DifferentialFilter_LHS_11, DifferentialFilter_LHS_11_loc, &qf_lhs));
117           break;
118         default:
119           SETERRQ(PetscObjectComm((PetscObject)honee->dm), PETSC_ERR_SUP, "Differential filtering not available for (%" PetscInt_FMT ") components",
120                   num_comp_filter);
121       }
122 
123       PetscCallCeed(ceed, CeedQFunctionSetContext(qf_lhs, diff_filter_qfctx));
124       PetscCallCeed(ceed, CeedQFunctionSetUserFlopsEstimate(qf_lhs, 0));
125       PetscCallCeed(ceed, CeedQFunctionAddInput(qf_lhs, "q", num_comp_filter, CEED_EVAL_INTERP));
126       PetscCallCeed(ceed, CeedQFunctionAddInput(qf_lhs, "Grad_q", num_comp_filter * dim, CEED_EVAL_GRAD));
127       PetscCallCeed(ceed, CeedQFunctionAddInput(qf_lhs, "anisotropy tensor", num_comp_grid_aniso, CEED_EVAL_NONE));
128       PetscCallCeed(ceed, CeedQFunctionAddInput(qf_lhs, "x", num_comp_x, CEED_EVAL_INTERP));
129       PetscCallCeed(ceed, CeedQFunctionAddInput(qf_lhs, "qdata", q_data_size, CEED_EVAL_NONE));
130       PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_lhs, "v", num_comp_filter, CEED_EVAL_INTERP));
131       PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_lhs, "Grad_v", num_comp_filter * dim, CEED_EVAL_GRAD));
132 
133       {
134         CeedOperatorField op_field;
135         char              field_name[PETSC_MAX_PATH_LEN];
136 
137         PetscCall(PetscSNPrintf(field_name, PETSC_MAX_PATH_LEN, "v%" PetscInt_FMT, i));
138         PetscCallCeed(ceed, CeedOperatorGetFieldByName(diff_filter->op_rhs_ctx->op, field_name, &op_field));
139         PetscCallCeed(ceed, CeedOperatorFieldGetElemRestriction(op_field, &elem_restr_filter));
140         PetscCallCeed(ceed, CeedOperatorFieldGetBasis(op_field, &basis_filter));
141       }
142 
143       PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_lhs, NULL, NULL, &op_lhs_sub));
144       PetscCallCeed(ceed, CeedOperatorSetField(op_lhs_sub, "q", elem_restr_filter, basis_filter, CEED_VECTOR_ACTIVE));
145       PetscCallCeed(ceed, CeedOperatorSetField(op_lhs_sub, "Grad_q", elem_restr_filter, basis_filter, CEED_VECTOR_ACTIVE));
146       PetscCallCeed(ceed, CeedOperatorSetField(op_lhs_sub, "anisotropy tensor", elem_restr_grid_aniso, CEED_BASIS_NONE, grid_aniso_ceed));
147       PetscCallCeed(ceed, CeedOperatorSetField(op_lhs_sub, "x", honee->elem_restr_x, honee->basis_x, honee->x_coord));
148       PetscCallCeed(ceed, CeedOperatorSetField(op_lhs_sub, "qdata", elem_restr_qd, CEED_BASIS_NONE, q_data));
149       PetscCallCeed(ceed, CeedOperatorSetField(op_lhs_sub, "v", elem_restr_filter, basis_filter, CEED_VECTOR_ACTIVE));
150       PetscCallCeed(ceed, CeedOperatorSetField(op_lhs_sub, "Grad_v", elem_restr_filter, basis_filter, CEED_VECTOR_ACTIVE));
151 
152       PetscCallCeed(ceed, CeedCompositeOperatorAddSub(op_lhs, op_lhs_sub));
153       PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_lhs));
154       PetscCallCeed(ceed, CeedOperatorDestroy(&op_lhs_sub));
155     }
156     PetscCallCeed(ceed, CeedVectorDestroy(&grid_aniso_ceed));
157     PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_grid_aniso));
158 
159     PetscCallCeed(ceed, CeedOperatorGetContextFieldLabel(op_lhs, "filter width scaling", &diff_filter->filter_width_scaling_label));
160     PetscCall(MatCreateCeed(dm_filter, dm_filter, op_lhs, NULL, &mat_lhs));
161 
162     PetscCall(KSPCreate(PetscObjectComm((PetscObject)dm_filter), &diff_filter->ksp));
163     PetscCall(KSPSetOptionsPrefix(diff_filter->ksp, "diff_filter_"));
164     {
165       PC pc;
166       PetscCall(KSPGetPC(diff_filter->ksp, &pc));
167       PetscCall(PCSetType(pc, PCJACOBI));
168       PetscCall(PCJacobiSetType(pc, PC_JACOBI_DIAGONAL));
169       PetscCall(KSPSetType(diff_filter->ksp, KSPCG));
170       PetscCall(KSPSetNormType(diff_filter->ksp, KSP_NORM_NATURAL));
171       PetscCall(KSPSetTolerances(diff_filter->ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT));
172     }
173     PetscCall(KSPSetFromOptions_WithMatCeed(diff_filter->ksp, mat_lhs));
174 
175     PetscCall(MatDestroy(&mat_lhs));
176     PetscCallCeed(ceed, CeedOperatorDestroy(&op_lhs));
177   }
178 
179   PetscCallCeed(ceed, CeedVectorDestroy(&q_data));
180   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_qd));
181   PetscFunctionReturn(PETSC_SUCCESS);
182 }
183 
184 // @brief Setup DM, operators, contexts, etc. for performing differential filtering
185 PetscErrorCode DifferentialFilterSetup(Ceed ceed, Honee honee, ProblemData problem) {
186   MPI_Comm                  comm = honee->comm;
187   NewtonianIdealGasContext  gas;
188   DifferentialFilterContext diff_filter_ctx;
189   CeedQFunctionContext      diff_filter_qfctx;
190 
191   PetscFunctionBeginUser;
192   PetscCall(PetscNew(&honee->diff_filter));
193   DiffFilterData diff_filter = honee->diff_filter;
194   PetscCall(PetscOptionsGetBool(NULL, NULL, "-diff_filter_mms", &diff_filter->do_mms_test, NULL));
195 
196   {  // Create DM for filtered quantities
197     PetscSection section;
198 
199     PetscCall(DMClone(honee->dm, &diff_filter->dm_filter));
200     PetscCall(DMSetMatrixPreallocateSkip(diff_filter->dm_filter, PETSC_TRUE));
201     PetscCall(PetscObjectSetName((PetscObject)diff_filter->dm_filter, "Differential Filtering"));
202 
203     diff_filter->num_filtered_fields = diff_filter->do_mms_test ? 1 : 2;
204     PetscCall(PetscMalloc1(diff_filter->num_filtered_fields, &diff_filter->num_field_components));
205 
206     if (diff_filter->do_mms_test) {
207       PetscInt field_components;
208       diff_filter->num_field_components[0] = field_components = 1;
209       PetscCall(DMSetupByOrder_FEM(PETSC_TRUE, PETSC_TRUE, honee->app_ctx->degree, 1, honee->app_ctx->q_extra, diff_filter->num_filtered_fields,
210                                    &field_components, diff_filter->dm_filter));
211 
212       PetscCall(DMGetLocalSection(diff_filter->dm_filter, &section));
213       PetscCall(PetscSectionSetFieldName(section, 0, ""));
214       PetscCall(PetscSectionSetComponentName(section, 0, 0, "FilteredPhi"));
215     } else {
216       PetscInt field_components[2];
217       diff_filter->num_field_components[0] = field_components[0] = DIFF_FILTER_STATE_NUM;
218       diff_filter->num_field_components[1] = field_components[1] = DIFF_FILTER_VELOCITY_SQUARED_NUM;
219       PetscCall(DMSetupByOrder_FEM(PETSC_TRUE, PETSC_TRUE, honee->app_ctx->degree, 1, honee->app_ctx->q_extra, diff_filter->num_filtered_fields,
220                                    field_components, diff_filter->dm_filter));
221 
222       diff_filter->field_prim_state = 0;
223       diff_filter->field_velo_prod  = 1;
224       PetscCall(DMGetLocalSection(diff_filter->dm_filter, &section));
225       PetscCall(PetscSectionSetFieldName(section, diff_filter->field_prim_state, "Filtered Primitive State Variables"));
226       PetscCall(PetscSectionSetComponentName(section, 0, DIFF_FILTER_PRESSURE, "FilteredPressure"));
227       PetscCall(PetscSectionSetComponentName(section, 0, DIFF_FILTER_VELOCITY_X, "FilteredVelocityX"));
228       PetscCall(PetscSectionSetComponentName(section, 0, DIFF_FILTER_VELOCITY_Y, "FilteredVelocityY"));
229       PetscCall(PetscSectionSetComponentName(section, 0, DIFF_FILTER_VELOCITY_Z, "FilteredVelocityZ"));
230       PetscCall(PetscSectionSetComponentName(section, 0, DIFF_FILTER_TEMPERATURE, "FilteredTemperature"));
231       PetscCall(PetscSectionSetFieldName(section, diff_filter->field_velo_prod, "Filtered Velocity Products"));
232       PetscCall(PetscSectionSetComponentName(section, 1, DIFF_FILTER_VELOCITY_SQUARED_XX, "FilteredVelocitySquaredXX"));
233       PetscCall(PetscSectionSetComponentName(section, 1, DIFF_FILTER_VELOCITY_SQUARED_YY, "FilteredVelocitySquaredYY"));
234       PetscCall(PetscSectionSetComponentName(section, 1, DIFF_FILTER_VELOCITY_SQUARED_ZZ, "FilteredVelocitySquaredZZ"));
235       PetscCall(PetscSectionSetComponentName(section, 1, DIFF_FILTER_VELOCITY_SQUARED_YZ, "FilteredVelocitySquaredYZ"));
236       PetscCall(PetscSectionSetComponentName(section, 1, DIFF_FILTER_VELOCITY_SQUARED_XZ, "FilteredVelocitySquaredXZ"));
237       PetscCall(PetscSectionSetComponentName(section, 1, DIFF_FILTER_VELOCITY_SQUARED_XY, "FilteredVelocitySquaredXY"));
238     }
239   }
240 
241   PetscCall(PetscNew(&diff_filter_ctx));
242   diff_filter_ctx->grid_based_width = false;
243   for (int i = 0; i < 3; i++) diff_filter_ctx->width_scaling[i] = 1;
244   diff_filter_ctx->kernel_scaling   = 0.1;
245   diff_filter_ctx->damping_function = DIFF_FILTER_DAMP_NONE;
246   diff_filter_ctx->friction_length  = 0;
247   diff_filter_ctx->damping_constant = 25;
248 
249   PetscOptionsBegin(comm, NULL, "Differential Filtering Options", NULL);
250   PetscInt narray = 3;
251   PetscCall(PetscOptionsBool("-diff_filter_grid_based_width", "Use filter width based on the grid size", NULL, diff_filter_ctx->grid_based_width,
252                              (PetscBool *)&diff_filter_ctx->grid_based_width, NULL));
253   PetscCall(PetscOptionsRealArray("-diff_filter_width_scaling", "Anisotropic scaling of filter width tensor", NULL, diff_filter_ctx->width_scaling,
254                                   &narray, NULL));
255   PetscCall(PetscOptionsReal("-diff_filter_kernel_scaling", "Scaling to make differential kernel size \"equivalent\" to other filter kernels", NULL,
256                              diff_filter_ctx->kernel_scaling, &diff_filter_ctx->kernel_scaling, NULL));
257   PetscCall(PetscOptionsEnum("-diff_filter_wall_damping_function", "Damping function to use at the wall", NULL, DifferentialFilterDampingFunctions,
258                              (PetscEnum)(diff_filter_ctx->damping_function), (PetscEnum *)&diff_filter_ctx->damping_function, NULL));
259   PetscCall(PetscOptionsReal("-diff_filter_wall_damping_constant", "Contant for the wall-damping function", NULL, diff_filter_ctx->damping_constant,
260                              &diff_filter_ctx->damping_constant, NULL));
261   PetscCall(PetscOptionsReal("-diff_filter_friction_length", "Friction length associated with the flow, \\delta_\\nu. For wall-damping functions",
262                              NULL, diff_filter_ctx->friction_length, &diff_filter_ctx->friction_length, NULL));
263   PetscOptionsEnd();
264 
265   Units units = honee->units;
266   for (int i = 0; i < 3; i++) diff_filter_ctx->width_scaling[i] *= units->meter;
267   diff_filter_ctx->kernel_scaling *= units->meter;
268   diff_filter_ctx->friction_length *= units->meter;
269 
270   // -- Create QFContext
271   PetscCallCeed(ceed, CeedQFunctionContextGetDataRead(problem->apply_vol_ifunction.qfctx, CEED_MEM_HOST, &gas));
272   diff_filter_ctx->gas = *gas;
273   PetscCallCeed(ceed, CeedQFunctionContextRestoreDataRead(problem->apply_vol_ifunction.qfctx, &gas));
274 
275   PetscCallCeed(ceed, CeedQFunctionContextCreate(ceed, &diff_filter_qfctx));
276   PetscCallCeed(ceed, CeedQFunctionContextSetData(diff_filter_qfctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*diff_filter_ctx), diff_filter_ctx));
277   PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(diff_filter_qfctx, CEED_MEM_HOST, FreeContextPetsc));
278   PetscCallCeed(ceed, CeedQFunctionContextRegisterDouble(
279                           diff_filter_qfctx, "filter width scaling", offsetof(struct DifferentialFilterContext_, width_scaling),
280                           sizeof(diff_filter_ctx->width_scaling) / sizeof(diff_filter_ctx->width_scaling[0]), "Filter width scaling"));
281 
282   // -- Setup Operators
283   PetscCall(DifferentialFilterCreateOperators(ceed, honee, diff_filter_qfctx));
284 
285   PetscCallCeed(ceed, CeedQFunctionContextDestroy(&diff_filter_qfctx));
286   PetscFunctionReturn(PETSC_SUCCESS);
287 }
288 
289 // @brief Apply differential filter to the solution given by Q
290 PetscErrorCode DifferentialFilterApply(Honee honee, const PetscReal solution_time, const Vec Q, Vec Filtered_Solution) {
291   DiffFilterData   diff_filter = honee->diff_filter;
292   PetscObjectState X_loc_state;
293   Vec              RHS;
294 
295   PetscFunctionBeginUser;
296   PetscCall(PetscLogEventBegin(FLUIDS_DifferentialFilter, Q, Filtered_Solution, 0, 0));
297   PetscCall(DMGetNamedGlobalVector(diff_filter->dm_filter, "RHS", &RHS));
298   PetscCall(UpdateBoundaryValues(honee, diff_filter->op_rhs_ctx->X_loc, solution_time));
299   PetscCall(VecGetState(diff_filter->op_rhs_ctx->X_loc, &X_loc_state));
300   if (X_loc_state != diff_filter->X_loc_state) {
301     PetscCall(ApplyCeedOperatorGlobalToGlobal(Q, RHS, diff_filter->op_rhs_ctx));
302     PetscCall(VecGetState(diff_filter->op_rhs_ctx->X_loc, &X_loc_state));
303     diff_filter->X_loc_state = X_loc_state;
304   }
305   PetscCall(VecViewFromOptions(RHS, NULL, "-diff_filter_rhs_view"));
306 
307   PetscCall(KSPSolve(diff_filter->ksp, RHS, Filtered_Solution));
308   PetscCall(DMRestoreNamedGlobalVector(diff_filter->dm_filter, "RHS", &RHS));
309   PetscCall(PetscLogEventEnd(FLUIDS_DifferentialFilter, Q, Filtered_Solution, 0, 0));
310   PetscFunctionReturn(PETSC_SUCCESS);
311 }
312 
313 // @brief TSMonitor for just applying differential filtering to the simulation
314 // This runs every time step and is primarily for testing purposes
315 PetscErrorCode TSMonitor_DifferentialFilter(TS ts, PetscInt steps, PetscReal solution_time, Vec Q, void *ctx) {
316   Honee          honee       = (Honee)ctx;
317   DiffFilterData diff_filter = honee->diff_filter;
318   Vec            Filtered_Field;
319 
320   PetscFunctionBeginUser;
321   PetscCall(DMGetGlobalVector(diff_filter->dm_filter, &Filtered_Field));
322 
323   PetscCall(DifferentialFilterApply(honee, solution_time, Q, Filtered_Field));
324   PetscCall(VecViewFromOptions(Filtered_Field, NULL, "-diff_filter_view"));
325   if (honee->app_ctx->test_type == TESTTYPE_DIFF_FILTER) PetscCall(RegressionTest(honee->app_ctx, Filtered_Field));
326 
327   PetscCall(DMRestoreGlobalVector(diff_filter->dm_filter, &Filtered_Field));
328   PetscFunctionReturn(PETSC_SUCCESS);
329 }
330 
331 PetscErrorCode DifferentialFilterDataDestroy(DiffFilterData diff_filter) {
332   PetscFunctionBeginUser;
333   if (!diff_filter) PetscFunctionReturn(PETSC_SUCCESS);
334 
335   PetscCall(OperatorApplyContextDestroy(diff_filter->op_rhs_ctx));
336   PetscCall(DMDestroy(&diff_filter->dm_filter));
337   PetscCall(KSPDestroy(&diff_filter->ksp));
338 
339   PetscCall(PetscFree(diff_filter->num_field_components));
340   PetscCall(PetscFree(diff_filter));
341   PetscFunctionReturn(PETSC_SUCCESS);
342 }
343 
344 PetscErrorCode DifferentialFilterMmsICSetup(ProblemData problem) {
345   PetscFunctionBeginUser;
346   problem->ics.qf_func_ptr = DifferentialFilter_MMS_IC;
347   problem->ics.qf_loc      = DifferentialFilter_MMS_IC_loc;
348   PetscFunctionReturn(PETSC_SUCCESS);
349 }
350