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

C test module for PICurv. More...

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

Go to the source code of this file.

Functions

static PetscErrorCode TestShouldWriteDataOutput (void)
 Test-local routine.
 
static PetscErrorCode TestVerifyPathExistence (void)
 Test-local routine.
 
static PetscErrorCode TestWriteAndReadSimulationFields (void)
 Test-local routine.
 
static PetscErrorCode TestParsePostProcessingSettings (void)
 Test-local routine.
 
static PetscErrorCode TestTrimWhitespace (void)
 Test-local routine.
 
static PetscErrorCode TestBoundaryConditionStringParsers (void)
 Test-local routine.
 
static PetscErrorCode TestValidateBCHandlerForBCType (void)
 Test-local routine.
 
static PetscErrorCode TestParseScalingInformation (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_io.c.

Function Documentation

◆ TestShouldWriteDataOutput()

static PetscErrorCode TestShouldWriteDataOutput ( void  )
static

Test-local routine.

Definition at line 16 of file test_io.c.

17{
18 SimCtx simCtx;
19
20 PetscFunctionBeginUser;
21 PetscCall(PetscMemzero(&simCtx, sizeof(simCtx)));
22 simCtx.tiout = 5;
23
24 PetscCall(PicurvAssertBool((PetscBool)!ShouldWriteDataOutput(NULL, 5), "NULL SimCtx should never request output"));
25 PetscCall(PicurvAssertBool((PetscBool)!ShouldWriteDataOutput(&simCtx, 4), "non-cadence step should not trigger output"));
26 PetscCall(PicurvAssertBool(ShouldWriteDataOutput(&simCtx, 10), "cadence-aligned step should trigger output"));
27 PetscFunctionReturn(0);
28}
PetscBool ShouldWriteDataOutput(const SimCtx *simCtx, PetscInt completed_step)
Returns whether full field/restart output should be written for the completed timestep.
Definition io.c:70
PetscErrorCode PicurvAssertBool(PetscBool value, const char *context)
Shared test-support routine.
PetscInt tiout
Definition variables.h:603
The master context for the entire simulation.
Definition variables.h:591
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestVerifyPathExistence()

static PetscErrorCode TestVerifyPathExistence ( void  )
static

Test-local routine.

Definition at line 33 of file test_io.c.

34{
35 char tmpdir[PETSC_MAX_PATH_LEN];
36 char filepath[PETSC_MAX_PATH_LEN];
37 FILE *file = NULL;
38 PetscBool exists = PETSC_FALSE;
39
40 PetscFunctionBeginUser;
41 PetscCall(PicurvMakeTempDir(tmpdir, sizeof(tmpdir)));
42 PetscCall(PetscSNPrintf(filepath, sizeof(filepath), "%s/sample.txt", tmpdir));
43
44 file = fopen(filepath, "w");
45 PetscCheck(file != NULL, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Failed to create temp file '%s'.", filepath);
46 fputs("picurv\n", file);
47 fclose(file);
48
49 PetscCall(VerifyPathExistence(tmpdir, PETSC_TRUE, PETSC_FALSE, "temp directory", &exists));
50 PetscCall(PicurvAssertBool(exists, "VerifyPathExistence should find the temp directory"));
51
52 PetscCall(VerifyPathExistence(filepath, PETSC_FALSE, PETSC_FALSE, "temp file", &exists));
53 PetscCall(PicurvAssertBool(exists, "VerifyPathExistence should find the temp file"));
54 PetscFunctionReturn(0);
55}
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
PetscErrorCode PicurvMakeTempDir(char *path, size_t path_len)
Shared test-support routine.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestWriteAndReadSimulationFields()

static PetscErrorCode TestWriteAndReadSimulationFields ( void  )
static

Test-local routine.

Definition at line 60 of file test_io.c.

61{
62 SimCtx *simCtx = NULL;
63 UserCtx *user = NULL;
64 char tmpdir[PETSC_MAX_PATH_LEN];
65 char euler_dir[PETSC_MAX_PATH_LEN];
66
67 PetscFunctionBeginUser;
68 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 4, 4, 4));
69 PetscCall(PicurvMakeTempDir(tmpdir, sizeof(tmpdir)));
70 PetscCall(PetscSNPrintf(euler_dir, sizeof(euler_dir), "%s/%s", tmpdir, simCtx->euler_subdir));
71 PetscCall(PicurvEnsureDir(euler_dir));
72
73 PetscCall(PetscStrncpy(simCtx->output_dir, tmpdir, sizeof(simCtx->output_dir)));
74 PetscCall(PetscStrncpy(simCtx->restart_dir, tmpdir, sizeof(simCtx->restart_dir)));
75 PetscCall(VecSet(user->P, 4.5));
76 PetscCall(VecSet(user->Nvert, 0.0));
77 PetscCall(VecSet(user->Ucat, 2.0));
78 PetscCall(VecSet(user->Ucont, 3.0));
79 PetscCall(PicurvPopulateIdentityMetrics(user));
80
81 PetscCall(WriteSimulationFields(user));
82 PetscCall(VecZeroEntries(user->P));
83 PetscCall(VecZeroEntries(user->Ucat));
84 PetscCall(VecZeroEntries(user->Ucont));
85
86 PetscCall(ReadSimulationFields(user, simCtx->step));
87 PetscCall(PicurvAssertVecConstant(user->P, 4.5, 1.0e-12, "ReadSimulationFields should restore P"));
88 PetscCall(PicurvAssertVecConstant(user->Ucat, 2.0, 1.0e-12, "ReadSimulationFields should restore Ucat"));
89 PetscCall(PicurvAssertVecConstant(user->Ucont, 3.0, 1.0e-12, "ReadSimulationFields should restore Ucont"));
90
91 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
92 PetscFunctionReturn(0);
93}
PetscErrorCode ReadSimulationFields(UserCtx *user, PetscInt ti)
Reads binary field data for velocity, pressure, and other required vectors.
Definition io.c:1126
PetscErrorCode WriteSimulationFields(UserCtx *user)
Writes simulation fields to files.
Definition io.c:1568
PetscErrorCode PicurvCreateMinimalContexts(SimCtx **simCtx_out, UserCtx **user_out, PetscInt mx, PetscInt my, PetscInt mz)
Shared test-support routine.
PetscErrorCode PicurvEnsureDir(const char *path)
Shared test-support routine.
PetscErrorCode PicurvDestroyMinimalContexts(SimCtx **simCtx_ptr, UserCtx **user_ptr)
Shared test-support routine.
PetscErrorCode PicurvAssertVecConstant(Vec vec, PetscScalar expected, PetscReal tol, const char *context)
Shared test-support routine.
PetscErrorCode PicurvPopulateIdentityMetrics(UserCtx *user)
Shared test-support routine.
char euler_subdir[PETSC_MAX_PATH_LEN]
Definition variables.h:613
char output_dir[PETSC_MAX_PATH_LEN]
Definition variables.h:612
Vec Ucont
Definition variables.h:764
Vec Ucat
Definition variables.h:764
PetscInt step
Definition variables.h:599
Vec Nvert
Definition variables.h:764
char restart_dir[PETSC_MAX_PATH_LEN]
Definition variables.h:611
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:

◆ TestParsePostProcessingSettings()

static PetscErrorCode TestParsePostProcessingSettings ( void  )
static

Test-local routine.

Definition at line 98 of file test_io.c.

99{
100 SimCtx *simCtx = NULL;
101 UserCtx *user = NULL;
102 char tmpdir[PETSC_MAX_PATH_LEN];
103 char cfg_path[PETSC_MAX_PATH_LEN];
104 FILE *file = NULL;
105
106 PetscFunctionBeginUser;
107 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, 4, 4, 4));
108 PetscCall(PetscCalloc1(1, &simCtx->pps));
109 PetscCall(PicurvMakeTempDir(tmpdir, sizeof(tmpdir)));
110 PetscCall(PetscSNPrintf(cfg_path, sizeof(cfg_path), "%s/post.run", tmpdir));
111 PetscCall(PetscStrncpy(simCtx->PostprocessingControlFile, cfg_path, sizeof(simCtx->PostprocessingControlFile)));
112
113 file = fopen(cfg_path, "w");
114 PetscCheck(file != NULL, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Failed to create temp config file '%s'.", cfg_path);
115 fputs("startTime = 2\n", file);
116 fputs("endTime = 6\n", file);
117 fputs("timeStep = 2\n", file);
118 fputs("output_particles = true\n", file);
119 fputs("output_prefix = SmokeField\n", file);
120 fclose(file);
121
122 PetscCall(ParsePostProcessingSettings(simCtx));
123 PetscCall(PicurvAssertIntEqual(2, simCtx->pps->startTime, "ParsePostProcessingSettings should parse startTime"));
124 PetscCall(PicurvAssertIntEqual(6, simCtx->pps->endTime, "ParsePostProcessingSettings should parse endTime"));
125 PetscCall(PicurvAssertIntEqual(2, simCtx->pps->timeStep, "ParsePostProcessingSettings should parse timeStep"));
126 PetscCall(PicurvAssertBool(simCtx->pps->outputParticles, "ParsePostProcessingSettings should parse output_particles"));
127 PetscCall(PicurvAssertBool((PetscBool)(strcmp(simCtx->pps->output_prefix, "SmokeField") == 0),
128 "ParsePostProcessingSettings should parse output_prefix"));
129
130 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
131 PetscFunctionReturn(0);
132}
PetscErrorCode ParsePostProcessingSettings(SimCtx *simCtx)
Initializes post-processing settings from a config file and command-line overrides.
Definition io.c:2173
PetscErrorCode PicurvAssertIntEqual(PetscInt expected, PetscInt actual, const char *context)
Shared test-support routine.
char output_prefix[256]
Definition variables.h:515
PetscInt timeStep
Definition variables.h:508
PetscBool outputParticles
Definition variables.h:509
PostProcessParams * pps
Definition variables.h:725
PetscInt startTime
Definition variables.h:506
char PostprocessingControlFile[PETSC_MAX_PATH_LEN]
Definition variables.h:724
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestTrimWhitespace()

