PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
Loading...
Searching...
No Matches
io.h
Go to the documentation of this file.
1#ifndef IO_H
2#define IO_H
3
4#include "variables.h" // Essential for SimCtx and UserCtx definitions
5#include "logging.h" // For logging macros.
6#include "Boundaries.h"
7/**
8 * @file io.h
9 * @brief Public interface for data input/output routines.
10 *
11 * This header declares functions responsible for parsing grid geometry
12 * information, either from command-line options for programmatically
13 * generated grids or by reading the header of a grid definition file.
14 */
15
16/**
17 * @brief Parses command-line options for a programmatically generated grid for a SINGLE block.
18 *
19 * This function reads all per-block array options related to grid geometry,
20 * such as dimensions (-im), domain bounds (-xMins), and stretching ratios (-rxs).
21 * It then populates the fields of the provided `UserCtx` struct using its
22 * internal block index `user->_this`.
23 *
24 * @param user Pointer to the `UserCtx` for a specific block. The function will
25 * populate the geometric fields (`IM`, `Min_X`, `rx`, etc.) within this struct.
26 * @return PetscErrorCode 0 on success, or a PETSc error code on failure.
27 */
28PetscErrorCode ReadGridGenerationInputs(UserCtx *user);
29
30/**
31 * @brief Parses grid resolution arrays (`-im`, `-jm`, `-km`) once and applies them to all finest-grid blocks.
32 *
33 * This helper centralizes one-time resolution ingestion for analytical grid setup.
34 * It fills `IM/JM/KM` in each element of the finest-level `UserCtx` array.
35 *
36 * @param finest_users Pointer to the finest-level `UserCtx` array (length `nblk`).
37 * @param nblk Number of blocks in the finest-level array.
38 * @return PetscErrorCode 0 on success, or a PETSc error code on failure.
39 */
40PetscErrorCode PopulateFinestUserGridResolutionFromOptions(UserCtx *finest_users, PetscInt nblk);
41
42/**
43 * @brief Sets grid dimensions from a file for a SINGLE block using a one-time read cache.
44 *
45 * This function uses a static-variable pattern to ensure the grid file header
46 * is read only once, collectively, by all processes on the first call.
47 * Subsequent calls simply retrieve the pre-loaded and broadcasted data for
48 * the specified block.
49 *
50 * @param user Pointer to the `UserCtx` for a specific block. This function will
51 * populate the `IM`, `JM`, and `KM` fields.
52 * @return PetscErrorCode 0 on success, or a PETSc error code on failure.
53 */
54PetscErrorCode ReadGridFile(UserCtx *user);
55
56/**
57 * @brief A parallel-safe helper to verify the existence of a generic file or directory path.
58 *
59 * This function centralizes the logic for checking arbitrary paths. Only Rank 0 performs the
60 * filesystem check, and the result is broadcast to all other processes. This ensures
61 * collective and synchronized decision-making across all ranks. It is intended for
62 * configuration files, source directories, etc., where the path is known completely.
63 *
64 * @param[in] path The full path to the file or directory to check.
65 * @param[in] is_dir PETSC_TRUE if checking for a directory, PETSC_FALSE for a file.
66 * @param[in] is_optional PETSC_TRUE if the path is optional (results in a warning),
67 * PETSC_FALSE if mandatory (results in an error).
68 * @param[in] description A user-friendly description of the path for logging (e.g., "Grid file").
69 * @param[out] exists The result of the check (identical on all ranks).
70 *
71 * @return PetscErrorCode
72 */
73PetscErrorCode VerifyPathExistence(const char *path, PetscBool is_dir, PetscBool is_optional, const char *description, PetscBool *exists);
74
75/**
76 * @brief Returns whether full field/restart output should be written for the
77 *
78 * completed timestep.
79 *
80 * @param simCtx Simulation context controlling the operation.
81 * @param completed_step Completed step index used by the decision helper.
82 * @return PetscBool indicating the result of `ShouldWriteDataOutput()`.
83 */
84PetscBool ShouldWriteDataOutput(const SimCtx *simCtx, PetscInt completed_step);
85
86/**
87 * @brief Reads binary field data for velocity, pressure, and other required vectors.
88 *
89 * Reads contravariant velocity (`Ucont`) from `vfield`, Cartesian velocity (`Ucat`) from `ufield`,
90 * pressure (`P`), node state (`Nvert_o`), and optionally statistical quantities, LES, and RANS fields
91 * from binary files. Logs missing files but continues execution.
92 *
93 * @param[in,out] user Pointer to the UserCtx structure containing the simulation context.
94 * @param[in] ti Timestep index used to construct restart file names.
95 *
96 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
97 */
98PetscErrorCode ReadSimulationFields(UserCtx *user, PetscInt ti);
99
100
101/**
102 * @brief Reads data for a specific field from a file into the provided vector.
103 *
104 * This function uses the field name to construct the file path and reads the data
105 * from the corresponding file into the provided PETSc vector.
106 *
107 * @param[in] user Pointer to the UserCtx structure containing simulation context.
108 * @param[in] field_name Name of the field (e.g., "ufield", "vfield", "pfield").
109 * @param[out] field_vec PETSc vector to store the field data.
110 * @param[in] ti Time index for constructing the file name.
111 * @param[in] ext File extension (e.g., "dat").
112 *
113 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
114 */
115PetscErrorCode ReadFieldData(UserCtx *user, const char *field_name, Vec field_vec, PetscInt ti, const char *ext);
116
117
118/**
119 * @brief Reads statistical fields used for time-averaged simulations.
120 *
121 * Reads statistical quantities such as velocity sums and pressure sums. Logs missing files and initializes
122 * fields to zero if files are unavailable.
123 *
124 * @param[in,out] user Pointer to the UserCtx structure containing the simulation context.
125 * @param[in] ti Timestep index used to construct statistics file names.
126 *
127 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
128 */
129PetscErrorCode ReadStatisticalFields(UserCtx *user,PetscInt ti);
130
131/**
132 * @brief Reads LES-related fields used in turbulence modeling.
133 *
134 * Reads the Smagorinsky constant (`Cs`) and transfers data to local vectors. Logs missing files and
135 * initializes fields to zero if files are unavailable.
136 *
137 * @param[in,out] user Pointer to the UserCtx structure containing the simulation context.
138 * @param[in] ti Time index for constructing the file name.
139 *
140 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
141 */
142PetscErrorCode ReadLESFields(UserCtx *user, PetscInt ti);
143
144/**
145 * @brief Reads RANS-related fields for turbulence modeling.
146 *
147 * Reads `K_Omega` fields (used in RANS modeling) and initializes them if files are unavailable.
148 *
149 * @param[in,out] user Pointer to the UserCtx structure containing the simulation context.
150 * @param[in] ti Time index for constructing the file name.
151 *
152 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
153 */
154PetscErrorCode ReadRANSFields(UserCtx *user, PetscInt ti);
155
156/**
157 * @brief Writes data from a specific PETSc vector to a file.
158 *
159 * This function uses the field name to construct the file path and writes the data
160 * from the provided PETSc vector to the corresponding file.
161 *
162 * @param[in] user Pointer to the UserCtx structure containing simulation context.
163 * @param[in] field_name Name of the field (e.g., "ufield", "vfield", "pfield").
164 * @param[in] field_vec PETSc vector containing the field data to write.
165 * @param[in] ti Time index for constructing the file name.
166 * @param[in] ext File extension (e.g., "dat").
167 *
168 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
169 */
170PetscErrorCode WriteFieldData(UserCtx *user, const char *field_name, Vec field_vec, PetscInt ti, const char *ext);
171
172
173/**
174 * @brief Writes simulation fields to files.
175 *
176 * This function writes contravariant velocity, Cartesian velocity, pressure, and node state
177 * fields to their respective binary files. It also conditionally writes LES, RANS, and
178 * statistical fields if they are enabled.
179 *
180 * @param[in] user Pointer to the UserCtx structure containing simulation context.
181 *
182 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
183 */
184PetscErrorCode WriteSimulationFields(UserCtx *user);
185
186
187/**
188 * @brief Writes statistical fields for averaging purposes.
189 *
190 * This function writes data for fields such as Ucat_sum, Ucat_cross_sum, Ucat_square_sum,
191 * and P_sum to their respective binary files.
192 *
193 * @param[in] user Pointer to the UserCtx structure containing simulation context.
194 *
195 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
196 */
197PetscErrorCode WriteStatisticalFields(UserCtx *user);
198
199/**
200 * @brief Writes LES-related fields.
201 *
202 * This function writes LES-related fields such as Cs (Smagorinsky constant)
203 * to their respective binary files.
204 *
205 * @param[in] user Pointer to the UserCtx structure containing simulation context.
206 *
207 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
208 */
209PetscErrorCode WriteLESFields(UserCtx *user);
210
211/**
212 * @brief Writes RANS-related fields.
213 *
214 * This function writes RANS-related fields such as K_Omega to their respective
215 * binary files.
216 *
217 * @param[in] user Pointer to the UserCtx structure containing simulation context.
218 *
219 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
220 */
221PetscErrorCode WriteRANSFields(UserCtx *user);
222
223/**
224 * @brief Writes data from a specific field in a PETSc Swarm to a file.
225 *
226 * This function retrieves the Swarm from the UserCtx (i.e., `user->swarm`) and
227 * creates a global PETSc vector from the specified Swarm field. It then calls
228 * the existing WriteFieldData() function to handle the actual I/O operation.
229 * After writing the data, the function destroys the temporary global vector
230 * to avoid memory leaks.
231 *
232 * @param[in] user Pointer to the UserCtx structure containing simulation context
233 * and the PetscSwarm (as `user->swarm`).
234 * @param[in] field_name Name of the Swarm field to be written (e.g., "my_field").
235 * @param[in] ti Time index used to construct the output file name.
236 * @param[in] ext File extension (e.g., "dat", "bin").
237 *
238 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
239 *
240 * @note Compatible with PETSc 3.14.4.
241 */
242PetscErrorCode WriteSwarmField(UserCtx *user, const char *field_name, PetscInt ti, const char *ext);
243
244/**
245 * @brief Writes integer data from a specific PETSc Swarm field to a file.
246 *
247 * This function is designed for swarm fields that store integer data (e.g.,
248 * DMSwarm_CellID), which cannot be converted to a standard PETSc Vec of
249 * PetscScalars. It accesses the raw data pointer for the field on each rank
250 * using DMSwarmGetField(), writes the local data to a rank-specific binary file,
251 * and then restores the field access.
252 *
253 * @param[in] user Pointer to the UserCtx structure containing the PetscSwarm.
254 * @param[in] field_name Name of the integer Swarm field to be written.
255 * @param[in] ti Time index used to construct the output file name.
256 * @param[in] ext File extension (e.g., "dat", "bin").
257 *
258 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
259 */
260PetscErrorCode WriteSwarmIntField(UserCtx *user, const char *field_name, PetscInt ti, const char *ext);
261
262/**
263 * @brief Writes a predefined set of PETSc Swarm fields to files.
264 *
265 * This function iterates through a hardcoded list of common swarm fields
266 * (position, velocity, etc.) and calls the WriteSwarmField() helper function
267 * for each one. This provides a straightforward way to output essential particle
268 * data at a given simulation step.
269 *
270 * This function will only execute if particles are enabled in the simulation
271 * (i.e., `user->simCtx->np > 0` and `user->swarm` is not NULL).
272 *
273 * @param[in] user Pointer to the UserCtx structure containing the simulation context
274 * and the PetscSwarm.
275 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
276 */
277PetscErrorCode WriteAllSwarmFields(UserCtx *user);
278
279/**
280 * @brief Reads a simple ASCII data file containing one numeric value per line.
281 *
282 * This helper performs rank-0 file I/O, broadcasts the parsed result to the rest
283 * of the communicator, and returns a replicated array on every rank.
284 *
285 * @param filename Path to the input data file.
286 * @param data_out Output pointer to the allocated scalar array.
287 * @param Nout Output pointer storing the number of values read.
288 * @param comm MPI communicator used for the coordinated read/broadcast sequence.
289 * @return Integer value produced by `ReadDataFileToArray()`.
290 */
291PetscInt ReadDataFileToArray(const char *filename,
292 double **data_out,
293 PetscInt *Nout,
294 MPI_Comm comm);
295
296/**
297 * @brief Creates a VTK file from prepared metadata and field payloads.
298 *
299 * This helper dispatches to the structured-grid or polydata writer based on the
300 * metadata contents and emits the assembled VTK file on the requested communicator.
301 *
302 * @param filename Path to the output VTK file.
303 * @param meta VTK metadata describing the output geometry and field payloads.
304 * @param comm MPI communicator used by the write operation.
305 * @return Integer value produced by `CreateVTKFileFromMetadata()`.
306 */
307PetscInt CreateVTKFileFromMetadata(const char *filename,
308 const VTKMetaData *meta,
309 MPI_Comm comm);
310
311/**
312 * @brief Gathers the contents of a distributed PETSc Vec into a single array on rank 0.
313 *
314 * @param[in] inVec The input (possibly distributed) Vec.
315 * @param[out] N The global size of the vector.
316 * @param[out] arrayOut On rank 0, points to the newly allocated array holding all data.
317 * On other ranks, it is set to NULL.
318 *
319 * @return PetscErrorCode Return 0 on success, nonzero on failure.
320 */
321PetscErrorCode VecToArrayOnRank0(Vec inVec, PetscInt *N, double **arrayOut);
322
323/**
324 * @brief Gathers any DMSwarm field from all ranks to a single, contiguous array on rank 0.
325 *
326 * This is a generic, type-aware version of SwarmFieldToArrayOnRank0.
327 * It is a COLLECTIVE operation.
328 *
329 * @param[in] swarm The DMSwarm to gather from.
330 * @param[in] field_name The name of the field to gather.
331 * @param[out] n_total_particles (Output on rank 0) Total number of particles in the global swarm.
332 * @param[out] n_components (Output on rank 0) Number of components for the field.
333 * @param[out] gathered_array (Output on rank 0) A newly allocated array containing the full, gathered data.
334 * The caller is responsible for freeing this memory and for casting it to the correct type.
335 * @return PetscErrorCode
336 */
337PetscErrorCode SwarmFieldToArrayOnRank0(DM swarm, const char *field_name, PetscInt *n_total_particles, PetscInt *n_components, void **gathered_array);
338
339/**
340 * @brief Reads data from a file into a specified field of a PETSc DMSwarm.
341 *
342 * This function is the counterpart to WriteSwarmField(). It creates a global PETSc vector
343 * that references the specified DMSwarm field, uses ReadFieldData() to read the data from
344 * a file, and then destroys the global vector reference.
345 *
346 * @param[in] user Pointer to the UserCtx structure (containing `user->swarm`).
347 * @param[in] field_name Name of the DMSwarm field to read into (must be previously declared/allocated).
348 * @param[in] ti Time index used to construct the input file name.
349 * @param[in] ext File extension (e.g., "dat" or "bin").
350 *
351 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
352 *
353 * @note Compatible with PETSc 3.14.x.
354 */
355PetscErrorCode ReadSwarmField(UserCtx *user, const char *field_name, PetscInt ti, const char *ext);
356
357/**
358 * @brief Reads integer swarm data by using ReadFieldData and casting the result.
359 *
360 * This function is the counterpart to WriteSwarmIntField. It reads a file
361 * containing floating-point data (that was originally integer) into a temporary
362 * Vec and then casts it back to the integer swarm field. It works by:
363 * 1. Creating a temporary parallel Vec.
364 * 2. Calling the standard ReadFieldData() to populate this Vec.
365 * 3. Accessing the local data of both the Vec and the swarm field.
366 * 4. Populating the swarm's integer field by casting each PetscScalar back to a PetscInt.
367 * 5. Destroying the temporary Vec.
368 *
369 * @param[in] user Pointer to the UserCtx structure.
370 * @param[in] field_name Name of the integer Swarm field to be read.
371 * @param[in] ti Time index for the input file.
372 * @param[in] ext File extension.
373 *
374 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
375 */
376PetscErrorCode ReadSwarmIntField(UserCtx *user, const char *field_name, PetscInt ti, const char *ext);
377
378/**
379 * @brief Reads multiple fields (positions, velocity, CellID, and weight) into a DMSwarm.
380 *
381 * This function is analogous to ReadSimulationFields() but targets a DMSwarm.
382 * Each Swarm field is read from a separate file using ReadSwarmField().
383 *
384 * @param[in,out] user Pointer to the UserCtx structure containing the DMSwarm (user->swarm).
385 * @param[in] ti Time index for constructing the file name.
386 *
387 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
388 */
389PetscErrorCode ReadAllSwarmFields(UserCtx *user, PetscInt ti);
390
391/**
392 * @brief Reads coordinate data (for particles) from file into a PETSc Vec, then gathers it to rank 0.
393 *
394 * This function uses \c ReadFieldData to fill a PETSc Vec with coordinate data,
395 * then leverages \c VecToArrayOnRank0 to gather that data into a contiguous array
396 * (valid on rank 0 only).
397 *
398 * @param[in] timeIndex The time index used to construct file names.
399 * @param[in] user Pointer to the user context.
400 * @param[out] coordsArray On rank 0, will point to a newly allocated array holding the coordinates.
401 * @param[out] Ncoords On rank 0, the length of \p coordsArray. On other ranks, 0.
402 *
403 * @return PetscErrorCode Returns 0 on success, or non-zero on failures.
404 */
405PetscErrorCode ReadPositionsFromFile(PetscInt timeIndex,
406 UserCtx *user,
407 double **coordsArray,
408 PetscInt *Ncoords);
409
410
411/**
412 * @brief Reads a named field from file into a PETSc Vec, then gathers it to rank 0.
413 *
414 * This function wraps \c ReadFieldData and \c VecToArrayOnRank0 into a single step.
415 * The gathered data is stored in \p scalarArray on rank 0, with its length in \p Nscalars.
416 *
417 * @param[in] timeIndex The time index used to construct file names.
418 * @param[in] fieldName Name of the field to be read (e.g., "velocity").
419 * @param[in] user Pointer to the user context.
420 * @param[out] scalarArray On rank 0, a newly allocated array holding the field data.
421 * @param[out] Nscalars On rank 0, length of \p scalarArray. On other ranks, 0.
422 *
423 * @return PetscErrorCode Returns 0 on success, or non-zero on failures.
424 */
425PetscErrorCode ReadFieldDataToRank0(PetscInt timeIndex,
426 const char *fieldName,
427 UserCtx *user,
428 double **scalarArray,
429 PetscInt *Nscalars);
430
431/**
432 * @brief Displays a structured banner summarizing the simulation configuration.
433 *
434 * This function prints key simulation parameters to standard output.
435 * It is intended to be called ONLY by MPI rank 0.
436 * It retrieves global domain bounds and block metadata from `simCtx`.
437 *
438 * @param[in] simCtx Pointer to the master simulation context.
439 *
440 * @return PetscErrorCode Returns `0` on success.
441 */
442PetscErrorCode DisplayBanner(SimCtx *simCtx);
443
444
445// --- Conversion and Validation Utilities ---
446// These are now public and can be used by other parts of the application.
447
448/**
449 * @brief Converts a face-token string (e.g., "-Xi", "+Eta") to the internal `BCFace` enum.
450 *
451 * @param[in] str Input token from configuration.
452 * @param[out] face_out Parsed enum value on success.
453 * @return PetscErrorCode 0 on success, non-zero for invalid tokens or null pointers.
454 */
455PetscErrorCode StringToBCFace(const char* str, BCFace* face_out);
456
457/**
458 * @brief Converts a mathematical BC type string (e.g., "PERIODIC", "WALL") to `BCType`.
459 *
460 * @param[in] str Input token from configuration.
461 * @param[out] type_out Parsed enum value on success.
462 * @return PetscErrorCode 0 on success, non-zero for invalid tokens or null pointers.
463 */
464PetscErrorCode StringToBCType(const char* str, BCType* type_out);
465
466/**
467 * @brief Converts a BC handler token (implementation strategy) to `BCHandlerType`.
468 *
469 * @param[in] str Input handler token from configuration.
470 * @param[out] handler_out Parsed enum value on success.
471 * @return PetscErrorCode 0 on success, non-zero for invalid tokens or null pointers.
472 */
473PetscErrorCode StringToBCHandlerType(const char* str, BCHandlerType* handler_out);
474
475/**
476 * @brief Validates that a selected handler is compatible with a mathematical BC type.
477 *
478 * @param[in] type Mathematical BC type (e.g., WALL, PERIODIC).
479 * @param[in] handler Selected handler implementation enum.
480 * @return PetscErrorCode 0 if compatible, non-zero if the combination is invalid.
481 */
482PetscErrorCode ValidateBCHandlerForBCType(BCType type, BCHandlerType handler);
483
484// --- Memory Management ---
485
486/**
487 * @brief Frees an entire linked list of boundary-condition parameters.
488 *
489 * @param[in,out] head Head pointer of the `BC_Param` list to destroy.
490 */
491void FreeBC_ParamList(BC_Param *head);
492
493/**
494 * @brief Searches a BC_Param linked list for a key and returns its value as a double.
495 * @param params The head of the BC_Param linked list.
496 * @param key The key to search for (case-insensitive).
497 * @param[out] value_out The found value, converted to a PetscReal.
498 * @param[out] found Set to PETSC_TRUE if the key was found, PETSC_FALSE otherwise.
499 * @return 0 on success.
500 */
501PetscErrorCode GetBCParamReal(BC_Param *params, const char *key, PetscReal *value_out, PetscBool *found);
502
503/**
504 * @brief Searches a BC_Param linked list for a key and returns its value as a bool.
505 * @param params The head of the BC_Param linked list.
506 * @param key The key to search for (case-insensitive).
507 * @param[out] value_out The found value, converted to a PetscBool.
508 * @param[out] found Set to PETSC_TRUE if the key was found, PETSC_FALSE otherwise.
509 * @return 0 on success.
510 */
511PetscErrorCode GetBCParamBool(BC_Param *params, const char *key, PetscBool *value_out, PetscBool *found);
512
513/**
514 * @brief Parses the boundary conditions file to configure the type, handler, and
515 * any associated parameters for all 6 global faces of the domain.
516 *
517 * This function performs the following steps:
518 * 1. On MPI rank 0, it reads the specified configuration file line-by-line.
519 * 2. It parses each line for `<Face> <Type> <Handler> [param=value]...` format.
520 * 3. It validates the parsed strings and stores the configuration, including a
521 * linked list of parameters, in a temporary array.
522 * 4. It then serializes this configuration and broadcasts it to all other MPI ranks.
523 * 5. All ranks (including rank 0) then deserialize the broadcasted data to populate
524 * their local `user->boundary_faces` array identically.
525 * 6. It also sets the particle inlet lookup fields in `UserCtx`.
526 *
527 * @param[in,out] user The main UserCtx struct where the final configuration
528 * for all ranks will be stored.
529 * @param[in] bcs_input_filename The path to the boundary conditions configuration file.
530 * @return PetscErrorCode 0 on success, error code on failure.
531 */
532PetscErrorCode ParseAllBoundaryConditions(UserCtx *user, const char *bcs_input_filename);
533
534/**
535 * @brief Scans all block-specific boundary condition files to determine a globally
536 * consistent periodicity for each dimension, reusing the core type parser.
537 *
538 * This is a lightweight pre-parser intended to be called before DMDA creation.
539 * It ensures that the periodicity setting is consistent across all blocks, which is a
540 * physical requirement for the domain.
541 *
542 * 1. It collectively verifies that the mandatory BCS file for each block exists.
543 * 2. On MPI rank 0, it then iterates through the files.
544 * 3. For each line, it attempts to convert the type string to a BCType enum using the
545 * standard `StringToBCType` helper.
546 * 4. If the conversion is successful AND the type is PERIODIC, it flags the corresponding face.
547 * 5. If the conversion fails (e.g., for "WALL", "INLET", etc.), the error is cleared
548 * and the line is simply ignored, as it's not relevant to periodicity.
549 * 6. It validates consistency (e.g., -Xi and +Xi match) and ensures all block files
550 * specify the same global periodicity.
551 * 7. It broadcasts the final three flags (as integers 0 or 1) to all MPI ranks.
552 * 8. All ranks update the i_periodic, j_periodic, and k_periodic fields in their SimCtx.
553 *
554 * @param[in,out] simCtx The master SimCtx struct, containing the bcs_files list and
555 * where the final periodicity flags will be stored.
556 * @return PetscErrorCode 0 on success, error code on failure.
557 */
558PetscErrorCode DeterminePeriodicity(SimCtx *simCtx);
559
560/**
561 * @brief Helper function to trim leading/trailing whitespace from a string.
562 * @param str The string to trim in-place.
563 */
564void TrimWhitespace(char *str);
565
566/**
567 * @brief Initializes post-processing settings from a config file and command-line overrides.
568 *
569 * This function establishes the configuration for a post-processing run by:
570 * 1. Setting hardcoded default values in the PostProcessParams struct.
571 * 2. Reading a configuration file to override the defaults.
572 * 3. Parsing command-line options (-startTime, -endTime, etc.) which can override
573 * both the defaults and the file settings.
574 *
575 * @param simCtx The pointer to the simulation context that contains the postprocessing file and struct.
576 * @return PetscErrorCode
577 */
578PetscErrorCode ParsePostProcessingSettings(SimCtx *simCtx);
579
580/**
581 * @brief Parses physical scaling parameters from command-line options.
582 *
583 * This function reads the reference length, velocity, and density from the
584 * PETSc options database (provided via -scaling_L_ref, etc.). It populates
585 * the simCtx->scaling struct and calculates the derived reference pressure.
586 * It sets default values of 1.0 for a fully non-dimensional case if the
587 * options are not provided.
588 *
589 * @param[in,out] simCtx The simulation context whose 'scaling' member will be populated.
590 * @return PetscErrorCode
591 */
592PetscErrorCode ParseScalingInformation(SimCtx *simCtx);
593
594#endif // IO_H
PetscErrorCode ReadStatisticalFields(UserCtx *user, PetscInt ti)
Reads statistical fields used for time-averaged simulations.
Definition io.c:1205
PetscErrorCode ParsePostProcessingSettings(SimCtx *simCtx)
Initializes post-processing settings from a config file and command-line overrides.
Definition io.c:2209
PetscErrorCode WriteAllSwarmFields(UserCtx *user)
Writes a predefined set of PETSc Swarm fields to files.
Definition io.c:1815
PetscErrorCode SwarmFieldToArrayOnRank0(DM swarm, const char *field_name, PetscInt *n_total_particles, PetscInt *n_components, void **gathered_array)
Gathers any DMSwarm field from all ranks to a single, contiguous array on rank 0.
Definition io.c:1953
PetscErrorCode ParseScalingInformation(SimCtx *simCtx)
Parses physical scaling parameters from command-line options.
Definition io.c:2357
PetscInt ReadDataFileToArray(const char *filename, double **data_out, PetscInt *Nout, MPI_Comm comm)
Reads a simple ASCII data file containing one numeric value per line.
Definition io.c:2401
PetscErrorCode ReadGridFile(UserCtx *user)
Sets grid dimensions from a file for a SINGLE block using a one-time read cache.
Definition io.c:219
PetscErrorCode StringToBCHandlerType(const char *str, BCHandlerType *handler_out)
Converts a BC handler token (implementation strategy) to BCHandlerType.
Definition io.c:347
PetscErrorCode ReadRANSFields(UserCtx *user, PetscInt ti)
Reads RANS-related fields for turbulence modeling.
Definition io.c:1257
PetscErrorCode GetBCParamReal(BC_Param *params, const char *key, PetscReal *value_out, PetscBool *found)
Searches a BC_Param linked list for a key and returns its value as a double.
Definition io.c:387
PetscErrorCode ReadSwarmIntField(UserCtx *user, const char *field_name, PetscInt ti, const char *ext)
Reads integer swarm data by using ReadFieldData and casting the result.
Definition io.c:1321
PetscErrorCode WriteSwarmField(UserCtx *user, const char *field_name, PetscInt ti, const char *ext)
Writes data from a specific field in a PETSc Swarm to a file.
Definition io.c:1700
PetscErrorCode WriteStatisticalFields(UserCtx *user)
Writes statistical fields for averaging purposes.
Definition io.c:1627
PetscErrorCode ReadSimulationFields(UserCtx *user, PetscInt ti)
Reads binary field data for velocity, pressure, and other required vectors.
Definition io.c:1126
PetscBool ShouldWriteDataOutput(const SimCtx *simCtx, PetscInt completed_step)
Returns whether full field/restart output should be written for the.
Definition io.c:70
PetscErrorCode WriteFieldData(UserCtx *user, const char *field_name, Vec field_vec, PetscInt ti, const char *ext)
Writes data from a specific PETSc vector to a file.
Definition io.c:1448
PetscErrorCode ReadLESFields(UserCtx *user, PetscInt ti)
Reads LES-related fields used in turbulence modeling.
Definition io.c:1229
PetscErrorCode ReadFieldData(UserCtx *user, const char *field_name, Vec field_vec, PetscInt ti, const char *ext)
Reads data for a specific field from a file into the provided vector.
Definition io.c:898
PetscErrorCode ParseAllBoundaryConditions(UserCtx *user, const char *bcs_input_filename)
Parses the boundary conditions file to configure the type, handler, and any associated parameters for...
Definition io.c:447
PetscErrorCode ValidateBCHandlerForBCType(BCType type, BCHandlerType handler)
Validates that a selected handler is compatible with a mathematical BC type.
Definition io.c:364
void TrimWhitespace(char *str)
Helper function to trim leading/trailing whitespace from a string.
Definition io.c:38
PetscErrorCode WriteSimulationFields(UserCtx *user)
Writes simulation fields to files.
Definition io.c:1568
PetscErrorCode ReadAllSwarmFields(UserCtx *user, PetscInt ti)
Reads multiple fields (positions, velocity, CellID, and weight) into a DMSwarm.
Definition io.c:1383
PetscErrorCode ReadGridGenerationInputs(UserCtx *user)
Parses command-line options for a programmatically generated grid for a SINGLE block.
Definition io.c:85
PetscErrorCode ReadSwarmField(UserCtx *user, const char *field_name, PetscInt ti, const char *ext)
Reads data from a file into a specified field of a PETSc DMSwarm.
Definition io.c:1291
PetscErrorCode WriteRANSFields(UserCtx *user)
Writes RANS-related fields.
Definition io.c:1679
PetscErrorCode PopulateFinestUserGridResolutionFromOptions(UserCtx *finest_users, PetscInt nblk)
Parses grid resolution arrays (-im, -jm, -km) once and applies them to all finest-grid blocks.
Definition io.c:170
PetscErrorCode DeterminePeriodicity(SimCtx *simCtx)
Scans all block-specific boundary condition files to determine a globally consistent periodicity for ...
Definition io.c:634
PetscErrorCode ReadFieldDataToRank0(PetscInt timeIndex, const char *fieldName, UserCtx *user, double **scalarArray, PetscInt *Nscalars)
Reads a named field from file into a PETSc Vec, then gathers it to rank 0.
Definition io.c:2571
PetscErrorCode VecToArrayOnRank0(Vec inVec, PetscInt *N, double **arrayOut)
Gathers the contents of a distributed PETSc Vec into a single array on rank 0.
Definition io.c:1868
PetscErrorCode WriteSwarmIntField(UserCtx *user, const char *field_name, PetscInt ti, const char *ext)
Writes integer data from a specific PETSc Swarm field to a file.
Definition io.c:1755
PetscErrorCode WriteLESFields(UserCtx *user)
Writes LES-related fields.
Definition io.c:1649
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 ReadPositionsFromFile(PetscInt timeIndex, UserCtx *user, double **coordsArray, PetscInt *Ncoords)
Reads coordinate data (for particles) from file into a PETSc Vec, then gathers it to rank 0.
Definition io.c:2533
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 GetBCParamBool(BC_Param *params, const char *key, PetscBool *value_out, PetscBool *found)
Searches a BC_Param linked list for a key and returns its value as a bool.
Definition io.c:408
void FreeBC_ParamList(BC_Param *head)
Frees an entire linked list of boundary-condition parameters.
Definition io.c:302
PetscInt CreateVTKFileFromMetadata(const char *filename, const VTKMetaData *meta, MPI_Comm comm)
Creates a VTK file from prepared metadata and field payloads.
Definition vtk_io.c:153
PetscErrorCode StringToBCType(const char *str, BCType *type_out)
Converts a mathematical BC type string (e.g., "PERIODIC", "WALL") to BCType.
Definition io.c:332
PetscErrorCode DisplayBanner(SimCtx *simCtx)
Displays a structured banner summarizing the simulation configuration.
Definition io.c:2031
Logging utilities and macros for PETSc-based applications.
Main header file for a complex fluid dynamics solver.
BCType
Defines the general mathematical/physical Category of a boundary.
Definition variables.h:251
BCHandlerType
Defines the specific computational "strategy" for a boundary handler.
Definition variables.h:271
BCFace
Identifies the six logical faces of a structured computational block.
Definition variables.h:244
A node in a linked list for storing key-value parameters from the bcs.dat file.
Definition variables.h:304
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