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