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