PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
 
Loading...
Searching...
No Matches
test_post_compute_mpi.c
Go to the documentation of this file.
1/**
2 * @file test_post_compute_mpi.c
3 * @brief Decomposition-independent regression tests for post-processing compute.
4 */
5
6#include "test_support.h"
7
8#include "postprocessor.h"
10#include "statistics_window.h"
11#include "statistics_target.h"
12
13static const PetscReal kSentinel = -9876.5;
14
15/** @brief Analytic scalar field used to expose misplaced or missing grid points. */
16static PetscReal ScalarValue(PetscInt i, PetscInt j, PetscInt k)
17{
18 return (PetscReal)(i + 10 * j + 100 * k);
19}
20
21/** @brief Requires every rank's maximum error to be within tolerance. */
22static PetscErrorCode AssertGlobalError(PetscReal local_error, PetscReal tolerance,
23 const char *context)
24{
25 PetscReal global_error = 0.0;
26
27 PetscFunctionBeginUser;
28 PetscCallMPI(MPI_Allreduce(&local_error, &global_error, 1, MPIU_REAL, MPI_MAX,
29 PETSC_COMM_WORLD));
30 PetscCall(PicurvAssertBool((PetscBool)(global_error <= tolerance), context));
31 PetscFunctionReturn(0);
32}
33
34/** @brief Exercises every Eulerian transformation currently dispatched by the pipeline. */
36{
37 SimCtx *simCtx = NULL;
38 UserCtx *user = NULL;
39 PostProcessParams *pps = NULL;
40 PetscReal ***pressure = NULL;
41 Cmpnts ***velocity = NULL;
42 const PetscReal ***pressure_result = NULL;
43 const PetscReal ***pressure_nodal = NULL;
44 const PetscReal ***qcrit = NULL;
45 const Cmpnts ***velocity_nodal = NULL;
46 PetscReal pressure_error = 0.0;
47 PetscReal pressure_nodal_error = 0.0;
48 PetscReal velocity_nodal_error = 0.0;
49 PetscReal qcrit_error = 0.0;
50 PetscInt local_physical_nodes = 0;
51 PetscInt global_physical_nodes = 0;
52 PetscInt local_q_points = 0;
53 PetscInt global_q_points = 0;
54
55 PetscFunctionBeginUser;
56 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 8, 6, 4));
57 PetscCall(PetscCalloc1(1, &simCtx->pps));
58 pps = simCtx->pps;
59 pps->reference[0] = 1;
60 pps->reference[1] = 1;
61 pps->reference[2] = 1;
62 simCtx->scaling.L_ref = 1.0;
63 simCtx->scaling.U_ref = 1.0;
64 simCtx->scaling.rho_ref = 2.0;
65 simCtx->scaling.P_ref = 2.0;
66
67 PetscCall(DMDAVecGetArray(user->da, user->P, &pressure));
68 PetscCall(DMDAVecGetArray(user->fda, user->Ucat, &velocity));
69 for (PetscInt k = user->info.zs; k < user->info.zs + user->info.zm; ++k) {
70 for (PetscInt j = user->info.ys; j < user->info.ys + user->info.ym; ++j) {
71 for (PetscInt i = user->info.xs; i < user->info.xs + user->info.xm; ++i) {
72 pressure[k][j][i] = ScalarValue(i, j, k);
73 velocity[k][j][i] = (Cmpnts){1.0, 2.0, 3.0};
74 }
75 }
76 }
77 PetscCall(DMDAVecRestoreArray(user->fda, user->Ucat, &velocity));
78 PetscCall(DMDAVecRestoreArray(user->da, user->P, &pressure));
79 PetscCall(VecSet(user->P_nodal, kSentinel));
80 PetscCall(VecSet(user->Ucat_nodal, kSentinel));
81 PetscCall(VecSet(user->Qcrit, kSentinel));
82
83 /* A dimensionalizing post-processor scales pressure as it is loaded, by rho U^2 = 2,
84 * before the pipeline runs; normalization is linear, so the expected values are
85 * the same whichever of the two is applied first. */
86 PetscCall(DimensionalizeField(user, "P"));
87 PetscCall(PetscStrncpy(
89 "NormalizeRelativeField:P;"
90 "CellToNodeAverage:P>P_nodal;CellToNodeAverage:Ucat>Ucat_nodal;"
91 "ComputeQCriterion",
92 sizeof(pps->process_pipeline)));
93 PetscCall(EulerianDataProcessingPipeline(user, pps));
94
95 PetscCall(DMDAVecGetArrayRead(user->da, user->P, &pressure_result));
96 PetscCall(DMDAVecGetArrayRead(user->da, user->P_nodal, &pressure_nodal));
97 PetscCall(DMDAVecGetArrayRead(user->fda, user->Ucat_nodal, &velocity_nodal));
98 PetscCall(DMDAVecGetArrayRead(user->da, user->Qcrit, &qcrit));
99 for (PetscInt k = user->info.zs; k < user->info.zs + user->info.zm; ++k) {
100 for (PetscInt j = user->info.ys; j < user->info.ys + user->info.ym; ++j) {
101 for (PetscInt i = user->info.xs; i < user->info.xs + user->info.xm; ++i) {
102 const PetscBool physical_node =
103 (PetscBool)(i < user->info.mx - 1 && j < user->info.my - 1 &&
104 k < user->info.mz - 1);
105 const PetscBool q_point =
106 (PetscBool)(i >= 1 && i < user->info.mx - 1 &&
107 j >= 1 && j < user->info.my - 1 &&
108 k >= 1 && k < user->info.mz - 1);
109 const PetscReal expected_pressure = 2.0 * (ScalarValue(i, j, k) - 111.0);
110
111 pressure_error = PetscMax(
112 pressure_error,
113 PetscAbsReal(pressure_result[k][j][i] - expected_pressure));
114 if (physical_node) {
115 const PetscReal expected_nodal =
116 2.0 * (ScalarValue(i, j, k) + 55.5 - 111.0);
117
118 pressure_nodal_error = PetscMax(
119 pressure_nodal_error,
120 PetscAbsReal(pressure_nodal[k][j][i] - expected_nodal));
121 velocity_nodal_error = PetscMax(
122 velocity_nodal_error,
123 PetscAbsReal(velocity_nodal[k][j][i].x - 1.0));
124 velocity_nodal_error = PetscMax(
125 velocity_nodal_error,
126 PetscAbsReal(velocity_nodal[k][j][i].y - 2.0));
127 velocity_nodal_error = PetscMax(
128 velocity_nodal_error,
129 PetscAbsReal(velocity_nodal[k][j][i].z - 3.0));
130 ++local_physical_nodes;
131 } else {
132 pressure_nodal_error = PetscMax(
133 pressure_nodal_error,
134 PetscAbsReal(pressure_nodal[k][j][i] - kSentinel));
135 velocity_nodal_error = PetscMax(
136 velocity_nodal_error,
137 PetscAbsReal(velocity_nodal[k][j][i].x - kSentinel));
138 velocity_nodal_error = PetscMax(
139 velocity_nodal_error,
140 PetscAbsReal(velocity_nodal[k][j][i].y - kSentinel));
141 velocity_nodal_error = PetscMax(
142 velocity_nodal_error,
143 PetscAbsReal(velocity_nodal[k][j][i].z - kSentinel));
144 }
145 qcrit_error = PetscMax(
146 qcrit_error,
147 PetscAbsReal(qcrit[k][j][i] - (q_point ? 0.0 : kSentinel)));
148 if (q_point) ++local_q_points;
149 }
150 }
151 }
152 PetscCall(DMDAVecRestoreArrayRead(user->da, user->Qcrit, &qcrit));
153 PetscCall(DMDAVecRestoreArrayRead(user->fda, user->Ucat_nodal, &velocity_nodal));
154 PetscCall(DMDAVecRestoreArrayRead(user->da, user->P_nodal, &pressure_nodal));
155 PetscCall(DMDAVecRestoreArrayRead(user->da, user->P, &pressure_result));
156
157 PetscCallMPI(MPI_Allreduce(&local_physical_nodes, &global_physical_nodes, 1,
158 MPIU_INT, MPI_SUM, PETSC_COMM_WORLD));
159 PetscCallMPI(MPI_Allreduce(&local_q_points, &global_q_points, 1,
160 MPIU_INT, MPI_SUM, PETSC_COMM_WORLD));
161 PetscCall(PicurvAssertIntEqual(user->IM * user->JM * user->KM,
162 global_physical_nodes,
163 "the Eulerian pipeline must visit every physical node exactly once"));
164 PetscCall(PicurvAssertIntEqual((user->IM - 1) * (user->JM - 1) * (user->KM - 1),
165 global_q_points,
166 "Q-criterion must visit every interior cell exactly once"));
167 PetscCall(AssertGlobalError(pressure_error, 1.0e-12,
168 "pressure normalization and dimensionalization are decomposition independent"));
169 PetscCall(AssertGlobalError(pressure_nodal_error, 1.0e-12,
170 "scalar nodal averaging covers rank interfaces and physical boundaries"));
171 PetscCall(AssertGlobalError(velocity_nodal_error, 1.0e-12,
172 "vector nodal averaging covers rank interfaces and physical boundaries"));
173 PetscCall(AssertGlobalError(qcrit_error, 1.0e-12,
174 "Q-criterion covers the complete distributed interior"));
175
176 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
177 PetscFunctionReturn(0);
178}
179
180/** @brief Verifies the production-created derived swarm mirrors source ownership. */
182{
183 SimCtx *simCtx = NULL;
184 UserCtx *user = NULL;
186 PetscMPIInt rank = 0, size = 1;
187 PetscInt source_local = 0;
188 PetscInt source_global = 0;
189 PetscInt post_local = 0;
190 PetscInt post_global = 0;
191 PetscInt offset = 0;
192 PetscScalar (*velocity)[3] = NULL;
193 const PetscScalar *ske = NULL;
194 PetscReal local_error = 0.0;
195 const PetscInt total_particles = 7;
196
197 PetscFunctionBeginUser;
198 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
199 PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
200 source_local = total_particles / size + (rank < total_particles % size ? 1 : 0);
201 PetscCall(PetscMemzero(&pps, sizeof(pps)));
202 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 4, 4, 4));
203 PetscCall(PicurvCreateSwarmPair(user, source_local, "unused"));
204 PetscCall(DMDestroy(&user->post_swarm));
205 PetscCall(PetscStrncpy(pps.particle_pipeline, "ComputeSpecificKE:velocity>ske",
206 sizeof(pps.particle_pipeline)));
207 PetscCall(SetupPostProcessSwarm(user, &pps));
208
209 if (size > 1) {
210 PetscCallMPI(MPI_Exscan(&source_local, &offset, 1, MPIU_INT, MPI_SUM,
211 PETSC_COMM_WORLD));
212 if (rank == 0) offset = 0;
213 }
214 PetscCall(DMSwarmGetField(user->swarm, "velocity", NULL, NULL, (void **)&velocity));
215 for (PetscInt p = 0; p < source_local; ++p) {
216 const PetscReal tag = (PetscReal)(offset + p + 1);
217
218 velocity[p][0] = tag;
219 velocity[p][1] = 2.0;
220 velocity[p][2] = -1.0;
221 }
222 PetscCall(DMSwarmRestoreField(user->swarm, "velocity", NULL, NULL,
223 (void **)&velocity));
224
225 PetscCall(ParticleDataProcessingPipeline(user, &pps));
226 PetscCall(DMSwarmGetLocalSize(user->swarm, &source_local));
227 PetscCall(DMSwarmGetSize(user->swarm, &source_global));
228 PetscCall(DMSwarmGetLocalSize(user->post_swarm, &post_local));
229 PetscCall(DMSwarmGetSize(user->post_swarm, &post_global));
230 PetscCall(PicurvAssertIntEqual(source_local, post_local,
231 "derived particle fields must share source local ownership"));
232 PetscCall(PicurvAssertIntEqual(source_global, post_global,
233 "derived and source swarms must have the same global size"));
234
235 PetscCall(DMSwarmGetField(user->post_swarm, "ske", NULL, NULL, (void **)&ske));
236 for (PetscInt p = 0; p < source_local; ++p) {
237 const PetscReal tag = (PetscReal)(offset + p + 1);
238 const PetscReal expected = 0.5 * (tag * tag + 5.0);
239
240 local_error = PetscMax(local_error,
241 PetscAbsReal(PetscRealPart(ske[p]) - expected));
242 }
243 PetscCall(DMSwarmRestoreField(user->post_swarm, "ske", NULL, NULL, (void **)&ske));
244 PetscCall(AssertGlobalError(local_error, 1.0e-12,
245 "specific kinetic energy must match every source particle"));
246
247 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
248 PetscFunctionReturn(0);
249}
250
251/** @brief Checks that global resizing produces one balanced global population. */
252static PetscErrorCode TestResizeSwarmGloballyBalanced(void)
253{
254 SimCtx *simCtx = NULL;
255 UserCtx *user = NULL;
256 PetscMPIInt rank = 0, size = 1;
257 const PetscInt targets[] = {7, 3, 9, 0};
258
259 PetscFunctionBeginUser;
260 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
261 PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
262 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 4, 4, 4));
263 PetscCall(PicurvCreateSwarmPair(user, 0, "unused"));
264
265 for (size_t target_index = 0;
266 target_index < sizeof(targets) / sizeof(targets[0]); ++target_index) {
267 PetscInt local = 0;
268 PetscInt global = 0;
269 const PetscInt target = targets[target_index];
270 const PetscInt expected_local =
271 target / size + (rank < target % size ? 1 : 0);
272
273 PetscCall(ResizeSwarmGlobally(user->swarm, target));
274 PetscCall(DMSwarmGetLocalSize(user->swarm, &local));
275 PetscCall(DMSwarmGetSize(user->swarm, &global));
276 PetscCall(PicurvAssertIntEqual(expected_local, local,
277 "global swarm resize must use balanced local sizes"));
278 PetscCall(PicurvAssertIntEqual(target, global,
279 "global swarm resize must conserve the requested total"));
280 }
281
282 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
283 PetscFunctionReturn(0);
284}
285
286/** @brief Exercises accumulator-to-derived-nodal field statistics across ownership boundaries. */
288{
289 SimCtx *simCtx = NULL;
290 UserCtx *user = NULL;
291 PicurvWindowDefinition definition;
292 PicurvWindow window;
293 PicurvWindowStorage storage;
294 PetscReal ***pressure = NULL;
295 const PetscReal ***nodal = NULL;
296 Vec result = NULL;
297 PetscInt components = 0;
298 PetscInt local_checked = 0;
299 PetscInt global_checked = 0;
300 PetscReal local_error = 0.0;
302
303 PetscFunctionBeginUser;
304 PetscCall(PetscMemzero(&definition, sizeof(definition)));
305 PetscCall(PetscStrncpy(definition.name, "analytic", sizeof(definition.name)));
308 definition.step_cadence = 1;
309 definition.field_count = 1;
310 definition.fields[0].field_id = FIELD_ID_P;
311
312 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 8, 6, 4));
313 PetscCall(VecSet(user->Nvert, 0.0));
314 PetscCall(PicurvWindowInit(&window, &definition));
315 PetscCall(PicurvWindowStorageCreate(user, &definition, &storage));
316 simCtx->fieldStatisticsEnabled = PETSC_TRUE;
317 simCtx->fieldStatisticsWindowCount = 1;
318 simCtx->fieldStatisticsWindows = &window;
319 user->fieldStatisticsStorage = &storage;
320
321 PetscCall(DMDAVecGetArray(user->da, user->P, &pressure));
322 for (PetscInt k = user->info.zs; k < user->info.zs + user->info.zm; ++k)
323 for (PetscInt j = user->info.ys; j < user->info.ys + user->info.ym; ++j)
324 for (PetscInt i = user->info.xs; i < user->info.xs + user->info.xm; ++i)
325 pressure[k][j][i] = ScalarValue(i, j, k);
326 PetscCall(DMDAVecRestoreArray(user->da, user->P, &pressure));
327 PetscCall(PicurvWindowAccumulate(user, &definition, &storage, 1.0));
328 PetscCall(ComputeWindowStatisticNodal(user, &definition, &storage, "mean", 0, name, sizeof(name),
329 &result, &components));
330 PetscCall(PicurvAssertIntEqual(1, components,
331 "the pressure mean must derive as a scalar field"));
332 PetscCall(PicurvAssertBool((PetscBool)(result == user->PostScalarNodal),
333 "the pressure mean must use the scalar nodal staging vector"));
334
335 PetscCall(DMDAVecGetArrayRead(user->da, result, &nodal));
336 for (PetscInt k = PetscMax(user->info.zs, 1);
337 k < PetscMin(user->info.zs + user->info.zm, user->info.mz - 2); ++k) {
338 for (PetscInt j = PetscMax(user->info.ys, 1);
339 j < PetscMin(user->info.ys + user->info.ym, user->info.my - 2); ++j) {
340 for (PetscInt i = PetscMax(user->info.xs, 1);
341 i < PetscMin(user->info.xs + user->info.xm, user->info.mx - 2); ++i) {
342 local_error = PetscMax(
343 local_error,
344 PetscAbsReal(nodal[k][j][i] - (ScalarValue(i, j, k) + 55.5)));
345 ++local_checked;
346 }
347 }
348 }
349 PetscCall(DMDAVecRestoreArrayRead(user->da, result, &nodal));
350 PetscCallMPI(MPI_Allreduce(&local_checked, &global_checked, 1, MPIU_INT, MPI_SUM,
351 PETSC_COMM_WORLD));
352 PetscCall(PicurvAssertIntEqual((user->IM - 2) * (user->JM - 2) * (user->KM - 2),
353 global_checked,
354 "the derived mean must cover every fully resolved nodal point"));
355 PetscCall(AssertGlobalError(local_error, 1.0e-12,
356 "field-statistics nodal derivation must cross rank interfaces"));
357
358 simCtx->fieldStatisticsEnabled = PETSC_FALSE;
359 simCtx->fieldStatisticsWindowCount = 0;
360 simCtx->fieldStatisticsWindows = NULL;
361 user->fieldStatisticsStorage = NULL;
362 PetscCall(PicurvWindowStorageDestroy(&storage));
363 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
364 PetscFunctionReturn(0);
365}
366
367/**
368 * @brief A wall-normal profile CSV holds each plane's pooled moments on any decomposition.
369 *
370 * One state of a field varying across every plane is merged over i and k. Each written
371 * row must carry the mean and RMS over that plane's points, computed here directly
372 * from the closed form, and the plane's mean cell-centre coordinate.
373 */
374static PetscErrorCode TestFieldStatisticsProfileMultiRank(void)
375{
376 SimCtx *simCtx = NULL;
377 UserCtx *user = NULL;
378 PicurvWindowDefinition definition;
379 PicurvWindowStorage storage;
381 const PetscBool over_ik[3] = {PETSC_TRUE, PETSC_FALSE, PETSC_TRUE};
382 PetscReal ***pressure = NULL, sums[3][64], reduced[3][64];
383 PetscMPIInt rank = 0;
384 char tmpdir[PETSC_MAX_PATH_LEN], prefix[PETSC_MAX_PATH_LEN + 16], path[PETSC_MAX_PATH_LEN + 64];
385
386 PetscFunctionBeginUser;
387 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
388 PetscCall(PetscMemzero(&definition, sizeof(definition)));
389 PetscCall(PetscStrncpy(definition.name, "plane", sizeof(definition.name)));
392 definition.step_cadence = 1;
393 definition.field_count = 1;
394 definition.fields[0].field_id = FIELD_ID_P;
395 definition.fields[0].want_second = PETSC_TRUE;
396
397 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 8, 6, 4));
398 PetscCall(VecSet(user->Nvert, 0.0));
399 PetscCall(VecSet(user->lNvert, 0.0));
400 PetscCall(PicurvPopulateUniformCellCenters(user));
401 PetscCall(PicurvWindowStorageCreate(user, &definition, &storage));
402 PetscCall(DMDAVecGetArray(user->da, user->P, &pressure));
403 for (PetscInt k = user->info.zs; k < user->info.zs + user->info.zm; ++k)
404 for (PetscInt j = user->info.ys; j < user->info.ys + user->info.ym; ++j)
405 for (PetscInt i = user->info.xs; i < user->info.xs + user->info.xm; ++i)
406 pressure[k][j][i] = ScalarValue(i, j, k);
407 PetscCall(DMDAVecRestoreArray(user->da, user->P, &pressure));
408 PetscCall(PicurvWindowAccumulate(user, &definition, &storage, 1.0));
409
410 /* The expected profile, straight from the closed form over each rank's points. */
412 PetscCall(PetscMemzero(sums, sizeof(sums)));
413 for (PetscInt k = plan.start[2]; k < plan.end[2]; ++k)
414 for (PetscInt j = plan.start[1]; j < plan.end[1]; ++j)
415 for (PetscInt i = plan.start[0]; i < plan.end[0]; ++i) {
416 sums[0][j] += 1.0;
417 sums[1][j] += ScalarValue(i, j, k);
418 sums[2][j] += PetscSqr(ScalarValue(i, j, k));
419 }
420 PetscCallMPI(MPI_Allreduce(sums, reduced, 3 * 64, MPIU_REAL, MPI_SUM, PETSC_COMM_WORLD));
421
422 if (rank == 0) PetscCall(PicurvMakeTempDir(tmpdir, sizeof(tmpdir)));
423 PetscCallMPI(MPI_Bcast(tmpdir, PETSC_MAX_PATH_LEN, MPI_CHAR, 0, PETSC_COMM_WORLD));
424 PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "%s/Field", tmpdir));
425 PetscCall(ComputeWindowStatisticsProfile(user, &definition, &storage, "mean,rms", over_ik, prefix, 7));
426
427 if (rank == 0) {
428 char line[1024];
429 PetscInt rows = 0, expected_rows = 0;
430 FILE *csv = NULL;
431
432 PetscCall(PetscSNPrintf(path, sizeof(path), "%s_profile_plane_00007.csv", prefix));
433 csv = fopen(path, "r");
434 PetscCheck(csv != NULL, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Cannot open %s", path);
435 PetscCheck(fgets(line, sizeof(line), csv) != NULL, PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "Empty profile");
436 PetscCall(PicurvAssertBool((PetscBool)(strcmp(line, "j,x,y,z,plane_P_mean,plane_P_rms\n") == 0),
437 "the header names the retained index, coordinates and outputs"));
438 while (fgets(line, sizeof(line), csv)) {
439 int cell = -1;
440 double x, y, z, mean, rms;
441 PetscReal expected_mean, expected_rms;
442
443 PetscCheck(sscanf(line, "%d,%lf,%lf,%lf,%lf,%lf", &cell, &x, &y, &z, &mean, &rms) == 6,
444 PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "Malformed profile row: %s", line);
445 expected_mean = reduced[1][cell + 1] / reduced[0][cell + 1];
446 expected_rms = PetscSqrtReal(reduced[2][cell + 1] / reduced[0][cell + 1] - PetscSqr(expected_mean));
447 PetscCall(PicurvAssertRealNear(expected_mean, mean, 1.0e-9, "the row mean is the plane mean"));
448 PetscCall(PicurvAssertRealNear(expected_rms, rms, 1.0e-9, "the row RMS is the plane's pooled RMS"));
449 PetscCall(PicurvAssertRealNear((cell + 0.5) / (PetscReal)user->JM, y, 1.0e-9,
450 "the row coordinate is the plane's cell centre"));
451 rows++;
452 }
453 fclose(csv);
454 for (PetscInt j = 0; j < 64; ++j) if (reduced[0][j] > 0.0) expected_rows++;
455 PetscCall(PicurvAssertIntEqual(expected_rows, rows, "one row per sampled plane"));
456 PetscCall(PicurvRemoveTempDir(tmpdir));
457 }
458
459 PetscCall(PicurvWindowStorageDestroy(&storage));
460 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
461 PetscFunctionReturn(0);
462}
463
464/**
465 * @brief A `point_variance` profile is the plane mean of each point's own variance.
466 *
467 * Two states differ at each point by i + 1, so every point's variance is ((i + 1)/2)^2
468 * while the point means also vary across the plane. The profile must carry the
469 * equal-weight mean of the point variances, without the spread of the point means
470 * that the pooled RMS includes, on any decomposition.
471 */
473{
474 SimCtx *simCtx = NULL;
475 UserCtx *user = NULL;
476 PicurvWindowDefinition definition;
477 PicurvWindowStorage storage;
479 const PetscBool over_ik[3] = {PETSC_TRUE, PETSC_FALSE, PETSC_TRUE};
480 PetscReal sums[2][64], reduced[2][64];
481 PetscMPIInt rank = 0;
482 char tmpdir[PETSC_MAX_PATH_LEN], prefix[PETSC_MAX_PATH_LEN + 16], path[PETSC_MAX_PATH_LEN + 64];
483
484 PetscFunctionBeginUser;
485 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
486 PetscCall(PetscMemzero(&definition, sizeof(definition)));
487 PetscCall(PetscStrncpy(definition.name, "plane", sizeof(definition.name)));
490 definition.step_cadence = 1;
491 definition.field_count = 1;
492 definition.fields[0].field_id = FIELD_ID_P;
493 definition.fields[0].want_second = PETSC_TRUE;
494
495 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 8, 6, 4));
496 PetscCall(VecSet(user->Nvert, 0.0));
497 PetscCall(VecSet(user->lNvert, 0.0));
498 PetscCall(PicurvPopulateUniformCellCenters(user));
499 PetscCall(PicurvWindowStorageCreate(user, &definition, &storage));
500 for (PetscInt state = 0; state < 2; ++state) {
501 PetscReal ***pressure = NULL;
502
503 PetscCall(DMDAVecGetArray(user->da, user->P, &pressure));
504 for (PetscInt k = user->info.zs; k < user->info.zs + user->info.zm; ++k)
505 for (PetscInt j = user->info.ys; j < user->info.ys + user->info.ym; ++j)
506 for (PetscInt i = user->info.xs; i < user->info.xs + user->info.xm; ++i)
507 pressure[k][j][i] = ScalarValue(i, j, k) + state * (PetscReal)(i + 1);
508 PetscCall(DMDAVecRestoreArray(user->da, user->P, &pressure));
509 PetscCall(PicurvWindowAccumulate(user, &definition, &storage, 1.0));
510 }
511
513 PetscCall(PetscMemzero(sums, sizeof(sums)));
514 for (PetscInt k = plan.start[2]; k < plan.end[2]; ++k)
515 for (PetscInt j = plan.start[1]; j < plan.end[1]; ++j)
516 for (PetscInt i = plan.start[0]; i < plan.end[0]; ++i) {
517 sums[0][j] += 1.0;
518 sums[1][j] += PetscSqr(0.5 * (PetscReal)(i + 1));
519 }
520 PetscCallMPI(MPI_Allreduce(sums, reduced, 2 * 64, MPIU_REAL, MPI_SUM, PETSC_COMM_WORLD));
521
522 if (rank == 0) PetscCall(PicurvMakeTempDir(tmpdir, sizeof(tmpdir)));
523 PetscCallMPI(MPI_Bcast(tmpdir, PETSC_MAX_PATH_LEN, MPI_CHAR, 0, PETSC_COMM_WORLD));
524 PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "%s/Field", tmpdir));
525 PetscCall(ComputeWindowStatisticsProfile(user, &definition, &storage, "rms,point_variance", over_ik, prefix, 3));
526
527 if (rank == 0) {
528 char line[1024];
529 PetscInt rows = 0;
530 FILE *csv = NULL;
531
532 PetscCall(PetscSNPrintf(path, sizeof(path), "%s_profile_plane_00003.csv", prefix));
533 csv = fopen(path, "r");
534 PetscCheck(csv != NULL, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Cannot open %s", path);
535 PetscCheck(fgets(line, sizeof(line), csv) != NULL, PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "Empty profile");
536 PetscCall(PicurvAssertBool((PetscBool)(strcmp(line, "j,x,y,z,plane_P_rms,plane_P_point_variance\n") == 0),
537 "the point variance is written beside the pooled RMS"));
538 while (fgets(line, sizeof(line), csv)) {
539 int cell = -1;
540 double x, y, z, rms, point_variance;
541 PetscCheck(sscanf(line, "%d,%lf,%lf,%lf,%lf,%lf", &cell, &x, &y, &z, &rms, &point_variance) == 6,
542 PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "Malformed profile row: %s", line);
543 PetscCall(PicurvAssertRealNear(reduced[1][cell + 1] / reduced[0][cell + 1], point_variance, 1.0e-12,
544 "the row point variance is the plane mean of point variances"));
545 PetscCall(PicurvAssertBool((PetscBool)(PetscSqr(rms) > point_variance + 1.0e-6),
546 "the pooled variance also carries the spread of the point means"));
547 rows++;
548 }
549 fclose(csv);
550 PetscInt expected_rows = 0;
551 for (PetscInt j = 0; j < 64; ++j) if (reduced[0][j] > 0.0) expected_rows++;
552 PetscCall(PicurvAssertIntEqual(expected_rows, rows, "one row per sampled plane"));
553 PetscCall(PicurvRemoveTempDir(tmpdir));
554 }
555
556 PetscCall(PicurvWindowStorageDestroy(&storage));
557 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
558 PetscFunctionReturn(0);
559}
560
561/** @brief Runs the serial/MPI post-processing compute regression suite. */
562int main(int argc, char **argv)
563{
564 PetscErrorCode ierr;
565 const PicurvTestCase cases[] = {
566 {"eulerian-pipeline-decomposition-independent", TestEulerianPipelineDecompositionIndependent},
567 {"particle-pipeline-matches-source-ownership", TestParticlePipelineMatchesSourceOwnership},
568 {"resize-swarm-globally-balanced", TestResizeSwarmGloballyBalanced},
569 {"field-statistics-derived-nodal-multi-rank", TestFieldStatisticsDerivedNodalMultiRank},
570 {"field-statistics-profile-multi-rank", TestFieldStatisticsProfileMultiRank},
571 {"field-statistics-point-variance-profile", TestFieldStatisticsPointVarianceProfile},
572 };
573
574 ierr = PetscInitialize(&argc, &argv, NULL,
575 "PICurv serial/MPI post-processing compute tests");
576 if (ierr) return (int)ierr;
577 ierr = PicurvRunTests("unit-post-compute-mpi", cases,
578 sizeof(cases) / sizeof(cases[0]));
579 if (ierr) {
580 PetscFinalize();
581 return (int)ierr;
582 }
583 ierr = PetscFinalize();
584 return (int)ierr;
585}
PetscErrorCode ResizeSwarmGlobally(DM swarm, PetscInt N_target)
Resizes a swarm collectively to a target global particle count.
@ FIELD_ID_P
PetscErrorCode ComputeWindowStatisticsProfile(UserCtx *user, const PicurvWindowDefinition *definition, const PicurvWindowStorage *storage, const char *outputs, const PetscBool average_direction[3], const char *output_prefix, PetscInt ti)
Writes a window's statistics merged over homogeneous directions as a profile.
PetscErrorCode ComputeWindowStatisticNodal(UserCtx *user, const PicurvWindowDefinition *definition, const PicurvWindowStorage *storage, const char *outputs, PetscInt output_index, char *out_name, size_t name_size, Vec *out_vec, PetscInt *out_components)
Derives one accumulated statistic and converts it to nodal values.
PetscErrorCode DimensionalizeField(UserCtx *user, const char *field_name)
Scales a specified field from non-dimensional to dimensional units in-place.
PetscErrorCode EulerianDataProcessingPipeline(UserCtx *user, PostProcessParams *pps)
Parses the processing pipeline string and executes the requested kernels.
PetscErrorCode ParticleDataProcessingPipeline(UserCtx *user, PostProcessParams *pps)
Parses and executes the particle pipeline using a robust two-pass approach.
PetscErrorCode SetupPostProcessSwarm(UserCtx *user, PostProcessParams *pps)
Creates a new, dedicated DMSwarm for post-processing tasks.
Per-window PETSc accumulator storage and pointwise application.
#define PICURV_STATISTICS_PAYLOAD_NAME_LENGTH
Maximum stored length of a payload name, including the terminator.
PetscErrorCode PicurvWindowStorageCreate(UserCtx *user, const PicurvWindowDefinition *definition, PicurvWindowStorage *storage)
Allocates the accumulator state one window owns on one block.
PetscErrorCode PicurvWindowAccumulate(UserCtx *user, const PicurvWindowDefinition *definition, PicurvWindowStorage *storage, PetscReal weight)
Applies one accepted completed state to a window's accumulators.
PetscErrorCode PicurvWindowStorageDestroy(PicurvWindowStorage *storage)
Releases accumulator state previously created for one window.
Independent accumulator state for one window on one block.
Spatial target resolution for the field-statistics pipeline.
@ PICURV_STATISTICS_MASK_FLUID
PetscInt end[3]
Exclusive end per dimension (i, j, k).
PetscInt start[3]
Inclusive start per dimension (i, j, k).
PetscErrorCode SpatialTargetPlanCreate(UserCtx *user, FieldId field_id, PicurvStatisticsMask mask, SpatialTargetPlan *plan)
Resolves the iteration domain for one field on one block.
Resolved iteration domain for one field on one block.
Window lifecycle, scheduling, and weighting for the field-statistics pipeline.
PicurvWindowFieldRequest fields[16]
PicurvCadenceKind cadence_kind
PetscInt step_cadence
Used when cadence_kind is step; must be positive.
PetscErrorCode PicurvWindowInit(PicurvWindow *window, const PicurvWindowDefinition *definition)
Validates a definition and initializes a window to the pending state.
PetscBool want_second
Also keep the centered second moment.
PetscInt field_id
Catalogued Eulerian field identity.
@ PICURV_WEIGHTING_SAMPLE
Equal weight per accepted state.
@ PICURV_CADENCE_STEP
Every n completed steps from activation.
Runtime state of one window.
The scientifically immutable definition of one window.
static PetscErrorCode TestResizeSwarmGloballyBalanced(void)
Checks that global resizing produces one balanced global population.
static PetscErrorCode TestFieldStatisticsDerivedNodalMultiRank(void)
Exercises accumulator-to-derived-nodal field statistics across ownership boundaries.
static PetscErrorCode TestParticlePipelineMatchesSourceOwnership(void)
Verifies the production-created derived swarm mirrors source ownership.
static const PetscReal kSentinel
static PetscReal ScalarValue(PetscInt i, PetscInt j, PetscInt k)
Analytic scalar field used to expose misplaced or missing grid points.
int main(int argc, char **argv)
Runs the serial/MPI post-processing compute regression suite.
static PetscErrorCode AssertGlobalError(PetscReal local_error, PetscReal tolerance, const char *context)
Requires every rank's maximum error to be within tolerance.
static PetscErrorCode TestFieldStatisticsProfileMultiRank(void)
A wall-normal profile CSV holds each plane's pooled moments on any decomposition.
static PetscErrorCode TestEulerianPipelineDecompositionIndependent(void)
Exercises every Eulerian transformation currently dispatched by the pipeline.
static PetscErrorCode TestFieldStatisticsPointVarianceProfile(void)
A point_variance profile is the plane mean of each point's own variance.
PetscErrorCode PicurvMakeTempDir(char *path, size_t path_len)
Creates a unique temporary directory for one test case.
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 PicurvCreateSwarmPair(UserCtx *user, PetscInt nlocal, const char *post_field_name)
Creates matched solver and post-processing swarms for tests.
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 PicurvPopulateUniformCellCenters(UserCtx *user)
Populates cell center coordinates for a uniform grid on [0,1]^3.
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.
PetscErrorCode PicurvRemoveTempDir(const char *path)
Recursively removes a temporary directory created by PicurvMakeTempDir.
Shared declarations for the PICurv C test fixture and assertion layer.
Named test case descriptor consumed by PicurvRunTests.
PetscInt fieldStatisticsWindowCount
Definition variables.h:989
Vec P_nodal
Definition variables.h:1281
Vec lNvert
Definition variables.h:1197
PetscInt KM
Definition variables.h:1171
PetscReal L_ref
Definition variables.h:898
DM post_swarm
Definition variables.h:1280
PetscInt reference[3]
Definition variables.h:844
Vec Ucat_nodal
Definition variables.h:1282
Vec PostScalarNodal
Definition variables.h:1219
PetscBool fieldStatisticsEnabled
Definition variables.h:988
Vec Qcrit
Definition variables.h:1283
struct PicurvWindow * fieldStatisticsWindows
Definition variables.h:990
char particle_pipeline[1024]
Definition variables.h:809
ScalingCtx scaling
Definition variables.h:1003
PetscInt JM
Definition variables.h:1171
char process_pipeline[1024]
Definition variables.h:806
struct PicurvWindowStorage * fieldStatisticsStorage
Definition variables.h:1222
DMDALocalInfo info
Definition variables.h:1169
PostProcessParams * pps
Definition variables.h:1131
PetscInt IM
Definition variables.h:1171
Vec Nvert
Definition variables.h:1197
PetscReal P_ref
Definition variables.h:901
PetscReal rho_ref
Definition variables.h:900
PetscReal U_ref
Definition variables.h:899
A 3D point or vector with PetscScalar components.
Definition variables.h:144
Holds all configuration parameters for a post-processing run.
Definition variables.h:793
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