Block-Structured AMR Software Framework
AMReX_EBCellFlag.H
Go to the documentation of this file.
1 #ifndef AMREX_EBCELLFLAG_H_
2 #define AMREX_EBCELLFLAG_H_
3 #include <AMReX_Config.H>
4 
5 #include <AMReX_Array.H>
6 #include <AMReX_IntVect.H>
7 #include <AMReX_BaseFab.H>
8 #include <AMReX_FabFactory.H>
9 
10 #include <cstdint>
11 #include <map>
12 #include <iosfwd>
13 
14 namespace amrex {
15 
17 {
18 public:
19 
20  constexpr EBCellFlag () noexcept = default;
21  ~EBCellFlag () noexcept = default;
22  constexpr EBCellFlag (const EBCellFlag& rhs) noexcept = default;
23  constexpr EBCellFlag (EBCellFlag&& rhs) noexcept = default;
24  constexpr EBCellFlag& operator= (const EBCellFlag& rhs) noexcept = default;
25  constexpr EBCellFlag& operator= (EBCellFlag&& rhs) noexcept = default;
26 
27  explicit constexpr EBCellFlag (uint32_t i) noexcept : flag(i) {}
28 
29  constexpr EBCellFlag& operator= (uint32_t i) noexcept { flag = i; return *this; }
30 
32  EBCellFlag& operator+= (const EBCellFlag& /* rhs */) {
33  amrex::Abort("EBCellFlag::operator+= not supported");
34  return *this;
35  }
36 
38  void setRegular () noexcept {
40  flag |= regular_bits;
42  }
43 
45  void setCovered () noexcept {
47  }
48 
50  void setSingleValued () noexcept {
54  }
55 
57  void setMultiValued (int n) noexcept {
60  AMREX_ASSERT(n >= 2 && n <= 7);
61  flag |= static_cast<uint32_t>(n) << pos_numvofs;
62  }
63 
65  bool operator==(const EBCellFlag& a_input) const noexcept
66  {
67  return (flag == (a_input.flag));
68  }
69 
71  bool operator!=(const EBCellFlag& a_input) const noexcept
72  {
73  return (flag != (a_input.flag));
74  }
75 
77  int getNumVoFs () const noexcept
78  {
79  return static_cast<int>((flag & one_numvofs_mask) >> pos_numvofs);
80  }
81 
83  bool isRegular () const noexcept {
84  return (flag & one_type_mask) == regular_bits;
85  }
86 
88  bool isSingleValued () const noexcept {
90  }
91 
93  bool isMultiValued () const noexcept {
94  return (flag & one_type_mask) == multi_valued_bits;
95  }
96 
98  bool isCovered () const noexcept {
99  return (flag & one_type_mask) == covered_bits;
100  }
101 
103  bool isConnected (const IntVect& iv) const noexcept {
104  int i=0, j=0, k=0;
105  AMREX_D_TERM(i=iv[0];, j=iv[1];, k=iv[2]);
106  const int n = w_lower_mask + 13 + i + 3*j + k*9; // pos_ngbr[k+1,j+1,i+1]
107  return flag & (1 << n);
108  }
109 
111  bool isConnected (int i, int j, int k) const noexcept {
112  const int n = w_lower_mask + 13 + i + 3*j + k*9; // pos_ngbr[k+1,j+1,i+1]
113  return flag & (1 << n);
114  }
115 
117  bool isDisconnected (const IntVect& iv) const noexcept {
118  int i=0, j=0, k=0;
119  AMREX_D_TERM(i=iv[0];, j=iv[1];, k=iv[2]);
120  const int n = w_lower_mask + 13 + i + 3*j + k*9; // pos_ngbr[k+1,j+1,i+1]
121  return !(flag & (1 << n));
122  }
123 
125  bool isDisconnected (int i, int j, int k) const noexcept {
126  const int n = w_lower_mask + 13 + i + 3*j + k*9; // pos_ngbr[k+1,j+1,i+1]
127  return !(flag & (1 << n));
128  }
129 
131  void setDisconnected () noexcept {
132  flag &= one_lower_mask;
133  }
134 
136  void setDisconnected (const IntVect& iv) noexcept {
137  int i=0, j=0, k=0;
138  AMREX_D_TERM(i=iv[0];, j=iv[1];, k=iv[2]);
139  const int n = w_lower_mask + 13 + i + 3*j + k*9; // pos_ngbr[k+1,j+1,i+1]
140  flag &= ~(1 << n);
141  }
142 
144  void setDisconnected (int i, int j, int k) noexcept {
145  const int n = w_lower_mask + 13 + i + 3*j + k*9; // pos_ngbr[k+1,j+1,i+1]
146  flag &= ~(1 << n);
147  }
148 
150  void setConnected () noexcept {
152  }
153 
155  void setConnected (const IntVect& iv) noexcept {
156  int i=0, j=0, k=0;
157  AMREX_D_TERM(i=iv[0];, j=iv[1];, k=iv[2]);
158  const int n = w_lower_mask + 13 + i + 3*j + k*9; // pos_ngbr[k+1,j+1,i+1]
159  flag |= 1 << n;
160  }
161 
163  void setConnected (int i, int j, int k) noexcept {
164  const int n = w_lower_mask + 13 + i + 3*j + k*9; // pos_ngbr[k+1,j+1,i+1]
165  flag |= 1 << n;
166  }
167 
169  int numNeighbors () const noexcept {
170  int n = 1;
171  n += isConnected(-1,-1, 0);
172  n += isConnected( 0,-1, 0);
173  n += isConnected( 1,-1, 0);
174  //
175  n += isConnected(-1, 0, 0);
176  n += isConnected( 1, 0, 0);
177  //
178  n += isConnected(-1, 1, 0);
179  n += isConnected( 0, 1, 0);
180  n += isConnected( 1, 1, 0);
181 #if (AMREX_SPACEDIM == 3)
182  n += isConnected(-1,-1,-1);
183  n += isConnected( 0,-1,-1);
184  n += isConnected( 1,-1,-1);
185  //
186  n += isConnected(-1, 0,-1);
187  n += isConnected( 0, 0,-1);
188  n += isConnected( 1, 0,-1);
189  //
190  n += isConnected(-1, 1,-1);
191  n += isConnected( 0, 1,-1);
192  n += isConnected( 1, 1,-1);
193  //
194  n += isConnected(-1,-1, 1);
195  n += isConnected( 0,-1, 1);
196  n += isConnected( 1,-1, 1);
197  //
198  n += isConnected(-1, 0, 1);
199  n += isConnected( 0, 0, 1);
200  n += isConnected( 1, 0, 1);
201  //
202  n += isConnected(-1, 1, 1);
203  n += isConnected( 0, 1, 1);
204  n += isConnected( 1, 1, 1);
205 #endif
206  return n;
207  }
208 
210  uint32_t getValue() const noexcept
211  {
212  return flag;
213  }
214 
216  static constexpr EBCellFlag TheDefaultCell () { return EBCellFlag{default_value}; }
217 
218  // DO NOT test covered cells using cell == TheCoveredCell()
219  // Use cell.isCovered() instead.
220  // Each covered cell is covered in its own way.
222  static constexpr EBCellFlag TheCoveredCell () { return EBCellFlag{covered_value}; }
223 
224 private:
225 
227  static constexpr uint32_t one_lower_mask = 0x1f;
228  static constexpr uint32_t zero_lower_mask = ~0x1f;
229 
231  static constexpr uint32_t one_type_mask = 0x3;
232  static constexpr uint32_t zero_type_mask = ~0x3;
233 
235  static constexpr uint32_t one_numvofs_mask = 0x1c;
236  static constexpr uint32_t zero_numvofs_mask = ~0x1c;
237 
240  static constexpr uint32_t regular_bits = 0x0;
241  static constexpr uint32_t single_valued_bits = 0x1;
242  static constexpr uint32_t multi_valued_bits = 0x2;
243  static constexpr uint32_t covered_bits = 0x3;
244 
246  static constexpr uint32_t single_vof_bits = 0x4;
247 
252 
253  static constexpr int w_lower_mask = 5;
254  static constexpr int w_type = 2;
255  static constexpr int w_numvofs = 3;
256  static constexpr int pos_numvofs = 2;
257 #if 0
258  static constexpr std::array<std::array<std::array<int,3>,3>,3> pos_ngbr
259  {{ std::array<std::array<int,3>,3>{{{ w_lower_mask , w_lower_mask+ 1, w_lower_mask+ 2 },
260  { w_lower_mask+ 3, w_lower_mask+ 4, w_lower_mask+ 5 },
261  { w_lower_mask+ 6, w_lower_mask+ 7, w_lower_mask+ 8 }}},
262  std::array<std::array<int,3>,3>{{{ w_lower_mask+ 9, w_lower_mask+10, w_lower_mask+11 },
263  { w_lower_mask+12, w_lower_mask+13, w_lower_mask+14 },
264  { w_lower_mask+15, w_lower_mask+16, w_lower_mask+17 }}},
265  std::array<std::array<int,3>,3>{{{ w_lower_mask+18, w_lower_mask+19, w_lower_mask+20 },
266  { w_lower_mask+21, w_lower_mask+22, w_lower_mask+23 },
267  { w_lower_mask+24, w_lower_mask+25, w_lower_mask+26 }}} }};
268 #endif
269 
271 #if AMREX_SPACEDIM == 3
272  static constexpr uint32_t default_value = zero_lower_mask | regular_bits | single_vof_bits;
273 #else
274  static constexpr uint32_t default_value = 0x7fc004;
275 #endif
276 // static constexpr uint32_t covered_value = zero_lower_mask | covered_bits;
277  static constexpr uint32_t covered_value = 0x40003;
278 
279  uint32_t flag = default_value;
280 };
281 
282 template <>
283 struct IsStoreAtomic<EBCellFlag> : std::true_type {};
284 
285 class EBCellFlagFab final
286  : public BaseFab<EBCellFlag>
287 {
288 public:
289 
290  explicit EBCellFlagFab (Arena* ar) noexcept;
291 
292  explicit EBCellFlagFab (const Box& b, int ncomp, Arena* ar);
293 
294  explicit EBCellFlagFab (const Box& b,
295  int ncomp=1,
296  bool alloc=true,
297  bool shared=false,
298  Arena* ar = nullptr);
299 
300  EBCellFlagFab (const EBCellFlagFab& rhs, MakeType make_type, int scomp, int ncomp);
301 
302  EBCellFlagFab () = default;
303  EBCellFlagFab (EBCellFlagFab&& rhs) = default;
304  ~EBCellFlagFab () override = default;
305 
306  EBCellFlagFab (const EBCellFlagFab&) = delete;
309 
313  FabType getType () const noexcept { return m_type; }
314 
318  FabType getType (const Box& bx) const noexcept;
319 
323  void resetType (int ng);
324 
328  int getNumRegularCells (const Box& bx) const noexcept;
329 
333  int getNumCutCells (const Box& bx) const noexcept;
334 
338  int getNumCoveredCells (const Box& bx) const noexcept;
339 
340  void setType (FabType t) noexcept { m_type = t; }
341 
342  struct NumCells {
343  int nregular = 0;
344  int nsingle = 0;
345  int nmulti = 0;
346  int ncovered = 0;
348  };
349 
350 private:
352  mutable std::map<Box,NumCells> m_typemap;
353 };
354 
355 std::ostream& operator<< (std::ostream& os, const EBCellFlag& flag);
356 
357 }
358 
359 #endif
#define AMREX_ASSERT(EX)
Definition: AMReX_BLassert.H:38
#define AMREX_FORCE_INLINE
Definition: AMReX_Extension.H:119
#define AMREX_GPU_HOST_DEVICE
Definition: AMReX_GpuQualifiers.H:20
#define AMREX_D_TERM(a, b, c)
Definition: AMReX_SPACE.H:129
A virtual base class for objects that manage their own dynamic memory allocation.
Definition: AMReX_Arena.H:100
A FortranArrayBox(FAB)-like object.
Definition: AMReX_BaseFab.H:183
Definition: AMReX_EBCellFlag.H:287
int getNumRegularCells(const Box &bx) const noexcept
Returns the number of regular cells in the given Box.
Definition: AMReX_EBCellFlag.cpp:147
~EBCellFlagFab() override=default
void setType(FabType t) noexcept
Definition: AMReX_EBCellFlag.H:340
void resetType(int ng)
Reset FabType.
Definition: AMReX_EBCellFlag.cpp:132
FabType m_type
Definition: AMReX_EBCellFlag.H:351
std::map< Box, NumCells > m_typemap
Definition: AMReX_EBCellFlag.H:352
EBCellFlagFab(EBCellFlagFab &&rhs)=default
int getNumCutCells(const Box &bx) const noexcept
Returns the number of cut cells in the given Box.
Definition: AMReX_EBCellFlag.cpp:188
int getNumCoveredCells(const Box &bx) const noexcept
Returns the number of covered cells in the given Box.
Definition: AMReX_EBCellFlag.cpp:226
FabType getType() const noexcept
Returns FabType of the whole EBCellFlagFab.
Definition: AMReX_EBCellFlag.H:313
EBCellFlagFab & operator=(const EBCellFlagFab &)=delete
EBCellFlagFab(const EBCellFlagFab &)=delete
Definition: AMReX_EBCellFlag.H:17
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void setConnected(const IntVect &iv) noexcept
Definition: AMReX_EBCellFlag.H:155
constexpr EBCellFlag & operator=(const EBCellFlag &rhs) noexcept=default
static constexpr int w_numvofs
Definition: AMReX_EBCellFlag.H:255
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void setConnected() noexcept
Definition: AMReX_EBCellFlag.H:150
static constexpr uint32_t zero_lower_mask
Definition: AMReX_EBCellFlag.H:228
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void setConnected(int i, int j, int k) noexcept
Definition: AMReX_EBCellFlag.H:163
static constexpr int pos_numvofs
Definition: AMReX_EBCellFlag.H:256
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE uint32_t getValue() const noexcept
Definition: AMReX_EBCellFlag.H:210
static constexpr uint32_t one_type_mask
masks lowest 2 bits (i.e., bit number 0-1)
Definition: AMReX_EBCellFlag.H:231
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void setDisconnected() noexcept
Definition: AMReX_EBCellFlag.H:131
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void setRegular() noexcept
Definition: AMReX_EBCellFlag.H:38
static constexpr uint32_t covered_value
zero out all neighbors
Definition: AMReX_EBCellFlag.H:277
static constexpr uint32_t one_numvofs_mask
masks for bit number 2-4
Definition: AMReX_EBCellFlag.H:235
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool operator==(const EBCellFlag &a_input) const noexcept
Definition: AMReX_EBCellFlag.H:65
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool operator!=(const EBCellFlag &a_input) const noexcept
Definition: AMReX_EBCellFlag.H:71
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool isConnected(int i, int j, int k) const noexcept
Definition: AMReX_EBCellFlag.H:111
AMREX_GPU_HOST_DEVICE static constexpr AMREX_FORCE_INLINE EBCellFlag TheDefaultCell()
Definition: AMReX_EBCellFlag.H:216
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool isSingleValued() const noexcept
Definition: AMReX_EBCellFlag.H:88
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void setCovered() noexcept
Definition: AMReX_EBCellFlag.H:45
static constexpr uint32_t zero_numvofs_mask
Definition: AMReX_EBCellFlag.H:236
static constexpr int w_type
Definition: AMReX_EBCellFlag.H:254
constexpr EBCellFlag() noexcept=default
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool isMultiValued() const noexcept
Definition: AMReX_EBCellFlag.H:93
static constexpr uint32_t single_vof_bits
this represent single vof (regular is considered as single vof too)
Definition: AMReX_EBCellFlag.H:246
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void setMultiValued(int n) noexcept
Definition: AMReX_EBCellFlag.H:57
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int getNumVoFs() const noexcept
Definition: AMReX_EBCellFlag.H:77
static constexpr uint32_t multi_valued_bits
Definition: AMReX_EBCellFlag.H:242
static constexpr uint32_t default_value
regular connected with all neighbors
Definition: AMReX_EBCellFlag.H:274
static constexpr uint32_t regular_bits
Definition: AMReX_EBCellFlag.H:240
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool isConnected(const IntVect &iv) const noexcept
Definition: AMReX_EBCellFlag.H:103
static constexpr int w_lower_mask
Definition: AMReX_EBCellFlag.H:253
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void setDisconnected(const IntVect &iv) noexcept
Definition: AMReX_EBCellFlag.H:136
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool isDisconnected(int i, int j, int k) const noexcept
Definition: AMReX_EBCellFlag.H:125
uint32_t flag
Definition: AMReX_EBCellFlag.H:279
static constexpr uint32_t one_lower_mask
masks for lowest 5 bits (i.e., bit number 0-4)
Definition: AMReX_EBCellFlag.H:227
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void setDisconnected(int i, int j, int k) noexcept
Definition: AMReX_EBCellFlag.H:144
AMREX_GPU_HOST_DEVICE static constexpr AMREX_FORCE_INLINE EBCellFlag TheCoveredCell()
Definition: AMReX_EBCellFlag.H:222
static constexpr uint32_t zero_type_mask
Definition: AMReX_EBCellFlag.H:232
static constexpr uint32_t single_valued_bits
Definition: AMReX_EBCellFlag.H:241
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void setSingleValued() noexcept
Definition: AMReX_EBCellFlag.H:50
static constexpr uint32_t covered_bits
Definition: AMReX_EBCellFlag.H:243
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool isDisconnected(const IntVect &iv) const noexcept
Definition: AMReX_EBCellFlag.H:117
AMREX_GPU_HOST_DEVICE EBCellFlag & operator+=(const EBCellFlag &)
Definition: AMReX_EBCellFlag.H:32
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool isRegular() const noexcept
Definition: AMReX_EBCellFlag.H:83
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int numNeighbors() const noexcept
Definition: AMReX_EBCellFlag.H:169
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool isCovered() const noexcept
Definition: AMReX_EBCellFlag.H:98
integer, dimension(-1:1,-1:1,-1:1), parameter, public pos_ngbr
Definition: AMReX_ebcellflag_mod.F90:16
Definition: AMReX_Amr.cpp:49
MakeType
Definition: AMReX_MakeType.H:7
FabType
Definition: AMReX_FabFactory.H:18
void Abort(const std::string &msg)
Print out message to cerr and exit via abort().
Definition: AMReX.cpp:225
std::ostream & operator<<(std::ostream &os, AmrMesh const &amr_mesh)
Definition: AMReX_AmrMesh.cpp:1236
void * alloc(std::size_t sz) const noexcept
Definition: AMReX_DataAllocator.H:16
Definition: AMReX_EBCellFlag.H:342
FabType type
Definition: AMReX_EBCellFlag.H:347
int nmulti
Definition: AMReX_EBCellFlag.H:345
int nsingle
Definition: AMReX_EBCellFlag.H:344
int ncovered
Definition: AMReX_EBCellFlag.H:346
int nregular
Definition: AMReX_EBCellFlag.H:343
Definition: AMReX_TypeTraits.H:266