PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
|
Header file for Particle Motion and migration related functions. More...
#include <petsc.h>
#include <petscdmswarm.h>
#include <stdbool.h>
#include <math.h>
#include "variables.h"
#include "logging.h"
#include "walkingsearch.h"
#include "Metric.h"
#include "io.h"
Go to the source code of this file.
Functions | |
PetscErrorCode | CreateParticleSwarm (UserCtx *user, PetscInt numParticles, PetscInt *particlesPerProcess, BoundingBox *bboxlist) |
Creates and initializes a Particle Swarm. | |
PetscErrorCode | InitializeSwarm (UserCtx *user) |
Initializes the DMSwarm object within the UserCtx structure. | |
PetscErrorCode | RegisterSwarmField (DM swarm, const char *fieldName, PetscInt fieldDim, PetscDataType dtype) |
Registers a swarm field without finalizing registration. | |
PetscErrorCode | RegisterParticleFields (DM swarm) |
Registers necessary particle fields within the DMSwarm. | |
PetscErrorCode | InitializeRandomGenerators (UserCtx *user, PetscRandom *randx, PetscRandom *randy, PetscRandom *randz) |
Initializes random number generators for assigning particle properties. | |
PetscErrorCode | InitializeLogicalSpaceRNGs (PetscRandom *rand_logic_i, PetscRandom *rand_logic_j, PetscRandom *rand_logic_k) |
Initializes random number generators for logical space operations [0.0, 1.0). | |
PetscErrorCode | AssignInitialPropertiesToSwarm (UserCtx *user, PetscInt particlesPerProcess, PetscRandom *rand_phys_x, PetscRandom *rand_phys_y, PetscRandom *rand_phys_z, PetscRandom *rand_logic_i, PetscRandom *rand_logic_j, PetscRandom *rand_logic_k, BoundingBox *bboxlist) |
Initializes all particle properties in the swarm. | |
PetscErrorCode | DistributeParticles (PetscInt numParticles, PetscMPIInt rank, PetscMPIInt size, PetscInt *particlesPerProcess, PetscInt *remainder) |
Distributes particles evenly across MPI processes, handling any remainders. | |
PetscErrorCode | FinalizeSwarmSetup (PetscRandom *randx, PetscRandom *randy, PetscRandom *randz, PetscRandom *rand_logic_i, PetscRandom *rand_logic_j, PetscRandom *rand_logic_k) |
Finalizes the swarm setup by destroying random generators and logging completion. | |
PetscErrorCode | UnpackSwarmFields (PetscInt i, const PetscInt64 *PIDs, const PetscReal *weights, const PetscReal *positions, const PetscInt *cellIndices, PetscReal *velocities, PetscInt *LocStatus, Particle *particle) |
Initializes a Particle struct with data from DMSwarm fields. | |
PetscErrorCode | UpdateSwarmFields (PetscInt i, const Particle *particle, PetscReal *weights, PetscInt *cellIndices, PetscInt *status_field) |
Updates DMSwarm fields with data from a Particle struct. | |
PetscBool | IsParticleInsideBoundingBox (const BoundingBox *bbox, const Particle *particle) |
Checks if a particle's location is within a specified bounding box. | |
PetscErrorCode | UpdateParticleWeights (PetscReal *d, Particle *particle) |
Updates a particle's interpolation weights based on distances to cell faces. | |
PetscErrorCode | PrepareLoadedSwarmForRelocation (UserCtx *user) |
Resets the location-dependent state of a loaded swarm to force relocation. | |
PetscErrorCode | InitializeParticleSwarm (SimCtx *simCtx) |
Perform particle swarm initialization, particle-grid interaction, and related operations. | |
Header file for Particle Motion and migration related functions.
Header file for Particle Swarm management functions.
This file contains declarations of functions responsible for moving and migrating particle swarms within a simulation using PETSc's DMSwarm.
This file contains declarations of functions responsible for creating, managing, initializing, migrating, and printing particle swarms within a simulation using PETSc's DMSwarm.
Definition in file ParticleSwarm.h.
PetscErrorCode CreateParticleSwarm | ( | UserCtx * | user, |
PetscInt | numParticles, | ||
PetscInt * | particlesPerProcess, | ||
BoundingBox * | bboxlist | ||
) |
Creates and initializes a Particle Swarm.
This function sets up a DMSwarm within the provided UserCtx structure, initializes particle fields, and distributes particles across MPI processes. It ensures that the number of particles is evenly divided among the available MPI ranks. If the total number of particles isn't divisible by the number of processes, the remainder is distributed to the first few ranks.
Additionally, it now takes a 'bboxlist' array as an input parameter and passes it on to AssignInitialProperties(), enabling particle initialization at the midpoint of each rank's bounding box if ParticleInitialization is set to 0.
[in,out] | user | Pointer to the UserCtx structure containing the simulation context. |
[in] | numParticles | Total number of particles to create across all MPI processes. |
[in] | bboxlist | Pointer to an array of BoundingBox structures, one per rank. |
numParticles
is a positive integer.control.dat
file should contain necessary PETSc options.The bboxlist
array should be properly populated before calling this function.
This function sets up a DMSwarm within the provided UserCtx structure, initializes particle fields, and distributes particles across MPI processes. It ensures that the number of particles is evenly divided among the available MPI ranks. If the total number of particles isn't divisible by the number of processes, the remainder is distributed to the first few ranks.
[in,out] | user | Pointer to the UserCtx structure containing the simulation context. |
[in] | numParticles | Total number of particles to create across all MPI processes. |
[in] | bboxlist | Pointer to an array of BoundingBox structures, one per rank. |
[in] | particlesPerProcess |
numParticles
is a positive integer.control.dat
file should contain necessary PETSc options.bboxlist
array should be properly populated before calling this function. Definition at line 764 of file ParticleSwarm.c.
PetscErrorCode InitializeSwarm | ( | UserCtx * | user | ) |
Initializes the DMSwarm object within the UserCtx structure.
This function creates the DMSwarm, sets its type and dimension, and configures basic swarm properties.
[in,out] | user | Pointer to the UserCtx structure containing simulation context. |
Definition at line 15 of file ParticleSwarm.c.
PetscErrorCode RegisterSwarmField | ( | DM | swarm, |
const char * | fieldName, | ||
PetscInt | fieldDim, | ||
PetscDataType | dtype | ||
) |
Registers a swarm field without finalizing registration.
This function calls DMSwarmRegisterPetscDatatypeField for the given field, but does not finalize the registration. The finalization is deferred until all fields have been registered.
swarm | [in] The DMSwarm object. |
fieldName | [in] Name of the field to register. |
fieldDim | [in] Dimension of the field (1 for scalar, 3 for vector, etc.). |
dtype | [in] The datatype of the swarm field being registered. |
Definition at line 41 of file ParticleSwarm.c.
PetscErrorCode RegisterParticleFields | ( | DM | swarm | ) |
Registers necessary particle fields within the DMSwarm.
This function registers fields such as position, velocity, CellID, and weight for each particle.
[in,out] | swarm | The DMSwarm object managing the particle swarm. |
Definition at line 64 of file ParticleSwarm.c.
PetscErrorCode InitializeRandomGenerators | ( | UserCtx * | user, |
PetscRandom * | randx, | ||
PetscRandom * | randy, | ||
PetscRandom * | randz | ||
) |
Initializes random number generators for assigning particle properties.
This function creates and configures separate PETSc random number generators for the x, y, and z coordinates.
[in,out] | user | Pointer to the UserCtx structure containing simulation context. |
[out] | randx | Pointer to store the RNG for the x-coordinate. |
[out] | randy | Pointer to store the RNG for the y-coordinate. |
[out] | randz | Pointer to store the RNG for the z-coordinate. |
Definition at line 112 of file ParticleSwarm.c.
PetscErrorCode InitializeLogicalSpaceRNGs | ( | PetscRandom * | rand_logic_i, |
PetscRandom * | rand_logic_j, | ||
PetscRandom * | rand_logic_k | ||
) |
Initializes random number generators for logical space operations [0.0, 1.0).
This function creates and configures three separate PETSc random number generators, one for each logical dimension (i, j, k or xi, eta, zeta equivalent). Each RNG is configured to produce uniformly distributed real numbers in the interval [0.0, 1.0). These are typically used for selecting owned cells or generating intra-cell logical coordinates.
[out] | rand_logic_i | Pointer to store the RNG for the i-logical dimension. |
[out] | rand_logic_j | Pointer to store the RNG for the j-logical dimension. |
[out] | rand_logic_k | Pointer to store the RNG for the k-logical dimension. |
Definition at line 158 of file ParticleSwarm.c.
PetscErrorCode AssignInitialPropertiesToSwarm | ( | UserCtx * | user, |
PetscInt | particlesPerProcess, | ||
PetscRandom * | rand_phys_x, | ||
PetscRandom * | rand_phys_y, | ||
PetscRandom * | rand_phys_z, | ||
PetscRandom * | rand_logic_i, | ||
PetscRandom * | rand_logic_j, | ||
PetscRandom * | rand_logic_k, | ||
BoundingBox * | bboxlist | ||
) |
Initializes all particle properties in the swarm.
This function orchestrates the initialization of particle properties. It first determines the inlet face if surface initialization (Mode 0) is selected by parsing "bcs.dat". Then, it initializes basic particle properties (physical position, Particle ID, and placeholder Cell IDs) by calling InitializeParticleBasicProperties
. This call uses the provided rand_logic_i/j/k
RNGs, which must be pre-initialized for [0,1). The rand_phys_x/y/z
RNGs (physically bounded) are passed but may not be used by InitializeParticleBasicProperties
for position setting if all initialization paths use logical-to-physical mapping. Finally, it calls helper functions to initialize other registered swarm fields like "velocity", "weight", and "P" (pressure) to default values.
[in,out] | user | Pointer to the UserCtx structure. |
[in] | particlesPerProcess | Number of particles assigned to this MPI process. |
[in] | rand_phys_x | RNG for physical x-coordinates (from InitializeRandomGenerators ). |
[in] | rand_phys_y | RNG for physical y-coordinates (from InitializeRandomGenerators ). |
[in] | rand_phys_z | RNG for physical z-coordinates (from InitializeRandomGenerators ). |
[in] | rand_logic_i | RNG for i-logical dimension tasks [0,1) (from InitializeLogicalSpaceRNGs ). |
[in] | rand_logic_j | RNG for j-logical dimension tasks [0,1) (from InitializeLogicalSpaceRNGs ). |
[in] | rand_logic_k | RNG for k-logical dimension tasks [0,1) (from InitializeLogicalSpaceRNGs ). |
[in] | bboxlist | Array of BoundingBox structures (potentially unused by IPBP). |
This function orchestrates the initialization of particle properties. It first determines the inlet face if surface initialization (Mode 0) is selected by parsing "bcs.dat". Then, it initializes basic particle properties (physical position, Particle ID, and placeholder Cell IDs) by calling InitializeParticleBasicProperties
. This call uses the provided rand_logic_i/j/k
RNGs, which must be pre-initialized for [0,1). The rand_phys_x/y/z
RNGs (physically bounded) are passed but may not be used by InitializeParticleBasicProperties
for position setting if all initialization paths use logical-to-physical mapping. Finally, it calls helper functions to initialize other registered swarm fields like "velocity", "weight", and "Psi" (scalar) to default values.
[in,out] | user | Pointer to the UserCtx structure. |
[in] | particlesPerProcess | Number of particles assigned to this MPI process. |
[in] | rand_phys_x | RNG for physical x-coordinates (from InitializeRandomGenerators ). |
[in] | rand_phys_y | RNG for physical y-coordinates (from InitializeRandomGenerators ). |
[in] | rand_phys_z | RNG for physical z-coordinates (from InitializeRandomGenerators ). |
[in] | rand_logic_i | RNG for i-logical dimension tasks [0,1) (from InitializeLogicalSpaceRNGs ). |
[in] | rand_logic_j | RNG for j-logical dimension tasks [0,1) (from InitializeLogicalSpaceRNGs ). |
[in] | rand_logic_k | RNG for k-logical dimension tasks [0,1) (from InitializeLogicalSpaceRNGs ). |
[in] | bboxlist | Array of BoundingBox structures (potentially unused by IPBP). |
Definition at line 606 of file ParticleSwarm.c.
PetscErrorCode DistributeParticles | ( | PetscInt | numParticles, |
PetscMPIInt | rank, | ||
PetscMPIInt | size, | ||
PetscInt * | particlesPerProcess, | ||
PetscInt * | remainder | ||
) |
Distributes particles evenly across MPI processes, handling any remainders.
This function calculates the number of particles each MPI process should handle, distributing the remainder particles to the first few ranks if necessary.
[in] | numParticles | Total number of particles to create across all MPI processes. |
[in] | rank | MPI rank of the current process. |
[in] | size | Total number of MPI processes. |
[out] | particlesPerProcess | Number of particles assigned to the current MPI process. |
[out] | remainder | Remainder particles when dividing numParticles by size. |
Definition at line 689 of file ParticleSwarm.c.
PetscErrorCode FinalizeSwarmSetup | ( | PetscRandom * | randx, |
PetscRandom * | randy, | ||
PetscRandom * | randz, | ||
PetscRandom * | rand_logic_i, | ||
PetscRandom * | rand_logic_j, | ||
PetscRandom * | rand_logic_k | ||
) |
Finalizes the swarm setup by destroying random generators and logging completion.
This function cleans up resources by destroying random number generators and LOG_ALLOWs the completion of swarm setup.
[in] | randx | Random number generator for the x-coordinate. |
[in] | randy | Random number generator for the y-coordinate. |
[in] | randz | Random number generator for the z-coordinate. |
[in] | rand_logic_i | Random number generator for the xi-coordinate. |
[in] | rand_logic_j | Random number generator for the eta-coordinate. |
[in] | rand_logic_k | Random number generator for the zeta-coordinate. |
Definition at line 720 of file ParticleSwarm.c.
PetscErrorCode UnpackSwarmFields | ( | PetscInt | i, |
const PetscInt64 * | PIDs, | ||
const PetscReal * | weights, | ||
const PetscReal * | positions, | ||
const PetscInt * | cellIndices, | ||
PetscReal * | velocities, | ||
PetscInt * | LocStatus, | ||
Particle * | particle | ||
) |
Initializes a Particle struct with data from DMSwarm fields.
This helper function populates a Particle structure using data retrieved from DMSwarm fields.
[in] | i | Index of the particle in the DMSwarm. |
[in] | PIDs | Pointer to the array of particle IDs. |
[in] | weights | Pointer to the array of particle weights. |
[in] | positions | Pointer to the array of particle positions. |
[in] | cellIndices | Pointer to the array of particle cell indices. |
[in] | velocities | Pointer to the array of particle velocities. |
[in] | LocStatus | Pointer to the array of cell location status indicators. |
[out] | particle | Pointer to the Particle struct to initialize. |
0
on success, non-zero on failure. Definition at line 837 of file ParticleSwarm.c.
PetscErrorCode UpdateSwarmFields | ( | PetscInt | i, |
const Particle * | particle, | ||
PetscReal * | weights, | ||
PetscInt * | cellIndices, | ||
PetscInt * | status_field | ||
) |
Updates DMSwarm fields with data from a Particle struct.
This helper function writes back the modified Particle data to the corresponding DMSwarm fields.
[in] | i | Index of the particle in the DMSwarm. |
[in] | particle | Pointer to the Particle struct containing updated data. |
[in,out] | weights | Pointer to the array of particle weights. |
[in,out] | cellIndices | Pointer to the array of particle cell indices. |
[in,out] | LocStatus | Pointer to the array of cell location status indicators. |
0
on success, non-zero on failure. Definition at line 909 of file ParticleSwarm.c.
PetscBool IsParticleInsideBoundingBox | ( | const BoundingBox * | bbox, |
const Particle * | particle | ||
) |
Checks if a particle's location is within a specified bounding box.
This function determines whether the given particle's location lies inside the provided bounding box. It performs an axis-aligned bounding box (AABB) check by comparing the particle's coordinates to the minimum and maximum coordinates of the bounding box in each dimension (x, y, z).
Logging statements are included to provide detailed information about the function's execution.
[in] | bbox | Pointer to the BoundingBox structure containing minimum and maximum coordinates. |
[in] | particle | Pointer to the Particle structure containing the particle's location and identifier. |
PETSC_TRUE
if the particle is inside the bounding box, PETSC_FALSE
otherwise.bbox
and particle
pointers are valid and non-NULL.This function determines whether the given particle's location lies inside the provided bounding box. It performs an axis-aligned bounding box (AABB) check by comparing the particle's coordinates to the minimum and maximum coordinates of the bounding box in each dimension (x, y, z).
logging statements are included to provide detailed information about the function's execution.
[in] | bbox | Pointer to the BoundingBox structure containing minimum and maximum coordinates. |
[in] | particle | Pointer to the Particle structure containing the particle's location and identifier. |
PETSC_TRUE
if the particle is inside the bounding box, PETSC_FALSE
otherwise.bbox
and particle
pointers are valid and non-NULL.LOG_ALLOW_SCOPE
variable is used to distinguish between GLOBAL
and LOCAL
LOG_ALLOW outputs.Definition at line 964 of file ParticleSwarm.c.
PetscErrorCode UpdateParticleWeights | ( | PetscReal * | d, |
Particle * | particle | ||
) |
Updates a particle's interpolation weights based on distances to cell faces.
This function computes interpolation weights using distances to the six cell faces (d
) and updates the weight
field of the provided particle.
[in] | d | Pointer to an array of distances to the six cell faces. |
[out] | particle | Pointer to the Particle structure whose weights are to be updated. |
Definition at line 1015 of file ParticleSwarm.c.
PetscErrorCode PrepareLoadedSwarmForRelocation | ( | UserCtx * | user | ) |
Resets the location-dependent state of a loaded swarm to force relocation.
This function is a critical part of the simulation restart procedure. It must be called immediately after ReadAllSwarmFields
has populated a swarm from restart files. Its purpose is to invalidate the "location" state of the loaded particles, ensuring that the LocateAllParticlesInGrid_TEST
orchestrator performs a fresh, comprehensive search for every particle based on its loaded position.
It does this by performing two actions on every locally-owned particle:
DMSwarm_CellID
field to a sentinel value of (-1, -1, -1)
. This invalidates any cell index that might have been loaded or defaulted to 0.DMSwarm_location_status
field to NEEDS_LOCATION
.This guarantees that the location logic will not mistakenly use a stale cell index from a previous run and will instead use the robust "Guess -> Verify" strategy appropriate for particles with unknown locations.
[in,out] | user | Pointer to the UserCtx structure which contains the DMSwarm object that has just been loaded with data from restart files. |
Definition at line 1078 of file ParticleSwarm.c.
PetscErrorCode InitializeParticleSwarm | ( | SimCtx * | simCtx | ) |
Perform particle swarm initialization, particle-grid interaction, and related operations.
This function handles the following tasks:
[in,out] | user | Pointer to the UserCtx structure containing grid and particle swarm information. |
[in] | np | Number of particles to initialize in the swarm. |
[in] | bboxlist | Pointer to an array of BoundingBox structures, one per MPI rank. |
np
(number of particles) is positive.bboxlist
array must be correctly computed and passed in before calling this function.Definition at line 1174 of file ParticleSwarm.c.