PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
 
Loading...
Searching...
No Matches
Functions | Variables
test_particle_kernels.c File Reference

C unit tests for particle walking-search helpers and the initial-value expression engine. More...

#include "test_support.h"
#include "Metric.h"
#include "walkingsearch.h"
#include "ParticleInitialConditions.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for test_particle_kernels.c:

Go to the source code of this file.

Functions

static PetscErrorCode TestCheckCellWithinLocalGrid (void)
 Tests whether a cell index lies within the local grid ownership range.
 
static PetscErrorCode TestInitializeTraversalParameters (void)
 Tests initialization of traversal parameters for particle search.
 
static PetscErrorCode TestRetrieveCurrentCell (void)
 Tests retrieval of the current cell from logical particle coordinates.
 
static void BuildShearedUnitCell (PetscReal shear, Cell *cell)
 Fills a cell with the unit cube's vertices in walking-search order, x sheared by shear * z.
 
static PetscErrorCode TestReflectParticleAcrossCellFace (void)
 Tests the face mirror on a flat face, a sheared face, and a two-face corner.
 
static PetscErrorCode TestPlaceParticleInsideCell (void)
 Tests that the reflection-cap fallback places a far-outside point just inside the cell.
 
static PetscErrorCode TestExpressionConformance (void)
 Tests the runtime evaluator against every shared conformance case.
 
static PetscErrorCode TestExpressionSpecialFunctions (void)
 Tests the Bessel and Kelvin functions against 30-digit references from their series, and that a non-integer Bessel order gives NaN.
 
static PetscErrorCode TestExpressionClosedFormProfiles (void)
 Tests the documented closed-form sources.
 
static PetscErrorCode TestExpressionRejectsOutsideLanguage (void)
 Tests that the compiler refuses what the Python validator also refuses.
 
static PetscErrorCode TestExpressionDrawsArePureAndKeyed (void)
 Tests that random draws are pure functions of their key.
 
int main (int argc, char **argv)
 Runs the unit-particles PETSc test binary.
 

Variables

static const char *const kConformanceNames [] = {"x", "y", "z"}
 

Detailed Description

C unit tests for particle walking-search helpers and the initial-value expression engine.

Definition in file test_particle_kernels.c.

Function Documentation

◆ TestCheckCellWithinLocalGrid()

static PetscErrorCode TestCheckCellWithinLocalGrid ( void  )
static

Tests whether a cell index lies within the local grid ownership range.

Definition at line 19 of file test_particle_kernels.c.

20{
21 SimCtx *simCtx = NULL;
22 UserCtx *user = NULL;
23 PetscBool within = PETSC_FALSE;
24
25 PetscFunctionBeginUser;
26 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 4, 4, 4));
27
28 PetscCall(CheckCellWithinLocalGrid(user, 1, 1, 1, &within));
29 PetscCall(PicurvAssertBool(within, "cell (1,1,1) should be within the serial local grid"));
30
31 PetscCall(CheckCellWithinLocalGrid(user, 4, 1, 1, &within));
32 PetscCall(PicurvAssertBool((PetscBool)!within, "cell (4,1,1) should fall outside the valid local cell range"));
33
34 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
35 PetscFunctionReturn(0);
36}
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 PicurvDestroyMinimalContexts(SimCtx **simCtx_ptr, UserCtx **user_ptr)
Destroys minimal SimCtx/UserCtx fixtures and all owned PETSc objects.
PetscErrorCode PicurvAssertBool(PetscBool value, const char *context)
Asserts that one boolean condition is true.
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 CheckCellWithinLocalGrid(UserCtx *user, PetscInt idx, PetscInt idy, PetscInt idz, PetscBool *is_within)
Checks if the current cell indices are within the local grid boundaries.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestInitializeTraversalParameters()

static PetscErrorCode TestInitializeTraversalParameters ( void  )
static

Tests initialization of traversal parameters for particle search.

Definition at line 41 of file test_particle_kernels.c.

42{
43 SimCtx *simCtx = NULL;
44 UserCtx *user = NULL;
45 Particle particle;
46 PetscInt idx = -1, idy = -1, idz = -1, steps = -1;
47
48 PetscFunctionBeginUser;
49 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 4, 4, 4));
50 PetscCall(PetscMemzero(&particle, sizeof(particle)));
51
52 particle.PID = 42;
53 particle.cell[0] = 1;
54 particle.cell[1] = 1;
55 particle.cell[2] = 1;
56
57 PetscCall(InitializeTraversalParameters(user, &particle, &idx, &idy, &idz, &steps));
58 PetscCall(PicurvAssertIntEqual(1, idx, "InitializeTraversalParameters should preserve prior i"));
59 PetscCall(PicurvAssertIntEqual(1, idy, "InitializeTraversalParameters should preserve prior j"));
60 PetscCall(PicurvAssertIntEqual(1, idz, "InitializeTraversalParameters should preserve prior k"));
61 PetscCall(PicurvAssertIntEqual(0, steps, "InitializeTraversalParameters should reset traversal steps"));
62
63 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
64 PetscFunctionReturn(0);
65}
PetscErrorCode PicurvAssertIntEqual(PetscInt expected, PetscInt actual, const char *context)
Asserts that two integer values are equal.
PetscInt cell[3]
Definition variables.h:238
PetscInt64 PID
Definition variables.h:237
Defines a particle's core properties for Lagrangian tracking.
Definition variables.h:236
PetscErrorCode InitializeTraversalParameters(UserCtx *user, Particle *particle, PetscInt *idx, PetscInt *idy, PetscInt *idz, PetscInt *traversal_steps)
Initializes traversal parameters for locating a particle.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestRetrieveCurrentCell()

static PetscErrorCode TestRetrieveCurrentCell ( void  )
static

Tests retrieval of the current cell from logical particle coordinates.

Definition at line 70 of file test_particle_kernels.c.

71{
72 SimCtx *simCtx = NULL;
73 UserCtx *user = NULL;
74 Cell cell;
75
76 PetscFunctionBeginUser;
77 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 4, 4, 4));
78 PetscCall(PetscMemzero(&cell, sizeof(cell)));
79
80 PetscCall(RetrieveCurrentCell(user, 1, 1, 1, &cell));
81 PetscCall(PicurvAssertRealNear(0.25, cell.vertices[0].x, 1.0e-12, "vertex 0 x coordinate"));
82 PetscCall(PicurvAssertRealNear(0.25, cell.vertices[0].y, 1.0e-12, "vertex 0 y coordinate"));
83 PetscCall(PicurvAssertRealNear(0.25, cell.vertices[0].z, 1.0e-12, "vertex 0 z coordinate"));
84 PetscCall(PicurvAssertRealNear(0.50, cell.vertices[5].x, 1.0e-12, "vertex 5 x coordinate"));
85 PetscCall(PicurvAssertRealNear(0.50, cell.vertices[5].y, 1.0e-12, "vertex 5 y coordinate"));
86 PetscCall(PicurvAssertRealNear(0.50, cell.vertices[5].z, 1.0e-12, "vertex 5 z coordinate"));
87
88 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
89 PetscFunctionReturn(0);
90}
PetscErrorCode PicurvAssertRealNear(PetscReal expected, PetscReal actual, PetscReal tol, const char *context)
Asserts that two real values agree within tolerance.
PetscScalar x
Definition variables.h:145
PetscScalar z
Definition variables.h:145
PetscScalar y
Definition variables.h:145
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
PetscErrorCode RetrieveCurrentCell(UserCtx *user, PetscInt idx, PetscInt idy, PetscInt idz, Cell *cell)
Retrieves the coordinates of the eight vertices of the current cell.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ BuildShearedUnitCell()

static void BuildShearedUnitCell ( PetscReal  shear,
Cell *  cell 
)
static

Fills a cell with the unit cube's vertices in walking-search order, x sheared by shear * z.

Definition at line 94 of file test_particle_kernels.c.

