ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_PlanarBoundary.H
Go to the documentation of this file.
1 #ifndef ERF_PLANAR_BOUNDARY_H_
2 #define ERF_PLANAR_BOUNDARY_H_
3 
4 #include <AMReX_BoxArray.H>
5 #include <AMReX_DistributionMapping.H>
6 #include <AMReX_Geometry.H>
7 #include <AMReX_GpuQualifiers.H>
8 #include <AMReX_IndexType.H>
9 #include <AMReX_MultiFab.H>
10 #include <AMReX_Periodicity.H>
11 #include <AMReX_Vector.H>
12 
13 #include <algorithm>
14 #include <memory>
15 
16 /**
17  * Ghost-cell fill for planar (2D) MultiFabs built on the collapse of a 3D BoxArray
18  * along a surface-normal direction.
19  *
20  * The surface-layer fields and the MOST averages are defined on one 2D box per 3D box.
21  * When the 3D BoxArray is split in the normal direction, several 3D boxes collapse onto
22  * the same 2D box, so the planar BoxArray holds duplicate boxes with overlapping valid
23  * regions, and only the copy that belongs to the 3D box touching the surface is ever
24  * computed. A FillBoundary on such a MultiFab may fill a ghost cell from an
25  * uncomputed copy.
26  *
27  * define() records which planar boxes are those surface copies. fill() copies them into
28  * a buffer without duplicates and ParallelCopies the buffer onto every planar box, valid
29  * region and ghost cells alike. Unlike FillBoundary it therefore also overwrites the
30  * valid region of the uncomputed copies, so that any copy can be read afterwards. Without
31  * duplicates fill() is a plain FillBoundary.
32  *
33  * The buffer is kept for each target layout, index type, and number of components, so only
34  * the first fill of each kind allocates. For a face-centered MultiFab (the MOST velocity averages)
35  * neighboring surface boxes share a face in the buffer. Both should hold the same value
36  * there, but nothing enforces it, so fill() gives the shared faces a single value before
37  * it copies: the lowest global box index wins, which does not depend on the decomposition
38  * or the rank count, and every copy of the field then agrees.
39  *
40  * A level none of whose boxes reaches the surface (a fine level away from the surface) has
41  * no computed copy to fill from, and fill() leaves the MultiFab as it is.
42  */
44 {
45 public:
46  /**
47  * Record the surface copies of a planar BoxArray.
48  *
49  * @param[in] ba3d 3D BoxArray the planar BoxArray was collapsed from
50  * @param[in] ba2d planar BoxArray, one box per box of ba3d and in the same order
51  * @param[in] dm DistributionMapping shared by ba3d and ba2d
52  * @param[in] surface_index index of the surface cell in the normal direction
53  * @param[in] is_low whether the surface is the low side of the domain
54  * @param[in] normal_dir normal direction of the surface
55  */
56  void define (const amrex::BoxArray& ba3d,
57  const amrex::BoxArray& ba2d,
58  const amrex::DistributionMapping& dm,
59  int surface_index,
60  bool is_low = true,
61  int normal_dir = 2);
62 
63  /**
64  * Fill every copy of a planar MultiFab, valid region and ghost cells, from the
65  * surface copies.
66  *
67  * @param[in,out] mf planar MultiFab on ba2d, of any index type and number of components
68  * @param[in] period periodicity of the level
69  */
70  void fill (amrex::MultiFab& mf, const amrex::Periodicity& period);
71 
72  /**
73  * Give every copy of a ghost cell outside a non-periodic lateral domain edge the value
74  * of the surface copy that holds the column it faces.
75  *
76  * Where a box seam meets a non-periodic edge, the ghost cells beyond the edge next to
77  * the seam lie in the grown boxes of both neighbors, and no FillBoundary reconciles
78  * them: each box fills its copy from its own valid region. A cell beyond the edge is
79  * here assigned to the surface copy whose valid region holds the cell's projection into
80  * the domain (its index clamped into the domain in each non-periodic direction), and that
81  * copy's value is written into every other copy, periodic images included. The result
82  * then does not depend on the decomposition, and a checkpoint, which keeps one copy,
83  * restarts from the same values. Only ghost cells beyond a non-periodic edge are
84  * written; interior, periodic and coarse/fine ghost cells are left as they are.
85  *
86  * On a fine level the column a cell beyond the edge faces need not be covered by any
87  * grid: where a coarse/fine boundary meets the domain edge, or two grids along the
88  * edge are separated by a gap narrower than twice the ghost width. Such a cell has no
89  * owner by projection, and it takes instead the value of the surface copy with the
90  * lowest global box index whose ghost region holds it (OverrideSync's precedence in
91  * fill()), so it too has one value that does not depend on the decomposition. Only
92  * surface copies take part: a cell held solely by the ghost region of an uncomputed
93  * copy (a 3D box above the surface whose footprint differs from those below it) is
94  * left as it is.
95  *
96  * Call it after fill(), on a cell-centered planar MultiFab; other index types are left
97  * as they are.
98  *
99  * @param[in,out] mf planar MultiFab on the planar BoxArray given to define
100  * @param[in] geom geometry of the level
101  */
102  void reconcile_outside_domain (amrex::MultiFab& mf, const amrex::Geometry& geom);
103 
104  /**
105  * Copy the computed surface copies of a planar MultiFab into a MultiFab without
106  * duplicates.
107  *
108  * define() gives each surface copy the rank of the planar box it is a copy of, so every
109  * box of dst is filled from a FAB of mf that lives on the same rank and the gather needs
110  * no communication. Reading the surface copies directly, rather than any copy of the
111  * field, means the caller need not have filled the duplicates with fill().
112  *
113  * @param[in] mf planar MultiFab on the planar BoxArray given to define
114  * @param[out] dst MultiFab on surface_boxes() converted to the index type of mf, and on
115  * surface_dm()
116  * @param[in] scomp first component to read from mf
117  * @param[in] dcomp first component to write in dst
118  * @param[in] ncomp number of components
119  */
120  void gather_surface (const amrex::MultiFab& mf, amrex::MultiFab& dst,
121  int scomp, int dcomp, int ncomp) const;
122 
123  //! Cell-centered surface copies of the planar boxes, without duplicates
124  [[nodiscard]] const amrex::BoxArray& surface_boxes () const { return m_ba_sfc; }
125 
126  //! Ranks owning the surface copies: each is on the rank of the planar box it is a copy of
127  [[nodiscard]] const amrex::DistributionMapping& surface_dm () const { return m_dm_sfc; }
128 
129  //! Index of each surface copy in the planar BoxArray
130  [[nodiscard]] const amrex::Vector<int>& surface_index () const { return m_src_index; }
131 
132  //! Whether a planar-box index is the computed copy that touches the surface
133  [[nodiscard]] AMREX_GPU_HOST
134  bool is_surface_copy (const int planar_index) const
135  {
136  return std::binary_search(m_src_index.begin(), m_src_index.end(), planar_index);
137  }
138 
139 private:
140  amrex::MultiFab& buffer (const amrex::MultiFab& mf);
141 
142  struct Buffer
143  {
144  amrex::IndexType ixtype;
145  int ncomp;
146  amrex::BoxArray ba;
147  amrex::DistributionMapping dm;
148  amrex::Vector<const amrex::MultiFab*> sources;
149  std::unique_ptr<amrex::MultiFab> mf;
150  };
151 
152  // The strips beyond the non-periodic domain edges that each surface copy owns, for
153  // one planar layout (BoxArray and DistributionMapping) and number of lateral ghost cells
154  struct EdgeStrips
155  {
156  amrex::BoxArray planar_ba;
157  amrex::DistributionMapping planar_dm;
158  amrex::IntVect ngrow;
159  amrex::BoxArray ba; // the strips, disjoint, inside the domain in periodic directions
160  amrex::DistributionMapping dm; // each on the rank of its owner
161  amrex::Vector<int> owner; // planar index of the surface copy owning each strip
162  amrex::Vector<amrex::IntVect> shift; // owner's cells are at the strip's shifted by this
163  amrex::Vector<std::unique_ptr<amrex::MultiFab>> bufs; // copy buffers, one per ncomp
164  };
165 
166  EdgeStrips& edge_strips (const amrex::MultiFab& mf, const amrex::Geometry& geom);
167 
168  int m_nplanar{0}; // Number of boxes in the planar BoxArray
169  amrex::BoxArray m_ba_sfc; // Surface copies
170  amrex::DistributionMapping m_dm_sfc; // Ranks owning them
171  amrex::Vector<int> m_src_index; // Their index in the planar BoxArray
172  amrex::Vector<Buffer> m_buffers; // Gather buffers
173  amrex::Vector<EdgeStrips> m_strips; // Edge strips, per planar layout and ghost width
174 };
175 
176 /**
177  * The ground plane of a field on a level's 3D grids, on one planar box per 3D box.
178  *
179  * Box ib of the result is the footprint of box ib of src, cut down to the plane k = klo in
180  * the index type of src (so a nodal-in-z field keeps the nodes klo and klo+1), on the rank
181  * of box ib. It therefore lines up, box for box, with any MultiFab on the collapsed
182  * BoxArray (ba2d) and the level's DistributionMapping, and a kernel that reads src at klo
183  * can read it instead from any box, including one that starts above the ground.
184  *
185  * Each planar box first takes the lowest plane of its own 3D box, moved down to klo; the
186  * ground plane is then copied in from the boxes that hold it. Where no box of the level
187  * reaches the ground (a refined level aloft), the planar box keeps that lowest plane: a
188  * finite stand-in, not the ground, which callers must not report as the ground.
189  *
190  * @param[in] src field on the level's 3D grids (any index type, any number of components)
191  * @param[in] klo index of the lowest cell of the domain
192  */
193 std::unique_ptr<amrex::MultiFab>
194 copy_ground_plane (const amrex::MultiFab& src, int klo);
195 
196 /**
197  * Finish a column sum computed box by box on a planar MultiFab.
198  *
199  * A column sum over a level's 3D grids, accumulated into the planar box of each 3D box,
200  * holds in each planar box only the part of the column inside its own 3D box. When the
201  * 3D BoxArray is split in z, several planar boxes cover one column; this adds their parts
202  * and gives every one of them the whole sum. A planar BoxArray without overlapping boxes
203  * is left as it is.
204  *
205  * @param[in,out] mf planar MultiFab, one box per 3D box, cell-centered, no ghost cells used
206  * @param[in] comp first component holding a column sum
207  * @param[in] ncomp number of components
208  */
209 void
210 sum_over_stacked_boxes (amrex::MultiFab& mf, int comp, int ncomp);
211 
212 #endif
GpuArray< Real, AMREX_SPACEDIM > period
Definition: ERF_ActuatorSpreading.cpp:131
const int klo
Definition: ERF_InitCustomPert_KE.H:9
void sum_over_stacked_boxes(amrex::MultiFab &mf, int comp, int ncomp)
std::unique_ptr< amrex::MultiFab > copy_ground_plane(const amrex::MultiFab &src, int klo)
Definition: ERF_PlanarBoundary.H:44
void fill(amrex::MultiFab &mf, const amrex::Periodicity &period)
Definition: ERF_PlanarBoundary.cpp:67
amrex::DistributionMapping m_dm_sfc
Definition: ERF_PlanarBoundary.H:170
const amrex::DistributionMapping & surface_dm() const
Ranks owning the surface copies: each is on the rank of the planar box it is a copy of.
Definition: ERF_PlanarBoundary.H:127
amrex::Vector< EdgeStrips > m_strips
Definition: ERF_PlanarBoundary.H:173
amrex::MultiFab & buffer(const amrex::MultiFab &mf)
Definition: ERF_PlanarBoundary.cpp:373
amrex::Vector< int > m_src_index
Definition: ERF_PlanarBoundary.H:171
EdgeStrips & edge_strips(const amrex::MultiFab &mf, const amrex::Geometry &geom)
Definition: ERF_PlanarBoundary.cpp:168
void define(const amrex::BoxArray &ba3d, const amrex::BoxArray &ba2d, const amrex::DistributionMapping &dm, int surface_index, bool is_low=true, int normal_dir=2)
Definition: ERF_PlanarBoundary.cpp:22
amrex::BoxArray m_ba_sfc
Definition: ERF_PlanarBoundary.H:169
void reconcile_outside_domain(amrex::MultiFab &mf, const amrex::Geometry &geom)
Definition: ERF_PlanarBoundary.cpp:108
int m_nplanar
Definition: ERF_PlanarBoundary.H:168
AMREX_GPU_HOST bool is_surface_copy(const int planar_index) const
Whether a planar-box index is the computed copy that touches the surface.
Definition: ERF_PlanarBoundary.H:134
amrex::Vector< Buffer > m_buffers
Definition: ERF_PlanarBoundary.H:172
void gather_surface(const amrex::MultiFab &mf, amrex::MultiFab &dst, int scomp, int dcomp, int ncomp) const
Definition: ERF_PlanarBoundary.cpp:288
const amrex::BoxArray & surface_boxes() const
Cell-centered surface copies of the planar boxes, without duplicates.
Definition: ERF_PlanarBoundary.H:124
const amrex::Vector< int > & surface_index() const
Index of each surface copy in the planar BoxArray.
Definition: ERF_PlanarBoundary.H:130
Definition: ERF_PlanarBoundary.H:143
amrex::BoxArray ba
Definition: ERF_PlanarBoundary.H:146
std::unique_ptr< amrex::MultiFab > mf
Definition: ERF_PlanarBoundary.H:149
amrex::IndexType ixtype
Definition: ERF_PlanarBoundary.H:144
int ncomp
Definition: ERF_PlanarBoundary.H:145
amrex::Vector< const amrex::MultiFab * > sources
Definition: ERF_PlanarBoundary.H:148
amrex::DistributionMapping dm
Definition: ERF_PlanarBoundary.H:147
Definition: ERF_PlanarBoundary.H:155
amrex::IntVect ngrow
Definition: ERF_PlanarBoundary.H:158
amrex::Vector< amrex::IntVect > shift
Definition: ERF_PlanarBoundary.H:162
amrex::Vector< int > owner
Definition: ERF_PlanarBoundary.H:161
amrex::DistributionMapping planar_dm
Definition: ERF_PlanarBoundary.H:157
amrex::DistributionMapping dm
Definition: ERF_PlanarBoundary.H:160
amrex::BoxArray planar_ba
Definition: ERF_PlanarBoundary.H:156
amrex::BoxArray ba
Definition: ERF_PlanarBoundary.H:159
amrex::Vector< std::unique_ptr< amrex::MultiFab > > bufs
Definition: ERF_PlanarBoundary.H:163