ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_PBLHeight.H
Go to the documentation of this file.
1 #ifndef ERF_PBL_HEIGHT_H_
2 #define ERF_PBL_HEIGHT_H_
3 
4 #include <AMReX_MultiFabUtil.H>
5 #include "ERF_Constants.H"
6 #include <ERF_TileNoZ.H>
7 #include <ERF_MoistUtils.H>
8 
9 /**
10  * @brief Diagnostic utility for the planetary boundary layer height.
11  */
12 struct MYNNPBLH {
13  /*
14  * Diagnose the PBL height
15  *
16  * Approach follows WRF, which uses a hybrid of the theta-increase
17  * method for CBLs and a TKE threshold method for SBLs. The TKE method is
18  * focused on PBL heights below ~500 m; above 1 km, the tanh blending makes
19  * the TKE-based contribution negligible.
20  *
21  * See Nielsen-Gammon et al. 2008, JAS
22  * Banta 2008, Acta Geophys.
23  */
24  AMREX_GPU_HOST
25  AMREX_FORCE_INLINE
26  void compute_pblh (const amrex::Geometry& geom,
27  const amrex::MultiFab* z_phys_cc,
28  amrex::MultiFab* pblh,
29  const amrex::MultiFab& cons,
30  const amrex::iMultiFab* lmask,
31  const MoistureComponentIndices& moisture_indices) const
32  {
33 #if 0
34  // NOTE: Cannot use ReduceToPlane because it clips the box to the
35  // validbox only, i.e., lateral ghost cells aren't updated
36  int dir = 2; // z
37  auto const& cons_arrs = cons.const_arrays();
38  auto thetav_min = amrex::ReduceToPlane<amrex::ReduceOpMin,amrex::Real>(dir, bxlow, cons,
39  [=] AMREX_GPU_DEVICE (int box_no, int i, int j, int k) -> amrex::Real
40  {
41  return GetThetav(i,j,k,cons_arrs[box_no],moisture_indices);
42  });
43 #endif
44 
45  // Create 2D multifabs like pblh
46  auto const& ba = pblh->boxArray();
47  auto const& dm = pblh->DistributionMap();
48  auto const& ng = pblh->nGrowVect();
49 
50  amrex::MultiFab min_thetav(ba,dm,1,ng);
51  min_thetav.setVal(bogus_large_value);
52 
53  amrex::MultiFab max_tke(ba,dm,1,ng);
54  max_tke.setVal(zero);
55 
56  amrex::MultiFab pblh_tke(ba,dm,1,ng);
57  pblh_tke.setVal(zero);
58 
59  pblh->setVal(zero);
60 
61  // The scan below ends at the top cell of each box and reads the cell above it, so
62  // the fields it reads must carry a ghost cell in z. On grids that all start at the
63  // ground that is the state's own halo; on grids that do not, it is the halo
64  // SurfaceLayer::compute_pblh copies onto the columns it hands over.
65  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(cons.nGrowVect()[2] >= 1,
66  "MYNNPBLH::compute_pblh: the state must carry a ghost cell in z; the scan reads "
67  "one cell above the top of every box");
68  AMREX_ALWAYS_ASSERT_WITH_MESSAGE((z_phys_cc == nullptr) || (z_phys_cc->nGrowVect()[2] >= 1),
69  "MYNNPBLH::compute_pblh: z_phys_cc must carry a ghost cell in z; the scan reads "
70  "one cell above the top of every box");
71 
72  // Now, loop over columns...
73  for (amrex::MFIter mfi(cons,TileNoZ()); mfi.isValid(); ++mfi)
74  {
75  const amrex::Box& domain = geom.Domain();
76 
77  // NOTE: this MFIter is over cons, which carries more ghost cells than the
78  // 2D fields we write here (pblh, pblh_tke, min_thetav all have the
79  // ghost cells of pblh). Grow by *their* ghost vector, not by the
80  // default, or we write past the end of their allocations.
81  //
82  // The scan covers the cells this box holds and no more. A box that stops
83  // below the top of the domain (a refined level of partial height) has no
84  // data above its top ghost cell, so the scan must stop with it; the
85  // k+1 reads below then end in that ghost cell. The caller hands over
86  // boxes that start at the ground (SurfaceLayer::compute_pblh builds them
87  // when the grids are stacked in z).
88  const amrex::Box& vbx = mfi.validbox();
89  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(vbx.smallEnd(2) == domain.smallEnd(2),
90  "MYNNPBLH::compute_pblh: every box must start at the bottom of the domain");
91  amrex::Box gtbx = mfi.growntilebox(ng);
92  gtbx.setSmall(2,vbx.smallEnd(2)); // don't loop over ghost cells
93  gtbx.setBig(2,vbx.bigEnd(2)); // in z
94 
95  // Columns are parallelized over; the k loops below are serialized within
96  // each thread. Everything in this routine is a per-column scan -- the
97  // minimum theta_v, and the lowest k at which each PBLH criterion is met --
98  // so parallelizing over k would both race on the 2D output and lose the
99  // "lowest k wins" semantics.
100  amrex::Box gtbx2d = gtbx; gtbx2d.setRange(2,0);
101  const int klo = gtbx.smallEnd(2);
102  const int khi = gtbx.bigEnd(2);
103 
104  auto min_thv_arr = min_thetav.array(mfi);
105  auto max_tke_arr = max_tke.array(mfi);
106  auto pblh_arr = pblh->array(mfi);
107  auto pblh_tke_arr = pblh_tke.array(mfi);
108 
109  const auto cons_arr = cons.const_array(mfi);
110  const auto lmask_arr = (lmask) ? lmask->const_array(mfi) : amrex::Array4<int> {};
111 
112  // -----------------------------------------------------
113  // WITH terrain/grid stretching
114  // -----------------------------------------------------
115  if (z_phys_cc)
116  {
117  const auto zphys_arr = z_phys_cc->const_array(mfi);
118 
119  // Need to sort out ghost cell differences (z_phys_cc has ng=1)
120  int imin = lbound(zphys_arr).x;
121  int jmin = lbound(zphys_arr).y;
122  int imax = ubound(zphys_arr).x;
123  int jmax = ubound(zphys_arr).y;
124 
125  // Heights here are above the ground. The ground is the bottom face of the
126  // column, where z_phys_cc in the ghost cell below mirrors the bottom cell.
127 
128  // Find minimum thetav and maximum TKE in the surface layer (this
129  // updates ghost cells, too); the bottom cell is always included
130  ParallelFor(gtbx2d, [=] AMREX_GPU_DEVICE(int i, int j, int) noexcept
131  {
132  int ii = amrex::max(amrex::min(i,imax),imin);
133  int jj = amrex::max(amrex::min(j,jmax),jmin);
134  const amrex::Real z_sfc = myhalf * (zphys_arr(ii,jj,klo) + zphys_arr(ii,jj,klo-1));
135 
136  amrex::Real min_thv = min_thv_arr(i,j,0);
137  amrex::Real maxtke = max_tke_arr(i,j,0);
138  for (int k(klo); k <= khi; ++k) {
139  if ((k == klo) || (zphys_arr(ii,jj,k) - z_sfc < thetamin_height)) {
140  amrex::Real thv = GetThetav(i, j, k, cons_arr, moisture_indices);
141  min_thv = amrex::min(min_thv, thv);
142  maxtke = amrex::max(maxtke, cons_arr(i,j,k,RhoKE_comp) / cons_arr(i,j,k,Rho_comp));
143  }
144  }
145  min_thv_arr(i,j,0) = min_thv;
146  max_tke_arr(i,j,0) = maxtke;
147  });
148 
149  // This depends on TileNoZ and k increasing monotonically
150  ParallelFor(gtbx2d, [=] AMREX_GPU_DEVICE(int i, int j, int) noexcept
151  {
152  int ii = amrex::max(amrex::min(i,imax),imin);
153  int jj = amrex::max(amrex::min(j,jmax),jmin);
154  const amrex::Real z_sfc = myhalf * (zphys_arr(ii,jj,klo) + zphys_arr(ii,jj,klo-1));
155 
156  const amrex::Real min_thv = min_thv_arr(i,j,0);
157  const int is_land = (lmask_arr) ? lmask_arr(i,j,0) : 1;
158 
159  amrex::Real zi = pblh_arr(i,j,0);
160  amrex::Real zi_tke = pblh_tke_arr(i,j,0);
161 
162  const amrex::Real TKEeps = tke_threshold(max_tke_arr(i,j,0));
163 
164  for (int k(klo); k <= khi; ++k)
165  {
166  if (zi == 0)
167  {
168  //
169  // Find PBL height based on thetav increase (best for CBLs)
170  //
171  amrex::Real thv = GetThetav(i, j, k , cons_arr, moisture_indices);
172  amrex::Real thv1 = GetThetav(i, j, k+1, cons_arr, moisture_indices);
173 
174  if (is_land && (thv1 >= min_thv + theta_incr_land)
175  && (thv < min_thv + theta_incr_land))
176  {
177  // Interpolate to get lowest height where theta = min_theta + theta_incr
178  zi = zphys_arr(ii,jj,k) - z_sfc
179  + (zphys_arr(ii,jj,k+1)-zphys_arr(ii,jj,k))/(thv1-thv)
180  * (min_thv + theta_incr_land - thv);
181  }
182  else if (!is_land && (thv1 >= min_thv + theta_incr_water)
183  && (thv < min_thv + theta_incr_water))
184  {
185  // Interpolate to get lowest height where theta = min_theta + theta_incr
186  zi = zphys_arr(ii,jj,k) - z_sfc
187  + (zphys_arr(ii,jj,k+1)-zphys_arr(ii,jj,k))/(thv1-thv)
188  * (min_thv + theta_incr_water - thv);
189  }
190  }
191  if (zi_tke == 0)
192  {
193  //
194  // Find PBL height based on TKE (for SBLs only)
195  //
196  amrex::Real tke = cons_arr(i,j,k ,RhoKE_comp) / cons_arr(i,j,k ,Rho_comp);
197  amrex::Real tke1 = cons_arr(i,j,k+1,RhoKE_comp) / cons_arr(i,j,k+1,Rho_comp);
198 
199  if ((tke1 <= TKEeps) && (tke > TKEeps))
200  {
201  // Interpolate to get lowest height where TKE -> 0
202  zi_tke = zphys_arr(ii,jj,k) - z_sfc
203  + (zphys_arr(ii,jj,k+1)-zphys_arr(ii,jj,k))/(tke1-tke)
204  * (TKEeps - tke);
205  }
206  }
207  // Both criteria are satisfied; the remaining levels are no-ops
208  if ((zi != 0) && (zi_tke != 0)) { break; }
209  }
210 
211  pblh_arr(i,j,0) = zi;
212  pblh_tke_arr(i,j,0) = zi_tke;
213  });
214  }
215  else
216  // -----------------------------------------------------
217  // NO terrain
218  // -----------------------------------------------------
219  {
220  const amrex::Real dz_no_terrain = geom.CellSize(2);
221 
222  // Find minimum thetav and maximum TKE in the surface layer (this
223  // updates ghost cells, too)
224  // - box size is known a priori: the cells whose centers, (k+1/2)*dz,
225  // lie below thetamin_height, the same strict test as with terrain;
226  // the bottom cell is always included
227  const int kmax = static_cast<int>(std::ceil(thetamin_height / dz_no_terrain - myhalf)) - 1;
228  const int khi_low = amrex::min(amrex::max(kmax, klo), khi);
229 
230  ParallelFor(gtbx2d, [=] AMREX_GPU_DEVICE(int i, int j, int) noexcept
231  {
232  amrex::Real min_thv = min_thv_arr(i,j,0);
233  amrex::Real maxtke = max_tke_arr(i,j,0);
234  for (int k(klo); k <= khi_low; ++k) {
235  amrex::Real thv = GetThetav(i, j, k, cons_arr, moisture_indices);
236  min_thv = amrex::min(min_thv, thv);
237  maxtke = amrex::max(maxtke, cons_arr(i,j,k,RhoKE_comp) / cons_arr(i,j,k,Rho_comp));
238  }
239  min_thv_arr(i,j,0) = min_thv;
240  max_tke_arr(i,j,0) = maxtke;
241  });
242 
243  // This depends on TileNoZ and k increasing monotonically
244  ParallelFor(gtbx2d, [=] AMREX_GPU_DEVICE(int i, int j, int) noexcept
245  {
246  const amrex::Real min_thv = min_thv_arr(i,j,0);
247  const int is_land = (lmask_arr) ? lmask_arr(i,j,0) : 1;
248 
249  amrex::Real zi = pblh_arr(i,j,0);
250  amrex::Real zi_tke = pblh_tke_arr(i,j,0);
251 
252  const amrex::Real TKEeps = tke_threshold(max_tke_arr(i,j,0));
253 
254  for (int k(klo); k <= khi; ++k)
255  {
256  if (zi == 0)
257  {
258  //
259  // Find PBL height based on thetav increase (best for CBLs)
260  //
261  amrex::Real thv = GetThetav(i, j, k , cons_arr, moisture_indices);
262  amrex::Real thv1 = GetThetav(i, j, k+1, cons_arr, moisture_indices);
263 
264  if (is_land && (thv1 >= min_thv + theta_incr_land)
265  && (thv < min_thv + theta_incr_land))
266  {
267  // Interpolate to get lowest height where theta = min_theta + theta_incr
268  zi = (k+myhalf)*dz_no_terrain
269  + dz_no_terrain/(thv1-thv)
270  * (min_thv + theta_incr_land - thv);
271  }
272  else if (!is_land && (thv1 >= min_thv + theta_incr_water)
273  && (thv < min_thv + theta_incr_water))
274  {
275  // Interpolate to get lowest height where theta = min_theta + theta_incr
276  zi = (k+myhalf)*dz_no_terrain
277  + dz_no_terrain/(thv1-thv)
278  * (min_thv + theta_incr_water - thv);
279  }
280  }
281  if (zi_tke == 0)
282  {
283  //
284  // Find PBL height based on TKE (for SBLs only)
285  //
286  amrex::Real tke = cons_arr(i,j,k ,RhoKE_comp) / cons_arr(i,j,k ,Rho_comp);
287  amrex::Real tke1 = cons_arr(i,j,k+1,RhoKE_comp) / cons_arr(i,j,k+1,Rho_comp);
288 
289  if ((tke1 <= TKEeps) && (tke > TKEeps))
290  {
291  // Interpolate to get lowest height where TKE -> 0
292  zi_tke = (k+myhalf)*dz_no_terrain
293  + dz_no_terrain/(tke1-tke) * (TKEeps - tke);
294  }
295  }
296  // Both criteria are satisfied; the remaining levels are no-ops
297  if ((zi != 0) && (zi_tke != 0)) { break; }
298  }
299 
300  pblh_arr(i,j,0) = zi;
301  pblh_tke_arr(i,j,0) = zi_tke;
302  });
303  }
304  }// MFIter
305 
306  //
307  // Calculate hybrid PBL height
308  //
309  for (amrex::MFIter mfi(*pblh); mfi.isValid(); ++mfi)
310  {
311  const auto cons_arr = cons.const_array(mfi);
312  auto pblh_tke_arr = pblh_tke.array(mfi);
313  auto pblh_arr = pblh->array(mfi);
314 
315  amrex::Box gtbx = mfi.growntilebox();
316  ParallelFor(gtbx, [=] AMREX_GPU_DEVICE(int i, int j, int) noexcept
317  {
318  //
319  // Clip PBLH_TKE to more realistic values
320  //
321  // Note from WRF MYNN-EDMF: TKE-based PBLH can be very large in cells
322  // with convective precipitation (> 8km!), so an artificial limit is
323  // imposed to not let PBLH_TKE exceed the theta_v-based PBL height
324  // +/- 350 m. This has no impact on 98-99% of the domain, but is the
325  // simplest patch that adequately addresses these extremely large
326  // PBLHs.
327  amrex::Real zi = pblh_arr(i,j,0);
328  pblh_tke_arr(i,j,0) = amrex::max(
329  amrex::min(pblh_tke_arr(i,j,0), zi+amrex::Real(350.)),
330  amrex::max(zi-amrex::Real(350.), amrex::Real(10.)));
331 
332  //
333  // Finally, blend between the two PBLH estimates
334  //
335  // The gate uses the surface qke (= 2 TKE), deliberately matching WRF,
336  // not the near-surface maximum used for the TKE threshold
337  amrex::Real qke_sfc = two * cons_arr(i,j,0 ,RhoKE_comp) / cons_arr(i,j,0 ,Rho_comp);
338  if (qke_sfc > amrex::Real(0.05)) {
339  amrex::Real wt = myhalf*std::tanh((zi - sbl_lim)/sbl_damp) + myhalf;
340  pblh_arr(i,j,0) = (one-wt)*pblh_tke_arr(i,j,0) + wt*pblh_arr(i,j,0);
341  }
342  });
343  }//MFIter
344  }
345 
346  /*
347  * TKE threshold for the TKE-based (SBL) PBL height: 5% of the maximum TKE in
348  * the surface layer (Kosovic & Curry 2000, JAS), floored at WRF's minimum.
349  * The maximum is taken over the same cells as the minimum theta_v, so it can
350  * sit above the first cell but is not set by turbulence far aloft.
351  */
352  AMREX_GPU_HOST_DEVICE
353  AMREX_FORCE_INLINE
354  static amrex::Real tke_threshold (const amrex::Real maxtke) noexcept
355  {
356  return amrex::max(amrex::Real(0.05) * maxtke, amrex::Real(0.02)); // min val from WRF
357  }
358 
359  /**
360  * @var MYNNPBLH::thetamin_height
361  * @brief Height below which minimum theta-v and maximum TKE are determined [m].
362  * @var MYNNPBLH::theta_incr_land
363  * @brief Theta increase determining the capping inversion height over land [K].
364  * @var MYNNPBLH::theta_incr_water
365  * @brief Theta increase determining the capping inversion height over water [K].
366  * @var MYNNPBLH::sbl_lim
367  * @brief Upper limit of the stable boundary layer height [m].
368  * @var MYNNPBLH::sbl_damp
369  * @brief Transition length for blending [m].
370  */
371  static constexpr amrex::Real thetamin_height = amrex::Real(200.0); // [m] height below which min thetav and max TKE are determined
372  static constexpr amrex::Real theta_incr_land = amrex::Real(1.25); // [K] theta increase that determines the height of the capping inversion over land
373  static constexpr amrex::Real theta_incr_water = one; // [K] theta increase that determines the height of the capping inversion over water
374  static constexpr amrex::Real sbl_lim = amrex::Real(200.0); // [m] upper limit of the stable BL height
375  static constexpr amrex::Real sbl_damp = amrex::Real(400.0); // [m] transition length for blending
376 };
377 #endif
constexpr amrex::Real bogus_large_value
Definition: ERF_Constants.H:19
#define Rho_comp
Definition: ERF_IndexDefines.H:61
#define RhoKE_comp
Definition: ERF_IndexDefines.H:63
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(axis >=0 &&axis< AMREX_SPACEDIM, "prob.axis must be 0, 1 or 2")
const int khi
Definition: ERF_InitCustomPert_Bubble.H:25
const int klo
Definition: ERF_InitCustomPert_KE.H:9
AMREX_GPU_DEVICE AMREX_FORCE_INLINE amrex::Real GetThetav(const int &i, const int &j, const int &k, const amrex::Array4< amrex::Real const > &cell_data, const MoistureComponentIndices &moisture_indices)
Definition: ERF_MoistUtils.H:93
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 two
Definition: ERF_NumericalConstants.H:38
constexpr amrex::Real one
Definition: ERF_NumericalConstants.H:37
constexpr amrex::Real zero
Definition: ERF_NumericalConstants.H:36
constexpr amrex::Real myhalf
Definition: ERF_NumericalConstants.H:41
AMREX_FORCE_INLINE amrex::IntVect TileNoZ()
Definition: ERF_TileNoZ.H:11
@ ng
Definition: ERF_Morrison.H:51
@ cons
Definition: ERF_IndexDefines.H:231
@ zi
Definition: ERF_AdvanceWDM6.cpp:277
Diagnostic utility for the planetary boundary layer height.
Definition: ERF_PBLHeight.H:12
static constexpr amrex::Real sbl_lim
Upper limit of the stable boundary layer height [m].
Definition: ERF_PBLHeight.H:374
AMREX_GPU_HOST AMREX_FORCE_INLINE void compute_pblh(const amrex::Geometry &geom, const amrex::MultiFab *z_phys_cc, amrex::MultiFab *pblh, const amrex::MultiFab &cons, const amrex::iMultiFab *lmask, const MoistureComponentIndices &moisture_indices) const
Definition: ERF_PBLHeight.H:26
static constexpr amrex::Real sbl_damp
Transition length for blending [m].
Definition: ERF_PBLHeight.H:375
static constexpr amrex::Real theta_incr_water
Theta increase determining the capping inversion height over water [K].
Definition: ERF_PBLHeight.H:373
static constexpr amrex::Real thetamin_height
Height below which minimum theta-v and maximum TKE are determined [m].
Definition: ERF_PBLHeight.H:371
static constexpr amrex::Real theta_incr_land
Theta increase determining the capping inversion height over land [K].
Definition: ERF_PBLHeight.H:372
AMREX_GPU_HOST_DEVICE static AMREX_FORCE_INLINE amrex::Real tke_threshold(const amrex::Real maxtke) noexcept
Definition: ERF_PBLHeight.H:354
The moisture data carried by the active microphysics scheme.
Definition: ERF_DataStruct.H:279