95{
96 const PetscReal corner[8][3] = {{0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 1, 0},
97 {0, 1, 1}, {1, 1, 1}, {1, 0, 1}, {0, 0, 1}};
98 for (int v = 0; v < 8; ++v) {
99 cell->vertices[v].x = corner[v][0] + shear * corner[v][2];
100 cell->vertices[v].y = corner[v][1];
101 cell->vertices[v].z = corner[v][2];
102 }
103}
Here is the caller graph for this function:

◆ TestReflectParticleAcrossCellFace()

static PetscErrorCode TestReflectParticleAcrossCellFace ( void  )
static

Tests the face mirror on a flat face, a sheared face, and a two-face corner.

Definition at line 108 of file test_particle_kernels.c.

109{
110 Cell cell;
111 CellFacePlanes planes;
112 Cmpnts p;
113
114 PetscFunctionBeginUser;
115 BuildShearedUnitCell(0.0, &cell);
116 MetricBuildCellFacePlanes(&cell, &planes);
117 p.x = -0.2; p.y = 0.5; p.z = 0.5;
118 PetscCall(ReflectParticleAcrossCellFace(&planes, LEFT, &p));
119 PetscCall(PicurvAssertRealNear(0.2, p.x, 1.0e-14, "flat face: x mirrors about x = 0"));
120 PetscCall(PicurvAssertRealNear(0.5, p.y, 1.0e-14, "flat face: y is unchanged"));
121 PetscCall(PicurvAssertRealNear(0.5, p.z, 1.0e-14, "flat face: z is unchanged"));
122
123 /* LEFT face of the sheared cell is the plane x = 0.5 z; (-0.3, 0.5, 0.4) mirrors to (0.5, 0.5, 0). */
124 BuildShearedUnitCell(0.5, &cell);
125 MetricBuildCellFacePlanes(&cell, &planes);
126 p.x = -0.3; p.y = 0.5; p.z = 0.4;
127 PetscCall(ReflectParticleAcrossCellFace(&planes, LEFT, &p));
128 PetscCall(PicurvAssertRealNear(0.5, p.x, 1.0e-14, "sheared face: mirrored x"));
129 PetscCall(PicurvAssertRealNear(0.5, p.y, 1.0e-14, "sheared face: y is unchanged"));
130 PetscCall(PicurvAssertRealNear(0.0, p.z, 1.0e-14, "sheared face: mirrored z"));
131
132 /* Corner: reflecting about LEFT and then BOTTOM mirrors both coordinates. */
133 BuildShearedUnitCell(0.0, &cell);
134 MetricBuildCellFacePlanes(&cell, &planes);
135 p.x = -0.1; p.y = -0.2; p.z = 0.5;
136 PetscCall(ReflectParticleAcrossCellFace(&planes, LEFT, &p));
137 PetscCall(ReflectParticleAcrossCellFace(&planes, BOTTOM, &p));
138 PetscCall(PicurvAssertRealNear(0.1, p.x, 1.0e-14, "corner: x mirrored"));
139 PetscCall(PicurvAssertRealNear(0.2, p.y, 1.0e-14, "corner: y mirrored"));
140 PetscFunctionReturn(0);
141}
void MetricBuildCellFacePlanes(const Cell *cell, CellFacePlanes *planes)
Builds the six face planes of a hexahedron, as the particle search tests them.
Definition Metric.c:108
static void BuildShearedUnitCell(PetscReal shear, Cell *cell)
Fills a cell with the unit cube's vertices in walking-search order, x sheared by shear * z.
@ BOTTOM
Definition variables.h:197
@ LEFT
Definition variables.h:197
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
PetscErrorCode ReflectParticleAcrossCellFace(const CellFacePlanes *planes, PetscInt face, Cmpnts *p)
Mirrors a point about the plane of one face of a cell.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestPlaceParticleInsideCell()

static PetscErrorCode TestPlaceParticleInsideCell ( void  )
static

Tests that the reflection-cap fallback places a far-outside point just inside the cell.

Definition at line 146 of file test_particle_kernels.c.

147{
148 Cell cell;
149 CellFacePlanes planes;
150 Cmpnts p;
151 PetscBool placed = PETSC_FALSE;
152 PetscReal d[NUM_FACES];
153 PetscInt position = -1;
154
155 PetscFunctionBeginUser;
156 BuildShearedUnitCell(0.0, &cell);
157 p.x = -1.0; p.y = 0.5; p.z = 0.5;
158 PetscCall(PlaceParticleInsideCell(&cell, &p, &placed));
159 PetscCall(PicurvAssertBool(placed, "a point outside one face of a box cell can be placed"));
160 PetscCall(PicurvAssertBool((PetscBool)(p.x > 0.0 && p.x < 1.0e-5), "placed just inside the crossed face"));
161 MetricBuildCellFacePlanes(&cell, &planes);
162 PetscCall(GetCellCharacteristicSize(&cell, &planes.size));
163 PetscCall(EvaluateParticlePositionInPlanes(&planes, d, p, &position, 1.0e-11));
164 PetscCall(PicurvAssertBool((PetscBool)(position >= 0), "the placed point evaluates inside the cell"));
165 PetscFunctionReturn(0);
166}
PetscReal size
Mean edge length (GetCellCharacteristicSize()); 0 where unused.
Definition variables.h:232
@ NUM_FACES
Definition variables.h:197
PetscErrorCode EvaluateParticlePositionInPlanes(const CellFacePlanes *planes, PetscReal *d, const Cmpnts p, PetscInt *position, const PetscReal threshold)
Classifies a point against a cell's precomputed face planes.
PetscErrorCode GetCellCharacteristicSize(const Cell *cell, PetscReal *cellSize)
Estimates a characteristic length of the cell for threshold scaling.
PetscErrorCode PlaceParticleInsideCell(const Cell *cell, Cmpnts *p, PetscBool *placed)
Moves a point to just inside a cell along the segment toward the cell centroid.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestExpressionConformance()

static PetscErrorCode TestExpressionConformance ( void  )
static

Tests the runtime evaluator against every shared conformance case.

tests/fixtures/expression_conformance.txt is also evaluated by generators/ic.gen in tests/test_expression_language.py, so the two implementations of the language cannot drift apart unnoticed.

Definition at line 175 of file test_particle_kernels.c.

