PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
 
Loading...
Searching...
No Matches
Data Structures | Macros | Enumerations | Functions
poisson.h File Reference

Pressure-Poisson projection: the multigrid pressure-correction solve, the pressure update, and the velocity correction that makes Ucont divergence-free. More...

#include "variables.h"
#include "Boundaries.h"
Include dependency graph for poisson.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  PoissonTransverseDifference
 Transverse difference used at one face. More...
 
struct  PoissonFaceMetrics
 Read-only face metric arrays: metric[n][b] is F_b on the n-faces. More...
 

Macros

#define POISSON_SOLID_THRESHOLD   0.1
 Cells whose nvert exceeds this value are solid.
 

Enumerations

enum  PoissonTransverseKind { POISSON_TRANSVERSE_NONE = 0 , POISSON_TRANSVERSE_LOWER , POISSON_TRANSVERSE_UPPER , POISSON_TRANSVERSE_CENTRAL }
 The transverse stencils a face gradient can use; see PoissonOperator_TransverseKind(). More...
 

Functions

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.
 
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.
 
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-pair hi - sum of the row-pair lo) * weight.
 
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(field) of the face gradient defined in poisson.c.
 
PetscErrorCode PoissonOperator_GetFaceMetrics (UserCtx *user, PoissonFaceMetrics *metrics)
 Borrows read access to the face metric arrays of user.
 
PetscErrorCode PoissonOperator_RestoreFaceMetrics (UserCtx *user, PoissonFaceMetrics *metrics)
 Returns the arrays borrowed by PoissonOperator_GetFaceMetrics().
 
PetscErrorCode PoissonSolver_Multigrid (UserMG *usermg)
 Solves the pressure-correction equation for every block with geometric multigrid.
 
PetscErrorCode AssemblePoissonOperator (UserCtx *user)
 Assembles the pressure-correction operator on one multigrid level.
 
PetscErrorCode ComputePoissonRHS (UserCtx *user, Vec B)
 Forms the right-hand side of the pressure-correction equation.
 
PetscErrorCode UpdatePressure (UserCtx *user)
 Adds the pressure correction to the pressure, P += Phi, and refreshes both fields' periodic images and ghosts.
 
PetscErrorCode ProjectVelocity (UserCtx *user)
 Corrects the contravariant flux with the gradient of Phi.
 

Detailed Description

Pressure-Poisson projection: the multigrid pressure-correction solve, the pressure update, and the velocity correction that makes Ucont divergence-free.

The discrete operator, the right-hand side, and the projection gradient are built from one face-gradient definition, so the Laplacian the solver inverts is exactly the divergence of the gradient the projection applies.

Every masked operation reads the solid field from the UserCtx of the level it acts on (lNvert). Solid cells are excluded from the operator, the right-hand side, the projection, and the multigrid transfers; cells next to a solid face use one-sided transverse differences.

Definition in file poisson.h.


Data Structure Documentation

◆ PoissonTransverseDifference

struct PoissonTransverseDifference

Transverse difference used at one face.

The difference is weight * (sum of Phi on row hi - sum of Phi on row lo), where a row is the pair of cells on either side of the face, displaced by the given offset along the transverse axis.

Definition at line 38 of file poisson.h.

Data Fields
PetscInt lo Transverse offset of the subtracted row pair.
PetscInt hi Transverse offset of the added row pair.
PetscReal weight 0.25 central, 0.5 one-sided, 0 when no fluid side remains.

◆ PoissonFaceMetrics

struct PoissonFaceMetrics

Read-only face metric arrays: metric[n][b] is F_b on the n-faces.

Definition at line 45 of file poisson.h.

Collaboration diagram for PoissonFaceMetrics:
[legend]
Data Fields
const Cmpnts *** metric[3][3]
const PetscReal *** aj[3]

Macro Definition Documentation

◆ POISSON_SOLID_THRESHOLD

#define POISSON_SOLID_THRESHOLD   0.1

Cells whose nvert exceeds this value are solid.

Definition at line 29 of file poisson.h.

Enumeration Type Documentation

◆ PoissonTransverseKind

The transverse stencils a face gradient can use; see PoissonOperator_TransverseKind().

Enumerator
POISSON_TRANSVERSE_NONE 

Neither side available: the transverse term vanishes.

POISSON_TRANSVERSE_LOWER 

One-sided over rows -1 and 0, weight 0.5.

POISSON_TRANSVERSE_UPPER 

One-sided over rows 0 and +1, weight 0.5.

POISSON_TRANSVERSE_CENTRAL 

Central over rows -1 and +1, weight 0.25.

Definition at line 51 of file poisson.h.

51 {
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. */
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

Function Documentation

◆ PoissonOperator_AtBN()

static PetscReal PoissonOperator_AtBN ( const PetscReal ***  field,
const PetscInt  c[3],
PetscInt  b,
PetscInt  p,
PetscInt  n,
PetscInt  q 
)
inlinestatic

Value of a cell-centred array at c + p e_b + q e_n.

With b, n, p and q constants every offset is a constant, so the per-face stencils below compile to straight-line loads.

Parameters
[in]fieldGhosted cell-centred array, indexed [k][j][i].
[in]cCell (i, j, k).
[in]bFirst axis.
[in]pDisplacement along b.
[in]nSecond axis.
[in]qDisplacement along n.
Returns
The value at the displaced cell.

Definition at line 72 of file poisson.h.

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}
Here is the caller graph for this function:

◆ PoissonOperator_TransverseKind()

static PoissonTransverseKind PoissonOperator_TransverseKind ( const PetscReal ***  nvert,
const PetscInt  c[3],
PetscInt  n,
PetscInt  t,
const PetscInt  m[3],
const PetscBool  periodic[3] 
)
inlinestatic

Chooses the transverse stencil along axis t at the face between cell c and c + e_n.

The central difference averages the two cells beside the face over rows -1 and +1. When the +1 row is a non-periodic boundary layer or touches a solid cell, the difference falls back to rows -1 and 0, and symmetrically to rows 0 and +1; with neither side available the transverse term vanishes.

Parameters
[in]nvertGhosted solid field.
[in]cCell on the low side of the face.
[in]nFace orientation (0, 1, 2 for i, j, k).
[in]tTransverse axis, not n.
[in]mGrid sizes (mx, my, mz).
[in]periodicWhether each axis is periodic.
Returns
The stencil to use.

Definition at line 95 of file poisson.h.

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}
#define POISSON_SOLID_THRESHOLD
Cells whose nvert exceeds this value are solid.
Definition poisson.h:29
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PoissonOperator_TransverseFieldDifference()

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] 
)
inlinestatic

Transverse difference of field along axis t at the face between cell c and c + e_n: (sum of the row-pair hi - sum of the row-pair lo) * weight.

Parameters
[in]nvertGhosted solid field.
[in]fieldGhosted cell-centred field.
[in]cCell on the low side of the face.
[in]nFace orientation (0, 1, 2 for i, j, k).
[in]tTransverse axis, not n.
[in]mGrid sizes (mx, my, mz).
[in]periodicWhether each axis is periodic.
Returns
The weighted difference, 0 when no side is available.

Definition at line 126 of file poisson.h.

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}
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PoissonOperator_FaceGradientFlux()

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] 
)
inlinestatic

Gradient flux of a cell-centred field through the face between cell c and c + e_n: sum_b g_nb D_b(field) of the face gradient defined in poisson.c.

The projection applies it to Phi; the momentum residual applies it to P, so both use the one discrete gradient.

Parameters
[in]metricsFace metric arrays from PoissonOperator_GetFaceMetrics().
[in]nvertGhosted solid field.
[in]fieldGhosted cell-centred field whose gradient is taken.
[in]cCell on the low side of the face.
[in]nFace orientation (0, 1, 2 for i, j, k).
[in]mGrid sizes (mx, my, mz).
[in]periodicWhether each axis is periodic.
Returns
The gradient flux through the face.

Definition at line 162 of file poisson.h.

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}
const PetscReal *** aj[3]
Definition poisson.h:47
const Cmpnts *** metric[3][3]
Definition poisson.h:46
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
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PoissonOperator_GetFaceMetrics()

PetscErrorCode PoissonOperator_GetFaceMetrics ( UserCtx *  user,
PoissonFaceMetrics *  metrics 
)

Borrows read access to the face metric arrays of user.

Parameters
[in]userBlock context whose ghosted face metrics are read.
[out]metricsFace metric arrays; return them with PoissonOperator_RestoreFaceMetrics().
Returns
PetscErrorCode 0 on success.

Borrows read access to the face metric arrays of user.

Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/poisson.h.

See also
PoissonOperator_GetFaceMetrics()

Definition at line 147 of file poisson.c.

148{
149 FieldView view;
150
151 PetscFunctionBeginUser;
153 for (PetscInt n = 0; n < 3; n++) {
154 for (PetscInt b = 0; b < 3; b++) {
155 PetscCall(FieldGetView(user, POISSON_FACE_METRIC_FIELDS[n][b], &view));
156 PetscCall(DMDAVecGetArrayRead(view.dm, view.local_vec, (void *)&metrics->metric[n][b]));
157 }
158 PetscCall(FieldGetView(user, POISSON_FACE_AJ_FIELDS[n], &view));
159 PetscCall(DMDAVecGetArrayRead(view.dm, view.local_vec, (void *)&metrics->aj[n]));
160 }
162 PetscFunctionReturn(0);
163}
PetscErrorCode FieldGetView(UserCtx *user, FieldId field_id, FieldView *view)
Resolve the existing DM and global/local vectors for one field.
Non-owning runtime objects resolved for one field and UserCtx.
#define PROFILE_FUNCTION_END
Marks the end of a profiled code block.
Definition logging.h:1029
#define PROFILE_FUNCTION_BEGIN
Marks the beginning of a profiled function: an always-on aggregate timer.
Definition logging.h:1000
static const FieldId POISSON_FACE_AJ_FIELDS[3]
Definition poisson.c:43
static const FieldId POISSON_FACE_METRIC_FIELDS[3][3]
Field IDs of the face metric vectors, indexed as PoissonFaceMetrics.
Definition poisson.c:38
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PoissonOperator_RestoreFaceMetrics()

PetscErrorCode PoissonOperator_RestoreFaceMetrics ( UserCtx *  user,
PoissonFaceMetrics *  metrics 
)

Returns the arrays borrowed by PoissonOperator_GetFaceMetrics().

Parameters
[in]userBlock context the arrays were borrowed from.
[in,out]metricsFace metric arrays to return.
Returns
PetscErrorCode 0 on success.

Returns the arrays borrowed by PoissonOperator_GetFaceMetrics().

Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/poisson.h.

See also
PoissonOperator_RestoreFaceMetrics()

Definition at line 171 of file poisson.c.

172{
173 FieldView view;
174
175 PetscFunctionBeginUser;
177 for (PetscInt n = 0; n < 3; n++) {
178 for (PetscInt b = 0; b < 3; b++) {
179 PetscCall(FieldGetView(user, POISSON_FACE_METRIC_FIELDS[n][b], &view));
180 PetscCall(DMDAVecRestoreArrayRead(view.dm, view.local_vec, (void *)&metrics->metric[n][b]));
181 }
182 PetscCall(FieldGetView(user, POISSON_FACE_AJ_FIELDS[n], &view));
183 PetscCall(DMDAVecRestoreArrayRead(view.dm, view.local_vec, (void *)&metrics->aj[n]));
184 }
186 PetscFunctionReturn(0);
187}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PoissonSolver_Multigrid()

PetscErrorCode PoissonSolver_Multigrid ( UserMG *  usermg)
extern

Solves the pressure-correction equation for every block with geometric multigrid.

On the first call for a block, assembles the operator on every multigrid level and builds the outer Krylov solver with its PCMG preconditioner, grid transfers, level smoothers, coarse solve, and null space. The solver is kept in the finest level's UserCtx::ksp and reused on every later call; it is destroyed with the context.

Each call refreshes the ghosted contravariant flux, forms the right-hand side, solves for Phi on the finest level, and appends the iteration history to Poisson_Solver_Convergence_History_Block_<bi>.log in the run's log directory.

Solver controls are read from PETSc options under the ps_ prefix (-ps_ksp_*, -ps_mg_levels_N_*, -ps_mg_coarse_*) when the solver is built.

Parameters
[in,out]usermgMultigrid hierarchy; the finest level's Phi receives the solution.
Returns
PETSc error code. A non-finite residual or a preconditioner that could not be built stops the run with PETSC_ERR_NOT_CONVERGED.

Solves the pressure-correction equation for every block with geometric multigrid.

Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/poisson.h.

See also
PoissonSolver_Multigrid()

Definition at line 922 of file poisson.c.

923{
924 SimCtx *simCtx = usermg->mgctx[0].user[0].simCtx;
925 const FieldId staggered_fields[] = {FIELD_ID_UCONT};
926
927 PetscFunctionBeginUser;
929 LOG_ALLOW(GLOBAL, LOG_INFO, "Starting Multigrid Poisson Solve...\n");
930
931 for (PetscInt bi = 0; bi < simCtx->block_number; bi++) {
932 UserCtx *user = &usermg->mgctx[usermg->mglevels - 1].user[bi];
933 KSPConvergedReason reason;
934
935 if (!user->ksp) PetscCall(PoissonMultigrid_Build(usermg, bi));
936
937 PetscCall(SynchronizePeriodicStaggeredFields(user, 1, staggered_fields));
938 PetscCall(ComputePoissonRHS(user, user->B));
939
940 PetscCall(PoissonMultigrid_OpenConvergenceLog(user->ksp, simCtx, bi));
941 PetscCall(KSPSolve(user->ksp, user->B, user->Phi));
943 /* KSPSolve projects only the right-hand side. The residual cannot see a constant in
944 Phi, so iterations near the round-off floor can leave one, and P += Phi would add it
945 up step after step. Projecting the solution the same way fixes the pressure gauge. */
946 PetscCall(MatNullSpaceRemove(user->nullsp, user->Phi));
947
948 /* A non-finite residual or a preconditioner that could not be built leaves Phi
949 meaningless, and the projection would carry it into the next momentum step,
950 where it surfaces as a failure of the wrong solver. Stopping at max_it is this
951 solve's normal mode and stays silent; any other divergence is reported, because
952 the projection proceeds on that Phi. */
953 PetscCall(KSPGetConvergedReason(user->ksp, &reason));
954 PetscCheck(reason != KSP_DIVERGED_NANORINF && reason != KSP_DIVERGED_PC_FAILED,
955 PETSC_COMM_WORLD, PETSC_ERR_NOT_CONVERGED,
956 "Pressure Poisson solve on block %" PetscInt_FMT " failed at step %" PetscInt_FMT
957 " (KSP reason %s). Known causes: a multigrid hierarchy coarsened too far "
958 "(reduce poisson_solver.multigrid.levels or refine the grid), or a momentum "
959 "field that has already diverged, such as an explicit time step beyond its "
960 "stability limit.",
961 bi, simCtx->step, KSPConvergedReasons[reason]);
962 if (reason < 0 && reason != KSP_DIVERGED_ITS) {
963 LOG(GLOBAL, LOG_WARNING, "Pressure Poisson solve on block %" PetscInt_FMT
964 " diverged at step %" PetscInt_FMT " (KSP reason %s); the projection uses the last iterate.\n",
965 bi, simCtx->step, KSPConvergedReasons[reason]);
966 }
967 }
968
969 LOG_ALLOW(GLOBAL, LOG_INFO, "Multigrid Poisson Solve complete.\n");
971 PetscFunctionReturn(0);
972}
PetscErrorCode SynchronizePeriodicStaggeredFields(UserCtx *user, PetscInt num_fields, const FieldId field_ids[])
Synchronizes persistent component-staggered vector fields.
FieldId
Compile-time identity for a catalogued Eulerian field.
@ FIELD_ID_UCONT
#define GLOBAL
Scope for global logging across all processes.
Definition logging.h:46
#define LOG_ALLOW(scope, level, fmt,...)
Logging macro that checks both the log level and whether the calling function is in the allowed-funct...
Definition logging.h:214
#define LOG(scope, level, fmt,...)
Logging macro for PETSc-based applications with scope control.
Definition logging.h:86
@ LOG_INFO
Informational messages about program execution.
Definition logging.h:31
@ LOG_WARNING
Non-critical issues that warrant attention.
Definition logging.h:30
static PetscErrorCode PoissonMultigrid_CloseConvergenceLog(KSP ksp)
Closes the log file opened by PoissonMultigrid_OpenConvergenceLog().
Definition poisson.c:901
static PetscErrorCode PoissonMultigrid_OpenConvergenceLog(KSP ksp, SimCtx *simCtx, PetscInt bi)
Prepares the convergence monitor for one solve and opens its log file on rank 0.
Definition poisson.c:872
PetscErrorCode ComputePoissonRHS(UserCtx *user, Vec B)
Implementation of ComputePoissonRHS().
Definition poisson.c:344
static PetscErrorCode PoissonMultigrid_Build(UserMG *usermg, PetscInt bi)
Builds the multigrid solver for block bi and stores it in the finest level.
Definition poisson.c:762
UserCtx * user
Definition variables.h:768
MatNullSpace nullsp
Definition variables.h:1228
PetscInt block_number
Definition variables.h:1009
SimCtx * simCtx
Back-pointer to the master simulation context.
Definition variables.h:1160
PetscInt mglevels
Definition variables.h:775
PetscInt step
Definition variables.h:924
MGCtx * mgctx
Definition variables.h:778
The master context for the entire simulation.
Definition variables.h:916
User-defined context containing data specific to a single computational grid level.
Definition variables.h:1157
Here is the call graph for this function:
Here is the caller graph for this function:

◆ AssemblePoissonOperator()

PetscErrorCode AssemblePoissonOperator ( UserCtx *  user)
extern

Assembles the pressure-correction operator on one multigrid level.

Allocates user->A on the first call and reassembles its entries on later calls into the same nonzero structure. Fluid rows carry the 19-point curvilinear Laplacian; dummy rows on the domain boundary and solid rows are identities. Non-periodic faces are homogeneous Neumann; periodic faces wrap to the opposite interior layer.

Parameters
[in,out]userLevel context supplying metrics, lNvert, and boundary types.
Returns
PETSc error code.

Assembles the pressure-correction operator on one multigrid level.

Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/poisson.h.

See also
AssemblePoissonOperator()

Definition at line 244 of file poisson.c.

245{
246 const DMDALocalInfo info = user->info;
247 const PetscInt m[3] = {info.mx, info.my, info.mz};
248 PetscBool periodic[3];
249 PoissonFaceMetrics metrics;
250 const PetscReal ***nvert, ***aj;
251 AO ao;
252
253 PetscFunctionBeginUser;
255
256 if (!user->A) {
257 PetscInt local_rows;
258 PetscCall(VecGetLocalSize(user->Phi, &local_rows));
259 PetscCall(MatCreateAIJ(PETSC_COMM_WORLD, local_rows, local_rows, m[0] * m[1] * m[2], m[0] * m[1] * m[2],
260 19, NULL, 19, NULL, &user->A));
261 }
262 PetscCall(MatZeroEntries(user->A));
263
264 GetPeriodicAxes(user->simCtx, periodic);
265 PetscCall(DMDAGetAO(user->da, &ao));
266 PetscCall(PoissonOperator_GetFaceMetrics(user, &metrics));
267 PetscCall(DMDAVecGetArrayRead(user->da, user->lNvert, (void *)&nvert));
268 PetscCall(DMDAVecGetArrayRead(user->da, user->lAj, (void *)&aj));
269
270 for (PetscInt k = info.zs; k < info.zs + info.zm; k++) {
271 for (PetscInt j = info.ys; j < info.ys + info.ym; j++) {
272 for (PetscInt i = info.xs; i < info.xs + info.xm; i++) {
273 const PetscInt c[3] = {i, j, k};
274 PetscInt row = i + j * m[0] + k * m[0] * m[1];
275 PetscInt columns[19];
276 PetscScalar coefficients[19] = {0.0};
277
278 PetscCall(AOApplicationToPetsc(ao, 1, &row));
279 if (i == 0 || i == m[0] - 1 || j == 0 || j == m[1] - 1 || k == 0 || k == m[2] - 1) {
280 const PetscScalar one = 1.0;
281 PetscCall(MatSetValues(user->A, 1, &row, 1, &row, &one, INSERT_VALUES));
282 continue;
283 }
284
285 for (PetscInt s = 0; s < 19; s++) {
286 const PetscInt *d = POISSON_STENCIL_OFFSETS[s];
287 columns[s] = PoissonOperator_NeighborIndex(i, d[0], m[0], periodic[0]) +
288 PoissonOperator_NeighborIndex(j, d[1], m[1], periodic[1]) * m[0] +
289 PoissonOperator_NeighborIndex(k, d[2], m[2], periodic[2]) * m[0] * m[1];
290 }
291 PetscCall(AOApplicationToPetsc(ao, 19, columns));
292
293 if (nvert[k][j][i] > POISSON_SOLID_THRESHOLD) {
294 /* Solid rows keep the fluid row's structure with zero couplings, so a
295 solid field that changes during a run reassembles in place. */
296 coefficients[0] = 1.0;
297 PetscCall(MatSetValues(user->A, 1, &row, 19, columns, coefficients, INSERT_VALUES));
298 continue;
299 }
300
301 /* Faces in the order east, west, north, south, top, bottom. Non-periodic
302 boundary faces carry no flux (homogeneous Neumann). */
303 for (PetscInt n = 0; n < 3; n++) {
304 const PetscInt first = periodic[n] ? 0 : 1;
305 const PetscInt last = periodic[n] ? m[n] - 1 : m[n] - 2;
306 for (PetscInt side = 0; side < 2; side++) {
307 PetscInt across[3] = {0, 0, 0}, own[3] = {0, 0, 0}, face_cell[3] = {i, j, k};
308 const PetscBool upper = (PetscBool)(side == 0);
309
310 across[n] = upper ? 1 : -1;
311 if (PoissonOperator_At(nvert, c, across) >= POISSON_SOLID_THRESHOLD) continue;
312 if (c[n] == (upper ? last : first)) continue;
313 if (!upper) { own[n] = -1; face_cell[n] -= 1; }
314
315 const PoissonFaceGradient face =
316 PoissonOperator_FaceGradientStencil(&metrics, nvert, face_cell, n, m, periodic);
317 PoissonOperator_AddFaceFlux(&face, own, n, upper ? 1.0 : -1.0, coefficients);
318 }
319 }
320
321 for (PetscInt s = 0; s < 19; s++) coefficients[s] *= -aj[k][j][i];
322 PetscCall(MatSetValues(user->A, 1, &row, 19, columns, coefficients, INSERT_VALUES));
323 }
324 }
325 }
326
327 PetscCall(DMDAVecRestoreArrayRead(user->da, user->lAj, (void *)&aj));
328 PetscCall(DMDAVecRestoreArrayRead(user->da, user->lNvert, (void *)&nvert));
329 PetscCall(PoissonOperator_RestoreFaceMetrics(user, &metrics));
330 PetscCall(MatAssemblyBegin(user->A, MAT_FINAL_ASSEMBLY));
331 PetscCall(MatAssemblyEnd(user->A, MAT_FINAL_ASSEMBLY));
332
333 LOG_ALLOW(GLOBAL, LOG_DEBUG, "Poisson operator assembled on level %" PetscInt_FMT ".\n", user->thislevel);
335 PetscFunctionReturn(0);
336}
@ LOG_DEBUG
Detailed debugging information.
Definition logging.h:32
PetscErrorCode PoissonOperator_RestoreFaceMetrics(UserCtx *user, PoissonFaceMetrics *metrics)
Implementation of PoissonOperator_RestoreFaceMetrics().
Definition poisson.c:171
static PetscInt PoissonOperator_NeighborIndex(PetscInt v, PetscInt d, PetscInt m, PetscBool periodic)
Index of the neighbour at offset d (-1, 0, +1) from v on an axis of m points, wrapping between the in...
Definition poisson.c:193
static PICURV_ALWAYS_INLINE PetscReal PoissonOperator_At(const PetscReal ***field, const PetscInt c[3], const PetscInt d[3])
Value of a cell-centred array at cell c displaced by d.
Definition poisson.c:60
static const PetscInt POISSON_STENCIL_OFFSETS[19][3]
Offsets of the 19 stencil points, in the column order used to insert each row.
Definition poisson.c:27
static void PoissonOperator_AddFaceFlux(const PoissonFaceGradient *face, const PetscInt own[3], PetscInt n, PetscReal sign, PetscScalar coefficients[19])
Adds the signed gradient flux through one face to a row's stencil coefficients.
Definition poisson.c:209
PetscErrorCode PoissonOperator_GetFaceMetrics(UserCtx *user, PoissonFaceMetrics *metrics)
Implementation of PoissonOperator_GetFaceMetrics().
Definition poisson.c:147
static PICURV_ALWAYS_INLINE PoissonFaceGradient PoissonOperator_FaceGradientStencil(const PoissonFaceMetrics *metrics, const PetscReal ***nvert, const PetscInt c[3], PetscInt n, const PetscInt m[3], const PetscBool periodic[3])
Gathers the metric coefficients and transverse differences of the gradient flux through the face betw...
Definition poisson.c:105
Everything needed to evaluate the gradient flux through one face.
Definition poisson.c:46
Read-only face metric arrays: metric[n][b] is F_b on the n-faces.
Definition poisson.h:45
static void GetPeriodicAxes(const SimCtx *simCtx, PetscBool periodic[3])
Which grid axes are periodic.
Definition setup.h:361
Vec lNvert
Definition variables.h:1197
PetscInt thislevel
Definition variables.h:1269
DMDALocalInfo info
Definition variables.h:1169
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ComputePoissonRHS()

PetscErrorCode ComputePoissonRHS ( UserCtx *  user,
Vec  B 
)
extern

Forms the right-hand side of the pressure-correction equation.

Writes the scaled divergence of the ghosted contravariant flux lUcont into B, zero on dummy and solid cells, and stores its domain integral in SimCtx::poissonSourceImbalance.

Parameters
[in]userFinest-level context.
[out]BRight-hand-side vector on user->da.
Returns
PETSc error code.

Forms the right-hand side of the pressure-correction equation.

Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/poisson.h.

See also
ComputePoissonRHS()

Definition at line 344 of file poisson.c.

345{
346 SimCtx *simCtx = user->simCtx;
347 const DMDALocalInfo info = user->info;
348 const PetscInt mx = info.mx, my = info.my, mz = info.mz;
349 const PetscReal dt = simCtx->dt;
350 const Cmpnts ***ucont;
351 const PetscReal ***nvert, ***aj;
352 PetscReal ***rhs;
353 PetscReal local_sum = 0.0, global_sum = 0.0;
354
355 PetscFunctionBeginUser;
357 PetscCall(DMDAVecGetArray(user->da, B, &rhs));
358 PetscCall(DMDAVecGetArrayRead(user->fda, user->lUcont, (void *)&ucont));
359 PetscCall(DMDAVecGetArrayRead(user->da, user->lNvert, (void *)&nvert));
360 PetscCall(DMDAVecGetArrayRead(user->da, user->lAj, (void *)&aj));
361
362 for (PetscInt k = info.zs; k < info.zs + info.zm; k++) {
363 for (PetscInt j = info.ys; j < info.ys + info.ym; j++) {
364 for (PetscInt i = info.xs; i < info.xs + info.xm; i++) {
365 if (i == 0 || i == mx - 1 || j == 0 || j == my - 1 || k == 0 || k == mz - 1 ||
366 nvert[k][j][i] > POISSON_SOLID_THRESHOLD) {
367 rhs[k][j][i] = 0.0;
368 } else {
369 rhs[k][j][i] = -(ucont[k][j][i].x - ucont[k][j][i-1].x +
370 ucont[k][j][i].y - ucont[k][j-1][i].y +
371 ucont[k][j][i].z - ucont[k-1][j][i].z) / dt * aj[k][j][i] * COEF_TIME_ACCURACY;
372 }
373 }
374 }
375 }
376
377 /* The integral of the right-hand side is the net volume flux into the domain carried
378 by the uncorrected velocity. With Neumann pressure boundaries it must vanish for
379 the equation to have a solution. */
380 for (PetscInt k = info.zs; k < info.zs + info.zm; k++) {
381 for (PetscInt j = info.ys; j < info.ys + info.ym; j++) {
382 for (PetscInt i = info.xs; i < info.xs + info.xm; i++) {
383 local_sum += rhs[k][j][i] / aj[k][j][i] * dt / COEF_TIME_ACCURACY;
384 }
385 }
386 }
387 PetscCallMPI(MPI_Allreduce(&local_sum, &global_sum, 1, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)B)));
388 simCtx->poissonSourceImbalance = global_sum;
389 LOG_ALLOW(GLOBAL, LOG_INFO, "Poisson source imbalance: %le\n", (double)global_sum);
390
391 PetscCall(DMDAVecRestoreArrayRead(user->da, user->lAj, (void *)&aj));
392 PetscCall(DMDAVecRestoreArrayRead(user->da, user->lNvert, (void *)&nvert));
393 PetscCall(DMDAVecRestoreArrayRead(user->fda, user->lUcont, (void *)&ucont));
394 PetscCall(DMDAVecRestoreArray(user->da, B, &rhs));
396 PetscFunctionReturn(0);
397}
PetscReal poissonSourceImbalance
Definition variables.h:1095
PetscReal dt
Definition variables.h:933
Vec lUcont
Definition variables.h:1197
#define COEF_TIME_ACCURACY
Coefficient controlling the temporal accuracy scheme (e.g., 1.5 for 2nd Order Backward Difference).
Definition variables.h:96
Here is the caller graph for this function:

◆ UpdatePressure()

PetscErrorCode UpdatePressure ( UserCtx *  user)
extern

Adds the pressure correction to the pressure, P += Phi, and refreshes both fields' periodic images and ghosts.

Parameters
[in,out]userBlock context holding P and Phi.
Returns
PETSc error code.

Adds the pressure correction to the pressure, P += Phi, and refreshes both fields' periodic images and ghosts.

Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/poisson.h.

See also
UpdatePressure()

Definition at line 405 of file poisson.c.

406{
407 const FieldId periodic_fields[] = {FIELD_ID_P, FIELD_ID_PHI};
408
409 PetscFunctionBeginUser;
411 PetscCall(VecAXPY(user->P, 1.0, user->Phi));
412 PetscCall(SynchronizePeriodicCellFields(user, 2, periodic_fields));
413 PetscCall(UpdateLocalGhosts(user, FIELD_ID_P));
414 PetscCall(UpdateLocalGhosts(user, FIELD_ID_PHI));
416 PetscFunctionReturn(0);
417}
PetscErrorCode SynchronizePeriodicCellFields(UserCtx *user, PetscInt num_fields, const FieldId field_ids[])
Synchronizes periodic endpoint cells for a list of cell-centered fields.
@ FIELD_ID_PHI
@ FIELD_ID_P
PetscErrorCode UpdateLocalGhosts(UserCtx *user, FieldId field_id)
Updates the local vector (including ghost points) from its corresponding global vector.
Definition setup.c:2593
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ProjectVelocity()

PetscErrorCode ProjectVelocity ( UserCtx *  user)
extern

Corrects the contravariant flux with the gradient of Phi.

Subtracts dt / COEF_TIME_ACCURACY times the face pressure-gradient flux from every fluid face of Ucont, then refreshes the periodic images, reconstructs the Cartesian velocity, and finalizes the cell fields that depend on it.

Parameters
[in,out]userBlock context; reads the ghosted lPhi.
Returns
PETSc error code.

Corrects the contravariant flux with the gradient of Phi.

Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/poisson.h.

See also
ProjectVelocity()

Definition at line 425 of file poisson.c.

426{
427 SimCtx *simCtx = user->simCtx;
428 const DMDALocalInfo info = user->info;
429 const PetscInt m[3] = {info.mx, info.my, info.mz};
430 const PetscInt start[3] = {info.xs, info.ys, info.zs};
431 const PetscReal scale = simCtx->dt / COEF_TIME_ACCURACY;
432 const FieldId staggered_fields[] = {FIELD_ID_UCONT};
433 PetscBool periodic[3];
434 PetscInt interior_start[3], interior_end[3];
435 PoissonFaceMetrics metrics;
436 const PetscReal ***nvert, ***phi;
437 Cmpnts ***ucont;
438
439 PetscFunctionBeginUser;
441 GetPeriodicAxes(user->simCtx, periodic);
442 GetOwnedInteriorCellBounds(&info, interior_start, interior_end);
443
444 PetscCall(PoissonOperator_GetFaceMetrics(user, &metrics));
445 PetscCall(DMDAVecGetArrayRead(user->da, user->lNvert, (void *)&nvert));
446 PetscCall(DMDAVecGetArrayRead(user->da, user->lPhi, (void *)&phi));
447 PetscCall(DMDAVecGetArray(user->fda, user->Ucont, &ucont));
448
449 /* One pass per face orientation. Faces between two interior cells are corrected on
450 every axis; a periodic axis also corrects its seam face at index 0. */
451 for (PetscInt n = 0; n < 3; n++) {
452 PetscInt lo[3], hi[3];
453 for (PetscInt a = 0; a < 3; a++) { lo[a] = interior_start[a]; hi[a] = interior_end[a]; }
454 if (periodic[n] && start[n] == 0) lo[n] = 0;
455 hi[n] = PetscMin(hi[n], periodic[n] ? m[n] - 1 : m[n] - 2);
456
457 for (PetscInt k = lo[2]; k < hi[2]; k++) {
458 for (PetscInt j = lo[1]; j < hi[1]; j++) {
459 for (PetscInt i = lo[0]; i < hi[0]; i++) {
460 const PetscInt c[3] = {i, j, k};
461 PetscInt across[3] = {0, 0, 0};
462
463 across[n] = 1;
464 if (nvert[k][j][i] > POISSON_SOLID_THRESHOLD ||
465 PoissonOperator_At(nvert, c, across) > POISSON_SOLID_THRESHOLD) continue;
466
467 const PetscReal flux = PoissonOperator_FaceGradientFlux(&metrics, nvert, phi, c, n, m, periodic);
468 const PetscReal correction = flux * scale;
469 if (n == 0) ucont[k][j][i].x -= correction;
470 else if (n == 1) ucont[k][j][i].y -= correction;
471 else ucont[k][j][i].z -= correction;
472 }
473 }
474 }
475 }
476
477 PetscCall(DMDAVecRestoreArray(user->fda, user->Ucont, &ucont));
478 PetscCall(DMDAVecRestoreArrayRead(user->da, user->lPhi, (void *)&phi));
479 PetscCall(DMDAVecRestoreArrayRead(user->da, user->lNvert, (void *)&nvert));
480 PetscCall(PoissonOperator_RestoreFaceMetrics(user, &metrics));
481
482 PetscCall(SynchronizePeriodicStaggeredFields(user, 1, staggered_fields));
483 PetscCall(Contra2Cart(user));
484 PetscCall(FinalizePostProjectionCellFields(user));
486 PetscFunctionReturn(0);
487}
PetscErrorCode FinalizePostProjectionCellFields(UserCtx *user)
Finalizes cell-centered fields after the projection step.
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
static void GetOwnedInteriorCellBounds(const DMDALocalInfo *info, PetscInt lo[3], PetscInt hi[3])
Index bounds of the interior cells this rank owns in the solver's cell-centred layout: the owned rang...
Definition setup.h:378
PetscErrorCode Contra2Cart(UserCtx *user)
Reconstructs Cartesian velocity (Ucat) at cell centers from contravariant velocity (Ucont) defined on...
Definition setup.c:3313
Vec Ucont
Definition variables.h:1197
Here is the call graph for this function:
Here is the caller graph for this function: