PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
 
Loading...
Searching...
No Matches
Macros | Functions
ParticlePhysics.c File Reference
#include "ParticlePhysics.h"
#include "verification_sources.h"
#include "ParticleMotion.h"
#include "interpolation.h"
Include dependency graph for ParticlePhysics.c:

Go to the source code of this file.

Macros

#define ERROR_MSG_BUFFER_SIZE   256
 
#define IN_PATCH(i, j, k)   ((i) >= gxs && (i) < gxs + gxm && (j) >= gys && (j) < gys + gym && (k) >= gzs && (k) < gzs + gzm)
 
#define CELL(a, d, o)   ((d) == 0 ? (a)[k][j][i + (o)] : (d) == 1 ? (a)[k][j + (o)][i] : (a)[k + (o)][j][i])
 

Functions

static PetscErrorCode ComputeCellScalarGradient (UserCtx *user, Vec lscalar, Vec lcount, Vec gradient)
 Cartesian gradient of a cell-centred scalar at every owned physical cell.
 
static PetscReal IEMMixingLength2 (PetscReal aj)
 Squared IEM mixing length of a cell: its volume to the power 2/3, from the reciprocal volume Aj, the volume floored at 1e-14 so a degenerate or cut cell cannot divide by zero.
 
static PetscReal IEMDecayFactor (PetscReal C_model, PetscReal diffusivity, PetscReal delta2, PetscReal dt)
 IEM decay factor exp(-omega dt) over one step, omega = C D / delta2.
 
PetscErrorCode UpdateParticleField (ParticleFieldId field_id, PetscReal dt, PetscReal *psi_io, PetscReal diffusivity, PetscReal mean_val, PetscReal delta2, PetscReal C_model)
 Implementation of UpdateParticleField().
 
static PetscErrorCode ComputeIEMTargetMoments (UserCtx *user, PetscReal dt, PetscReal C_model, Vec lslope, Vec *lmoments_out, Vec *lcentroid_out)
 Per-cell quantities of the bounded, conservative IEM target.
 
PetscErrorCode UpdateFieldForAllParticles (UserCtx *user, ParticleFieldId field_id)
 Implementation of UpdateFieldForAllParticles().
 
PetscErrorCode UpdateAllParticleFields (UserCtx *user)
 Implementation of UpdateAllParticleFields().
 
PetscErrorCode ComputeEulerianDiffusivity (UserCtx *user)
 Implementation of ComputeEulerianDiffusivity().
 
PetscErrorCode ComputeEulerianDiffusivityGradient (UserCtx *user)
 Implementation of ComputeEulerianDiffusivityGradient().
 

Macro Definition Documentation

◆ ERROR_MSG_BUFFER_SIZE

#define ERROR_MSG_BUFFER_SIZE   256

Definition at line 7 of file ParticlePhysics.c.

◆ IN_PATCH

#define IN_PATCH (   i,
  j,
  k 
)    ((i) >= gxs && (i) < gxs + gxm && (j) >= gys && (j) < gys + gym && (k) >= gzs && (k) < gzs + gzm)

◆ CELL

#define CELL (   a,
  d,
  o 
)    ((d) == 0 ? (a)[k][j][i + (o)] : (d) == 1 ? (a)[k][j + (o)][i] : (a)[k + (o)][j][i])

Function Documentation

◆ ComputeCellScalarGradient()

static PetscErrorCode ComputeCellScalarGradient ( UserCtx *  user,
Vec  lscalar,
Vec  lcount,
Vec  gradient 
)
static

Cartesian gradient of a cell-centred scalar at every owned physical cell.

Each computational derivative is the central difference, or at a non-periodic boundary cell the second-order one-sided difference into the domain, mapped to Cartesian components with the cell's metrics. With lcount, a derivative whose stencil reaches a cell holding no particles is zero, so an empty cell's zero mean does not enter. Writes owned entries only; local to this translation unit.

Parameters
[in]userGrid context.
[in]lscalarGhosted scalar, its ghosts current.
[in]lcountGhosted particle count, its ghosts current; or NULL.
[out]gradientGlobal vector on fda.

Definition at line 502 of file ParticlePhysics.c.

