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

Referenced by ComputeTurbulentViscosity().

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