PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
 
Loading...
Searching...
No Matches
les.h
Go to the documentation of this file.
1#ifndef LES_H
2#define LES_H
3
4#include "variables.h"
5#include "logging.h"
6#include "Metric.h"
7#include "setup.h"
8#include "Filter.h"
9#include "Boundaries.h"
10
11/*================================================================================*
12 * SYMMETRIC TENSOR KERNELS (no LES-specific knowledge) *
13 *================================================================================*/
14
15/**
16 * @brief Forms a symmetric tensor from a vector's outer product with itself.
17 *
18 * Used to build the resolved velocity products the Leonard stress subtracts.
19 *
20 * @param[in] v Vector to square.
21 * @return The tensor `v_i v_j`.
22 */
24
25/**
26 * @brief Forms the linear combination `a*x + b*y`.
27 *
28 * One primitive covers addition, subtraction, and scaling, so tensor algebra at the
29 * call sites reads as algebra instead of six repeated component expressions.
30 *
31 * @param[in] a Scalar multiplying @p x.
32 * @param[in] x First tensor.
33 * @param[in] b Scalar multiplying @p y.
34 * @param[in] y Second tensor.
35 * @return The combined tensor.
36 */
37SymTensor SymTensorCombine(PetscReal a, SymTensor x, PetscReal b, SymTensor y);
38
39/**
40 * @brief Returns the trace `t_kk`.
41 * @param[in] t Tensor to trace.
42 * @return Sum of the three diagonal components.
43 */
44PetscReal SymTensorTrace(SymTensor t);
45
46/**
47 * @brief Removes the isotropic part, returning `t_ij - (1/3) delta_ij t_kk`.
48 *
49 * The Germano model tensor must be deviatoric so that the trace of the Leonard
50 * stress, which the incompressible pressure absorbs, cannot enter the contraction
51 * through discrete divergence error.
52 *
53 * @param[in] t Tensor to project.
54 * @return The trace-free part of @p t.
55 */
57
58/**
59 * @brief Contracts two symmetric tensors as `a_ij b_ij`.
60 *
61 * Only six components are stored, so the three off-diagonal products are counted
62 * twice. Centralising that factor is the point: written out at each call site it is
63 * a silent error whenever one term is missed.
64 *
65 * @param[in] a First tensor.
66 * @param[in] b Second tensor.
67 * @return The full nine-term contraction.
68 */
70
71/**
72 * @brief Applies a symmetric tensor to a vector, returning `t_ij v_j`.
73 *
74 * The three stored off-diagonal components each stand for two entries of the full
75 * tensor, so a caller writing this out by hand has six chances to reference the wrong
76 * one. Centralising it is the same reason `SymTensorContract()` exists.
77 *
78 * @param[in] t Tensor to apply.
79 * @param[in] v Vector to apply it to.
80 * @return The resulting vector.
81 */
83{
84 Cmpnts result;
85
86 result.x = t.xx * v.x + t.xy * v.y + t.xz * v.z;
87 result.y = t.xy * v.x + t.yy * v.y + t.yz * v.z;
88 result.z = t.xz * v.x + t.yz * v.y + t.zz * v.z;
89 return result;
90}
91
92/**
93 * @brief Returns the squared Frobenius norm `t_ij t_ij`.
94 * @param[in] t Tensor to measure.
95 * @return The self-contraction of @p t.
96 */
97PetscReal SymTensorNormSq(SymTensor t);
98
99/*================================================================================*
100 * STRAIN RATE AND FILTER WIDTH *
101 *================================================================================*/
102
103/**
104 * @brief Builds the strain-rate tensor and its magnitude from a velocity gradient.
105 *
106 * `S_ij = 0.5 (du_i/dx_j + du_j/dx_i)` and `|S| = sqrt(2 S_ij S_ij)`. This is the
107 * single definition of both quantities; the dynamic procedure and the eddy-viscosity
108 * assembly consume it rather than each expanding the algebra themselves.
109 *
110 * @param[in] dudx Gradient of the x velocity component.
111 * @param[in] dvdx Gradient of the y velocity component.
112 * @param[in] dwdx Gradient of the z velocity component.
113 * @param[out] strain Resolved strain-rate tensor; may be NULL if only the magnitude is wanted.
114 * @param[out] magnitude Strain-rate magnitude `|S|`; may be NULL.
115 * @return PetscErrorCode 0 on success.
116 */
117PetscErrorCode StrainRateFromGradients(Cmpnts dudx, Cmpnts dvdx, Cmpnts dwdx,
118 SymTensor *strain, PetscReal *magnitude);
119
120/**
121 * @brief Computes one cell's grid filter width under the selected width model.
122 *
123 * ::LES_FILTER_WIDTH_CUBE_ROOT_VOLUME uses the cell volume alone and so ignores
124 * anisotropy; the other models take the cell's extents along its own grid directions
125 * from @ref ComputeCellDirectionalExtents, so they follow the cell's shape and are
126 * unchanged by its orientation.
127 *
128 * @param[in] model Width model to apply.
129 * @param[in] aj Inverse cell volume (the Jacobian determinant) at the cell.
130 * @param[in] csi Xi-direction metric vector at the cell.
131 * @param[in] eta Eta-direction metric vector at the cell.
132 * @param[in] zet Zeta-direction metric vector at the cell.
133 * @param[out] delta Resolved filter width.
134 * @return PetscErrorCode 0 on success, or `PETSC_ERR_ARG_OUTOFRANGE` for a
135 * non-positive Jacobian.
136 */
137PetscErrorCode ComputeCellFilterWidth(LESFilterWidthModel model, PetscReal aj,
138 Cmpnts csi, Cmpnts eta, Cmpnts zet, PetscReal *delta);
139
140/*================================================================================*
141 * GERMANO IDENTITY KERNELS *
142 *================================================================================*/
143
144/**
145 * @brief Forms the Leonard stress `L_ij = (u_i u_j)^ - u^_i u^_j`.
146 *
147 * `L_ij` is the stress carried by the scales between the grid filter and the test
148 * filter. It is computable entirely from the resolved field, which is what makes the
149 * dynamic procedure possible.
150 *
151 * @param[in] velocity_filtered Test-filtered resolved velocity `u^_i`.
152 * @param[in] velocity_product_filtered Test filter of the products `(u_i u_j)^`.
153 * @return The Leonard stress tensor.
154 */
155SymTensor LeonardStress(Cmpnts velocity_filtered, SymTensor velocity_product_filtered);
156
157/**
158 * @brief Forms the deviatoric Germano model tensor `M_ij`.
159 *
160 * `M_ij = -2 Delta^2 ( alpha |S^| S^_ij - (|S| S_ij)^ )`, made trace-free.
161 *
162 * The two terms are structurally different and must stay so. The first is the model
163 * evaluated on the test-filtered strain, the product of separately filtered
164 * quantities. The second is the grid-level model formed pointwise and then
165 * test-filtered, that is, the filter of a product. Their difference is the entire
166 * information content of the dynamic procedure: collapsing them into the same
167 * expression yields a well-scaled, flow-responsive, and wrong coefficient that is
168 * not the Germano-Lilly coefficient and cannot be cited as one.
169 *
170 * @param[in] delta Grid filter width at the cell.
171 * @param[in] alpha Squared test-to-grid filter width ratio.
172 * @param[in] strain_magnitude_filtered Test-filtered strain magnitude `|S|^`.
173 * @param[in] strain_filtered Test-filtered strain tensor `S^_ij`.
174 * @param[in] strain_product_filtered Test filter of the product `(|S| S_ij)^`.
175 * @return The deviatoric model tensor.
176 */
177SymTensor GermanoModelTensor(PetscReal delta, PetscReal alpha,
178 PetscReal strain_magnitude_filtered,
179 SymTensor strain_filtered,
180 SymTensor strain_product_filtered);
181
182/*================================================================================*
183 * COEFFICIENT AVERAGING AND LIMITING *
184 *================================================================================*/
185
186/**
187 * @brief Resolves which logical directions the dynamic coefficient is averaged over.
188 *
189 * The resulting selector is passed to `PicurvSpatialRatioAverage()`, which performs
190 * the reduction; this routine only decides which directions it spans.
191 *
192 * ::LES_AVERAGING_LOCAL selects none, ::LES_AVERAGING_GLOBAL selects all three, and
193 * ::LES_AVERAGING_HOMOGENEOUS uses the directions the configuration names, defaulting
194 * to the block's periodic axes when it names none. Periodicity is read from the
195 * boundary face configuration, which is the same source the periodic field
196 * synchronization uses.
197 *
198 * @param[in] user Block context supplying configuration and boundary faces.
199 * @param[out] direction Per-direction averaging flags in `(xi, eta, zeta)` order.
200 * @return PetscErrorCode 0 on success, or `PETSC_ERR_ARG_WRONGSTATE` when
201 * homogeneous averaging is requested but no direction can be resolved.
202 */
203PetscErrorCode ResolveLESAveragingDirections(UserCtx *user, PetscBool direction[3]);
204
205/**
206 * @brief Applies the configured admissible range to one model coefficient.
207 *
208 * The coefficient carries the sign of `<L:M>`; a negative value is backscatter,
209 * energy returning from the unresolved scales, not an error. ::LES_CLIP_NONE keeps
210 * it, leaving stability to the total-viscosity floor applied downstream.
211 *
212 * @param[in] coefficient Raw coefficient from the Germano contraction.
213 * @param[in] config LES configuration supplying the mode and ceiling.
214 * @param[out] limited Set to `PETSC_TRUE` when the value was modified; may be NULL.
215 * @return The coefficient after limiting.
216 */
217PetscReal ClipModelCoefficient(PetscReal coefficient, const LESConfig *config, PetscBool *limited);
218
219/**
220 * @brief Builds the eddy viscosity from a model coefficient.
221 *
222 * `nu_t = C Delta^2 |S|`, floored so that `nu + nu_t >= min_viscosity_ratio * nu`.
223 * The floor constrains the quantity that must stay positive for the momentum
224 * operator to remain well posed, which is why backscatter is limited here rather
225 * than by forcing the coefficient positive.
226 *
227 * @param[in] coefficient Model coefficient, signed.
228 * @param[in] delta Grid filter width at the cell.
229 * @param[in] strain_magnitude Resolved strain magnitude `|S|`.
230 * @param[in] molecular_viscosity Molecular kinematic viscosity.
231 * @param[in] min_viscosity_ratio Lower bound on total viscosity as a multiple of molecular.
232 * @return The admissible eddy viscosity.
233 */
234PetscReal EddyViscosityFromCoefficient(PetscReal coefficient, PetscReal delta,
235 PetscReal strain_magnitude,
236 PetscReal molecular_viscosity,
237 PetscReal min_viscosity_ratio);
238
239/**
240 * @brief WALE eddy viscosity (Nicoud & Ducros 1999) from the resolved velocity gradient.
241 *
242 * `nu_t = (C_w Delta)^2 (S^d_ij S^d_ij)^(3/2) / [(S_ij S_ij)^(5/2) + (S^d_ij S^d_ij)^(5/4)]`,
243 * where `S^d` is the traceless symmetric part of the squared gradient. It vanishes in pure
244 * shear, so a laminar shear layer gets none, and it falls as the cube of wall distance
245 * approaching a wall without a damping function. It needs no coefficient field, no test
246 * filter, and no averaging, so it is valid on geometry with no homogeneous direction.
247 *
248 * @param[in] dudx Gradient of the x velocity component.
249 * @param[in] dvdx Gradient of the y velocity component.
250 * @param[in] dwdx Gradient of the z velocity component.
251 * @param[in] delta Grid filter width at the cell.
252 * @param[in] coefficient Model constant `C_w`.
253 * @return The eddy viscosity, zero where the gradient carries no model invariant.
254 */
255PetscReal WALEEddyViscosity(Cmpnts dudx, Cmpnts dvdx, Cmpnts dwdx,
256 PetscReal delta, PetscReal coefficient);
257
258/**
259 * @brief Vreman eddy viscosity (Vreman 2004), weighting each grid direction by its own spacing.
260 *
261 * `nu_t = c sqrt(B_beta / (alpha_ij alpha_ij))`, with `alpha_ij = du_j/dx_i` and
262 * `beta_ij = sum_m d_i^(m) d_j^(m)`, where `d^(m)` is the change in velocity across the
263 * cell along grid direction `m` - `edges[m] . grad(u)`. On an aligned Cartesian grid that
264 * is Vreman's `Delta_m^2 alpha_mi alpha_mj`; on a curvilinear grid it is the same quantity
265 * measured along the cell's own edges, so the model is resolved per direction rather than
266 * through one scalar width, and unchanged by the grid's orientation. It vanishes in pure
267 * shear and needs no coefficient field, test filter, or averaging.
268 *
269 * @param[in] dudx Gradient of the x velocity component.
270 * @param[in] dvdx Gradient of the y velocity component.
271 * @param[in] dwdx Gradient of the z velocity component.
272 * @param[in] edges Cell edge vectors along xi, eta, zeta, from @ref ComputeCellEdgeVectors.
273 * @param[in] coefficient Model constant `c`.
274 * @return The eddy viscosity, zero where `B_beta` vanishes.
275 */
276PetscReal VremanEddyViscosity(Cmpnts dudx, Cmpnts dvdx, Cmpnts dwdx,
277 const Cmpnts edges[3], PetscReal coefficient);
278
279/**
280 * @brief Returns the modelled subgrid kinetic energy at a cell.
281 *
282 * Uses the Yoshizawa relation `k_sgs = 2 C_I Delta^2 |S|^2`. Reported as a
283 * diagnostic: compared against the resolved turbulent kinetic energy from the
284 * field-statistics pipeline it measures how much of the energy the grid resolves.
285 *
286 * @param[in] yoshizawa_ci Yoshizawa constant `C_I`.
287 * @param[in] delta Grid filter width at the cell.
288 * @param[in] strain_magnitude Resolved strain magnitude `|S|`.
289 * @return The modelled subgrid kinetic energy.
290 */
291PetscReal SubgridKineticEnergy(PetscReal yoshizawa_ci, PetscReal delta, PetscReal strain_magnitude);
292
293/*================================================================================*
294 * RUNTIME ENTRY POINTS *
295 *================================================================================*/
296
297/**
298 * @brief Computes the dynamic Smagorinsky coefficient field for one block.
299 *
300 * Runs the Germano-Lilly procedure: test-filters the resolved field, forms the
301 * Leonard stress and the model tensor, contracts them, averages the two
302 * contractions over the configured directions, limits the quotient, and stores it in
303 * `UserCtx::CS`.
304 *
305 * The stored quantity is the coefficient `C` that multiplies `Delta^2 |S|` in the
306 * eddy viscosity, that is, `Cs^2` rather than `Cs`. It is signed under
307 * ::LES_CLIP_NONE. Reported values are converted to `Cs` at the diagnostic boundary,
308 * where the familiar target near 0.16 applies.
309 *
310 * Only ::DYNAMIC_SMAGORINSKY reaches this routine; the constant model prescribes its
311 * coefficient from configuration and allocates no field. Callers honour
312 * `LESConfig::dynamic_frequency`, and the caller must have refreshed `UserCtx::lUcat`
313 * and its periodic images beforehand.
314 *
315 * @param[in,out] user The user context for a single computational block.
316 * @return PetscErrorCode 0 on success.
317 */
318PetscErrorCode ComputeSmagorinskyConstant(UserCtx *user);
319
320/**
321 * @brief Computes the turbulent eddy viscosity for one block.
322 *
323 * Evaluates `nu_t = C Delta^2 |S|` at every fluid cell and stores it in
324 * `UserCtx::Nu_t`. The coefficient comes from `UserCtx::lCs` for the dynamic model
325 * and directly from `LESConfig::constant_cs` for the constant model, which therefore
326 * needs no coefficient field at all.
327 *
328 * @param[in,out] user The user context for a single computational block.
329 * @return PetscErrorCode 0 on success.
330 */
331PetscErrorCode ComputeEddyViscosityLES(UserCtx *user);
332
333/**
334 * @brief Appends one row of LES coefficient statistics to the run's log directory.
335 *
336 * Writes `les_coefficient.csv`: the effective coefficient reported as `Cs`, its
337 * spatial spread, eddy-viscosity levels, the modelled subgrid energy, and the
338 * fractions of the domain that were backscattering or limited before this step's
339 * clipping. Those last two describe the pre-clipping state, which no stored field
340 * preserves.
341 *
342 * Values are instantaneous volume statistics, not time averages; window averages of
343 * the same model's fields come from the field-statistics pipeline instead.
344 *
345 * @param[in] user The user context for a single computational block.
346 * @return PetscErrorCode 0 on success.
347 */
348PetscErrorCode LogLESDiagnostics(UserCtx *user);
349
350#endif // LES_H
PetscReal SymTensorNormSq(SymTensor t)
Returns the squared Frobenius norm t_ij t_ij.
Definition les.c:210
PetscErrorCode ComputeEddyViscosityLES(UserCtx *user)
Computes the turbulent eddy viscosity for one block.
Definition les.c:899
SymTensor SymTensorCombine(PetscReal a, SymTensor x, PetscReal b, SymTensor y)
Forms the linear combination a*x + b*y.
Definition les.c:151
SymTensor SymTensorSelfOuter(Cmpnts v)
Forms a symmetric tensor from a vector's outer product with itself.
Definition les.c:132
SymTensor SymTensorDeviator(SymTensor t)
Removes the isotropic part, returning t_ij - (1/3) delta_ij t_kk.
Definition les.c:181
PetscReal SubgridKineticEnergy(PetscReal yoshizawa_ci, PetscReal delta, PetscReal strain_magnitude)
Returns the modelled subgrid kinetic energy at a cell.
Definition les.c:544
PetscReal SymTensorTrace(SymTensor t)
Returns the trace t_kk.
Definition les.c:170
PetscReal EddyViscosityFromCoefficient(PetscReal coefficient, PetscReal delta, PetscReal strain_magnitude, PetscReal molecular_viscosity, PetscReal min_viscosity_ratio)
Builds the eddy viscosity from a model coefficient.
Definition les.c:448
SymTensor GermanoModelTensor(PetscReal delta, PetscReal alpha, PetscReal strain_magnitude_filtered, SymTensor strain_filtered, SymTensor strain_product_filtered)
Forms the deviatoric Germano model tensor M_ij.
Definition les.c:326
PetscReal ClipModelCoefficient(PetscReal coefficient, const LESConfig *config, PetscBool *limited)
Applies the configured admissible range to one model coefficient.
Definition les.c:419
PetscErrorCode ComputeCellFilterWidth(LESFilterWidthModel model, PetscReal aj, Cmpnts csi, Cmpnts eta, Cmpnts zet, PetscReal *delta)
Computes one cell's grid filter width under the selected width model.
Definition les.c:252
PetscErrorCode StrainRateFromGradients(Cmpnts dudx, Cmpnts dvdx, Cmpnts dwdx, SymTensor *strain, PetscReal *magnitude)
Builds the strain-rate tensor and its magnitude from a velocity gradient.
Definition les.c:225
PetscErrorCode ResolveLESAveragingDirections(UserCtx *user, PetscBool direction[3])
Resolves which logical directions the dynamic coefficient is averaged over.
Definition les.c:360
SymTensor LeonardStress(Cmpnts velocity_filtered, SymTensor velocity_product_filtered)
Forms the Leonard stress L_ij = (u_i u_j)^ - u^_i u^_j.
Definition les.c:313
PetscReal SymTensorContract(SymTensor a, SymTensor b)
Contracts two symmetric tensors as a_ij b_ij.
Definition les.c:197
PetscErrorCode LogLESDiagnostics(UserCtx *user)
Appends one row of LES coefficient statistics to the run's log directory.
Definition les.c:991
static Cmpnts SymTensorTimesVector(SymTensor t, Cmpnts v)
Applies a symmetric tensor to a vector, returning t_ij v_j.
Definition les.h:82
PetscErrorCode ComputeSmagorinskyConstant(UserCtx *user)
Computes the dynamic Smagorinsky coefficient field for one block.
Definition les.c:664
PetscReal WALEEddyViscosity(Cmpnts dudx, Cmpnts dvdx, Cmpnts dwdx, PetscReal delta, PetscReal coefficient)
WALE eddy viscosity (Nicoud & Ducros 1999) from the resolved velocity gradient.
Definition les.c:469
PetscReal VremanEddyViscosity(Cmpnts dudx, Cmpnts dvdx, Cmpnts dwdx, const Cmpnts edges[3], PetscReal coefficient)
Vreman eddy viscosity (Vreman 2004), weighting each grid direction by its own spacing.
Definition les.c:506
Logging utilities and macros for PETSc-based applications.
Main header file for a complex fluid dynamics solver.
PetscReal yy
Definition variables.h:162
PetscScalar x
Definition variables.h:145
PetscScalar z
Definition variables.h:145
PetscReal xx
Definition variables.h:162
PetscScalar y
Definition variables.h:145
PetscReal yz
Definition variables.h:162
PetscReal zz
Definition variables.h:162
LESFilterWidthModel
Selects how a cell's grid filter width is derived from its metrics.
Definition variables.h:614
PetscReal xz
Definition variables.h:162
PetscReal xy
Definition variables.h:162
A 3D point or vector with PetscScalar components.
Definition variables.h:144
Every user-selectable parameter of the LES closure.
Definition variables.h:664
A symmetric second-order tensor stored by its six independent components.
Definition variables.h:161
User-defined context containing data specific to a single computational grid level.
Definition variables.h:1157