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

C unit tests for grid bounding-box helper routines. More...

#include "test_support.h"
#include "grid.h"
#include "io.h"
#include <stdlib.h>
Include dependency graph for test_grid.c:

Go to the source code of this file.

Functions

static PetscErrorCode TestComputeLocalBoundingBoxUniformGrid (void)
 Tests local bounding-box construction on a uniform grid.
 
static PetscErrorCode TestGatherAndBroadcastBoundingBoxes (void)
 Tests collective gather and broadcast of rank-local bounding boxes.
 
static PetscErrorCode TestReadGridGenerationInputsNonDimensionalizesBounds (void)
 Tests that programmatic domain bounds are non-dimensionalized by L_ref.
 
static PetscErrorCode TestBalanceCoarsestOwnershipBalancesFinestLevel (void)
 Tests that the coarsest split puts each refined finest-level boundary nearest its share.
 
static PetscErrorCode TestOversizedGridIsRefusedOn32BitIndices (void)
 Tests that a grid too large for 32-bit PetscInt is refused before its DMs are created.
 
int main (int argc, char **argv)
 Runs the unit-grid PETSc test binary.
 

Detailed Description

C unit tests for grid bounding-box helper routines.

Definition in file test_grid.c.

Function Documentation

◆ TestComputeLocalBoundingBoxUniformGrid()

static PetscErrorCode TestComputeLocalBoundingBoxUniformGrid ( void  )
static

Tests local bounding-box construction on a uniform grid.

Definition at line 16 of file test_grid.c.

17{
18 SimCtx *simCtx = NULL;
19 UserCtx *user = NULL;
20 BoundingBox bbox;
21 PetscReal expected_max = 0.0;
22
23 PetscFunctionBeginUser;
24 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 4, 4, 4));
25 PetscCall(ComputeLocalBoundingBox(user, &bbox));
26 expected_max = ((PetscReal)(user->IM - 1) / (PetscReal)user->IM) + 1.0e-6;
27
28 PetscCall(PicurvAssertRealNear(-1.0e-6, bbox.min_coords.x, 1.0e-10, "bbox min x"));
29 PetscCall(PicurvAssertRealNear(-1.0e-6, bbox.min_coords.y, 1.0e-10, "bbox min y"));
30 PetscCall(PicurvAssertRealNear(-1.0e-6, bbox.min_coords.z, 1.0e-10, "bbox min z"));
31 PetscCall(PicurvAssertRealNear(expected_max, bbox.max_coords.x, 1.0e-10, "bbox max x"));
32 PetscCall(PicurvAssertRealNear(expected_max, bbox.max_coords.y, 1.0e-10, "bbox max y"));
33 PetscCall(PicurvAssertRealNear(expected_max, bbox.max_coords.z, 1.0e-10, "bbox max z"));
34
35 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
36 PetscFunctionReturn(0);
37}
PetscErrorCode ComputeLocalBoundingBox(UserCtx *user, BoundingBox *localBBox)
Computes the local bounding box of the grid on the current process.
Definition grid.c:869
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.
Cmpnts max_coords
Maximum x, y, z coordinates of the bounding box.
Definition variables.h:214
Cmpnts min_coords
Minimum x, y, z coordinates of the bounding box.
Definition variables.h:213
PetscScalar x
Definition variables.h:145
PetscScalar z
Definition variables.h:145
PetscScalar y
Definition variables.h:145
PetscInt IM
Definition variables.h:1171
Defines a 3D axis-aligned bounding box.
Definition variables.h:212
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestGatherAndBroadcastBoundingBoxes()

static PetscErrorCode TestGatherAndBroadcastBoundingBoxes ( void  )
static

Tests collective gather and broadcast of rank-local bounding boxes.

Definition at line 42 of file test_grid.c.

43{
44 SimCtx *simCtx = NULL;
45 UserCtx *user = NULL;
46 BoundingBox local_bbox;
47 BoundingBox *boxes = NULL;
48 PetscMPIInt rank = 0, size = 1;
49
50 PetscFunctionBeginUser;
51 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
52 PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
53
54 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 4, 4, 4));
55 PetscCall(ComputeLocalBoundingBox(user, &local_bbox));
56 PetscCall(GatherAllBoundingBoxes(user, &boxes));
57
58 if (rank == 0) {
59 PetscCall(PicurvAssertBool((PetscBool)(boxes != NULL), "root must receive gathered bounding boxes"));
60 } else {
61 PetscCall(PicurvAssertBool((PetscBool)(boxes == NULL), "non-root should have NULL pre-broadcast bbox pointer"));
62 }
63
64 PetscCall(BroadcastAllBoundingBoxes(user, &boxes));
65 PetscCall(PicurvAssertBool((PetscBool)(boxes != NULL), "all ranks must have bbox array after broadcast"));
66
67 for (PetscMPIInt r = 0; r < size; ++r) {
68 PetscCall(PicurvAssertBool((PetscBool)(boxes[r].min_coords.x <= boxes[r].max_coords.x), "bbox x range is valid"));
69 PetscCall(PicurvAssertBool((PetscBool)(boxes[r].min_coords.y <= boxes[r].max_coords.y), "bbox y range is valid"));
70 PetscCall(PicurvAssertBool((PetscBool)(boxes[r].min_coords.z <= boxes[r].max_coords.z), "bbox z range is valid"));
71 }
72
73 PetscCall(PicurvAssertRealNear(local_bbox.min_coords.x, boxes[rank].min_coords.x, 1.0e-10, "rank-local bbox min x"));
74 PetscCall(PicurvAssertRealNear(local_bbox.max_coords.x, boxes[rank].max_coords.x, 1.0e-10, "rank-local bbox max x"));
75
76 free(boxes);
77 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
78 PetscFunctionReturn(0);
79}
PetscErrorCode BroadcastAllBoundingBoxes(UserCtx *user, BoundingBox **bboxlist)
Broadcasts the bounding box information collected on rank 0 to all other ranks.
Definition grid.c:1071
PetscErrorCode GatherAllBoundingBoxes(UserCtx *user, BoundingBox **allBBoxes)
Gathers local bounding boxes from all MPI processes to rank 0.
Definition grid.c:1011
PetscErrorCode PicurvAssertBool(PetscBool value, const char *context)
Asserts that one boolean condition is true.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestReadGridGenerationInputsNonDimensionalizesBounds()

static PetscErrorCode TestReadGridGenerationInputsNonDimensionalizesBounds ( void  )
static

Tests that programmatic domain bounds are non-dimensionalized by L_ref.

A file-backed grid is divided by the reference length when the conductor publishes it as an asset; a programmatic grid has no such staging step, so ReadGridGenerationInputs() must perform the same division so all three grid modes take dimensional user input and the postprocessor's unconditional Coordinates * L_ref round-trips correctly.

Definition at line 88 of file test_grid.c.

89{
90 SimCtx *simCtx = NULL;
91 UserCtx *user = NULL;
92
93 PetscFunctionBeginUser;
94 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 4, 4, 4));
95
96 PetscCall(PetscOptionsSetValue(NULL, "-im", "4"));
97 PetscCall(PetscOptionsSetValue(NULL, "-jm", "4"));
98 PetscCall(PetscOptionsSetValue(NULL, "-km", "4"));
99 PetscCall(PetscOptionsSetValue(NULL, "-xMins", "0.0"));
100 PetscCall(PetscOptionsSetValue(NULL, "-xMaxs", "0.2"));
101 PetscCall(PetscOptionsSetValue(NULL, "-yMins", "0.0"));
102 PetscCall(PetscOptionsSetValue(NULL, "-yMaxs", "0.2"));
103 PetscCall(PetscOptionsSetValue(NULL, "-zMins", "0.0"));
104 PetscCall(PetscOptionsSetValue(NULL, "-zMaxs", "0.4"));
105
106 /* Authored in physical units (metres); L_ref converts to the solver units the
107 * simulator evolves in. 0.2 m at L_ref=0.05 is a domain of 4 solver units, the same
108 * shape a nondimensional-input user would have written by hand. */
109 simCtx->scaling.L_ref = 0.05;
110 PetscCall(ReadGridGenerationInputs(user));
111
112 PetscCall(PicurvAssertRealNear(0.0, user->Min_X, 1.0e-12, "xMin divides by L_ref"));
113 PetscCall(PicurvAssertRealNear(4.0, user->Max_X, 1.0e-12, "0.2 / 0.05 = 4.0 solver units"));
114 PetscCall(PicurvAssertRealNear(4.0, user->Max_Y, 1.0e-12, "yMax non-dimensionalized"));
115 PetscCall(PicurvAssertRealNear(8.0, user->Max_Z, 1.0e-12, "0.4 / 0.05 = 8.0 solver units"));
116
117 /* L_ref = 1.0 is a no-op, so an author already working in solver units is
118 * unaffected - this is the common case and must not silently change behaviour. */
119 simCtx->scaling.L_ref = 1.0;
120 PetscCall(ReadGridGenerationInputs(user));
121 PetscCall(PicurvAssertRealNear(0.2, user->Max_X, 1.0e-12, "L_ref=1.0 leaves bounds unchanged"));
122
123 /* A non-positive reference length is refused rather than dividing by zero or
124 * flipping the domain's sign silently. */
125 {
126 PetscErrorCode bad = 0;
127
128 simCtx->scaling.L_ref = 0.0;
129 PetscCall(PetscPushErrorHandler(PetscIgnoreErrorHandler, NULL));
130 bad = ReadGridGenerationInputs(user);
131 PetscCall(PetscPopErrorHandler());
132 PetscCall(PicurvAssertIntEqual(PETSC_ERR_ARG_OUTOFRANGE, bad,
133 "L_ref <= 0 is refused rather than dividing by zero"));
134 }
135
136 PetscCall(PetscOptionsClearValue(NULL, "-im"));
137 PetscCall(PetscOptionsClearValue(NULL, "-jm"));
138 PetscCall(PetscOptionsClearValue(NULL, "-km"));
139 PetscCall(PetscOptionsClearValue(NULL, "-xMins"));
140 PetscCall(PetscOptionsClearValue(NULL, "-xMaxs"));
141 PetscCall(PetscOptionsClearValue(NULL, "-yMins"));
142 PetscCall(PetscOptionsClearValue(NULL, "-yMaxs"));
143 PetscCall(PetscOptionsClearValue(NULL, "-zMins"));
144 PetscCall(PetscOptionsClearValue(NULL, "-zMaxs"));
145 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
146 PetscFunctionReturn(0);
147}
PetscErrorCode ReadGridGenerationInputs(UserCtx *user)
Parses command-line options for a programmatically generated grid for a SINGLE block.
Definition io.c:537
PetscErrorCode PicurvAssertIntEqual(PetscInt expected, PetscInt actual, const char *context)
Asserts that two integer values are equal.
PetscReal Min_X
Definition variables.h:1172
PetscReal L_ref
Definition variables.h:898
PetscReal Max_Y
Definition variables.h:1172
ScalingCtx scaling
Definition variables.h:1003
PetscReal Max_X
Definition variables.h:1172
PetscReal Max_Z
Definition variables.h:1172
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestBalanceCoarsestOwnershipBalancesFinestLevel()

