ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_ComputeDiffusivityMYNNEDMF.cpp File Reference
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
#include <functional>
#include <limits>
#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_ComputeDiffusivityMYNNEDMF.cpp:

Functions

void ComputeDiffusivityMYNNEDMF (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_sfc_nd, const std::unique_ptr< MultiFab > &z_phys_cc, const MoistureComponentIndices &moisture_indices)
 Compute eddy viscosity and diffusivity coefficients using the MYNN-EDMF closure. More...
 

Function Documentation

◆ ComputeDiffusivityMYNNEDMF()

void ComputeDiffusivityMYNNEDMF ( 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_sfc_nd,
const std::unique_ptr< MultiFab > &  z_phys_cc,
const MoistureComponentIndices &  moisture_indices 
)

Compute eddy viscosity and diffusivity coefficients using the MYNN-EDMF closure.

Parameters
[in]xvelHorizontal x-velocity field.
[in]yvelHorizontal y-velocity field.
[in]cons_inField of conservative variables.
[out]eddyViscosityMultiFab to be filled with computed eddy diffusivities.
[in]geomGrid geometry.
[in]turbChoiceTurbulence closure options and parameters.
[in]SurfLayerSurface layer data for MOST parameters.
[in]use_terrain_fitted_coordsFlag to use terrain-fitted vertical coordinates.
[in]use_moistureFlag to include moisture in buoyancy calculations.
[in]levelCurrent level index.
[in]bc_ptrPointer to boundary condition records.
[in]z_phys_ndPhysical height field at nodes.
[in]z_sfc_ndNodal heights of the bottom of the domain
[in]z_phys_ccPhysical height field at cell centers.
[in]moisture_indicesIndices for mapping moisture variables in the conservative field.
4211 {
4212  auto mynn = turbChoice.pbl_mynn;
4213  const StratType pbl_strat = turbChoice.pbl_strat_type;
4214  auto level2 = turbChoice.pbl_mynn_level2;
4215 
4216  Real Lt_alpha = mynn.Lt_alpha;
4217 
4218  // Dirichlet flags to switch derivative stencil
4219  bool c_ext_dir_on_zlo = ( (bc_ptr[BCVars::cons_bc].lo(2) == ERFBCType::ext_dir) );
4220  bool c_ext_dir_on_zhi = ( (bc_ptr[BCVars::cons_bc].hi(2) == ERFBCType::ext_dir) );
4221  bool u_ext_dir_on_zlo = ( (bc_ptr[BCVars::xvel_bc].lo(2) == ERFBCType::ext_dir) );
4222  bool u_ext_dir_on_zhi = ( (bc_ptr[BCVars::xvel_bc].hi(2) == ERFBCType::ext_dir) );
4223  bool v_ext_dir_on_zlo = ( (bc_ptr[BCVars::yvel_bc].lo(2) == ERFBCType::ext_dir) );
4224  bool v_ext_dir_on_zhi = ( (bc_ptr[BCVars::yvel_bc].hi(2) == ERFBCType::ext_dir) );
4225 
4226  // Epsilon
4228 
4229 #ifdef _OPENMP
4230 #pragma omp parallel if (Gpu::notInLaunchRegion())
4231 #endif
4232  // NOTE: we must not tile in z here because the body of this loop assumes that each
4233  // iterate spans the entire column: it accumulates vertical integrals into a
4234  // per-iterate qintegral fab (as in MYNN25)
4235  for ( MFIter mfi(eddyViscosity,TileNoZ()); mfi.isValid(); ++mfi) {
4236 
4237  // NOTE: the valid box, not a grown box, as in MYNN25. Growing by one in z put
4238  // k = -1 and k = nz in the loop, and the vertical-derivative stencil below
4239  // reaches k-1 and k+1, so the scheme read cons_in, xvel and yvel two cells
4240  // outside the domain -- ghost cells this routine has no guarantee about.
4241  // The ghost values it computed were thrown away regardless:
4242  // ComputeTurbulentViscosity refills every eddy-viscosity ghost cell after
4243  // this routine returns, by FillBoundary and by extrapolation onto the
4244  // physical-boundary planes.
4245  const Box& bx = mfi.tilebox();
4246  const Array4<Real const>& cell_data = cons_in.array(mfi);
4247  const Array4<Real >& K_turb = eddyViscosity.array(mfi);
4248  const Array4<Real const>& uvel = xvel.array(mfi);
4249  const Array4<Real const>& vvel = yvel.array(mfi);
4250 
4251  // Compute some quantities that are constant in each column: each iterate must
4252  // hold a whole column for the vertical integrals below to be complete
4253  const Box& dbx = geom.Domain();
4254  AMREX_ALWAYS_ASSERT(bx.smallEnd(2) == dbx.smallEnd(2) && bx.bigEnd(2) == dbx.bigEnd(2));
4255 
4256  const GeometryData gdata = geom.data();
4257 
4258  const Box xybx = PerpendicularBox<ZDir>(bx, IntVect{0,0,0});
4259 
4260  FArrayBox qintegral(xybx,2,The_Async_Arena());
4261  FArrayBox qturb(bx,1,The_Async_Arena());
4262 
4263  qintegral.setVal<RunOn::Device>(0);
4264 
4265  const Array4<Real> qint = qintegral.array();
4266  const Array4<Real> qvel = qturb.array();
4267 
4268  // vertical integrals to compute lengthscale
4269  if (use_terrain_fitted_coords) {
4270  const Array4<Real const> &z_nd_arr = z_phys_nd->array(mfi);
4271  const Array4<Real const> &z_sfc_arr = z_sfc_nd->const_array(mfi);
4272  const auto invCellSize = geom.InvCellSizeArray();
4273  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
4274  {
4275  qvel(i,j,k) = std::sqrt(two * cell_data(i,j,k,RhoKE_comp) / cell_data(i,j,k,Rho_comp));
4276  AMREX_ASSERT_WITH_MESSAGE(qvel(i,j,k) > zero, "KE must have a positive value");
4277 
4278  const Real Zval = Compute_Zrel_AtCellCenter(i,j,k,z_sfc_arr,z_nd_arr);
4279  const Real dz = Compute_h_zeta_AtCellCenter(i,j,k,invCellSize,z_nd_arr);
4280  Gpu::Atomic::Add(&qint(i,j,0,0), Zval*qvel(i,j,k)*dz);
4281  Gpu::Atomic::Add(&qint(i,j,0,1), qvel(i,j,k)*dz);
4282  });
4283  } else {
4284  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
4285  {
4286  qvel(i,j,k) = std::sqrt(two * cell_data(i,j,k,RhoKE_comp) / cell_data(i,j,k,Rho_comp));
4287  AMREX_ASSERT_WITH_MESSAGE(qvel(i,j,k) > zero, "KE must have a positive value");
4288 
4289  // Not multiplying by dz: its constant and would fall out when we divide qint0/qint1 anyway
4290 
4291  const Real Zval = (k + myhalf)*gdata.CellSize(2);
4292  Gpu::Atomic::Add(&qint(i,j,0,0), Zval*qvel(i,j,k));
4293  Gpu::Atomic::Add(&qint(i,j,0,1), qvel(i,j,k));
4294  });
4295  }
4296 
4297  int izmin = geom.Domain().smallEnd(2);
4298  int izmax = geom.Domain().bigEnd(2);
4299 
4300  // Spatially varying MOST
4301  Real d_kappa = KAPPA;
4302  Real d_gravity = CONST_GRAV;
4303 
4304  const auto& t_mean_mf = SurfLayer->get_mac_avg(level,5); // theta_v
4305  const auto& q_mean_mf = SurfLayer->get_mac_avg(level,4); // q_v
4306  const auto& u_star_mf = SurfLayer->get_u_star(level);
4307  const similarity_funs l_sfuns = SurfLayer->similarity_functions();
4308  const auto& t_star_mf = SurfLayer->get_t_star(level);
4309  const auto& q_star_mf = SurfLayer->get_q_star(level);
4310 
4311  const auto& tm_arr = t_mean_mf->const_array(mfi);
4312  const auto& qm_arr = q_mean_mf->const_array(mfi);
4313  const auto& u_star_arr = u_star_mf->const_array(mfi);
4314  const auto& t_star_arr = t_star_mf->const_array(mfi);
4315  const auto& q_star_arr = (use_moisture) ? q_star_mf->const_array(mfi) : Array4<Real>{};
4316 
4317  const Array4<Real const> z_nd_arr = z_phys_nd->const_array(mfi);
4318  const Array4<Real const> z_sfc_arr = z_sfc_nd->const_array(mfi);
4319  const PBLDerivativeDzInv_T pbl_derivative_dz_inv{z_phys_cc->const_array(mfi)};
4320 
4321  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
4322  {
4323  // Compute some partial derivatives that we will need (second order)
4324  // U and V derivatives are interpolated to account for staggered grid
4325  Real dthetadz, dudz, dvdz;
4327  uvel, vvel, cell_data, izmin, izmax, pbl_derivative_dz_inv(i,j,k),
4328  c_ext_dir_on_zlo, c_ext_dir_on_zhi,
4329  u_ext_dir_on_zlo, u_ext_dir_on_zhi,
4330  v_ext_dir_on_zlo, v_ext_dir_on_zhi,
4331  dthetadz, dudz, dvdz,
4332  moisture_indices, pbl_strat);
4333 
4334  // Spatially varying MOST
4335  Real theta0 = tm_arr(i,j,0);
4336  Real qv0 = qm_arr(i,j,0);
4337  Real surface_heat_flux = -u_star_arr(i,j,0) * t_star_arr(i,j,0);
4338  Real surface_latent_heat{0};
4339  if (use_moisture) {
4340  // Compute buoyancy flux (Stull Eqn. 4.4.5d)
4341  surface_latent_heat = -u_star_arr(i,j,0) * q_star_arr(i,j,0);
4342  surface_heat_flux *= (one + Real(0.61)*qv0);
4343  surface_heat_flux += Real(0.61) * theta0 * surface_latent_heat;
4344  }
4345 
4346  Real l_obukhov;
4347  if (std::abs(surface_heat_flux) > eps && u_star_arr(i,j,0) > eps) {
4348  l_obukhov = -( theta0 * u_star_arr(i,j,0)*u_star_arr(i,j,0)*u_star_arr(i,j,0) )
4349  / ( d_kappa * d_gravity * surface_heat_flux );
4350  } else {
4351  l_obukhov = std::numeric_limits<Real>::max();
4352  }
4353 
4354  // Surface-layer length scale (NN09, Eqn. 53)
4355  AMREX_ASSERT(l_obukhov != 0);
4356  const Real zval = use_terrain_fitted_coords
4357  ? Compute_Zrel_AtCellCenter(i,j,k,z_sfc_arr,z_nd_arr)
4358  : (k + myhalf)*gdata.CellSize(2);
4359  const Real zeta = zval/l_obukhov;
4360  Real l_S;
4361  if (zeta >= one) {
4362  l_S = KAPPA*zval/Real(3.7);
4363  } else if (zeta >= 0) {
4364  l_S = KAPPA*zval/(1+Real(2.7)*zeta);
4365  } else {
4366  l_S = KAPPA*zval*std::pow(one - Real(100.0) * zeta, Real(0.2));
4367  }
4368 
4369  // Replace the resolved gradients in the first cell with the MOST
4370  // profile gradients; see ApplySurfaceLayerGradientsPBL (ERF #4037)
4371  if (k == izmin) {
4373  sl.u_star = u_star_arr(i,j,0);
4374  sl.tstar_v = ComputeVirtualTStarPBL(t_star_arr(i,j,0),
4375  (use_moisture) ? q_star_arr(i,j,0) : zero,
4376  theta0, qv0, use_moisture);
4377  sl.zval = zval;
4378  sl.zeta = zeta;
4379  sl.sfuns = l_sfuns;
4380  ApplySurfaceLayerGradientsPBL(sl, dthetadz, dudz, dvdz);
4381  }
4382 
4383  // ABL-depth length scale (NN09, Eqn. 54)
4384  Real l_T;
4385  if (qint(i,j,0,1) > zero) {
4386  l_T = Lt_alpha*qint(i,j,0,0)/qint(i,j,0,1);
4387  } else {
4388  l_T = std::numeric_limits<Real>::max();
4389  }
4390 
4391  // Buoyancy length scale (NN09, Eqn. 55)
4392  Real l_B;
4393  if (dthetadz > 0) {
4394  Real N_brunt_vaisala = std::sqrt(CONST_GRAV/theta0 * dthetadz);
4395  if (zeta < 0) {
4396  Real qc = CONST_GRAV/theta0 * surface_heat_flux * l_T; // velocity scale
4397  qc = std::pow(qc,one/three);
4398  l_B = (one + Real(5.0)*std::sqrt(qc/(N_brunt_vaisala * l_T))) * qvel(i,j,k)/N_brunt_vaisala;
4399  } else {
4400  l_B = qvel(i,j,k) / N_brunt_vaisala;
4401  }
4402  } else {
4403  l_B = std::numeric_limits<Real>::max();
4404  }
4405 
4406  // Master length scale
4407  Real Lm;
4408  if (mynn.config == MYNNConfigType::Chen2021) {
4409  Lm = std::pow(one/(l_S*l_S) + one/(l_T*l_T) + one/(l_B*l_B), -myhalf);
4410  } else {
4411  // NN09, Eqn 52
4412  Lm = one / (one/l_S + one/l_T + one/l_B);
4413  }
4414 
4415  // Calculate nondimensional production terms
4416  Real shearProd = dudz*dudz + dvdz*dvdz;
4417  Real buoyProd = -(CONST_GRAV/theta0) * dthetadz;
4418  Real L2_over_q2 = Lm*Lm/(qvel(i,j,k)*qvel(i,j,k));
4419  Real GM = L2_over_q2 * shearProd;
4420  Real GH = L2_over_q2 * buoyProd;
4421 
4422  // Equilibrium (Level-2) q calculation follows NN09, Appendix 2
4423  Real Rf = level2.calc_Rf(GM, GH);
4424  Real SM2 = level2.calc_SM(Rf);
4425  Real qe2 = mynn.B1*Lm*Lm*SM2*(one-Rf)*shearProd;
4426  // Clamp before the sqrt rather than selecting after it: the optimizer
4427  // evaluates a guarded sqrt of a negative qe2 before the selection, which
4428  // trips amrex.fpe_trap_invalid. The value is unchanged for every qe2.
4429  Real qe = std::sqrt(amrex::max(qe2, amrex::Real(zero)));
4430 
4431  // Level 2 limiting (Helfand and Labraga 1988)
4432  Real alphac = (qvel(i,j,k) > qe) ? one : qvel(i,j,k) / (qe + eps);
4433 
4434  // Level Real(2.5) stability functions
4435  Real SM, SH, SQ;
4436  mynn.calc_stability_funcs(SM,SH,SQ,GM,GH,alphac);
4437 
4438  // Clip SM, SH, SQ following WRF. SQ is proportional to the *unclipped*
4439  // SM (NN09 Eqn. 67 is evaluated before SM is limited), so it needs its own
4440  // bounds; without them the TKE diffusivity below can go negative and turn
4441  // the vertical TKE diffusion anti-diffusive.
4442  SM = amrex::min(amrex::max(SM,mynn.SMmin), mynn.SMmax);
4443  SH = amrex::min(amrex::max(SH,mynn.SHmin), mynn.SHmax);
4444  SQ = amrex::min(amrex::max(SQ,mynn.SQmin), mynn.SQmax);
4445 
4446  // Finally, compute the eddy viscosity/diffusivities
4447  const Real rho = cell_data(i,j,k,Rho_comp);
4448  K_turb(i,j,k,EddyDiff::Mom_v) = rho * Lm * qvel(i,j,k) * SM;
4449  K_turb(i,j,k,EddyDiff::Theta_v) = rho * Lm * qvel(i,j,k) * SH;
4450  K_turb(i,j,k,EddyDiff::KE_v) = rho * Lm * qvel(i,j,k) * SQ;
4451 
4452  // TODO: implement partial-condensation scheme?
4453  // Currently, implementation matches NN09 without rain (i.e.,
4454  // the liquid water potential temperature is equal to the
4455  // potential temperature.
4456 
4457  // NN09 gives the total water content flux; this assumes that
4458  // all the species have the same eddy diffusivity.
4459  K_turb(i,j,k,EddyDiff::Q_v) = rho * Lm * qvel(i,j,k) * SH;
4460 
4461  K_turb(i,j,k,EddyDiff::Turb_lengthscale) = Lm;
4462  });
4463  }
4464 }
constexpr amrex::Real KAPPA
Definition: ERF_Constants.H:71
constexpr amrex::Real CONST_GRAV
Definition: ERF_Constants.H:72
#define Rho_comp
Definition: ERF_IndexDefines.H:61
#define RhoKE_comp
Definition: ERF_IndexDefines.H:63
const bool use_moisture
Definition: ERF_InitCustomPert_ABL.H:127
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
ParallelFor(fab_box, [=] AMREX_GPU_DEVICE(int i, int j, int k) { qrcuten_arr(i, j, k)=Real(0);qscuten_arr(i, j, k)=Real(0);qicuten_arr(i, j, k)=Real(0);})
constexpr amrex::Real 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:291
AMREX_GPU_DEVICE AMREX_FORCE_INLINE void ApplySurfaceLayerGradientsPBL(const PBLSurfaceLayerGradient &sl, amrex::Real &dthetadz, amrex::Real &dudz, amrex::Real &dvdz)
Definition: ERF_PBLModels.H:411
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:374
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_sfc_nd, const amrex::Array4< const amrex::Real > &z_nd)
Definition: ERF_TerrainMetrics.H:764
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real Compute_h_zeta_AtCellCenter(const int &i, const int &j, const int &k, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &cellSizeInv, const amrex::Array4< const amrex::Real > &z_nd)
Definition: ERF_TerrainMetrics.H:202
AMREX_FORCE_INLINE amrex::IntVect TileNoZ()
Definition: ERF_TileNoZ.H:11
AMREX_ASSERT_WITH_MESSAGE(wbar_cutoff_min > wbar_cutoff_max, "ERROR: wbar_cutoff_min < wbar_cutoff_max")
@ yvel_bc
Definition: ERF_IndexDefines.H:139
@ cons_bc
Definition: ERF_IndexDefines.H:122
@ xvel_bc
Definition: ERF_IndexDefines.H:138
@ ext_dir
Definition: ERF_IndexDefines.H:317
@ Theta_v
Definition: ERF_IndexDefines.H:267
@ Turb_lengthscale
Definition: ERF_IndexDefines.H:271
@ Q_v
Definition: ERF_IndexDefines.H:270
@ Mom_v
Definition: ERF_IndexDefines.H:266
@ KE_v
Definition: ERF_IndexDefines.H:268
@ rho
Definition: ERF_Kessler.H:25
@ qc
Definition: ERF_Morrison.H:110
@ xvel
Definition: ERF_IndexDefines.H:232
@ yvel
Definition: ERF_IndexDefines.H:233
@ dz
Definition: ERF_AdvanceWDM6.cpp:273
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Real eps()
Definition: ERF_ShocMoments.cpp:19
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:652
Definition: ERF_PBLModels.H:353
amrex::Real tstar_v
virtual temperature scale
Definition: ERF_PBLModels.H:355
amrex::Real zeta
stability coordinate z/L
Definition: ERF_PBLModels.H:357
similarity_funs sfuns
the surface layer's similarity functions (most.stable_functions)
Definition: ERF_PBLModels.H:358
amrex::Real u_star
friction velocity
Definition: ERF_PBLModels.H:354
amrex::Real zval
height of the cell center above the surface
Definition: ERF_PBLModels.H:356
MYNNLevel2 pbl_mynn_level2
MYNN level-2 closure coefficients for limiting.
Definition: ERF_TurbStruct.H:1058
StratType pbl_strat_type
Definition: ERF_TurbStruct.H:951
MYNNLevel25 pbl_mynn
MYNN level-2.5 closure coefficients.
Definition: ERF_TurbStruct.H:1057
Definition: ERF_MOSTUtils.H:84

Referenced by ComputeTurbulentViscosity().

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