ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ERF_ComputeDiffusivityMRF.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 "ERF_TileNoZ.H"
Include dependency graph for ERF_ComputeDiffusivityMRF.cpp:

Functions

void ComputeDiffusivityMRF (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, int level, const BCRec *bc_ptr, bool, const std::unique_ptr< MultiFab > &z_phys_nd)
 

Function Documentation

◆ ComputeDiffusivityMRF()

void ComputeDiffusivityMRF ( 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  ,
int  level,
const BCRec *  bc_ptr,
bool  ,
const std::unique_ptr< MultiFab > &  z_phys_nd 
)
25 {
26  /*
27  Implementation of the older MRF Scheme based on Hong and Pan (1996)
28  " Nonlocal Boundary Layer Vertical Diffusion in a Medium-Range Forecast
29  Model"
30  */
31 
32  // Domain extent in z-dir
33  int klo = geom.Domain().smallEnd(2);
34  int khi = geom.Domain().bigEnd(2);
35 
36 #ifdef _OPENMP
37 #pragma omp parallel if (Gpu::notInLaunchRegion())
38 #endif
39  for (MFIter mfi(eddyViscosity, TileNoZ()); mfi.isValid(); ++mfi) {
40 
41  // Box operated on must span fill domain in z-dir
42  const Box& gbx = mfi.growntilebox(IntVect(1,1,0));
43  AMREX_ALWAYS_ASSERT( gbx.smallEnd(2) == klo &&
44  gbx.bigEnd(2) == khi );
45 
46  // Step 1: Compute the height of the PBL without thermal excess
47  // h = Rib_cf * theta_va * | U(h) |^2 / (g * (theta_va - theta_surf))
48  // create flattened boxes to store PBL height
49  const GeometryData gdata = geom.data();
50  const Box xybx = PerpendicularBox<ZDir>(gbx, IntVect{0, 0, 0});
51  FArrayBox pbl_height_predictor(xybx, 1, The_Async_Arena());
52  FArrayBox pbl_height_corrector(xybx, 1, The_Async_Arena());
53  IArrayBox pbl_index(xybx, 1, The_Async_Arena());
54  const auto& pblh_arr = pbl_height_predictor.array();
55  const auto& pblh_corr_arr = pbl_height_corrector.array();
56  const auto& pbli_arr = pbl_index.array();
57 
58  // Get some data in arrays
59  const auto& cell_data = cons_in.const_array(mfi);
60  const auto& uvel = xvel.const_array(mfi);
61  const auto& vvel = yvel.const_array(mfi);
62 
63  const Real Ribcr = turbChoice.pbl_mrf_Ribcr;
64  //const Real f0 = turbChoice.pbl_mrf_coriolis_freq;
65  const auto& u_star_arr = SurfLayer->get_u_star(level)->const_array(mfi);
66  const auto& t_star_arr = SurfLayer->get_t_star(level)->const_array(mfi);
67  const auto& l_obuk_arr = SurfLayer->get_olen(level)->const_array(mfi);
68  const auto& t10av_arr = SurfLayer->get_mac_avg(level, 2)->const_array(mfi);
69  const auto& t_surf_arr = SurfLayer->get_t_surf(level)->const_array(mfi);
70  const Array4<Real const> z_nd_arr = z_phys_nd->array(mfi);
71 
72  ParallelFor(xybx, [=] AMREX_GPU_DEVICE(int i, int j, int) noexcept
73  {
74  const Real t_surf = t_surf_arr(i, j, 0);
75  const Real t_layer = t10av_arr(i, j, 0);
76 
77  int kpbl = klo;
78  Real zval = 10;
79  bool above_critical = false;
80  while (!above_critical && ((kpbl + 1) <= khi)) {
81  zval = (use_terrain_fitted_coords)
82  ? Compute_Zrel_AtCellCenter(i, j, kpbl, z_nd_arr)
83  : gdata.ProbLo(2) + (kpbl + 0.5) * gdata.CellSize(2);
84  kpbl += 1;
85 
86  const Real theta = cell_data(i, j, kpbl, RhoTheta_comp) /
87  cell_data(i, j, kpbl, Rho_comp);
88  const Real ws2 = 0.25 * ( (uvel(i, j, kpbl) + uvel(i + 1, j, kpbl)) *
89  (uvel(i, j, kpbl) + uvel(i + 1, j, kpbl)) +
90  (vvel(i, j, kpbl) + vvel(i, j + 1, kpbl)) *
91  (vvel(i, j, kpbl) + vvel(i, j + 1, kpbl)) );
92  const Real Rib = CONST_GRAV * zval * (theta - t_surf) / (ws2 * t_layer);
93  above_critical = (Rib >= Ribcr);
94  }
95  // Initial PBL Height
96  // Avoiding detailed interpolation here and just using map-nearest
97  // neighbor Empirical expression for PBLH is given by h = c u* / f
98  // Garratt (1994) and Tennekes (1982)
99  //const Real c_pblh = (l_obuk_arr(i, j, 0) > 0) ? 0.16 : 0.60;
100  //const Real pblh_emp = c_pblh * u_star_arr(i, j, 0) / f0;
101  const Real pblh_emp = gdata.ProbLo(2) + 0.5 * gdata.CellSize(2);
102  pblh_arr(i, j, 0) = (above_critical) ? zval : pblh_emp;
103  pbli_arr(i, j, 0) = (above_critical) ? kpbl : 1;
104  });
105 
106  // Corrector PBL height for thermal excess
107  const Real const_b = turbChoice.pbl_mrf_const_b;
108  const Real sf = turbChoice.pbl_mrf_sf;
109  ParallelFor(xybx, [=] AMREX_GPU_DEVICE(int i, int j, int) noexcept
110  {
111  const Real t_layer = t10av_arr(i, j, 0);
112  const Real phiM = (l_obuk_arr(i, j, 0) > 0)
113  ? (1 + 5 * sf * pblh_arr(i, j, 0) / l_obuk_arr(i, j, 0))
114  : std::pow(
115  (1 - 8 * sf * pblh_arr(i, j, 0) / l_obuk_arr(i, j, 0)),
116  -1.0 / 3.0);
117  const Real wstar = u_star_arr(i, j, 0) / phiM;
118  const Real t_excess = -const_b * u_star_arr(i, j, 0) * t_star_arr(i, j, 0) / wstar;
119  const Real t_surf = t_layer + std::max(std::min(t_excess, 3.0), 0.0);
120 
121  int kpbl = klo;
122  Real zval = 10;
123  bool above_critical = false;
124  while (!above_critical && ((kpbl + 1) <= khi)) {
125  zval = (use_terrain_fitted_coords)
126  ? Compute_Zrel_AtCellCenter(i, j, kpbl, z_nd_arr)
127  : gdata.ProbLo(2) + (kpbl + 0.5) * gdata.CellSize(2);
128  kpbl += 1;
129  const Real theta = cell_data(i, j, kpbl, RhoTheta_comp) /
130  cell_data(i, j, kpbl, Rho_comp);
131  const Real ws2 = 0.25 * ( (uvel(i, j, kpbl) + uvel(i + 1, j, kpbl)) *
132  (uvel(i, j, kpbl) + uvel(i + 1, j, kpbl)) +
133  (vvel(i, j, kpbl) + vvel(i, j + 1, kpbl)) *
134  (vvel(i, j, kpbl) + vvel(i, j + 1, kpbl)) );
135  const Real Rib = CONST_GRAV * zval * (theta - t_surf) / (ws2 * t_layer);
136  above_critical = (Rib >= Ribcr);
137  }
138  //const Real c_pblh = (l_obuk_arr(i, j, 0) > 0) ? 0.16 : 0.60;
139  //const Real pblh_emp = c_pblh * u_star_arr(i, j, 0) / f0;
140  const Real pblh_emp = gdata.ProbLo(2) + 0.5 * gdata.CellSize(2);
141  pblh_corr_arr(i, j, 0) = (above_critical) ? zval : pblh_emp;
142  pbli_arr(i, j, 0) = (above_critical) ? kpbl : 1;
143  });
144  /*
145  amrex::Print() << "PBL height computed for MRF scheme at level "
146  << pblh_arr(2, 2, 0) << " " << pblh_corr_arr(2, 2, 0)
147  << std::endl;
148  amrex::Print() << "PBL Temp:" << t_surf_arr(2, 2, 0) << " "
149  << t10av_arr(2, 2, 0) << std::endl;
150  */
151 
152  // -- Compute diffusion coefficients --
153 
154  const Array4<Real>& K_turb = eddyViscosity.array(mfi);
155 
156  // Dirichlet flags to switch derivative stencil
157  bool c_ext_dir_on_zlo = ((bc_ptr[BCVars::cons_bc].lo(2) == ERFBCType::ext_dir));
158  bool c_ext_dir_on_zhi = ((bc_ptr[BCVars::cons_bc].lo(5) == ERFBCType::ext_dir));
159  bool u_ext_dir_on_zlo = ((bc_ptr[BCVars::xvel_bc].lo(2) == ERFBCType::ext_dir));
160  bool u_ext_dir_on_zhi = ((bc_ptr[BCVars::xvel_bc].lo(5) == ERFBCType::ext_dir));
161  bool v_ext_dir_on_zlo = ((bc_ptr[BCVars::yvel_bc].lo(2) == ERFBCType::ext_dir));
162  bool v_ext_dir_on_zhi = ((bc_ptr[BCVars::yvel_bc].lo(5) == ERFBCType::ext_dir));
163 
164  const auto& dxInv = geom.InvCellSizeArray();
165  const Real dz_inv = geom.InvCellSize(2);
166  const int izmin = geom.Domain().smallEnd(2);
167  const int izmax = geom.Domain().bigEnd(2);
168 
169  ParallelFor(gbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
170  {
171  const Real zval = (use_terrain_fitted_coords)
172  ? Compute_Zrel_AtCellCenter(i, j, k, z_nd_arr)
173  : gdata.ProbLo(2) + (k + 0.5) * gdata.CellSize(2);
174  const Real rho = cell_data(i, j, k, Rho_comp);
175  const Real met_h_zeta = (use_terrain_fitted_coords)
176  ? Compute_h_zeta_AtCellCenter(i, j, k, dxInv, z_nd_arr) : 1.0;
177  const Real dz_terrain = met_h_zeta / dz_inv;
178  if (k < pbli_arr(i, j, 0)) {
179  const Real phiM = (l_obuk_arr(i, j, 0) > 0)
180  ? (1 + 5 * sf * pblh_arr(i, j, 0) / l_obuk_arr(i, j, 0))
181  : std::pow(
182  (1 - 8 * sf * pblh_arr(i, j, 0) / l_obuk_arr(i, j, 0)),
183  -1.0 / 3.0);
184  const Real phit = (l_obuk_arr(i, j, 0) > 0)
185  ? (1 + 5 * sf * pblh_arr(i, j, 0) / l_obuk_arr(i, j, 0))
186  : std::pow(
187  (1 - 16 * sf * pblh_arr(i, j, 0) / l_obuk_arr(i, j, 0)),
188  -1.0 / 2.0);
189  const Real Prt = phit / phiM + const_b * KAPPA * sf;
190  const Real wstar = u_star_arr(i, j, 0) / phiM;
191  K_turb(i, j, k, EddyDiff::Mom_v) = rho * wstar * KAPPA * zval
192  * (1 - zval / pblh_corr_arr(i, j, 0))
193  * (1 - zval / pblh_corr_arr(i, j, 0));
194  K_turb(i, j, k, EddyDiff::Theta_v) = K_turb(i, j, k, EddyDiff::Mom_v) / Prt;
195  } else {
196  const Real lambda = 150.0;
197  const Real lscale = (KAPPA * zval * lambda) / (KAPPA * zval + lambda);
198  Real dthetadz, dudz, dvdz;
199  const int RhoQv_comp = -1;
200  const int RhoQc_comp = -1;
201  const int RhoQr_comp = -1;
202  ComputeVerticalDerivativesPBL(i, j, k, uvel, vvel, cell_data, izmin, izmax, 1.0 / dz_terrain,
203  c_ext_dir_on_zlo, c_ext_dir_on_zhi, u_ext_dir_on_zlo,
204  u_ext_dir_on_zhi, v_ext_dir_on_zlo, v_ext_dir_on_zhi, dthetadz,
205  dudz, dvdz, RhoQv_comp, RhoQc_comp, RhoQr_comp);
206  const Real wind_shear = dudz * dudz + dvdz * dvdz + 1.0e-9;
207  const Real theta = cell_data(i, j, k, RhoTheta_comp) / cell_data(i, j, k, Rho_comp);
208  const Real grad_Ri = std::max(CONST_GRAV / theta * dthetadz / wind_shear, -100.0);
209  /*
210  const Real Pr = 1.5 + 3.08 * grad_Ri;
211  const Real fm =
212  (grad_Ri > 0)
213  ? (std::exp(-8.5 * grad_Ri) + (0.15 / (grad_Ri + 3.0)) * Pr)
214  : std::pow((1 - 12 * grad_Ri), -1.0 / 3.0);
215  const Real ft =
216  (grad_Ri > 0)
217  ? (std::exp(-8.5 * grad_Ri) + (0.15 / (grad_Ri + 3.0)))
218  : std::pow((1 - 16 * grad_Ri), -1.0 / 2.0);
219  */
220  // Using YSU model instead of MRF model
221  const Real Pr = 1.0 + 2.1 * grad_Ri;
222  const Real fm = (grad_Ri > 0)
223  ? 1.0 / ((1.0 + 5.0 * grad_Ri) * (1.0 + 5.0 * grad_Ri))
224  : 1 - 8 * grad_Ri / (1 + 1.746 * std::sqrt(-grad_Ri));
225  const Real ft = (grad_Ri > 0)
226  ? 1.0 / ((1.0 + 5.0 * grad_Ri) * (1.0 + 5.0 * grad_Ri))
227  : 1 - 8 * grad_Ri / (1 + 1.286 * std::sqrt(-grad_Ri));
228  const Real rl2wsp = rho * lscale * lscale * std::sqrt(wind_shear);
229  K_turb(i, j, k, EddyDiff::Mom_v) = rl2wsp * fm * Pr;
230  K_turb(i, j, k, EddyDiff::Theta_v) = rl2wsp * ft;
231  }
232 
233  // limit both diffusion coefficients - from WRF, not documented in
234  // papers
235  constexpr Real ckz = 0.001;
236  constexpr Real Kmax = 1000.0;
237  const Real rhoKmin = ckz * dz_terrain * rho;
238  const Real rhoKmax = rho * Kmax;
239  K_turb(i, j, k, EddyDiff::Mom_v) = std::max(
240  std::min(K_turb(i, j, k, EddyDiff::Mom_v), rhoKmax), rhoKmin);
241  K_turb(i, j, k, EddyDiff::Theta_v) = std::max(
242  std::min(K_turb(i, j, k, EddyDiff::Theta_v), rhoKmax), rhoKmin);
243  K_turb(i, j, k, EddyDiff::Q_v) = K_turb(i, j, k, EddyDiff::Theta_v);
244  K_turb(i, j, k, EddyDiff::Turb_lengthscale) = pblh_arr(i, j, 0);
245  });
246 
247  // FOEXTRAP top and bottom ghost cells
248  ParallelFor(xybx, [=] AMREX_GPU_DEVICE(int i, int j, int ) noexcept
249  {
250  K_turb(i, j, klo-1, EddyDiff::Mom_v ) = K_turb(i, j, klo, EddyDiff::Mom_v );
251  K_turb(i, j, klo-1, EddyDiff::Theta_v) = K_turb(i, j, klo, EddyDiff::Theta_v);
252  K_turb(i, j, klo-1, EddyDiff::Q_v ) = K_turb(i, j, klo, EddyDiff::Q_v );
253  K_turb(i, j, khi+1, EddyDiff::Mom_v ) = K_turb(i, j, khi, EddyDiff::Mom_v );
254  K_turb(i, j, khi+1, EddyDiff::Theta_v) = K_turb(i, j, khi, EddyDiff::Theta_v);
255  K_turb(i, j, khi+1, EddyDiff::Q_v ) = K_turb(i, j, khi, EddyDiff::Q_v );
256  });
257  }// mfi
258 }
constexpr amrex::Real KAPPA
Definition: ERF_Constants.H:20
constexpr amrex::Real CONST_GRAV
Definition: ERF_Constants.H:21
TurbChoice turbChoice
Definition: ERF_DiffSetup.H:2
#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)
Definition: ERF_PBLModels.H:149
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:47
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:390
AMREX_FORCE_INLINE amrex::IntVect TileNoZ()
Definition: ERF_TileNoZ.H:11
@ yvel_bc
Definition: ERF_IndexDefines.H:88
@ cons_bc
Definition: ERF_IndexDefines.H:76
@ xvel_bc
Definition: ERF_IndexDefines.H:87
@ ext_dir
Definition: ERF_IndexDefines.H:209
@ Theta_v
Definition: ERF_IndexDefines.H:176
@ Turb_lengthscale
Definition: ERF_IndexDefines.H:180
@ Q_v
Definition: ERF_IndexDefines.H:179
@ Mom_v
Definition: ERF_IndexDefines.H:175
@ theta
Definition: ERF_MM5.H:20
@ rho
Definition: ERF_Kessler.H:22
@ xvel
Definition: ERF_IndexDefines.H:141
@ yvel
Definition: ERF_IndexDefines.H:142
amrex::Real pbl_mrf_const_b
Definition: ERF_TurbStruct.H:389
amrex::Real pbl_mrf_Ribcr
Definition: ERF_TurbStruct.H:387
amrex::Real pbl_mrf_sf
Definition: ERF_TurbStruct.H:391

Referenced by ComputeTurbulentViscosity().

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