PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
 
Loading...
Searching...
No Matches
poisson.h
Go to the documentation of this file.
1#ifndef POISSON_H
2#define POISSON_H
3
4#include "variables.h"
5#include "Boundaries.h"
6
7/**
8 * @file poisson.h
9 * @brief Pressure-Poisson projection: the multigrid pressure-correction solve, the
10 * pressure update, and the velocity correction that makes `Ucont` divergence-free.
11 *
12 * The discrete operator, the right-hand side, and the projection gradient are built from
13 * one face-gradient definition, so the Laplacian the solver inverts is exactly the
14 * divergence of the gradient the projection applies.
15 *
16 * Every masked operation reads the solid field from the `UserCtx` of the level it acts
17 * on (`lNvert`). Solid cells are excluded from the operator, the right-hand side, the
18 * projection, and the multigrid transfers; cells next to a solid face use one-sided
19 * transverse differences.
20 */
21
22/* ===========================================================================================
23 * Face-gradient operator, shared with the momentum residual's pressure gradient.
24 * poisson.c documents the face gradient; the per-face primitives are inline so the
25 * momentum residual's hot loop does not pay a call per face.
26 * =========================================================================================== */
27
28/** Cells whose `nvert` exceeds this value are solid. */
29#define POISSON_SOLID_THRESHOLD 0.1
30
31/**
32 * @brief Transverse difference used at one face.
33 *
34 * The difference is `weight * (sum of Phi on row hi - sum of Phi on row lo)`, where a row
35 * is the pair of cells on either side of the face, displaced by the given offset along the
36 * transverse axis.
37 */
38typedef struct {
39 PetscInt lo; /**< Transverse offset of the subtracted row pair. */
40 PetscInt hi; /**< Transverse offset of the added row pair. */
41 PetscReal weight; /**< 0.25 central, 0.5 one-sided, 0 when no fluid side remains. */
43
44/** @brief Read-only face metric arrays: `metric[n][b]` is F_b on the n-faces. */
45typedef struct {
46 const Cmpnts ***metric[3][3];
47 const PetscReal ***aj[3];
49
50/** @brief The transverse stencils a face gradient can use; see PoissonOperator_TransverseKind(). */
51typedef enum {
52 POISSON_TRANSVERSE_NONE = 0, /**< Neither side available: the transverse term vanishes. */
53 POISSON_TRANSVERSE_LOWER, /**< One-sided over rows -1 and 0, weight 0.5. */
54 POISSON_TRANSVERSE_UPPER, /**< One-sided over rows 0 and +1, weight 0.5. */
55 POISSON_TRANSVERSE_CENTRAL /**< Central over rows -1 and +1, weight 0.25. */
57
58/**
59 * @brief Value of a cell-centred array at `c + p e_b + q e_n`.
60 *
61 * With @p b, @p n, @p p and @p q constants every offset is a constant, so the per-face stencils
62 * below compile to straight-line loads.
63 *
64 * @param[in] field Ghosted cell-centred array, indexed [k][j][i].
65 * @param[in] c Cell (i, j, k).
66 * @param[in] b First axis.
67 * @param[in] p Displacement along @p b.
68 * @param[in] n Second axis.
69 * @param[in] q Displacement along @p n.
70 * @return The value at the displaced cell.
71 */
72static PICURV_ALWAYS_INLINE PetscReal PoissonOperator_AtBN(const PetscReal ***field, const PetscInt c[3],
73 PetscInt b, PetscInt p, PetscInt n, PetscInt q)
74{
75 return field[c[2] + (b == 2) * p + (n == 2) * q][c[1] + (b == 1) * p + (n == 1) * q][c[0] + (b == 0) * p + (n == 0) * q];
76}
77
78/**
79 * @brief Chooses the transverse stencil along axis @p t at the face between cell @p c and
80 * `c + e_n`.
81 *
82 * The central difference averages the two cells beside the face over rows -1 and +1.
83 * When the +1 row is a non-periodic boundary layer or touches a solid cell, the difference
84 * falls back to rows -1 and 0, and symmetrically to rows 0 and +1; with neither side
85 * available the transverse term vanishes.
86 *
87 * @param[in] nvert Ghosted solid field.
88 * @param[in] c Cell on the low side of the face.
89 * @param[in] n Face orientation (0, 1, 2 for i, j, k).
90 * @param[in] t Transverse axis, not @p n.
91 * @param[in] m Grid sizes (mx, my, mz).
92 * @param[in] periodic Whether each axis is periodic.
93 * @return The stencil to use.
94 */
96 const PetscReal ***nvert, const PetscInt c[3], PetscInt n, PetscInt t,
97 const PetscInt m[3], const PetscBool periodic[3])
98{
99 const PetscInt s = c[t];
100 const PetscReal solid_plus = PoissonOperator_AtBN(nvert, c, t, 1, n, 0) + PoissonOperator_AtBN(nvert, c, t, 1, n, 1);
101 const PetscReal solid_minus = PoissonOperator_AtBN(nvert, c, t, -1, n, 0) + PoissonOperator_AtBN(nvert, c, t, -1, n, 1);
102
103 if ((s == m[t] - 2 && !periodic[t]) || solid_plus > POISSON_SOLID_THRESHOLD) {
104 return (solid_minus < POISSON_SOLID_THRESHOLD && (s != 1 || periodic[t])) ? POISSON_TRANSVERSE_LOWER
106 }
107 if ((s == 1 && !periodic[t]) || solid_minus > POISSON_SOLID_THRESHOLD) {
109 }
111}
112
113/**
114 * @brief Transverse difference of @p field along axis @p t at the face between cell @p c and
115 * `c + e_n`: `(sum of the row-pair hi - sum of the row-pair lo) * weight`.
116 *
117 * @param[in] nvert Ghosted solid field.
118 * @param[in] field Ghosted cell-centred field.
119 * @param[in] c Cell on the low side of the face.
120 * @param[in] n Face orientation (0, 1, 2 for i, j, k).
121 * @param[in] t Transverse axis, not @p n.
122 * @param[in] m Grid sizes (mx, my, mz).
123 * @param[in] periodic Whether each axis is periodic.
124 * @return The weighted difference, 0 when no side is available.
125 */
127 const PetscReal ***nvert, const PetscReal ***field, const PetscInt c[3], PetscInt n, PetscInt t,
128 const PetscInt m[3], const PetscBool periodic[3])
129{
130 switch (PoissonOperator_TransverseKind(nvert, c, n, t, m, periodic)) {
132 return (PoissonOperator_AtBN(field, c, t, 0, n, 0) + PoissonOperator_AtBN(field, c, t, 0, n, 1) -
133 PoissonOperator_AtBN(field, c, t, -1, n, 0) - PoissonOperator_AtBN(field, c, t, -1, n, 1)) * 0.5;
135 return (PoissonOperator_AtBN(field, c, t, 1, n, 0) + PoissonOperator_AtBN(field, c, t, 1, n, 1) -
136 PoissonOperator_AtBN(field, c, t, 0, n, 0) - PoissonOperator_AtBN(field, c, t, 0, n, 1)) * 0.5;
138 return (PoissonOperator_AtBN(field, c, t, 1, n, 0) + PoissonOperator_AtBN(field, c, t, 1, n, 1) -
139 PoissonOperator_AtBN(field, c, t, -1, n, 0) - PoissonOperator_AtBN(field, c, t, -1, n, 1)) * 0.25;
141 default:
142 return 0.0;
143 }
144}
145
146/**
147 * @brief Gradient flux of a cell-centred @p field through the face between cell @p c and
148 * `c + e_n`: `sum_b g_nb D_b(field)` of the face gradient defined in poisson.c.
149 *
150 * The projection applies it to `Phi`; the momentum residual applies it to `P`, so both use
151 * the one discrete gradient.
152 *
153 * @param[in] metrics Face metric arrays from PoissonOperator_GetFaceMetrics().
154 * @param[in] nvert Ghosted solid field.
155 * @param[in] field Ghosted cell-centred field whose gradient is taken.
156 * @param[in] c Cell on the low side of the face.
157 * @param[in] n Face orientation (0, 1, 2 for i, j, k).
158 * @param[in] m Grid sizes (mx, my, mz).
159 * @param[in] periodic Whether each axis is periodic.
160 * @return The gradient flux through the face.
161 */
163 const PoissonFaceMetrics *metrics, const PetscReal ***nvert, const PetscReal ***field,
164 const PetscInt c[3], PetscInt n, const PetscInt m[3], const PetscBool periodic[3])
165{
166 const Cmpnts normal = metrics->metric[n][n][c[2]][c[1]][c[0]];
167 const PetscReal aj = metrics->aj[n][c[2]][c[1]][c[0]];
168 PetscReal dot[3], difference[3];
169
170 for (PetscInt b = 0; b < 3; b++) {
171 const Cmpnts base = metrics->metric[n][b][c[2]][c[1]][c[0]];
172 dot[b] = base.x * normal.x + base.y * normal.y + base.z * normal.z;
173 }
174 difference[n] = PoissonOperator_AtBN(field, c, n, 0, n, 1) - field[c[2]][c[1]][c[0]];
175 difference[(n + 1) % 3] = PoissonOperator_TransverseFieldDifference(nvert, field, c, n, (n + 1) % 3, m, periodic);
176 difference[(n + 2) % 3] = PoissonOperator_TransverseFieldDifference(nvert, field, c, n, (n + 2) % 3, m, periodic);
177 return difference[0] * dot[0] * aj +
178 difference[1] * dot[1] * aj +
179 difference[2] * dot[2] * aj;
180}
181
182
183/**
184 * @brief Borrows read access to the face metric arrays of @p user.
185 *
186 * @param[in] user Block context whose ghosted face metrics are read.
187 * @param[out] metrics Face metric arrays; return them with PoissonOperator_RestoreFaceMetrics().
188 * @return PetscErrorCode 0 on success.
189 */
190PetscErrorCode PoissonOperator_GetFaceMetrics(UserCtx *user, PoissonFaceMetrics *metrics);
191
192/**
193 * @brief Returns the arrays borrowed by PoissonOperator_GetFaceMetrics().
194 *
195 * @param[in] user Block context the arrays were borrowed from.
196 * @param[in,out] metrics Face metric arrays to return.
197 * @return PetscErrorCode 0 on success.
198 */
200
201/**
202 * @brief Solves the pressure-correction equation for every block with geometric multigrid.
203 *
204 * On the first call for a block, assembles the operator on every multigrid level and
205 * builds the outer Krylov solver with its `PCMG` preconditioner, grid transfers, level
206 * smoothers, coarse solve, and null space. The solver is kept in the finest level's
207 * `UserCtx::ksp` and reused on every later call; it is destroyed with the context.
208 *
209 * Each call refreshes the ghosted contravariant flux, forms the right-hand side, solves
210 * for `Phi` on the finest level, and appends the iteration history to
211 * `Poisson_Solver_Convergence_History_Block_<bi>.log` in the run's log directory.
212 *
213 * Solver controls are read from PETSc options under the `ps_` prefix
214 * (`-ps_ksp_*`, `-ps_mg_levels_N_*`, `-ps_mg_coarse_*`) when the solver is built.
215 *
216 * @param[in,out] usermg Multigrid hierarchy; the finest level's `Phi` receives the solution.
217 * @return PETSc error code. A non-finite residual or a preconditioner that could not be
218 * built stops the run with PETSC_ERR_NOT_CONVERGED.
219 */
220extern PetscErrorCode PoissonSolver_Multigrid(UserMG *usermg);
221
222/**
223 * @brief Assembles the pressure-correction operator on one multigrid level.
224 *
225 * Allocates `user->A` on the first call and reassembles its entries on later calls into
226 * the same nonzero structure. Fluid rows carry the 19-point curvilinear Laplacian;
227 * dummy rows on the domain boundary and solid rows are identities. Non-periodic faces
228 * are homogeneous Neumann; periodic faces wrap to the opposite interior layer.
229 *
230 * @param[in,out] user Level context supplying metrics, `lNvert`, and boundary types.
231 * @return PETSc error code.
232 */
233extern PetscErrorCode AssemblePoissonOperator(UserCtx *user);
234
235/**
236 * @brief Forms the right-hand side of the pressure-correction equation.
237 *
238 * Writes the scaled divergence of the ghosted contravariant flux `lUcont` into @p B,
239 * zero on dummy and solid cells, and stores its domain integral in
240 * `SimCtx::poissonSourceImbalance`.
241 *
242 * @param[in] user Finest-level context.
243 * @param[out] B Right-hand-side vector on `user->da`.
244 * @return PETSc error code.
245 */
246extern PetscErrorCode ComputePoissonRHS(UserCtx *user, Vec B);
247
248/**
249 * @brief Adds the pressure correction to the pressure, `P += Phi`, and refreshes both
250 * fields' periodic images and ghosts.
251 * @param[in,out] user Block context holding `P` and `Phi`.
252 * @return PETSc error code.
253 */
254extern PetscErrorCode UpdatePressure(UserCtx *user);
255
256/**
257 * @brief Corrects the contravariant flux with the gradient of `Phi`.
258 *
259 * Subtracts `dt / COEF_TIME_ACCURACY` times the face pressure-gradient flux from every
260 * fluid face of `Ucont`, then refreshes the periodic images, reconstructs the Cartesian
261 * velocity, and finalizes the cell fields that depend on it.
262 *
263 * @param[in,out] user Block context; reads the ghosted `lPhi`.
264 * @return PETSc error code.
265 */
266extern PetscErrorCode ProjectVelocity(UserCtx *user);
267
268#endif /* POISSON_H */
PetscErrorCode PoissonOperator_RestoreFaceMetrics(UserCtx *user, PoissonFaceMetrics *metrics)
Returns the arrays borrowed by PoissonOperator_GetFaceMetrics().
Definition poisson.c:171
const PetscReal *** aj[3]
Definition poisson.h:47
PetscReal weight
0.25 central, 0.5 one-sided, 0 when no fluid side remains.
Definition poisson.h:41
const Cmpnts *** metric[3][3]
Definition poisson.h:46
static PoissonTransverseKind PoissonOperator_TransverseKind(const PetscReal ***nvert, const PetscInt c[3], PetscInt n, PetscInt t, const PetscInt m[3], const PetscBool periodic[3])
Chooses the transverse stencil along axis t at the face between cell c and c + e_n.
Definition poisson.h:95
PetscErrorCode AssemblePoissonOperator(UserCtx *user)
Assembles the pressure-correction operator on one multigrid level.
Definition poisson.c:244
PoissonTransverseKind
The transverse stencils a face gradient can use; see PoissonOperator_TransverseKind().
Definition poisson.h:51
@ POISSON_TRANSVERSE_NONE
Neither side available: the transverse term vanishes.
Definition poisson.h:52
@ POISSON_TRANSVERSE_CENTRAL
Central over rows -1 and +1, weight 0.25.
Definition poisson.h:55
@ POISSON_TRANSVERSE_LOWER
One-sided over rows -1 and 0, weight 0.5.
Definition poisson.h:53
@ POISSON_TRANSVERSE_UPPER
One-sided over rows 0 and +1, weight 0.5.
Definition poisson.h:54
PetscInt hi
Transverse offset of the added row pair.
Definition poisson.h:40
static PetscReal PoissonOperator_FaceGradientFlux(const PoissonFaceMetrics *metrics, const PetscReal ***nvert, const PetscReal ***field, const PetscInt c[3], PetscInt n, const PetscInt m[3], const PetscBool periodic[3])
Gradient flux of a cell-centred field through the face between cell c and c + e_n: sum_b g_nb D_b(fie...
Definition poisson.h:162
#define POISSON_SOLID_THRESHOLD
Cells whose nvert exceeds this value are solid.
Definition poisson.h:29
PetscErrorCode PoissonOperator_GetFaceMetrics(UserCtx *user, PoissonFaceMetrics *metrics)
Borrows read access to the face metric arrays of user.
Definition poisson.c:147
PetscErrorCode UpdatePressure(UserCtx *user)
Adds the pressure correction to the pressure, P += Phi, and refreshes both fields' periodic images an...
Definition poisson.c:405
PetscErrorCode ProjectVelocity(UserCtx *user)
Corrects the contravariant flux with the gradient of Phi.
Definition poisson.c:425
static PetscReal PoissonOperator_TransverseFieldDifference(const PetscReal ***nvert, const PetscReal ***field, const PetscInt c[3], PetscInt n, PetscInt t, const PetscInt m[3], const PetscBool periodic[3])
Transverse difference of field along axis t at the face between cell c and c + e_n: (sum of the row-p...
Definition poisson.h:126
PetscErrorCode PoissonSolver_Multigrid(UserMG *usermg)
Solves the pressure-correction equation for every block with geometric multigrid.
Definition poisson.c:922
PetscInt lo
Transverse offset of the subtracted row pair.
Definition poisson.h:39
PetscErrorCode ComputePoissonRHS(UserCtx *user, Vec B)
Forms the right-hand side of the pressure-correction equation.
Definition poisson.c:344
static PetscReal PoissonOperator_AtBN(const PetscReal ***field, const PetscInt c[3], PetscInt b, PetscInt p, PetscInt n, PetscInt q)
Value of a cell-centred array at c + p e_b + q e_n.
Definition poisson.h:72
Read-only face metric arrays: metric[n][b] is F_b on the n-faces.
Definition poisson.h:45
Transverse difference used at one face.
Definition poisson.h:38
Main header file for a complex fluid dynamics solver.
#define PICURV_ALWAYS_INLINE
Forces inlining of a per-cell or per-face kernel whose size defeats the compiler's inlining heuristic...
Definition variables.h:65
PetscScalar x
Definition variables.h:145
PetscScalar z
Definition variables.h:145
PetscScalar y
Definition variables.h:145
A 3D point or vector with PetscScalar components.
Definition variables.h:144
User-defined context containing data specific to a single computational grid level.
Definition variables.h:1157
User-level context for managing the entire multigrid hierarchy.
Definition variables.h:774