ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_ComputeTurbulentViscosity.cpp File Reference
Include dependency graph for ERF_ComputeTurbulentViscosity.cpp:

Functions

void ComputeTurbulentViscosityLES (Vector< std::unique_ptr< MultiFab >> &Tau_lev, const MultiFab &cons_in, MultiFab &eddyViscosity, MultiFab &Diss, const Geometry &geom, bool use_terrain_fitted_coords, Vector< std::unique_ptr< MultiFab >> &mapfac, const std::unique_ptr< MultiFab > &z_phys_nd, const TurbChoice &turbChoice, const Real const_grav, std::unique_ptr< SurfaceLayer > &, const MoistureComponentIndices &moisture_indices, const MultiFab *xvel, const MultiFab *yvel)
 
void ComputeTurbulentViscosityLES_EB (Vector< std::unique_ptr< MultiFab >> &Tau_lev, const MultiFab &cons_in, MultiFab &eddyViscosity, MultiFab &Hfx1, MultiFab &Hfx2, MultiFab &Hfx3, const Geometry &geom, Vector< std::unique_ptr< MultiFab >> &mapfac, const TurbChoice &turbChoice, const Real const_grav, [[maybe_unused]] const SolverChoice &solverChoice, std::unique_ptr< SurfaceLayer > &, const MoistureComponentIndices &moisture_indices, const eb_ &ebfact, const MultiFab *xvel, const MultiFab *yvel)
 
void ComputeTurbulentViscosityRANS (int level, Vector< std::unique_ptr< MultiFab >> &Tau_lev, const MultiFab &cons_in, const MultiFab &wdist, MultiFab &eddyViscosity, MultiFab &Diss, const Geometry &geom, bool use_terrain_fitted_coords, const std::unique_ptr< MultiFab > &z_phys_nd, const TurbChoice &turbChoice, const Real const_grav, std::unique_ptr< SurfaceLayer > &SurfLayer, const MultiFab *z_0, const CanopyTurbulence &canopy, const MultiFab *forest_drag, const MultiFab *ib_blank, bool flat_wall_diss, Real ib_small_volfrac, Real ib_z0, const MultiFab *xvel, const MultiFab *yvel)
 
void ComputeTurbulentViscosity (double dt, const MultiFab &xvel, const MultiFab &yvel, Vector< std::unique_ptr< MultiFab >> &Tau_lev, MultiFab &cons_in, const MultiFab &wdist, MultiFab &eddyViscosity, MultiFab &Hfx1, MultiFab &Hfx2, MultiFab &Hfx3, MultiFab &Diss, const Geometry &geom, Vector< std::unique_ptr< MultiFab >> &mapfac, 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 SolverChoice &solverChoice, std::unique_ptr< SurfaceLayer > &SurfLayer, const MultiFab *z_0, const bool &use_terrain_fitted_coords, const bool &use_moisture, int level, const BCRec *bc_ptr, const eb_ &ebfact, bool vert_only, const MultiFab *qheating_rates, const MultiFab *terrain_blank, const MultiFab *forest_drag, const MultiFab *Q_fire_atm)
 

Function Documentation

◆ ComputeTurbulentViscosity()

void ComputeTurbulentViscosity ( double  dt,
const MultiFab &  xvel,
const MultiFab &  yvel,
Vector< std::unique_ptr< MultiFab >> &  Tau_lev,
MultiFab &  cons_in,
const MultiFab &  wdist,
MultiFab &  eddyViscosity,
MultiFab &  Hfx1,
MultiFab &  Hfx2,
MultiFab &  Hfx3,
MultiFab &  Diss,
const Geometry &  geom,
Vector< std::unique_ptr< MultiFab >> &  mapfac,
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 SolverChoice &  solverChoice,
std::unique_ptr< SurfaceLayer > &  SurfLayer,
const MultiFab *  z_0,
const bool &  use_terrain_fitted_coords,
const bool &  use_moisture,
int  level,
const BCRec *  bc_ptr,
const eb_ &  ebfact,
bool  vert_only,
const MultiFab *  qheating_rates,
const MultiFab *  terrain_blank,
const MultiFab *  forest_drag,
const MultiFab *  Q_fire_atm 
)

Wrapper to compute turbulent viscosity with LES or PBL.

Parameters
[in]dttime step
[in]xvelvelocity in x-dir
[in]yvelvelocity in y-dir
[in]Tau_levstrain at this level
[in]cons_incell center conserved quantities
[in]wdistwall distance
[out]eddyViscosityturbulent viscosity
[out]Hfx1heat flux in x-dir
[out]Hfx2heat flux in y-dir
[out]Hfx3heat flux in z-dir
[out]Dissdissipation of turbulent kinetic energy
[in]geomproblem geometry
[in]mapfacmap factors
[in]z_phys_ndnodal physical z coordinates
[in]z_sfc_ndNodal heights of the bottom of the domain
[in]z_phys_cccell-centered physical z coordinates
[in]solverChoicecontainer with solver, diffusion, and turbulence parameters
[in]SurfLayeroptional surface-layer model
[in]z_0roughness length
[in]use_terrain_fitted_coordsflag for terrain-fitted coordinates
[in]use_moistureflag for moisture physics
[in]levelAMR level
[in]bc_ptrboundary condition records
[in]ebfactEB factories for cell- and face-centered variables
[in]vert_onlyflag for vertical components of eddyViscosity
[in]qheating_ratesradiation heating rates (SW, LW components)
[in]terrain_blankimmersed forcing mask, or null
[in]forest_dragforest drag field C_d LAD, or null without a forest
[in]Q_fire_atmlagged fire heat flux [W/m2] for the MRF w* boost, or null without it
986 {
987  BL_PROFILE_VAR("ComputeTurbulentViscosity()",ComputeTurbulentViscosity);
988  //
989  // In LES mode, the turbulent viscosity is isotropic (unless mix_isotropic is set to false), so
990  // the LES model sets both horizontal and vertical viscosities
991  //
992  // In PBL mode, the primary purpose of the PBL model is to control vertical transport, so the PBL model sets the vertical viscosity.
993  // Optionally, the PBL model can be run in conjunction with an LES model that sets the horizontal viscosity
994  // (this isn’t truly LES, but the model form is the same as Smagorinsky).
995  //
996  // ComputeTurbulentViscosityLES populates the LES viscosity for both horizontal and vertical components.
997  // ComputeTurbulentViscosityPBL computes the PBL viscosity just for the vertical component.
998  //
999 
1000  TurbChoice turbChoice = solverChoice.turbChoice[level];
1001  const Real const_grav = solverChoice.gravity;
1002 
1003  if (!SurfLayer) {
1004  AMREX_ALWAYS_ASSERT(!vert_only);
1005  }
1006 
1007  bool impose_phys_bcs = true;
1008 
1009  if (turbChoice.les_type != LESType::None) {
1010  if (solverChoice.terrain_type == TerrainType::EB) {
1012  cons_in, eddyViscosity,
1013  Hfx1, Hfx2, Hfx3,
1014  geom,
1015  mapfac, turbChoice, const_grav,
1016  solverChoice,
1017  SurfLayer, solverChoice.moisture_indices,
1018  ebfact, &xvel, &yvel);
1019  } else {
1021  cons_in, eddyViscosity,
1022  Diss,
1023  geom, use_terrain_fitted_coords,
1024  mapfac, z_phys_nd, turbChoice, const_grav,
1025  SurfLayer, solverChoice.moisture_indices,
1026  &xvel, &yvel);
1027  }
1028  }
1029 
1030  if (turbChoice.rans_type != RANSType::None) {
1031  ComputeTurbulentViscosityRANS(level, Tau_lev, cons_in, wdist,
1032  eddyViscosity,
1033  Diss,
1034  geom, use_terrain_fitted_coords,
1035  z_phys_nd, turbChoice, const_grav,
1036  SurfLayer, z_0,
1037  solverChoice.canopyTurb, forest_drag,
1038  (solverChoice.if_fraction_stress) ? terrain_blank : nullptr,
1039  solverChoice.if_flat_wall_dissipation,
1040  solverChoice.if_small_volfrac,
1041  solverChoice.if_z0,
1042  &xvel, &yvel);
1043  }
1044 
1045  if (turbChoice.pbl_type == PBLType::MYJ) {
1046  ComputeDiffusivityMYJ(dt, xvel, yvel, cons_in, eddyViscosity,
1047  geom, turbChoice, SurfLayer,
1048  use_terrain_fitted_coords, use_moisture,
1049  level, bc_ptr, vert_only, z_phys_nd, z_sfc_nd, z_phys_cc,
1050  solverChoice.moisture_indices);
1051  } else if (turbChoice.pbl_type == PBLType::MYNN25) {
1052  ComputeDiffusivityMYNN25(xvel, yvel, cons_in, eddyViscosity,
1053  geom, turbChoice, SurfLayer,
1054  use_terrain_fitted_coords, use_moisture,
1055  level, bc_ptr, vert_only, z_phys_nd, z_sfc_nd, z_phys_cc,
1056  solverChoice.moisture_indices);
1057  } else if (turbChoice.pbl_type == PBLType::MYNNEDMF) {
1058  ComputeDiffusivityMYNNEDMF(xvel, yvel, cons_in, eddyViscosity,
1059  geom, turbChoice, SurfLayer,
1060  use_terrain_fitted_coords, use_moisture,
1061  level, bc_ptr, vert_only, z_phys_nd, z_sfc_nd, z_phys_cc,
1062  solverChoice.moisture_indices);
1063  } else if (turbChoice.pbl_type == PBLType::MRF) {
1064  ComputeDiffusivityMRF(xvel, yvel, cons_in, eddyViscosity,
1065  geom, turbChoice, SurfLayer,
1066  use_terrain_fitted_coords, use_moisture,
1067  level, bc_ptr, vert_only, z_phys_nd, z_sfc_nd, z_phys_cc,
1068  solverChoice.moisture_indices, terrain_blank, Q_fire_atm,
1069  {{mapfac[MapFacType::m_x].get(), mapfac[MapFacType::m_y].get()}});
1070  } else if (turbChoice.pbl_type == PBLType::YSU) {
1071  ComputeDiffusivityYSU(xvel, yvel, cons_in, eddyViscosity,
1072  geom, turbChoice, SurfLayer,
1073  use_terrain_fitted_coords, use_moisture,
1074  level, bc_ptr, vert_only, z_phys_nd, z_sfc_nd, z_phys_cc,
1075  solverChoice.moisture_indices,
1076  qheating_rates, terrain_blank);
1077  } else if (turbChoice.uses_shoc_family()) {
1078  // NOTE: Nothing to do here. The SHOC class handles setting the vertical
1079  // components of eddyDiffs in slow RHS pre.
1080  } else if (turbChoice.pbl_type != PBLType::None) {
1081  // YSUNew, SHOC and EAMXX_SHOC are input names only, resolved when read (ERF_TurbStruct.H)
1082  Abort("ComputeTurbulentViscosity: no PBL diffusivity for erf.pbl_type = " +
1083  std::string(amrex::getEnumNameString(turbChoice.pbl_type)));
1084  }
1085 
1086  //
1087  // At all levels we need to fill values outside the physical boundary for the LES coeffs.
1088  // In addition, for all cases, if at level > 0, we want to fill fine ghost cell values that
1089  // overlie coarse grid cells (and that are not in another fine valid region) with
1090  // extrapolated values from the interior, rather than interpolating from the coarser level,
1091  // since we may be using a different turbulence model there.
1092  //
1093  // Note: here "covered" refers to "covered by valid region of another grid at this level"
1094  // Note: here "physbnd" refers to "cells outside the domain if not periodic"
1095  // Note: here "interior" refers to "valid cells, i.e. inside 'my' grid"
1096  //
1097  {
1098  int is_covered = 0;
1099  int is_notcovered = 1;
1100  int is_physbnd = 2;
1101  int is_interior = 3;
1102  iMultiFab cc_mask(eddyViscosity.boxArray(),eddyViscosity.DistributionMap(),1,1);
1103  cc_mask.BuildMask(geom.Domain(), geom.periodicity(), is_covered, is_notcovered, is_physbnd, is_interior);
1104 
1105  Box domain = geom.Domain();
1106  for (int i = 0; i < AMREX_SPACEDIM; ++i) {
1107  if (geom.isPeriodic(i)) {
1108  domain.grow(i,1);
1109  }
1110  }
1111 
1112  eddyViscosity.FillBoundary(geom.periodicity());
1113 
1114  int ncomp = eddyViscosity.nComp();
1115 
1116 #ifdef _OPENMP
1117 #pragma omp parallel if (Gpu::notInLaunchRegion())
1118 #endif
1119  for (MFIter mfi(eddyViscosity); mfi.isValid(); ++mfi)
1120  {
1121  Box vbx = mfi.validbox();
1122 
1123  Box planex_lo = mfi.growntilebox(1); planex_lo.setBig(0, vbx.smallEnd(0)-1);
1124  Box planey_lo = mfi.growntilebox(1); planey_lo.setBig(1, vbx.smallEnd(1)-1);
1125  Box planez_lo = mfi.growntilebox(1); planez_lo.setBig(2, vbx.smallEnd(2)-1);
1126 
1127  Box planex_hi = mfi.growntilebox(1); planex_hi.setSmall(0, vbx.bigEnd(0)+1);
1128  Box planey_hi = mfi.growntilebox(1); planey_hi.setSmall(1, vbx.bigEnd(1)+1);
1129  Box planez_hi = mfi.growntilebox(1); planez_hi.setSmall(2, vbx.bigEnd(2)+1);
1130 
1131  int i_lo = vbx.smallEnd(0); int i_hi = vbx.bigEnd(0);
1132  int j_lo = vbx.smallEnd(1); int j_hi = vbx.bigEnd(1);
1133  int k_lo = vbx.smallEnd(2); int k_hi = vbx.bigEnd(2);
1134 
1135  const Array4<Real>& mu_turb = eddyViscosity.array(mfi);
1136  const Array4<int>& mask_arr = cc_mask.array(mfi);
1137 
1138  auto domlo = lbound(domain);
1139  auto domhi = ubound(domain);
1140 
1141  ParallelFor(planex_lo, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
1142  {
1143  int lj = amrex::min(amrex::max(j, domlo.y), domhi.y);
1144  int lk = amrex::min(amrex::max(k, domlo.z), domhi.z);
1145  if ((mask_arr(i,j,k) == is_notcovered && mask_arr(i_lo,lj,lk) != is_notcovered) ||
1146  (mask_arr(i,j,k) == is_physbnd && i < domlo.x && impose_phys_bcs)) {
1147  for (int n = 0; n < ncomp; n++) {
1148  mu_turb(i,j,k,n) = mu_turb(i_lo,lj,lk,n);
1149  }
1150  }
1151  });
1152  ParallelFor(planex_hi, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
1153  {
1154  int lj = amrex::min(amrex::max(j, domlo.y), domhi.y);
1155  int lk = amrex::min(amrex::max(k, domlo.z), domhi.z);
1156  if ((mask_arr(i,j,k) == is_notcovered && mask_arr(i_hi,lj,lk) != is_notcovered) ||
1157  (mask_arr(i,j,k) == is_physbnd && i > domhi.x && impose_phys_bcs)) {
1158  for (int n = 0; n < ncomp; n++) {
1159  mu_turb(i,j,k,n) = mu_turb(i_hi,lj,lk,n);
1160  }
1161  }
1162  });
1163  ParallelFor(planey_lo, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
1164  {
1165  int lk = amrex::min(amrex::max(k, domlo.z), domhi.z);
1166  if ((mask_arr(i,j,k) == is_notcovered && mask_arr(i,j_lo,lk) != is_notcovered) ||
1167  (mask_arr(i,j,k) == is_physbnd && j < domlo.y && impose_phys_bcs)) {
1168  for (int n = 0; n < ncomp; n++) {
1169  mu_turb(i,j,k,n) = mu_turb(i,j_lo,lk,n);
1170  }
1171  }
1172  });
1173  ParallelFor(planey_hi, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
1174  {
1175  int lk = amrex::min(amrex::max(k, domlo.z), domhi.z);
1176  if ((mask_arr(i,j,k) == is_notcovered && mask_arr(i,j_hi,lk) != is_notcovered)||
1177  (mask_arr(i,j,k) == is_physbnd && j > domhi.y && impose_phys_bcs)) {
1178  for (int n = 0; n < ncomp; n++) {
1179  mu_turb(i,j,k,n) = mu_turb(i,j_hi,lk,n);
1180  }
1181  }
1182  });
1183  ParallelFor(planez_lo, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
1184  {
1185  if ((mask_arr(i,j,k) == is_notcovered && mask_arr(i,j,k_lo) != is_notcovered) ||
1186  (mask_arr(i,j,k) == is_physbnd && k < domlo.z && impose_phys_bcs)) {
1187  for (int n = 0; n < ncomp; n++) {
1188  mu_turb(i,j,k,n) = mu_turb(i,j,k_lo,n);
1189  }
1190  }
1191  });
1192  ParallelFor(planez_hi, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
1193  {
1194  if ((mask_arr(i,j,k) == is_notcovered && mask_arr(i,j,k_hi) != is_notcovered) ||
1195  (mask_arr(i,j,k) == is_physbnd && k > domhi.z && impose_phys_bcs)) {
1196  for (int n = 0; n < ncomp; n++) {
1197  mu_turb(i,j,k,n) = mu_turb(i,j,k_hi,n);
1198  }
1199  }
1200  });
1201  //
1202  // Edge and corner ghost cells at a C/F boundary are skipped above when every face
1203  // neighbor toward the valid box is itself a C/F ghost cell, e.g. (i_lo-1,j_lo-1).
1204  // Fill these from a cell that is valid at this level (in this grid or another),
1205  // stepping toward the valid box in two directions (xy, xz, yz) before all three.
1206  // The sources are never written in this block, and taking covered cells as well as
1207  // our own keeps the result independent of how the level is divided into grids.
1208  //
1209  ParallelFor(mfi.growntilebox(1), [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
1210  {
1211  if (mask_arr(i,j,k) != is_notcovered) { return; }
1212 
1213  int si = amrex::min(amrex::max(i, i_lo), i_hi);
1214  int sj = amrex::min(amrex::max(j, j_lo), j_hi);
1215  int sk = amrex::min(amrex::max(k, k_lo), k_hi);
1216  bool gx = (si != i);
1217  bool gy = (sj != j);
1218  bool gz = (sk != k);
1219 
1220  // Already filled by one of the plane kernels above
1221  if (gx && mask_arr(si,j,k) != is_notcovered) { return; }
1222  if (gy && mask_arr(i,sj,k) != is_notcovered) { return; }
1223  if (gz && mask_arr(i,j,sk) != is_notcovered) { return; }
1224 
1225  int ii = si; int jj = sj; int kk = sk;
1226  if (gx && gy && mask_arr(si,sj,k ) != is_notcovered) {
1227  kk = k;
1228  } else if (gx && gz && mask_arr(si,j ,sk) != is_notcovered) {
1229  jj = j;
1230  } else if (gy && gz && mask_arr(i ,sj,sk) != is_notcovered) {
1231  ii = i;
1232  }
1233  for (int n = 0; n < ncomp; n++) {
1234  mu_turb(i,j,k,n) = mu_turb(ii,jj,kk,n);
1235  }
1236  });
1237  } // mfi
1238 
1239  eddyViscosity.FillBoundary(geom.periodicity());
1240  }
1241 }
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 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, const MultiFab *terrain_blank, const MultiFab *Q_fire_atm, GpuArray< const MultiFab *, 2 > mf_m)
Definition: ERF_ComputeDiffusivityMRF.cpp:37
void ComputeDiffusivityMYJ(double dt, const MultiFab &xvel, const MultiFab &yvel, 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)
Definition: ERF_ComputeDiffusivityMYJ.cpp:34
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)
Definition: ERF_ComputeDiffusivityMYNN25.cpp:32
void ComputeDiffusivityMYNNEDMF(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 viscosity and diffusivity coefficients using the MYNN-EDMF closure.
Definition: ERF_ComputeDiffusivityMYNNEDMF.cpp:4195
void ComputeDiffusivityYSU(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, const MultiFab *qheating_rates, const MultiFab *terrain_blank)
Definition: ERF_ComputeDiffusivityYSU.cpp:45
void ComputeTurbulentViscosityLES_EB(Vector< std::unique_ptr< MultiFab >> &Tau_lev, const MultiFab &cons_in, MultiFab &eddyViscosity, MultiFab &Hfx1, MultiFab &Hfx2, MultiFab &Hfx3, const Geometry &geom, Vector< std::unique_ptr< MultiFab >> &mapfac, const TurbChoice &turbChoice, const Real const_grav, [[maybe_unused]] const SolverChoice &solverChoice, std::unique_ptr< SurfaceLayer > &, const MoistureComponentIndices &moisture_indices, const eb_ &ebfact, const MultiFab *xvel, const MultiFab *yvel)
Definition: ERF_ComputeTurbulentViscosity.cpp:362
void ComputeTurbulentViscosity(double dt, const MultiFab &xvel, const MultiFab &yvel, Vector< std::unique_ptr< MultiFab >> &Tau_lev, MultiFab &cons_in, const MultiFab &wdist, MultiFab &eddyViscosity, MultiFab &Hfx1, MultiFab &Hfx2, MultiFab &Hfx3, MultiFab &Diss, const Geometry &geom, Vector< std::unique_ptr< MultiFab >> &mapfac, 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 SolverChoice &solverChoice, std::unique_ptr< SurfaceLayer > &SurfLayer, const MultiFab *z_0, const bool &use_terrain_fitted_coords, const bool &use_moisture, int level, const BCRec *bc_ptr, const eb_ &ebfact, bool vert_only, const MultiFab *qheating_rates, const MultiFab *terrain_blank, const MultiFab *forest_drag, const MultiFab *Q_fire_atm)
Definition: ERF_ComputeTurbulentViscosity.cpp:961
void ComputeTurbulentViscosityRANS(int level, Vector< std::unique_ptr< MultiFab >> &Tau_lev, const MultiFab &cons_in, const MultiFab &wdist, MultiFab &eddyViscosity, MultiFab &Diss, const Geometry &geom, bool use_terrain_fitted_coords, const std::unique_ptr< MultiFab > &z_phys_nd, const TurbChoice &turbChoice, const Real const_grav, std::unique_ptr< SurfaceLayer > &SurfLayer, const MultiFab *z_0, const CanopyTurbulence &canopy, const MultiFab *forest_drag, const MultiFab *ib_blank, bool flat_wall_diss, Real ib_small_volfrac, Real ib_z0, const MultiFab *xvel, const MultiFab *yvel)
Definition: ERF_ComputeTurbulentViscosity.cpp:637
void ComputeTurbulentViscosityLES(Vector< std::unique_ptr< MultiFab >> &Tau_lev, const MultiFab &cons_in, MultiFab &eddyViscosity, MultiFab &Diss, const Geometry &geom, bool use_terrain_fitted_coords, Vector< std::unique_ptr< MultiFab >> &mapfac, const std::unique_ptr< MultiFab > &z_phys_nd, const TurbChoice &turbChoice, const Real const_grav, std::unique_ptr< SurfaceLayer > &, const MoistureComponentIndices &moisture_indices, const MultiFab *xvel, const MultiFab *yvel)
Definition: ERF_ComputeTurbulentViscosity.cpp:38
const bool use_moisture
Definition: ERF_InitCustomPert_ABL.H:127
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);})
Real z_0
Definition: ERF_UpdateWSubsidence_Bomex.H:10
@ None
Definition: ERF_LSM_Types.H:6
@ xvel
Definition: ERF_IndexDefines.H:232
@ yvel
Definition: ERF_IndexDefines.H:233
amrex::Vector< TurbChoice > turbChoice
Turbulence options for each AMR level.
Definition: ERF_DataStruct.H:3020
bool if_fraction_stress
erf.if_wall_form = fraction_stress: the wall stress of ERF_ImmersedWallCell.H in the wall cell of eac...
Definition: ERF_DataStruct.H:3126
amrex::Real gravity
Effective gravitational acceleration.
Definition: ERF_DataStruct.H:3148
amrex::Real if_small_volfrac
Immersed-forcing solid fraction below which a cell or face counts as fluid; read from eb2....
Definition: ERF_DataStruct.H:3135
amrex::Real if_z0
Immersed-forcing roughness length [m].
Definition: ERF_DataStruct.H:3113
static TerrainType terrain_type
Terrain or immersed-boundary representation.
Definition: ERF_DataStruct.H:2989
bool if_flat_wall_dissipation
Partial fraction-stress wall cells dissipate TKE with the length scale of flat ground's first cell; o...
Definition: ERF_DataStruct.H:3130
CanopyTurbulence canopyTurb
Definition: ERF_DataStruct.H:3548
MoistureComponentIndices moisture_indices
Index map of the moisture data carried by the active scheme: conserved-state components for the speci...
Definition: ERF_DataStruct.H:3504
Definition: ERF_TurbStruct.H:129
RANSType rans_type
Selected RANS closure.
Definition: ERF_TurbStruct.H:960
bool uses_shoc_family() const noexcept
Query whether this level uses any SHOC-family PBL scheme.
Definition: ERF_TurbStruct.H:1018
LESType les_type
Selected LES closure.
Definition: ERF_TurbStruct.H:909
PBLType pbl_type
Selected PBL closure.
Definition: ERF_TurbStruct.H:1003

