PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
 
Loading...
Searching...
No Matches
test_particle_kernels.c
Go to the documentation of this file.
1/**
2 * @file test_particle_kernels.c
3 * @brief C unit tests for particle walking-search helpers and the initial-value expression engine.
4 */
5
6#include "test_support.h"
7#include "Metric.h"
8
9#include "walkingsearch.h"
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15/**
16 * @brief Tests whether a cell index lies within the local grid ownership range.
17 */
18
19static PetscErrorCode TestCheckCellWithinLocalGrid(void)
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}
37/**
38 * @brief Tests initialization of traversal parameters for particle search.
39 */
40
41static PetscErrorCode TestInitializeTraversalParameters(void)
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}
66/**
67 * @brief Tests retrieval of the current cell from logical particle coordinates.
68 */
69
70static PetscErrorCode TestRetrieveCurrentCell(void)
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}
91/**
92 * @brief Fills a cell with the unit cube's vertices in walking-search order, x sheared by `shear * z`.
93 */
94static void BuildShearedUnitCell(PetscReal shear, Cell *cell)
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}
104
105/**
106 * @brief Tests the face mirror on a flat face, a sheared face, and a two-face corner.
107 */
108static PetscErrorCode TestReflectParticleAcrossCellFace(void)
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}
142
143/**
144 * @brief Tests that the reflection-cap fallback places a far-outside point just inside the cell.
145 */
146static PetscErrorCode TestPlaceParticleInsideCell(void)
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}
167static const char *const kConformanceNames[] = {"x", "y", "z"};
168
169/**
170 * @brief Tests the runtime evaluator against every shared conformance case.
171 * @details `tests/fixtures/expression_conformance.txt` is also evaluated by
172 * `generators/ic.gen` in `tests/test_expression_language.py`, so the two
173 * implementations of the language cannot drift apart unnoticed.
174 */
175static PetscErrorCode TestExpressionConformance(void)
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}
209
210/**
211 * @brief Tests the Bessel and Kelvin functions against 30-digit references from their series,
212 * and that a non-integer Bessel order gives NaN.
213 */
214static PetscErrorCode TestExpressionSpecialFunctions(void)
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}
252
253/**
254 * @brief Tests the documented closed-form sources. The Womersley source, written in real
255 * 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}}`
256 * evaluated independently from its series (K = 1, w = 2 pi, a = 5, R = 0.5); the
257 * rectangular-duct series against the Python evaluator.
258 */
259static PetscErrorCode TestExpressionClosedFormProfiles(void)
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}
297
298/**
299 * @brief Tests that the compiler refuses what the Python validator also refuses.
300 */
301static PetscErrorCode TestExpressionRejectsOutsideLanguage(void)
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}
330
331/**
332 * @brief Tests that random draws are pure functions of their key.
333 * @details The same particle always draws the same number; a different particle, event,
334 * or field default stream draws independently; an explicit stream is shared
335 * across fields; and the normal draw has zero mean and unit variance.
336 */
337static PetscErrorCode TestExpressionDrawsArePureAndKeyed(void)
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}
388
389/**
390 * @brief Runs the unit-particles PETSc test binary.
391 */
392
393int main(int argc, char **argv)
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}
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
Configured initial values of particle-carried fields.
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
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
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
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
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.
int main(int argc, char **argv)
Runs the unit-particles PETSc test binary.
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 const char *const kConformanceNames[]
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 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 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.
PetscErrorCode PicurvAssertBool(PetscBool value, const char *context)
Asserts that one boolean condition is true.
Shared declarations for the PICurv C test fixture and assertion layer.
Named test case descriptor consumed by PicurvRunTests.
PetscInt cell[3]
Definition variables.h:238
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
PetscScalar y
Definition variables.h:145
@ BOTTOM
Definition variables.h:197
@ LEFT
Definition variables.h:197
@ NUM_FACES
Definition variables.h:197
PetscInt64 PID
Definition variables.h:237
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
Defines a particle's core properties for Lagrangian tracking.
Definition variables.h:236
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
Header file for particle location functions using the walking search algorithm.
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 ReflectParticleAcrossCellFace(const CellFacePlanes *planes, PetscInt face, Cmpnts *p)
Mirrors a point about the plane of one face of a cell.
PetscErrorCode PlaceParticleInsideCell(const Cell *cell, Cmpnts *p, PetscBool *placed)
Moves a point to just inside a cell along the segment toward the cell centroid.
PetscErrorCode RetrieveCurrentCell(UserCtx *user, PetscInt idx, PetscInt idy, PetscInt idz, Cell *cell)
Retrieves the coordinates of the eight vertices of the current cell.
PetscErrorCode InitializeTraversalParameters(UserCtx *user, Particle *particle, PetscInt *idx, PetscInt *idy, PetscInt *idz, PetscInt *traversal_steps)
Initializes traversal parameters for locating a particle.
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.