ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_ComputeDiffusivityYSU.cpp File Reference
#include "ERF_ABLMost.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_ComputeDiffusivityYSU.cpp:

Functions

void ComputeDiffusivityYSU (const MultiFab &xvel, const MultiFab &yvel, const MultiFab &cons_in, MultiFab &eddyViscosity, const Geometry &geom, const TurbChoice &turbChoice, std::unique_ptr< ABLMost > &most, bool, int level, const BCRec *bc_ptr, bool, const std::unique_ptr< MultiFab > &z_phys_nd)
 

Function Documentation

◆ ComputeDiffusivityYSU()

void ComputeDiffusivityYSU ( const MultiFab &  xvel,
const MultiFab &  yvel,
const MultiFab &  cons_in,
MultiFab &  eddyViscosity,
const Geometry &  geom,
const TurbChoice turbChoice,
std::unique_ptr< ABLMost > &  most,
bool  ,
int  level,
const BCRec *  bc_ptr,
bool  ,
const std::unique_ptr< MultiFab > &  z_phys_nd 
)
23 {
24  const bool use_terrain = (z_phys_nd != nullptr);
25 
26  {
27  /*
28  YSU PBL initially introduced by S.-Y. Hong, Y. Noh, and J. Dudhia, MWR, 2006 [HND06]
29 
30  Further Modifications from S.-Y. Hong, Q. J. R. Meteorol. Soc., 2010 [H10]
31 
32  Implementation follows WRF as of early 2024 with some simplifications
33  */
34 
35 #ifdef _OPENMP
36 #pragma omp parallel if (Gpu::notInLaunchRegion())
37 #endif
38  for ( MFIter mfi(eddyViscosity,TilingIfNotGPU()); mfi.isValid(); ++mfi) {
39 
40  // Pull out the box we're working on, make sure it covers full domain in z-direction
41  const Box &bx = mfi.growntilebox(1);
42  const Box &dbx = geom.Domain();
43  Box sbx(bx.smallEnd(), bx.bigEnd());
44  sbx.grow(2,-1);
45  AMREX_ALWAYS_ASSERT(sbx.smallEnd(2) == dbx.smallEnd(2) && sbx.bigEnd(2) == dbx.bigEnd(2));
46 
47  // Get some data in arrays
48  const auto& cell_data = cons_in.const_array(mfi);
49  const auto& uvel = xvel.const_array(mfi);
50  const auto& vvel = yvel.const_array(mfi);
51 
52  const auto& z0_arr = most->get_z0(level)->const_array();
53  const auto& ws10av_arr = most->get_mac_avg(level,5)->const_array(mfi);
54  const auto& t10av_arr = most->get_mac_avg(level,2)->const_array(mfi);
55  const auto& t_surf_arr = most->get_t_surf(level)->const_array(mfi);
56  const auto& over_land_arr = (most->get_lmask(level)) ? most->get_lmask(level)->const_array(mfi) : Array4<int> {};
57  const Array4<Real const> z_nd_arr = use_terrain ? z_phys_nd->array(mfi) : Array4<Real>{};
58  const Real most_zref = most->get_zref();
59 
60  // Require that MOST zref is 10 m so we get the wind speed at 10 m from most
61  bool invalid_zref = false;
62  if (use_terrain) {
63  invalid_zref = most_zref != 10.0;
64  } else {
65  // zref gets reset to nearest cell center, so assert that zref is in the same cell as the 10m point
66  Real dz = geom.CellSize(2);
67  invalid_zref = int((most_zref - 0.5*dz)/dz) != int((10.0 - 0.5*dz)/dz);
68  }
69  if (invalid_zref) {
70  Print() << "most_zref = " << most_zref << std::endl;
71  Abort("MOST Zref must be 10m for YSU PBL scheme");
72  }
73 
74  // create flattened boxes to store PBL height
75  const GeometryData gdata = geom.data();
76  const Box xybx = PerpendicularBox<ZDir>(bx, IntVect{0,0,0});
77  FArrayBox pbl_height(xybx,1);
78  IArrayBox pbl_index(xybx,1);
79  const auto& pblh_arr = pbl_height.array();
80  const auto& pbli_arr = pbl_index.array();
81 
82  // -- Diagnose PBL height - starting out assuming non-moist --
83  // loop is only over i,j in order to find height at each x,y
84  const Real f0 = turbChoice.pbl_ysu_coriolis_freq;
85  const bool force_over_water = turbChoice.pbl_ysu_force_over_water;
86  const Real land_Ribcr = turbChoice.pbl_ysu_land_Ribcr;
87  const Real unst_Ribcr = turbChoice.pbl_ysu_unst_Ribcr;
88  ParallelFor(xybx, [=] AMREX_GPU_DEVICE (int i, int j, int) noexcept
89  {
90  // Reconstruct a surface bulk Richardson number from the surface layer model
91  // In WRF, this value is supplied to YSU by the MM5 surface layer model
92  const Real t_surf = t_surf_arr(i,j,0);
93  const Real t_layer = t10av_arr(i,j,0);
94  const Real ws_layer = ws10av_arr(i,j,0);
95  const Real Rib_layer = CONST_GRAV * most_zref / (ws_layer*ws_layer) * (t_layer - t_surf)/(t_layer);
96 
97  // For now, we only support stable boundary layers
98  if (Rib_layer < unst_Ribcr) {
99  Abort("For now, YSU PBL only supports stable conditions");
100  }
101 
102  // TODO: unstable BLs
103 
104  // PBL Height: Stable Conditions
105  Real Rib_cr;
106  bool over_land = (over_land_arr) ? over_land_arr(i,j,0) : 1;
107  if (over_land && !force_over_water) {
108  Rib_cr = land_Ribcr;
109  } else { // over water
110  // Velocity at z=10 m comes from MOST -> currently the average using whatever averaging MOST uses.
111  // TODO: Revisit this calculation with local ws10?
112  const Real z0 = z0_arr(i,j,0);
113  const Real Rossby = ws_layer/(f0*z0);
114  Rib_cr = min(0.16*std::pow(1.0e-7*Rossby,-0.18),0.3); // Note: upper bound in WRF code, but not H10 paper
115  }
116 
117  bool above_critical = false;
118  int kpbl = 0;
119  Real Rib_up = Rib_layer, Rib_dn;
120  const Real base_theta = cell_data(i,j,0,RhoTheta_comp) / cell_data(i,j,0,Rho_comp);
121  while (!above_critical and bx.contains(i,j,kpbl+1)) {
122  kpbl += 1;
123  const Real zval = use_terrain ? Compute_Zrel_AtCellCenter(i,j,kpbl,z_nd_arr) : gdata.ProbLo(2) + (kpbl + 0.5)*gdata.CellSize(2);
124  const Real ws2_level = 0.25*( (uvel(i,j,kpbl)+uvel(i+1,j ,kpbl))*(uvel(i,j,kpbl)+uvel(i+1,j ,kpbl))
125  + (vvel(i,j,kpbl)+vvel(i ,j+1,kpbl))*(vvel(i,j,kpbl)+vvel(i ,j+1,kpbl)) );
126  const Real theta = cell_data(i,j,kpbl,RhoTheta_comp) / cell_data(i,j,kpbl,Rho_comp);
127  Rib_dn = Rib_up;
128  Rib_up = (theta-base_theta)/base_theta * CONST_GRAV * zval / ws2_level;
129  above_critical = Rib_up >= Rib_cr;
130  }
131 
132  Real interp_fact;
133  if (Rib_dn >= Rib_cr) {
134  interp_fact = 0.0;
135  } else if (Rib_up <= Rib_cr)
136  interp_fact = 1.0;
137  else {
138  interp_fact = (Rib_cr - Rib_dn) / (Rib_up - Rib_dn);
139  }
140 
141  const Real zval_up = use_terrain ? Compute_Zrel_AtCellCenter(i,j,kpbl,z_nd_arr) : gdata.ProbLo(2) + (kpbl + 0.5)*gdata.CellSize(2);
142  const Real zval_dn = use_terrain ? Compute_Zrel_AtCellCenter(i,j,kpbl-1,z_nd_arr) : gdata.ProbLo(2) + (kpbl-1 + 0.5)*gdata.CellSize(2);
143  pblh_arr(i,j,0) = zval_dn + interp_fact*(zval_up-zval_dn);
144 
145  const Real zval_0 = use_terrain ? Compute_Zrel_AtCellCenter(i,j,0,z_nd_arr) : gdata.ProbLo(2) + (0.5)*gdata.CellSize(2);
146  const Real zval_1 = use_terrain ? Compute_Zrel_AtCellCenter(i,j,1,z_nd_arr) : gdata.ProbLo(2) + (1.5)*gdata.CellSize(2);
147  if (pblh_arr(i,j,0) < 0.5*(zval_0+zval_1) ) {
148  kpbl = 0;
149  }
150  pbli_arr(i,j,0) = kpbl;
151  });
152 
153  // -- Compute nonlocal/countergradient mixing parameters --
154  // Not included for stable so nothing to do until unstable treatment is added
155 
156  // -- Compute entrainment parameters --
157  // 0 for stable so nothing to do?
158 
159  // -- Compute diffusion coefficients --
160 
161  const auto& u_star_arr = most->get_u_star(level)->const_array(mfi);
162  const auto& l_obuk_arr = most->get_olen(level)->const_array(mfi);
163  const Array4<Real > &K_turb = eddyViscosity.array(mfi);
164 
165  // Dirichlet flags to switch derivative stencil
166  bool c_ext_dir_on_zlo = ( (bc_ptr[BCVars::cons_bc].lo(2) == ERFBCType::ext_dir) );
167  bool c_ext_dir_on_zhi = ( (bc_ptr[BCVars::cons_bc].lo(5) == ERFBCType::ext_dir) );
168  bool u_ext_dir_on_zlo = ( (bc_ptr[BCVars::xvel_bc].lo(2) == ERFBCType::ext_dir) );
169  bool u_ext_dir_on_zhi = ( (bc_ptr[BCVars::xvel_bc].lo(5) == ERFBCType::ext_dir) );
170  bool v_ext_dir_on_zlo = ( (bc_ptr[BCVars::yvel_bc].lo(2) == ERFBCType::ext_dir) );
171  bool v_ext_dir_on_zhi = ( (bc_ptr[BCVars::yvel_bc].lo(5) == ERFBCType::ext_dir) );
172 
173  const auto& dxInv = geom.InvCellSizeArray();
174  const Real dz_inv = geom.InvCellSize(2);
175  const int izmin = geom.Domain().smallEnd(2);
176  const int izmax = geom.Domain().bigEnd(2);
177 
178  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
179  {
180  const Real zval = use_terrain ? Compute_Zrel_AtCellCenter(i,j,k,z_nd_arr) : gdata.ProbLo(2) + (k + 0.5)*gdata.CellSize(2);
181  const Real rho = cell_data(i,j,k,Rho_comp);
182  const Real met_h_zeta = use_terrain ? Compute_h_zeta_AtCellCenter(i,j,k,dxInv,z_nd_arr) : 1.0;
183  const Real dz_terrain = met_h_zeta/dz_inv;
184  if (k < pbli_arr(i,j,0)) {
185  // -- Compute diffusion coefficients within PBL
186  constexpr Real zfacmin = 1e-8; // value from WRF
187  constexpr Real phifac = 8.0; // value from H10 and WRF
188  constexpr Real wstar3 = 0.0; // only nonzero for unstable
189  constexpr Real pfac = 2.0; // profile exponent
190  const Real zfac = std::min(std::max(1 - zval / pblh_arr(i,j,0), zfacmin ), 1.0);
191  // Not including YSU top down PBL term (not in H10, added to WRF later)
192  const Real ust3 = u_star_arr(i,j,0) * u_star_arr(i,j,0) * u_star_arr(i,j,0);
193  Real wscalek = ust3 + phifac * KAPPA * wstar3 * (1.0 - zfac);
194  wscalek = std::pow(wscalek, 1.0/3.0);
195  // stable only
196  const Real phi_term = 1 + 5 * zval / l_obuk_arr(i,j,0); // phi_term appears in WRF but not papers
197  wscalek = std::max(u_star_arr(i,j,0) / phi_term, 0.001); // 0.001 limit appears in WRF but not papers
198  K_turb(i,j,k,EddyDiff::Mom_v) = rho * wscalek * KAPPA * zval * std::pow(zfac, pfac);
199  K_turb(i,j,k,EddyDiff::Theta_v) = K_turb(i,j,k,EddyDiff::Mom_v);
200  } else {
201  // -- Compute coefficients in free stream above PBL
202  constexpr Real lam0 = 30.0;
203  constexpr Real min_richardson = -100.0;
204  constexpr Real prandtl_max = 4.0;
205  Real dthetadz, dudz, dvdz;
206  const int RhoQv_comp = -1;
207  const int RhoQc_comp = -1;
208  const int RhoQr_comp = -1;
210  uvel, vvel, cell_data, izmin, izmax, 1.0/dz_terrain,
211  c_ext_dir_on_zlo, c_ext_dir_on_zhi,
212  u_ext_dir_on_zlo, u_ext_dir_on_zhi,
213  v_ext_dir_on_zlo, v_ext_dir_on_zhi,
214  dthetadz, dudz, dvdz, RhoQv_comp, RhoQc_comp, RhoQr_comp);
215  const Real shear_squared = dudz*dudz + dvdz*dvdz + 1.0e-9; // 1.0e-9 from WRF to avoid divide by zero
216  const Real theta = cell_data(i,j,k,RhoTheta_comp) / cell_data(i,j,k,Rho_comp);
217  Real richardson = CONST_GRAV / theta * dthetadz / shear_squared;
218  const Real lambdadz = std::min(std::max(0.1*dz_terrain , lam0), 300.0); // in WRF, H10 paper just says use lam0
219  const Real lengthscale = lambdadz * KAPPA * zval / (lambdadz + KAPPA * zval);
220  const Real turbfact = lengthscale * lengthscale * std::sqrt(shear_squared);
221 
222  if (richardson < 0) {
223  richardson = max(richardson, min_richardson);
224  Real sqrt_richardson = std::sqrt(-richardson);
225  K_turb(i,j,k,EddyDiff::Mom_v) = rho * turbfact * (1.0 - 8.0 * richardson / (1.0 + 1.746 * sqrt_richardson));
226  K_turb(i,j,k,EddyDiff::Theta_v) = rho * turbfact * (1.0 - 8.0 * richardson / (1.0 + 1.286 * sqrt_richardson));
227  } else {
228  const Real oneplus5ri = 1.0 + 5.0 * richardson;
229  K_turb(i,j,k,EddyDiff::Theta_v) = rho * turbfact / (oneplus5ri * oneplus5ri);
230  const Real prandtl = std::min(1.0+2.1*richardson, prandtl_max); // limit from WRF
231  K_turb(i,j,k,EddyDiff::Mom_v) = K_turb(i,j,k,EddyDiff::Theta_v) * prandtl;
232  }
233  }
234 
235  // limit both diffusion coefficients - from WRF, not documented in papers
236  constexpr Real ckz = 0.001;
237  constexpr Real Kmax = 1000.0;
238  const Real rhoKmin = ckz * dz_terrain * rho;
239  const Real rhoKmax = rho * Kmax;
240  K_turb(i,j,k,EddyDiff::Mom_v) = std::max(std::min(K_turb(i,j,k,EddyDiff::Mom_v) ,rhoKmax), rhoKmin);
241  K_turb(i,j,k,EddyDiff::Theta_v) = std::max(std::min(K_turb(i,j,k,EddyDiff::Theta_v) ,rhoKmax), rhoKmin);
242  K_turb(i,j,k,EddyDiff::Turb_lengthscale) = pblh_arr(i,j,0);
243  });
244 
245  // HACK set bottom ghost cell to 1st cell
246  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
247  {
248  if (k==-1) {
249  K_turb(i,j,k,EddyDiff::Mom_v) = K_turb(i,j,0,EddyDiff::Mom_v);
250  K_turb(i,j,k,EddyDiff::Theta_v) = K_turb(i,j,0,EddyDiff::Theta_v);
251  }
252  });
253  }
254  }
255 }
constexpr amrex::Real KAPPA
Definition: ERF_Constants.H:20
constexpr amrex::Real CONST_GRAV
Definition: ERF_Constants.H:21
#define Rho_comp
Definition: ERF_IndexDefines.H:36
#define RhoTheta_comp
Definition: ERF_IndexDefines.H:37
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, const bool use_most=true)
Definition: ERF_PBLModels.H:82
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:39
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:352
@ yvel_bc
Definition: ERF_IndexDefines.H:89
@ cons_bc
Definition: ERF_IndexDefines.H:76
@ xvel_bc
Definition: ERF_IndexDefines.H:88
@ ext_dir
Definition: ERF_IndexDefines.H:180
@ Theta_v
Definition: ERF_IndexDefines.H:157
@ Turb_lengthscale
Definition: ERF_IndexDefines.H:161
@ Mom_v
Definition: ERF_IndexDefines.H:156
@ theta
Definition: ERF_MM5.H:20
@ rho
Definition: ERF_Kessler.H:30
@ xvel
Definition: ERF_IndexDefines.H:130
@ yvel
Definition: ERF_IndexDefines.H:131
amrex::Real pbl_ysu_land_Ribcr
Definition: ERF_TurbStruct.H:211
amrex::Real pbl_ysu_coriolis_freq
Definition: ERF_TurbStruct.H:208
bool pbl_ysu_force_over_water
Definition: ERF_TurbStruct.H:210
amrex::Real pbl_ysu_unst_Ribcr
Definition: ERF_TurbStruct.H:212

Referenced by ComputeTurbulentViscosity().

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