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

C unit tests for VTK output preparation and file writing helpers. More...

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

Go to the source code of this file.

Functions

static PetscErrorCode TestPrepareOutputCoordinatesSubsamplesInteriorGrid (void)
 Tests coordinate extraction with interior-grid subsampling.
 
static PetscErrorCode TestPrepareOutputEulerianFieldDataSubsamplesScalar (void)
 Tests Eulerian scalar field extraction with subsampling.
 
static PetscErrorCode TestPrepareOutputParticleDataSubsampling (void)
 Tests particle-data extraction with output subsampling.
 
static PetscErrorCode TestCreateVTKFileFromMetadataWritesStructuredGrid (void)
 Tests structured-grid VTK file creation from assembled metadata.
 
int main (int argc, char **argv)
 Runs the unit-post-vtk PETSc test binary.
 

Detailed Description

C unit tests for VTK output preparation and file writing helpers.

Definition in file test_vtk_io.c.

Function Documentation

◆ TestPrepareOutputCoordinatesSubsamplesInteriorGrid()

static PetscErrorCode TestPrepareOutputCoordinatesSubsamplesInteriorGrid ( void  )
static

Tests coordinate extraction with interior-grid subsampling.

Definition at line 16 of file test_vtk_io.c.

17{
18 SimCtx *simCtx = NULL;
19 UserCtx *user = NULL;
20 PetscScalar *coords = NULL;
21 PetscInt nx = 0, ny = 0, nz = 0, npoints = 0, local_start[3], local_size[3];
22 PetscReal expected_last_coord = 0.0;
23
24 PetscFunctionBeginUser;
25 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 4, 4, 4));
26 expected_last_coord = (PetscReal)(user->IM - 1) / (PetscReal)user->IM;
27
28 PetscCall(PrepareOutputCoordinates(user, &coords, &nx, &ny, &nz, &npoints, local_start, local_size));
29 PetscCall(PicurvAssertIntEqual(4, nx, "PrepareOutputCoordinates should output nx based on the production IM+1 DA"));
30 PetscCall(PicurvAssertIntEqual(4, ny, "PrepareOutputCoordinates should output ny based on the production JM+1 DA"));
31 PetscCall(PicurvAssertIntEqual(4, nz, "PrepareOutputCoordinates should output nz based on the production KM+1 DA"));
32 PetscCall(PicurvAssertIntEqual(64, npoints, "PrepareOutputCoordinates should output the full physical-node lattice"));
33
34 if (simCtx->size == 1) {
35 PetscCall(PicurvAssertIntEqual(64, local_size[0] * local_size[1] * local_size[2],
36 "One rank should hold the whole output grid"));
37 PetscCall(PicurvAssertBool((PetscBool)(coords != NULL), "PrepareOutputCoordinates should allocate this rank's coordinates"));
38 PetscCall(PicurvAssertRealNear(0.0, PetscRealPart(coords[0]), 1.0e-12, "First coordinate x should be 0"));
39 PetscCall(PicurvAssertRealNear(0.0, PetscRealPart(coords[1]), 1.0e-12, "First coordinate y should be 0"));
40 PetscCall(PicurvAssertRealNear(0.0, PetscRealPart(coords[2]), 1.0e-12, "First coordinate z should be 0"));
41
42 PetscCall(PicurvAssertRealNear(expected_last_coord, PetscRealPart(coords[3 * (npoints - 1) + 0]), 1.0e-12, "Last output coordinate x should be the normalized physical-node domain end"));
43 PetscCall(PicurvAssertRealNear(expected_last_coord, PetscRealPart(coords[3 * (npoints - 1) + 1]), 1.0e-12, "Last output coordinate y should be the normalized physical-node domain end"));
44 PetscCall(PicurvAssertRealNear(expected_last_coord, PetscRealPart(coords[3 * (npoints - 1) + 2]), 1.0e-12, "Last output coordinate z should be the normalized physical-node domain end"));
45 }
46 PetscCall(PetscFree(coords));
47
48 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
49 PetscFunctionReturn(0);
50}
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 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.
PetscMPIInt size
Definition variables.h:920
PetscInt IM
Definition variables.h:1171
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
PetscErrorCode PrepareOutputCoordinates(UserCtx *user, PetscScalar **out_coords, PetscInt *out_nx, PetscInt *out_ny, PetscInt *out_nz, PetscInt *out_npoints, PetscInt local_start[3], PetscInt local_size[3])
Copies this rank's part of the output grid's coordinates.
Definition vtk_io.c:260
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestPrepareOutputEulerianFieldDataSubsamplesScalar()

static PetscErrorCode TestPrepareOutputEulerianFieldDataSubsamplesScalar ( void  )
static

Tests Eulerian scalar field extraction with subsampling.

Definition at line 55 of file test_vtk_io.c.

56{
57 SimCtx *simCtx = NULL;
58 UserCtx *user = NULL;
59 PetscScalar *field_out = NULL;
60 PetscInt start = 0, end = 0;
61
62 PetscFunctionBeginUser;
63 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 4, 4, 4));
64
65 PetscCall(VecGetOwnershipRange(user->P_nodal, &start, &end));
66 for (PetscInt idx = start; idx < end; ++idx) {
67 PetscCall(VecSetValue(user->P_nodal, idx, (PetscScalar)idx, INSERT_VALUES));
68 }
69 PetscCall(VecAssemblyBegin(user->P_nodal));
70 PetscCall(VecAssemblyEnd(user->P_nodal));
71
72 PetscCall(PrepareOutputEulerianFieldData(user, user->P_nodal, 1, &field_out));
73 if (simCtx->size == 1) {
74 PetscCall(PicurvAssertBool((PetscBool)(field_out != NULL),
75 "PrepareOutputEulerianFieldData should allocate this rank's data"));
76 PetscCall(PicurvAssertRealNear(0.0, PetscRealPart(field_out[0]), 1.0e-12, "Subsampled first value should map to source index 0"));
77 PetscCall(PicurvAssertRealNear(93.0, PetscRealPart(field_out[63]), 1.0e-12,
78 "Subsampled last value should map to source index (3,3,3)=93 in the production-sized DA"));
79 }
80 PetscCall(PetscFree(field_out));
81
82 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
83 PetscFunctionReturn(0);
84}
Vec P_nodal
Definition variables.h:1281
PetscErrorCode PrepareOutputEulerianFieldData(UserCtx *user, Vec field_vec, PetscInt num_components, PetscScalar **out_data)
Copies this rank's part of a nodal field on the output grid.
Definition vtk_io.c:361
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestPrepareOutputParticleDataSubsampling()

static PetscErrorCode TestPrepareOutputParticleDataSubsampling ( void  )
static

Tests particle-data extraction with output subsampling.

Definition at line 89 of file test_vtk_io.c.