176{
177 FILE *file = fopen("tests/fixtures/expression_conformance.txt", "r");
178 char line[1024];
179 PetscInt cases = 0;
180
181 PetscFunctionBeginUser;
182 PetscCheck(file, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Cannot open the expression conformance cases.");
183 while (fgets(line, sizeof(line), file)) {
184 char *fields[5];
185 PetscInt count = 0;
186 PicurvExpression *expression = NULL;
187 PetscReal values[3], result = 0.0;
188
189 if (line[0] == '#' || line[0] == '\n') continue;
190 for (char *cursor = line; count < 5; ++count) {
191 fields[count] = cursor;
192 char *separator = strchr(cursor, ';');
193 if (!separator) { ++count; break; }
194 *separator = '\0';
195 cursor = separator + 1;
196 }
197 PetscCheck(count == 5, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Malformed conformance line.");
198 for (int v = 0; v < 3; ++v) values[v] = strtod(fields[v + 1], NULL);
199 PetscCall(PicurvExpressionCompile(fields[0], kConformanceNames, 3, PETSC_FALSE, &expression));
200 PetscCall(PicurvExpressionEvaluate(expression, values, NULL, &result));
201 PetscCall(PicurvAssertRealNear(strtod(fields[4], NULL), result, 1.0e-12, fields[0]));
202 PetscCall(PicurvExpressionDestroy(&expression));
203 ++cases;
204 }
205 fclose(file);
206 PetscCall(PicurvAssertBool((PetscBool)(cases >= 20), "the conformance file should hold its cases"));
207 PetscFunctionReturn(0);
208}
PetscErrorCode PicurvExpressionEvaluate(const PicurvExpression *expression, const PetscReal *values, const PicurvExpressionDrawKey *key, PetscReal *result)
Evaluate a compiled expression.
Definition expression.c:724
PetscErrorCode PicurvExpressionDestroy(PicurvExpression **expression)
Free a compiled expression.
Definition expression.c:818
PetscErrorCode PicurvExpressionCompile(const char *text, const char *const *names, PetscInt name_count, PetscBool allow_random, PicurvExpression **expression)
Compile one expression against a list of variable names.
Definition expression.c:551
static const char *const kConformanceNames[]
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestExpressionSpecialFunctions()

static PetscErrorCode TestExpressionSpecialFunctions ( void  )
static

Tests the Bessel and Kelvin functions against 30-digit references from their series, and that a non-integer Bessel order gives NaN.

Definition at line 214 of file test_particle_kernels.c.

215{
216 const struct { const char *text; PetscReal expected; } cases[] = {
217 {"besselj(0, 1)", 0.76519768655796661}, {"besselj(1, x)", 0.4400505857449335},
218 {"besselj(2, 2.5)", 0.44605905843961724}, {"besselj(0, 10)", -0.24593576445134835},
219 {"bessely(0, 1)", 0.088256964215676956}, {"bessely(0, 2.5)", 0.49807035961523188},
220 {"ber(1)", 0.98438178121308684}, {"bei(x)", 0.24956604003665972},
221 {"ber(5)", -6.2300824786663576}, {"bei(5)", 0.11603438155020038},
222 {"ber(20)", 47489.370265061763}, {"bei(20)", 114775.19736006622},
223 /* Either side of the change to the asymptotic expansion at 30, and well beyond the series' reach. */
224 {"ber(29.5)", -3674268.2459448087}, {"bei(29.5)", 84356546.89143799},
225 {"ber(30.5)", -114878788.18171336}, {"bei(30.5)", 123131394.62887576},
226 {"ber(50)", -117623968512357.44}, {"bei(50)", -50192646254462.21},
227 {"ber(120)", -2.417573872141002e+35}, {"bei(120)", 9.200088423773959e+34},
228 {"ber(-40)", -112596696872.07408}, {"bei(-40)", 45628130285.64126},
229 };
230 const PetscReal values[3] = {1.0, 0.0, 0.0};
231
232 PetscFunctionBeginUser;
233 for (size_t n = 0; n < sizeof(cases) / sizeof(cases[0]); ++n) {
234 PicurvExpression *expression = NULL;
235 PetscReal result = 0.0;
236 PetscCall(PicurvExpressionCompile(cases[n].text, kConformanceNames, 3, PETSC_FALSE, &expression));
237 PetscCall(PicurvExpressionEvaluate(expression, values, NULL, &result));
238 PetscCall(PicurvAssertRealNear(cases[n].expected, result, 1.0e-12 * PetscMax(1.0, PetscAbsReal(cases[n].expected)),
239 cases[n].text));
240 PetscCall(PicurvExpressionDestroy(&expression));
241 }
242 {
243 PicurvExpression *expression = NULL;
244 PetscReal result = 0.0;
245 PetscCall(PicurvExpressionCompile("besselj(0.5, 1)", kConformanceNames, 3, PETSC_FALSE, &expression));
246 PetscCall(PicurvExpressionEvaluate(expression, values, NULL, &result));
247 PetscCall(PicurvAssertBool((PetscBool)PetscIsNanReal(result), "a non-integer order is not a value"));
248 PetscCall(PicurvExpressionDestroy(&expression));
249 }
250 PetscFunctionReturn(0);
251}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestExpressionClosedFormProfiles()

static PetscErrorCode TestExpressionClosedFormProfiles ( void  )
static

Tests the documented closed-form sources.

The Womersley source, written in real arithmetic with ber and bei, against the complex solution Re{(K/i w)(1 - J0(i^1.5 a s)/J0(i^1.5 a)) e^{iwt}} evaluated independently from its series (K = 1, w = 2 pi, a = 5, R = 0.5); the rectangular-duct series against the Python evaluator.

Definition at line 259 of file test_particle_kernels.c.

260{
261 static const char *const names[] = {"x", "y", "z", "t"};
262 const char *text =
263 "(1.0/(2*pi))*((ber(5.0*(sqrt(x**2 + y**2)/0.5))*bei(5.0) - bei(5.0*(sqrt(x**2 + y**2)/0.5))*ber(5.0))"
264 "/(ber(5.0)**2 + bei(5.0)**2)*cos(2*pi*t) + (1 - (ber(5.0*(sqrt(x**2 + y**2)/0.5))*ber(5.0)"
265 " + bei(5.0*(sqrt(x**2 + y**2)/0.5))*bei(5.0))/(ber(5.0)**2 + bei(5.0)**2))*sin(2*pi*t))";
266 const PetscReal points[4][5] = {{0.0, 0.0, 0.0, 0.1, 0.10894419422254727},
267 {0.2, 0.1, 0.0, 0.3, 0.15619922873565129},
268 {0.3, -0.2, 0.0, 0.7, -0.1332246948462772},
269 {0.0, 0.45, 0.0, 0.9, 0.004632857508023867}};
270 PicurvExpression *expression = NULL;
271
272 PetscFunctionBeginUser;
273 PetscCall(PicurvExpressionCompile(text, names, 4, PETSC_FALSE, &expression));
274 for (PetscInt n = 0; n < 4; ++n) {
275 PetscReal result = 0.0;
276 PetscCall(PicurvExpressionEvaluate(expression, points[n], NULL, &result));
277 PetscCall(PicurvAssertRealNear(points[n][4], result, 1.0e-12, "Womersley velocity"));
278 }
279 PetscCall(PicurvExpressionDestroy(&expression));
280 {
281 /* The documented rectangular-duct series (unit bulk, square section, xi -> x, eta -> y),
282 against the Python evaluator's value at (0.3, 0.6). */
283 const char *duct =
284 "(1.0)*sum(k, 0, 99, (-1)**k/(2*k+1)**3 *(1 - (exp(abs((2*k+1)*pi*(1.0)*(y - 0.5)) - (2*k+1"
285 ")*pi*(1.0)/2) + exp(-abs((2*k+1)*pi*(1.0)*(y - 0.5)) - (2*k+1)*pi*(1.0)/2))/(1 + exp(-(2*k"
286 "+1)*pi*(1.0)))) *cos((2*k+1)*pi*(x - 0.5))) / sum(k, 0, 99, 2/(pi*(2*k+1)**4)*(1 - 2*tanh("
287 "(2*k+1)*pi*(1.0)/2)/((2*k+1)*pi*(1.0))))";
288 const PetscReal at[4] = {0.3, 0.6, 0.0, 0.0};
289 PetscReal result = 0.0;
290 PetscCall(PicurvExpressionCompile(duct, names, 4, PETSC_FALSE, &expression));
291 PetscCall(PicurvExpressionEvaluate(expression, at, NULL, &result));
292 PetscCall(PicurvAssertRealNear(1.7442020681945691, result, 1.0e-12, "duct Poiseuille series"));
293 PetscCall(PicurvExpressionDestroy(&expression));
294 }
295 PetscFunctionReturn(0);
296}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestExpressionRejectsOutsideLanguage()

static PetscErrorCode TestExpressionRejectsOutsideLanguage ( void  )
static

Tests that the compiler refuses what the Python validator also refuses.

Definition at line 301 of file test_particle_kernels.c.

302{
303 const char *bad[] = {"0x1F", "1_000", "x // 2", "abs", "abs(1, 2)", "where(x, 1)", "foo",
304 "x +", "(x", "x y", "uniform(x)", "uniform(1.5)", "1.e", "not",
305 "sum(n, 0, 1.5, n)", "sum(n, 0, y, n)", "sum(x, 0, 2, x)", "sum(n, 0, 2)",
306 "sum(n, 0, 2, sum(n, 0, 1, n))", "sum(n, 0, 2, sum(m, 0, n, m))", "sum(n, 0, 2, n) + n", "sum(n, 0, 2000000, n)",
307 "sum(pi, 0, 2, 1)", "besselj(1)", "ber(1, 2)"};
308
309 PetscFunctionBeginUser;
310 for (size_t n = 0; n < sizeof(bad) / sizeof(bad[0]); ++n) {
311 PicurvExpression *expression = NULL;
312 PetscErrorCode refused;
313
314 PetscCall(PetscPushErrorHandler(PetscIgnoreErrorHandler, NULL));
315 refused = PicurvExpressionCompile(bad[n], kConformanceNames, 3, PETSC_TRUE, &expression);
316 PetscCall(PetscPopErrorHandler());
317 PetscCall(PicurvAssertIntEqual(PETSC_ERR_ARG_WRONG, refused, bad[n]));
318 }
319 {
320 PicurvExpression *expression = NULL;
321 PetscErrorCode refused;
322
323 PetscCall(PetscPushErrorHandler(PetscIgnoreErrorHandler, NULL));
324 refused = PicurvExpressionCompile("uniform()", kConformanceNames, 3, PETSC_FALSE, &expression);
325 PetscCall(PetscPopErrorHandler());
326 PetscCall(PicurvAssertIntEqual(PETSC_ERR_ARG_WRONG, refused, "draws need allow_random"));
327 }
328 PetscFunctionReturn(0);
329}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestExpressionDrawsArePureAndKeyed()

static PetscErrorCode TestExpressionDrawsArePureAndKeyed ( void  )
static

Tests that random draws are pure functions of their key.

The same particle always draws the same number; a different particle, event, or field default stream draws independently; an explicit stream is shared across fields; and the normal draw has zero mean and unit variance.

Definition at line 337 of file test_particle_kernels.c.

338{
339 PicurvExpression *uniform = NULL, *shared = NULL, *normal = NULL;
340 PicurvExpressionDrawKey key = {12345, 7, 0, 6};
341 PetscReal a, b, sum = 0.0, square = 0.0;
342 const PetscInt samples = 20000;
343
344 PetscFunctionBeginUser;
345 PetscCall(PicurvExpressionCompile("uniform()", kConformanceNames, 3, PETSC_TRUE, &uniform));
346 PetscCall(PicurvExpressionCompile("uniform(3)", kConformanceNames, 3, PETSC_TRUE, &shared));
347 PetscCall(PicurvExpressionCompile("normal()", kConformanceNames, 3, PETSC_TRUE, &normal));
348
349 PetscCall(PicurvExpressionEvaluate(uniform, NULL, &key, &a));
350 PetscCall(PicurvExpressionEvaluate(uniform, NULL, &key, &b));
351 PetscCall(PicurvAssertBool((PetscBool)(a == b && a >= 0.0 && a < 1.0), "a draw is pure and in [0, 1)"));
352 key.pid = 8;
353 PetscCall(PicurvExpressionEvaluate(uniform, NULL, &key, &b));
354 PetscCall(PicurvAssertBool((PetscBool)(a != b), "another particle draws independently"));
355 key.pid = 7;
356 key.salt = 1;
357 PetscCall(PicurvExpressionEvaluate(uniform, NULL, &key, &b));
358 PetscCall(PicurvAssertBool((PetscBool)(a != b), "another event draws independently"));
359 key.salt = 0;
360 key.field = 5;
361 PetscCall(PicurvExpressionEvaluate(uniform, NULL, &key, &b));
362 PetscCall(PicurvAssertBool((PetscBool)(a != b), "another field's default stream is independent"));
363 key.field = 6;
364 key.grid = PETSC_TRUE;
365 PetscCall(PicurvExpressionEvaluate(uniform, NULL, &key, &b));
366 PetscCall(PicurvAssertBool((PetscBool)(a != b), "a grid point with the same number as a particle draws independently"));
367 key.grid = PETSC_FALSE;
368 key.field = 5;
369 PetscCall(PicurvExpressionEvaluate(shared, NULL, &key, &a));
370 key.field = 6;
371 PetscCall(PicurvExpressionEvaluate(shared, NULL, &key, &b));
372 PetscCall(PicurvAssertBool((PetscBool)(a == b), "an explicit stream is shared across fields"));
373
374 for (PetscInt pid = 0; pid < samples; ++pid) {
375 key.pid = pid;
376 PetscCall(PicurvExpressionEvaluate(normal, NULL, &key, &a));
377 sum += a;
378 square += a * a;
379 }
380 PetscCall(PicurvAssertRealNear(0.0, sum / samples, 0.03, "normal draws have zero mean"));
381 PetscCall(PicurvAssertRealNear(1.0, square / samples, 0.04, "normal draws have unit variance"));
382
383 PetscCall(PicurvExpressionDestroy(&uniform));
384 PetscCall(PicurvExpressionDestroy(&shared));
385 PetscCall(PicurvExpressionDestroy(&normal));
386 PetscFunctionReturn(0);
387}
PetscInt64 pid
Global identity of the particle, or on the grid of the cell or face cell.
Definition expression.h:87
PetscBool grid
The draw is for a grid point, not a particle.
Definition expression.h:91
PetscInt64 salt
Event: for a particle what produced it, 0 for the t=0 population; on a face the step; in a cell PICUR...
Definition expression.h:88
PetscInt64 field
Field or component whose expression draws, the default stream's namespace.
Definition expression.h:90
What a random draw is keyed on, besides its stream.
Definition expression.h:85
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char **  argv 
)

Runs the unit-particles PETSc test binary.

Definition at line 393 of file test_particle_kernels.c.

394{
395 PetscErrorCode ierr;
396 const PicurvTestCase cases[] = {
397 {"check-cell-within-local-grid", TestCheckCellWithinLocalGrid},
398 {"initialize-traversal-parameters", TestInitializeTraversalParameters},
399 {"retrieve-current-cell", TestRetrieveCurrentCell},
400 {"reflect-particle-across-cell-face", TestReflectParticleAcrossCellFace},
401 {"place-particle-inside-cell", TestPlaceParticleInsideCell},
402 {"expression-conformance", TestExpressionConformance},
403 {"expression-rejects-outside-language", TestExpressionRejectsOutsideLanguage},
404 {"expression-special-functions", TestExpressionSpecialFunctions},
405 {"expression-closed-form-profiles", TestExpressionClosedFormProfiles},
406 {"expression-draws-are-pure-and-keyed", TestExpressionDrawsArePureAndKeyed},
407 };
408
409 ierr = PetscInitialize(&argc, &argv, NULL, "PICurv particle kernel tests");
410 if (ierr) {
411 return (int)ierr;
412 }
413
414 ierr = PicurvRunTests("unit-particles", cases, sizeof(cases) / sizeof(cases[0]));
415 if (ierr) {
416 PetscFinalize();
417 return (int)ierr;
418 }
419
420 ierr = PetscFinalize();
421 return (int)ierr;
422}
static PetscErrorCode TestCheckCellWithinLocalGrid(void)
Tests whether a cell index lies within the local grid ownership range.
static PetscErrorCode TestExpressionConformance(void)
Tests the runtime evaluator against every shared conformance case.
static PetscErrorCode TestExpressionRejectsOutsideLanguage(void)
Tests that the compiler refuses what the Python validator also refuses.
static PetscErrorCode TestInitializeTraversalParameters(void)
Tests initialization of traversal parameters for particle search.
static PetscErrorCode TestExpressionSpecialFunctions(void)
Tests the Bessel and Kelvin functions against 30-digit references from their series,...
static PetscErrorCode TestRetrieveCurrentCell(void)
Tests retrieval of the current cell from logical particle coordinates.
static PetscErrorCode TestExpressionDrawsArePureAndKeyed(void)
Tests that random draws are pure functions of their key.
static PetscErrorCode TestExpressionClosedFormProfiles(void)
Tests the documented closed-form sources.
static PetscErrorCode TestReflectParticleAcrossCellFace(void)
Tests the face mirror on a flat face, a sheared face, and a two-face corner.
static PetscErrorCode TestPlaceParticleInsideCell(void)
Tests that the reflection-cap fallback places a far-outside point just inside the cell.
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.
Named test case descriptor consumed by PicurvRunTests.
Here is the call graph for this function:

Variable Documentation

◆ kConformanceNames

const char* const kConformanceNames[] = {"x", "y", "z"}
static

Definition at line 167 of file test_particle_kernels.c.

167{"x", "y", "z"};