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

#include <ERF_MOSTAverage.H>

Collaboration diagram for MOSTAverage:

Classes

struct  InterpStencilBounds
 

Public Member Functions

 MOSTAverage (amrex::Orientation face, amrex::Vector< amrex::Geometry > geom, const bool &has_zphys, std::string a_pp_prefix, const MeshType &m_mesh_type, const TerrainType &m_terrain_type, const amrex::Vector< amrex::Vector< amrex::Real >> &zlevels_stag, const amrex::Vector< const eb_ * > &eb_vec={})
 
 ~MOSTAverage ()
 
 MOSTAverage (MOSTAverage &&) noexcept=default
 
MOSTAverage & operator= (MOSTAverage &&other) noexcept=delete
 
 MOSTAverage (const MOSTAverage &other)=delete
 
MOSTAverage & operator= (const MOSTAverage &other)=delete
 
void make_MOSTAverage_at_level (const int &lev, const amrex::Vector< amrex::MultiFab * > &vars_old, std::unique_ptr< amrex::MultiFab > &Theta_prim, std::unique_ptr< amrex::MultiFab > &Qv_prim, std::unique_ptr< amrex::MultiFab > &Qr_prim, std::unique_ptr< amrex::MultiFab > &z_phys_nd, const amrex::Vector< std::unique_ptr< amrex::MultiFab >> *mapfac=nullptr)
 
void update_field_ptrs (const int &lev, amrex::Vector< amrex::Vector< amrex::MultiFab >> &vars_old, amrex::Vector< std::unique_ptr< amrex::MultiFab >> &Theta_prim, amrex::Vector< std::unique_ptr< amrex::MultiFab >> &Qv_prim, amrex::Vector< std::unique_ptr< amrex::MultiFab >> &Qr_prim)
 
void set_velocity_field_ptrs (const int &lev, amrex::MultiFab *xvel, amrex::MultiFab *yvel)
 
void set_rotated_fields (const int &lev)
 
void set_plane_normalization (const int &lev)
 
void set_eb_normalization (const int &lev)
 
void set_region_normalization (const int &)
 
void set_k_indices_N (const int &lev)
 
void validate_lateral_reference_stencil (const int &lev)
 
void set_k_indices_T (const int &lev)
 
void set_norm_indices_T (const int &lev)
 
void set_z_positions_T (const int &lev)
 
void set_z_positions_EB (const int &lev)
 
void set_norm_positions_T (const int &lev)
 
void compute_averages (const int &lev)
 
void compute_plane_averages (const int &lev)
 
void compute_region_averages (const int &lev)
 
void compute_eb_averages (const int &lev)
 
void write_k_indices (const int &lev)
 
void write_norm_indices (const int &lev)
 
void write_xz_positions (const int &lev, const int &j)
 
void write_averages (const int &lev)
 
const amrex::MultiFab * get_average (const int &lev, const int &comp) const
 
amrex::MultiFab * get_average (const int &lev, const int &comp)
 
bool do_time_averaging () const
 
int get_navg () const
 
bool time_avg_is_initialized (const int &lev) const
 
void set_time_avg_initialized (const int &lev)
 
amrex::Vector< amrex::Real > get_plane_average (const int &lev) const
 
bool set_plane_average (const int &lev, const amrex::Vector< amrex::Real > &pavg)
 
amrex::MultiFab * get_zref (const int &lev) const
 
const amrex::MultiFab * get_field_ptr (const int &lev, const int &comp) const
 
const amrex::iMultiFab * get_k_indices (const int &lev) const
 
amrex::IntVect get_ng_fill (const int &lev) const
 
InterpStencilBounds interp_stencil_bounds (const int &lev, const amrex::MFIter &mfi) const
 
int max_k_read (const int &lev) const
 
bool max_k_read_can_change () const
 
void fill_planar_boundary (const int &lev, amrex::MultiFab &mf)
 
void fill_zref_planar (const int &lev)
 
void extrap_ghost_cells (const int &lev, const int &iavg, const amrex::IntVect &ng_fill)
 

Static Public Member Functions

AMREX_GPU_HOST_DEVICE static AMREX_INLINE bool in_cell_z (const amrex::Real &z, const amrex::Real &z_lo, const amrex::Real &z_hi)
 
static int k_index_below (const amrex::Vector< amrex::Real > &zlevels, const amrex::Real &zref)
 
static amrex::Real cell_center_height (const amrex::Vector< amrex::Real > &zlevels, const int &k)
 
static amrex::Real column_height (const amrex::Vector< amrex::Real > &zlevels)
 
AMREX_GPU_HOST_DEVICE static AMREX_INLINE bool find_interp_height_T (const amrex::Real &xp, const amrex::Real &yp, const amrex::Real &zp, amrex::Array4< amrex::Real const > const &z_arr, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &plo, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &dxi, amrex::Real &ireal, amrex::Real &jreal, amrex::Real &zval)
 
AMREX_GPU_HOST_DEVICE static AMREX_INLINE void trilinear_interp_T (const amrex::Real &xp, const amrex::Real &yp, const amrex::Real &zp, amrex::Real *interp_vals, amrex::Array4< amrex::Real const > const &interp_array, amrex::Array4< amrex::Real const > const &z_arr, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &plo, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &dxi, const int interp_comp)
 

Protected Attributes

amrex::Orientation m_face
 
const amrex::Vector< amrex::Geometry > m_geom
 
amrex::Vector< amrex::Vector< amrex::MultiFab * > > m_fields
 
amrex::Vector< amrex::MultiFab * > m_z_phys_nd
 
amrex::Vector< amrex::Vector< amrex::MultiFab * > > m_mapfac
 
std::string m_pp_prefix
 
MeshType m_mesh_type
 
TerrainType m_terrain_type
 
amrex::Vector< amrex::Vector< amrex::Real > > m_zlevels_stag
 
int m_nvar {6}
 
int m_navg {9}
 
int m_maxlev {0}
 
int m_policy {0}
 
bool m_rotate {false}
 
amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_zref
 
amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_x_pos
 
amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_y_pos
 
amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_z_pos
 
amrex::Vector< std::unique_ptr< amrex::iMultiFab > > m_i_indx
 
amrex::Vector< std::unique_ptr< amrex::iMultiFab > > m_j_indx
 
amrex::Vector< std::unique_ptr< amrex::iMultiFab > > m_k_indx
 
amrex::Vector< amrex::Vector< std::unique_ptr< amrex::MultiFab > > > m_averages
 
amrex::Vector< PlanarBoundary > m_planar_bndry
 
amrex::Vector< amrex::Vector< std::unique_ptr< amrex::MultiFab > > > m_rot_fields
 
amrex::Vector< amrex::Vector< int > > m_ncell_plane
 
amrex::Vector< amrex::Vector< amrex::Real > > m_plane_average
 
int m_radius {0}
 
int m_ncell_region {1}
 
amrex::Vector< int > m_k_in
 
bool m_interp {false}
 
bool m_norm_vec {false}
 
amrex::Vector< const eb_ * > m_eb_vec
 
amrex::Vector< amrex::Vector< amrex::Real > > m_total_bndry_area
 
bool m_t_avg {false}
 
amrex::Vector< int > m_t_init
 
double m_time_window {1.0e-16}
 
amrex::Real m_fact_new
 
amrex::Real m_fact_old
 
bool include_subgrid_vel = false
 
amrex::Vector< amrex::Real > m_Vsg
 
const amrex::Real zref_sentinel = amrex::Real(-1.0)
 

Constructor & Destructor Documentation

◆ MOSTAverage() [1/3]

MOSTAverage::MOSTAverage ( amrex::Orientation  face,
amrex::Vector< amrex::Geometry >  geom,
const bool &  has_zphys,
std::string  a_pp_prefix,
const MeshType &  m_mesh_type,
const TerrainType &  m_terrain_type,
const amrex::Vector< amrex::Vector< amrex::Real >> &  zlevels_stag,
const amrex::Vector< const eb_ * > &  eb_vec = {} 
)
explicit

Construct the MOST averaging helper.

Parameters
[in]faceorientation of face for MOST averaging
[in]geomgeometry for all AMR levels
[in]has_zphyswhether nodal physical-height data are available
[in]a_pp_prefixParmParse prefix
[in]m_mesh_typemesh type
[in]m_terrain_typeterrain representation
[in]zlevels_stagnominal staggered z levels for all AMR levels
[in]eb_vecoptional embedded-boundary geometry data

◆ ~MOSTAverage()

MOSTAverage::~MOSTAverage ( )
inline

Destroy the MOST averaging helper.

47  {}

◆ MOSTAverage() [2/3]

MOSTAverage::MOSTAverage ( MOSTAverage &&  )
defaultnoexcept

Default move constructor.

◆ MOSTAverage() [3/3]

MOSTAverage::MOSTAverage ( const MOSTAverage &  other)
delete

Deleted copy constructor.

Parameters
[in]othersource object

Member Function Documentation

◆ cell_center_height()

static amrex::Real MOSTAverage::cell_center_height ( const amrex::Vector< amrex::Real > &  zlevels,
const int &  k 
)
inlinestatic

Height of a cell center above the bottom of the domain, on a column of staggered (face) heights.

Parameters
[in]zlevelsstaggered heights of the column (nz+1 faces)
[in]kcell index
418  {
419  return myhalf * (zlevels[k] + zlevels[k+1]) - zlevels[0];
420  }
constexpr amrex::Real myhalf
Definition: ERF_NumericalConstants.H:41

Referenced by k_index_below(), and set_k_indices_N().

Here is the caller graph for this function:

◆ column_height()

static amrex::Real MOSTAverage::column_height ( const amrex::Vector< amrex::Real > &  zlevels)
inlinestatic

Height of the top of the column above its bottom, on a column of staggered (face) heights; the high z face's distances are measured from it.

Parameters
[in]zlevelsstaggered heights of the column (nz+1 faces)
429  {
430  return zlevels.back() - zlevels[0];
431  }

Referenced by set_k_indices_N().

Here is the caller graph for this function:

◆ compute_averages()

void MOSTAverage::compute_averages ( const int &  lev)

Driver for the selected average policy.

Parameters
[in]levlevel index

Function to call the type of average computation.

Parameters
[in]levCurrent level
1696 {
1697  if (m_rotate) set_rotated_fields(lev);
1698 
1699  switch(m_policy) {
1700  case 0: // Standard plane average
1702  break;
1703  case 1: // Local region/point
1705  break;
1706  case 2: // EB Terrain average
1707  compute_eb_averages(lev);
1708  break;
1709  default:
1710  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(false, "Unknown policy for MOSTAverage!");
1711  }
1712 
1713  // We have initialized the averages
1714  if (m_t_avg) m_t_init[lev] = 1;
1715 }
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(axis >=0 &&axis< AMREX_SPACEDIM, "prob.axis must be 0, 1 or 2")
bool m_t_avg
Definition: ERF_MOSTAverage.H:764
void compute_plane_averages(const int &lev)
Definition: ERF_MOSTAverage.cpp:1724
int m_policy
Definition: ERF_MOSTAverage.H:728
void compute_region_averages(const int &lev)
Definition: ERF_MOSTAverage.cpp:2342
amrex::Vector< int > m_t_init
Definition: ERF_MOSTAverage.H:765
void set_rotated_fields(const int &lev)
Definition: ERF_MOSTAverage.cpp:660
bool m_rotate
Definition: ERF_MOSTAverage.H:729
void compute_eb_averages(const int &lev)
Definition: ERF_MOSTAverage.cpp:2975
Here is the call graph for this function:

◆ compute_eb_averages()

void MOSTAverage::compute_eb_averages ( const int &  lev)

Fill averages for the embedded-boundary policy.

Parameters
[in]levlevel index

Function to compute average over an EB surface.

Parameters
[in]levCurrent level
2976 {
2978  m_face.coordDir() == 2,
2979  "EB MOST averaging currently supports only z faces.");
2980  AMREX_ALWAYS_ASSERT(m_eb_vec[lev] != nullptr);
2981 
2982  // Peel back the level
2983  auto& fields = m_fields[lev];
2984  auto& averages = m_averages[lev];
2985  auto& plane_average = m_plane_average[lev];
2986 
2987  // Get EB data
2988  const auto& cc_flags = m_eb_vec[lev]->get_const_factory()->getMultiEBCellFlagFab();
2989  auto cc_afrac = m_eb_vec[lev]->get_const_factory()->getAreaFrac();
2990  const auto& cc_bnorm = m_eb_vec[lev]->get_const_factory()->getBndryNormal();
2991  const auto& u_vfrac = m_eb_vec[lev]->get_u_const_factory()->getVolFrac();
2992  const auto& v_vfrac = m_eb_vec[lev]->get_v_const_factory()->getVolFrac();
2993  const auto& w_vfrac = m_eb_vec[lev]->get_w_const_factory()->getVolFrac();
2994 
2995  // Get geometry for cell sizes
2996  auto const& dx_arr = m_geom[lev].CellSizeArray();
2997  Real dx = dx_arr[0];
2998  Real dy = dx_arr[1];
2999  Real dz = dx_arr[2];
3000 
3001  // Set factors for time averaging
3002  Real d_fact_new, d_fact_old;
3003  if (m_t_avg && m_t_init[lev]) {
3004  d_fact_new = m_fact_new;
3005  d_fact_old = m_fact_old;
3006  } else {
3007  d_fact_new = one;
3008  d_fact_old = zero;
3009  }
3010 
3011  // GPU array to accumulate averages into
3012  Gpu::DeviceVector<Real> pavg(plane_average.size(), zero);
3013  Real* plane_avg = pavg.data();
3014 
3015  // Vectors for normalization and buffer storage
3016  Vector<Real> denom(plane_average.size(),zero);
3017  Vector<Real> val_old(plane_average.size(),zero);
3018 
3019  //
3020  //----------------------------------------------------------
3021  // Averages for U, V, and tangential velocity (in local coordinate)
3022  //----------------------------------------------------------
3023  //
3024  {
3025  denom[0] = one / m_total_bndry_area[lev][0];
3026  val_old[0] = plane_average[0]*d_fact_old;
3027  denom[1] = one / m_total_bndry_area[lev][1];
3028  val_old[1] = plane_average[1]*d_fact_old;
3029 
3030  int iavg = m_navg - 3; // Tangential velocity magnitude
3031  denom[iavg] = one / m_total_bndry_area[lev][iavg];
3032  val_old[iavg] = plane_average[iavg]*d_fact_old;
3033 
3034  const Real Vsg = m_Vsg[lev]; // Subgrid scale velocity
3035 
3036  //
3037  // NOTE: deliberately not threaded. The body is a pure reduction into the shared
3038  // plane_avg entries, and on the host Gpu::deviceReduceSum is an unordered
3039  // "#pragma omp atomic" add. Threading it would make the surface-layer averages
3040  // -- and through them the surface fluxes -- depend on thread scheduling in the
3041  // last bits. See the same note in PlaneAverage::compute_averages.
3042  //
3043  for (MFIter mfi(*fields[3], TileNoZ()); mfi.isValid(); ++mfi) {
3044  const auto& flag = cc_flags[mfi];
3045 
3046  // Skip boxes that are not singlevalued (MultiCutFab only has data for singlevalued boxes)
3047  if (flag.getType() != FabType::singlevalued) continue;
3048 
3049  Box bx = mfi.tilebox(); // Full 3D box
3050 
3051  // Get EB arrays
3052  auto const flag_arr = flag.const_array();
3053  auto const afrac_x = cc_afrac[0]->const_array(mfi);
3054  auto const afrac_y = cc_afrac[1]->const_array(mfi);
3055  auto const afrac_z = cc_afrac[2]->const_array(mfi);
3056  auto const bnorm_arr = cc_bnorm.const_array(mfi);
3057  auto const u_vf_arr = u_vfrac.const_array(mfi);
3058  auto const v_vf_arr = v_vfrac.const_array(mfi);
3059  auto const w_vf_arr = w_vfrac.const_array(mfi);
3060 
3061  // Get velocity arrays
3062  auto const u_arr = fields[0]->const_array(mfi);
3063  auto const v_arr = fields[1]->const_array(mfi);
3064  auto const w_arr = fields[2]->const_array(mfi);
3065 
3066  ParallelFor(Gpu::KernelInfo().setReduction(true), bx, [=]
3067  AMREX_GPU_DEVICE(int i, int j, int k, Gpu::Handler const& handler) noexcept
3068  {
3069  Real val_u = zero;
3070  Real val_v = zero;
3071  Real val_mag = zero;
3072  // Area-weighted averaging over cut cells at any k
3073  if (flag_arr(i,j,k).isSingleValued()) {
3074  // Compute area from face-centered area fractions
3075  Real axm = afrac_x(i ,j ,k );
3076  Real axp = afrac_x(i+1,j ,k );
3077  Real aym = afrac_y(i ,j ,k );
3078  Real ayp = afrac_y(i ,j+1,k );
3079  Real azm = afrac_z(i ,j ,k );
3080  Real azp = afrac_z(i ,j ,k+1);
3081 
3082  Real adx = (axm - axp) * dy * dz;
3083  Real ady = (aym - ayp) * dx * dz;
3084  Real adz = (azm - azp) * dx * dy;
3085 
3086  Real area = std::sqrt(adx*adx + ady*ady + adz*adz);
3087 
3088  // Volume-weighted interpolation of velocities to cell center
3089  Real vf_u_lo = u_vf_arr(i,j,k);
3090  Real vf_u_hi = u_vf_arr(i+1,j,k);
3091  Real sum_vf_u = vf_u_lo + vf_u_hi;
3092  Real u_cc = (sum_vf_u > zero) ? (u_arr(i,j,k)*vf_u_lo + u_arr(i+1,j,k)*vf_u_hi) / sum_vf_u : zero;
3093 
3094  Real vf_v_lo = v_vf_arr(i,j,k);
3095  Real vf_v_hi = v_vf_arr(i,j+1,k);
3096  Real sum_vf_v = vf_v_lo + vf_v_hi;
3097  Real v_cc = (sum_vf_v > zero) ? (v_arr(i,j,k)*vf_v_lo + v_arr(i,j+1,k)*vf_v_hi) / sum_vf_v : zero;
3098 
3099  Real vf_w_lo = w_vf_arr(i,j,k);
3100  Real vf_w_hi = w_vf_arr(i,j,k+1);
3101  Real sum_vf_w = vf_w_lo + vf_w_hi;
3102  Real w_cc = (sum_vf_w > zero) ? (w_arr(i,j,k)*vf_w_lo + w_arr(i,j,k+1)*vf_w_hi) / sum_vf_w : zero;
3103 
3104  // Get normal vector components
3105  Real nx = bnorm_arr(i,j,k,0);
3106  Real ny = bnorm_arr(i,j,k,1);
3107  Real nz = bnorm_arr(i,j,k,2);
3108 
3109  // Compute tangential velocity components
3110  Real v_dot_n = u_cc*nx + v_cc*ny + w_cc*nz;
3111  Real u_tangent = u_cc - v_dot_n * nx;
3112  Real v_tangent = v_cc - v_dot_n * ny;
3113  Real mag = std::sqrt(u_tangent*u_tangent + v_tangent*v_tangent + Vsg*Vsg);
3114 
3115  // Area-weighted sum
3116  val_u = u_tangent * area;
3117  val_v = v_tangent * area;
3118  val_mag = mag * area;
3119  }
3120  // The reduction is collective across the active GPU block.
3121  Gpu::deviceReduceSum(&plane_avg[0], val_u, handler);
3122  Gpu::deviceReduceSum(&plane_avg[1], val_v, handler);
3123  Gpu::deviceReduceSum(&plane_avg[iavg], val_mag, handler);
3124  });
3125  }
3126  }
3127 
3128  //
3129  //----------------------------------------------------------
3130  // Averages for T,Qv (cell-centered scalars)
3131  //----------------------------------------------------------
3132  //
3133  for (int imf(3); imf < 5; ++imf) {
3134 
3135  // Continue if no valid Qv pointer
3136  if (!fields[imf]) continue;
3137 
3138  denom[imf] = one / m_total_bndry_area[lev][imf];
3139  val_old[imf] = plane_average[imf]*d_fact_old;
3140 
3141  //
3142  // NOTE: deliberately not threaded. The body is a pure reduction into the shared
3143  // plane_avg entries, and on the host Gpu::deviceReduceSum is an unordered
3144  // "#pragma omp atomic" add. Threading it would make the surface-layer averages
3145  // -- and through them the surface fluxes -- depend on thread scheduling in the
3146  // last bits. See the same note in PlaneAverage::compute_averages.
3147  //
3148  for (MFIter mfi(*fields[imf], TileNoZ()); mfi.isValid(); ++mfi) {
3149  const auto& flag = cc_flags[mfi];
3150 
3151  // Skip boxes that are not singlevalued (MultiCutFab only has data for singlevalued boxes)
3152  if (flag.getType() != FabType::singlevalued) continue;
3153 
3154  Box bx = mfi.tilebox(); // Full 3D box
3155  auto const flag_arr = flag.const_array();
3156  auto const afrac_x = cc_afrac[0]->const_array(mfi);
3157  auto const afrac_y = cc_afrac[1]->const_array(mfi);
3158  auto const afrac_z = cc_afrac[2]->const_array(mfi);
3159  auto const mf_arr = fields[imf]->const_array(mfi);
3160 
3161  ParallelFor(Gpu::KernelInfo().setReduction(true), bx, [=]
3162  AMREX_GPU_DEVICE(int i, int j, int k, Gpu::Handler const& handler) noexcept
3163  {
3164  Real val = zero;
3165  // Area-weighted averaging over cut cells at any k
3166  if (flag_arr(i,j,k).isSingleValued()) {
3167  // Compute area from face-centered area fractions
3168  Real axm = afrac_x(i ,j ,k );
3169  Real axp = afrac_x(i+1,j ,k );
3170  Real aym = afrac_y(i ,j ,k );
3171  Real ayp = afrac_y(i ,j+1,k );
3172  Real azm = afrac_z(i ,j ,k );
3173  Real azp = afrac_z(i ,j ,k+1);
3174 
3175  Real adx = (axm - axp) * dy * dz;
3176  Real ady = (aym - ayp) * dx * dz;
3177  Real adz = (azm - azp) * dx * dy;
3178 
3179  Real area = std::sqrt(adx*adx + ady*ady + adz*adz);
3180 
3181  val = mf_arr(i,j,k) * area;
3182  }
3183  Gpu::deviceReduceSum(&plane_avg[imf], val, handler);
3184  });
3185  }
3186  }
3187 
3188  //
3189  //------------------------------------------------------------------------
3190  // Averages for virtual potential temperature
3191  //------------------------------------------------------------------------
3192  //
3193  if (fields[4]) // We have water vapor
3194  {
3195  int iavg = 5;
3196  denom[iavg] = one / m_total_bndry_area[lev][iavg];
3197  val_old[iavg] = plane_average[iavg]*d_fact_old;
3198 
3199  //
3200  // NOTE: deliberately not threaded. The body is a pure reduction into the shared
3201  // plane_avg entries, and on the host Gpu::deviceReduceSum is an unordered
3202  // "#pragma omp atomic" add. Threading it would make the surface-layer averages
3203  // -- and through them the surface fluxes -- depend on thread scheduling in the
3204  // last bits. See the same note in PlaneAverage::compute_averages.
3205  //
3206  for (MFIter mfi(*fields[4], TileNoZ()); mfi.isValid(); ++mfi)
3207  {
3208  const auto& flag = cc_flags[mfi];
3209 
3210  // Skip boxes that are not singlevalued (MultiCutFab only has data for singlevalued boxes)
3211  if (flag.getType() != FabType::singlevalued) continue;
3212 
3213  Box bx = mfi.tilebox(); // Full 3D box
3214  auto const flag_arr = flag.const_array();
3215  auto const afrac_x = cc_afrac[0]->const_array(mfi);
3216  auto const afrac_y = cc_afrac[1]->const_array(mfi);
3217  auto const afrac_z = cc_afrac[2]->const_array(mfi);
3218 
3219  const Array4<Real const> T_mf_arr = fields[3]->const_array(mfi);
3220  const Array4<Real const> qv_mf_arr = fields[4]->const_array(mfi);
3221  const Array4<Real const> qr_mf_arr = (fields[5]) ? fields[5]->const_array(mfi) :
3222  Array4<const Real> {};
3223 
3224  ParallelFor(Gpu::KernelInfo().setReduction(true), bx, [=]
3225  AMREX_GPU_DEVICE(int i, int j, int k, Gpu::Handler const& handler) noexcept
3226  {
3227  Real val = zero;
3228  if (flag_arr(i,j,k).isSingleValued()) {
3229  // Compute area from face-centered area fractions
3230  Real axm = afrac_x(i ,j ,k );
3231  Real axp = afrac_x(i+1,j ,k );
3232  Real aym = afrac_y(i ,j ,k );
3233  Real ayp = afrac_y(i ,j+1,k );
3234  Real azm = afrac_z(i ,j ,k );
3235  Real azp = afrac_z(i ,j ,k+1);
3236 
3237  Real adx = (axm - axp) * dy * dz;
3238  Real ady = (aym - ayp) * dx * dz;
3239  Real adz = (azm - azp) * dx * dy;
3240 
3241  Real area = std::sqrt(adx*adx + ady*ady + adz*adz);
3242 
3243  Real vfac;
3244  if (qr_mf_arr) {
3245  // We also have liquid water
3246  vfac = one + epsv*qv_mf_arr(i,j,k) - qr_mf_arr(i,j,k);
3247  } else {
3248  vfac = one + epsv*qv_mf_arr(i,j,k);
3249  }
3250  val = T_mf_arr(i,j,k) * vfac * area;
3251  }
3252  Gpu::deviceReduceSum(&plane_avg[iavg], val, handler);
3253  });
3254  }
3255  }
3256  else // copy temperature
3257  {
3258  int iavg = m_navg - 4;
3259  denom[iavg] = one / m_total_bndry_area[lev][iavg];
3260  // Dry virtual temperature shares temperature's filtered history.
3261  val_old[iavg] = val_old[3];
3262  Gpu::copy(Gpu::deviceToDevice, pavg.begin() + 3, pavg.begin() + 4,
3263  pavg.begin() + iavg);
3264  }
3265 
3266  // Copy to host and sum across procs
3267  Gpu::copy(Gpu::deviceToHost, pavg.begin(), pavg.end(), plane_average.begin());
3268  ParallelDescriptor::ReduceRealSum(plane_average.data(), plane_average.size());
3269 
3270  // Normalize by total area and apply time averaging
3271  for (int iavg(0); iavg < m_navg; ++iavg){
3272  plane_average[iavg] *= denom[iavg]*d_fact_new;
3273  plane_average[iavg] += val_old[iavg];
3274  averages[iavg]->setVal(plane_average[iavg]);
3275  }
3276 }
GpuArray< Real, AMREX_SPACEDIM > dx
Definition: ERF_ActuatorSpreading.cpp:131
constexpr amrex::Real epsv
Definition: ERF_Constants.H:56
Real vfac
Definition: ERF_InitCustomPertVels_ABL.H:42
const int nx
Definition: ERF_InitCustomPertVels_CloudChamber.H:14
const int ny
Definition: ERF_InitCustomPertVels_CloudChamber.H:15
const Real dy
Definition: ERF_InitCustomPert_ABL.H:101
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
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);})
constexpr amrex::Real one
Definition: ERF_NumericalConstants.H:37
constexpr amrex::Real zero
Definition: ERF_NumericalConstants.H:36
AMREX_FORCE_INLINE amrex::IntVect TileNoZ()
Definition: ERF_TileNoZ.H:11
int m_navg
Definition: ERF_MOSTAverage.H:726
amrex::Vector< amrex::Vector< std::unique_ptr< amrex::MultiFab > > > m_averages
Definition: ERF_MOSTAverage.H:737
amrex::Vector< amrex::Vector< amrex::Real > > m_total_bndry_area
Definition: ERF_MOSTAverage.H:760
amrex::Orientation m_face
Definition: ERF_MOSTAverage.H:713
amrex::Vector< amrex::Real > m_Vsg
Definition: ERF_MOSTAverage.H:772
amrex::Vector< amrex::Vector< amrex::Real > > m_plane_average
Definition: ERF_MOSTAverage.H:744
amrex::Real m_fact_new
Definition: ERF_MOSTAverage.H:767
amrex::Vector< amrex::Vector< amrex::MultiFab * > > m_fields
Definition: ERF_MOSTAverage.H:715
amrex::Vector< const eb_ * > m_eb_vec
Definition: ERF_MOSTAverage.H:759
amrex::Real m_fact_old
Definition: ERF_MOSTAverage.H:767
const amrex::Vector< amrex::Geometry > m_geom
Definition: ERF_MOSTAverage.H:714
@ dz
Definition: ERF_AdvanceWDM6.cpp:273

Referenced by compute_averages().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ compute_plane_averages()

void MOSTAverage::compute_plane_averages ( const int &  lev)

Fill averages for the plane policy.

Parameters
[in]levlevel index

Function to compute average over a plane.

Parameters
[in]levCurrent level
1725 {
1726  // Peel back the level
1727  auto& fields = m_fields[lev];
1728  auto& rot_fields = m_rot_fields[lev];
1729  auto& averages = m_averages[lev];
1730  const auto & geom = m_geom[lev];
1731 
1732  auto& z_phys = m_z_phys_nd[lev];
1733  auto& x_pos = m_x_pos[lev];
1734  auto& y_pos = m_y_pos[lev];
1735  auto& z_pos = m_z_pos[lev];
1736 
1737  auto& i_indx = m_i_indx[lev];
1738  auto& j_indx = m_j_indx[lev];
1739  auto& k_indx = m_k_indx[lev];
1740 
1741  auto& ncell_plane = m_ncell_plane[lev];
1742  auto& plane_average = m_plane_average[lev];
1743 
1744  // Set factors for time averaging
1745  Real d_fact_new, d_fact_old;
1746  if (m_t_avg && m_t_init[lev]) {
1747  d_fact_new = m_fact_new;
1748  d_fact_old = m_fact_old;
1749  } else {
1750  d_fact_new = one;
1751  d_fact_old = zero;
1752  }
1753 
1754 
1755  // GPU array to accumulate averages into
1756  Gpu::DeviceVector<Real> pavg(plane_average.size(), zero);
1757  Real* plane_avg = pavg.data();
1758 
1759  // Vectors for normalization and buffer storage
1760  Vector<Real> denom(plane_average.size(),zero);
1761  Vector<Real> val_old(plane_average.size(),zero);
1762 
1763  //
1764  //----------------------------------------------------------
1765  // Averages over all the fields
1766  //----------------------------------------------------------
1767  //
1768  Box domain = geom.Domain();
1769 
1770  Array<int,AMREX_SPACEDIM> is_per = {0,0,0};
1771  for (int idim(0); idim < AMREX_SPACEDIM; ++idim) {
1772  if (geom.isPeriodic(idim)) is_per[idim] = 1;
1773  }
1774 
1775  const int dir = m_face.coordDir();
1776  // The non-terrain reference is constant on a level. Do not index the
1777  // cell-centered k-index FAB with an MFIter from a staggered velocity FAB;
1778  // distributed BoxArrays have the same box ordering but different valid
1779  // extents. Spatial index arrays are retained for terrain-normal cases.
1780  const bool fitted_terrain =
1781  (m_terrain_type == TerrainType::StaticFittedMesh) ||
1782  (m_terrain_type == TerrainType::MovingFittedMesh);
1783  const bool use_spatial_indices =
1784  m_norm_vec || (dir == 2 && fitted_terrain);
1785  if (!use_spatial_indices) {
1787  m_k_indx[lev] != nullptr,
1788  "Plane averaging requires a reference-index field.");
1789  }
1790  const int wall_normal_ref = use_spatial_indices ? 0 : m_k_indx[lev]->min(0);
1791  // Averages for U,V,W,T,Qv (not Qc)
1792  for (int imf(0); imf < 5; ++imf) {
1793 
1794  int sm_index = 0;
1795  if (m_face.isLow()) {
1796  sm_index = m_geom[lev].Domain().smallEnd(dir);
1797  } else {
1798  sm_index = m_geom[lev].Domain().bigEnd(dir);
1799  if (imf < 3 && imf == dir) {
1800  sm_index += 1;
1801  }
1802  }
1803 
1804  const int normal_face_offset =
1805  (!m_face.isLow() && imf < 3 && imf == dir) ? 1 : 0;
1806 
1807  // Continue if no valid Qv pointer
1808  if (!fields[imf]) continue;
1809 
1810  denom[imf] = one / (Real)ncell_plane[imf];
1811  val_old[imf] = plane_average[imf]*d_fact_old;
1812 
1813  //
1814  // NOTE: deliberately not threaded. The body is a pure reduction into the shared
1815  // plane_avg entries, and on the host Gpu::deviceReduceSum is an unordered
1816  // "#pragma omp atomic" add. Threading it would make the surface-layer averages
1817  // -- and through them the surface fluxes -- depend on thread scheduling in the
1818  // last bits. See the same note in PlaneAverage::compute_averages.
1819  //
1820  for (MFIter mfi(*fields[imf], TileNoZ()); mfi.isValid(); ++mfi) {
1821  Box vbx = mfi.validbox(); // This is the grid (not tile)
1822  Box pbx = mfi.tilebox(); // This is the tile (not grid)
1823 
1824  if (use_spatial_indices) {
1825  if (m_face.isLow()) {
1826  if (vbx.smallEnd(dir) != sm_index ||
1827  pbx.smallEnd(dir) != sm_index) {
1828  continue;
1829  }
1830  } else {
1831  if (vbx.bigEnd(dir) != sm_index ||
1832  pbx.bigEnd(dir) != sm_index) {
1833  continue;
1834  }
1835  }
1836  } else {
1837  // For Cartesian averages, the source FAB must own the
1838  // requested reference plane, not necessarily the boundary
1839  // face. A distributed high-side FAB can own the face while
1840  // the reference plane is in another FAB.
1841  Box reference_box = pbx;
1842  reference_box.setRange(dir, wall_normal_ref);
1843  if (!pbx.contains(reference_box)) {
1844  continue;
1845  }
1846  }
1847 
1848  // Make planar since mfiter is over fields
1849  pbx.setRange(dir, use_spatial_indices ? sm_index : wall_normal_ref);
1850 
1851  // Avoid double counting nodal data by changing the high end when we are
1852  // at the high side of the grid (not just of the tile)
1853  IndexType ixt = averages[imf]->boxArray().ixType();
1854  for (int idim(0); idim < AMREX_SPACEDIM; ++idim) {
1855  if (idim != dir && ixt.nodeCentered(idim) &&
1856  pbx.bigEnd(idim) == vbx.bigEnd(idim)) {
1857  int dom_hi = domain.bigEnd(idim) + 1;
1858  if (pbx.bigEnd(idim) < dom_hi || is_per[idim]) {
1859  pbx.growHi(idim,-1);
1860  }
1861  }
1862  }
1863 
1864  auto mf_arr = (m_rotate && imf != 2) ? rot_fields[imf]->const_array(mfi) :
1865  fields[imf]->const_array(mfi);
1866 
1867  if (m_interp) {
1868  const auto plo = geom.ProbLoArray();
1869  const auto dxInv = geom.InvCellSizeArray();
1870  const auto z_phys_arr = z_phys->const_array(mfi);
1871  auto x_pos_arr = x_pos->array(mfi);
1872  auto y_pos_arr = y_pos->array(mfi);
1873  auto z_pos_arr = z_pos->array(mfi);
1874  ParallelFor(Gpu::KernelInfo().setReduction(true), pbx, [=]
1875  AMREX_GPU_DEVICE(int i, int j, int k, Gpu::Handler const& handler) noexcept
1876  {
1877  Real interp{0};
1878  trilinear_interp_T(x_pos_arr(i,j,k), y_pos_arr(i,j,k), z_pos_arr(i,j,k),
1879  &interp, mf_arr, z_phys_arr, plo, dxInv, 1);
1880  Real val = interp;
1881  Gpu::deviceReduceSum(&plane_avg[imf], val, handler);
1882  });
1883  } else {
1884  auto k_arr = use_spatial_indices
1885  ? k_indx->const_array(mfi) : Array4<const int>{};
1886  auto j_arr = j_indx ? j_indx->const_array(mfi) : Array4<const int> {};
1887  auto i_arr = i_indx ? i_indx->const_array(mfi) : Array4<const int> {};
1888  // Spatial index fields are populated in their ghost cells
1889  // for local averaging. Clamp to the FAB extent, not just
1890  // the valid box, so those terrain-dependent ghost indices
1891  // remain available.
1892  const Box k_box = use_spatial_indices
1893  ? k_indx->fabbox(mfi.index()) : Box{};
1894  ParallelFor(Gpu::KernelInfo().setReduction(true), pbx, [=]
1895  AMREX_GPU_DEVICE(int i, int j, int k, Gpu::Handler const& handler) noexcept
1896  {
1897  const int ki = use_spatial_indices
1898  ? max(k_box.smallEnd(0), min(k_box.bigEnd(0), i)) : i;
1899  const int kj = use_spatial_indices
1900  ? max(k_box.smallEnd(1), min(k_box.bigEnd(1), j)) : j;
1901  const int kk = use_spatial_indices
1902  ? max(k_box.smallEnd(2), min(k_box.bigEnd(2), k)) : k;
1903  const int ref = (use_spatial_indices
1904  ? k_arr(ki,kj,kk) : wall_normal_ref) + normal_face_offset;
1905  int mi = i_arr ? i_arr(ki,kj,k) : i;
1906  int mj = j_arr ? j_arr(ki,kj,k) : j;
1907  int mk = k;
1908  if (dir == 0) {
1909  mi = ref;
1910  } else if (dir == 1) {
1911  mj = ref;
1912  } else {
1913  mk = ref;
1914  }
1915  Real val = mf_arr(mi,mj,mk);
1916  Gpu::deviceReduceSum(&plane_avg[imf], val, handler);
1917  });
1918  }
1919  }
1920  }
1921 
1922  //
1923  //------------------------------------------------------------------------
1924  // Averages for virtual potential temperature
1925  // (This is cell-centered so we don't need to worry about double-counting)
1926  //------------------------------------------------------------------------
1927  //
1928  if (fields[4]) // We have water vapor
1929  {
1930  int sm_index = 0;
1931  if (m_face.isLow()) {
1932  sm_index = m_geom[lev].Domain().smallEnd(dir);
1933  } else {
1934  sm_index = m_geom[lev].Domain().bigEnd(dir);
1935  }
1936 
1937  int iavg = 5;
1938  denom[iavg] = one / (Real)ncell_plane[iavg];
1939  val_old[iavg] = plane_average[iavg]*d_fact_old;
1940 
1941  //
1942  // NOTE: deliberately not threaded. The body is a pure reduction into the shared
1943  // plane_avg entries, and on the host Gpu::deviceReduceSum is an unordered
1944  // "#pragma omp atomic" add. Threading it would make the surface-layer averages
1945  // -- and through them the surface fluxes -- depend on thread scheduling in the
1946  // last bits. See the same note in PlaneAverage::compute_averages.
1947  //
1948  for (MFIter mfi(*fields[4], TileNoZ()); mfi.isValid(); ++mfi)
1949  {
1950  Box pbx = mfi.tilebox();
1951  const Box vbx = mfi.validbox();
1952 
1953  if (use_spatial_indices) {
1954  if (m_face.isLow()) {
1955  if (vbx.smallEnd(dir) != sm_index ||
1956  pbx.smallEnd(dir) != sm_index) {
1957  continue;
1958  }
1959  } else {
1960  if (vbx.bigEnd(dir) != sm_index ||
1961  pbx.bigEnd(dir) != sm_index) {
1962  continue;
1963  }
1964  }
1965  } else {
1966  Box reference_box = pbx;
1967  reference_box.setRange(dir, wall_normal_ref);
1968  if (!pbx.contains(reference_box)) {
1969  continue;
1970  }
1971  }
1972 
1973  pbx.setRange(dir, use_spatial_indices ? sm_index : wall_normal_ref);
1974 
1975  const Array4<Real const>& T_mf_arr = fields[3]->const_array(mfi);
1976  const Array4<Real const>& qv_mf_arr = (fields[4])? fields[4]->const_array(mfi) : Array4<const Real>{};
1977  const Array4<Real const>& qr_mf_arr = (fields[5])? fields[5]->const_array(mfi) : Array4<const Real>{};
1978 
1979  if (m_interp) {
1980  const auto plo = m_geom[lev].ProbLoArray();
1981  const auto dxInv = m_geom[lev].InvCellSizeArray();
1982  const auto z_phys_arr = z_phys->const_array(mfi);
1983  auto x_pos_arr = x_pos->array(mfi);
1984  auto y_pos_arr = y_pos->array(mfi);
1985  auto z_pos_arr = z_pos->array(mfi);
1986  ParallelFor(Gpu::KernelInfo().setReduction(true), pbx, [=]
1987  AMREX_GPU_DEVICE(int i, int j, int k, Gpu::Handler const& handler) noexcept
1988  {
1989  Real T_interp{0};
1990  Real qv_interp{0};
1991  trilinear_interp_T(x_pos_arr(i,j,k), y_pos_arr(i,j,k), z_pos_arr(i,j,k),
1992  &T_interp, T_mf_arr, z_phys_arr, plo, dxInv, 1);
1993  trilinear_interp_T(x_pos_arr(i,j,k), y_pos_arr(i,j,k), z_pos_arr(i,j,k),
1994  &qv_interp, qv_mf_arr, z_phys_arr, plo, dxInv, 1);
1995  Real vfac;
1996  if (qr_mf_arr) {
1997  // We also have liquid water
1998  Real qr_interp{0};
1999  trilinear_interp_T(x_pos_arr(i,j,k), y_pos_arr(i,j,k), z_pos_arr(i,j,k),
2000  &qr_interp, qr_mf_arr, z_phys_arr, plo, dxInv, 1);
2001  vfac = one + epsv*qv_interp - qr_interp;
2002  } else {
2003  vfac = one + epsv*qv_interp;
2004  }
2005  const Real val = T_interp * vfac;
2006  Gpu::deviceReduceSum(&plane_avg[iavg], val, handler);
2007  });
2008  } else {
2009  auto k_arr = use_spatial_indices
2010  ? k_indx->const_array(mfi) : Array4<const int>{};
2011  auto j_arr = j_indx ? j_indx->const_array(mfi) : Array4<const int> {};
2012  auto i_arr = i_indx ? i_indx->const_array(mfi) : Array4<const int> {};
2013  const Box k_box = use_spatial_indices
2014  ? k_indx->fabbox(mfi.index()) : Box{};
2015  ParallelFor(Gpu::KernelInfo().setReduction(true), pbx, [=]
2016  AMREX_GPU_DEVICE(int i, int j, int k, Gpu::Handler const& handler) noexcept
2017  {
2018  const int ki = use_spatial_indices
2019  ? max(k_box.smallEnd(0), min(k_box.bigEnd(0), i)) : i;
2020  const int kj = use_spatial_indices
2021  ? max(k_box.smallEnd(1), min(k_box.bigEnd(1), j)) : j;
2022  const int kk = use_spatial_indices
2023  ? max(k_box.smallEnd(2), min(k_box.bigEnd(2), k)) : k;
2024  const int ref = use_spatial_indices
2025  ? k_arr(ki,kj,kk) : wall_normal_ref;
2026  int mi = i_arr ? i_arr(ki,kj,k) : i;
2027  int mj = j_arr ? j_arr(ki,kj,k) : j;
2028  int mk = k;
2029  if (dir == 0) {
2030  mi = ref;
2031  } else if (dir == 1) {
2032  mj = ref;
2033  } else {
2034  mk = ref;
2035  }
2036  Real vfac;
2037  if (qr_mf_arr) {
2038  // We also have liquid water
2039  vfac = one + epsv*qv_mf_arr(mi,mj,mk) - qr_mf_arr(mi,mj,mk);
2040  } else {
2041  vfac = one + epsv*qv_mf_arr(mi,mj,mk);
2042  }
2043  const Real val = T_mf_arr(mi,mj,mk) * vfac;
2044  Gpu::deviceReduceSum(&plane_avg[iavg], val, handler);
2045  });
2046  }
2047  }
2048  }
2049  else // copy temperature
2050  {
2051  int iavg = m_navg - 4;
2052  denom[iavg] = one / (Real)ncell_plane[iavg];
2053  // plane_avg[iavg] = plane_avg[2]
2054  Gpu::copy(Gpu::deviceToDevice, pavg.begin() + 3, pavg.begin() + 4,
2055  pavg.begin() + iavg);
2056  }
2057 
2058  //
2059  //------------------------------------------------------------------------
2060  // Averages for the tangential velocity magnitude
2061  // (This is cell-centered so we don't need to worry about double-counting)
2062  //------------------------------------------------------------------------
2063  //
2064  {
2065  int sm_index = 0;
2066  if (m_face.isLow()) {
2067  sm_index = m_geom[lev].Domain().smallEnd(dir);
2068  } else {
2069  sm_index = m_geom[lev].Domain().bigEnd(dir);
2070  }
2071 
2072  const int imf_cc = 3;
2073  const int imf = 0;
2074  const int iavg = m_navg - 3;
2075  const int iavg_xz = m_navg - 2;
2076  const int iavg_yz = m_navg - 1;
2077  denom[iavg] = one / (Real)ncell_plane[iavg];
2078  denom[iavg_xz] = one / (Real)ncell_plane[iavg_xz];
2079  denom[iavg_yz] = one / (Real)ncell_plane[iavg_yz];
2080  val_old[iavg] = plane_average[iavg]*d_fact_old;
2081  val_old[iavg_xz] = plane_average[iavg_xz]*d_fact_old;
2082  val_old[iavg_yz] = plane_average[iavg_yz]*d_fact_old;
2083 
2084  const Real Vsg = m_Vsg[lev];
2085 
2086  //
2087  // NOTE: deliberately not threaded. The body is a pure reduction into the shared
2088  // plane_avg entries, and on the host Gpu::deviceReduceSum is an unordered
2089  // "#pragma omp atomic" add. Threading it would make the surface-layer averages
2090  // -- and through them the surface fluxes -- depend on thread scheduling in the
2091  // last bits. See the same note in PlaneAverage::compute_averages.
2092  //
2093  for (MFIter mfi(*fields[imf_cc], TileNoZ()); mfi.isValid(); ++mfi)
2094  {
2095  Box pbx = mfi.tilebox();
2096  const Box vbx = mfi.validbox();
2097 
2098  if (use_spatial_indices) {
2099  if (m_face.isLow()) {
2100  if (vbx.smallEnd(dir) != sm_index ||
2101  pbx.smallEnd(dir) != sm_index) {
2102  continue;
2103  }
2104  } else {
2105  if (vbx.bigEnd(dir) != sm_index ||
2106  pbx.bigEnd(dir) != sm_index) {
2107  continue;
2108  }
2109  }
2110  } else {
2111  Box reference_box = pbx;
2112  reference_box.setRange(dir, wall_normal_ref);
2113  if (!pbx.contains(reference_box)) {
2114  continue;
2115  }
2116  }
2117 
2118  pbx.setRange(dir, use_spatial_indices ? sm_index : wall_normal_ref);
2119 
2120  // Last element is Umag and always cell centered
2121  auto u_mf_arr = (m_rotate) ? rot_fields[imf ]->const_array(mfi) :
2122  fields[imf ]->const_array(mfi);
2123  auto v_mf_arr = (m_rotate) ? rot_fields[imf+1]->const_array(mfi) :
2124  fields[imf+1]->const_array(mfi);
2125  auto w_mf_arr = fields[imf+2]->const_array(mfi);
2126 
2127  if (m_interp) {
2128  // TODO: trilinear interp needs to be fixed for X and Y faces
2129  const auto plo = m_geom[lev].ProbLoArray();
2130  const auto dxInv = m_geom[lev].InvCellSizeArray();
2131  const auto z_phys_arr = z_phys->const_array(mfi);
2132  auto x_pos_arr = x_pos->array(mfi);
2133  auto y_pos_arr = y_pos->array(mfi);
2134  auto z_pos_arr = z_pos->array(mfi);
2135  ParallelFor(Gpu::KernelInfo().setReduction(true), pbx, [=]
2136  AMREX_GPU_DEVICE(int i, int j, int k, Gpu::Handler const& handler) noexcept
2137  {
2138  Real u_interp{0};
2139  Real v_interp{0};
2140  trilinear_interp_T(x_pos_arr(i,j,k), y_pos_arr(i,j,k), z_pos_arr(i,j,k),
2141  &u_interp, u_mf_arr, z_phys_arr, plo, dxInv, 1);
2142  trilinear_interp_T(x_pos_arr(i,j,k), y_pos_arr(i,j,k), z_pos_arr(i,j,k),
2143  &v_interp, v_mf_arr, z_phys_arr, plo, dxInv, 1);
2144  const Real val = std::sqrt(u_interp*u_interp + v_interp*v_interp + Vsg*Vsg);
2145  Gpu::deviceReduceSum(&plane_avg[iavg], val, handler);
2146 
2147  if (dir < 2) {
2148  // averages for mean velocity on XZ and YZ planes
2149  Real w_interp{0};
2150  trilinear_interp_T(x_pos_arr(i,j,k), y_pos_arr(i,j,k), z_pos_arr(i,j,k),
2151  &w_interp, w_mf_arr, z_phys_arr, plo, dxInv, 1);
2152  const Real val_xz = std::sqrt(u_interp*u_interp + w_interp*w_interp + Vsg*Vsg);
2153  const Real val_yz = std::sqrt(v_interp*v_interp + w_interp*w_interp + Vsg*Vsg);
2154  Gpu::deviceReduceSum(&plane_avg[iavg_xz], val_xz, handler);
2155  Gpu::deviceReduceSum(&plane_avg[iavg_yz], val_yz, handler);
2156  }
2157  });
2158  } else {
2159  auto k_arr = use_spatial_indices
2160  ? k_indx->const_array(mfi) : Array4<const int>{};
2161  auto j_arr = j_indx ? j_indx->const_array(mfi) : Array4<const int> {};
2162  auto i_arr = i_indx ? i_indx->const_array(mfi) : Array4<const int> {};
2163  const Box k_box = use_spatial_indices
2164  ? k_indx->fabbox(mfi.index()) : Box{};
2165  ParallelFor(Gpu::KernelInfo().setReduction(true), pbx, [=]
2166  AMREX_GPU_DEVICE(int i, int j, int k, Gpu::Handler const& handler) noexcept
2167  {
2168  const int ki = use_spatial_indices
2169  ? max(k_box.smallEnd(0), min(k_box.bigEnd(0), i)) : i;
2170  const int kj = use_spatial_indices
2171  ? max(k_box.smallEnd(1), min(k_box.bigEnd(1), j)) : j;
2172  const int kk = use_spatial_indices
2173  ? max(k_box.smallEnd(2), min(k_box.bigEnd(2), k)) : k;
2174  const int ref = use_spatial_indices
2175  ? k_arr(ki,kj,kk) : wall_normal_ref;
2176  int mi = i_arr ? i_arr(ki,kj,k) : i;
2177  int mj = j_arr ? j_arr(ki,kj,k) : j;
2178  int mk = k;
2179  if (dir == 0) {
2180  mi = ref;
2181  } else if (dir == 1) {
2182  mj = ref;
2183  } else {
2184  mk = ref;
2185  }
2186  const Real u_val = myhalf * (u_mf_arr(mi,mj,mk) + u_mf_arr(mi+1,mj ,mk));
2187  const Real v_val = myhalf * (v_mf_arr(mi,mj,mk) + v_mf_arr(mi ,mj+1,mk));
2188  const Real val = std::sqrt(u_val*u_val + v_val*v_val + Vsg*Vsg);
2189  Gpu::deviceReduceSum(&plane_avg[iavg], val, handler);
2190 
2191  if (dir < 2) {
2192  // averages for mean velocity on XZ and YZ planes
2193  const Real w_val = myhalf * (w_mf_arr(mi,mj,mk) + w_mf_arr(mi,mj,mk+1));
2194  const Real val_xz = std::sqrt(u_val*u_val + w_val*w_val + Vsg*Vsg);
2195  const Real val_yz = std::sqrt(v_val*v_val + w_val*w_val + Vsg*Vsg);
2196  Gpu::deviceReduceSum(&plane_avg[iavg_xz], val_xz, handler);
2197  Gpu::deviceReduceSum(&plane_avg[iavg_yz], val_yz, handler);
2198  }
2199  });
2200  }
2201  }
2202  }
2203 
2204  // Copy to host and sum across procs
2205  Gpu::copy(Gpu::deviceToHost, pavg.begin(), pavg.end(), plane_average.begin());
2206  ParallelDescriptor::ReduceRealSum(plane_average.data(), static_cast<int>(plane_average.size()));
2207 
2208  // No spatial variation with plane averages
2209  for (int iavg(0); iavg < m_navg; ++iavg){
2210  plane_average[iavg] *= denom[iavg]*d_fact_new;
2211  plane_average[iavg] += val_old[iavg];
2212  averages[iavg]->setVal(plane_average[iavg]);
2213  }
2214 }
GpuArray< Real, AMREX_SPACEDIM > plo
Definition: ERF_ActuatorSpreading.cpp:131
amrex::GpuArray< Real, AMREX_SPACEDIM > dxInv
Definition: ERF_InitCustomPertVels_ParticleTests.H:16
amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_y_pos
Definition: ERF_MOSTAverage.H:732
amrex::Vector< std::unique_ptr< amrex::iMultiFab > > m_i_indx
Definition: ERF_MOSTAverage.H:734
amrex::Vector< amrex::MultiFab * > m_z_phys_nd
Definition: ERF_MOSTAverage.H:716
amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_x_pos
Definition: ERF_MOSTAverage.H:731
amrex::Vector< amrex::Vector< std::unique_ptr< amrex::MultiFab > > > m_rot_fields
Definition: ERF_MOSTAverage.H:739
amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_z_pos
Definition: ERF_MOSTAverage.H:733
amrex::Vector< amrex::Vector< int > > m_ncell_plane
Definition: ERF_MOSTAverage.H:743
TerrainType m_terrain_type
Definition: ERF_MOSTAverage.H:720
bool m_norm_vec
Definition: ERF_MOSTAverage.H:755
amrex::Vector< std::unique_ptr< amrex::iMultiFab > > m_j_indx
Definition: ERF_MOSTAverage.H:735
bool m_interp
Definition: ERF_MOSTAverage.H:754
AMREX_GPU_HOST_DEVICE static AMREX_INLINE void trilinear_interp_T(const amrex::Real &xp, const amrex::Real &yp, const amrex::Real &zp, amrex::Real *interp_vals, amrex::Array4< amrex::Real const > const &interp_array, amrex::Array4< amrex::Real const > const &z_arr, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &plo, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &dxi, const int interp_comp)
Definition: ERF_MOSTAverage.H:526
amrex::Vector< std::unique_ptr< amrex::iMultiFab > > m_k_indx
Definition: ERF_MOSTAverage.H:736

Referenced by compute_averages().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ compute_region_averages()

void MOSTAverage::compute_region_averages ( const int &  lev)

Fill averages for the point or region policy.

Parameters
[in]levlevel index

Function to compute average over local region.

Parameters
[in]levCurrent level
2343 {
2344  // Peel back the level
2345  auto& fields = m_fields[lev];
2346  auto& rot_fields = m_rot_fields[lev];
2347  auto& averages = m_averages[lev];
2348  const auto & geom = m_geom[lev];
2349 
2350  auto& z_phys = m_z_phys_nd[lev];
2351  auto& x_pos = m_x_pos[lev];
2352  auto& y_pos = m_y_pos[lev];
2353  auto& z_pos = m_z_pos[lev];
2354 
2355  auto& i_indx = m_i_indx[lev];
2356  auto& j_indx = m_j_indx[lev];
2357  auto& k_indx = m_k_indx[lev];
2358 
2359  const int dir = m_face.coordDir();
2360  // The non-terrain reference is constant on a level. Do not index the
2361  // cell-centered k-index FAB with an MFIter from a staggered velocity FAB;
2362  // distributed BoxArrays have the same box ordering but different valid
2363  // extents. Spatial index arrays are retained for terrain-normal cases.
2364  const bool fitted_terrain =
2365  (m_terrain_type == TerrainType::StaticFittedMesh) ||
2366  (m_terrain_type == TerrainType::MovingFittedMesh);
2367  const bool use_spatial_indices =
2368  m_norm_vec || (dir == 2 && fitted_terrain);
2369  if (!use_spatial_indices) {
2371  m_k_indx[lev] != nullptr,
2372  "Region averaging requires a reference-index field.");
2373  }
2374  const int wall_normal_ref = use_spatial_indices ? 0 : m_k_indx[lev]->min(0);
2375 
2376  // Set factors for time averaging
2377  Real d_fact_new, d_fact_old;
2378  if (m_t_avg && m_t_init[lev]) {
2379  d_fact_new = m_fact_new;
2380  d_fact_old = m_fact_old;
2381  } else {
2382  d_fact_new = one;
2383  d_fact_old = zero;
2384  }
2385 
2386  // Number of cells contained in the local average
2387  const Real denom = one / (Real) m_ncell_region;
2388 
2389  // Capture radius for device
2390  int d_radius = m_radius;
2391 
2392  // NOTE: With more than one level we must explicitly fill the ghost cells of
2393  // the averages. An isolated fine patch has no neighboring box for the
2394  // FillBoundary below to communicate with, while the fields do carry
2395  // valid ghost data (filled from the coarse level), so we compute the
2396  // averages there as well. Where fine boxes do abut, the FillBoundary
2397  // overwrites what we compute with the neighbor's valid data.
2398  const IntVect ng_fill = get_ng_fill(lev);
2399 
2400  //
2401  //----------------------------------------------------------
2402  // Averages for U,V,W,T,Qv
2403  //----------------------------------------------------------
2404  //
2405  for (int imf(0); imf < 5; ++imf) {
2406 
2407  int sm_index = 0;
2408  if (m_face.isLow()) {
2409  sm_index = m_geom[lev].Domain().smallEnd(dir);
2410  } else {
2411  sm_index = m_geom[lev].Domain().bigEnd(dir);
2412  if (imf < 3 && imf == dir) {
2413  sm_index += 1;
2414  }
2415  }
2416 
2417  const int normal_face_offset =
2418  (!m_face.isLow() && imf < 3 && imf == dir) ? 1 : 0;
2419 
2420  // Continue if no valid Qv pointer
2421  if (!fields[imf]) continue;
2422 
2423 #ifdef _OPENMP
2424 #pragma omp parallel if (Gpu::notInLaunchRegion())
2425 #endif
2426  for (MFIter mfi(*fields[imf], TileNoZ()); mfi.isValid(); ++mfi) {
2427  Box pbx = mfi.growntilebox(ng_fill);
2428 
2429  if (m_face.isLow()) {
2430  if (mfi.validbox().smallEnd(dir) != sm_index ||
2431  pbx.smallEnd(dir) != sm_index) {
2432  continue;
2433  }
2434  } else {
2435  if (mfi.validbox().bigEnd(dir) != sm_index ||
2436  pbx.bigEnd(dir) != sm_index) {
2437  continue;
2438  }
2439  }
2440 
2441  pbx.setSmall(dir, sm_index); pbx.setBig(dir, sm_index);
2442 
2443  auto mf_arr = (m_rotate && imf != 2) ? rot_fields[imf]->const_array(mfi) :
2444  fields[imf]->const_array(mfi);
2445  auto ma_arr = averages[imf]->array(mfi);
2446 
2447  if (m_interp) {
2448  const auto plo = geom.ProbLoArray();
2449  const auto dx = geom.CellSizeArray();
2450  const auto dxInv = geom.InvCellSizeArray();
2451  const auto z_phys_arr = z_phys->const_array(mfi);
2452  auto x_pos_arr = x_pos->array(mfi);
2453  auto y_pos_arr = y_pos->array(mfi);
2454  auto z_pos_arr = z_pos->array(mfi);
2455  ParallelFor(pbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
2456  {
2457  ma_arr(i,j,k) *= d_fact_old;
2458 
2459  Real met_h_zeta = Compute_h_zeta_AtCellCenter(i,j,k,dxInv,z_phys_arr);
2460  for (int lk(-d_radius); lk <= (d_radius); ++lk) {
2461  for (int lj(-d_radius); lj <= (d_radius); ++lj) {
2462  for (int li(-d_radius); li <= (d_radius); ++li) {
2463  Real interp{0};
2464  Real xp = x_pos_arr(i+li,j+lj,k);
2465  Real yp = y_pos_arr(i+li,j+lj,k);
2466  Real zp = z_pos_arr(i+li,j+lj,k) + met_h_zeta*lk*dx[2];
2467  trilinear_interp_T(xp, yp, zp, &interp, mf_arr, z_phys_arr, plo, dxInv, 1);
2468  Real val = denom * interp * d_fact_new;
2469  ma_arr(i,j,k) += val;
2470  }
2471  }
2472  }
2473  });
2474  } else {
2475  auto k_arr = use_spatial_indices
2476  ? k_indx->const_array(mfi) : Array4<const int>{};
2477  auto j_arr = j_indx ? j_indx->const_array(mfi) : Array4<const int> {};
2478  auto i_arr = i_indx ? i_indx->const_array(mfi) : Array4<const int> {};
2479  const Box k_box = use_spatial_indices
2480  ? k_indx->fabbox(mfi.index()) : Box{};
2481  ParallelFor(pbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
2482  {
2483  const int ki = use_spatial_indices
2484  ? max(k_box.smallEnd(0), min(k_box.bigEnd(0), i)) : i;
2485  const int kj = use_spatial_indices
2486  ? max(k_box.smallEnd(1), min(k_box.bigEnd(1), j)) : j;
2487  const int kk = use_spatial_indices
2488  ? max(k_box.smallEnd(2), min(k_box.bigEnd(2), k)) : k;
2489  const int ref = (use_spatial_indices
2490  ? k_arr(ki,kj,kk) : wall_normal_ref) + normal_face_offset;
2491  int mi = i_arr ? i_arr(ki,kj,k) : i;
2492  int mj = j_arr ? j_arr(ki,kj,k) : j;
2493  int mk = k;
2494  if (dir == 0) {
2495  mi = ref;
2496  } else if (dir == 1) {
2497  mj = ref;
2498  } else {
2499  mk = ref;
2500  }
2501 
2502  ma_arr(i,j,k) *= d_fact_old;
2503  for (int lk(mk-d_radius); lk <= (mk+d_radius); ++lk) {
2504  for (int lj(mj-d_radius); lj <= (mj+d_radius); ++lj) {
2505  for (int li(mi-d_radius); li <= (mi+d_radius); ++li) {
2506  Real val = denom * mf_arr(li, lj, lk) * d_fact_new;
2507  ma_arr(i,j,k) += val;
2508  }
2509  }
2510  }
2511  });
2512  }
2513  } // MFiter
2514 
2515  // Fill the ghost cells we could not compute above
2516  //***********************************************************************************
2517  extrap_ghost_cells(lev,imf,ng_fill);
2518 
2519  // Fill interior ghost cells and any ghost cells outside a periodic domain
2520  //***********************************************************************************
2521  fill_planar_boundary(lev, *averages[imf]);
2522  } // imf
2523 
2524  //
2525  //----------------------------------------------------------
2526  // Averages for virtual potential temperature
2527  //----------------------------------------------------------
2528  //
2529  if (fields[4]) // We have water vapor
2530  {
2531  int sm_index = 0;
2532  if (m_face.isLow()) {
2533  sm_index = m_geom[lev].Domain().smallEnd(dir);
2534  } else {
2535  sm_index = m_geom[lev].Domain().bigEnd(dir);
2536  }
2537 
2538  int iavg = 5;
2539 
2540 #ifdef _OPENMP
2541 #pragma omp parallel if (Gpu::notInLaunchRegion())
2542 #endif
2543  for (MFIter mfi(*fields[4], TileNoZ()); mfi.isValid(); ++mfi) {
2544  Box pbx = mfi.growntilebox(ng_fill);
2545 
2546  if (m_face.isLow()) {
2547  if (mfi.validbox().smallEnd(dir) != sm_index ||
2548  pbx.smallEnd(dir) != sm_index) {
2549  continue;
2550  }
2551  } else {
2552  if (mfi.validbox().bigEnd(dir) != sm_index ||
2553  pbx.bigEnd(dir) != sm_index) {
2554  continue;
2555  }
2556  }
2557 
2558  pbx.setSmall(dir, sm_index); pbx.setBig(dir, sm_index);
2559 
2560  const Array4<Real const>& T_mf_arr = fields[3]->const_array(mfi);
2561  const Array4<Real const>& qv_mf_arr = (fields[4])? fields[4]->const_array(mfi) : Array4<const Real>{};
2562  const Array4<Real const>& qr_mf_arr = (fields[5])? fields[5]->const_array(mfi) : Array4<const Real>{};
2563  auto ma_arr = averages[iavg]->array(mfi);
2564 
2565  if (m_interp) {
2566  const auto plo = geom.ProbLoArray();
2567  const auto dx = geom.CellSizeArray();
2568  const auto dxInv = geom.InvCellSizeArray();
2569  const auto z_phys_arr = z_phys->const_array(mfi);
2570  auto x_pos_arr = x_pos->array(mfi);
2571  auto y_pos_arr = y_pos->array(mfi);
2572  auto z_pos_arr = z_pos->array(mfi);
2573  ParallelFor(pbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
2574  {
2575  ma_arr(i,j,k) *= d_fact_old;
2576 
2577  Real met_h_zeta = Compute_h_zeta_AtCellCenter(i,j,k,dxInv,z_phys_arr);
2578  for (int lk(-d_radius); lk <= (d_radius); ++lk) {
2579  for (int lj(-d_radius); lj <= (d_radius); ++lj) {
2580  for (int li(-d_radius); li <= (d_radius); ++li) {
2581  Real T_interp{0};
2582  Real qv_interp{0};
2583  Real xp = x_pos_arr(i+li,j+lj,k);
2584  Real yp = y_pos_arr(i+li,j+lj,k);
2585  Real zp = z_pos_arr(i+li,j+lj,k) + met_h_zeta*lk*dx[2];
2586  trilinear_interp_T(xp, yp, zp, &T_interp, T_mf_arr, z_phys_arr, plo, dxInv, 1);
2587  trilinear_interp_T(xp, yp, zp, &qv_interp, qv_mf_arr, z_phys_arr, plo, dxInv, 1);
2588  Real vfac;
2589  if (qr_mf_arr) {
2590  // We also have liquid water
2591  Real qr_interp{0};
2592  trilinear_interp_T(x_pos_arr(i,j,k), y_pos_arr(i,j,k), z_pos_arr(i,j,k),
2593  &qr_interp, qr_mf_arr, z_phys_arr, plo, dxInv, 1);
2594  vfac = one + epsv*qv_interp - qr_interp;
2595  } else {
2596  vfac = one + epsv*qv_interp;
2597  }
2598  const Real mag = T_interp * vfac;
2599  const Real val = denom * mag * d_fact_new;
2600  ma_arr(i,j,k) += val;
2601  }
2602  }
2603  }
2604  });
2605  } else {
2606  auto k_arr = use_spatial_indices
2607  ? k_indx->const_array(mfi) : Array4<const int>{};
2608  auto j_arr = j_indx ? j_indx->const_array(mfi) : Array4<const int> {};
2609  auto i_arr = i_indx ? i_indx->const_array(mfi) : Array4<const int> {};
2610  const Box k_box = use_spatial_indices
2611  ? k_indx->fabbox(mfi.index()) : Box{};
2612  ParallelFor(pbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
2613  {
2614  const int ki = use_spatial_indices
2615  ? max(k_box.smallEnd(0), min(k_box.bigEnd(0), i)) : i;
2616  const int kj = use_spatial_indices
2617  ? max(k_box.smallEnd(1), min(k_box.bigEnd(1), j)) : j;
2618  const int kk = use_spatial_indices
2619  ? max(k_box.smallEnd(2), min(k_box.bigEnd(2), k)) : k;
2620  const int ref = use_spatial_indices
2621  ? k_arr(ki,kj,kk) : wall_normal_ref;
2622  int mi = i_arr ? i_arr(ki,kj,k) : i;
2623  int mj = j_arr ? j_arr(ki,kj,k) : j;
2624  int mk = k;
2625  if (dir == 0) {
2626  mi = ref;
2627  } else if (dir == 1) {
2628  mj = ref;
2629  } else {
2630  mk = ref;
2631  }
2632 
2633  ma_arr(i,j,k) *= d_fact_old;
2634  for (int lk(mk-d_radius); lk <= (mk+d_radius); ++lk) {
2635  for (int lj(mj-d_radius); lj <= (mj+d_radius); ++lj) {
2636  for (int li(mi-d_radius); li <= (mi+d_radius); ++li) {
2637  Real vfac;
2638  if (qr_mf_arr) {
2639  // We also have liquid water
2640  vfac = one + epsv*qv_mf_arr(li,lj,lk) - qr_mf_arr(li,lj,lk);
2641  } else {
2642  vfac = one + epsv*qv_mf_arr(li,lj,lk);
2643  }
2644  const Real mag = T_mf_arr(li,lj,lk) * vfac;
2645  const Real val = denom * mag * d_fact_new;
2646  ma_arr(i,j,k) += val;
2647  }
2648  }
2649  }
2650  });
2651  }
2652  } // MFiter
2653 
2654  // Fill the ghost cells we could not compute above
2655  //***********************************************************************************
2656  extrap_ghost_cells(lev,iavg,ng_fill);
2657 
2658  // Fill interior ghost cells and any ghost cells outside a periodic domain
2659  //***********************************************************************************
2660  fill_planar_boundary(lev, *averages[iavg]);
2661 
2662  }
2663  else // copy temperature
2664  {
2665  int iavg = m_navg - 4;
2666  IntVect ng = averages[iavg]->nGrowVect();
2667  MultiFab::Copy(*(averages[iavg]),*(averages[3]),0,0,1,ng);
2668  }
2669 
2670  //
2671  //----------------------------------------------------------
2672  // Averages for the tangential velocity magnitude
2673  //----------------------------------------------------------
2674  //
2675  {
2676  int sm_index = 0;
2677  if (m_face.isLow()) {
2678  sm_index = m_geom[lev].Domain().smallEnd(dir);
2679  } else {
2680  sm_index = m_geom[lev].Domain().bigEnd(dir);
2681  }
2682 
2683  const int imf_cc = 3;
2684  const int imf = 0;
2685  const int iavg = m_navg - 3;
2686  const int iavg_xz = m_navg - 2;
2687  const int iavg_yz = m_navg - 1;
2688 
2689  const Real Vsg = m_Vsg[lev];
2690 
2691 #ifdef _OPENMP
2692 #pragma omp parallel if (Gpu::notInLaunchRegion())
2693 #endif
2694  for (MFIter mfi(*fields[imf_cc], TileNoZ()); mfi.isValid(); ++mfi) {
2695  Box pbx = mfi.growntilebox(ng_fill);
2696 
2697  if (m_face.isLow()) {
2698  if (mfi.validbox().smallEnd(dir) != sm_index ||
2699  pbx.smallEnd(dir) != sm_index) {
2700  continue;
2701  }
2702  } else {
2703  if (mfi.validbox().bigEnd(dir) != sm_index ||
2704  pbx.bigEnd(dir) != sm_index) {
2705  continue;
2706  }
2707  }
2708 
2709  pbx.setSmall(dir, sm_index); pbx.setBig(dir, sm_index);
2710 
2711  auto u_mf_arr = (m_rotate) ? rot_fields[imf ]->const_array(mfi) :
2712  fields[imf ]->const_array(mfi);
2713  auto v_mf_arr = (m_rotate) ? rot_fields[imf+1]->const_array(mfi) :
2714  fields[imf+1]->const_array(mfi);
2715  auto w_mf_arr = fields[imf+2]->const_array(mfi);
2716  auto ma_arr = averages[iavg]->array(mfi);
2717  auto ma_xz_arr = averages[iavg_xz]->array(mfi);
2718  auto ma_yz_arr = averages[iavg_yz]->array(mfi);
2719 
2720 
2721  if (m_interp) {
2722  const auto plo = geom.ProbLoArray();
2723  const auto dx = geom.CellSizeArray();
2724  const auto dxInv = geom.InvCellSizeArray();
2725  const auto z_phys_arr = z_phys->const_array(mfi);
2726  auto x_pos_arr = x_pos->array(mfi);
2727  auto y_pos_arr = y_pos->array(mfi);
2728  auto z_pos_arr = z_pos->array(mfi);
2729  ParallelFor(pbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
2730  {
2731  ma_arr(i,j,k) *= d_fact_old;
2732 
2733  Real met_h_zeta = Compute_h_zeta_AtCellCenter(i,j,k,dxInv,z_phys_arr);
2734  for (int lk(-d_radius); lk <= (d_radius); ++lk) {
2735  for (int lj(-d_radius); lj <= (d_radius); ++lj) {
2736  for (int li(-d_radius); li <= (d_radius); ++li) {
2737  Real u_interp{0};
2738  Real v_interp{0};
2739  Real xp = x_pos_arr(i+li,j+lj,k);
2740  Real yp = y_pos_arr(i+li,j+lj,k);
2741  Real zp = z_pos_arr(i+li,j+lj,k) + met_h_zeta*lk*dx[2];
2742  trilinear_interp_T(xp, yp, zp, &u_interp, u_mf_arr, z_phys_arr, plo, dxInv, 1);
2743  trilinear_interp_T(xp, yp, zp, &v_interp, v_mf_arr, z_phys_arr, plo, dxInv, 1);
2744  const Real mag = std::sqrt(u_interp*u_interp + v_interp*v_interp + Vsg*Vsg);
2745  Real val = denom * mag * d_fact_new;
2746  ma_arr(i,j,k) += val;
2747  }
2748  }
2749  }
2750  });
2751  } else {
2752  auto k_arr = use_spatial_indices
2753  ? k_indx->const_array(mfi) : Array4<const int>{};
2754  auto j_arr = j_indx ? j_indx->const_array(mfi) : Array4<const int> {};
2755  auto i_arr = i_indx ? i_indx->const_array(mfi) : Array4<const int> {};
2756  const Box k_box = use_spatial_indices
2757  ? k_indx->fabbox(mfi.index()) : Box{};
2758  ParallelFor(pbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
2759  {
2760  const int ki = use_spatial_indices
2761  ? max(k_box.smallEnd(0), min(k_box.bigEnd(0), i)) : i;
2762  const int kj = use_spatial_indices
2763  ? max(k_box.smallEnd(1), min(k_box.bigEnd(1), j)) : j;
2764  const int kk = use_spatial_indices
2765  ? max(k_box.smallEnd(2), min(k_box.bigEnd(2), k)) : k;
2766  const int ref = use_spatial_indices
2767  ? k_arr(ki,kj,kk) : wall_normal_ref;
2768  int mi = i_arr ? i_arr(ki,kj,k) : i;
2769  int mj = j_arr ? j_arr(ki,kj,k) : j;
2770  int mk = k;
2771  if (dir == 0) {
2772  mi = ref;
2773  } else if (dir == 1) {
2774  mj = ref;
2775  } else {
2776  mk = ref;
2777  }
2778 
2779  ma_arr(i,j,k) *= d_fact_old;
2780 
2781  if (dir < 2) {
2782  ma_xz_arr(i,j,k) *= d_fact_old;
2783  ma_yz_arr(i,j,k) *= d_fact_old;
2784  } else {
2785  ma_xz_arr(i,j,k) = 0.0;
2786  ma_yz_arr(i,j,k) = 0.0;
2787  }
2788  for (int lk(mk-d_radius); lk <= (mk+d_radius); ++lk) {
2789  for (int lj(mj-d_radius); lj <= (mj+d_radius); ++lj) {
2790  for (int li(mi-d_radius); li <= (mi+d_radius); ++li) {
2791  const Real u_val = myhalf * (u_mf_arr(li,lj,lk) + u_mf_arr(li+1,lj ,lk));
2792  const Real v_val = myhalf * (v_mf_arr(li,lj,lk) + v_mf_arr(li ,lj+1,lk));
2793  const Real mag = std::sqrt(u_val*u_val + v_val*v_val + Vsg*Vsg);
2794  Real val = denom * mag * d_fact_new;
2795  ma_arr(i,j,k) += val;
2796 
2797  if (dir < 2) {
2798  // averages for mean velocity on XZ and YZ planes
2799  const Real w_val = myhalf * (w_mf_arr(li,lj,lk) + w_mf_arr(li,lj,lk+1));
2800  const Real val_xz = std::sqrt(u_val*u_val + w_val*w_val + Vsg*Vsg);
2801  const Real val_yz = std::sqrt(v_val*v_val + w_val*w_val + Vsg*Vsg);
2802 
2803  ma_xz_arr(i,j,k) += denom * val_xz * d_fact_new;
2804  ma_yz_arr(i,j,k) += denom * val_yz * d_fact_new;
2805  }
2806  }
2807  }
2808  }
2809  });
2810  }
2811  } // MFiter
2812 
2813  // Fill the ghost cells we could not compute above
2814  //***********************************************************************************
2815  extrap_ghost_cells(lev,iavg,ng_fill);
2816 
2817  // Fill interior ghost cells and any ghost cells outside a periodic domain
2818  //***********************************************************************************
2819  fill_planar_boundary(lev, *averages[iavg]);
2820  if (dir < 2) {
2821  fill_planar_boundary(lev, *averages[iavg_xz]);
2822  fill_planar_boundary(lev, *averages[iavg_yz]);
2823  }
2824  }
2825 
2826  // NOTE: Checking periodicity with the geom structure is not
2827  // sufficient at higher levels. The BA may be contained
2828  // within the domain and it's exterior ghost cells filled
2829  // from interpolation; yet the domain BCs are periodic.
2830 
2831  // Need to fill ghost cells outside the domain if not periodic
2832  bool not_per_x = !(geom.periodicity().isPeriodic(0));
2833  bool not_per_y = !(geom.periodicity().isPeriodic(1));
2834  const bool per_x = geom.periodicity().isPeriodic(0);
2835  const bool per_y = geom.periodicity().isPeriodic(1);
2836  const bool per_z = geom.periodicity().isPeriodic(2);
2837  Box cc_bnd_bx = (m_fields[lev][3]->boxArray()).minimalBox();
2838  Box domain = geom.Domain();
2839 
2840  if (domain.contains(cc_bnd_bx) || (not_per_x || not_per_y)) {
2841  for (int iavg(0); iavg < m_navg; ++iavg) {
2842  IntVect ng = averages[iavg]->nGrowVect();
2843 
2844  // NOTE: Level 0 spans the whole domain, but finer
2845  // levels do not have such a restriction.
2846  // For now, use the bounding box of the boxArray.
2847 
2848  // NOTE: The fields and averages have different indexing.
2849  // The averages are: U/V/W/T/Qv/Tv/Umag_XY/Umag_XZ/Umag_YZ
2850  // The fields are: U/V/W/T/Qv/Qr
2851  // We clip iavg at 3 since all the remaining data is CC
2852 
2853  int imf = min(iavg,3);
2854 
2855  // For a z face, tangential extrapolation follows the level's
2856  // actual BoxArray extent. Fine patches may occupy only part of
2857  // the domain, and their interior ghosts must remain untouched.
2858  Box bnd_bx = m_fields[lev][imf]->boxArray().minimalBox();
2859 
2860  // Domain box in the index type of the current source field.
2861  // This is the physical-domain skip extent, including the
2862  // high-side nodal offset for velocity fields.
2863  Box dom_bx = convert(domain, fields[imf]->boxArray().ixType());
2864 
2865  int sm_index = 0;
2866  if (m_face.isLow()) {
2867  sm_index = dom_bx.smallEnd(dir);
2868  } else {
2869  sm_index = dom_bx.bigEnd(dir);
2870  }
2871 #ifdef _OPENMP
2872 #pragma omp parallel if (Gpu::notInLaunchRegion())
2873 #endif
2874  for (MFIter mfi(*fields[imf], TileNoZ()); mfi.isValid(); ++mfi) {
2875  Box vbx = mfi.validbox();
2876  Box pbx = mfi.tilebox();
2877  Box gpbx = mfi.growntilebox(ng);
2878 
2879  if (dom_bx.contains(gpbx)) {
2880  continue;
2881  }
2882 
2883  if (m_face.isLow()) {
2884  if (vbx.smallEnd(dir) != sm_index ||
2885  pbx.smallEnd(dir) != sm_index) {
2886  continue;
2887  }
2888  gpbx.setBig(dir, sm_index);
2889  } else {
2890  if (vbx.bigEnd(dir) != sm_index ||
2891  pbx.bigEnd(dir) != sm_index) {
2892  continue;
2893  }
2894  gpbx.setSmall(dir, sm_index);
2895  }
2896 
2897  auto ma_arr = averages[iavg]->array(mfi);
2898 
2899  if (dir == 0) {
2900  int j_lo = dom_bx.smallEnd(1); int j_hi = dom_bx.bigEnd(1);
2901  int k_lo = dom_bx.smallEnd(2); int k_hi = dom_bx.bigEnd(2);
2902 
2903  ParallelFor(gpbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
2904  {
2905  // Periodic tangential ghosts were populated by the
2906  // FillBoundary above. Leave them untouched; this
2907  // kernel only supplies non-periodic ghost values.
2908  if ((per_y && (j < j_lo || j > j_hi)) ||
2909  (per_z && (k < k_lo || k > k_hi))) {
2910  return;
2911  }
2912 
2913  int lj, lk;
2914  lj = j < j_lo ? j_lo : j;
2915  lj = lj > j_hi ? j_hi : lj;
2916  lk = k < k_lo ? k_lo : k;
2917  lk = lk > k_hi ? k_hi : lk;
2918 
2919  ma_arr(i,j,k) = ma_arr(sm_index,lj,lk);
2920  });
2921  } else if (dir == 1) {
2922  int i_lo = dom_bx.smallEnd(0); int i_hi = dom_bx.bigEnd(0);
2923  int k_lo = dom_bx.smallEnd(2); int k_hi = dom_bx.bigEnd(2);
2924  ParallelFor(gpbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
2925  {
2926  // Periodic tangential ghosts were populated by the
2927  // FillBoundary above. Leave them untouched; this
2928  // kernel only supplies non-periodic ghost values.
2929  if ((per_x && (i < i_lo || i > i_hi)) ||
2930  (per_z && (k < k_lo || k > k_hi))) {
2931  return;
2932  }
2933 
2934  int li, lk;
2935  li = i < i_lo ? i_lo : i;
2936  li = li > i_hi ? i_hi : li;
2937  lk = k < k_lo ? k_lo : k;
2938  lk = lk > k_hi ? k_hi : lk;
2939 
2940  ma_arr(i,j,k) = ma_arr(li,sm_index,lk);
2941  });
2942  } else {
2943  int i_lo = bnd_bx.smallEnd(0); int i_hi = bnd_bx.bigEnd(0);
2944  int j_lo = bnd_bx.smallEnd(1); int j_hi = bnd_bx.bigEnd(1);
2945  ParallelFor(gpbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
2946  {
2947  // Periodic tangential ghosts were populated by the
2948  // FillBoundary above. Leave them untouched; this
2949  // kernel only supplies non-periodic ghost values.
2950  if ((per_x && (i < i_lo || i > i_hi)) ||
2951  (per_y && (j < j_lo || j > j_hi))) {
2952  return;
2953  }
2954 
2955  int li, lj;
2956  li = i < i_lo ? i_lo : i;
2957  li = li > i_hi ? i_hi : li;
2958  lj = j < j_lo ? j_lo : j;
2959  lj = lj > j_hi ? j_hi : lj;
2960 
2961  ma_arr(i,j,k) = ma_arr(li,lj,sm_index);
2962  });
2963  }
2964  } // MFiter
2965  } // iavg
2966  } // Not periodic
2967 }
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real Compute_h_zeta_AtCellCenter(const int &i, const int &j, const int &k, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &cellSizeInv, const amrex::Array4< const amrex::Real > &z_nd)
Definition: ERF_TerrainMetrics.H:202
int m_radius
Definition: ERF_MOSTAverage.H:748
void fill_planar_boundary(const int &lev, amrex::MultiFab &mf)
Definition: ERF_MOSTAverage.cpp:2305
int m_ncell_region
Definition: ERF_MOSTAverage.H:749
void extrap_ghost_cells(const int &lev, const int &iavg, const amrex::IntVect &ng_fill)
Definition: ERF_MOSTAverage.cpp:2232
amrex::IntVect get_ng_fill(const int &lev) const
Definition: ERF_MOSTAverage.cpp:505
@ ng
Definition: ERF_Morrison.H:51

Referenced by compute_averages().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ do_time_averaging()

bool MOSTAverage::do_time_averaging ( ) const
inline

Return whether the averages are filtered in time (erf.most.time_average).

286 { return m_t_avg; }

Referenced by SurfaceLayer::mac_avg_is_time_averaged().

Here is the caller graph for this function:

◆ extrap_ghost_cells()

void MOSTAverage::extrap_ghost_cells ( const int &  lev,
const int &  iavg,
const amrex::IntVect &  ng_fill 
)

Fill the ghost cells of one average that cannot be computed from the field data with a zeroth-order extrapolation of the nearest computed value.

The averages hold as many ghost cells as the fields, which is more than can be computed from them (see get_ng_fill), so the outermost layers are filled here to leave no average undefined. This is done before the FillBoundary, which then overwrites whatever is shared with a neighbor.

Parameters
[in]levlevel index
[in]iavgaverage component index
[in]ng_fillnumber of ghost cells that hold computed data

Function to fill the ghost cells of one average that cannot be computed from the field data with a zeroth-order extrapolation of the nearest computed value.

The averages hold as many ghost cells as the fields, which is more than can be computed from them (see get_ng_fill), so the outermost layers are filled here to leave no average undefined. This is done before the FillBoundary, which then overwrites whatever is shared with a neighboring box.

Parameters
[in]levCurrent level
[in]iavgAverage component
[in]ng_fillNumber of ghost cells holding computed data
2235 {
2236  // Peel back the level
2237  auto& averages = m_averages[lev];
2238 
2239  // Extrapolation is performed directly in each average's own index space;
2240  // the source fields use different centering and are not needed here.
2241 
2242  IntVect ng = averages[iavg]->nGrowVect();
2243  // Only the selected wall-normal direction is collapsed. For x/y faces
2244  // the z ghosts are tangential and must be extrapolated/fillable.
2245  ng[m_face.coordDir()] = 0;
2246 
2247  // Everything we hold was computed above
2248  if (ng.allLE(ng_fill)) { return; }
2249 
2250  IntVect ng_computed = ng_fill;
2251  ng_computed[m_face.coordDir()] = 0;
2252 
2253 #ifdef _OPENMP
2254 #pragma omp parallel if (Gpu::notInLaunchRegion())
2255 #endif
2256  for (MFIter mfi(*averages[iavg], false); mfi.isValid(); ++mfi) {
2257  // Use the average's index space. The source field and the average
2258  // have different layouts for U/V/W and for the selected wall face.
2259  const Box avg_fab = averages[iavg]->fabbox(mfi.index());
2260  Box gpbx = averages[iavg]->boxArray()[mfi.index()];
2261  gpbx.grow(ng);
2262  gpbx &= avg_fab;
2263 
2264  // Region of this average FAB that was computed from field data.
2265  Box cbx = averages[iavg]->boxArray()[mfi.index()];
2266  cbx.grow(ng_computed);
2267  cbx &= avg_fab;
2268 
2269  if (cbx.contains(gpbx)) { continue; }
2270 
2271  auto ma_arr = averages[iavg]->array(mfi);
2272 
2273  int i_lo = cbx.smallEnd(0); int i_hi = cbx.bigEnd(0);
2274  int j_lo = cbx.smallEnd(1); int j_hi = cbx.bigEnd(1);
2275  int k_lo = cbx.smallEnd(2); int k_hi = cbx.bigEnd(2);
2276  ParallelFor(gpbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
2277  {
2278  int li, lj, lk;
2279  li = i < i_lo ? i_lo : i;
2280  li = li > i_hi ? i_hi : li;
2281  lj = j < j_lo ? j_lo : j;
2282  lj = lj > j_hi ? j_hi : lj;
2283  lk = k < k_lo ? k_lo : k;
2284  lk = lk > k_hi ? k_hi : lk;
2285 
2286  ma_arr(i,j,k) = ma_arr(li,lj,lk);
2287  });
2288  } // MFiter
2289 }

Referenced by compute_region_averages().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fill_planar_boundary()

void MOSTAverage::fill_planar_boundary ( const int &  lev,
amrex::MultiFab &  mf 
)

Fill the ghost cells of one planar average, and the valid region of its uncomputed copies when the 3D BoxArray is split in z (see PlanarBoundary).

Parameters
[in]levlevel index
[in,out]mfplanar average to fill

Function to fill the ghost cells of one planar average.

The averages hold one box per 3D box, so a 3D BoxArray split in the face-normal direction gives duplicate planar boxes, of which only the surface copy is computed by compute_region_averages; a FillBoundary could then fill a ghost cell from an uncomputed copy (see PlanarBoundary). With the split, the valid region of the uncomputed copies is filled as well. With EB terrain the averages are computed on every box and FillBoundary is well defined.

Parameters
[in]levCurrent level
[in,out]mfPlanar average to fill
2306 {
2307  const int dir = m_face.coordDir();
2308  const Periodicity period = tangential_periodicity(m_geom[lev], dir);
2309 
2310  if (m_terrain_type == TerrainType::EB) {
2311  mf.FillBoundary(mf.nGrowVect(), period);
2312  } else {
2313  m_planar_bndry[lev].fill(mf, period);
2314  }
2315 }
GpuArray< Real, AMREX_SPACEDIM > period
Definition: ERF_ActuatorSpreading.cpp:131
amrex::Vector< PlanarBoundary > m_planar_bndry
Definition: ERF_MOSTAverage.H:738

Referenced by compute_region_averages(), and fill_zref_planar().

Here is the caller graph for this function:

◆ fill_zref_planar()

void MOSTAverage::fill_zref_planar ( const int &  lev)

Copy the reference heights of the wall-owning planar boxes onto every planar box of the level and check that no column is left at the sentinel.

Parameters
[in]levCurrent level

Copy the reference heights of the wall-owning planar boxes onto every planar box.

Parameters
[in]levCurrent level

The lookups fill only the planar boxes whose 3D box touches the wall. When the level is split in the wall-normal direction the planar BoxArray holds duplicates of those boxes, which would otherwise keep the sentinel: a plain FillBoundary may copy it into a ghost cell, and min() would report it. No column may be left at the sentinel afterwards.

2330 {
2331  fill_planar_boundary(lev, *m_zref[lev]);
2333  "MOSTAverage: a column has no reference height");
2334 }
amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_zref
Definition: ERF_MOSTAverage.H:730

Referenced by set_k_indices_T(), set_norm_indices_T(), set_norm_positions_T(), and set_z_positions_T().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ find_interp_height_T()

AMREX_GPU_HOST_DEVICE static AMREX_INLINE bool MOSTAverage::find_interp_height_T ( const amrex::Real &  xp,
const amrex::Real &  yp,
const amrex::Real &  zp,
amrex::Array4< amrex::Real const > const &  z_arr,
const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &  plo,
const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &  dxi,
amrex::Real &  ireal,
amrex::Real &  jreal,
amrex::Real &  zval 
)
inlinestatic

Find the vertical position, in cell units, of a point for trilinear_interp_T: floor(zval) is the upper of the two cells whose centers bracket the point, and the interpolation reads that cell and the one below it.

Parameters
[in]xpX-position
[in]ypY-position
[in]zpZ-position
[in]z_arrPhysical heights
[in]ploProblem lower bounds
[in]dxiInverse cell size array
[out]irealx-position in cell units
[out]jrealy-position in cell units
[out]zvalz-position in cell units
Returns
whether the column holds the point
459  {
460  // Search to get z/k
461  bool found = false;
462  int kmax = ubound(z_arr).z;
463  zval = zero;
464  amrex::Real z_target = zp;
465 
466  // Map position to i,j (must be same mapping in cpp file)
467  ireal = (xp - plo[0]) * dxi[0];
468  jreal = (yp - plo[1]) * dxi[1];
469  int i_new = (int) (ireal - myhalf);
470  int j_new = (int) (jreal - myhalf);
471 
472  for (int lk(0); lk<kmax; ++lk) {
473  amrex::Real z_lo = fourth * ( z_arr(i_new,j_new ,lk ) + z_arr(i_new+1,j_new ,lk )
474  + z_arr(i_new,j_new+1,lk ) + z_arr(i_new+1,j_new+1,lk ) );
475  amrex::Real z_hi = fourth * ( z_arr(i_new,j_new ,lk+1) + z_arr(i_new+1,j_new ,lk+1)
476  + z_arr(i_new,j_new+1,lk+1) + z_arr(i_new+1,j_new+1,lk+1) );
477  if (in_cell_z(z_target, z_lo, z_hi)) {
478  found = true;
479  // Weight between the two cell centers that bracket z_target by
480  // their physical heights, so the interpolation is linear in z
481  // on a stretched or terrain-following column. A weight taken
482  // from the fraction of the containing cell alone (as before)
483  // is exact only for equal cell heights: at the 10 m face of a
484  // 10 m / 11 m pair it returned the wind at 10.25 m. A center
485  // beyond the nodes searched here is mirrored about the shared
486  // face, which gives the equal-height weight at those ends.
487  amrex::Real zc = myhalf * (z_lo + z_hi);
488  if (z_target >= zc) {
489  amrex::Real zc_up = two * z_hi - zc;
490  if (lk + 2 <= kmax) {
491  amrex::Real z_up = fourth * ( z_arr(i_new,j_new ,lk+2) + z_arr(i_new+1,j_new ,lk+2)
492  + z_arr(i_new,j_new+1,lk+2) + z_arr(i_new+1,j_new+1,lk+2) );
493  zc_up = myhalf * (z_hi + z_up);
494  }
495  zval = (amrex::Real) (lk + 1) + (z_target - zc) / (zc_up - zc);
496  } else {
497  amrex::Real zc_dn = two * z_lo - zc;
498  if (lk >= 1) {
499  amrex::Real z_dn = fourth * ( z_arr(i_new,j_new ,lk-1) + z_arr(i_new+1,j_new ,lk-1)
500  + z_arr(i_new,j_new+1,lk-1) + z_arr(i_new+1,j_new+1,lk-1) );
501  zc_dn = myhalf * (z_dn + z_lo);
502  }
503  zval = (amrex::Real) lk + (z_target - zc_dn) / (zc - zc_dn);
504  }
505  break;
506  }
507  }
508 
509  return found;
510  }
const Real zc
Definition: ERF_InitCustomPert_ABL.H:112
constexpr amrex::Real two
Definition: ERF_NumericalConstants.H:38
constexpr amrex::Real fourth
Definition: ERF_NumericalConstants.H:42
AMREX_GPU_HOST_DEVICE static AMREX_INLINE bool in_cell_z(const amrex::Real &z, const amrex::Real &z_lo, const amrex::Real &z_hi)
Definition: ERF_MOSTAverage.H:382

Referenced by max_k_read(), and trilinear_interp_T().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_average() [1/2]

amrex::MultiFab* MOSTAverage::get_average ( const int &  lev,
const int &  comp 
)
inline

Return one 2D average MultiFab for modification (used to restore the exponential filter state from a checkpoint).

Parameters
[in]levlevel index
[in]compaverage component index
281 { return m_averages[lev][comp].get(); }

◆ get_average() [2/2]

const amrex::MultiFab* MOSTAverage::get_average ( const int &  lev,
const int &  comp 
) const
inline

Return one 2D average MultiFab.

Parameters
[in]levlevel index
[in]compaverage component index
272 { return m_averages[lev][comp].get(); }

Referenced by SurfaceLayer::get_mac_avg(), and SurfaceLayer::get_mac_avg_ptr().

Here is the caller graph for this function:

◆ get_field_ptr()

const amrex::MultiFab* MOSTAverage::get_field_ptr ( const int &  lev,
const int &  comp 
) const
inline

Return the field the averages read.

Parameters
[in]levlevel index
[in]comp0 x-velocity, 1 y-velocity, 2 z-velocity, 3 theta, 4 qv, 5 qr
359  {
360  return m_fields[lev][comp];
361  }

◆ get_k_indices()

const amrex::iMultiFab* MOSTAverage::get_k_indices ( const int &  lev) const
inline

Return the k-index iMultiFab.

Parameters
[in]levlevel index
368 { return m_k_indx[lev].get(); }

◆ get_navg()

int MOSTAverage::get_navg ( ) const
inline

Return the number of average components.

291 { return m_navg; }

Referenced by SurfaceLayer::get_num_mac_avg().

Here is the caller graph for this function:

◆ get_ng_fill()

IntVect MOSTAverage::get_ng_fill ( const int &  lev) const

Return the number of ghost cells of the 2D data (averages, indices and positions) that can be filled from the field data at this level.

With more than one level we must explicitly fill the ghost cells of the 2D data: an isolated fine patch has no neighboring box for a FillBoundary to communicate with, while the fields themselves do carry valid ghost data (filled from the coarse level). The 2D data are allocated with as many ghost cells as the fields they are built from, but an average reads the fields over m_radius cells in each direction and the tangential velocity magnitude (like the interpolation stencil) reaches one cell beyond that. So a ghost cell of the 2D data can only be computed where the fields carry m_radius+1 ghost cells past it.

Parameters
[in]levlevel index

Function to return the number of ghost cells of the 2D data (averages, indices and positions) that can be filled from the field data.

With more than one level we must explicitly fill the ghost cells of the 2D data: an isolated fine patch has no neighboring box for a FillBoundary to communicate with, while the fields themselves do carry valid ghost data (filled from the coarse level). The 2D data are allocated with as many ghost cells as the fields they are built from, but an average reads the fields over m_radius cells in each direction and the tangential velocity magnitude (like the interpolation stencil) reaches one cell beyond that. So a ghost cell of the 2D data can only be computed where the fields carry m_radius+1 ghost cells past it.

Parameters
[in]levCurrent level
506 {
507  // The averages can hold no more than they were allocated with. Keep a
508  // separate limit for each direction: z is a tangential direction for
509  // x/y faces, while x/y are tangential for a z face.
510  IntVect ng_min = m_averages[lev][0]->nGrowVect();
511  for (int iavg(0); iavg < m_navg; ++iavg) {
512  const IntVect ng = m_averages[lev][iavg]->nGrowVect();
513  for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
514  ng_min[idim] = min(ng_min[idim], ng[idim]);
515  }
516  }
517 
518  // The velocities carry one fewer ghost cell than the CC fields, so they
519  // are what limits us in practice. Apply the same per-direction limit.
520  for (int imf(0); imf < m_nvar; ++imf) {
521  if (!m_fields[lev][imf]) { continue; }
522  const IntVect ng = m_fields[lev][imf]->nGrowVect();
523  for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
524  ng_min[idim] = min(ng_min[idim], ng[idim]);
525  }
526  }
527 
528  IntVect ng_fill(0);
529  for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
530  if (idim == m_face.coordDir()) { continue; }
531  // A radius-r average reads r cells in either direction. Staggered
532  // velocity interpolation reaches one additional cell on the high
533  // side, so that cell must also be available in the source field.
534  ng_fill[idim] = max(0, ng_min[idim] - (m_radius + 1));
535  }
536  return ng_fill;
537 }
int m_nvar
Definition: ERF_MOSTAverage.H:725

Referenced by compute_region_averages(), interp_stencil_bounds(), max_k_read(), set_k_indices_T(), set_norm_indices_T(), set_norm_positions_T(), and set_z_positions_T().

Here is the caller graph for this function:

◆ get_plane_average()

amrex::Vector<amrex::Real> MOSTAverage::get_plane_average ( const int &  lev) const
inline

Return the filtered plane averages, which hold the filter state for the plane and EB policies. The region policy holds its state in m_averages instead and never allocates these, in which case this is empty.

Parameters
[in]levlevel index
323  {
324  if (lev < static_cast<int>(m_plane_average.size())) { return m_plane_average[lev]; }
325  return amrex::Vector<amrex::Real>{};
326  }

Referenced by SurfaceLayer::get_mac_plane_avg().

Here is the caller graph for this function:

◆ get_zref()

amrex::MultiFab* MOSTAverage::get_zref ( const int &  lev) const
inline

Return z_ref, which may be computed from a specified k-index.

Parameters
[in]levlevel index
350 { return m_zref[lev].get(); }

Referenced by SurfaceLayer::get_zref(), SurfaceLayer::get_zref_min(), and SurfaceLayer::make_SurfaceLayer_at_level().

Here is the caller graph for this function:

◆ in_cell_z()

AMREX_GPU_HOST_DEVICE static AMREX_INLINE bool MOSTAverage::in_cell_z ( const amrex::Real &  z,
const amrex::Real &  z_lo,
const amrex::Real &  z_hi 
)
inlinestatic

Whether a height lies in the vertical extent [z_lo, z_hi) of a cell.

The interval is closed below so that a query height landing exactly on a face (e.g. the default 10 m on a flat mesh with a 10 m first cell) belongs to the cell above it rather than to neither cell.

Parameters
[in]zquery height
[in]z_loheight of the lower face
[in]z_hiheight of the upper face
385  {
386  return ( (z >= z_lo) && (z < z_hi) );
387  }

Referenced by find_interp_height_T(), and set_norm_indices_T().

Here is the caller graph for this function:

◆ interp_stencil_bounds()

MOSTAverage::InterpStencilBounds MOSTAverage::interp_stencil_bounds ( const int &  lev,
const amrex::MFIter &  mfi 
) const

Return the lateral bounds on the interpolation stencil in the box of mfi.

Parameters
[in]levlevel index
[in]mfiiterator over the cell-centered fields

Function to return the lateral bounds on the interpolation stencil in one box.

Parameters
[in]levCurrent level
[in]mfiIterator over the cell-centered fields
547 {
548  InterpStencilBounds bounds;
549 
550  // Columns each average reads: its pbx in compute_region_averages plus the radius
551  const IntVect ng_read = get_ng_fill(lev) + IntVect(m_radius,m_radius,0);
552  const Box& vbx = m_fields[lev][3]->boxArray()[mfi.index()];
553 
554  for (int imf(0); imf < 5; ++imf) {
555  if (!m_fields[lev][imf]) { continue; }
556  // The averages read the rotated fields where they exist (not for W)
557  const MultiFab* mf = (m_rotate && imf != 2 && m_rot_fields[lev][imf])
558  ? m_rot_fields[lev][imf].get() : m_fields[lev][imf];
559  const IndexType ixt = m_fields[lev][imf]->ixType();
560  bounds.reach[bounds.nfields] = amrex::grow(amrex::convert(vbx, ixt), ng_read);
561  bounds.held [bounds.nfields] = mf->fabbox(mfi.index());
562  ++bounds.nfields;
563  }
564  return bounds;
565 }
pp_for_pert_vels get("prob_type", prob_type)

Referenced by set_norm_positions_T().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ k_index_below()

static int MOSTAverage::k_index_below ( const amrex::Vector< amrex::Real > &  zlevels,
const amrex::Real &  zref 
)
inlinestatic

Index of the highest cell whose center lies at or below a height above the bottom of the domain, on a column of staggered (face) heights. This is the stretched-mesh counterpart of floor(zref/dz - 1/2) on a uniform mesh.

Parameters
[in]zlevelsstaggered heights of the column (nz+1 faces)
[in]zrefheight above zlevels[0]
Returns
the cell index, or -1 if zref lies below the first cell center
400  {
401  const int nz = static_cast<int>(zlevels.size()) - 1;
402  int lk = -1;
403  for (int k(0); k < nz; ++k) {
404  if (cell_center_height(zlevels, k) <= zref) { lk = k; } else { break; }
405  }
406  return lk;
407  }
static amrex::Real cell_center_height(const amrex::Vector< amrex::Real > &zlevels, const int &k)
Definition: ERF_MOSTAverage.H:416

Referenced by set_k_indices_N().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_MOSTAverage_at_level()

void MOSTAverage::make_MOSTAverage_at_level ( const int &  lev,
const amrex::Vector< amrex::MultiFab * > &  vars_old,
std::unique_ptr< amrex::MultiFab > &  Theta_prim,
std::unique_ptr< amrex::MultiFab > &  Qv_prim,
std::unique_ptr< amrex::MultiFab > &  Qr_prim,
std::unique_ptr< amrex::MultiFab > &  z_phys_nd,
const amrex::Vector< std::unique_ptr< amrex::MultiFab >> *  mapfac = nullptr 
)

Make MOST-average data structures at one level.

Parameters
[in]levlevel index
[in]vars_oldold-time state and velocity fields
[in]Theta_primprimitive potential-temperature field
[in]Qv_primprimitive water-vapor field
[in]Qr_primprimitive rain-water field
[in]z_phys_ndnodal physical-height field
[in]mapfacmap-factor fields at the level (MapFacType); none means m = 1

Make MOSTAverage data structures at one level.

Parameters
[in]levCurrent level
[in]vars_oldState data used by the average calculator
[in]Theta_primPrimitive theta component at this level
[in]Qv_primPrimitive water-vapor component at this level
[in]Qr_primPrimitive rain-water component at this level
[in]z_phys_ndNodal physical height at this level
181 {
182  m_fields[lev].resize(m_nvar);
183  m_rot_fields[lev].resize(m_nvar-1);
184  m_averages[lev].resize(m_navg);
185  m_z_phys_nd[lev] = z_phys_nd.get();
186 
187  // Map factors (empty means m = 1)
188  m_mapfac[lev].clear();
189  if (mapfac) {
190  m_mapfac[lev].resize(mapfac->size());
191  for (int i = 0; i < static_cast<int>(mapfac->size()); ++i) {
192  m_mapfac[lev][i] = (*mapfac)[i].get();
193  }
194  }
195 
196  bool use_terrain_fitted_coords = ( (m_terrain_type == TerrainType::StaticFittedMesh) ||
197  (m_terrain_type == TerrainType::MovingFittedMesh) );
198 
199  const int dir = m_face.coordDir();
200 
201  bool use_eb = (m_terrain_type == TerrainType::EB);
202 
203  { // Nodal in x
204  auto& mf = *vars_old[Vars::xvel];
205  // Create a 2D ba, dm, & ghost cells
206  int sm_index;
207  const BoxArray& ba = mf.boxArray();
208  const Box dom_bx = convert(m_geom[lev].Domain(), ba.ixType());
209  if (m_face.isLow()) {
210  sm_index = dom_bx.smallEnd(dir);
211  } else {
212  sm_index = dom_bx.bigEnd(dir);
213  }
214 
215  BoxList bl2d = ba.boxList();
216  for (auto& b : bl2d) { b.setRange(dir, sm_index); }
217  BoxArray ba2d(std::move(bl2d));
218  const DistributionMapping& dm = mf.DistributionMap();
219  const int ncomp = 1;
220  IntVect ng = mf.nGrowVect();
221  ng[dir] = 0;
222 
223  m_fields[lev][0] = vars_old[Vars::xvel];
224  m_averages[lev][0] = std::make_unique<MultiFab>(ba2d,dm,ncomp,ng);
225  m_averages[lev][0]->setVal(bogus_large_value);
226  if (m_rotate) {
227  m_rot_fields[lev][0] = std::make_unique<MultiFab>(ba,dm,ncomp,ng);
228  MultiFab::Copy(*m_rot_fields[lev][0],mf,0,0,1,ng);
229  } else {
230  m_rot_fields[lev][0] = nullptr;
231  }
232  }
233  { // Nodal in y
234  auto& mf = *vars_old[Vars::yvel];
235  // Create a 2D ba, dm, & ghost cells
236  int sm_index;
237  const BoxArray& ba = mf.boxArray();
238  const Box dom_bx = convert(m_geom[lev].Domain(), ba.ixType());
239 
240  if (m_face.isLow()) {
241  sm_index = dom_bx.smallEnd(dir);
242  } else {
243  sm_index = dom_bx.bigEnd(dir);
244  }
245 
246  BoxList bl2d = ba.boxList();
247  for (auto& b : bl2d) { b.setRange(dir, sm_index); }
248  BoxArray ba2d(std::move(bl2d));
249  const DistributionMapping& dm = mf.DistributionMap();
250  const int ncomp = 1;
251  IntVect ng = mf.nGrowVect();
252  ng[dir] = 0;
253 
254  m_fields[lev][1] = vars_old[Vars::yvel];
255  m_averages[lev][1] = std::make_unique<MultiFab>(ba2d,dm,ncomp,ng);
256  m_averages[lev][1]->setVal(bogus_large_value);
257  if (m_rotate) {
258  m_rot_fields[lev][1] = std::make_unique<MultiFab>(ba,dm,ncomp,ng);
259  MultiFab::Copy(*m_rot_fields[lev][1],mf,0,0,1,ng);
260  } else {
261  m_rot_fields[lev][1] = nullptr;
262  }
263  }
264  { // Nodal in z
265  auto& mf = *vars_old[Vars::zvel];
266  // Create a 2D ba, dm, & ghost cells
267  int sm_index;
268  const BoxArray& ba = mf.boxArray();
269  const Box dom_bx = convert(m_geom[lev].Domain(), ba.ixType());
270 
271  if (m_face.isLow()) {
272  sm_index = dom_bx.smallEnd(dir);
273  } else {
274  sm_index = dom_bx.bigEnd(dir);
275  }
276 
277  BoxList bl2d = ba.boxList();
278  for (auto& b : bl2d) b.setRange(dir, sm_index);
279  BoxArray ba2d(std::move(bl2d));
280  const DistributionMapping& dm = mf.DistributionMap();
281  const int ncomp = 1;
282  IntVect ng = mf.nGrowVect();
283  ng[dir] = 0;
284 
285  m_fields[lev][2] = vars_old[Vars::zvel];
286  m_averages[lev][2] = std::make_unique<MultiFab>(ba2d,dm,ncomp,ng);
287  m_averages[lev][2]->setVal(1.E34);
288  }
289  { // CC vars
290  auto& mf = *Theta_prim;
291  // Create a 2D ba, dm, & ghost cells
292  int sm_index;
293  if (m_face.isLow()) {
294  sm_index = m_geom[lev].Domain().smallEnd(dir);
295  } else {
296  sm_index = m_geom[lev].Domain().bigEnd(dir);
297  }
298 
299  const BoxArray& ba = mf.boxArray();
300  BoxList bl2d = ba.boxList();
301  for (auto& b : bl2d) { b.setRange(dir, sm_index); }
302  BoxArray ba2d(std::move(bl2d));
303  const DistributionMapping& dm = mf.DistributionMap();
304  const int ncomp = 1;
305  const int incomp = 1;
306  IntVect ng = mf.nGrowVect();
307  ng[dir] = 0;
308 
309  // Get field pointers
310  m_fields[lev][3] = Theta_prim.get();
311  m_fields[lev][4] = Qv_prim.get();
312  m_fields[lev][5] = Qr_prim.get();
313 
314  if (!use_eb) {
315  const int surface_index = m_face.isLow()
316  ? m_geom[lev].Domain().smallEnd(dir)
317  : m_geom[lev].Domain().bigEnd(dir);
318  m_planar_bndry[lev].define(ba, ba2d, dm, surface_index, m_face.isLow(), dir);
319  }
320 
321  // Initialize remaining multifabs
322  for (int iavg(3); iavg < m_navg; ++iavg) {
323  m_averages[lev][iavg] = std::make_unique<MultiFab>(ba2d,dm,ncomp,ng);
324  m_averages[lev][iavg]->setVal(bogus_large_value);
325  }
326 
327  // Default to dry
328  m_averages[lev][4]->setVal(0.0); // Qv
329 
330  if (m_rotate) {
331  m_rot_fields[lev][3] = std::make_unique<MultiFab>(ba,dm,ncomp,ng);
332  m_rot_fields[lev][4] = std::make_unique<MultiFab>(ba,dm,ncomp,ng);
333  MultiFab::Copy(*m_rot_fields[lev][3],*Theta_prim,0,0,1,ng);
334  if (Qv_prim) MultiFab::Copy(*m_rot_fields[lev][4],*Qv_prim,0,0,1,ng);
335  } else {
336  m_rot_fields[lev][3] = nullptr;
337  m_rot_fields[lev][4] = nullptr;
338  }
339 
340  // Filled by the lookup routine below; the sentinel marks a column it missed
341  m_zref[lev] = std::make_unique<MultiFab>(ba2d,dm,1,ng);
342  m_zref[lev]->setVal(zref_sentinel);
343 
344  if (use_terrain_fitted_coords && m_norm_vec && m_interp) {
345  m_x_pos[lev] = std::make_unique<MultiFab>(ba2d,dm,ncomp,ng);
346  m_y_pos[lev] = std::make_unique<MultiFab>(ba2d,dm,ncomp,ng);
347  m_z_pos[lev] = std::make_unique<MultiFab>(ba2d,dm,ncomp,ng);
348  } else if (use_terrain_fitted_coords && m_interp) {
349  m_x_pos[lev] = std::make_unique<MultiFab>(ba2d,dm,ncomp,ng);
350  m_y_pos[lev] = std::make_unique<MultiFab>(ba2d,dm,ncomp,ng);
351  m_z_pos[lev] = std::make_unique<MultiFab>(ba2d,dm,ncomp,ng);
352  } else if (use_terrain_fitted_coords && m_norm_vec) {
353  m_i_indx[lev] = std::make_unique<iMultiFab>(ba2d,dm,incomp,ng);
354  m_j_indx[lev] = std::make_unique<iMultiFab>(ba2d,dm,incomp,ng);
355  m_k_indx[lev] = std::make_unique<iMultiFab>(ba2d,dm,incomp,ng);
356  } else {
357  if (!use_eb) {
358  m_k_indx[lev] = std::make_unique<iMultiFab>(ba2d,dm,incomp,ng);
359  }
360  }
361  }
362 
363  // Setup auxiliary data for spatial configuration & policy
364  //--------------------------------------------------------
365  if (use_terrain_fitted_coords && m_norm_vec && m_interp) { // Terrain w/ norm & w/ interpolation
367  } else if (use_terrain_fitted_coords && m_interp) { // Terrain w/ interpolation
368  set_z_positions_T(lev);
369  } else if (use_terrain_fitted_coords && m_norm_vec) { // Terrain w/ norm & w/o interpolation
370  set_norm_indices_T(lev);
371  } else if (use_terrain_fitted_coords) { // Terrain
372  set_k_indices_T(lev);
373  } else if (use_eb) { // EB
374  set_z_positions_EB(lev);
375  } else { // No Terrain
376  set_k_indices_N(lev);
377  }
378 
379  // Check once that the direct lateral regional path can read the reference
380  // stencil from every face-owned FAB; distributed staging can replace this
381  // guard in the future.
383  // Report the reference height the surface layer will use at this level
384  {
385  const Real zref_min = m_zref[lev]->min(0);
386  const Real zref_max = m_zref[lev]->max(0);
387  Print() << "MOST reference height at level " << lev << ": " << zref_min;
388  if (zref_max > zref_min) { Print() << " to " << zref_max; }
389  Print() << std::endl;
390  }
391 
392  // Setup normalization data for the chosen policy
393  //--------------------------------------------------------
394  switch(m_policy) {
395  case 0: // Plane average
397  break;
398  case 1: // Local region/point
400  break;
401  case 2: // EB
403  break;
404  default:
405  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(false, "Unknown policy for MOSTAverage!");
406  }
407 
408  // Set up the exponential time filtering
409  //--------------------------------------------------------
410  if (m_t_avg) {
411  // Exponential filter function
412  m_fact_old = static_cast<amrex::Real>(std::exp(-1.0 / m_time_window));
413 
414  // Enforce discrete normalization: (mfn*val_new + mfo*val_old)
416 
417  // None of the averages are initialized
418  m_t_init.resize(m_maxlev,0);
419 
420  // We have just (re)built the average containers at this level, so whatever
421  // filter history this level held is gone: m_averages holds bogus_large_value
422  // and set_plane_normalization has zeroed m_plane_average. Note that resize
423  // above does not touch existing entries, so this must be set explicitly or a
424  // regrid would blend the bogus values into the filtered average. On restart
425  // ReadCheckpointFileSurfaceLayer restores the state and sets this back to 1.
426  m_t_init[lev] = 0;
427  }
428 
429  // Correction to the mean surface velocity at this level
430  m_Vsg[lev] = zero;
431  if (include_subgrid_vel) {
432  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(dir == 2, "Subgrid velocity correction only for Z faces!");
433  Print() << "Subgrid velocity scale correction at level : " << lev << ' ';
434  // dx in place of a dy over one cell, and the reverse
435  const auto dxArr = LengthScaleCellSize(m_geom[lev]);
436  Real dx = std::sqrt(dxArr[0]*dxArr[1]);
437 
438  // The physical cell is dx/m_x by dy/m_y: use the mean of 1/(m_x m_y) over the level
439  if (!m_mapfac[lev].empty()) {
440  const MultiFab& mf_mx = *m_mapfac[lev][MapFacType::m_x];
441  const MultiFab& mf_my = *m_mapfac[lev][MapFacType::m_y];
442  Real inv_area_sum = ReduceSum(mf_mx, mf_my, 0,
443  [=] AMREX_GPU_HOST_DEVICE (Box const& bx, Array4<Real const> const& mx,
444  Array4<Real const> const& my) -> Real
445  {
446  Real r = zero;
447  amrex::Loop(bx, [=,&r] (int i, int j, int k) noexcept
448  {
449  r += one / (mx(i,j,k) * my(i,j,k));
450  });
451  return r;
452  });
453  ParallelDescriptor::ReduceRealSum(inv_area_sum);
454  dx *= std::sqrt(inv_area_sum / static_cast<Real>(mf_mx.boxArray().numPts()));
455  }
456  if (dx > Real(5000.)) {
457  m_Vsg[lev] = Real(0.32) * std::pow(dx/Real(5000.)-1, Real(0.33));
458  }
459  Print() << m_Vsg[lev] << std::endl;
460  }
461 }
constexpr amrex::Real bogus_large_value
Definition: ERF_Constants.H:19
@ m_y
Definition: ERF_DataStruct.H:41
@ m_x
Definition: ERF_DataStruct.H:40
const bool use_eb
Definition: ERF_InitCustomPert_KE.H:8
amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > LengthScaleCellSize(const amrex::Geometry &geom)
Definition: ERF_LengthScaleCellSize.H:28
const amrex::Real zref_sentinel
Definition: ERF_MOSTAverage.H:789
double m_time_window
Definition: ERF_MOSTAverage.H:766
void set_z_positions_EB(const int &lev)
Definition: ERF_MOSTAverage.cpp:1150
void validate_lateral_reference_stencil(const int &lev)
Definition: ERF_MOSTAverage.cpp:1111
void set_region_normalization(const int &)
Definition: ERF_MOSTAverage.H:152
void set_z_positions_T(const int &lev)
Definition: ERF_MOSTAverage.cpp:1481
void set_norm_positions_T(const int &lev)
Definition: ERF_MOSTAverage.cpp:1560
void set_k_indices_T(const int &lev)
Definition: ERF_MOSTAverage.cpp:1177
void set_plane_normalization(const int &lev)
Definition: ERF_MOSTAverage.cpp:751
amrex::Vector< amrex::Vector< amrex::MultiFab * > > m_mapfac
Definition: ERF_MOSTAverage.H:717
void set_norm_indices_T(const int &lev)
Definition: ERF_MOSTAverage.cpp:1335
int m_maxlev
Definition: ERF_MOSTAverage.H:727
void set_eb_normalization(const int &lev)
Definition: ERF_MOSTAverage.cpp:809
bool include_subgrid_vel
Definition: ERF_MOSTAverage.H:771
void set_k_indices_N(const int &lev)
Definition: ERF_MOSTAverage.cpp:905
@ xvel
Definition: ERF_IndexDefines.H:232
@ zvel
Definition: ERF_IndexDefines.H:234
@ yvel
Definition: ERF_IndexDefines.H:233

Referenced by SurfaceLayer::make_SurfaceLayer_at_level().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ max_k_read()

int MOSTAverage::max_k_read ( const int &  lev) const

Return the highest k at which the averages on the z-low face read the horizontal velocities on this level: the reference index plus the radius, or with interpolation the upper of the two cells around each sample point. The bound is global over the level.

Parameters
[in]levlevel index

Function to return the highest k at which the averages on the z-low face read the horizontal velocities.

It follows the reads of compute_plane_averages and compute_region_averages: the columns of every box at the surface, over the faces of the U & V averages and the ghost cells the averages fill (get_ng_fill). Without interpolation a column reads its reference index plus the radius; with it, the upper of the two cells around each sample point of its region, the highest of which is m_radius cells along the normal. Without terrain the reference index is the same in every column.

Parameters
[in]levCurrent level
582 {
583  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(m_face.coordDir() == 2 && m_face.isLow(),
584  "MOSTAverage::max_k_read is only defined on the z-low face.");
585  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(m_policy != 2, "MOSTAverage::max_k_read does not cover EB averages.");
586 
587  const int klo = m_geom[lev].Domain().smallEnd(2);
588  const bool fitted_terrain =
589  (m_terrain_type == TerrainType::StaticFittedMesh) ||
590  (m_terrain_type == TerrainType::MovingFittedMesh);
591  const bool use_spatial_indices = m_norm_vec || fitted_terrain;
592  const int d_radius = m_radius;
593 
594  if (!m_interp && !use_spatial_indices) {
595  return m_k_indx[lev]->max(0) + d_radius;
596  }
597 
598  const IntVect ng_fill = get_ng_fill(lev);
599  const auto plo = m_geom[lev].ProbLoArray();
600  const auto dx = m_geom[lev].CellSizeArray();
601  const auto dxInv = m_geom[lev].InvCellSizeArray();
602 
603  ReduceOps<ReduceOpMax> reduce_op;
604  ReduceData<int> reduce_data(reduce_op);
605  using ReduceTuple = typename decltype(reduce_data)::Type;
606 
607  for (MFIter mfi(*m_fields[lev][3]); mfi.isValid(); ++mfi) {
608  const Box& vbx = mfi.validbox();
609  if (vbx.smallEnd(2) != klo) { continue; }
610 
611  Box bx = grow(convert(vbx, IntVect(1,1,0)), ng_fill);
612  bx.makeSlab(2, klo);
613 
614  if (m_interp) {
615  const auto z_phys_arr = m_z_phys_nd[lev]->const_array(mfi);
616  const auto x_pos_arr = m_x_pos[lev]->const_array(mfi);
617  const auto y_pos_arr = m_y_pos[lev]->const_array(mfi);
618  const auto z_pos_arr = m_z_pos[lev]->const_array(mfi);
619  reduce_op.eval(bx, reduce_data, [=] AMREX_GPU_DEVICE (int i, int j, int k) -> ReduceTuple
620  {
621  const Real met_h_zeta = Compute_h_zeta_AtCellCenter(i,j,k,dxInv,z_phys_arr);
622  int kmax = klo;
623  for (int lj(-d_radius); lj <= d_radius; ++lj) {
624  for (int li(-d_radius); li <= d_radius; ++li) {
625  const Real zp = z_pos_arr(i+li,j+lj,k) + met_h_zeta*d_radius*dx[2];
626  Real ireal, jreal, zval;
627  if (find_interp_height_T(x_pos_arr(i+li,j+lj,k), y_pos_arr(i+li,j+lj,k), zp,
628  z_phys_arr, plo, dxInv, ireal, jreal, zval)) {
629  kmax = amrex::max(kmax, static_cast<int>(std::floor(zval)));
630  }
631  }
632  }
633  return {kmax};
634  });
635  } else {
636  // The averages clamp the column to the reference-index box
637  const auto k_arr = m_k_indx[lev]->const_array(mfi);
638  const Box k_box = m_k_indx[lev]->fabbox(mfi.index());
639  reduce_op.eval(bx, reduce_data, [=] AMREX_GPU_DEVICE (int i, int j, int k) -> ReduceTuple
640  {
641  const int ki = amrex::max(k_box.smallEnd(0), amrex::min(k_box.bigEnd(0), i));
642  const int kj = amrex::max(k_box.smallEnd(1), amrex::min(k_box.bigEnd(1), j));
643  const int kk = amrex::max(k_box.smallEnd(2), amrex::min(k_box.bigEnd(2), k));
644  return {k_arr(ki,kj,kk) + d_radius};
645  });
646  }
647  }
648 
649  int kmax = amrex::max(klo, amrex::get<0>(reduce_data.value(reduce_op)));
650  ParallelDescriptor::ReduceIntMax(kmax);
651  return kmax;
652 }
const int klo
Definition: ERF_InitCustomPert_KE.H:9
AMREX_GPU_HOST_DEVICE static AMREX_INLINE bool find_interp_height_T(const amrex::Real &xp, const amrex::Real &yp, const amrex::Real &zp, amrex::Array4< amrex::Real const > const &z_arr, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &plo, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &dxi, amrex::Real &ireal, amrex::Real &jreal, amrex::Real &zval)
Definition: ERF_MOSTAverage.H:450
Here is the call graph for this function:

◆ max_k_read_can_change()

bool MOSTAverage::max_k_read_can_change ( ) const
inline

Return whether max_k_read can change after the level is made: the interpolation searches the current heights, which move with a moving mesh.

671  {
672  return m_interp && (m_terrain_type == TerrainType::MovingFittedMesh);
673  }

◆ operator=() [1/2]

MOSTAverage& MOSTAverage::operator= ( const MOSTAverage &  other)
delete

Deleted copy-assignment operator.

Parameters
[in]othersource object

◆ operator=() [2/2]

MOSTAverage& MOSTAverage::operator= ( MOSTAverage &&  other)
deletenoexcept

Deleted move-assignment operator.

Parameters
[in]othersource object

◆ set_eb_normalization()

void MOSTAverage::set_eb_normalization ( const int &  lev)

Compute total embedded-boundary surface area.

Parameters
[in]levlevel index

Function to compute normalization for average over EB.

Parameters
[in]levCurrent level
810 {
811  AMREX_ALWAYS_ASSERT(m_eb_vec[lev] != nullptr);
812 
813  // Get EB data - need both cell-centered and face-centered flags
814  const auto& cc_flags = m_eb_vec[lev]->get_const_factory()->getMultiEBCellFlagFab();
815 
816  // Get area fractions for different centerings
817  auto cc_afrac = m_eb_vec[lev]->get_const_factory()->getAreaFrac(); // Cell-centered area fractions (Array of 3 MultiCutFab*)
818 
819  // Initialize storage
821  m_total_bndry_area[lev].resize(m_navg, zero);
822  m_plane_average.resize(m_maxlev);
823  m_plane_average[lev].resize(m_navg, zero);
824 
825  // Compute total area for each field type based on its centering
826  // iavg: 0=U(xface), 1=V(yface), 2=W(zface), 3=T(cc), 4=Qv(cc),
827  // 5=Tv(cc), 6=Umag_XY(cc), 7=Umag_XZ(cc), 8=Umag_YZ(cc)
828 
829  // Get geometry for cell sizes
830  auto const& dx_arr = m_geom[lev].CellSizeArray();
831  Real dx = dx_arr[0];
832  Real dy = dx_arr[1];
833  Real dz = dx_arr[2];
834 
835  // GPU array to accumulate areas
836  Gpu::DeviceVector<Real> area_vec(m_navg, zero);
837  Real* area_device = area_vec.data();
838 
839  // All fields are now averaged on cell-centered grid
840  // Compute total area once on cell-centered grid and use for all iavg
841  Real total_area = zero;
842  //
843  // NOTE: deliberately not threaded. The body is a pure reduction into the shared
844  // area_device entry, and on the host Gpu::deviceReduceSum is an unordered
845  // "#pragma omp atomic" add. Threading it would make the cut-cell area -- which
846  // normalizes every EB surface-layer average -- depend on thread scheduling in the
847  // last bits. See the same note in PlaneAverage::compute_averages.
848  //
849  for (MFIter mfi(cc_flags, TileNoZ()); mfi.isValid(); ++mfi) {
850  const auto& flag = cc_flags[mfi];
851 
852  // Skip boxes that are not singlevalued (MultiCutFab only has data for singlevalued boxes)
853  if (flag.getType() != FabType::singlevalued) continue;
854 
855  Box bx = mfi.tilebox();
856  auto const flag_arr = flag.const_array();
857  auto const afrac_x = cc_afrac[0]->const_array(mfi);
858  auto const afrac_y = cc_afrac[1]->const_array(mfi);
859  auto const afrac_z = cc_afrac[2]->const_array(mfi);
860 
861  // Sum area only for cut cells using atomic reduction
862  ParallelFor(Gpu::KernelInfo().setReduction(true), bx, [=]
863  AMREX_GPU_DEVICE(int i, int j, int k, Gpu::Handler const& handler) noexcept
864  {
865  Real area = zero;
866  if (flag_arr(i,j,k).isSingleValued()) {
867  // Compute area from face-centered area fractions
868  Real axm = afrac_x(i ,j ,k );
869  Real axp = afrac_x(i+1,j ,k );
870  Real aym = afrac_y(i ,j ,k );
871  Real ayp = afrac_y(i ,j+1,k );
872  Real azm = afrac_z(i ,j ,k );
873  Real azp = afrac_z(i ,j ,k+1);
874 
875  Real adx = (axm - axp) * dy * dz;
876  Real ady = (aym - ayp) * dx * dz;
877  Real adz = (azm - azp) * dx * dy;
878 
879  area = std::sqrt(adx*adx + ady*ady + adz*adz);
880  }
881  // Every active thread must enter the block reduction, including
882  // regular and covered cells, which contribute zero surface area.
883  Gpu::deviceReduceSum(&area_device[0], area, handler);
884  });
885  }
886 
887  // Copy to host and sum across MPI ranks
888  Gpu::copy(Gpu::deviceToHost, area_vec.begin(), area_vec.begin() + 1, &total_area);
889  ParallelDescriptor::ReduceRealSum(&total_area, 1);
890 
891  // Set the same total area for all iavg
892  for (int iavg = 0; iavg < m_navg; ++iavg) {
893  m_total_bndry_area[lev][iavg] = total_area;
894  }
895 
896  Print() << "EB surface area on cell-centerd grid at level " << lev << ": " << total_area << std::endl;
897 }

Referenced by make_MOSTAverage_at_level().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_k_indices_N()

void MOSTAverage::set_k_indices_N ( const int &  lev)

Populate the 2D k-index iMultiFab without terrain.

Parameters
[in]levlevel index

Function to set K indices without terrain.

Parameters
[in]levCurrent level
906 {
907  ParmParse pp(m_pp_prefix);
908  // See zref_sentinel in ERF_MOSTAverage.H: probe with the sentinel and test the
909  // value, since queryAdd's return value stops meaning "user specified" after the
910  // first parse of the key and this routine runs once per level.
911  Real zref_tmp = zref_sentinel;
912  pp.queryAdd("most.zref",zref_tmp);
913  bool read_z = (zref_tmp > Real(0));
914  auto read_k = pp.queryarr("most.k_arr_in",m_k_in);
915 
916  const int dir = m_face.coordDir();
917  const bool is_lo_face = m_face.isLow();
918  const bool zlo = (dir == 2 && is_lo_face);
919 
920  const bool stretched = (dir == 2 && m_mesh_type != MeshType::ConstantDz);
921  if (stretched) {
922  for (int ilev : {0, lev}) {
923  const int nz = m_geom[ilev].Domain().length(2);
925  ilev < static_cast<int>(m_zlevels_stag.size()) &&
926  static_cast<int>(m_zlevels_stag[ilev].size()) == nz + 1,
927  "MOSTAverage: z levels are needed for a stretched mesh without terrain!");
928  }
929  }
930 
931  // Height above the wall of the default reference on zlo of a constant-dz mesh, kept
932  // apart from the absolute z the input convention there uses, so that subtracting
933  // prob_lo again cannot round it below the first cell center (negative when unset)
934  Real zlo_default_height = -one;
935 
936  // Default behavior is to use the first cell center
937  if (!read_z && !read_k) {
938  if (stretched) {
939  const auto& zlevels = m_zlevels_stag[0];
940  zref_tmp = zlo ? cell_center_height(zlevels, 0)
941  : column_height(zlevels) - cell_center_height(zlevels, zlevels.size()-2);
942  } else {
943  const Real dz = m_geom[0].CellSize(dir);
944  zref_tmp = zlo ? m_geom[0].ProbLo(dir) + myhalf * dz : myhalf * dz;
945  if (zlo) { zlo_default_height = myhalf * dz; }
946  }
947  if (zlo && !stretched) {
948  // zlo input convention: an absolute z
949  Print() << "Reference height for MOST set to " << zref_tmp - m_geom[0].ProbLo(dir)
950  << " (z = " << zref_tmp << ")" << std::endl;
951  } else {
952  Print() << "Reference height for MOST set to " << zref_tmp << std::endl;
953  }
954  read_z = true;
955  }
956 
957  // Specify z_ref & compute k_indx (z_ref takes precedence)
958  if (read_z && stretched) {
959  const auto& zlevels = m_zlevels_stag[lev];
960  const int nz = static_cast<int>(zlevels.size()) - 1;
961  const Real ztop = column_height(zlevels);
962  const Real target = zlo ? zref_tmp : ztop - zref_tmp;
963  const Real first_center = cell_center_height(zlevels, 0);
964  const Real last_center = cell_center_height(zlevels, nz - 1);
965  const Real target_min = zlo ? first_center : ztop - last_center;
966  const Real target_max = zlo ? last_center : ztop - first_center;
967  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(zref_tmp >= target_min && zref_tmp <= target_max,
968  "Query point must remain inside the stretched mesh!");
969  const int lk = k_index_below(zlevels, target);
970  AMREX_ALWAYS_ASSERT_WITH_MESSAGE((zlo ? lk : nz - 1 - lk) >= m_radius,
971  "K index must be larger than averaging radius!");
972  m_k_indx[lev]->setVal(lk);
973  m_zref[lev]->setVal(zlo ? cell_center_height(zlevels, lk)
974  : ztop - cell_center_height(zlevels, lk));
975  } else if (read_z) {
976  const Real m_zlo = m_geom[lev].ProbLo(dir);
977  const Real m_zhi = m_geom[lev].ProbHi(dir);
978  const Real m_dz = m_geom[lev].CellSize(dir);
979 
980  const int dom_lo = m_geom[lev].Domain().smallEnd(dir);
981  const int dom_hi = m_geom[lev].Domain().bigEnd(dir);
982 
983  if (zlo) {
984  // Existing behavior: zlo input is an absolute coordinate. The stored
985  // reference height is the height above the wall, as on every other face:
986  // the surface layer uses it as one, e.g. in ln(zref / z0).
987  // Height above the wall. A query at a cell center (prob_lo + dz/2 typed as a z,
988  // or the default dz0/2 on a level refined by an odd ratio) must not round below
989  // it, so the cell count gets an allowance of a few round-off units of the
990  // numbers it came from
991  const Real height = (zlo_default_height >= zero) ? zlo_default_height
992  : zref_tmp - m_zlo;
993  const Real slack = Real(8) * std::numeric_limits<Real>::epsilon()
994  * ((std::abs(zref_tmp) + std::abs(m_zlo)) / m_dz + one);
996  "erf.most.zref on the low z face of a constant-dz mesh is an absolute z: it must be "
997  "at least geometry.prob_lo[2] + dz/2");
998  AMREX_ALWAYS_ASSERT_WITH_MESSAGE((m_zhi - m_zlo - height) / m_dz + slack >= myhalf,
999  "erf.most.zref on the low z face of a constant-dz mesh is an absolute z: it must be "
1000  "at most geometry.prob_hi[2] - dz/2");
1001 
1002  const int wall_offset =
1003  static_cast<int>(floor(height / m_dz - myhalf + slack));
1004 
1005  AMREX_ALWAYS_ASSERT(wall_offset >= m_radius);
1006 
1007  m_k_indx[lev]->setVal(dom_lo + wall_offset);
1008  m_zref[lev]->setVal((static_cast<Real>(wall_offset) + myhalf) * m_dz);
1009  } else {
1010  const Real domain_length = m_zhi - m_zlo;
1011 
1012  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(zref_tmp >= myhalf * m_dz,
1013  "Query distance must reach the first cell center!");
1014  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(zref_tmp <= domain_length - myhalf * m_dz,
1015  "Query distance must remain inside the domain!");
1016 
1017  const int wall_offset = static_cast<int>(floor(zref_tmp / m_dz - myhalf));
1018  const int lk = is_lo_face ? dom_lo + wall_offset : dom_hi - wall_offset;
1019 
1020  AMREX_ALWAYS_ASSERT(wall_offset >= m_radius);
1021 
1022  const Real zref_abs = is_lo_face
1023  ? m_zlo + (static_cast<Real>(wall_offset) + myhalf) * m_dz
1024  : m_zhi - (static_cast<Real>(wall_offset) + myhalf) * m_dz;
1025  const Real zref_height = is_lo_face
1026  ? zref_abs - m_zlo
1027  : m_zhi - zref_abs;
1028 
1029  m_k_indx[lev]->setVal(lk);
1030  m_zref[lev]->setVal(zref_height);
1031  }
1032  // Specified k_indx & compute z_ref
1033  } else if (read_k) {
1034  const int dom_lo = m_geom[lev].Domain().smallEnd(dir);
1035  const int dom_hi = m_geom[lev].Domain().bigEnd(dir);
1036  const int ncell = m_geom[lev].Domain().length(dir);
1037 
1038  // Preserve the established z-low convention: k_arr_in is an
1039  // absolute cell index on zlo. All other faces interpret k_arr_in as
1040  // a distance in cells from the selected wall.
1041  const int wall_offset = zlo
1042  ? m_k_in[lev] - dom_lo
1043  : m_k_in[lev];
1044  if (zlo) {
1046  m_k_in[lev] >= dom_lo && m_k_in[lev] <= dom_hi,
1047  "MOST zlo reference index must lie inside the domain!");
1048  } else {
1050  wall_offset >= 0 && wall_offset < ncell,
1051  "MOST wall-relative reference offset must lie inside the domain!");
1052  }
1054  "K index must be larger than averaging radius!");
1055  const int ref_index = zlo
1056  ? m_k_in[lev]
1057  : (is_lo_face ? dom_lo + wall_offset : dom_hi - wall_offset);
1058  m_k_indx[lev]->setVal(ref_index);
1059 
1060  // TODO: check that z_ref is constant across levels
1061  if (dir == 2) {
1062  const int dom_lo0 = m_geom[0].Domain().smallEnd(2);
1063  const int dom_hi0 = m_geom[0].Domain().bigEnd(2);
1064  const int wall_offset0 = zlo ? m_k_in[0] - dom_lo0 : m_k_in[0];
1065  const int ref_index0 = zlo ? m_k_in[0] : dom_hi0 - wall_offset0;
1066  const int k0 = ref_index0 - dom_lo0;
1067  AMREX_ALWAYS_ASSERT(wall_offset0 >= m_radius);
1068 
1069  if (stretched) {
1070  const auto& zlevels = m_zlevels_stag[0];
1072  k0 >= 0 && k0 < m_geom[0].Domain().length(2),
1073  "MOST reference index must lie inside the stretched domain!");
1074  const Real zcell = cell_center_height(zlevels, k0);
1075  m_zref[lev]->setVal(zlo ? zcell : column_height(zlevels) - zcell);
1076  } else {
1077  const Real dz0 = m_geom[0].CellSize(2);
1078  const Real zlo0 = m_geom[0].ProbLo(2);
1079  const Real zhi0 = m_geom[0].ProbHi(2);
1080  const Real zref_abs = is_lo_face
1081  ? zlo0 + (static_cast<Real>(wall_offset0) + myhalf) * dz0
1082  : zhi0 - (static_cast<Real>(wall_offset0) + myhalf) * dz0;
1083  m_zref[lev]->setVal(is_lo_face ? zref_abs - zlo0 : zhi0 - zref_abs);
1084  }
1085  } else {
1086  const Real m_dz = m_geom[lev].CellSize(dir);
1087  const Real m_zlo = m_geom[lev].ProbLo(dir);
1088  const Real m_zhi = m_geom[lev].ProbHi(dir);
1089  const Real zref_abs = is_lo_face
1090  ? m_zlo + (static_cast<Real>(wall_offset) + myhalf) * m_dz
1091  : m_zhi - (static_cast<Real>(wall_offset) + myhalf) * m_dz;
1092 
1093  m_zref[lev]->setVal(is_lo_face ? zref_abs - m_zlo : m_zhi - zref_abs);
1094  }
1095  }
1096 }
const Real ztop
Definition: ERF_InitCustomPertVels_ParticleTests.H:4
const Real length
Definition: ERF_InitCustomPert_AnelasticWallDiffusion.H:16
ParmParse pp("prob")
Real height
Definition: ERF_InitCustomPert_SquallLine.H:37
static amrex::Real column_height(const amrex::Vector< amrex::Real > &zlevels)
Definition: ERF_MOSTAverage.H:428
static int k_index_below(const amrex::Vector< amrex::Real > &zlevels, const amrex::Real &zref)
Definition: ERF_MOSTAverage.H:398
std::string m_pp_prefix
Definition: ERF_MOSTAverage.H:718
MeshType m_mesh_type
Definition: ERF_MOSTAverage.H:719
amrex::Vector< amrex::Vector< amrex::Real > > m_zlevels_stag
Definition: ERF_MOSTAverage.H:721
amrex::Vector< int > m_k_in
Definition: ERF_MOSTAverage.H:750
real(c_double), parameter epsilon
Definition: ERF_module_model_constants.F90:12

Referenced by make_MOSTAverage_at_level(), and set_k_indices_T().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_k_indices_T()

void MOSTAverage::set_k_indices_T ( const int &  lev)

Populate the 2D k-index iMultiFab with terrain.

Parameters
[in]levlevel index

Function to set K indices with terrain (w/o terrain normals or interpolation).

Parameters
[in]levCurrent level
1178 {
1179  // Terrain-fitted coordinates only deform the vertical coordinate. A
1180  // lateral wall therefore uses the uniform wall-normal indexing logic.
1181  if (m_face.coordDir() != 2) {
1182  set_k_indices_N(lev);
1183  return;
1184  }
1185 
1186  ParmParse pp(m_pp_prefix);
1187  // See zref_sentinel in ERF_MOSTAverage.H: probe with the sentinel and test the
1188  // value, since queryAdd's return value stops meaning "user specified" after the
1189  // first parse of the key and this routine runs once per level.
1190  Real zref_tmp = zref_sentinel;
1191  pp.queryAdd("most.zref",zref_tmp);
1192  const bool user_zref = (zref_tmp > Real(0));
1193  auto read_k = pp.queryarr("most.k_arr_in",m_k_in);
1194  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(!(user_zref && read_k),
1195  "Specify erf.most.zref or erf.most.k_arr_in for MOST, not both");
1196 
1197  // Without most.zref the reference is the center of the first cell above the
1198  // wall, as on every other mesh. On a terrain-fitted mesh its height above
1199  // the wall varies by column, so it is filled column by column below
1200  // (erf-model/ERF#4935).
1201  const bool read_z = user_zref || !read_k;
1202  if (!user_zref && !read_k) {
1203  Print() << "erf.most.zref not set: the MOST reference is the center of the first cell above the surface"
1204  << std::endl;
1206  "erf.most.radius > 0 needs erf.most.zref placing the reference at least radius cells "
1207  "above the surface; without it the reference is the first cell");
1208  }
1209 
1210  // Capture for device
1211  Real d_zref = zref_tmp;
1212  Real d_radius = static_cast<Real>(m_radius);
1213  const bool d_user_zref = user_zref;
1214  const bool is_lo_face = m_face.isLow();
1215  const int ng = m_k_indx[lev]->nGrow() - 1;
1216 
1217  // The k indices are needed everywhere an average is computed, ghost cells
1218  // included; the box is made nodal so that we also cover the U & V averages,
1219  // which are face centered
1220  const IntVect ng_indx = max(get_ng_fill(lev), IntVect(ng,ng,0));
1221 
1222  // Specify z_ref & compute k_indx (z_ref takes precedence)
1223  if (read_z) {
1224  const Box domain = m_geom[lev].Domain();
1225  const int zlo = domain.smallEnd(2);
1226  const int zhi = domain.bigEnd(2);
1227  const int top_node = zhi + 1;
1228 
1229  // Iterate the full 3-D source layout so z-split grids can be tested
1230  // for ownership of the selected wall before collapsing the kernel box.
1231  for (MFIter mfi(*m_fields[lev][3], TileNoZ()); mfi.isValid(); ++mfi) {
1232  Box npbx = mfi.tilebox(IntVect(1,1,0),ng_indx);
1233  const Box vbx = mfi.validbox();
1234 
1235  if (is_lo_face) {
1236  if (vbx.smallEnd(2) != zlo) { continue; }
1237  npbx.makeSlab(2,zlo);
1238  } else {
1239  if (vbx.bigEnd(2) != zhi) { continue; }
1240  npbx.makeSlab(2,zhi);
1241  }
1242 
1243  const Box& z_phys_fab = m_z_phys_nd[lev]->fabbox(mfi.index());
1244  // The z_phys FAB may cover only one slab of a vertically
1245  // decomposed domain. The zlo search starts at the bottom and
1246  // normally finds the reference cell before leaving that FAB, so
1247  // it must not require the domain-wide top node. The zhi search
1248  // does require the top node because that is its first interval.
1249  if (!is_lo_face) {
1251  z_phys_fab.bigEnd(2) >= top_node,
1252  "Terrain FAB does not contain the top nodal plane.");
1253  }
1254  const int search_last_cell = is_lo_face
1255  ? min(zhi, z_phys_fab.bigEnd(2) - 1)
1256  : max(zlo, z_phys_fab.smallEnd(2));
1257  const int first_search_cell = is_lo_face ? zlo : zhi;
1259  (is_lo_face && search_last_cell >= first_search_cell) ||
1260  (!is_lo_face && search_last_cell <= first_search_cell),
1261  "Terrain FAB does not contain a complete search interval.");
1262  const auto z_phys_arr = m_z_phys_nd[lev]->const_array(mfi);
1263  auto k_arr = m_k_indx[lev]->array(mfi);
1264  auto zref_arr = m_zref[lev]->array(mfi);
1265  auto z_at = [=] AMREX_GPU_DEVICE (int i, int j, int node_k) noexcept -> Real
1266  {
1267  return fourth * ( z_phys_arr(i ,j ,node_k) + z_phys_arr(i+1,j ,node_k)
1268  + z_phys_arr(i ,j+1,node_k) + z_phys_arr(i+1,j+1,node_k) );
1269  };
1270 
1271  ParallelFor(npbx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
1272  {
1273  // Terrain heights are nodal. Use the horizontal average of
1274  // the four nodes at a given vertical node for the face and
1275  // for every cell interval searched below.
1276  const int face_node = is_lo_face ? zlo : top_node;
1277  const Real z_face = z_at(i, j, face_node);
1278  const Real z_target = z_face + (is_lo_face ? d_zref : -d_zref);
1279  const int first_cell = is_lo_face ? zlo : zhi;
1280  const int cell_step = is_lo_face ? 1 : -1;
1281  k_arr(i,j,k) = first_cell;
1282 
1283  // Default: the center of this column's first cell, half its
1284  // thickness above the wall
1285  if (!d_user_zref) {
1286  zref_arr(i,j,k) = myhalf * (z_at(i, j, first_cell + 1) - z_at(i, j, first_cell));
1287  return;
1288  }
1289 
1290  bool found = false;
1291  for (int cell = first_cell; ; cell += cell_step) {
1292  const Real z_lo = z_at(i, j, cell);
1293  const Real z_hi = z_at(i, j, cell + 1);
1294  // A requested zref on a cell face keeps the wall-adjacent cell
1295  const bool in_cell = (z_target >= z_lo && z_target <= z_hi);
1296  if (in_cell) {
1297  const int wall_offset = is_lo_face ? cell - zlo : zhi - cell;
1298  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(wall_offset >= d_radius,
1299  "K index must be larger than averaging radius!");
1300  k_arr(i,j,k) = cell;
1301  const Real z_cell = myhalf * (z_hi + z_lo);
1302  zref_arr(i,j,k) = is_lo_face ? z_cell - z_face : z_face - z_cell;
1303  found = true;
1304  break;
1305  }
1306  // z_at(cell) reads both cell nodes, so stop at the last
1307  // interval available in this FAB. If the reference
1308  // height is not available locally, the assertion below
1309  // reports the problem instead of reading another slab.
1310  if (cell == search_last_cell) { break; }
1311  }
1312 
1314  "zref not found with terrain!");
1315  });
1316  }
1317 
1318  const Periodicity period = tangential_periodicity(m_geom[lev], 2);
1319  m_k_indx[lev]->FillBoundary(m_k_indx[lev]->nGrowVect(), period);
1320  fill_zref_planar(lev);
1321 
1322  // Specified k_indx & compute z_ref
1323  } else if (read_k) {
1324  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(false, "Specified k-indx with terrain not implemented!");
1325  }
1326 }
void fill_zref_planar(const int &lev)
Definition: ERF_MOSTAverage.cpp:2329

Referenced by make_MOSTAverage_at_level().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_norm_indices_T()

void MOSTAverage::set_norm_indices_T ( const int &  lev)

Populate all 2D normal-index iMultiFabs with terrain.

Parameters
[in]levlevel index

Function to set I,J,K indices with terrain normals (w/o interpolation).

Parameters
[in]levCurrent level
1336 {
1337  // Peel back the level
1338  auto& fields = m_fields[lev];
1339 
1340  // MFIter over CC data
1341  int imf_cc = 3;
1342 
1343  ParmParse pp(m_pp_prefix);
1344  // See zref_sentinel in ERF_MOSTAverage.H: probe with the sentinel and test the
1345  // value, since queryAdd's return value stops meaning "user specified" after the
1346  // first parse of the key and this routine runs once per level.
1347  Real zref_tmp = zref_sentinel;
1348  pp.queryAdd("most.zref",zref_tmp);
1349  const bool user_zref = (zref_tmp > Real(0));
1350  if (!user_zref) {
1351  // The center of each column's first cell (erf-model/ERF#4935)
1352  Print() << "erf.most.zref not set: the MOST reference is the center of the first cell above the surface"
1353  << std::endl;
1355  "erf.most.radius > 0 needs erf.most.zref placing the reference at least radius cells "
1356  "above the surface; without it the reference is the first cell");
1357  }
1358 
1359  AMREX_ALWAYS_ASSERT(m_face.coordDir() == 2);
1360  int klo = m_geom[lev].Domain().smallEnd(2);
1361 
1362  // Capture for device
1363  Real d_zref = zref_tmp;
1364  Real d_radius = static_cast<Real>(m_radius);
1365  const bool d_user_zref = user_zref;
1366 
1367  const auto dxInv = m_geom[lev].InvCellSizeArray();
1368 
1369  // The indices are needed everywhere an average is computed, ghost cells
1370  // included; the box is made nodal so that we also cover the U & V averages,
1371  // which are face centered
1372  const IntVect ng_indx = max(get_ng_fill(lev), IntVect(1,1,0));
1373 
1374  for (MFIter mfi(*fields[imf_cc], TileNoZ()); mfi.isValid(); ++mfi) {
1375  Box npbx = mfi.tilebox(IntVect(1,1,0),IntVect(1,1,0));
1376  const Box vbx = mfi.validbox();
1377  npbx = mfi.tilebox(IntVect(1,1,0),ng_indx);
1378 
1379  if (vbx.smallEnd(2) != klo) { continue; }
1380 
1381  int kmax = npbx.bigEnd(2);
1382 
1383  npbx.makeSlab(2,klo);
1384 
1385  const auto z_phys_arr = m_z_phys_nd[lev]->const_array(mfi);
1386  auto i_arr = m_i_indx[lev]->array(mfi);
1387  auto j_arr = m_j_indx[lev]->array(mfi);
1388  auto k_arr = m_k_indx[lev]->array(mfi);
1389  auto zref_arr = m_zref[lev]->array(mfi);
1390 
1391  // Map factors at the cell centers (m = 1 if none)
1392  const bool has_mf = !m_mapfac[lev].empty();
1393  Array4<const Real> mf_mx, mf_my;
1394  if (has_mf) {
1395  mf_mx = m_mapfac[lev][MapFacType::m_x]->const_array(mfi);
1396  mf_my = m_mapfac[lev][MapFacType::m_y]->const_array(mfi);
1397  }
1398 
1399  // The displaced column (i_new, j_new) is read here from z_phys at i_new..i_new+1 and
1400  // j_new..j_new+1, and later from the averaged fields at (i_new, j_new); keep it
1401  // inside both FABs, so a displacement past the ghost cells uses the nearest column
1402  const auto zp_lo = lbound(z_phys_arr), zp_hi = ubound(z_phys_arr);
1403  const auto cc_lo = lbound(fields[imf_cc]->const_array(mfi));
1404  const auto cc_hi = ubound(fields[imf_cc]->const_array(mfi));
1405  const int inew_lo = amrex::max(zp_lo.x, cc_lo.x), inew_hi = amrex::min(zp_hi.x - 1, cc_hi.x);
1406  const int jnew_lo = amrex::max(zp_lo.y, cc_lo.y), jnew_hi = amrex::min(zp_hi.y - 1, cc_hi.y);
1407 
1408  ParallelFor(npbx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
1409  {
1410  // Search for k (grid is stretched in z)
1411  Real z_bot_face = fourth * ( z_phys_arr(i ,j ,k) + z_phys_arr(i+1,j ,k)
1412  + z_phys_arr(i ,j+1,k) + z_phys_arr(i+1,j+1,k) );
1413  Real z_top_face = fourth * ( z_phys_arr(i ,j ,k+1) + z_phys_arr(i+1,j ,k+1)
1414  + z_phys_arr(i ,j+1,k+1) + z_phys_arr(i+1,j+1,k+1) );
1415 
1416  // Default: the center of this column's first cell, no displacement
1417  if (!d_user_zref) {
1418  i_arr(i,j,0) = i;
1419  j_arr(i,j,0) = j;
1420  k_arr(i,j,0) = klo;
1421  zref_arr(i,j,0) = myhalf * (z_top_face - z_bot_face);
1422  return;
1423  }
1424 
1425  // Elements of normal vector, with the physical slopes m_x h_xi and m_y h_eta
1426  Real mfx = one, mfy = one;
1427  if (has_mf) {
1428  const int im = amrex::Clamp(i, mf_mx.begin[0], mf_mx.end[0]-1);
1429  const int jm = amrex::Clamp(j, mf_mx.begin[1], mf_mx.end[1]-1);
1430  mfx = mf_mx(im,jm,0);
1431  mfy = mf_my(im,jm,0);
1432  }
1433  Real slope_x = mfx * Compute_h_xi_AtCellCenter (i,j,k,dxInv,z_phys_arr);
1434  Real slope_y = mfy * Compute_h_eta_AtCellCenter(i,j,k,dxInv,z_phys_arr);
1435  Real mag = std::sqrt(slope_x*slope_x + slope_y*slope_y + one);
1436 
1437  // Unit-normal vector scaled by z_ref (physical displacements)
1438  Real delta_x = -slope_x/mag * d_zref;
1439  Real delta_y = -slope_y/mag * d_zref;
1440  Real delta_z = one/mag * d_zref;
1441 
1442  // Compute i & j as displacements (no grid stretching); the physical
1443  // cell size is dx/m_x by dy/m_y
1444  int delta_i = static_cast<int>(std::round(delta_x*mfx*dxInv[0]));
1445  int delta_j = static_cast<int>(std::round(delta_y*mfy*dxInv[1]));
1446  int i_new = amrex::Clamp(i + delta_i, inew_lo, inew_hi);
1447  int j_new = amrex::Clamp(j + delta_j, jnew_lo, jnew_hi);
1448  i_arr(i,j,0) = i_new;
1449  j_arr(i,j,0) = j_new;
1450 
1451  Real z_target = z_bot_face + delta_z;
1452  k_arr(i,j,0) = klo;
1453  // Fallback if no cell holds the target: the first cell's center height
1454  zref_arr(i,j,0) = myhalf * (z_top_face - z_bot_face);
1455  for (int lk(klo); lk<=kmax; ++lk) {
1456  Real z_lo = fourth * ( z_phys_arr(i_new,j_new ,lk ) + z_phys_arr(i_new+1,j_new ,lk )
1457  + z_phys_arr(i_new,j_new+1,lk ) + z_phys_arr(i_new+1,j_new+1,lk ) );
1458  Real z_hi = fourth * ( z_phys_arr(i_new,j_new ,lk+1) + z_phys_arr(i_new+1,j_new ,lk+1)
1459  + z_phys_arr(i_new,j_new+1,lk+1) + z_phys_arr(i_new+1,j_new+1,lk+1) );
1460  if (in_cell_z(z_target, z_lo, z_hi)) {
1461  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(lk >= d_radius,
1462  "K index must be larger than averaging radius!");
1463  amrex::ignore_unused(d_radius);
1464  k_arr(i,j,0) = lk;
1465  zref_arr(i,j,0) = myhalf * (z_hi + z_lo) - z_bot_face;
1466  break;
1467  }
1468  }
1469  });
1470  }
1471  fill_zref_planar(lev);
1472 }
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real Compute_h_xi_AtCellCenter(const int &i, const int &j, const int &k, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &cellSizeInv, const amrex::Array4< const amrex::Real > &z_nd)
Definition: ERF_TerrainMetrics.H:227
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real Compute_h_eta_AtCellCenter(const int &i, const int &j, const int &k, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &cellSizeInv, const amrex::Array4< const amrex::Real > &z_nd)
Definition: ERF_TerrainMetrics.H:252

Referenced by make_MOSTAverage_at_level().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_norm_positions_T()

void MOSTAverage::set_norm_positions_T ( const int &  lev)

Populate terrain-aware normal positions.

Parameters
[in]levlevel index

Function to set positions with terrain and normal vector (with interpolation).

Parameters
[in]levCurrent level
1561 {
1562  AMREX_ALWAYS_ASSERT(m_face.coordDir() == 2);
1563 
1564  // Peel back the level
1565  auto& fields = m_fields[lev];
1566 
1567  // MFIter over CC data
1568  int imf_cc = 3;
1569 
1570  ParmParse pp(m_pp_prefix);
1571  // See zref_sentinel in ERF_MOSTAverage.H: probe with the sentinel and test the
1572  // value, since queryAdd's return value stops meaning "user specified" after the
1573  // first parse of the key and this routine runs once per level.
1574  Real zref_tmp = zref_sentinel;
1575  pp.queryAdd("most.zref",zref_tmp);
1576  const bool user_zref = (zref_tmp > Real(0));
1577  if (!user_zref) {
1578  // The center of each column's first cell (erf-model/ERF#4935)
1579  Print() << "erf.most.zref not set: the MOST reference is the center of the first cell above the surface"
1580  << std::endl;
1582  "erf.most.radius > 0 needs erf.most.zref placing the reference at least radius cells "
1583  "above the surface; without it the reference is the first cell");
1584  }
1585  int klo = m_geom[lev].Domain().smallEnd(2);
1586 
1587  // Capture for device
1588  Real d_zref = zref_tmp;
1589  const bool d_user_zref = user_zref;
1590  const auto plo = m_geom[lev].ProbLoArray();
1591 
1592  const auto dx = m_geom[lev].CellSizeArray();
1593  const auto dxInv = m_geom[lev].InvCellSizeArray();
1594  const int position_ng = (m_radius > 1) ? m_radius : 1;
1595 
1596  // The positions are read over the averaging stencil at every cell where an
1597  // average is computed, ghost cells included; the box is made nodal so that
1598  // we also cover the U & V averages, which are face centered
1599  const IntVect ng_pos = max(get_ng_fill(lev) + IntVect(m_radius,m_radius,0),
1600  IntVect(position_ng,position_ng,0));
1601 
1602  for (MFIter mfi(*fields[imf_cc], TileNoZ()); mfi.isValid(); ++mfi) {
1603  Box npbx = mfi.tilebox(IntVect(1,1,0),ng_pos);
1604  Box vbx = mfi.validbox();
1605 
1606  if (vbx.smallEnd(2) != klo) { continue; }
1607 
1608  npbx.makeSlab(2,klo);
1609 
1610  // The cells each average can interpolate from
1611  const auto bounds = interp_stencil_bounds(lev, mfi);
1612 
1613  const auto z_phys_arr = m_z_phys_nd[lev]->const_array(mfi);
1614  auto x_pos_arr = m_x_pos[lev]->array(mfi);
1615  auto y_pos_arr = m_y_pos[lev]->array(mfi);
1616  auto z_pos_arr = m_z_pos[lev]->array(mfi);
1617  auto zref_arr = m_zref[lev]->array(mfi);
1618 
1619  // Map factors at the cell centers (m = 1 if none)
1620  const bool has_mf = !m_mapfac[lev].empty();
1621  Array4<const Real> mf_mx, mf_my;
1622  if (has_mf) {
1623  mf_mx = m_mapfac[lev][MapFacType::m_x]->const_array(mfi);
1624  mf_my = m_mapfac[lev][MapFacType::m_y]->const_array(mfi);
1625  }
1626 
1627  ParallelFor(npbx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
1628  {
1629  // Position of the current node (indx:0,0,1)
1630  Real x0 = plo[0] + ((Real) i + myhalf) * dx[0];
1631  Real y0 = plo[1] + ((Real) j + myhalf) * dx[1];
1632  Real z_bot_face = fourth * ( z_phys_arr(i ,j ,k) + z_phys_arr(i+1,j ,k)
1633  + z_phys_arr(i ,j+1,k) + z_phys_arr(i+1,j+1,k) );
1634 
1635  // Default: the center of this column's first cell, no displacement
1636  if (!d_user_zref) {
1637  Real z_top_face = fourth * ( z_phys_arr(i ,j ,k+1) + z_phys_arr(i+1,j ,k+1)
1638  + z_phys_arr(i ,j+1,k+1) + z_phys_arr(i+1,j+1,k+1) );
1639  x_pos_arr(i,j,0) = x0;
1640  y_pos_arr(i,j,0) = y0;
1641  z_pos_arr(i,j,0) = myhalf * (z_bot_face + z_top_face);
1642  zref_arr(i,j,0) = myhalf * (z_top_face - z_bot_face);
1643  return;
1644  }
1645 
1646  // Elements of normal vector, with the physical slopes m_x h_xi and m_y h_eta
1647  Real mfx = one, mfy = one;
1648  if (has_mf) {
1649  const int im = amrex::Clamp(i, mf_mx.begin[0], mf_mx.end[0]-1);
1650  const int jm = amrex::Clamp(j, mf_mx.begin[1], mf_mx.end[1]-1);
1651  mfx = mf_mx(im,jm,0);
1652  mfy = mf_my(im,jm,0);
1653  }
1654  Real slope_x = mfx * Compute_h_xi_AtCellCenter (i,j,k,dxInv,z_phys_arr);
1655  Real slope_y = mfy * Compute_h_eta_AtCellCenter(i,j,k,dxInv,z_phys_arr);
1656  Real imag = one / std::sqrt(slope_x*slope_x + slope_y*slope_y + one);
1657 
1658  // Unit-normal vector scaled by z_ref (physical displacements)
1659  Real delta_x = -slope_x * imag * d_zref;
1660  Real delta_y = -slope_y * imag * d_zref;
1661  Real delta_z = imag * d_zref;
1662 
1663  // Final position at end of vector; x and y are computational
1664  // coordinates, so the physical displacements are scaled by m_x and m_y
1665  x_pos_arr(i,j,0) = x0 + mfx * delta_x;
1666  y_pos_arr(i,j,0) = y0 + mfy * delta_y;
1667  z_pos_arr(i,j,0) = z_bot_face + delta_z;
1668 
1669  // Keep the interpolation stencil in the data of this box
1670  bounds.clamp_to_held(i, j, x_pos_arr(i,j,0), y_pos_arr(i,j,0), plo, dxInv);
1671 
1672  // NOTE: Normal vector end point can be below the surface for concave regions.
1673  // Here we protect against that by augmenting the normal if needed.
1674  int i_new = (int) ((x_pos_arr(i,j,0) - plo[0]) / dx[0] - myhalf);
1675  int j_new = (int) ((y_pos_arr(i,j,0) - plo[1]) / dx[1] - myhalf);
1676  Real z_new_bot_face = fourth * ( z_phys_arr(i_new,j_new ,k) + z_phys_arr(i_new+1,j_new ,k)
1677  + z_phys_arr(i_new,j_new+1,k) + z_phys_arr(i_new+1,j_new+1,k) );
1678  if (z_pos_arr(i,j,0) < z_new_bot_face) {
1679  z_pos_arr(i,j,0) = z_new_bot_face + delta_z;
1680  }
1681 
1682  zref_arr(i,j,0) = delta_z;
1683  });
1684  }
1685  fill_zref_planar(lev);
1686 }
InterpStencilBounds interp_stencil_bounds(const int &lev, const amrex::MFIter &mfi) const
Definition: ERF_MOSTAverage.cpp:546

Referenced by make_MOSTAverage_at_level().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_plane_average()

bool MOSTAverage::set_plane_average ( const int &  lev,
const amrex::Vector< amrex::Real > &  pavg 
)
inline

Restore the filtered plane averages from a checkpoint. Returns false if the checkpoint does not hold what this run expects, e.g. because the averaging policy changed, in which case nothing is restored.

Parameters
[in]levlevel index
[in]pavgfiltered plane averages, one per average component
337  {
338  const int n_have = (lev < static_cast<int>(m_plane_average.size())) ?
339  static_cast<int>(m_plane_average[lev].size()) : 0;
340  if (n_have != static_cast<int>(pavg.size())) { return false; }
341  if (n_have > 0) { m_plane_average[lev] = pavg; }
342  return true;
343  }

Referenced by SurfaceLayer::set_mac_plane_avg().

Here is the caller graph for this function:

◆ set_plane_normalization()

void MOSTAverage::set_plane_normalization ( const int &  lev)

Compute number of cells per averaging plane.

Parameters
[in]levlevel index

Function to compute normalization for plane average.

Parameters
[in]levCurrent level
752 {
753  // Cells per plane and temp avg storage
754  m_ncell_plane.resize(m_maxlev);
755  m_plane_average.resize(m_maxlev);
756 
757  // True domain not used for normalization
758  Box domain = m_geom[lev].Domain();
759 
760  // NOTE: Level 0 spans the whole domain, but finer
761  // levels do not have such a restriction.
762  // For now, use the bounding box of the boxArray
763  // for normalization, consistent with avg routine.
764 
765  // Bounded box of CC data used for normalization
766  Box bnd_bx = (m_fields[lev][3]->boxArray()).minimalBox();
767 
768  // NOTE: Bounding box must lie on the periodic boundaries
769  // in order to trip the is_per flag
770 
771  // Num components, plane avg, cells per plane
772  Array<int,AMREX_SPACEDIM> is_per = {0,0,0};
773  for (int idim(0); idim < AMREX_SPACEDIM; ++idim) {
774  if ( m_geom[lev].isPeriodic(idim) &&
775  bnd_bx.bigEnd(idim)==domain.bigEnd(idim) &&
776  bnd_bx.smallEnd(idim)==domain.smallEnd(idim) ) { is_per[idim] = 1; }
777  }
778 
779  m_ncell_plane[lev].resize(m_navg);
780  m_plane_average[lev].resize(m_navg);
781  for (int iavg(0); iavg < m_navg; ++iavg) {
782  // Convert bnd_bx to current index type
783  IndexType ixt = m_averages[lev][iavg]->boxArray().ixType();
784  bnd_bx.convert(ixt);
785  IntVect bnd_bx_lo(bnd_bx.loVect());
786  IntVect bnd_bx_hi(bnd_bx.hiVect());
787 
788  m_plane_average[lev][iavg] = zero;
789 
790  m_ncell_plane[lev][iavg] = 1;
791  for (int idim(0); idim < AMREX_SPACEDIM; ++idim) {
792  if (idim != m_face.coordDir()) {
793  if (ixt.nodeCentered(idim) && is_per[idim]) {
794  m_ncell_plane[lev][iavg] *= (bnd_bx_hi[idim] - bnd_bx_lo[idim]);
795  } else {
796  m_ncell_plane[lev][iavg] *= (bnd_bx_hi[idim] - bnd_bx_lo[idim] + 1);
797  }
798  }
799  } // idim
800  } // iavg
801 }

Referenced by make_MOSTAverage_at_level().

Here is the caller graph for this function:

◆ set_region_normalization()

void MOSTAverage::set_region_normalization ( const int &  )
inline

Compute number of cells in the region average.

Parameters
[in]levlevel index
153  {m_ncell_region = (2 * m_radius + 1) * (2 * m_radius + 1) * (2 * m_radius + 1);}

Referenced by make_MOSTAverage_at_level().

Here is the caller graph for this function:

◆ set_rotated_fields()

void MOSTAverage::set_rotated_fields ( const int &  lev)

Update the rotated fields.

Parameters
[in]levlevel index

Function to set the rotated velocities.

Parameters
[in]levCurrent level
661 {
662  // Peel back the level
663  auto& fields = m_fields[lev];
664  auto& rot_fields = m_rot_fields[lev];
665  auto z_phys_nd = m_z_phys_nd[lev];
666 
667  // Inverse grid size
668  const auto dxInv = m_geom[lev].InvCellSizeArray();
669 
670  // Single MFIter over CC data
671  int imf_cc = 3;
672 
673  // NOTE: The region average reads the rotated velocities out into the ghost
674  // region (see get_ng_fill) and a FillBoundary here would not help an
675  // isolated fine patch, so we rotate the ghost cells as well. We stop
676  // one cell shy of the full ghost region since the rotation reaches one
677  // cell ahead in w (and one node ahead in z_phys).
678  IntVect ngu = max(rot_fields[0]->nGrowVect() - IntVect(1,1,1), IntVect(0));
679  IntVect ngv = max(rot_fields[1]->nGrowVect() - IntVect(1,1,1), IntVect(0));
680 
681  // Populate rotated U & V for terrain
682 #ifdef _OPENMP
683 #pragma omp parallel if (Gpu::notInLaunchRegion())
684 #endif
685  for (MFIter mfi(*fields[imf_cc], TileNoZ()); mfi.isValid(); ++mfi) {
686  Box ubx = mfi.tilebox(IntVect(1,0,0),ngu);
687  Box vbx = mfi.tilebox(IntVect(0,1,0),ngv);
688 
689  const Array4<const Real>& z_phys_arr = z_phys_nd->const_array(mfi);
690 
691  // Map factors m_x at the x faces and m_y at the y faces (m = 1 if none)
692  const bool has_mf = !m_mapfac[lev].empty();
693  Array4<const Real> mf_ux, mf_vy;
694  if (has_mf) {
695  mf_ux = m_mapfac[lev][MapFacType::u_x]->const_array(mfi);
696  mf_vy = m_mapfac[lev][MapFacType::v_y]->const_array(mfi);
697  }
698 
699  const Array4<const Real>& u_arr = fields[0]->const_array(mfi);
700  const Array4<const Real>& v_arr = fields[1]->const_array(mfi);
701  const Array4<const Real>& w_arr = fields[2]->const_array(mfi);
702 
703  // Velocities cut above the planes the averages read (set_velocity_field_ptrs)
704  // are rotated only where they are held
705  ubx.setBig(2, amrex::min(ubx.bigEnd(2), amrex::ubound(u_arr).z));
706  vbx.setBig(2, amrex::min(vbx.bigEnd(2), amrex::ubound(v_arr).z));
707 
708  const Array4<Real>& u_rot_arr = rot_fields[0]->array(mfi);
709  const Array4<Real>& v_rot_arr = rot_fields[1]->array(mfi);
710 
711  // U rotated magnitude
712  ParallelFor(ubx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
713  {
714  // Elements of first tangent vector, with the physical slope m_x h_xi
715  Real mfx = one;
716  if (has_mf) {
717  mfx = mf_ux(amrex::Clamp(i, mf_ux.begin[0], mf_ux.end[0]-1),
718  amrex::Clamp(j, mf_ux.begin[1], mf_ux.end[1]-1), 0);
719  }
720  Real slope_x = mfx * Compute_h_xi_AtIface(i,j,k,dxInv,z_phys_arr);
721  u_rot_arr(i,j,k) = (u_arr(i,j,k) + slope_x*w_arr(i,j,k))
722  / std::sqrt(slope_x*slope_x + one);
723  });
724 
725  // V rotated magnitude
726  ParallelFor(vbx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
727  {
728  // Elements of second tangent vector, with the physical slope m_y h_eta
729  Real mfy = one;
730  if (has_mf) {
731  mfy = mf_vy(amrex::Clamp(i, mf_vy.begin[0], mf_vy.end[0]-1),
732  amrex::Clamp(j, mf_vy.begin[1], mf_vy.end[1]-1), 0);
733  }
734  Real slope_y = mfy * Compute_h_eta_AtJface(i,j,k,dxInv,z_phys_arr);
735  v_rot_arr(i,j,k) = (v_arr(i,j,k) + slope_y*w_arr(i,j,k))
736  / std::sqrt(slope_y*slope_y + one);
737  });
738  }
739 
740  // Direct copy of other scalar variables
741  MultiFab::Copy(*rot_fields[3],*fields[3],0,0,1,rot_fields[3]->nGrowVect());
742  if (fields[4]) MultiFab::Copy(*rot_fields[4],*fields[4],0,0,1,rot_fields[4]->nGrowVect());
743 }
@ v_y
Definition: ERF_DataStruct.H:41
@ u_x
Definition: ERF_DataStruct.H:40
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real Compute_h_eta_AtJface(const int &i, const int &j, const int &k, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &cellSizeInv, const amrex::Array4< const amrex::Real > &z_nd)
Definition: ERF_TerrainMetrics.H:397
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real Compute_h_xi_AtIface(const int &i, const int &j, const int &k, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &cellSizeInv, const amrex::Array4< const amrex::Real > &z_nd)
Definition: ERF_TerrainMetrics.H:304

Referenced by compute_averages().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_time_avg_initialized()

void MOSTAverage::set_time_avg_initialized ( const int &  lev)
inline

Declare the time filter at this level to hold meaningful history. This is used on restart, once the filter state has been read back in.

Parameters
[in]levlevel index
311  {
312  if (m_t_avg) { m_t_init[lev] = 1; }
313  }

Referenced by SurfaceLayer::set_mac_avg_initialized().

Here is the caller graph for this function:

◆ set_velocity_field_ptrs()

void MOSTAverage::set_velocity_field_ptrs ( const int &  lev,
amrex::MultiFab *  xvel,
amrex::MultiFab *  yvel 
)
inline

Average these horizontal velocities instead of the level's own, until the next update_field_ptrs or make_MOSTAverage_at_level, which point the averages back at the level's own. They must have the level's boxes, ghost cells included, cut at the top no lower than max_k_read: each box keeps its lower end, and a box wholly above max_k_read may keep a single plane, which is never read.

Parameters
[in]levlevel index
[in]xvelx-velocity to average
[in]yvely-velocity to average
121  {
122  m_fields[lev][0] = xvel;
123  m_fields[lev][1] = yvel;
124  }

Referenced by SurfaceLayer::update_mac_ptrs().

Here is the caller graph for this function:

◆ set_z_positions_EB()

void MOSTAverage::set_z_positions_EB ( const int &  lev)

Populate embedded-boundary z positions.

Parameters
[in]levlevel index

Function to set K indices for EB.

Parameters
[in]levCurrent level
1151 {
1152  // See zref_sentinel in ERF_MOSTAverage.H: probe with the sentinel and test the
1153  // value, since queryAdd's return value stops meaning "user specified" after the
1154  // first parse of the key and this routine runs once per level.
1155  Real zref_tmp = zref_sentinel;
1156  ParmParse pp(m_pp_prefix);
1157  pp.queryAdd("most.zref",zref_tmp);
1158  bool read_z = (zref_tmp > Real(0));
1159 
1160  if (read_z) {
1161  m_zref[lev]->setVal( zref_tmp );
1162  // Default behavior is to use the first cell center
1163  } else {
1164  Real m_dz = m_geom[0].CellSize(2);
1165  zref_tmp = myhalf * m_dz;
1166  m_zref[lev]->setVal( zref_tmp );
1167  Print() << "Reference height for MOST set to " << zref_tmp << std::endl;
1168  }
1169 }

Referenced by make_MOSTAverage_at_level().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_z_positions_T()

void MOSTAverage::set_z_positions_T ( const int &  lev)

Populate terrain-aware z positions.

Parameters
[in]levlevel index

Function to set positions with terrain and e_z vector (with interpolation but no terrain normals)

Parameters
[in]levCurrent level
1482 {
1483  AMREX_ALWAYS_ASSERT(m_face.coordDir() == 2);
1484 
1485  // Peel back the level
1486  auto& fields = m_fields[lev];
1487 
1488  // MFIter over CC data
1489  int imf_cc = 3;
1490 
1491  ParmParse pp(m_pp_prefix);
1492  // See zref_sentinel in ERF_MOSTAverage.H: probe with the sentinel and test the
1493  // value, since queryAdd's return value stops meaning "user specified" after the
1494  // first parse of the key and this routine runs once per level.
1495  Real zref_tmp = zref_sentinel;
1496  pp.queryAdd("most.zref",zref_tmp);
1497  const bool user_zref = (zref_tmp > Real(0));
1498  if (!user_zref) {
1499  // The center of each column's first cell (erf-model/ERF#4935)
1500  Print() << "erf.most.zref not set: the MOST reference is the center of the first cell above the surface"
1501  << std::endl;
1503  "erf.most.radius > 0 needs erf.most.zref placing the reference at least radius cells "
1504  "above the surface; without it the reference is the first cell");
1505  }
1506  int klo = m_geom[lev].Domain().smallEnd(2);
1507 
1508  // Capture for device
1509  Real d_zref = zref_tmp;
1510  const bool d_user_zref = user_zref;
1511  const auto plo = m_geom[lev].ProbLoArray();
1512 
1513  const auto dx = m_geom[lev].CellSizeArray();
1514  const int position_ng = (m_radius > 1) ? m_radius : 1;
1515 
1516  // The positions are read over the averaging stencil at every cell where an
1517  // average is computed, ghost cells included; the box is made nodal so that
1518  // we also cover the U & V averages, which are face centered
1519  const IntVect ng_pos = max(get_ng_fill(lev) + IntVect(m_radius,m_radius,0),
1520  IntVect(position_ng,position_ng,0));
1521 
1522  for (MFIter mfi(*fields[imf_cc], TileNoZ()); mfi.isValid(); ++mfi) {
1523  Box npbx = mfi.tilebox(IntVect(1,1,0),ng_pos);
1524  Box vbx = mfi.validbox();
1525 
1526  if (vbx.smallEnd(2) != klo) { continue; }
1527 
1528  npbx.makeSlab(2,klo);
1529 
1530  const auto z_phys_arr = m_z_phys_nd[lev]->const_array(mfi);
1531  auto x_pos_arr = m_x_pos[lev]->array(mfi);
1532  auto y_pos_arr = m_y_pos[lev]->array(mfi);
1533  auto z_pos_arr = m_z_pos[lev]->array(mfi);
1534  auto zref_arr = m_zref[lev]->array(mfi);
1535  ParallelFor(npbx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
1536  {
1537  // The column's center, whose interpolation stencil get_ng_fill keeps in the data
1538  x_pos_arr(i,j,k) = plo[0] + ((Real) i + myhalf) * dx[0];
1539  y_pos_arr(i,j,k) = plo[1] + ((Real) j + myhalf) * dx[1];
1540  Real z_bot_face = fourth * ( z_phys_arr(i ,j ,k ) + z_phys_arr(i+1,j ,k )
1541  + z_phys_arr(i ,j+1,k ) + z_phys_arr(i+1,j+1,k ) );
1542  Real z_top_face = fourth * ( z_phys_arr(i ,j ,k+1) + z_phys_arr(i+1,j ,k+1)
1543  + z_phys_arr(i ,j+1,k+1) + z_phys_arr(i+1,j+1,k+1) );
1544  // Query distance: the requested height, or half of this column's first cell
1545  const Real zq = d_user_zref ? d_zref : myhalf * (z_top_face - z_bot_face);
1546  zref_arr(i,j,k) = zq;
1547  z_pos_arr(i,j,k) = z_bot_face + zq;
1548  });
1549  }
1550  fill_zref_planar(lev);
1551 }

Referenced by make_MOSTAverage_at_level().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ time_avg_is_initialized()

bool MOSTAverage::time_avg_is_initialized ( const int &  lev) const
inline

Return whether the time filter at this level holds meaningful history. Until it does, the next average is taken to be the instantaneous one.

Parameters
[in]levlevel index
300  {
301  return ( m_t_avg && (lev < static_cast<int>(m_t_init.size())) && (m_t_init[lev] == 1) );
302  }

Referenced by SurfaceLayer::mac_avg_is_initialized().

Here is the caller graph for this function:

◆ trilinear_interp_T()

AMREX_GPU_HOST_DEVICE static AMREX_INLINE void MOSTAverage::trilinear_interp_T ( const amrex::Real &  xp,
const amrex::Real &  yp,
const amrex::Real &  zp,
amrex::Real *  interp_vals,
amrex::Array4< amrex::Real const > const &  interp_array,
amrex::Array4< amrex::Real const > const &  z_arr,
const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &  plo,
const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &  dxi,
const int  interp_comp 
)
inlinestatic

Function to compute trilinear interpolation with terrain.

Parameters
[in]xpX-position
[in]ypY-position
[in]zpZ-position
[out]interp_valsValues interpolated
[in]interp_arrayArray to interpolate on
[in]z_arrPhysical heights
[in]ploProblem lower bounds
[in]dxiInverse cell size array
[in]interp_compNumber of components to interpolate
535  {
536  amrex::Real ireal, jreal, zval;
537  const bool found = find_interp_height_T(xp, yp, zp, z_arr, plo, dxi, ireal, jreal, zval);
538 
539  amrex::ignore_unused(found);
540  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(found, "MOSTAverage: Height above terrain not found, try increasing z_ref!");
541 
542  // NOTE: This is the point ahead of the current i,j (e.g. i/j_new + 1);
543  // in z, floor(zval) is the upper of the two bracketing cells
544  const amrex::RealVect lx(ireal + myhalf, jreal + myhalf, zval);
545 
546  const amrex::IntVect ijk = lx.floor();
547 
548  int i = ijk[0]; int j = ijk[1]; int k = ijk[2];
549 
550  // Convert ijk (IntVect) to a RealVect explicitly
551  amrex::RealVect ijk_r(static_cast<amrex::Real>(ijk[0]),
552  static_cast<amrex::Real>(ijk[1]),
553  static_cast<amrex::Real>(ijk[2]));
554 
555  // Weights
556  const amrex::RealVect sx_hi = lx - ijk_r;
557  const amrex::RealVect sx_lo = one - sx_hi;
558 
559  for (int n = 0; n < interp_comp; n++) {
560  interp_vals[n] = sx_lo[0]*sx_lo[1]*sx_lo[2]*interp_array(i-1, j-1, k-1,n) +
561  sx_lo[0]*sx_lo[1]*sx_hi[2]*interp_array(i-1, j-1, k ,n) +
562  sx_lo[0]*sx_hi[1]*sx_lo[2]*interp_array(i-1, j , k-1,n) +
563  sx_lo[0]*sx_hi[1]*sx_hi[2]*interp_array(i-1, j , k ,n) +
564  sx_hi[0]*sx_lo[1]*sx_lo[2]*interp_array(i , j-1, k-1,n) +
565  sx_hi[0]*sx_lo[1]*sx_hi[2]*interp_array(i , j-1, k ,n) +
566  sx_hi[0]*sx_hi[1]*sx_lo[2]*interp_array(i , j , k-1,n) +
567  sx_hi[0]*sx_hi[1]*sx_hi[2]*interp_array(i , j , k ,n);
568  }
569  }
const Real lx
Definition: ERF_InitCustomPertVels_CloudChamber.H:10

Referenced by compute_plane_averages(), and compute_region_averages().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ update_field_ptrs()

void MOSTAverage::update_field_ptrs ( const int &  lev,
amrex::Vector< amrex::Vector< amrex::MultiFab >> &  vars_old,
amrex::Vector< std::unique_ptr< amrex::MultiFab >> &  Theta_prim,
amrex::Vector< std::unique_ptr< amrex::MultiFab >> &  Qv_prim,
amrex::Vector< std::unique_ptr< amrex::MultiFab >> &  Qr_prim 
)

Reset pointers to field MultiFabs.

Parameters
[in]levlevel index
[in]vars_oldold-time state and velocity fields
[in]Theta_primprimitive potential-temperature fields by level
[in]Qv_primprimitive water-vapor fields by level
[in]Qr_primprimitive rain-water fields by level

Function to reset the pointers to field variables.

Parameters
[in]levCurrent level
[in]vars_oldConserved variables at each level
[in]Theta_primPrimitive theta component at each level
[in]Qv_primPrimitive water-vapor component at each level
[in]Qr_primPrimitive rain-water component at each level
479 {
480  m_fields[lev][0] = &vars_old[lev][Vars::xvel];
481  m_fields[lev][1] = &vars_old[lev][Vars::yvel];
482  m_fields[lev][2] = &vars_old[lev][Vars::zvel];
483  m_fields[lev][3] = Theta_prim[lev].get();
484  m_fields[lev][4] = Qv_prim[lev].get();
485  m_fields[lev][5] = Qr_prim[lev].get();
486 }

Referenced by SurfaceLayer::update_mac_ptrs().

Here is the caller graph for this function:

◆ validate_lateral_reference_stencil()

void MOSTAverage::validate_lateral_reference_stencil ( const int &  lev)

Verify that the lateral face-owned FAB contains the reference stencil.

Parameters
[in]levlevel index

This is a conservative preflight check for the direct regional-average path. It verifies that the reference point plus averaging radius, with allowance for staggered-field neighbors, is available in the owned FAB.

Verify that lateral face-owned FABs contain the direct regional stencil.

Parameters
[in]levCurrent level.

The check uses the full cell-centered source field as a conservative layout proxy for the staggered fields used by the regional kernels. It includes ghost cells and one extra cell on each high side for staggered neighbors.

TODO: Remove this check if a distributed reference-plane staging implementation is added to the direct lateral regional path.

1112 {
1113  const int dir = m_face.coordDir();
1114  if (dir == 2 || m_interp || m_norm_vec || !m_k_indx[lev]) { return; }
1115 
1116  const MultiFab& source = *m_fields[lev][3];
1117  const int face_index = m_face.isLow()
1118  ? m_geom[lev].Domain().smallEnd(dir)
1119  : m_geom[lev].Domain().bigEnd(dir);
1120  const int reference = m_k_indx[lev]->min(0);
1121 
1122  for (MFIter mfi(source, false); mfi.isValid(); ++mfi) {
1123  const Box& valid_box = mfi.validbox();
1124  const bool owns_face = m_face.isLow()
1125  ? valid_box.smallEnd(dir) == face_index
1126  : valid_box.bigEnd(dir) == face_index;
1127  if (!owns_face) { continue; }
1128 
1129  Box required = valid_box;
1130  required.setSmall(dir, reference - m_radius);
1131  required.setBig(dir, reference + m_radius);
1132  required.growHi(0, 1);
1133  required.growHi(1, 1);
1134  required.growHi(2, 1);
1135 
1136  Box available = valid_box;
1137  available.grow(source.nGrowVect());
1139  available.contains(required),
1140  "MOST lateral reference plus averaging radius is outside the face-owned FAB.");
1141  }
1142 }

Referenced by make_MOSTAverage_at_level().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_averages()

void MOSTAverage::write_averages ( const int &  lev)

Write averages on the 2D MultiFab.

Parameters
[in]levlevel index

Function to write averages to text file.

Parameters
[in]levCurrent level
3483 {
3484  // Peel back the level
3485  auto& fields = m_fields[lev];
3486  auto& averages = m_averages[lev];
3487 
3488  // MFIter on CC
3489  int imf_cc = 3;
3490 
3491 
3492  int navg = m_navg - 1;
3493 
3494  std::ofstream ofile;
3495  ofile.open (std::string("MOST_averages_" + std::to_string(m_face) + ".txt"));
3496  ofile << "Averages computed via MOSTAverages class:\n";
3497 
3498  const int dir = m_face.coordDir();
3499  int sm_index;
3500  if (m_face.isLow()) {
3501  sm_index = m_geom[lev].Domain().smallEnd(dir);
3502  } else {
3503  sm_index = m_geom[lev].Domain().bigEnd(dir);
3504  }
3505 
3506  for (MFIter mfi(*fields[imf_cc], TileNoZ()); mfi.isValid(); ++mfi) {
3507  Box pbx = mfi.tilebox();
3508 
3509  if (m_face.isLow()) {
3510  if (mfi.validbox().smallEnd(dir) != sm_index ||
3511  pbx.smallEnd(dir) != sm_index) {
3512  continue;
3513  }
3514  } else {
3515  if (mfi.validbox().bigEnd(dir) != sm_index ||
3516  pbx.bigEnd(dir) != sm_index) {
3517  continue;
3518  }
3519  }
3520 
3521  pbx.setSmall(dir, sm_index); pbx.setBig(dir, sm_index);
3522 
3523  int il = pbx.smallEnd(0); int iu = pbx.bigEnd(0);
3524  int jl = pbx.smallEnd(1); int ju = pbx.bigEnd(1);
3525  int kl = pbx.smallEnd(2); int ku = pbx.bigEnd(2);
3526 
3527  for (int k(kl); k <= ku; ++k) {
3528  for (int j(jl); j <= ju; ++j) {
3529  for (int i(il); i <= iu; ++i) {
3530  if (dir == 0) {
3531  ofile << "(J,K): " << "(" << j << "," << k << ")" << "\n";
3532  } else if (dir == 1) {
3533  ofile << "(I,K): " << "(" << i << "," << k << ")" << "\n";
3534  } else {
3535  ofile << "(I,J): " << "(" << i << "," << j << ")" << "\n";
3536  }
3537 
3538  for (int iavg(0); iavg <= navg; ++iavg) {
3539  auto mf_arr = averages[iavg]->array(mfi);
3540  ofile << "iavg val: "
3541  << iavg << ' '
3542  << mf_arr(i,j,k) << "\n";
3543  }
3544  ofile << "\n";
3545  }
3546  }
3547  }
3548  }
3549  ofile.close();
3550 }
Here is the call graph for this function:

◆ write_k_indices()

void MOSTAverage::write_k_indices ( const int &  lev)

Write k-index data.

Parameters
[in]levlevel index

Function to write the K indices to text file.

Parameters
[in]levCurrent level
3285 {
3286  // Peel back the level
3287  auto& fields = m_fields[lev];
3288  auto& k_indx = m_k_indx[lev];
3289 
3290  // MFIter on CC
3291  int imf_cc = 3;
3292 
3293  std::ofstream ofile;
3294  ofile.open ("MOST_k_indices_" + std::to_string(m_face) + ".txt");
3295  ofile << "K indices used to compute averages via MOSTAverages class:\n";
3296 
3297  const int dir = m_face.coordDir();
3298  int sm_index;
3299  if (m_face.isLow()) {
3300  sm_index = m_geom[lev].Domain().smallEnd(dir);
3301  } else {
3302  sm_index = m_geom[lev].Domain().bigEnd(dir);
3303  }
3304 
3305  for (MFIter mfi(*fields[imf_cc], TileNoZ()); mfi.isValid(); ++mfi) {
3306  Box pbx = mfi.tilebox();
3307 
3308  if (m_face.isLow()) {
3309  if (mfi.validbox().smallEnd(dir) != sm_index ||
3310  pbx.smallEnd(dir) != sm_index) {
3311  continue;
3312  }
3313  } else {
3314  if (mfi.validbox().bigEnd(dir) != sm_index ||
3315  pbx.bigEnd(dir) != sm_index) {
3316  continue;
3317  }
3318  }
3319 
3320  pbx.setSmall(dir, sm_index); pbx.setBig(dir, sm_index);
3321 
3322  int il = pbx.smallEnd(0); int iu = pbx.bigEnd(0);
3323  int jl = pbx.smallEnd(1); int ju = pbx.bigEnd(1);
3324  int kl = pbx.smallEnd(2); int ku = pbx.bigEnd(2);
3325 
3326  auto k_arr = k_indx->array(mfi);
3327  for (int k(kl); k <= ku; ++k) {
3328  for (int j(jl); j <= ju; ++j) {
3329  for (int i(il); i <= iu; ++i) {
3330  if (dir == 0) {
3331  ofile << "(J,K): " << "(" << j << "," << k << ")" << "\n";
3332  } else if (dir == 1) {
3333  ofile << "(I,K): " << "(" << i << "," << k << ")" << "\n";
3334  } else {
3335  ofile << "(I,J): " << "(" << i << "," << j << ")" << "\n";
3336  }
3337  ofile << "K_ind: "
3338  << k_arr(i,j,k) << "\n";
3339  ofile << "\n";
3340  }
3341  }
3342  }
3343  }
3344  ofile.close();
3345 }
Here is the call graph for this function:

◆ write_norm_indices()

void MOSTAverage::write_norm_indices ( const int &  lev)

Write normal-index data.

Parameters
[in]levlevel index

Function to write I,J,K indices to text file.

Parameters
[in]levCurrent level
3355 {
3356  // Peel back the level
3357  auto& fields = m_fields[lev];
3358  auto& k_indx = m_k_indx[lev];
3359  auto& j_indx = m_j_indx[lev];
3360  auto& i_indx = m_i_indx[lev];
3361 
3362  // MFIter on CC
3363  int imf_cc = 3;
3364 
3365  std::ofstream ofile;
3366  ofile.open ("MOST_ijk_indices_" + std::to_string(m_face) + ".txt");
3367  ofile << "IJK indices used to compute averages via MOSTAverages class:\n";
3368 
3369  const int dir = m_face.coordDir();
3370  int sm_index;
3371  if (m_face.isLow()) {
3372  sm_index = m_geom[lev].Domain().smallEnd(dir);
3373  } else {
3374  sm_index = m_geom[lev].Domain().bigEnd(dir);
3375  }
3376 
3377  for (MFIter mfi(*fields[imf_cc], TileNoZ()); mfi.isValid(); ++mfi) {
3378  Box pbx = mfi.tilebox();
3379 
3380  if (m_face.isLow()) {
3381  if (mfi.validbox().smallEnd(dir) != sm_index ||
3382  pbx.smallEnd(dir) != sm_index) {
3383  continue;
3384  }
3385  } else {
3386  if (mfi.validbox().bigEnd(dir) != sm_index ||
3387  pbx.bigEnd(dir) != sm_index) {
3388  continue;
3389  }
3390  }
3391 
3392  pbx.setSmall(dir, sm_index); pbx.setBig(dir, sm_index);
3393 
3394  int il = pbx.smallEnd(0); int iu = pbx.bigEnd(0);
3395  int jl = pbx.smallEnd(1); int ju = pbx.bigEnd(1);
3396  int kl = pbx.smallEnd(2); int ku = pbx.bigEnd(2);
3397 
3398  auto k_arr = k_indx->array(mfi);
3399  auto j_arr = j_indx ? j_indx->array(mfi) : Array4<int> {};
3400  auto i_arr = i_indx ? i_indx->array(mfi) : Array4<int> {};
3401 
3402  for (int k(kl); k <= ku; ++k) {
3403  for (int j(jl); j <= ju; ++j) {
3404  for (int i(il); i <= iu; ++i) {
3405  ofile << "(I1,J1,K1): " << "(" << i << "," << j << "," << k << ")" << "\n";
3406 
3407  // This diagnostic reports the cell-centered reference used
3408  // for the CC fields. The high-face +1 adjustment applies
3409  // only to the face-centered velocity normal to the wall.
3410  const int ref = k_arr(i,j,k);
3411  int im = i_arr ? i_arr(i,j,k) : i;
3412  int jm = j_arr ? j_arr(i,j,k) : j;
3413  int km = k;
3414  if (dir == 0) {
3415  im = ref;
3416  } else if (dir == 1) {
3417  jm = ref;
3418  } else {
3419  km = ref;
3420  }
3421 
3422  ofile << "(I2,J2,K2): "
3423  << "(" << im << "," << jm << "," << km << ")" << "\n";
3424  ofile << "\n";
3425  }
3426  }
3427  }
3428  }
3429  ofile.close();
3430 }
Here is the call graph for this function:

◆ write_xz_positions()

void MOSTAverage::write_xz_positions ( const int &  lev,
const int &  j 
)

Write XZ planar positions.

Parameters
[in]levlevel index
[in]jy-index of the plane to write

Function to write X & Z positions to text file.

Parameters
[in]levCurrent level
[in]jIndex in y-dir
3442 {
3443  // Peel back the level
3444  auto& fields = m_fields[lev];
3445  auto& x_pos_mf = m_x_pos[lev];
3446  auto& z_pos_mf = m_z_pos[lev];
3447 
3448  // MFIter on CC
3449  int imf_cc = 3;
3450 
3451  int klo = m_geom[lev].Domain().smallEnd(2);
3452 
3453  std::ofstream ofile;
3454  ofile.open ("MOST_xz_positions.txt");
3455 
3456  for (MFIter mfi(*fields[imf_cc], TileNoZ()); mfi.isValid(); ++mfi) {
3457  Box pbx = mfi.tilebox();
3458 
3459  if(pbx.smallEnd(2) != klo) { continue; }
3460 
3461  pbx.makeSlab(2,klo);
3462 
3463  int il = pbx.smallEnd(0); int iu = pbx.bigEnd(0);
3464 
3465  auto x_pos_arr = x_pos_mf->array(mfi);
3466  auto z_pos_arr = z_pos_mf->array(mfi);
3467 
3468  int k = 0;
3469  for (int i(il); i <= iu; ++i)
3470  ofile << x_pos_arr(i,j,k) << ' ' << z_pos_arr(i,j,k) << "\n";
3471  }
3472  ofile.close();
3473 }
Here is the call graph for this function:

Member Data Documentation

◆ include_subgrid_vel

bool MOSTAverage::include_subgrid_vel = false
protected

◆ m_averages

amrex::Vector<amrex::Vector<std::unique_ptr<amrex::MultiFab> > > MOSTAverage::m_averages
protected

◆ m_eb_vec

amrex::Vector<const eb_*> MOSTAverage::m_eb_vec
protected

◆ m_face

◆ m_fact_new

amrex::Real MOSTAverage::m_fact_new
protected

◆ m_fact_old

amrex::Real MOSTAverage::m_fact_old
protected

◆ m_fields

◆ m_geom

◆ m_i_indx

amrex::Vector<std::unique_ptr<amrex::iMultiFab> > MOSTAverage::m_i_indx
protected

◆ m_interp

◆ m_j_indx

amrex::Vector<std::unique_ptr<amrex::iMultiFab> > MOSTAverage::m_j_indx
protected

◆ m_k_in

amrex::Vector<int> MOSTAverage::m_k_in
protected

Referenced by set_k_indices_N(), and set_k_indices_T().

◆ m_k_indx

◆ m_mapfac

amrex::Vector<amrex::Vector<amrex::MultiFab*> > MOSTAverage::m_mapfac
protected

◆ m_maxlev

int MOSTAverage::m_maxlev {0}
protected

◆ m_mesh_type

MeshType MOSTAverage::m_mesh_type
protected

Referenced by set_k_indices_N().

◆ m_navg

◆ m_ncell_plane

amrex::Vector<amrex::Vector<int> > MOSTAverage::m_ncell_plane
protected

◆ m_ncell_region

int MOSTAverage::m_ncell_region {1}
protected

◆ m_norm_vec

◆ m_nvar

int MOSTAverage::m_nvar {6}
protected

◆ m_planar_bndry

amrex::Vector<PlanarBoundary> MOSTAverage::m_planar_bndry
protected

◆ m_plane_average

amrex::Vector<amrex::Vector<amrex::Real> > MOSTAverage::m_plane_average
protected

◆ m_policy

int MOSTAverage::m_policy {0}
protected

◆ m_pp_prefix

std::string MOSTAverage::m_pp_prefix
protected

◆ m_radius

◆ m_rot_fields

amrex::Vector<amrex::Vector<std::unique_ptr<amrex::MultiFab> > > MOSTAverage::m_rot_fields
protected

◆ m_rotate

◆ m_t_avg

◆ m_t_init

◆ m_terrain_type

◆ m_time_window

double MOSTAverage::m_time_window {1.0e-16}
protected

◆ m_total_bndry_area

amrex::Vector<amrex::Vector<amrex::Real> > MOSTAverage::m_total_bndry_area
protected

◆ m_Vsg

amrex::Vector<amrex::Real> MOSTAverage::m_Vsg
protected

◆ m_x_pos

amrex::Vector<std::unique_ptr<amrex::MultiFab> > MOSTAverage::m_x_pos
protected

◆ m_y_pos

amrex::Vector<std::unique_ptr<amrex::MultiFab> > MOSTAverage::m_y_pos
protected

◆ m_z_phys_nd

◆ m_z_pos

amrex::Vector<std::unique_ptr<amrex::MultiFab> > MOSTAverage::m_z_pos
protected

◆ m_zlevels_stag

amrex::Vector<amrex::Vector<amrex::Real> > MOSTAverage::m_zlevels_stag
protected

Referenced by set_k_indices_N().

◆ m_zref

amrex::Vector<std::unique_ptr<amrex::MultiFab> > MOSTAverage::m_zref
protected

◆ zref_sentinel

const amrex::Real MOSTAverage::zref_sentinel = amrex::Real(-1.0)
protected

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