|
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 <petscsys.h>#include <math.h>#include "variables.h"#include "logging.h"#include "walkingsearch.h"Go to the source code of this file.
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. | |
| PetscErrorCode | CheckAndRemoveOutOfBoundsParticles (UserCtx *user, PetscInt *removedCountLocal, PetscInt *removedCountGlobal, const BoundingBox *bboxlist) |
| Checks for particles outside the physical domain boundaries and removes them using DMSwarmRemovePointAtIndex. | |
| PetscErrorCode | CheckAndRemoveLostParticles (UserCtx *user, PetscInt *removedCountLocal, PetscInt *removedCountGlobal) |
| Removes particles that have been definitively flagged as LOST by the location algorithm. | |
| PetscErrorCode | DefineBasicMigrationPattern (UserCtx *user) |
| Defines the basic migration pattern for particles within the swarm. | |
| PetscErrorCode | PerformBasicMigration (UserCtx *user) |
| Performs the basic migration of particles based on the defined migration pattern. | |
| PetscErrorCode | IdentifyMigratingParticles (UserCtx *user, const BoundingBox *bboxlist, MigrationInfo **migrationList, PetscInt *migrationCount, PetscInt *listCapacity) |
| Identifies particles leaving the local bounding box and finds their target neighbor rank. | |
| PetscErrorCode | SetMigrationRanks (UserCtx *user, const MigrationInfo *migrationList, PetscInt migrationCount) |
| Writes migration destinations into the DMSwarm rank field for marked particles. | |
| 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) |
| Resizes a swarm collectively to a target global particle count. | |
| PetscErrorCode | PreCheckAndResizeSwarm (UserCtx *user, PetscInt ti, const char *ext) |
| Checks particle count in the reference file and resizes the swarm if needed. | |
| PetscErrorCode | PerformSingleParticleMigrationCycle (UserCtx *user, const BoundingBox *bboxlist, MigrationInfo **migrationList_p, PetscInt *migrationCount_p, PetscInt *migrationListCapacity_p, PetscReal currentTime, PetscInt step, const char *migrationCycleName, PetscInt *globalMigrationCount_out) |
| Performs one full cycle of particle migration: identify, set ranks, and migrate. | |
| 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. | |
| 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 | LocateAllParticlesInGrid (UserCtx *user, BoundingBox *bboxlist) |
| Orchestrates the complete particle location and migration process for one timestep. | |
| PetscErrorCode | ResetAllParticleStatuses (UserCtx *user) |
Marks all local particles as NEEDS_LOCATION for the next settlement pass. | |
Header file for Particle Motion and migration related functions.
This file contains declarations of functions responsible for moving and migrating particle swarms within a simulation using PETSc's DMSwarm.
Definition in file ParticleMotion.h.
| 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. |
Generates two independent standard normal random variables N(0,1) using the Box-Muller transform.
Local to this translation unit.
Definition at line 16 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). |
Calculates the stochastic displacement vector (Brownian motion) for a single particle.
Local to this translation unit.
Definition at line 56 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). |
Updates a particle's position based on its velocity and the timestep dt (stored in user->dt).
Local to this translation unit.
Definition at line 104 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). |
Loops over all local particles in the DMSwarm, updating their positions based on velocity and the global timestep user->dt.
Local to this translation unit.
Definition at line 133 of file ParticleMotion.c.
| PetscErrorCode CheckAndRemoveOutOfBoundsParticles | ( | UserCtx * | user, |
| PetscInt * | removedCountLocal, | ||
| PetscInt * | removedCountGlobal, | ||
| const BoundingBox * | bboxlist | ||
| ) |
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 (x, y, or z) is outside the specified physical domain boundaries [xMin, xMax], [yMin, yMax], [zMin, zMax].
If a particle is found out of bounds, it is removed using DMSwarmRemovePointAtIndex. NOTE: Removing points changes the indices of subsequent points in the iteration. Therefore, it's crucial to iterate BACKWARDS or carefully manage indices after a removal. Iterating backwards is generally safer.
| user | Pointer to the UserCtx structure. | |
| [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 physical domain boundaries and removes them using DMSwarmRemovePointAtIndex.
Local to this translation unit.
Definition at line 227 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. It should be called after the LocateAllParticlesInGrid orchestrator has run and every particle's status has been definitively determined.
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.
This approach centralizes the removal logic, making the DMSwarm_location_status the single source of truth for a particle's validity, which is more robust than relying on secondary geometric checks (like bounding boxes).
| [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. |
Removes particles that have been definitively flagged as LOST by the location algorithm.
Local to this translation unit.
Definition at line 335 of file ParticleMotion.c.
| PetscErrorCode DefineBasicMigrationPattern | ( | UserCtx * | user | ) |
Defines the basic migration pattern for particles within the swarm.
This function establishes the migration pattern that dictates how particles move between different MPI ranks in the simulation. It initializes a migration list where each particle is assigned a target rank based on predefined conditions. The migration pattern can be customized to implement various migration behaviors.
| [in,out] | user | Pointer to the UserCtx structure containing simulation context. |
| PetscErrorCode PerformBasicMigration | ( | UserCtx * | user | ) |
Performs the basic migration of particles based on the defined migration pattern.
This function updates the positions of particles within the swarm by migrating them to target MPI ranks as specified in the migration list. It handles the migration process by setting the 'DMSwarm_rank' field for each particle and invokes the DMSwarm migration mechanism to relocate particles across MPI processes. After migration, it cleans up allocated resources and ensures synchronization across all MPI ranks.
| [in,out] | user | Pointer to the UserCtx structure containing simulation context. |
| PetscErrorCode IdentifyMigratingParticles | ( | UserCtx * | user, |
| const BoundingBox * | bboxlist, | ||
| MigrationInfo ** | migrationList, | ||
| PetscInt * | migrationCount, | ||
| PetscInt * | listCapacity | ||
| ) |
Identifies particles leaving the local bounding box and finds their target neighbor rank.
Iterates local particles, checks against local bounding box. If outside, checks the pre-computed immediate neighbors (user->neighbors) using the global bboxlist to see if the particle landed in one of them. Populates the migrationList. Does NOT handle particles leaving the global domain (assumes CheckAndRemove was called).
| user | Pointer to the UserCtx (contains local bbox and neighbors). |
| bboxlist | Array of BoundingBox structs for all ranks (for checking neighbor boxes). |
| migrationList | Pointer to an array of MigrationInfo structs (output, allocated/reallocated by this func). |
| migrationCount | Pointer to the number of particles marked for migration (output). |
| listCapacity | Pointer to the current allocated capacity of migrationList (in/out). |
| PetscErrorCode SetMigrationRanks | ( | UserCtx * | user, |
| const MigrationInfo * | migrationList, | ||
| PetscInt | migrationCount | ||
| ) |
Writes migration destinations into the DMSwarm rank field for marked particles.
This helper consumes the migration list produced by IdentifyMigratingParticles and updates each selected particle's destination rank so that a subsequent PerformMigration call can transfer ownership correctly.
| [in,out] | user | Context containing the swarm and migration rank field. |
| [in] | migrationList | Array of migration directives (local index + destination rank). |
| [in] | migrationCount | Number of valid entries in migrationList. |
Writes migration destinations into the DMSwarm rank field for marked particles.
Local to this translation unit.
Definition at line 439 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. |
Performs particle migration based on the pre-populated DMSwarmPICField_rank field.
Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/ParticleMotion.h.
Definition at line 472 of file ParticleMotion.c.
| PetscErrorCode CalculateParticleCountPerCell | ( | UserCtx * | user | ) |
Counts particles in each cell of the DMDA 'da' and stores the result in user->ParticleCount.
Assumes user->ParticleCount is a pre-allocated global vector associated with user->da and initialized to zero before calling this function (though it resets it internally). Assumes particle 'DMSwarm_CellID' field contains local cell indices.
| [in,out] | user | Pointer to the UserCtx structure containing da, swarm, and ParticleCount. |
Counts particles in each cell of the DMDA 'da' and stores the result in user->ParticleCount.
Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/logging.h.
Definition at line 504 of file ParticleMotion.c.
| PetscErrorCode ResizeSwarmGlobally | ( | DM | swarm, |
| PetscInt | N_target | ||
| ) |
Resizes a swarm collectively to a target global particle count.
If particles are removed, the current implementation trims from the global tail ordering. If particles are added, new entries are created according to PETSc DMSwarm resize semantics.
| [in,out] | swarm | Swarm object to resize. |
| [in] | N_target | Target global particle count. |
Resizes a swarm collectively to a target global particle count.
Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/ParticleMotion.h.
Definition at line 673 of file ParticleMotion.c.
| PetscErrorCode PreCheckAndResizeSwarm | ( | UserCtx * | user, |
| PetscInt | ti, | ||
| const char * | ext | ||
| ) |
Checks particle count in the reference file and resizes the swarm if needed.
Reads the specified field file (e.g., position) into a temporary Vec to determine the number of particles (N_file) represented in that file for the given timestep. Compares N_file with the current swarm size (N_current). If they differ, resizes the swarm globally (adds or removes particles) to match N_file. Removal assumes excess particles are the globally last ones.
| [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"). |
Checks particle count in the reference file and resizes the swarm if needed.
Local to this translation unit.
Definition at line 762 of file ParticleMotion.c.
| PetscErrorCode PerformSingleParticleMigrationCycle | ( | UserCtx * | user, |
| const BoundingBox * | bboxlist, | ||
| MigrationInfo ** | migrationList_p, | ||
| PetscInt * | migrationCount_p, | ||
| PetscInt * | migrationListCapacity_p, | ||
| PetscReal | currentTime, | ||
| PetscInt | step, | ||
| const char * | migrationCycleName, | ||
| PetscInt * | globalMigrationCount_out | ||
| ) |
Performs one full cycle of particle migration: identify, set ranks, and migrate.
This function encapsulates the three main steps of migrating particles between MPI ranks:
bboxlist).DMSwarmMigrate. It also calculates and logs the global number of particles migrated.| user | Pointer to the UserCtx structure. | |
| bboxlist | Array of BoundingBox structures defining the spatial domain of each MPI rank. | |
| migrationList_p | Pointer to a pointer for the MigrationInfo array. This array will be allocated/reallocated by IdentifyMigratingParticles if necessary. The caller is responsible for freeing this list eventually. | |
| migrationCount_p | Pointer to store the number of particles identified for migration on the local rank. This is reset to 0 after migration for the current cycle. | |
| migrationListCapacity_p | Pointer to store the current capacity of the migrationList_p array. | |
| currentTime | Current simulation time (used for logging). | |
| step | Current simulation step number (used for logging). | |
| migrationCycleName | A descriptive name for this migration cycle (e.g., "Preliminary Sort", "Main Loop") for logging purposes. | |
| [out] | globalMigrationCount_out | Pointer to store the total number of particles migrated across all MPI ranks during this cycle. |
| 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). |
Re-initializes the positions of particles currently on this rank if this rank owns part of the designated inlet surface.
Local to this translation unit.
Definition at line 883 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. |
Creates a sorted snapshot of all Particle IDs (PIDs) from a raw data array.
Local to this translation unit.
Definition at line 1097 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. |
Safely adds a new migration task to a dynamically sized list.
Local to this translation unit.
Definition at line 1152 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.Identifies newly arrived particles after migration and flags them for a location search.
Local to this translation unit.
Definition at line 1210 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. |
Fast-path migration for restart particles using preloaded Cell IDs.
Local to this translation unit.
Definition at line 1303 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. |
Orchestrates the complete particle location and migration process for one timestep.
Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/ParticleMotion.h.
Definition at line 1506 of file ParticleMotion.c.
| PetscErrorCode ResetAllParticleStatuses | ( | UserCtx * | user | ) |
Marks all local particles as NEEDS_LOCATION for the next settlement pass.
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. |
Marks all local particles as NEEDS_LOCATION for the next settlement pass.
Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/ParticleMotion.h.
Definition at line 1740 of file ParticleMotion.c.