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