Block-Structured AMR Software Framework
AMReX_AmrMesh.H
Go to the documentation of this file.
1 #ifndef BL_AMRMESH_H_
2 #define BL_AMRMESH_H_
3 #include <AMReX_Config.H>
4 
5 #include <AMReX_Array.H>
6 #include <AMReX_Vector.H>
7 #include <AMReX_RealBox.H>
8 #include <AMReX_IntVect.H>
9 #include <AMReX_Geometry.H>
11 #include <AMReX_BoxArray.H>
12 #include <AMReX_TagBox.H>
13 
14 #ifdef AMREX_USE_BITTREE
15 #include <Bittree_BittreeAmr.h>
16 #endif
17 
18 namespace amrex {
19 
20 struct AmrInfo {
21  int verbose = 0;
23  int max_level = 0;
29 #if defined(AMREX_USE_GPU)
31 #else
33 #endif
37  Real grid_eff = static_cast<Real>(0.7);
39  int n_proper = 1;
41  bool use_fixed_coarse_grids = false;
42 
47  bool refine_grid_layout = true;
48 
54 
55  bool check_input = true;
56  bool use_new_chop = false;
57  bool iterate_on_new_grids = true;
58 };
59 
60 class AmrMesh
61  : protected AmrInfo
62 {
63 public:
64 
65  friend std::ostream& operator<< (std::ostream& os, AmrMesh const& amr_mesh);
66 
67  AmrMesh ();
68 
69  AmrMesh (const RealBox* rb, int max_level_in,
70  const Vector<int>& n_cell_in, int coord=-1,
72  const int* is_per = nullptr);
73 
74  AmrMesh (const RealBox& rb, int max_level_in,
75  const Vector<int>& n_cell_in, int coord,
76  Vector<IntVect> const& a_refrat,
77  Array<int,AMREX_SPACEDIM> const& is_per);
78 
79  AmrMesh (Geometry const& level_0_geom, AmrInfo const& amr_info);
80 
81  AmrMesh (const AmrMesh& rhs) = delete;
82  AmrMesh& operator= (const AmrMesh& rhs) = delete;
83 
84  AmrMesh (AmrMesh&& rhs) = default;
85  AmrMesh& operator= (AmrMesh&& rhs) = default;
86 
87  virtual ~AmrMesh () = default;
88 
89  [[nodiscard]] int Verbose () const noexcept { return verbose; }
90 
92  [[nodiscard]] int maxLevel () const noexcept { return max_level; }
93 
95  [[nodiscard]] int finestLevel () const noexcept { return finest_level; }
96 
98  [[nodiscard]] IntVect refRatio (int lev) const noexcept { return ref_ratio[lev]; }
99 
101  [[nodiscard]] int MaxRefRatio (int lev) const noexcept;
102 
104  [[nodiscard]] const Vector<IntVect>& refRatio () const noexcept { return ref_ratio; }
105 
106  [[nodiscard]] const Vector<Geometry>& Geom () const noexcept { return geom; }
107  [[nodiscard]] const Vector<DistributionMapping>& DistributionMap () const noexcept { return dmap; }
108  [[nodiscard]] const Vector<BoxArray>& boxArray () const noexcept { return grids; }
109 
110  [[nodiscard]] const Geometry& Geom (int lev) const noexcept { return geom[lev]; }
111  [[nodiscard]] const DistributionMapping& DistributionMap (int lev) const noexcept { return dmap[lev]; }
112  [[nodiscard]] const BoxArray& boxArray (int lev) const noexcept { return grids[lev]; }
113 
114  [[nodiscard]] Vector<Geometry> Geom (int a_coarsest_lev, int a_finest_lev) const noexcept {
116  r.reserve(a_finest_lev-a_coarsest_lev+1);
117  for (int lev = a_coarsest_lev; lev <= a_finest_lev; ++lev) {
118  r.push_back(geom[lev]);
119  }
120  return r;
121  }
122  [[nodiscard]] Vector<BoxArray> boxArray (int a_coarsest_lev, int a_finest_lev) const noexcept {
124  r.reserve(a_finest_lev-a_coarsest_lev+1);
125  for (int lev = a_coarsest_lev; lev <= a_finest_lev; ++lev) {
126  r.push_back(grids[lev]);
127  }
128  return r;
129  }
130  [[nodiscard]] Vector<DistributionMapping> DistributionMap (int a_coarsest_lev, int a_finest_lev) const noexcept {
132  r.reserve(a_finest_lev-a_coarsest_lev+1);
133  for (int lev = a_coarsest_lev; lev <= a_finest_lev; ++lev) {
134  r.push_back(dmap[lev]);
135  }
136  return r;
137  }
138 
139  Vector<Geometry>& Geom () noexcept { return geom; }
140  Geometry& Geom (int lev) noexcept { return geom[lev]; }
141 
142  void SetMaxGridSize (int new_mgs) noexcept {
143  max_grid_size.assign(max_level+1, IntVect{AMREX_D_DECL(new_mgs,new_mgs,new_mgs)});
144  }
145  void SetMaxGridSize (const IntVect& new_mgs) noexcept {
146  max_grid_size.assign(max_level+1, new_mgs);
147  }
148  void SetMaxGridSize (const Vector<int>& new_mgs) noexcept {
149  max_grid_size.resize(max_level+1);
150  for (int i = 0; i <= max_level; ++i) {
151  max_grid_size[i] = IntVect{AMREX_D_DECL(new_mgs[i],new_mgs[i],new_mgs[i])};
152  }
153  }
154  void SetMaxGridSize (const Vector<IntVect>& new_mgs) noexcept {
155  max_grid_size.assign(new_mgs.cbegin(), new_mgs.cbegin()+max_level+1);
156  }
157 
158  void SetBlockingFactor (int new_bf) noexcept {
159  blocking_factor.assign(max_level+1, IntVect{AMREX_D_DECL(new_bf,new_bf,new_bf)});
160  }
161  void SetBlockingFactor (const IntVect& new_bf) noexcept {
162  blocking_factor.assign(max_level+1, new_bf);
163  }
164  void SetBlockingFactor (const Vector<int>& new_bf) noexcept {
165  blocking_factor.resize(max_level+1);
166  for (int i = 0; i <= max_level; ++i) {
167  blocking_factor[i] = IntVect{AMREX_D_DECL(new_bf[i],new_bf[i],new_bf[i])};
168  }
169  }
170  void SetBlockingFactor (const Vector<IntVect>& new_bf) noexcept {
171  blocking_factor.assign(new_bf.cbegin(), new_bf.cend()+max_level+1);
172  }
173 
174  void SetGridEff (Real eff) noexcept { grid_eff = eff; }
175  void SetNProper (int n) noexcept { n_proper = n; }
176 
178 
179  void SetFinestLevel (int new_finest_level) noexcept { finest_level = new_finest_level; }
180  void SetDistributionMap (int lev, const DistributionMapping& dmap_in) noexcept;
181  void SetBoxArray (int lev, const BoxArray& ba_in) noexcept;
182  void SetGeometry (int lev, const Geometry& geom_in) noexcept;
183 
185  [[nodiscard]] int GetLevel (Box const& domain) const noexcept;
186 
187  void ClearDistributionMap (int lev) noexcept;
188  void ClearBoxArray (int lev) noexcept;
189 
191  [[nodiscard]] int nErrorBuf (int lev, int direction = 0) const noexcept { return n_error_buf[lev][direction]; }
192 
194  [[nodiscard]] const IntVect& nErrorBufVect (int lev) const noexcept { return n_error_buf[lev]; }
195 
197  [[nodiscard]] Real gridEff () const noexcept { return grid_eff; }
198 
200  [[nodiscard]] int nProper () const noexcept { return n_proper; }
201 
203  [[nodiscard]] const IntVect& blockingFactor (int lev) const noexcept { return blocking_factor[lev]; }
204 
206  [[nodiscard]] const IntVect& maxGridSize (int lev) const noexcept { return max_grid_size[lev]; }
207 
208  [[nodiscard]] bool LevelDefined (int lev) const noexcept;
209 
211  [[nodiscard]] bool useFixedCoarseGrids () const noexcept { return use_fixed_coarse_grids; }
212 
214  [[nodiscard]] int useFixedUpToLevel () const noexcept { return use_fixed_upto_level; }
215 
217  void ChopGrids (int lev, BoxArray& ba, int target_size) const;
218 
220  [[nodiscard]] BoxArray MakeBaseGrids () const;
221 
236  void MakeNewGrids (int lbase, Real time, int& new_finest, Vector<BoxArray>& new_grids);
237 
239  void MakeNewGrids (Real time = 0.0);
240 
244  virtual void MakeNewLevelFromScratch (int /*lev*/, Real /*time*/, const BoxArray& /*ba*/, const DistributionMapping& /*dm*/) {}
245 
247  virtual void ErrorEst (int /*lev*/, TagBoxArray& /*tags*/, Real /*time*/, int /*ngrow*/) {}
248 
250  virtual void ManualTagsPlacement (int /*lev*/, TagBoxArray& /*tags*/, const Vector<IntVect>& /*bf_lev*/) {}
251 
258  virtual void PostProcessBaseGrids(BoxArray& /*box_array*/) const {}
259 
260  [[nodiscard]] virtual BoxArray GetAreaNotToTag (int /*lev*/) { return BoxArray(); }
261 
262  [[nodiscard]] Long CountCells (int lev) const noexcept;
263 
264  [[nodiscard]] virtual DistributionMapping MakeDistributionMap (int lev, BoxArray const& ba);
265 
266 protected:
267 
272 
273 #ifdef AMREX_USE_BITTREE
274  bool use_bittree = false;
275  std::unique_ptr<bittree::BittreeAmr> btmesh;
276 #endif
277 
278  unsigned int num_setdm = 0;
279  unsigned int num_setba = 0;
280 
281  void checkInput();
282 
283  void SetIterateToFalse () noexcept { iterate_on_new_grids = false; }
284  void SetUseNewChop () noexcept { use_new_chop = true; }
285 
286 private:
287  void InitAmrMesh (int max_level_in, const Vector<int>& n_cell_in,
288  Vector<IntVect> refrat = Vector<IntVect>(),
289  const RealBox* rb = nullptr, int coord = -1,
290  const int* is_per = nullptr);
291 
292  static void ProjPeriodic (BoxList& blout, const Box& domain,
293  Array<int,AMREX_SPACEDIM> const& is_per);
294 };
295 
296 std::ostream& operator<< (std::ostream& os, AmrMesh const& amr_mesh);
297 
298 }
299 
300 #endif
#define AMREX_D_PICK(a, b, c)
Definition: AMReX_SPACE.H:151
#define AMREX_D_DECL(a, b, c)
Definition: AMReX_SPACE.H:104
Definition: AMReX_AmrMesh.H:62
void SetBlockingFactor(const Vector< IntVect > &new_bf) noexcept
Definition: AMReX_AmrMesh.H:170
const IntVect & nErrorBufVect(int lev) const noexcept
Return the number of buffer cells (as an IntVect) in error estimator.
Definition: AMReX_AmrMesh.H:194
const DistributionMapping & DistributionMap(int lev) const noexcept
Definition: AMReX_AmrMesh.H:111
AmrMesh()
Definition: AMReX_AmrMesh.cpp:18
Vector< Geometry > geom
Definition: AMReX_AmrMesh.H:269
Vector< Geometry > & Geom() noexcept
Definition: AMReX_AmrMesh.H:139
virtual void MakeNewLevelFromScratch(int, Real, const BoxArray &, const DistributionMapping &)
Definition: AMReX_AmrMesh.H:244
int maxLevel() const noexcept
Return the max level.
Definition: AMReX_AmrMesh.H:92
void SetIterateToFalse() noexcept
Definition: AMReX_AmrMesh.H:283
unsigned int num_setdm
Definition: AMReX_AmrMesh.H:278
const Vector< BoxArray > & boxArray() const noexcept
Definition: AMReX_AmrMesh.H:108
const IntVect & maxGridSize(int lev) const noexcept
Return the largest allowable grid.
Definition: AMReX_AmrMesh.H:206
void SetBlockingFactor(const Vector< int > &new_bf) noexcept
Definition: AMReX_AmrMesh.H:164
const Vector< Geometry > & Geom() const noexcept
Definition: AMReX_AmrMesh.H:106
const Vector< IntVect > & refRatio() const noexcept
Return refinement ratios between all levels.
Definition: AMReX_AmrMesh.H:104
void SetBoxArray(int lev, const BoxArray &ba_in) noexcept
Definition: AMReX_AmrMesh.cpp:406
AmrMesh(AmrMesh &&rhs)=default
unsigned int num_setba
Definition: AMReX_AmrMesh.H:279
void SetDistributionMap(int lev, const DistributionMapping &dmap_in) noexcept
Definition: AMReX_AmrMesh.cpp:399
Vector< DistributionMapping > DistributionMap(int a_coarsest_lev, int a_finest_lev) const noexcept
Definition: AMReX_AmrMesh.H:130
void SetMaxGridSize(int new_mgs) noexcept
Definition: AMReX_AmrMesh.H:142
bool LevelDefined(int lev) const noexcept
Definition: AMReX_AmrMesh.cpp:441
AmrMesh & operator=(const AmrMesh &rhs)=delete
void SetUseNewChop() noexcept
Definition: AMReX_AmrMesh.H:284
const BoxArray & boxArray(int lev) const noexcept
Definition: AMReX_AmrMesh.H:112
static void ProjPeriodic(BoxList &blout, const Box &domain, Array< int, AMREX_SPACEDIM > const &is_per)
Definition: AMReX_AmrMesh.cpp:1047
int useFixedUpToLevel() const noexcept
Up to what level should we keep the coarser grids fixed (and not regrid those levels)?
Definition: AMReX_AmrMesh.H:214
int finestLevel() const noexcept
Return the finest level.
Definition: AMReX_AmrMesh.H:95
int GetLevel(Box const &domain) const noexcept
Given domain box, return AMR level. Return -1 if there is no match.
Definition: AMReX_AmrMesh.cpp:419
void SetGridEff(Real eff) noexcept
Definition: AMReX_AmrMesh.H:174
int finest_level
Current finest level.
Definition: AMReX_AmrMesh.H:268
Real gridEff() const noexcept
Return the minimum allowable grid efficiency.
Definition: AMReX_AmrMesh.H:197
IntVect refRatio(int lev) const noexcept
Return the refinement ratio for level lev.
Definition: AMReX_AmrMesh.H:98
friend std::ostream & operator<<(std::ostream &os, AmrMesh const &amr_mesh)
Definition: AMReX_AmrMesh.cpp:1236
void ChopGrids(int lev, BoxArray &ba, int target_size) const
"Try" to chop up grids so that the number of boxes in the BoxArray is greater than the target_size.
Definition: AMReX_AmrMesh.cpp:467
virtual void ErrorEst(int, TagBoxArray &, Real, int)
Tag cells for refinement. TagBoxArray tags is built on level lev grids.
Definition: AMReX_AmrMesh.H:247
int MaxRefRatio(int lev) const noexcept
Return the maximum refinement ratio in any direction.
Definition: AMReX_AmrMesh.cpp:389
virtual ~AmrMesh()=default
const Vector< DistributionMapping > & DistributionMap() const noexcept
Definition: AMReX_AmrMesh.H:107
Geometry & Geom(int lev) noexcept
Definition: AMReX_AmrMesh.H:140
void checkInput()
Definition: AMReX_AmrMesh.cpp:1118
void SetMaxGridSize(const IntVect &new_mgs) noexcept
Definition: AMReX_AmrMesh.H:145
virtual BoxArray GetAreaNotToTag(int)
Definition: AMReX_AmrMesh.H:260
int Verbose() const noexcept
Definition: AMReX_AmrMesh.H:89
int nProper() const noexcept
Return the number of cells to define proper nesting.
Definition: AMReX_AmrMesh.H:200
AmrMesh(const AmrMesh &rhs)=delete
void SetGeometry(int lev, const Geometry &geom_in) noexcept
Definition: AMReX_AmrMesh.cpp:413
Vector< Geometry > Geom(int a_coarsest_lev, int a_finest_lev) const noexcept
Definition: AMReX_AmrMesh.H:114
void SetFinestLevel(int new_finest_level) noexcept
Set ref_ratio would require rebuilding Geometry objects.
Definition: AMReX_AmrMesh.H:179
void InitAmrMesh(int max_level_in, const Vector< int > &n_cell_in, Vector< IntVect > refrat=Vector< IntVect >(), const RealBox *rb=nullptr, int coord=-1, const int *is_per=nullptr)
Definition: AMReX_AmrMesh.cpp:75
Vector< DistributionMapping > dmap
Definition: AMReX_AmrMesh.H:270
void SetBlockingFactor(int new_bf) noexcept
Definition: AMReX_AmrMesh.H:158
virtual void ManualTagsPlacement(int, TagBoxArray &, const Vector< IntVect > &)
Manually tag. Note that tags is built on level lev grids coarsened by bf_lev[lev].
Definition: AMReX_AmrMesh.H:250
virtual DistributionMapping MakeDistributionMap(int lev, BoxArray const &ba)
Definition: AMReX_AmrMesh.cpp:447
BoxArray MakeBaseGrids() const
Make a level 0 grids covering the whole domain. It does NOT install the new grids.
Definition: AMReX_AmrMesh.cpp:521
void MakeNewGrids(int lbase, Real time, int &new_finest, Vector< BoxArray > &new_grids)
Make new grids based on error estimates. This function expects that valid BoxArrays exist in this->gr...
Definition: AMReX_AmrMesh.cpp:548
int nErrorBuf(int lev, int direction=0) const noexcept
Return the number of buffer cells (as a single integer) in error estimator.
Definition: AMReX_AmrMesh.H:191
virtual void PostProcessBaseGrids(BoxArray &) const
Definition: AMReX_AmrMesh.H:258
void SetMaxGridSize(const Vector< IntVect > &new_mgs) noexcept
Definition: AMReX_AmrMesh.H:154
Long CountCells(int lev) const noexcept
Definition: AMReX_AmrMesh.cpp:1231
void SetBlockingFactor(const IntVect &new_bf) noexcept
Definition: AMReX_AmrMesh.H:161
const Geometry & Geom(int lev) const noexcept
Definition: AMReX_AmrMesh.H:110
void SetMaxGridSize(const Vector< int > &new_mgs) noexcept
Definition: AMReX_AmrMesh.H:148
Vector< BoxArray > grids
Definition: AMReX_AmrMesh.H:271
void SetNProper(int n) noexcept
Definition: AMReX_AmrMesh.H:175
bool useFixedCoarseGrids() const noexcept
Should we keep the coarser grids fixed (and not regrid those levels) at all?
Definition: AMReX_AmrMesh.H:211
Vector< BoxArray > boxArray(int a_coarsest_lev, int a_finest_lev) const noexcept
Definition: AMReX_AmrMesh.H:122
const IntVect & blockingFactor(int lev) const noexcept
Return the blocking factor at level lev.
Definition: AMReX_AmrMesh.H:203
void ClearDistributionMap(int lev) noexcept
Definition: AMReX_AmrMesh.cpp:429
void ClearBoxArray(int lev) noexcept
Definition: AMReX_AmrMesh.cpp:435
A collection of Boxes stored in an Array.
Definition: AMReX_BoxArray.H:550
A class for managing a List of Boxes that share a common IndexType. This class implements operations ...
Definition: AMReX_BoxList.H:52
Calculates the distribution of FABs to MPI processes.
Definition: AMReX_DistributionMapping.H:41
Rectangular problem domain geometry.
Definition: AMReX_Geometry.H:73
A Box with real dimensions. A RealBox is OK iff volume >= 0.
Definition: AMReX_RealBox.H:21
An array of TagBoxes.
Definition: AMReX_TagBox.H:150
This class is a thin wrapper around std::vector. Unlike vector, Vector::operator[] provides bound che...
Definition: AMReX_Vector.H:27
Definition: AMReX_Amr.cpp:49
IntVectND< AMREX_SPACEDIM > IntVect
Definition: AMReX_BaseFwd.H:30
std::ostream & operator<<(std::ostream &os, AmrMesh const &amr_mesh)
Definition: AMReX_AmrMesh.cpp:1236
std::array< T, N > Array
Definition: AMReX_Array.H:24
Definition: AMReX_AmrMesh.H:20
int n_proper
Cells required for proper nesting.
Definition: AMReX_AmrMesh.H:39
bool refine_grid_layout
Definition: AMReX_AmrMesh.H:47
bool use_fixed_coarse_grids
Definition: AMReX_AmrMesh.H:41
Vector< IntVect > ref_ratio
Refinement ratios.
Definition: AMReX_AmrMesh.H:25
bool use_new_chop
Definition: AMReX_AmrMesh.H:56
Real grid_eff
Grid efficiency.
Definition: AMReX_AmrMesh.H:37
bool iterate_on_new_grids
Definition: AMReX_AmrMesh.H:57
bool check_input
Definition: AMReX_AmrMesh.H:55
Vector< IntVect > n_error_buf
Buffer cells around each tagged cell.
Definition: AMReX_AmrMesh.H:35
int max_level
Maximum allowed level.
Definition: AMReX_AmrMesh.H:23
IntVect refine_grid_layout_dims
Definition: AMReX_AmrMesh.H:53
Vector< IntVect > max_grid_size
Maximum allowable grid size (by level).
Definition: AMReX_AmrMesh.H:30
int use_fixed_upto_level
Definition: AMReX_AmrMesh.H:40
int verbose
Definition: AMReX_AmrMesh.H:21
Vector< IntVect > blocking_factor
Blocking factor in grid generation (by level).
Definition: AMReX_AmrMesh.H:27