xref: /honee/qfunctions/differential_filter.h (revision b78d7c7d152a2530c4ff7c4fb0143fe9be02cbec)
1 // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2 // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
3 //
4 /// @file
5 /// Implementation of differential filtering
6 #include <ceed/types.h>
7 
8 #include "differential_filter_enums.h"
9 #include "newtonian_state.h"
10 #include "newtonian_types.h"
11 #include "utils.h"
12 
13 enum DifferentialFilterDampingFunction { DIFF_FILTER_DAMP_NONE, DIFF_FILTER_DAMP_VAN_DRIEST, DIFF_FILTER_DAMP_MMS };
14 
15 typedef struct DifferentialFilterContext_ *DifferentialFilterContext;
16 struct DifferentialFilterContext_ {
17   bool                                   grid_based_width;
18   CeedScalar                             width_scaling[3];
19   CeedScalar                             kernel_scaling;
20   CeedScalar                             friction_length;
21   enum DifferentialFilterDampingFunction damping_function;
22   CeedScalar                             damping_constant;
23   struct NewtonianIdealGasContext_       gas;
24 };
25 
26 CEED_QFUNCTION_HELPER int DifferentialFilter_RHS(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
27   const CeedScalar(*q)[CEED_Q_VLA]      = (const CeedScalar(*)[CEED_Q_VLA])in[0];
28   const CeedScalar(*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[1];
29   CeedScalar(*v0)[CEED_Q_VLA]           = (CeedScalar(*)[CEED_Q_VLA])out[0];
30   CeedScalar(*v1)[CEED_Q_VLA]           = (CeedScalar(*)[CEED_Q_VLA])out[1];
31 
32   DifferentialFilterContext context = (DifferentialFilterContext)ctx;
33   NewtonianIdealGasContext  gas     = &context->gas;
34 
35   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
36     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
37     const CeedScalar wdetJ = q_data[0][i];
38     const State      s     = StateFromQ(gas, qi, state_var);
39 
40     v0[DIFF_FILTER_PRESSURE][i]            = wdetJ * s.Y.pressure;
41     v0[DIFF_FILTER_VELOCITY_X][i]          = wdetJ * s.Y.velocity[0];
42     v0[DIFF_FILTER_VELOCITY_Y][i]          = wdetJ * s.Y.velocity[1];
43     v0[DIFF_FILTER_VELOCITY_Z][i]          = wdetJ * s.Y.velocity[2];
44     v0[DIFF_FILTER_TEMPERATURE][i]         = wdetJ * s.Y.temperature;
45     v1[DIFF_FILTER_VELOCITY_SQUARED_XX][i] = wdetJ * s.Y.velocity[0] * s.Y.velocity[0];
46     v1[DIFF_FILTER_VELOCITY_SQUARED_YY][i] = wdetJ * s.Y.velocity[1] * s.Y.velocity[1];
47     v1[DIFF_FILTER_VELOCITY_SQUARED_ZZ][i] = wdetJ * s.Y.velocity[2] * s.Y.velocity[2];
48     v1[DIFF_FILTER_VELOCITY_SQUARED_YZ][i] = wdetJ * s.Y.velocity[1] * s.Y.velocity[2];
49     v1[DIFF_FILTER_VELOCITY_SQUARED_XZ][i] = wdetJ * s.Y.velocity[0] * s.Y.velocity[2];
50     v1[DIFF_FILTER_VELOCITY_SQUARED_XY][i] = wdetJ * s.Y.velocity[0] * s.Y.velocity[1];
51   }
52   return 0;
53 }
54 
55 CEED_QFUNCTION(DifferentialFilter_RHS_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
56   return DifferentialFilter_RHS(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
57 }
58 
59 CEED_QFUNCTION(DifferentialFilter_RHS_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
60   return DifferentialFilter_RHS(ctx, Q, in, out, STATEVAR_PRIMITIVE);
61 }
62 
63 CEED_QFUNCTION(DifferentialFilter_RHS_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
64   return DifferentialFilter_RHS(ctx, Q, in, out, STATEVAR_ENTROPY);
65 }
66 
67 CEED_QFUNCTION_HELPER CeedScalar VanDriestWallDamping(const CeedScalar wall_dist_plus, const CeedScalar A_plus) {
68   return -expm1(-wall_dist_plus / A_plus);
69 }
70 
71 CEED_QFUNCTION_HELPER int DifferentialFilter_LHS_N(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, const CeedInt N) {
72   const CeedScalar(*q)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[0];
73   const CeedScalar(*Grad_q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[1];
74   const CeedScalar(*A_ij_delta)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
75   const CeedScalar(*x)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[3];
76   const CeedScalar(*q_data)                 = in[4];
77   CeedScalar(*v)[CEED_Q_VLA]                = (CeedScalar(*)[CEED_Q_VLA])out[0];
78   CeedScalar(*Grad_v)[CEED_Q_VLA]           = (CeedScalar(*)[CEED_Q_VLA])out[1];
79 
80   DifferentialFilterContext context = (DifferentialFilterContext)ctx;
81 
82   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
83     CeedPragmaSIMD for (CeedInt j = 0; j < N; j++) {
84       const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
85       CeedScalar       wdetJ, dXdx[3][3];
86       QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
87 
88       CeedScalar Delta_ij[3][3] = {{0.}};
89       if (context->grid_based_width) {
90         CeedScalar       km_A_ij[6] = {A_ij_delta[0][i], A_ij_delta[1][i], A_ij_delta[2][i], A_ij_delta[3][i], A_ij_delta[4][i], A_ij_delta[5][i]};
91         const CeedScalar delta      = A_ij_delta[6][i];
92         ScaleN(km_A_ij, delta, 6);  // Dimensionalize the normalized anisotropy tensor
93         KMUnpack(km_A_ij, Delta_ij);
94       } else {
95         Delta_ij[0][0] = Delta_ij[1][1] = Delta_ij[2][2] = 1;
96       }
97 
98       CeedScalar scaling_matrix[3][3] = {{0}};
99       if (context->damping_function == DIFF_FILTER_DAMP_VAN_DRIEST) {
100         const CeedScalar damping_coeff = VanDriestWallDamping(x_i[1] / context->friction_length, context->damping_constant);
101         scaling_matrix[0][0]           = Max(1, damping_coeff * context->width_scaling[0]);
102         scaling_matrix[1][1]           = damping_coeff * context->width_scaling[1];
103         scaling_matrix[2][2]           = Max(1, damping_coeff * context->width_scaling[2]);
104       } else if (context->damping_function == DIFF_FILTER_DAMP_NONE) {
105         scaling_matrix[0][0] = context->width_scaling[0];
106         scaling_matrix[1][1] = context->width_scaling[1];
107         scaling_matrix[2][2] = context->width_scaling[2];
108       } else if (context->damping_function == DIFF_FILTER_DAMP_MMS) {
109         const CeedScalar damping_coeff = tanh(60 * x_i[1]);
110         scaling_matrix[0][0]           = 1;
111         scaling_matrix[1][1]           = damping_coeff;
112         scaling_matrix[2][2]           = 1;
113       }
114 
115       CeedScalar scaled_Delta_ij[3][3] = {{0.}};
116       MatMat3(scaling_matrix, Delta_ij, CEED_NOTRANSPOSE, CEED_NOTRANSPOSE, scaled_Delta_ij);
117       CopyMat3(scaled_Delta_ij, Delta_ij);
118 
119       CeedScalar alpha_ij[3][3] = {{0.}};
120       MatMat3(Delta_ij, Delta_ij, CEED_NOTRANSPOSE, CEED_NOTRANSPOSE, alpha_ij);
121       ScaleN((CeedScalar *)alpha_ij, context->kernel_scaling, 9);
122 
123       v[j][i] = wdetJ * q[j][i];
124       CeedScalar dq[3], dq_dXdx[3] = {0.}, dq_dXdx_a[3] = {0.};
125       for (int k = 0; k < 3; k++) {
126         dq[k] = Grad_q[0 * N + j][i] * dXdx[0][k] + Grad_q[1 * N + j][i] * dXdx[1][k] + Grad_q[2 * N + j][i] * dXdx[2][k];
127       }
128       MatVec3(dXdx, dq, CEED_NOTRANSPOSE, dq_dXdx);
129       MatVec3(alpha_ij, dq_dXdx, CEED_NOTRANSPOSE, dq_dXdx_a);
130       for (int k = 0; k < 3; k++) {
131         Grad_v[k * N + j][i] = wdetJ * dq_dXdx_a[k];
132       }
133     }
134   }
135   return 0;
136 }
137 
138 CEED_QFUNCTION(DifferentialFilter_LHS_1)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
139   return DifferentialFilter_LHS_N(ctx, Q, in, out, 1);
140 }
141 
142 CEED_QFUNCTION(DifferentialFilter_LHS_5)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
143   return DifferentialFilter_LHS_N(ctx, Q, in, out, 5);
144 }
145 
146 CEED_QFUNCTION(DifferentialFilter_LHS_6)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
147   return DifferentialFilter_LHS_N(ctx, Q, in, out, 6);
148 }
149 
150 CEED_QFUNCTION(DifferentialFilter_LHS_11)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
151   return DifferentialFilter_LHS_N(ctx, Q, in, out, 11);
152 }
153 
154 CEED_QFUNCTION_HELPER CeedScalar MMS_Solution(const CeedScalar x_i[3], const CeedScalar omega) {
155   return sin(6 * omega * x_i[0]) + sin(6 * omega * x_i[1]);
156 }
157 
158 CEED_QFUNCTION(DifferentialFilter_MMS_RHS)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
159   const CeedScalar(*q)[CEED_Q_VLA]      = (const CeedScalar(*)[CEED_Q_VLA])in[0];
160   const CeedScalar(*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[1];
161   CeedScalar(*v)[CEED_Q_VLA]            = (CeedScalar(*)[CEED_Q_VLA])out[0];
162 
163   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
164     const CeedScalar wdetJ = q_data[0][i];
165     v[0][i]                = wdetJ * q[0][i];
166   }
167   return 0;
168 }
169 
170 // @brief Generate initial condition such that the solution of the differential filtering is given by MMS_Solution() above
171 //
172 // This requires a *very* specific grid, as the anisotropic filtering is grid dependent.
173 // It's for a 75x75x1 grid on a [0,0.5]x3 domain.
174 // The grid is evenly distributed in x, but distributed based on the analytical mesh distribution \Delta_y = .001 + .01*tanh(6*y).
175 // The MMS test can optionally include a wall damping function (must also be enabled for the differential filtering itself).
176 // It can be run via:
177 // ./navierstokes -options_file tests-output/blasius_test.yaml -diff_filter_monitor -diff_filter_view cgns:filtered_solution.cgns -ts_max_steps 0
178 // -diff_filter_mms -diff_filter_kernel_scaling 1 -diff_filter_wall_damping_function mms -dm_plex_box_upper 0.5,0.5,0.5 -dm_plex_box_faces 75,75,1
179 // -mesh_transform platemesh -platemesh_y_node_locs_path tests-output/diff_filter_mms_y_spacing.dat -platemesh_top_angle 0
180 // -diff_filter_grid_based_width
181 CEED_QFUNCTION(DifferentialFilter_MMS_IC)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
182   const CeedScalar(*x)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
183   CeedScalar(*q0)[CEED_Q_VLA]      = (CeedScalar(*)[CEED_Q_VLA])out[0];
184 
185   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
186     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
187 
188     const CeedScalar aniso_scale_factor = 1;  // Must match the one passed in by -diff_filter_aniso_scale
189     const CeedScalar omega              = 2 * M_PI;
190     const CeedScalar omega6             = 6 * omega;
191     const CeedScalar phi_bar            = MMS_Solution(x_i, omega);
192     const CeedScalar dx                 = 0.5 / 75;
193     const CeedScalar dy_analytic        = .001 + .01 * tanh(6 * x_i[1]);
194     const CeedScalar dy                 = dy_analytic;
195     const CeedScalar d_dy_dy            = 0.06 * Square(1 / cosh(6 * x_i[1]));  // Change of \Delta_y w.r.t. y
196     CeedScalar       alpha[2]           = {Square(dx) * aniso_scale_factor, Square(dy) * aniso_scale_factor};
197     bool             damping            = true;
198     CeedScalar       dalpha1dy;
199     if (damping) {
200       CeedScalar damping_coeff   = tanh(60 * x_i[1]);
201       CeedScalar d_damping_coeff = 60 / Square(cosh(60 * x_i[1]));
202       dalpha1dy                  = aniso_scale_factor * 2 * (damping_coeff * dy) * (dy * d_damping_coeff + damping_coeff * d_dy_dy);
203       alpha[1] *= Square(damping_coeff);
204     } else {
205       dalpha1dy = aniso_scale_factor * 2 * dy * d_dy_dy;
206     }
207 
208     CeedScalar phi = phi_bar + alpha[0] * Square(omega6) * sin(6 * omega * x_i[0]) + alpha[1] * Square(omega6) * sin(omega6 * x_i[1]);
209     phi -= dalpha1dy * omega6 * cos(omega6 * x_i[1]);
210 
211     for (CeedInt j = 0; j < 5; j++) q0[j][i] = phi;
212   }
213   return 0;
214 }
215