ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
PlaneAverage Class Reference

#include <ERF_PlaneAverage.H>

Collaboration diagram for PlaneAverage:

Public Member Functions

AMREX_FORCE_INLINE PlaneAverage (const amrex::MultiFab *field_in, amrex::Geometry geom_in, int axis_in, amrex::IntVect n_ghost_to_inc=amrex::IntVect{0})
 
 PlaneAverage ()=delete
 
 ~PlaneAverage ()=default
 
AMREX_FORCE_INLINE void operator() ()
 
AMREX_FORCE_INLINE amrex::Real line_average_interpolated (amrex::Real x, int comp) const
 
void set_precision (int p)
 
amrex::Real dx () const
 
amrex::Real xlo () const
 
int axis () const
 
int level () const
 
int ncomp () const
 
int ncell_plane () const
 
int ncell_line () const
 
const amrex::Vector< amrex::Real > & line_average () const
 
AMREX_FORCE_INLINE void line_average (int comp, amrex::Gpu::HostVector< amrex::Real > &l_vec)
 
const amrex::Vector< amrex::Real > & line_centroids () const
 
const amrex::MultiFab & field () const
 
template<typename IndexSelector >
AMREX_FORCE_INLINE void compute_averages (const IndexSelector &idxOp, const amrex::MultiFab &mfab)
 
template<typename IndexSelector >
void compute_averages (const IndexSelector &idxOp, const amrex::MultiFab &mfab)
 

Protected Attributes

int m_ncomp
 
amrex::Vector< amrex::Realm_line_average
 
amrex::Vector< amrex::Realm_line_xcentroid
 
amrex::Real m_dx
 
amrex::Real m_xlo
 
int m_ncell_plane
 
int m_ncell_line
 
int m_precision = 4
 
const int m_level = 0
 
const amrex::MultiFab * m_field
 
amrex::Geometry m_geom
 
const int m_axis
 
amrex::IntVect m_ghost_to_inc = amrex::IntVect{0}
 
amrex::IntVect m_ixtype
 

Detailed Description

Basic averaging and interpolation operations

Constructor & Destructor Documentation

◆ PlaneAverage() [1/2]

PlaneAverage::PlaneAverage ( const amrex::MultiFab *  field_in,
amrex::Geometry  geom_in,
int  axis_in,
amrex::IntVect  n_ghost_to_inc = amrex::IntVect{0} 
)
explicit

Construct a plane averaging object.

Parameters
[in]field_inMultiFab to be averaged.
[in]geom_inGeometry of the domain.
[in]axis_inAxis along which to average.
[in]n_ghost_to_incGhost cells to include in the average.
112  : m_field(field_in), m_geom(geom_in), m_axis(axis_in), m_ghost_to_inc(n_ghost_to_inc)
113 {
114  AMREX_ALWAYS_ASSERT(m_axis >= 0 && m_axis < AMREX_SPACEDIM);
115 
116  m_xlo = m_geom.ProbLo(m_axis);
117  m_dx = m_geom.CellSize(m_axis);
118  m_ncomp = m_field->nComp();
119  m_ixtype = m_field->boxArray().ixType().toIntVect();
120 
121  amrex::Box domain = m_geom.Domain();
122  m_ghost_to_inc = std::min(field_in->nGrowVect(),m_ghost_to_inc);
123  domain.grow(axis_in, m_ghost_to_inc[axis_in]);
124 
125  amrex::IntVect dom_lo(domain.loVect());
126  amrex::IntVect dom_hi(domain.hiVect());
127 
128  // Domain guarantees enough space and we usually average in z-dir (spans domain)
129  m_ncell_line = dom_hi[m_axis] - dom_lo[m_axis] + 1 + m_ixtype[m_axis];
130 
131  // First estimate is with domain (updated with MFiter later)
132  m_ncell_plane = 1;
133  auto period = m_geom.periodicity();
134  for (int i = 0; i < AMREX_SPACEDIM; ++i) {
135  int p_fac = (!period.isPeriodic(i)) ? 1 : 0;
136  if (i != m_axis) m_ncell_plane *= (dom_hi[i] - dom_lo[i] + 1 + p_fac*m_ixtype[i]);
137  }
138 
139  m_line_average.resize(static_cast<size_t>(m_ncell_line) * m_ncomp, zero);
141 
142  for (int i = 0; i < m_ncell_line; ++i) {
143  m_line_xcentroid[i] = m_xlo + (i + myhalf) * m_dx;
144  }
145 }
constexpr amrex::Real zero
Definition: ERF_Constants.H:8
constexpr amrex::Real myhalf
Definition: ERF_Constants.H:13
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
int m_ncell_line
Definition: ERF_PlaneAverage.H:82
amrex::Real m_xlo
Definition: ERF_PlaneAverage.H:79
amrex::IntVect m_ixtype
Definition: ERF_PlaneAverage.H:91
amrex::Vector< amrex::Real > m_line_xcentroid
Definition: ERF_PlaneAverage.H:76
const amrex::MultiFab * m_field
Definition: ERF_PlaneAverage.H:87
int m_ncell_plane
Definition: ERF_PlaneAverage.H:81
amrex::Vector< amrex::Real > m_line_average
Definition: ERF_PlaneAverage.H:74
const int m_axis
Definition: ERF_PlaneAverage.H:89
amrex::Real m_dx
Definition: ERF_PlaneAverage.H:78
amrex::Geometry m_geom
Definition: ERF_PlaneAverage.H:88
int m_ncomp
Definition: ERF_PlaneAverage.H:71
amrex::IntVect m_ghost_to_inc
Definition: ERF_PlaneAverage.H:90
Here is the call graph for this function:

◆ PlaneAverage() [2/2]

PlaneAverage::PlaneAverage ( )
delete

◆ ~PlaneAverage()

PlaneAverage::~PlaneAverage ( )
default

Member Function Documentation

◆ axis()

int PlaneAverage::axis ( ) const
inline
44 { return m_axis; }

◆ compute_averages() [1/2]

template<typename IndexSelector >
AMREX_FORCE_INLINE void PlaneAverage::compute_averages ( const IndexSelector &  idxOp,
const amrex::MultiFab &  mfab 
)

fill line storage with averages

Referenced by ERF::advance_dycore(), SAM::Compute_Coefficients(), make_mom_sources(), make_sources(), and operator()().

Here is the caller graph for this function:

◆ compute_averages() [2/2]

template<typename IndexSelector >
void PlaneAverage::compute_averages ( const IndexSelector &  idxOp,
const amrex::MultiFab &  mfab 
)

Compute averages across a plane.

Template Parameters
IndexSelectorType of index selector functor.
Parameters
[in]idxOpIndex selector to map cell indices to the line index.
[in]mfabMultiFab containing the data to average.
225 {
226  amrex::Vector<amrex::Real> cnt_h(m_ncell_line,zero);
227  amrex::AsyncArray<amrex::Real> cnt_d(cnt_h.data(), cnt_h.size());
228  amrex::AsyncArray<amrex::Real> lavg(m_line_average.data(), m_line_average.size());
229  amrex::Real* cnt_avg = cnt_d.data();
230  amrex::Real* line_avg = lavg.data();
231  const int ncomp = m_ncomp;
232 
233  amrex::Box domain = amrex::convert(m_geom.Domain(),m_ixtype);
234 
235  amrex::IntVect ng = amrex::IntVect(0);
237  ng[m_axis] = offset;
238 
239  std::unique_ptr<amrex::iMultiFab> mask = OwnerMask(*m_field, m_geom.periodicity(), ng);
240 
241 #ifdef _OPENMP
242 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
243 #endif
244  for (amrex::MFIter mfi(mfab, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi) {
245  amrex::Box tbx = mfi.tilebox();
246  if (tbx.smallEnd(m_axis) == domain.smallEnd(m_axis)) tbx.growLo(m_axis,offset);
247  if (tbx.bigEnd (m_axis) == domain.bigEnd (m_axis)) tbx.growHi(m_axis,offset);
248  amrex::Box pbx = PerpendicularBox<IndexSelector>(tbx, amrex::IntVect(0));
249 
250  const amrex::Array4<const amrex::Real>& fab_arr = mfab.const_array(mfi);
251  const amrex::Array4<const int >& mask_arr = mask->const_array(mfi);
252 
253  amrex::ParallelFor(amrex::Gpu::KernelInfo().setReduction(true), pbx, [=]
254  AMREX_GPU_DEVICE( int p_i, int p_j, int p_k,
255  amrex::Gpu::Handler const& handler) noexcept
256  {
257  // Loop over the direction perpendicular to the plane.
258  // This reduces the atomic pressure on the destination arrays.
259  amrex::Box lbx = ParallelBox<IndexSelector>(tbx, amrex::IntVect{p_i, p_j, p_k});
260 
261  for (int k = lbx.smallEnd(2); k <= lbx.bigEnd(2); ++k) {
262  for (int j = lbx.smallEnd(1); j <= lbx.bigEnd(1); ++j) {
263  for (int i = lbx.smallEnd(0); i <= lbx.bigEnd(0); ++i) {
264  int ind = idxOp.getIndx(i, j, k) + offset;
265  // NOTE: This factor is to avoid an if statement that will break
266  // the devicereducesum since all threads won't participate.
267  // This more performant than Gpu::Atomic::Add.
268  amrex::Real fac = (mask_arr(i,j,k)) ? one : zero;
269  amrex::Gpu::deviceReduceSum(&cnt_avg[ind], fac, handler);
270  for (int n = 0; n < ncomp; ++n) {
271  amrex::Gpu::deviceReduceSum(&line_avg[ncomp * ind + n],
272  fab_arr(i, j, k, n) * fac, handler);
273  }
274  }
275  }
276  }
277  });
278  }
279 
280  cnt_d.copyToHost(cnt_h.data(), cnt_h.size());
281  lavg.copyToHost(m_line_average.data(), m_line_average.size());
282  amrex::ParallelDescriptor::ReduceRealSum(cnt_h.data(), cnt_h.size());
283  amrex::ParallelDescriptor::ReduceRealSum(m_line_average.data(), m_line_average.size());
284  //
285  // With AMR, a level need not cover every plane in the direction we average over: a
286  // fine level typically covers only the bottom of the domain. A plane holding no
287  // cells has nothing to divide by, and leaving it at the accumulated sum -- exactly
288  // zero, since m_line_average was zeroed before the reduction -- is indistinguishable
289  // from a legitimate average of zero. A consumer that divides one profile by another,
290  // as the subsidence forcing in MakeSources does, then gets 0/0 = NaN far from the
291  // cause. So fill each empty plane from the closest plane that does hold cells,
292  // preferring the one below it, which extends the profile as a constant beyond the
293  // range of the level.
294  //
295  amrex::Vector<int> plane_has_cells(m_ncell_line);
296  int ncell_line_empty = 0;
297  for (int ind(0); ind<m_ncell_line; ++ind) {
298  plane_has_cells[ind] = (cnt_h[ind] > zero);
299  if (plane_has_cells[ind]) {
300  for (int n(0); n<ncomp; ++n) {
301  m_line_average[ncomp*ind + n] /= cnt_h[ind];
302  }
303  } else {
304  ++ncell_line_empty;
305  }
306  }
307 
308  if (ncell_line_empty == m_ncell_line) {
309  amrex::Abort("PlaneAverage: no cells were found in any plane");
310  }
311 
312  if (ncell_line_empty > 0) {
313  // Sweeping up first and then down leaves every empty plane holding the value of
314  // the nearest non-empty plane below it, or above it if there is none below.
315  for (int ind(1); ind<m_ncell_line; ++ind) {
316  if (!plane_has_cells[ind] && plane_has_cells[ind-1]) {
317  for (int n(0); n<ncomp; ++n) {
318  m_line_average[ncomp*ind + n] = m_line_average[ncomp*(ind-1) + n];
319  }
320  plane_has_cells[ind] = 1;
321  }
322  }
323  for (int ind(m_ncell_line-2); ind>=0; --ind) {
324  if (!plane_has_cells[ind] && plane_has_cells[ind+1]) {
325  for (int n(0); n<ncomp; ++n) {
326  m_line_average[ncomp*ind + n] = m_line_average[ncomp*(ind+1) + n];
327  }
328  plane_has_cells[ind] = 1;
329  }
330  }
331  }
332 
333  // Take the size of a plane from one that actually holds cells: plane 0 need not be
334  // one of them on a level whose grids do not reach the bottom of the domain.
335  amrex::Real cnt_max = zero;
336  for (int ind(0); ind<m_ncell_line; ++ind) { cnt_max = amrex::max(cnt_max,cnt_h[ind]); }
337  m_ncell_plane = static_cast<int>(cnt_max);
338 }
constexpr amrex::Real one
Definition: ERF_Constants.H:9
ParallelFor(fab_box, [=] AMREX_GPU_DEVICE(int i, int j, int k) { qrcuten_arr(i, j, k)=Real(0);qscuten_arr(i, j, k)=Real(0);qicuten_arr(i, j, k)=Real(0);})
AMREX_FORCE_INLINE IntVect offset(const int face_dir, const int normal)
Definition: ERF_ReadBndryPlanes.cpp:31
amrex::Real Real
Definition: ERF_ShocInterface.H:19
int ncomp() const
Definition: ERF_PlaneAverage.H:46
@ ng
Definition: ERF_Morrison.H:49
Here is the call graph for this function:

◆ dx()

amrex::Real PlaneAverage::dx ( ) const
inline
41 { return m_dx; }

◆ field()

const amrex::MultiFab& PlaneAverage::field ( ) const
inline
68 { return *m_field; }

Referenced by ERF::advance_dycore(), SAM::Compute_Coefficients(), make_mom_sources(), and make_sources().

Here is the caller graph for this function:

◆ level()

int PlaneAverage::level ( ) const
inline
45 { return m_level; }
const int m_level
Definition: ERF_PlaneAverage.H:85

◆ line_average() [1/2]

const amrex::Vector<amrex::Real>& PlaneAverage::line_average ( ) const
inline
51  {
52  return m_line_average;
53  }

Referenced by ERF::advance_dycore(), SAM::Compute_Coefficients(), make_mom_sources(), and make_sources().

Here is the caller graph for this function:

◆ line_average() [2/2]

void PlaneAverage::line_average ( int  comp,
amrex::Gpu::HostVector< amrex::Real > &  l_vec 
)

Copy the line average of a specific component to a host vector.

Parameters
[in]compComponent index.
[out]l_vecHost vector to store the line average.
185 {
186  AMREX_ALWAYS_ASSERT(comp >= 0 && comp < m_ncomp);
187 
188  for (int i = 0; i < m_ncell_line; i++) {
189  l_vec[i] = m_line_average[m_ncomp * i + comp];
190  }
191 }
Here is the call graph for this function:

◆ line_average_interpolated()

amrex::Real PlaneAverage::line_average_interpolated ( amrex::Real  x,
int  comp 
) const

evaluate line average at specific location for any average component

Evaluate line average at specific location for any average component.

Parameters
[in]xPhysical coordinate along the averaging axis.
[in]compComponent index.
Returns
Interpolated value of the line average.
155 {
156  AMREX_ALWAYS_ASSERT(comp >= 0 && comp < m_ncomp);
157 
158  amrex::Real c = zero;
159  int ind = 0;
160 
161  if (x > m_xlo + myhalf * m_dx) {
162  ind = static_cast<int>(floor((x - m_xlo) / m_dx - myhalf));
163  const amrex::Real x1 = m_xlo + (ind + myhalf) * m_dx;
164  c = (x - x1) / m_dx;
165  }
166 
167  if (ind + 1 >= m_ncell_line) {
168  ind = m_ncell_line - 2;
169  c = one;
170  }
171 
172  AMREX_ALWAYS_ASSERT(ind >= 0 && ind + 1 < m_ncell_line);
173 
174  return m_line_average[m_ncomp * ind + comp] * (one - c) +
175  m_line_average[m_ncomp * (ind + 1) + comp] * c;
176 }
Here is the call graph for this function:

◆ line_centroids()

const amrex::Vector<amrex::Real>& PlaneAverage::line_centroids ( ) const
inline
64  {
65  return m_line_xcentroid;
66  }

◆ ncell_line()

int PlaneAverage::ncell_line ( ) const
inline
48 { return m_ncell_line; }

Referenced by ERF::advance_dycore(), SAM::Compute_Coefficients(), make_mom_sources(), and make_sources().

Here is the caller graph for this function:

◆ ncell_plane()

int PlaneAverage::ncell_plane ( ) const
inline
47 { return m_ncell_plane; }

◆ ncomp()

int PlaneAverage::ncomp ( ) const
inline
46 { return m_ncomp; }

Referenced by compute_averages().

Here is the caller graph for this function:

◆ operator()()

void PlaneAverage::operator() ( )

Compute the plane averages for the stored field.

198 {
199  std::fill(m_line_average.begin(), m_line_average.end(), zero);
200  switch (m_axis) {
201  case 0:
203  break;
204  case 1:
206  break;
207  case 2:
209  break;
210  default:
211  amrex::Abort("axis must be equal to 0, 1, or 2");
212  break;
213  }
214 }
DirectionSelector< 0 > XDir
Definition: ERF_DirectionSelector.H:53
DirectionSelector< 1 > YDir
Definition: ERF_DirectionSelector.H:54
DirectionSelector< 2 > ZDir
Definition: ERF_DirectionSelector.H:55
AMREX_FORCE_INLINE void compute_averages(const IndexSelector &idxOp, const amrex::MultiFab &mfab)
void fill(MultiFab &dst, int temperature_comp, int mixing_ratio_comp, int source_comp, const Sources &sources, Real missing_value)
Definition: ERF_NearSurfaceDiagnostics.cpp:41
Here is the call graph for this function:

◆ set_precision()

void PlaneAverage::set_precision ( int  p)
inline

change precision of text file output

39 { m_precision = p; }
int m_precision
Definition: ERF_PlaneAverage.H:84
@ p
Definition: ERF_WSM6.H:191

◆ xlo()

amrex::Real PlaneAverage::xlo ( ) const
inline
42 { return m_xlo; }

Member Data Documentation

◆ m_axis

const int PlaneAverage::m_axis
protected

◆ m_dx

amrex::Real PlaneAverage::m_dx
protected

line storage for centroids of each cell along a line

Referenced by dx(), line_average_interpolated(), and PlaneAverage().

◆ m_field

const amrex::MultiFab* PlaneAverage::m_field
protected

level for plane averaging for now fixed at level=0

Referenced by compute_averages(), field(), operator()(), and PlaneAverage().

◆ m_geom

amrex::Geometry PlaneAverage::m_geom
protected

Referenced by compute_averages(), and PlaneAverage().

◆ m_ghost_to_inc

amrex::IntVect PlaneAverage::m_ghost_to_inc = amrex::IntVect{0}
protected

Referenced by compute_averages(), and PlaneAverage().

◆ m_ixtype

amrex::IntVect PlaneAverage::m_ixtype
protected

Referenced by compute_averages(), and PlaneAverage().

◆ m_level

const int PlaneAverage::m_level = 0
protected

precision for line plot text file

Referenced by level().

◆ m_line_average

amrex::Vector<amrex::Real> PlaneAverage::m_line_average
protected

number of average components line storage for the average velocity and tracer variables

Referenced by compute_averages(), line_average(), line_average_interpolated(), operator()(), and PlaneAverage().

◆ m_line_xcentroid

amrex::Vector<amrex::Real> PlaneAverage::m_line_xcentroid
protected

Referenced by line_centroids(), and PlaneAverage().

◆ m_ncell_line

int PlaneAverage::m_ncell_line
protected

◆ m_ncell_plane

int PlaneAverage::m_ncell_plane
protected

bottom of domain in axis direction

Referenced by compute_averages(), ncell_plane(), and PlaneAverage().

◆ m_ncomp

int PlaneAverage::m_ncomp
protected

◆ m_precision

int PlaneAverage::m_precision = 4
protected

number of cells along line

Referenced by set_precision().

◆ m_xlo

amrex::Real PlaneAverage::m_xlo
protected

grid spacing in axis direction

Referenced by line_average_interpolated(), PlaneAverage(), and xlo().


The documentation for this class was generated from the following file: