ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 int RhoQv_comp, const int RhoQc_comp, const int RhoQr_comp)
 

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 int  RhoQv_comp,
const int  RhoQc_comp,
const int  RhoQr_comp 
)
29 {
30  auto mynn = turbChoice.pbl_mynn;
31  auto level2 = turbChoice.pbl_mynn_level2;
32 
33  Real Lt_alpha = (mynn.config == MYNNConfigType::CHEN2021) ? 0.1 : 0.23;
34 
35  // Dirichlet flags to switch derivative stencil
36  bool c_ext_dir_on_zlo = ( (bc_ptr[BCVars::cons_bc].lo(2) == ERFBCType::ext_dir) );
37  bool c_ext_dir_on_zhi = ( (bc_ptr[BCVars::cons_bc].lo(5) == ERFBCType::ext_dir) );
38  bool u_ext_dir_on_zlo = ( (bc_ptr[BCVars::xvel_bc].lo(2) == ERFBCType::ext_dir) );
39  bool u_ext_dir_on_zhi = ( (bc_ptr[BCVars::xvel_bc].lo(5) == ERFBCType::ext_dir) );
40  bool v_ext_dir_on_zlo = ( (bc_ptr[BCVars::yvel_bc].lo(2) == ERFBCType::ext_dir) );
41  bool v_ext_dir_on_zhi = ( (bc_ptr[BCVars::yvel_bc].lo(5) == ERFBCType::ext_dir) );
42 
43  // Epsilon
45 
46 #ifdef _OPENMP
47 #pragma omp parallel if (Gpu::notInLaunchRegion())
48 #endif
49  for ( MFIter mfi(eddyViscosity,false); mfi.isValid(); ++mfi) {
50 
51  const Box &bx = mfi.growntilebox(1);
52  const Array4<Real const>& cell_data = cons_in.array(mfi);
53  const Array4<Real >& K_turb = eddyViscosity.array(mfi);
54  const Array4<Real const>& uvel = xvel.array(mfi);
55  const Array4<Real const>& vvel = yvel.array(mfi);
56 
57  // Compute some quantities that are constant in each column
58  // Sbox is shrunk to only include the interior of the domain in the vertical direction to compute integrals
59  // Box includes one ghost cell in each direction
60  const Box &dbx = geom.Domain();
61  Box sbx(bx.smallEnd(), bx.bigEnd());
62  sbx.grow(2,-1);
63  AMREX_ALWAYS_ASSERT(sbx.smallEnd(2) == dbx.smallEnd(2) && sbx.bigEnd(2) == dbx.bigEnd(2));
64 
65  const GeometryData gdata = geom.data();
66 
67  const Box xybx = PerpendicularBox<ZDir>(bx, IntVect{0,0,0});
68  FArrayBox qintegral(xybx,2);
69  qintegral.setVal<RunOn::Device>(0.0);
70  FArrayBox qturb(bx,1); FArrayBox qturb_old(bx,1);
71  const Array4<Real> qint = qintegral.array();
72  const Array4<Real> qvel = qturb.array();
73 
74  // vertical integrals to compute lengthscale
75  if (use_terrain_fitted_coords) {
76  const Array4<Real const> &z_nd_arr = z_phys_nd->array(mfi);
77  const auto invCellSize = geom.InvCellSizeArray();
78  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
79  {
80  // q^2 / 2 is the TKE
81  qvel(i,j,k) = std::sqrt(2.0 * cell_data(i,j,k,RhoKE_comp) / cell_data(i,j,k,Rho_comp));
82  AMREX_ASSERT_WITH_MESSAGE(qvel(i,j,k) > 0.0, "KE must have a positive value");
83 
84  Real fac = (sbx.contains(i,j,k)) ? 1.0 : 0.0;
85  const Real Zval = Compute_Zrel_AtCellCenter(i,j,k,z_nd_arr);
86  const Real dz = Compute_h_zeta_AtCellCenter(i,j,k,invCellSize,z_nd_arr);
87  Gpu::Atomic::Add(&qint(i,j,0,0), Zval*qvel(i,j,k)*dz*fac);
88  Gpu::Atomic::Add(&qint(i,j,0,1), qvel(i,j,k)*dz*fac);
89  });
90  } else {
91  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
92  {
93  // q^2 / 2 is the TKE
94  qvel(i,j,k) = std::sqrt(2.0 * cell_data(i,j,k,RhoKE_comp) / cell_data(i,j,k,Rho_comp));
95  AMREX_ASSERT_WITH_MESSAGE(qvel(i,j,k) > 0.0, "KE must have a positive value");
96 
97  // Not multiplying by dz: it's constant and would fall out when we divide qint0/qint1 anyway
98 
99  Real fac = (sbx.contains(i,j,k)) ? 1.0 : 0.0;
100  const Real Zval = gdata.ProbLo(2) + (k + 0.5)*gdata.CellSize(2);
101  Gpu::Atomic::Add(&qint(i,j,0,0), Zval*qvel(i,j,k)*fac);
102  Gpu::Atomic::Add(&qint(i,j,0,1), qvel(i,j,k)*fac);
103  });
104  }
105 
106  Real dz_inv = geom.InvCellSize(2);
107  const auto& dxInv = geom.InvCellSizeArray();
108  int izmin = geom.Domain().smallEnd(2);
109  int izmax = geom.Domain().bigEnd(2);
110 
111  // Spatially varying MOST
112  Real d_kappa = KAPPA;
113  Real d_gravity = CONST_GRAV;
114 
115  const auto& t_mean_mf = SurfLayer->get_mac_avg(level,4); // theta_v
116  const auto& q_mean_mf = SurfLayer->get_mac_avg(level,3); // q_v
117  const auto& u_star_mf = SurfLayer->get_u_star(level);
118  const auto& t_star_mf = SurfLayer->get_t_star(level);
119  const auto& q_star_mf = SurfLayer->get_q_star(level);
120 
121  const auto& tm_arr = t_mean_mf->const_array(mfi);
122  const auto& qm_arr = q_mean_mf->const_array(mfi);
123  const auto& u_star_arr = u_star_mf->const_array(mfi);
124  const auto& t_star_arr = t_star_mf->const_array(mfi);
125  const auto& q_star_arr = (use_moisture) ? q_star_mf->const_array(mfi) : Array4<Real>{};
126 
127  const Array4<Real const> z_nd_arr = z_phys_nd->const_array(mfi);
128 
129  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
130  {
131  // Compute some partial derivatives that we will need (second order)
132  // U and V derivatives are interpolated to account for staggered grid
133  const Real met_h_zeta = use_terrain_fitted_coords ? Compute_h_zeta_AtCellCenter(i,j,k,dxInv,z_nd_arr) : 1.0;
134 
135  Real dthetavdz, dudz, dvdz;
137  uvel, vvel, cell_data, izmin, izmax, dz_inv/met_h_zeta,
138  c_ext_dir_on_zlo, c_ext_dir_on_zhi,
139  u_ext_dir_on_zlo, u_ext_dir_on_zhi,
140  v_ext_dir_on_zlo, v_ext_dir_on_zhi,
141  dthetavdz, dudz, dvdz,
142  RhoQv_comp, RhoQc_comp, RhoQr_comp);
143 
144  // Spatially varying MOST
145  Real theta0 = tm_arr(i,j,0);
146  Real qv0 = qm_arr(i,j,0);
147  Real surface_heat_flux = -u_star_arr(i,j,0) * t_star_arr(i,j,0);
148  Real surface_latent_heat{0};
149  if (use_moisture) {
150  // Compute buoyancy flux (Stull Eqn. 4.4.5d)
151  surface_latent_heat = -u_star_arr(i,j,0) * q_star_arr(i,j,0);
152  surface_heat_flux *= (1.0 + 0.61*qv0);
153  surface_heat_flux += 0.61 * theta0 * surface_latent_heat;
154  }
155 
156  Real l_obukhov;
157  if (std::abs(surface_heat_flux) > eps) {
158  l_obukhov = -( theta0 * u_star_arr(i,j,0)*u_star_arr(i,j,0)*u_star_arr(i,j,0) )
159  / ( d_kappa * d_gravity * surface_heat_flux );
160  } else {
161  l_obukhov = std::numeric_limits<Real>::max();
162  }
163 
164  // Surface-layer length scale (NN09, Eqn. 53)
165  AMREX_ASSERT(l_obukhov != 0);
166  int lk = amrex::max(k,0);
167  const Real zval = use_terrain_fitted_coords ? Compute_Zrel_AtCellCenter(i,j,lk,z_nd_arr)
168  : gdata.ProbLo(2) + (lk + 0.5)*gdata.CellSize(2);
169  const Real zeta = zval/l_obukhov;
170  Real l_S;
171  if (zeta >= 1.0) {
172  l_S = KAPPA*zval/3.7;
173  } else if (zeta >= 0) {
174  l_S = KAPPA*zval/(1+2.7*zeta);
175  } else {
176  l_S = KAPPA*zval*std::pow(1.0 - 100.0 * zeta, 0.2);
177  }
178 
179  // ABL-depth length scale (NN09, Eqn. 54)
180  Real l_T;
181  if (qint(i,j,0,1) > 0.0) {
182  l_T = Lt_alpha*qint(i,j,0,0)/qint(i,j,0,1);
183  } else {
184  l_T = std::numeric_limits<Real>::max();
185  }
186 
187  // Buoyancy length scale (NN09, Eqn. 55)
188  Real l_B;
189  if (dthetavdz > 0) {
190  Real N_brunt_vaisala = std::sqrt(CONST_GRAV/theta0 * dthetavdz);
191  if (zeta < 0) {
192  Real qc = CONST_GRAV/theta0 * surface_heat_flux * l_T; // velocity scale
193  qc = std::pow(qc,1.0/3.0);
194  l_B = (1.0 + 5.0*std::sqrt(qc/(N_brunt_vaisala * l_T))) * qvel(i,j,k)/N_brunt_vaisala;
195  } else {
196  l_B = qvel(i,j,k) / N_brunt_vaisala;
197  }
198  } else {
199  l_B = std::numeric_limits<Real>::max();
200  }
201 
202  // Master length scale
203  Real Lm;
204  if (mynn.config == MYNNConfigType::CHEN2021) {
205  Lm = std::pow(1.0/(l_S*l_S) + 1.0/(l_T*l_T) + 1.0/(l_B*l_B), -0.5);
206  } else {
207  // NN09, Eqn 52
208  Lm = 1.0 / (1.0/l_S + 1.0/l_T + 1.0/l_B);
209  }
210 
211  // Calculate nondimensional production terms
212  Real shearProd = dudz*dudz + dvdz*dvdz;
213  Real buoyProd = -(CONST_GRAV/theta0) * dthetavdz;
214  Real L2_over_q2 = Lm*Lm/(qvel(i,j,k)*qvel(i,j,k));
215  Real GM = L2_over_q2 * shearProd;
216  Real GH = L2_over_q2 * buoyProd;
217 
218  // Equilibrium (Level-2) q calculation follows NN09, Appendix A
219  Real Rf = level2.calc_Rf(GM, GH);
220  Real SM2 = level2.calc_SM(Rf);
221  Real qe2 = mynn.B1 * Lm*Lm * SM2 * (1.0-Rf) * shearProd;
222  Real qe = (qe2 < 0.0) ? 0.0 : std::sqrt(qe2);
223 
224  // Level 2 limiting (Helfand and Labraga 1988)
225  Real alphac = (qvel(i,j,k) >= qe) ? 1.0 : qvel(i,j,k) / (qe + eps);
226 #ifdef EXTRA_MYNN25_CHECKS
227  Real Ri = -GH/(GM+level2.eps);
228  if (alphac < 1 && (Ri > 1 || Ri < -1)) {
229  Warning("Level 2 limiting being applied with Ri out of expected range");
230  //AllPrint() << "alphac"<<IntVect(i,j,k)<<"= " << alphac
231  // << " Ri,SM2,SH2= " << Ri << " " << SM2 << " " << level2.calc_SH(Rf)
232  // << std::endl;
233  }
234 #endif
235 
236  // Level 2.5 stability functions
237  Real SM, SH, SQ;
238  mynn.calc_stability_funcs(SM,SH,SQ,GM,GH,alphac);
239 
240  // Clip SM, SH following WRF
241  SM = amrex::min(amrex::max(SM, mynn.SMmin), mynn.SMmax);
242  SH = amrex::min(amrex::max(SH, mynn.SHmin), mynn.SHmax);
243 #ifdef EXTRA_MYNN25_CHECKS
244  if (SM == mynn.SMmin) {
245  Warning("SM clipped at min val");
246  } else if (SM == mynn.SMmax) {
247  Warning("SM clipped at max val");
248  }
249  if (SH == mynn.SHmin) {
250  Warning("SH clipped at min val");
251  } else if (SH == mynn.SHmax) {
252  Warning("SH clipped at max val");
253  }
254 #endif
255 
256  // Finally, compute the eddy viscosity/diffusivities
257  const Real rho = cell_data(i,j,k,Rho_comp);
258  K_turb(i,j,k,EddyDiff::Mom_v) = rho * Lm * qvel(i,j,k) * SM;
259  K_turb(i,j,k,EddyDiff::Theta_v) = rho * Lm * qvel(i,j,k) * SH;
260  K_turb(i,j,k,EddyDiff::KE_v) = rho * Lm * qvel(i,j,k) * SQ;
261 
262  // TODO: implement partial-condensation scheme?
263  // Currently, implementation matches NN09 without rain (i.e.,
264  // the liquid water potential temperature is equal to the
265  // potential temperature.
266 
267  // NN09 gives the total water content flux; this assumes that
268  // all the species have the same eddy diffusivity
269  if (mynn.diffuse_moistvars) {
270  K_turb(i,j,k,EddyDiff::Q_v) = rho * Lm * qvel(i,j,k) * SH;
271  }
272 
273  K_turb(i,j,k,EddyDiff::Turb_lengthscale) = Lm;
274  });
275  }
276 }
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 int RhoQv_comp, const int RhoQc_comp, const int RhoQr_comp)
Definition: ERF_PBLModels.H:149
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:358
MYNNLevel2 pbl_mynn_level2
Definition: ERF_TurbStruct.H:359

Referenced by ComputeTurbulentViscosity().

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