PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
 
Loading...
Searching...
No Matches
Macros | Typedefs | Functions
interpolation.h File Reference
#include <petscpf.h>
#include <petscdmswarm.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <petsctime.h>
#include <petscdmcomposite.h>
#include <petscerror.h>
#include <petscsys.h>
#include "variables.h"
#include "ParticleSwarm.h"
#include "walkingsearch.h"
#include "grid.h"
#include "logging.h"
#include "io.h"
#include "setup.h"
Include dependency graph for interpolation.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define NUM_WEIGHTS   8
 
#define InterpolateFieldFromCornerToCenter(field, centfield, user)
 Generic macro to call the appropriate interpolation function based on the field type.
 
#define InterpolateFieldFromCenterToCorner(blockSize, centfield_ptr, corner_ptr, user_ctx)
 Macro to dispatch to the correct scalar or vector center-to-corner function based on a runtime block size variable.
 
#define InterpolateCornerToFaceCenter(corner_arr, faceX_arr, faceY_arr, faceZ_arr, user_ctx)
 A type-generic macro that interpolates a field from corner nodes to all face centers.
 
#define TrilinearInterpolation(fieldPtr, i, j, k, a1, a2, a3, outPtr)
 Macro that calls either the scalar or vector trilinear interpolation function based on the type of the fieldPtr parameter (3D array).
 

Typedefs

typedef struct VelocityPointInterpolator VelocityPointInterpolator
 The arrays InterpolateVelocityAtPoint() reads, held from Begin to End.
 

Functions

PetscErrorCode TrilinearInterpolation_Scalar (PetscReal ***fieldScal, PetscInt i, PetscInt j, PetscInt k, PetscReal a1, PetscReal a2, PetscReal a3, PetscReal *val)
 Trilinear interpolation of a scalar from the eight entries (i..i+1, j..j+1, k..k+1).
 
PetscErrorCode TrilinearInterpolation_Vector (Cmpnts ***fieldVec, PetscInt i, PetscInt j, PetscInt k, PetscReal a1, PetscReal a2, PetscReal a3, Cmpnts *vec)
 Trilinear interpolation of a vector from the eight entries (i..i+1, j..j+1, k..k+1).
 
PetscErrorCode InterpolateVelocityAtPointBegin (UserCtx *user, VelocityPointInterpolator **interp)
 Opens the arrays for evaluating the particle velocity at points, with the configured interpolation method.
 
PetscErrorCode InterpolateVelocityAtPoint (const VelocityPointInterpolator *interp, const PetscInt cell[3], const PetscReal weights[3], Cmpnts point, Cmpnts *u)
 The velocity a particle at a point would be given: the value the swarm pass of the configured method computes there.
 
PetscErrorCode InterpolateVelocityAtPointEnd (VelocityPointInterpolator **interp)
 Closes the arrays InterpolateVelocityAtPointBegin() opened.
 
PetscErrorCode InterpolateAllFieldsToSwarm (UserCtx *user)
 Interpolates the Eulerian fields the particles carry to every local particle.
 
PetscErrorCode InterpolateFieldFromCornerToCenter_Scalar (PetscReal ***field_arr, PetscReal ***centfield_arr, UserCtx *user)
 Safely interpolate a scalar field from corner nodes (from the coordinate DM) to cell centers (from the cell-centered DM) using the provided UserCtx.
 
PetscErrorCode InterpolateFieldFromCornerToCenter_Vector (Cmpnts ***field_arr, Cmpnts ***centfield_arr, UserCtx *user)
 Safely interpolate a vector field from corner nodes (from the coordinate DM) to cell centers (from the cell-centered DM) using the provided UserCtx.
 
PetscErrorCode TestCornerToCenterInterpolation (UserCtx *user)
 Tests the InterpolateFieldFromCornerToCenter function by reproducing the Cent vector.
 
PetscErrorCode InterpolateFieldFromCenterToCorner_Vector (Cmpnts ***centfield_arr, Cmpnts ***corner_arr, UserCtx *user)
 Interpolates a vector field from cell centers to corner nodes.
 
PetscErrorCode InterpolateFieldFromCenterToCorner_Scalar (PetscReal ***centfield_arr, PetscReal ***corner_arr, UserCtx *user)
 Interpolates a scalar field from cell centers to corner nodes.
 
PetscErrorCode AccumulateParticleField (DM swarm, ParticleFieldId particle_field_id, DM gridSumDM, Vec gridSumVec)
 Accumulates a particle field (scalar or vector) into a target grid sum vector.
 
PetscErrorCode NormalizeGridVectorByCount (DM countDM, Vec countVec, DM dataDM, Vec sumVec, Vec avgVec)
 Normalizes a grid vector of sums by a grid vector of counts to produce an average.
 
PetscErrorCode ScatterParticleFieldCellMean (UserCtx *user, ParticleFieldId particle_field_id, DM dataDM, Vec countVec, PetscBool recount, Vec meanVec)
 Cell mean of a particle field: the field summed over each cell's particles, divided by the cell's count.
 
PetscErrorCode ScatterParticleFieldToEulerField (UserCtx *user, ParticleFieldId particle_field_id, Vec eulerFieldAverageVec)
 Scatters a particle field (scalar or vector) to the corresponding Eulerian field average.
 
PetscErrorCode ScatterAllParticleFieldsToEulerFields (UserCtx *user)
 Scatters a predefined set of particle fields to their corresponding Eulerian fields.
 
PetscErrorCode InterpolateCornerToFaceCenter_Scalar (PetscReal ***corner_arr, PetscReal ***faceX_arr, PetscReal ***faceY_arr, PetscReal ***faceZ_arr, UserCtx *user)
 Interpolates a scalar field from corner nodes to all face centers.
 
PetscErrorCode InterpolateCornerToFaceCenter_Vector (Cmpnts ***corner_arr, Cmpnts ***faceX_arr, Cmpnts ***faceY_arr, Cmpnts ***faceZ_arr, UserCtx *user)
 Interpolates a vector field from corner nodes to all face centers.
 

Macro Definition Documentation

◆ NUM_WEIGHTS

#define NUM_WEIGHTS   8

Definition at line 24 of file interpolation.h.

◆ InterpolateFieldFromCornerToCenter

#define InterpolateFieldFromCornerToCenter (   field,
  centfield,
  user 
)
Value:
( (void)sizeof(char[1 - 2*!!(!__builtin_types_compatible_p(typeof(field), typeof(centfield)))]),\
_Generic((field), \
)(field, centfield, user) )
PetscErrorCode InterpolateFieldFromCornerToCenter_Vector(Cmpnts ***field_arr, Cmpnts ***centfield_arr, UserCtx *user)
Safely interpolate a vector field from corner nodes (from the coordinate DM) to cell centers (from th...
PetscErrorCode InterpolateFieldFromCornerToCenter_Scalar(PetscReal ***field_arr, PetscReal ***centfield_arr, UserCtx *user)
Safely interpolate a scalar field from corner nodes (from the coordinate DM) to cell centers (from th...
A 3D point or vector with PetscScalar components.
Definition variables.h:144

Generic macro to call the appropriate interpolation function based on the field type.

This macro will select either the scalar or vector interpolation function based on the type of the 'field' pointer. It also performs a compile-time check that 'centfield' is of the same type as 'field'. If the types are not the same, a compile-time error is produced.

Usage: InterpolateFieldFromCornerToCenter(field, centfield, user);

Definition at line 36 of file interpolation.h.

◆ InterpolateFieldFromCenterToCorner

#define InterpolateFieldFromCenterToCorner (   blockSize,
  centfield_ptr,
  corner_ptr,
  user_ctx 
)
Value:
( (blockSize) == 1 ? \
InterpolateFieldFromCenterToCorner_Scalar((PetscReal***)(centfield_ptr), (PetscReal***)(corner_ptr), (user_ctx)) : \
InterpolateFieldFromCenterToCorner_Vector((Cmpnts***)(centfield_ptr), (Cmpnts***)(corner_ptr), (user_ctx)) \
)
PetscErrorCode InterpolateFieldFromCenterToCorner_Vector(Cmpnts ***centfield_arr, Cmpnts ***corner_arr, UserCtx *user)
Interpolates a vector field from cell centers to corner nodes.
PetscErrorCode InterpolateFieldFromCenterToCorner_Scalar(PetscReal ***centfield_arr, PetscReal ***corner_arr, UserCtx *user)
Interpolates a scalar field from cell centers to corner nodes.

Macro to dispatch to the correct scalar or vector center-to-corner function based on a runtime block size variable.

This macro uses a ternary operator to inspect the runtime value of 'blockSize' and select the appropriate implementation. It expects the input and output pointers to be void* and handles casting them to the correct, strongly-typed pointers.

Parameters
blockSizeThe runtime block size (1 for scalar, 3 for vector).
centfield_ptrThe input void* pointer to the 3D cell-centered data array.
corner_ptrThe output void* pointer to the 3D corner data array.
user_ctxThe UserCtx structure.

Definition at line 56 of file interpolation.h.

58 : \
59 InterpolateFieldFromCenterToCorner_Vector((Cmpnts***)(centfield_ptr), (Cmpnts***)(corner_ptr), (user_ctx)) \
60 )

◆ InterpolateCornerToFaceCenter

#define InterpolateCornerToFaceCenter (   corner_arr,
  faceX_arr,
  faceY_arr,
  faceZ_arr,
  user_ctx 
)
Value:
_Generic((corner_arr), \
)(corner_arr, faceX_arr, faceY_arr, faceZ_arr, user_ctx)
PetscErrorCode InterpolateCornerToFaceCenter_Vector(Cmpnts ***corner_arr, Cmpnts ***faceX_arr, Cmpnts ***faceY_arr, Cmpnts ***faceZ_arr, UserCtx *user)
Interpolates a vector field from corner nodes to all face centers.
PetscErrorCode InterpolateCornerToFaceCenter_Scalar(PetscReal ***corner_arr, PetscReal ***faceX_arr, PetscReal ***faceY_arr, PetscReal ***faceZ_arr, UserCtx *user)
Interpolates a scalar field from corner nodes to all face centers.

A type-generic macro that interpolates a field from corner nodes to all face centers.

This macro uses C11's _Generic feature to dispatch to the appropriate underlying function based on the type of the input corner_arr.

  • If corner_arr is of type PetscReal***, it calls InterpolateCornerToFaceCenter_Scalar.
  • If corner_arr is of type Cmpnts***, it calls InterpolateCornerToFaceCenter_Vector.
Parameters
corner_arrGhosted node-centered array (global indexing). The type of this argument determines which function is called.
faceX_arrLocal array for X-faces.
faceY_arrLocal array for Y-faces.
faceZ_arrLocal array for Z-faces.
user_ctxUser context containing DMDA 'fda'.

Definition at line 79 of file interpolation.h.

83 )(corner_arr, faceX_arr, faceY_arr, faceZ_arr, user_ctx)

◆ TrilinearInterpolation

#define TrilinearInterpolation (   fieldPtr,
  i,
  j,
  k,
  a1,
  a2,
  a3,
  outPtr 
)
Value:
_Generic((fieldPtr), \
)(fieldPtr, i, j, k, a1, a2, a3, outPtr)
PetscErrorCode TrilinearInterpolation_Vector(Cmpnts ***fieldVec, PetscInt i, PetscInt j, PetscInt k, PetscReal a1, PetscReal a2, PetscReal a3, Cmpnts *vec)
Trilinear interpolation of a vector from the eight entries (i..i+1, j..j+1, k..k+1).
PetscErrorCode TrilinearInterpolation_Scalar(PetscReal ***fieldScal, PetscInt i, PetscInt j, PetscInt k, PetscReal a1, PetscReal a2, PetscReal a3, PetscReal *val)
Trilinear interpolation of a scalar from the eight entries (i..i+1, j..j+1, k..k+1).

Macro that calls either the scalar or vector trilinear interpolation function based on the type of the fieldPtr parameter (3D array).

Usage example:

PetscReal result; Cmpnts vec;

// For scalars: TrilinearInterpolation(fieldScal, i, j, k, a1, a2, a3, &result);

// For vectors: TrilinearInterpolation(fieldVec, i, j, k, a1, a2, a3, &vec);

Definition at line 100 of file interpolation.h.

104 )(fieldPtr, i, j, k, a1, a2, a3, outPtr)

Typedef Documentation

◆ VelocityPointInterpolator

The arrays InterpolateVelocityAtPoint() reads, held from Begin to End.

Definition at line 160 of file interpolation.h.

Function Documentation

◆ TrilinearInterpolation_Scalar()

PetscErrorCode TrilinearInterpolation_Scalar ( PetscReal ***  fieldScal,
PetscInt  i,
PetscInt  j,
PetscInt  k,
PetscReal  a1,
PetscReal  a2,
PetscReal  a3,
PetscReal *  val 
)

Trilinear interpolation of a scalar from the eight entries (i..i+1, j..j+1, k..k+1).

The weights come from ComputeTrilinearWeights(), which clamps the coordinates to [0,1].

Parameters
[in]fieldScalScalar array indexed [k][j][i].
[in]ii index of the lowest of the eight entries.
[in]jj index of the lowest of the eight entries.
[in]kk index of the lowest of the eight entries.
[in]a1Coordinate in the eight-entry cell along i, in [0,1].
[in]a2Coordinate along j, in [0,1].
[in]a3Coordinate along k, in [0,1].
[out]valThe interpolated value.
Returns
PetscErrorCode 0 on success.

Trilinear interpolation of a scalar from the eight entries (i..i+1, j..j+1, k..k+1).

See also
TrilinearInterpolation_Scalar()

Definition at line 473 of file interpolation.c.

482{
483 PetscFunctionBegin; // PETSc macro for error/stack tracing
484
485 // Compute the 8 corner weights
486 PetscReal wcorner[8];
487 ComputeTrilinearWeights(a1, a2, a3, wcorner);
488
489 // Offsets for cell corners
490 PetscInt i1 = i + 1;
491 PetscInt j1 = j + 1;
492 PetscInt k1 = k + 1;
493
494 // Initialize the output scalar
495 PetscReal sum = 0.0;
496
497 // Corner 0 => (i, j, k)
498 sum += wcorner[0] * fieldScal[k ][j ][i ];
499 // Corner 1 => (i+1, j, k)
500 sum += wcorner[1] * fieldScal[k ][j ][i1];
501 // Corner 2 => (i, j+1, k)
502 sum += wcorner[2] * fieldScal[k ][j1][i ];
503 // Corner 3 => (i+1, j+1, k)
504 sum += wcorner[3] * fieldScal[k ][j1][i1];
505 // Corner 4 => (i, j, k+1)
506 sum += wcorner[4] * fieldScal[k1][j ][i ];
507 // Corner 5 => (i+1, j, k+1)
508 sum += wcorner[5] * fieldScal[k1][j ][i1];
509 // Corner 6 => (i, j+1, k+1)
510 sum += wcorner[6] * fieldScal[k1][j1][i ];
511 // Corner 7 => (i+1, j+1, k+1)
512 sum += wcorner[7] * fieldScal[k1][j1][i1];
513
514 *val = sum;
515
516 PetscFunctionReturn(0);
517}
static void ComputeTrilinearWeights(PetscReal a1, PetscReal a2, PetscReal a3, PetscReal *w)
Compute the eight trilinear interpolation weights for a particle's local coordinates.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TrilinearInterpolation_Vector()

PetscErrorCode TrilinearInterpolation_Vector ( Cmpnts ***  fieldVec,
PetscInt  i,
PetscInt  j,
PetscInt  k,
PetscReal  a1,
PetscReal  a2,
PetscReal  a3,
Cmpnts *  vec 
)

Trilinear interpolation of a vector from the eight entries (i..i+1, j..j+1, k..k+1).

The weights come from ComputeTrilinearWeights(), which clamps the coordinates to [0,1]; the weighted sum is divided by the weights' sum, and is zero when that sum is below 1e-14.

Parameters
[in]fieldVecVector array indexed [k][j][i].
[in]ii index of the lowest of the eight entries.
[in]jj index of the lowest of the eight entries.
[in]kk index of the lowest of the eight entries.
[in]a1Coordinate in the eight-entry cell along i, in [0,1].
[in]a2Coordinate along j, in [0,1].
[in]a3Coordinate along k, in [0,1].
[out]vecThe interpolated vector.
Returns
PetscErrorCode 0 on success.

Trilinear interpolation of a vector from the eight entries (i..i+1, j..j+1, k..k+1).

See also
TrilinearInterpolation_Vector()

Definition at line 524 of file interpolation.c.

533{
534 PetscFunctionBegin; // PETSc macro for error/stack tracing
535
536 // Compute the 8 corner weights
537 PetscReal wcorner[8];
538
539 ComputeTrilinearWeights(a1, a2, a3, wcorner);
540
541 // For partial interpolation, we'll keep track of how many corners are valid
542 // and how much sum of weights is used. Then we do a final normalization.
543 PetscReal sumW = 0.0;
544 Cmpnts accum = {0.0, 0.0, 0.0};
545
546 // The eight corner indices, with their weights:
547 // corners: (i,j,k), (i+1,j,k), (i,j+1,k), (i+1,j+1,k), etc.
548 // We store them in an array to iterate cleanly.
549 const PetscInt cornerOffsets[8][3] = {
550 {0, 0, 0},
551 {1, 0, 0},
552 {0, 1, 0},
553 {1, 1, 0},
554 {0, 0, 1},
555 {1, 0, 1},
556 {0, 1, 1},
557 {1, 1, 1}
558 };
559
560 // Weighted partial sum
561 for (PetscInt c = 0; c < 8; c++) {
562 const PetscInt di = cornerOffsets[c][0];
563 const PetscInt dj = cornerOffsets[c][1];
564 const PetscInt dk = cornerOffsets[c][2];
565 PetscInt iC = i + di;
566 PetscInt jC = j + dj;
567 PetscInt kC = k + dk;
568
569 /*
570 // skip if out of domain
571 // (Assuming you know global domain is [0..mx), [0..my), [0..mz).)
572 if (iC < 0 || iC >= (PetscInt)userGlobalMx ||
573 jC < 0 || jC >= (PetscInt)userGlobalMy ||
574 kC < 0 || kC >= (PetscInt)userGlobalMz)
575 {
576 // skip this corner
577 continue;
578 }
579
580 */
581
582 // Otherwise, accumulate
583 accum.x += wcorner[c] * fieldVec[kC][jC][iC].x;
584 accum.y += wcorner[c] * fieldVec[kC][jC][iC].y;
585 accum.z += wcorner[c] * fieldVec[kC][jC][iC].z;
586 sumW += wcorner[c];
587 }
588
589 // If sumW=0 => out-of-range or zero weighting => set (0,0,0)
590 if (sumW > 1.0e-14) {
591 vec->x = accum.x / sumW;
592 vec->y = accum.y / sumW;
593 vec->z = accum.z / sumW;
594 } else {
595 vec->x = 0.0; vec->y = 0.0; vec->z = 0.0;
596 }
597
598 PetscFunctionReturn(0);
599}
PetscScalar x
Definition variables.h:145
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:

◆ InterpolateVelocityAtPointBegin()

PetscErrorCode InterpolateVelocityAtPointBegin ( UserCtx *  user,
VelocityPointInterpolator **  interp 
)

Opens the arrays for evaluating the particle velocity at points, with the configured interpolation method.

Collective: the corner-averaged method stages Ucat at the corners, as its swarm pass does, which overwrites the vector corner workspace.

Parameters
[in]userGrid context; its Ucat, Ucont and metrics must be current.
[out]interpClose with InterpolateVelocityAtPointEnd().
Returns
PetscErrorCode 0 on success.

Opens the arrays for evaluating the particle velocity at points, with the configured interpolation method.

See also
InterpolateVelocityAtPointBegin()

Definition at line 1250 of file interpolation.c.

1251{
1252 VelocityPointInterpolator *v = NULL;
1253
1254 PetscFunctionBegin;
1256 PetscCall(PetscNew(&v));
1257 v->user = user;
1258 v->method = user->simCtx->interpolationMethod;
1259 GetPeriodicAxes(user->simCtx, v->periodic);
1260 if (v->method == INTERP_FACE_FLUX) {
1261 PetscCall(DMDAVecGetArrayRead(user->fda, user->lUcont, (void *)&v->field));
1262 PetscCall(DMDAVecGetArrayRead(user->fda, user->lCsi, (void *)&v->csi));
1263 PetscCall(DMDAVecGetArrayRead(user->fda, user->lEta, (void *)&v->eta));
1264 PetscCall(DMDAVecGetArrayRead(user->fda, user->lZet, (void *)&v->zet));
1265 } else if (v->method == INTERP_CORNER_AVERAGED) {
1266 FieldView corner;
1267
1268 // The same staging as the corner-averaged swarm pass.
1269 PetscCall(StageCellFieldAtCorners(user, user->lUcat, 3, &corner));
1270 PetscCall(DMDAVecGetArrayRead(user->fda, corner.local_vec, (void *)&v->field));
1271 } else {
1272 PetscCall(DMDAVecGetArrayRead(user->fda, user->lUcat, (void *)&v->field));
1273 PetscCall(MetricGetFacePlaneArrays(user, PETSC_TRUE, &v->dual));
1274 }
1275 *interp = v;
1277 PetscFunctionReturn(0);
1278}
PetscErrorCode MetricGetFacePlaneArrays(UserCtx *user, PetscBool dual, FacePlaneArrays *arrays)
Opens a family's face-plane arrays for reading; MetricComputeFacePlanes() must have run.
Definition Metric.c:376
Non-owning runtime objects resolved for one field and UserCtx.
InterpolationMethod method
static PetscErrorCode StageCellFieldAtCorners(UserCtx *user, Vec cell_local, PetscInt bs, FieldView *corner_view)
Stage a ghosted cell-centred field at the grid corners, in the corner workspace for its block size: z...
The arrays one method's point evaluation reads.
#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 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
InterpolationMethod interpolationMethod
Definition variables.h:1055
@ INTERP_CORNER_AVERAGED
Definition variables.h:762
@ INTERP_FACE_FLUX
Definition variables.h:763
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:

◆ InterpolateVelocityAtPoint()

PetscErrorCode InterpolateVelocityAtPoint ( const VelocityPointInterpolator *  interp,
const PetscInt  cell[3],
const PetscReal  weights[3],
Cmpnts  point,
Cmpnts *  u 
)

The velocity a particle at a point would be given: the value the swarm pass of the configured method computes there.

Parameters
[in]interpFrom InterpolateVelocityAtPointBegin().
[in]cellHost cell, geometric indices; the point must be in a cell this rank owns.
[in]weightsThe point's logical coordinates in the host cell.
[in]pointThe point.
[out]uThe velocity; zero, as in the swarm pass, if the stencil lies outside the ghosted region.
Returns
PetscErrorCode 0 on success.

The velocity a particle at a point would be given: the value the swarm pass of the configured method computes there.

See also
InterpolateVelocityAtPoint()

Definition at line 1284 of file interpolation.c.

1286{
1287 const DMDALocalInfo *info = &interp->user->info;
1288
1289 PetscFunctionBegin;
1290 /* Called once per draw: a helper event, timed only when enabled by name. */
1292 u->x = u->y = u->z = 0.0;
1293 if (interp->method == INTERP_FACE_FLUX) {
1294 /* Host cell in cell-centred array numbering; its minimum faces are one index below. */
1295 const Cmpnts w = {weights[0], weights[1], weights[2]};
1296 if (FaceFluxStencilIsLocal(info, cell))
1297 PetscCall(ContravariantToCartesianInCell(interp->field, interp->csi, interp->eta, interp->zet,
1298 cell[0] + 1, cell[1] + 1, cell[2] + 1, w, u));
1299 } else if (interp->method == INTERP_CORNER_AVERAGED) {
1300 if (CornerStencilIsLocal(info, cell))
1301 PetscCall(TrilinearInterpolation_Vector(interp->field, cell[0], cell[1], cell[2],
1302 weights[0], weights[1], weights[2], u));
1303 } else {
1304 PetscInt base[3];
1305 PetscReal a[3], w[8] = {0};
1306 PetscBool unclamped, outside;
1307 PetscCall(CenterDualCellWeights(info, &interp->dual, interp->periodic, cell, weights, point, base, a, w,
1308 &unclamped, &outside));
1309 if (!outside) PetscCall(CenterInterpolateVector(interp->field, base, a, w, unclamped, u));
1310 }
1312 PetscFunctionReturn(0);
1313}
static PetscBool FaceFluxStencilIsLocal(const DMDALocalInfo *info, const PetscInt cell[3])
Whether the face fluxes of a host cell lie in the ghosted region: in cell-centred numbering (i,...
PetscErrorCode TrilinearInterpolation_Vector(Cmpnts ***fieldVec, PetscInt i, PetscInt j, PetscInt k, PetscReal a1, PetscReal a2, PetscReal a3, Cmpnts *vec)
Implementation of TrilinearInterpolation_Vector().
static PetscErrorCode CenterInterpolateVector(Cmpnts ***fieldVec, const PetscInt base[3], const PetscReal a[3], const PetscReal w[8], PetscBool unclamped, Cmpnts *vec)
A cell-centred vector at a point, from the dual cell CenterDualCellWeights() found.
static PetscBool CornerStencilIsLocal(const DMDALocalInfo *info, const PetscInt cell[3])
Whether the eight corners of a host cell (nodes cell to cell + 1) lie in the ghosted region.
static PetscErrorCode CenterDualCellWeights(const DMDALocalInfo *info, const FacePlaneArrays *dual, const PetscBool periodic[3], const PetscInt cell[3], const PetscReal weights[3], Cmpnts loc, PetscInt base[3], PetscReal a[3], PetscReal w[8], PetscBool *unclamped, PetscBool *outside)
The dual cell of cell centres a point interpolates from, and its trilinear weights.
#define PROFILE_HELPER_BEGIN
Marks the beginning of a profiled helper: timed only when enabled by name.
Definition logging.h:1014
#define PROFILE_HELPER_END
Alias of PROFILE_FUNCTION_END for a function that began with PROFILE_HELPER_BEGIN.
Definition logging.h:1033
static PetscErrorCode ContravariantToCartesianInCell(Cmpnts ***lucont, Cmpnts ***lcsi, Cmpnts ***leta, Cmpnts ***lzet, PetscInt i, PetscInt j, PetscInt k, Cmpnts w, Cmpnts *ucat)
Cartesian velocity at a point of one cell from the cell's face fluxes.
Definition setup.h:503
DMDALocalInfo info
Definition variables.h:1169
Here is the call graph for this function:
Here is the caller graph for this function:

◆ InterpolateVelocityAtPointEnd()

PetscErrorCode InterpolateVelocityAtPointEnd ( VelocityPointInterpolator **  interp)

Closes the arrays InterpolateVelocityAtPointBegin() opened.

Parameters
[in,out]interpSet to NULL.
Returns
PetscErrorCode 0 on success.

Closes the arrays InterpolateVelocityAtPointBegin() opened.

See also
InterpolateVelocityAtPointEnd()

Definition at line 1319 of file interpolation.c.

1320{
1321 VelocityPointInterpolator *v = *interp;
1322 UserCtx *user = v->user;
1323
1324 PetscFunctionBegin;
1326 if (v->method == INTERP_FACE_FLUX) {
1327 PetscCall(DMDAVecRestoreArrayRead(user->fda, user->lZet, (void *)&v->zet));
1328 PetscCall(DMDAVecRestoreArrayRead(user->fda, user->lEta, (void *)&v->eta));
1329 PetscCall(DMDAVecRestoreArrayRead(user->fda, user->lCsi, (void *)&v->csi));
1330 PetscCall(DMDAVecRestoreArrayRead(user->fda, user->lUcont, (void *)&v->field));
1331 } else if (v->method == INTERP_CORNER_AVERAGED) {
1332 FieldView corner;
1333 PetscCall(FieldGetView(user, FIELD_ID_CELL_VECTOR_AT_CORNER, &corner));
1334 PetscCall(DMDAVecRestoreArrayRead(user->fda, corner.local_vec, (void *)&v->field));
1335 } else {
1336 PetscCall(MetricRestoreFacePlaneArrays(user, &v->dual));
1337 PetscCall(DMDAVecRestoreArrayRead(user->fda, user->lUcat, (void *)&v->field));
1338 }
1339 PetscCall(PetscFree(*interp));
1341 PetscFunctionReturn(0);
1342}
PetscErrorCode MetricRestoreFacePlaneArrays(UserCtx *user, FacePlaneArrays *arrays)
Closes arrays opened by MetricGetFacePlaneArrays().
Definition Metric.c:401
PetscErrorCode FieldGetView(UserCtx *user, FieldId field_id, FieldView *view)
Resolve the existing DM and global/local vectors for one field.
@ FIELD_ID_CELL_VECTOR_AT_CORNER
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:

◆ InterpolateAllFieldsToSwarm()

PetscErrorCode InterpolateAllFieldsToSwarm ( UserCtx *  user)

Interpolates the Eulerian fields the particles carry to every local particle.

Interpolates Ucat to the particle velocity, Diffusivity to Diffusivity and DiffusivityGradient to DiffusivityGradient, with the method in simCtx->interpolationMethod:

  • Trilinear: from the dual cell of the eight nearest cell centres, in one pass that shares each particle's dual cell and weights across the fields;
  • CornerAveraged: cell centres averaged to the corners, then trilinear from the host cell's corners, one pass per field;
  • FaceFlux: velocity from the host cell's face fluxes; the other fields as Trilinear.

Reads the ghosted local vectors of the source fields, which must be current. A particle whose stencil lies outside the ghosted region gets zero. Collective.

Parameters
[in,out]userBlock context holding the swarm and the source fields.
Returns
PetscErrorCode Returns 0 on success, non-zero on failure.

Interpolates the Eulerian fields the particles carry to every local particle.

See also
InterpolateAllFieldsToSwarm()

Definition at line 1420 of file interpolation.c.

1421{
1422 PetscErrorCode ierr;
1423 PetscMPIInt rank;
1427 PetscFunctionBegin;
1428
1430
1431 rank = user->simCtx->rank;
1432
1434 " Interpolation of ucat to velocity begins on rank %d.\n",rank);
1435 /* Add fields as necessary to sources/targets; one pass shares each particle's dual cell. */
1436 ierr = InterpolateEulerFieldsToSwarm(user, (PetscInt)(sizeof(sources) / sizeof(sources[0])), sources, targets); CHKERRQ(ierr);
1437
1438 ierr = MPI_Barrier(PETSC_COMM_WORLD); CHKERRQ(ierr);
1440 "[rank %d]Completed Interpolateting all fields to the swarm.\n",rank);
1441
1443
1444 PetscFunctionReturn(0);
1445}
FieldId
Compile-time identity for a catalogued Eulerian field.
@ FIELD_ID_UCAT
@ FIELD_ID_DIFFUSIVITY_GRADIENT
@ FIELD_ID_DIFFUSIVITY
static PetscErrorCode InterpolateEulerFieldsToSwarm(UserCtx *user, PetscInt n, const FieldId source_field_ids[], const ParticleFieldId target_field_ids[])
Interpolates several Eulerian fields to their particle fields with the configured method.
#define LOCAL
Logging scope definitions for controlling message output.
Definition logging.h:45
#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_INFO
Informational messages about program execution.
Definition logging.h:31
@ LOG_DEBUG
Detailed debugging information.
Definition logging.h:32
ParticleFieldId
Compile-time identity for a persistent solver-particle field.
@ PARTICLE_FIELD_ID_DIFFUSIVITY_GRADIENT
@ PARTICLE_FIELD_ID_DIFFUSIVITY
@ PARTICLE_FIELD_ID_VELOCITY
PetscMPIInt rank
Definition variables.h:919
Here is the call graph for this function:
Here is the caller graph for this function:

◆ InterpolateFieldFromCornerToCenter_Scalar()

PetscErrorCode InterpolateFieldFromCornerToCenter_Scalar ( PetscReal ***  field_arr,
PetscReal ***  centfield_arr,
UserCtx *  user 
)

Safely interpolate a scalar field from corner nodes (from the coordinate DM) to cell centers (from the cell-centered DM) using the provided UserCtx.

For each cell center in the physical region of the cell-centered DM (fda), this function averages the up to 8 surrounding scalar values from the coordinate DM (da). On boundaries, where fewer corners are available, a partial average is computed.

The coordinate DM (da) is built on corners (IM+1 x JM+1 x KM+1) while the cell-centered DM (fda) covers the physical cells (IM x JM x KM). Index offsets are adjusted via DMDAGetLocalInfo.

Parameters
[in]field_arr3D array of corner-based scalar data (from user->da).
[out]centfield_arr3D array for interpolated cell-center scalar data (for user->fda).
[in]userUser context containing:
  • da : DM for the coordinate (corner) data.
  • fda : DM for the cell-centered data.
Returns
PetscErrorCode 0 on success.

Safely interpolate a scalar field from corner nodes (from the coordinate DM) to cell centers (from the cell-centered DM) using the provided UserCtx.

See also
InterpolateFieldFromCornerToCenter_Scalar()

Definition at line 71 of file interpolation.c.

75{
76 DMDALocalInfo info;
77
78 PetscFunctionBeginUser;
80
81 info = user->info;
82
83 // Interior cells only: the dummy layers at index 0 and m-1 are excluded.
84 PetscInt lo[3], hi[3];
85 GetOwnedInteriorCellBounds(&info, lo, hi);
86 PetscInt is = lo[0], js = lo[1], ks = lo[2], ie = hi[0], je = hi[1], ke = hi[2];
87
88 // Loop over the locally owned INTERIOR cells.
89 for (PetscInt k = ks; k < ke; k++) {
90 for (PetscInt j = js; j < je; j++) {
91 for (PetscInt i = is; i < ie; i++) {
92 // Calculate cell center value as the average of its 8 corner nodes
93 centfield_arr[k][j][i] = 0.125 * (field_arr[k][j][i] + field_arr[k][j-1][i] +
94 field_arr[k-1][j][i] + field_arr[k-1][j-1][i] +
95 field_arr[k][j][i-1] + field_arr[k][j-1][i-1] +
96 field_arr[k-1][j][i-1] + field_arr[k-1][j-1][i-1]);
97 }
98 }
99 }
100
102 PetscFunctionReturn(0);
103}
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ InterpolateFieldFromCornerToCenter_Vector()

PetscErrorCode InterpolateFieldFromCornerToCenter_Vector ( Cmpnts ***  field_arr,
Cmpnts ***  centfield_arr,
UserCtx *  user 
)

Safely interpolate a vector field from corner nodes (from the coordinate DM) to cell centers (from the cell-centered DM) using the provided UserCtx.

For each cell center in the physical region of the cell-centered DM (fda), this function averages the 8 surrounding corner values from the coordinate DM (da). The coordinate DM (da) is built on corners (IM+1 x JM+1 x KM+1) while the cell-centered DM (fda) covers the physical cells (IM x JM x KM). Index offsets are adjusted using DMDAGetLocalInfo.

Parameters
[in]field_arr3D array of corner-based vector data (from user->da).
[out]centfield_arr3D array for interpolated cell-center vector data (for user->fda).
[in]userUser context containing:
  • da : DM for the coordinate (corner) data.
  • fda : DM for the cell-centered data.
Returns
PetscErrorCode 0 on success.

Safely interpolate a vector field from corner nodes (from the coordinate DM) to cell centers (from the cell-centered DM) using the provided UserCtx.

See also
InterpolateFieldFromCornerToCenter_Vector()

Definition at line 23 of file interpolation.c.

27{
28 DMDALocalInfo info;
29
30 PetscFunctionBeginUser;
32
33 info = user->info;
34
35 // Interior cells only: the dummy layers at index 0 and m-1 are excluded.
36 PetscInt lo[3], hi[3];
37 GetOwnedInteriorCellBounds(&info, lo, hi);
38 PetscInt is = lo[0], js = lo[1], ks = lo[2], ie = hi[0], je = hi[1], ke = hi[2];
39
40 // Loop over the locally owned INTERIOR cells.
41 for (PetscInt k = ks; k < ke; k++) {
42 for (PetscInt j = js; j < je; j++) {
43 for (PetscInt i = is; i < ie; i++) {
44 // Calculate cell center value as the average of its 8 corner nodes
45 centfield_arr[k][j][i].x = 0.125 * (field_arr[k][j][i].x + field_arr[k][j-1][i].x +
46 field_arr[k-1][j][i].x + field_arr[k-1][j-1][i].x +
47 field_arr[k][j][i-1].x + field_arr[k][j-1][i-1].x +
48 field_arr[k-1][j][i-1].x + field_arr[k-1][j-1][i-1].x);
49
50 centfield_arr[k][j][i].y = 0.125 * (field_arr[k][j][i].y + field_arr[k][j-1][i].y +
51 field_arr[k-1][j][i].y + field_arr[k-1][j-1][i].y +
52 field_arr[k][j][i-1].y + field_arr[k][j-1][i-1].y +
53 field_arr[k-1][j][i-1].y + field_arr[k-1][j-1][i-1].y);
54
55 centfield_arr[k][j][i].z = 0.125 * (field_arr[k][j][i].z + field_arr[k][j-1][i].z +
56 field_arr[k-1][j][i].z + field_arr[k-1][j-1][i].z +
57 field_arr[k][j][i-1].z + field_arr[k][j-1][i-1].z +
58 field_arr[k-1][j][i-1].z + field_arr[k-1][j-1][i-1].z);
59 }
60 }
61 }
62
64 PetscFunctionReturn(0);
65}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestCornerToCenterInterpolation()

PetscErrorCode TestCornerToCenterInterpolation ( UserCtx *  user)

Tests the InterpolateFieldFromCornerToCenter function by reproducing the Cent vector.

This function serves as a unit test. It performs the following steps:

  1. Takes the corner-centered nodal coordinates (from DMGetCoordinatesLocal) as input.
  2. Uses the InterpolateFieldFromCornerToCenter macro to interpolate these coordinates to the cell centers, storing the result in a new temporary vector.
  3. Compares this new vector with the user->Cent vector, which is assumed to have been computed by ComputeCellCenters and serves as the ground truth.
  4. A 2-norm of the difference is computed. If it is below a small tolerance, the test passes.
Note
This function should be called immediately after ComputeCellCenters has been successfully executed.
Parameters
userThe UserCtx for a specific grid level.
Returns
PetscErrorCode 0 on success, or a PETSc error code on failure.

Tests the InterpolateFieldFromCornerToCenter function by reproducing the Cent vector.

See also
TestCornerToCenterInterpolation()

Definition at line 109 of file interpolation.c.

110{
111 PetscErrorCode ierr;
112 Vec lCoords, TestCent;
113 Cmpnts ***coor_arr, ***test_cent_arr;
114 PetscReal diff_norm;
115
116 PetscFunctionBeginUser;
118
119 // 1. Create a temporary vector to hold the result of our interpolation.
120 // It must have the same layout and size as the ground-truth user->Cent vector.
121 ierr = VecDuplicate(user->Cent, &TestCent); CHKERRQ(ierr);
122
123 // 2. Get the input (corner coordinates) and output (our test vector) arrays.
124 lCoords = user->lCoor;
125 ierr = DMDAVecGetArrayRead(user->fda, lCoords, &coor_arr); CHKERRQ(ierr);
126 ierr = DMDAVecGetArray(user->fda, TestCent, &test_cent_arr); CHKERRQ(ierr);
127
128 // 3. Call the generic interpolation macro.
129 // The macro will see that `coor_arr` is of type `Cmpnts***` and correctly
130 // call the `InterpolateFieldFromCornerToCenter_Vector` function.
131 ierr = InterpolateFieldFromCornerToCenter(coor_arr, test_cent_arr, user); CHKERRQ(ierr);
132
133 // 4. Restore the arrays.
134 ierr = DMDAVecRestoreArrayRead(user->fda, lCoords, &coor_arr); CHKERRQ(ierr);
135 ierr = DMDAVecRestoreArray(user->fda, TestCent, &test_cent_arr); CHKERRQ(ierr);
136
137 // 5. IMPORTANT: Assemble the vector so its values are communicated across processors
138 // and it's ready for global operations like VecNorm.
139 ierr = VecAssemblyBegin(TestCent); CHKERRQ(ierr);
140 ierr = VecAssemblyEnd(TestCent); CHKERRQ(ierr);
141
142 // 6. Compare the result with the ground truth.
143 // We compute TestCent = -1.0 * user->Cent + 1.0 * TestCent.
144 // This calculates the difference vector: TestCent - user->Cent.
145 ierr = VecAXPY(TestCent, -1.0, user->Cent); CHKERRQ(ierr);
146
147 // Now, compute the L2 norm of the difference vector. If the functions are
148 // identical, the norm should be zero (or very close due to floating point).
149 ierr = VecNorm(TestCent, NORM_2, &diff_norm); CHKERRQ(ierr);
150
151 // 7. Report the result and clean up.
152 if (diff_norm < 1.0e-12) {
153 LOG_ALLOW(GLOBAL,LOG_DEBUG,"[SUCCESS] Test passed. Norm of difference is %g.\n", (double)diff_norm);
154 } else {
155 LOG_ALLOW(GLOBAL,LOG_DEBUG, "[FAILURE] Test failed. Norm of difference is %g.\n", (double)diff_norm);
156 }
157
158 ierr = VecDestroy(&TestCent); CHKERRQ(ierr);
159
161 PetscFunctionReturn(0);
162}
#define InterpolateFieldFromCornerToCenter(field, centfield, user)
Generic macro to call the appropriate interpolation function based on the field type.
#define GLOBAL
Scope for global logging across all processes.
Definition logging.h:46
Vec lCoor
Definition variables.h:1243

◆ InterpolateFieldFromCenterToCorner_Vector()

PetscErrorCode InterpolateFieldFromCenterToCorner_Vector ( Cmpnts ***  centfield_arr,
Cmpnts ***  corner_arr,
UserCtx *  user 
)

Interpolates a vector field from cell centers to corner nodes.

This version is adapted to write directly into a ghosted local array obtained from DMDAVecGetArray(), which allows using GLOBAL indices for writing to the OWNED portion of the array.

Parameters
[in]centfield_arrInput: 3D array (ghosted) of cell-centered data, accessed via GLOBAL indices.
[out]corner_arrOutput: 3D array (ghosted) where interpolated node values are stored, also accessed via GLOBAL indices for the owned part.
[in]userUser context containing DMDA information.
Returns
PetscErrorCode 0 on success.

Interpolates a vector field from cell centers to corner nodes.

See also
InterpolateFieldFromCenterToCorner_Vector()

Definition at line 168 of file interpolation.c.

172{
173 DMDALocalInfo info;
174 PetscMPIInt rank;
176 rank = user->simCtx->rank;
177 info = user->info;
178
179 // Node ownership range (GLOBAL indices)
180 PetscInt xs_node = info.xs, xm_node = info.xm, xe_node = xs_node + xm_node;
181 PetscInt ys_node = info.ys, ym_node = info.ym, ye_node = ys_node + ym_node;
182 PetscInt zs_node = info.zs, zm_node = info.zm, ze_node = zs_node + zm_node;
183
184 PetscInt nCellsX = info.mx - 2; // Number of cells in x-direction
185 PetscInt nCellsY = info.my - 2; // Number of cells in y-direction
186 PetscInt nCellsZ = info.mz - 2; // Number of cells in z-direction
187
188
189 // Global grid dimensions (used for valid cell check)
190 PetscInt IM = info.mx - 1; // Total nodes in i-direction
191 PetscInt JM = info.my - 1; // Total nodes in j-direction
192 PetscInt KM = info.mz - 1; // Total nodes in k-direction
193
195 "[Rank %d] Starting -- Node ownership k=%" PetscInt_FMT "..%" PetscInt_FMT ", j=%" PetscInt_FMT "..%" PetscInt_FMT ", i=%" PetscInt_FMT "..%" PetscInt_FMT "\n",
196 rank, zs_node, ze_node-1, ys_node, ye_node-1, xs_node, xe_node-1);
197
198 // Loop over the GLOBAL indices of the NODES owned by this processor
199 for (PetscInt k = zs_node; k < ze_node; k++) {
200 for (PetscInt j = ys_node; j < ye_node; j++) {
201 for (PetscInt i = xs_node; i < xe_node; i++) {
202 Cmpnts sum = {0.0, 0.0, 0.0};
203 PetscInt count = 0;
204
205 // DEBUG 1 TEST
206 /*
207 if(rank == 1 && i == 24 && j == 12 && k == 49){
208 PetscInt i_cell_A = i - 1;
209 PetscInt i_cell_B = i;
210
211 Cmpnts ucat_A = centfield_arr[k][j][i_cell_A]; // 23
212 Cmpnts ucat_B = centfield_arr[k][j][i_cell_B]; // 24 (out-of-bounds if IM=25)
213
214 PetscPrintf(PETSC_COMM_WORLD,"[Rank %d] DEBUG TEST at Node(k,j,i)=%d,%d,%d: Read { Valid Cell }Ucat[%d][%d][%d]=(%.2f,%.2f,%.2f) and {Out-of-Bounds Cell}Ucat[%d][%d][%d]=(%.2f,%.2f,%.2f)\n",
215 rank, k, j, i,
216 k, j, i_cell_A, ucat_A.x, ucat_A.y, ucat_A.z,
217 k, j, i_cell_B, ucat_B.x, ucat_B.y, ucat_B.z);
218
219 }
220 */
221
222 // Skip processing the unused last node in each dimension.
223 if(i >= IM || j >= JM || k >= KM){
224 continue;
225 }
226 // Loop over the 8 potential cells surrounding node N(k,j,i) and accumulate values.
227 // The index offsets correspond to the relative position of the indices(shifted) that represent cell-centered field values of cells that share the node N(k,j,i) as a corner
228 for (PetscInt dk_offset = -1; dk_offset <= 0; dk_offset++) {
229 for (PetscInt dj_offset = -1; dj_offset <= 0; dj_offset++) {
230 for (PetscInt di_offset = -1; di_offset <= 0; di_offset++) {
231
232 // These are still GLOBAL cell indices
233 PetscInt global_cell_k = k + dk_offset;
234 PetscInt global_cell_j = j + dj_offset;
235 PetscInt global_cell_i = i + di_offset;
236
237 // Check if this corresponds to a valid GLOBAL cell index
238 if (global_cell_i >= 0 && global_cell_i < nCellsX &&
239 global_cell_j >= 0 && global_cell_j < nCellsY &&
240 global_cell_k >= 0 && global_cell_k < nCellsZ)
241 {
242 Cmpnts cell_val = centfield_arr[global_cell_k + 1][global_cell_j + 1][global_cell_i + 1];
243
244 LOG_LOOP_ALLOW_EXACT(LOCAL, LOG_VERBOSE,k,49,"[Rank %d] successful read from [%" PetscInt_FMT "][%" PetscInt_FMT "][%" PetscInt_FMT "] -> (%.2f, %.2f, %.2f)\n",
245 rank,global_cell_k,global_cell_j,global_cell_i,cell_val.x, cell_val.y, cell_val.z);
246
247 sum.x += cell_val.x;
248 sum.y += cell_val.y;
249 sum.z += cell_val.z;
250 count++;
251 }
252 }
253 }
254 }
255
256 PetscInt i_global_write = i; // Global index in GLOBAL array.
257 PetscInt j_global_write = j;
258 PetscInt k_global_write = k;
259
260 // We write directly into the array using the global loop indices.
261 if (count > 0) {
262 corner_arr[k_global_write][j_global_write][i_global_write].x = sum.x / (PetscReal)count;
263 corner_arr[k_global_write][j_global_write][i_global_write].y = sum.y / (PetscReal)count;
264 corner_arr[k_global_write][j_global_write][i_global_write].z = sum.z / (PetscReal)count;
265 } else {
266 // This case should ideally not happen for a valid owned node, but as a failsafe:
267 corner_arr[k_global_write][j_global_write][i_global_write] = (Cmpnts){0.0, 0.0, 0.0};
268 }
269
270 // DEBUG 2
271 /*
272 if(rank == 1){
273 if(i == 11 && j == 11 && k == 49){
274 Cmpnts ucat_node = corner_arr[k][j][i];
275 PetscPrintf(PETSC_COMM_WORLD,"[Rank %d] DEBUG TEST at Node(k,j,i)=%d,%d,%d: Wrote CornerUcat[%d][%d][%d]=(%.2f,%.2f,%.2f)\n",
276 rank, k, j, i,
277 k, j, i, ucat_node.x, ucat_node.y, ucat_node.z);
278 }
279 }
280
281 if(rank == 0 && i == 24 && j == 12 && k == 0){
282 Cmpnts ucat_node = corner_arr[k][j][i];
283 PetscPrintf(PETSC_COMM_WORLD,"[Rank %d] DEBUG TEST at Node(k,j,i)=%d,%d,%d: Wrote CornerUcat[%d][%d][%d]=(%.2f,%.2f,%.2f)\n",
284 rank, k, j, i,
285 k, j, i, ucat_node.x, ucat_node.y, ucat_node.z);
286 }
287 */
288 // LOG_LOOP_ALLOW_EXACT(LOCAL, LOG_VERBOSE,k,48,"[Rank %d] Node(k,j,i)=%d,%d,%d finished loops and write.\n", rank, k, j, i);
289 }
290 }
291 }
293 return 0;
294}
#define LOG_ALLOW_SYNC(scope, level, fmt,...)
Synchronized logging macro that checks both the log level and whether the calling function is in the ...
Definition logging.h:273
#define LOG_LOOP_ALLOW_EXACT(scope, level, var, val, fmt,...)
Logs a custom message if a variable equals a specific value.
Definition logging.h:405
@ LOG_VERBOSE
Extremely detailed logs, typically for development use only.
Definition logging.h:34

◆ InterpolateFieldFromCenterToCorner_Scalar()

PetscErrorCode InterpolateFieldFromCenterToCorner_Scalar ( PetscReal ***  centfield_arr,
PetscReal ***  corner_arr,
UserCtx *  user 
)

Interpolates a scalar field from cell centers to corner nodes.

This version is adapted to write directly into a ghosted local array obtained from DMDAVecGetArray(), which allows using GLOBAL indices for writing to the OWNED portion of the array.

Parameters
[in]centfield_arrInput: 3D array (ghosted) of scalar data at cell centers, accessed via GLOBAL indices.
[out]corner_arrOutput: 3D array (ghosted) where interpolated node values are stored, also accessed via GLOBAL indices for the owned part.
[in]userUser context containing DMDA information.
Returns
PetscErrorCode 0 on success.

Interpolates a scalar field from cell centers to corner nodes.

See also
InterpolateFieldFromCenterToCorner_Scalar()

Definition at line 300 of file interpolation.c.

304{
305 DMDALocalInfo info;
306 PetscMPIInt rank;
308 rank = user->simCtx->rank;
309 info = user->info;
310
311 // Node ownership range (GLOBAL indices)
312 PetscInt xs_node = info.xs, xm_node = info.xm, xe_node = xs_node + xm_node;
313 PetscInt ys_node = info.ys, ym_node = info.ym, ye_node = ys_node + ym_node;
314 PetscInt zs_node = info.zs, zm_node = info.zm, ze_node = zs_node + zm_node;
315
316 PetscInt nCellsX = info.mx - 2; // Number of cells in x-direction
317 PetscInt nCellsY = info.my - 2; // Number of cells in y-direction
318 PetscInt nCellsZ = info.mz - 2; // Number of cells in z-direction
319
320
321 // Global grid dimensions (used for valid cell check)
322 PetscInt IM = info.mx - 1; // Total nodes in i-direction
323 PetscInt JM = info.my - 1; // Total nodes in j-direction
324 PetscInt KM = info.mz - 1; // Total nodes in k-direction
325
327 "[Rank %d] Starting -- Node ownership k=%" PetscInt_FMT "..%" PetscInt_FMT ", j=%" PetscInt_FMT "..%" PetscInt_FMT ", i=%" PetscInt_FMT "..%" PetscInt_FMT "\n",
328 rank, zs_node, ze_node-1, ys_node, ye_node-1, xs_node, xe_node-1);
329
330 // Loop over the GLOBAL indices of the NODES owned by this processor
331 for (PetscInt k = zs_node; k < ze_node; k++) {
332 for (PetscInt j = ys_node; j < ye_node; j++) {
333 for (PetscInt i = xs_node; i < xe_node; i++) {
334 PetscReal sum = 0.0;
335 PetscInt count = 0;
336
337 // DEBUG 1 TEST
338 /*
339 if(rank == 1 && i == 24 && j == 12 && k == 49){
340 PetscInt i_cell_A = i - 1;
341 PetscInt i_cell_B = i;
342
343 Cmpnts ucat_A = centfield_arr[k][j][i_cell_A]; // 23
344 Cmpnts ucat_B = centfield_arr[k][j][i_cell_B]; // 24 (out-of-bounds if IM=25)
345
346 PetscPrintf(PETSC_COMM_WORLD,"[Rank %d] DEBUG TEST at Node(k,j,i)=%d,%d,%d: Read { Valid Cell }Ucat[%d][%d][%d]=(%.2f,%.2f,%.2f) and {Out-of-Bounds Cell}Ucat[%d][%d][%d]=(%.2f,%.2f,%.2f)\n",
347 rank, k, j, i,
348 k, j, i_cell_A, ucat_A.x, ucat_A.y, ucat_A.z,
349 k, j, i_cell_B, ucat_B.x, ucat_B.y, ucat_B.z);
350
351 }
352 */
353
354 // Skip processing the unused last node in each dimension.
355 if(i >= IM || j >= JM || k >= KM){
356 continue;
357 }
358 // Loop over the 8 potential cells surrounding node N(k,j,i) and accumulate values.
359 // The index offsets correspond to the relative position of the indices(shifted) that represent cell-centered field values of cells that share the node N(k,j,i) as a corner
360 for (PetscInt dk_offset = -1; dk_offset <= 0; dk_offset++) {
361 for (PetscInt dj_offset = -1; dj_offset <= 0; dj_offset++) {
362 for (PetscInt di_offset = -1; di_offset <= 0; di_offset++) {
363
364 // These are still GLOBAL cell indices
365 PetscInt global_cell_k = k + dk_offset;
366 PetscInt global_cell_j = j + dj_offset;
367 PetscInt global_cell_i = i + di_offset;
368
369 // Check if this corresponds to a valid GLOBAL cell index
370 if (global_cell_i >= 0 && global_cell_i < nCellsX &&
371 global_cell_j >= 0 && global_cell_j < nCellsY &&
372 global_cell_k >= 0 && global_cell_k < nCellsZ)
373 {
374 PetscReal cell_val = centfield_arr[global_cell_k + 1][global_cell_j + 1][global_cell_i + 1];
375
376 LOG_LOOP_ALLOW_EXACT(LOCAL, LOG_VERBOSE,k,49,"[Rank %d] successful read from [%" PetscInt_FMT "][%" PetscInt_FMT "][%" PetscInt_FMT "] -> (%.2f)\n",
377 rank,global_cell_k,global_cell_j,global_cell_i,cell_val);
378
379 sum += cell_val;
380 count++;
381 }
382 }
383 }
384 }
385
386 PetscInt i_global_write = i; // Global index in GLOBAL array.
387 PetscInt j_global_write = j;
388 PetscInt k_global_write = k;
389
390 // We write directly into the array using the global loop indices.
391 if (count > 0) {
392 corner_arr[k_global_write][j_global_write][i_global_write] = sum / (PetscReal)count;
393 } else {
394 // This case should ideally not happen for a valid owned node, but as a failsafe:
395 corner_arr[k_global_write][j_global_write][i_global_write] = 0.0;
396 }
397
398 // DEBUG 2
399 /*
400 if(rank == 1){
401 if(i == 11 && j == 11 && k == 49){
402 Cmpnts ucat_node = corner_arr[k][j][i];
403 PetscPrintf(PETSC_COMM_WORLD,"[Rank %d] DEBUG TEST at Node(k,j,i)=%d,%d,%d: Wrote CornerUcat[%d][%d][%d]=(%.2f,%.2f,%.2f)\n",
404 rank, k, j, i,
405 k, j, i, ucat_node.x, ucat_node.y, ucat_node.z);
406 }
407 }
408
409 if(rank == 0 && i == 24 && j == 12 && k == 0){
410 Cmpnts ucat_node = corner_arr[k][j][i];
411 PetscPrintf(PETSC_COMM_WORLD,"[Rank %d] DEBUG TEST at Node(k,j,i)=%d,%d,%d: Wrote CornerUcat[%d][%d][%d]=(%.2f,%.2f,%.2f)\n",
412 rank, k, j, i,
413 k, j, i, ucat_node.x, ucat_node.y, ucat_node.z);
414 }
415 */
416 // LOG_LOOP_ALLOW_EXACT(LOCAL, LOG_VERBOSE,k,48,"[Rank %d] Node(k,j,i)=%d,%d,%d finished loops and write.\n", rank, k, j, i);
417 }
418 }
419 }
421 return 0;
422}

◆ InterpolateCornerToFaceCenter_Scalar()

PetscErrorCode InterpolateCornerToFaceCenter_Scalar ( PetscReal ***  corner_arr,
PetscReal ***  faceX_arr,
PetscReal ***  faceY_arr,
PetscReal ***  faceZ_arr,
UserCtx *  user 
)

Interpolates a scalar field from corner nodes to all face centers.

This routine computes the average of the four corner-node values defining each face of a hexahedral cell:

  • X-faces (perpendicular to X): face between (i-1,i) in X-dir
  • Y-faces (perpendicular to Y): face between (j-1,j) in Y-dir
  • Z-faces (perpendicular to Z): face between (k-1,k) in Z-dir
Parameters
[in]corner_arrGhosted node-centered array (global indexing) from user->fda.
[out]faceX_arrLocal array for X-faces sized [zm][ym][xm+1].
[out]faceY_arrLocal array for Y-faces sized [zm][ym+1][xm].
[out]faceZ_arrLocal array for Z-faces sized [zm+1][ym][xm].
[in]userUser context containing DMDA 'fda' and GetOwnedCellRange.
Returns
PetscErrorCode 0 on success, non-zero on failure.

Interpolates a scalar field from corner nodes to all face centers.

See also
InterpolateCornerToFaceCenter_Scalar()

Definition at line 1910 of file interpolation.c.

1916{
1917 PetscErrorCode ierr;
1918 DMDALocalInfo info;
1919
1920 PetscFunctionBeginUser;
1921
1923
1924 info = user->info;
1925
1926 // Determine owned-cell ranges based on corner-node ownership
1927 PetscInt xs, xm, ys, ym, zs, zm;
1928 ierr = GetOwnedCellRange(&info, 0, &xs, &xm); CHKERRQ(ierr);
1929 ierr = GetOwnedCellRange(&info, 1, &ys, &ym); CHKERRQ(ierr);
1930 ierr = GetOwnedCellRange(&info, 2, &zs, &zm); CHKERRQ(ierr);
1931
1932 // Global exclusive end indices for cells
1933 PetscInt xe = xs + xm;
1934 PetscInt ye = ys + ym;
1935 PetscInt ze = zs + zm;
1936
1937 // --- X‐faces: loops k=zs..ze-1, j=ys..ye-1, i=xs..xe (xm+1 faces per row) ---
1938 for (PetscInt k = zs; k < ze; ++k) {
1939 PetscInt k_loc = k - zs;
1940 for (PetscInt j = ys; j < ye; ++j) {
1941 PetscInt j_loc = j - ys;
1942 for (PetscInt i = xs; i <= xe; ++i) {
1943 PetscInt i_loc = i - xs; // 0..xm
1944 // Average the four corners of the Y-Z face at X = i
1945 PetscReal sum = corner_arr[k ][j ][i]
1946 + corner_arr[k+1][j ][i]
1947 + corner_arr[k ][j+1][i]
1948 + corner_arr[k+1][j+1][i];
1949 faceX_arr[k_loc][j_loc][i_loc] = sum * 0.25;
1950 }
1951 }
1952 }
1953
1954 // --- Y‐faces: loops k=zs..ze-1, j=ys..ye (ym+1 faces), i=xs..xe-1 ---
1955 for (PetscInt k = zs; k < ze; ++k) {
1956 PetscInt k_loc = k - zs;
1957 for (PetscInt j = ys; j <= ye; ++j) {
1958 PetscInt j_loc = j - ys; // 0..ym
1959 for (PetscInt i = xs; i < xe; ++i) {
1960 PetscInt i_loc = i - xs;
1961 // Average the four corners of the X-Z face at Y = j
1962 PetscReal sum = corner_arr[k ][j][i ]
1963 + corner_arr[k+1][j][i ]
1964 + corner_arr[k ][j][i+1]
1965 + corner_arr[k+1][j][i+1];
1966 faceY_arr[k_loc][j_loc][i_loc] = sum * 0.25;
1967 }
1968 }
1969 }
1970
1971 // --- Z‐faces: loops k=zs..ze (zm+1), j=ys..ye-1, i=xs..xe-1 ---
1972 for (PetscInt k = zs; k <= ze; ++k) {
1973 PetscInt k_loc = k - zs;
1974 for (PetscInt j = ys; j < ye; ++j) {
1975 PetscInt j_loc = j - ys;
1976 for (PetscInt i = xs; i < xe; ++i) {
1977 PetscInt i_loc = i - xs;
1978 // Average the four corners of the X-Y face at Z = k
1979 PetscReal sum = corner_arr[k][j ][i ]
1980 + corner_arr[k][j ][i+1]
1981 + corner_arr[k][j+1][i ]
1982 + corner_arr[k][j+1][i+1];
1983 faceZ_arr[k_loc][j_loc][i_loc] = sum * 0.25;
1984 }
1985 }
1986 }
1987
1989
1990 PetscFunctionReturn(0);
1991}
PetscErrorCode GetOwnedCellRange(const DMDALocalInfo *info_nodes, PetscInt dim, PetscInt *xs_cell_global_out, PetscInt *xm_cell_local_out)
Determines the global starting index and number of CELLS owned by the current processor in a specifie...
Definition setup.c:3021
Here is the call graph for this function:

◆ InterpolateCornerToFaceCenter_Vector()

PetscErrorCode InterpolateCornerToFaceCenter_Vector ( Cmpnts ***  corner_arr,
Cmpnts ***  faceX_arr,
Cmpnts ***  faceY_arr,
Cmpnts ***  faceZ_arr,
UserCtx *  user 
)

Interpolates a vector field from corner nodes to all face centers.

Identical to the scalar version, except it averages each component of the Cmpnts struct at the four corner-nodes per face.

Parameters
[in]corner_arrGhosted 3-component array (global node indices).
[out]faceX_arrLocal array of Cmpnts for X-faces sized [zm][ym][xm+1].
[out]faceY_arrLocal array of Cmpnts for Y-faces sized [zm][ym+1][xm].
[out]faceZ_arrLocal array of Cmpnts for Z-faces sized [zm+1][ym][xm].
[in]userUser context containing DMDA 'fda'.
Returns
PetscErrorCode 0 on success.

Interpolates a vector field from corner nodes to all face centers.

See also
InterpolateCornerToFaceCenter_Vector()

Definition at line 1998 of file interpolation.c.

2004{
2005 PetscErrorCode ierr;
2006 DMDALocalInfo info;
2007 PetscMPIInt rank;
2008
2009 PetscFunctionBeginUser;
2010
2012
2013 rank = user->simCtx->rank;
2015 "Rank %d starting InterpolateFieldFromCornerToFaceCenter_Vector.\n", rank);
2016
2017 info = user->info;
2018
2019 PetscInt xs, xm, ys, ym, zs, zm;
2020 ierr = GetOwnedCellRange(&info, 0, &xs, &xm); CHKERRQ(ierr);
2021 ierr = GetOwnedCellRange(&info, 1, &ys, &ym); CHKERRQ(ierr);
2022 ierr = GetOwnedCellRange(&info, 2, &zs, &zm); CHKERRQ(ierr);
2023
2024 PetscInt xe = xs + xm;
2025 PetscInt ye = ys + ym;
2026 PetscInt ze = zs + zm;
2027
2028 // X-faces
2029 for (PetscInt k = zs; k < ze; ++k) {
2030 PetscInt k_loc = k - zs;
2031 for (PetscInt j = ys; j < ye; ++j) {
2032 PetscInt j_loc = j - ys;
2033 for (PetscInt i = xs; i <= xe; ++i) {
2034 PetscInt i_loc = i - xs;
2035 Cmpnts sum = {0,0,0};
2036 sum.x = corner_arr[k ][j ][i].x + corner_arr[k+1][j ][i].x
2037 + corner_arr[k ][j+1][i].x + corner_arr[k+1][j+1][i].x;
2038 sum.y = corner_arr[k ][j ][i].y + corner_arr[k+1][j ][i].y
2039 + corner_arr[k ][j+1][i].y + corner_arr[k+1][j+1][i].y;
2040 sum.z = corner_arr[k ][j ][i].z + corner_arr[k+1][j ][i].z
2041 + corner_arr[k ][j+1][i].z + corner_arr[k+1][j+1][i].z;
2042 faceX_arr[k_loc][j_loc][i_loc].x = sum.x * 0.25;
2043 faceX_arr[k_loc][j_loc][i_loc].y = sum.y * 0.25;
2044 faceX_arr[k_loc][j_loc][i_loc].z = sum.z * 0.25;
2045 }
2046 }
2047 }
2048
2050 "Rank %d x-face Interpolation complete.\n", rank);
2051
2052 // Y-faces
2053 for (PetscInt k = zs; k < ze; ++k) {
2054 PetscInt k_loc = k - zs;
2055 for (PetscInt j = ys; j <= ye; ++j) {
2056 PetscInt j_loc = j - ys;
2057 for (PetscInt i = xs; i < xe; ++i) {
2058 PetscInt i_loc = i - xs;
2059 Cmpnts sum = {0,0,0};
2060 sum.x = corner_arr[k ][j][i ].x + corner_arr[k+1][j][i ].x
2061 + corner_arr[k ][j][i+1].x + corner_arr[k+1][j][i+1].x;
2062 sum.y = corner_arr[k ][j][i ].y + corner_arr[k+1][j][i ].y
2063 + corner_arr[k ][j][i+1].y + corner_arr[k+1][j][i+1].y;
2064 sum.z = corner_arr[k ][j][i ].z + corner_arr[k+1][j][i ].z
2065 + corner_arr[k ][j][i+1].z + corner_arr[k+1][j][i+1].z;
2066 faceY_arr[k_loc][j_loc][i_loc].x = sum.x * 0.25;
2067 faceY_arr[k_loc][j_loc][i_loc].y = sum.y * 0.25;
2068 faceY_arr[k_loc][j_loc][i_loc].z = sum.z * 0.25;
2069 }
2070 }
2071 }
2072
2074 "Rank %d y-face Interpolation complete.\n", rank);
2075
2076 // Z-faces
2077 for (PetscInt k = zs; k <= ze; ++k) {
2078 PetscInt k_loc = k - zs;
2079 for (PetscInt j = ys; j < ye; ++j) {
2080 PetscInt j_loc = j - ys;
2081 for (PetscInt i = xs; i < xe; ++i) {
2082 PetscInt i_loc = i - xs;
2083 Cmpnts sum = {0,0,0};
2084 sum.x = corner_arr[k][j ][i ].x + corner_arr[k][j ][i+1].x
2085 + corner_arr[k][j+1][i ].x + corner_arr[k][j+1][i+1].x;
2086 sum.y = corner_arr[k][j ][i ].y + corner_arr[k][j ][i+1].y
2087 + corner_arr[k][j+1][i ].y + corner_arr[k][j+1][i+1].y;
2088 sum.z = corner_arr[k][j ][i ].z + corner_arr[k][j ][i+1].z
2089 + corner_arr[k][j+1][i ].z + corner_arr[k][j+1][i+1].z;
2090 faceZ_arr[k_loc][j_loc][i_loc].x = sum.x * 0.25;
2091 faceZ_arr[k_loc][j_loc][i_loc].y = sum.y * 0.25;
2092 faceZ_arr[k_loc][j_loc][i_loc].z = sum.z * 0.25;
2093 }
2094 }
2095 }
2096
2098 "Rank %d z-face Interpolation complete.\n", rank);
2099
2101 PetscFunctionReturn(0);
2102}
@ LOG_TRACE
Very fine-grained tracing information for in-depth debugging.
Definition logging.h:33
Here is the call graph for this function: