chombo-discharge
Loading...
Searching...
No Matches
CD_NearestNeighborParticleMerge.H File Reference

Declaration of a distributed, MPI-safe nearest-neighbor particle-merge algorithm. More...

#include <algorithm>
#include <functional>
#include <limits>
#include <map>
#include <optional>
#include <unordered_set>
#include <unordered_map>
#include <utility>
#include <vector>
#include <RealVect.H>
#include <IntVect.H>
#include <EBGeometry.hpp>
#include <CD_ParticleSoA.H>
#include <CD_ParticleContainer.H>
#include <CD_ParticleManagement.H>
#include <CD_NamespaceHeader.H>
#include <CD_NamespaceFooter.H>
#include <CD_NearestNeighborParticleMergeImplem.H>
Include dependency graph for CD_NearestNeighborParticleMerge.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  ParticleManagement::detail::NNPooledParticle< Packed >
 Pooled per-round record for one particle: the particle plus its cached boundary-exposure flag. More...
struct  ParticleManagement::detail::NNFallbackCandidate
 One additional ranked candidate beyond a query's primary nearest neighbor. More...
struct  ParticleManagement::detail::NNMergeEdge< Packed >
 One candidate merge edge: a single particle's own nearest-neighbor query result. More...
struct  ParticleManagement::detail::NNMergeProposal< Packed >
 A cross-patch merge proposal: one particle's own nearest-neighbor query result, bundled as a self-contained, atomic pair for shipment to whichever rank owns the candidate. More...
struct  ParticleManagement::detail::NNMergeVerdict
 The judge's reply to one proposal. More...
struct  ParticleManagement::detail::NNMergeResult< Packed >
 One resolved merge: the two consumed particles' ids and the newly created merged particle (not yet placed into any patch – see placeMergedParticles()). More...
struct  ParticleManagement::detail::NNCellKeyHasher
 Hash functor for NNCellKey – REQUIRED whenever NNCellKey is used as a hash-map key; NNCellKey must never be used as an ordered (std::map or std::set) key. More...
class  ParticleManagement::NNCellBudget
 The per-cell particle count this merge drains each cell down to. More...
struct  ParticleManagement::detail::NNSpatialIndex< Packed, Cloud >
 A patch's spatial index for one round: an EBGeometry point-cloud index (PointCloudBVH or PointCloudHashGrid, selected via Cloud) over the whole patch (every local valid particle followed by every ghost) plus its precomputed k-nearest-neighbor graph. More...
struct  ParticleManagement::detail::NNCellMeta
 Per-point metadata carried by a one-cell spatial index's PointCloudBVH: a particle's global id and whether it is a local-valid particle of the patch the index was built for. More...
struct  ParticleManagement::detail::NNPatchWork
 One patch's identity for the duration of a merge round: its AMR level, local DataIndex, and its own level's cell spacing. More...

Namespaces

namespace  ParticleManagement
 Namespace for various particle management tools.
namespace  ParticleManagement::detail
 Internal-only implementation details of the ParticleManagement API.

Typedefs

template<typename Packed>
using ParticleManagement::PayloadCombine
 Illustrative signature of the combine callback: produce a merged particle's opaque payload from its two parents' payloads and weights.
using ParticleManagement::PositionValid = std::function<bool(const RealVect& a_mergedPosition)>
 Illustrative signature of the position-validity predicate for a would-be merged particle.
using ParticleManagement::IDAllocator = std::function<ParticleID()>
 Illustrative signature of the fresh-id allocator: produce one globally-unique id for a newly created merged particle.
template<typename Packed>
using ParticleManagement::MergeCommittedCallback
 Optional per-merge diagnostic callback: observe every individual merge as it commits.
using ParticleManagement::detail::NNCellKey = std::pair<int, IntVect>
 A cell key together with the AMR level it was computed on – see cellKeyOf()'s warning.
using ParticleManagement::detail::NNWalkCell = IntVect
 A non-level-qualified cell index, distinct from NNCellKey, used where a cell is expressed in a single consistent frame (the query patch's own level).
using ParticleManagement::detail::NNCellCloud = EBGeometry::PointCloudBVH<Real, NNCellMeta>
 One cell's PointCloudBVH for the one-cell nearest-neighbor merge, over every local valid and ghost particle whose cellKeyOf() falls in that cell.
using ParticleManagement::detail::NNCellSpatialIndex = std::unordered_map<NNWalkCell, std::shared_ptr<NNCellCloud>, LevelTiles::TileHasher>
 A patch's one-cell spatial index: one NNCellCloud per occupied cell, keyed by NNWalkCell (see its docs for the single-frame convention this uses).
using ParticleManagement::detail::PositionLocator = std::function<LevelTiles::LevelAndBox(const RealVect& a_position)>
 Locate-by-position callback – see placeMergedParticles().

Functions

IntVect ParticleManagement::detail::cellKeyOf (const RealVect &a_position, const RealVect &a_probLo, const RealVect &a_dx) noexcept
 The unclamped, position-derived cell index a given physical position falls in.
template<typename Packed, typename Cloud = EBGeometry::PointCloudBVH<Real, ParticleID>>
void ParticleManagement::detail::buildNNSpatialIndex (NNSpatialIndex< Packed, Cloud > &a_index, const std::vector< MergeParticle< Packed > > &a_localValid, const std::vector< MergeParticle< Packed > > &a_ghosts, const RealVect &a_probLo, const RealVect &a_dx, const NNCellBudget &a_cellBudget, const int a_kNearest) noexcept
 Build a patch's spatial index for one round: an EBGeometry point-cloud index (see Cloud) over the whole patch (every local valid particle then every ghost) plus its precomputed k-nearest-neighbor graph – UNLESS no cell in this patch could possibly be crowded, in which case nothing is built.
template<typename Packed, typename Cloud = EBGeometry::PointCloudBVH<Real, ParticleID>>
void ParticleManagement::detail::findNearestNeighborCandidates (std::vector< NNMergeEdge< Packed > > &a_edges, const std::vector< MergeParticle< Packed > > &a_localValid, const std::vector< MergeParticle< Packed > > &a_ghosts, const std::unordered_set< ParticleID > &a_consumedIDs, const NNSpatialIndex< Packed, Cloud > &a_spatialIndex, const std::unordered_map< NNCellKey, int, NNCellKeyHasher > &a_liveCellCount, const RealVect &a_probLo, const RealVect &a_dx, const NNCellBudget &a_cellBudget, const int a_maxFallbackCandidates=0, const std::optional< int > a_maxCellDistance=std::nullopt) noexcept
 For every over-threshold, currently-alive valid particle in a patch, find its single TRUE nearest neighbor among {this patch's own other valid particles} union {every ghost shipped to this patch}.
template<typename Packed>
void ParticleManagement::detail::buildNNCellSpatialIndex (NNCellSpatialIndex &a_index, const std::vector< MergeParticle< Packed > > &a_localValid, const std::vector< MergeParticle< Packed > > &a_ghosts, const RealVect &a_probLo, const RealVect &a_dx, const NNCellBudget &a_cellBudget) noexcept
 Build a patch's one-cell spatial index for the one-cell nearest-neighbor merge: one PointCloudBVH per occupied cell – UNLESS no cell in this patch could possibly be crowded, in which case nothing is built.
template<typename Packed>
void ParticleManagement::detail::findNearestNeighborCandidatesOneCell (std::vector< NNMergeEdge< Packed > > &a_edges, const std::vector< MergeParticle< Packed > > &a_localValid, const std::unordered_set< ParticleID > &a_consumedIDs, const NNCellSpatialIndex &a_cellIndex, const std::unordered_map< NNCellKey, int, NNCellKeyHasher > &a_liveCellCount, const RealVect &a_probLo, const RealVect &a_dx, const std::vector< RealVect > &a_dxByLevel, const NNCellBudget &a_cellBudget, const int a_maxFallbackCandidates=0) noexcept
 For every over-threshold, currently-alive valid particle in a patch, find its single TRUE nearest neighbor restricted to its own cell and its Moore-adjacent neighbor cells (Chebyshev cell distance <= 1) – the one-cell counterpart of findNearestNeighborCandidates().
template<typename Packed, typename Combine, typename PosValid, typename Allocator, typename OnMerge>
void ParticleManagement::detail::resolveTrivialTier (std::unordered_map< NNCellKey, int, NNCellKeyHasher > &a_liveCellCount, std::unordered_set< ParticleID > &a_consumedIDs, std::unordered_set< ParticleID > &a_hasOutgoingCommitment, std::vector< NNMergeResult< Packed > > &a_results, const std::vector< NNMergeEdge< Packed > > &a_edges, const std::unordered_map< ParticleID, NNPooledParticle< Packed > > &a_particlesByID, const Combine &a_combine, const NNCellBudget &a_cellBudget, const RealVect &a_probLo, const std::vector< RealVect > &a_dxByLevel, const PosValid &a_isPositionValid, const Allocator &a_allocateID, const int a_maxFallbackCandidates=0, const std::optional< int > a_maxCellDistance=std::nullopt, unsigned long long *a_crossLevelMergeCount=nullptr, const OnMerge &a_onMergeCommitted=[](const MergeParticle< Packed > &, const MergeParticle< Packed > &, const MergeParticle< Packed > &) { }) noexcept
 Resolve every candidate edge that is safely mergeable with zero cross-rank coordination, and mark which remaining particles have committed to a non-trivial outgoing proposal.
template<typename Packed>
void ParticleManagement::detail::generateProposals (std::vector< std::vector< NNMergeProposal< Packed > > > &a_proposalsByDestRank, const std::vector< NNMergeEdge< Packed > > &a_edges, const std::unordered_set< ParticleID > &a_hasOutgoingCommitment, const std::unordered_map< ParticleID, NNPooledParticle< Packed > > &a_particlesByID, const int a_numRanks) noexcept
 Package one outgoing proposal for every edge that resolveTrivialTier() could not resolve locally, bucketed by destination rank.
template<typename Packed, typename Combine, typename PosValid, typename Allocator, typename OnMerge>
void ParticleManagement::detail::judgeProposals (std::unordered_map< NNCellKey, int, NNCellKeyHasher > &a_liveCellCount, std::vector< NNMergeResult< Packed > > &a_results, std::vector< std::vector< NNMergeVerdict > > &a_verdictsByDestRank, const std::vector< NNMergeProposal< Packed > > &a_incomingProposals, const std::unordered_map< ParticleID, NNPooledParticle< Packed > > &a_particlesByID, const std::unordered_set< ParticleID > &a_consumedIDs, const std::unordered_set< ParticleID > &a_hasOutgoingCommitment, const std::map< ParticleID, ParticleID > &a_outgoingTargetOf, const Combine &a_combine, const NNCellBudget &a_cellBudget, const RealVect &a_probLo, const std::vector< RealVect > &a_dxByLevel, const PosValid &a_isPositionValid, const Allocator &a_allocateID, const std::optional< int > a_maxCellDistance=std::nullopt, unsigned long long *a_crossLevelMergeCount=nullptr, const OnMerge &a_onMergeCommitted=[](const MergeParticle< Packed > &, const MergeParticle< Packed > &, const MergeParticle< Packed > &) { }) noexcept
 Judge every incoming proposal that targets a particle this rank owns, and produce the verdicts to send back.
void ParticleManagement::detail::applyVerdicts (std::unordered_set< ParticleID > &a_consumedIDs, const std::vector< NNMergeVerdict > &a_incomingVerdicts) noexcept
 Apply every verdict addressed to one of this rank's own outgoing proposals: remove the source particle for every accepted proposal, do nothing for rejected ones.
template<typename Packed>
void ParticleManagement::detail::placeMergedParticles (std::map< std::pair< int, unsigned int >, std::vector< MergeParticle< Packed > > > &a_localByPatch, std::vector< std::vector< MergeParticle< Packed > > > &a_scatterByDestRank, const std::vector< NNMergeResult< Packed > > &a_results, const PositionLocator &a_locate, const int a_thisRank) noexcept
 Point-locate a newly created merged particle against the AMR grid, and bucket it for a scatter exchange if it belongs to someone else.
template<typename P, typename Packed, typename Traits, typename Gather>
void ParticleManagement::detail::gatherMergeParticles (ParticleContainer< P, Traits > &a_particles, std::vector< NNPatchWork > &a_patchWork, std::vector< std::vector< MergeParticle< Packed > > > &a_patchLocalValid, std::vector< std::vector< MergeParticle< Packed > > > &a_patchGhosts, std::unordered_map< ParticleID, NNPooledParticle< Packed > > &a_particlesByID, std::unordered_map< NNCellKey, int, NNCellKeyHasher > &a_liveCellCount, const AmrMesh &a_amr, const std::string &a_realm, const int a_finestLevel, const RealVect &a_probLo, const std::vector< RealVect > &a_dxByLevel, const int a_ghostWidth, const Gather &a_gather) noexcept
 Gather every patch this rank owns into the backend-agnostic per-patch particle sets and pooled bookkeeping a merge round needs, shared by every search backend.
template<typename P, typename Packed, typename Traits, typename Combine, typename Scatter, typename PosValid, typename Allocator, typename OnMerge>
void ParticleManagement::detail::finishMergeRound (ParticleContainer< P, Traits > &a_particles, std::unordered_map< ParticleID, NNPooledParticle< Packed > > &a_particlesByID, std::unordered_set< ParticleID > &a_consumedIDs, std::unordered_map< NNCellKey, int, NNCellKeyHasher > &a_liveCellCount, std::vector< NNMergeResult< Packed > > &a_allResults, unsigned long long *a_crossLevelMergeCount, const AmrMesh &a_amr, const std::string &a_realm, const NNCellBudget &a_cellBudget, const Combine &a_combine, const Scatter &a_scatter, const Allocator &a_allocateID, const std::optional< int > a_maxCellDistance, const PosValid &a_isPositionValid, const RealVect &a_probLo, const std::vector< RealVect > &a_dxByLevel, const std::vector< NNPatchWork > &a_patchWork, const std::vector< NNMergeEdge< Packed > > &a_pooledEdges, const std::unordered_set< ParticleID > &a_hasOutgoingCommitment, const OnMerge &a_onMergeCommitted) noexcept
 Finish a merge round: propose/judge/verdict the edges the local tier could not resolve, place every merge result, and apply everything to the real particle containers – the backend-agnostic tail shared by mergeNearestNeighborsTree() and mergeNearestNeighborsOneCell().
template<typename P, typename Packed, typename Traits, typename Cloud, typename Gather, typename Combine, typename Scatter, typename Allocator, typename PosValid, typename OnMerge>
void ParticleManagement::detail::mergeNearestNeighborsRoundImpl (ParticleContainer< P, Traits > &a_particles, unsigned long long *a_crossLevelMergeCount, const AmrMesh &a_amr, const NNCellBudget &a_cellBudget, const Gather &a_gather, const Combine &a_combine, const Scatter &a_scatter, const Allocator &a_allocateID, const bool a_iterateLocalTierToConvergence, const int a_maxFallbackCandidates, const std::optional< int > a_maxCellDistance, const int a_ghostWidth, const PosValid &a_isPositionValid, const OnMerge &a_onMergeCommitted)
 Shared implementation behind mergeNearestNeighborsTree() and mergeNearestNeighborsHash(): one full round of the whole-patch-tree nearest-neighbor merge, generic over which EBGeometry point-cloud index backs the per-patch spatial search.
template<typename P, typename Packed, typename Traits = ParticleTraits<P>, typename Gather, typename Combine, typename Scatter, typename Allocator, typename PosValid = std::function<bool(const RealVect&)>, typename OnMerge = std::function< void(const MergeParticle<Packed>&, const MergeParticle<Packed>&, const MergeParticle<Packed>&)>>
void ParticleManagement::mergeNearestNeighborsTree (ParticleContainer< P, Traits > &a_particles, const AmrMesh &a_amr, const NNCellBudget &a_cellBudget, const Gather &a_gather, const Combine &a_combine, const Scatter &a_scatter, const Allocator &a_allocateID, const bool a_iterateLocalTierToConvergence=false, const int a_maxFallbackCandidates=0, const std::optional< int > a_maxCellDistance=std::nullopt, const int a_ghostWidth=1, const PosValid &a_isPositionValid=[](const RealVect &) { return true;}, unsigned long long *a_crossLevelMergeCount=nullptr, const OnMerge &a_onMergeCommitted=[](const MergeParticle< Packed > &, const MergeParticle< Packed > &, const MergeParticle< Packed > &) { })
 Run one full round (one timestep's worth) of the nearest-neighbor merge algorithm.
template<typename P, typename Packed, typename Traits = ParticleTraits<P>, typename Gather, typename Combine, typename Scatter, typename Allocator, typename PosValid = std::function<bool(const RealVect&)>, typename OnMerge = std::function< void(const MergeParticle<Packed>&, const MergeParticle<Packed>&, const MergeParticle<Packed>&)>>
void ParticleManagement::mergeNearestNeighborsHash (ParticleContainer< P, Traits > &a_particles, const AmrMesh &a_amr, const NNCellBudget &a_cellBudget, const Gather &a_gather, const Combine &a_combine, const Scatter &a_scatter, const Allocator &a_allocateID, const bool a_iterateLocalTierToConvergence=false, const int a_maxFallbackCandidates=0, const std::optional< int > a_maxCellDistance=std::nullopt, const int a_ghostWidth=1, const PosValid &a_isPositionValid=[](const RealVect &) { return true;}, unsigned long long *a_crossLevelMergeCount=nullptr, const OnMerge &a_onMergeCommitted=[](const MergeParticle< Packed > &, const MergeParticle< Packed > &, const MergeParticle< Packed > &) { })
 Run one full round of the nearest-neighbor merge algorithm, identical in every respect to mergeNearestNeighborsTree() except that the per-patch spatial search is backed by an EBGeometry::PointCloudHashGrid (a uniform-grid index) instead of an EBGeometry::PointCloudBVH.
template<typename P, typename Packed, typename Traits = ParticleTraits<P>, typename Gather, typename Combine, typename Scatter, typename Allocator, typename PosValid = std::function<bool(const RealVect&)>, typename OnMerge = std::function< void(const MergeParticle<Packed>&, const MergeParticle<Packed>&, const MergeParticle<Packed>&)>>
void ParticleManagement::mergeNearestNeighborsOneCell (ParticleContainer< P, Traits > &a_particles, const AmrMesh &a_amr, const NNCellBudget &a_cellBudget, const Gather &a_gather, const Combine &a_combine, const Scatter &a_scatter, const Allocator &a_allocateID, const bool a_iterateLocalTierToConvergence=false, const int a_maxFallbackCandidates=0, const PosValid &a_isPositionValid=[](const RealVect &) { return true;}, unsigned long long *a_crossLevelMergeCount=nullptr, const OnMerge &a_onMergeCommitted=[](const MergeParticle< Packed > &, const MergeParticle< Packed > &, const MergeParticle< Packed > &) { })
 Run one full round of the one-cell nearest-neighbor merge: the same distributed propose/judge/verdict protocol as mergeNearestNeighborsTree(), but with candidates found via a per-cell PointCloudBVH spatial index (see NNCellSpatialIndex) instead of one whole-patch tree.

Detailed Description

Declaration of a distributed, MPI-safe nearest-neighbor particle-merge algorithm.

This file adds a nearest-neighbor super-particle merge that is safe across patch and rank boundaries. Candidates are found against a particle's full neighborhood (its own patch plus every ghost, filled once per round); a pair where neither participant is reachable from outside the patch resolves immediately (the "trivial tier"), and everything else goes through a propose/judge/verdict protocol that is safe for any number of simultaneous cross-patch suitors.

Author
Robert Marskar