Block-Structured AMR Software Framework
 
Loading...
Searching...
No Matches
AMReX_IndexType.H
Go to the documentation of this file.
1
2#ifndef BL_INDEXTYPE_H
3#define BL_INDEXTYPE_H
4#include <AMReX_Config.H>
5
6#include <AMReX_IntVect.H>
7#include <AMReX_SPACE.H>
8#include <AMReX_Tuple.H>
9
10#include <iosfwd>
11
12namespace amrex {
13
21 enum CellIndex { CELL = 0, NODE = 1 };
22};
23
34template<int dim>
36{
37public:
38 static_assert(1 <= dim && dim <= 31, "The number of dimensions of IndexTypeND must be positive"
39 " and less than 32");
40
43 constexpr IndexTypeND () noexcept = default;
46 explicit IndexTypeND (const IntVectND<dim>& iv) noexcept {
47 for (int i=0; i<dim; ++i) {
48 itype |= (iv[i] ? 1U : 0U) << i;
49 }
50 }
56 template <class...Args,
57 std::enable_if_t<
58 (sizeof...(Args)+1 == dim) &&
59 IsConvertible_v<CellIndex, Args...>,
60 int> = 0>
62 constexpr IndexTypeND (CellIndex i, Args...js) noexcept {
63 CellIndex locarr[dim] = {i, static_cast<CellIndex>(js)...};
64 for (int s=0; s<dim; ++s) {
65 itype |= ((locarr[s] == CellIndex::NODE) ? 1U : 0U) << s;
66 }
67 }
68 // dtor, copy-ctor, copy-op=, move-ctor, and move-op= are compiler generated.
69
72 void set (int dir) noexcept { itype |= mask(dir); }
75 void unset (int dir) noexcept { itype &= ~mask(dir); }
78 bool test (int dir) const noexcept { return (itype & mask(dir)) != 0; }
81 void setall () noexcept { itype = (1U << dim) - 1; }
84 void clear () noexcept { itype = 0; }
87 bool any () const noexcept { return itype != 0; }
90 bool ok () const noexcept { return itype < (1U << dim); }
93 void flip (int i) noexcept { itype ^= mask(i); }
96 bool operator== (const IndexTypeND& t) const noexcept { return t.itype == itype; }
99 bool operator!= (const IndexTypeND& t) const noexcept { return t.itype != itype; }
101 bool operator< (const IndexTypeND& t) const noexcept { return itype < t.itype; }
104 bool cellCentered () const noexcept { return itype == 0; }
107 bool cellCentered (int dir) const noexcept { return (itype & mask(dir)) == 0; }
110 bool nodeCentered () const noexcept { return itype == (1U<<dim)-1; }
113 bool nodeCentered (int dir) const noexcept { return (itype & mask(dir)) != 0; }
116 void setType (int dir, CellIndex t) noexcept { t == CELL ? unset(dir) : set(dir); }
118 [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE constexpr
119 CellIndex ixType (int dir) const noexcept { return (CellIndex) ((itype & (1U<<dir)) >> dir); }
122 int operator[] (int dir) const noexcept { return test(dir); }
124 template<std::size_t i>
125 [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE constexpr
126 CellIndex get () const noexcept { static_assert(0<=i && i<dim); return ixType(i); }
129 IntVectND<dim> ixType () const noexcept {
130 IntVectND<dim> retval(0);
131 for (int i=0; i<dim; ++i) {
132 retval[i] = test(i);
133 }
134 return retval;
135 }
138 IntVectND<dim> toIntVect () const noexcept {
139 IntVectND<dim> retval(0);
140 for (int i=0; i<dim; ++i) {
141 retval[i] = test(i);
142 }
143 return retval;
144 }
152 static constexpr IndexTypeND<dim> TheCellType () noexcept {
153 return IndexTypeND<dim>{};
154 }
162 static constexpr IndexTypeND<dim> TheNodeType () noexcept {
163 IndexTypeND<dim> retval{};
164 retval.setall();
165 return retval;
166 }
167
170 static constexpr std::size_t size () noexcept {
171 return static_cast<std::size_t>(dim);
172 }
173
176 static constexpr int isize () noexcept {
177 return dim;
178 }
179
181
186 template<int new_dim>
188 IndexTypeND<new_dim> shrink () const noexcept {
189 static_assert(new_dim <= dim);
190 IndexTypeND<new_dim> retval{};
191 retval.getBits() = itype & ((1U << new_dim) - 1);
192 return retval;
193 }
194
199 template<int new_dim>
202 static_assert(new_dim >= dim);
203 IndexTypeND<new_dim> retval{};
204 retval.getBits() = itype;
205 if (fill_extra == CellIndex::NODE) {
206 retval.getBits() |= (1U << new_dim) - (1U << dim);
207 }
208 return retval;
209 }
210
215 template<int new_dim>
218 if constexpr (new_dim > dim) {
219 return expand<new_dim>(fill_extra);
220 } else {
221 return shrink<new_dim>();
222 }
223 }
224
226 [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE constexpr
227 unsigned int& getBits () noexcept { return itype; }
228
230 [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE constexpr
231 const unsigned int& getBits () const noexcept { return itype; }
232
233private:
236 static constexpr unsigned int mask (int k) noexcept { return 1U<<k; }
238 unsigned int itype{0};
239};
240
243using IndexType = IndexTypeND<AMREX_SPACEDIM>;
244
245// Template deduction guide for IndexTypeND
246template<int dim>
247AMREX_GPU_HOST_DEVICE // __device__ for HIP
249
250// Template deduction guide for IndexTypeND
251template <class...Args,
252 std::enable_if_t<
254 int> = 0>
255AMREX_GPU_HOST_DEVICE // __device__ for HIP
256IndexTypeND(IndexType::CellIndex, Args...) -> IndexTypeND<sizeof...(Args)+1>;
257
259namespace detail {
260 std::ostream& index_type_write (std::ostream& os, const unsigned int& iv, int dim);
261 std::istream& index_type_read (std::istream& is, unsigned int& iv, int dim);
262
263 template<class T, std::size_t...Ns>
264 [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE constexpr
265 T IndexTypeSplit_imp (T& retval, std::index_sequence<Ns...>, unsigned int src) noexcept {
266 int dim_shift = 0;
267 (
268 (
269 amrex::get<Ns>(retval).getBits() =
270 (src >> dim_shift) & ((1U << amrex::get<Ns>(retval).isize()) - 1),
271 dim_shift += amrex::get<Ns>(retval).isize()
272 ), ...
273 );
274 return retval;
275 }
276}
278
280template<int dim>
281std::ostream& operator<< (std::ostream& os, const IndexTypeND<dim>& it) {
282 return detail::index_type_write(os, it.getBits(), dim);
283}
285template<int dim>
286std::istream& operator>> (std::istream& is, IndexTypeND<dim>& it) {
287 return detail::index_type_read(is, it.getBits(), dim);
288}
289
294template<int d, int...dims>
296constexpr IndexTypeND<detail::get_sum<d, dims...>()>
297IndexTypeCat (const IndexTypeND<d>& v, const IndexTypeND<dims>&...vects) noexcept {
298 IndexTypeND<detail::get_sum<d, dims...>()> retval{};
299 retval.getBits() |= v.getBits();
300 int dim_shift = v.isize();
301 (
302 (
303 retval.getBits() |= (vects.getBits() << dim_shift),
304 dim_shift += vects.isize()
305 ), ...
306 );
307 return retval;
308}
309
314template<int d, int...dims>
316constexpr GpuTuple<IndexTypeND<d>, IndexTypeND<dims>...>
317IndexTypeSplit (const IndexTypeND<detail::get_sum<d, dims...>()>& v) noexcept {
319 return detail::IndexTypeSplit_imp(retval,
320 std::make_index_sequence<1 + sizeof...(dims)>(),
321 v.getBits());
322}
323
328template<int new_dim, int old_dim>
330constexpr IndexTypeND<new_dim>
332 return v.template shrink<new_dim>();
333}
334
339template<int new_dim, int old_dim>
341constexpr IndexTypeND<new_dim>
344 return v.template expand<new_dim>(fill_extra);
345}
346
351template<int new_dim, int old_dim>
353constexpr IndexTypeND<new_dim>
356 return v.template resize<new_dim>(fill_extra);
357}
358
359} // namespace amrex
360
361// Spcialize std::tuple_size for IndexTypeND. Used by structured bindings.
362template<int dim>
363struct std::tuple_size<amrex::IndexTypeND<dim>> {
364 static constexpr std::size_t value = dim;
365};
366
367// Spcialize std::tuple_element for IndexTypeND. Used by structured bindings.
368template<std::size_t s, int dim>
369struct std::tuple_element<s, amrex::IndexTypeND<dim>> {
371};
372
373#endif /*BL_INDEXTYPE_H*/
#define AMREX_FORCE_INLINE
Definition AMReX_Extension.H:119
#define AMREX_GPU_HOST_DEVICE
Definition AMReX_GpuQualifiers.H:20
Array4< int const > mask
Definition AMReX_InterpFaceRegister.cpp:93
GPU-compatible tuple.
Definition AMReX_Tuple.H:98
Cell-Based or Node-Based Indices.
Definition AMReX_IndexType.H:36
__host__ __device__ constexpr CellIndex ixType(int dir) const noexcept
Returns the CellIndex in direction dir.
Definition AMReX_IndexType.H:119
__host__ __device__ bool operator==(const IndexTypeND &t) const noexcept
True if IndexTypeNDs are identical.
Definition AMReX_IndexType.H:96
__host__ __device__ bool test(int dir) const noexcept
True if IndexTypeND is NODE based in direction dir.
Definition AMReX_IndexType.H:78
__host__ __device__ bool cellCentered() const noexcept
True if the IndexTypeND is CELL based in all directions.
Definition AMReX_IndexType.H:104
__host__ __device__ IndexTypeND< new_dim > expand(CellIndex fill_extra=CellIndex::CELL) const noexcept
Returns a new IndexTypeND of size new_dim and assigns all values of this IndexTypeND to it and fill_e...
Definition AMReX_IndexType.H:201
__host__ __device__ void flip(int i) noexcept
Change from CELL to NODE or NODE to CELL in direction dir.
Definition AMReX_IndexType.H:93
unsigned int itype
An integer holding the CellIndex in bits 0 - dim-1.
Definition AMReX_IndexType.H:238
__host__ static __device__ constexpr std::size_t size() noexcept
Return the size of this IndexTypeND.
Definition AMReX_IndexType.H:170
__host__ __device__ IntVectND< dim > toIntVect() const noexcept
Fill an IntVectND of size dim with IndexTypeNDs.
Definition AMReX_IndexType.H:138
__host__ static __device__ constexpr int isize() noexcept
Return the size of this IndexTypeND.
Definition AMReX_IndexType.H:176
__host__ __device__ IntVectND< dim > ixType() const noexcept
Fill an IntVectND of size dim with IndexTypeNDs.
Definition AMReX_IndexType.H:129
__host__ __device__ constexpr CellIndex get() const noexcept
Returns the i'th CellIndex of the IndexTypeND. Used by structured bindings.
Definition AMReX_IndexType.H:126
__host__ __device__ constexpr const unsigned int & getBits() const noexcept
Return the bit field representing the underlying data.
Definition AMReX_IndexType.H:231
__host__ __device__ void setType(int dir, CellIndex t) noexcept
Set IndexTypeND to CellIndex type t in direction dir.
Definition AMReX_IndexType.H:116
__host__ __device__ bool any() const noexcept
True if this IndexTypeND is NODE based in any direction.
Definition AMReX_IndexType.H:87
__host__ __device__ constexpr void setall() noexcept
Set NODE based in all directions.
Definition AMReX_IndexType.H:81
__host__ __device__ constexpr unsigned int & getBits() noexcept
Return the bit field representing the underlying data.
Definition AMReX_IndexType.H:227
__host__ __device__ bool ok() const noexcept
True if IndexTypeND is valid.
Definition AMReX_IndexType.H:90
__host__ __device__ bool cellCentered(int dir) const noexcept
True if the IndexTypeND is CELL based in dir-direction.
Definition AMReX_IndexType.H:107
__host__ __device__ IndexTypeND< new_dim > shrink() const noexcept
Returns a new IndexTypeND of size new_dim and assigns the first new_dim values of this IndexTypeND to...
Definition AMReX_IndexType.H:188
__host__ __device__ bool nodeCentered(int dir) const noexcept
True if the IndexTypeND is NODE based in dir-direction.
Definition AMReX_IndexType.H:113
__host__ __device__ void set(int dir) noexcept
Set IndexTypeND to be NODE based in direction dir.
Definition AMReX_IndexType.H:72
__host__ __device__ int operator[](int dir) const noexcept
Return an integer representing the IndexTypeND in direction dir.
Definition AMReX_IndexType.H:122
__host__ __device__ constexpr IndexTypeND(CellIndex i, Args...js) noexcept
Construct an IndexTypeND given an explicit CellIndex for each direction. The inputs for this construc...
Definition AMReX_IndexType.H:62
__host__ static __device__ constexpr IndexTypeND< dim > TheCellType() noexcept
This static member function returns an IndexTypeND object of value IndexTypeND::CELL....
Definition AMReX_IndexType.H:152
__host__ static __device__ constexpr unsigned int mask(int k) noexcept
Returns 1<<k.
Definition AMReX_IndexType.H:236
__host__ __device__ bool nodeCentered() const noexcept
True if the IndexTypeND is NODE based in all directions.
Definition AMReX_IndexType.H:110
__host__ __device__ IndexTypeND< new_dim > resize(CellIndex fill_extra=CellIndex::CELL) const noexcept
Returns a new IndexTypeND of size new_dim by either shrinking or expanding this IndexTypeND.
Definition AMReX_IndexType.H:217
__host__ __device__ bool operator!=(const IndexTypeND &t) const noexcept
True if IndexTypeNDs are not identical.
Definition AMReX_IndexType.H:99
__host__ __device__ bool operator<(const IndexTypeND &t) const noexcept
Definition AMReX_IndexType.H:101
__host__ __device__ constexpr IndexTypeND() noexcept=default
The default constructor.
__host__ __device__ void clear() noexcept
Set CELL based in all directions.
Definition AMReX_IndexType.H:84
__host__ __device__ void unset(int dir) noexcept
Set IndexTypeND to be CELL based in direction dir.
Definition AMReX_IndexType.H:75
__host__ static __device__ constexpr IndexTypeND< dim > TheNodeType() noexcept
This static member function returns an IndexTypeND object of value IndexTypeND::NODE....
Definition AMReX_IndexType.H:162
An Integer Vector in dim-Dimensional Space.
Definition AMReX_IntVect.H:57
Definition AMReX_Amr.cpp:49
constexpr bool IsConvertible_v
Definition AMReX_TypeTraits.H:276
IndexTypeND< 3 > IndexType
IndexType is an alias for amrex::IndexTypeND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:33
__host__ __device__ constexpr IndexTypeND< detail::get_sum< d, dims... >()> IndexTypeCat(const IndexTypeND< d > &v, const IndexTypeND< dims > &...vects) noexcept
Returns a IndexTypeND obtained by concatenating the input IndexTypeNDs. The dimension of the return v...
Definition AMReX_IndexType.H:297
__host__ __device__ constexpr IndexTypeND< new_dim > IndexTypeShrink(const IndexTypeND< old_dim > &v) noexcept
Returns a new IndexTypeND of size new_dim and assigns the first new_dim values of v to it.
Definition AMReX_IndexType.H:331
__host__ __device__ constexpr IndexTypeND< new_dim > IndexTypeResize(const IndexTypeND< old_dim > &v, IndexType::CellIndex fill_extra=IndexType::CellIndex::CELL) noexcept
Returns a new IndexTypeND of size new_dim by either shrinking or expanding iv.
Definition AMReX_IndexType.H:354
__host__ __device__ constexpr IndexTypeND< new_dim > IndexTypeExpand(const IndexTypeND< old_dim > &v, IndexType::CellIndex fill_extra=IndexType::CellIndex::CELL) noexcept
Returns a new IndexTypeND of size new_dim and assigns all values of iv to it and fill_extra to the re...
Definition AMReX_IndexType.H:342
std::istream & operator>>(std::istream &is, BoxND< dim > &bx)
Read from istream.
Definition AMReX_Box.H:1825
std::ostream & operator<<(std::ostream &os, AmrMesh const &amr_mesh)
Definition AMReX_AmrMesh.cpp:1236
__host__ __device__ constexpr GpuTuple< IndexTypeND< d >, IndexTypeND< dims >... > IndexTypeSplit(const IndexTypeND< detail::get_sum< d, dims... >()> &v) noexcept
Returns a tuple of IndexTypeND obtained by splitting the input IndexTypeND according to the dimension...
Definition AMReX_IndexType.H:317
Type for defining CellIndex so that all IndexTypeND with different dimensions have the same CellIndex...
Definition AMReX_IndexType.H:19
CellIndex
The cell index type: one of CELL or NODE.
Definition AMReX_IndexType.H:21
@ CELL
Definition AMReX_IndexType.H:21
@ NODE
Definition AMReX_IndexType.H:21
typename amrex::IndexTypeND< dim >::CellIndex type
Definition AMReX_IndexType.H:370