ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
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 std::unique_ptr< MultiFab > &z_phys_cc, const MoistureComponentIndices &moisture_indices)
 

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

Referenced by ComputeTurbulentViscosity().

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