ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_PlaneAverage.H
Go to the documentation of this file.
1 #ifndef ERF_PlaneAverage_H
2 #define ERF_PlaneAverage_H
3 
4 #include "AMReX_Gpu.H"
5 #include "AMReX_iMultiFab.H"
6 #include "AMReX_MultiFab.H"
7 #include "AMReX_GpuContainers.H"
9 
10 /**
11  * Basic averaging and interpolation operations
12  */
13 
14 class PlaneAverage {
15 public:
16  AMREX_FORCE_INLINE
17  explicit PlaneAverage (const amrex::MultiFab* field_in,
18  amrex::Geometry geom_in,
19  int axis_in,
20  bool inc_ghost=false);
21  PlaneAverage () = delete;
22  ~PlaneAverage () = default;
23 
24  AMREX_FORCE_INLINE
25  void operator()();
26 
27  /** evaluate line average at specific location for any average component */
28  [[nodiscard]] AMREX_FORCE_INLINE
29  amrex::Real line_average_interpolated (amrex::Real x, int comp) const;
30 
31  /** change precision of text file output */
32  void set_precision (int p) { m_precision = p; }
33 
34  [[nodiscard]] amrex::Real dx () const { return m_dx; }
35  [[nodiscard]] amrex::Real xlo () const { return m_xlo; }
36 
37  [[nodiscard]] int axis () const { return m_axis; }
38  [[nodiscard]] int level () const { return m_level; }
39  [[nodiscard]] int ncomp () const { return m_ncomp; }
40  [[nodiscard]] int ncell_plane () const { return m_ncell_plane; }
41  [[nodiscard]] int ncell_line () const { return m_ncell_line; }
42 
43  [[nodiscard]] const amrex::Vector<amrex::Real>& line_average () const
44  {
45  return m_line_average;
46  }
47 
48  AMREX_FORCE_INLINE
49  void line_average (int comp, amrex::Gpu::HostVector<amrex::Real>& l_vec);
50 
51  [[nodiscard]] const amrex::Vector<amrex::Real>& line_centroids () const
52  {
53  return m_line_xcentroid;
54  }
55 
56  [[nodiscard]] const amrex::MultiFab& field () const { return *m_field; }
57 
58 protected:
59  int m_ncomp; /** number of average components */
60 
61  /** line storage for the average velocity and tracer variables */
62  amrex::Vector<amrex::Real> m_line_average;
63 
64  amrex::Vector<amrex::Real> m_line_xcentroid; /** line storage for centroids of each cell along a line*/
65 
66  amrex::Real m_dx; /** mesh spacing in axis direction*/
67  amrex::Real m_xlo; /** bottom of domain in axis direction */
68 
69  int m_ncell_plane; /** number of cells in plane */
70  int m_ncell_line; /** number of cells along line */
71 
72  int m_precision = 4; /** precision for line plot text file */
73  const int m_level = 0; /** level for plane averaging for now fixed at level=0 */
74 
75  const amrex::MultiFab* m_field;
76  amrex::Geometry m_geom;
77  const int m_axis;
78  bool m_inc_ghost = false;
79  amrex::IntVect m_ixtype;
80  amrex::IntVect m_ng = amrex::IntVect(0);
81 
82 public:
83  /** fill line storage with averages */
84  template <typename IndexSelector>
85  AMREX_FORCE_INLINE
86  void compute_averages (const IndexSelector& idxOp, const amrex::MultiFab& mfab);
87 };
88 
89 
90 PlaneAverage::PlaneAverage (const amrex::MultiFab* field_in,
91  amrex::Geometry geom_in,
92  int axis_in,
93  bool inc_ghost)
94  : m_field(field_in), m_geom(geom_in), m_axis(axis_in), m_inc_ghost(inc_ghost)
95 {
96  AMREX_ALWAYS_ASSERT(m_axis >= 0 && m_axis < AMREX_SPACEDIM);
97 
98  m_xlo = m_geom.ProbLo(m_axis);
99  m_dx = m_geom.CellSize(m_axis);
100  m_ncomp = m_field->nComp();
101  m_ixtype = m_field->boxArray().ixType().toIntVect();
102 
103  amrex::Box domain = m_geom.Domain();
104  if (inc_ghost) {
105  m_ng = field_in->nGrowVect();
106  domain.grow(axis_in, m_ng[axis_in]);
107  }
108  amrex::IntVect dom_lo(domain.loVect());
109  amrex::IntVect dom_hi(domain.hiVect());
110 
111  m_ncell_line = dom_hi[m_axis] - dom_lo[m_axis] + 1 + m_ixtype[m_axis];
112 
113  m_ncell_plane = 1;
114  auto period = m_geom.periodicity();
115  for (int i = 0; i < AMREX_SPACEDIM; ++i) {
116  int p_fac = (!period.isPeriodic(i)) ? 1 : 0;
117  if (i != m_axis) m_ncell_plane *= (dom_hi[i] - dom_lo[i] + 1 + p_fac*m_ixtype[i]);
118  }
119 
120  m_line_average.resize(static_cast<size_t>(m_ncell_line) * m_ncomp, 0.0);
122 
123  for (int i = 0; i < m_ncell_line; ++i) {
124  m_line_xcentroid[i] = m_xlo + (i + 0.5) * m_dx;
125  }
126 }
127 
128 amrex::Real
129 PlaneAverage::line_average_interpolated (amrex::Real x, int comp) const
130 {
131  AMREX_ALWAYS_ASSERT(comp >= 0 && comp < m_ncomp);
132 
133  amrex::Real c = 0.0;
134  int ind = 0;
135 
136  if (x > m_xlo + 0.5 * m_dx) {
137  ind = static_cast<int>(floor((x - m_xlo) / m_dx - 0.5));
138  const amrex::Real x1 = m_xlo + (ind + 0.5) * m_dx;
139  c = (x - x1) / m_dx;
140  }
141 
142  if (ind + 1 >= m_ncell_line) {
143  ind = m_ncell_line - 2;
144  c = 1.0;
145  }
146 
147  AMREX_ALWAYS_ASSERT(ind >= 0 && ind + 1 < m_ncell_line);
148 
149  return m_line_average[m_ncomp * ind + comp] * (1.0 - c) +
150  m_line_average[m_ncomp * (ind + 1) + comp] * c;
151 }
152 
153 void
154 PlaneAverage::line_average (int comp, amrex::Gpu::HostVector<amrex::Real>& l_vec)
155 {
156  AMREX_ALWAYS_ASSERT(comp >= 0 && comp < m_ncomp);
157 
158  for (int i = 0; i < m_ncell_line; i++)
159  l_vec[i] = m_line_average[m_ncomp * i + comp];
160 }
161 
162 void
164 {
165  std::fill(m_line_average.begin(), m_line_average.end(), 0.0);
166  switch (m_axis) {
167  case 0:
169  break;
170  case 1:
172  break;
173  case 2:
175  break;
176  default:
177  amrex::Abort("axis must be equal to 0, 1, or 2");
178  break;
179  }
180 }
181 
182 template <typename IndexSelector>
183 void
184 PlaneAverage::compute_averages (const IndexSelector& idxOp, const amrex::MultiFab& mfab)
185 {
186  const amrex::Real denom = 1.0 / (amrex::Real)m_ncell_plane;
187  amrex::AsyncArray<amrex::Real> lavg(m_line_average.data(), m_line_average.size());
188  amrex::Real* line_avg = lavg.data();
189  const int ncomp = m_ncomp;
190 
191  amrex::Box domain = amrex::convert(m_geom.Domain(),m_ixtype);
192 
193  amrex::IntVect ng = amrex::IntVect(0);
194  int offset = m_ng[m_axis];
195  if (m_inc_ghost) ng[m_axis] = offset;
196 
197  std::unique_ptr<amrex::iMultiFab> mask = OwnerMask(*m_field, m_geom.periodicity(), ng);
198 
199 #ifdef _OPENMP
200 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
201 #endif
202  for (amrex::MFIter mfi(mfab, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi) {
203  amrex::Box tbx = mfi.tilebox();
204  if (tbx.smallEnd(m_axis) == domain.smallEnd(m_axis)) tbx.growLo(m_axis,offset);
205  if (tbx.bigEnd (m_axis) == domain.bigEnd (m_axis)) tbx.growHi(m_axis,offset);
206  amrex::Box pbx = PerpendicularBox<IndexSelector>(tbx, amrex::IntVect(0));
207 
208  const amrex::Array4<const amrex::Real>& fab_arr = mfab.const_array(mfi);
209  const amrex::Array4<const int >& mask_arr = mask->const_array(mfi);
210 
211  amrex::ParallelFor(amrex::Gpu::KernelInfo().setReduction(true), pbx, [=]
212  AMREX_GPU_DEVICE( int p_i, int p_j, int p_k,
213  amrex::Gpu::Handler const& handler) noexcept
214  {
215  // Loop over the direction perpendicular to the plane.
216  // This reduces the atomic pressure on the destination arrays.
217  amrex::Box lbx = ParallelBox<IndexSelector>(tbx, amrex::IntVect{p_i, p_j, p_k});
218 
219  for (int k = lbx.smallEnd(2); k <= lbx.bigEnd(2); ++k) {
220  for (int j = lbx.smallEnd(1); j <= lbx.bigEnd(1); ++j) {
221  for (int i = lbx.smallEnd(0); i <= lbx.bigEnd(0); ++i) {
222  int ind = idxOp.getIndx(i, j, k) + offset;
223  for (int n = 0; n < ncomp; ++n) {
224  // NOTE: This factor is to avoid an if statement that will break
225  // the devicereducesum since all threads won't participate.
226  // This more performant than Gpu::Atomic::Add.
227  amrex::Real fac = (mask_arr(i,j,k)) ? 1.0 : 0.0;
228  amrex::Gpu::deviceReduceSum(&line_avg[ncomp * ind + n],
229  fab_arr(i, j, k, n) * denom * fac, handler);
230  }
231  }
232  }
233  }
234  });
235  }
236 
237  lavg.copyToHost(m_line_average.data(), m_line_average.size());
238  amrex::ParallelDescriptor::ReduceRealSum(m_line_average.data(), m_line_average.size());
239 }
240 #endif /* ERF_PlaneAverage.H */
DirectionSelector< 0 > XDir
Definition: ERF_DirectionSelector.H:36
DirectionSelector< 1 > YDir
Definition: ERF_DirectionSelector.H:37
DirectionSelector< 2 > ZDir
Definition: ERF_DirectionSelector.H:38
AMREX_FORCE_INLINE IntVect offset(const int face_dir, const int normal)
Definition: ERF_ReadBndryPlanes.cpp:28
Definition: ERF_PlaneAverage.H:14
const amrex::MultiFab & field() const
Definition: ERF_PlaneAverage.H:56
int m_ncell_line
Definition: ERF_PlaneAverage.H:70
AMREX_FORCE_INLINE amrex::Real line_average_interpolated(amrex::Real x, int comp) const
Definition: ERF_PlaneAverage.H:129
int m_precision
Definition: ERF_PlaneAverage.H:72
int ncomp() const
Definition: ERF_PlaneAverage.H:39
amrex::Real m_xlo
Definition: ERF_PlaneAverage.H:67
amrex::IntVect m_ixtype
Definition: ERF_PlaneAverage.H:79
amrex::IntVect m_ng
Definition: ERF_PlaneAverage.H:80
amrex::Vector< amrex::Real > m_line_xcentroid
Definition: ERF_PlaneAverage.H:64
const amrex::Vector< amrex::Real > & line_centroids() const
Definition: ERF_PlaneAverage.H:51
AMREX_FORCE_INLINE void compute_averages(const IndexSelector &idxOp, const amrex::MultiFab &mfab)
void set_precision(int p)
Definition: ERF_PlaneAverage.H:32
const int m_level
Definition: ERF_PlaneAverage.H:73
const amrex::MultiFab * m_field
Definition: ERF_PlaneAverage.H:75
int level() const
Definition: ERF_PlaneAverage.H:38
~PlaneAverage()=default
int m_ncell_plane
Definition: ERF_PlaneAverage.H:69
amrex::Vector< amrex::Real > m_line_average
Definition: ERF_PlaneAverage.H:62
amrex::Real xlo() const
Definition: ERF_PlaneAverage.H:35
AMREX_FORCE_INLINE void operator()()
Definition: ERF_PlaneAverage.H:163
int ncell_plane() const
Definition: ERF_PlaneAverage.H:40
const int m_axis
Definition: ERF_PlaneAverage.H:77
int ncell_line() const
Definition: ERF_PlaneAverage.H:41
bool m_inc_ghost
Definition: ERF_PlaneAverage.H:78
amrex::Real dx() const
Definition: ERF_PlaneAverage.H:34
amrex::Real m_dx
Definition: ERF_PlaneAverage.H:66
PlaneAverage()=delete
amrex::Geometry m_geom
Definition: ERF_PlaneAverage.H:76
int axis() const
Definition: ERF_PlaneAverage.H:37
int m_ncomp
Definition: ERF_PlaneAverage.H:59
const amrex::Vector< amrex::Real > & line_average() const
Definition: ERF_PlaneAverage.H:43