PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
 
Loading...
Searching...
No Matches
Functions
rhs.c File Reference
#include "rhs.h"
#include "les.h"
Include dependency graph for rhs.c:

Go to the source code of this file.

Functions

static PICURV_ALWAYS_INLINE Cmpnts RHSVelocityAtBN (const Cmpnts ***ucat, const PetscInt c[3], PetscInt b, PetscInt p, PetscInt n, PetscInt q)
 Velocity at c + p e_b + q e_n, the Cartesian counterpart of PoissonOperator_AtBN().
 
static Cmpnts RHSVelocityAt (const Cmpnts ***ucat, const PetscInt c[3], PetscInt n, PetscInt d)
 Cartesian velocity at cell c displaced by d along axis n.
 
static Cmpnts RHSQuickFlux (const Cmpnts ***ucat, const PetscInt c[3], PetscInt n, PetscReal ucon, PetscInt m1, PetscInt p2)
 QUICK flux through the face between cell c and c + e_n, upwinded by the sign of ucon.
 
static Cmpnts RHSCentralFlux (const Cmpnts ***ucat, const PetscInt c[3], PetscInt n, PetscReal ucon)
 Central flux through the face between cell c and c + e_n.
 
static Cmpnts RHSConvectiveFaceFlux (const Cmpnts ***ucat, const PetscReal ***nvert, const PetscInt c[3], PetscInt n, PetscReal ucon, PetscInt m, PetscBool periodic, PetscBool central)
 Convective flux of Cartesian momentum through the face between cell c and c + e_n, carried by half the contravariant flux on that face.
 
PetscErrorCode ComputeConvection (UserCtx *user, Vec Ucont, Vec Ucat, Vec Conv)
 Implementation of ComputeConvection().
 
static PICURV_ALWAYS_INLINE Cmpnts RHSTransverseVelocityDifference (const Cmpnts ***ucat, const PetscInt c[3], PetscInt n, PetscInt b, PoissonTransverseDifference diff)
 (u(c+hi e_b+e_n) + u(c+hi e_b) - u(c+lo e_b+e_n) - u(c+lo e_b)) * weight, for diff's rows and weight.
 
static PICURV_ALWAYS_INLINE Cmpnts RHSTransverseFaceDifference (const Cmpnts ***ucat, const PetscReal ***nvert, const PetscInt c[3], PetscInt n, PetscInt b)
 Velocity difference along transverse axis b on the face between cell c and c + e_n: the average over the two cells beside the face, one-sided away from a blanked solid cell.
 
static PICURV_ALWAYS_INLINE void RHSFaceVelocityDifferences (const Cmpnts ***ucat, const PetscReal ***nvert, const PetscInt c[3], PetscInt n, Cmpnts d[3])
 Velocity differences on the face between cell c and c + e_n, one per computational direction: across the face along n, and along each transverse axis.
 
static PICURV_ALWAYS_INLINE Cmpnts RHSViscousFaceFlux (const PoissonFaceMetrics *metrics, const Cmpnts d[3], const PetscInt c[3], PetscInt n, PetscReal nu, PetscReal nu_t, PetscBool les, PetscBool clark)
 Viscous stress through the face between cell c and c + e_n, in Cartesian components, from the face differences d and the face metrics.
 
static PICURV_ALWAYS_INLINE void RHSViscousFluxesAlong (const UserCtx *user, const PoissonFaceMetrics *metrics, const Cmpnts ***ucat, const PetscReal ***nvert, const PetscReal ***lnu_t, const Cmpnts ***lwall_stress, const PetscInt lo[3], const PetscInt hi[3], const PetscInt m[3], PetscInt n, PetscBool les, PetscBool clark, Cmpnts ***fp)
 Viscous stresses on every face of orientation n of the owned cells, from the face below the first cell; n is a constant at each call, so each expands to its own loop.
 
PetscErrorCode ComputeViscous (UserCtx *user, Vec Ucat, Vec Visc)
 Implementation of ComputeViscous().
 
PetscErrorCode ComputeBodyForces (UserCtx *user, Vec Rct)
 Implementation of ComputeBodyForces().
 
static PetscErrorCode RHSContravariantSpatialTerms (UserCtx *user, Vec Conv, Vec Visc, Vec Rct)
 Contravariant cell-centre components of the spatial terms, Rct = aj F_n .
 
static PICURV_ALWAYS_INLINE PetscReal RHSFaceTerm (const PoissonFaceMetrics *metrics, const PetscReal ***nvert, const PetscReal ***p, const Cmpnts ***rct, const PetscInt c[3], PetscInt n, const PetscInt m[3], const PetscBool periodic[3])
 Face residual of component n at cell c: the two-cell average of rct less the pressure-gradient flux through the face.
 
static PetscErrorCode RHSFaceTermsWithPressureGradient (UserCtx *user, Vec Rct, Vec Rhs)
 Face residual from the ghosted cell-centre terms: the average of Rct over the two cells beside each face, less the pressure-gradient flux through it.
 
static PetscErrorCode RHSZeroInactiveRows (UserCtx *user, Vec Rhs)
 Zeroes the residual rows that carry no unknown: the component a 2D run disables, every component of a solid cell, and each face component whose far cell is solid.
 
PetscErrorCode ComputeRHS (UserCtx *user, Vec Rhs)
 Implementation of ComputeRHS().
 

Function Documentation

◆ RHSVelocityAtBN()

static PICURV_ALWAYS_INLINE Cmpnts RHSVelocityAtBN ( const Cmpnts ***  ucat,
const PetscInt  c[3],
PetscInt  b,
PetscInt  p,
PetscInt  n,
PetscInt  q 
)
static

Velocity at c + p e_b + q e_n, the Cartesian counterpart of PoissonOperator_AtBN().

Definition at line 5 of file rhs.c.

7{
8 return ucat[c[2] + (b == 2) * p + (n == 2) * q][c[1] + (b == 1) * p + (n == 1) * q][c[0] + (b == 0) * p + (n == 0) * q];
9}
Here is the caller graph for this function:

◆ RHSVelocityAt()

static Cmpnts RHSVelocityAt ( const Cmpnts ***  ucat,
const PetscInt  c[3],
PetscInt  n,
PetscInt  d 
)
inlinestatic

Cartesian velocity at cell c displaced by d along axis n.

Definition at line 12 of file rhs.c.

13{
14 return RHSVelocityAtBN(ucat, c, n, d, n, 0);
15}
static PICURV_ALWAYS_INLINE Cmpnts RHSVelocityAtBN(const Cmpnts ***ucat, const PetscInt c[3], PetscInt b, PetscInt p, PetscInt n, PetscInt q)
Velocity at c + p e_b + q e_n, the Cartesian counterpart of PoissonOperator_AtBN().
Definition rhs.c:5
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RHSQuickFlux()

static Cmpnts RHSQuickFlux ( const Cmpnts ***  ucat,
const PetscInt  c[3],
PetscInt  n,
PetscReal  ucon,
PetscInt  m1,
PetscInt  p2 
)
inlinestatic

QUICK flux through the face between cell c and c + e_n, upwinded by the sign of ucon.

m1 and p2 are the offsets used for the far upwind cells, -1 and +2 in the interior; a one-sided face substitutes 0 or +1 for the side it cannot reach.

Definition at line 22 of file rhs.c.

24{
25 const PetscReal coef = 0.125;
26 const PetscReal up = ucon + fabs(ucon), um = ucon - fabs(ucon);
27 const Cmpnts u0 = RHSVelocityAt(ucat, c, n, 0), u1 = RHSVelocityAt(ucat, c, n, 1);
28 const Cmpnts um1 = RHSVelocityAt(ucat, c, n, m1), up2 = RHSVelocityAt(ucat, c, n, p2);
29 Cmpnts f;
30
31 f.x = um * (coef * (-up2.x -2. * u1.x +3. * u0.x) +u1.x) +
32 up * (coef * (-um1.x -2. * u0.x +3. * u1.x) +u0.x);
33 f.y = um * (coef * (-up2.y -2. * u1.y +3. * u0.y) +u1.y) +
34 up * (coef * (-um1.y -2. * u0.y +3. * u1.y) +u0.y);
35 f.z = um * (coef * (-up2.z -2. * u1.z +3. * u0.z) +u1.z) +
36 up * (coef * (-um1.z -2. * u0.z +3. * u1.z) +u0.z);
37 return f;
38}
static Cmpnts RHSVelocityAt(const Cmpnts ***ucat, const PetscInt c[3], PetscInt n, PetscInt d)
Cartesian velocity at cell c displaced by d along axis n.
Definition rhs.c:12
PetscScalar x
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:

◆ RHSCentralFlux()

static Cmpnts RHSCentralFlux ( const Cmpnts ***  ucat,
const PetscInt  c[3],
PetscInt  n,
PetscReal  ucon 
)
inlinestatic

Central flux through the face between cell c and c + e_n.

Definition at line 41 of file rhs.c.

42{
43 const Cmpnts u0 = RHSVelocityAt(ucat, c, n, 0), u1 = RHSVelocityAt(ucat, c, n, 1);
44 Cmpnts f;
45
46 f.x = ucon * ( u0.x + u1.x );
47 f.y = ucon * ( u0.y + u1.y );
48 f.z = ucon * ( u0.z + u1.z );
49 return f;
50}
PetscScalar z
Definition variables.h:145
PetscScalar y
Definition variables.h:145
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RHSConvectiveFaceFlux()

static Cmpnts RHSConvectiveFaceFlux ( const Cmpnts ***  ucat,
const PetscReal ***  nvert,
const PetscInt  c[3],
PetscInt  n,
PetscReal  ucon,
PetscInt  m,
PetscBool  periodic,
PetscBool  central 
)
inlinestatic

Convective flux of Cartesian momentum through the face between cell c and c + e_n, carried by half the contravariant flux on that face.

Interior faces use the central flux when central (central differencing or LES) and QUICK otherwise; with central differencing the two end faces of an axis are central as well. A face at the low end of an axis or behind a solid cell is QUICK one-sided toward +n, and one at the high end or ahead of a solid cell one-sided toward -n, except across a periodic seam, which keeps the full stencil. A face meeting none of these carries no flux.

Definition at line 62 of file rhs.c.

65{
66 const PetscInt s = c[n];
67 const PetscReal nv_0 = nvert[c[2]][c[1]][c[0]];
68 const PetscReal nv_p = nvert[c[2] + (n == 2)][c[1] + (n == 1)][c[0] + (n == 0)];
69 const Cmpnts zero = {0.0, 0.0, 0.0};
70 /* The cell below exists in the local array everywhere but at s = 0 on a bounded axis. */
71 const PetscBool has_m = (PetscBool)(s > 0 || periodic);
72 const PetscReal nv_m = has_m ? nvert[c[2] - (n == 2)][c[1] - (n == 1)][c[0] - (n == 0)] : 0.0;
73 const PetscBool full_seam = (PetscBool)(periodic && nv_m < POISSON_SOLID_THRESHOLD && nv_p < POISSON_SOLID_THRESHOLD);
74
75 if (s > 0 && s < m - 2 && RHSConvectsAsFluid(nv_p) && RHSConvectsAsFluid(nv_m)) {
76 return central ? RHSCentralFlux(ucat, c, n, ucon) : RHSQuickFlux(ucat, c, n, ucon, -1, 2);
77 }
78 if (central && (s == 0 || s == m - 2) && RHSConvectsAsFluid(nv_p) && RHSConvectsAsFluid(nv_0)) {
79 return RHSCentralFlux(ucat, c, n, ucon);
80 }
81 if (s < m - 2 && (s == 0 || nv_m > POISSON_SOLID_THRESHOLD)) {
82 return RHSQuickFlux(ucat, c, n, ucon, (full_seam && s == 0) ? -1 : 0, 2);
83 }
84 if (s > 0 && (s == m - 2 || nv_p > POISSON_SOLID_THRESHOLD)) {
85 return RHSQuickFlux(ucat, c, n, ucon, -1, (full_seam && s == m - 2) ? 2 : 1);
86 }
87 return zero;
88}
#define POISSON_SOLID_THRESHOLD
Cells whose nvert exceeds this value are solid.
Definition poisson.h:29
static Cmpnts RHSQuickFlux(const Cmpnts ***ucat, const PetscInt c[3], PetscInt n, PetscReal ucon, PetscInt m1, PetscInt p2)
QUICK flux through the face between cell c and c + e_n, upwinded by the sign of ucon.
Definition rhs.c:22
static Cmpnts RHSCentralFlux(const Cmpnts ***ucat, const PetscInt c[3], PetscInt n, PetscReal ucon)
Central flux through the face between cell c and c + e_n.
Definition rhs.c:41
static PetscBool RHSConvectsAsFluid(PetscReal nvert)
Whether a cell takes part in convection: fluid, or a blanked inner-body cell.
Definition rhs.h:19
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ComputeConvection()

PetscErrorCode ComputeConvection ( UserCtx *  user,
Vec  Ucont,
Vec  Ucat,
Vec  Conv 
)

Implementation of ComputeConvection().

Computes the convective contribution to the contravariant momentum RHS.

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

See also
ComputeConvection()

Definition at line 96 of file rhs.c.

97{
98 const SimCtx *simCtx = user->simCtx;
99 const DMDALocalInfo *info = &user->info;
100 const PetscInt m[3] = {info->mx, info->my, info->mz};
101 const PetscBool central = (PetscBool)(simCtx->les || simCtx->central);
102 PetscBool periodic[3];
103 PetscInt lo[3], hi[3];
104 Vec Fp[3];
105 Cmpnts ***fp[3], ***conv;
106 const Cmpnts ***ucont, ***ucat;
107 const PetscReal ***nvert;
108
109 PetscFunctionBeginUser;
111 GetOwnedInteriorCellBounds(info, lo, hi);
112 GetPeriodicAxes(user->simCtx, periodic);
113 PetscCall(PreparePeriodicQuickStencilFields(user, Ucat, user->lNvert));
114 PetscCall(VecSet(Conv, 0.0));
115 /* Face fluxes per axis, from fda's pool and zeroed as fresh vectors would be: faces that
116 carry no flux must read as zero. */
117 for (PetscInt n = 0; n < 3; n++) {
118 PetscCall(DMGetLocalVector(user->fda, &Fp[n]));
119 PetscCall(VecZeroEntries(Fp[n]));
120 PetscCall(DMDAVecGetArray(user->fda, Fp[n], &fp[n]));
121 }
122 PetscCall(DMDAVecGetArrayRead(user->fda, Ucont, (void *)&ucont));
123 PetscCall(DMDAVecGetArrayRead(user->fda, Ucat, (void *)&ucat));
124 PetscCall(DMDAVecGetArrayRead(user->da, user->lNvert, (void *)&nvert));
125 PetscCall(DMDAVecGetArray(user->fda, Conv, &conv));
126
127 /* Fluxes on every face of the owned cells: along n, from the face below the first cell. */
128 for (PetscInt n = 0; n < 3; n++) {
129 PetscInt flo[3] = {lo[0], lo[1], lo[2]};
130
131 flo[n] -= 1;
132 for (PetscInt k = flo[2]; k < hi[2]; k++) {
133 for (PetscInt j = flo[1]; j < hi[1]; j++) {
134 for (PetscInt i = flo[0]; i < hi[0]; i++) {
135 const PetscInt c[3] = {i, j, k};
136 const PetscReal ucon = (&ucont[k][j][i].x)[n] * 0.5;
137
138 fp[n][k][j][i] = RHSConvectiveFaceFlux(ucat, (const PetscReal ***)nvert, c, n, ucon,
139 m[n], periodic[n], central);
140 }
141 }
142 }
143 }
144
145 /* Convective term in Cartesian components: the net flux out of each cell. */
146 for (PetscInt k = lo[2]; k < hi[2]; k++) {
147 for (PetscInt j = lo[1]; j < hi[1]; j++) {
148 for (PetscInt i = lo[0]; i < hi[0]; i++) {
149 conv[k][j][i].x =
150 fp[0][k][j][i].x - fp[0][k][j][i-1].x +
151 fp[1][k][j][i].x - fp[1][k][j-1][i].x +
152 fp[2][k][j][i].x - fp[2][k-1][j][i].x;
153 conv[k][j][i].y =
154 fp[0][k][j][i].y - fp[0][k][j][i-1].y +
155 fp[1][k][j][i].y - fp[1][k][j-1][i].y +
156 fp[2][k][j][i].y - fp[2][k-1][j][i].y;
157 conv[k][j][i].z =
158 fp[0][k][j][i].z - fp[0][k][j][i-1].z +
159 fp[1][k][j][i].z - fp[1][k][j-1][i].z +
160 fp[2][k][j][i].z - fp[2][k-1][j][i].z;
161 }
162 }
163 }
164
165 PetscCall(DMDAVecRestoreArray(user->fda, Conv, &conv));
166 PetscCall(DMDAVecRestoreArrayRead(user->da, user->lNvert, (void *)&nvert));
167 PetscCall(DMDAVecRestoreArrayRead(user->fda, Ucat, (void *)&ucat));
168 PetscCall(DMDAVecRestoreArrayRead(user->fda, Ucont, (void *)&ucont));
169 for (PetscInt n = 0; n < 3; n++) {
170 PetscCall(DMDAVecRestoreArray(user->fda, Fp[n], &fp[n]));
171 PetscCall(DMRestoreLocalVector(user->fda, &Fp[n]));
172 }
173 LOG_ALLOW(GLOBAL, LOG_DEBUG, "Convective term calculated.\n");
175 PetscFunctionReturn(0);
176}
PetscErrorCode PreparePeriodicQuickStencilFields(UserCtx *user, Vec local_vector_field, Vec local_scalar_field)
Repairs the outer adjacent periodic ghosts used by QUICK cell stencils.
#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 PROFILE_FUNCTION_END
Marks the end of a profiled code block.
Definition logging.h:1029
@ LOG_DEBUG
Detailed debugging information.
Definition logging.h:32
#define PROFILE_FUNCTION_BEGIN
Marks the beginning of a profiled function: an always-on aggregate timer.
Definition logging.h:1000
static Cmpnts RHSConvectiveFaceFlux(const Cmpnts ***ucat, const PetscReal ***nvert, const PetscInt c[3], PetscInt n, PetscReal ucon, PetscInt m, PetscBool periodic, PetscBool central)
Convective flux of Cartesian momentum through the face between cell c and c + e_n,...
Definition rhs.c:62
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
static void GetPeriodicAxes(const SimCtx *simCtx, PetscBool periodic[3])
Which grid axes are periodic.
Definition setup.h:361
Vec lNvert
Definition variables.h:1197
SimCtx * simCtx
Back-pointer to the master simulation context.
Definition variables.h:1160
PetscInt central
Definition variables.h:963
DMDALocalInfo info
Definition variables.h:1169
PetscInt les
Active LES closure; an LESModelType value.
Definition variables.h:1041
The master context for the entire simulation.
Definition variables.h:916
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RHSTransverseVelocityDifference()

static PICURV_ALWAYS_INLINE Cmpnts RHSTransverseVelocityDifference ( const Cmpnts ***  ucat,
const PetscInt  c[3],
PetscInt  n,
PetscInt  b,
PoissonTransverseDifference  diff 
)
static

(u(c+hi e_b+e_n) + u(c+hi e_b) - u(c+lo e_b+e_n) - u(c+lo e_b)) * weight, for diff's rows and weight.

Definition at line 180 of file rhs.c.

183{
184 const Cmpnts a1 = RHSVelocityAtBN(ucat, c, b, diff.hi, n, 1), a0 = RHSVelocityAtBN(ucat, c, b, diff.hi, n, 0);
185 const Cmpnts b1 = RHSVelocityAtBN(ucat, c, b, diff.lo, n, 1), b0 = RHSVelocityAtBN(ucat, c, b, diff.lo, n, 0);
186 Cmpnts d;
187
188 d.x = (a1.x + a0.x - b1.x - b0.x) * diff.weight;
189 d.y = (a1.y + a0.y - b1.y - b0.y) * diff.weight;
190 d.z = (a1.z + a0.z - b1.z - b0.z) * diff.weight;
191 return d;
192}
PetscReal weight
0.25 central, 0.5 one-sided, 0 when no fluid side remains.
Definition poisson.h:41
PetscInt hi
Transverse offset of the added row pair.
Definition poisson.h:40
PetscInt lo
Transverse offset of the subtracted row pair.
Definition poisson.h:39
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RHSTransverseFaceDifference()

static PICURV_ALWAYS_INLINE Cmpnts RHSTransverseFaceDifference ( const Cmpnts ***  ucat,
const PetscReal ***  nvert,
const PetscInt  c[3],
PetscInt  n,
PetscInt  b 
)
static

Velocity difference along transverse axis b on the face between cell c and c + e_n: the average over the two cells beside the face, one-sided away from a blanked solid cell.

Each branch has constant offsets once n and b are constants.

Definition at line 199 of file rhs.c.

201{
202 static const PoissonTransverseDifference lower = {-1, 0, 0.5}, upper = {0, 1, 0.5}, central = {-1, 1, 0.25};
203
204 if (RHSIsBlankedSolid(PoissonOperator_AtBN(nvert, c, b, 1, n, 0)) || RHSIsBlankedSolid(PoissonOperator_AtBN(nvert, c, b, 1, n, 1)))
205 return RHSTransverseVelocityDifference(ucat, c, n, b, lower);
206 if (RHSIsBlankedSolid(PoissonOperator_AtBN(nvert, c, b, -1, n, 0)) || RHSIsBlankedSolid(PoissonOperator_AtBN(nvert, c, b, -1, n, 1)))
207 return RHSTransverseVelocityDifference(ucat, c, n, b, upper);
208 return RHSTransverseVelocityDifference(ucat, c, n, b, central);
209}
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
Transverse difference used at one face.
Definition poisson.h:38
static PICURV_ALWAYS_INLINE Cmpnts RHSTransverseVelocityDifference(const Cmpnts ***ucat, const PetscInt c[3], PetscInt n, PetscInt b, PoissonTransverseDifference diff)
(u(c+hi e_b+e_n) + u(c+hi e_b) - u(c+lo e_b+e_n) - u(c+lo e_b)) * weight, for diff's rows and weight.
Definition rhs.c:180
static PetscBool RHSIsBlankedSolid(PetscReal nvert)
Whether a cell is a blanked solid cell, which ComputeViscous()'s transverse differences avoid with a ...
Definition rhs.h:30
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RHSFaceVelocityDifferences()

static PICURV_ALWAYS_INLINE void RHSFaceVelocityDifferences ( const Cmpnts ***  ucat,
const PetscReal ***  nvert,
const PetscInt  c[3],
PetscInt  n,
Cmpnts  d[3] 
)
static

Velocity differences on the face between cell c and c + e_n, one per computational direction: across the face along n, and along each transverse axis.

Definition at line 215 of file rhs.c.

217{
218 const Cmpnts u0 = RHSVelocityAtBN(ucat, c, n, 0, n, 0), u1 = RHSVelocityAtBN(ucat, c, n, 0, n, 1);
219
220 d[n].x = u1.x - u0.x; d[n].y = u1.y - u0.y; d[n].z = u1.z - u0.z;
221 d[(n + 1) % 3] = RHSTransverseFaceDifference(ucat, nvert, c, n, (n + 1) % 3);
222 d[(n + 2) % 3] = RHSTransverseFaceDifference(ucat, nvert, c, n, (n + 2) % 3);
223}
static PICURV_ALWAYS_INLINE Cmpnts RHSTransverseFaceDifference(const Cmpnts ***ucat, const PetscReal ***nvert, const PetscInt c[3], PetscInt n, PetscInt b)
Velocity difference along transverse axis b on the face between cell c and c + e_n: the average over ...
Definition rhs.c:199
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RHSViscousFaceFlux()

static PICURV_ALWAYS_INLINE Cmpnts RHSViscousFaceFlux ( const PoissonFaceMetrics *  metrics,
const Cmpnts  d[3],
const PetscInt  c[3],
PetscInt  n,
PetscReal  nu,
PetscReal  nu_t,
PetscBool  les,
PetscBool  clark 
)
static

Viscous stress through the face between cell c and c + e_n, in Cartesian components, from the face differences d and the face metrics.

With F_b the contravariant base vectors on the face and N = F_n its normal, the flux of each velocity component U is (sum_b (F_b . N) dU/dxi_b + sum_c (sum_b dU_c/dxi_b F_b) . e_U N_c) aj: the full stress, gradient plus transpose. The eddy viscosity nu_t carries it when LES is on, and the molecular viscosity nu is added; with the gradient (Clark) model the tensor sum_b d_b d_b^T contracted with N, over 12, is subtracted.

Definition at line 235 of file rhs.c.

238{
239 const Cmpnts F0 = metrics->metric[n][0][c[2]][c[1]][c[0]];
240 const Cmpnts F1 = metrics->metric[n][1][c[2]][c[1]][c[0]];
241 const Cmpnts F2 = metrics->metric[n][2][c[2]][c[1]][c[0]];
242 const Cmpnts N = (n == 0) ? F0 : (n == 1) ? F1 : F2;
243 const PetscReal ajc = metrics->aj[n][c[2]][c[1]][c[0]];
244 const PetscReal g0 = F0.x * N.x + F0.y * N.y + F0.z * N.z;
245 const PetscReal g1 = F1.x * N.x + F1.y * N.y + F1.z * N.z;
246 const PetscReal g2 = F2.x * N.x + F2.y * N.y + F2.z * N.z;
247 /* r_<velocity component><Cartesian axis> = sum_b dU/dxi_b F_b[axis] */
248 const PetscReal r11 = d[0].x * F0.x + d[1].x * F1.x + d[2].x * F2.x;
249 const PetscReal r21 = d[0].y * F0.x + d[1].y * F1.x + d[2].y * F2.x;
250 const PetscReal r31 = d[0].z * F0.x + d[1].z * F1.x + d[2].z * F2.x;
251 const PetscReal r12 = d[0].x * F0.y + d[1].x * F1.y + d[2].x * F2.y;
252 const PetscReal r22 = d[0].y * F0.y + d[1].y * F1.y + d[2].y * F2.y;
253 const PetscReal r32 = d[0].z * F0.y + d[1].z * F1.y + d[2].z * F2.y;
254 const PetscReal r13 = d[0].x * F0.z + d[1].x * F1.z + d[2].x * F2.z;
255 const PetscReal r23 = d[0].y * F0.z + d[1].y * F1.z + d[2].y * F2.z;
256 const PetscReal r33 = d[0].z * F0.z + d[1].z * F1.z + d[2].z * F2.z;
257 const PetscReal ex = g0 * d[0].x + g1 * d[1].x + g2 * d[2].x + r11 * N.x + r21 * N.y + r31 * N.z;
258 const PetscReal ey = g0 * d[0].y + g1 * d[1].y + g2 * d[2].y + r12 * N.x + r22 * N.y + r32 * N.z;
259 const PetscReal ez = g0 * d[0].z + g1 * d[1].z + g2 * d[2].z + r13 * N.x + r23 * N.y + r33 * N.z;
260 Cmpnts f = {0.0, 0.0, 0.0};
261
262 if (les) {
263 f.x = ex * ajc * (nu_t);
264 f.y = ey * ajc * (nu_t);
265 f.z = ez * ajc * (nu_t);
266 }
267 f.x += ex * ajc * (nu);
268 f.y += ey * ajc * (nu);
269 f.z += ez * ajc * (nu);
270 if (clark) {
271 /* d_b is the velocity difference between neighbouring cells along direction b, so it
272 already carries that direction's spacing: sum_b d_b d_b^T is Delta_k^2 du_i/dx_k
273 du_j/dx_k as it stands, and no further cell extent belongs in it. */
274 const SymTensor gradient_tensor =
275 SymTensorCombine(1.0, SymTensorSelfOuter(d[0]), 1.0,
277 1.0, SymTensorSelfOuter(d[2])));
278 const Cmpnts gradient_flux = SymTensorTimesVector(gradient_tensor, N);
279
280 f.x -= gradient_flux.x / 12.;
281 f.y -= gradient_flux.y / 12.;
282 f.z -= gradient_flux.z / 12.;
283 }
284 return f;
285}
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
static Cmpnts SymTensorTimesVector(SymTensor t, Cmpnts v)
Applies a symmetric tensor to a vector, returning t_ij v_j.
Definition les.h:82
const PetscReal *** aj[3]
Definition poisson.h:47
const Cmpnts *** metric[3][3]
Definition poisson.h:46
A symmetric second-order tensor stored by its six independent components.
Definition variables.h:161
double nu_t(double yplus)
Computes turbulent eddy viscosity ratio (ν_t / ν)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RHSViscousFluxesAlong()

static PICURV_ALWAYS_INLINE void RHSViscousFluxesAlong ( const UserCtx *  user,
const PoissonFaceMetrics *  metrics,
const Cmpnts ***  ucat,
const PetscReal ***  nvert,
const PetscReal ***  lnu_t,
const Cmpnts ***  lwall_stress,
const PetscInt  lo[3],
const PetscInt  hi[3],
const PetscInt  m[3],
PetscInt  n,
PetscBool  les,
PetscBool  clark,
Cmpnts ***  fp 
)
static

Viscous stresses on every face of orientation n of the owned cells, from the face below the first cell; n is a constant at each call, so each expands to its own loop.

Definition at line 291 of file rhs.c.

297{
298 const PetscBool wall_lo = BoundaryFaceHasWallShear(&user->boundary_faces[2 * n]);
299 const PetscBool wall_hi = BoundaryFaceHasWallShear(&user->boundary_faces[2 * n + 1]);
300 const PetscReal ren = user->simCtx->ren;
301 PetscInt flo[3] = {lo[0], lo[1], lo[2]};
302 PetscInt across[3] = {0, 0, 0};
303
304 flo[n] -= 1;
305 across[n] = 1;
306 for (PetscInt k = flo[2]; k < hi[2]; k++) {
307 for (PetscInt j = flo[1]; j < hi[1]; j++) {
308 for (PetscInt i = flo[0]; i < hi[0]; i++) {
309 const PetscInt c[3] = {i, j, k};
310 PetscReal nu = 1./ren, nu_t = 0;
311 Cmpnts d[3];
312
313 if ((wall_lo && c[n] == 0) || (wall_hi && c[n] == m[n] - 2)) {
314 /* A wall-modelled face carries the modelled stress itself, already
315 oriented as this face's flux and stored in the face's dummy cell
316 (ApplyWallFunction), times the face area: the law, not a gradient
317 across the resolved first cell, sets the wall shear. A wall-resolved
318 face keeps the molecular gradient flux; the subgrid stress vanishes at
319 a wall, so the eddy viscosity there is zero. */
320 if (lwall_stress) {
321 const Cmpnts N = metrics->metric[n][n][k][j][i];
322 const PetscReal area = PetscSqrtReal(N.x * N.x + N.y * N.y + N.z * N.z);
323 PetscInt w[3] = {i, j, k};
324 w[n] = (c[n] == 0) ? 0 : m[n] - 1;
325 fp[k][j][i].x = lwall_stress[w[2]][w[1]][w[0]].x * area;
326 fp[k][j][i].y = lwall_stress[w[2]][w[1]][w[0]].y * area;
327 fp[k][j][i].z = lwall_stress[w[2]][w[1]][w[0]].z * area;
328 continue;
329 }
330 } else if (les) {
331 nu_t = 0.5 * (lnu_t[k][j][i] + lnu_t[k + across[2]][j + across[1]][i + across[0]]);
332 }
333 RHSFaceVelocityDifferences(ucat, nvert, c, n, d);
334 fp[k][j][i] = RHSViscousFaceFlux(metrics, d, c, n, nu, nu_t, les, clark);
335 }
336 }
337 }
338}
PetscBool BoundaryFaceHasWallShear(const BoundaryFaceConfig *face)
Reports whether a boundary face is a wall that exerts shear on the flow.
Definition Boundaries.c:9
static PICURV_ALWAYS_INLINE void RHSFaceVelocityDifferences(const Cmpnts ***ucat, const PetscReal ***nvert, const PetscInt c[3], PetscInt n, Cmpnts d[3])
Velocity differences on the face between cell c and c + e_n, one per computational direction: across ...
Definition rhs.c:215
static PICURV_ALWAYS_INLINE Cmpnts RHSViscousFaceFlux(const PoissonFaceMetrics *metrics, const Cmpnts d[3], const PetscInt c[3], PetscInt n, PetscReal nu, PetscReal nu_t, PetscBool les, PetscBool clark)
Viscous stress through the face between cell c and c + e_n, in Cartesian components,...
Definition rhs.c:235
BoundaryFaceConfig boundary_faces[6]
Definition variables.h:1182
PetscReal ren
Definition variables.h:965
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ComputeViscous()

PetscErrorCode ComputeViscous ( UserCtx *  user,
Vec  Ucat,
Vec  Visc 
)

Implementation of ComputeViscous().

Computes the viscous contribution to the contravariant momentum RHS.

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

See also
ComputeViscous()

Definition at line 346 of file rhs.c.

347{
348 const SimCtx *simCtx = user->simCtx;
349 const DMDALocalInfo *info = &user->info;
350 const PetscInt m[3] = {info->mx, info->my, info->mz};
351 const PetscBool les = (PetscBool)(simCtx->les != 0);
352 const PetscBool clark = (PetscBool)(simCtx->les_gradient_model != 0);
353 PetscInt lo[3], hi[3];
354 PoissonFaceMetrics metrics;
355 Vec Fp[3];
356 Cmpnts ***fp[3], ***visc;
357 const Cmpnts ***ucat;
358 const PetscReal ***nvert, ***lnu_t = NULL;
359 const Cmpnts ***lwall_stress = NULL;
360
361 PetscFunctionBeginUser;
363 GetOwnedInteriorCellBounds(info, lo, hi);
364 PetscCall(VecSet(Visc, 0.0));
365 for (PetscInt n = 0; n < 3; n++) {
366 PetscCall(DMGetLocalVector(user->fda, &Fp[n]));
367 PetscCall(VecZeroEntries(Fp[n]));
368 PetscCall(DMDAVecGetArray(user->fda, Fp[n], &fp[n]));
369 }
370 PetscCall(PoissonOperator_GetFaceMetrics(user, &metrics));
371 PetscCall(DMDAVecGetArrayRead(user->fda, Ucat, (void *)&ucat));
372 PetscCall(DMDAVecGetArrayRead(user->da, user->lNvert, (void *)&nvert));
373 if (les) PetscCall(DMDAVecGetArrayRead(user->da, user->lNu_t, (void *)&lnu_t));
374 /* A wall model supplies its own face's flux: the modelled stress, in place of the
375 gradient flux across the resolved first cell. */
376 if (simCtx->wallfunction) PetscCall(DMDAVecGetArrayRead(user->fda, user->lWall_Stress, (void *)&lwall_stress));
377 PetscCall(DMDAVecGetArray(user->fda, Visc, &visc));
378
379 /* Stresses on every face of the owned cells, one constant direction per call. */
380 RHSViscousFluxesAlong(user, &metrics, ucat, nvert, lnu_t, lwall_stress, lo, hi, m, 0, les, clark, fp[0]);
381 RHSViscousFluxesAlong(user, &metrics, ucat, nvert, lnu_t, lwall_stress, lo, hi, m, 1, les, clark, fp[1]);
382 RHSViscousFluxesAlong(user, &metrics, ucat, nvert, lnu_t, lwall_stress, lo, hi, m, 2, les, clark, fp[2]);
383
384 /* Viscous term in Cartesian components: the net stress out of each cell. */
385 for (PetscInt k = lo[2]; k < hi[2]; k++) {
386 for (PetscInt j = lo[1]; j < hi[1]; j++) {
387 for (PetscInt i = lo[0]; i < hi[0]; i++) {
388 visc[k][j][i].x =
389 (fp[0][k][j][i].x - fp[0][k][j][i-1].x +
390 fp[1][k][j][i].x - fp[1][k][j-1][i].x +
391 fp[2][k][j][i].x - fp[2][k-1][j][i].x);
392 visc[k][j][i].y =
393 (fp[0][k][j][i].y - fp[0][k][j][i-1].y +
394 fp[1][k][j][i].y - fp[1][k][j-1][i].y +
395 fp[2][k][j][i].y - fp[2][k-1][j][i].y);
396 visc[k][j][i].z =
397 (fp[0][k][j][i].z - fp[0][k][j][i-1].z +
398 fp[1][k][j][i].z - fp[1][k][j-1][i].z +
399 fp[2][k][j][i].z - fp[2][k-1][j][i].z);
400 }
401 }
402 }
403
404 PetscCall(DMDAVecRestoreArray(user->fda, Visc, &visc));
405 if (lwall_stress) PetscCall(DMDAVecRestoreArrayRead(user->fda, user->lWall_Stress, (void *)&lwall_stress));
406 if (lnu_t) PetscCall(DMDAVecRestoreArrayRead(user->da, user->lNu_t, (void *)&lnu_t));
407 PetscCall(DMDAVecRestoreArrayRead(user->da, user->lNvert, (void *)&nvert));
408 PetscCall(DMDAVecRestoreArrayRead(user->fda, Ucat, (void *)&ucat));
409 PetscCall(PoissonOperator_RestoreFaceMetrics(user, &metrics));
410 for (PetscInt n = 0; n < 3; n++) {
411 PetscCall(DMDAVecRestoreArray(user->fda, Fp[n], &fp[n]));
412 PetscCall(DMRestoreLocalVector(user->fda, &Fp[n]));
413 }
414 LOG_ALLOW(GLOBAL, LOG_DEBUG, "Viscous terms calculated.\n");
416 PetscFunctionReturn(0);
417}
PetscErrorCode PoissonOperator_RestoreFaceMetrics(UserCtx *user, PoissonFaceMetrics *metrics)
Returns the arrays borrowed by PoissonOperator_GetFaceMetrics().
Definition poisson.c:171
PetscErrorCode PoissonOperator_GetFaceMetrics(UserCtx *user, PoissonFaceMetrics *metrics)
Borrows read access to the face metric arrays of user.
Definition poisson.c:147
Read-only face metric arrays: metric[n][b] is F_b on the n-faces.
Definition poisson.h:45
static PICURV_ALWAYS_INLINE void RHSViscousFluxesAlong(const UserCtx *user, const PoissonFaceMetrics *metrics, const Cmpnts ***ucat, const PetscReal ***nvert, const PetscReal ***lnu_t, const Cmpnts ***lwall_stress, const PetscInt lo[3], const PetscInt hi[3], const PetscInt m[3], PetscInt n, PetscBool les, PetscBool clark, Cmpnts ***fp)
Viscous stresses on every face of orientation n of the owned cells, from the face below the first cel...
Definition rhs.c:291
Vec lNu_t
Definition variables.h:1259
PetscInt les_gradient_model
Add the Clark gradient (tensor-diffusivity) term to the viscous flux.
Definition variables.h:1045
PetscInt wallfunction
Enable wall functions on WALL faces.
Definition variables.h:1042
Vec lWall_Stress
Definition variables.h:1193
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ComputeBodyForces()

PetscErrorCode ComputeBodyForces ( UserCtx *  user,
Vec  Rct 
)

Implementation of ComputeBodyForces().

General dispatcher for applying all active body forces (momentum sources).

A body force added here follows the contract in include/BodyForces.h: this runs once per residual evaluation, not once per step, so a force that carries state across calls (a filter, ramp, moving average, or integral term) gates its update on simCtx->step.

See also
ComputeBodyForces()

Definition at line 426 of file rhs.c.

427{
428 PetscFunctionBeginUser;
430 /* The driven channel/pipe momentum source; it does nothing unless a driven face is active. */
431 PetscCall(ComputeDrivenChannelFlowSource(user, Rct));
433 PetscFunctionReturn(0);
434}
PetscErrorCode ComputeDrivenChannelFlowSource(UserCtx *user, Vec Rct)
Applies a momentum source term to drive flow in a periodic channel or pipe.
Definition BodyForces.c:38
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RHSContravariantSpatialTerms()

static PetscErrorCode RHSContravariantSpatialTerms ( UserCtx *  user,
Vec  Conv,
Vec  Visc,
Vec  Rct 
)
static

Contravariant cell-centre components of the spatial terms, Rct = aj F_n .

(Visc - Conv), with each base vector F_n averaged from its two faces to the cell centre.

Definition at line 440 of file rhs.c.

441{
442 PetscInt lo[3], hi[3];
443 const Cmpnts ***csi, ***eta, ***zet, ***conv, ***visc;
444 const PetscReal ***aj;
445 Cmpnts ***rct;
446
447 PetscFunctionBeginUser;
449 GetOwnedInteriorCellBounds(&user->info, lo, hi);
450 PetscCall(DMDAVecGetArrayRead(user->fda, user->lCsi, (void *)&csi));
451 PetscCall(DMDAVecGetArrayRead(user->fda, user->lEta, (void *)&eta));
452 PetscCall(DMDAVecGetArrayRead(user->fda, user->lZet, (void *)&zet));
453 PetscCall(DMDAVecGetArrayRead(user->da, user->lAj, (void *)&aj));
454 PetscCall(DMDAVecGetArrayRead(user->fda, Conv, (void *)&conv));
455 PetscCall(DMDAVecGetArrayRead(user->fda, Visc, (void *)&visc));
456 PetscCall(DMDAVecGetArray(user->fda, Rct, &rct));
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 Cmpnts rc = {visc[k][j][i].x - conv[k][j][i].x,
461 visc[k][j][i].y - conv[k][j][i].y,
462 visc[k][j][i].z - conv[k][j][i].z};
463 rct[k][j][i].x = aj[k][j][i] *
464 (0.5 * (csi[k][j][i].x + csi[k][j][i-1].x) * rc.x +
465 0.5 * (csi[k][j][i].y + csi[k][j][i-1].y) * rc.y +
466 0.5 * (csi[k][j][i].z + csi[k][j][i-1].z) * rc.z);
467 rct[k][j][i].y = aj[k][j][i] *
468 (0.5 * (eta[k][j][i].x + eta[k][j-1][i].x) * rc.x +
469 0.5 * (eta[k][j][i].y + eta[k][j-1][i].y) * rc.y +
470 0.5 * (eta[k][j][i].z + eta[k][j-1][i].z) * rc.z);
471 rct[k][j][i].z = aj[k][j][i] *
472 (0.5 * (zet[k][j][i].x + zet[k-1][j][i].x) * rc.x +
473 0.5 * (zet[k][j][i].y + zet[k-1][j][i].y) * rc.y +
474 0.5 * (zet[k][j][i].z + zet[k-1][j][i].z) * rc.z);
475 }
476 }
477 }
478 PetscCall(DMDAVecRestoreArray(user->fda, Rct, &rct));
479 PetscCall(DMDAVecRestoreArrayRead(user->fda, Visc, (void *)&visc));
480 PetscCall(DMDAVecRestoreArrayRead(user->fda, Conv, (void *)&conv));
481 PetscCall(DMDAVecRestoreArrayRead(user->da, user->lAj, (void *)&aj));
482 PetscCall(DMDAVecRestoreArrayRead(user->fda, user->lZet, (void *)&zet));
483 PetscCall(DMDAVecRestoreArrayRead(user->fda, user->lEta, (void *)&eta));
484 PetscCall(DMDAVecRestoreArrayRead(user->fda, user->lCsi, (void *)&csi));
486 PetscFunctionReturn(0);
487}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RHSFaceTerm()

static PICURV_ALWAYS_INLINE PetscReal RHSFaceTerm ( const PoissonFaceMetrics *  metrics,
const PetscReal ***  nvert,
const PetscReal ***  p,
const Cmpnts ***  rct,
const PetscInt  c[3],
PetscInt  n,
const PetscInt  m[3],
const PetscBool  periodic[3] 
)
static

Face residual of component n at cell c: the two-cell average of rct less the pressure-gradient flux through the face.

Definition at line 491 of file rhs.c.

494{
495 const PetscReal *own = &rct[c[2]][c[1]][c[0]].x;
496 const PetscReal *next = &rct[c[2] + (n == 2)][c[1] + (n == 1)][c[0] + (n == 0)].x;
497
498 return 0.5 * (own[n] + next[n]) - PoissonOperator_FaceGradientFlux(metrics, nvert, p, c, n, m, periodic);
499}
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RHSFaceTermsWithPressureGradient()

static PetscErrorCode RHSFaceTermsWithPressureGradient ( UserCtx *  user,
Vec  Rct,
Vec  Rhs 
)
static

Face residual from the ghosted cell-centre terms: the average of Rct over the two cells beside each face, less the pressure-gradient flux through it.

The flux is the projection's face gradient (PoissonOperator_FaceGradientFlux()) applied to P. Every interior face is set, and on a periodic axis also the seam face at index 0.

Definition at line 508 of file rhs.c.

509{
510 const DMDALocalInfo *info = &user->info;
511 const PetscInt m[3] = {info->mx, info->my, info->mz};
512 const PetscInt start[3] = {info->xs, info->ys, info->zs};
513 PetscBool periodic[3];
514 PetscInt interior_lo[3], interior_hi[3];
515 PoissonFaceMetrics metrics;
516 const PetscReal ***nvert, ***p;
517 const Cmpnts ***rct;
518 Cmpnts ***rhs;
519
520 PetscFunctionBeginUser;
522 GetOwnedInteriorCellBounds(info, interior_lo, interior_hi);
523 GetPeriodicAxes(user->simCtx, periodic);
524 PetscCall(PoissonOperator_GetFaceMetrics(user, &metrics));
525 PetscCall(DMDAVecGetArrayRead(user->da, user->lNvert, (void *)&nvert));
526 PetscCall(DMDAVecGetArrayRead(user->da, user->lP, (void *)&p));
527 PetscCall(DMDAVecGetArrayRead(user->fda, Rct, (void *)&rct));
528 PetscCall(DMDAVecGetArray(user->fda, Rhs, &rhs));
529 /* One pass over the owned cells sets all three face components; with each direction a
530 constant, every call below compiles to its own straight-line stencil. */
531 for (PetscInt k = interior_lo[2]; k < interior_hi[2]; k++) {
532 for (PetscInt j = interior_lo[1]; j < interior_hi[1]; j++) {
533 for (PetscInt i = interior_lo[0]; i < interior_hi[0]; i++) {
534 const PetscInt c[3] = {i, j, k};
535
536 rhs[k][j][i].x = RHSFaceTerm(&metrics, nvert, p, rct, c, 0, m, periodic);
537 rhs[k][j][i].y = RHSFaceTerm(&metrics, nvert, p, rct, c, 1, m, periodic);
538 rhs[k][j][i].z = RHSFaceTerm(&metrics, nvert, p, rct, c, 2, m, periodic);
539 }
540 }
541 }
542 /* On a periodic axis the seam face at index 0 also carries an unknown. */
543 if (periodic[0] && start[0] == 0) {
544 for (PetscInt k = interior_lo[2]; k < interior_hi[2]; k++)
545 for (PetscInt j = interior_lo[1]; j < interior_hi[1]; j++) {
546 const PetscInt c[3] = {0, j, k};
547 rhs[k][j][0].x = RHSFaceTerm(&metrics, nvert, p, rct, c, 0, m, periodic);
548 }
549 }
550 if (periodic[1] && start[1] == 0) {
551 for (PetscInt k = interior_lo[2]; k < interior_hi[2]; k++)
552 for (PetscInt i = interior_lo[0]; i < interior_hi[0]; i++) {
553 const PetscInt c[3] = {i, 0, k};
554 rhs[k][0][i].y = RHSFaceTerm(&metrics, nvert, p, rct, c, 1, m, periodic);
555 }
556 }
557 if (periodic[2] && start[2] == 0) {
558 for (PetscInt j = interior_lo[1]; j < interior_hi[1]; j++)
559 for (PetscInt i = interior_lo[0]; i < interior_hi[0]; i++) {
560 const PetscInt c[3] = {i, j, 0};
561 rhs[0][j][i].z = RHSFaceTerm(&metrics, nvert, p, rct, c, 2, m, periodic);
562 }
563 }
564 PetscCall(DMDAVecRestoreArray(user->fda, Rhs, &rhs));
565 PetscCall(DMDAVecRestoreArrayRead(user->fda, Rct, (void *)&rct));
566 PetscCall(DMDAVecRestoreArrayRead(user->da, user->lP, (void *)&p));
567 PetscCall(DMDAVecRestoreArrayRead(user->da, user->lNvert, (void *)&nvert));
568 PetscCall(PoissonOperator_RestoreFaceMetrics(user, &metrics));
570 PetscFunctionReturn(0);
571}
static PICURV_ALWAYS_INLINE PetscReal RHSFaceTerm(const PoissonFaceMetrics *metrics, const PetscReal ***nvert, const PetscReal ***p, const Cmpnts ***rct, const PetscInt c[3], PetscInt n, const PetscInt m[3], const PetscBool periodic[3])
Face residual of component n at cell c: the two-cell average of rct less the pressure-gradient flux t...
Definition rhs.c:491
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RHSZeroInactiveRows()

static PetscErrorCode RHSZeroInactiveRows ( UserCtx *  user,
Vec  Rhs 
)
static

Zeroes the residual rows that carry no unknown: the component a 2D run disables, every component of a solid cell, and each face component whose far cell is solid.

Definition at line 577 of file rhs.c.

578{
579 const PetscInt two_d = user->simCtx->TwoD;
580 PetscInt lo[3], hi[3];
581 const PetscReal ***nvert;
582 Cmpnts ***rhs;
583
584 PetscFunctionBeginUser;
586 GetOwnedInteriorCellBounds(&user->info, lo, hi);
587 PetscCall(DMDAVecGetArrayRead(user->da, user->lNvert, (void *)&nvert));
588 PetscCall(DMDAVecGetArray(user->fda, Rhs, &rhs));
589 for (PetscInt k = lo[2]; k < hi[2]; k++) {
590 for (PetscInt j = lo[1]; j < hi[1]; j++) {
591 for (PetscInt i = lo[0]; i < hi[0]; i++) {
592 if (two_d == 1) rhs[k][j][i].x = 0.0;
593 else if (two_d == 2) rhs[k][j][i].y = 0.0;
594 else if (two_d == 3) rhs[k][j][i].z = 0.0;
595 if (nvert[k][j][i] > POISSON_SOLID_THRESHOLD) {
596 rhs[k][j][i].x = 0.0; rhs[k][j][i].y = 0.0; rhs[k][j][i].z = 0.0;
597 }
598 if (nvert[k][j][i+1] > POISSON_SOLID_THRESHOLD) rhs[k][j][i].x = 0.0;
599 if (nvert[k][j+1][i] > POISSON_SOLID_THRESHOLD) rhs[k][j][i].y = 0.0;
600 if (nvert[k+1][j][i] > POISSON_SOLID_THRESHOLD) rhs[k][j][i].z = 0.0;
601 }
602 }
603 }
604 PetscCall(DMDAVecRestoreArray(user->fda, Rhs, &rhs));
605 PetscCall(DMDAVecRestoreArrayRead(user->da, user->lNvert, (void *)&nvert));
607 PetscFunctionReturn(0);
608}
PetscInt TwoD
Definition variables.h:951
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ComputeRHS()

PetscErrorCode ComputeRHS ( UserCtx *  user,
Vec  Rhs 
)

Implementation of ComputeRHS().

Computes the Right-Hand Side (RHS) of the momentum equations.

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

See also
ComputeRHS()

Definition at line 616 of file rhs.c.

617{
618 const FieldId cell_fields[] = {FIELD_ID_UCAT};
619 Vec Conv, Visc, Rct;
620
621 PetscFunctionBeginUser;
623 LOG_ALLOW(LOCAL, LOG_DEBUG, "Rank %d, Block %" PetscInt_FMT ": Computing RHS...\n",
624 user->simCtx->rank, user->_this);
625 /* Work vectors from fda's pool: the residual is evaluated many times per step, and
626 allocating them each time costs more than zeroing them. ComputeConvection() and
627 ComputeViscous() zero their own outputs; the rest are zeroed here, as fresh vectors
628 would be. */
629 PetscCall(DMGetLocalVector(user->fda, &Conv));
630 PetscCall(DMGetLocalVector(user->fda, &Visc));
631 PetscCall(DMGetLocalVector(user->fda, &Rct)); PetscCall(VecZeroEntries(Rct));
632
633 /* Cartesian velocity, with its periodic copies and ghosts, from the contravariant flux. */
634 PetscCall(Contra2Cart(user));
635 PetscCall(SynchronizePeriodicCellFields(user, 1, cell_fields));
636 PetscCall(UpdateLocalGhosts(user, FIELD_ID_UCAT));
637
638 /* Spatial terms in Cartesian components at cell centres, then contravariant. */
639 LOG_ALLOW(LOCAL, LOG_DEBUG, " Calculating convective terms...\n");
640 PetscCall(ComputeConvection(user, user->lUcont, user->lUcat, Conv));
641 if (!user->simCtx->invicid) {
642 LOG_ALLOW(LOCAL, LOG_DEBUG, " Calculating viscous terms...\n");
643 PetscCall(ComputeViscous(user, user->lUcat, Visc));
644 } else {
645 PetscCall(VecZeroEntries(Visc));
646 }
647 LOG_ALLOW(LOCAL, LOG_DEBUG, " Converting Cartesian RHS to Contravariant RHS...\n");
648 PetscCall(RHSContravariantSpatialTerms(user, Conv, Visc, Rct));
649 PetscCall(ComputeBodyForces(user, Rct));
650 PetscCall(SynchronizePeriodicLocalStaggeredField(user, Rct));
651
652 /* Face residual: averaged spatial terms less the pressure gradient, then inactive rows. */
653 LOG_ALLOW(LOCAL, LOG_DEBUG, " Adding pressure gradient term to RHS...\n");
654 PetscCall(RHSFaceTermsWithPressureGradient(user, Rct, Rhs));
655 PetscCall(RHSZeroInactiveRows(user, Rhs));
656
657 PetscCall(DMRestoreLocalVector(user->fda, &Rct));
658 PetscCall(DMRestoreLocalVector(user->fda, &Visc));
659 PetscCall(DMRestoreLocalVector(user->fda, &Conv));
660 LOG_ALLOW(LOCAL, LOG_DEBUG, "Rank %d, Block %" PetscInt_FMT ": RHS computation complete.\n",
661 user->simCtx->rank, user->_this);
663 PetscFunctionReturn(0);
664}
PetscErrorCode SynchronizePeriodicLocalStaggeredField(UserCtx *user, Vec local_field)
Synchronizes one local-only component-staggered periodic work field.
PetscErrorCode SynchronizePeriodicCellFields(UserCtx *user, PetscInt num_fields, const FieldId field_ids[])
Synchronizes periodic endpoint cells for a list of cell-centered fields.
FieldId
Compile-time identity for a catalogued Eulerian field.
@ FIELD_ID_UCAT
#define LOCAL
Logging scope definitions for controlling message output.
Definition logging.h:45
PetscErrorCode ComputeBodyForces(UserCtx *user, Vec Rct)
Implementation of ComputeBodyForces().
Definition rhs.c:426
static PetscErrorCode RHSZeroInactiveRows(UserCtx *user, Vec Rhs)
Zeroes the residual rows that carry no unknown: the component a 2D run disables, every component of a...
Definition rhs.c:577
PetscErrorCode ComputeConvection(UserCtx *user, Vec Ucont, Vec Ucat, Vec Conv)
Implementation of ComputeConvection().
Definition rhs.c:96
static PetscErrorCode RHSContravariantSpatialTerms(UserCtx *user, Vec Conv, Vec Visc, Vec Rct)
Contravariant cell-centre components of the spatial terms, Rct = aj F_n .
Definition rhs.c:440
PetscErrorCode ComputeViscous(UserCtx *user, Vec Ucat, Vec Visc)
Implementation of ComputeViscous().
Definition rhs.c:346
static PetscErrorCode RHSFaceTermsWithPressureGradient(UserCtx *user, Vec Rct, Vec Rhs)
Face residual from the ghosted cell-centre terms: the average of Rct over the two cells beside each f...
Definition rhs.c:508
PetscErrorCode Contra2Cart(UserCtx *user)
Reconstructs Cartesian velocity (Ucat) at cell centers from contravariant velocity (Ucont) defined on...
Definition setup.c:3313
PetscErrorCode UpdateLocalGhosts(UserCtx *user, FieldId field_id)
Updates the local vector (including ghost points) from its corresponding global vector.
Definition setup.c:2593
PetscMPIInt rank
Definition variables.h:919
PetscInt _this
Definition variables.h:1175
PetscInt invicid
Definition variables.h:951
Vec lUcont
Definition variables.h:1197
Vec lUcat
Definition variables.h:1197
Here is the call graph for this function:
Here is the caller graph for this function: