PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
|
#include "ParticleMotion.h"
Go to the source code of this file.
Macros | |
#define | ERROR_MSG_BUFFER_SIZE 256 |
#define | __FUNCT__ "UpdateAllParticlePositions" |
Removes particles that have been definitively flagged as LOST by the location algorithm. | |
#define | __FUNCT__ "CheckAndRemoveLostParticles" |
Removes particles that have been definitively flagged as LOST by the location algorithm. | |
#define | __FUNCT__ "LocateAllParticlesInGrid_TEST" |
Removes particles that have been definitively flagged as LOST by the location algorithm. | |
Functions | |
PetscErrorCode | UpdateParticlePosition (UserCtx *user, Cmpnts *position, const Cmpnts *velocity) |
Updates a particle's position based on its velocity and the timestep dt (stored in user->dt). | |
PetscErrorCode | UpdateAllParticlePositions (UserCtx *user) |
Loops over all local particles in the DMSwarm, updating their positions based on velocity and the global timestep user->dt. | |
static PetscBool | IsParticleInBox (const BoundingBox *bbox, const Cmpnts *pos) |
Checks if a particle position is within the bounds of a given bounding box. | |
PetscErrorCode | CheckAndRemoveOutOfBoundsParticles (UserCtx *user, PetscInt *removedCountLocal, PetscInt *removedCountGlobal, const BoundingBox *bboxlist) |
Checks for particles outside the global physical domain and removes them. | |
PetscErrorCode | CheckAndRemoveLostParticles (UserCtx *user, PetscInt *removedCountLocal, PetscInt *removedCountGlobal) |
Removes particles that have been definitively flagged as LOST by the location algorithm. | |
PetscErrorCode | SetMigrationRanks (UserCtx *user, const MigrationInfo *migrationList, PetscInt migrationCount) |
Sets the target rank field (DMSwarmPICField_rank) for particles scheduled for migration. | |
PetscErrorCode | PerformMigration (UserCtx *user) |
Performs particle migration based on the pre-populated DMSwarmPICField_rank field. | |
PetscErrorCode | CalculateParticleCountPerCell (UserCtx *user) |
Counts particles in each cell of the DMDA 'da' and stores the result in user->ParticleCount. | |
PetscErrorCode | ResizeSwarmGlobally (DM swarm, PetscInt N_target) |
PetscErrorCode | PreCheckAndResizeSwarm (UserCtx *user, PetscInt ti, const char *ext) |
Checks particle count from a saved file and resizes the swarm globally. | |
PetscErrorCode | ReinitializeParticlesOnInletSurface (UserCtx *user, PetscReal currentTime, PetscInt step) |
Re-initializes the positions of particles currently on this rank if this rank owns part of the designated inlet surface. | |
static int | compare_PetscInt64 (const void *a, const void *b) |
PetscErrorCode | GetLocalPIDSnapshot (const PetscInt64 pid_field[], PetscInt n_local, PetscInt64 **pids_snapshot_out) |
Creates a sorted snapshot of all Particle IDs (PIDs) from a raw data array. | |
PetscErrorCode | AddToMigrationList (MigrationInfo **migration_list_p, PetscInt *capacity_p, PetscInt *count_p, PetscInt particle_local_idx, PetscMPIInt destination_rank) |
Safely adds a new migration task to a dynamically sized list. | |
PetscErrorCode | FlagNewcomersForLocation (DM swarm, PetscInt n_local_before, const PetscInt64 pids_before[]) |
Identifies newly arrived particles after migration and flags them for a location search. | |
PetscErrorCode | GuessParticleOwnerWithBBox (UserCtx *user, const Particle *particle, const BoundingBox *bboxlist, PetscMPIInt *guess_rank_out) |
Provides a fast, heuristic-based guess for a particle's owner rank using bounding boxes. | |
PetscErrorCode | LocateAllParticlesInGrid_TEST (UserCtx *user, BoundingBox *bboxlist) |
Orchestrates the complete particle location and migration process for one timestep. | |
PetscErrorCode | ResetAllParticleStatuses (UserCtx *user) |
This function is designed to be called at the end of a full timestep, after all particle-based calculations are complete. | |
#define ERROR_MSG_BUFFER_SIZE 256 |
Definition at line 7 of file ParticleMotion.c.
#define __FUNCT__ "UpdateAllParticlePositions" |
Removes particles that have been definitively flagged as LOST by the location algorithm.
This function is the designated cleanup utility for particles that have exited the physical domain or could not be located for any other reason. It should be called after a particle location and migration phase is complete.
It iterates through all locally owned particles and checks their DMSwarm_location_status
field. If a particle's status is LOST
, it is permanently removed from the simulation using DMSwarmRemovePointAtIndex
.
The function uses a robust "Restore-Remove-Reacquire" pattern for modifying the swarm. When a particle is removed, all pointers to swarm data are restored, the removal operation is performed, and then the pointers are reacquired if the loop needs to continue.
[in,out] | user | Pointer to the UserCtx structure containing the swarm. |
[out] | removedCountLocal | Pointer to store the number of particles removed on THIS rank. |
[out] | removedCountGlobal | Pointer to store the total number of particles removed ACROSS ALL ranks. |
Definition at line 34 of file ParticleMotion.c.
#define __FUNCT__ "CheckAndRemoveLostParticles" |
Removes particles that have been definitively flagged as LOST by the location algorithm.
This function is the designated cleanup utility for particles that have exited the physical domain or could not be located for any other reason. It should be called after a particle location and migration phase is complete.
It iterates through all locally owned particles and checks their DMSwarm_location_status
field. If a particle's status is LOST
, it is permanently removed from the simulation using DMSwarmRemovePointAtIndex
.
The function uses a robust "Restore-Remove-Reacquire" pattern for modifying the swarm. When a particle is removed, all pointers to swarm data are restored, the removal operation is performed, and then the pointers are reacquired if the loop needs to continue.
[in,out] | user | Pointer to the UserCtx structure containing the swarm. |
[out] | removedCountLocal | Pointer to store the number of particles removed on THIS rank. |
[out] | removedCountGlobal | Pointer to store the total number of particles removed ACROSS ALL ranks. |
Definition at line 34 of file ParticleMotion.c.
#define __FUNCT__ "LocateAllParticlesInGrid_TEST" |
Removes particles that have been definitively flagged as LOST by the location algorithm.
This function is the designated cleanup utility for particles that have exited the physical domain or could not be located for any other reason. It should be called after a particle location and migration phase is complete.
It iterates through all locally owned particles and checks their DMSwarm_location_status
field. If a particle's status is LOST
, it is permanently removed from the simulation using DMSwarmRemovePointAtIndex
.
The function uses a robust "Restore-Remove-Reacquire" pattern for modifying the swarm. When a particle is removed, all pointers to swarm data are restored, the removal operation is performed, and then the pointers are reacquired if the loop needs to continue.
[in,out] | user | Pointer to the UserCtx structure containing the swarm. |
[out] | removedCountLocal | Pointer to store the number of particles removed on THIS rank. |
[out] | removedCountGlobal | Pointer to store the total number of particles removed ACROSS ALL ranks. |
Definition at line 34 of file ParticleMotion.c.
PetscErrorCode UpdateParticlePosition | ( | UserCtx * | user, |
Cmpnts * | position, | ||
const Cmpnts * | velocity | ||
) |
Updates a particle's position based on its velocity and the timestep dt (stored in user->dt).
[in] | user | Pointer to your UserCtx (must contain user->dt). |
[in,out] | position | Pointer to the particle's current position (Cmpnts). |
[in] | velocity | Pointer to the particle's velocity (Cmpnts). |
Definition at line 19 of file ParticleMotion.c.
PetscErrorCode UpdateAllParticlePositions | ( | UserCtx * | user | ) |
Loops over all local particles in the DMSwarm, updating their positions based on velocity and the global timestep user->dt.
[in,out] | user | Pointer to UserCtx (must contain dt). |
Definition at line 42 of file ParticleMotion.c.
|
inlinestatic |
Checks if a particle position is within the bounds of a given bounding box.
bbox | Pointer to the BoundingBox structure. |
pos | Pointer to the particle's position (Cmpnts). |
Definition at line 114 of file ParticleMotion.c.
PetscErrorCode CheckAndRemoveOutOfBoundsParticles | ( | UserCtx * | user, |
PetscInt * | removedCountLocal, | ||
PetscInt * | removedCountGlobal, | ||
const BoundingBox * | bboxlist | ||
) |
Checks for particles outside the global physical domain and removes them.
Checks for particles outside the physical domain boundaries and removes them using DMSwarmRemovePointAtIndex.
This function iterates through all particles local to the current MPI rank. It checks if a particle's position is inside ANY of the bounding boxes that define the subdomains of the MPI ranks. The list of all bounding boxes must be available on all ranks.
If a particle is not found within any of the provided bounding boxes, it is considered to be "out of bounds" and is permanently removed from the simulation using DMSwarmRemovePointAtIndex().
This function uses a robust "Restore-Remove-Reacquire" pattern for modifying the swarm. When a particle is removed, all pointers to swarm data are restored, the removal operation is performed, and then the pointers are reacquired if the loop needs to continue. This is the safest way to modify the swarm's structure during iteration.
LOST
status) and a corresponding cleanup function (e.g., CheckAndRemoveLostParticles
) are already in use. Using both can introduce complexity.[in,out] | user | Pointer to the UserCtx structure containing the swarm and MPI info. |
[out] | removedCountLocal | Pointer to store the number of particles removed on THIS rank. |
[out] | removedCountGlobal | Pointer to store the total number of particles removed ACROSS ALL ranks. |
[in] | bboxlist | An array of BoundingBox structures for ALL MPI ranks, indexed 0 to (size-1). This array must be up-to-date and available on all ranks. |
Checks for particles outside the global physical domain and removes them.
This function iterates through all particles local to the current MPI rank. It determines if a particle's physical position is outside of ALL subdomains owned by the MPI processes. To perform this check, it requires a list of the bounding boxes for every MPI rank's subdomain.
If a particle is not found within any of the subdomains, it is considered "out of bounds" and is permanently removed from the simulation using DMSwarmRemovePointAtIndex().
The function is carefully designed to handle modifications to the DMSwarm during iteration safely. It does this by:
LOST
status) and a corresponding cleanup function are already in use.[in,out] | user | Pointer to the UserCtx structure containing the swarm and MPI info. |
[out] | removedCountLocal | Pointer to an integer that will store the number of particles removed on THIS rank. |
[out] | removedCountGlobal | Pointer to an integer that will store the total number of particles removed ACROSS ALL ranks. |
[in] | bboxlist | An array of BoundingBox structures for ALL MPI ranks, indexed 0 to (size-1). This array must be up-to-date and available on every rank. |
Definition at line 291 of file ParticleMotion.c.
PetscErrorCode CheckAndRemoveLostParticles | ( | UserCtx * | user, |
PetscInt * | removedCountLocal, | ||
PetscInt * | removedCountGlobal | ||
) |
Removes particles that have been definitively flagged as LOST by the location algorithm.
This function is the designated cleanup utility for particles that have exited the physical domain or could not be located for any other reason. It should be called after a particle location and migration phase is complete.
It iterates through all locally owned particles and checks their DMSwarm_location_status
field. If a particle's status is LOST
, it is permanently removed from the simulation using DMSwarmRemovePointAtIndex
.
The function is carefully designed to handle modifications to the DMSwarm during iteration safely. It does this by:
[in,out] | user | Pointer to the UserCtx structure containing the swarm. |
[out] | removedCountLocal | Pointer to an integer that will store the number of particles removed on THIS rank. |
[out] | removedCountGlobal | Pointer to an integer that will store the total number of particles removed ACROSS ALL ranks. |
Definition at line 542 of file ParticleMotion.c.
PetscErrorCode SetMigrationRanks | ( | UserCtx * | user, |
const MigrationInfo * | migrationList, | ||
PetscInt | migrationCount | ||
) |
Sets the target rank field (DMSwarmPICField_rank) for particles scheduled for migration.
user | Pointer to UserCtx pa(contains swarm). |
migrationList | Array of MigrationInfo structs containing local indices and target ranks. |
migrationCount | Number of particles in the migrationList. |
Definition at line 648 of file ParticleMotion.c.
PetscErrorCode PerformMigration | ( | UserCtx * | user | ) |
Performs particle migration based on the pre-populated DMSwarmPICField_rank field.
Assumes SetMigrationRanks has already been called to mark particles with their target ranks. Calls DMSwarmMigrate to execute the communication and removal of un-migrated particles.
user | Pointer to the UserCtx structure containing the swarm. |
Definition at line 679 of file ParticleMotion.c.
PetscErrorCode ResizeSwarmGlobally | ( | DM | swarm, |
PetscInt | N_target | ||
) |
Definition at line 857 of file ParticleMotion.c.
PetscErrorCode PreCheckAndResizeSwarm | ( | UserCtx * | user, |
PetscInt | ti, | ||
const char * | ext | ||
) |
Checks particle count from a saved file and resizes the swarm globally.
Checks particle count in the reference file and resizes the swarm if needed.
This function uses a robust parallel pattern: only Rank 0 reads the reference position file to determine the total number of particles saved (N_file
). This count is then broadcast to all other ranks. Finally, each rank compares N_file with the current swarm size and participates in resizing if necessary.
[in,out] | user | Pointer to the UserCtx structure containing the DMSwarm. |
[in] | ti | Time index for constructing the file name. |
[in] | ext | File extension (e.g., "dat"). |
Definition at line 951 of file ParticleMotion.c.
PetscErrorCode ReinitializeParticlesOnInletSurface | ( | UserCtx * | user, |
PetscReal | currentTime, | ||
PetscInt | step | ||
) |
Re-initializes the positions of particles currently on this rank if this rank owns part of the designated inlet surface.
This function is intended for user->ParticleInitialization == 0
(Surface Initialization mode) and is typically called after an initial migration step (e.g., in PerformInitialSetup
). It ensures that all particles that should originate from the inlet surface and are now on the correct MPI rank are properly distributed across that rank's portion of the inlet.
user | Pointer to the UserCtx structure, containing simulation settings and grid information. |
currentTime | Current simulation time (used for logging). |
step | Current simulation step number (used for logging). |
Definition at line 1059 of file ParticleMotion.c.
|
static |
Definition at line 1186 of file ParticleMotion.c.
PetscErrorCode GetLocalPIDSnapshot | ( | const PetscInt64 | pid_field[], |
PetscInt | n_local, | ||
PetscInt64 ** | pids_snapshot_out | ||
) |
Creates a sorted snapshot of all Particle IDs (PIDs) from a raw data array.
This function is a crucial helper for the migration process. It captures the state of which particles are on the current MPI rank before migration occurs by taking a pointer to the swarm's raw PID data array. The resulting sorted array can then be used with an efficient binary search to quickly identify newcomer particles after migration.
This function does NOT call DMSwarmGetField/RestoreField. It is the caller's responsibility to acquire the pid_field
pointer before calling and restore it afterward.
[in] | pid_field | A read-only pointer to the raw array of PIDs for the local swarm. |
[in] | n_local | The number of particles currently on the local rank. |
[out] | pids_snapshot_out | A pointer to a PetscInt64* array. This function will allocate memory for this array, and the caller is responsible for freeing it with PetscFree() when it is no longer needed. |
Definition at line 1215 of file ParticleMotion.c.
PetscErrorCode AddToMigrationList | ( | MigrationInfo ** | migration_list_p, |
PetscInt * | capacity_p, | ||
PetscInt * | count_p, | ||
PetscInt | particle_local_idx, | ||
PetscMPIInt | destination_rank | ||
) |
Safely adds a new migration task to a dynamically sized list.
This utility function manages a dynamic array of MigrationInfo structs. It appends a new entry to the list and automatically doubles the array's capacity using PetscRealloc
if the current capacity is exceeded. This prevents buffer overflows and avoids the need to know the number of migrating particles in advance.
[in,out] | migration_list_p | A pointer to the MigrationInfo array pointer. The function will update this pointer if the array is reallocated. |
[in,out] | capacity_p | A pointer to an integer holding the current allocated capacity of the list (in number of elements). This will be updated upon reallocation. |
[in,out] | count_p | A pointer to an integer holding the current number of items in the list. This will be incremented by one. |
[in] | particle_local_idx | The local index (from 0 to nlocal-1) of the particle that needs to be migrated. |
[in] | destination_rank | The target MPI rank for the particle. |
Definition at line 1280 of file ParticleMotion.c.
PetscErrorCode FlagNewcomersForLocation | ( | DM | swarm, |
PetscInt | n_local_before, | ||
const PetscInt64 | pids_before[] | ||
) |
Identifies newly arrived particles after migration and flags them for a location search.
This function is a critical component of the iterative migration process managed by the main particle settlement orchestrator (e.g., SettleParticles
). After a DMSwarmMigrate
call, each rank's local particle list is a new mix of resident particles and newly received ones. This function's job is to efficiently identify these "newcomers" and set their DMSwarm_location_status
field to NEEDS_LOCATION
.
This ensures that in the subsequent pass of the migration do-while
loop, only the newly arrived particles are processed by the expensive location algorithm, preventing redundant work on particles that are already settled on the current rank.
The identification is done by comparing the PIDs of particles currently on the rank against a "snapshot" of PIDs taken before the migration occurred.
[in] | swarm | The DMSwarm object, which has just completed a migration. |
[in] | n_local_before | The number of particles that were on this rank before the migration was performed. |
[in] | pids_before | A pre-sorted array of the PIDs that were on this rank before the migration. This is used for fast lookups. |
pids_before
array is sorted in ascending order to enable the use of an efficient binary search. Definition at line 1355 of file ParticleMotion.c.
PetscErrorCode GuessParticleOwnerWithBBox | ( | UserCtx * | user, |
const Particle * | particle, | ||
const BoundingBox * | bboxlist, | ||
PetscMPIInt * | guess_rank_out | ||
) |
Provides a fast, heuristic-based guess for a particle's owner rank using bounding boxes.
This function is part of the "Guess and Verify" strategy, called only for "lost" particles. It attempts to find a candidate owner by checking which rank's bounding box contains the particle's physical position.
To optimize the search, it uses the particle's position relative to the local bounding box to intelligently check the most likely neighboring ranks first. For example, if a particle's x-coordinate is less than the local minimum x, it will check the -X neighbor first. If no owner is found in the immediate neighbors, it performs a full search of all other ranks as a fallback.
[in] | user | Pointer to the UserCtx, which must contain the pre-computed bbox (local), neighbors struct, and the global bboxlist . |
[in] | particle | A pointer to the particle whose owner is being guessed. |
[in] | bboxlist | An array of BoundingBox structures for ALL MPI ranks, indexed 0 to (size-1). This array must be up-to-date and available on all ranks. |
[out] | guess_rank_out | A pointer to a PetscMPIInt. Set to the candidate owner's rank if found, otherwise set to -1 (or MPI_PROC_NULL). |
Definition at line 1462 of file ParticleMotion.c.
PetscErrorCode LocateAllParticlesInGrid_TEST | ( | UserCtx * | user, |
BoundingBox * | bboxlist | ||
) |
Orchestrates the complete particle location and migration process for one timestep.
This function is the master orchestrator for ensuring every particle is on its correct MPI rank and has a valid host cell index. It is designed to be called once per timestep after particle positions have been updated.
The function uses a robust, iterative "Guess and Verify" strategy within a do-while loop to handle complex particle motion across processor boundaries, especially on curvilinear grids.
LocateParticleOrFindMigrationTarget
) that determines the particle's status: located locally, needs migration, or is lost.SetMigrationRanks
and PerformMigration
helpers.do-while
loop until a pass occurs where no particles migrate, ensuring the entire swarm is in a stable, consistent state.[in,out] | user | Pointer to the UserCtx, containing the swarm and all necessary domain topology information (bboxlist, RankCellInfoMap, etc.). |
[in] | bboxlist | An array of BoundingBox structures for ALL MPI ranks, indexed 0 to (size-1). This array must be up-to-date and available on all ranks. |
Definition at line 1598 of file ParticleMotion.c.
PetscErrorCode ResetAllParticleStatuses | ( | UserCtx * | user | ) |
This function is designed to be called at the end of a full timestep, after all particle-based calculations are complete.
It prepares the swarm for the next timestep by ensuring that after the next position update, every particle will be re-evaluated by the LocateAllParticlesInGrid orchestrator.
It iterates through all locally owned particles and sets their DMSwarm_location_status
field to NEEDS_LOCATION
.
[in,out] | user | Pointer to the UserCtx containing the swarm. |
Definition at line 1818 of file ParticleMotion.c.