PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
Loading...
Searching...
No Matches
vtk_io.h
Go to the documentation of this file.
1#ifndef VTK_IO_H
2#define VTK_IO_H
3
4#include "variables.h"
5#include "logging.h"
6#include "io.h"
7
8// --- Public Function Prototypes ---
9
10/**
11 * @brief Creates and writes a VTK file (either .vts or .vtp) from a populated metadata struct.
12 * @param[in] filename The output file name.
13 * @param[in] meta Pointer to a VTKMetaData structure containing all necessary fields.
14 * @param[in] comm The MPI communicator.
15 * @return 0 on success, non-zero on failure.
16 */
17PetscInt CreateVTKFileFromMetadata(const char *filename, const VTKMetaData *meta, MPI_Comm comm);
18
19/**
20 * @brief Creates a C array of coordinates corresponding to a subsampled (legacy-style) grid.
21 *
22 * This function gathers the full, distributed grid coordinates onto rank 0. On rank 0,
23 * it then allocates a new, smaller C array and copies only the coordinates for the
24 * nodes within the range [0..IM-2, 0..JM-2, 0..KM-2]. This produces a contiguous
25 * array of points for a grid of size (IM-1)x(JM-1)x(KM-1), matching the legacy output.
26 * The output arrays are only allocated and valid on rank 0.
27 *
28 * @param[in] user The UserCtx containing the grid information (DM, IM/JM/KM).
29 * @param[out] out_coords On rank 0, a pointer to the newly allocated C array for coordinate data. NULL on other ranks.
30 * @param[out] out_nx The number of points in the x-dimension for the new grid (IM-1).
31 * @param[out] out_ny The number of points in the y-dimension for the new grid (JM-1).
32 * @param[out] out_nz The number of points in the z-dimension for the new grid (KM-1).
33 * @param[out] out_npoints The total number of points in the new grid.
34 * @return PetscErrorCode
35 */
36PetscErrorCode PrepareOutputCoordinates(UserCtx* user, PetscScalar** out_coords, PetscInt* out_nx, PetscInt* out_ny, PetscInt* out_nz, PetscInt* out_npoints);
37
38/**
39 * @brief Creates a C array of field data corresponding to a subsampled (legacy-style) grid.
40 *
41 * This function gathers a full, distributed PETSc vector to rank 0. On rank 0,
42 * it then allocates a new, smaller C array and copies only the data components
43 * for nodes within the range [0..IM-2, 0..JM-2, 0..KM-2]. This produces a contiguous
44 * data array that perfectly matches the point ordering of the subsampled coordinates.
45 * The output array is only allocated and valid on rank 0.
46 *
47 * @param[in] user The UserCtx for grid information.
48 * @param[in] field_vec The full-sized PETSc vector containing the field data (e.g., user->P_nodal).
49 * @param[in] num_components The number of components for this field (1 for scalar, 3 for vector).
50 * @param[out] out_data On rank 0, a pointer to the newly allocated C array for the field data. NULL on other ranks.
51 * @return PetscErrorCode
52 */
53PetscErrorCode PrepareOutputEulerianFieldData(UserCtx* user, Vec field_vec, PetscInt num_components, PetscScalar** out_data);
54
55/**
56 * @brief Begins one structured VTK file: clears the metadata and builds its coordinates.
57 *
58 * Every Eulerian VTK file is assembled the same way — coordinates, then a set of
59 * point-data fields, then the write. This trio exists so the two producers share that
60 * shape instead of restating it, and so the buffers each producer allocates are
61 * released in one place rather than in neither.
62 *
63 * @param[in] user Block context supplying the grid.
64 * @param[out] meta Metadata to initialize.
65 * @return Zero on success, or a PETSc error.
66 */
67PetscErrorCode BeginStructuredVTKOutput(UserCtx *user, VTKMetaData *meta);
68
69/**
70 * @brief Adds one point-data field to a structured VTK file being assembled.
71 *
72 * Skips the field with a warning when the file is already at its field limit, so a
73 * long output list degrades rather than corrupting the metadata.
74 *
75 * @param[in] user Block context supplying the grid.
76 * @param[in,out] meta Metadata to append to.
77 * @param[in] name Field name as it will appear in the file.
78 * @param[in] field_vec Nodal vector holding the values.
79 * @param[in] components One for a scalar, three for a vector.
80 * @return Zero on success, or a PETSc error.
81 */
82PetscErrorCode AppendStructuredVTKField(UserCtx *user, VTKMetaData *meta, const char *name,
83 Vec field_vec, PetscInt components);
84
85/**
86 * @brief Writes an assembled structured VTK file and releases its buffers.
87 * @param[in,out] meta Metadata to write and then release.
88 * @param[in] filename Destination path.
89 * @return Zero on success, or a PETSc error.
90 */
91PetscErrorCode FinishStructuredVTKOutput(VTKMetaData *meta, const char *filename);
92
93/**
94 * @brief Gathers, subsamples, and prepares all particle data for VTK output.
95 *
96 * This function is a COLLECTIVE operation. All ranks must enter it.
97 * The heavy lifting (memory allocation, subsampling) is performed only on rank 0.
98 *
99 * 1. Gathers the full coordinate and field data from the distributed DMSwarm to rank 0.
100 * 2. On rank 0, subsamples the data based on pps->particle_output_freq.
101 * 3. On rank 0, populates the VTKMetaData struct with the new, smaller, subsampled data arrays.
102 *
103 * @param[in] user The UserCtx containing the DMSwarm.
104 * @param[in] pps The PostProcessParams struct for configuration.
105 * @param[out] meta A pointer to the VTKMetaData struct to be populated (on rank 0).
106 * @param[out] p_n_total On rank 0, the total number of particles before subsampling.
107 * @return PetscErrorCode
108 */
109PetscErrorCode PrepareOutputParticleData(UserCtx* user, PostProcessParams* pps, VTKMetaData* meta, PetscInt* p_n_total);
110
111#endif // VTK_IO_H
112
Public interface for data input/output routines.
Logging utilities and macros for PETSc-based applications.
Main header file for a complex fluid dynamics solver.
Holds all configuration parameters for a post-processing run.
Definition variables.h:596
User-defined context containing data specific to a single computational grid level.
Definition variables.h:906
PetscErrorCode PrepareOutputEulerianFieldData(UserCtx *user, Vec field_vec, PetscInt num_components, PetscScalar **out_data)
Creates a C array of field data corresponding to a subsampled (legacy-style) grid.
Definition vtk_io.c:377
PetscErrorCode FinishStructuredVTKOutput(VTKMetaData *meta, const char *filename)
Writes an assembled structured VTK file and releases its buffers.
Definition vtk_io.c:354
PetscErrorCode BeginStructuredVTKOutput(UserCtx *user, VTKMetaData *meta)
Begins one structured VTK file: clears the metadata and builds its coordinates.
Definition vtk_io.c:308
PetscErrorCode PrepareOutputCoordinates(UserCtx *user, PetscScalar **out_coords, PetscInt *out_nx, PetscInt *out_ny, PetscInt *out_nz, PetscInt *out_npoints)
Creates a C array of coordinates corresponding to a subsampled (legacy-style) grid.
Definition vtk_io.c:241
PetscErrorCode AppendStructuredVTKField(UserCtx *user, VTKMetaData *meta, const char *name, Vec field_vec, PetscInt components)
Adds one point-data field to a structured VTK file being assembled.
Definition vtk_io.c:326
PetscInt CreateVTKFileFromMetadata(const char *filename, const VTKMetaData *meta, MPI_Comm comm)
Creates and writes a VTK file (either .vts or .vtp) from a populated metadata struct.
Definition vtk_io.c:149
PetscErrorCode PrepareOutputParticleData(UserCtx *user, PostProcessParams *pps, VTKMetaData *meta, PetscInt *p_n_total)
Gathers, subsamples, and prepares all particle data for VTK output.
Definition vtk_io.c:486