23 PetscBool within = PETSC_FALSE;
25 PetscFunctionBeginUser;
29 PetscCall(
PicurvAssertBool(within,
"cell (1,1,1) should be within the serial local grid"));
32 PetscCall(
PicurvAssertBool((PetscBool)!within,
"cell (4,1,1) should fall outside the valid local cell range"));
35 PetscFunctionReturn(0);
46 PetscInt idx = -1, idy = -1, idz = -1, steps = -1;
48 PetscFunctionBeginUser;
50 PetscCall(PetscMemzero(&particle,
sizeof(particle)));
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"));
64 PetscFunctionReturn(0);
76 PetscFunctionBeginUser;
78 PetscCall(PetscMemzero(&cell,
sizeof(cell)));
89 PetscFunctionReturn(0);
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];
114 PetscFunctionBeginUser;
117 p.
x = -0.2; p.
y = 0.5; p.
z = 0.5;
126 p.
x = -0.3; p.
y = 0.5; p.
z = 0.4;
135 p.
x = -0.1; p.
y = -0.2; p.
z = 0.5;
140 PetscFunctionReturn(0);
151 PetscBool placed = PETSC_FALSE;
153 PetscInt position = -1;
155 PetscFunctionBeginUser;
157 p.
x = -1.0; p.
y = 0.5; p.
z = 0.5;
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"));
164 PetscCall(
PicurvAssertBool((PetscBool)(position >= 0),
"the placed point evaluates inside the cell"));
165 PetscFunctionReturn(0);
177 FILE *file = fopen(
"tests/fixtures/expression_conformance.txt",
"r");
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)) {
187 PetscReal values[3], result = 0.0;
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; }
195 cursor = separator + 1;
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);
206 PetscCall(
PicurvAssertBool((PetscBool)(cases >= 20),
"the conformance file should hold its cases"));
207 PetscFunctionReturn(0);
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},
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},
230 const PetscReal values[3] = {1.0, 0.0, 0.0};
232 PetscFunctionBeginUser;
233 for (
size_t n = 0; n <
sizeof(cases) /
sizeof(cases[0]); ++n) {
235 PetscReal result = 0.0;
238 PetscCall(
PicurvAssertRealNear(cases[n].expected, result, 1.0e-12 * PetscMax(1.0, PetscAbsReal(cases[n].expected)),
244 PetscReal result = 0.0;
247 PetscCall(
PicurvAssertBool((PetscBool)PetscIsNanReal(result),
"a non-integer order is not a value"));
250 PetscFunctionReturn(0);
261 static const char *
const names[] = {
"x",
"y",
"z",
"t"};
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}};
272 PetscFunctionBeginUser;
274 for (PetscInt n = 0; n < 4; ++n) {
275 PetscReal result = 0.0;
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;
295 PetscFunctionReturn(0);
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)"};
309 PetscFunctionBeginUser;
310 for (
size_t n = 0; n <
sizeof(bad) /
sizeof(bad[0]); ++n) {
312 PetscErrorCode refused;
314 PetscCall(PetscPushErrorHandler(PetscIgnoreErrorHandler, NULL));
316 PetscCall(PetscPopErrorHandler());
321 PetscErrorCode refused;
323 PetscCall(PetscPushErrorHandler(PetscIgnoreErrorHandler, NULL));
325 PetscCall(PetscPopErrorHandler());
328 PetscFunctionReturn(0);
341 PetscReal a, b, sum = 0.0, square = 0.0;
342 const PetscInt samples = 20000;
344 PetscFunctionBeginUser;
351 PetscCall(
PicurvAssertBool((PetscBool)(a == b && a >= 0.0 && a < 1.0),
"a draw is pure and in [0, 1)"));
354 PetscCall(
PicurvAssertBool((PetscBool)(a != b),
"another particle draws independently"));
358 PetscCall(
PicurvAssertBool((PetscBool)(a != b),
"another event draws independently"));
362 PetscCall(
PicurvAssertBool((PetscBool)(a != b),
"another field's default stream is independent"));
364 key.
grid = PETSC_TRUE;
366 PetscCall(
PicurvAssertBool((PetscBool)(a != b),
"a grid point with the same number as a particle draws independently"));
367 key.
grid = PETSC_FALSE;
372 PetscCall(
PicurvAssertBool((PetscBool)(a == b),
"an explicit stream is shared across fields"));
374 for (PetscInt pid = 0; pid < samples; ++pid) {
381 PetscCall(
PicurvAssertRealNear(1.0, square / samples, 0.04,
"normal draws have unit variance"));
386 PetscFunctionReturn(0);
409 ierr = PetscInitialize(&argc, &argv, NULL,
"PICurv particle kernel tests");
414 ierr =
PicurvRunTests(
"unit-particles", cases,
sizeof(cases) /
sizeof(cases[0]));
420 ierr = PetscFinalize();
void MetricBuildCellFacePlanes(const Cell *cell, CellFacePlanes *planes)
Builds the six face planes of a hexahedron, as the particle search tests them.
Configured initial values of particle-carried fields.
PetscErrorCode PicurvExpressionEvaluate(const PicurvExpression *expression, const PetscReal *values, const PicurvExpressionDrawKey *key, PetscReal *result)
Evaluate a compiled expression.
PetscErrorCode PicurvExpressionDestroy(PicurvExpression **expression)
Free a compiled expression.
PetscInt64 pid
Global identity of the particle, or on the grid of the cell or face cell.
PetscBool grid
The draw is for a grid point, not a particle.
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.
PetscInt64 salt
Event: for a particle what produced it, 0 for the t=0 population; on a face the step; in a cell PICUR...
PetscInt64 field
Field or component whose expression draws, the default stream's namespace.
What a random draw is keyed on, besides its stream.
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.
PetscReal size
Mean edge length (GetCellCharacteristicSize()); 0 where unused.
Cmpnts vertices[8]
Coordinates of the eight vertices of the cell.
Defines the vertices of a single hexahedral grid cell.
The six face planes of one hexahedron, indexed by Face, and its characteristic size.
A 3D point or vector with PetscScalar components.
Defines a particle's core properties for Lagrangian tracking.
The master context for the entire simulation.
User-defined context containing data specific to a single computational grid level.
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.