chombo-discharge
Loading...
Searching...
No Matches
CD_ParticleManagement.H
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2021-2026 SINTEF Energy Research
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
13#ifndef CD_PARTICLEMANAGEMENT_H
14#define CD_PARTICLEMANAGEMENT_H
15
16// Std includes
17#include <functional>
18#include <string>
19#include <utility>
20#include <vector>
21
22// Chombo includes
23#include <RealVect.H>
24#include <MayDay.H>
25
26// Our includes
27#include <CD_CellInfo.H>
28#include <CD_ParticleSoA.H>
29#include <CD_NamespaceHeader.H>
30
34namespace ParticleManagement {
35
47enum class KDPartition
48{
49 Weight,
50 Count,
51 Hybrid,
53};
54
65{
66 Center,
67 Jitter,
68 Cell
69};
70
79enum class KDPlacement
80{
81 Centroid,
82 Sample,
83 Random
84};
85
93enum class KDAmrBoundary
94{
95 Carve,
96 Nn
97};
98
104enum class NNSearch
105{
106 Tree,
107 Hash,
108 OneCell
109};
110
118{
119 None,
120 KdCell,
121 KdPatch,
122 KdAmr,
123 NnAmr,
125 External
126};
127
142mergeMethodFromString(const std::string& a_str) noexcept;
143
150inline KDPartition
151kdPartitionFromString(const std::string& a_str) noexcept;
152
159inline KDPlacement
160kdPlacementFromString(const std::string& a_str) noexcept;
161
168inline KDSplitPlacement
169kdSplitPlacementFromString(const std::string& a_str) noexcept;
170
177inline KDAmrBoundary
178kdAmrBoundaryFromString(const std::string& a_str) noexcept;
179
186inline NNSearch
187nnSearchFromString(const std::string& a_str) noexcept;
188
195template <class P, class Traits = ParticleTraits<P>>
196using ParticleMerger = std::function<
197 void(ParticleSoA<P, Traits>& a_particles, const CellInfo& a_cellInfo, const int a_numTargetParticles)>;
198
206template <class P>
207using BinaryParticleReconcile = std::function<void(P& p1, P& p2, const P& p0)>;
208
215namespace detail {
216
259template <class P, Real P::*weight, RealVect P::*position>
260inline void
261buildKDCellLeaves(const std::vector<P>& a_particles,
262 const int a_maxLeaves,
263 const Real a_weightMedianLength,
264 const bool a_capWeights,
265 const std::function<RealVect(const RealVect&)>& a_splitPosition,
266 const BinaryParticleReconcile<P>& a_particleReconcile,
267 std::vector<std::pair<const P*, const P*>>& a_leaves) noexcept;
268
269} // namespace detail
270
310template <class Packed,
311 Real Packed::*packWeight,
312 RealVect Packed::*packPosition,
313 class P,
314 class Traits = ParticleTraits<P>>
315inline ParticleMerger<P, Traits>
317 const Real a_weightMedianCellWidths,
318 const bool a_capWeights,
319 const KDSplitPlacement a_splitPlacement,
320 std::function<RealVect()> a_probLo,
321 std::function<Packed(const ParticleSoA<P, Traits>&, std::size_t)> a_gather,
322 BinaryParticleReconcile<Packed> a_reconcile,
323 std::function<void(ParticleSoA<P, Traits>&, const Packed*, const Packed*, const CellInfo&)> a_scatterLeaf) noexcept;
324
355template <class Context, class P, class Traits = ParticleTraits<P>>
356inline ParticleMerger<P, Traits>
357makeReinitializeMerger(std::function<std::pair<long long, Context>(const ParticleSoA<P, Traits>&)> a_aggregate,
358 std::function<void(ParticleSoA<P, Traits>&, const RealVect&, long long, const Context&)> a_emit,
359 std::function<RealVect()> a_probLo) noexcept;
360
370template <typename P, typename Traits, typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
371static inline void
372removePhysicalParticles(ParticleSoA<P, Traits>& a_particles, const T a_numPhysPartToRemove) noexcept;
373
381template <typename P, typename Traits>
382static inline void
383deleteParticles(ParticleSoA<P, Traits>& a_particles, const Real a_weightThresh) noexcept;
384
396template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
397static inline void
398partitionParticleWeights(std::vector<T>& a_weights,
399 const T a_numPhysicalParticles,
400 const T a_maxCompParticles) noexcept;
401
408template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
409static inline std::vector<T>
410partitionParticleWeights(const T a_numPhysicalParticles, const T a_maxCompParticles) noexcept;
411
418namespace detail {
419
425template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
426static inline T
427partitionParticles(const T a_numParticles);
428
429} // namespace detail
430
445template <typename P, typename Traits, typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
446static inline void
448 const T a_numParticles,
449 const std::function<RealVect()>& a_distribution);
450
463template <typename P, typename Traits, typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
464static inline void
466 const T a_numParticles,
467 const RealVect& a_center,
468 const Real a_radius) noexcept;
469
482template <typename P, typename Traits, typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
483static inline void
485 const T a_numParticles,
486 const RealVect& a_loCorner,
487 const RealVect& a_hiCorner) noexcept;
488
501template <typename P, typename Traits, typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
502static inline void
504 const T a_numParticles,
505 const RealVect& a_center,
506 const Real a_radius) noexcept;
507
517template <typename Packed>
519{
523 RealVect position = RealVect::Zero;
524
528 Real weight = 0.0;
529
538
544
551 bool isGhost = false;
552
557 int level = 0;
558
563 Packed payload;
564};
565
566} // namespace ParticleManagement
567
568#include <CD_NamespaceFooter.H>
569
571
572#endif
Simple class for holding some quantities relevant in a grid cell.
Implementation of CD_ParticleManagement.H.
Declaration of ParticleSoA, an arena-backed Struct-of-Arrays particle container.
std::int32_t RankID
Owning-rank identifier type (container-owned metadata column; fixed-width for I/O).
Definition CD_ParticleSoA.H:166
std::int64_t ParticleID
Global particle identifier type (container-owned metadata column; fixed-width for I/O).
Definition CD_ParticleSoA.H:161
Class for the cell-information that is often queried when merging particles inside a cell.
Definition CD_CellInfo.H:26
Arena-backed Struct-of-Arrays particle container for a single grid patch.
Definition CD_ParticleSoA.H:655
Class for encapsulating random number generation. This class is MPI and OpenMP safe.
Definition CD_Random.H:39
void buildKDCellLeaves(const std::vector< P > &a_particles, const int a_maxLeaves, const Real a_weightMedianLength, const bool a_capWeights, const std::function< RealVect(const RealVect &)> &a_splitPosition, const BinaryParticleReconcile< P > &a_particleReconcile, std::vector< std::pair< const P *, const P * > > &a_leaves) noexcept
Build a KD partition of a list of particles and return the leaf particle ranges.
Definition CD_ParticleManagementImplem.H:275
Namespace for various particle management tools.
Definition CD_KDParticleMerge.H:33
std::function< void(ParticleSoA< P, Traits > &a_particles, const CellInfo &a_cellInfo, const int a_numTargetParticles)> ParticleMerger
Concept for splitting/merging particles.
Definition CD_ParticleManagement.H:197
KDAmrBoundary kdAmrBoundaryFromString(const std::string &a_str) noexcept
Turn an input string into a kd AMR boundary rule.
Definition CD_ParticleManagementImplem.H:183
KDSplitPlacement kdSplitPlacementFromString(const std::string &a_str) noexcept
Turn an input string into a split placement rule.
Definition CD_ParticleManagementImplem.H:162
void drawBoxParticles(ParticleSoA< P, Traits > &a_particles, const T a_numParticles, const RealVect &a_loCorner, const RealVect &a_hiCorner) noexcept
Draw box-distributed particles into an SoA container (unit weight; positions only).
Definition CD_ParticleManagementImplem.H:910
KDSplitPlacement
Where the pieces of a split particle go.
Definition CD_ParticleManagement.H:65
@ Cell
Each piece drawn uniformly in the owning cell.
@ Jitter
Each piece drawn from the local mean interparticle spacing around the parent, truncated to the cell.
@ Center
Every piece at the parent's position. No spatial perturbation, duplicate positions.
std::function< void(P &p1, P &p2, const P &p0)> BinaryParticleReconcile
Declaration of a reconciliation function when splitting particles.
Definition CD_ParticleManagement.H:207
ParticleMerger< P, Traits > makeKDCellMerger(const Real a_weightMedianCellWidths, const bool a_capWeights, const KDSplitPlacement a_splitPlacement, std::function< RealVect()> a_probLo, std::function< Packed(const ParticleSoA< P, Traits > &, std::size_t)> a_gather, BinaryParticleReconcile< Packed > a_reconcile, std::function< void(ParticleSoA< P, Traits > &, const Packed *, const Packed *, const CellInfo &)> a_scatterLeaf) noexcept
Create a per-cell KD-tree super-particle merger as a reusable ParticleMerger.
Definition CD_ParticleManagementImplem.H:595
void drawSphereParticles(ParticleSoA< P, Traits > &a_particles, const T a_numParticles, const RealVect &a_center, const Real a_radius) noexcept
Draw sphere-distributed particles into an SoA container (unit weight; positions only).
Definition CD_ParticleManagementImplem.H:930
KDPartition
How a kd merge divides a node into two children.
Definition CD_ParticleManagement.H:48
@ WeightCapped
PROTOTYPE. Cap every particle at the target leaf weight, then split at the weight median.
@ Weight
Split at the weight median at every node; halves carry near-equal weight.
@ Count
Split at the count median at every node; halves hold near-equal particle counts.
@ Hybrid
Count median while the node is wider than the crossover length, weight median below it.
KDAmrBoundary
How the AMR-scope kd merge resolves the leaves that touch a patch or rank boundary.
Definition CD_ParticleManagement.H:94
@ Nn
Merge the interior with the kd tree and the boundary skin with nearest-neighbour pairs.
@ Carve
Arbitrate contested particles between patches (z-buffer carve).
KDPartition kdPartitionFromString(const std::string &a_str) noexcept
Turn an input string into a kd partition rule.
Definition CD_ParticleManagementImplem.H:117
ParticleMerger< P, Traits > makeReinitializeMerger(std::function< std::pair< long long, Context >(const ParticleSoA< P, Traits > &)> a_aggregate, std::function< void(ParticleSoA< P, Traits > &, const RealVect &, long long, const Context &)> a_emit, std::function< RealVect()> a_probLo) noexcept
Create a reinitialize super-particle merger as a reusable ParticleMerger.
Definition CD_ParticleManagementImplem.H:671
void drawGaussianParticles(ParticleSoA< P, Traits > &a_particles, const T a_numParticles, const RealVect &a_center, const Real a_radius) noexcept
Draw Gaussian-distributed particles into an SoA container (unit weight; positions only).
Definition CD_ParticleManagementImplem.H:892
NNSearch nnSearchFromString(const std::string &a_str) noexcept
Turn an input string into a nearest-neighbour search backend.
Definition CD_ParticleManagementImplem.H:201
NNSearch
How the AMR-scope nearest-neighbour merge finds a particle's merge candidates.
Definition CD_ParticleManagement.H:105
@ Tree
One whole-patch PointCloudBVH per patch.
@ OneCell
One PointCloudBVH per occupied cell; merge distance structurally fixed at 1 cell.
@ Hash
One whole-patch PointCloudHashGrid per patch.
KDPlacement
Where a kd merge puts the particle a leaf reduces to.
Definition CD_ParticleManagement.H:80
@ Centroid
The leaf's weighted centroid.
@ Sample
One of the leaf's own particles, drawn with probability proportional to weight.
ParticleMergeMethod
The super-particle merge methods, named by the scope they group particles over.
Definition CD_ParticleManagement.H:118
@ KdPatch
Patch: mergeKDPatch, one kd tree per patch, patch-local (no ghosts).
@ NnAmr
AMR: nearest-neighbour pair merge over the hierarchy – see NNSearch.
@ KdCell
Cell: makeKDCellMerger, one kd tree per cell.
@ KdAmr
AMR: one kd tree per patch, resolved across patches – see KDAmrBoundary.
@ External
Cell: caller-supplied per-cell merger.
@ Reinitialize
Cell: makeReinitializeMerger; positions discarded and redrawn.
void drawRandomParticles(ParticleSoA< P, Traits > &a_particles, const T a_numParticles, const std::function< RealVect()> &a_distribution)
Draw random SoA particles into a free buffer, distributed according to a predefined distribution.
Definition CD_ParticleManagementImplem.H:875
ParticleMergeMethod mergeMethodFromString(const std::string &a_str) noexcept
Map a merge-method selector string to a ParticleMergeMethod.
Definition CD_ParticleManagementImplem.H:37
KDPlacement kdPlacementFromString(const std::string &a_str) noexcept
Turn an input string into a kd placement rule.
Definition CD_ParticleManagementImplem.H:141
Minimal, payload-agnostic description of one particle as input to a distributed merge.
Definition CD_ParticleManagement.H:519
bool isGhost
True iff this is a ghost copy in the patch currently holding it, i.e. not physically stored there....
Definition CD_ParticleManagement.H:551
RealVect position
Particle position.
Definition CD_ParticleManagement.H:523
Packed payload
Opaque, caller-defined payload. Never inspected by the merge logic – only carried through to the comb...
Definition CD_ParticleManagement.H:563
Real weight
Particle weight. Not required to be an integer.
Definition CD_ParticleManagement.H:528
int level
The AMR level this particle lives on. Used by the nearest-neighbor mergers, whose cell keys are level...
Definition CD_ParticleManagement.H:557
ParticleID globalID
Globally unique particle id, unique within one merge round across every rank.
Definition CD_ParticleManagement.H:537
RankID ownerRank
The rank owning this particle. Always read from the particle's own data, never inferred from MPI tran...
Definition CD_ParticleManagement.H:543
Traits class that a user specializes to describe the PAYLOAD columns of a particle type....
Definition CD_ParticleSoA.H:192