Header file for Particle Swarm management functions. More...
#include <petsc.h>#include <petscdmswarm.h>#include <stdbool.h>#include <math.h>#include "variables.h"#include "particle_field_catalog.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) |
| Creates the particle swarm, registers its fields and sizes it for this rank's share. | |
| 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 | AssignInitialPropertiesToSwarm (UserCtx *user, PetscInt particlesPerProcess) |
| 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 | UnpackSwarmFields (PetscInt i, const PetscInt64 *PIDs, const PetscReal *weights, const PetscReal *positions, const PetscInt *cellIndices, PetscReal *velocities, PetscInt *LocStatus, PetscReal *diffusivity, Cmpnts *diffusivitygradient, PetscReal *psi, Particle *particle) |
| Initializes a Particle struct with data from DMSwarm fields. | |
| PetscErrorCode | UpdateSwarmFields (PetscInt i, const Particle *particle, PetscReal *positions, PetscReal *velocities, PetscReal *weights, PetscInt *cellIndices, PetscInt *status, PetscReal *diffusivity, Cmpnts *diffusivitygradient, PetscReal *psi) |
| Updates DMSwarm data arrays from a Particle struct. | |
| PetscErrorCode | UpdateParticleWeights (PetscReal *d, Particle *particle) |
| Updates a particle's interpolation weights based on distances to cell faces. | |
| PetscErrorCode | InitializeParticleSwarm (SimCtx *simCtx) |
| High-level particle initialization orchestrator for a simulation run. | |
| PetscErrorCode | ParticleInletsCreate (SimCtx *simCtx) |
| Read the inlets that inject particles from the options database. | |
| PetscErrorCode | ParticleInletsDestroy (SimCtx *simCtx) |
| Release what ParticleInletsCreate() and InjectParticlesAtInlets() allocated. | |
| PetscErrorCode | InjectParticlesAtInlets (UserCtx *user) |
| Add the particles that entered through each injecting inlet during this step. | |
Header file for Particle Swarm management functions.
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 | ||
| ) |
Creates the particle swarm, registers its fields and sizes it for this rank's share.
Volume and Surface seeding give each rank the particles its owned cells seed (by volume, or by area on the inlet face, the stratified split of the seeding table); PointSource divides the count evenly, the first ranks taking one more. Collective.
| [in,out] | user | Finest-level context; receives the swarm. |
| [in] | numParticles | Total number of particles to create across all MPI processes. |
| [out] | particlesPerProcess | Number of particles assigned to the local rank. |
Creates the particle swarm, registers its fields and sizes it for this rank's share.
Definition at line 725 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. |
Initializes the DMSwarm object within the UserCtx structure.
Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/ParticleSwarm.h.
Definition at line 16 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. |
Registers a swarm field without finalizing registration.
Definition at line 37 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. |
Registers necessary particle fields within the DMSwarm.
Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/ParticleSwarm.h.
Definition at line 60 of file ParticleSwarm.c.
| PetscErrorCode AssignInitialPropertiesToSwarm | ( | UserCtx * | user, |
| PetscInt | particlesPerProcess | ||
| ) |
Initializes all particle properties in the swarm.
Surface seeding requires an INLET face. The basic properties (physical position, particle ID and the seeded cell) are set by InitializeParticleBasicProperties: Volume and Surface seeding stratify the particles over the cells' volume or inlet area and key every placement draw on (seed, particle ID), so the population does not depend on the rank layout. Every field the particle catalog marks for default initialization (velocity, weight, Diffusivity, DiffusivityGradient and Psi) then takes its catalog default. Collective.
| [in,out] | user | Pointer to the UserCtx structure. |
| [in] | particlesPerProcess | Number of particles assigned to this MPI process. |
Initializes all particle properties in the swarm.
Definition at line 620 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. |
Distributes particles evenly across MPI processes, handling any remainders.
Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/ParticleSwarm.h.
Definition at line 671 of file ParticleSwarm.c.
| PetscErrorCode UnpackSwarmFields | ( | PetscInt | i, |
| const PetscInt64 * | PIDs, | ||
| const PetscReal * | weights, | ||
| const PetscReal * | positions, | ||
| const PetscInt * | cellIndices, | ||
| PetscReal * | velocities, | ||
| PetscInt * | LocStatus, | ||
| PetscReal * | diffusivity, | ||
| Cmpnts * | diffusivitygradient, | ||
| PetscReal * | psi, | ||
| Particle * | particle | ||
| ) |
Initializes a Particle struct with data from DMSwarm fields.
This helper function populates a Particle structure using data retrieved from DMSwarm fields. PIDs, positions, cellIndices and LocStatus are required. Pass NULL for an optional field the caller does not need; it then takes a default: weights (1, 1, 1), velocity and diffusivity gradient zero, diffusivity 1, psi 0. It logs nothing: the stage that uses a particle reports its values.
| [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. |
| [in] | diffusivity | Pointer to the array of particle diffusivities. |
| [in] | diffusivitygradient | Pointer to the array of particle diffusivity gradients. |
| [in] | psi | Pointer to the array of particle psi values. |
| [out] | particle | Pointer to the Particle struct to initialize. |
0 on success, non-zero on failure.Initializes a Particle struct with data from DMSwarm fields.
Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/ParticleSwarm.h.
Definition at line 804 of file ParticleSwarm.c.
| PetscErrorCode UpdateSwarmFields | ( | PetscInt | i, |
| const Particle * | particle, | ||
| PetscReal * | positions, | ||
| PetscReal * | velocities, | ||
| PetscReal * | weights, | ||
| PetscInt * | cellIndices, | ||
| PetscInt * | status, | ||
| PetscReal * | diffusivity, | ||
| Cmpnts * | diffusivitygradient, | ||
| PetscReal * | psi | ||
| ) |
Updates DMSwarm data arrays from a Particle struct.
This function writes data from the Particle struct back into the raw DMSwarm arrays. It is robust: if any array pointer is NULL, that specific field is skipped. This allows selective updating (e.g., update position but not velocity).
| [in] | i | Index of the particle in the local swarm arrays. |
| [in] | particle | Pointer to the Particle struct containing updated data. |
| [in,out] | positions | (Optional) Array of particle positions (size 3*n). |
| [in,out] | velocities | (Optional) Array of particle velocities (size 3*n). |
| [in,out] | weights | (Optional) Array of particle weights (size 3*n). |
| [in,out] | cellIndices | (Optional) Array of particle cell indices (size 3*n). |
| [in,out] | status | (Optional) Array of location status (size 1*n). |
| [in,out] | diffusivity | (Optional) Array of diffusivity values (size 1*n). |
| [in,out] | diffusivitygradient | (Optional) Array of diffusivity gradient values (size 3*n). |
| [in,out] | psi | (Optional) Array of scalar Psi values (size 1*n). |
Updates DMSwarm data arrays from a Particle struct.
Definition at line 887 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. |
Updates a particle's interpolation weights based on distances to cell faces.
Definition at line 960 of file ParticleSwarm.c.
| PetscErrorCode InitializeParticleSwarm | ( | SimCtx * | simCtx | ) |
High-level particle initialization orchestrator for a simulation run.
This routine drives end-to-end swarm setup from the top-level simulation context: creation/registration of particle fields, initial placement according to configured mode, initial localization on the Eulerian grid, and startup interpolation needed before entering the main run loop.
| [in,out] | simCtx | Master simulation context containing all blocks and run settings. |
High-level particle initialization orchestrator for a simulation run.
Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/ParticleSwarm.h.
Definition at line 1002 of file ParticleSwarm.c.
| PetscErrorCode ParticleInletsCreate | ( | SimCtx * | simCtx | ) |
Read the inlets that inject particles from the options database.
Reads -particle_inlet_count, and for each inlet n, -particle_inlet_<n>_face, -particle_inlet_<n>_particles_per_cell (the mean per cell of the grid, an expression in t) and the field plan under particle_inlet_<n>_fields.
| [in,out] | simCtx | Context receiving the inlets; none when the count is absent or zero. |
Read the inlets that inject particles from the options database.
Definition at line 1086 of file ParticleSwarm.c.
| PetscErrorCode ParticleInletsDestroy | ( | SimCtx * | simCtx | ) |
Release what ParticleInletsCreate() and InjectParticlesAtInlets() allocated.
| [in,out] | simCtx | Context holding the inlets. |
Release what ParticleInletsCreate() and InjectParticlesAtInlets() allocated.
Definition at line 1132 of file ParticleSwarm.c.
| PetscErrorCode InjectParticlesAtInlets | ( | UserCtx * | user | ) |
Add the particles that entered through each injecting inlet during this step.
Collective. A face cell receives on average n (u.A) dt particles, none where the flow leaves, so the entering fluid has number density n whatever its velocity profile. Counts, positions and identities come from keyed draws on the grid position, the step and the particle, so they carry no state and do not depend on the rank layout. A particle is placed just inside the face, by area and inward velocity, and its field values are set there, where it crossed. It then moves in by a uniform share of dt times the velocity the configured interpolation method gives at that point (the face cell's mean inward velocity where the cell has none) plus this step's Brownian kick from the face cell's diffusivity. The new particles are left NEEDS_LOCATION for the settlement that follows. Writes particle_injection.csv.
| [in,out] | user | Finest-level context of the block holding the swarm; reads lUcont, lCoor, the face-normal metric, lDiffusivity, and the interpolated velocity's sources (lUcat, or lUcont and the metrics for FaceFlux). |
Add the particles that entered through each injecting inlet during this step.
Definition at line 1327 of file ParticleSwarm.c.