PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
 
Loading...
Searching...
No Matches
ParticleSwarm.h
Go to the documentation of this file.
1/**
2 * @file ParticleSwarm.h
3 * @brief Header file for Particle Swarm management functions.
4 *
5 * This file contains declarations of functions responsible for creating, managing,
6 * initializing, migrating, and printing particle swarms within a simulation using PETSc's DMSwarm.
7 */
8
9#ifndef PARTICLE_SWARM_H
10#define PARTICLE_SWARM_H
11
12// Include necessary headers
13#include <petsc.h> // PETSc library header
14#include <petscdmswarm.h> // PETSc DMSwarm header
15#include <stdbool.h>
16#include <math.h>
17#include "variables.h" // Common type definitions
19#include "logging.h" // Logging macros and definitions
20#include "walkingsearch.h"
21#include "Metric.h"
22#include "io.h"
23// --------------------- Function Declarations ---------------------
24
25/**
26 * @brief Creates the particle swarm, registers its fields and sizes it for this rank's share.
27 *
28 * Volume and Surface seeding give each rank the particles its owned cells seed (by volume, or
29 * by area on the inlet face, the stratified split of the seeding table); PointSource divides
30 * the count evenly, the first ranks taking one more. Collective.
31 *
32 * @param[in,out] user Finest-level context; receives the swarm.
33 * @param[in] numParticles Total number of particles to create across all MPI processes.
34 * @param[out] particlesPerProcess Number of particles assigned to the local rank.
35 *
36 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
37 */
38PetscErrorCode CreateParticleSwarm(UserCtx *user, PetscInt numParticles, PetscInt *particlesPerProcess);
39
40/**
41 * @brief Initializes the DMSwarm object within the UserCtx structure.
42 *
43 * This function creates the DMSwarm, sets its type and dimension, and configures basic swarm properties.
44 *
45 * @param[in,out] user Pointer to the UserCtx structure containing simulation context.
46 *
47 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
48 */
49PetscErrorCode InitializeSwarm(UserCtx* user);
50
51/**
52 * @brief Registers a swarm field without finalizing registration.
53 *
54 * This function calls DMSwarmRegisterPetscDatatypeField for the given field,
55 * but does not finalize the registration. The finalization is deferred until
56 * all fields have been registered.
57 *
58 * @param swarm [in] The DMSwarm object.
59 * @param fieldName [in] Name of the field to register.
60 * @param fieldDim [in] Dimension of the field (1 for scalar, 3 for vector, etc.).
61 * @param dtype [in] The datatype of the swarm field being registered.
62 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
63 */
64PetscErrorCode RegisterSwarmField(DM swarm, const char *fieldName, PetscInt fieldDim, PetscDataType dtype);
65
66/**
67 * @brief Registers necessary particle fields within the DMSwarm.
68 *
69 * This function registers fields such as position, velocity, CellID, and weight for each particle.
70 *
71 * @param[in,out] swarm The DMSwarm object managing the particle swarm.
72 *
73 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
74 */
75PetscErrorCode RegisterParticleFields(DM swarm);
76
77/**
78 * @brief Initializes all particle properties in the swarm.
79 *
80 * Surface seeding requires an INLET face. The basic properties (physical position, particle
81 * ID and the seeded cell) are set by `InitializeParticleBasicProperties`: Volume and Surface
82 * seeding stratify the particles over the cells' volume or inlet area and key every placement
83 * draw on (seed, particle ID), so the population does not depend on the rank layout. Every
84 * field the particle catalog marks for default initialization (velocity, weight, Diffusivity,
85 * DiffusivityGradient and Psi) then takes its catalog default. Collective.
86 *
87 * @param[in,out] user Pointer to the `UserCtx` structure.
88 * @param[in] particlesPerProcess Number of particles assigned to this MPI process.
89 *
90 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
91 */
92PetscErrorCode AssignInitialPropertiesToSwarm(UserCtx* user,
93 PetscInt particlesPerProcess);
94
95/**
96 * @brief Distributes particles evenly across MPI processes, handling any remainders.
97 *
98 * This function calculates the number of particles each MPI process should handle,
99 * distributing the remainder particles to the first few ranks if necessary.
100 *
101 * @param[in] numParticles Total number of particles to create across all MPI processes.
102 * @param[in] rank MPI rank of the current process.
103 * @param[in] size Total number of MPI processes.
104 * @param[out] particlesPerProcess Number of particles assigned to the current MPI process.
105 * @param[out] remainder Remainder particles when dividing numParticles by size.
106 *
107 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
108 */
109PetscErrorCode DistributeParticles(PetscInt numParticles, PetscMPIInt rank, PetscMPIInt size, PetscInt* particlesPerProcess, PetscInt* remainder);
110
111/**
112 * @brief Initializes a Particle struct with data from DMSwarm fields.
113 *
114 * This helper function populates a Particle structure using data retrieved from DMSwarm fields.
115 * `PIDs`, `positions`, `cellIndices` and `LocStatus` are required. Pass NULL for an optional
116 * field the caller does not need; it then takes a default: weights (1, 1, 1), velocity and
117 * diffusivity gradient zero, diffusivity 1, psi 0. It logs nothing: the stage that uses a
118 * particle reports its values.
119 *
120 * @param[in] i Index of the particle in the DMSwarm.
121 * @param[in] PIDs Pointer to the array of particle IDs.
122 * @param[in] weights Pointer to the array of particle weights.
123 * @param[in] positions Pointer to the array of particle positions.
124 * @param[in] cellIndices Pointer to the array of particle cell indices.
125 * @param[in] velocities Pointer to the array of particle velocities.
126 * @param[in] LocStatus Pointer to the array of cell location status indicators.
127 * @param[in] diffusivity Pointer to the array of particle diffusivities.
128 * @param[in] diffusivitygradient Pointer to the array of particle diffusivity gradients.
129 * @param[in] psi Pointer to the array of particle psi values.
130 * @param[out] particle Pointer to the Particle struct to initialize.
131 *
132 * @return PetscErrorCode Returns `0` on success, non-zero on failure.
133 */
134PetscErrorCode UnpackSwarmFields(PetscInt i, const PetscInt64 *PIDs, const PetscReal *weights,
135 const PetscReal *positions, const PetscInt *cellIndices,
136 PetscReal *velocities,PetscInt *LocStatus,PetscReal *diffusivity, Cmpnts *diffusivitygradient, PetscReal *psi, Particle *particle);
137
138/**
139 * @brief Updates DMSwarm data arrays from a Particle struct.
140 *
141 * This function writes data from the `Particle` struct back into the raw DMSwarm arrays.
142 * It is robust: if any array pointer is NULL, that specific field is skipped.
143 * This allows selective updating (e.g., update position but not velocity).
144 *
145 * @param[in] i Index of the particle in the local swarm arrays.
146 * @param[in] particle Pointer to the Particle struct containing updated data.
147 * @param[in,out] positions (Optional) Array of particle positions (size 3*n).
148 * @param[in,out] velocities (Optional) Array of particle velocities (size 3*n).
149 * @param[in,out] weights (Optional) Array of particle weights (size 3*n).
150 * @param[in,out] cellIndices (Optional) Array of particle cell indices (size 3*n).
151 * @param[in,out] status (Optional) Array of location status (size 1*n).
152 * @param[in,out] diffusivity (Optional) Array of diffusivity values (size 1*n).
153 * @param[in,out] diffusivitygradient (Optional) Array of diffusivity gradient values (size 3*n).
154 * @param[in,out] psi (Optional) Array of scalar Psi values (size 1*n).
155 *
156 * @return PetscErrorCode Returns 0 on success.
157 */
158PetscErrorCode UpdateSwarmFields(PetscInt i, const Particle *particle,
159 PetscReal *positions,
160 PetscReal *velocities,
161 PetscReal *weights,
162 PetscInt *cellIndices,
163 PetscInt *status,
164 PetscReal *diffusivity,
165 Cmpnts *diffusivitygradient,
166 PetscReal *psi);
167
168/**
169 * @brief Updates a particle's interpolation weights based on distances to cell faces.
170 *
171 * This function computes interpolation weights using distances to the six
172 * cell faces (`d`) and updates the `weight` field of the provided particle.
173 *
174 * @param[in] d Pointer to an array of distances to the six cell faces.
175 * @param[out] particle Pointer to the Particle structure whose weights are to be updated.
176 *
177 * @return PetscErrorCode Returns 0 on success, or a non-zero error code on failure.
178 */
179PetscErrorCode UpdateParticleWeights(PetscReal *d, Particle *particle);
180
181/**
182 * @brief High-level particle initialization orchestrator for a simulation run.
183 *
184 * This routine drives end-to-end swarm setup from the top-level simulation context:
185 * creation/registration of particle fields, initial placement according to configured
186 * mode, initial localization on the Eulerian grid, and startup interpolation needed
187 * before entering the main run loop.
188 *
189 * @param[in,out] simCtx Master simulation context containing all blocks and run settings.
190 *
191 * @return PetscErrorCode Returns 0 on success, non-zero on failure.
192 */
193PetscErrorCode InitializeParticleSwarm(SimCtx *simCtx);
194
195/**
196 * @brief Read the inlets that inject particles from the options database.
197 * @details Reads `-particle_inlet_count`, and for each inlet `n`, `-particle_inlet_<n>_face`,
198 * `-particle_inlet_<n>_particles_per_cell` (the mean per cell of the grid, an expression in `t`) and the
199 * field plan under `particle_inlet_<n>_fields`.
200 * @param[in,out] simCtx Context receiving the inlets; none when the count is absent or zero.
201 * @return PetscErrorCode 0 on success.
202 */
203PetscErrorCode ParticleInletsCreate(SimCtx *simCtx);
204
205/**
206 * @brief Release what ParticleInletsCreate() and InjectParticlesAtInlets() allocated.
207 * @param[in,out] simCtx Context holding the inlets.
208 * @return PetscErrorCode 0 on success.
209 */
210PetscErrorCode ParticleInletsDestroy(SimCtx *simCtx);
211
212/**
213 * @brief Add the particles that entered through each injecting inlet during this step.
214 * @details Collective. A face cell receives on average `n (u.A) dt` particles, none where the flow
215 * leaves, so the entering fluid has number density `n` whatever its velocity profile.
216 * Counts, positions and identities come from keyed draws on the grid position, the step
217 * and the particle, so they carry no state and do not depend on the rank layout. A
218 * particle is placed just inside the face, by area and inward velocity, and its field
219 * values are set there, where it crossed. It then moves in by a uniform share of `dt`
220 * times the velocity the configured interpolation method gives at that point (the face
221 * cell's mean inward velocity where the cell has none) plus this step's Brownian kick
222 * from the face cell's diffusivity. The new particles are left `NEEDS_LOCATION` for the
223 * settlement that follows. Writes `particle_injection.csv`.
224 * @param[in,out] user Finest-level context of the block holding the swarm; reads `lUcont`, `lCoor`,
225 * the face-normal metric, `lDiffusivity`, and the interpolated velocity's
226 * sources (`lUcat`, or `lUcont` and the metrics for FaceFlux).
227 * @return PetscErrorCode 0 on success; an error on a face that is not an INLET, an invalid number
228 * density, or a particle-ID or particle-count limit being reached.
229 */
230PetscErrorCode InjectParticlesAtInlets(UserCtx *user);
231
232#endif // PARTICLE_SWARM_H
PetscErrorCode UpdateParticleWeights(PetscReal *d, Particle *particle)
Updates a particle's interpolation weights based on distances to cell faces.
PetscErrorCode CreateParticleSwarm(UserCtx *user, PetscInt numParticles, PetscInt *particlesPerProcess)
Creates the particle swarm, registers its fields and sizes it for this rank's share.
PetscErrorCode InjectParticlesAtInlets(UserCtx *user)
Add the particles that entered through each injecting inlet during this step.
PetscErrorCode DistributeParticles(PetscInt numParticles, PetscMPIInt rank, PetscMPIInt size, PetscInt *particlesPerProcess, PetscInt *remainder)
Distributes particles evenly across MPI processes, handling any remainders.
PetscErrorCode ParticleInletsDestroy(SimCtx *simCtx)
Release what ParticleInletsCreate() and InjectParticlesAtInlets() allocated.
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 ParticleInletsCreate(SimCtx *simCtx)
Read the inlets that inject particles from the options database.
PetscErrorCode InitializeSwarm(UserCtx *user)
Initializes the DMSwarm object within the UserCtx structure.
PetscErrorCode AssignInitialPropertiesToSwarm(UserCtx *user, PetscInt particlesPerProcess)
Initializes all particle properties in the swarm.
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 InitializeParticleSwarm(SimCtx *simCtx)
High-level particle initialization orchestrator for a simulation run.
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.
Public interface for data input/output routines.
Logging utilities and macros for PETSc-based applications.
Typed identities and metadata for persistent solver-particle fields.
Main header file for a complex fluid dynamics solver.
A 3D point or vector with PetscScalar components.
Definition variables.h:144
Defines a particle's core properties for Lagrangian tracking.
Definition variables.h:236
The master context for the entire simulation.
Definition variables.h:916
User-defined context containing data specific to a single computational grid level.
Definition variables.h:1157
Header file for particle location functions using the walking search algorithm.