ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_MOSTAverage.H
Go to the documentation of this file.
1 #ifndef ERF_MOSTAverage_H
2 #define ERF_MOSTAverage_H
3 
4 #include "AMReX_Gpu.H"
5 #include "AMReX_FArrayBox.H"
6 #include "AMReX_MultiFab.H"
7 #include "AMReX_iMultiFab.H"
8 #include "AMReX_ParmParse.H"
9 
10 #include <limits>
11 
12 #include "ERF_IndexDefines.H"
13 #include "ERF_TerrainMetrics.H"
14 #include "ERF_DataStruct.H"
15 #include "ERF_EB.H"
16 #include "ERF_PlanarBoundary.H"
17 
18 class MOSTAverage {
19 public:
20  /**
21  * Construct the MOST averaging helper.
22  *
23  * @param[in] face orientation of face for MOST averaging
24  * @param[in] geom geometry for all AMR levels
25  * @param[in] has_zphys whether nodal physical-height data are available
26  * @param[in] a_pp_prefix ParmParse prefix
27  * @param[in] m_mesh_type mesh type
28  * @param[in] m_terrain_type terrain representation
29  * @param[in] zlevels_stag nominal staggered z levels for all AMR levels
30  * @param[in] eb_vec optional embedded-boundary geometry data
31  */
32  explicit MOSTAverage (amrex::Orientation face,
33  amrex::Vector<amrex::Geometry> geom,
34  const bool& has_zphys,
35  std::string a_pp_prefix,
36  const MeshType& m_mesh_type,
37  const TerrainType& m_terrain_type,
38  const amrex::Vector<amrex::Vector<amrex::Real>>& zlevels_stag,
39  const amrex::Vector<const eb_*>& eb_vec = {});
40 
41  // MOSTAverage() = default;
42 
43  /**
44  * Destroy the MOST averaging helper.
45  */
47  {}
48 
49  /**
50  * Default move constructor.
51  */
52  MOSTAverage (MOSTAverage&&) noexcept = default;
53 
54  /**
55  * Deleted move-assignment operator.
56  *
57  * @param[in] other source object
58  */
59  MOSTAverage& operator= (MOSTAverage&& other) noexcept = delete;
60 
61  /**
62  * Deleted copy constructor.
63  *
64  * @param[in] other source object
65  */
66  MOSTAverage (const MOSTAverage& other) = delete;
67 
68  /**
69  * Deleted copy-assignment operator.
70  *
71  * @param[in] other source object
72  */
73  MOSTAverage& operator= (const MOSTAverage& other) = delete;
74 
75  /**
76  * Make MOST-average data structures at one level.
77  *
78  * @param[in] lev level index
79  * @param[in] vars_old old-time state and velocity fields
80  * @param[in] Theta_prim primitive potential-temperature field
81  * @param[in] Qv_prim primitive water-vapor field
82  * @param[in] Qr_prim primitive rain-water field
83  * @param[in] z_phys_nd nodal physical-height field
84  * @param[in] mapfac map-factor fields at the level (MapFacType); none means m = 1
85  */
86  void make_MOSTAverage_at_level (const int& lev,
87  const amrex::Vector<amrex::MultiFab*>& vars_old,
88  std::unique_ptr<amrex::MultiFab>& Theta_prim,
89  std::unique_ptr<amrex::MultiFab>& Qv_prim,
90  std::unique_ptr<amrex::MultiFab>& Qr_prim,
91  std::unique_ptr<amrex::MultiFab>& z_phys_nd,
92  const amrex::Vector<std::unique_ptr<amrex::MultiFab>>* mapfac = nullptr);
93 
94  /**
95  * Reset pointers to field MultiFabs.
96  *
97  * @param[in] lev level index
98  * @param[in] vars_old old-time state and velocity fields
99  * @param[in] Theta_prim primitive potential-temperature fields by level
100  * @param[in] Qv_prim primitive water-vapor fields by level
101  * @param[in] Qr_prim primitive rain-water fields by level
102  */
103  void update_field_ptrs (const int& lev,
104  amrex::Vector<amrex::Vector<amrex::MultiFab>>& vars_old,
105  amrex::Vector<std::unique_ptr<amrex::MultiFab>>& Theta_prim,
106  amrex::Vector<std::unique_ptr<amrex::MultiFab>>& Qv_prim,
107  amrex::Vector<std::unique_ptr<amrex::MultiFab>>& Qr_prim);
108 
109  /**
110  * Average these horizontal velocities instead of the level's own, until the next
111  * update_field_ptrs or make_MOSTAverage_at_level, which point the averages back at the
112  * level's own. They must have the level's boxes, ghost cells included, cut at the top
113  * no lower than max_k_read: each box keeps its lower end, and a box wholly above
114  * max_k_read may keep a single plane, which is never read.
115  *
116  * @param[in] lev level index
117  * @param[in] xvel x-velocity to average
118  * @param[in] yvel y-velocity to average
119  */
120  void set_velocity_field_ptrs (const int& lev, amrex::MultiFab* xvel, amrex::MultiFab* yvel)
121  {
122  m_fields[lev][0] = xvel;
123  m_fields[lev][1] = yvel;
124  }
125 
126  /**
127  * Update the rotated fields.
128  *
129  * @param[in] lev level index
130  */
131  void set_rotated_fields (const int& lev);
132 
133  /**
134  * Compute number of cells per averaging plane.
135  *
136  * @param[in] lev level index
137  */
138  void set_plane_normalization (const int& lev);
139 
140  /**
141  * Compute total embedded-boundary surface area.
142  *
143  * @param[in] lev level index
144  */
145  void set_eb_normalization (const int& lev);
146 
147  /**
148  * Compute number of cells in the region average.
149  *
150  * @param[in] lev level index
151  */
152  void set_region_normalization (const int& /*lev*/)
153  {m_ncell_region = (2 * m_radius + 1) * (2 * m_radius + 1) * (2 * m_radius + 1);}
154 
155  /**
156  * Populate the 2D k-index iMultiFab without terrain.
157  *
158  * @param[in] lev level index
159  */
160  void set_k_indices_N (const int& lev);
161 
162  /**
163  * Verify that the lateral face-owned FAB contains the reference stencil.
164  *
165  * @param[in] lev level index
166  *
167  * This is a conservative preflight check for the direct regional-average
168  * path. It verifies that the reference point plus averaging radius, with
169  * allowance for staggered-field neighbors, is available in the owned FAB.
170  */
171  void validate_lateral_reference_stencil (const int& lev);
172 
173  /**
174  * Populate the 2D k-index iMultiFab with terrain.
175  *
176  * @param[in] lev level index
177  */
178  void set_k_indices_T (const int& lev);
179 
180  /**
181  * Populate all 2D normal-index iMultiFabs with terrain.
182  *
183  * @param[in] lev level index
184  */
185  void set_norm_indices_T (const int& lev);
186 
187  /**
188  * Populate terrain-aware z positions.
189  *
190  * @param[in] lev level index
191  */
192  void set_z_positions_T (const int& lev);
193 
194  /**
195  * Populate embedded-boundary z positions.
196  *
197  * @param[in] lev level index
198  */
199  void set_z_positions_EB (const int& lev);
200 
201  /**
202  * Populate terrain-aware normal positions.
203  *
204  * @param[in] lev level index
205  */
206  void set_norm_positions_T (const int& lev);
207 
208  /**
209  * Driver for the selected average policy.
210  *
211  * @param[in] lev level index
212  */
213  void compute_averages (const int& lev);
214 
215  /**
216  * Fill averages for the plane policy.
217  *
218  * @param[in] lev level index
219  */
220  void compute_plane_averages (const int& lev);
221 
222  /**
223  * Fill averages for the point or region policy.
224  *
225  * @param[in] lev level index
226  */
227  void compute_region_averages (const int& lev);
228 
229  /**
230  * Fill averages for the embedded-boundary policy.
231  *
232  * @param[in] lev level index
233  */
234  void compute_eb_averages (const int& lev);
235 
236  /**
237  * Write k-index data.
238  *
239  * @param[in] lev level index
240  */
241  void write_k_indices (const int& lev);
242 
243  /**
244  * Write normal-index data.
245  *
246  * @param[in] lev level index
247  */
248  void write_norm_indices (const int& lev);
249 
250  /**
251  * Write XZ planar positions.
252  *
253  * @param[in] lev level index
254  * @param[in] j y-index of the plane to write
255  */
256  void write_xz_positions (const int& lev,
257  const int& j);
258 
259  /**
260  * Write averages on the 2D MultiFab.
261  *
262  * @param[in] lev level index
263  */
264  void write_averages (const int& lev);
265 
266  /**
267  * Return one 2D average MultiFab.
268  *
269  * @param[in] lev level index
270  * @param[in] comp average component index
271  */
272  [[nodiscard]] const amrex::MultiFab* get_average (const int& lev, const int& comp) const { return m_averages[lev][comp].get(); }
273 
274  /**
275  * Return one 2D average MultiFab for modification (used to restore the
276  * exponential filter state from a checkpoint).
277  *
278  * @param[in] lev level index
279  * @param[in] comp average component index
280  */
281  [[nodiscard]] amrex::MultiFab* get_average (const int& lev, const int& comp) { return m_averages[lev][comp].get(); }
282 
283  /**
284  * Return whether the averages are filtered in time (erf.most.time_average).
285  */
286  [[nodiscard]] bool do_time_averaging () const { return m_t_avg; }
287 
288  /**
289  * Return the number of average components.
290  */
291  [[nodiscard]] int get_navg () const { return m_navg; }
292 
293  /**
294  * Return whether the time filter at this level holds meaningful history.
295  * Until it does, the next average is taken to be the instantaneous one.
296  *
297  * @param[in] lev level index
298  */
299  [[nodiscard]] bool time_avg_is_initialized (const int& lev) const
300  {
301  return ( m_t_avg && (lev < static_cast<int>(m_t_init.size())) && (m_t_init[lev] == 1) );
302  }
303 
304  /**
305  * Declare the time filter at this level to hold meaningful history. This is
306  * used on restart, once the filter state has been read back in.
307  *
308  * @param[in] lev level index
309  */
310  void set_time_avg_initialized (const int& lev)
311  {
312  if (m_t_avg) { m_t_init[lev] = 1; }
313  }
314 
315  /**
316  * Return the filtered plane averages, which hold the filter state for the
317  * plane and EB policies. The region policy holds its state in m_averages
318  * instead and never allocates these, in which case this is empty.
319  *
320  * @param[in] lev level index
321  */
322  [[nodiscard]] amrex::Vector<amrex::Real> get_plane_average (const int& lev) const
323  {
324  if (lev < static_cast<int>(m_plane_average.size())) { return m_plane_average[lev]; }
325  return amrex::Vector<amrex::Real>{};
326  }
327 
328  /**
329  * Restore the filtered plane averages from a checkpoint. Returns false if the
330  * checkpoint does not hold what this run expects, e.g. because the averaging
331  * policy changed, in which case nothing is restored.
332  *
333  * @param[in] lev level index
334  * @param[in] pavg filtered plane averages, one per average component
335  */
336  bool set_plane_average (const int& lev, const amrex::Vector<amrex::Real>& pavg)
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  }
344 
345  /**
346  * Return z_ref, which may be computed from a specified k-index.
347  *
348  * @param[in] lev level index
349  */
350  [[nodiscard]] amrex::MultiFab* get_zref (const int& lev) const { return m_zref[lev].get(); }
351 
352  /**
353  * Return the field the averages read.
354  *
355  * @param[in] lev level index
356  * @param[in] comp 0 x-velocity, 1 y-velocity, 2 z-velocity, 3 theta, 4 qv, 5 qr
357  */
358  [[nodiscard]] const amrex::MultiFab* get_field_ptr (const int& lev, const int& comp) const
359  {
360  return m_fields[lev][comp];
361  }
362 
363  /**
364  * Return the k-index iMultiFab.
365  *
366  * @param[in] lev level index
367  */
368  [[nodiscard]] const amrex::iMultiFab* get_k_indices (const int& lev) const { return m_k_indx[lev].get(); }
369 
370  /**
371  * Whether a height lies in the vertical extent [z_lo, z_hi) of a cell.
372  *
373  * The interval is closed below so that a query height landing exactly on a
374  * face (e.g. the default 10 m on a flat mesh with a 10 m first cell) belongs
375  * to the cell above it rather than to neither cell.
376  *
377  * @param[in] z query height
378  * @param[in] z_lo height of the lower face
379  * @param[in] z_hi height of the upper face
380  */
381  AMREX_GPU_HOST_DEVICE AMREX_INLINE
382  static bool in_cell_z (const amrex::Real& z,
383  const amrex::Real& z_lo,
384  const amrex::Real& z_hi)
385  {
386  return ( (z >= z_lo) && (z < z_hi) );
387  }
388 
389  /**
390  * Index of the highest cell whose center lies at or below a height above the
391  * bottom of the domain, on a column of staggered (face) heights. This is the
392  * stretched-mesh counterpart of floor(zref/dz - 1/2) on a uniform mesh.
393  *
394  * @param[in] zlevels staggered heights of the column (nz+1 faces)
395  * @param[in] zref height above zlevels[0]
396  * @return the cell index, or -1 if zref lies below the first cell center
397  */
398  static int k_index_below (const amrex::Vector<amrex::Real>& zlevels,
399  const amrex::Real& zref)
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  }
408 
409  /**
410  * Height of a cell center above the bottom of the domain, on a column of
411  * staggered (face) heights.
412  *
413  * @param[in] zlevels staggered heights of the column (nz+1 faces)
414  * @param[in] k cell index
415  */
416  static amrex::Real cell_center_height (const amrex::Vector<amrex::Real>& zlevels,
417  const int& k)
418  {
419  return myhalf * (zlevels[k] + zlevels[k+1]) - zlevels[0];
420  }
421 
422  /**
423  * Height of the top of the column above its bottom, on a column of staggered
424  * (face) heights; the high z face's distances are measured from it.
425  *
426  * @param[in] zlevels staggered heights of the column (nz+1 faces)
427  */
428  static amrex::Real column_height (const amrex::Vector<amrex::Real>& zlevels)
429  {
430  return zlevels.back() - zlevels[0];
431  }
432 
433  /**
434  * Find the vertical position, in cell units, of a point for trilinear_interp_T:
435  * floor(zval) is the upper of the two cells whose centers bracket the point, and
436  * the interpolation reads that cell and the one below it.
437  *
438  * @param[in] xp X-position
439  * @param[in] yp Y-position
440  * @param[in] zp Z-position
441  * @param[in] z_arr Physical heights
442  * @param[in] plo Problem lower bounds
443  * @param[in] dxi Inverse cell size array
444  * @param[out] ireal x-position in cell units
445  * @param[out] jreal y-position in cell units
446  * @param[out] zval z-position in cell units
447  * @return whether the column holds the point
448  */
449  AMREX_GPU_HOST_DEVICE AMREX_INLINE
450  static bool find_interp_height_T (const amrex::Real& xp,
451  const amrex::Real& yp,
452  const amrex::Real& zp,
453  amrex::Array4<amrex::Real const> const& z_arr,
454  const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM>& plo,
455  const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM>& dxi,
456  amrex::Real& ireal,
457  amrex::Real& jreal,
458  amrex::Real& zval)
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  }
511 
512  /**
513  * Function to compute trilinear interpolation with terrain.
514  *
515  * @param[in] xp X-position
516  * @param[in] yp Y-position
517  * @param[in] zp Z-position
518  * @param[out] interp_vals Values interpolated
519  * @param[in] interp_array Array to interpolate on
520  * @param[in] z_arr Physical heights
521  * @param[in] plo Problem lower bounds
522  * @param[in] dxi Inverse cell size array
523  * @param[in] interp_comp Number of components to interpolate
524  */
525  AMREX_GPU_HOST_DEVICE AMREX_INLINE
526  static void trilinear_interp_T (const amrex::Real& xp,
527  const amrex::Real& yp,
528  const amrex::Real& zp,
529  amrex::Real* interp_vals,
530  amrex::Array4<amrex::Real const> const& interp_array,
531  amrex::Array4<amrex::Real const> const& z_arr,
532  const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM>& plo,
533  const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM>& dxi,
534  const int interp_comp)
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  }
570 
571  /**
572  * Return the number of ghost cells of the 2D data (averages, indices and
573  * positions) that can be filled from the field data at this level.
574  *
575  * With more than one level we must explicitly fill the ghost cells of the 2D
576  * data: an isolated fine patch has no neighboring box for a FillBoundary to
577  * communicate with, while the fields themselves do carry valid ghost data
578  * (filled from the coarse level). The 2D data are allocated with as many
579  * ghost cells as the fields they are built from, but an average reads the
580  * fields over m_radius cells in each direction and the tangential velocity
581  * magnitude (like the interpolation stencil) reaches one cell beyond that.
582  * So a ghost cell of the 2D data can only be computed where the fields carry
583  * m_radius+1 ghost cells past it.
584  *
585  * @param[in] lev level index
586  */
587  [[nodiscard]] amrex::IntVect get_ng_fill (const int& lev) const;
588 
589  /**
590  * Lateral bounds on the interpolation stencil of the MOST reference points in one box.
591  *
592  * The average of field n (U, V, W, T, Qv) reads the positions of the columns in
593  * reach[n], its own staggered box grown by the ghost cells the averages fill and
594  * the radius, and interpolates from the two cells in x and in y that bracket each
595  * point; those cells must lie in held[n], the cells its FAB holds. The tangential
596  * speed and virtual temperature averages read U, V, T and Qv at cell-centered
597  * columns, which lie in the reaches of those fields.
598  *
599  * get_ng_fill leaves the stencil one cell of room at the outermost columns, so a
600  * point displaced along the terrain normal by more than a cell can leave the data
601  * there, whatever the number of ghost cells.
602  */
604  {
605  amrex::GpuArray<amrex::Box,5> reach;
606  amrex::GpuArray<amrex::Box,5> held;
607  int nfields = 0;
608 
609  /**
610  * Move the point (xp,yp) read by column (i,j) the least distance, in x and in y,
611  * that puts its stencil in the data of every field whose average reads the
612  * column. As set_norm_indices_T does without interpolation, a displacement past
613  * the ghost cells then uses the nearest point held; only ghost columns near the
614  * edge of the box are moved, whose averages FillBoundary overwrites where boxes
615  * abut.
616  */
617  AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
618  void clamp_to_held (const int i, const int j,
619  amrex::Real& xp, amrex::Real& yp,
620  const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM>& plo,
621  const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM>& dxi) const
622  {
623  // Lowest and highest lower cell of the two that bracket the point, as
624  // trilinear_interp_T reads them, for every field that reads this column
625  int ilo = std::numeric_limits<int>::lowest(), ihi = std::numeric_limits<int>::max();
626  int jlo = std::numeric_limits<int>::lowest(), jhi = std::numeric_limits<int>::max();
627  for (int n = 0; n < nfields; ++n) {
628  const amrex::Box& r = reach[n];
629  if (i < r.smallEnd(0) || i > r.bigEnd(0) ||
630  j < r.smallEnd(1) || j > r.bigEnd(1)) { continue; }
631  const amrex::Box& h = held[n];
632  ilo = amrex::max(ilo, h.smallEnd(0)); ihi = amrex::min(ihi, h.bigEnd(0) - 1);
633  jlo = amrex::max(jlo, h.smallEnd(1)); jhi = amrex::min(jhi, h.bigEnd(1) - 1);
634  }
635 
636  // Position in units of cells, measured from the center of cell 0; a point
637  // already held is left exactly as it is
638  const amrex::Real xi = (xp - plo[0]) * dxi[0] - myhalf;
639  const amrex::Real yj = (yp - plo[1]) * dxi[1] - myhalf;
640  if (xi < amrex::Real(ilo)) { xp = plo[0] + (amrex::Real(ilo) + myhalf) / dxi[0]; }
641  if (xi > amrex::Real(ihi)) { xp = plo[0] + (amrex::Real(ihi) + myhalf) / dxi[0]; }
642  if (yj < amrex::Real(jlo)) { yp = plo[1] + (amrex::Real(jlo) + myhalf) / dxi[1]; }
643  if (yj > amrex::Real(jhi)) { yp = plo[1] + (amrex::Real(jhi) + myhalf) / dxi[1]; }
644  }
645  };
646 
647  /**
648  * Return the lateral bounds on the interpolation stencil in the box of mfi.
649  *
650  * @param[in] lev level index
651  * @param[in] mfi iterator over the cell-centered fields
652  */
653  [[nodiscard]] InterpStencilBounds interp_stencil_bounds (const int& lev,
654  const amrex::MFIter& mfi) const;
655 
656  /**
657  * Return the highest k at which the averages on the z-low face read the horizontal
658  * velocities on this level: the reference index plus the radius, or with
659  * interpolation the upper of the two cells around each sample point. The bound is
660  * global over the level.
661  *
662  * @param[in] lev level index
663  */
664  [[nodiscard]] int max_k_read (const int& lev) const;
665 
666  /**
667  * Return whether max_k_read can change after the level is made: the interpolation
668  * searches the current heights, which move with a moving mesh.
669  */
670  [[nodiscard]] bool max_k_read_can_change () const
671  {
672  return m_interp && (m_terrain_type == TerrainType::MovingFittedMesh);
673  }
674 
675  /**
676  * Fill the ghost cells of one planar average, and the valid region of its
677  * uncomputed copies when the 3D BoxArray is split in z (see PlanarBoundary).
678  *
679  * @param[in] lev level index
680  * @param[in,out] mf planar average to fill
681  */
682  void fill_planar_boundary (const int& lev, amrex::MultiFab& mf);
683 
684  /**
685  * Copy the reference heights of the wall-owning planar boxes onto every planar
686  * box of the level and check that no column is left at the sentinel.
687  *
688  * @param[in] lev Current level
689  */
690  void fill_zref_planar (const int& lev);
691 
692  /**
693  * Fill the ghost cells of one average that cannot be computed from the field
694  * data with a zeroth-order extrapolation of the nearest computed value.
695  *
696  * The averages hold as many ghost cells as the fields, which is more than
697  * can be computed from them (see get_ng_fill), so the outermost layers are
698  * filled here to leave no average undefined. This is done before the
699  * FillBoundary, which then overwrites whatever is shared with a neighbor.
700  *
701  * @param[in] lev level index
702  * @param[in] iavg average component index
703  * @param[in] ng_fill number of ghost cells that hold computed data
704  */
705  void extrap_ghost_cells (const int& lev,
706  const int& iavg,
707  const amrex::IntVect& ng_fill);
708 
709 protected:
710 
711  // Passed through constructor
712  //--------------------------------------------
713  amrex::Orientation m_face; // Face to perform averages on
714  const amrex::Vector<amrex::Geometry> m_geom; // Geometry at each level
715  amrex::Vector<amrex::Vector<amrex::MultiFab*>> m_fields; // Ptr to fields to be averaged
716  amrex::Vector<amrex::MultiFab*> m_z_phys_nd; // Ptr to terrain height coords
717  amrex::Vector<amrex::Vector<amrex::MultiFab*>> m_mapfac; // Ptr to map factors (MapFacType)
718  std::string m_pp_prefix; // ParmParse prefix
719  MeshType m_mesh_type; // Mesh type
720  TerrainType m_terrain_type; // Terrain type
721  amrex::Vector<amrex::Vector<amrex::Real>> m_zlevels_stag; // Nominal staggered z levels (maxlev)
722 
723  // General vars for multiple or all policies
724  //--------------------------------------------
725  int m_nvar{6}; // 6 fields for U/V/W/T/Qv/Qr
726  int m_navg{9}; // 9 averages for U/V/W/T/Qv/Tv/Umag_XY,Umag_XZ,Umag_YZ
727  int m_maxlev{0}; // Total number of levels
728  int m_policy{0}; // Policy for type of averaging
729  bool m_rotate{false}; // Do vector rotations for terrain?
730  amrex::Vector<std::unique_ptr<amrex::MultiFab>> m_zref; // Height above surface for MOST BC
731  amrex::Vector<std::unique_ptr<amrex::MultiFab>> m_x_pos; // Ptr to 2D mf to hold x position (maxlev)
732  amrex::Vector<std::unique_ptr<amrex::MultiFab>> m_y_pos; // Ptr to 2D mf to hold y position (maxlev)
733  amrex::Vector<std::unique_ptr<amrex::MultiFab>> m_z_pos; // Ptr to 2D mf to hold z position (maxlev)
734  amrex::Vector<std::unique_ptr<amrex::iMultiFab>> m_i_indx; // Ptr to 2D imf to hold i indices (maxlev)
735  amrex::Vector<std::unique_ptr<amrex::iMultiFab>> m_j_indx; // Ptr to 2D imf to hold j indices (maxlev)
736  amrex::Vector<std::unique_ptr<amrex::iMultiFab>> m_k_indx; // Ptr to 2D imf to hold k indices (maxlev)
737  amrex::Vector<amrex::Vector<std::unique_ptr<amrex::MultiFab>>> m_averages; // Ptr to 2D mf to hold averages (maxlev,navg)
738  amrex::Vector<PlanarBoundary> m_planar_bndry; // Fills the planar averages on a split BoxArray (maxlev)
739  amrex::Vector<amrex::Vector<std::unique_ptr<amrex::MultiFab>>> m_rot_fields; // Rotated field data
740 
741  // Vars for planar average policy
742  //--------------------------------------------
743  amrex::Vector<amrex::Vector<int>> m_ncell_plane; // Number of cells in plane (maxlev,navg)
744  amrex::Vector<amrex::Vector<amrex::Real>> m_plane_average; // Plane avgs (maxlev,navg)
745 
746  // Vars for point/region average policy
747  //--------------------------------------------
748  int m_radius{0}; // Radius around k_index
749  int m_ncell_region{1}; // Number of cells in local region
750  amrex::Vector<int> m_k_in; // Specified k_index for region avg (maxlev)
751 
752  // Vars for normal vector policy
753  //--------------------------------------------
754  bool m_interp{false}; // Do interpolation on destination?
755  bool m_norm_vec{false}; // Use normal vector to find IJK?
756 
757  // Vars for EB averaging policy
758  //--------------------------------------------
759  amrex::Vector<const eb_*> m_eb_vec; // Vector of pointers to EB objects (one per level)
760  amrex::Vector<amrex::Vector<amrex::Real>> m_total_bndry_area; // Total EB surface areas (maxlev, navg)
761 
762  // Time average w/ exponential filter fun
763  //--------------------------------------------
764  bool m_t_avg{false}; // Flag to do moving average in time
765  amrex::Vector<int> m_t_init; // Flag to specify if averages are initialized
766  double m_time_window{1.0e-16}; // Width of the exp filter function
767  amrex::Real m_fact_new, m_fact_old; // Time average factors for new and old means
768 
769  // Surface velocity correction
770  //--------------------------------------------
771  bool include_subgrid_vel = false;
772  amrex::Vector<amrex::Real> m_Vsg; // Subgrid velocity scale (Mahrt & Sun 1995 MWR)
773 
774  // Default values
775  //--------------------------------------------
776  // There is no stored default reference height. When most.zref is unset every
777  // mesh uses the center of the first cell above the wall: a single height on a
778  // flat or EB mesh, and on a terrain-fitted mesh a height that varies by column
779  // and is filled column by column by the lookup routines (erf-model/ERF#4935).
780 
781  // Sentinel used to detect whether most.zref was set by the user. Any positive
782  // value is a legal user value, so "was it specified" cannot be read off the
783  // value unless we probe with something unreachable first. For lateral and
784  // z-high faces, most.zref is a wall-relative distance; z-low retains its
785  // legacy absolute-coordinate convention. See the parse sites in
786  // ERF_MOSTAverage.cpp: they use ParmParse::queryAdd, whose return value is
787  // only meaningful on the first parse of a key, and these routines run once
788  // per level.
789  const amrex::Real zref_sentinel = amrex::Real(-1.0);
790 };
791 #endif
GpuArray< Real, AMREX_SPACEDIM > plo
Definition: ERF_ActuatorSpreading.cpp:131
Declares the embedded-boundary factory manager used by ERF levels.
const Real lx
Definition: ERF_InitCustomPertVels_CloudChamber.H:10
const Real zc
Definition: ERF_InitCustomPert_ABL.H:112
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(axis >=0 &&axis< AMREX_SPACEDIM, "prob.axis must be 0, 1 or 2")
constexpr amrex::Real two
Definition: ERF_NumericalConstants.H:38
constexpr amrex::Real one
Definition: ERF_NumericalConstants.H:37
constexpr amrex::Real fourth
Definition: ERF_NumericalConstants.H:42
constexpr amrex::Real zero
Definition: ERF_NumericalConstants.H:36
constexpr amrex::Real myhalf
Definition: ERF_NumericalConstants.H:41
Definition: ERF_MOSTAverage.H:18
int m_navg
Definition: ERF_MOSTAverage.H:726
bool m_t_avg
Definition: ERF_MOSTAverage.H:764
void fill_zref_planar(const int &lev)
Definition: ERF_MOSTAverage.cpp:2329
void write_xz_positions(const int &lev, const int &j)
Definition: ERF_MOSTAverage.cpp:3440
void compute_plane_averages(const int &lev)
Definition: ERF_MOSTAverage.cpp:1724
int m_policy
Definition: ERF_MOSTAverage.H:728
static amrex::Real column_height(const amrex::Vector< amrex::Real > &zlevels)
Definition: ERF_MOSTAverage.H:428
amrex::Vector< amrex::Vector< std::unique_ptr< amrex::MultiFab > > > m_averages
Definition: ERF_MOSTAverage.H:737
bool set_plane_average(const int &lev, const amrex::Vector< amrex::Real > &pavg)
Definition: ERF_MOSTAverage.H:336
amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_y_pos
Definition: ERF_MOSTAverage.H:732
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={})
static int k_index_below(const amrex::Vector< amrex::Real > &zlevels, const amrex::Real &zref)
Definition: ERF_MOSTAverage.H:398
const amrex::Real zref_sentinel
Definition: ERF_MOSTAverage.H:789
double m_time_window
Definition: ERF_MOSTAverage.H:766
amrex::MultiFab * get_average(const int &lev, const int &comp)
Definition: ERF_MOSTAverage.H:281
int m_radius
Definition: ERF_MOSTAverage.H:748
void set_z_positions_EB(const int &lev)
Definition: ERF_MOSTAverage.cpp:1150
void write_averages(const int &lev)
Definition: ERF_MOSTAverage.cpp:3482
amrex::Vector< std::unique_ptr< amrex::iMultiFab > > m_i_indx
Definition: ERF_MOSTAverage.H:734
void compute_region_averages(const int &lev)
Definition: ERF_MOSTAverage.cpp:2342
amrex::Vector< amrex::MultiFab * > m_z_phys_nd
Definition: ERF_MOSTAverage.H:716
amrex::Vector< int > m_t_init
Definition: ERF_MOSTAverage.H:765
void compute_averages(const int &lev)
Definition: ERF_MOSTAverage.cpp:1695
void validate_lateral_reference_stencil(const int &lev)
Definition: ERF_MOSTAverage.cpp:1111
void set_rotated_fields(const int &lev)
Definition: ERF_MOSTAverage.cpp:660
amrex::Vector< amrex::Vector< amrex::Real > > m_total_bndry_area
Definition: ERF_MOSTAverage.H:760
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 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)
Definition: ERF_MOSTAverage.cpp:174
amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_x_pos
Definition: ERF_MOSTAverage.H:731
amrex::Orientation m_face
Definition: ERF_MOSTAverage.H:713
amrex::Vector< amrex::Real > get_plane_average(const int &lev) const
Definition: ERF_MOSTAverage.H:322
void set_norm_positions_T(const int &lev)
Definition: ERF_MOSTAverage.cpp:1560
amrex::Vector< amrex::Real > m_Vsg
Definition: ERF_MOSTAverage.H:772
bool max_k_read_can_change() const
Definition: ERF_MOSTAverage.H:670
bool do_time_averaging() const
Definition: ERF_MOSTAverage.H:286
InterpStencilBounds interp_stencil_bounds(const int &lev, const amrex::MFIter &mfi) const
Definition: ERF_MOSTAverage.cpp:546
void set_k_indices_T(const int &lev)
Definition: ERF_MOSTAverage.cpp:1177
amrex::Vector< amrex::Vector< std::unique_ptr< amrex::MultiFab > > > m_rot_fields
Definition: ERF_MOSTAverage.H:739
bool m_rotate
Definition: ERF_MOSTAverage.H:729
amrex::MultiFab * get_zref(const int &lev) const
Definition: ERF_MOSTAverage.H:350
amrex::Vector< amrex::Vector< amrex::Real > > m_plane_average
Definition: ERF_MOSTAverage.H:744
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
void set_plane_normalization(const int &lev)
Definition: ERF_MOSTAverage.cpp:751
void write_k_indices(const int &lev)
Definition: ERF_MOSTAverage.cpp:3284
TerrainType m_terrain_type
Definition: ERF_MOSTAverage.H:720
std::string m_pp_prefix
Definition: ERF_MOSTAverage.H:718
void set_time_avg_initialized(const int &lev)
Definition: ERF_MOSTAverage.H:310
bool m_norm_vec
Definition: ERF_MOSTAverage.H:755
int m_nvar
Definition: ERF_MOSTAverage.H:725
int get_navg() const
Definition: ERF_MOSTAverage.H:291
void fill_planar_boundary(const int &lev, amrex::MultiFab &mf)
Definition: ERF_MOSTAverage.cpp:2305
amrex::Real m_fact_new
Definition: ERF_MOSTAverage.H:767
amrex::Vector< amrex::Vector< amrex::MultiFab * > > m_mapfac
Definition: ERF_MOSTAverage.H:717
const amrex::iMultiFab * get_k_indices(const int &lev) const
Definition: ERF_MOSTAverage.H:368
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
int m_ncell_region
Definition: ERF_MOSTAverage.H:749
void set_norm_indices_T(const int &lev)
Definition: ERF_MOSTAverage.cpp:1335
amrex::Vector< PlanarBoundary > m_planar_bndry
Definition: ERF_MOSTAverage.H:738
amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_zref
Definition: ERF_MOSTAverage.H:730
int m_maxlev
Definition: ERF_MOSTAverage.H:727
const amrex::MultiFab * get_average(const int &lev, const int &comp) const
Definition: ERF_MOSTAverage.H:272
void extrap_ghost_cells(const int &lev, const int &iavg, const amrex::IntVect &ng_fill)
Definition: ERF_MOSTAverage.cpp:2232
void set_eb_normalization(const int &lev)
Definition: ERF_MOSTAverage.cpp:809
int max_k_read(const int &lev) const
Definition: ERF_MOSTAverage.cpp:581
bool include_subgrid_vel
Definition: ERF_MOSTAverage.H:771
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)
Definition: ERF_MOSTAverage.cpp:474
static amrex::Real cell_center_height(const amrex::Vector< amrex::Real > &zlevels, const int &k)
Definition: ERF_MOSTAverage.H:416
~MOSTAverage()
Definition: ERF_MOSTAverage.H:46
MeshType m_mesh_type
Definition: ERF_MOSTAverage.H:719
amrex::Vector< std::unique_ptr< amrex::iMultiFab > > m_j_indx
Definition: ERF_MOSTAverage.H:735
amrex::Vector< amrex::Vector< amrex::Real > > m_zlevels_stag
Definition: ERF_MOSTAverage.H:721
void set_k_indices_N(const int &lev)
Definition: ERF_MOSTAverage.cpp:905
MOSTAverage(MOSTAverage &&) noexcept=default
void write_norm_indices(const int &lev)
Definition: ERF_MOSTAverage.cpp:3354
amrex::Vector< amrex::Vector< amrex::MultiFab * > > m_fields
Definition: ERF_MOSTAverage.H:715
amrex::Vector< int > m_k_in
Definition: ERF_MOSTAverage.H:750
amrex::Vector< const eb_ * > m_eb_vec
Definition: ERF_MOSTAverage.H:759
const amrex::MultiFab * get_field_ptr(const int &lev, const int &comp) const
Definition: ERF_MOSTAverage.H:358
amrex::Real m_fact_old
Definition: ERF_MOSTAverage.H:767
bool time_avg_is_initialized(const int &lev) const
Definition: ERF_MOSTAverage.H:299
bool m_interp
Definition: ERF_MOSTAverage.H:754
const amrex::Vector< amrex::Geometry > m_geom
Definition: ERF_MOSTAverage.H:714
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
void compute_eb_averages(const int &lev)
Definition: ERF_MOSTAverage.cpp:2975
void set_velocity_field_ptrs(const int &lev, amrex::MultiFab *xvel, amrex::MultiFab *yvel)
Definition: ERF_MOSTAverage.H:120
amrex::IntVect get_ng_fill(const int &lev) const
Definition: ERF_MOSTAverage.cpp:505
amrex::Vector< std::unique_ptr< amrex::iMultiFab > > m_k_indx
Definition: ERF_MOSTAverage.H:736
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
@ xvel
Definition: ERF_IndexDefines.H:232
@ yvel
Definition: ERF_IndexDefines.H:233
Definition: ERF_ConsoleIO.cpp:17
Definition: ERF_MOSTAverage.H:604
amrex::GpuArray< amrex::Box, 5 > reach
Definition: ERF_MOSTAverage.H:605
int nfields
Definition: ERF_MOSTAverage.H:607
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void clamp_to_held(const int i, const int j, amrex::Real &xp, amrex::Real &yp, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &plo, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &dxi) const
Definition: ERF_MOSTAverage.H:618
amrex::GpuArray< amrex::Box, 5 > held
Definition: ERF_MOSTAverage.H:606