PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
Loading...
Searching...
No Matches
test_statistics.c
Go to the documentation of this file.
1/**
2 * @file test_statistics.c
3 * @brief C unit tests for particle statistics kernels and CSV output.
4 */
5
6#include "test_support.h"
7
9
10#include <stdio.h>
11#include <string.h>
12/**
13 * @brief Tests MSD CSV generation for a populated particle swarm.
14 */
15
16static PetscErrorCode TestComputeParticleMSDWritesCSV(void)
17{
18 SimCtx *simCtx = NULL;
19 UserCtx *user = NULL;
20 char tmpdir[PETSC_MAX_PATH_LEN];
21 char csv_prefix[PETSC_MAX_PATH_LEN];
22 char csv_path[PETSC_MAX_PATH_LEN];
23 FILE *file = NULL;
24 char header[512];
25 char row[512];
26 PetscReal (*pos_arr)[3] = NULL;
27
28 PetscFunctionBeginUser;
29 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 4, 4, 4));
30 PetscCall(PicurvCreateSwarmPair(user, 2, "ske"));
31 simCtx->dt = 1.0;
32 simCtx->ren = 1.0;
33 simCtx->schmidt_number = 1.0;
34 simCtx->psrc_x = 0.0;
35 simCtx->psrc_y = 0.0;
36 simCtx->psrc_z = 0.0;
37
38 PetscCall(PicurvMakeTempDir(tmpdir, sizeof(tmpdir)));
39 PetscCall(PetscSNPrintf(csv_prefix, sizeof(csv_prefix), "%s/stats", tmpdir));
40 PetscCall(PetscSNPrintf(csv_path, sizeof(csv_path), "%s_msd.csv", csv_prefix));
41
42 PetscCall(DMSwarmGetField(user->swarm, "position", NULL, NULL, (void *)&pos_arr));
43 pos_arr[0][0] = 1.0;
44 pos_arr[0][1] = 0.0;
45 pos_arr[0][2] = 0.0;
46 pos_arr[1][0] = -1.0;
47 pos_arr[1][1] = 0.0;
48 pos_arr[1][2] = 0.0;
49 PetscCall(DMSwarmRestoreField(user->swarm, "position", NULL, NULL, (void *)&pos_arr));
50
51 PetscCall(ComputeParticleMSD(user, csv_prefix, 1));
52 PetscCall(PicurvAssertFileExists(csv_path, "ComputeParticleMSD should emit a CSV summary"));
53
54 file = fopen(csv_path, "r");
55 PetscCheck(file != NULL, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Failed to open generated stats CSV '%s'.", csv_path);
56 PetscCheck(fgets(header, sizeof(header), file) != NULL, PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "CSV header is missing.");
57 PetscCheck(fgets(row, sizeof(row), file) != NULL, PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "CSV data row is missing.");
58 fclose(file);
59
60 PetscCall(PicurvAssertBool((PetscBool)(strstr(header, "MSD_total") != NULL),
61 "MSD CSV header should contain MSD_total"));
62 PetscCall(PicurvAssertBool((PetscBool)(strstr(row, "2") != NULL),
63 "MSD CSV row should include the particle count"));
64
65 PetscCall(PicurvRemoveTempDir(tmpdir));
66 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
67 PetscFunctionReturn(0);
68}
69/**
70 * @brief Tests that empty swarms do not emit MSD CSV output.
71 */
72
74{
75 SimCtx *simCtx = NULL;
76 UserCtx *user = NULL;
77 char tmpdir[PETSC_MAX_PATH_LEN];
78 char csv_prefix[PETSC_MAX_PATH_LEN];
79 char csv_path[PETSC_MAX_PATH_LEN];
80 PetscBool exists = PETSC_FALSE;
81
82 PetscFunctionBeginUser;
83 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 4, 4, 4));
84 PetscCall(PicurvCreateSwarmPair(user, 0, "ske"));
85
86 PetscCall(PicurvMakeTempDir(tmpdir, sizeof(tmpdir)));
87 PetscCall(PetscSNPrintf(csv_prefix, sizeof(csv_prefix), "%s/stats", tmpdir));
88 PetscCall(PetscSNPrintf(csv_path, sizeof(csv_path), "%s_msd.csv", csv_prefix));
89
90 PetscCall(ComputeParticleMSD(user, csv_prefix, 5));
91 PetscCall(VerifyPathExistence(csv_path, PETSC_FALSE, PETSC_TRUE, "MSD CSV for empty swarm", &exists));
92 PetscCall(PicurvAssertBool((PetscBool)!exists,
93 "ComputeParticleMSD should not write output when no particles are present"));
94
95 PetscCall(PicurvRemoveTempDir(tmpdir));
96 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
97 PetscFunctionReturn(0);
98}
99/**
100 * @brief Runs the unit-post-statistics PETSc test binary.
101 */
102
103int main(int argc, char **argv)
104{
105 PetscErrorCode ierr;
106 const PicurvTestCase cases[] = {
107 {"compute-particle-msd-writes-csv", TestComputeParticleMSDWritesCSV},
108 {"compute-particle-msd-empty-swarm-no-output", TestComputeParticleMSDEmptySwarmNoOutput},
109 };
110
111 ierr = PetscInitialize(&argc, &argv, NULL, "PICurv statistics tests");
112 if (ierr) {
113 return (int)ierr;
114 }
115
116 ierr = PicurvRunTests("unit-post-statistics", cases, sizeof(cases) / sizeof(cases[0]));
117 if (ierr) {
118 PetscFinalize();
119 return (int)ierr;
120 }
121
122 ierr = PetscFinalize();
123 return (int)ierr;
124}
PetscErrorCode VerifyPathExistence(const char *path, PetscBool is_dir, PetscBool is_optional, const char *description, PetscBool *exists)
A parallel-safe helper to verify the existence of a generic file or directory path.
Definition io.c:738
Global statistics kernels for the Statistics Pipeline.
PetscErrorCode ComputeParticleMSD(UserCtx *user, const char *stats_prefix, PetscInt ti)
Computes the mean-squared displacement (MSD) of a particle cloud.
int main(int argc, char **argv)
Runs the unit-post-statistics PETSc test binary.
static PetscErrorCode TestComputeParticleMSDEmptySwarmNoOutput(void)
Tests that empty swarms do not emit MSD CSV output.
static PetscErrorCode TestComputeParticleMSDWritesCSV(void)
Tests MSD CSV generation for a populated particle swarm.
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 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 PicurvAssertFileExists(const char *path, const char *context)
Asserts that a filesystem path exists as a readable file.
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.
PetscReal schmidt_number
Definition variables.h:709
PetscReal psrc_x
Definition variables.h:706
PetscReal ren
Definition variables.h:691
PetscReal dt
Definition variables.h:658
PetscReal psrc_z
Point source location for PARTICLE_INIT_POINT_SOURCE.
Definition variables.h:706
PetscReal psrc_y
Definition variables.h:706
The master context for the entire simulation.
Definition variables.h:643
User-defined context containing data specific to a single computational grid level.
Definition variables.h:811