Referenced by ERF::advance_dycore().

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

◆ ComputeTurbulentViscosityLES()

void ComputeTurbulentViscosityLES ( Vector< std::unique_ptr< MultiFab >> &  Tau_lev,
const MultiFab &  cons_in,
MultiFab &  eddyViscosity,
MultiFab &  Diss,
const Geometry &  geom,
bool  use_terrain_fitted_coords,
Vector< std::unique_ptr< MultiFab >> &  mapfac,
const std::unique_ptr< MultiFab > &  z_phys_nd,
const TurbChoice &  turbChoice,
const Real  const_grav,
std::unique_ptr< SurfaceLayer > &  ,
const MoistureComponentIndices &  moisture_indices,
const MultiFab *  xvel,
const MultiFab *  yvel 
)

Function for computing the turbulent viscosity with LES.

Parameters
[in]Tau_levstrain at this level
[in]cons_incell center conserved quantities
[out]eddyViscosityturbulent viscosity
[out]Dissdissipation of turbulent kinetic energy
[in]geomproblem geometry
[in]use_terrain_fitted_coordsflag for terrain-fitted coordinates
[in]mapfacmap factors
[in]z_phys_ndnodal physical z coordinates
[in]turbChoicecontainer with turbulence parameters
[in]const_gravgravitational acceleration
[in]moisture_indicesmoisture component indices
[in]xvelx-direction velocity (for moist Ri correction)
[in]yvely-direction velocity (for moist Ri correction)
49 {
50  const GpuArray<Real, AMREX_SPACEDIM> cellSizeInv = geom.InvCellSizeArray();
51  // The filter widths take dx in place of a dy over one cell, and the reverse
52  const GpuArray<Real, AMREX_SPACEDIM> lenInv = LengthScaleInvCellSize(geom);
53  const Box& domain = geom.Domain();
54 
55  Real inv_Pr_t = turbChoice.Pr_t_inv;
56  Real inv_Sc_t = turbChoice.Sc_t_inv;
57  Real inv_sigma_k = one / turbChoice.sigma_k;
58 
59  bool use_thetav_grad = (turbChoice.strat_type == StratType::thetav);
60  bool use_thetal_grad = (turbChoice.strat_type == StratType::thetal);
61 
62  bool isotropic = turbChoice.mix_isotropic;
63 
64  // SMAGORINSKY: Fill Kturb for momentum in horizontal and vertical
65  //***********************************************************************************
66  if (turbChoice.les_type == LESType::Smagorinsky)
67  {
68  Real Cs = turbChoice.Cs;
69  bool smag2d = turbChoice.smag2d;
70 
71  // Opt-in limits of WRF's smag2d_km on the horizontal viscosity (see
72  // Smag2DLimitedViscosity). The slope limiter needs the terrain slope, so it acts
73  // on terrain-fitted meshes only; SolverChoice aborts if it is asked for elsewhere.
74  bool l_smag2d_slope = smag2d && turbChoice.smag2d_slope_limiter && use_terrain_fitted_coords;
75  Real l_smag2d_cap = (smag2d) ? turbChoice.smag2d_kh_cap : zero;
76  bool l_smag2d_limit = l_smag2d_slope || (l_smag2d_cap > zero);
77 
78  // Define variables required inside device lambdas (scalars only)
79  Real l_abs_g = const_grav;
80  Real l_Ri_crit = turbChoice.Ri_crit;
81  bool l_use_Ri_corr = turbChoice.use_Ri_correction;
82  bool l_has_xvel = (xvel != nullptr);
83  bool l_has_yvel = (yvel != nullptr);
84 
85 #ifdef _OPENMP
86 #pragma omp parallel if (Gpu::notInLaunchRegion())
87 #endif
88  for (MFIter mfi(eddyViscosity,TilingIfNotGPU()); mfi.isValid(); ++mfi)
89  {
90  Box bxcc = mfi.growntilebox(1) & domain;
91  // NOTE: the closures deliberately store no subgrid heat flux. hfx_z (Hfx3)
92  // is z-nodal, (0,0,1), while the closure's -K dtheta/dz is cell-centered,
93  // and the theta diffusion of every RK stage overwrites all z-faces (the
94  // surface layer the bottom face) before anything reads them, so the value
95  // written here was both misplaced and unused. The TKE buoyancy source
96  // reads the face fluxes of that diffusion (ERF_TurbKESources.H).
97  const Array4<Real>& mu_turb = eddyViscosity.array(mfi);
98  const Array4<Real const >& cell_data = cons_in.array(mfi);
99  const StrainArrays tau = strain_arrays(Tau_lev, mfi);
100  const Array4<Real const> tau11 = tau.tau11, tau22 = tau.tau22, tau33 = tau.tau33;
101  const Array4<Real const> tau12 = tau.tau12, tau13 = tau.tau13, tau23 = tau.tau23;
102  Array4<Real const> mf_mx = mapfac[MapFacType::m_x]->const_array(mfi);
103  Array4<Real const> mf_my = mapfac[MapFacType::m_y]->const_array(mfi);
104  Array4<Real const> z_nd_arr = z_phys_nd->const_array(mfi);
105 
106  Array4<Real const> u_arr = (l_has_xvel) ? xvel->const_array(mfi) : Array4<Real const>{};
107  Array4<Real const> v_arr = (l_has_yvel) ? yvel->const_array(mfi) : Array4<Real const>{};
108 
109  ParallelFor(bxcc, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
110  {
111  // =====================================================================
112  // 1. STRAIN RATE MAGNITUDE CALCULATION
113  // =====================================================================
114  Real SmnSmn;
115  if (smag2d) {
116  SmnSmn = ComputeSmnSmn2D(i,j,k,tau11,tau22,tau12);
117  } else {
118  SmnSmn = ComputeSmnSmn(i,j,k,tau11,tau22,tau33,tau12,tau13,tau23);
119  }
120  Real strain_rate_magnitude = std::sqrt(two * SmnSmn);
121 
122  // =====================================================================
123  // 2. GRID SCALE CALCULATION (filter width Δ)
124  // =====================================================================
125  Real dxInv = lenInv[0];
126  Real dyInv = lenInv[1];
127  Real dzInv = cellSizeInv[2];
128  if (use_terrain_fitted_coords) {
129  dzInv /= Compute_h_zeta_AtCellCenter(i,j,k, cellSizeInv, z_nd_arr);
130  }
131 
132  Real Delta;
133  Real DeltaH;
134  if (isotropic) {
135  Real cellVolMsf = one / (dxInv * mf_mx(i,j,0) * dyInv * mf_my(i,j,0) * dzInv);
136  Delta = std::cbrt(cellVolMsf);
137  DeltaH = Delta;
138  } else {
139  Delta = one / dzInv;
140  DeltaH = std::sqrt(one / (dxInv * mf_mx(i,j,0) * dyInv * mf_my(i,j,0)));
141  }
142 
143  Real rho = cell_data(i, j, k, Rho_comp);
144  Real CsDeltaSqr_h = Cs * Cs * DeltaH * DeltaH;
145  Real CsDeltaSqr_v = Cs * Cs * Delta * Delta;
146 
147  Real nu_turb_base_h = CsDeltaSqr_h * strain_rate_magnitude;
148  Real nu_turb_base_v = CsDeltaSqr_v * strain_rate_magnitude;
149 
150  // WRF smag2d_km limits on K_h only, applied before the scalar diffusivities
151  // (Theta_h, Scalar_h, Q_h) are derived from Mom_h below; Mom_v is unchanged.
152  if (l_smag2d_limit) {
153  Real alpha = one;
154  if (l_smag2d_slope) {
156  }
157  nu_turb_base_h = Smag2DLimitedViscosity(nu_turb_base_h, strain_rate_magnitude,
158  DeltaH, alpha, l_smag2d_cap, l_smag2d_slope);
159  }
160 
161  Real stability_factor = one;
162 
163  if (l_use_Ri_corr && l_has_xvel && l_has_yvel) {
164  Real N2 = ComputeN2(i, j, k, dzInv, l_abs_g, cell_data, moisture_indices);
165  Real S2_vert = ComputeVerticalShear2(i, j, k, dzInv, u_arr, v_arr);
166  Real Ri = ComputeRichardson(N2, S2_vert);
167  stability_factor = StabilityFunction(Ri, l_Ri_crit);
168  }
169 
170  if (isotropic) {
171  mu_turb(i, j, k, EddyDiff::Mom_h) = rho * nu_turb_base_h * stability_factor;
172  mu_turb(i, j, k, EddyDiff::Mom_v) = rho * nu_turb_base_v * stability_factor;
173  } else {
174  mu_turb(i, j, k, EddyDiff::Mom_h) = rho * nu_turb_base_h;
175  mu_turb(i, j, k, EddyDiff::Mom_v) = rho * nu_turb_base_v * stability_factor;
176  }
177  });
178  }
179  }
180  // DEARDORFF: Fill Kturb for momentum in horizontal and vertical
181  //***********************************************************************************
182  else if (turbChoice.les_type == LESType::Deardorff)
183  {
184  const Real l_C_k = turbChoice.Ck;
185  const Real l_C_e = turbChoice.Ce;
186  const Real l_C_e_wall = turbChoice.Ce_wall;
187  const Real Ce_lcoeff = amrex::max(amrex::Real(zero), l_C_e - Real(1.9)*l_C_k);
188  const Real l_abs_g = const_grav;
189 
190  // Clamped divisor: the select is if-converted, so 1/theta_ref runs even
191  // when theta_ref = 0 and would trip fpe_trap_zero (see ERF_SetupDiff.H)
192  const bool use_ref_theta = (turbChoice.theta_ref > 0);
193  const Real inv_theta_ref = one / amrex::max(turbChoice.theta_ref, std::numeric_limits<Real>::min());
194  const Real l_inv_theta0 = (use_ref_theta) ? inv_theta_ref : one;
195 
196 #ifdef _OPENMP
197 #pragma omp parallel if (Gpu::notInLaunchRegion())
198 #endif
199  for ( MFIter mfi(eddyViscosity,TilingIfNotGPU()); mfi.isValid(); ++mfi)
200  {
201  Box bxcc = mfi.tilebox();
202 
203  // NOTE: the closures deliberately store no subgrid heat flux. hfx_z (Hfx3)
204  // is z-nodal, (0,0,1), while the closure's -K dtheta/dz is cell-centered,
205  // and the theta diffusion of every RK stage overwrites all z-faces (the
206  // surface layer the bottom face) before anything reads them, so the value
207  // written here was both misplaced and unused. The TKE buoyancy source
208  // reads the face fluxes of that diffusion (ERF_TurbKESources.H).
209  const Array4<Real>& mu_turb = eddyViscosity.array(mfi);
210  const Array4<Real>& diss = Diss.array(mfi);
211 
212  const Array4<Real const > &cell_data = cons_in.array(mfi);
213 
214  Array4<Real const> mf_mx = mapfac[MapFacType::m_x]->const_array(mfi);
215  Array4<Real const> mf_my = mapfac[MapFacType::m_y]->const_array(mfi);
216 
217  Array4<Real const> z_nd_arr = z_phys_nd->const_array(mfi);
218 
219  ParallelFor(bxcc, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
220  {
221  Real dxInv = lenInv[0];
222  Real dyInv = lenInv[1];
223  Real dzInv = cellSizeInv[2];
224  if (use_terrain_fitted_coords) {
225  // the terrain grid is only deformed in z for now
226  dzInv /= Compute_h_zeta_AtCellCenter(i,j,k, cellSizeInv, z_nd_arr);
227  }
228  Real Delta;
229  if (isotropic) {
230  Real cellVolMsf = one / (dxInv * mf_mx(i,j,0) * dyInv * mf_my(i,j,0) * dzInv);
231  Delta = std::cbrt(cellVolMsf);
232  } else {
233  Delta = one / dzInv;
234  }
235 
236  Real dtheta_dz;
237  if (use_thetav_grad) {
238  dtheta_dz = myhalf * ( GetThetav(i, j, k+1, cell_data, moisture_indices)
239  -GetThetav(i, j, k-1, cell_data, moisture_indices) )*dzInv;
240  } else if (use_thetal_grad) {
241  dtheta_dz = myhalf * ( GetThetal(i, j, k+1, cell_data, moisture_indices)
242  -GetThetal(i, j, k-1, cell_data, moisture_indices) )*dzInv;
243  } else {
244  dtheta_dz = myhalf * ( cell_data(i, j, k+1, RhoTheta_comp) / cell_data(i, j, k+1, Rho_comp)
245  -cell_data(i, j, k-1, RhoTheta_comp) / cell_data(i, j, k-1, Rho_comp) )*dzInv;
246  }
247 
248  // Calculate stratification-dependent mixing length (Deardorff 1980, Eqn. 10a)
249  Real E = amrex::max(cell_data(i,j,k,RhoKE_comp)/cell_data(i,j,k,Rho_comp),Real(0.0));
250  Real stratification = l_abs_g * dtheta_dz * l_inv_theta0;
251  if (!use_ref_theta) {
252  // l_inv_theta0 == 1, divide by actual theta
253  stratification *= cell_data(i,j,k,Rho_comp) /
254  cell_data(i,j,k,RhoTheta_comp);
255  }
256 
257  // Following WRF, the stratification effects are applied to the vertical length scales
258  // in the case of anisotropic mixing
259  Real length;
261  if (stratification <= eps) {
262  length = Delta; // cbrt(dx*dy*dz) -or- dz
263  } else {
264  length = Real(0.76) * std::sqrt(E / amrex::max(stratification,eps));
265  // mixing length should be _reduced_ for stable stratification
266  length = amrex::min(length, Delta);
267  // following WRF, make sure the mixing length isn't too small
268  length = amrex::max(length, Real(0.001) * Delta);
269  }
270 
271  Real DeltaH = (isotropic) ? length : std::sqrt(one / (dxInv * mf_mx(i,j,0) * dyInv * mf_my(i,j,0)));
272 
273  Real Pr_inv_v = (one + two*length/Delta);
274  Real Pr_inv_h = (isotropic) ? Pr_inv_v : inv_Pr_t;
275 
276  // Calculate eddy diffusivities
277  // K = rho * C_k * l * KE^(1/2)
278  mu_turb(i,j,k,EddyDiff::Mom_h) = cell_data(i,j,k,Rho_comp) * l_C_k * DeltaH * std::sqrt(E);
279  mu_turb(i,j,k,EddyDiff::Mom_v) = cell_data(i,j,k,Rho_comp) * l_C_k * length * std::sqrt(E);
280  // KH = (1 + 2*l/delta) * mu_turb
281  mu_turb(i,j,k,EddyDiff::Theta_h) = Pr_inv_h * mu_turb(i,j,k,EddyDiff::Mom_h);
282  mu_turb(i,j,k,EddyDiff::Theta_v) = Pr_inv_v * mu_turb(i,j,k,EddyDiff::Mom_v);
283  // Store lengthscale for TKE source terms
284  mu_turb(i,j,k,EddyDiff::Turb_lengthscale) = length;
285 
286  // Calculate SFS quantities
287  // - dissipation
288  Real Ce;
289  if ((l_C_e_wall > 0) && (k==0)) {
290  Ce = l_C_e_wall;
291  } else {
292  Ce = Real(1.9)*l_C_k + Ce_lcoeff*length / Delta;
293  }
294  diss(i,j,k) = cell_data(i,j,k,Rho_comp) * Ce * std::pow(E,Real(1.5)) / length;
295  });
296  }
297  }
298 
299  // Extrapolate Kturb in x/y, fill remaining elements (relevant to lev==0)
300  //***********************************************************************************
301  int ngc(1);
302  // EddyDiff mapping : Theta_h KE_h Scalar_h Q_h
303  Vector<Real> Factors = {inv_Pr_t, inv_sigma_k, inv_Sc_t, inv_Sc_t}; // alpha = mu/Pr
304  Gpu::AsyncVector<Real> d_Factors; d_Factors.resize(Factors.size());
305  Gpu::copy(Gpu::hostToDevice, Factors.begin(), Factors.end(), d_Factors.begin());
306  Real* fac_ptr = d_Factors.data();
307 
308  const bool use_KE = ( turbChoice.les_type == LESType::Deardorff );
309 
310 #ifdef _OPENMP
311 #pragma omp parallel if (Gpu::notInLaunchRegion())
312 #endif
313  for ( MFIter mfi(eddyViscosity,TilingIfNotGPU()); mfi.isValid(); ++mfi)
314  {
315  Box bxcc = mfi.tilebox();
316  Box planex = bxcc; planex.setSmall(0, 1); planex.setBig(0, ngc); planex.grow(1,1);
317  Box planey = bxcc; planey.setSmall(1, 1); planey.setBig(1, ngc); planey.grow(0,1);
318  //
319  // NOTE: growntilebox, not the tilebox grown by hand. Growing by hand pushes every tile
320  // ngc cells past its own share of the grid, so once the grid is tiled two tiles
321  // store to the same mu_turb cells -- a data race under OpenMP, even though both
322  // store the same value. The union of the tiles, and the values stored, are
323  // unchanged, and this is identical to the old expression with one tile per grid.
324  //
325  bxcc = mfi.growntilebox(IntVect(ngc,ngc,0));
326 
327  const Array4<Real>& mu_turb = eddyViscosity.array(mfi);
328 
329  for (auto n = 1; n < EddyDiff::Mom_v; ++n) {
330  int offset = EddyDiff::Mom_v;
331  switch (n)
332  {
333  case EddyDiff::KE_h:
334  if (use_KE) {
335  ParallelFor(bxcc, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
336  {
337  int indx = n;
338  int indx_v = indx + offset;
339  mu_turb(i,j,k,indx) = mu_turb(i,j,k,EddyDiff::Mom_h) * fac_ptr[indx-1];
340  mu_turb(i,j,k,indx_v) = mu_turb(i,j,k,EddyDiff::Mom_v) * fac_ptr[indx-1];
341  });
342  }
343  break;
344  default:
345  ParallelFor(bxcc, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
346  {
347  int indx = n;
348  int indx_v = indx + offset;
349 
350  // NOTE: Theta_h, Theta_v have already been set for Deardorff
351  if (!(indx_v == EddyDiff::Theta_v && use_KE)) {
352  mu_turb(i,j,k,indx) = mu_turb(i,j,k,EddyDiff::Mom_h) * fac_ptr[indx-1];
353  mu_turb(i,j,k,indx_v) = mu_turb(i,j,k,EddyDiff::Mom_v) * fac_ptr[indx-1];
354  }
355  });
356  break;
357  }
358  }
359  }
360 }
@ tau12
Definition: ERF_DataStruct.H:51
@ tau23
Definition: ERF_DataStruct.H:51
@ tau33
Definition: ERF_DataStruct.H:51
@ tau22
Definition: ERF_DataStruct.H:51
@ tau11
Definition: ERF_DataStruct.H:51
@ tau13
Definition: ERF_DataStruct.H:51
@ m_y
Definition: ERF_DataStruct.H:41
@ m_x
Definition: ERF_DataStruct.H:40
AMREX_GPU_DEVICE AMREX_FORCE_INLINE amrex::Real ComputeSmnSmn2D(int &i, int &j, int &k, const amrex::Array4< amrex::Real const > &tau11, const amrex::Array4< amrex::Real const > &tau22, const amrex::Array4< amrex::Real const > &tau12)
Definition: ERF_EddyViscosity.H:297
StrainArrays strain_arrays(amrex::Vector< std::unique_ptr< amrex::MultiFab >> &Tau_lev, const amrex::MFIter &mfi)
Definition: ERF_EddyViscosity.H:164
AMREX_GPU_DEVICE AMREX_FORCE_INLINE amrex::Real ComputeSmnSmn(int &i, int &j, int &k, const amrex::Array4< amrex::Real const > &tau11, const amrex::Array4< amrex::Real const > &tau22, const amrex::Array4< amrex::Real const > &tau33, const amrex::Array4< amrex::Real const > &tau12, const amrex::Array4< amrex::Real const > &tau13, const amrex::Array4< amrex::Real const > &tau23)
Definition: ERF_EddyViscosity.H:133
#define Rho_comp
Definition: ERF_IndexDefines.H:61
#define RhoTheta_comp
Definition: ERF_IndexDefines.H:62
#define RhoKE_comp
Definition: ERF_IndexDefines.H:63
amrex::GpuArray< Real, AMREX_SPACEDIM > dxInv
Definition: ERF_InitCustomPertVels_ParticleTests.H:16
const Real length
Definition: ERF_InitCustomPert_AnelasticWallDiffusion.H:16
amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > LengthScaleInvCellSize(const amrex::Geometry &geom)
Definition: ERF_LengthScaleCellSize.H:49
AMREX_GPU_DEVICE AMREX_FORCE_INLINE amrex::Real GetThetav(const int &i, const int &j, const int &k, const amrex::Array4< amrex::Real const > &cell_data, const MoistureComponentIndices &moisture_indices)
Definition: ERF_MoistUtils.H:93
AMREX_GPU_DEVICE AMREX_FORCE_INLINE amrex::Real GetThetal(const int &i, const int &j, const int &k, const amrex::Array4< amrex::Real const > &cell_data, const MoistureComponentIndices &moisture_indices)
Definition: ERF_MoistUtils.H:127
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_FORCE_INLINE IntVect offset(const int face_dir, const int normal)
Definition: ERF_ReadBndryPlanes.cpp:36
AMREX_GPU_DEVICE AMREX_FORCE_INLINE amrex::Real StabilityFunction(amrex::Real Ri, amrex::Real Ri_crit)
Definition: ERF_RichardsonNumber.H:522
AMREX_GPU_DEVICE AMREX_FORCE_INLINE amrex::Real ComputeVerticalShear2(int i, int j, int k, amrex::Real dzInv, const amrex::Array4< const amrex::Real > &u, const amrex::Array4< const amrex::Real > &v)
Definition: ERF_RichardsonNumber.H:366
AMREX_GPU_DEVICE AMREX_FORCE_INLINE amrex::Real ComputeRichardson(amrex::Real N2_moist, amrex::Real S2_vert)
Definition: ERF_RichardsonNumber.H:506
AMREX_GPU_DEVICE AMREX_FORCE_INLINE amrex::Real ComputeN2(int i, int j, int k, amrex::Real dzInv, amrex::Real const_grav, const amrex::Array4< const amrex::Real > &cell_data, const MoistureComponentIndices &moisture_indices)
Definition: ERF_RichardsonNumber.H:130
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real Smag2DLimitedViscosity(amrex::Real nu, amrex::Real strain, amrex::Real DeltaH, amrex::Real alpha, amrex::Real kh_cap, bool use_slope) noexcept
Definition: ERF_TerrainDiffusionLimits.H:110
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real TerrainSlopeFactor(const TerrainCellDrops &d) noexcept
Definition: ERF_TerrainDiffusionLimits.H:78
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE TerrainCellDrops ComputeTerrainCellDrops(int i, int j, int k, const amrex::Array4< const amrex::Real > &z_nd) noexcept
Definition: ERF_TerrainDiffusionLimits.H:44
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
@ Theta_v
Definition: ERF_IndexDefines.H:267
@ Turb_lengthscale
Definition: ERF_IndexDefines.H:271
@ Mom_h
Definition: ERF_IndexDefines.H:261
@ Mom_v
Definition: ERF_IndexDefines.H:266
@ Theta_h
Definition: ERF_IndexDefines.H:262
@ KE_h
Definition: ERF_IndexDefines.H:263
@ rho
Definition: ERF_Kessler.H:25
@ alpha
Definition: ERF_SLM.H:153
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
Definition: ERF_EddyViscosity.H:158
amrex::Array4< amrex::Real const > tau23
Definition: ERF_EddyViscosity.H:159
amrex::Array4< amrex::Real const > tau13
Definition: ERF_EddyViscosity.H:159
amrex::Array4< amrex::Real const > tau12
Definition: ERF_EddyViscosity.H:159
amrex::Array4< amrex::Real const > tau11
Definition: ERF_EddyViscosity.H:159
amrex::Array4< amrex::Real const > tau22
Definition: ERF_EddyViscosity.H:159
amrex::Array4< amrex::Real const > tau33
Definition: ERF_EddyViscosity.H:159
StratType strat_type
Thermodynamic variable used for stability stratification.
Definition: ERF_TurbStruct.H:949
amrex::Real Ce
Deardorff dissipation coefficient.
Definition: ERF_TurbStruct.H:931
amrex::Real sigma_k
TKE diffusivity coefficient denominator.
Definition: ERF_TurbStruct.H:944
bool use_Ri_correction
Whether Richardson-number correction is applied.
Definition: ERF_TurbStruct.H:956
amrex::Real Ck
Deardorff eddy-viscosity coefficient.
Definition: ERF_TurbStruct.H:933
amrex::Real Ce_wall
Wall value for the Deardorff dissipation coefficient.
Definition: ERF_TurbStruct.H:932
amrex::Real theta_ref
Reference potential temperature for stable stratification.
Definition: ERF_TurbStruct.H:947
amrex::Real Sc_t_inv
Inverse turbulent Schmidt number.
Definition: ERF_TurbStruct.H:917
bool mix_isotropic
Whether subgrid mixing uses isotropic length scales.
Definition: ERF_TurbStruct.H:954
amrex::Real smag2d_kh_cap
Cap K_h <= smag2d_kh_cap * Delta_h for Smagorinsky-2D (m/s); 0 means no cap.
Definition: ERF_TurbStruct.H:925
amrex::Real Ri_crit
Critical Richardson number for stability correction.
Definition: ERF_TurbStruct.H:957
amrex::Real Cs
Smagorinsky model coefficient.
Definition: ERF_TurbStruct.H:920
bool smag2d_slope_limiter
Apply WRF's Smagorinsky-2D slope limiter (K_h / alpha or / alpha^2) on terrain-fitted meshes.
Definition: ERF_TurbStruct.H:924
amrex::Real Pr_t_inv
Inverse turbulent Prandtl number.
Definition: ERF_TurbStruct.H:913
bool smag2d
Whether the 2-D Smagorinsky formulation is used.
Definition: ERF_TurbStruct.H:921

Referenced by ComputeTurbulentViscosity().

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

◆ ComputeTurbulentViscosityLES_EB()

void ComputeTurbulentViscosityLES_EB ( Vector< std::unique_ptr< MultiFab >> &  Tau_lev,
const MultiFab &  cons_in,
MultiFab &  eddyViscosity,
MultiFab &  Hfx1,
MultiFab &  Hfx2,
MultiFab &  Hfx3,
const Geometry &  geom,
Vector< std::unique_ptr< MultiFab >> &  mapfac,
const TurbChoice &  turbChoice,
const Real  const_grav,
[[maybe_unused] ] const SolverChoice &  solverChoice,
std::unique_ptr< SurfaceLayer > &  ,
const MoistureComponentIndices &  moisture_indices,
const eb_ &  ebfact,
const MultiFab *  xvel,
const MultiFab *  yvel 
)
374 {
375  const GpuArray<Real, AMREX_SPACEDIM> cellSizeInv = geom.InvCellSizeArray();
376  // The filter widths take dx in place of a dy over one cell, and the reverse
377  const GpuArray<Real, AMREX_SPACEDIM> lenInv = LengthScaleInvCellSize(geom);
378  Real dxInv = lenInv[0];
379  Real dyInv = lenInv[1];
380  Real dzInv = cellSizeInv[2];
381 
382  const Box& domain = geom.Domain();
383 
384  Real inv_Pr_t = turbChoice.Pr_t_inv;
385  Real inv_Sc_t = turbChoice.Sc_t_inv;
386  Real inv_sigma_k = one / turbChoice.sigma_k;
387 
388  bool isotropic = turbChoice.mix_isotropic;
389 
390  AMREX_ASSERT(turbChoice.les_type == LESType::Smagorinsky);
391 
392  // SMAGORINSKY: Fill Kturb for momentum in horizontal and vertical
393  //***********************************************************************************
394  if (turbChoice.les_type == LESType::Smagorinsky)
395  {
396  Real Cs = turbChoice.Cs;
397 
398  // Define variables required inside device lambdas (scalars only)
399  Real l_abs_g = const_grav;
400  Real l_Ri_crit = turbChoice.Ri_crit;
401  bool l_use_Ri_corr = turbChoice.use_Ri_correction;
402  bool l_has_xvel = (xvel != nullptr);
403  bool l_has_yvel = (yvel != nullptr);
404 
405 #ifdef _OPENMP
406 #pragma omp parallel if (Gpu::notInLaunchRegion())
407 #endif
408  for (MFIter mfi(eddyViscosity,TilingIfNotGPU()); mfi.isValid(); ++mfi)
409  {
410  Box bxcc = mfi.growntilebox(1) & domain;
411  const Array4<Real>& mu_turb = eddyViscosity.array(mfi);
412  const Array4<Real>& hfx_x = Hfx1.array(mfi);
413  const Array4<Real>& hfx_y = Hfx2.array(mfi);
414  const Array4<Real>& hfx_z = Hfx3.array(mfi);
415  const Array4<Real const >& cell_data = cons_in.array(mfi);
416  const StrainArrays tau = strain_arrays(Tau_lev, mfi);
417  const Array4<Real const> tau11 = tau.tau11, tau22 = tau.tau22, tau33 = tau.tau33;
418  const Array4<Real const> tau12 = tau.tau12, tau13 = tau.tau13, tau23 = tau.tau23;
419  Array4<Real const> mf_mx = mapfac[MapFacType::m_x]->const_array(mfi);
420  Array4<Real const> mf_my = mapfac[MapFacType::m_y]->const_array(mfi);
421 
422  Array4<Real const> u_arr = (l_has_xvel) ? xvel->const_array(mfi) : Array4<Real const>{};
423  Array4<Real const> v_arr = (l_has_yvel) ? yvel->const_array(mfi) : Array4<Real const>{};
424 
425  Array4<const EBCellFlag> c_cflag = (ebfact.get_const_factory())->getMultiEBCellFlagFab()[mfi].const_array();
426  Array4<const EBCellFlag> u_cflag = (ebfact.get_u_const_factory())->getMultiEBCellFlagFab()[mfi].const_array();
427  Array4<const EBCellFlag> v_cflag = (ebfact.get_v_const_factory())->getMultiEBCellFlagFab()[mfi].const_array();
428  Array4<const EBCellFlag> w_cflag = (ebfact.get_w_const_factory())->getMultiEBCellFlagFab()[mfi].const_array();
429  Array4<const Real > u_vfrac = (ebfact.get_u_const_factory())->getVolFrac().const_array(mfi);
430  Array4<const Real > v_vfrac = (ebfact.get_v_const_factory())->getVolFrac().const_array(mfi);
431 
432  ParallelFor(bxcc, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
433  {
434  if (!c_cflag(i,j,k).isCovered()) {
435  // =====================================================================
436  // 1. STRAIN RATE MAGNITUDE CALCULATION
437  // =====================================================================
438  Real SmnSmn = zero;
439  if (c_cflag(i,j,k).isRegular()) {
440  SmnSmn = ComputeSmnSmn(i,j,k,tau11,tau22,tau33,tau12,tau13,tau23);
441  } else if (c_cflag(i,j,k).isSingleValued()) {
442  SmnSmn = ComputeSmnSmn_EB(i,j,k,tau11,tau22,tau33,tau12,tau13,tau23,c_cflag,u_cflag,v_cflag,w_cflag);
443  }
444  Real strain_rate_magnitude = std::sqrt(two * SmnSmn);
445 
446  // =====================================================================
447  // 2. GRID SCALE CALCULATION (filter width Δ)
448  // =====================================================================
449  Real Delta;
450  Real DeltaH;
451  if (isotropic) {
452  Real cellVolMsf = one / (dxInv * mf_mx(i,j,0) * dyInv * mf_my(i,j,0) * dzInv);
453  Delta = std::cbrt(cellVolMsf);
454  DeltaH = Delta;
455  } else {
456  Delta = one / dzInv;
457  DeltaH = std::sqrt(one / (dxInv * mf_mx(i,j,0) * dyInv * mf_my(i,j,0)));
458  }
459 
460  Real rho = cell_data(i, j, k, Rho_comp);
461  Real CsDeltaSqr_h = Cs * Cs * DeltaH * DeltaH;
462  Real CsDeltaSqr_v = Cs * Cs * Delta * Delta;
463 
464  Real nu_turb_base_h = CsDeltaSqr_h * strain_rate_magnitude;
465  Real nu_turb_base_v = CsDeltaSqr_v * strain_rate_magnitude;
466 
467  Real stability_factor = one;
468 
469  if (l_use_Ri_corr && l_has_xvel && l_has_yvel) {
470  Real N2 = zero;
471  Real S2_vert = zero;
472  // Covered and multi-valued cells fall through both branches below
473  // with no shear computed, so start from "not valid" and let the
474  // branch that does the work say otherwise.
475  bool shear_is_valid = false;
476  if (c_cflag(i,j,k).isRegular()) {
477  N2 = ComputeN2(i, j, k, dzInv, l_abs_g, cell_data, moisture_indices);
478  S2_vert = ComputeVerticalShear2(i, j, k, dzInv, u_arr, v_arr);
479  shear_is_valid = true;
480  } else if (c_cflag(i,j,k).isSingleValued()) {
481  N2 = ComputeN2_EB(i, j, k, c_cflag, dzInv, l_abs_g, cell_data, moisture_indices);
482  S2_vert = ComputeVerticalShear2_EB(i, j, k, c_cflag, u_vfrac, v_vfrac, dzInv, u_arr, v_arr, shear_is_valid);
483  }
484  // When the EB geometry leaves no usable shear stencil we keep
485  // stability_factor = 1, i.e. no stratification damping: without a
486  // trustworthy gradient we would rather leave the Smagorinsky
487  // viscosity alone than suppress it on the strength of a Ri we do
488  // not believe. This is a deliberate choice -- damping to 0 instead
489  // would be just as defensible -- so change it knowingly.
490  if (shear_is_valid) {
491  Real Ri = ComputeRichardson(N2, S2_vert);
492  stability_factor = StabilityFunction(Ri, l_Ri_crit);
493  }
494  }
495 
496  if (isotropic) {
497  mu_turb(i, j, k, EddyDiff::Mom_h) = rho * nu_turb_base_h * stability_factor;
498  mu_turb(i, j, k, EddyDiff::Mom_v) = rho * nu_turb_base_v * stability_factor;
499  } else {
500  mu_turb(i, j, k, EddyDiff::Mom_h) = rho * nu_turb_base_h;
501  mu_turb(i, j, k, EddyDiff::Mom_v) = rho * nu_turb_base_v * stability_factor;
502  }
503 
504  Real dtheta_dz = zero;
505 
506  if (c_cflag(i,j,k).isSingleValued() && c_cflag(i,j,k+1).isCovered()) {
507  amrex::Real theta = cell_data(i, j, k, RhoTheta_comp) / rho;
508  amrex::Real theta_km1 = cell_data(i,j,k-1,RhoTheta_comp)/cell_data(i,j,k-1,Rho_comp);
509  amrex::Real theta_km2 = cell_data(i,j,k-2,RhoTheta_comp)/cell_data(i,j,k-2,Rho_comp);
510  dtheta_dz = (three*theta - Real(4.)*theta_km1 + theta_km2) * myhalf * dzInv;
511  } else if (c_cflag(i,j,k).isSingleValued() && c_cflag(i,j,k-1).isCovered()) {
512  amrex::Real theta = cell_data(i, j, k, RhoTheta_comp) / rho;
513  amrex::Real theta_kp1 = cell_data(i,j,k+1,RhoTheta_comp)/cell_data(i,j,k+1,Rho_comp);
514  amrex::Real theta_kp2 = cell_data(i,j,k+2,RhoTheta_comp)/cell_data(i,j,k+2,Rho_comp);
515  dtheta_dz = (-theta_kp2 + Real(4.)*theta_kp1 - three*theta) * myhalf * dzInv;
516  } else if (!c_cflag(i,j,k).isCovered()) {
517  amrex::Real theta_km1 = cell_data(i,j,k-1,RhoTheta_comp)/cell_data(i,j,k-1,Rho_comp);
518  amrex::Real theta_kp1 = cell_data(i,j,k+1,RhoTheta_comp)/cell_data(i,j,k+1,Rho_comp);
519  dtheta_dz = myhalf * (theta_kp1 - theta_km1) * dzInv;
520  }
521 
522  // The EB diffusion does not write hfx_z, so on EB terrain this is
523  // the only writer of the SGS heat flux output (the surface layer
524  // overwrites the bottom). Elsewhere the theta diffusion writes the
525  // face fluxes and the closures do not store a heat flux. This write
526  // keeps the z-nodal/cell-centered mismatch noted above; moving the EB
527  // heat-flux output to the EB face fluxes belongs with that diffusion.
528  hfx_x(i,j,k) = zero;
529  hfx_y(i,j,k) = zero;
530  hfx_z(i,j,k) = -inv_Pr_t * mu_turb(i,j,k,EddyDiff::Mom_v) * dtheta_dz;
531 
532  } else {
533 
534  hfx_x(i,j,k) = zero;
535  hfx_y(i,j,k) = zero;
536  hfx_z(i,j,k) = zero;
537  }// End of if not covered
538  });
539  }
540  }
541 
542  // Extrapolate Kturb in x/y, fill remaining elements (relevant to lev==0)
543  //***********************************************************************************
544  int ngc(1);
545  // EddyDiff mapping : Theta_h KE_h Scalar_h Q_h
546  Vector<Real> Factors = {inv_Pr_t, inv_sigma_k, inv_Sc_t, inv_Sc_t}; // alpha = mu/Pr
547  Gpu::AsyncVector<Real> d_Factors; d_Factors.resize(Factors.size());
548  Gpu::copy(Gpu::hostToDevice, Factors.begin(), Factors.end(), d_Factors.begin());
549  Real* fac_ptr = d_Factors.data();
550 
551  const bool use_KE = ( turbChoice.les_type == LESType::Deardorff );
552 
553 #ifdef _OPENMP
554 #pragma omp parallel if (Gpu::notInLaunchRegion())
555 #endif
556  for ( MFIter mfi(eddyViscosity,TilingIfNotGPU()); mfi.isValid(); ++mfi)
557  {
558  Box bxcc = mfi.tilebox();
559  Box planex = bxcc; planex.setSmall(0, 1); planex.setBig(0, ngc); planex.grow(1,1);
560  Box planey = bxcc; planey.setSmall(1, 1); planey.setBig(1, ngc); planey.grow(0,1);
561  //
562  // NOTE: growntilebox, not the tilebox grown by hand. Growing by hand pushes every tile
563  // ngc cells past its own share of the grid, so once the grid is tiled two tiles
564  // store to the same mu_turb cells -- a data race under OpenMP, even though both
565  // store the same value. The union of the tiles, and the values stored, are
566  // unchanged, and this is identical to the old expression with one tile per grid.
567  //
568  bxcc = mfi.growntilebox(IntVect(ngc,ngc,0));
569 
570  const Array4<Real>& mu_turb = eddyViscosity.array(mfi);
571 
572  for (auto n = 1; n < EddyDiff::Mom_v; ++n) {
573  int offset = EddyDiff::Mom_v;
574  switch (n)
575  {
576  case EddyDiff::KE_h:
577  if (use_KE) {
578  ParallelFor(bxcc, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
579  {
580  int indx = n;
581  int indx_v = indx + offset;
582  mu_turb(i,j,k,indx) = mu_turb(i,j,k,EddyDiff::Mom_h) * fac_ptr[indx-1];
583  mu_turb(i,j,k,indx_v) = mu_turb(i,j,k,EddyDiff::Mom_v) * fac_ptr[indx-1];
584  });
585  }
586  break;
587  default:
588  ParallelFor(bxcc, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
589  {
590  int indx = n;
591  int indx_v = indx + offset;
592 
593  // NOTE: Theta_h, Theta_v have already been set for Deardorff
594  if (!(indx_v == EddyDiff::Theta_v && use_KE)) {
595  mu_turb(i,j,k,indx) = mu_turb(i,j,k,EddyDiff::Mom_h) * fac_ptr[indx-1];
596  mu_turb(i,j,k,indx_v) = mu_turb(i,j,k,EddyDiff::Mom_v) * fac_ptr[indx-1];
597  }
598  });
599  break;
600  }
601  }
602  }
603 }
AMREX_GPU_DEVICE AMREX_FORCE_INLINE amrex::Real ComputeSmnSmn_EB(int &i, int &j, int &k, const amrex::Array4< amrex::Real const > &tau11, const amrex::Array4< amrex::Real const > &tau22, const amrex::Array4< amrex::Real const > &tau33, const amrex::Array4< amrex::Real const > &tau12, const amrex::Array4< amrex::Real const > &tau13, const amrex::Array4< amrex::Real const > &tau23, const amrex::Array4< const amrex::EBCellFlag > &c_cflag, const amrex::Array4< const amrex::EBCellFlag > &u_cflag, const amrex::Array4< const amrex::EBCellFlag > &v_cflag, const amrex::Array4< const amrex::EBCellFlag > &w_cflag)
Definition: ERF_EddyViscosity.H:192
constexpr amrex::Real three
Definition: ERF_NumericalConstants.H:39
AMREX_GPU_DEVICE AMREX_FORCE_INLINE amrex::Real ComputeN2_EB(int i, int j, int k, const amrex::Array4< const amrex::EBCellFlag > &c_cflag, amrex::Real dzInv, amrex::Real const_grav, const amrex::Array4< const amrex::Real > &cell_data, const MoistureComponentIndices &moisture_indices)
Definition: ERF_RichardsonNumber.H:192
AMREX_GPU_DEVICE AMREX_FORCE_INLINE amrex::Real ComputeVerticalShear2_EB(int i, int j, int k, const amrex::Array4< const amrex::EBCellFlag > &c_cflag, const amrex::Array4< const amrex::Real > &u_vfrac, const amrex::Array4< const amrex::Real > &v_vfrac, amrex::Real dzInv, const amrex::Array4< const amrex::Real > &u, const amrex::Array4< const amrex::Real > &v, bool &shear_is_valid)
Definition: ERF_RichardsonNumber.H:403
const std::unique_ptr< amrex::EBFArrayBoxFactory > & get_const_factory() const noexcept
Return the cell-centered EB factory.
Definition: ERF_EB.H:110
amrex::EBFArrayBoxFactory const * get_w_const_factory() const noexcept
Return the native AMReX z-face EB factory.
Definition: ERF_EB.H:136
amrex::EBFArrayBoxFactory const * get_u_const_factory() const noexcept
Return the native AMReX x-face EB factory.
Definition: ERF_EB.H:132
amrex::EBFArrayBoxFactory const * get_v_const_factory() const noexcept
Return the native AMReX y-face EB factory.
Definition: ERF_EB.H:134
@ theta
Definition: ERF_Kessler.H:26

Referenced by ComputeTurbulentViscosity().

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

◆ ComputeTurbulentViscosityRANS()

void ComputeTurbulentViscosityRANS ( int  level,
Vector< std::unique_ptr< MultiFab >> &  Tau_lev,
const MultiFab &  cons_in,
const MultiFab &  wdist,
MultiFab &  eddyViscosity,
MultiFab &  Diss,
const Geometry &  geom,
bool  use_terrain_fitted_coords,
const std::unique_ptr< MultiFab > &  z_phys_nd,
const TurbChoice &  turbChoice,
const Real  const_grav,
std::unique_ptr< SurfaceLayer > &  SurfLayer,
const MultiFab *  z_0,
const CanopyTurbulence &  canopy,
const MultiFab *  forest_drag,
const MultiFab *  ib_blank,
bool  flat_wall_diss,
Real  ib_small_volfrac,
Real  ib_z0,
const MultiFab *  xvel,
const MultiFab *  yvel 
)

Function for computing the eddy viscosity with RANS.

Parameters
[in]Tau_levstrain at this level (the realizability cap reads S_ij)
[in]cons_incell center conserved quantities
[in]wdistwall distance
[out]eddyViscosityturbulent viscosity
[out]Dissdissipation of turbulent kinetic energy
[in]geomproblem geometry
[in]use_terrain_fitted_coordsflag for terrain-fitted coordinates
[in]z_phys_ndnodal physical z coordinates
[in]turbChoicecontainer with turbulence parameters
[in]const_gravgravitational acceleration
[in]SurfLayeroptional surface-layer model
[in]z_0roughness length
[in]canopycanopy terms of the k-eqn model (erf.forest_canopy_*)
[in]forest_dragforest drag field C_d LAD, or null without a forest
[in]ib_blanksolid fraction for the fraction-stress immersed wall law, or null without it
[in]flat_wall_disspartial wall cells dissipate with flat ground's first-cell length (erf.if_flat_wall_dissipation)
[in]ib_small_volfracsolid fractions below this count as fluid (eb2.small_volfrac)
[in]ib_z0roughness length of the immersed wall law (erf.if_z0), used without a surface layer
658 {
659  const GpuArray<Real, AMREX_SPACEDIM> cellSizeInv = geom.InvCellSizeArray();
660  const bool use_SurfLayer = (SurfLayer != nullptr);
661 
662  // Optional cap of the geometric length from the diagnosed PBL height
663  const bool lscale_from_pblh = turbChoice.rans_lscale_from_pblh;
664  if (lscale_from_pblh) {
665  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(use_SurfLayer && SurfLayer->computes_pblh(),
666  "erf.rans_lscale_from_pblh needs zlo.type = surface_layer and erf.most.pblh_calc = MYNN25");
667  }
668  const MultiFab* pblh_mf = (lscale_from_pblh) ? SurfLayer->get_pblh(level) : nullptr;
669 
670  Real inv_Pr_t = turbChoice.Pr_t_inv;
671  Real inv_Sc_t = turbChoice.Sc_t_inv;
672  Real inv_sigma_k = one / turbChoice.sigma_k;
673 
674  // One-Equation k model (Axell & Liungman 2001, Environ Fluid Mech)
675  //***********************************************************************************
676  if (turbChoice.rans_type == RANSType::kEqn)
677  {
678  const Real Cmu0 = turbChoice.Cmu0;
679  const Real Cmu0_pow3 = Cmu0 * Cmu0 * Cmu0;
680  const Real inv_Cb_sq = one / (turbChoice.Cb * turbChoice.Cb);
681  const Real Rt_crit = turbChoice.Rt_crit;
682  const Real Rt_min = turbChoice.Rt_min;
683  const Real l_g_max = turbChoice.l_g_max;
684  const Real l_min = turbChoice.rans_lscale_min;
685  const Real abs_g = const_grav;
686  // floor on k: erf.tke_floor if set, otherwise machine epsilon
687  const Real tke_floor = amrex::max(turbChoice.tke_floor, std::numeric_limits<Real>::epsilon());
688 
689  // Canopy cap of the turbulent length scale, L <= alpha / (C_d LAD)
690  const bool use_canopy_length = canopy.enabled && (canopy.length_alpha > zero);
691  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(!use_canopy_length || forest_drag,
692  "erf.forest_canopy_tke needs the forest drag field");
693  const Real canopy_alpha = canopy.length_alpha;
694  // Realizability cap nu_t <= C_r k / S (AL01::realizable_nut); off at 0
695  const Real Cr = turbChoice.rans_realizability;
696  const bool use_cap = (Cr > zero);
697  // The S of the cap with the log-consistent vertical shear (ERF_LogShear.H), as in the
698  // shear production of k
699  const bool cap_log_shear = use_cap && (turbChoice.rans_shear_stencil == RANSShearStencil::log_law);
700  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(!cap_log_shear || (use_SurfLayer && xvel && yvel),
701  "erf.rans_shear_stencil = log_law needs zlo.type = surface_layer and the velocities of the strain");
702  const int dom_klo = geom.Domain().smallEnd(2);
703  const int dom_khi = geom.Domain().bigEnd(2);
704 
705  // Clamped divisor: the select is if-converted, so 1/theta_ref runs even
706  // when theta_ref = 0 and would trip fpe_trap_zero (see ERF_SetupDiff.H)
707  const bool use_ref_theta = (turbChoice.theta_ref > 0);
708  const Real inv_theta_ref = one / amrex::max(turbChoice.theta_ref, std::numeric_limits<Real>::min());
709  const Real inv_theta0 = (use_ref_theta) ? inv_theta_ref : one;
710 
711 #ifdef _OPENMP
712 #pragma omp parallel if (Gpu::notInLaunchRegion())
713 #endif
714  for ( MFIter mfi(eddyViscosity,TilingIfNotGPU()); mfi.isValid(); ++mfi)
715  {
716  Box bxcc = mfi.tilebox();
717 
718  const Array4<Real const>& d_arr = wdist.const_array(mfi);
719  const Array4<Real const>& z0_arr = (use_SurfLayer) ? z_0->const_array(mfi) : Array4<Real const>{};
720  const Array4<Real const>& pblh_arr = (pblh_mf) ? pblh_mf->const_array(mfi) : Array4<Real const>{};
721  const Array4<Real const>& f_drag_arr = (use_canopy_length) ? forest_drag->const_array(mfi)
722  : Array4<Real const>{};
723 
724  // NOTE: the closures deliberately store no subgrid heat flux. hfx_z (Hfx3)
725  // is z-nodal, (0,0,1), while the closure's -K dtheta/dz is cell-centered,
726  // and the theta diffusion of every RK stage overwrites all z-faces (the
727  // surface layer the bottom face) before anything reads them, so the value
728  // written here was both misplaced and unused. The TKE buoyancy source
729  // reads the face fluxes of that diffusion (ERF_TurbKESources.H).
730  const Array4<Real>& mu_turb = eddyViscosity.array(mfi);
731  const Array4<Real>& diss = Diss.array(mfi);
732 
733  const Array4<Real const>& cell_data = cons_in.array(mfi);
734 
735  const Array4<Real const>& z_nd_arr = z_phys_nd->const_array(mfi);
736  // fraction-stress immersed wall law: solid fraction, to find the wall cell
737  const Array4<Real const>& ib_arr = (ib_blank) ? ib_blank->const_array(mfi) : Array4<Real const>{};
738  const int ib_klo = geom.Domain().smallEnd(2);
739  const int ib_khi = geom.Domain().bigEnd(2);
740  const Real ib_small = ib_small_volfrac;
741 
742  // Strain components for the cap only; the arrays stay empty otherwise. The k-eqn
743  // closure cannot run with EB terrain (SolverChoice stops the run), so the plain
744  // ComputeSmnSmn is the right form here.
745  const StrainArrays tau = (use_cap) ? strain_arrays(Tau_lev, mfi) : StrainArrays{};
746  const Array4<Real const>& u_arr = (cap_log_shear) ? xvel->const_array(mfi) : Array4<Real const>{};
747  const Array4<Real const>& v_arr = (cap_log_shear) ? yvel->const_array(mfi) : Array4<Real const>{};
748 
749  ParallelFor(bxcc, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
750  {
752  Real tke = amrex::max(cell_data(i,j,k,RhoKE_comp)/cell_data(i,j,k,Rho_comp), tke_floor);
753 
754  // Estimate stratification
755  Real dzInv = cellSizeInv[2];
756  if (use_terrain_fitted_coords) {
757  // the terrain grid is only deformed in z for now
758  dzInv /= Compute_h_zeta_AtCellCenter(i,j,k, cellSizeInv, z_nd_arr);
759  }
760  // In the wall cell of the fraction-stress immersed wall law the cell below is
761  // solid: read the wall cell itself there, as the first-order extrapolated ghost
762  // of the zlo face does on flat ground, not the solid's temperature
763  int km1 = k-1;
764  if (ib_arr) {
765  const Real b_m = if_rans_beta(ib_arr, i, j, k-1, ib_klo, ib_khi, ib_small);
766  const Real b_0 = if_rans_beta(ib_arr, i, j, k, ib_klo, ib_khi, ib_small);
767  const Real b_p = if_rans_beta(ib_arr, i, j, k+1, ib_klo, ib_khi, ib_small);
768  if (ib_wall::is_wall_cell(b_m, b_0, b_p, ib_small)) { km1 = k; }
769  }
770  Real dtheta_dz = myhalf * ( cell_data(i,j,k+1,RhoTheta_comp)/cell_data(i,j,k+1,Rho_comp)
771  - cell_data(i,j,km1,RhoTheta_comp)/cell_data(i,j,km1,Rho_comp) )*dzInv;
772  Real N2 = abs_g * inv_theta0 * dtheta_dz; // Brunt–Väisälä frequency squared
773  if (!use_ref_theta) {
774  // inv_theta0 == 1, divide by actual theta
775  N2 *= cell_data(i,j,k,Rho_comp) /
776  cell_data(i,j,k,RhoTheta_comp);
777  }
778 
779  // Geometric length scale (AL01, Eqn. 22)
780  // the surface layer's roughness, or the immersed wall law's (fraction_stress) when
781  // there is no surface layer at the bottom
782  Real l_g = (z0_arr) ? KAPPA * (d_arr(i, j, k) + z0_arr(i, j, 0))
783  : (ib_arr) ? KAPPA * (d_arr(i, j, k) + ib_z0)
784  : KAPPA * d_arr(i, j, k);
785 
786  // Enforce a maximum value: fixed, or kappa * 0.1 * zi clamped to
787  // [rans_lscale_min, max_geom_lscale] when the PBL height is diagnosed
788  Real l_cap = l_g_max;
789  if (pblh_arr) {
790  l_cap = amrex::min(l_g_max, amrex::max(l_min, KAPPA * Real(0.1) * pblh_arr(i,j,0)));
791  }
792  l_g = AL01::geom_length(l_g, l_cap);
793 
794  // Turbulent length scale (neutral / stable Eq. 26 / unstable Eq. 28,
795  // the latter bounded through the smoothed Rt)
796  Real length = AL01::turb_length(l_g, N2, tke, Cmu0_pow3, inv_Cb_sq, Rt_crit, Rt_min, eps);
797  // In the canopy the eddies are limited by the foliage: cap L before the
798  // dissipation, Rt and the stability functions are evaluated from it
799  if (f_drag_arr) {
800  length = canopy_turbulence::limited_length(length, f_drag_arr(i,j,k), canopy_alpha);
801  }
802  mu_turb(i, j, k, EddyDiff::Turb_lengthscale) = length;
803 
804  // Dissipation rate (AL01, Eqn. 19)
805  diss(i, j, k) = AL01::dissipation(cell_data(i, j, k, Rho_comp), Cmu0_pow3, tke, length);
806 
807  // erf.if_flat_wall_dissipation (kynema-sgf KLAxell.flat_wall_dissipation): a partial
808  // wall cell of the fraction-stress law has its length at the fluid centroid,
809  // (1 - beta) dz / 2 from the surface, and a nearly solid one then dissipates TKE
810  // several times faster than flat ground's first cell, faster than the wall value
811  // restores it. Keep the centroid length for the viscosity, but dissipate with the
812  // length the same formula gives at dz / 2, the distance of flat ground's first cell
813  if (flat_wall_diss && ib_arr) {
814  const Real bk = ib_arr(i,j,k);
815  if (bk >= ib_small && bk < one - ib_small) { // a partial cell as ib_wall::is_wall_cell sees it
816  Real l_flat = (z0_arr) ? KAPPA * (myhalf / cellSizeInv[2] + z0_arr(i, j, 0))
817  : KAPPA * (myhalf / cellSizeInv[2] + ib_z0);
818  l_flat = AL01::geom_length(l_flat, l_cap);
819  const Real len_flat = AL01::turb_length(l_flat, N2, tke, Cmu0_pow3, inv_Cb_sq, Rt_crit, Rt_min, eps);
820  diss(i, j, k) = AL01::dissipation(cell_data(i, j, k, Rho_comp), Cmu0_pow3, tke, len_flat);
821  }
822  }
823 
824  // Turbulent Richardson number (AL01, Eqn. 29 combined with Eqn. 19),
825  // smoothed below Rt_crit (Burchard & Petersen)
826  Real Rt = AL01::smooth_Rt(AL01::richardson(length, N2, tke, Cmu0_pow3), Rt_crit, Rt_min);
827 
828  // Stability functions (AL01, Eqns. 31 and 32), using the smoothed Rt
829  Real cmu = AL01::cmu(Rt, Cmu0);
830  Real cmu_prime = AL01::cmu_prime(Rt, Cmu0);
831  mu_turb(i, j, k, EddyDiff::RANS_Rt) = Rt;
832  mu_turb(i, j, k, EddyDiff::RANS_cmu) = cmu;
833  mu_turb(i, j, k, EddyDiff::RANS_cmu_prime) = cmu_prime;
834 
835  // Calculate eddy diffusivities
836  // K = rho * nu_t = rho * c_mu * tke^(1/2) * length
837  Real nut = cmu * std::sqrt(tke) * length; // eddy viscosity
838 
839  // Realizability cap nu_t <= C_r k / S; cap_ratio = nu_t / (C_r k / S)
840  Real cap_ratio = zero;
841  if (use_cap) {
842  Real SmnSmn = ComputeSmnSmn(i,j,k,tau.tau11,tau.tau22,tau.tau33,tau.tau12,tau.tau13,tau.tau23);
843  if (cap_log_shear && k > dom_klo && k < dom_khi) {
844  SmnSmn = log_shear::corrected_SmnSmn(i, j, k, SmnSmn, u_arr, v_arr, tau.tau13, tau.tau23,
845  d_arr, z0_arr(i,j,0), z_nd_arr);
846  }
847  Real S = std::sqrt(two * SmnSmn);
848  nut = AL01::realizable_nut(nut, tke, S, Cr, cap_ratio);
849  }
850  mu_turb(i, j, k, EddyDiff::RANS_cap_ratio) = cap_ratio;
851 
852  Real nut_prime = cmu_prime / cmu * nut; // eddy diffusivity, capped with nu_t
853  mu_turb(i, j, k, EddyDiff::Mom_h) = cell_data(i, j, k, Rho_comp) * nut;
854  mu_turb(i, j, k, EddyDiff::Mom_v) = mu_turb(i, j, k, EddyDiff::Mom_h);
855  mu_turb(i, j, k, EddyDiff::Theta_v) = cell_data(i, j, k, Rho_comp) * nut_prime;
856  });
857  }
858  }
859 
860  // Fill the remaining eddy-diffusivity components from Mom_h. Ghost cells
861  // are filled afterwards by the FillBoundary and physical-boundary
862  // extrapolation in ComputeTurbulentViscosity, so only the valid cells are
863  // set here.
864  //***********************************************************************************
865  // EddyDiff mapping : Theta_h KE_h Scalar_h Q_h
866  Vector<Real> Factors = {inv_Pr_t, inv_sigma_k, inv_Sc_t, inv_Sc_t}; // alpha = mu/Pr
867  Gpu::AsyncVector<Real> d_Factors; d_Factors.resize(Factors.size());
868  Gpu::copy(Gpu::hostToDevice, Factors.begin(), Factors.end(), d_Factors.begin());
869  Real* fac_ptr = d_Factors.data();
870 
871  const bool use_KE = ( turbChoice.rans_type == RANSType::kEqn );
872  // With erf.rans_consistent_diffusivities every heat, scalar and moisture
873  // diffusivity is rho * cmu' * sqrt(k) * L, i.e. the Theta_v value set by
874  // the closure; without it the horizontal heat and all scalar diffusivities
875  // are the eddy viscosity over Pr_t / Sc_t (the historical behavior).
876  const bool consistent = use_KE && turbChoice.rans_consistent_diffusivities;
877 
878 #ifdef _OPENMP
879 #pragma omp parallel if (Gpu::notInLaunchRegion())
880 #endif
881  for ( MFIter mfi(eddyViscosity,TilingIfNotGPU()); mfi.isValid(); ++mfi)
882  {
883  Box bxcc = mfi.tilebox();
884 
885  const Array4<Real>& mu_turb = eddyViscosity.array(mfi);
886 
887  for (auto n = 1; n < EddyDiff::Mom_v; ++n) {
888  int offset = EddyDiff::Mom_v;
889  switch (n)
890  {
891  case EddyDiff::KE_h:
892  if (use_KE) {
893  ParallelFor(bxcc, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
894  {
895  int indx = n;
896  int indx_v = indx + offset;
897  mu_turb(i,j,k,indx) = mu_turb(i,j,k,EddyDiff::Mom_h) * fac_ptr[indx-1];
898  mu_turb(i,j,k,indx_v) = mu_turb(i,j,k,indx);
899  });
900  }
901  break;
902  default:
903  ParallelFor(bxcc, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
904  {
905  int indx = n;
906  int indx_v = indx + offset;
907 
908  if (consistent) {
909  mu_turb(i,j,k,indx) = mu_turb(i,j,k,EddyDiff::Theta_v);
910  if (indx_v != EddyDiff::Theta_v) {
911  mu_turb(i,j,k,indx_v) = mu_turb(i,j,k,EddyDiff::Theta_v);
912  }
913  } else {
914  mu_turb(i,j,k,indx) = mu_turb(i,j,k,EddyDiff::Mom_h) * fac_ptr[indx-1];
915 
916  // NOTE: Theta_v has already been set for the closure
917  if (!(indx_v == EddyDiff::Theta_v && use_KE)) {
918  mu_turb(i,j,k,indx_v) = mu_turb(i,j,k,indx);
919  }
920  }
921  });
922  break;
923  }
924  }
925  }
926 }
constexpr amrex::Real KAPPA
Definition: ERF_Constants.H:71
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(axis >=0 &&axis< AMREX_SPACEDIM, "prob.axis must be 0, 1 or 2")
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real cmu_prime(amrex::Real Rt, amrex::Real Cmu0)
Definition: ERF_RANSClosure.H:128
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real turb_length(amrex::Real l_g, amrex::Real N2, amrex::Real tke, amrex::Real Cmu0_pow3, amrex::Real inv_Cb_sq, amrex::Real Rt_crit, amrex::Real Rt_min, amrex::Real eps)
Definition: ERF_RANSClosure.H:70
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real richardson(amrex::Real length, amrex::Real N2, amrex::Real tke, amrex::Real Cmu0_pow3)
Definition: ERF_RANSClosure.H:111
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real realizable_nut(amrex::Real nut, amrex::Real tke, amrex::Real S, amrex::Real Cr, amrex::Real &ratio)
Definition: ERF_RANSClosure.H:179
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real cmu(amrex::Real Rt, amrex::Real Cmu0)
Definition: ERF_RANSClosure.H:120
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real smooth_Rt(amrex::Real Rt, amrex::Real Rt_crit, amrex::Real Rt_min)
Definition: ERF_RANSClosure.H:40
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real geom_length(amrex::Real l_g_raw, amrex::Real l_g_max)
Definition: ERF_RANSClosure.H:25
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real dissipation(amrex::Real rho, amrex::Real Cmu0_pow3, amrex::Real tke, amrex::Real length)
Definition: ERF_RANSClosure.H:102
@ RANS_Rt
Definition: ERF_IndexDefines.H:283
@ RANS_cap_ratio
Definition: ERF_IndexDefines.H:288
@ RANS_cmu
Definition: ERF_IndexDefines.H:284
@ RANS_cmu_prime
Definition: ERF_IndexDefines.H:285
constexpr int S
Definition: ERF_TwoStreamColumn.H:654
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real limited_length(amrex::Real length, amrex::Real f_drag, amrex::Real alpha)
Definition: ERF_CanopyTurbulence.H:54
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool is_wall_cell(amrex::Real b_km1, amrex::Real b_k, amrex::Real b_kp1, amrex::Real small) noexcept
Definition: ERF_ImmersedWallCell.H:57
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real corrected_SmnSmn(int i, int j, int k, amrex::Real SmnSmn, const amrex::Array4< amrex::Real const > &u, const amrex::Array4< amrex::Real const > &v, const amrex::Array4< amrex::Real const > &s13, const amrex::Array4< amrex::Real const > &s23, const amrex::Array4< amrex::Real const > &d, amrex::Real z0, const amrex::Array4< amrex::Real const > &z_nd) noexcept
Definition: ERF_LogShear.H:102
amrex::Real length_alpha
Definition: ERF_CanopyTurbStruct.H:25
bool enabled
erf.forest_canopy_tke: add the canopy terms to the k-eqn RANS model
Definition: ERF_CanopyTurbStruct.H:18
bool rans_consistent_diffusivities
Definition: ERF_TurbStruct.H:976
RANSShearStencil rans_shear_stencil
Definition: ERF_TurbStruct.H:994
amrex::Real l_g_max
Maximum geometric length scale.
Definition: ERF_TurbStruct.H:940
amrex::Real tke_floor
Definition: ERF_TurbStruct.H:967
amrex::Real rans_realizability
Definition: ERF_TurbStruct.H:989
bool rans_lscale_from_pblh
Definition: ERF_TurbStruct.H:981
amrex::Real rans_lscale_min
Definition: ERF_TurbStruct.H:982
amrex::Real Rt_min
Minimum turbulent Reynolds number.
Definition: ERF_TurbStruct.H:939
amrex::Real Cb
One-equation RANS buoyancy coefficient.
Definition: ERF_TurbStruct.H:937
amrex::Real Rt_crit
Critical turbulent Reynolds number.
Definition: ERF_TurbStruct.H:938
amrex::Real Cmu0
One-equation RANS Cmu0 coefficient.
Definition: ERF_TurbStruct.H:936

Referenced by ComputeTurbulentViscosity().

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