22 PetscBool within = PETSC_FALSE;
24 PetscFunctionBeginUser;
28 PetscCall(
PicurvAssertBool(within,
"cell (1,1,1) should be within the serial local grid"));
31 PetscCall(
PicurvAssertBool((PetscBool)!within,
"cell (4,1,1) should fall outside the valid local cell range"));
34 PetscFunctionReturn(0);
45 PetscInt idx = -1, idy = -1, idz = -1, steps = -1;
47 PetscFunctionBeginUser;
49 PetscCall(PetscMemzero(&particle,
sizeof(particle)));
57 PetscCall(
PicurvAssertIntEqual(1, idx,
"InitializeTraversalParameters should preserve prior i"));
58 PetscCall(
PicurvAssertIntEqual(1, idy,
"InitializeTraversalParameters should preserve prior j"));
59 PetscCall(
PicurvAssertIntEqual(1, idz,
"InitializeTraversalParameters should preserve prior k"));
60 PetscCall(
PicurvAssertIntEqual(0, steps,
"InitializeTraversalParameters should reset traversal steps"));
63 PetscFunctionReturn(0);
75 PetscFunctionBeginUser;
77 PetscCall(PetscMemzero(&cell,
sizeof(cell)));
88 PetscFunctionReturn(0);
100 FILE *file = fopen(
"tests/fixtures/expression_conformance.txt",
"r");
104 PetscFunctionBeginUser;
105 PetscCheck(file, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN,
"Cannot open the expression conformance cases.");
106 while (fgets(line,
sizeof(line), file)) {
110 PetscReal values[3], result = 0.0;
112 if (line[0] ==
'#' || line[0] ==
'\n')
continue;
113 for (
char *cursor = line; count < 5; ++count) {
114 fields[count] = cursor;
115 char *separator = strchr(cursor,
';');
116 if (!separator) { ++count;
break; }
118 cursor = separator + 1;
120 PetscCheck(count == 5, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED,
"Malformed conformance line.");
121 for (
int v = 0; v < 3; ++v) values[v] = strtod(fields[v + 1], NULL);
129 PetscCall(
PicurvAssertBool((PetscBool)(cases >= 20),
"the conformance file should hold its cases"));
130 PetscFunctionReturn(0);
138 const char *bad[] = {
"0x1F",
"1_000",
"x // 2",
"abs",
"abs(1, 2)",
"where(x, 1)",
"foo",
139 "x +",
"(x",
"x y",
"uniform(x)",
"uniform(1.5)",
"1.e",
"not"};
141 PetscFunctionBeginUser;
142 for (
size_t n = 0; n <
sizeof(bad) /
sizeof(bad[0]); ++n) {
144 PetscErrorCode refused;
146 PetscCall(PetscPushErrorHandler(PetscIgnoreErrorHandler, NULL));
148 PetscCall(PetscPopErrorHandler());
153 PetscErrorCode refused;
155 PetscCall(PetscPushErrorHandler(PetscIgnoreErrorHandler, NULL));
157 PetscCall(PetscPopErrorHandler());
160 PetscFunctionReturn(0);
173 PetscReal a, b, sum = 0.0, square = 0.0;
174 const PetscInt samples = 20000;
176 PetscFunctionBeginUser;
183 PetscCall(
PicurvAssertBool((PetscBool)(a == b && a >= 0.0 && a < 1.0),
"a draw is pure and in [0, 1)"));
186 PetscCall(
PicurvAssertBool((PetscBool)(a != b),
"another particle draws independently"));
190 PetscCall(
PicurvAssertBool((PetscBool)(a != b),
"another event draws independently"));
194 PetscCall(
PicurvAssertBool((PetscBool)(a != b),
"another field's default stream is independent"));
198 PetscCall(
PicurvAssertBool((PetscBool)(a == b),
"an explicit stream is shared across fields"));
200 for (PetscInt pid = 0; pid < samples; ++pid) {
207 PetscCall(
PicurvAssertRealNear(1.0, square / samples, 0.04,
"normal draws have unit variance"));
212 PetscFunctionReturn(0);
231 ierr = PetscInitialize(&argc, &argv, NULL,
"PICurv particle kernel tests");
236 ierr =
PicurvRunTests(
"unit-particles", cases,
sizeof(cases) /
sizeof(cases[0]));
242 ierr = PetscFinalize();
Configured initial values of particle-carried fields, and the expression language that defines them.
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
Particle identity; draws are pure functions of it.
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 that produced the particle: 0 for the t=0 population.
PetscInt64 field
Field whose expression draws, the default stream's namespace.
What a random draw is keyed on, besides the particle and 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 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[]
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.
Cmpnts vertices[8]
Coordinates of the eight vertices of the cell.
Defines the vertices of a single hexahedral grid cell.
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 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.