PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
 
Loading...
Searching...
No Matches
test_metric.c
Go to the documentation of this file.
1/**
2 * @file test_metric.c
3 * @brief C unit tests for metric tensors, face geometry, and length-scale helpers.
4 */
5
6#include "test_support.h"
7
8#include "Metric.h"
9/**
10 * @brief Tests inversion of a diagonal covariant metric tensor.
11 */
12
13static PetscErrorCode TestInvertCovariantMetricTensorDiagonal(void)
14{
15 double covariant[3][3] = {
16 {4.0, 0.0, 0.0},
17 {0.0, 9.0, 0.0},
18 {0.0, 0.0, 16.0},
19 };
20 double contravariant[3][3] = {{0.0}};
21
22 PetscFunctionBeginUser;
23 PetscCall(InvertCovariantMetricTensor(covariant, contravariant));
24 PetscCall(PicurvAssertRealNear(0.25, contravariant[0][0], 1.0e-12, "inverse(0,0)"));
25 PetscCall(PicurvAssertRealNear(1.0 / 9.0, contravariant[1][1], 1.0e-12, "inverse(1,1)"));
26 PetscCall(PicurvAssertRealNear(0.0625, contravariant[2][2], 1.0e-12, "inverse(2,2)"));
27 PetscFunctionReturn(0);
28}
29/**
30 * @brief Tests contravariant velocity computation for identity metrics.
31 */
32
33static PetscErrorCode TestMetricVelocityContravariantIdentity(void)
34{
35 const PetscReal J[3][3] = {
36 {1.0, 0.0, 0.0},
37 {0.0, 1.0, 0.0},
38 {0.0, 0.0, 1.0},
39 };
40 const PetscReal u[3] = {1.5, -2.0, 4.0};
41 PetscReal uc[3] = {0.0, 0.0, 0.0};
42
43 PetscFunctionBeginUser;
44 PetscCall(MetricVelocityContravariant(J, 1.0, u, uc));
45 PetscCall(PicurvAssertRealNear(1.5, uc[0], 1.0e-12, "identity map uc[0]"));
46 PetscCall(PicurvAssertRealNear(-2.0, uc[1], 1.0e-12, "identity map uc[1]"));
47 PetscCall(PicurvAssertRealNear(4.0, uc[2], 1.0e-12, "identity map uc[2]"));
48 PetscFunctionReturn(0);
49}
50/**
51 * @brief Tests contravariant velocity computation for scaled axes.
52 */
53
55{
56 const PetscReal J[3][3] = {
57 {2.0, 0.0, 0.0},
58 {0.0, 3.0, 0.0},
59 {0.0, 0.0, 4.0},
60 };
61 const PetscReal u[3] = {2.0, 3.0, 4.0};
62 PetscReal uc[3] = {0.0, 0.0, 0.0};
63
64 PetscFunctionBeginUser;
65 PetscCall(MetricVelocityContravariant(J, 24.0, u, uc));
66 PetscCall(PicurvAssertRealNear(1.0, uc[0], 1.0e-12, "scaled map uc[0]"));
67 PetscCall(PicurvAssertRealNear(1.0, uc[1], 1.0e-12, "scaled map uc[1]"));
68 PetscCall(PicurvAssertRealNear(1.0, uc[2], 1.0e-12, "scaled map uc[2]"));
69 PetscFunctionReturn(0);
70}
71/**
72 * @brief Tests face-normal and face-area calculation on an axis-aligned cell.
73 */
74
76{
77 const Cmpnts csi = {2.0, 0.0, 0.0};
78 const Cmpnts eta = {0.0, 3.0, 0.0};
79 const Cmpnts zet = {0.0, 0.0, 4.0};
80 double ni[3] = {0.0, 0.0, 0.0};
81 double nj[3] = {0.0, 0.0, 0.0};
82 double nk[3] = {0.0, 0.0, 0.0};
83 double Ai = 0.0, Aj = 0.0, Ak = 0.0;
84
85 PetscFunctionBeginUser;
86 PetscCall(CalculateFaceNormalAndArea(csi, eta, zet, ni, nj, nk, &Ai, &Aj, &Ak));
87 PetscCall(PicurvAssertRealNear(2.0, Ai, 1.0e-12, "Ai"));
88 PetscCall(PicurvAssertRealNear(3.0, Aj, 1.0e-12, "Aj"));
89 PetscCall(PicurvAssertRealNear(4.0, Ak, 1.0e-12, "Ak"));
90 PetscCall(PicurvAssertRealNear(1.0, ni[0], 1.0e-12, "ni.x"));
91 PetscCall(PicurvAssertRealNear(0.0, ni[1], 1.0e-12, "ni.y"));
92 PetscCall(PicurvAssertRealNear(0.0, ni[2], 1.0e-12, "ni.z"));
93 PetscCall(PicurvAssertRealNear(0.0, nj[0], 1.0e-12, "nj.x"));
94 PetscCall(PicurvAssertRealNear(1.0, nj[1], 1.0e-12, "nj.y"));
95 PetscCall(PicurvAssertRealNear(0.0, nj[2], 1.0e-12, "nj.z"));
96 PetscCall(PicurvAssertRealNear(0.0, nk[0], 1.0e-12, "nk.x"));
97 PetscCall(PicurvAssertRealNear(0.0, nk[1], 1.0e-12, "nk.y"));
98 PetscCall(PicurvAssertRealNear(1.0, nk[2], 1.0e-12, "nk.z"));
99 PetscFunctionReturn(0);
100}
101/**
102 * @brief Tests characteristic length-scale computation on an axis-aligned cell.
103 */
104
106{
107 const Cmpnts csi = {2.0, 0.0, 0.0};
108 const Cmpnts eta = {0.0, 3.0, 0.0};
109 const Cmpnts zet = {0.0, 0.0, 4.0};
110 double dx = 0.0, dy = 0.0, dz = 0.0;
111
112 PetscFunctionBeginUser;
113 PetscCall(ComputeCellCharacteristicLengthScale(1.0, csi, eta, zet, &dx, &dy, &dz));
114 PetscCall(PicurvAssertRealNear(0.5, dx, 1.0e-12, "dx"));
115 PetscCall(PicurvAssertRealNear(1.0 / 3.0, dy, 1.0e-12, "dy"));
116 PetscCall(PicurvAssertRealNear(0.25, dz, 1.0e-12, "dz"));
117 PetscFunctionReturn(0);
118}
119/**
120 * @brief Tests the length scales of a wall-resolved cell, whose metrics are tiny.
121 *
122 * @details The determinant of the face-area vectors is the square of the cell volume for
123 * an orthogonal cell, so it falls as the sixth power of the spacing. The cell
124 * here is one from the Humphrey wall-resolved grid: 4.73e-4 x 4.73e-4 x 3.05e-2,
125 * whose determinant is about 4.7e-17. An absolute singularity floor rejected it
126 * and left the length scales uninitialized, which reached the solver as a NaN
127 * eddy viscosity rather than as an error.
128 */
130{
131 const double wall_normal = 4.73e-4, streamwise = 3.049381e-2;
132 const Cmpnts csi = {wall_normal * streamwise, 0.0, 0.0};
133 const Cmpnts eta = {0.0, wall_normal * streamwise, 0.0};
134 const Cmpnts zet = {0.0, 0.0, wall_normal * wall_normal};
135 const double volume = wall_normal * wall_normal * streamwise;
136 double dx = 0.0, dy = 0.0, dz = 0.0;
137
138 PetscFunctionBeginUser;
139 PetscCall(ComputeCellCharacteristicLengthScale(1.0 / volume, csi, eta, zet, &dx, &dy, &dz));
140 PetscCall(PicurvAssertRealNear(wall_normal, dx, 1.0e-15, "dx of a wall-resolved cell"));
141 PetscCall(PicurvAssertRealNear(wall_normal, dy, 1.0e-15, "dy of a wall-resolved cell"));
142 PetscCall(PicurvAssertRealNear(streamwise, dz, 1.0e-15, "dz of a wall-resolved cell"));
143 PetscFunctionReturn(0);
144}
145/**
146 * @brief Tests that MetricJacobian returns the physical Jacobian of a sheared, scaled cell, whose determinant is its volume.
147 */
149{
150 const PetscReal a = 0.5, b = 2.0, c = 3.0, shear = 0.7;
151 Cmpnts nodes[2][2][2], *rows[2][2], **planes[2];
152 PetscReal J[3][3], detJ;
153
154 PetscFunctionBeginUser;
155 for (PetscInt k = 0; k < 2; ++k) {
156 for (PetscInt j = 0; j < 2; ++j) {
157 for (PetscInt i = 0; i < 2; ++i) {
158 nodes[k][j][i].x = a * i + shear * k;
159 nodes[k][j][i].y = b * j;
160 nodes[k][j][i].z = c * k;
161 }
162 rows[k][j] = nodes[k][j];
163 }
164 planes[k] = rows[k];
165 }
166 PetscCall(MetricJacobian(NULL, (const Cmpnts ***)planes, 0, 0, 0, 0.3, 0.6, 0.9, J, &detJ));
167 PetscCall(PicurvAssertRealNear(a * b * c, detJ, 1.0e-14, "detJ of a parallelepiped is its volume"));
168 PetscCall(PicurvAssertRealNear(a, J[0][0], 1.0e-14, "dx/dxi is the i edge length"));
169 PetscCall(PicurvAssertRealNear(shear, J[0][2], 1.0e-14, "dx/dzta is the shear of the k edge"));
170 PetscCall(PicurvAssertRealNear(c, J[2][2], 1.0e-14, "dz/dzta is the k edge length"));
171 PetscFunctionReturn(0);
172}
173/**
174 * @brief Tests that a degenerate cell is still refused, and that the refusal propagates.
175 */
176
177static PetscErrorCode TestDegenerateCellIsRefused(void)
178{
179 const Cmpnts csi = {2.0, 0.0, 0.0};
180 const Cmpnts eta = {4.0, 0.0, 0.0}; /* parallel to csi: the rows are coplanar */
181 const Cmpnts zet = {0.0, 0.0, 3.0};
182 double dx = 0.0, dy = 0.0, dz = 0.0;
183 PetscErrorCode refused = 0;
184
185 PetscFunctionBeginUser;
186 PetscCall(PetscPushErrorHandler(PetscIgnoreErrorHandler, NULL));
187 refused = ComputeCellCharacteristicLengthScale(1.0, csi, eta, zet, &dx, &dy, &dz);
188 PetscCall(PetscPopErrorHandler());
189 PetscCall(PicurvAssertIntEqual(PETSC_ERR_MAT_LU_ZRPVT, refused,
190 "a coplanar cell is refused, and the caller is told"));
191 PetscFunctionReturn(0);
192}
193/**
194 * @brief Tests that the shared per-face planes reproduce each cell's own planes and are observable.
195 * @details On the fixture's uniform grid every face is planar, so a grid cell's gathered planes
196 * and a dual cell's must give the per-cell distances CalculateDistancesToCellFaces()
197 * computes from the same vertices, to rounding; the catalog's diagnostics must accept them.
198 */
199static PetscErrorCode TestFacePlanesMatchPerCellPlanes(void)
200{
201 SimCtx *simCtx = NULL;
202 UserCtx *user = NULL;
203 FacePlaneArrays arrays;
204 CellFacePlanes shared;
205 Cell cell;
206 PetscReal d_shared[NUM_FACES], d_cell[NUM_FACES], size;
207 const Cmpnts p = {0.31, 0.47, 0.52};
208 const Cmpnts ***cent = NULL;
209
210 PetscFunctionBeginUser;
211 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 6, 6, 6));
212 PetscCall(MetricComputeFacePlanes(user));
213
214 PetscCall(MetricGetFacePlaneArrays(user, PETSC_FALSE, &arrays));
215 for (PetscInt c = 0; c < 3; ++c) {
216 MetricGatherCellFacePlanes(&arrays, &user->info, c, c + 1, c, &shared);
217 PetscCall(RetrieveCurrentCell(user, c, c + 1, c, &cell));
218 PetscCall(CalculateDistancesToCellFaces(p, &cell, d_cell, 0.0, NULL));
219 PetscCall(CalculateDistancesToCellFacePlanes(p, &shared, d_shared, 0.0, NULL));
220 PetscCall(GetCellCharacteristicSize(&cell, &size));
221 PetscCall(PicurvAssertIntEqual(0, shared.degenerate, "grid cell faces all have planes"));
222 PetscCall(PicurvAssertRealNear(size, shared.size, 1.0e-15, "grid cell size"));
223 for (PetscInt f = 0; f < NUM_FACES; ++f) PetscCall(PicurvAssertRealNear(d_cell[f], d_shared[f], 1.0e-14, "grid cell face distance"));
224 }
225 PetscCall(MetricRestoreFacePlaneArrays(user, &arrays));
226
227 PetscCall(MetricGetFacePlaneArrays(user, PETSC_TRUE, &arrays));
228 PetscCall(DMDAVecGetArrayRead(user->fda, user->lCent, (void *)&cent));
229 for (PetscInt c = 1; c < 4; ++c) {
230 const PetscInt i = c, j = 2, k = 4 - c;
231 cell.vertices[0] = cent[k][j][i]; cell.vertices[1] = cent[k][j][i + 1];
232 cell.vertices[2] = cent[k][j + 1][i + 1]; cell.vertices[3] = cent[k][j + 1][i];
233 cell.vertices[4] = cent[k + 1][j + 1][i]; cell.vertices[5] = cent[k + 1][j + 1][i + 1];
234 cell.vertices[6] = cent[k + 1][j][i + 1]; cell.vertices[7] = cent[k + 1][j][i];
235 MetricGatherCellFacePlanes(&arrays, &user->info, i, j, k, &shared);
236 PetscCall(CalculateDistancesToCellFaces(p, &cell, d_cell, 0.0, NULL));
237 PetscCall(CalculateDistancesToCellFacePlanes(p, &shared, d_shared, 0.0, NULL));
238 for (PetscInt f = 0; f < NUM_FACES; ++f) PetscCall(PicurvAssertRealNear(d_cell[f], d_shared[f], 1.0e-14, "dual cell face distance"));
239 }
240 PetscCall(DMDAVecRestoreArrayRead(user->fda, user->lCent, (void *)&cent));
241 PetscCall(MetricRestoreFacePlaneArrays(user, &arrays));
242
244 PetscCall(LOG_FIELD_ANATOMY(user, FIELD_ID_DUAL_PLANE_CENTROID_K, "face-plane test"));
245 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
246 PetscFunctionReturn(0);
247}
248
249/**
250 * @brief Runs the unit-metric PETSc test binary.
251 */
252
253int main(int argc, char **argv)
254{
255 PetscErrorCode ierr;
256 const PicurvTestCase cases[] = {
257 {"invert-covariant-metric-diagonal", TestInvertCovariantMetricTensorDiagonal},
258 {"metric-velocity-contravariant-identity", TestMetricVelocityContravariantIdentity},
259 {"metric-velocity-contravariant-scaled-axes", TestMetricVelocityContravariantScaledAxes},
260 {"face-normal-and-area-axis-aligned", TestCalculateFaceNormalAndAreaAxisAligned},
261 {"characteristic-length-scale-axis-aligned", TestComputeCellCharacteristicLengthScaleAxisAligned},
262 {"characteristic-length-scale-wall-resolved-cell", TestComputeCellCharacteristicLengthScaleWallResolvedCell},
263 {"degenerate-cell-is-refused", TestDegenerateCellIsRefused},
264 {"metric-jacobian-is-cell-volume-for-parallelepiped", TestMetricJacobianIsCellVolumeForParallelepiped},
265 {"face-planes-match-per-cell-planes", TestFacePlanesMatchPerCellPlanes},
266 };
267
268 ierr = PetscInitialize(&argc, &argv, NULL, "PICurv metric tests");
269 if (ierr) {
270 return (int)ierr;
271 }
272
273 ierr = PicurvRunTests("unit-metric", cases, sizeof(cases) / sizeof(cases[0]));
274 if (ierr) {
275 PetscFinalize();
276 return (int)ierr;
277 }
278
279 ierr = PetscFinalize();
280 return (int)ierr;
281}
PetscErrorCode MetricRestoreFacePlaneArrays(UserCtx *user, FacePlaneArrays *arrays)
Closes arrays opened by MetricGetFacePlaneArrays().
Definition Metric.c:401
PetscErrorCode MetricJacobian(UserCtx *user, const Cmpnts ***X, PetscInt i, PetscInt j, PetscInt k, PetscReal xi, PetscReal eta, PetscReal zta, PetscReal J[3][3], PetscReal *detJ)
Evaluates the trilinear mapping Jacobian and its determinant in one cell.
Definition Metric.c:502
PetscErrorCode InvertCovariantMetricTensor(double covariantTensor[3][3], double contravariantTensor[3][3])
Inverts the 3x3 matrix of face-area vectors to obtain the covariant directions.
Definition Metric.c:595
void MetricGatherCellFacePlanes(const FacePlaneArrays *arrays, const DMDALocalInfo *info, PetscInt i, PetscInt j, PetscInt k, CellFacePlanes *planes)
Assembles one cell's six outward face planes from the shared per-face planes.
Definition Metric.c:421
PetscErrorCode MetricComputeFacePlanes(UserCtx *user)
Builds the block's particle face planes once, as catalogued per-face fields.
Definition Metric.c:208
PetscErrorCode MetricGetFacePlaneArrays(UserCtx *user, PetscBool dual, FacePlaneArrays *arrays)
Opens a family's face-plane arrays for reading; MetricComputeFacePlanes() must have run.
Definition Metric.c:376
PetscErrorCode ComputeCellCharacteristicLengthScale(PetscReal ajc, Cmpnts csi, Cmpnts eta, Cmpnts zet, double *dx, double *dy, double *dz)
Computes characteristic length scales (dx, dy, dz) for a curvilinear cell.
Definition Metric.c:688
PetscErrorCode CalculateFaceNormalAndArea(Cmpnts csi, Cmpnts eta, Cmpnts zet, double ni[3], double nj[3], double nk[3], double *Ai, double *Aj, double *Ak)
Computes the unit normal vectors and areas of the three faces of a computational cell.
Definition Metric.c:644
PetscErrorCode MetricVelocityContravariant(const PetscReal J[3][3], PetscReal detJ, const PetscReal u[3], PetscReal uc[3])
Converts a Cartesian velocity vector to contravariant logical components.
Definition Metric.c:560
Read-only flat arrays of one family of face planes (grid cells or dual cells).
Definition Metric.h:528
@ FIELD_ID_PLANE_NORMAL_I
@ FIELD_ID_DUAL_PLANE_CENTROID_K
PetscErrorCode LOG_FIELD_MIN_MAX(UserCtx *user, FieldId field_id)
Computes and logs the local and global min/max values of a 3-component vector field.
Definition logging.c:2501
PetscErrorCode LOG_FIELD_ANATOMY(UserCtx *user, FieldId field_id, const char *stage_name)
Logs the anatomy of a specified field at key boundary locations, respecting the solver's specific gri...
Definition logging.c:2903
static PetscErrorCode TestMetricVelocityContravariantScaledAxes(void)
Tests contravariant velocity computation for scaled axes.
Definition test_metric.c:54
int main(int argc, char **argv)
Runs the unit-metric PETSc test binary.
static PetscErrorCode TestInvertCovariantMetricTensorDiagonal(void)
Tests inversion of a diagonal covariant metric tensor.
Definition test_metric.c:13
static PetscErrorCode TestMetricVelocityContravariantIdentity(void)
Tests contravariant velocity computation for identity metrics.
Definition test_metric.c:33
static PetscErrorCode TestCalculateFaceNormalAndAreaAxisAligned(void)
Tests face-normal and face-area calculation on an axis-aligned cell.
Definition test_metric.c:75
static PetscErrorCode TestComputeCellCharacteristicLengthScaleAxisAligned(void)
Tests characteristic length-scale computation on an axis-aligned cell.
static PetscErrorCode TestComputeCellCharacteristicLengthScaleWallResolvedCell(void)
Tests the length scales of a wall-resolved cell, whose metrics are tiny.
static PetscErrorCode TestMetricJacobianIsCellVolumeForParallelepiped(void)
Tests that MetricJacobian returns the physical Jacobian of a sheared, scaled cell,...
static PetscErrorCode TestFacePlanesMatchPerCellPlanes(void)
Tests that the shared per-face planes reproduce each cell's own planes and are observable.
static PetscErrorCode TestDegenerateCellIsRefused(void)
Tests that a degenerate cell is still refused, and that the refusal propagates.
PetscErrorCode PicurvCreateMinimalContexts(SimCtx **simCtx_out, UserCtx **user_out, PetscInt mx, PetscInt my, PetscInt mz)
Builds minimal SimCtx and UserCtx fixtures for C unit tests.
PetscErrorCode PicurvAssertRealNear(PetscReal expected, PetscReal actual, PetscReal tol, const char *context)
Asserts that two real values agree within tolerance.
PetscErrorCode PicurvDestroyMinimalContexts(SimCtx **simCtx_ptr, UserCtx **user_ptr)
Destroys minimal SimCtx/UserCtx fixtures and all owned PETSc objects.
PetscErrorCode PicurvRunTests(const char *suite_name, const PicurvTestCase *cases, size_t case_count)
Runs a named C test suite and prints pass/fail progress markers.
PetscErrorCode PicurvAssertIntEqual(PetscInt expected, PetscInt actual, const char *context)
Asserts that two integer values are equal.
Shared declarations for the PICurv C test fixture and assertion layer.
Named test case descriptor consumed by PicurvRunTests.
Vec lCent
Definition variables.h:1244
PetscScalar x
Definition variables.h:145
PetscReal size
Mean edge length (GetCellCharacteristicSize()); 0 where unused.
Definition variables.h:232
PetscScalar z
Definition variables.h:145
PetscInt degenerate
Bit f set when face f's normal magnitude is below 1e-12.
Definition variables.h:231
DMDALocalInfo info
Definition variables.h:1169
PetscScalar y
Definition variables.h:145
@ NUM_FACES
Definition variables.h:197
Cmpnts vertices[8]
Coordinates of the eight vertices of the cell.
Definition variables.h:219
Defines the vertices of a single hexahedral grid cell.
Definition variables.h:218
The six face planes of one hexahedron, indexed by Face, and its characteristic size.
Definition variables.h:229
A 3D point or vector with PetscScalar components.
Definition variables.h:144
The master context for the entire simulation.
Definition variables.h:916
User-defined context containing data specific to a single computational grid level.
Definition variables.h:1157
PetscErrorCode CalculateDistancesToCellFacePlanes(const Cmpnts p, const CellFacePlanes *planes, PetscReal *d, const PetscReal threshold, Cmpnts *normals)
Signed distances from a point to precomputed cell face planes.
PetscErrorCode CalculateDistancesToCellFaces(const Cmpnts p, const Cell *cell, PetscReal *d, const PetscReal threshold, Cmpnts *normals)
Computes the signed distances from a point to each face of a cubic cell.
PetscErrorCode GetCellCharacteristicSize(const Cell *cell, PetscReal *cellSize)
Estimates a characteristic length of the cell for threshold scaling.
PetscErrorCode RetrieveCurrentCell(UserCtx *user, PetscInt idx, PetscInt idy, PetscInt idz, Cell *cell)
Retrieves the coordinates of the eight vertices of the current cell.