PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
|
Header file for particle location functions using the walking search algorithm. More...
#include <petsc.h>
#include <stdbool.h>
#include <math.h>
#include "variables.h"
#include "logging.h"
#include "setup.h"
Go to the source code of this file.
Functions | |
PetscErrorCode | GetCellCharacteristicSize (const Cell *cell, PetscReal *cellSize) |
Estimates a characteristic length of the cell for threshold scaling. | |
PetscErrorCode | ComputeSignedDistanceToPlane (const Cmpnts v1, const Cmpnts v2, const Cmpnts v3, const Cmpnts v4, const Cmpnts cell_centroid, const Cmpnts p_target, PetscReal *d_signed, const PetscReal threshold) |
Computes the signed distance from a point to the plane approximating a quadrilateral face. | |
PetscErrorCode | CalculateDistancesToCellFaces (const Cmpnts p, const Cell *cell, PetscReal *d, const PetscReal threshold) |
Computes the signed distances from a point to each face of a cubic cell. | |
PetscErrorCode | DeterminePointPosition (PetscReal *d, PetscInt *result) |
Classifies a point based on precomputed face distances. | |
PetscErrorCode | GetCellVerticesFromGrid (Cmpnts ***coor, PetscInt idx, PetscInt idy, PetscInt idz, Cell *cell) |
Retrieves the coordinates of the eight vertices of a cell based on grid indices. | |
PetscErrorCode | InitializeTraversalParameters (UserCtx *user, Particle *particle, PetscInt *idx, PetscInt *idy, PetscInt *idz, PetscInt *traversal_steps) |
Initializes traversal parameters for locating a particle. | |
PetscErrorCode | CheckCellWithinLocalGrid (UserCtx *user, PetscInt idx, PetscInt idy, PetscInt idz, PetscBool *is_within) |
Checks if the current cell indices are within the local grid boundaries. | |
PetscErrorCode | RetrieveCurrentCell (UserCtx *user, PetscInt idx, PetscInt idy, PetscInt idz, Cell *cell) |
Retrieves the coordinates of the eight vertices of the current cell. | |
PetscErrorCode | EvaluateParticlePosition (const Cell *cell, PetscReal *d, const Cmpnts p, PetscInt *position, const PetscReal threshold) |
Determines the spatial relationship of a particle relative to a cubic cell. | |
PetscErrorCode | LocateParticleInGrid (UserCtx *user, Particle *particle) |
Locates the grid cell containing a particle using a walking search. | |
PetscErrorCode | UpdateCellIndicesBasedOnDistances (PetscReal d[NUM_FACES], PetscInt *idx, PetscInt *idy, PetscInt *idz, DMDALocalInfo *info) |
Updates the cell indices based on the signed distances to each face. | |
PetscErrorCode | FinalizeTraversal (UserCtx *user, Particle *particle, PetscInt traversal_steps, PetscBool cell_found, PetscInt idx, PetscInt idy, PetscInt idz) |
Finalizes the traversal by reporting the results. | |
PetscErrorCode | FindOwnerOfCell (UserCtx *user, PetscInt i, PetscInt j, PetscInt k, PetscMPIInt *owner_rank) |
Finds the MPI rank that owns a given global cell index. | |
PetscErrorCode | LocateParticleOrFindMigrationTarget_TEST (UserCtx *user, Particle *particle, ParticleLocationStatus *status_out) |
Locates a particle's host cell or identifies its migration target using a robust walk search. | |
PetscErrorCode | ReportSearchOutcome (const Particle *particle, ParticleLocationStatus status, PetscInt traversal_steps) |
Logs the final outcome of the particle location search. | |
PetscErrorCode | UpdateCellIndicesBasedOnDistancesTEST (PetscReal d[NUM_FACES], PetscInt *idx, PetscInt *idy, PetscInt *idz) |
Updates the cell indices based on the signed distances to each face. | |
Header file for particle location functions using the walking search algorithm.
This file contains declarations of functions that implement the walking search algorithm to locate particles within a computational grid. It includes functions for computing distances to cell faces, determining particle positions relative to cells, and updating traversal parameters.
Definition in file walkingsearch.h.
PetscErrorCode GetCellCharacteristicSize | ( | const Cell * | cell, |
PetscReal * | cellSize | ||
) |
Estimates a characteristic length of the cell for threshold scaling.
For a hexahedral cell with vertices cell->vertices[0..7], we approximate the cell size by some measure—e.g. average edge length or diagonal.
[in] | cell | A pointer to the Cell structure |
[out] | cellSize | A pointer to a PetscReal where the characteristic size is stored. |
Definition at line 25 of file walkingsearch.c.
PetscErrorCode ComputeSignedDistanceToPlane | ( | const Cmpnts | v1, |
const Cmpnts | v2, | ||
const Cmpnts | v3, | ||
const Cmpnts | v4, | ||
const Cmpnts | cell_centroid, | ||
const Cmpnts | p_target, | ||
PetscReal * | d_signed, | ||
const PetscReal | threshold | ||
) |
Computes the signed distance from a point to the plane approximating a quadrilateral face.
This function calculates the signed distance from a given point p_target
to the plane approximating a quadrilateral face defined by points v1
, v2
, v3
, and v4
. The plane's initial normal is determined using two edge vectors emanating from v1
(i.e., v2-v1
and v4-v1
).
Normal Orientation: The initial normal's orientation is checked against the cell's interior. A vector from the face centroid to the cell centroid (vec_face_to_cell_centroid
) is computed. If the dot product of the initial normal and vec_face_to_cell_centroid
is positive, it means the initial normal is pointing towards the cell's interior (it's an inward normal relative to the cell). In this case, the normal is flipped to ensure it points outward.
Signed Distance Convention: The signed distance is the projection of the vector from p_target
to the face's centroid onto the (now guaranteed) outward-pointing plane's normal vector.
d_signed < 0
: p_target
is "outside" and "beyond" the face, in the direction of the outward normal. This is the primary case indicating the particle has crossed this face.d_signed > 0
: p_target
is "outside" but "behind" the face (on the same side as the cell interior relative to this face plane).d_signed = 0
: p_target
is on the face plane (within threshold).[in] | v1 | First vertex defining the face (used as origin for initial normal calculation). |
[in] | v2 | Second vertex defining the face (used for first edge vector: v2-v1). |
[in] | v3 | Third vertex defining the face (used for face centroid calculation). |
[in] | v4 | Fourth vertex defining the face (used for second edge vector: v4-v1). |
[in] | cell_centroid | The centroid of the 8-vertex cell to which this face belongs. |
[in] | p_target | The point from which the distance to the plane is calculated. |
[out] | d_signed | Pointer to store the computed signed distance. |
[in] | threshold | The threshold below which the absolute distance is considered zero. |
v1, v2, v3, v4
is used for calculating the face centroid. The order of v1, v2, v4
is used for the initial normal calculation.cell_centroid
is crucial for robustly determining the outward direction of the normal.d_signed
is NULL, or PETSC_ERR_USER if a degenerate plane (near-zero normal) is detected. Definition at line 102 of file walkingsearch.c.
PetscErrorCode CalculateDistancesToCellFaces | ( | const Cmpnts | p, |
const Cell * | cell, | ||
PetscReal * | d, | ||
const PetscReal | threshold | ||
) |
Computes the signed distances from a point to each face of a cubic cell.
This function calculates the signed distances from a specified point p
to each of the six faces of a cubic cell. The cell is defined by its eight vertices, and the distances are stored in the memory location pointed to by d
.
[in] | p | The target point in 3D space. |
[in] | cell | Pointer to a Cell structure that defines the cubic cell via its vertices. |
[out] | d | A pointer to an array of six PetscReal values where the computed signed distances will be stored. |
[in] | threshold | A PetscReal value that specifies the minimum distance below which a computed distance is considered to be zero. |
This function calculates the signed distances from a specified point p
to each of the six faces of a cubic cell. The cell is defined by its eight vertices, and the distances are stored in the memory location pointed to by d
. Each distance corresponds to a specific face of the cell, as enumerated by the Face
enumeration. A threshold value is used to determine when a distance should be considered effectively zero, accounting for floating-point precision limitations.
[in] | p | The target point in 3D space for which distances to the cell's faces are to be calculated. Represented by the Cmpnts structure. |
[in] | cell | A pointer to a Cell structure that defines the cubic cell via its eight vertices. The vertices must be ordered consistently to ensure correct normal directions for each face. |
[out] | d | A pointer to an array of six PetscReal values where the computed signed distances will be stored. Each index in the array corresponds to a specific face as defined by the Face enumeration:
|
[in] | threshold | A PetscReal value that specifies the minimum distance below which a computed distance is considered to be zero. This helps in mitigating issues arising from floating-point arithmetic inaccuracies. |
threshold
parameter provides flexibility in handling cases where the point p
is extremely close to a face, allowing the user to define what constitutes "close enough" to be treated as zero distance based on the specific requirements of their application. Definition at line 271 of file walkingsearch.c.
PetscErrorCode DeterminePointPosition | ( | PetscReal * | d, |
PetscInt * | result | ||
) |
Classifies a point based on precomputed face distances.
Given an array of six distances d[NUM_FACES] from the point to each face of a hexahedral cell, this function determines: 0 => inside 1 => on a single face 2 => on an edge (2 faces = 0) 3 => on a corner (3+ faces = 0) -1 => outside
[in] | d | Array of six face distances. |
[out] | result | Pointer to an integer classification: {0,1,2,3,-1}. |
Definition at line 407 of file walkingsearch.c.
PetscErrorCode GetCellVerticesFromGrid | ( | Cmpnts *** | coor, |
PetscInt | idx, | ||
PetscInt | idy, | ||
PetscInt | idz, | ||
Cell * | cell | ||
) |
Retrieves the coordinates of the eight vertices of a cell based on grid indices.
This function populates the cell
structure with the coordinates of the eight vertices of a hexahedral cell given its grid indices (idx, idy, idz)
.
[in] | coor | Pointer to a 3D array of Cmpnts structures representing the grid coordinates. |
[in] | idx | The i-index of the cell. |
[in] | idy | The j-index of the cell. |
[in] | idz | The k-index of the cell. |
[out] | cell | Pointer to a Cell structure to store the cell's vertices. |
This function populates the cell
structure with the coordinates of the eight vertices of a hexahedral cell given its grid indices (idx, idy, idz)
. The vertex numbering follows the convention depicted in the provided figure:
Vertex Numbering and Positions:
(i, j, k)
→ cell->vertices[0]
(i+1, j, k)
→ cell->vertices[1]
(i+1, j+1, k)
→ cell->vertices[2]
(i, j+1, k)
→ cell->vertices[3]
(i, j+1, k+1)
→ cell->vertices[4]
(i+1, j+1, k+1)
→ cell->vertices[5]
(i+1, j, k+1)
→ cell->vertices[6]
(i, j, k+1)
→ cell->vertices[7]
[in] | coor | Pointer to a 3D array of Cmpnts structures representing the grid coordinates. |
[in] | idx | The i-index of the cell. |
[in] | idy | The j-index of the cell. |
[in] | idz | The k-index of the cell. |
[out] | cell | Pointer to a Cell structure to store the cell's vertices. |
(idx, idy, idz)
are within valid bounds.coor
array should be properly initialized with accurate coordinates for all grid points.Definition at line 521 of file walkingsearch.c.
PetscErrorCode InitializeTraversalParameters | ( | UserCtx * | user, |
Particle * | particle, | ||
PetscInt * | idx, | ||
PetscInt * | idy, | ||
PetscInt * | idz, | ||
PetscInt * | traversal_steps | ||
) |
Initializes traversal parameters for locating a particle.
This function sets the initial cell indices based on the local grid boundaries and initializes the traversal step counter.
[in] | user | Pointer to the user-defined context containing grid information. |
[in] | particle | Pointer to the Particle structure containing its location. |
[out] | idx | Pointer to store the initial i-index of the cell. |
[out] | idy | Pointer to store the initial j-index of the cell. |
[out] | idz | Pointer to store the initial k-index of the cell. |
[out] | traversal_steps | Pointer to store the initial traversal step count. |
This function sets the initial cell indices for the grid search.
[in] | user | Pointer to the user-defined context containing grid information. |
[in] | particle | Pointer to the Particle structure containing its location and previous cell (if any). |
[out] | idx | Pointer to store the initial i-index of the cell. |
[out] | idy | Pointer to store the initial j-index of the cell. |
[out] | idz | Pointer to store the initial k-index of the cell. |
[out] | traversal_steps | Pointer to store the initial traversal step count. |
Definition at line 571 of file walkingsearch.c.
PetscErrorCode CheckCellWithinLocalGrid | ( | UserCtx * | user, |
PetscInt | idx, | ||
PetscInt | idy, | ||
PetscInt | idz, | ||
PetscBool * | is_within | ||
) |
Checks if the current cell indices are within the local grid boundaries.
[in] | user | Pointer to the user-defined context containing grid information. |
[in] | idx | The i-index of the current cell. |
[in] | idy | The j-index of the current cell. |
[in] | idz | The k-index of the current cell. |
[out] | is_within | Pointer to a PetscBool that will be set to PETSC_TRUE if within bounds, else PETSC_FALSE. |
Checks if the current cell indices are within the local grid boundaries.
This function determines if the provided global cell indices (idx, idy, idz) fall within the range of cells covered by the current process's owned and ghost NODES. A cell C(i,j,k) (origin N(i,j,k)) is considered within this ghosted region if its origin node N(i,j,k) is within the rank's ghosted nodal region, AND node N(i+1,j+1,k+1) is also within it (to ensure the entire cell extent is covered by available node data). More simply, we check if the cell's origin node is within the range of nodes that can form the start of a ghosted cell.
[in] | user | Pointer to the user-defined context (needs user->fda for node info). |
[in] | idx | The global i-index of the cell's origin node. |
[in] | idy | The global j-index of the cell's origin node. |
[in] | idz | The global k-index of the cell's origin node. |
[out] | is_within | Pointer to a PetscBool that will be set to PETSC_TRUE if within ghosted bounds, else PETSC_FALSE. |
Definition at line 650 of file walkingsearch.c.
PetscErrorCode RetrieveCurrentCell | ( | UserCtx * | user, |
PetscInt | idx, | ||
PetscInt | idy, | ||
PetscInt | idz, | ||
Cell * | cell | ||
) |
Retrieves the coordinates of the eight vertices of the current cell.
[in] | user | Pointer to the user-defined context containing grid information. |
[in] | idx | The i-index of the current cell. |
[in] | idy | The j-index of the current cell. |
[in] | idz | The k-index of the current cell. |
[out] | cell | Pointer to a Cell structure to store the cell's vertices. |
Definition at line 722 of file walkingsearch.c.
PetscErrorCode EvaluateParticlePosition | ( | const Cell * | cell, |
PetscReal * | d, | ||
const Cmpnts | p, | ||
PetscInt * | position, | ||
const PetscReal | threshold | ||
) |
Determines the spatial relationship of a particle relative to a cubic cell.
This function evaluates whether a particle located at a specific point p
in 3D space is positioned inside the cell, on the boundary of the cell, or outside the cell. The determination is based on the signed distances from the particle to each of the six faces of the cell. The function utilizes the computed distances to ascertain the particle's position with respect to the cell boundaries, considering a threshold to account for floating-point precision.
[in] | cell | A pointer to a Cell structure that defines the cubic cell via its vertices. The cell's geometry is essential for accurately computing the distances to each face. |
[in] | d | A pointer to an array of six PetscReal values that store the signed distances from the particle to each face of the cell. These distances are typically computed using the CalculateDistancesToCellFaces function. |
[in] | p | The location of the particle in 3D space, represented by the Cmpnts structure. This point is the reference for distance calculations to the cell's faces. |
[out] | position | A pointer to an integer that will be set based on the particle's position relative to the cell:
|
[in] | threshold | A PetscReal value that defines the minimum distance below which a computed distance is considered to be zero. This threshold helps in mitigating inaccuracies due to floating-point arithmetic, especially when determining if the particle lies exactly on the boundary. |
0
if the function executes successfully. If an error occurs, a non-zero error code is returned, indicating the type of failure.d
array has been properly allocated and contains valid distance measurements before calling this function.CalculateDistancesToCellFaces
to accurately compute the signed distances to each face. Any inaccuracies in distance calculations can affect the determination of the particle's position.threshold
parameter should be chosen based on the specific precision requirements of the application to balance between sensitivity and robustness against floating-point errors.Definition at line 811 of file walkingsearch.c.
Locates the grid cell containing a particle using a walking search.
This function implements a walking search algorithm to find the specific cell (identified by global indices i, j, k) that encloses the particle's physical location (particle->loc
).
The search starts from an initial guess cell (either the particle's previously known cell or the corner of the local process domain) and iteratively steps to adjacent cells based on the signed distances from the particle to the faces of the current cell.
Upon successful location (position == 0
), the particle's cell
field is updated with the found indices (i,j,k), and the corresponding interpolation weights
are calculated and stored using the distances (d
) relative to the final cell.
Handles particles exactly on cell boundaries by attempting a tie-breaker if the search gets stuck oscillating between adjacent cells due to the boundary condition.
[in] | user | Pointer to the UserCtx structure containing grid information (DMDA, coordinates) and domain boundaries. |
[in,out] | particle | Pointer to the Particle structure. Its loc field provides the target position. On successful return, its cell and weights fields are updated. On failure, cell is set to {-1, -1, -1} and weights to {0.0, 0.0, 0.0} . |
particle->cell[0] >= 0
afterward.InitializeTraversalParameters
, CheckCellWithinLocalGrid
, RetrieveCurrentCell
, EvaluateParticlePosition
, UpdateCellIndicesBasedOnDistances
, UpdateParticleWeights
, and FinalizeTraversal
. particle->loc
is set correctly before calling. MAX_TRAVERSAL
steps are exceeded. PetscErrorCode UpdateCellIndicesBasedOnDistances | ( | PetscReal | d[NUM_FACES], |
PetscInt * | idx, | ||
PetscInt * | idy, | ||
PetscInt * | idz, | ||
DMDALocalInfo * | info | ||
) |
Updates the cell indices based on the signed distances to each face.
This function modifies the cell indices (idx
, idy
, idz
) to move towards the direction where the particle is likely to be located, based on positive distances indicating that the particle is outside in that particular direction.
[in] | d | An array of six PetscReal values representing the signed distances to each face:
|
[out] | idx | Pointer to the i-index of the cell to be updated. |
[out] | idy | Pointer to the j-index of the cell to be updated. |
[out] | idz | Pointer to the k-index of the cell to be updated. |
[in] | info | DMDALocalInfo structure that holds local & global domain bounds. |
PetscErrorCode FinalizeTraversal | ( | UserCtx * | user, |
Particle * | particle, | ||
PetscInt | traversal_steps, | ||
PetscBool | cell_found, | ||
PetscInt | idx, | ||
PetscInt | idy, | ||
PetscInt | idz | ||
) |
Finalizes the traversal by reporting the results.
This function prints the outcome of the traversal, indicating whether the particle was found within a cell or not, and updates the particle's cell indices accordingly.
[in] | user | Pointer to the user-defined context containing grid information. |
[out] | particle | Pointer to the Particle structure to update with cell indices. |
[in] | traversal_steps | The number of traversal steps taken. |
[in] | cell_found | Flag indicating whether the particle was found within a cell. |
[in] | idx | The i-index of the found cell. |
[in] | idy | The j-index of the found cell. |
[in] | idz | The k-index of the found cell. |
Definition at line 977 of file walkingsearch.c.
PetscErrorCode FindOwnerOfCell | ( | UserCtx * | user, |
PetscInt | i, | ||
PetscInt | j, | ||
PetscInt | k, | ||
PetscMPIInt * | owner_rank | ||
) |
Finds the MPI rank that owns a given global cell index.
This function performs a linear search through the pre-computed decomposition map (user->RankCellInfoMap
) to determine which process is responsible for the cell with global indices (i, j, k). It is the definitive method for resolving cell ownership in the "Walk and Handoff" migration algorithm.
If the provided indices are outside the range of any rank (e.g., negative or beyond the global domain), the function will not find an owner and owner_rank
will be set to -1.
[in] | user | Pointer to the UserCtx structure, which must contain the initialized RankCellInfoMap and num_ranks . |
[in] | i | Global i-index of the cell to find. |
[in] | j | Global j-index of the cell to find. |
[in] | k | Global k-index of the cell to find. |
[out] | owner_rank | Pointer to a PetscMPIInt where the resulting owner rank will be stored. It is set to -1 if no owner is found. |
Definition at line 1026 of file walkingsearch.c.
PetscErrorCode LocateParticleOrFindMigrationTarget_TEST | ( | UserCtx * | user, |
Particle * | particle, | ||
ParticleLocationStatus * | status_out | ||
) |
Locates a particle's host cell or identifies its migration target using a robust walk search.
This is the core search engine. It starts from a guess cell and walks through the grid. It returns a definitive, actionable status indicating the outcome:
ACTIVE_AND_LOCATED
: The particle was found in a cell on the current rank.MIGRATING_OUT
: The particle was found to belong to another rank. particle->destination_rank
is set.LOST
: The search failed to find the particle within the global domain.This function is globally aware and can walk across MPI rank boundaries. It contains robust checks for global domain boundaries and a tie-breaker for numerically "stuck" particles on cell faces.
[in] | user | Pointer to the UserCtx containing all grid and domain info. |
[in,out] | particle | Pointer to the Particle struct. Its fields are updated based on the search outcome. |
[out] | status_out | The final, actionable status of the particle after the search. |
Definition at line 1094 of file walkingsearch.c.
PetscErrorCode ReportSearchOutcome | ( | const Particle * | particle, |
ParticleLocationStatus | status, | ||
PetscInt | traversal_steps | ||
) |
Logs the final outcome of the particle location search.
Definition at line 1247 of file walkingsearch.c.
PetscErrorCode UpdateCellIndicesBasedOnDistancesTEST | ( | PetscReal | d[NUM_FACES], |
PetscInt * | idx, | ||
PetscInt * | idy, | ||
PetscInt * | idz | ||
) |
Updates the cell indices based on the signed distances to each face.
This function modifies the cell indices (idx
, idy
, idz
) to move towards the direction where the particle is likely to be located, based on positive distances indicating that the particle is outside in that particular direction.
[in] | d | An array of six PetscReal values representing the signed distances to each face:
|
[out] | idx | Pointer to the i-index of the cell to be updated. |
[out] | idy | Pointer to the j-index of the cell to be updated. |
[out] | idz | Pointer to the k-index of the cell to be updated. |
[in] | info | DMDALocalInfo structure that holds local & global domain bounds. |
Definition at line 882 of file walkingsearch.c.