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
54
74mergeMethodFromString(const std::string& a_str) noexcept;
75
82template <class P, class Traits = ParticleTraits<P>>
83using ParticleMerger = std::function<
84 void(ParticleSoA<P, Traits>& a_particles, const CellInfo& a_cellInfo, const int a_numTargetParticles)>;
85
93template <class P>
94using BinaryParticleReconcile = std::function<void(P& p1, P& p2, const P& p0)>;
95
102namespace detail {
103
134template <class P, Real P::*weight, RealVect P::*position>
135inline void
136buildEqualWeightKDLeaves(const std::vector<P>& a_particles,
137 const int a_maxLeaves,
138 const BinaryParticleReconcile<P>& a_particleReconcile,
139 std::vector<std::pair<const P*, const P*>>& a_leaves) noexcept;
140
156template <class P, RealVect P::*position>
157inline void
158mergeAdjacentNearest(std::vector<P>& a_particles,
159 const std::size_t a_target,
160 const std::function<void(P&, const P&)>& a_combine) noexcept;
161
162} // namespace detail
163
198template <class Packed,
199 Real Packed::*packWeight,
200 RealVect Packed::*packPosition,
201 class P,
202 class Traits = ParticleTraits<P>>
203inline ParticleMerger<P, Traits>
204makeSfcNearestNeighborMerger(std::function<Packed(const ParticleSoA<P, Traits>&, std::size_t)> a_gather,
205 std::function<void(Packed&, const Packed&)> a_combine,
206 std::function<void(ParticleSoA<P, Traits>&, const Packed&)> a_scatter) noexcept;
207
242template <class Packed,
243 Real Packed::*packWeight,
244 RealVect Packed::*packPosition,
245 class P,
246 class Traits = ParticleTraits<P>>
247inline ParticleMerger<P, Traits>
249 std::function<Packed(const ParticleSoA<P, Traits>&, std::size_t)> a_gather,
250 BinaryParticleReconcile<Packed> a_reconcile,
251 std::function<void(ParticleSoA<P, Traits>&, const Packed*, const Packed*, const CellInfo&)> a_scatterLeaf) noexcept;
252
283template <class Context, class P, class Traits = ParticleTraits<P>>
284inline ParticleMerger<P, Traits>
285makeReinitializeMerger(std::function<std::pair<long long, Context>(const ParticleSoA<P, Traits>&)> a_aggregate,
286 std::function<void(ParticleSoA<P, Traits>&, const RealVect&, long long, const Context&)> a_emit,
287 std::function<RealVect()> a_probLo) noexcept;
288
298template <typename P, typename Traits, typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
299static inline void
300removePhysicalParticles(ParticleSoA<P, Traits>& a_particles, const T a_numPhysPartToRemove) noexcept;
301
309template <typename P, typename Traits>
310static inline void
311deleteParticles(ParticleSoA<P, Traits>& a_particles, const Real a_weightThresh) noexcept;
312
319template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
320static inline std::vector<T>
321partitionParticleWeights(const T a_numPhysicalParticles, const T a_maxCompParticles) noexcept;
322
329namespace detail {
330
336template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
337static inline T
338partitionParticles(const T a_numParticles);
339
340} // namespace detail
341
356template <typename P, typename Traits, typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
357static inline void
359 const T a_numParticles,
360 const std::function<RealVect()>& a_distribution);
361
374template <typename P, typename Traits, typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
375static inline void
377 const T a_numParticles,
378 const RealVect& a_center,
379 const Real a_radius) noexcept;
380
393template <typename P, typename Traits, typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
394static inline void
396 const T a_numParticles,
397 const RealVect& a_loCorner,
398 const RealVect& a_hiCorner) noexcept;
399
412template <typename P, typename Traits, typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
413static inline void
415 const T a_numParticles,
416 const RealVect& a_center,
417 const Real a_radius) noexcept;
418
428template <typename Packed>
430{
434 RealVect position = RealVect::Zero;
435
439 Real weight = 0.0;
440
449
455
462 bool isGhost = false;
463
468 int level = 0;
469
474 Packed payload;
475};
476
477} // namespace ParticleManagement
478
479#include <CD_NamespaceFooter.H>
480
482
483#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
void mergeAdjacentNearest(std::vector< P > &a_particles, const std::size_t a_target, const std::function< void(P &, const P &)> &a_combine) noexcept
Merge an already spatially-ordered particle list down to a target count by repeatedly combining the n...
Definition CD_ParticleManagementImplem.H:320
void buildEqualWeightKDLeaves(const std::vector< P > &a_particles, const int a_maxLeaves, const BinaryParticleReconcile< P > &a_particleReconcile, std::vector< std::pair< const P *, const P * > > &a_leaves) noexcept
Build an equal-weight KD partition of a list of particles and return the leaf particle ranges.
Definition CD_ParticleManagementImplem.H:87
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:84
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:839
std::function< void(P &p1, P &p2, const P &p0)> BinaryParticleReconcile
Declaration of a reconciliation function when splitting particles.
Definition CD_ParticleManagement.H:94
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:859
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:611
ParticleMerger< P, Traits > makeSfcNearestNeighborMerger(std::function< Packed(const ParticleSoA< P, Traits > &, std::size_t)> a_gather, std::function< void(Packed &, const Packed &)> a_combine, std::function< void(ParticleSoA< P, Traits > &, const Packed &)> a_scatter) noexcept
Create a space-filling-curve nearest-neighbor super-particle merger as a reusable ParticleMerger.
Definition CD_ParticleManagementImplem.H:441
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:821
ParticleMergeMethod
The specific super-particle merge methods.
Definition CD_ParticleManagement.H:40
@ NnSfc
Cell: makeSfcNearestNeighborMerger.
@ KdSkinNn
AMR: mergeKDInterior for the uncontested tier, nearest-neighbor pairs for the skin.
@ NnPairTree
AMR: mergeNearestNeighborsTree (whole-patch PointCloudBVH search).
@ KdPatch
AMR: mergeKDPatch (same build, patch-local – no ghosts, no contested particles).
@ NnPairOneCell
AMR: mergeNearestNeighborsOneCell (per-cell PointCloudBVH search, Chebyshev distance 1 only).
@ External
Cell: caller-supplied per-cell merger.
@ Reinitialize
Cell: makeReinitializeMerger.
@ EqualWeightKD
Cell: makeEqualWeightKDMerger.
@ KdCarve
AMR: mergeKDCarve (whole-patch kd-tree build, arbitrated patch boundaries).
@ ReinitializeBVH
Cell: makeEqualWeightKDMerger with reinitialized leaf positions.
@ NnPairHash
AMR: mergeNearestNeighborsHash (whole-patch PointCloudHashGrid search).
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:804
ParticleMergeMethod mergeMethodFromString(const std::string &a_str) noexcept
Map a merge-algorithm selector string to a ParticleMergeMethod.
Definition CD_ParticleManagementImplem.H:38
ParticleMerger< P, Traits > makeEqualWeightKDMerger(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 an equal-weight KD-tree super-particle merger as a reusable ParticleMerger.
Definition CD_ParticleManagementImplem.H:564
Minimal, payload-agnostic description of one particle as input to a distributed merge.
Definition CD_ParticleManagement.H:430
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:462
RealVect position
Particle position.
Definition CD_ParticleManagement.H:434
Packed payload
Opaque, caller-defined payload. Never inspected by the merge logic – only carried through to the comb...
Definition CD_ParticleManagement.H:474
Real weight
Particle weight. Not required to be an integer.
Definition CD_ParticleManagement.H:439
int level
The AMR level this particle lives on. Used by the nearest-neighbor mergers, whose cell keys are level...
Definition CD_ParticleManagement.H:468
ParticleID globalID
Globally unique particle id, unique within one merge round across every rank.
Definition CD_ParticleManagement.H:448
RankID ownerRank
The rank owning this particle. Always read from the particle's own data, never inferred from MPI tran...
Definition CD_ParticleManagement.H:454
Traits class that a user specializes to describe the PAYLOAD columns of a particle type....
Definition CD_ParticleSoA.H:192