#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"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. | |
| #define NUM_WEIGHTS 8 |
Definition at line 24 of file interpolation.h.
| #define InterpolateFieldFromCornerToCenter | ( | field, | |
| centfield, | |||
| user | |||
| ) |
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.
| #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.
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.
| blockSize | The runtime block size (1 for scalar, 3 for vector). |
| centfield_ptr | The input void* pointer to the 3D cell-centered data array. |
| corner_ptr | The output void* pointer to the 3D corner data array. |
| user_ctx | The UserCtx structure. |
Definition at line 56 of file interpolation.h.
| #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.
This macro uses C11's _Generic feature to dispatch to the appropriate underlying function based on the type of the input corner_arr.
corner_arr is of type PetscReal***, it calls InterpolateCornerToFaceCenter_Scalar.corner_arr is of type Cmpnts***, it calls InterpolateCornerToFaceCenter_Vector.| corner_arr | Ghosted node-centered array (global indexing). The type of this argument determines which function is called. |
| faceX_arr | Local array for X-faces. |
| faceY_arr | Local array for Y-faces. |
| faceZ_arr | Local array for Z-faces. |
| user_ctx | User context containing DMDA 'fda'. |
Definition at line 79 of file interpolation.h.
| #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).
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.
| typedef struct VelocityPointInterpolator VelocityPointInterpolator |
The arrays InterpolateVelocityAtPoint() reads, held from Begin to End.
Definition at line 160 of file interpolation.h.
| 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].
| [in] | fieldScal | Scalar array indexed [k][j][i]. |
| [in] | i | i index of the lowest of the eight entries. |
| [in] | j | j index of the lowest of the eight entries. |
| [in] | k | k index of the lowest of the eight entries. |
| [in] | a1 | Coordinate in the eight-entry cell along i, in [0,1]. |
| [in] | a2 | Coordinate along j, in [0,1]. |
| [in] | a3 | Coordinate along k, in [0,1]. |
| [out] | val | The interpolated value. |
Trilinear interpolation of a scalar from the eight entries (i..i+1, j..j+1, k..k+1).
Definition at line 473 of file interpolation.c.
| 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.
| [in] | fieldVec | Vector array indexed [k][j][i]. |
| [in] | i | i index of the lowest of the eight entries. |
| [in] | j | j index of the lowest of the eight entries. |
| [in] | k | k index of the lowest of the eight entries. |
| [in] | a1 | Coordinate in the eight-entry cell along i, in [0,1]. |
| [in] | a2 | Coordinate along j, in [0,1]. |
| [in] | a3 | Coordinate along k, in [0,1]. |
| [out] | vec | The interpolated vector. |
Trilinear interpolation of a vector from the eight entries (i..i+1, j..j+1, k..k+1).
Definition at line 524 of file interpolation.c.
| 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.
| [in] | user | Grid context; its Ucat, Ucont and metrics must be current. |
| [out] | interp | Close with InterpolateVelocityAtPointEnd(). |
Opens the arrays for evaluating the particle velocity at points, with the configured interpolation method.
Definition at line 1250 of file interpolation.c.
| 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.
| [in] | interp | From InterpolateVelocityAtPointBegin(). |
| [in] | cell | Host cell, geometric indices; the point must be in a cell this rank owns. |
| [in] | weights | The point's logical coordinates in the host cell. |
| [in] | point | The point. |
| [out] | u | The velocity; zero, as in the swarm pass, if the stencil lies outside the ghosted region. |
The velocity a particle at a point would be given: the value the swarm pass of the configured method computes there.
Definition at line 1284 of file interpolation.c.
| PetscErrorCode InterpolateVelocityAtPointEnd | ( | VelocityPointInterpolator ** | interp | ) |
Closes the arrays InterpolateVelocityAtPointBegin() opened.
| [in,out] | interp | Set to NULL. |
Closes the arrays InterpolateVelocityAtPointBegin() opened.
Definition at line 1319 of file interpolation.c.
| 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:
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.
| [in,out] | user | Block context holding the swarm and the source fields. |
Interpolates the Eulerian fields the particles carry to every local particle.
Definition at line 1420 of file interpolation.c.
| 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.
| [in] | field_arr | 3D array of corner-based scalar data (from user->da). |
| [out] | centfield_arr | 3D array for interpolated cell-center scalar data (for user->fda). |
| [in] | user | User context containing:
|
Safely interpolate a scalar field from corner nodes (from the coordinate DM) to cell centers (from the cell-centered DM) using the provided UserCtx.
Definition at line 71 of file interpolation.c.
| 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.
| [in] | field_arr | 3D array of corner-based vector data (from user->da). |
| [out] | centfield_arr | 3D array for interpolated cell-center vector data (for user->fda). |
| [in] | user | User context containing:
|
Safely interpolate a vector field from corner nodes (from the coordinate DM) to cell centers (from the cell-centered DM) using the provided UserCtx.
Definition at line 23 of file interpolation.c.
| 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:
InterpolateFieldFromCornerToCenter macro to interpolate these coordinates to the cell centers, storing the result in a new temporary vector.user->Cent vector, which is assumed to have been computed by ComputeCellCenters and serves as the ground truth.ComputeCellCenters has been successfully executed.| user | The UserCtx for a specific grid level. |
Tests the InterpolateFieldFromCornerToCenter function by reproducing the Cent vector.
Definition at line 109 of file interpolation.c.
| 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.
| [in] | centfield_arr | Input: 3D array (ghosted) of cell-centered data, accessed via GLOBAL indices. |
| [out] | corner_arr | Output: 3D array (ghosted) where interpolated node values are stored, also accessed via GLOBAL indices for the owned part. |
| [in] | user | User context containing DMDA information. |
Interpolates a vector field from cell centers to corner nodes.
Definition at line 168 of file interpolation.c.
| 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.
| [in] | centfield_arr | Input: 3D array (ghosted) of scalar data at cell centers, accessed via GLOBAL indices. |
| [out] | corner_arr | Output: 3D array (ghosted) where interpolated node values are stored, also accessed via GLOBAL indices for the owned part. |
| [in] | user | User context containing DMDA information. |
Interpolates a scalar field from cell centers to corner nodes.
Definition at line 300 of file interpolation.c.
| 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:
| [in] | corner_arr | Ghosted node-centered array (global indexing) from user->fda. |
| [out] | faceX_arr | Local array for X-faces sized [zm][ym][xm+1]. |
| [out] | faceY_arr | Local array for Y-faces sized [zm][ym+1][xm]. |
| [out] | faceZ_arr | Local array for Z-faces sized [zm+1][ym][xm]. |
| [in] | user | User context containing DMDA 'fda' and GetOwnedCellRange. |
Interpolates a scalar field from corner nodes to all face centers.
Definition at line 1910 of file interpolation.c.
| 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.
| [in] | corner_arr | Ghosted 3-component array (global node indices). |
| [out] | faceX_arr | Local array of Cmpnts for X-faces sized [zm][ym][xm+1]. |
| [out] | faceY_arr | Local array of Cmpnts for Y-faces sized [zm][ym+1][xm]. |
| [out] | faceZ_arr | Local array of Cmpnts for Z-faces sized [zm+1][ym][xm]. |
| [in] | user | User context containing DMDA 'fda'. |
Interpolates a vector field from corner nodes to all face centers.
Definition at line 1998 of file interpolation.c.