503{
504 DMDALocalInfo info = user->info;
505 const PetscInt n[3] = {info.mx, info.my, info.mz};
506 PetscBool periodic[3];
507 GetPeriodicAxes(user->simCtx, periodic);
508 // Grid uses indices 1 to M-2 for physical cells; index 0 and M-1 are boundary holders.
509 const PetscInt lo[3] = {PetscMax(info.xs, 1), PetscMax(info.ys, 1), PetscMax(info.zs, 1)};
510 const PetscInt hi[3] = {PetscMin(info.xs + info.xm, n[0] - 1), PetscMin(info.ys + info.ym, n[1] - 1),
511 PetscMin(info.zs + info.zm, n[2] - 1)};
512 const PetscReal ***f = NULL, ***count = NULL, ***aj = NULL;
513 const Cmpnts ***csi = NULL, ***eta = NULL, ***zet = NULL;
514 Cmpnts ***grad = NULL;
515
516 PetscFunctionBeginUser;
518 PetscCall(DMDAVecGetArrayRead(user->da, lscalar, (void *)&f));
519 if (lcount) PetscCall(DMDAVecGetArrayRead(user->da, lcount, (void *)&count));
520 PetscCall(DMDAVecGetArrayRead(user->fda, user->lCsi, (void *)&csi));
521 PetscCall(DMDAVecGetArrayRead(user->fda, user->lEta, (void *)&eta));
522 PetscCall(DMDAVecGetArrayRead(user->fda, user->lZet, (void *)&zet));
523 PetscCall(DMDAVecGetArrayRead(user->da, user->lAj, (void *)&aj));
524 PetscCall(DMDAVecGetArray(user->fda, gradient, &grad));
525#define CELL(a, d, o) ((d) == 0 ? (a)[k][j][i + (o)] : (d) == 1 ? (a)[k][j + (o)][i] : (a)[k + (o)][j][i])
526 for (PetscInt k = lo[2]; k < hi[2]; k++) {
527 for (PetscInt j = lo[1]; j < hi[1]; j++) {
528 for (PetscInt i = lo[0]; i < hi[0]; i++) {
529 const PetscInt idx[3] = {i, j, k};
530 PetscReal dG[3];
531 for (PetscInt d = 0; d < 3; ++d) {
532 PetscInt o[3] = {-1, 1, 0};
533 PetscReal c[3] = {-1.0, 1.0, 0.0}; /* central: [-1, 0, 1] / 2 */
534 if (!periodic[d] && idx[d] == 1) {
535 o[0] = 0; o[1] = 1; o[2] = 2; c[0] = -3.0; c[1] = 4.0; c[2] = -1.0; /* forward: [-3, 4, -1] / 2 */
536 } else if (!periodic[d] && idx[d] == n[d] - 2) {
537 o[0] = 0; o[1] = -1; o[2] = -2; c[0] = 3.0; c[1] = -4.0; c[2] = 1.0; /* backward: [1, -4, 3] / 2 */
538 }
539 dG[d] = (c[0] * CELL(f, d, o[0]) + c[1] * CELL(f, d, o[1]) + c[2] * CELL(f, d, o[2])) * 0.5;
540 if (count) {
541 for (PetscInt q = 0; q < 3; ++q) if (c[q] != 0.0 && CELL(count, d, o[q]) < 0.5) dG[d] = 0.0;
542 }
543 }
544 TransformScalarDerivativesToPhysical(aj[k][j][i], csi[k][j][i], eta[k][j][i], zet[k][j][i],
545 dG[0], dG[1], dG[2], &grad[k][j][i]);
546 }
547 }
548 }
549#undef CELL
550 PetscCall(DMDAVecRestoreArray(user->fda, gradient, &grad));
551 PetscCall(DMDAVecRestoreArrayRead(user->da, user->lAj, (void *)&aj));
552 PetscCall(DMDAVecRestoreArrayRead(user->fda, user->lZet, (void *)&zet));
553 PetscCall(DMDAVecRestoreArrayRead(user->fda, user->lEta, (void *)&eta));
554 PetscCall(DMDAVecRestoreArrayRead(user->fda, user->lCsi, (void *)&csi));
555 if (lcount) PetscCall(DMDAVecRestoreArrayRead(user->da, lcount, (void *)&count));
556 PetscCall(DMDAVecRestoreArrayRead(user->da, lscalar, (void *)&f));
558 PetscFunctionReturn(0);
559}
#define CELL(a, d, o)
#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
void TransformScalarDerivativesToPhysical(PetscReal jacobian, Cmpnts csi_metrics, Cmpnts eta_metrics, Cmpnts zet_metrics, PetscReal dPhi_dcsi, PetscReal dPhi_deta, PetscReal dPhi_dzet, Cmpnts *gradPhi)
Transforms scalar derivatives from computational space to physical space.
Definition setup.c:3753
static void GetPeriodicAxes(const SimCtx *simCtx, PetscBool periodic[3])
Which grid axes are periodic.
Definition setup.h:361
SimCtx * simCtx
Back-pointer to the master simulation context.
Definition variables.h:1160
DMDALocalInfo info
Definition variables.h:1169
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:

◆ IEMMixingLength2()

static PetscReal IEMMixingLength2 ( PetscReal  aj)
inlinestatic

Squared IEM mixing length of a cell: its volume to the power 2/3, from the reciprocal volume Aj, the volume floored at 1e-14 so a degenerate or cut cell cannot divide by zero.

Definition at line 16 of file ParticlePhysics.c.

17{
18 PetscReal vol = (aj > 1.0e-14) ? (1.0 / aj) : 1.0e-14;
19 if (vol < 1.0e-14) vol = 1.0e-14;
20 return PetscPowReal(vol, 0.6666667);
21}
Here is the caller graph for this function:

◆ IEMDecayFactor()

static PetscReal IEMDecayFactor ( PetscReal  C_model,
PetscReal  diffusivity,
PetscReal  delta2,
PetscReal  dt 
)
inlinestatic

IEM decay factor exp(-omega dt) over one step, omega = C D / delta2.

Definition at line 26 of file ParticlePhysics.c.

27{
28 PetscReal omega = C_model * diffusivity / delta2;
29 return PetscExpReal(-omega * dt);
30}
Here is the caller graph for this function:

◆ UpdateParticleField()

PetscErrorCode UpdateParticleField ( ParticleFieldId  field_id,
PetscReal  dt,
PetscReal *  psi_io,
PetscReal  diffusivity,
PetscReal  mean_val,
PetscReal  delta2,
PetscReal  C_model 
)

Implementation of UpdateParticleField().

Updates a single particle's field based on its state and physics model.

See also
UpdateParticleField()

Definition at line 36 of file ParticlePhysics.c.

43{
44 PetscFunctionBeginUser;
45
46 if (field_id == PARTICLE_FIELD_ID_PSI) {
47 // The IEM model relaxes Psi exponentially toward its target, mean_val, over dt.
48 PetscReal decay = IEMDecayFactor(C_model, diffusivity, delta2, dt);
49
50 PetscReal psi_old = *psi_io;
51 *psi_io = mean_val + (psi_old - mean_val) * decay;
52 }
53
54 PetscFunctionReturn(0);
55}
static PetscReal IEMDecayFactor(PetscReal C_model, PetscReal diffusivity, PetscReal delta2, PetscReal dt)
IEM decay factor exp(-omega dt) over one step, omega = C D / delta2.
@ PARTICLE_FIELD_ID_PSI
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ComputeIEMTargetMoments()

static PetscErrorCode ComputeIEMTargetMoments ( UserCtx *  user,
PetscReal  dt,
PetscReal  C_model,
Vec  lslope,
Vec *  lmoments_out,
Vec *  lcentroid_out 
)
static

Per-cell quantities of the bounded, conservative IEM target.

The target of particle p in cell c is t_p = m_c + zeta_c g_c . (x_p - x_c), with

  • m_c and x_c the cell's Psi mean and particle centroid, each weighted by the particle's relaxation weight c_p = 1 - exp(-omega_p dt). With omega_p the particle's own rate, the cell's Psi total changes by sum_p c_p (t_p - psi_p), which these weights make zero for any zeta_c (Jenny et al. 2001, Eq. 48; Viswanathan, Wang & Pope 2011, Eqs. 20 and 29);
  • g_c the gradient of the plain cell means (lslope); any slope keeps the total;
  • zeta_c in [0, 1] the largest factor that keeps every target of the cell's particles within the extrema of all particles' Psi at the start of the step (Viswanathan et al. 2011, Eqs. 17 and 28), so the update, a convex combination of psi_p and t_p, cannot leave them. A cell's particles may sit on a neighbouring rank, so the sums and the offset extrema are accumulated into ghosted copies and reduced into the owner (ADD, then MAX through the DMDA scatter), and the owner's results are read back through ghosted copies. A cell whose weights sum to zero (every particle with zero rate) keeps its plain mean; its particles do not change. Collective.
    Parameters
    [in]userContext; lPsi, lAj, ParticleCount current.
    [in]dtStep.
    [in]C_modelIEM constant.
    [in]lslopeGhosted gradient of the plain cell means, its ghosts current.
    [out]lmoments_outGhosted fda vector: (m_c, zeta_c, 0) per cell; restore with DMRestoreLocalVector.
    [out]lcentroid_outGhosted fda vector: x_c per cell; restore with DMRestoreLocalVector.

Definition at line 81 of file ParticlePhysics.c.

83{
84 DM swarm = user->swarm, fda = user->fda;
85 DMDALocalInfo info = user->info;
86 PetscInt n_local = 0, gxs, gys, gzs, gxm, gym, gzm;
87 const PetscReal *psi = NULL, *diff = NULL, *pos = NULL;
88 const PetscInt *cell = NULL;
89 PetscReal bounds[2] = {PETSC_MAX_REAL, PETSC_MAX_REAL}; /* min psi, -max psi */
90 Vec lw = NULL, lwx = NULL, gw = NULL, gwx = NULL, gmom = NULL, gcen = NULL, lext = NULL, gext = NULL;
91 Vec lmom = NULL, lcen = NULL;
92 VecScatter gtol = NULL;
93 const PetscReal ***aj = NULL, ***mean = NULL, ***count = NULL;
94 Cmpnts ***w = NULL, ***wx = NULL, ***mom = NULL, ***cen = NULL, ***ext = NULL;
95 const Cmpnts ***slope = NULL, ***lcen_arr = NULL, ***cent = NULL;
96 /* Owned physical cells: indices 1 to M-2; 0 and M-1 hold boundary values. */
97 const PetscInt lo[3] = {PetscMax(info.xs, 1), PetscMax(info.ys, 1), PetscMax(info.zs, 1)};
98 const PetscInt hi[3] = {PetscMin(info.xs + info.xm, info.mx - 1), PetscMin(info.ys + info.ym, info.my - 1),
99 PetscMin(info.zs + info.zm, info.mz - 1)};
100
101 PetscFunctionBeginUser;
103 PetscCall(DMDAGetGhostCorners(fda, &gxs, &gys, &gzs, &gxm, &gym, &gzm));
104#define IN_PATCH(i, j, k) ((i) >= gxs && (i) < gxs + gxm && (j) >= gys && (j) < gys + gym && (k) >= gzs && (k) < gzs + gzm)
105 PetscCall(DMSwarmGetLocalSize(swarm, &n_local));
106 PetscCall(DMSwarmGetField(swarm, ParticleFieldName(PARTICLE_FIELD_ID_PSI), NULL, NULL, (void **)&psi));
107 PetscCall(DMSwarmGetField(swarm, ParticleFieldName(PARTICLE_FIELD_ID_DIFFUSIVITY), NULL, NULL, (void **)&diff));
108 PetscCall(DMSwarmGetField(swarm, ParticleFieldName(PARTICLE_FIELD_ID_POSITION), NULL, NULL, (void **)&pos));
109 PetscCall(DMSwarmGetField(swarm, ParticleFieldName(PARTICLE_FIELD_ID_CELL_ID), NULL, NULL, (void **)&cell));
110
111 /* Pass 1: the Psi extrema, and per cell the weight sums (sum c, sum c psi) and sum c x. */
112 PetscCall(DMGetLocalVector(fda, &lw));
113 PetscCall(DMGetLocalVector(fda, &lwx));
114 PetscCall(VecSet(lw, 0.0));
115 PetscCall(VecSet(lwx, 0.0));
116 PetscCall(DMDAVecGetArrayRead(user->da, user->lAj, (void *)&aj));
117 PetscCall(DMDAVecGetArray(fda, lw, &w));
118 PetscCall(DMDAVecGetArray(fda, lwx, &wx));
119 for (PetscInt p = 0; p < n_local; ++p) {
120 const PetscInt i = cell[3*p] + 1, j = cell[3*p+1] + 1, k = cell[3*p+2] + 1;
121 bounds[0] = PetscMin(bounds[0], psi[p]);
122 bounds[1] = PetscMin(bounds[1], -psi[p]);
123 if (!IN_PATCH(i, j, k)) continue;
124 const PetscReal c = 1.0 - IEMDecayFactor(C_model, diff[p], IEMMixingLength2(aj[k][j][i]), dt);
125 w[k][j][i].x += c;
126 w[k][j][i].y += c * psi[p];
127 wx[k][j][i].x += c * pos[3*p];
128 wx[k][j][i].y += c * pos[3*p+1];
129 wx[k][j][i].z += c * pos[3*p+2];
130 }
131 PetscCall(DMDAVecRestoreArray(fda, lwx, &wx));
132 PetscCall(DMDAVecRestoreArray(fda, lw, &w));
133 PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, bounds, 2, MPIU_REAL, MPIU_MIN, PETSC_COMM_WORLD));
134 const PetscReal psi_min = bounds[0], psi_max = -bounds[1];
135
136 PetscCall(DMGetGlobalVector(fda, &gw));
137 PetscCall(DMGetGlobalVector(fda, &gwx));
138 PetscCall(VecSet(gw, 0.0));
139 PetscCall(VecSet(gwx, 0.0));
140 PetscCall(DMLocalToGlobal(fda, lw, ADD_VALUES, gw));
141 PetscCall(DMLocalToGlobal(fda, lwx, ADD_VALUES, gwx));
142 PetscCall(DMRestoreLocalVector(fda, &lwx));
143 PetscCall(DMRestoreLocalVector(fda, &lw));
144
145 /* The owner forms the weighted mean and centroid. */
146 PetscCall(DMGetGlobalVector(fda, &gmom));
147 PetscCall(DMGetGlobalVector(fda, &gcen));
148 PetscCall(VecSet(gmom, 0.0));
149 PetscCall(VecSet(gcen, 0.0));
150 PetscCall(DMDAVecGetArrayRead(user->da, user->lPsi, (void *)&mean));
151 PetscCall(DMDAVecGetArrayRead(user->da, user->ParticleCount, (void *)&count));
152 PetscCall(DMDAVecGetArrayRead(fda, user->lCent, (void *)&cent));
153 PetscCall(DMDAVecGetArrayRead(fda, gw, (void *)&w));
154 PetscCall(DMDAVecGetArrayRead(fda, gwx, (void *)&wx));
155 PetscCall(DMDAVecGetArray(fda, gmom, &mom));
156 PetscCall(DMDAVecGetArray(fda, gcen, &cen));
157 for (PetscInt k = lo[2]; k < hi[2]; ++k) {
158 for (PetscInt j = lo[1]; j < hi[1]; ++j) {
159 for (PetscInt i = lo[0]; i < hi[0]; ++i) {
160 const PetscReal sw = w[k][j][i].x;
161 if (count[k][j][i] > 0.5 && sw > 0.0) {
162 mom[k][j][i].x = w[k][j][i].y / sw;
163 cen[k][j][i].x = wx[k][j][i].x / sw;
164 cen[k][j][i].y = wx[k][j][i].y / sw;
165 cen[k][j][i].z = wx[k][j][i].z / sw;
166 } else {
167 mom[k][j][i].x = mean[k][j][i];
168 cen[k][j][i] = cent[k][j][i];
169 }
170 }
171 }
172 }
173 PetscCall(DMDAVecRestoreArray(fda, gcen, &cen));
174 PetscCall(DMDAVecRestoreArray(fda, gmom, &mom));
175 PetscCall(DMDAVecRestoreArrayRead(fda, gwx, (void *)&wx));
176 PetscCall(DMDAVecRestoreArrayRead(fda, gw, (void *)&w));
177 PetscCall(DMDAVecRestoreArrayRead(fda, user->lCent, (void *)&cent));
178 PetscCall(DMDAVecRestoreArrayRead(user->da, user->ParticleCount, (void *)&count));
179 PetscCall(DMDAVecRestoreArrayRead(user->da, user->lPsi, (void *)&mean));
180 PetscCall(DMRestoreGlobalVector(fda, &gwx));
181 PetscCall(DMRestoreGlobalVector(fda, &gw));
182 PetscCall(DMGetLocalVector(fda, &lcen));
183 PetscCall(DMGlobalToLocal(fda, gcen, INSERT_VALUES, lcen));
184 PetscCall(DMGetLocalVector(fda, &lmom));
185 PetscCall(DMGlobalToLocal(fda, gmom, INSERT_VALUES, lmom));
186
187 /* Pass 2: per cell the largest offset g . (x - x_c) above and below zero. Both are >= 0 over a
188 cell's particles, since x_c is their weighted centroid, so an untouched 0 never wins the MAX. */
189 PetscCall(DMGetLocalVector(fda, &lext));
190 PetscCall(VecSet(lext, 0.0));
191 PetscCall(DMDAVecGetArrayRead(fda, lslope, (void *)&slope));
192 PetscCall(DMDAVecGetArrayRead(fda, lcen, (void *)&lcen_arr));
193 PetscCall(DMDAVecGetArray(fda, lext, &ext));
194 for (PetscInt p = 0; p < n_local; ++p) {
195 const PetscInt i = cell[3*p] + 1, j = cell[3*p+1] + 1, k = cell[3*p+2] + 1;
196 if (!IN_PATCH(i, j, k)) continue;
197 const Cmpnts g = slope[k][j][i], xc = lcen_arr[k][j][i];
198 const PetscReal d = g.x * (pos[3*p] - xc.x) + g.y * (pos[3*p+1] - xc.y) + g.z * (pos[3*p+2] - xc.z);
199 ext[k][j][i].x = PetscMax(ext[k][j][i].x, d);
200 ext[k][j][i].y = PetscMax(ext[k][j][i].y, -d);
201 }
202 PetscCall(DMDAVecRestoreArray(fda, lext, &ext));
203 PetscCall(DMDAVecRestoreArrayRead(fda, lcen, (void *)&lcen_arr));
204 PetscCall(DMDAVecRestoreArrayRead(fda, lslope, (void *)&slope));
205 /* DMLocalToGlobal takes only ADD and INSERT on a DMDA; its own scatter, reversed, takes MAX. */
206 PetscCall(DMGetGlobalVector(fda, &gext));
207 PetscCall(VecSet(gext, 0.0));
208 PetscCall(DMDAGetScatter(fda, &gtol, NULL));
209 PetscCall(VecScatterBegin(gtol, lext, gext, MAX_VALUES, SCATTER_REVERSE));
210 PetscCall(VecScatterEnd(gtol, lext, gext, MAX_VALUES, SCATTER_REVERSE));
211 PetscCall(DMRestoreLocalVector(fda, &lext));
212
213 /* The owner sets zeta: the largest factor in [0, 1] keeping m_c + zeta d within [psi_min, psi_max]. */
214 PetscCall(DMDAVecGetArrayRead(fda, gext, (void *)&ext));
215 PetscCall(DMDAVecGetArray(fda, gmom, &mom));
216 for (PetscInt k = lo[2]; k < hi[2]; ++k) {
217 for (PetscInt j = lo[1]; j < hi[1]; ++j) {
218 for (PetscInt i = lo[0]; i < hi[0]; ++i) {
219 const PetscReal m = mom[k][j][i].x;
220 PetscReal zeta = 1.0;
221 if (ext[k][j][i].x > 0.0) zeta = PetscMin(zeta, (psi_max - m) / ext[k][j][i].x);
222 if (ext[k][j][i].y > 0.0) zeta = PetscMin(zeta, (m - psi_min) / ext[k][j][i].y);
223 mom[k][j][i].y = PetscMax(zeta, 0.0);
224 }
225 }
226 }
227 PetscCall(DMDAVecRestoreArray(fda, gmom, &mom));
228 PetscCall(DMDAVecRestoreArrayRead(fda, gext, (void *)&ext));
229 PetscCall(DMRestoreGlobalVector(fda, &gext));
230 PetscCall(DMGlobalToLocal(fda, gmom, INSERT_VALUES, lmom));
231 PetscCall(DMRestoreGlobalVector(fda, &gcen));
232 PetscCall(DMRestoreGlobalVector(fda, &gmom));
233#undef IN_PATCH
234
235 PetscCall(DMDAVecRestoreArrayRead(user->da, user->lAj, (void *)&aj));
236 PetscCall(DMSwarmRestoreField(swarm, ParticleFieldName(PARTICLE_FIELD_ID_CELL_ID), NULL, NULL, (void **)&cell));
237 PetscCall(DMSwarmRestoreField(swarm, ParticleFieldName(PARTICLE_FIELD_ID_POSITION), NULL, NULL, (void **)&pos));
238 PetscCall(DMSwarmRestoreField(swarm, ParticleFieldName(PARTICLE_FIELD_ID_DIFFUSIVITY), NULL, NULL, (void **)&diff));
239 PetscCall(DMSwarmRestoreField(swarm, ParticleFieldName(PARTICLE_FIELD_ID_PSI), NULL, NULL, (void **)&psi));
240 *lmoments_out = lmom;
241 *lcentroid_out = lcen;
243 PetscFunctionReturn(0);
244}
static PetscReal IEMMixingLength2(PetscReal aj)
Squared IEM mixing length of a cell: its volume to the power 2/3, from the reciprocal volume Aj,...
#define IN_PATCH(i, j, k)
const char * ParticleFieldName(ParticleFieldId field_id)
Return the canonical PETSc DMSwarm name for an ID.
@ PARTICLE_FIELD_ID_POSITION
@ PARTICLE_FIELD_ID_CELL_ID
@ PARTICLE_FIELD_ID_DIFFUSIVITY
Vec lCent
Definition variables.h:1244
PetscScalar x
Definition variables.h:145
PetscScalar z
Definition variables.h:145
Vec ParticleCount
Definition variables.h:1276
PetscScalar y
Definition variables.h:145
Here is the call graph for this function:
Here is the caller graph for this function:

◆ UpdateFieldForAllParticles()

PetscErrorCode UpdateFieldForAllParticles ( UserCtx *  user,
ParticleFieldId  field_id 
)

Implementation of UpdateFieldForAllParticles().

Loops over all local particles and updates a specified field.

See also
UpdateFieldForAllParticles()

Definition at line 250 of file ParticlePhysics.c.

251{
252 PetscErrorCode ierr;
253 DM swarm = user->swarm;
254 DM da = user->da;
255 PetscInt n_local;
256 PetscReal dt = user->simCtx->dt;
257 PetscReal C_IEM = user->simCtx->iem_constant;
258
259 PetscReal *psi_arr = NULL;
260 PetscReal *diff_arr = NULL;
261 PetscReal *pos_arr = NULL;
262 PetscInt *cell_arr = NULL;
263 PetscInt64 *pid_arr = NULL;
264
265 PetscReal ***grid_aj = NULL;
266 Cmpnts ***grid_slope = NULL, ***grid_centroid = NULL, ***grid_moments = NULL;
267 Vec lslope = NULL, lcentroid = NULL, lmoments = NULL;
268
269 PetscBool accessed_eulerian = PETSC_FALSE;
270 const ParticleFieldDescriptor *descriptor = NULL;
271 const char *fieldName = NULL;
272
273 PetscFunctionBeginUser;
275
276 ierr = ParticleFieldGetDescriptor(field_id, &descriptor); CHKERRQ(ierr);
277 PetscCheck((descriptor->capabilities & PARTICLE_FIELD_CAPABILITY_MODEL_UPDATE) != 0,
278 PETSC_COMM_SELF, PETSC_ERR_SUP,
279 "Particle field '%s' has no registered model-update kernel.",
280 descriptor->canonical_name);
281 fieldName = descriptor->canonical_name;
282
283 // Relax toward the mean of the particles where they are now. The Eulerian Psi otherwise
284 // holds the previous step's scatter, taken before the particles moved and migrated; the
285 // scatter recounts the particles per cell itself. Collective, so it comes before the early
286 // return of ranks with no particles.
287 if (field_id == PARTICLE_FIELD_ID_PSI) {
288 Vec slope = NULL;
289
290 // Psi relaxation requires ghosted Eulerian mean and Jacobian fields.
291 PetscCheck(user->lPsi && user->lAj, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "UserCtx lPsi or lAj not initialized.");
292 ierr = ScatterAllParticleFieldsToEulerFields(user); CHKERRQ(ierr);
293 // The target is the cell's limited linear reconstruction m + zeta g . (x - xc), with m and
294 // xc weighted by each particle's relaxation weight so each cell's total is kept, and zeta
295 // keeping every target within the particles' Psi extrema (ComputeIEMTargetMoments).
296 // Relaxing to the stepped mean alone adds transport that does not vanish with refinement.
297 // A particle's cell is stored one index past its origin node, so the cell fields are read
298 // through ghosted copies.
299 // The scatter's count holds only this rank's particles in its ghost cells; the empty-cell
300 // test reads neighbours across subdomain seams, so their totals are fetched first.
301 ierr = UpdateLocalGhosts(user, FIELD_ID_PARTICLE_COUNT); CHKERRQ(ierr);
302 ierr = DMGetGlobalVector(user->fda, &slope); CHKERRQ(ierr);
303 ierr = ComputeCellScalarGradient(user, user->lPsi, user->lParticleCount, slope); CHKERRQ(ierr);
304 ierr = DMGetLocalVector(user->fda, &lslope); CHKERRQ(ierr);
305 ierr = DMGlobalToLocal(user->fda, slope, INSERT_VALUES, lslope); CHKERRQ(ierr);
306 ierr = DMRestoreGlobalVector(user->fda, &slope); CHKERRQ(ierr);
307 ierr = ComputeIEMTargetMoments(user, dt, C_IEM, lslope, &lmoments, &lcentroid); CHKERRQ(ierr);
308 }
309
310 ierr = DMSwarmGetLocalSize(swarm, &n_local); CHKERRQ(ierr);
311 if (n_local == 0) {
312 if (lslope) { ierr = DMRestoreLocalVector(user->fda, &lslope); CHKERRQ(ierr); }
313 if (lcentroid) { ierr = DMRestoreLocalVector(user->fda, &lcentroid); CHKERRQ(ierr); }
314 if (lmoments) { ierr = DMRestoreLocalVector(user->fda, &lmoments); CHKERRQ(ierr); }
316 PetscFunctionReturn(0);
317 }
318
319 ierr = DMSwarmGetField(swarm, fieldName, NULL, NULL, (void**)&psi_arr); CHKERRQ(ierr);
320 ierr = DMSwarmGetField(swarm, ParticleFieldName(PARTICLE_FIELD_ID_PID), NULL, NULL, (void**)&pid_arr); CHKERRQ(ierr);
321
322 if (field_id == PARTICLE_FIELD_ID_PSI) {
323 ierr = DMSwarmGetField(swarm, ParticleFieldName(PARTICLE_FIELD_ID_DIFFUSIVITY), NULL, NULL, (void**)&diff_arr); CHKERRQ(ierr);
324 ierr = DMSwarmGetField(swarm, ParticleFieldName(PARTICLE_FIELD_ID_CELL_ID), NULL, NULL, (void**)&cell_arr); CHKERRQ(ierr);
325 ierr = DMSwarmGetField(swarm, ParticleFieldName(PARTICLE_FIELD_ID_POSITION), NULL, NULL, (void**)&pos_arr); CHKERRQ(ierr);
326
327 ierr = DMDAVecGetArrayRead(da, user->lAj, &grid_aj); CHKERRQ(ierr);
328 ierr = DMDAVecGetArrayRead(user->fda, lslope, &grid_slope); CHKERRQ(ierr);
329 ierr = DMDAVecGetArrayRead(user->fda, lcentroid, &grid_centroid); CHKERRQ(ierr);
330 ierr = DMDAVecGetArrayRead(user->fda, lmoments, &grid_moments); CHKERRQ(ierr);
331 accessed_eulerian = PETSC_TRUE;
332 }
333
334 // The filter width depends only on the cell, and the particles are stored in cell order,
335 // so it is recomputed only when the cell changes.
336 PetscInt width_cell[3] = {-1, -1, -1};
337 PetscReal p_delta2 = 1.0;
338 for (PetscInt p = 0; p < n_local; ++p) {
339 PetscReal p_diff = 0.0;
340 PetscReal p_mean = 0.0;
341
342 if (field_id == PARTICLE_FIELD_ID_PSI) {
343 // Cell IDs are 0-based geometric indices; cell-centred storage starts at index 1
344 // (index 0 is the boundary slot), the shift the scatter applies when it writes lPsi.
345 PetscInt i = cell_arr[3*p + 0] + 1;
346 PetscInt j = cell_arr[3*p + 1] + 1;
347 PetscInt k = cell_arr[3*p + 2] + 1;
348
349 const Cmpnts g = grid_slope[k][j][i], c = grid_centroid[k][j][i], mz = grid_moments[k][j][i];
350 p_diff = diff_arr[p];
351 p_mean = mz.x + mz.y * (g.x * (pos_arr[3*p + 0] - c.x) + g.y * (pos_arr[3*p + 1] - c.y)
352 + g.z * (pos_arr[3*p + 2] - c.z));
353
354 if (i != width_cell[0] || j != width_cell[1] || k != width_cell[2]) {
355 // Aj is the reciprocal cell volume in the curvilinear-grid representation.
356 p_delta2 = IEMMixingLength2(grid_aj[k][j][i]);
357 width_cell[0] = i; width_cell[1] = j; width_cell[2] = k;
358 }
359 }
360
361 const PetscReal psi_old = psi_arr[p];
362 ierr = UpdateParticleField(field_id, dt, &psi_arr[p], p_diff, p_mean, p_delta2, C_IEM);
363 CHKERRQ(ierr);
364 LOG_PARTICLE(user->simCtx, pid_arr[p], "mix",
365 "%s %.10e -> %.10e | target %.10e | diffusivity %.6e mixing length^2 %.6e C %.4f dt %.6e\n",
366 fieldName, psi_old, psi_arr[p], p_mean, p_diff, p_delta2, C_IEM, dt);
367 }
368
369 ierr = DMSwarmRestoreField(swarm, ParticleFieldName(PARTICLE_FIELD_ID_PID), NULL, NULL, (void**)&pid_arr); CHKERRQ(ierr);
370 ierr = DMSwarmRestoreField(swarm, fieldName, NULL, NULL, (void**)&psi_arr); CHKERRQ(ierr);
371
372 if (field_id == PARTICLE_FIELD_ID_PSI) {
373 ierr = DMSwarmRestoreField(swarm, ParticleFieldName(PARTICLE_FIELD_ID_DIFFUSIVITY), NULL, NULL, (void**)&diff_arr); CHKERRQ(ierr);
374 ierr = DMSwarmRestoreField(swarm, ParticleFieldName(PARTICLE_FIELD_ID_CELL_ID), NULL, NULL, (void**)&cell_arr); CHKERRQ(ierr);
375 ierr = DMSwarmRestoreField(swarm, ParticleFieldName(PARTICLE_FIELD_ID_POSITION), NULL, NULL, (void**)&pos_arr); CHKERRQ(ierr);
376 }
377
378 if (accessed_eulerian) {
379 ierr = DMDAVecRestoreArrayRead(da, user->lAj, &grid_aj); CHKERRQ(ierr);
380 ierr = DMDAVecRestoreArrayRead(user->fda, lslope, &grid_slope); CHKERRQ(ierr);
381 ierr = DMDAVecRestoreArrayRead(user->fda, lcentroid, &grid_centroid); CHKERRQ(ierr);
382 ierr = DMDAVecRestoreArrayRead(user->fda, lmoments, &grid_moments); CHKERRQ(ierr);
383 ierr = DMRestoreLocalVector(user->fda, &lslope); CHKERRQ(ierr);
384 ierr = DMRestoreLocalVector(user->fda, &lcentroid); CHKERRQ(ierr);
385 ierr = DMRestoreLocalVector(user->fda, &lmoments); CHKERRQ(ierr);
386 }
387
388 LOG_ALLOW(GLOBAL, LOG_DEBUG, "Updated particle physics for field '%s'.\n", fieldName);
389
391 PetscFunctionReturn(0);
392}
static PetscErrorCode ComputeCellScalarGradient(UserCtx *user, Vec lscalar, Vec lcount, Vec gradient)
Cartesian gradient of a cell-centred scalar at every owned physical cell.
static PetscErrorCode ComputeIEMTargetMoments(UserCtx *user, PetscReal dt, PetscReal C_model, Vec lslope, Vec *lmoments_out, Vec *lcentroid_out)
Per-cell quantities of the bounded, conservative IEM target.
PetscErrorCode UpdateParticleField(ParticleFieldId field_id, PetscReal dt, PetscReal *psi_io, PetscReal diffusivity, PetscReal mean_val, PetscReal delta2, PetscReal C_model)
Implementation of UpdateParticleField().
@ FIELD_ID_PARTICLE_COUNT
PetscErrorCode ScatterAllParticleFieldsToEulerFields(UserCtx *user)
Scatters a predefined set of particle fields to their corresponding Eulerian fields.
#define LOG_PARTICLE(simCtx, pid, stage, fmt,...)
Reports one particle at one stage of a step: the consolidated per-particle line.
Definition logging.h:355
#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
@ LOG_DEBUG
Detailed debugging information.
Definition logging.h:32
@ PARTICLE_FIELD_ID_PID
@ PARTICLE_FIELD_CAPABILITY_MODEL_UPDATE
PetscErrorCode ParticleFieldGetDescriptor(ParticleFieldId field_id, const ParticleFieldDescriptor **descriptor)
Return immutable metadata for a valid particle field ID.
Immutable metadata for one persistent particle field.
PetscErrorCode UpdateLocalGhosts(UserCtx *user, FieldId field_id)
Updates the local vector (including ghost points) from its corresponding global vector.
Definition setup.c:2593
PetscReal iem_constant
IEM mixing constant C_IEM in Omega = C_IEM Gamma / Delta^2 (default 2.0).
Definition variables.h:1006
PetscReal dt
Definition variables.h:933
Vec lParticleCount
Definition variables.h:1276
Here is the call graph for this function:
Here is the caller graph for this function:

◆ UpdateAllParticleFields()

PetscErrorCode UpdateAllParticleFields ( UserCtx *  user)

Implementation of UpdateAllParticleFields().

Orchestrates the update of all physical properties for particles.

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

See also
UpdateAllParticleFields()

Definition at line 400 of file ParticlePhysics.c.

401{
402 PetscErrorCode ierr;
403 PetscFunctionBeginUser;
405
406 LOG_ALLOW(GLOBAL, LOG_INFO, "Updating all particle physical properties...\n");
407
409 // Verification profiles define Psi exactly, so bypass the model-driven update.
410 ierr = ApplyVerificationScalarOverrideToParticles(user); CHKERRQ(ierr);
411 LOG_ALLOW(GLOBAL, LOG_INFO, "Verification scalar override active; skipped model-driven Psi update.\n");
413 PetscFunctionReturn(0);
414 }
415
416 /* C_IEM = 0 switches micromixing off. The update is skipped rather than run with a
417 * zero rate: mean + (psi - mean) * 1 need not round-trip to psi exactly, and a passive
418 * label must stay bit-identical. */
419 if (user->simCtx->iem_constant == 0.0) {
420 LOG_ALLOW(GLOBAL, LOG_INFO, "IEM constant is zero; Psi is carried unchanged.\n");
421 } else {
422 ierr = UpdateFieldForAllParticles(user, PARTICLE_FIELD_ID_PSI); CHKERRQ(ierr);
423 }
424
425 LOG_ALLOW(GLOBAL, LOG_INFO, "All particle physical properties updated.\n");
426
428 PetscFunctionReturn(0);
429}
PetscErrorCode UpdateFieldForAllParticles(UserCtx *user, ParticleFieldId field_id)
Implementation of UpdateFieldForAllParticles().
@ LOG_INFO
Informational messages about program execution.
Definition logging.h:31
PetscErrorCode ApplyVerificationScalarOverrideToParticles(UserCtx *user)
Populates the particle Psi field from a verification-only source override.
PetscBool VerificationScalarOverrideActive(const SimCtx *simCtx)
Reports whether a verification-only scalar override is active.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ComputeEulerianDiffusivity()

PetscErrorCode ComputeEulerianDiffusivity ( UserCtx *  user)

Implementation of ComputeEulerianDiffusivity().

Computes the effective diffusivity scalar field (Gamma_eff) on the Eulerian grid.

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

See also
ComputeEulerianDiffusivity()

Definition at line 437 of file ParticlePhysics.c.

438{
439 const SimCtx *simCtx = user->simCtx;
440 const DMDALocalInfo info = user->info;
441 // Grid uses indices 1 to M-2 for physical cells; index 0 and M-1 are boundary holders.
442 const PetscInt lo[3] = {PetscMax(info.xs, 1), PetscMax(info.ys, 1), PetscMax(info.zs, 1)};
443 const PetscInt hi[3] = {PetscMin(info.xs + info.xm, info.mx - 1), PetscMin(info.ys + info.ym, info.my - 1),
444 PetscMin(info.zs + info.zm, info.mz - 1)};
445 PetscReal ***diff_arr = NULL;
446 const PetscReal ***nut_arr = NULL;
447
448 PetscFunctionBeginUser;
450
454 PetscFunctionReturn(0);
455 }
456
457 // Gamma = nu / Sc + nu_t / Sc_t, with nu = 1/Re (0 when Re is unset or not positive) and the
458 // Schmidt numbers 1 and 0.7 when unset. The eddy viscosity enters only with LES; a dynamic
459 // model can give a slightly negative one, which is clipped to 0 so the Brownian amplitude
460 // sqrt(2 Gamma dt) stays real.
461 const PetscReal nu_molecular = (simCtx->ren > 1.0e-12) ? 1.0 / simCtx->ren : 0.0;
462 const PetscReal Sc = (simCtx->schmidt_number > 1.0e-6) ? simCtx->schmidt_number : 1.0;
463 const PetscReal Sct = (simCtx->Turbulent_schmidt_number > 1.0e-6) ? simCtx->Turbulent_schmidt_number : 0.7;
464 const PetscReal gamma_molecular = nu_molecular / Sc;
465 const PetscBool use_turbulence_model = simCtx->les ? PETSC_TRUE : PETSC_FALSE;
466
467 PetscCall(DMDAVecGetArray(user->da, user->Diffusivity, &diff_arr));
468 if (use_turbulence_model) PetscCall(DMDAVecGetArrayRead(user->da, user->Nu_t, (void *)&nut_arr));
469 for (PetscInt k = lo[2]; k < hi[2]; k++) {
470 for (PetscInt j = lo[1]; j < hi[1]; j++) {
471 for (PetscInt i = lo[0]; i < hi[0]; i++) {
472 PetscReal gamma_turbulent = 0.0;
473 if (use_turbulence_model) gamma_turbulent = PetscMax(nut_arr[k][j][i], 0.0) / Sct;
474 diff_arr[k][j][i] = gamma_molecular + gamma_turbulent;
475 }
476 }
477 }
478 if (use_turbulence_model) PetscCall(DMDAVecRestoreArrayRead(user->da, user->Nu_t, (void *)&nut_arr));
479 PetscCall(DMDAVecRestoreArray(user->da, user->Diffusivity, &diff_arr));
480
481 // The diffusivity gradient and the particle interpolation read the periodic dummy cells and
482 // the ghosts.
483 const FieldId periodic_fields[] = {FIELD_ID_DIFFUSIVITY};
484 PetscCall(SynchronizePeriodicCellFields(user, 1, periodic_fields));
485 PetscCall(UpdateLocalGhosts(user, FIELD_ID_DIFFUSIVITY));
487 PetscFunctionReturn(0);
488}
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_DIFFUSIVITY
PetscReal schmidt_number
Definition variables.h:1005
PetscReal Turbulent_schmidt_number
Definition variables.h:1005
PetscReal ren
Definition variables.h:965
Vec Diffusivity
Definition variables.h:1200
PetscInt les
Active LES closure; an LESModelType value.
Definition variables.h:1041
The master context for the entire simulation.
Definition variables.h:916
PetscErrorCode ApplyVerificationDiffusivityOverride(UserCtx *user)
Populates the Eulerian diffusivity field from a verification-only source override.
PetscBool VerificationDiffusivityOverrideActive(const SimCtx *simCtx)
Reports whether a verification-only diffusivity override is active.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ComputeEulerianDiffusivityGradient()

PetscErrorCode ComputeEulerianDiffusivityGradient ( UserCtx *  user)

Implementation of ComputeEulerianDiffusivityGradient().

Computes the Eulerian gradient of the effective diffusivity field.

See also
ComputeEulerianDiffusivityGradient()

Definition at line 565 of file ParticlePhysics.c.

566{
567 PetscFunctionBeginUser;
569 // Central differences read the neighbours across subdomain and periodic seams.
570 PetscCall(UpdateLocalGhosts(user, FIELD_ID_DIFFUSIVITY));
571 PetscCall(ComputeCellScalarGradient(user, user->lDiffusivity, NULL, user->DiffusivityGradient));
572 // A particle within half a cell of a periodic seam interpolates across it, reading the
573 // periodic dummy cells, which the gradient's owned-cell loop does not write.
574 const FieldId periodic_fields[] = {FIELD_ID_DIFFUSIVITY_GRADIENT};
575 PetscCall(SynchronizePeriodicCellFields(user, 1, periodic_fields));
576 // Particles near subdomain boundaries interpolate the gradient, so its ghosts are filled.
579 PetscFunctionReturn(0);
580}
@ FIELD_ID_DIFFUSIVITY_GRADIENT
Vec DiffusivityGradient
Definition variables.h:1201
Vec lDiffusivity
Definition variables.h:1200
Here is the call graph for this function:
Here is the caller graph for this function: