Tests MSD CSV generation for a populated particle swarm.
17{
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;
37
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
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
61 "MSD CSV header should contain MSD_total"));
63 "MSD CSV row should include the particle count"));
64
67 PetscFunctionReturn(0);
68}
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)
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 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.
PetscReal psrc_z
Point source location for PARTICLE_INIT_POINT_SOURCE.
The master context for the entire simulation.
User-defined context containing data specific to a single computational grid level.