static PetscErrorCode TestTrimWhitespace ( void  )
static

Test-local routine.

Definition at line 137 of file test_io.c.

138{
139 char value_a[] = " inlet_value ";
140 char value_b[] = " ";
141
142 PetscFunctionBeginUser;
143 TrimWhitespace(value_a);
144 PetscCall(PicurvAssertBool((PetscBool)(strcmp(value_a, "inlet_value") == 0),
145 "TrimWhitespace should remove leading and trailing whitespace"));
146
147 TrimWhitespace(value_b);
148 PetscCall(PicurvAssertBool((PetscBool)(strcmp(value_b, "") == 0),
149 "TrimWhitespace should reduce all-whitespace strings to empty"));
150 PetscFunctionReturn(0);
151}
void TrimWhitespace(char *str)
Helper function to trim leading/trailing whitespace from a string.
Definition io.c:38
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestBoundaryConditionStringParsers()

static PetscErrorCode TestBoundaryConditionStringParsers ( void  )
static

Test-local routine.

Definition at line 156 of file test_io.c.

157{
158 BCFace face = BC_FACE_NEG_X;
159 BCType type = WALL;
161
162 PetscFunctionBeginUser;
163 PetscCall(StringToBCFace("+Zeta", &face));
164 PetscCall(PicurvAssertIntEqual(BC_FACE_POS_Z, face, "StringToBCFace should parse +Zeta"));
165
166 PetscCall(StringToBCType("periodic", &type));
167 PetscCall(PicurvAssertIntEqual(PERIODIC, type, "StringToBCType should parse PERIODIC case-insensitively"));
168
169 PetscCall(StringToBCHandlerType("constant_flux", &handler));
171 "StringToBCHandlerType should parse constant_flux"));
172 PetscFunctionReturn(0);
173}
PetscErrorCode StringToBCHandlerType(const char *str, BCHandlerType *handler_out)
Converts a BC handler token (implementation strategy) to BCHandlerType.
Definition io.c:347
PetscErrorCode StringToBCFace(const char *str, BCFace *face_out)
Converts a face-token string (e.g., "-Xi", "+Eta") to the internal BCFace enum.
Definition io.c:317
PetscErrorCode StringToBCType(const char *str, BCType *type_out)
Converts a mathematical BC type string (e.g., "PERIODIC", "WALL") to BCType.
Definition io.c:332
BCType
Defines the general mathematical/physical Category of a boundary.
Definition variables.h:210
@ PERIODIC
Definition variables.h:219
@ WALL
Definition variables.h:213
BCHandlerType
Defines the specific computational "strategy" for a boundary handler.
Definition variables.h:230
@ BC_HANDLER_PERIODIC_DRIVEN_CONSTANT_FLUX
Definition variables.h:245
@ BC_HANDLER_WALL_NOSLIP
Definition variables.h:232
BCFace
Identifies the six logical faces of a structured computational block.
Definition variables.h:203
@ BC_FACE_NEG_X
Definition variables.h:204
@ BC_FACE_POS_Z
Definition variables.h:206
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestValidateBCHandlerForBCType()

static PetscErrorCode TestValidateBCHandlerForBCType ( void  )
static

Test-local routine.

Definition at line 178 of file test_io.c.

179{
180 PetscFunctionBeginUser;
182 "WALL + noslip should be a valid combination"));
184 "PERIODIC + geometric should be a valid combination"));
186 "INLET + noslip should be rejected"));
187 PetscFunctionReturn(0);
188}
PetscErrorCode ValidateBCHandlerForBCType(BCType type, BCHandlerType handler)
Validates that a selected handler is compatible with a mathematical BC type.
Definition io.c:364
@ INLET
Definition variables.h:217
@ BC_HANDLER_PERIODIC_GEOMETRIC
Definition variables.h:243
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestParseScalingInformation()

static PetscErrorCode TestParseScalingInformation ( void  )
static

Test-local routine.

Definition at line 193 of file test_io.c.

194{
195 SimCtx simCtx;
196
197 PetscFunctionBeginUser;
198 PetscCall(PetscMemzero(&simCtx, sizeof(simCtx)));
199
200 PetscCall(PetscOptionsClearValue(NULL, "-scaling_L_ref"));
201 PetscCall(PetscOptionsClearValue(NULL, "-scaling_U_ref"));
202 PetscCall(PetscOptionsClearValue(NULL, "-scaling_rho_ref"));
203
204 PetscCall(ParseScalingInformation(&simCtx));
205 PetscCall(PicurvAssertRealNear(1.0, simCtx.scaling.L_ref, 1.0e-12, "Default scaling_L_ref should be 1.0"));
206 PetscCall(PicurvAssertRealNear(1.0, simCtx.scaling.U_ref, 1.0e-12, "Default scaling_U_ref should be 1.0"));
207 PetscCall(PicurvAssertRealNear(1.0, simCtx.scaling.rho_ref, 1.0e-12, "Default scaling_rho_ref should be 1.0"));
208 PetscCall(PicurvAssertRealNear(1.0, simCtx.scaling.P_ref, 1.0e-12, "Default scaling_P_ref should be 1.0"));
209
210 PetscCall(PetscOptionsSetValue(NULL, "-scaling_L_ref", "2.5"));
211 PetscCall(PetscOptionsSetValue(NULL, "-scaling_U_ref", "4.0"));
212 PetscCall(PetscOptionsSetValue(NULL, "-scaling_rho_ref", "1.2"));
213
214 PetscCall(ParseScalingInformation(&simCtx));
215 PetscCall(PicurvAssertRealNear(2.5, simCtx.scaling.L_ref, 1.0e-12, "scaling_L_ref should honor options"));
216 PetscCall(PicurvAssertRealNear(4.0, simCtx.scaling.U_ref, 1.0e-12, "scaling_U_ref should honor options"));
217 PetscCall(PicurvAssertRealNear(1.2, simCtx.scaling.rho_ref, 1.0e-12, "scaling_rho_ref should honor options"));
218 PetscCall(PicurvAssertRealNear(19.2, simCtx.scaling.P_ref, 1.0e-12, "scaling_P_ref should be rho_ref*U_ref^2"));
219
220 PetscCall(PetscOptionsClearValue(NULL, "-scaling_L_ref"));
221 PetscCall(PetscOptionsClearValue(NULL, "-scaling_U_ref"));
222 PetscCall(PetscOptionsClearValue(NULL, "-scaling_rho_ref"));
223 PetscFunctionReturn(0);
224}
PetscErrorCode ParseScalingInformation(SimCtx *simCtx)
Parses physical scaling parameters from command-line options.
Definition io.c:2321
PetscErrorCode PicurvAssertRealNear(PetscReal expected, PetscReal actual, PetscReal tol, const char *context)
Shared test-support routine.
PetscReal L_ref
Definition variables.h:573
ScalingCtx scaling
Definition variables.h:651
PetscReal P_ref
Definition variables.h:576
PetscReal rho_ref
Definition variables.h:575
PetscReal U_ref
Definition variables.h:574
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 229 of file test_io.c.

230{
231 PetscErrorCode ierr;
232 const PicurvTestCase cases[] = {
233 {"should-write-data-output", TestShouldWriteDataOutput},
234 {"verify-path-existence", TestVerifyPathExistence},
235 {"write-and-read-simulation-fields", TestWriteAndReadSimulationFields},
236 {"parse-post-processing-settings", TestParsePostProcessingSettings},
237 {"trim-whitespace", TestTrimWhitespace},
238 {"bc-string-parsers", TestBoundaryConditionStringParsers},
239 {"validate-bc-handler-for-type", TestValidateBCHandlerForBCType},
240 {"parse-scaling-information", TestParseScalingInformation},
241 };
242
243 ierr = PetscInitialize(&argc, &argv, NULL, "PICurv I/O tests");
244 if (ierr) {
245 return (int)ierr;
246 }
247
248 ierr = PicurvRunTests("unit-io", cases, sizeof(cases) / sizeof(cases[0]));
249 if (ierr) {
250 PetscFinalize();
251 return (int)ierr;
252 }
253
254 ierr = PetscFinalize();
255 return (int)ierr;
256}
static PetscErrorCode TestParseScalingInformation(void)
Test-local routine.
Definition test_io.c:193
static PetscErrorCode TestValidateBCHandlerForBCType(void)
Test-local routine.
Definition test_io.c:178
static PetscErrorCode TestParsePostProcessingSettings(void)
Test-local routine.
Definition test_io.c:98
static PetscErrorCode TestVerifyPathExistence(void)
Test-local routine.
Definition test_io.c:33
static PetscErrorCode TestTrimWhitespace(void)
Test-local routine.
Definition test_io.c:137
static PetscErrorCode TestShouldWriteDataOutput(void)
Test-local routine.
Definition test_io.c:16
static PetscErrorCode TestBoundaryConditionStringParsers(void)
Test-local routine.
Definition test_io.c:156
static PetscErrorCode TestWriteAndReadSimulationFields(void)
Test-local routine.
Definition test_io.c:60
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: