PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
 
Loading...
Searching...
No Matches
Filter.c
Go to the documentation of this file.
1/**
2 * @file Filter.c
3 * @brief Implements numerical filtering schemes for Large Eddy Simulation (LES).
4 *
5 * This file contains the functions necessary to apply a "test filter" to the resolved
6 * velocity field, which is a core component of the dynamic Smagorinsky turbulence model.
7 * The choice of filter (e.g., a general 3D box filter or a specialized 2D
8 * homogeneous filter) is determined by the simulation's configuration.
9 */
10
11#include "Filter.h"
12#include <math.h> // Required for fabs() in the safe division check.
13
14/*================================================================================*
15 * INTERNAL (STATIC) FUNCTIONS *
16 *================================================================================*/
17
18/**
19 * @brief Apply the two-dimensional Simpson stencil on the central homogeneous-plane samples.
20 */
21static double ApplySimpsonRuleHomogeneousFilter(double values[3][3][3])
22{
24 // The stencil only uses the central j-plane (j-index = 1, corresponding to the y-direction).
25 // The formula is a weighted sum of the 9 points in that plane.
26 const double corners = values[0][1][0] + values[2][1][0] + values[0][1][2] + values[2][1][2]; // 4 corner points
27 const double edges = values[0][1][1] + values[1][1][0] + values[2][1][1] + values[1][1][2]; // 4 edge-center points
28 const double center = values[1][1][1]; // 1 center point
29
30 // The weights (1, 4, 16) and normalization factor (36) come from the 2D Simpson's rule.
32 return (corners + 4.0 * edges + 16.0 * center) / 36.0;
33}
34
35
36/**
37 * @brief Average a 3x3x3 stencil with cell-volume weights while excluding solid cells.
38 */
39static double ApplyVolumeWeightedBoxFilter(double values[3][3][3], double weights[3][3][3])
40{
41 // v1...v8 store the sum of (value * weight) for each of the 8 sub-cubes.
42 // w1...w8 store the sum of (weight) for each of the 8 sub-cubes.
43 double v1, v2, v3, v4, v5, v6, v7, v8;
44 double w1, w2, w3, w4, w5, w6, w7, w8;
45
46 // --- Calculations for the 4 sub-cubes on the bottom layer (k-indices 0 and 1) ---
47
48 // Bottom-Back-Left sub-cube (i-indices: 0,1; j-indices: 0,1; k-indices: 0,1)
49 v1 = ( values[0][0][0]*weights[0][0][0] + values[1][0][0]*weights[1][0][0] + values[0][1][0]*weights[0][1][0] + values[1][1][0]*weights[1][1][0] +
50 values[0][0][1]*weights[0][0][1] + values[1][0][1]*weights[1][0][1] + values[0][1][1]*weights[0][1][1] + values[1][1][1]*weights[1][1][1] );
51 w1 = ( weights[0][0][0] + weights[1][0][0] + weights[0][1][0] + weights[1][1][0] +
52 weights[0][0][1] + weights[1][0][1] + weights[0][1][1] + weights[1][1][1] );
53
54 // Bottom-Back-Right sub-cube (i-indices: 1,2; j-indices: 0,1; k-indices: 0,1)
55 v2 = ( values[1][0][0]*weights[1][0][0] + values[2][0][0]*weights[2][0][0] + values[1][1][0]*weights[1][1][0] + values[2][1][0]*weights[2][1][0] +
56 values[1][0][1]*weights[1][0][1] + values[2][0][1]*weights[2][0][1] + values[1][1][1]*weights[1][1][1] + values[2][1][1]*weights[2][1][1] );
57 w2 = ( weights[1][0][0] + weights[2][0][0] + weights[1][1][0] + weights[2][1][0] +
58 weights[1][0][1] + weights[2][0][1] + weights[1][1][1] + weights[2][1][1] );
59
60 // Bottom-Front-Left sub-cube (i-indices: 0,1; j-indices: 1,2; k-indices: 0,1)
61 v3 = ( values[0][1][0]*weights[0][1][0] + values[1][1][0]*weights[1][1][0] + values[0][2][0]*weights[0][2][0] + values[1][2][0]*weights[1][2][0] +
62 values[0][1][1]*weights[0][1][1] + values[1][1][1]*weights[1][1][1] + values[0][2][1]*weights[0][2][1] + values[1][2][1]*weights[1][2][1] );
63 w3 = ( weights[0][1][0] + weights[1][1][0] + weights[0][2][0] + weights[1][2][0] +
64 weights[0][1][1] + weights[1][1][1] + weights[0][2][1] + weights[1][2][1] );
65
66 // Bottom-Front-Right sub-cube (i-indices: 1,2; j-indices: 1,2; k-indices: 0,1)
67 v4 = ( values[1][1][0]*weights[1][1][0] + values[2][1][0]*weights[2][1][0] + values[1][2][0]*weights[1][2][0] + values[2][2][0]*weights[2][2][0] +
68 values[1][1][1]*weights[1][1][1] + values[2][1][1]*weights[2][1][1] + values[1][2][1]*weights[1][2][1] + values[2][2][1]*weights[2][2][1] );
69 w4 = ( weights[1][1][0] + weights[2][1][0] + weights[1][2][0] + weights[2][2][0] +
70 weights[1][1][1] + weights[2][1][1] + weights[1][2][1] + weights[2][2][1] );
71
72
73 // --- Calculations for the 4 sub-cubes on the top layer (k-indices 1 and 2) ---
74
75 // Top-Back-Left sub-cube (i-indices: 0,1; j-indices: 0,1; k-indices: 1,2)
76 v5 = ( values[0][0][1]*weights[0][0][1] + values[1][0][1]*weights[1][0][1] + values[0][1][1]*weights[0][1][1] + values[1][1][1]*weights[1][1][1] +
77 values[0][0][2]*weights[0][0][2] + values[1][0][2]*weights[1][0][2] + values[0][1][2]*weights[0][1][2] + values[1][1][2]*weights[1][1][2] );
78 w5 = ( weights[0][0][1] + weights[1][0][1] + weights[0][1][1] + weights[1][1][1] +
79 weights[0][0][2] + weights[1][0][2] + weights[0][1][2] + weights[1][1][2] );
80
81 // Top-Back-Right sub-cube (i-indices: 1,2; j-indices: 0,1; k-indices: 1,2)
82 v6 = ( values[1][0][1]*weights[1][0][1] + values[2][0][1]*weights[2][0][1] + values[1][1][1]*weights[1][1][1] + values[2][1][1]*weights[2][1][1] +
83 values[1][0][2]*weights[1][0][2] + values[2][0][2]*weights[2][0][2] + values[1][1][2]*weights[1][1][2] + values[2][1][2]*weights[2][1][2] );
84 w6 = ( weights[1][0][1] + weights[2][0][1] + weights[1][1][1] + weights[2][1][1] +
85 weights[1][0][2] + weights[2][0][2] + weights[1][1][2] + weights[2][1][2] );
86
87 // Top-Front-Left sub-cube (i-indices: 0,1; j-indices: 1,2; k-indices: 1,2)
88 v7 = ( values[0][1][1]*weights[0][1][1] + values[1][1][1]*weights[1][1][1] + values[0][2][1]*weights[0][2][1] + values[1][2][1]*weights[1][2][1] +
89 values[0][1][2]*weights[0][1][2] + values[1][1][2]*weights[1][1][2] + values[0][2][2]*weights[0][2][2] + values[1][2][2]*weights[1][2][2] );
90 w7 = ( weights[0][1][1] + weights[1][1][1] + weights[0][2][1] + weights[1][2][1] +
91 weights[0][1][2] + weights[1][1][2] + weights[0][2][2] + weights[1][2][2] );
92
93 // Top-Front-Right sub-cube (i-indices: 1,2; j-indices: 1,2; k-indices: 1,2)
94 v8 = ( values[1][1][1]*weights[1][1][1] + values[2][1][1]*weights[2][1][1] + values[1][2][1]*weights[1][2][1] + values[2][2][1]*weights[2][2][1] +
95 values[1][1][2]*weights[1][1][2] + values[2][1][2]*weights[2][1][2] + values[1][2][2]*weights[1][2][2] + values[2][2][2]*weights[2][2][2] );
96 w8 = ( weights[1][1][1] + weights[2][1][1] + weights[1][2][1] + weights[2][2][1] +
97 weights[1][1][2] + weights[2][1][2] + weights[1][2][2] + weights[2][2][2] );
98
99 // Sum the contributions from all 8 octants.
100 double total_weighted_value = v1+v2+v3+v4+v5+v6+v7+v8;
101 double total_weight = w1+w2+w3+w4+w5+w6+w7+w8;
102
103 // Production safety check: avoid division by zero if all weights are zero
104 // (e.g., if the stencil is entirely inside a solid body).
105 if (fabs(total_weight) < 1.0e-12) {
106 return 0.0;
107 }
108
109 return total_weighted_value / total_weight;
110}
111
112
113/*================================================================================*
114 * PUBLIC FUNCTIONS *
115 *================================================================================*/
116
117/**
118 * @brief Implementation of \ref ApplyLESTestFilter().
119 * @details Full API contract (arguments, ownership, side effects) is documented with
120 * the header declaration in `include/Filter.h`.
121 * @see ApplyLESTestFilter()
122 */
123double ApplyLESTestFilter(LESTestFilterKernel kernel, double values[3][3][3], double weights[3][3][3])
124{
125 // This function acts as a dispatcher over the configured stencil.
126 switch (kernel) {
128 // The caller has declared the i and k directions homogeneous, so the more
129 // accurate Simpson stencil applies. Volume weights are irrelevant there.
132 default:
133 // Default for general, non-uniform, curvilinear grids. The volume-weighted
134 // box filter keeps the average correct when neighbouring cells differ in size.
135 return ApplyVolumeWeightedBoxFilter(values, weights);
136 }
137}
138
139/**
140 * @brief Implementation of \ref ApplyLESTestFilterSymTensor().
141 * @details Full API contract (arguments, ownership, side effects) is documented with
142 * the header declaration in `include/Filter.h`.
143 * @see ApplyLESTestFilterSymTensor()
144 */
145PetscErrorCode ApplyLESTestFilterSymTensor(LESTestFilterKernel kernel, SymTensor values[3][3][3],
146 double weights[3][3][3], SymTensor *filtered)
147{
148 // One scratch plane per component keeps the scalar filter unaware of tensors.
149 double component[6][3][3][3];
150
151 PetscFunctionBeginUser;
152 PetscCheck(filtered != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
153 "Filtered tensor destination cannot be NULL.");
154
155 for (PetscInt r = 0; r < 3; r++)
156 for (PetscInt q = 0; q < 3; q++)
157 for (PetscInt p = 0; p < 3; p++) {
158 component[0][r][q][p] = values[r][q][p].xx;
159 component[1][r][q][p] = values[r][q][p].xy;
160 component[2][r][q][p] = values[r][q][p].xz;
161 component[3][r][q][p] = values[r][q][p].yy;
162 component[4][r][q][p] = values[r][q][p].yz;
163 component[5][r][q][p] = values[r][q][p].zz;
164 }
165
166 filtered->xx = ApplyLESTestFilter(kernel, component[0], weights);
167 filtered->xy = ApplyLESTestFilter(kernel, component[1], weights);
168 filtered->xz = ApplyLESTestFilter(kernel, component[2], weights);
169 filtered->yy = ApplyLESTestFilter(kernel, component[3], weights);
170 filtered->yz = ApplyLESTestFilter(kernel, component[4], weights);
171 filtered->zz = ApplyLESTestFilter(kernel, component[5], weights);
172
173 PetscFunctionReturn(0);
174}
static double ApplyVolumeWeightedBoxFilter(double values[3][3][3], double weights[3][3][3])
Average a 3x3x3 stencil with cell-volume weights while excluding solid cells.
Definition Filter.c:39
static double ApplySimpsonRuleHomogeneousFilter(double values[3][3][3])
Apply the two-dimensional Simpson stencil on the central homogeneous-plane samples.
Definition Filter.c:21
double ApplyLESTestFilter(LESTestFilterKernel kernel, double values[3][3][3], double weights[3][3][3])
Implementation of ApplyLESTestFilter().
Definition Filter.c:123
PetscErrorCode ApplyLESTestFilterSymTensor(LESTestFilterKernel kernel, SymTensor values[3][3][3], double weights[3][3][3], SymTensor *filtered)
Implementation of ApplyLESTestFilterSymTensor().
Definition Filter.c:145
#define PROFILE_HELPER_BEGIN
Marks the beginning of a profiled helper: timed only when enabled by name.
Definition logging.h:1014
#define PROFILE_HELPER_END
Alias of PROFILE_FUNCTION_END for a function that began with PROFILE_HELPER_BEGIN.
Definition logging.h:1033
PetscReal yy
Definition variables.h:162
LESTestFilterKernel
Selects the discrete test-filter kernel used by the dynamic procedure.
Definition variables.h:626
@ LES_TEST_FILTER_SIMPSON_IK
Definition variables.h:628
@ LES_TEST_FILTER_VOLUME_WEIGHTED_BOX
Definition variables.h:627
PetscReal xx
Definition variables.h:162
PetscReal yz
Definition variables.h:162
PetscReal zz
Definition variables.h:162
PetscReal xz
Definition variables.h:162
PetscReal xy
Definition variables.h:162
A symmetric second-order tensor stored by its six independent components.
Definition variables.h:161