PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
 
Loading...
Searching...
No Matches
postprocessing_kernels.h
Go to the documentation of this file.
1#ifndef POSTPROCESSING_KERNELS_H
2#define POSTPROCESSING_KERNELS_H
3
4#include "variables.h"
5#include "logging.h"
6#include "io.h" // For the UpdateLocalGhosts function prototype
8
9// Function prototypes for post-processing kernels
10
11/**
12 * @brief Interpolates a cell-centered field to nodal locations using local stencil averaging.
13 *
14 * The kernel reads the input field by name, computes nodal values, and stores the
15 * output in the named destination field. Both fields must already exist in the
16 * current `UserCtx`.
17 *
18 * @param[in,out] user Block-level context that owns the source and destination vectors.
19 * @param[in] in_field_name Name of the input field to sample.
20 * @param[in] out_field_name Name of the output field to populate.
21 * @return PetscErrorCode 0 on success.
22 */
23PetscErrorCode ComputeNodalAverage(UserCtx* user, const char* in_field_name, const char* out_field_name);
24
25/**
26 * @brief Computes the Q-criterion diagnostic from the local velocity-gradient tensor.
27 *
28 * This kernel evaluates rotational versus strain-rate dominance and writes the
29 * result into the configured Q-criterion output vector for visualization and flow
30 * feature identification.
31 *
32 * @param[in,out] user Block-level context containing velocity fields and target output storage.
33 * @return PetscErrorCode 0 on success.
34 */
35PetscErrorCode ComputeQCriterion(UserCtx* user);
36
37/**
38 * @brief Normalizes pressure using the value at the configured logical grid point.
39 *
40 * The owning rank reads the reference value, shares it collectively, and every
41 * rank subtracts it in-place from its distributed portion of the field.
42 *
43 * @param[in,out] user Block-level context containing pressure and reference configuration.
44 * @param[in] relative_field_name Name of the field to normalize.
45 * @return PetscErrorCode 0 on success.
46 */
47PetscErrorCode NormalizeRelativeField(UserCtx* user, const char* relative_field_name);
48
49// Add more post-processing kernel prototypes as needed
50// =========================================================================
51// Dimensionalization Kernels
52// =========================================================================
53/**
54 * @brief Scales a specified field from non-dimensional to dimensional units in-place.
55 *
56 * Resolves the name in the Eulerian field catalog, then the particle field catalog,
57 * and scales the field's storage (its global Vec, the DM coordinates, or its DMSwarm
58 * field) by the reference scale of the dimension recorded on its catalog entry.
59 *
60 * @param[in,out] user The UserCtx whose storage is modified.
61 * @param[in] field_name Case-insensitive catalogued name or alias, e.g. "Ucat",
62 * "Nu_t", "Coordinates", "ParticlePosition".
63 * @return Zero on success; `PETSC_ERR_ARG_UNKNOWN_TYPE` for a name in neither catalog,
64 * and `PETSC_ERR_ARG_WRONGSTATE` for a field without a fixed dimension or without
65 * storage in this run.
66 */
67PetscErrorCode DimensionalizeField(UserCtx *user, const char *field_name);
68
69// ===========================================================================
70// Particle Post-Processing Kernels
71// ===========================================================================
72
73
74/**
75 * @brief Computes the specific kinetic energy (KE per unit mass) for each particle.
76 *
77 * This kernel calculates SKE = 0.5 * |velocity|^2. It requires that the
78 * velocity field exists and will populate the specific kinetic energy field.
79 * The output field must be registered before this kernel is called.
80 *
81 * @param user The UserCtx containing the DMSwarm.
82 * @param velocity_field The name of the input vector field for particle velocity.
83 * @param ske_field The name of the output scalar field to store specific KE.
84 * @return PetscErrorCode
85 */
86PetscErrorCode ComputeSpecificKE(UserCtx* user, const char* velocity_field, const char* ske_field);
87
88/**
89 * @brief Computes the displacement magnitude |r_i - r_0| for each particle (per-particle VTK kernel).
90 *
91 * Reference point r_0 = (simCtx->psrc_x, psrc_y, psrc_z). Writes the scalar displacement to
92 * post_swarm[disp_field]. This is a visualisation kernel only — use ComputeParticleMSD from
93 * particle_statistics.h for quantitative global statistics.
94 *
95 * @param user The UserCtx containing the DMSwarms.
96 * @param disp_field Name of the output scalar field in post_swarm.
97 * @return PetscErrorCode
98 */
99PetscErrorCode ComputeDisplacement(UserCtx *user, const char *disp_field);
100
101/**
102 * @brief Derives one accumulated statistic and converts it to nodal values.
103 *
104 * The counterpart of ComputeNodalAverage() for accumulated window state: it takes an
105 * index into a window's requested output set, normalizes the centered state behind
106 * it, and leaves the result in the shared post-processing staging field ready for
107 * output. The staging vectors are reused between calls, so a caller must consume the
108 * result before requesting the next one.
109 *
110 * With `global_operations.dimensionalize` set, the result carries physical units: the
111 * source field's reference scale raised to the power the derived kind carries: a mean
112 * and an RMS are linear in it, a Reynolds stress and a turbulent kinetic energy
113 * quadratic. A co-moment flux relates two possibly different fields, so its factor is
114 * the product of their scales rather than one squared. That is what the per-field
115 * scaling table alone cannot express, and why one blanket velocity factor would have
116 * been wrong for three of the five kinds.
117 *
118 * @param[in] user Block context holding the staging fields.
119 * @param[in] definition Window or ensemble whose state is derived.
120 * @param[in] storage Its accumulated state.
121 * @param[in] outputs Comma-separated output kinds the recipe requested.
122 * @param[in] output_index Index into that output set.
123 * @param[out] out_name Name of the derived field, window qualified.
124 * @param[in] name_size Capacity of @p out_name.
125 * @param[out] out_vec Nodal vector holding the result; borrowed, not owned.
126 * @param[out] out_components Component count of the result.
127 * @return Zero on success, or a PETSc error.
128 */
129PetscErrorCode ComputeWindowStatisticNodal(UserCtx *user, const PicurvWindowDefinition *definition,
130 const PicurvWindowStorage *storage,
131 const char *outputs, PetscInt output_index,
132 char *out_name, size_t name_size,
133 Vec *out_vec, PetscInt *out_components);
134
135/**
136 * @brief Writes a window's statistics merged over homogeneous directions as a profile.
137 *
138 * Merges the pointwise state over the selected directions through
139 * `PicurvWindowStorageReduce()`, derives every requested output from the merged state,
140 * and writes one CSV row per retained slot the window sampled: the slot's 0-based cell
141 * index along each retained direction, its mean cell-centre coordinates, and each
142 * derived output, a vector one column per component. A variance or stress is that of
143 * the slot's whole sample set, including the spread of the point means across it.
144 * `point_variance` is the exception: it is derived at each point first and averaged
145 * over the points of the slot that sampled it, each counted once, so it excludes that
146 * spread. Selecting every direction gives one row for the whole domain.
147 *
148 * With `global_operations.dimensionalize` set, the outputs and coordinates are physical.
149 *
150 * @param[in] user Block context holding the staging fields.
151 * @param[in] definition Window or ensemble whose state is reduced.
152 * @param[in] storage Its pointwise state.
153 * @param[in] outputs Comma-separated output kinds the recipe requested.
154 * @param[in] average_direction Directions to merge over, in `(i, j, k)` order.
155 * @param[in] output_prefix Output path prefix; `_profile_<name>_<step>.csv` is appended.
156 * @param[in] ti Step being processed.
157 * @return Zero on success, or a PETSc error.
158 */
159PetscErrorCode ComputeWindowStatisticsProfile(UserCtx *user, const PicurvWindowDefinition *definition,
160 const PicurvWindowStorage *storage, const char *outputs,
161 const PetscBool average_direction[3],
162 const char *output_prefix, PetscInt ti);
163
164/**
165 * @brief Appends one convergence row for an accumulated window to its CSV history.
166 *
167 * The counterpart of ComputeParticleMSD() for Eulerian window state: it reduces the
168 * window to the few numbers that answer whether it has run long enough — sample
169 * count, total weight, represented time, the per-point valid-fraction range, and the
170 * mean turbulent kinetic energy — and appends them as one row per processed step.
171 * No single field snapshot can answer that question, which is why the history exists
172 * beside the field output rather than instead of it.
173 *
174 * The mean is taken over the fluid cells the window actually sampled. Cells outside
175 * the target domain, and cells a moving mask excluded, hold zeros that mean "never
176 * measured"; averaging over them would scale the result down by the fraction of the
177 * grid the window never covered.
178 *
179 * @param[in] user Block context holding the accumulators and staging fields.
180 * @param[in] window_index Window to summarize.
181 * @param[in] output_prefix Output path prefix; the window name and `.csv` are appended.
182 * @param[in] ti Step being processed, recorded as the row's key.
183 * @return Zero on success, or a PETSc error.
184 */
185PetscErrorCode ComputeWindowStatisticsSummary(UserCtx *user, PetscInt window_index,
186 const char *output_prefix, PetscInt ti);
187
188#endif // POSTPROCESSING_KERNELS_H
Public interface for data input/output routines.
Logging utilities and macros for PETSc-based applications.
PetscErrorCode ComputeQCriterion(UserCtx *user)
Computes the Q-criterion diagnostic from the local velocity-gradient tensor.
PetscErrorCode ComputeWindowStatisticsProfile(UserCtx *user, const PicurvWindowDefinition *definition, const PicurvWindowStorage *storage, const char *outputs, const PetscBool average_direction[3], const char *output_prefix, PetscInt ti)
Writes a window's statistics merged over homogeneous directions as a profile.
PetscErrorCode ComputeWindowStatisticNodal(UserCtx *user, const PicurvWindowDefinition *definition, const PicurvWindowStorage *storage, const char *outputs, PetscInt output_index, char *out_name, size_t name_size, Vec *out_vec, PetscInt *out_components)
Derives one accumulated statistic and converts it to nodal values.
PetscErrorCode ComputeSpecificKE(UserCtx *user, const char *velocity_field, const char *ske_field)
Computes the specific kinetic energy (KE per unit mass) for each particle.
PetscErrorCode ComputeDisplacement(UserCtx *user, const char *disp_field)
Computes the displacement magnitude |r_i - r_0| for each particle (per-particle VTK kernel).
PetscErrorCode NormalizeRelativeField(UserCtx *user, const char *relative_field_name)
Normalizes pressure using the value at the configured logical grid point.
PetscErrorCode ComputeWindowStatisticsSummary(UserCtx *user, PetscInt window_index, const char *output_prefix, PetscInt ti)
Appends one convergence row for an accumulated window to its CSV history.
PetscErrorCode DimensionalizeField(UserCtx *user, const char *field_name)
Scales a specified field from non-dimensional to dimensional units in-place.
PetscErrorCode ComputeNodalAverage(UserCtx *user, const char *in_field_name, const char *out_field_name)
Interpolates a cell-centered field to nodal locations using local stencil averaging.
Per-window PETSc accumulator storage and pointwise application.
Independent accumulator state for one window on one block.
The scientifically immutable definition of one window.
Main header file for a complex fluid dynamics solver.
User-defined context containing data specific to a single computational grid level.
Definition variables.h:1157