static PetscErrorCode TestBalanceCoarsestOwnershipBalancesFinestLevel ( void  )
static

Tests that the coarsest split puts each refined finest-level boundary nearest its share.

128 cells on 4 ranks at 3 levels: PETSc's even coarsest split (9,9,8,8 of 34 points) refines to finest owned cells 33/36/32/27; 9,8,8,9 refines to 33/32/32/31, the best refinement allows. Unrefined, the last rank takes both spare points of 130, 32/32/32/34, so each owns 32 cells, which PETSc's 33/33/32/32 does not.

Definition at line 156 of file test_grid.c.

157{
158 const PetscInt refined[4] = {9, 8, 8, 9}, unrefined[4] = {25, 25, 25, 24}, one_level[4] = {32, 32, 32, 34};
159 PetscInt l[4];
160
161 PetscFunctionBeginUser;
162 PetscCall(BalanceCoarsestOwnership(34, 128, 2, 4, l));
163 for (PetscInt r = 0; r < 4; ++r) PetscCall(PicurvAssertIntEqual(refined[r], l[r], "3-level coarsest split"));
164 PetscCall(BalanceCoarsestOwnership(99, 97, 0, 4, l));
165 for (PetscInt r = 0; r < 4; ++r) PetscCall(PicurvAssertIntEqual(unrefined[r], l[r], "unrefined axis keeps PETSc's split"));
166 PetscCall(BalanceCoarsestOwnership(130, 128, 0, 4, l));
167 for (PetscInt r = 0; r < 4; ++r) PetscCall(PicurvAssertIntEqual(one_level[r], l[r], "one level gives the last rank both spare points"));
168 PetscFunctionReturn(0);
169}
PetscErrorCode BalanceCoarsestOwnership(PetscInt coarse_nodes, PetscInt fine_cells, PetscInt refinements, PetscInt ranks, PetscInt *l)
Split one axis of the coarsest level so the finest level's owned cells balance.
Definition grid.c:121
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestOversizedGridIsRefusedOn32BitIndices()

static PetscErrorCode TestOversizedGridIsRefusedOn32BitIndices ( void  )
static

Tests that a grid too large for 32-bit PetscInt is refused before its DMs are created.

1300^3 cells give 1301^3 nodes, whose 3-component fields need about 6.6e9 entries. A 64-bit-index build indexes them, so the test applies to 32-bit builds only.

Definition at line 176 of file test_grid.c.

