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_sfc_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_sfc_nd,
const std::unique_ptr< MultiFab > &  z_phys_cc,
const MoistureComponentIndices &  moisture_indices 
)

Compute eddy diffusivities using the MYNN PBL model.

Parameters
[in]xvelX-velocity MultiFab.
[in]yvelY-velocity MultiFab.
[in]cons_inConserved variables MultiFab.
[out]eddyViscosityMultiFab for storing computed eddy diffusivities.
[in]geomGrid geometry.
[in]turbChoiceTurbulence model options and configuration.
[in]SurfLayerSurface layer data.
[in]use_terrain_fitted_coordsFlag to use terrain-fitted coordinates.
[in]use_moistureFlag to include moisture.
[in]levelLevel index.
[in]bc_ptrBoundary condition record pointers.
[in]z_phys_ndNodal physical height.
[in]z_sfc_ndNodal heights of the bottom of the domain
[in]z_phys_ccCell-centered physical height.
[in]moisture_indicesMoisture component indices.
48 {
49  const StratType pbl_strat = turbChoice.pbl_strat_type;
50  auto mynn = turbChoice.pbl_mynn;
51  auto level2 = turbChoice.pbl_mynn_level2;
52 
53  Real Lt_alpha = mynn.Lt_alpha;
54  const bool use_surface_lm = mynn.surface_lm;
55 
56  // Dirichlet flags to switch derivative stencil
57  bool c_ext_dir_on_zlo = ( (bc_ptr[BCVars::cons_bc].lo(2) == ERFBCType::ext_dir) );
58  bool c_ext_dir_on_zhi = ( (bc_ptr[BCVars::cons_bc].hi(2) == ERFBCType::ext_dir) );
59  bool u_ext_dir_on_zlo = ( (bc_ptr[BCVars::xvel_bc].lo(2) == ERFBCType::ext_dir) );
60  bool u_ext_dir_on_zhi = ( (bc_ptr[BCVars::xvel_bc].hi(2) == ERFBCType::ext_dir) );
61  bool v_ext_dir_on_zlo = ( (bc_ptr[BCVars::yvel_bc].lo(2) == ERFBCType::ext_dir) );
62  bool v_ext_dir_on_zhi = ( (bc_ptr[BCVars::yvel_bc].hi(2) == ERFBCType::ext_dir) );
63 
64  // Epsilon
66 
67 #ifdef _OPENMP
68 #pragma omp parallel if (Gpu::notInLaunchRegion())
69 #endif
70  for ( MFIter mfi(eddyViscosity,TileNoZ()); mfi.isValid(); ++mfi) {
71 
72  const Box& bx = mfi.tilebox();
73  const Array4<Real const>& cell_data = cons_in.array(mfi);
74  const Array4<Real >& K_turb = eddyViscosity.array(mfi);
75  const Array4<Real const>& uvel = xvel.array(mfi);
76  const Array4<Real const>& vvel = yvel.array(mfi);
77 
78  // Compute some quantities that are constant in each column
79  // Sbox is shrunk to only include the interior of the domain in the vertical direction to compute integrals
80  // Box includes one ghost cell in each direction
81  const Box& dbx = geom.Domain();
82  AMREX_ALWAYS_ASSERT(bx.smallEnd(2) == dbx.smallEnd(2) && bx.bigEnd(2) == dbx.bigEnd(2));
83 
84  const GeometryData gdata = geom.data();
85 
86  const Box xybx = makeSlab(bx,2,0);
87  FArrayBox qturb(bx,1,The_Async_Arena());
88  FArrayBox qintegral(xybx,2,The_Async_Arena());
89  qintegral.setVal<RunOn::Device>(zero);
90  const Array4<Real> qint = qintegral.array();
91  const Array4<Real> qvel = qturb.array();
92 
93  // vertical integrals to compute lengthscale
94  if (use_terrain_fitted_coords) {
95  const Array4<Real const> &z_nd_arr = z_phys_nd->array(mfi);
96  const Array4<Real const> &z_sfc_arr = z_sfc_nd->const_array(mfi);
97  const auto invCellSize = geom.InvCellSizeArray();
98  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
99  {
100  // q^2 / 2 is the TKE
101  qvel(i,j,k) = std::sqrt(two * cell_data(i,j,k,RhoKE_comp) / cell_data(i,j,k,Rho_comp));
102  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(qvel(i,j,k) > zero, "KE must have a positive value");
103 
104  const Real Zval = Compute_Zrel_AtCellCenter(i,j,k,z_sfc_arr,z_nd_arr);
105  const Real dz = Compute_h_zeta_AtCellCenter(i,j,k,invCellSize,z_nd_arr);
106  Gpu::Atomic::Add(&qint(i,j,0,0), Zval*qvel(i,j,k)*dz);
107  Gpu::Atomic::Add(&qint(i,j,0,1), qvel(i,j,k)*dz);
108  });
109  } else {
110  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
111  {
112  // q^2 / 2 is the TKE
113  qvel(i,j,k) = std::sqrt(two * cell_data(i,j,k,RhoKE_comp) / cell_data(i,j,k,Rho_comp));
114  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(qvel(i,j,k) > zero, "KE must have a positive value");
115 
116  // Not multiplying by dz: it's constant and would fall out when we divide qint0/qint1 anyway
117 
118  const Real Zval = (k + myhalf)*gdata.CellSize(2);
119  Gpu::Atomic::Add(&qint(i,j,0,0), Zval*qvel(i,j,k));
120  Gpu::Atomic::Add(&qint(i,j,0,1), qvel(i,j,k));
121  });
122  }
123 
124  int izmin = geom.Domain().smallEnd(2);
125  int izmax = geom.Domain().bigEnd(2);
126 
127  // Spatially varying MOST
128  Real d_kappa = KAPPA;
129  Real d_gravity = CONST_GRAV;
130 
131  const auto& t_mean_mf = SurfLayer->get_mac_avg(level,5); // theta_v
132  const auto& q_mean_mf = SurfLayer->get_mac_avg(level,4); // q_v
133  const auto& u_star_mf = SurfLayer->get_u_star(level);
134  const similarity_funs l_sfuns = SurfLayer->similarity_functions();
135  const auto& t_star_mf = SurfLayer->get_t_star(level);
136  const auto& q_star_mf = SurfLayer->get_q_star(level);
137 
138  const auto& tm_arr = t_mean_mf->const_array(mfi);
139  const auto& qm_arr = q_mean_mf->const_array(mfi);
140  const auto& u_star_arr = u_star_mf->const_array(mfi);
141  const auto& t_star_arr = t_star_mf->const_array(mfi);
142  const auto& q_star_arr = (use_moisture) ? q_star_mf->const_array(mfi) : Array4<Real>{};
143 
144  const Array4<Real const> z_nd_arr = z_phys_nd->const_array(mfi);
145  const Array4<Real const> z_sfc_arr = z_sfc_nd->const_array(mfi);
146  const PBLDerivativeDzInv_T pbl_derivative_dz_inv{z_phys_cc->const_array(mfi)};
147 
148  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
149  {
150  // Compute some partial derivatives that we will need (second order)
151  // U and V derivatives are interpolated to account for staggered grid
152  Real dthetavdz, dudz, dvdz;
154  uvel, vvel, cell_data, izmin, izmax, pbl_derivative_dz_inv(i,j,k),
155  c_ext_dir_on_zlo, c_ext_dir_on_zhi,
156  u_ext_dir_on_zlo, u_ext_dir_on_zhi,
157  v_ext_dir_on_zlo, v_ext_dir_on_zhi,
158  dthetavdz, dudz, dvdz,
159  moisture_indices, pbl_strat);
160 
161  // Spatially varying MOST
162  Real theta0 = tm_arr(i,j,0);
163  Real qv0 = qm_arr(i,j,0);
164  Real surface_heat_flux = -u_star_arr(i,j,0) * t_star_arr(i,j,0);
165  Real surface_latent_heat{0};
166  if (use_moisture) {
167  // Compute buoyancy flux (Stull Eqn. 4.4.5d)
168  surface_latent_heat = -u_star_arr(i,j,0) * q_star_arr(i,j,0);
169  surface_heat_flux *= (one + epsv*qv0);
170  surface_heat_flux += epsv * theta0 * surface_latent_heat;
171  }
172 
173  Real l_obukhov;
174  if (std::abs(surface_heat_flux) > eps && u_star_arr(i,j,0) > eps) {
175  l_obukhov = -( theta0 * u_star_arr(i,j,0)*u_star_arr(i,j,0)*u_star_arr(i,j,0) )
176  / ( d_kappa * d_gravity * surface_heat_flux );
177  } else {
178  l_obukhov = std::numeric_limits<Real>::max();
179  }
180 
181  // Surface-layer length scale (NN09, Eqn. 53)
182  AMREX_ASSERT(l_obukhov != 0);
183  const Real zval = use_terrain_fitted_coords ? Compute_Zrel_AtCellCenter(i,j,k,z_sfc_arr,z_nd_arr) :
184  (k + myhalf)*gdata.CellSize(2);
185  const Real zeta = zval/l_obukhov;
186  Real l_S;
187  if (zeta >= one) {
188  l_S = KAPPA*zval/Real(3.7);
189  } else if (zeta >= 0) {
190  l_S = KAPPA*zval/(one + Real(2.7) * zeta);
191  } else {
192  l_S = KAPPA*zval*std::pow(one - Real(100.0) * zeta, Real(0.2));
193  }
194 
195  // At the first cell center the vertical gradients computed above are
196  // built from the foextrap (cons) and hoextrap (velocity) ghost values
197  // that zlo.type = surface_layer installs, and those carry no surface
198  // information: the potential-temperature gradient comes out half the
199  // one-sided value and the velocity gradient has no dependence on the
200  // friction velocity at all (ERF #4037). Replace them with the MOST
201  // profile gradients that the surface layer is imposing on this cell,
202  // |dU/dz| = u_* phi_m(zeta) / (kappa z)
203  // dthetav/dz = thetav_* phi_h(zeta) / (kappa z)
204  // which is the same profile the stress and heat flux were derived from.
205  if (k == izmin) {
207  sl.u_star = u_star_arr(i,j,0);
208  sl.tstar_v = ComputeVirtualTStarPBL(t_star_arr(i,j,0),
209  (use_moisture) ? q_star_arr(i,j,0) : zero,
210  theta0, qv0, use_moisture);
211  sl.zval = zval;
212  sl.zeta = zeta;
213  sl.sfuns = l_sfuns;
214  ApplySurfaceLayerGradientsPBL(sl, dthetavdz, dudz, dvdz);
215  }
216 
217  // ABL-depth length scale (NN09, Eqn. 54)
218  Real l_T;
219  if (qint(i,j,0,1) > zero) {
220  l_T = Lt_alpha*qint(i,j,0,0)/qint(i,j,0,1);
221  } else {
222  l_T = std::numeric_limits<Real>::max();
223  }
224 
225  // Buoyancy length scale (NN09, Eqn. 55)
226  Real l_B;
227  if (dthetavdz > zero) {
228  Real N_brunt_vaisala = std::sqrt(CONST_GRAV/theta0 * dthetavdz);
229  if (zeta < zero) {
230  Real qc = CONST_GRAV/theta0 * surface_heat_flux * l_T; // velocity scale
231  qc = std::pow(qc,one/three);
232  l_B = (one + Real(5.0)*std::sqrt(qc/(N_brunt_vaisala * l_T))) * qvel(i,j,k)/N_brunt_vaisala;
233  } else {
234  l_B = qvel(i,j,k) / N_brunt_vaisala;
235  }
236  } else {
237  l_B = std::numeric_limits<Real>::max();
238  }
239 
240  // Master length scale
241  Real Lm;
242  if (mynn.config == MYNNConfigType::Chen2021) {
243  Lm = std::pow(one/(l_S*l_S) + one/(l_T*l_T) + one/(l_B*l_B), -myhalf);
244  } else {
245  // NN09, Eqn 52
246  Lm = one / (one/l_S + one/l_T + one/l_B);
247  }
248  // erf.pbl_mynn_surface_lm: in the first cell the production comes from the
249  // MOST profile (above), u_*^3 phi_m/(kappa z); give it the surface-layer length
250  // scale alone so the local balance is the surface-layer one.
251  if (use_surface_lm && k == izmin) {
252  Lm = l_S;
253  }
254 
255  // Calculate nondimensional production terms
256  Real shearProd = dudz*dudz + dvdz*dvdz;
257  Real buoyProd = -(CONST_GRAV/theta0) * dthetavdz;
258  Real L2_over_q2 = Lm*Lm/(qvel(i,j,k)*qvel(i,j,k));
259  Real GM = L2_over_q2 * shearProd;
260  Real GH = L2_over_q2 * buoyProd;
261 
262  // Equilibrium (Level-2) q calculation follows NN09, Appendix A
263  Real Rf = level2.calc_Rf(GM, GH);
264  Real SM2 = level2.calc_SM(Rf);
265  Real qe2 = mynn.B1 * Lm*Lm * SM2 * (one-Rf) * shearProd;
266  Real qe = (qe2 < zero) ? zero : amrex::max(std::sqrt(qe2),eps);
267 
268  // Level 2 limiting introduced by Helfand and Labraga 1988 (NN09, Eqn. 42)
269  Real alphac = (qvel(i,j,k) >= qe) ? one : qvel(i,j,k) / qe;
270 //#if EXTRA_MYNN25_CHECKS
271 #if 0
272  // VERY verbose diagnostic
273  Real lGM = std::copysign(std::max(std::fabs(GM),level2.eps),GM);
274  Real Ri = -GH/lGM;
275  if (alphac < one) {
276  AllPrint() << "Level 2 limiter at " << IntVect(i,j,k) << " :"
277  << " ustar= " << u_star_arr(i,j,0)
278  << " alphac= " << alphac
279  << " Ri,SM2,SH2= " << Ri << " " << SM2 << " " << level2.calc_SH(Rf)
280  << std::endl;
281  }
282 #endif
283 
284  // Level Real(2.5) stability functions
285  Real SM, SH, SQ;
286  mynn.calc_stability_funcs(SM,SH,SQ,GM,GH,alphac);
287 
288  // Clip SM, SH following WRF
289  SM = amrex::min(amrex::max(SM, mynn.SMmin), mynn.SMmax);
290  SH = amrex::min(amrex::max(SH, mynn.SHmin), mynn.SHmax);
291  SQ = amrex::min(amrex::max(SQ, mynn.SQmin), mynn.SQmax);
292 #if EXTRA_MYNN25_CHECKS
293  if (SM == mynn.SMmin) {
294  Warning("SM clipped at min val");
295  } else if (SM == mynn.SMmax) {
296  Warning("SM clipped at max val");
297  }
298  if (SH == mynn.SHmin) {
299  Warning("SH clipped at min val");
300  } else if (SH == mynn.SHmax) {
301  Warning("SH clipped at max val");
302  }
303 #endif
304 
305  // Finally, compute the eddy viscosity/diffusivities
306  const Real rho = cell_data(i,j,k,Rho_comp);
307  K_turb(i,j,k,EddyDiff::Mom_v) = rho * Lm * qvel(i,j,k) * SM;
308  K_turb(i,j,k,EddyDiff::Theta_v) = rho * Lm * qvel(i,j,k) * SH;
309  K_turb(i,j,k,EddyDiff::KE_v) = rho * Lm * qvel(i,j,k) * SQ;
310 
311  // TODO: implement partial-condensation scheme?
312  // Currently, implementation matches NN09 without rain (i.e.,
313  // the liquid water potential temperature is equal to the
314  // potential temperature.
315 
316  // NN09 gives the total water content flux; this assumes that
317  // all the species have the same eddy diffusivity. eddyDiffs is
318  // zeroed once at allocation and nothing else writes Q_v, so this
319  // must be unconditional: skipping it leaves the moisture variables
320  // with a surface flux and no vertical turbulent transport.
321  K_turb(i,j,k,EddyDiff::Q_v) = rho * Lm * qvel(i,j,k) * SH;
322 
323  K_turb(i,j,k,EddyDiff::Turb_lengthscale) = Lm;
324  });
325  }
326 }
constexpr amrex::Real epsv
Definition: ERF_Constants.H:56
constexpr amrex::Real KAPPA
Definition: ERF_Constants.H:71
constexpr amrex::Real CONST_GRAV
Definition: ERF_Constants.H:72
#define Rho_comp
Definition: ERF_IndexDefines.H:61
#define RhoKE_comp
Definition: ERF_IndexDefines.H:63
const bool use_moisture
Definition: ERF_InitCustomPert_ABL.H:127
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(axis >=0 &&axis< AMREX_SPACEDIM, "prob.axis must be 0, 1 or 2")
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
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);})
constexpr amrex::Real three
Definition: ERF_NumericalConstants.H:39
constexpr amrex::Real two
Definition: ERF_NumericalConstants.H:38
constexpr amrex::Real one
Definition: ERF_NumericalConstants.H:37
constexpr amrex::Real zero
Definition: ERF_NumericalConstants.H:36
constexpr amrex::Real myhalf
Definition: ERF_NumericalConstants.H:41
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, const StratType strat=StratType::thetav)
Definition: ERF_PBLModels.H:291
AMREX_GPU_DEVICE AMREX_FORCE_INLINE void ApplySurfaceLayerGradientsPBL(const PBLSurfaceLayerGradient &sl, amrex::Real &dthetadz, amrex::Real &dudz, amrex::Real &dvdz)
Definition: ERF_PBLModels.H:411
AMREX_GPU_DEVICE AMREX_FORCE_INLINE amrex::Real ComputeVirtualTStarPBL(amrex::Real t_star, amrex::Real q_star, amrex::Real theta, amrex::Real qv, bool use_moisture)
Definition: ERF_PBLModels.H:374
AMREX_GPU_HOST_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_sfc_nd, const amrex::Array4< const amrex::Real > &z_nd)
Definition: ERF_TerrainMetrics.H:764
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE 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:202
AMREX_FORCE_INLINE amrex::IntVect TileNoZ()
Definition: ERF_TileNoZ.H:11
@ yvel_bc
Definition: ERF_IndexDefines.H:139
@ cons_bc
Definition: ERF_IndexDefines.H:122
@ xvel_bc
Definition: ERF_IndexDefines.H:138
@ ext_dir
Definition: ERF_IndexDefines.H:317
@ Theta_v
Definition: ERF_IndexDefines.H:267
@ Turb_lengthscale
Definition: ERF_IndexDefines.H:271
@ Q_v
Definition: ERF_IndexDefines.H:270
@ Mom_v
Definition: ERF_IndexDefines.H:266
@ KE_v
Definition: ERF_IndexDefines.H:268
@ rho
Definition: ERF_Kessler.H:25
@ qc
Definition: ERF_Morrison.H:110
@ xvel
Definition: ERF_IndexDefines.H:232
@ yvel
Definition: ERF_IndexDefines.H:233
@ dz
Definition: ERF_AdvanceWDM6.cpp:273
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Real eps()
Definition: ERF_ShocMoments.cpp:19
real(c_double), parameter epsilon
Definition: ERF_module_model_constants.F90:12
Functor for inverse vertical spacings for terrain-following grids using cell-center heights.
Definition: ERF_PBLModels.H:652
Definition: ERF_PBLModels.H:353
amrex::Real tstar_v
virtual temperature scale
Definition: ERF_PBLModels.H:355
amrex::Real zeta
stability coordinate z/L
Definition: ERF_PBLModels.H:357
similarity_funs sfuns
the surface layer's similarity functions (most.stable_functions)
Definition: ERF_PBLModels.H:358
amrex::Real u_star
friction velocity
Definition: ERF_PBLModels.H:354
amrex::Real zval
height of the cell center above the surface
Definition: ERF_PBLModels.H:356
MYNNLevel2 pbl_mynn_level2
MYNN level-2 closure coefficients for limiting.
Definition: ERF_TurbStruct.H:1058
StratType pbl_strat_type
Definition: ERF_TurbStruct.H:951
MYNNLevel25 pbl_mynn
MYNN level-2.5 closure coefficients.
Definition: ERF_TurbStruct.H:1057
Definition: ERF_MOSTUtils.H:84

Referenced by ComputeTurbulentViscosity().

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