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 Reads binary field data for velocity, pressure, and other required vectors.
77 *
78 * Reads contravariant velocity (`Ucont`) from `vfield`, Cartesian velocity (`Ucat`) from `ufield`,
79 * pressure (`P`), node state (`Nvert_o`), and optionally statistical quantities, LES, and RANS fields
80 * from binary files. Logs missing files but continues execution.
81 *
82 * @param[in,out] user Pointer to the UserCtx structure containing the simulation context.
83 * @param[in] The timestep at which the simulation field data needs to be read.
84 *
85 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
86 */
87PetscErrorCode ReadSimulationFields(UserCtx *user, PetscInt ti);
88
89
90/**
91 * @brief Reads data for a specific field from a file into the provided vector.
92 *
93 * This function uses the field name to construct the file path and reads the data
94 * from the corresponding file into the provided PETSc vector.
95 *
96 * @param[in] user Pointer to the UserCtx structure containing simulation context.
97 * @param[in] field_name Name of the field (e.g., "ufield", "vfield", "pfield").
98 * @param[out] field_vec PETSc vector to store the field data.
99 * @param[in] ti Time index for constructing the file name.
100 * @param[in] ext File extension (e.g., "dat").
101 *
102 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
103 */
104PetscErrorCode ReadFieldData(UserCtx *user, const char *field_name, Vec field_vec, PetscInt ti, const char *ext);
105
106
107/**
108 * @brief Reads statistical fields used for time-averaged simulations.
109 *
110 * Reads statistical quantities such as velocity sums and pressure sums. Logs missing files and initializes
111 * fields to zero if files are unavailable.
112 *
113 * @param[in,out] user Pointer to the UserCtx structure containing the simulation context.
114 * @param[in] The timestep at which the simulation field data needs to be read.
115 *
116 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
117 */
118PetscErrorCode ReadStatisticalFields(UserCtx *user,PetscInt ti);
119
120/**
121 * @brief Reads LES-related fields used in turbulence modeling.
122 *
123 * Reads the Smagorinsky constant (`Cs`) and transfers data to local vectors. Logs missing files and
124 * initializes fields to zero if files are unavailable.
125 *
126 * @param[in,out] user Pointer to the UserCtx structure containing the simulation context.
127 * @param[in] ti Time index for constructing the file name.
128 *
129 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
130 */
131PetscErrorCode ReadLESFields(UserCtx *user, PetscInt ti);
132
133/**
134 * @brief Reads RANS-related fields for turbulence modeling.
135 *
136 * Reads `K_Omega` fields (used in RANS modeling) and initializes them if files are unavailable.
137 *
138 * @param[in,out] user Pointer to the UserCtx structure containing the simulation context.
139 * @param[in] ti Time index for constructing the file name.
140 *
141 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
142 */
143PetscErrorCode ReadRANSFields(UserCtx *user, PetscInt ti);
144
145/**
146 * @brief Writes data from a specific PETSc vector to a file.
147 *
148 * This function uses the field name to construct the file path and writes the data
149 * from the provided PETSc vector to the corresponding file.
150 *
151 * @param[in] user Pointer to the UserCtx structure containing simulation context.
152 * @param[in] field_name Name of the field (e.g., "ufield", "vfield", "pfield").
153 * @param[in] field_vec PETSc vector containing the field data to write.
154 * @param[in] ti Time index for constructing the file name.
155 * @param[in] ext File extension (e.g., "dat").
156 *
157 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
158 */
159PetscErrorCode WriteFieldData(UserCtx *user, const char *field_name, Vec field_vec, PetscInt ti, const char *ext);
160
161
162/**
163 * @brief Writes simulation fields to files.
164 *
165 * This function writes contravariant velocity, Cartesian velocity, pressure, and node state
166 * fields to their respective binary files. It also conditionally writes LES, RANS, and
167 * statistical fields if they are enabled.
168 *
169 * @param[in] user Pointer to the UserCtx structure containing simulation context.
170 *
171 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
172 */
173PetscErrorCode WriteSimulationFields(UserCtx *user);
174
175
176/**
177 * @brief Writes statistical fields for averaging purposes.
178 *
179 * This function writes data for fields such as Ucat_sum, Ucat_cross_sum, Ucat_square_sum,
180 * and P_sum to their respective binary files.
181 *
182 * @param[in] user Pointer to the UserCtx structure containing simulation context.
183 *
184 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
185 */
186PetscErrorCode WriteStatisticalFields(UserCtx *user);
187
188/**
189 * @brief Writes LES-related fields.
190 *
191 * This function writes LES-related fields such as Cs (Smagorinsky constant)
192 * to their respective binary files.
193 *
194 * @param[in] user Pointer to the UserCtx structure containing simulation context.
195 *
196 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
197 */
198PetscErrorCode WriteLESFields(UserCtx *user);
199
200/**
201 * @brief Writes RANS-related fields.
202 *
203 * This function writes RANS-related fields such as K_Omega to their respective
204 * binary files.
205 *
206 * @param[in] user Pointer to the UserCtx structure containing simulation context.
207 *
208 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
209 */
210PetscErrorCode WriteRANSFields(UserCtx *user);
211
212/**
213 * @brief Writes data from a specific field in a PETSc Swarm to a file.
214 *
215 * This function retrieves the Swarm from the UserCtx (i.e., `user->swarm`) and
216 * creates a global PETSc vector from the specified Swarm field. It then calls
217 * the existing WriteFieldData() function to handle the actual I/O operation.
218 * After writing the data, the function destroys the temporary global vector
219 * to avoid memory leaks.
220 *
221 * @param[in] user Pointer to the UserCtx structure containing simulation context
222 * and the PetscSwarm (as `user->swarm`).
223 * @param[in] field_name Name of the Swarm field to be written (e.g., "my_field").
224 * @param[in] ti Time index used to construct the output file name.
225 * @param[in] ext File extension (e.g., "dat", "bin").
226 *
227 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
228 *
229 * @note Compatible with PETSc 3.14.4.
230 */
231PetscErrorCode WriteSwarmField(UserCtx *user, const char *field_name, PetscInt ti, const char *ext);
232
233/**
234 * @brief Writes integer data from a specific PETSc Swarm field to a file.
235 *
236 * This function is designed for swarm fields that store integer data (e.g.,
237 * DMSwarm_CellID), which cannot be converted to a standard PETSc Vec of
238 * PetscScalars. It accesses the raw data pointer for the field on each rank
239 * using DMSwarmGetField(), writes the local data to a rank-specific binary file,
240 * and then restores the field access.
241 *
242 * @param[in] user Pointer to the UserCtx structure containing the PetscSwarm.
243 * @param[in] field_name Name of the integer Swarm field to be written.
244 * @param[in] ti Time index used to construct the output file name.
245 * @param[in] ext File extension (e.g., "dat", "bin").
246 *
247 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
248 */
249PetscErrorCode WriteSwarmIntField(UserCtx *user, const char *field_name, PetscInt ti, const char *ext);
250
251/**
252 * @brief Writes a predefined set of PETSc Swarm fields to files.
253 *
254 * This function iterates through a hardcoded list of common swarm fields
255 * (position, velocity, etc.) and calls the WriteSwarmField() helper function
256 * for each one. This provides a straightforward way to output essential particle
257 * data at a given simulation step.
258 *
259 * This function will only execute if particles are enabled in the simulation
260 * (i.e., `user->simCtx->np > 0` and `user->swarm` is not NULL).
261 *
262 * @param[in] user Pointer to the UserCtx structure containing the simulation context
263 * and the PetscSwarm.
264 *
265 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
266 */
267PetscErrorCode WriteAllSwarmFields(UserCtx *user);
268
269/* --------------------------------------------------------------------
270 ReadDataFileToArray
271
272 Reads a simple ASCII .dat file containing one numeric value per line.
273
274 PARAMETERS:
275 filename - (input) path to the .dat file
276 data_out - (output) pointer to newly allocated array (rank 0
277 broadcasts to all ranks so each rank gets a copy)
278 Nout - (output) number of values read; same on all ranks
279 comm - (input) MPI communicator (we use rank 0 for I/O)
280
281 RETURN:
282 PetscErrorCode (0 = success, or error code on failure)
283
284 NOTES:
285 1) This function checks file existence on rank 0 and broadcasts
286 the result. If the file doesn't exist, it sets an error.
287 2) The array is allocated on all ranks, and the data is
288 broadcast so each rank has a local copy.
289 3) If your file format is more complex (e.g. multiple columns,
290 binary, etc.), you can adapt the reading logic here.
291-------------------------------------------------------------------- */
292PetscInt ReadDataFileToArray(const char *filename,
293 double **data_out,
294 PetscInt *Nout,
295 MPI_Comm comm);
296
297/* --------------------------------------------------------------------
298 CreateVTKFileFromMetadata
299
300 Creates a .vts (if fileType=VTK_STRUCTURED) or .vtp (if fileType=VTK_POLYDATA),
301 writing out the data contained in the VTKMetaData structure.
302
303 PARAMETERS:
304 filename - (input) path to the output file, e.g. "myfile.vts" or "myfile.vtp"
305 meta - (input) pointer to the VTKMetaData struct with all geometry/fields
306 comm - (input) MPI communicator (only rank 0 writes the file)
307
308 RETURN:
309 PetscErrorCode (0 = success, or error code on failure)
310
311 NOTES:
312 1) The function writes an XML header, the appended binary data blocks
313 (coordinates, scalar field, connectivity if needed), and the closing tags.
314 2) If meta->fileType = VTK_STRUCTURED, it calls .vts-specific logic.
315 If meta->fileType = VTK_POLYDATA, it calls .vtp logic.
316 3) Coordinates and field arrays must be properly sized. For instance,
317 if fileType=VTK_STRUCTURED, coords => length=3*nnodes, field=>nnodes.
318 If fileType=VTK_POLYDATA, coords => length=3*npoints, field=>npoints,
319 plus connectivity/offsets => length=npoints each.
320-------------------------------------------------------------------- */
321PetscInt CreateVTKFileFromMetadata(const char *filename,
322 const VTKMetaData *meta,
323 MPI_Comm comm);
324
325/**
326 * @brief Gathers the contents of a distributed PETSc Vec into a single array on rank 0.
327 *
328 * @param[in] inVec The input (possibly distributed) Vec.
329 * @param[out] N The global size of the vector.
330 * @param[out] arrayOut On rank 0, points to the newly allocated array holding all data.
331 * On other ranks, it is set to NULL.
332 *
333 * @return PetscErrorCode Return 0 on success, nonzero on failure.
334 */
335PetscErrorCode VecToArrayOnRank0(Vec inVec, PetscInt *N, double **arrayOut);
336
337/**
338 * @brief Gathers any DMSwarm field from all ranks to a single, contiguous array on rank 0.
339 *
340 * This is a generic, type-aware version of SwarmFieldToArrayOnRank0.
341 * It is a COLLECTIVE operation.
342 *
343 * @param[in] swarm The DMSwarm to gather from.
344 * @param[in] field_name The name of the field to gather.
345 * @param[out] n_total_particles (Output on rank 0) Total number of particles in the global swarm.
346 * @param[out] n_components (Output on rank 0) Number of components for the field.
347 * @param[out] gathered_array (Output on rank 0) A newly allocated array containing the full, gathered data.
348 * The caller is responsible for freeing this memory and for casting it to the correct type.
349 * @return PetscErrorCode
350 */
351PetscErrorCode SwarmFieldToArrayOnRank0(DM swarm, const char *field_name, PetscInt *n_total_particles, PetscInt *n_components, void **gathered_array);
352
353/**
354 * @brief Reads data from a file into a specified field of a PETSc DMSwarm.
355 *
356 * This function is the counterpart to WriteSwarmField(). It creates a global PETSc vector
357 * that references the specified DMSwarm field, uses ReadFieldData() to read the data from
358 * a file, and then destroys the global vector reference.
359 *
360 * @param[in] user Pointer to the UserCtx structure (containing `user->swarm`).
361 * @param[in] field_name Name of the DMSwarm field to read into (must be previously declared/allocated).
362 * @param[in] ti Time index used to construct the input file name.
363 * @param[in] ext File extension (e.g., "dat" or "bin").
364 *
365 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
366 *
367 * @note Compatible with PETSc 3.14.x.
368 */
369PetscErrorCode ReadSwarmField(UserCtx *user, const char *field_name, PetscInt ti, const char *ext);
370
371/**
372 * @brief Reads integer swarm data by using ReadFieldData and casting the result.
373 *
374 * This function is the counterpart to WriteSwarmIntField. It reads a file
375 * containing floating-point data (that was originally integer) into a temporary
376 * Vec and then casts it back to the integer swarm field. It works by:
377 * 1. Creating a temporary parallel Vec.
378 * 2. Calling the standard ReadFieldData() to populate this Vec.
379 * 3. Accessing the local data of both the Vec and the swarm field.
380 * 4. Populating the swarm's integer field by casting each PetscScalar back to a PetscInt.
381 * 5. Destroying the temporary Vec.
382 *
383 * @param[in] user Pointer to the UserCtx structure.
384 * @param[in] field_name Name of the integer Swarm field to be read.
385 * @param[in] ti Time index for the input file.
386 * @param[in] ext File extension.
387 *
388 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
389 */
390PetscErrorCode ReadSwarmIntField(UserCtx *user, const char *field_name, PetscInt ti, const char *ext);
391
392/**
393 * @brief Reads multiple fields (positions, velocity, CellID, and weight) into a DMSwarm.
394 *
395 * This function is analogous to ReadSimulationFields() but targets a DMSwarm.
396 * Each Swarm field is read from a separate file using ReadSwarmField().
397 *
398 * @param[in,out] user Pointer to the UserCtx structure containing the DMSwarm (user->swarm).
399 * @param[in] ti Time index for constructing the file name.
400 *
401 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
402 */
403PetscErrorCode ReadAllSwarmFields(UserCtx *user, PetscInt ti);
404
405/**
406 * @brief Reads coordinate data (for particles) from file into a PETSc Vec, then gathers it to rank 0.
407 *
408 * This function uses \c ReadFieldData to fill a PETSc Vec with coordinate data,
409 * then leverages \c VecToArrayOnRank0 to gather that data into a contiguous array
410 * (valid on rank 0 only).
411 *
412 * @param[in] timeIndex The time index used to construct file names.
413 * @param[in] user Pointer to the user context.
414 * @param[out] coordsArray On rank 0, will point to a newly allocated array holding the coordinates.
415 * @param[out] Ncoords On rank 0, the length of \p coordsArray. On other ranks, 0.
416 *
417 * @return PetscErrorCode Returns 0 on success, or non-zero on failures.
418 */
419PetscErrorCode ReadPositionsFromFile(PetscInt timeIndex,
420 UserCtx *user,
421 double **coordsArray,
422 PetscInt *Ncoords);
423
424
425/**
426 * @brief Reads a named field from file into a PETSc Vec, then gathers it to rank 0.
427 *
428 * This function wraps \c ReadFieldData and \c VecToArrayOnRank0 into a single step.
429 * The gathered data is stored in \p scalarArray on rank 0, with its length in \p Nscalars.
430 *
431 * @param[in] timeIndex The time index used to construct file names.
432 * @param[in] fieldName Name of the field to be read (e.g., "velocity").
433 * @param[in] user Pointer to the user context.
434 * @param[out] scalarArray On rank 0, a newly allocated array holding the field data.
435 * @param[out] Nscalars On rank 0, length of \p scalarArray. On other ranks, 0.
436 *
437 * @return PetscErrorCode Returns 0 on success, or non-zero on failures.
438 */
439PetscErrorCode ReadFieldDataToRank0(PetscInt timeIndex,
440 const char *fieldName,
441 UserCtx *user,
442 double **scalarArray,
443 PetscInt *Nscalars);
444
445/**
446 * @brief Displays a structured banner summarizing the simulation configuration.
447 *
448 * This function prints key simulation parameters to standard output.
449 * It is intended to be called ONLY by MPI rank 0.
450 * It retrieves global domain bounds from `user->global_domain_bbox` and boundary
451 * conditions for all faces from `user->face_bc_types`.
452 *
453 * @param[in] user Pointer to `UserCtx` structure.
454 * @param[in] bboxlist (If rank 0 needed to compute global_domain_bbox here, otherwise NULL)
455 *
456 * @return PetscErrorCode Returns `0` on success.
457 */
458PetscErrorCode DisplayBanner(SimCtx *simCtx);
459
460
461// --- Conversion and Validation Utilities ---
462// These are now public and can be used by other parts of the application.
463
464PetscErrorCode StringToBCFace(const char* str, BCFace* face_out);
465PetscErrorCode StringToBCType(const char* str, BCType* type_out);
466PetscErrorCode StringToBCHandlerType(const char* str, BCHandlerType* handler_out);
467PetscErrorCode ValidateBCHandlerForBCType(BCType type, BCHandlerType handler);
468
469// --- Memory Management ---
470
471void FreeBC_ParamList(BC_Param *head);
472
473/**
474 * @brief Searches a BC_Param linked list for a key and returns its value as a double.
475 * @param params The head of the BC_Param linked list.
476 * @param key The key to search for (case-insensitive).
477 * @param[out] value_out The found value, converted to a PetscReal.
478 * @param[out] found Set to PETSC_TRUE if the key was found, PETSC_FALSE otherwise.
479 * @return 0 on success.
480 */
481PetscErrorCode GetBCParamReal(BC_Param *params, const char *key, PetscReal *value_out, PetscBool *found);
482
483/**
484 * @brief Searches a BC_Param linked list for a key and returns its value as a bool.
485 * @param params The head of the BC_Param linked list.
486 * @param key The key to search for (case-insensitive).
487 * @param[out] value_out The found value, converted to a PetscBool.
488 * @param[out] found Set to PETSC_TRUE if the key was found, PETSC_FALSE otherwise.
489 * @return 0 on success.
490 */
491PetscErrorCode GetBCParamBool(BC_Param *params, const char *key, PetscBool *value_out, PetscBool *found);
492
493/**
494 * @brief Parses the boundary conditions file to configure the type, handler, and
495 * any associated parameters for all 6 global faces of the domain.
496 *
497 * This function performs the following steps:
498 * 1. On MPI rank 0, it reads the specified configuration file line-by-line.
499 * 2. It parses each line for `<Face> <Type> <Handler> [param=value]...` format.
500 * 3. It validates the parsed strings and stores the configuration, including a
501 * linked list of parameters, in a temporary array.
502 * 4. It then serializes this configuration and broadcasts it to all other MPI ranks.
503 * 5. All ranks (including rank 0) then deserialize the broadcasted data to populate
504 * their local `user->boundary_faces` array identically.
505 * 6. It also sets legacy fields in UserCtx for compatibility with other modules.
506 *
507 * @param[in,out] user The main UserCtx struct where the final configuration
508 * for all ranks will be stored.
509 * @param[in] bcs_input_filename The path to the boundary conditions configuration file.
510 * @return PetscErrorCode 0 on success, error code on failure.
511 */
512PetscErrorCode ParseAllBoundaryConditions(UserCtx *user, const char *bcs_input_filename);
513
514/**
515 * @brief Scans all block-specific boundary condition files to determine a globally
516 * consistent periodicity for each dimension, reusing the core type parser.
517 *
518 * This is a lightweight pre-parser intended to be called before DMDA creation.
519 * It ensures that the periodicity setting is consistent across all blocks, which is a
520 * physical requirement for the domain.
521 *
522 * 1. It collectively verifies that the mandatory BCS file for each block exists.
523 * 2. On MPI rank 0, it then iterates through the files.
524 * 3. For each line, it attempts to convert the type string to a BCType enum using the
525 * standard `StringToBCType` helper.
526 * 4. If the conversion is successful AND the type is PERIODIC, it flags the corresponding face.
527 * 5. If the conversion fails (e.g., for "WALL", "INLET", etc.), the error is cleared
528 * and the line is simply ignored, as it's not relevant to periodicity.
529 * 6. It validates consistency (e.g., -Xi and +Xi match) and ensures all block files
530 * specify the same global periodicity.
531 * 7. It broadcasts the final three flags (as integers 0 or 1) to all MPI ranks.
532 * 8. All ranks update the i_periodic, j_periodic, and k_periodic fields in their SimCtx.
533 *
534 * @param[in,out] simCtx The master SimCtx struct, containing the bcs_files list and
535 * where the final periodicity flags will be stored.
536 * @return PetscErrorCode 0 on success, error code on failure.
537 */
538PetscErrorCode DeterminePeriodicity(SimCtx *simCtx);
539
540/**
541 * @brief Helper function to trim leading/trailing whitespace from a string.
542 * @param str The string to trim in-place.
543 */
544void TrimWhitespace(char *str);
545
546/**
547 * @brief Initializes post-processing settings from a config file and command-line overrides.
548 *
549 * This function establishes the configuration for a post-processing run by:
550 * 1. Setting hardcoded default values in the PostProcessParams struct.
551 * 2. Reading a configuration file to override the defaults.
552 * 3. Parsing command-line options (-startTime, -endTime, etc.) which can override
553 * both the defaults and the file settings.
554 *
555 * @param simCtx The pointer to the simulation context that contains the postprocessing file and struct.
556 * @return PetscErrorCode
557 */
558PetscErrorCode ParsePostProcessingSettings(SimCtx *simCtx);
559
560/**
561 * @brief Parses physical scaling parameters from command-line options.
562 *
563 * This function reads the reference length, velocity, and density from the
564 * PETSc options database (provided via -scaling_L_ref, etc.). It populates
565 * the simCtx->scaling struct and calculates the derived reference pressure.
566 * It sets default values of 1.0 for a fully non-dimensional case if the
567 * options are not provided.
568 *
569 * @param[in,out] simCtx The simulation context whose 'scaling' member will be populated.
570 * @return PetscErrorCode
571 */
572PetscErrorCode ParseScalingInformation(SimCtx *simCtx);
573
574#endif // IO_H
PetscErrorCode ReadStatisticalFields(UserCtx *user, PetscInt ti)
Reads statistical fields used for time-averaged simulations.
Definition io.c:1360
PetscErrorCode ParsePostProcessingSettings(SimCtx *simCtx)
Initializes post-processing settings from a config file and command-line overrides.
Definition io.c:2523
PetscErrorCode WriteAllSwarmFields(UserCtx *user)
Writes a predefined set of PETSc Swarm fields to files.
Definition io.c:2126
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:2279
PetscErrorCode ParseScalingInformation(SimCtx *simCtx)
Parses physical scaling parameters from command-line options.
Definition io.c:2677
PetscInt ReadDataFileToArray(const char *filename, double **data_out, PetscInt *Nout, MPI_Comm comm)
Definition io.c:2727
PetscErrorCode ReadGridFile(UserCtx *user)
Sets grid dimensions from a file for a SINGLE block using a one-time read cache.
Definition io.c:222
PetscErrorCode StringToBCHandlerType(const char *str, BCHandlerType *handler_out)
Converts a string representation of a handler to a BCHandlerType enum.
Definition io.c:359
PetscErrorCode ReadRANSFields(UserCtx *user, PetscInt ti)
Reads RANS-related fields for turbulence modeling.
Definition io.c:1426
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:405
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:1517
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:1985
PetscErrorCode WriteStatisticalFields(UserCtx *user)
Writes statistical fields for averaging purposes.
Definition io.c:1889
PetscErrorCode ReadSimulationFields(UserCtx *user, PetscInt ti)
Reads binary field data for velocity, pressure, and other required vectors.
Definition io.c:1276
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:1701
PetscErrorCode ReadLESFields(UserCtx *user, PetscInt ti)
Reads LES-related fields used in turbulence modeling.
Definition io.c:1391
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:1040
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:484
PetscErrorCode ValidateBCHandlerForBCType(BCType type, BCHandlerType handler)
Validates that a specific handler is compatible with a general BC type.
Definition io.c:378
void TrimWhitespace(char *str)
Helper function to trim leading/trailing whitespace from a string.
Definition io.c:36
PetscErrorCode WriteSimulationFields(UserCtx *user)
Writes simulation fields to files.
Definition io.c:1826
PetscErrorCode ReadAllSwarmFields(UserCtx *user, PetscInt ti)
Reads multiple fields (positions, velocity, CellID, and weight) into a DMSwarm.
Definition io.c:1595
PetscErrorCode ReadGridGenerationInputs(UserCtx *user)
Parses command-line options for a programmatically generated grid for a SINGLE block.
Definition io.c:77
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:1472
PetscErrorCode WriteRANSFields(UserCtx *user)
Writes RANS-related fields.
Definition io.c:1951
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:161
PetscErrorCode DeterminePeriodicity(SimCtx *simCtx)
Scans all block-specific boundary condition files to determine a globally consistent periodicity for ...
Definition io.c:690
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:2917
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:2184
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:2054
PetscErrorCode WriteLESFields(UserCtx *user)
Writes LES-related fields.
Definition io.c:1917
PetscErrorCode StringToBCFace(const char *str, BCFace *face_out)
Converts a string representation of a face to a BCFace enum.
Definition io.c:325
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:2869
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:807
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:430
void FreeBC_ParamList(BC_Param *head)
Frees the memory allocated for a linked list of BC_Param structs.
Definition io.c:308
PetscInt CreateVTKFileFromMetadata(const char *filename, const VTKMetaData *meta, MPI_Comm comm)
Definition vtk_io.c:128
PetscErrorCode StringToBCType(const char *str, BCType *type_out)
Converts a string representation of a BC type to a BCType enum.
Definition io.c:342
PetscErrorCode DisplayBanner(SimCtx *simCtx)
Displays a structured banner summarizing the simulation configuration.
Definition io.c:2372
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:210
BCHandlerType
Defines the specific computational "strategy" for a boundary handler.
Definition variables.h:230
BCFace
Identifies the six logical faces of a structured computational block.
Definition variables.h:203
A node in a linked list for storing key-value parameters from the bcs.dat file.
Definition variables.h:263
The master context for the entire simulation.
Definition variables.h:589
User-defined context containing data specific to a single computational grid level.
Definition variables.h:733