|
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__ "GenerateGaussianNoise" |
| #define | __FUNCT__ "CalculateBrownianDisplacement" |
| #define | __FUNCT__ "UpdateParticlePosition" |
| #define | __FUNCT__ "UpdateAllParticlePositions" |
| #define | __FUNCT__ "CheckAndRemoveOutOfBoundsParticles" |
| #define | __FUNCT__ "CheckAndRemoveLostParticles" |
| #define | __FUNCT__ "SetMigrationRanks" |
| #define | __FUNCT__ "PerformMigration" |
| #define | __FUNCT__ "CalculateParticleCountPerCell" |
| #define | __FUNCT__ "ResizeSwarmGlobally" |
| #define | __FUNCT__ "PreCheckAndResizeSwarm" |
| #define | __FUNCT__ "ReinitializeParticlesOnInletSurface" |
| #define | __FUNCT__ "GetLocalPIDSnapshot" |
| #define | __FUNCT__ "AddToMigrationList" |
| #define | __FUNCT__ "FlagNewComersForLocation" |
| #define | __FUNCT__ "MigrateRestartParticlesUsingCellID" |
| #define | __FUNCT__ "GuessParticleOwnerWithBBox" |
| #define | __FUNCT__ "LocateAllParticlesInGrid" |
| #define | __FUNCT__ "ResetAllParticleStatuses" |
Functions | |
| PetscErrorCode | GenerateGaussianNoise (PetscRandom rnd, PetscReal *n1, PetscReal *n2) |
| Generates two independent standard normal random variables N(0,1) using the Box-Muller transform. | |
| PetscErrorCode | CalculateBrownianDisplacement (UserCtx *user, PetscReal diff_eff, Cmpnts *displacement) |
| Calculates the stochastic displacement vector (Brownian motion) for a single particle. | |
| PetscErrorCode | UpdateParticlePosition (UserCtx *user, Particle *particle) |
| 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 | MigrateRestartParticlesUsingCellID (UserCtx *user) |
| Fast-path migration for restart particles using preloaded Cell IDs. | |
| 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 (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__ "GenerateGaussianNoise" |
Definition at line 11 of file ParticleMotion.c.
| #define __FUNCT__ "CalculateBrownianDisplacement" |
Definition at line 11 of file ParticleMotion.c.
| #define __FUNCT__ "UpdateParticlePosition" |
Definition at line 11 of file ParticleMotion.c.
| #define __FUNCT__ "UpdateAllParticlePositions" |
Definition at line 11 of file ParticleMotion.c.
| #define __FUNCT__ "CheckAndRemoveOutOfBoundsParticles" |
Definition at line 11 of file ParticleMotion.c.
| #define __FUNCT__ "CheckAndRemoveLostParticles" |
Definition at line 11 of file ParticleMotion.c.
| #define __FUNCT__ "SetMigrationRanks" |
Definition at line 11 of file ParticleMotion.c.
| #define __FUNCT__ "PerformMigration" |
Definition at line 11 of file ParticleMotion.c.
| #define __FUNCT__ "CalculateParticleCountPerCell" |
Definition at line 11 of file ParticleMotion.c.
| #define __FUNCT__ "ResizeSwarmGlobally" |
Definition at line 11 of file ParticleMotion.c.
| #define __FUNCT__ "PreCheckAndResizeSwarm" |
Definition at line 11 of file ParticleMotion.c.
| #define __FUNCT__ "ReinitializeParticlesOnInletSurface" |
Definition at line 11 of file ParticleMotion.c.
| #define __FUNCT__ "GetLocalPIDSnapshot" |
Definition at line 11 of file ParticleMotion.c.
| #define __FUNCT__ "AddToMigrationList" |
Definition at line 11 of file ParticleMotion.c.
| #define __FUNCT__ "FlagNewComersForLocation" |
Definition at line 11 of file ParticleMotion.c.
| #define __FUNCT__ "MigrateRestartParticlesUsingCellID" |
Definition at line 11 of file ParticleMotion.c.
| #define __FUNCT__ "GuessParticleOwnerWithBBox" |
Definition at line 11 of file ParticleMotion.c.
| #define __FUNCT__ "LocateAllParticlesInGrid" |
Definition at line 11 of file ParticleMotion.c.
| #define __FUNCT__ "ResetAllParticleStatuses" |
Definition at line 11 of file ParticleMotion.c.
| PetscErrorCode GenerateGaussianNoise | ( | PetscRandom | rnd, |
| PetscReal * | n1, | ||
| PetscReal * | n2 | ||
| ) |
Generates two independent standard normal random variables N(0,1) using the Box-Muller transform.
| [in] | rnd | The PETSc Random context (Uniform [0,1)). |
| [out] | n1 | First Gaussian number. |
| [out] | n2 | Second Gaussian number. |
Definition at line 22 of file ParticleMotion.c.
| PetscErrorCode CalculateBrownianDisplacement | ( | UserCtx * | user, |
| PetscReal | diff_eff, | ||
| Cmpnts * | displacement | ||
| ) |
Calculates the stochastic displacement vector (Brownian motion) for a single particle.
Equation: dX_stoch = sqrt(2 * Gamma_eff * dt) * N(0,1)
| [in] | user | Pointer to UserCtx (access to dt and BrownianMotionRNG). |
| [in] | diff_eff | The effective diffusivity (Gamma + Gamma_t) at the particle's location. |
| [out] | displacement | Pointer to a Cmpnts struct to store the resulting (dx, dy, dz). |
Definition at line 68 of file ParticleMotion.c.
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] | particle | Pointer to the particle struct (contains, pos,vel,diffusivity etc). |
Definition at line 120 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 152 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 236 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 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 281 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 415 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 524 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 561 of file ParticleMotion.c.
| PetscErrorCode ResizeSwarmGlobally | ( | DM | swarm, |
| PetscInt | N_target | ||
| ) |
Definition at line 758 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 857 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 or 3 (Surface Initialization modes) 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 988 of file ParticleMotion.c.
|
static |
Definition at line 1197 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 1228 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 1300 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., LocateAllParticlesInGrid). 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 1382 of file ParticleMotion.c.
| PetscErrorCode MigrateRestartParticlesUsingCellID | ( | UserCtx * | user | ) |
Fast-path migration for restart particles using preloaded Cell IDs.
This function provides an optimized migration path specifically for particles loaded from restart files. Unlike the standard LocateAllParticlesInGrid() which performs expensive walking searches, this function leverages the fact that restart particles already have valid global Cell IDs loaded from disk.
How It Works:
FindOwnerOfCell(ci, cj, ck) to determine the correct rank.ACTIVE_AND_LOCATED status is preserved.SetMigrationRanks() and PerformMigration() infrastructure.| [in,out] | user | Pointer to UserCtx containing the swarm and RankCellInfoMap. The function updates particle status fields and performs migration. |
Definition at line 1493 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 1593 of file ParticleMotion.c.
| PetscErrorCode LocateAllParticlesInGrid | ( | 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 1737 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 1965 of file ParticleMotion.c.