177{
178#if defined(PETSC_USE_64BIT_INDICES)
179 /* Nothing outgrows a 64-bit PetscInt, and PETSC_MAX_INT + 1 would overflow. */
180 PetscFunctionBeginUser;
181 PetscFunctionReturn(0);
182#else
183 SimCtx *simCtx = NULL;
184 char tmpdir[PETSC_MAX_PATH_LEN] = "";
185 PetscErrorCode setup_ierr;
186
187 PetscFunctionBeginUser;
188 PetscCall(PetscPushErrorHandler(PetscReturnErrorHandler, NULL));
189 PetscCall(PicurvCheckPetscIntRange(PETSC_COMM_WORLD, (PetscInt64)PETSC_MAX_INT, "an object at the limit"));
190 setup_ierr = PicurvCheckPetscIntRange(PETSC_COMM_WORLD, (PetscInt64)PETSC_MAX_INT + 1, "an object past the limit");
191 PetscCall(PetscPopErrorHandler());
192 PetscCall(PicurvAssertIntEqual(PETSC_ERR_ARG_OUTOFRANGE, setup_ierr, "one entry past PETSC_MAX_INT must be refused"));
193
194 PetscCall(PetscPushErrorHandler(PetscReturnErrorHandler, NULL));
195 setup_ierr = PicurvBuildTinyRuntimeContextWithOptions(NULL, PETSC_FALSE, "-im 1300\n-jm 1300\n-km 1300\n-mg_level 1\n",
196 &simCtx, NULL, tmpdir, sizeof(tmpdir));
197 PetscCall(PetscPopErrorHandler());
198 PetscCall(PicurvAssertIntEqual(PETSC_ERR_ARG_OUTOFRANGE, setup_ierr,
199 "grid setup must refuse a grid whose fields outgrow 32-bit PetscInt"));
200 if (simCtx) PetscCall(PicurvDestroyRuntimeContext(&simCtx));
201 if (tmpdir[0]) PetscCall(PicurvRemoveTempDir(tmpdir));
202 PetscCall(PetscOptionsClear(NULL));
203 PetscFunctionReturn(0);
204#endif
205}
PetscErrorCode PicurvCheckPetscIntRange(MPI_Comm comm, PetscInt64 entries, const char *object)
Refuse a PETSc object whose global size this build's PetscInt cannot index.
Definition setup.c:102
PetscErrorCode PicurvDestroyRuntimeContext(SimCtx **simCtx_ptr)
Finalizes and frees a runtime context built by PicurvBuildTinyRuntimeContext.
PetscErrorCode PicurvBuildTinyRuntimeContextWithOptions(const char *bcs_contents, PetscBool enable_particles, const char *extra_options, SimCtx **simCtx_out, UserCtx **user_out, char *tmpdir, size_t tmpdir_len)
Builds the tiny runtime context with extra control-file options appended.
PetscErrorCode PicurvRemoveTempDir(const char *path)
Recursively removes a temporary directory created by PicurvMakeTempDir.
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-grid PETSc test binary.

Definition at line 211 of file test_grid.c.

212{
213 PetscErrorCode ierr;
214 const PicurvTestCase cases[] = {
215 {"compute-local-bbox-uniform-grid", TestComputeLocalBoundingBoxUniformGrid},
216 {"gather-and-broadcast-bboxes", TestGatherAndBroadcastBoundingBoxes},
217 {"read-grid-generation-inputs-nondimensionalizes-bounds",
219 {"balance-coarsest-ownership-balances-finest-level", TestBalanceCoarsestOwnershipBalancesFinestLevel},
220 {"oversized-grid-is-refused-on-32-bit-indices", TestOversizedGridIsRefusedOn32BitIndices},
221 };
222
223 ierr = PetscInitialize(&argc, &argv, NULL, "PICurv grid tests");
224 if (ierr) {
225 return (int)ierr;
226 }
227
228 ierr = PicurvRunTests("unit-grid", cases, sizeof(cases) / sizeof(cases[0]));
229 if (ierr) {
230 PetscFinalize();
231 return (int)ierr;
232 }
233
234 ierr = PetscFinalize();
235 return (int)ierr;
236}
static PetscErrorCode TestOversizedGridIsRefusedOn32BitIndices(void)
Tests that a grid too large for 32-bit PetscInt is refused before its DMs are created.
Definition test_grid.c:176
static PetscErrorCode TestComputeLocalBoundingBoxUniformGrid(void)
Tests local bounding-box construction on a uniform grid.
Definition test_grid.c:16
static PetscErrorCode TestGatherAndBroadcastBoundingBoxes(void)
Tests collective gather and broadcast of rank-local bounding boxes.
Definition test_grid.c:42
static PetscErrorCode TestReadGridGenerationInputsNonDimensionalizesBounds(void)
Tests that programmatic domain bounds are non-dimensionalized by L_ref.
Definition test_grid.c:88
static PetscErrorCode TestBalanceCoarsestOwnershipBalancesFinestLevel(void)
Tests that the coarsest split puts each refined finest-level boundary nearest its share.
Definition test_grid.c:156
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: