ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_ComputeDiffusivityMYNN25.cpp File Reference
#include "ERF_SurfaceLayer.H"
#include "ERF_DirectionSelector.H"
#include "ERF_Diffusion.H"
#include "ERF_Constants.H"
#include "ERF_TurbStruct.H"
#include "ERF_PBLModels.H"
Include dependency graph for ERF_ComputeDiffusivityMYNN25.cpp:

Macros

#define EXTRA_MYNN25_CHECKS   0
 

Functions

void ComputeDiffusivityMYNN25 (const MultiFab &xvel, const MultiFab &yvel, const MultiFab &cons_in, MultiFab &eddyViscosity, const Geometry &geom, const TurbChoice &turbChoice, std::unique_ptr< SurfaceLayer > &SurfLayer, bool use_terrain_fitted_coords, bool use_moisture, int level, const BCRec *bc_ptr, bool, const std::unique_ptr< MultiFab > &z_phys_nd, const std::unique_ptr< MultiFab > &z_phys_cc, const MoistureComponentIndices &moisture_indices)
 

Macro Definition Documentation

◆ EXTRA_MYNN25_CHECKS

#define EXTRA_MYNN25_CHECKS   0

Function Documentation

◆ ComputeDiffusivityMYNN25()

void ComputeDiffusivityMYNN25 ( const MultiFab &  xvel,
const MultiFab &  yvel,
const MultiFab &  cons_in,
MultiFab &  eddyViscosity,
const Geometry &  geom,
const TurbChoice &  turbChoice,
std::unique_ptr< SurfaceLayer > &  SurfLayer,
bool  use_terrain_fitted_coords,
bool  use_moisture,
int  level,
const BCRec *  bc_ptr,
bool  ,
const std::unique_ptr< MultiFab > &  z_phys_nd,
const std::unique_ptr< MultiFab > &  z_phys_cc,
const MoistureComponentIndices &  moisture_indices 
)

Compute eddy diffusivities using the MYNN PBL model.

Parameters
[in]xvelX-velocity MultiFab.
[in]yvelY-velocity MultiFab.
[in]cons_inConserved variables MultiFab.
[out]eddyViscosityMultiFab for storing computed eddy diffusivities.
[in]geomGrid geometry.
[in]turbChoiceTurbulence model options and configuration.
[in]SurfLayerSurface layer data.
[in]use_terrain_fitted_coordsFlag to use terrain-fitted coordinates.
[in]use_moistureFlag to include moisture.
[in]levelLevel index.
[in]bc_ptrBoundary condition record pointers.
[in]z_phys_ndNodal physical height.
[in]z_phys_ccCell-centered physical height.
[in]moisture_indicesMoisture component indices.
46 {
47  const StratType pbl_strat = turbChoice.pbl_strat_type;
48  auto mynn = turbChoice.pbl_mynn;
49  auto level2 = turbChoice.pbl_mynn_level2;
50 
51  Real Lt_alpha = mynn.Lt_alpha;
52 
53  // Optional boundary-layer-depth taper of l_T; needs a diagnosed PBL height.
54  const bool use_Lt_taper = (mynn.Lt_taper_exp > zero);
55  const Real Lt_taper_exp = mynn.Lt_taper_exp;
56  if (use_Lt_taper && !SurfLayer->computes_pblh()) {
57  Abort("erf.pbl_mynn_Lt_taper_exp > 0 needs a diagnosed PBL height; "
58  "set erf.most.pblh_calc = MYNN25");
59  }
60 
61  // Dirichlet flags to switch derivative stencil
62  bool c_ext_dir_on_zlo = ( (bc_ptr[BCVars::cons_bc].lo(2) == ERFBCType::ext_dir) );
63  bool c_ext_dir_on_zhi = ( (bc_ptr[BCVars::cons_bc].hi(2) == ERFBCType::ext_dir) );
64  bool u_ext_dir_on_zlo = ( (bc_ptr[BCVars::xvel_bc].lo(2) == ERFBCType::ext_dir) );
65  bool u_ext_dir_on_zhi = ( (bc_ptr[BCVars::xvel_bc].hi(2) == ERFBCType::ext_dir) );
66  bool v_ext_dir_on_zlo = ( (bc_ptr[BCVars::yvel_bc].lo(2) == ERFBCType::ext_dir) );
67  bool v_ext_dir_on_zhi = ( (bc_ptr[BCVars::yvel_bc].hi(2) == ERFBCType::ext_dir) );
68 
69  // Epsilon
71 
72 #ifdef _OPENMP
73 #pragma omp parallel if (Gpu::notInLaunchRegion())
74 #endif
75  for ( MFIter mfi(eddyViscosity,TileNoZ()); mfi.isValid(); ++mfi) {
76 
77  const Box& bx = mfi.tilebox();
78  const Array4<Real const>& cell_data = cons_in.array(mfi);
79  const Array4<Real >& K_turb = eddyViscosity.array(mfi);
80  const Array4<Real const>& uvel = xvel.array(mfi);
81  const Array4<Real const>& vvel = yvel.array(mfi);
82 
83  // Compute some quantities that are constant in each column
84  // Sbox is shrunk to only include the interior of the domain in the vertical direction to compute integrals
85  // Box includes one ghost cell in each direction
86  const Box& dbx = geom.Domain();
87  AMREX_ALWAYS_ASSERT(bx.smallEnd(2) == dbx.smallEnd(2) && bx.bigEnd(2) == dbx.bigEnd(2));
88 
89  const GeometryData gdata = geom.data();
90 
91  const Box xybx = makeSlab(bx,2,0);
92  FArrayBox qturb(bx,1,The_Async_Arena());
93  FArrayBox qintegral(xybx,2,The_Async_Arena());
94  qintegral.setVal<RunOn::Device>(zero);
95  const Array4<Real> qint = qintegral.array();
96  const Array4<Real> qvel = qturb.array();
97 
98  // vertical integrals to compute lengthscale
99  if (use_terrain_fitted_coords) {
100  const Array4<Real const> &z_nd_arr = z_phys_nd->array(mfi);
101  const auto invCellSize = geom.InvCellSizeArray();
102  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
103  {
104  // q^2 / 2 is the TKE
105  qvel(i,j,k) = std::sqrt(two * cell_data(i,j,k,RhoKE_comp) / cell_data(i,j,k,Rho_comp));
106  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(qvel(i,j,k) > zero, "KE must have a positive value");
107 
108  const Real Zval = Compute_Zrel_AtCellCenter(i,j,k,z_nd_arr);
109  const Real dz = Compute_h_zeta_AtCellCenter(i,j,k,invCellSize,z_nd_arr);
110  Gpu::Atomic::Add(&qint(i,j,0,0), Zval*qvel(i,j,k)*dz);
111  Gpu::Atomic::Add(&qint(i,j,0,1), qvel(i,j,k)*dz);
112  });
113  } else {
114  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
115  {
116  // q^2 / 2 is the TKE
117  qvel(i,j,k) = std::sqrt(two * cell_data(i,j,k,RhoKE_comp) / cell_data(i,j,k,Rho_comp));
118  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(qvel(i,j,k) > zero, "KE must have a positive value");
119 
120  // Not multiplying by dz: it's constant and would fall out when we divide qint0/qint1 anyway
121 
122  const Real Zval = (k + myhalf)*gdata.CellSize(2);
123  Gpu::Atomic::Add(&qint(i,j,0,0), Zval*qvel(i,j,k));
124  Gpu::Atomic::Add(&qint(i,j,0,1), qvel(i,j,k));
125  });
126  }
127 
128  int izmin = geom.Domain().smallEnd(2);
129  int izmax = geom.Domain().bigEnd(2);
130 
131  // Spatially varying MOST
132  Real d_kappa = KAPPA;
133  Real d_gravity = CONST_GRAV;
134 
135  const auto& t_mean_mf = SurfLayer->get_mac_avg(level,5); // theta_v
136  const auto& q_mean_mf = SurfLayer->get_mac_avg(level,4); // q_v
137  const auto& u_star_mf = SurfLayer->get_u_star(level);
138  const auto& t_star_mf = SurfLayer->get_t_star(level);
139  const auto& q_star_mf = SurfLayer->get_q_star(level);
140 
141  const auto& tm_arr = t_mean_mf->const_array(mfi);
142  const auto& qm_arr = q_mean_mf->const_array(mfi);
143  const auto& u_star_arr = u_star_mf->const_array(mfi);
144  const auto& t_star_arr = t_star_mf->const_array(mfi);
145  const auto& q_star_arr = (use_moisture) ? q_star_mf->const_array(mfi) : Array4<Real>{};
146 
147  const auto& pblh_mf = SurfLayer->get_pblh(level);
148  const auto pblh_arr = (use_Lt_taper) ? pblh_mf->const_array(mfi)
149  : Array4<const Real>{};
150 
151  const Array4<Real const> z_nd_arr = z_phys_nd->const_array(mfi);
152  const PBLDerivativeDzInv_T pbl_derivative_dz_inv{z_phys_cc->const_array(mfi)};
153 
154  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
155  {
156  // Compute some partial derivatives that we will need (second order)
157  // U and V derivatives are interpolated to account for staggered grid
158  Real dthetavdz, dudz, dvdz;
160  uvel, vvel, cell_data, izmin, izmax, pbl_derivative_dz_inv(i,j,k),
161  c_ext_dir_on_zlo, c_ext_dir_on_zhi,
162  u_ext_dir_on_zlo, u_ext_dir_on_zhi,
163  v_ext_dir_on_zlo, v_ext_dir_on_zhi,
164  dthetavdz, dudz, dvdz,
165  moisture_indices, pbl_strat);
166 
167  // Spatially varying MOST
168  Real theta0 = tm_arr(i,j,0);
169  Real qv0 = qm_arr(i,j,0);
170  Real surface_heat_flux = -u_star_arr(i,j,0) * t_star_arr(i,j,0);
171  Real surface_latent_heat{0};
172  if (use_moisture) {
173  // Compute buoyancy flux (Stull Eqn. 4.4.5d)
174  surface_latent_heat = -u_star_arr(i,j,0) * q_star_arr(i,j,0);
175  surface_heat_flux *= (one + epsv*qv0);
176  surface_heat_flux += epsv * theta0 * surface_latent_heat;
177  }
178 
179  Real l_obukhov;
180  if (std::abs(surface_heat_flux) > eps && u_star_arr(i,j,0) > eps) {
181  l_obukhov = -( theta0 * u_star_arr(i,j,0)*u_star_arr(i,j,0)*u_star_arr(i,j,0) )
182  / ( d_kappa * d_gravity * surface_heat_flux );
183  } else {
184  l_obukhov = std::numeric_limits<Real>::max();
185  }
186 
187  // Surface-layer length scale (NN09, Eqn. 53)
188  AMREX_ASSERT(l_obukhov != 0);
189  const Real zval = use_terrain_fitted_coords ? Compute_Zrel_AtCellCenter(i,j,k,z_nd_arr) :
190  (k + myhalf)*gdata.CellSize(2);
191  const Real zeta = zval/l_obukhov;
192  Real l_S;
193  if (zeta >= one) {
194  l_S = KAPPA*zval/Real(3.7);
195  } else if (zeta >= 0) {
196  l_S = KAPPA*zval/(one + Real(2.7) * zeta);
197  } else {
198  l_S = KAPPA*zval*std::pow(one - Real(100.0) * zeta, Real(0.2));
199  }
200 
201  // At the first cell center the vertical gradients computed above are
202  // built from the foextrap (cons) and hoextrap (velocity) ghost values
203  // that zlo.type = surface_layer installs, and those carry no surface
204  // information: the potential-temperature gradient comes out half the
205  // one-sided value and the velocity gradient has no dependence on the
206  // friction velocity at all (ERF #4037). Replace them with the MOST
207  // profile gradients that the surface layer is imposing on this cell,
208  // |dU/dz| = u_* phi_m(zeta) / (kappa z)
209  // dthetav/dz = thetav_* phi_h(zeta) / (kappa z)
210  // which is the same profile the stress and heat flux were derived from.
211  if (k == izmin) {
213  sl.u_star = u_star_arr(i,j,0);
214  sl.tstar_v = ComputeVirtualTStarPBL(t_star_arr(i,j,0),
215  (use_moisture) ? q_star_arr(i,j,0) : zero,
216  theta0, qv0, use_moisture);
217  sl.zval = zval;
218  sl.zeta = zeta;
219  ApplySurfaceLayerGradientsPBL(sl, dthetavdz, dudz, dvdz);
220  }
221 
222  // ABL-depth length scale (NN09, Eqn. 54)
223  Real l_T;
224  if (qint(i,j,0,1) > zero) {
225  l_T = Lt_alpha*qint(i,j,0,0)/qint(i,j,0,1);
226  } else {
227  l_T = std::numeric_limits<Real>::max();
228  }
229 
230  // l_T is also the ABL-depth scale that the convective branch of
231  // l_B below is built from; keep that one untapered, since there it
232  // stands for the depth of the layer rather than a mixing length.
233  const Real l_T_abl = l_T;
234 
235  // Optional taper toward the top of the boundary layer. The factor
236  // is floored rather than allowed to reach zero so that the 1/l_T
237  // term in the master length scale stays finite.
238  //
239  // NOTE: above z = h the factor clamps at that floor, so the master
240  // length scale is driven to ~0 there. That is harmless when the
241  // turbulence is confined to the boundary layer, but it would
242  // suppress genuine elevated shear-driven mixing; hence the taper
243  // is opt-in rather than a default.
244  if (use_Lt_taper) {
245  const Real pblh = pblh_arr(i,j,0);
246  if (pblh > zero) {
247  Real f = one - zval/pblh;
248  f = (f < Real(1.0e-4)) ? Real(1.0e-4) : f;
249  l_T *= std::pow(f, Lt_taper_exp);
250  }
251  }
252 
253  // Buoyancy length scale (NN09, Eqn. 55)
254  Real l_B;
255  if (dthetavdz > zero) {
256  Real N_brunt_vaisala = std::sqrt(CONST_GRAV/theta0 * dthetavdz);
257  if (zeta < zero) {
258  Real qc = CONST_GRAV/theta0 * surface_heat_flux * l_T_abl; // velocity scale
259  qc = std::pow(qc,one/three);
260  l_B = (one + Real(5.0)*std::sqrt(qc/(N_brunt_vaisala * l_T_abl))) * qvel(i,j,k)/N_brunt_vaisala;
261  } else {
262  l_B = qvel(i,j,k) / N_brunt_vaisala;
263  }
264  } else {
265  l_B = std::numeric_limits<Real>::max();
266  }
267 
268  // Master length scale
269  Real Lm;
270  if (mynn.config == MYNNConfigType::Chen2021) {
271  Lm = std::pow(one/(l_S*l_S) + one/(l_T*l_T) + one/(l_B*l_B), -myhalf);
272  } else {
273  // NN09, Eqn 52
274  Lm = one / (one/l_S + one/l_T + one/l_B);
275  }
276 
277  // Calculate nondimensional production terms
278  Real shearProd = dudz*dudz + dvdz*dvdz;
279  Real buoyProd = -(CONST_GRAV/theta0) * dthetavdz;
280  Real L2_over_q2 = Lm*Lm/(qvel(i,j,k)*qvel(i,j,k));
281  Real GM = L2_over_q2 * shearProd;
282  Real GH = L2_over_q2 * buoyProd;
283 
284  // Equilibrium (Level-2) q calculation follows NN09, Appendix A
285  Real Rf = level2.calc_Rf(GM, GH);
286  Real SM2 = level2.calc_SM(Rf);
287  Real qe2 = mynn.B1 * Lm*Lm * SM2 * (one-Rf) * shearProd;
288  Real qe = (qe2 < zero) ? zero : amrex::max(std::sqrt(qe2),eps);
289 
290  // Level 2 limiting introduced by Helfand and Labraga 1988 (NN09, Eqn. 42)
291  Real alphac = (qvel(i,j,k) >= qe) ? one : qvel(i,j,k) / qe;
292 //#if EXTRA_MYNN25_CHECKS
293 #if 0
294  // VERY verbose diagnostic
295  Real lGM = std::copysign(std::max(std::fabs(GM),level2.eps),GM);
296  Real Ri = -GH/lGM;
297  if (alphac < one) {
298  AllPrint() << "Level 2 limiter at " << IntVect(i,j,k) << " :"
299  << " ustar= " << u_star_arr(i,j,0)
300  << " alphac= " << alphac
301  << " Ri,SM2,SH2= " << Ri << " " << SM2 << " " << level2.calc_SH(Rf)
302  << std::endl;
303  }
304 #endif
305 
306  // Level Real(2.5) stability functions
307  Real SM, SH, SQ;
308  mynn.calc_stability_funcs(SM,SH,SQ,GM,GH,alphac);
309 
310  // Clip SM, SH following WRF
311  SM = amrex::min(amrex::max(SM, mynn.SMmin), mynn.SMmax);
312  SH = amrex::min(amrex::max(SH, mynn.SHmin), mynn.SHmax);
313  SQ = amrex::min(amrex::max(SQ, mynn.SQmin), mynn.SQmax);
314 #if EXTRA_MYNN25_CHECKS
315  if (SM == mynn.SMmin) {
316  Warning("SM clipped at min val");
317  } else if (SM == mynn.SMmax) {
318  Warning("SM clipped at max val");
319  }
320  if (SH == mynn.SHmin) {
321  Warning("SH clipped at min val");
322  } else if (SH == mynn.SHmax) {
323  Warning("SH clipped at max val");
324  }
325 #endif
326 
327  // Finally, compute the eddy viscosity/diffusivities
328  const Real rho = cell_data(i,j,k,Rho_comp);
329  K_turb(i,j,k,EddyDiff::Mom_v) = rho * Lm * qvel(i,j,k) * SM;
330  K_turb(i,j,k,EddyDiff::Theta_v) = rho * Lm * qvel(i,j,k) * SH;
331  K_turb(i,j,k,EddyDiff::KE_v) = rho * Lm * qvel(i,j,k) * SQ;
332 
333  // TODO: implement partial-condensation scheme?
334  // Currently, implementation matches NN09 without rain (i.e.,
335  // the liquid water potential temperature is equal to the
336  // potential temperature.
337 
338  // NN09 gives the total water content flux; this assumes that
339  // all the species have the same eddy diffusivity. eddyDiffs is
340  // zeroed once at allocation and nothing else writes Q_v, so this
341  // must be unconditional: skipping it leaves the moisture variables
342  // with a surface flux and no vertical turbulent transport.
343  K_turb(i,j,k,EddyDiff::Q_v) = rho * Lm * qvel(i,j,k) * SH;
344 
345  K_turb(i,j,k,EddyDiff::Turb_lengthscale) = Lm;
346  });
347  }
348 }
constexpr amrex::Real epsv
Definition: ERF_Constants.H:52
constexpr amrex::Real KAPPA
Definition: ERF_Constants.H:67
constexpr amrex::Real CONST_GRAV
Definition: ERF_Constants.H:68
#define Rho_comp
Definition: ERF_IndexDefines.H:42
#define RhoKE_comp
Definition: ERF_IndexDefines.H:44
const bool use_moisture
Definition: ERF_InitCustomPert_ABL.H:71
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(m_cloud_chamber_config.active, "Cloud Chamber: initializer reached without a parsed configuration")
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 three
Definition: ERF_NumericalConstants.H:39
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_GPU_DEVICE AMREX_FORCE_INLINE void ComputeVerticalDerivativesPBL(int i, int j, int k, const amrex::Array4< const amrex::Real > &uvel, const amrex::Array4< const amrex::Real > &vvel, const amrex::Array4< const amrex::Real > &cell_data, const int izmin, const int izmax, const PBLDerivativeDzInv &dz_inv, const bool c_ext_dir_on_zlo, const bool c_ext_dir_on_zhi, const bool u_ext_dir_on_zlo, const bool u_ext_dir_on_zhi, const bool v_ext_dir_on_zlo, const bool v_ext_dir_on_zhi, amrex::Real &dthetadz, amrex::Real &dudz, amrex::Real &dvdz, const MoistureComponentIndices &moisture_indices, const StratType strat=StratType::thetav)
Definition: ERF_PBLModels.H:304
AMREX_GPU_DEVICE AMREX_FORCE_INLINE void ApplySurfaceLayerGradientsPBL(const PBLSurfaceLayerGradient &sl, amrex::Real &dthetadz, amrex::Real &dudz, amrex::Real &dvdz)
Definition: ERF_PBLModels.H:423
AMREX_GPU_DEVICE AMREX_FORCE_INLINE amrex::Real ComputeVirtualTStarPBL(amrex::Real t_star, amrex::Real q_star, amrex::Real theta, amrex::Real qv, bool use_moisture)
Definition: ERF_PBLModels.H:386
amrex::Real Real
Definition: ERF_ShocInterface.H:19
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real Compute_Zrel_AtCellCenter(const int &i, const int &j, const int &k, const amrex::Array4< const amrex::Real > &z_nd)
Definition: ERF_TerrainMetrics.H:758
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:197
AMREX_FORCE_INLINE amrex::IntVect TileNoZ()
Definition: ERF_TileNoZ.H:11
@ yvel_bc
Definition: ERF_IndexDefines.H:109
@ cons_bc
Definition: ERF_IndexDefines.H:92
@ xvel_bc
Definition: ERF_IndexDefines.H:108
@ ext_dir
Definition: ERF_IndexDefines.H:300
@ Theta_v
Definition: ERF_IndexDefines.H:253
@ Turb_lengthscale
Definition: ERF_IndexDefines.H:257
@ Q_v
Definition: ERF_IndexDefines.H:256
@ Mom_v
Definition: ERF_IndexDefines.H:252
@ KE_v
Definition: ERF_IndexDefines.H:254
@ rho
Definition: ERF_Kessler.H:25
@ qc
Definition: ERF_SatAdj.H:42
@ xvel
Definition: ERF_IndexDefines.H:218
@ yvel
Definition: ERF_IndexDefines.H:219
@ dz
Definition: ERF_AdvanceWDM6.cpp:272
real(c_double), parameter epsilon
Definition: ERF_module_model_constants.F90:12
Functor for inverse vertical spacings for terrain-following grids using cell-center heights.
Definition: ERF_PBLModels.H:663
Definition: ERF_PBLModels.H:366
amrex::Real tstar_v
virtual temperature scale
Definition: ERF_PBLModels.H:368
amrex::Real zeta
stability coordinate z/L
Definition: ERF_PBLModels.H:370
amrex::Real u_star
friction velocity
Definition: ERF_PBLModels.H:367
amrex::Real zval
height of the cell center above the surface
Definition: ERF_PBLModels.H:369
MYNNLevel2 pbl_mynn_level2
MYNN level-2 closure coefficients for limiting.
Definition: ERF_TurbStruct.H:918
StratType pbl_strat_type
Definition: ERF_TurbStruct.H:819
MYNNLevel25 pbl_mynn
MYNN level-2.5 closure coefficients.
Definition: ERF_TurbStruct.H:917

Referenced by ComputeTurbulentViscosity().

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