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" // Provides definitions for UserCtx, UserMG, Vec, Mat, etc.
5#include "Metric.h" // Provides some primitives necessary.
6#include "Boundaries.h"
7/*================================================================================*
8 * HIGH-LEVEL POISSON SOLVER *
9 *================================================================================*/
10
11/**
12 * @brief Solves the pressure-Poisson equation using a geometric multigrid method.
13 *
14 * This function orchestrates the entire multigrid V-cycle for the pressure
15 * correction equation. It assembles the Laplacian matrix on all grid levels,
16 * sets up the KSP solvers, smoothers, restriction/interpolation operators,
17 * and executes the solve.
18 *
19 * @param usermg The UserMG context containing the entire multigrid hierarchy.
20 * @return PetscErrorCode 0 on success.
21 *
22 * @note Testing status:
23 * This routine is exercised in runtime smoke, but still needs deeper
24 * direct bespoke coverage for debugging and branch isolation.
25 */
26extern PetscErrorCode PoissonSolver_MG(UserMG *usermg);
27
28
29/*================================================================================*
30 * CORE COMPONENTS OF THE POISSON SOLVER *
31 *================================================================================*/
32
33/**
34 * @brief Assembles the Left-Hand-Side (LHS) matrix (Laplacian operator) for the
35 * Poisson equation on a single grid level.
36 *
37 * @param user The UserCtx for the grid level on which to assemble the matrix.
38 * @return PetscErrorCode 0 on success.
39 *
40 * @note Testing status:
41 * Direct unit coverage exists for core operator assembly, but periodic and
42 * immersed-boundary stencil branches remain thinner than the Cartesian baseline.
43 */
44extern PetscErrorCode PoissonLHSNew(UserCtx *user);
45
46/**
47 * @brief Computes the Right-Hand-Side (RHS) of the Poisson equation, which is
48 * the divergence of the intermediate velocity field.
49 *
50 * @param user The UserCtx for the grid level.
51 * @param B The PETSc Vec where the RHS result will be stored.
52 * @return PetscErrorCode 0 on success.
53 */
54extern PetscErrorCode PoissonRHS(UserCtx *user, Vec B);
55
56/**
57 * @brief Updates the pressure field `P` with the pressure correction `Phi`
58 * computed by the Poisson solver. (P = P + Phi)
59 *
60 * @param user The UserCtx containing the P and Phi vectors.
61 * @return PetscErrorCode 0 on success.
62 */
63extern PetscErrorCode UpdatePressure(UserCtx *user);
64
65/**
66 * @brief Enforces a constant volumetric flux profile along the entire length of a driven periodic channel.
67 *
68 * This function is a "hard" corrector, called at the end of the projection step.
69 * The projection ensures the velocity field is divergence-free (3D continuity), but this
70 * function enforces a stricter 1D continuity condition (`Flux(plane) = constant`)
71 * required for physically realistic, fully-developed periodic channel/pipe flow.
72 *
73 * The process is as follows:
74 * 1. Introspects the boundary condition handlers to detect if a `DRIVEN_` flow is active
75 * and in which direction ('X', 'Y', or 'Z'). If none is found, it exits.
76 * 2. Measures the current volumetric flux through *every single cross-sectional plane*
77 * in the driven direction.
78 * 3. For each plane, it calculates the velocity correction required to make its flux
79 * match the global `targetVolumetricFlux` (which was set by the controller).
80 * 4. It applies this spatially-uniform (but plane-dependent) velocity correction directly
81 * to the `ucont` field, ensuring `Flux(plane) = TargetFlux` for all planes.
82 *
83 * @param user The UserCtx containing the simulation state for a single block.
84 * @return PetscErrorCode 0 on success.
85 */
86PetscErrorCode CorrectChannelFluxProfile(UserCtx *user);
87
88/**
89 * @brief Corrects the contravariant velocity field `Ucont` to be divergence-free
90 * using the gradient of the pressure correction field `Phi`.
91 *
92 * @param user The UserCtx containing the Ucont and Phi vectors.
93 * @return PetscErrorCode 0 on success.
94 *
95 * @note Testing status:
96 * Direct unit coverage exists for basic projection invariants; periodic
97 * and immersed-boundary correction branches remain part of the next-gap backlog.
98 */
99extern PetscErrorCode Projection(UserCtx *user);
100
101
102/*================================================================================*
103 * MULTIGRID & NULL SPACE HELPERS *
104 *================================================================================*/
105
106/**
107 * @brief The callback function for PETSc's MatNullSpace object.
108 *
109 * This function removes the null space from the Poisson solution vector by
110 * ensuring the average pressure is zero, which is necessary for problems with
111 * pure Neumann boundary conditions.
112 *
113 * @param nullsp The MatNullSpace context.
114 * @param X The vector to be corrected.
115 * @param ctx A void pointer to the UserCtx.
116 * @return PetscErrorCode 0 on success.
117 */
118extern PetscErrorCode PoissonNullSpaceFunction(MatNullSpace nullsp, Vec X, void *ctx);
119
120/**
121 * @brief The callback function for the multigrid restriction operator (MatShell).
122 *
123 * Defines the fine-to-coarse grid transfer for the Poisson residual.
124 *
125 * @param A The shell matrix context.
126 * @param X The fine-grid source vector.
127 * @param F The coarse-grid destination vector.
128 * @return PetscErrorCode 0 on success.
129 */
130extern PetscErrorCode MyRestriction(Mat A, Vec X, Vec F);
131
132/**
133 * @brief The callback function for the multigrid interpolation operator (MatShell).
134 *
135 * Defines the coarse-to-fine grid transfer for the pressure correction.
136 *
137 * @param A The shell matrix context.
138 * @param X The coarse-grid source vector.
139 * @param F The fine-grid destination vector.
140 * @return PetscErrorCode 0 on success.
141 */
142extern PetscErrorCode MyInterpolation(Mat A, Vec X, Vec F);
143
144
145/*================================================================================*
146 * IMMERSED BOUNDARY RELATED HELPERS (Optional) *
147 *================================================================================*/
148
149// These functions are called by the Poisson solver but are specific to the
150// immersed boundary method. It is good practice to declare them here as they
151// are part of the Poisson module's dependencies.
152
153/**
154 * @brief Calculates the net flux across the immersed boundary surface.
155 * @param user The UserCtx for the grid level.
156 * @param ibm_Flux (Output) The calculated net flux.
157 * @param ibm_Area (Output) The total surface area of the IB.
158 * @param flg A flag controlling the correction behavior.
159 * @return PetscErrorCode 0 on success.
160 */
161extern PetscErrorCode VolumeFlux(UserCtx *user, PetscReal *ibm_Flux, PetscReal *ibm_Area, PetscInt flg);
162
163/**
164 * @brief A specialized version of VolumeFlux, likely for reversed normals.
165 * @param user The UserCtx for the grid level.
166 * @param ibm_Flux (Output) The calculated net flux.
167 * @param ibm_Area (Output) The total surface area of the IB.
168 * @param flg A flag controlling the correction behavior.
169 * @return PetscErrorCode 0 on success.
170 */
171extern PetscErrorCode VolumeFlux_rev(UserCtx *user, PetscReal *ibm_Flux, PetscReal *ibm_Area, PetscInt flg);
172
173
174#endif // POISSON_H
PetscErrorCode PoissonNullSpaceFunction(MatNullSpace nullsp, Vec X, void *ctx)
The callback function for PETSc's MatNullSpace object.
Definition poisson.c:1031
PetscErrorCode PoissonLHSNew(UserCtx *user)
Assembles the Left-Hand-Side (LHS) matrix (Laplacian operator) for the Poisson equation on a single g...
Definition poisson.c:1532
PetscErrorCode Projection(UserCtx *user)
Corrects the contravariant velocity field Ucont to be divergence-free using the gradient of the press...
Definition poisson.c:328
PetscErrorCode VolumeFlux_rev(UserCtx *user, PetscReal *ibm_Flux, PetscReal *ibm_Area, PetscInt flg)
A specialized version of VolumeFlux, likely for reversed normals.
Definition poisson.c:2230
PetscErrorCode MyRestriction(Mat A, Vec X, Vec F)
The callback function for the multigrid restriction operator (MatShell).
Definition poisson.c:1426
PetscErrorCode UpdatePressure(UserCtx *user)
Updates the pressure field P with the pressure correction Phi computed by the Poisson solver.
Definition poisson.c:854
PetscErrorCode VolumeFlux(UserCtx *user, PetscReal *ibm_Flux, PetscReal *ibm_Area, PetscInt flg)
Calculates the net flux across the immersed boundary surface.
Definition poisson.c:2472
PetscErrorCode PoissonRHS(UserCtx *user, Vec B)
Computes the Right-Hand-Side (RHS) of the Poisson equation, which is the divergence of the intermedia...
Definition poisson.c:2141
PetscErrorCode CorrectChannelFluxProfile(UserCtx *user)
Enforces a constant volumetric flux profile along the entire length of a driven periodic channel.
Definition poisson.c:107
PetscErrorCode MyInterpolation(Mat A, Vec X, Vec F)
The callback function for the multigrid interpolation operator (MatShell).
Definition poisson.c:1233
PetscErrorCode PoissonSolver_MG(UserMG *usermg)
Solves the pressure-Poisson equation using a geometric multigrid method.
Definition poisson.c:3344
Main header file for a complex fluid dynamics solver.
User-defined context containing data specific to a single computational grid level.
Definition variables.h:811
User-level context for managing the entire multigrid hierarchy.
Definition variables.h:534