ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_EB.H
Go to the documentation of this file.
1 /**
2  * \file ERF_EB.H
3  * \brief Declares the embedded-boundary factory manager used by ERF levels.
4  */
5 #ifndef ERF_EB_H_
6 #define ERF_EB_H_
7 
8 #include <AMReX_Geometry.H>
9 #include <AMReX_DistributionMapping.H>
10 #include <AMReX_BoxArray.H>
11 
12 #include <AMReX_EB2.H>
13 #include <AMReX_EBFabFactory.H>
14 #include <AMReX_Dim3.H>
15 #include <AMReX_GpuContainers.H>
16 
17 #include <ERF_EBAux.H>
18 
19 /**
20  * \brief Owns and exposes cell-centered and face-centered EB factories.
21  *
22  * The class builds AMReX EB factories for cell-centered state data and, depending
23  * on USE_FC_FACTORY, either native AMReX or ERF auxiliary factories for staggered
24  * velocity components.
25  */
26 class eb_ {
27 
28  public:
29 
30  using CutCellList = amrex::Gpu::DeviceVector<amrex::Dim3>;
31 
32  //! Destroy the EB factory manager.
33  ~eb_ ();
34 
35  //! Construct an empty EB factory manager.
36  eb_ ();
37 
38  /**
39  * \brief Build cell-centered and staggered EB factories for the level.
40  * \param level AMR level index.
41  * \param a_geom Geometry for the level.
42  * \param ba BoxArray over which factories are defined.
43  * \param dm DistributionMapping for the BoxArray.
44  * \param a_eb_level AMReX EB level used to populate factory data.
45  * \param cover_zero_area_faces Cover FC faces with zero CC area fraction.
46  */
47  void make_all_factories ( int level,
48  amrex::Geometry const& a_geom,
49  amrex::BoxArray const& ba,
50  amrex::DistributionMapping const& dm,
51  amrex::EB2::Level const& a_eb_level,
52  bool cover_zero_area_faces = false);
53 
54  /**
55  * \brief Build only the cell-centered EB factory for the level.
56  * \param level AMR level index.
57  * \param a_geom Geometry for the level.
58  * \param ba BoxArray over which the factory is defined.
59  * \param dm DistributionMapping for the BoxArray.
60  * \param a_eb_level AMReX EB level used to populate factory data.
61  */
62  void make_cc_factory ( int level,
63  amrex::Geometry const& a_geom,
64  amrex::BoxArray const& ba,
65  amrex::DistributionMapping const& dm,
66  amrex::EB2::Level const& a_eb_level);
67 
68  /**
69  * \brief Release the factories and cut-face lists built for the level's grids, as for a
70  * level that no longer exists. The object itself is kept: other objects hold pointers
71  * to it, and the factories are built again when the level is made again.
72  */
73  void clear_factories ();
74 
75  //! Number of ghost cells needed for basic EB flags.
76  int nghost_basic () const { return 5; } // nghost_eb_basic ()
77  //! Number of ghost cells needed for EB volume data.
78  int nghost_volume () const { return 5; } // nghost_eb_volume ()
79  //! Number of ghost cells needed for full EB geometric data.
80  int nghost_full () const { return 4; } // nghost_eb_full ()
81 
82  // Toggle between eb_aux_ and native AMReX FC factories
83  #ifndef USE_FC_FACTORY
84  /**
85  * \def USE_FC_FACTORY
86  * \brief Select native AMReX face-centered factories instead of ERF auxiliary factories.
87  */
88  #define USE_FC_FACTORY 1 // Default to 1: use amrex native FC factory
89  #endif
90 
91  //! Return cut x-faces owned by a grid, cached when the EB factories are built.
92  [[nodiscard]] CutCellList const& get_u_cut_cells (int grid_index) const noexcept
93  {
94  return m_u_cut_cells[grid_index];
95  }
96 
97  //! Return cut y-faces owned by a grid, cached when the EB factories are built.
98  [[nodiscard]] CutCellList const& get_v_cut_cells (int grid_index) const noexcept
99  {
100  return m_v_cut_cells[grid_index];
101  }
102 
103  //! Return cut z-faces owned by a grid, cached when the EB factories are built.
104  [[nodiscard]] CutCellList const& get_w_cut_cells (int grid_index) const noexcept
105  {
106  return m_w_cut_cells[grid_index];
107  }
108 
109  //! Return the cell-centered EB factory.
110  const std::unique_ptr<amrex::EBFArrayBoxFactory>& get_const_factory () const noexcept { return m_factory; }
111 
112  /**
113  * \brief Disconnect covered neighbors in an EB factory's cell flags.
114  * \param factory Factory whose non-const flag data are updated.
115  */
116  void set_connection_flags (amrex::EBFArrayBoxFactory* factory);
117 
118  /**
119  * \brief Enforce FC-CC consistency by marking FC faces as covered when both CC neighbors are covered.
120  * \param fc_factory Face-centered factory to modify (non-const)
121  * \param cc_factory Cell-centered factory to read neighbor states (const)
122  * \param idim Direction of face centering (0=x, 1=y, 2=z)
123  * \param cover_zero_area_faces Also cover faces with zero CC area fraction
124  */
125  void mask_fc_from_cc (amrex::EBFArrayBoxFactory* fc_factory,
126  const amrex::EBFArrayBoxFactory* cc_factory,
127  int idim,
128  bool cover_zero_area_faces);
129 
130  #if USE_FC_FACTORY
131  //! Return the native AMReX x-face EB factory.
132  amrex::EBFArrayBoxFactory const* get_u_const_factory () const noexcept { return m_u_factory_fc.get(); }
133  //! Return the native AMReX y-face EB factory.
134  amrex::EBFArrayBoxFactory const* get_v_const_factory () const noexcept { return m_v_factory_fc.get(); }
135  //! Return the native AMReX z-face EB factory.
136  amrex::EBFArrayBoxFactory const* get_w_const_factory () const noexcept { return m_w_factory_fc.get(); }
137  #else
138  //! Return the ERF auxiliary x-face EB factory.
139  eb_aux_ const* get_u_const_factory () const noexcept { return &m_u_factory; }
140  //! Return the ERF auxiliary y-face EB factory.
141  eb_aux_ const* get_v_const_factory () const noexcept { return &m_v_factory; }
142  //! Return the ERF auxiliary z-face EB factory.
143  eb_aux_ const* get_w_const_factory () const noexcept { return &m_w_factory; }
144  #endif
145 
146  //! Forward declaration for EB surface output.
147  class EBToPVD;
148 
149  private:
150 
151  int m_has_eb;
152  std::string m_type;
153 
154  amrex::EBSupport m_support_level;
155 
157 
158  amrex::FabArray<amrex::EBCellFlagFab>* m_cellflags = nullptr;
159 
160  //! EB level constructed from building GeometryShop
161  amrex::EB2::Level const* m_eb_level;
162 
163  std::unique_ptr<amrex::EBFArrayBoxFactory> m_factory = nullptr;
164 
165  // Original eb_aux_ factories (USE_FC_FACTORY=0)
169 
170  // New native AMReX FC factories (USE_FC_FACTORY=1)
171  std::unique_ptr<amrex::EBFArrayBoxFactory> m_u_factory_fc = nullptr;
172  std::unique_ptr<amrex::EBFArrayBoxFactory> m_v_factory_fc = nullptr;
173  std::unique_ptr<amrex::EBFArrayBoxFactory> m_w_factory_fc = nullptr;
174 
175  // Persistent per-grid cut-face lists for sparse GPU wall-correction launches.
176  amrex::Vector<CutCellList> m_u_cut_cells;
177  amrex::Vector<CutCellList> m_v_cut_cells;
178  amrex::Vector<CutCellList> m_w_cut_cells;
179 
180  void build_cut_cell_lists (
181  amrex::FabArray<amrex::EBCellFlagFab> const& flags,
182  amrex::Vector<CutCellList>& cut_cells);
183 
184  /**
185  * \brief Build terrain EB geometry for the supplied Geometry.
186  * \param a_geom Geometry used to define the EB index space.
187  */
188  void make_terrain (amrex::Geometry const& a_geom);
189 
190  /**
191  * \brief Construct EB levels from an AMReX GeometryShop.
192  * \tparam F Implicit-function type stored by the GeometryShop.
193  * \param a_geom Geometry used to build the EB index space.
194  * \param a_gshop GeometryShop describing the implicit EB.
195  */
196  template<class F>
197  void build_level (amrex::Geometry const& a_geom,
198  amrex::EB2::GeometryShop<F> a_gshop)
199  {
200  int const req_lev(0);
201  int const max_lev(2);
202 
203  amrex::EB2::Build(a_gshop, a_geom, req_lev, max_lev);
204  const amrex::EB2::IndexSpace& ebis = amrex::EB2::IndexSpace::top();
205  m_eb_level = &(ebis.getLevel(a_geom));
206  }
207 
208  //! Return mutable EB cell flags from an AMReX factory.
209  inline amrex::FabArray<amrex::EBCellFlagFab>&
210  getNonConstEBCellFlags (const amrex::EBFArrayBoxFactory& ebfact)
211  {
212  const amrex::FabArray<amrex::EBCellFlagFab>& flags_const = ebfact.getMultiEBCellFlagFab();
213  return const_cast<amrex::FabArray<amrex::EBCellFlagFab>&>(flags_const);
214  }
215 
216  //! Return mutable volume fractions from an AMReX factory.
217  inline amrex::MultiFab&
218  getNonConstVolFrac (const amrex::EBFArrayBoxFactory& ebfact)
219  {
220  const amrex::MultiFab& vfrac_const = ebfact.getVolFrac();
221  return const_cast<amrex::MultiFab&>(vfrac_const);
222  }
223 
224  //! Return mutable cell centroids from an AMReX factory.
225  inline amrex::MultiCutFab&
226  getNonConstCentroid (const amrex::EBFArrayBoxFactory& ebfact)
227  {
228  const amrex::MultiCutFab& vcent_const = ebfact.getCentroid();
229  return const_cast<amrex::MultiCutFab&>(vcent_const);
230  }
231 
232  //! Return mutable area-fraction arrays from an AMReX factory.
233  inline amrex::Array<amrex::MultiCutFab*, AMREX_SPACEDIM>
234  getNonConstAreaFrac (const amrex::EBFArrayBoxFactory& ebfact)
235  {
236  auto afrac_const = ebfact.getAreaFrac();
237  amrex::Array<amrex::MultiCutFab*, AMREX_SPACEDIM> afrac;
238  for (int dir = 0; dir < AMREX_SPACEDIM; ++dir) {
239  afrac[dir] = const_cast<amrex::MultiCutFab*>(afrac_const[dir]);
240  }
241  return afrac;
242  }
243 
244  //! Return mutable face-centroid arrays from an AMReX factory.
245  inline amrex::Array<amrex::MultiCutFab*, AMREX_SPACEDIM>
246  getNonConstFaceCent (const amrex::EBFArrayBoxFactory& ebfact)
247  {
248  auto fcent_const = ebfact.getFaceCent();
249  amrex::Array<amrex::MultiCutFab*, AMREX_SPACEDIM> fcent;
250  for (int dir = 0; dir < AMREX_SPACEDIM; ++dir) {
251  fcent[dir] = const_cast<amrex::MultiCutFab*>(fcent_const[dir]);
252  }
253  return fcent;
254  }
255 
256 };
257 #endif
Declares auxiliary EB factories for face-centered data.
Owns and exposes cell-centered and face-centered EB factories.
Definition: ERF_EB.H:26
amrex::FabArray< amrex::EBCellFlagFab > & getNonConstEBCellFlags(const amrex::EBFArrayBoxFactory &ebfact)
Return mutable EB cell flags from an AMReX factory.
Definition: ERF_EB.H:210
CutCellList const & get_u_cut_cells(int grid_index) const noexcept
Return cut x-faces owned by a grid, cached when the EB factories are built.
Definition: ERF_EB.H:92
eb_()
Construct an empty EB factory manager.
Definition: ERF_EB.cpp:61
amrex::Vector< CutCellList > m_w_cut_cells
Definition: ERF_EB.H:178
void make_terrain(amrex::Geometry const &a_geom)
Build terrain EB geometry for the supplied Geometry.
CutCellList const & get_v_cut_cells(int grid_index) const noexcept
Return cut y-faces owned by a grid, cached when the EB factories are built.
Definition: ERF_EB.H:98
void build_level(amrex::Geometry const &a_geom, amrex::EB2::GeometryShop< F > a_gshop)
Construct EB levels from an AMReX GeometryShop.
Definition: ERF_EB.H:197
amrex::Array< amrex::MultiCutFab *, AMREX_SPACEDIM > getNonConstAreaFrac(const amrex::EBFArrayBoxFactory &ebfact)
Return mutable area-fraction arrays from an AMReX factory.
Definition: ERF_EB.H:234
void mask_fc_from_cc(amrex::EBFArrayBoxFactory *fc_factory, const amrex::EBFArrayBoxFactory *cc_factory, int idim, bool cover_zero_area_faces)
Enforce FC-CC consistency by marking FC faces as covered when both CC neighbors are covered.
Definition: ERF_EB.cpp:284
CutCellList const & get_w_cut_cells(int grid_index) const noexcept
Return cut z-faces owned by a grid, cached when the EB factories are built.
Definition: ERF_EB.H:104
void build_cut_cell_lists(amrex::FabArray< amrex::EBCellFlagFab > const &flags, amrex::Vector< CutCellList > &cut_cells)
Definition: ERF_EB.cpp:202
amrex::Vector< CutCellList > m_v_cut_cells
Definition: ERF_EB.H:177
amrex::EB2::Level const * m_eb_level
EB level constructed from building GeometryShop.
Definition: ERF_EB.H:161
amrex::Vector< CutCellList > m_u_cut_cells
Definition: ERF_EB.H:176
amrex::FabArray< amrex::EBCellFlagFab > * m_cellflags
Definition: ERF_EB.H:158
const std::unique_ptr< amrex::EBFArrayBoxFactory > & get_const_factory() const noexcept
Return the cell-centered EB factory.
Definition: ERF_EB.H:110
amrex::MultiFab & getNonConstVolFrac(const amrex::EBFArrayBoxFactory &ebfact)
Return mutable volume fractions from an AMReX factory.
Definition: ERF_EB.H:218
eb_aux_ m_w_factory
Definition: ERF_EB.H:168
std::unique_ptr< amrex::EBFArrayBoxFactory > m_factory
Definition: ERF_EB.H:163
amrex::EBSupport m_support_level
Definition: ERF_EB.H:154
amrex::EBFArrayBoxFactory const * get_w_const_factory() const noexcept
Return the native AMReX z-face EB factory.
Definition: ERF_EB.H:136
amrex::EBFArrayBoxFactory const * get_u_const_factory() const noexcept
Return the native AMReX x-face EB factory.
Definition: ERF_EB.H:132
int nghost_volume() const
Number of ghost cells needed for EB volume data.
Definition: ERF_EB.H:78
amrex::Array< amrex::MultiCutFab *, AMREX_SPACEDIM > getNonConstFaceCent(const amrex::EBFArrayBoxFactory &ebfact)
Return mutable face-centroid arrays from an AMReX factory.
Definition: ERF_EB.H:246
void make_cc_factory(int level, amrex::Geometry const &a_geom, amrex::BoxArray const &ba, amrex::DistributionMapping const &dm, amrex::EB2::Level const &a_eb_level)
Build only the cell-centered EB factory for the level.
Definition: ERF_EB.cpp:181
amrex::Gpu::DeviceVector< amrex::Dim3 > CutCellList
Definition: ERF_EB.H:30
amrex::EBFArrayBoxFactory const * get_v_const_factory() const noexcept
Return the native AMReX y-face EB factory.
Definition: ERF_EB.H:134
amrex::MultiCutFab & getNonConstCentroid(const amrex::EBFArrayBoxFactory &ebfact)
Return mutable cell centroids from an AMReX factory.
Definition: ERF_EB.H:226
eb_aux_ m_u_factory
Definition: ERF_EB.H:166
void clear_factories()
Release the factories and cut-face lists built for the level's grids, as for a level that no longer e...
Definition: ERF_EB.cpp:163
~eb_()
Destroy the EB factory manager.
Definition: ERF_EB.cpp:56
void make_all_factories(int level, amrex::Geometry const &a_geom, amrex::BoxArray const &ba, amrex::DistributionMapping const &dm, amrex::EB2::Level const &a_eb_level, bool cover_zero_area_faces=false)
Build cell-centered and staggered EB factories for the level.
Definition: ERF_EB.cpp:68
std::unique_ptr< amrex::EBFArrayBoxFactory > m_w_factory_fc
Definition: ERF_EB.H:173
eb_aux_ m_v_factory
Definition: ERF_EB.H:167
std::unique_ptr< amrex::EBFArrayBoxFactory > m_v_factory_fc
Definition: ERF_EB.H:172
int m_has_eb
Definition: ERF_EB.H:147
int nghost_full() const
Number of ghost cells needed for full EB geometric data.
Definition: ERF_EB.H:80
int nghost_basic() const
Number of ghost cells needed for basic EB flags.
Definition: ERF_EB.H:76
int m_write_eb_surface
Definition: ERF_EB.H:156
std::unique_ptr< amrex::EBFArrayBoxFactory > m_u_factory_fc
Definition: ERF_EB.H:171
std::string m_type
Definition: ERF_EB.H:152
void set_connection_flags(amrex::EBFArrayBoxFactory *factory)
Disconnect covered neighbors in an EB factory's cell flags.
Definition: ERF_EB.cpp:239
Stores face-centered EB geometry reconstructed from a cell-centered factory.
Definition: ERF_EBAux.H:24