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 "ERF_IndexDefines.H"
11 #include "ERF_TerrainMetrics.H"
12 #include "ERF_DataStruct.H"
13 #include "ERF_EB.H"
14 
15 class MOSTAverage {
16 public:
17  /**
18  * Construct the MOST averaging helper.
19  *
20  * @param[in] geom geometry for all AMR levels
21  * @param[in] has_zphys whether nodal physical-height data are available
22  * @param[in] a_pp_prefix ParmParse prefix
23  * @param[in] m_mesh_type mesh type
24  * @param[in] m_terrain_type terrain representation
25  * @param[in] eb_vec optional embedded-boundary geometry data
26  */
27  explicit MOSTAverage (amrex::Vector<amrex::Geometry> geom,
28  const bool& has_zphys,
29  std::string a_pp_prefix,
30  const MeshType& m_mesh_type,
31  const TerrainType& m_terrain_type,
32  const amrex::Vector<const eb_*>& eb_vec = {});
33 
34  // MOSTAverage() = default;
35 
36  /**
37  * Destroy the MOST averaging helper.
38  */
40  {}
41 
42  /**
43  * Default move constructor.
44  */
45  MOSTAverage (MOSTAverage&&) noexcept = default;
46 
47  /**
48  * Deleted move-assignment operator.
49  *
50  * @param[in] other source object
51  */
52  MOSTAverage& operator=(MOSTAverage&& other) noexcept = delete;
53 
54  /**
55  * Deleted copy constructor.
56  *
57  * @param[in] other source object
58  */
59  MOSTAverage (const MOSTAverage& other) = delete;
60 
61  /**
62  * Deleted copy-assignment operator.
63  *
64  * @param[in] other source object
65  */
66  MOSTAverage& operator=(const MOSTAverage& other) = delete;
67 
68  /**
69  * Make MOST-average data structures at one level.
70  *
71  * @param[in] lev level index
72  * @param[in] vars_old old-time state and velocity fields
73  * @param[in] Theta_prim primitive potential-temperature field
74  * @param[in] Qv_prim primitive water-vapor field
75  * @param[in] Qr_prim primitive rain-water field
76  * @param[in] z_phys_nd nodal physical-height field
77  */
78  void make_MOSTAverage_at_level (const int& lev,
79  const amrex::Vector<amrex::MultiFab*>& vars_old,
80  std::unique_ptr<amrex::MultiFab>& Theta_prim,
81  std::unique_ptr<amrex::MultiFab>& Qv_prim,
82  std::unique_ptr<amrex::MultiFab>& Qr_prim,
83  std::unique_ptr<amrex::MultiFab>& z_phys_nd);
84 
85  /**
86  * Reset pointers to field MultiFabs.
87  *
88  * @param[in] lev level index
89  * @param[in] vars_old old-time state and velocity fields
90  * @param[in] Theta_prim primitive potential-temperature fields by level
91  * @param[in] Qv_prim primitive water-vapor fields by level
92  * @param[in] Qr_prim primitive rain-water fields by level
93  */
94  void update_field_ptrs (const int& lev,
95  amrex::Vector<amrex::Vector<amrex::MultiFab>>& vars_old,
96  amrex::Vector<std::unique_ptr<amrex::MultiFab>>& Theta_prim,
97  amrex::Vector<std::unique_ptr<amrex::MultiFab>>& Qv_prim,
98  amrex::Vector<std::unique_ptr<amrex::MultiFab>>& Qr_prim);
99 
100  /**
101  * Update the rotated fields.
102  *
103  * @param[in] lev level index
104  */
105  void set_rotated_fields (const int& lev);
106 
107  /**
108  * Compute number of cells per averaging plane.
109  *
110  * @param[in] lev level index
111  */
112  void set_plane_normalization (const int& lev);
113 
114  /**
115  * Compute total embedded-boundary surface area.
116  *
117  * @param[in] lev level index
118  */
119  void set_eb_normalization (const int& lev);
120 
121  /**
122  * Compute number of cells in the region average.
123  *
124  * @param[in] lev level index
125  */
126  void set_region_normalization (const int& /*lev*/)
127  {m_ncell_region = (2 * m_radius + 1) * (2 * m_radius + 1) * (2 * m_radius + 1);}
128 
129  /**
130  * Populate the 2D k-index iMultiFab without terrain.
131  *
132  * @param[in] lev level index
133  */
134  void set_k_indices_N (const int& lev);
135 
136  /**
137  * Populate the 2D k-index iMultiFab with terrain.
138  *
139  * @param[in] lev level index
140  */
141  void set_k_indices_T (const int& lev);
142 
143  /**
144  * Populate all 2D normal-index iMultiFabs with terrain.
145  *
146  * @param[in] lev level index
147  */
148  void set_norm_indices_T (const int& lev);
149 
150  /**
151  * Populate terrain-aware z positions.
152  *
153  * @param[in] lev level index
154  */
155  void set_z_positions_T (const int& lev);
156 
157  /**
158  * Populate embedded-boundary z positions.
159  *
160  * @param[in] lev level index
161  */
162  void set_z_positions_EB (const int& lev);
163 
164  /**
165  * Populate terrain-aware normal positions.
166  *
167  * @param[in] lev level index
168  */
169  void set_norm_positions_T (const int& lev);
170 
171  /**
172  * Driver for the selected average policy.
173  *
174  * @param[in] lev level index
175  */
176  void compute_averages (const int& lev);
177 
178  /**
179  * Fill averages for the plane policy.
180  *
181  * @param[in] lev level index
182  */
183  void compute_plane_averages (const int& lev);
184 
185  /**
186  * Fill averages for the point or region policy.
187  *
188  * @param[in] lev level index
189  */
190  void compute_region_averages (const int& lev);
191 
192  /**
193  * Fill averages for the embedded-boundary policy.
194  *
195  * @param[in] lev level index
196  */
197  void compute_eb_averages (const int& lev);
198 
199  /**
200  * Write k-index data.
201  *
202  * @param[in] lev level index
203  */
204  void write_k_indices (const int& lev);
205 
206  /**
207  * Write normal-index data.
208  *
209  * @param[in] lev level index
210  */
211  void write_norm_indices (const int& lev);
212 
213  /**
214  * Write XZ planar positions.
215  *
216  * @param[in] lev level index
217  * @param[in] j y-index of the plane to write
218  */
219  void write_xz_positions (const int& lev,
220  const int& j);
221 
222  /**
223  * Write averages on the 2D MultiFab.
224  *
225  * @param[in] lev level index
226  */
227  void write_averages (const int& lev);
228 
229  /**
230  * Return one 2D average MultiFab.
231  *
232  * @param[in] lev level index
233  * @param[in] comp average component index
234  */
235  [[nodiscard]] const amrex::MultiFab* get_average (const int& lev, const int& comp) const { return m_averages[lev][comp].get(); }
236 
237  /**
238  * Return z_ref, which may be computed from a specified k-index.
239  *
240  * @param[in] lev level index
241  */
242  [[nodiscard]] amrex::MultiFab* get_zref (const int& lev) const { return m_zref[lev].get(); }
243 
244  /**
245  * Return the k-index iMultiFab.
246  *
247  * @param[in] lev level index
248  */
249  [[nodiscard]] const amrex::iMultiFab* get_k_indices (const int& lev) const { return m_k_indx[lev].get(); }
250 
251  /**
252  * Function to compute trilinear interpolation with terrain.
253  *
254  * @param[in] xp X-position
255  * @param[in] yp Y-position
256  * @param[in] zp Z-position
257  * @param[out] interp_vals Values interpolated
258  * @param[in] interp_array Array to interpolate on
259  * @param[in] z_arr Physical heights
260  * @param[in] plo Problem lower bounds
261  * @param[in] dxi Inverse cell size array
262  * @param[in] interp_comp Number of components to interpolate
263  */
264  AMREX_GPU_HOST_DEVICE AMREX_INLINE
265  static void trilinear_interp_T (const amrex::Real& xp,
266  const amrex::Real& yp,
267  const amrex::Real& zp,
268  amrex::Real* interp_vals,
269  amrex::Array4<amrex::Real const> const& interp_array,
270  amrex::Array4<amrex::Real const> const& z_arr,
271  const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM>& plo,
272  const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM>& dxi,
273  const int interp_comp)
274  {
275  // Search to get z/k
276  bool found = false;
277  int kmax = ubound(z_arr).z;
278  amrex::Real zval = zero;
279  amrex::Real z_target = zp;
280 
281  // Map position to i,j (must be same mapping in cpp file)
282  amrex::Real ireal = (xp - plo[0]) * dxi[0];
283  amrex::Real jreal = (yp - plo[1]) * dxi[1];
284  int i_new = (int) (ireal - myhalf);
285  int j_new = (int) (jreal - myhalf);
286 
287  for (int lk(0); lk<kmax; ++lk) {
288  amrex::Real z_lo = fourth * ( z_arr(i_new,j_new ,lk ) + z_arr(i_new+1,j_new ,lk )
289  + z_arr(i_new,j_new+1,lk ) + z_arr(i_new+1,j_new+1,lk ) );
290  amrex::Real z_hi = fourth * ( z_arr(i_new,j_new ,lk+1) + z_arr(i_new+1,j_new ,lk+1)
291  + z_arr(i_new,j_new+1,lk+1) + z_arr(i_new+1,j_new+1,lk+1) );
292  if (z_target > z_lo && z_target < z_hi){
293  found = true;
294  zval = (amrex::Real) lk + ((z_target - z_lo) / (z_hi - z_lo)) + myhalf;
295  break;
296  }
297  }
298 
299  amrex::ignore_unused(found);
300  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(found, "MOSTAverage: Height above terrain not found, try increasing z_ref!");
301 
302  // NOTE: This is the point ahead of the current i,j (e.g. i/j_new + 1)
303  const amrex::RealVect lx(ireal + myhalf, jreal + myhalf, zval);
304 
305  const amrex::IntVect ijk = lx.floor();
306 
307  int i = ijk[0]; int j = ijk[1]; int k = ijk[2];
308 
309  // Convert ijk (IntVect) to a RealVect explicitly
310  amrex::RealVect ijk_r(static_cast<amrex::Real>(ijk[0]),
311  static_cast<amrex::Real>(ijk[1]),
312  static_cast<amrex::Real>(ijk[2]));
313 
314  // Weights
315  const amrex::RealVect sx_hi = lx - ijk_r;
316  const amrex::RealVect sx_lo = one - sx_hi;
317 
318  for (int n = 0; n < interp_comp; n++) {
319  interp_vals[n] = sx_lo[0]*sx_lo[1]*sx_lo[2]*interp_array(i-1, j-1, k-1,n) +
320  sx_lo[0]*sx_lo[1]*sx_hi[2]*interp_array(i-1, j-1, k ,n) +
321  sx_lo[0]*sx_hi[1]*sx_lo[2]*interp_array(i-1, j , k-1,n) +
322  sx_lo[0]*sx_hi[1]*sx_hi[2]*interp_array(i-1, j , k ,n) +
323  sx_hi[0]*sx_lo[1]*sx_lo[2]*interp_array(i , j-1, k-1,n) +
324  sx_hi[0]*sx_lo[1]*sx_hi[2]*interp_array(i , j-1, k ,n) +
325  sx_hi[0]*sx_hi[1]*sx_lo[2]*interp_array(i , j , k-1,n) +
326  sx_hi[0]*sx_hi[1]*sx_hi[2]*interp_array(i , j , k ,n);
327  }
328  }
329 
330 protected:
331 
332  // Passed through constructor
333  //--------------------------------------------
334  const amrex::Vector<amrex::Geometry> m_geom; // Geometry at each level
335  amrex::Vector<amrex::Vector<amrex::MultiFab*>> m_fields; // Ptr to fields to be averaged
336  amrex::Vector<amrex::MultiFab*> m_z_phys_nd; // Ptr to terrain height coords
337  std::string m_pp_prefix; // ParmParse prefix
338  MeshType m_mesh_type; // Mesh type
339  TerrainType m_terrain_type; // Terrain type
340 
341  // General vars for multiple or all policies
342  //--------------------------------------------
343  int m_nvar{6}; // 6 fields for U/V/T/Qv/Qr/W
344  int m_navg{6}; // 6 averages for U/V/T/Qv/Tv/Umag
345  int m_maxlev{0}; // Total number of levels
346  int m_policy{0}; // Policy for type of averaging
347  bool m_rotate{false}; // Do vector rotations for terrain?
348  amrex::Vector<std::unique_ptr<amrex::MultiFab>> m_zref; // Height above surface for MOST BC
349  amrex::Vector<std::unique_ptr<amrex::MultiFab>> m_x_pos; // Ptr to 2D mf to hold x position (maxlev)
350  amrex::Vector<std::unique_ptr<amrex::MultiFab>> m_y_pos; // Ptr to 2D mf to hold y position (maxlev)
351  amrex::Vector<std::unique_ptr<amrex::MultiFab>> m_z_pos; // Ptr to 2D mf to hold z position (maxlev)
352  amrex::Vector<std::unique_ptr<amrex::iMultiFab>> m_i_indx; // Ptr to 2D imf to hold i indices (maxlev)
353  amrex::Vector<std::unique_ptr<amrex::iMultiFab>> m_j_indx; // Ptr to 2D imf to hold j indices (maxlev)
354  amrex::Vector<std::unique_ptr<amrex::iMultiFab>> m_k_indx; // Ptr to 2D imf to hold k indices (maxlev)
355  amrex::Vector<amrex::Vector<std::unique_ptr<amrex::MultiFab>>> m_averages; // Ptr to 2D mf to hold averages (maxlev,navg)
356  amrex::Vector<amrex::Vector<std::unique_ptr<amrex::MultiFab>>> m_rot_fields; // Rotated field data
357 
358  // Vars for planar average policy
359  //--------------------------------------------
360  amrex::Vector<amrex::Vector<int>> m_ncell_plane; // Number of cells in plane (maxlev,navg)
361  amrex::Vector<amrex::Vector<amrex::Real>> m_plane_average; // Plane avgs (maxlev,navg)
362 
363  // Vars for point/region average policy
364  //--------------------------------------------
365  int m_radius{0}; // Radius around k_index
366  int m_ncell_region{1}; // Number of cells in local region
367  amrex::Vector<int> m_k_in; // Specified k_index for region avg (maxlev)
368 
369  // Vars for normal vector policy
370  //--------------------------------------------
371  bool m_interp{false}; // Do interpolation on destination?
372  bool m_norm_vec{false}; // Use normal vector to find IJK?
373 
374  // Vars for EB averaging policy
375  //--------------------------------------------
376  amrex::Vector<const eb_*> m_eb_vec; // Vector of pointers to EB objects (one per level)
377  amrex::Vector<amrex::Vector<amrex::Real>> m_total_bndry_area; // Total EB surface areas (maxlev, navg)
378 
379  // Time average w/ exponential filter fun
380  //--------------------------------------------
381  bool m_t_avg{false}; // Flag to do moving average in time
382  amrex::Vector<int> m_t_init; // Flag to specify if averages are initialized
383  double m_time_window{1.0e-16}; // Width of the exp filter function
384  amrex::Real m_fact_new, m_fact_old; // Time average factors for new and old means
385 
386  // Surface velocity correction
387  //--------------------------------------------
388  bool include_subgrid_vel = false;
389  amrex::Vector<amrex::Real> m_Vsg; // Subgrid velocity scale (Mahrt & Sun 1995 MWR)
390 
391  // Default values
392  //--------------------------------------------
394 };
395 #endif
constexpr amrex::Real one
Definition: ERF_Constants.H:9
constexpr amrex::Real fourth
Definition: ERF_Constants.H:14
constexpr amrex::Real zero
Definition: ERF_Constants.H:8
constexpr amrex::Real myhalf
Definition: ERF_Constants.H:13
Declares the embedded-boundary factory manager used by ERF levels.
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(m_cloud_chamber_config.active, "Cloud Chamber: initializer reached without a parsed configuration")
amrex::Real Real
Definition: ERF_ShocInterface.H:19
Definition: ERF_MOSTAverage.H:15
int m_navg
Definition: ERF_MOSTAverage.H:344
bool m_t_avg
Definition: ERF_MOSTAverage.H:381
void write_xz_positions(const int &lev, const int &j)
Definition: ERF_MOSTAverage.cpp:2022
void compute_plane_averages(const int &lev)
Definition: ERF_MOSTAverage.cpp:954
int m_policy
Definition: ERF_MOSTAverage.H:346
amrex::Vector< amrex::Vector< std::unique_ptr< amrex::MultiFab > > > m_averages
Definition: ERF_MOSTAverage.H:355
amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_y_pos
Definition: ERF_MOSTAverage.H:350
double m_time_window
Definition: ERF_MOSTAverage.H:383
int m_radius
Definition: ERF_MOSTAverage.H:365
void set_z_positions_EB(const int &lev)
Definition: ERF_MOSTAverage.cpp:577
void write_averages(const int &lev)
Definition: ERF_MOSTAverage.cpp:2064
amrex::Vector< std::unique_ptr< amrex::iMultiFab > > m_i_indx
Definition: ERF_MOSTAverage.H:352
void compute_region_averages(const int &lev)
Definition: ERF_MOSTAverage.cpp:1253
amrex::Vector< amrex::MultiFab * > m_z_phys_nd
Definition: ERF_MOSTAverage.H:336
amrex::Vector< int > m_t_init
Definition: ERF_MOSTAverage.H:382
void compute_averages(const int &lev)
Definition: ERF_MOSTAverage.cpp:925
void set_rotated_fields(const int &lev)
Definition: ERF_MOSTAverage.cpp:313
amrex::Vector< amrex::Vector< amrex::Real > > m_total_bndry_area
Definition: ERF_MOSTAverage.H:377
void set_region_normalization(const int &)
Definition: ERF_MOSTAverage.H:126
void set_z_positions_T(const int &lev)
Definition: ERF_MOSTAverage.cpp:770
amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_x_pos
Definition: ERF_MOSTAverage.H:349
void set_norm_positions_T(const int &lev)
Definition: ERF_MOSTAverage.cpp:835
amrex::Vector< amrex::Real > m_Vsg
Definition: ERF_MOSTAverage.H:389
void set_k_indices_T(const int &lev)
Definition: ERF_MOSTAverage.cpp:600
amrex::Vector< amrex::Vector< std::unique_ptr< amrex::MultiFab > > > m_rot_fields
Definition: ERF_MOSTAverage.H:356
bool m_rotate
Definition: ERF_MOSTAverage.H:347
amrex::MultiFab * get_zref(const int &lev) const
Definition: ERF_MOSTAverage.H:242
amrex::Vector< amrex::Vector< amrex::Real > > m_plane_average
Definition: ERF_MOSTAverage.H:361
amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_z_pos
Definition: ERF_MOSTAverage.H:351
amrex::Vector< amrex::Vector< int > > m_ncell_plane
Definition: ERF_MOSTAverage.H:360
void set_plane_normalization(const int &lev)
Definition: ERF_MOSTAverage.cpp:373
void write_k_indices(const int &lev)
Definition: ERF_MOSTAverage.cpp:1918
TerrainType m_terrain_type
Definition: ERF_MOSTAverage.H:339
std::string m_pp_prefix
Definition: ERF_MOSTAverage.H:337
bool m_norm_vec
Definition: ERF_MOSTAverage.H:372
int m_nvar
Definition: ERF_MOSTAverage.H:343
amrex::Real m_fact_new
Definition: ERF_MOSTAverage.H:384
const amrex::iMultiFab * get_k_indices(const int &lev) const
Definition: ERF_MOSTAverage.H:249
int m_ncell_region
Definition: ERF_MOSTAverage.H:366
void set_norm_indices_T(const int &lev)
Definition: ERF_MOSTAverage.cpp:681
amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_zref
Definition: ERF_MOSTAverage.H:348
int m_maxlev
Definition: ERF_MOSTAverage.H:345
const amrex::MultiFab * get_average(const int &lev, const int &comp) const
Definition: ERF_MOSTAverage.H:235
void set_eb_normalization(const int &lev)
Definition: ERF_MOSTAverage.cpp:431
bool include_subgrid_vel
Definition: ERF_MOSTAverage.H:388
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:293
~MOSTAverage()
Definition: ERF_MOSTAverage.H:39
MOSTAverage(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< const eb_ * > &eb_vec={})
MeshType m_mesh_type
Definition: ERF_MOSTAverage.H:338
amrex::Vector< std::unique_ptr< amrex::iMultiFab > > m_j_indx
Definition: ERF_MOSTAverage.H:353
void set_k_indices_N(const int &lev)
Definition: ERF_MOSTAverage.cpp:520
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)
Definition: ERF_MOSTAverage.cpp:104
MOSTAverage(MOSTAverage &&) noexcept=default
void write_norm_indices(const int &lev)
Definition: ERF_MOSTAverage.cpp:1965
amrex::Vector< amrex::Vector< amrex::MultiFab * > > m_fields
Definition: ERF_MOSTAverage.H:335
amrex::Vector< int > m_k_in
Definition: ERF_MOSTAverage.H:367
amrex::Vector< const eb_ * > m_eb_vec
Definition: ERF_MOSTAverage.H:376
const amrex::Real zref_default
Definition: ERF_MOSTAverage.H:393
amrex::Real m_fact_old
Definition: ERF_MOSTAverage.H:384
bool m_interp
Definition: ERF_MOSTAverage.H:371
const amrex::Vector< amrex::Geometry > m_geom
Definition: ERF_MOSTAverage.H:334
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:265
void compute_eb_averages(const int &lev)
Definition: ERF_MOSTAverage.cpp:1630
amrex::Vector< std::unique_ptr< amrex::iMultiFab > > m_k_indx
Definition: ERF_MOSTAverage.H:354
Definition: ERF_ConsoleIO.cpp:15