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)
 

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 
)
4192 {
4193  Print()<<"reached mynnedmf"<<std::endl;
4194  {
4195  int n=1;
4196  Real a=1;
4197  Real b=1;
4198  Real c=1;
4199  Real d=1;
4200  Real x=0;
4201 #if 0
4202  tridiag2_cc(n,&a,&b,&c,&d,&x);
4203 #endif
4204  printf("ran tridiag2_cc with n=%d and got %g %g %g %g %g",n,a,b,c,d,x);
4205  }
4206 
4207  auto mynn = turbChoice.pbl_mynn;
4208  auto level2 = turbChoice.pbl_mynn_level2;
4209 
4210  Real Lt_alpha = (mynn.config == MYNNConfigType::CHEN2021) ? Real(0.1) : Real(0.23);
4211 
4212  // Dirichlet flags to switch derivative stencil
4213  bool c_ext_dir_on_zlo = ( (bc_ptr[BCVars::cons_bc].lo(2) == ERFBCType::ext_dir) );
4214  bool c_ext_dir_on_zhi = ( (bc_ptr[BCVars::cons_bc].hi(2) == ERFBCType::ext_dir) );
4215  bool u_ext_dir_on_zlo = ( (bc_ptr[BCVars::xvel_bc].lo(2) == ERFBCType::ext_dir) );
4216  bool u_ext_dir_on_zhi = ( (bc_ptr[BCVars::xvel_bc].hi(2) == ERFBCType::ext_dir) );
4217  bool v_ext_dir_on_zlo = ( (bc_ptr[BCVars::yvel_bc].lo(2) == ERFBCType::ext_dir) );
4218  bool v_ext_dir_on_zhi = ( (bc_ptr[BCVars::yvel_bc].hi(2) == ERFBCType::ext_dir) );
4219 
4220  // Epsilon
4222 
4223 #ifdef _OPENMP
4224 #pragma omp parallel if (Gpu::notInLaunchRegion())
4225 #endif
4226  for ( MFIter mfi(eddyViscosity,TilingIfNotGPU()); mfi.isValid(); ++mfi) {
4227 
4228  const Box &bx = mfi.growntilebox(1);
4229  const Array4<Real const>& cell_data = cons_in.array(mfi);
4230  const Array4<Real >& K_turb = eddyViscosity.array(mfi);
4231  const Array4<Real const>& uvel = xvel.array(mfi);
4232  const Array4<Real const>& vvel = yvel.array(mfi);
4233 
4234  // Compute some quantities that are constant in each column
4235  // Sbox is shrunk to only include the interior of the domain in the vertical direction to compute integrals
4236  // Box includes one ghost cell in each direction
4237  const Box &dbx = geom.Domain();
4238  Box sbx(bx.smallEnd(), bx.bigEnd());
4239  sbx.grow(2,-1);
4240  AMREX_ALWAYS_ASSERT(sbx.smallEnd(2) == dbx.smallEnd(2) && sbx.bigEnd(2) == dbx.bigEnd(2));
4241 
4242  const GeometryData gdata = geom.data();
4243 
4244  const Box xybx = PerpendicularBox<ZDir>(bx, IntVect{0,0,0});
4245 
4246  FArrayBox qintegral(xybx,2,The_Async_Arena());
4247  FArrayBox qturb(bx,1,The_Async_Arena());
4248  FArrayBox qturb_old(bx,1,The_Async_Arena());
4249 
4250  qintegral.setVal<RunOn::Device>(0);
4251 
4252  const Array4<Real> qint = qintegral.array();
4253  const Array4<Real> qvel = qturb.array();
4254 
4255  // vertical integrals to compute lengthscale
4256  if (use_terrain_fitted_coords) {
4257  const Array4<Real const> &z_nd_arr = z_phys_nd->array(mfi);
4258  const auto invCellSize = geom.InvCellSizeArray();
4259  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
4260  {
4261  qvel(i,j,k) = std::sqrt(two * cell_data(i,j,k,RhoKE_comp) / cell_data(i,j,k,Rho_comp));
4262  AMREX_ASSERT_WITH_MESSAGE(qvel(i,j,k) > zero, "KE must have a positive value");
4263 
4264  Real fac = (sbx.contains(i,j,k)) ? one : zero;
4265  const Real Zval = Compute_Zrel_AtCellCenter(i,j,k,z_nd_arr);
4266  const Real dz = Compute_h_zeta_AtCellCenter(i,j,k,invCellSize,z_nd_arr);
4267  Gpu::Atomic::Add(&qint(i,j,0,0), Zval*qvel(i,j,k)*dz*fac);
4268  Gpu::Atomic::Add(&qint(i,j,0,1), qvel(i,j,k)*dz*fac);
4269  });
4270  } else {
4271  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
4272  {
4273  qvel(i,j,k) = std::sqrt(two * cell_data(i,j,k,RhoKE_comp) / cell_data(i,j,k,Rho_comp));
4274  AMREX_ASSERT_WITH_MESSAGE(qvel(i,j,k) > zero, "KE must have a positive value");
4275 
4276  // Not multiplying by dz: its constant and would fall out when we divide qint0/qint1 anyway
4277 
4278  Real fac = (sbx.contains(i,j,k)) ? one : zero;
4279  const Real Zval = gdata.ProbLo(2) + (k + myhalf)*gdata.CellSize(2);
4280  Gpu::Atomic::Add(&qint(i,j,0,0), Zval*qvel(i,j,k)*fac);
4281  Gpu::Atomic::Add(&qint(i,j,0,1), qvel(i,j,k)*fac);
4282  });
4283  }
4284 
4285  int izmin = geom.Domain().smallEnd(2);
4286  int izmax = geom.Domain().bigEnd(2);
4287 
4288  // Spatially varying MOST
4289  Real d_kappa = KAPPA;
4290  Real d_gravity = CONST_GRAV;
4291 
4292  const auto& t_mean_mf = SurfLayer->get_mac_avg(level,4); // theta_v
4293  const auto& q_mean_mf = SurfLayer->get_mac_avg(level,3); // q_v
4294  const auto& u_star_mf = SurfLayer->get_u_star(level);
4295  const auto& t_star_mf = SurfLayer->get_t_star(level);
4296  const auto& q_star_mf = SurfLayer->get_q_star(level);
4297 
4298  const auto& tm_arr = t_mean_mf->const_array(mfi);
4299  const auto& qm_arr = q_mean_mf->const_array(mfi);
4300  const auto& u_star_arr = u_star_mf->const_array(mfi);
4301  const auto& t_star_arr = t_star_mf->const_array(mfi);
4302  const auto& q_star_arr = (use_moisture) ? q_star_mf->const_array(mfi) : Array4<Real>{};
4303 
4304  const Array4<Real const> z_nd_arr = z_phys_nd->const_array(mfi);
4305  const PBLDerivativeDzInv_T pbl_derivative_dz_inv{z_phys_cc->const_array(mfi)};
4306 
4307  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
4308  {
4309  // Compute some partial derivatives that we will need (second order)
4310  // U and V derivatives are interpolated to account for staggered grid
4311  Real dthetadz, dudz, dvdz;
4313  uvel, vvel, cell_data, izmin, izmax, pbl_derivative_dz_inv(i,j,k),
4314  c_ext_dir_on_zlo, c_ext_dir_on_zhi,
4315  u_ext_dir_on_zlo, u_ext_dir_on_zhi,
4316  v_ext_dir_on_zlo, v_ext_dir_on_zhi,
4317  dthetadz, dudz, dvdz,
4318  moisture_indices);
4319 
4320  // Spatially varying MOST
4321  Real theta0 = tm_arr(i,j,0);
4322  Real qv0 = qm_arr(i,j,0);
4323  Real surface_heat_flux = -u_star_arr(i,j,0) * t_star_arr(i,j,0);
4324  Real surface_latent_heat{0};
4325  if (use_moisture) {
4326  // Compute buoyancy flux (Stull Eqn. 4.4.5d)
4327  surface_latent_heat = -u_star_arr(i,j,0) * q_star_arr(i,j,0);
4328  surface_heat_flux *= (one + Real(0.61)*qv0);
4329  surface_heat_flux += Real(0.61) * theta0 * surface_latent_heat;
4330  }
4331 
4332  Real l_obukhov;
4333  if (std::abs(surface_heat_flux) > eps) {
4334  l_obukhov = -( theta0 * u_star_arr(i,j,0)*u_star_arr(i,j,0)*u_star_arr(i,j,0) )
4335  / ( d_kappa * d_gravity * surface_heat_flux );
4336  } else {
4337  l_obukhov = std::numeric_limits<Real>::max();
4338  }
4339 
4340  // Surface-layer length scale (NN09, Eqn. 53)
4341  AMREX_ASSERT(l_obukhov != 0);
4342  int lk = amrex::max(k,0);
4343  const Real zval = use_terrain_fitted_coords ? Compute_Zrel_AtCellCenter(i,j,lk,z_nd_arr)
4344  : gdata.ProbLo(2) + (lk + myhalf)*gdata.CellSize(2);
4345  const Real zeta = zval/l_obukhov;
4346  Real l_S;
4347  if (zeta >= one) {
4348  l_S = KAPPA*zval/Real(3.7);
4349  } else if (zeta >= 0) {
4350  l_S = KAPPA*zval/(1+Real(2.7)*zeta);
4351  } else {
4352  l_S = KAPPA*zval*std::pow(one - Real(100.0) * zeta, Real(0.2));
4353  }
4354 
4355  // ABL-depth length scale (NN09, Eqn. 54)
4356  Real l_T;
4357  if (qint(i,j,0,1) > zero) {
4358  l_T = Lt_alpha*qint(i,j,0,0)/qint(i,j,0,1);
4359  } else {
4360  l_T = std::numeric_limits<Real>::max();
4361  }
4362 
4363  // Buoyancy length scale (NN09, Eqn. 55)
4364  Real l_B;
4365  if (dthetadz > 0) {
4366  Real N_brunt_vaisala = std::sqrt(CONST_GRAV/theta0 * dthetadz);
4367  if (zeta < 0) {
4368  Real qc = CONST_GRAV/theta0 * surface_heat_flux * l_T; // velocity scale
4369  qc = std::pow(qc,one/three);
4370  l_B = (one + Real(5.0)*std::sqrt(qc/(N_brunt_vaisala * l_T))) * qvel(i,j,k)/N_brunt_vaisala;
4371  } else {
4372  l_B = qvel(i,j,k) / N_brunt_vaisala;
4373  }
4374  } else {
4375  l_B = std::numeric_limits<Real>::max();
4376  }
4377 
4378  // Master length scale
4379  Real Lm;
4380  if (mynn.config == MYNNConfigType::CHEN2021) {
4381  Lm = std::pow(one/(l_S*l_S) + one/(l_T*l_T) + one/(l_B*l_B), -myhalf);
4382  } else {
4383  // NN09, Eqn 52
4384  Lm = one / (one/l_S + one/l_T + one/l_B);
4385  }
4386 
4387  // Calculate nondimensional production terms
4388  Real shearProd = dudz*dudz + dvdz*dvdz;
4389  Real buoyProd = -(CONST_GRAV/theta0) * dthetadz;
4390  Real L2_over_q2 = Lm*Lm/(qvel(i,j,k)*qvel(i,j,k));
4391  Real GM = L2_over_q2 * shearProd;
4392  Real GH = L2_over_q2 * buoyProd;
4393 
4394  // Equilibrium (Level-2) q calculation follows NN09, Appendix 2
4395  Real Rf = level2.calc_Rf(GM, GH);
4396  Real SM2 = level2.calc_SM(Rf);
4397  Real qe2 = mynn.B1*Lm*Lm*SM2*(one-Rf)*shearProd;
4398  Real qe = (qe2 < zero) ? zero : std::sqrt(qe2);
4399 
4400  // Level 2 limiting (Helfand and Labraga 1988)
4401  Real alphac = (qvel(i,j,k) > qe) ? one : qvel(i,j,k) / (qe + eps);
4402 
4403  // Level Real(2.5) stability functions
4404  Real SM, SH, SQ;
4405  mynn.calc_stability_funcs(SM,SH,SQ,GM,GH,alphac);
4406 
4407  // Clip SM, SH following WRF
4408  SM = amrex::min(amrex::max(SM,mynn.SMmin), mynn.SMmax);
4409  SH = amrex::min(amrex::max(SH,mynn.SHmin), mynn.SHmax);
4410 
4411  // Finally, compute the eddy viscosity/diffusivities
4412  const Real rho = cell_data(i,j,k,Rho_comp);
4413  K_turb(i,j,k,EddyDiff::Mom_v) = rho * Lm * qvel(i,j,k) * SM;
4414  K_turb(i,j,k,EddyDiff::Theta_v) = rho * Lm * qvel(i,j,k) * SH;
4415  K_turb(i,j,k,EddyDiff::KE_v) = rho * Lm * qvel(i,j,k) * SQ;
4416 
4417  // TODO: implement partial-condensation scheme?
4418  // Currently, implementation matches NN09 without rain (i.e.,
4419  // the liquid water potential temperature is equal to the
4420  // potential temperature.
4421 
4422  // NN09 gives the total water content flux; this assumes that
4423  // all the species have the same eddy diffusivity
4424  if (mynn.diffuse_moistvars) {
4425  K_turb(i,j,k,EddyDiff::Q_v) = rho * Lm * qvel(i,j,k) * SH;
4426  }
4427 
4428  K_turb(i,j,k,EddyDiff::Turb_lengthscale) = Lm;
4429  });
4430  }
4431 }
constexpr amrex::Real three
Definition: ERF_Constants.H:11
constexpr amrex::Real KAPPA
Definition: ERF_Constants.H:63
constexpr amrex::Real two
Definition: ERF_Constants.H:10
constexpr amrex::Real one
Definition: ERF_Constants.H:9
constexpr amrex::Real zero
Definition: ERF_Constants.H:8
constexpr amrex::Real myhalf
Definition: ERF_Constants.H:13
constexpr amrex::Real CONST_GRAV
Definition: ERF_Constants.H:64
#define Rho_comp
Definition: ERF_IndexDefines.H:36
#define RhoKE_comp
Definition: ERF_IndexDefines.H:38
const bool use_moisture
Definition: ERF_InitCustomPert_Bomex.H:14
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
rho
Definition: ERF_InitCustomPert_Bubble.H:107
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);})
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)
Definition: ERF_PBLModels.H:254
amrex::Real Real
Definition: ERF_ShocInterface.H:19
AMREX_FORCE_INLINE AMREX_GPU_DEVICE 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:55
AMREX_GPU_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:389
AMREX_ASSERT_WITH_MESSAGE(wbar_cutoff_min > wbar_cutoff_max, "ERROR: wbar_cutoff_min < wbar_cutoff_max")
@ yvel_bc
Definition: ERF_IndexDefines.H:103
@ cons_bc
Definition: ERF_IndexDefines.H:86
@ xvel_bc
Definition: ERF_IndexDefines.H:102
@ ext_dir
Definition: ERF_IndexDefines.H:249
@ Theta_v
Definition: ERF_IndexDefines.H:212
@ Turb_lengthscale
Definition: ERF_IndexDefines.H:216
@ Q_v
Definition: ERF_IndexDefines.H:215
@ Mom_v
Definition: ERF_IndexDefines.H:211
@ KE_v
Definition: ERF_IndexDefines.H:213
@ qc
Definition: ERF_SatAdj.H:40
@ xvel
Definition: ERF_IndexDefines.H:177
@ yvel
Definition: ERF_IndexDefines.H:178
@ dz
Definition: ERF_AdvanceWSM6.cpp:104
real(c_double), parameter epsilon
Definition: ERF_module_model_constants.F90:12
Definition: ERF_PBLModels.H:416
MYNNLevel2 pbl_mynn_level2
MYNN level-2 closure coefficients for limiting.
Definition: ERF_TurbStruct.H:657
MYNNLevel25 pbl_mynn
MYNN level-2.5 closure coefficients.
Definition: ERF_TurbStruct.H:656

Referenced by ComputeTurbulentViscosity().

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