90{
91 SimCtx *simCtx = NULL;
92 UserCtx *user = NULL;
94 VTKMetaData meta;
95 PetscInt n_total = 0;
96 PetscReal (*position)[3] = NULL;
97 PetscReal (*velocity)[3] = NULL;
98
99 PetscFunctionBeginUser;
100 PetscCall(PetscMemzero(&pps, sizeof(pps)));
101 PetscCall(PetscMemzero(&meta, sizeof(meta)));
102 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 4, 4, 4));
103 PetscCall(PicurvCreateSwarmPair(user, 5, "ske"));
104
105 pps.particle_output_freq = 2;
106 PetscCall(PetscStrncpy(pps.particle_fields, "position,velocity", sizeof(pps.particle_fields)));
107
108 PetscCall(DMSwarmGetField(user->swarm, "position", NULL, NULL, (void *)&position));
109 PetscCall(DMSwarmGetField(user->swarm, "velocity", NULL, NULL, (void *)&velocity));
110 for (PetscInt p = 0; p < 5; ++p) {
111 position[p][0] = (PetscReal)p;
112 position[p][1] = 0.0;
113 position[p][2] = 0.0;
114 velocity[p][0] = 10.0 + (PetscReal)p;
115 velocity[p][1] = 20.0 + (PetscReal)p;
116 velocity[p][2] = 30.0 + (PetscReal)p;
117 }
118 PetscCall(DMSwarmRestoreField(user->swarm, "position", NULL, NULL, (void *)&position));
119 PetscCall(DMSwarmRestoreField(user->swarm, "velocity", NULL, NULL, (void *)&velocity));
120
121 PetscCall(PrepareOutputParticleData(user, &pps, &meta, &n_total));
122 if (simCtx->size == 1) {
123 PetscCall(PicurvAssertIntEqual(5, n_total, "PrepareOutputParticleData should report total particles before subsampling"));
124 PetscCall(PicurvAssertIntEqual(3, meta.npoints, "Stride-2 subsampling over 5 particles should keep 3 output particles"));
125 PetscCall(PicurvAssertIntEqual(VTK_POLYDATA, meta.fileType, "Particle output metadata should be PolyData"));
126 PetscCall(PicurvAssertIntEqual(1, meta.num_point_data_fields, "Velocity field should be exported as one point-data field"));
127 PetscCall(PicurvAssertBool((PetscBool)(strcmp(meta.point_data_fields[0].name, "velocity") == 0),
128 "Particle field name should propagate to VTK metadata"));
129
130 PetscCall(PicurvAssertRealNear(0.0, PetscRealPart(meta.coords[0]), 1.0e-12, "First exported particle should be index 0"));
131 PetscCall(PicurvAssertRealNear(2.0, PetscRealPart(meta.coords[3]), 1.0e-12, "Second exported particle should be index 2"));
132 PetscCall(PicurvAssertRealNear(4.0, PetscRealPart(meta.coords[6]), 1.0e-12, "Third exported particle should be index 4"));
133 }
134 PetscCall(PetscFree(meta.coords));
135 for (PetscInt f = 0; f < meta.num_point_data_fields; ++f) {
136 PetscCall(PetscFree(meta.point_data_fields[f].data));
137 }
138 PetscCall(PetscFree(meta.connectivity));
139 PetscCall(PetscFree(meta.offsets));
140
141 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
142 PetscFunctionReturn(0);
143}
PetscErrorCode PicurvCreateSwarmPair(UserCtx *user, PetscInt nlocal, const char *post_field_name)
Creates matched solver and post-processing swarms for tests.
PetscInt npoints
Definition variables.h:874
PetscInt num_point_data_fields
Definition variables.h:879
PetscInt * connectivity
Definition variables.h:880
PetscInt * offsets
Definition variables.h:881
VTKFileType fileType
Definition variables.h:872
PetscScalar * data
Definition variables.h:855
char name[64]
Definition variables.h:853
PetscScalar * coords
Definition variables.h:877
PetscInt particle_output_freq
Definition variables.h:812
char particle_fields[1024]
Definition variables.h:810
VTKFieldInfo point_data_fields[20]
Definition variables.h:878
@ VTK_POLYDATA
Definition variables.h:861
Holds all configuration parameters for a post-processing run.
Definition variables.h:793
One VTK file being assembled collectively.
Definition variables.h:871
PetscErrorCode PrepareOutputParticleData(UserCtx *user, PostProcessParams *pps, VTKMetaData *meta, PetscInt *p_n_total)
Prepares this rank's part of the subsampled particle VTK output.
Definition vtk_io.c:384
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestCreateVTKFileFromMetadataWritesStructuredGrid()

static PetscErrorCode TestCreateVTKFileFromMetadataWritesStructuredGrid ( void  )
static

Tests structured-grid VTK file creation from assembled metadata.

Definition at line 148 of file test_vtk_io.c.

149{
150 char tmpdir[PETSC_MAX_PATH_LEN];
151 char vtk_path[PETSC_MAX_PATH_LEN];
152 VTKMetaData meta;
153 FILE *file = NULL;
154 char probe[512];
155 size_t nread = 0;
156
157 PetscFunctionBeginUser;
158 PetscCall(PetscMemzero(&meta, sizeof(meta)));
159 PetscCall(PicurvMakeTempDir(tmpdir, sizeof(tmpdir)));
160 PetscCall(PetscSNPrintf(vtk_path, sizeof(vtk_path), "%s/field_00001.vts", tmpdir));
161
163 meta.mx = 2;
164 meta.my = 2;
165 meta.mz = 2;
166 meta.npoints = 8;
167 meta.local_size[0] = meta.local_size[1] = meta.local_size[2] = 2; /* one rank holds the whole grid */
168 meta.num_point_data_fields = 1;
169
170 PetscCall(PetscMalloc1(3 * meta.npoints, &meta.coords));
171 PetscCall(PetscMalloc1(meta.npoints, &meta.point_data_fields[0].data));
172 PetscCall(PetscStrncpy(meta.point_data_fields[0].name, "P_nodal", sizeof(meta.point_data_fields[0].name)));
174
175 for (PetscInt p = 0; p < meta.npoints; ++p) {
176 meta.coords[3 * p + 0] = (PetscScalar)(p % 2);
177 meta.coords[3 * p + 1] = (PetscScalar)((p / 2) % 2);
178 meta.coords[3 * p + 2] = (PetscScalar)(p / 4);
179 meta.point_data_fields[0].data[p] = (PetscScalar)p;
180 }
181
182 PetscCall(CreateVTKFileFromMetadata(vtk_path, &meta, PETSC_COMM_WORLD));
183 PetscCall(PicurvAssertFileExists(vtk_path, "CreateVTKFileFromMetadata should emit a .vts file"));
184
185 file = fopen(vtk_path, "rb");
186 PetscCheck(file != NULL, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Failed to open generated VTK file '%s'.", vtk_path);
187 nread = fread(probe, 1, sizeof(probe) - 1, file);
188 fclose(file);
189 probe[nread] = '\0';
190 PetscCall(PicurvAssertBool((PetscBool)(strstr(probe, "StructuredGrid") != NULL),
191 "Generated VTK header should declare StructuredGrid type"));
192
193 PetscCall(PetscFree(meta.coords));
194 PetscCall(PetscFree(meta.point_data_fields[0].data));
195 PetscCall(PicurvRemoveTempDir(tmpdir));
196 PetscFunctionReturn(0);
197}
PetscErrorCode CreateVTKFileFromMetadata(const char *filename, const VTKMetaData *meta, MPI_Comm comm)
Creates a VTK file from prepared metadata and field payloads.
Definition vtk_io.c:172
PetscErrorCode PicurvMakeTempDir(char *path, size_t path_len)
Creates a unique temporary directory for one test case.
PetscErrorCode PicurvAssertFileExists(const char *path, const char *context)
Asserts that a filesystem path exists as a readable file.
PetscErrorCode PicurvRemoveTempDir(const char *path)
Recursively removes a temporary directory created by PicurvMakeTempDir.
PetscInt num_components
Definition variables.h:854
PetscInt local_size[3]
Definition variables.h:876
PetscInt mz
Definition variables.h:873
PetscInt my
Definition variables.h:873
PetscInt mx
Definition variables.h:873
@ VTK_STRUCTURED
Definition variables.h:860
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-post-vtk PETSc test binary.

Definition at line 202 of file test_vtk_io.c.

203{
204 PetscErrorCode ierr;
205 const PicurvTestCase cases[] = {
206 {"prepare-output-coordinates-subsamples-interior-grid", TestPrepareOutputCoordinatesSubsamplesInteriorGrid},
207 {"prepare-output-eulerian-field-data-subsamples-scalar", TestPrepareOutputEulerianFieldDataSubsamplesScalar},
208 {"prepare-output-particle-data-subsampling", TestPrepareOutputParticleDataSubsampling},
209 {"create-vtk-file-from-metadata-writes-structured-grid", TestCreateVTKFileFromMetadataWritesStructuredGrid},
210 };
211
212 ierr = PetscInitialize(&argc, &argv, NULL, "PICurv VTK I/O tests");
213 if (ierr) {
214 return (int)ierr;
215 }
216
217 ierr = PicurvRunTests("unit-post-vtk", cases, sizeof(cases) / sizeof(cases[0]));
218 if (ierr) {
219 PetscFinalize();
220 return (int)ierr;
221 }
222
223 ierr = PetscFinalize();
224 return (int)ierr;
225}
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.
static PetscErrorCode TestPrepareOutputEulerianFieldDataSubsamplesScalar(void)
Tests Eulerian scalar field extraction with subsampling.
Definition test_vtk_io.c:55
static PetscErrorCode TestPrepareOutputParticleDataSubsampling(void)
Tests particle-data extraction with output subsampling.
Definition test_vtk_io.c:89
static PetscErrorCode TestCreateVTKFileFromMetadataWritesStructuredGrid(void)
Tests structured-grid VTK file creation from assembled metadata.
static PetscErrorCode TestPrepareOutputCoordinatesSubsamplesInteriorGrid(void)
Tests coordinate extraction with interior-grid subsampling.
Definition test_vtk_io.c:16
Here is the call graph for this function: