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

C test module for PICurv. More...

#include "test_support.h"
#include "particle_statistics.h"
#include <stdio.h>
#include <string.h>
Include dependency graph for test_statistics.c:

Go to the source code of this file.

Functions

static PetscErrorCode TestComputeParticleMSDWritesCSV (void)
 Test-local routine.
 
static PetscErrorCode TestComputeParticleMSDEmptySwarmNoOutput (void)
 Test-local routine.
 
int main (int argc, char **argv)
 Entry point for this unit-test binary.
 

Detailed Description

C test module for PICurv.

Definition in file test_statistics.c.

Function Documentation

◆ TestComputeParticleMSDWritesCSV()

static PetscErrorCode TestComputeParticleMSDWritesCSV ( void  )
static

Test-local routine.

Definition at line 16 of file test_statistics.c.

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(PicurvDestroyMinimalContexts(&simCtx, &user));
66 PetscFunctionReturn(0);
67}
PetscErrorCode ComputeParticleMSD(UserCtx *user, const char *stats_prefix, PetscInt ti)
Computes the mean-squared displacement (MSD) of a particle cloud.
PetscErrorCode PicurvMakeTempDir(char *path, size_t path_len)
Shared test-support routine.
PetscErrorCode PicurvCreateMinimalContexts(SimCtx **simCtx_out, UserCtx **user_out, PetscInt mx, PetscInt my, PetscInt mz)
Shared test-support routine.
PetscErrorCode PicurvDestroyMinimalContexts(SimCtx **simCtx_ptr, UserCtx **user_ptr)
Shared test-support routine.
PetscErrorCode PicurvCreateSwarmPair(UserCtx *user, PetscInt nlocal, const char *post_field_name)
Shared test-support routine.
PetscErrorCode PicurvAssertFileExists(const char *path, const char *context)
Shared test-support routine.
PetscErrorCode PicurvAssertBool(PetscBool value, const char *context)
Shared test-support routine.
PetscReal schmidt_number
Definition variables.h:653
PetscReal psrc_x
Definition variables.h:650
PetscReal ren
Definition variables.h:638
PetscReal dt
Definition variables.h:606
PetscReal psrc_z
Point source location for PARTICLE_INIT_POINT_SOURCE.
Definition variables.h:650
PetscReal psrc_y
Definition variables.h:650
The master context for the entire simulation.
Definition variables.h:591
User-defined context containing data specific to a single computational grid level.
Definition variables.h:738
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestComputeParticleMSDEmptySwarmNoOutput()

static PetscErrorCode TestComputeParticleMSDEmptySwarmNoOutput ( void  )
static

Test-local routine.

Definition at line 72 of file test_statistics.c.

73{
74 SimCtx *simCtx = NULL;
75 UserCtx *user = NULL;
76 char tmpdir[PETSC_MAX_PATH_LEN];
77 char csv_prefix[PETSC_MAX_PATH_LEN];
78 char csv_path[PETSC_MAX_PATH_LEN];
79 PetscBool exists = PETSC_FALSE;
80
81 PetscFunctionBeginUser;
82 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 4, 4, 4));
83 PetscCall(PicurvCreateSwarmPair(user, 0, "ske"));
84
85 PetscCall(PicurvMakeTempDir(tmpdir, sizeof(tmpdir)));
86 PetscCall(PetscSNPrintf(csv_prefix, sizeof(csv_prefix), "%s/stats", tmpdir));
87 PetscCall(PetscSNPrintf(csv_path, sizeof(csv_path), "%s_msd.csv", csv_prefix));
88
89 PetscCall(ComputeParticleMSD(user, csv_prefix, 5));
90 PetscCall(VerifyPathExistence(csv_path, PETSC_FALSE, PETSC_TRUE, "MSD CSV for empty swarm", &exists));
91 PetscCall(PicurvAssertBool((PetscBool)!exists,
92 "ComputeParticleMSD should not write output when no particles are present"));
93
94 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
95 PetscFunctionReturn(0);
96}
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char **  argv 
)

Entry point for this unit-test binary.

Definition at line 101 of file test_statistics.c.

102{
103 PetscErrorCode ierr;
104 const PicurvTestCase cases[] = {
105 {"compute-particle-msd-writes-csv", TestComputeParticleMSDWritesCSV},
106 {"compute-particle-msd-empty-swarm-no-output", TestComputeParticleMSDEmptySwarmNoOutput},
107 };
108
109 ierr = PetscInitialize(&argc, &argv, NULL, "PICurv statistics tests");
110 if (ierr) {
111 return (int)ierr;
112 }
113
114 ierr = PicurvRunTests("unit-post-statistics", cases, sizeof(cases) / sizeof(cases[0]));
115 if (ierr) {
116 PetscFinalize();
117 return (int)ierr;
118 }
119
120 ierr = PetscFinalize();
121 return (int)ierr;
122}
static PetscErrorCode TestComputeParticleMSDEmptySwarmNoOutput(void)
Test-local routine.
static PetscErrorCode TestComputeParticleMSDWritesCSV(void)
Test-local routine.
PetscErrorCode PicurvRunTests(const char *suite_name, const PicurvTestCase *cases, size_t case_count)
Shared test-support routine.
Named test case descriptor consumed by PicurvRunTests.
Here is the call graph for this function: