chombo-discharge
Loading...
Searching...
No Matches
CD_ParticleGhostMaskImplem.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_PARTICLEGHOSTMASKIMPLEM_H
14#define CD_PARTICLEGHOSTMASKIMPLEM_H
15
16// Our includes
18#include <CD_NamespaceHeader.H>
19
20inline void
21ParticleGhostMask::define(const Box& a_box) noexcept
22{
23 m_box = a_box;
24
25 m_count.resize(a_box, 1);
26 m_count.setVal(0);
27 m_start.resize(a_box, 1);
28 m_start.setVal(-1);
29 m_flat.clear();
30}
31
32inline void
33ParticleGhostMask::incrementCount(const IntVect& a_iv) noexcept
34{
35 m_count(a_iv, 0) += 1;
36}
37
38inline void
40{
41 int offset = 0;
42 for (BoxIterator bit(m_box); bit.ok(); ++bit) {
43 const int c = m_count(bit(), 0);
44 if (c > 0) {
45 m_start(bit(), 0) = offset;
46 offset += c;
47 }
48 }
49 m_flat.resize(offset);
50}
51
52inline void
54{
55 m_flatBox.resize(m_flat.size());
56}
57
58inline void
59ParticleGhostMask::addTarget(const IntVect& a_iv, const LevelTiles::BoxIDs& a_target) noexcept
60{
61 CH_assert(m_start(a_iv, 0) >= 0);
62 CH_assert(m_start(a_iv, 0) < static_cast<int>(m_flat.size()));
63
64 // m_start doubles as the per-cell write head during the fill; finalize() rewinds it.
65 m_flat[m_start(a_iv, 0)] = a_target;
66 m_start(a_iv, 0) += 1;
67}
68
69inline void
70ParticleGhostMask::addTarget(const IntVect& a_iv, const LevelTiles::BoxIDs& a_target, const Box& a_targetBox) noexcept
71{
72 CH_assert(m_flatBox.size() == m_flat.size());
73 CH_assert(m_start(a_iv, 0) >= 0 && m_start(a_iv, 0) < static_cast<int>(m_flatBox.size()));
74
75 // Write the acceptance box at the same packed slot the target will take, then let addTarget() store the
76 // target and advance the write head.
77 m_flatBox[m_start(a_iv, 0)] = a_targetBox;
78 this->addTarget(a_iv, a_target);
79}
80
81inline void
83{
84 for (BoxIterator bit(m_box); bit.ok(); ++bit) {
85 m_start(bit(), 0) -= m_count(bit(), 0);
86 }
87}
88
89inline const Box&
90ParticleGhostMask::box() const noexcept
91{
92 // An allocated-but-undefined mask (e.g. a boundary-level fineToCoar[0]/coarToFine[finestLevel]) has an
93 // empty box and must never be queried -- callers must respect the level bounds. See Realm build site.
94 CH_assert(!m_box.isEmpty());
95
96 return m_box;
97}
98
99inline int
100ParticleGhostMask::numTargets(const IntVect& a_iv) const noexcept
101{
102 CH_assert(!m_box.isEmpty()); // guard against querying an undefined (boundary-level) mask; see box()
103
104 return m_count(a_iv, 0);
105}
106
107inline const LevelTiles::BoxIDs&
108ParticleGhostMask::target(const IntVect& a_iv, const int a_i) const noexcept
109{
110 CH_assert(a_i >= 0 && a_i < m_count(a_iv, 0));
111
112 return m_flat[m_start(a_iv, 0) + a_i];
113}
114
115inline unsigned int
116ParticleGhostMask::targetGridIndex(const IntVect& a_iv, const int a_i) const noexcept
117{
118 return this->target(a_iv, a_i).first;
119}
120
121inline unsigned int
122ParticleGhostMask::targetRank(const IntVect& a_iv, const int a_i) const noexcept
123{
124 return this->target(a_iv, a_i).second;
125}
126
127inline const Box&
128ParticleGhostMask::targetBox(const IntVect& a_iv, const int a_i) const noexcept
129{
130 CH_assert(a_i >= 0 && a_i < m_count(a_iv, 0));
131 CH_assert(m_flatBox.size() == m_flat.size());
132
133 return m_flatBox[m_start(a_iv, 0) + a_i];
134}
135
136inline bool
138{
139 return !m_flatBox.empty();
140}
141
142#include <CD_NamespaceFooter.H>
143
144#endif
Declaration of ParticleGhostMask, a per-box CSR lookup of particle ghost scatter targets.
std::pair< unsigned int, unsigned int > BoxIDs
alias type alias.
Definition CD_LevelTiles.H:46
void define(const Box &a_box) noexcept
Reset to an empty table over a box (every cell has zero targets).
Definition CD_ParticleGhostMaskImplem.H:21
const Box & box() const noexcept
Get the box that the source cells live on.
Definition CD_ParticleGhostMaskImplem.H:90
unsigned int targetGridIndex(const IntVect &a_iv, const int a_i) const noexcept
Get the destination grid index of the i-th target for a cell.
Definition CD_ParticleGhostMaskImplem.H:116
const Box & targetBox(const IntVect &a_iv, const int a_i) const noexcept
Get the acceptance box of the i-th target for a cell.
Definition CD_ParticleGhostMaskImplem.H:128
int numTargets(const IntVect &a_iv) const noexcept
Get the number of scatter targets for a cell.
Definition CD_ParticleGhostMaskImplem.H:100
void allocate() noexcept
Build step between the passes: turn the per-cell counts into CSR offsets and size the packed target a...
Definition CD_ParticleGhostMaskImplem.H:39
const LevelTiles::BoxIDs & target(const IntVect &a_iv, const int a_i) const noexcept
Get the i-th scatter target for a cell.
Definition CD_ParticleGhostMaskImplem.H:108
unsigned int targetRank(const IntVect &a_iv, const int a_i) const noexcept
Get the receiving rank of the i-th target for a cell.
Definition CD_ParticleGhostMaskImplem.H:122
void allocateTargetBoxes() noexcept
Optional build step: size the per-target acceptance-box array to match the packed target array.
Definition CD_ParticleGhostMaskImplem.H:53
std::vector< LevelTiles::BoxIDs > m_flat
Packed targets, size == sum(m_count).
Definition CD_ParticleGhostMask.H:199
bool hasTargetBoxes() const noexcept
Whether this mask carries per-target acceptance boxes.
Definition CD_ParticleGhostMaskImplem.H:137
void addTarget(const IntVect &a_iv, const LevelTiles::BoxIDs &a_target) noexcept
Build pass 2: append one target for cell a_iv.
Definition CD_ParticleGhostMaskImplem.H:59
BaseFab< int > m_start
Per cell: first index into m_flat, or -1 if the cell has no targets.
Definition CD_ParticleGhostMask.H:189
BaseFab< int > m_count
Per cell: number of targets.
Definition CD_ParticleGhostMask.H:194
void finalize() noexcept
Finish the build: rewind m_start (advanced by addTarget) back to the CSR start offsets.
Definition CD_ParticleGhostMaskImplem.H:82
Box m_box
Box that the source cells (and m_start/m_count) live on.
Definition CD_ParticleGhostMask.H:184
void incrementCount(const IntVect &a_iv) noexcept
Build pass 1: register that cell a_iv gains one more target.
Definition CD_ParticleGhostMaskImplem.H:33
std::vector< Box > m_flatBox
Packed per-target acceptance boxes, parallel to m_flat. Empty unless the mask carries them.
Definition CD_ParticleGhostMask.H:204