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.
 
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:850
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:199
Cmpnts min_coords
Minimum x, y, z coordinates of the bounding box.
Definition variables.h:198
PetscScalar x
Definition variables.h:122
PetscScalar z
Definition variables.h:122
PetscScalar y
Definition variables.h:122
PetscInt IM
Definition variables.h:1088
Defines a 3D axis-aligned bounding box.
Definition variables.h:197
The master context for the entire simulation.
Definition variables.h:859
User-defined context containing data specific to a single computational grid level.
Definition variables.h:1074
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:1061
PetscErrorCode GatherAllBoundingBoxes(UserCtx *user, BoundingBox **allBBoxes)
Gathers local bounding boxes from all MPI processes to rank 0.
Definition grid.c:999
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:441
PetscErrorCode PicurvAssertIntEqual(PetscInt expected, PetscInt actual, const char *context)
Asserts that two integer values are equal.
PetscReal Min_X
Definition variables.h:1089
PetscReal L_ref
Definition variables.h:841
PetscReal Max_Y
Definition variables.h:1089
ScalingCtx scaling
Definition variables.h:946
PetscReal Max_X
Definition variables.h:1089
PetscReal Max_Z
Definition variables.h:1089
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 153 of file test_grid.c.

154{
155 PetscErrorCode ierr;
156 const PicurvTestCase cases[] = {
157 {"compute-local-bbox-uniform-grid", TestComputeLocalBoundingBoxUniformGrid},
158 {"gather-and-broadcast-bboxes", TestGatherAndBroadcastBoundingBoxes},
159 {"read-grid-generation-inputs-nondimensionalizes-bounds",
161 };
162
163 ierr = PetscInitialize(&argc, &argv, NULL, "PICurv grid tests");
164 if (ierr) {
165 return (int)ierr;
166 }
167
168 ierr = PicurvRunTests("unit-grid", cases, sizeof(cases) / sizeof(cases[0]));
169 if (ierr) {
170 PetscFinalize();
171 return (int)ierr;
172 }
173
174 ierr = PetscFinalize();
175 return (int)ierr;
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
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: