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 
)

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

Referenced by ComputeTurbulentViscosity().

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