chombo-discharge
Loading...
Searching...
No Matches
CD_LevelTiles.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_LEVELTILES_H
14#define CD_LEVELTILES_H
15
16// Std includes
17#include <cmath>
18#include <cstddef>
19#include <unordered_map>
20#include <vector>
21
22// Chombo includes
23#include <IntVect.H>
24#include <RealVect.H>
25#include <Vector.H>
26#include <RefCountedPtr.H>
27#include <SPMD.H>
28#include <DisjointBoxLayout.H>
29#include <DataIndex.H>
30
31// Our includes
32#include <CD_NamespaceHeader.H>
33
43{
44public:
46 using BoxIDs = std::pair<unsigned int, unsigned int>;
47
55 {
61 std::size_t
62 operator()(const IntVect& a_tile) const noexcept
63 {
64 std::size_t hash = 14695981039346656037ULL; // FNV-1a 64-bit offset basis
65 for (int dir = 0; dir < SpaceDim; dir++) {
66 hash ^= static_cast<std::size_t>(static_cast<unsigned int>(a_tile[dir]));
67 hash *= 1099511628211ULL;
68 }
69 return hash;
70 }
71 };
72
76 LevelTiles() noexcept;
77
83 LevelTiles(const DisjointBoxLayout& a_dbl, const int a_minBlockSize) noexcept;
84
88 virtual ~LevelTiles() noexcept;
89
95 virtual void
96 define(const DisjointBoxLayout& a_dbl, const int a_minBlockSize) noexcept;
97
102 virtual const std::unordered_map<IntVect, unsigned int, TileHasher>&
103 getMyTiles() const noexcept;
104
109 virtual const std::unordered_map<IntVect, BoxIDs, TileHasher>&
110 getOtherTiles() const noexcept;
111
116 virtual const std::unordered_map<unsigned int, DataIndex>&
117 getMyGrids() const noexcept;
118
123 {
127 int level;
128
132 unsigned int gridIndex;
133
137 int rank;
138
142 bool valid;
143 };
144
160 static inline LevelAndBox
161 findDestination(const RealVect& a_pos,
162 const RealVect& a_probLo,
163 const Vector<RealVect>& a_dx,
164 const int a_minBlockSize,
165 const Vector<RefCountedPtr<LevelTiles>>& a_levelTiles,
166 const int a_finestLevel) noexcept
167 {
168 // Finest level whose min-block tile contains the point wins.
169 for (int lvl = a_finestLevel; lvl >= 0; lvl--) {
170 IntVect tile;
171
172 for (int dir = 0; dir < SpaceDim; dir++) {
173 tile[dir] = static_cast<int>(std::floor((a_pos[dir] - a_probLo[dir]) / (a_minBlockSize * a_dx[lvl][dir])));
174 }
175
176 const LevelTiles& tiles = *a_levelTiles[lvl];
177
178 const auto& myTiles = tiles.getMyTiles();
179 const auto mit = myTiles.find(tile);
180
181 if (mit != myTiles.end()) {
182 return LevelAndBox{lvl, mit->second, procID(), true};
183 }
184
185#ifdef CH_MPI
186 const auto& otherTiles = tiles.getOtherTiles();
187 const auto oit = otherTiles.find(tile);
188
189 if (oit != otherTiles.end()) {
190 return LevelAndBox{lvl, oit->second.first, static_cast<int>(oit->second.second), true};
191 }
192#endif
193 }
194
195 return LevelAndBox{-1, 0u, -1, false};
196 }
197
198protected:
203
207 std::unordered_map<IntVect, unsigned int, TileHasher> m_myTiles;
208
212 std::unordered_map<IntVect, BoxIDs, TileHasher> m_otherTiles;
213
217 std::unordered_map<unsigned int, DataIndex> m_myGrids;
218};
219
220#include <CD_NamespaceFooter.H>
221
222#endif
Class for storing the AMR hierarchy as a collection of tiles.
Definition CD_LevelTiles.H:43
virtual const std::unordered_map< IntVect, BoxIDs, TileHasher > & getOtherTiles() const noexcept
Get the tiles owned by other ranks.
Definition CD_LevelTiles.cpp:95
virtual void define(const DisjointBoxLayout &a_dbl, const int a_minBlockSize) noexcept
Define function. Puts object in usable state.
Definition CD_LevelTiles.cpp:41
bool m_isDefined
Is defined or not.
Definition CD_LevelTiles.H:202
std::unordered_map< IntVect, BoxIDs, TileHasher > m_otherTiles
Grids owned by other ranks.
Definition CD_LevelTiles.H:212
LevelTiles() noexcept
Weak constructor. Must subsequently call the define function.
Definition CD_LevelTiles.cpp:23
virtual const std::unordered_map< IntVect, unsigned int, TileHasher > & getMyTiles() const noexcept
Get the tiles owned by this rank.
Definition CD_LevelTiles.cpp:87
std::unordered_map< IntVect, unsigned int, TileHasher > m_myTiles
"Tiles" owned by this rank
Definition CD_LevelTiles.H:207
std::unordered_map< unsigned int, DataIndex > m_myGrids
Mapping of grid index to DataIndex.
Definition CD_LevelTiles.H:217
static LevelAndBox findDestination(const RealVect &a_pos, const RealVect &a_probLo, const Vector< RealVect > &a_dx, const int a_minBlockSize, const Vector< RefCountedPtr< LevelTiles > > &a_levelTiles, const int a_finestLevel) noexcept
Map a physical position to its owning (level, grid index, rank) via the finest containing tile.
Definition CD_LevelTiles.H:161
virtual const std::unordered_map< unsigned int, DataIndex > & getMyGrids() const noexcept
Get the mapping of grid index to DataIndex.
Definition CD_LevelTiles.cpp:103
std::pair< unsigned int, unsigned int > BoxIDs
alias type alias.
Definition CD_LevelTiles.H:46
Result of a point->block query. See findDestination.
Definition CD_LevelTiles.H:123
unsigned int gridIndex
Global grid/box index of the covering box within that level's LevelTiles.
Definition CD_LevelTiles.H:132
int rank
MPI rank owning that box.
Definition CD_LevelTiles.H:137
int level
Owning AMR level (finest tile that contains the point), or -1 if not found.
Definition CD_LevelTiles.H:127
bool valid
True if a covering tile/box was found (false when the point is off-domain).
Definition CD_LevelTiles.H:142
Hash functor for using IntVect tiles as unordered_map keys.
Definition CD_LevelTiles.H:55
std::size_t operator()(const IntVect &a_tile) const noexcept
Hash operator.
Definition CD_LevelTiles.H:62