ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_MakeMomSources.cpp File Reference
#include <AMReX_MultiFab.H>
#include <AMReX_ArrayLim.H>
#include <AMReX_BCRec.H>
#include <AMReX_TableData.H>
#include <AMReX_GpuContainers.H>
#include <limits>
#include "ERF_ImmersedForcing.H"
#include "ERF_NumericalDiffusion.H"
#include "ERF_PlaneAverage.H"
#include "ERF_TI_slow_headers.H"
#include "ERF_SrcHeaders.H"
#include "ERF_Utils.H"
Include dependency graph for ERF_MakeMomSources.cpp:

Functions

void make_mom_sources (int level, double time_d, double dt, const Vector< MultiFab > &S_data, const MultiFab *z_phys_nd, const MultiFab *z_phys_cc, Vector< Real > &stretched_dz_h, const MultiFab &xvel, const MultiFab &yvel, const MultiFab &wvel, MultiFab &xmom_src, MultiFab &ymom_src, MultiFab &zmom_src, const MultiFab &base_state, MultiFab *forest_drag, MultiFab *terrain_blank, MultiFab *terrain_blank_xface, MultiFab *terrain_blank_yface, MultiFab *terrain_blank_zface, MultiFab *cosPhi_mf, MultiFab *sinPhi_mf, const Geometry geom, const SolverChoice &solverChoice, Vector< std::unique_ptr< MultiFab >> &, const Real *dptr_u_geos, const Real *dptr_v_geos, const Real *dptr_wbar_sub, const Vector< Real * > d_rayleigh_ptrs_at_lev, const amrex::Real *d_sinesq_at_lev, const amrex::Real *d_sinesq_stag_at_lev, const Vector< Real * > d_sponge_ptrs_at_lev, const Vector< MultiFab > *forecast_state_at_lev, const InputSoundingData &input_sounding_data, const LargeScaleForcingData &lsf_data, std::unique_ptr< amrex::MultiFab > &lsf_tendencies, const eb_ &ebfact, bool is_slow_step)
 

Function Documentation

◆ make_mom_sources()

void make_mom_sources ( int  level,
double  time_d,
double  dt,
const Vector< MultiFab > &  S_data,
const MultiFab *  z_phys_nd,
const MultiFab *  z_phys_cc,
Vector< Real > &  stretched_dz_h,
const MultiFab &  xvel,
const MultiFab &  yvel,
const MultiFab &  wvel,
MultiFab &  xmom_src,
MultiFab &  ymom_src,
MultiFab &  zmom_src,
const MultiFab &  base_state,
MultiFab *  forest_drag,
MultiFab *  terrain_blank,
MultiFab *  terrain_blank_xface,
MultiFab *  terrain_blank_yface,
MultiFab *  terrain_blank_zface,
MultiFab *  cosPhi_mf,
MultiFab *  sinPhi_mf,
const Geometry  geom,
const SolverChoice &  solverChoice,
Vector< std::unique_ptr< MultiFab >> &  ,
const Real *  dptr_u_geos,
const Real *  dptr_v_geos,
const Real *  dptr_wbar_sub,
const Vector< Real * >  d_rayleigh_ptrs_at_lev,
const amrex::Real *  d_sinesq_at_lev,
const amrex::Real *  d_sinesq_stag_at_lev,
const Vector< Real * >  d_sponge_ptrs_at_lev,
const Vector< MultiFab > *  forecast_state_at_lev,
const InputSoundingData &  input_sounding_data,
const LargeScaleForcingData &  lsf_data,
std::unique_ptr< amrex::MultiFab > &  lsf_tendencies,
const eb_ &  ebfact,
bool  is_slow_step 
)

Function for computing the slow RHS for the evolution equations for the density, potential temperature and momentum.

Parameters
[in]levellevel of resolution
[in]timecurrent time
[in]dtcurrent slow or fast timestep size
[in]S_datacurrent solution
[in]xvelx-component of velocity
[in]yvely-component of velocity
[in]xmom_srcsource terms for x-momentum
[in]ymom_srcsource terms for y-momentum
[in]zmom_srcsource terms for z-momentum
[in]geomContainer for geometric information
[in]solverChoiceContainer for solver parameters
[in]mapfacmap factors
[in]dptr_u_geoscustom geostrophic wind profile
[in]dptr_v_geoscustom geostrophic wind profile
[in]dptr_wbar_subsubsidence source term
[in]d_rayleigh_ptrs_at_levVector of {strength of Rayleigh damping, reference value for xvel/yvel/zvel/theta} used to define Rayleigh damping
[in]d_sinesq_at_levsin( (pi/2) (z-z_t)/(damping depth)) at cell centers
[in]d_sinesq_stag_at_levsin( (pi/2) (z-z_t)/(damping depth)) at z-faces
78 {
79  BL_PROFILE_REGION("erf_make_mom_sources()");
80 
81  Real time = static_cast<Real>(time_d);
82 
83  Box domain(geom.Domain());
84  const GpuArray<Real, AMREX_SPACEDIM> dxInv = geom.InvCellSizeArray();
85 
86  // Initialize sources to zero each time we may use them
87  xmom_src.setVal(0.0);
88  ymom_src.setVal(0.0);
89  zmom_src.setVal(0.0);
90 
91  MultiFab r_hse (base_state, make_alias, BaseState::r0_comp , 1);
92 
93  // flags to apply certain source terms in substep call only
94  // (none of them on a level that takes no acoustic substeps)
95  bool use_Rayleigh_fast_uv = solverChoice.rayleigh_fast_uvT(level);
96  bool use_Rayleigh_fast_w = solverChoice.rayleigh_fast_w(level);
97  bool use_Rayleigh_impl_w = solverChoice.rayleigh_implicit_w(level) ||
98  solverChoice.rayleigh_slow_implicit_w(level);
99  bool use_canopy_fast = solverChoice.forest_fast(level);
100  bool use_ImmersedForcing_fast = solverChoice.immersed_forcing_fast(level);
101 
102  // *****************************************************************************
103  // Define source term for all three components of momenta from
104  // 1. Coriolis forcing for (xmom,ymom,zmom)
105  // 2. Rayleigh damping for (xmom,ymom,zmom)
106  // 3. Constant / height-dependent geostrophic forcing
107  // 4. Subsidence
108  // 5. Nudging towards input sounding data
109  // 6. Numerical diffusion for (xmom,ymom,zmom)
110  // 7. Sponge
111  // 8. Forest canopy
112  // 9a. Immersed forcing for terrain
113  // 9b. Immersed forcing for buildings
114  // 10. Constant mass flux
115  // *****************************************************************************
116  // NOTE: buoyancy is now computed in a separate routine - it should not appear here
117  // *****************************************************************************
118  //const bool l_use_ndiff = solverChoice.use_num_diff;
119 
120  if (solverChoice.terrain_type == TerrainType::ImmersedForcing) {
121  if (solverChoice.do_forest_drag) {
122  amrex::Error(" Currently forest canopy cannot be used with immersed forcing");
123  }
124  }
125 
126 
127  // *****************************************************************************
128  // Data for Coriolis forcing
129  // *****************************************************************************
130  auto use_coriolis = solverChoice.use_coriolis;
131  auto coriolis_factor = solverChoice.coriolis_factor;
132  auto cosphi = solverChoice.cosphi;
133  auto sinphi = solverChoice.sinphi;
134  auto var_coriolis = solverChoice.variable_coriolis;
135 
136  // *****************************************************************************
137  // Flag for Geostrophic forcing
138  // *****************************************************************************
139  auto abl_geo_forcing = solverChoice.abl_geo_forcing;
140  auto geo_wind_profile = solverChoice.have_geo_wind_profile;
141 
142  // *****************************************************************************
143  // Data for Rayleigh damping
144  // *****************************************************************************
145  auto rayleigh_damp_U = solverChoice.dampingChoice.rayleigh_damp_U;
146  auto rayleigh_damp_V = solverChoice.dampingChoice.rayleigh_damp_V;
147  auto rayleigh_damp_W = solverChoice.dampingChoice.rayleigh_damp_W;
148 
149  Real* ubar = d_rayleigh_ptrs_at_lev[Rayleigh::ubar];
150  Real* vbar = d_rayleigh_ptrs_at_lev[Rayleigh::vbar];
151  Real* wbar = d_rayleigh_ptrs_at_lev[Rayleigh::wbar];
152 
153  // *****************************************************************************
154  // Data for constant mass flux
155  // *****************************************************************************
156  bool enforce_massflux_x = (solverChoice.const_massflux_u != 0);
157  bool enforce_massflux_y = (solverChoice.const_massflux_v != 0);
158  Real U_target = solverChoice.const_massflux_u;
159  Real V_target = solverChoice.const_massflux_v;
160  int massflux_klo = solverChoice.massflux_klo;
161  int massflux_khi = solverChoice.massflux_khi;
162 
163  // These will be updated by integrating through the planar average profiles
164  Real rhoUA_target{0};
165  Real rhoVA_target{0};
166  Real rhoUA{0};
167  Real rhoVA{0};
168 
169  // Cross-sectional areas the integrals above are taken over; the integrated mass-flux
170  // error is divided by these to give a momentum-density tendency
171  Real massflux_area_x{0};
172  Real massflux_area_y{0};
173 
174  // *****************************************************************************
175  // Planar averages for subsidence, nudging, or constant mass flux
176  // *****************************************************************************
177  Table1D<Real> dptr_r_plane, dptr_u_plane, dptr_v_plane;
178  TableData<Real, 1> r_plane_tab, u_plane_tab, v_plane_tab;
179 
180  if (is_slow_step && (dptr_wbar_sub ||
181  (solverChoice.nudging_from_input_sounding &&
182  (solverChoice.large_scale_forcing || solverChoice.nudging_u)) ||
183  enforce_massflux_x || enforce_massflux_y))
184  {
185  // The plane averaging operates at fixed z rather than fixed physical
186  // height. It is valid for a variable-dz fitted mesh only when that
187  // mesh has been explicitly validated as horizontally flat.
188  const bool planar_averages_valid =
189  solverChoice.mesh_type != MeshType::VariableDz ||
190  solverChoice.flat_terrain;
192  planar_averages_valid,
193  "Fixed-index planar averages require a non-variable-dz mesh or "
194  "a validated flat fitted mesh (erf.flat_terrain = true).");
195 
196  const int offset = 1;
197  const int u_offset = 1;
198  const int v_offset = 1;
199 
200  //
201  // We use the alias here to control ncomp inside the PlaneAverage
202  //
203  MultiFab cons(S_data[IntVars::cons], make_alias, 0, 1);
204 
205  IntVect ng_c = S_data[IntVars::cons].nGrowVect(); ng_c[2] = offset;
206  PlaneAverage r_ave(&cons, geom, solverChoice.ave_plane, ng_c);
207  r_ave.compute_averages(ZDir(), r_ave.field());
208 
209  int ncell = r_ave.ncell_line();
210  Gpu::HostVector< Real> r_plane_h(ncell);
211  Gpu::DeviceVector< Real> r_plane_d(ncell);
212 
213  r_ave.line_average(Rho_comp, r_plane_h);
214 
215  Gpu::copyAsync(Gpu::hostToDevice, r_plane_h.begin(), r_plane_h.end(), r_plane_d.begin());
216 
217  Real* dptr_r = r_plane_d.data();
218 
219  Box tdomain = domain; tdomain.grow(2,ng_c[2]);
220  r_plane_tab.resize({tdomain.smallEnd(2)}, {tdomain.bigEnd(2)});
221 
222  dptr_r_plane = r_plane_tab.table();
223  ParallelFor(ncell, [=] AMREX_GPU_DEVICE (int k) noexcept
224  {
225  dptr_r_plane(k-offset) = dptr_r[k];
226  });
227 
228  // U and V momentum
229  IntVect ng_u = S_data[IntVars::xmom].nGrowVect(); ng_u[2] = u_offset;
230  PlaneAverage u_ave(&(S_data[IntVars::xmom]), geom, solverChoice.ave_plane, ng_u);
231 
232  IntVect ng_v = S_data[IntVars::ymom].nGrowVect(); ng_v[2] = v_offset;
233  PlaneAverage v_ave(&(S_data[IntVars::ymom]), geom, solverChoice.ave_plane, ng_v);
234 
235  u_ave.compute_averages(ZDir(), u_ave.field());
236  v_ave.compute_averages(ZDir(), v_ave.field());
237 
238  int u_ncell = u_ave.ncell_line();
239  int v_ncell = v_ave.ncell_line();
240  Gpu::HostVector< Real> u_plane_h(u_ncell), v_plane_h(v_ncell);
241  Gpu::DeviceVector< Real> u_plane_d(u_ncell), v_plane_d(v_ncell);
242 
243  u_ave.line_average(0, u_plane_h);
244  v_ave.line_average(0, v_plane_h);
245 
246  Gpu::copyAsync(Gpu::hostToDevice, u_plane_h.begin(), u_plane_h.end(), u_plane_d.begin());
247  Gpu::copyAsync(Gpu::hostToDevice, v_plane_h.begin(), v_plane_h.end(), v_plane_d.begin());
248 
249  Real* dptr_u = u_plane_d.data();
250  Real* dptr_v = v_plane_d.data();
251 
252  Box udomain = domain; udomain.grow(2,ng_u[2]);
253  Box vdomain = domain; vdomain.grow(2,ng_v[2]);
254  u_plane_tab.resize({udomain.smallEnd(2)}, {udomain.bigEnd(2)});
255  v_plane_tab.resize({vdomain.smallEnd(2)}, {vdomain.bigEnd(2)});
256 
257  dptr_u_plane = u_plane_tab.table();
258  ParallelFor(u_ncell, [=] AMREX_GPU_DEVICE (int k) noexcept
259  {
260  dptr_u_plane(k-u_offset) = dptr_u[k];
261  });
262 
263  dptr_v_plane = v_plane_tab.table();
264  ParallelFor(v_ncell, [=] AMREX_GPU_DEVICE (int k) noexcept
265  {
266  dptr_v_plane(k-v_offset) = dptr_v[k];
267  });
268 
269  // sum in z for massflux adjustment
270  if (enforce_massflux_x || enforce_massflux_y) {
271  Real Lx = geom.ProbHi(0) - geom.ProbLo(0);
272  Real Ly = geom.ProbHi(1) - geom.ProbLo(1);
273 
274  if (solverChoice.mesh_type == MeshType::ConstantDz) {
275  // note: massflux_khi corresponds to unstaggered indices in this case
276  rhoUA = std::accumulate(u_plane_h.begin() + u_offset + massflux_klo,
277  u_plane_h.begin() + u_offset + massflux_khi+1, zero);
278  rhoVA = std::accumulate(v_plane_h.begin() + v_offset + massflux_klo,
279  v_plane_h.begin() + v_offset + massflux_khi+1, zero);
280  rhoUA_target = std::accumulate(r_plane_h.begin() + offset + massflux_klo,
281  r_plane_h.begin() + offset + massflux_khi+1, zero);
282  rhoVA_target = rhoUA_target;
283 
284  rhoUA *= geom.CellSize(2) * Ly;
285  rhoVA *= geom.CellSize(2) * Lx;
286  rhoUA_target *= geom.CellSize(2) * Ly;
287  rhoVA_target *= geom.CellSize(2) * Lx;
288 
289  const Real H = geom.CellSize(2) * static_cast<Real>(massflux_khi - massflux_klo + 1);
290  massflux_area_x = H * Ly;
291  massflux_area_y = H * Lx;
292 
293  } else if (solverChoice.mesh_type == MeshType::StretchedDz) {
294  // note: massflux_khi corresponds to staggered indices in this case
295  Real H{0};
296  for (int k=massflux_klo; k < massflux_khi; ++k) {
297  rhoUA += u_plane_h[k + u_offset] * stretched_dz_h[k];
298  rhoVA += v_plane_h[k + v_offset] * stretched_dz_h[k];
299  rhoUA_target += r_plane_h[k + offset] * stretched_dz_h[k];
300  H += stretched_dz_h[k];
301  }
302  rhoVA_target = rhoUA_target;
303 
304  rhoUA *= Ly;
305  rhoVA *= Lx;
306  rhoUA_target *= Ly;
307  rhoVA_target *= Lx;
308 
309  massflux_area_x = H * Ly;
310  massflux_area_y = H * Lx;
311  }
312 
313  // at this point, this is integrated rho*dA
314  rhoUA_target *= U_target;
315  rhoVA_target *= V_target;
316 
317  Print() << "Integrated mass flux : " << rhoUA << " " << rhoVA
318  << " (target: " << rhoUA_target << " " << rhoVA_target << ")"
319  << std::endl;
320  }
321  }
322 
323  // *****************************************************************************
324  // Add all the other forcings
325  // *****************************************************************************
326  for ( MFIter mfi(S_data[IntVars::cons]); mfi.isValid(); ++mfi)
327  {
328  Box tbx = mfi.nodaltilebox(0);
329  Box tby = mfi.nodaltilebox(1);
330  Box tbz = mfi.nodaltilebox(2);
331  if (tbz.bigEnd(2) == domain.bigEnd(2)+1) tbz.growHi(2,-1);
332 
333  const Array4<const Real>& cell_data = S_data[IntVars::cons].array(mfi);
334  const Array4<const Real>& rho_u = S_data[IntVars::xmom].array(mfi);
335  const Array4<const Real>& rho_v = S_data[IntVars::ymom].array(mfi);
336  const Array4<const Real>& rho_w = S_data[IntVars::zmom].array(mfi);
337 
338  const Array4<const Real>& u = xvel.array(mfi);
339  const Array4<const Real>& v = yvel.array(mfi);
340  const Array4<const Real>& w = wvel.array(mfi);
341 
342  const Array4< Real>& xmom_src_arr = xmom_src.array(mfi);
343  const Array4< Real>& ymom_src_arr = ymom_src.array(mfi);
344  const Array4< Real>& zmom_src_arr = zmom_src.array(mfi);
345 
346  const Array4<const Real>& r0 = r_hse.const_array(mfi);
347 
348  const Array4<const Real>& f_drag_arr = (forest_drag) ? forest_drag->const_array(mfi) :
349  Array4<const Real>{};
350  const Array4<const Real>& t_blank_arr = (terrain_blank) ? terrain_blank->const_array(mfi) :
351  Array4<const Real>{};
352  const Array4<const Real>& t_blank_xface_arr = (terrain_blank_xface) ? terrain_blank_xface->const_array(mfi) :
353  Array4<const Real>{};
354  const Array4<const Real>& t_blank_yface_arr = (terrain_blank_yface) ? terrain_blank_yface->const_array(mfi) :
355  Array4<const Real>{};
356  const Array4<const Real>& t_blank_zface_arr = (terrain_blank_zface) ? terrain_blank_zface->const_array(mfi) :
357  Array4<const Real>{};
358 
359  const Array4<const Real>& cphi_arr = (cosPhi_mf) ? cosPhi_mf->const_array(mfi) :
360  Array4<const Real>{};
361  const Array4<const Real>& sphi_arr = (sinPhi_mf) ? sinPhi_mf->const_array(mfi) :
362  Array4<const Real>{};
363 
364  const Array4<const Real>& z_nd_arr = z_phys_nd->const_array(mfi);
365  const Array4<const Real>& z_cc_arr = z_phys_cc->const_array(mfi);
366 
367 
368  // *****************************************************************************
369  // 1. Add Coriolis forcing (this assumes east is +x, north is +y)
370  // *****************************************************************************
371  if (use_coriolis && is_slow_step) {
372  // The hindcast latitude is there only with erf.hindcast_lateral_forcing (the callers
373  // pass nullptr otherwise); without it, fall through to the variable or constant latitude
374  if (solverChoice.init_type == InitType::HindCast && forecast_state_at_lev != nullptr) {
375  const Array4<const Real>& latlon_arr = (*forecast_state_at_lev)[4].const_array(mfi);
376  ParallelFor(tbx, tby, tbz,
377  [=] AMREX_GPU_DEVICE (int i, int j, int k)
378  {
379  Real rho_on_u_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i-1,j,k,Rho_comp) );
380  Real v_loc = fourth * (v(i,j+1,k) + v(i,j,k) + v(i-1,j+1,k) + v(i-1,j,k));
381  Real w_loc = fourth * (w(i,j,k+1) + w(i,j,k) + w(i-1,j,k+1) + w(i-1,j,k));
382  Real latitude = latlon_arr(i,j,k,0);
383  Real sphi_loc = std::sin(latitude*PI/Real(180.0));
384  Real cphi_loc = std::cos(latitude*PI/Real(180.0));
385  xmom_src_arr(i, j, k) += coriolis_factor * rho_on_u_face * (v_loc * sphi_loc - w_loc * cphi_loc);
386  },
387  [=] AMREX_GPU_DEVICE (int i, int j, int k) {
388  Real rho_on_v_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i,j-1,k,Rho_comp) );
389  Real u_loc = fourth * (u(i+1,j,k) + u(i,j,k) + u(i+1,j-1,k) + u(i,j-1,k));
390  Real latitude = latlon_arr(i,j,k,0);
391  Real sphi_loc = std::sin(latitude*PI/Real(180.0));
392  ymom_src_arr(i, j, k) += -coriolis_factor * rho_on_v_face * u_loc * sphi_loc;
393  },
394  [=] AMREX_GPU_DEVICE (int i, int j, int k) {
395  Real rho_on_w_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i,j,k-1,Rho_comp) );
396  Real u_loc = fourth * (u(i+1,j,k) + u(i,j,k) + u(i+1,j,k-1) + u(i,j,k-1));
397  Real latitude = latlon_arr(i,j,k,0);
398  Real cphi_loc = std::cos(latitude*PI/Real(180.0));
399  zmom_src_arr(i, j, k) += coriolis_factor * rho_on_w_face * u_loc * cphi_loc;
400  });
401  }
402  else if (var_coriolis && (sinPhi_mf) && (cosPhi_mf)) {
403  ParallelFor(tbx, tby, tbz,
404  [=] AMREX_GPU_DEVICE (int i, int j, int k)
405  {
406  Real rho_on_u_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i-1,j,k,Rho_comp) );
407  Real v_loc = fourth * (v(i,j+1,k) + v(i,j,k) + v(i-1,j+1,k) + v(i-1,j,k));
408  Real w_loc = fourth * (w(i,j,k+1) + w(i,j,k) + w(i-1,j,k+1) + w(i-1,j,k));
409  Real sphi_loc = myhalf * (sphi_arr(i,j,0) + sphi_arr(i-1,j,0));
410  Real cphi_loc = myhalf * (cphi_arr(i,j,0) + cphi_arr(i-1,j,0));
411  xmom_src_arr(i, j, k) += coriolis_factor * rho_on_u_face * (v_loc * sphi_loc - w_loc * cphi_loc);
412  },
413  [=] AMREX_GPU_DEVICE (int i, int j, int k) {
414  Real rho_on_v_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i,j-1,k,Rho_comp) );
415  Real u_loc = fourth * (u(i+1,j,k) + u(i,j,k) + u(i+1,j-1,k) + u(i,j-1,k));
416  Real sphi_loc = myhalf * (sphi_arr(i,j,0) + sphi_arr(i,j-1,0));
417  ymom_src_arr(i, j, k) += -coriolis_factor * rho_on_v_face * u_loc * sphi_loc;
418  },
419  [=] AMREX_GPU_DEVICE (int i, int j, int k) {
420  Real rho_on_w_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i,j,k-1,Rho_comp) );
421  Real u_loc = fourth * (u(i+1,j,k) + u(i,j,k) + u(i+1,j,k-1) + u(i,j,k-1));
422  zmom_src_arr(i, j, k) += coriolis_factor * rho_on_w_face * u_loc * cphi_arr(i,j,0);
423  });
424  } else {
425  if (solverChoice.terrain_type == TerrainType::EB) {
426  Array4<const Real> u_volfrac = (ebfact.get_u_const_factory())->getVolFrac().const_array(mfi);
427  Array4<const Real> v_volfrac = (ebfact.get_v_const_factory())->getVolFrac().const_array(mfi);
428  Array4<const Real> w_volfrac = (ebfact.get_w_const_factory())->getVolFrac().const_array(mfi);
429  ParallelFor(tbx, [=] AMREX_GPU_DEVICE (int i, int j, int k) {
430  Real v_loc = 0.0;
431  Real w_loc = 0.0;
432  Real v_vol = v_volfrac(i,j+1,k) + v_volfrac(i,j,k) + v_volfrac(i-1,j+1,k) + v_volfrac(i-1,j,k);
433  Real w_vol = w_volfrac(i,j,k+1) + w_volfrac(i,j,k) + w_volfrac(i-1,j,k+1) + w_volfrac(i-1,j,k);
434  Real rho_on_u_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i-1,j,k,Rho_comp) );
435  if (v_vol > 0.0) {
436  v_loc = ( v_volfrac(i ,j+1,k) * v(i ,j+1,k) + v_volfrac(i ,j,k) * v(i ,j,k)
437  + v_volfrac(i-1,j+1,k) * v(i-1,j+1,k) + v_volfrac(i-1,j,k) * v(i-1,j,k)) / v_vol;
438  }
439  if (w_vol > 0.0) {
440  w_loc = ( w_volfrac(i ,j,k+1) * w(i ,j,k+1) + w_volfrac(i ,j,k) * w(i ,j,k)
441  + w_volfrac(i-1,j,k+1) * w(i-1,j,k+1) + w_volfrac(i-1,j,k) * w(i-1,j,k)) / w_vol;
442  }
443  xmom_src_arr(i, j, k) += coriolis_factor * rho_on_u_face * (v_loc * sinphi - w_loc * cosphi);
444  });
445  ParallelFor(tby, [=] AMREX_GPU_DEVICE (int i, int j, int k) {
446  Real u_loc = 0.0;
447  Real u_vol = u_volfrac(i+1,j,k) + u_volfrac(i,j,k) + u_volfrac(i+1,j-1,k) + u_volfrac(i,j-1,k);
448  Real rho_on_v_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i,j-1,k,Rho_comp) );
449  if (u_vol > 0.0) {
450  u_loc = ( u_volfrac(i+1,j ,k) * u(i+1,j ,k) + u_volfrac(i,j ,k) * u(i,j ,k)
451  + u_volfrac(i+1,j-1,k) * u(i+1,j-1,k) + u_volfrac(i,j-1,k) * u(i,j-1,k)) / u_vol;
452  }
453  ymom_src_arr(i, j, k) += -coriolis_factor * rho_on_v_face * u_loc * sinphi;
454  });
455  ParallelFor(tbz, [=] AMREX_GPU_DEVICE (int i, int j, int k) {
456  Real u_loc = 0.0;
457  Real u_vol = u_volfrac(i+1,j,k) + u_volfrac(i,j,k) + u_volfrac(i+1,j,k-1) + u_volfrac(i,j,k-1);
458  Real rho_on_w_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i,j,k-1,Rho_comp) );
459  if (u_vol > 0.0) {
460  u_loc = ( u_volfrac(i+1,j,k ) * u(i+1,j,k ) + u_volfrac(i,j,k) * u(i,j,k )
461  + u_volfrac(i+1,j,k-1) * u(i+1,j,k-1) + u_volfrac(i,j,k-1) * u(i,j,k-1)) / u_vol;
462  }
463  zmom_src_arr(i, j, k) += coriolis_factor * rho_on_w_face * u_loc * cosphi;
464  });
465  } else {
466  ParallelFor(tbx, tby, tbz,
467  [=] AMREX_GPU_DEVICE (int i, int j, int k)
468  {
469  Real v_loc = fourth * (v(i,j+1,k) + v(i,j,k) + v(i-1,j+1,k) + v(i-1,j,k));
470  Real w_loc = fourth * (w(i,j,k+1) + w(i,j,k) + w(i-1,j,k+1) + w(i-1,j,k));
471  Real rho_on_u_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i-1,j,k,Rho_comp) );
472  xmom_src_arr(i, j, k) += rho_on_u_face * ( coriolis_factor * (v_loc * sinphi - w_loc * cosphi) );
473  },
474  [=] AMREX_GPU_DEVICE (int i, int j, int k) {
475  Real rho_on_v_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i,j-1,k,Rho_comp) );
476  Real u_loc = fourth * (u(i+1,j,k) + u(i,j,k) + u(i+1,j-1,k) + u(i,j-1,k));
477  ymom_src_arr(i, j, k) += rho_on_v_face * ( -coriolis_factor * u_loc * sinphi );
478  },
479  [=] AMREX_GPU_DEVICE (int i, int j, int k) {
480  Real u_loc = fourth * (u(i+1,j,k) + u(i,j,k) + u(i+1,j,k-1) + u(i,j,k-1));
481  Real rho_on_w_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i,j,k-1,Rho_comp) );
482  zmom_src_arr(i, j, k) += rho_on_w_face * ( coriolis_factor * u_loc * cosphi );
483  });
484  }
485  } // var_coriolis
486  } // use_coriolis
487 
488  // *****************************************************************************
489  // 2. Add RAYLEIGH damping
490  // *****************************************************************************
491  Real dampcoef = solverChoice.dampingChoice.rayleigh_dampcoef;
492 
493  if ( (is_slow_step && !use_Rayleigh_fast_uv) || (!is_slow_step && use_Rayleigh_fast_uv)) {
494  if (rayleigh_damp_U) {
495  ParallelFor(tbx, [=] AMREX_GPU_DEVICE (int i, int j, int k)
496  {
497  Real rho_on_u_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i-1,j,k,Rho_comp) );
498  Real uu = rho_u(i,j,k) / rho_on_u_face;
499  Real sinesq = d_sinesq_at_lev[k];
500  xmom_src_arr(i, j, k) -= dampcoef*sinesq * (uu - ubar[k]) * rho_on_u_face;
501  });
502  }
503 
504  if (rayleigh_damp_V) {
505  ParallelFor(tby, [=] AMREX_GPU_DEVICE (int i, int j, int k)
506  {
507  Real rho_on_v_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i,j-1,k,Rho_comp) );
508  Real vv = rho_v(i,j,k) / rho_on_v_face;
509  Real sinesq = d_sinesq_at_lev[k];
510  ymom_src_arr(i, j, k) -= dampcoef*sinesq * (vv - vbar[k]) * rho_on_v_face;
511  });
512  }
513  } // fast or slow step
514 
515  // With FastImplicit the w damping is done implicitly in the substeps instead, and with
516  // SlowImplicit in the slow update (no_substep_fun)
517  if ( (is_slow_step && !use_Rayleigh_fast_w && !use_Rayleigh_impl_w) ||
518  (!is_slow_step && use_Rayleigh_fast_w) ) {
519  if (rayleigh_damp_W) {
520  ParallelFor(tbz, [=] AMREX_GPU_DEVICE (int i, int j, int k)
521  {
522  Real rho_on_w_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i,j,k-1,Rho_comp) );
523  Real ww = rho_w(i,j,k) / rho_on_w_face;
524  Real sinesq = d_sinesq_stag_at_lev[k];
525  zmom_src_arr(i, j, k) -= dampcoef*sinesq * (ww - wbar[k]) * rho_on_w_face;
526  });
527  }
528  } // fast or slow step
529 
530  // *****************************************************************************
531  // 3a. Add constant GEOSTROPHIC forcing
532  // *****************************************************************************
533  if (is_slow_step) {
534  ParallelFor(tbx, tby, tbz,
535  [=] AMREX_GPU_DEVICE (int i, int j, int k)
536  {
537  Real rho_on_u_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i-1,j,k,Rho_comp) );
538  xmom_src_arr(i, j, k) += rho_on_u_face * abl_geo_forcing[0];
539  },
540  [=] AMREX_GPU_DEVICE (int i, int j, int k)
541  {
542  Real rho_on_v_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i,j-1,k,Rho_comp) );
543  ymom_src_arr(i, j, k) += rho_on_v_face * abl_geo_forcing[1];
544  },
545  [=] AMREX_GPU_DEVICE (int i, int j, int k)
546  {
547  Real rho_on_w_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i,j,k-1,Rho_comp) );
548  zmom_src_arr(i, j, k) += rho_on_w_face * abl_geo_forcing[2];
549  });
550  }
551 
552  // *****************************************************************************
553  // 3b. Add height-dependent GEOSTROPHIC forcing
554  // *****************************************************************************
555  if (geo_wind_profile && is_slow_step) {
556  // sin(latitude) on each face, taken the way the Coriolis forcing above takes it:
557  // from the hindcast latitude, from the spatially varying factor, or constant
558  const bool geo_hindcast_lat = (solverChoice.init_type == InitType::HindCast) &&
559  (forecast_state_at_lev != nullptr);
560  const bool geo_var_sinphi = !geo_hindcast_lat && var_coriolis && (sinPhi_mf != nullptr);
561  const Array4<const Real> geo_latlon_arr = (geo_hindcast_lat) ?
562  (*forecast_state_at_lev)[4].const_array(mfi) : Array4<const Real>{};
563  ParallelFor(tbx, tby,
564  [=] AMREX_GPU_DEVICE (int i, int j, int k)
565  {
566  Real sphi_loc = (geo_hindcast_lat) ? std::sin(geo_latlon_arr(i,j,k,0)*PI/Real(180.0)) :
567  (geo_var_sinphi) ? myhalf * (sphi_arr(i,j,0) + sphi_arr(i-1,j,0)) : sinphi;
568  Real rho_on_u_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i-1,j,k,Rho_comp) );
569  xmom_src_arr(i, j, k) -= coriolis_factor * rho_on_u_face * dptr_v_geos[k] * sphi_loc;
570  },
571  [=] AMREX_GPU_DEVICE (int i, int j, int k)
572  {
573  Real sphi_loc = (geo_hindcast_lat) ? std::sin(geo_latlon_arr(i,j,k,0)*PI/Real(180.0)) :
574  (geo_var_sinphi) ? myhalf * (sphi_arr(i,j,0) + sphi_arr(i,j-1,0)) : sinphi;
575  Real rho_on_v_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i,j-1,k,Rho_comp) );
576  ymom_src_arr(i, j, k) += coriolis_factor * rho_on_v_face * dptr_u_geos[k] * sphi_loc;
577  });
578  } // geo_wind_profile
579 
580  // *****************************************************************************
581  // 4. Add custom SUBSIDENCE terms
582  // *****************************************************************************
583  if (solverChoice.custom_w_subsidence && is_slow_step && solverChoice.do_mom_advection) {
584  // -w dU/dz is a tendency of the velocity whichever way the custom forcing is
585  // specified, so it is weighted by the face density to give one of rho*u, rho*v
586  const int nr = Rho_comp;
587  ParallelFor(tbx, tby,
588  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
589  {
590  Real dzInv = myhalf*dxInv[2];
591  if (z_nd_arr) {
592  Real z_xf_lo = fourth * ( z_nd_arr(i,j,k ) + z_nd_arr(i,j+1,k )
593  + z_nd_arr(i,j,k-1) + z_nd_arr(i,j+1,k-1) );
594  Real z_xf_hi = fourth * ( z_nd_arr(i,j,k+1) + z_nd_arr(i,j+1,k+1)
595  + z_nd_arr(i,j,k+2) + z_nd_arr(i,j+1,k+2) );
596  dzInv = one / (z_xf_hi - z_xf_lo);
597  }
598  Real rho_on_u_face = myhalf * ( cell_data(i,j,k,nr) + cell_data(i-1,j,k,nr) );
599  Real U_hi = dptr_u_plane(k+1) / dptr_r_plane(k+1);
600  Real U_lo = dptr_u_plane(k-1) / dptr_r_plane(k-1);
601  Real wbar_xf = myhalf * (dptr_wbar_sub[k] + dptr_wbar_sub[k+1]);
602  xmom_src_arr(i, j, k) -= rho_on_u_face * wbar_xf * (U_hi - U_lo) * dzInv;
603  },
604  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
605  {
606  Real dzInv = myhalf*dxInv[2];
607  if (z_nd_arr) {
608  Real z_yf_lo = fourth * ( z_nd_arr(i,j,k ) + z_nd_arr(i+1,j,k )
609  + z_nd_arr(i,j,k-1) + z_nd_arr(i+1,j,k-1) );
610  Real z_yf_hi = fourth * ( z_nd_arr(i,j,k+1) + z_nd_arr(i+1,j,k+1)
611  + z_nd_arr(i,j,k+2) + z_nd_arr(i+1,j,k+2) );
612  dzInv = one / (z_yf_hi - z_yf_lo);
613  }
614  Real rho_on_v_face = myhalf * ( cell_data(i,j,k,nr) + cell_data(i,j-1,k,nr) );
615  Real V_hi = dptr_v_plane(k+1) / dptr_r_plane(k+1);
616  Real V_lo = dptr_v_plane(k-1) / dptr_r_plane(k-1);
617  Real wbar_yf = myhalf * (dptr_wbar_sub[k] + dptr_wbar_sub[k+1]);
618  ymom_src_arr(i, j, k) -= rho_on_v_face * wbar_yf * (V_hi - V_lo) * dzInv;
619  });
620  }
621 
622  if (solverChoice.large_scale_forcing && is_slow_step)
623  {
624  // subsidence terms for U and V
625  auto lsf_arr = lsf_tendencies->const_array(mfi);
626 
627  const int kmin = domain.smallEnd(2) + 1; // minimum k for vertical subsidence
628  const int kmax = domain.bigEnd(2) - 1; // maximum k for vertical subsidence
629 
630  ParallelFor(tbx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
631  {
632  if (k >= kmin && k <= kmax) {
633  int k1, k2;
634  if (lsf_arr(i, j, k, 2) >= 0.0)
635  {
636  k1 = k;
637  k2 = k-1;
638  } else {
639  k1 = k+1;
640  k2 = k;
641  }
642 
643  // one-sided difference over the single cell k1->k2
644  Real dzInv = dxInv[2];
645  if (z_nd_arr) {
646  Real z_uf_1 = fourth * ( z_nd_arr(i,j,k1 ) + z_nd_arr(i,j+1,k1 )
647  + z_nd_arr(i,j,k1+1) + z_nd_arr(i,j+1,k1+1) );
648  Real z_uf_2 = fourth * ( z_nd_arr(i,j,k2 ) + z_nd_arr(i,j+1,k2 )
649  + z_nd_arr(i,j,k2+1) + z_nd_arr(i,j+1,k2+1) );
650  dzInv = one / (z_uf_1 - z_uf_2);
651  }
652  Real rho_on_u_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i-1,j,k,Rho_comp) );
653  amrex::Real utend = -(dzInv * lsf_arr(i, j, k, 2)) * ( u(i, j, k1) - u(i, j, k2) );
654  xmom_src_arr(i, j, k) += utend * rho_on_u_face;
655  }
656  });
657 
658  ParallelFor(tby, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
659  {
660  if (k >= kmin && k <= kmax) {
661  int k1, k2;
662  if (lsf_arr(i, j, k, 2) >= 0.0)
663  {
664  k1 = k;
665  k2 = k-1;
666  } else {
667  k1 = k+1;
668  k2 = k;
669  }
670 
671  // one-sided difference over the single cell k1->k2
672  Real dzInv = dxInv[2];
673  if (z_nd_arr) {
674  Real z_vf_1 = fourth * ( z_nd_arr(i,j,k1 ) + z_nd_arr(i+1,j,k1 )
675  + z_nd_arr(i,j,k1+1) + z_nd_arr(i+1,j,k1+1) );
676  Real z_vf_2 = fourth * ( z_nd_arr(i,j,k2 ) + z_nd_arr(i+1,j,k2 )
677  + z_nd_arr(i,j,k2+1) + z_nd_arr(i+1,j,k2+1) );
678  dzInv = one / (z_vf_1 - z_vf_2);
679  }
680  Real rho_on_v_face = 0.5 * ( cell_data(i,j,k,Rho_comp) + cell_data(i,j-1,k,Rho_comp) );
681  amrex::Real vtend = -(dzInv * lsf_arr(i, j, k, 2)) * ( v(i, j, k1) - v(i, j, k2) );
682  ymom_src_arr(i, j, k) += vtend * rho_on_v_face;
683  }
684  });
685  }
686 
687  // *************************************************************************************
688  // 5. Add nudging towards value specified in input sounding
689  // NOTE: nudging_u only gates sounding-based wind nudging (the !large_scale_forcing
690  // branch below); LSF-based wind nudging (towards uls/vls) is controlled solely by
691  // large_scale_forcing, per the NOTE in Inputs.rst. Likewise, nudging_u_z1/z2 only
692  // restrict the sounding-based branch.
693  // *************************************************************************************
694  if (solverChoice.nudging_from_input_sounding && is_slow_step &&
695  (solverChoice.large_scale_forcing || solverChoice.nudging_u))
696  {
697  const bool l_lsf = solverChoice.large_scale_forcing;
698  const Real u_z1 = solverChoice.nudging_u_z1;
699  const Real u_z2 = solverChoice.nudging_u_z2;
700  Real uv_coeff_n = 1.0;
701  Real uv_coeff_np1 = 0.0;
702  Real tau = Real(1.0) / input_sounding_data.tau_nudging;
703  const Real *u_nudge_n, *u_nudge_np1, *v_nudge_n, *v_nudge_np1;
704  if (!l_lsf)
705  {
706  int itime_n = 0;
707  int itime_np1 = 0;
708  int n_sounding_times = input_sounding_data.input_sounding_time.size();
709 
710  for (int nt = 1; nt < n_sounding_times; nt++) {
711  if (time > input_sounding_data.input_sounding_time[nt]) itime_n = nt;
712  }
713  if (itime_n == n_sounding_times-1) {
714  itime_np1 = itime_n;
715  } else {
716  itime_np1 = itime_n+1;
717  uv_coeff_np1 = (time - input_sounding_data.input_sounding_time[itime_n]) /
718  (input_sounding_data.input_sounding_time[itime_np1] - input_sounding_data.input_sounding_time[itime_n]);
719  uv_coeff_n = Real(1.0) - uv_coeff_np1;
720  }
721  u_nudge_n = input_sounding_data.U_inp_sound_d[itime_n].dataPtr() + 1;
722  u_nudge_np1 = input_sounding_data.U_inp_sound_d[itime_np1].dataPtr() + 1;
723  v_nudge_n = input_sounding_data.V_inp_sound_d[itime_n].dataPtr() + 1;
724  v_nudge_np1 = input_sounding_data.V_inp_sound_d[itime_np1].dataPtr() + 1;
725  } else {
726  int itime_curr = 0;
727  int itime_next = 0;
728  uv_coeff_n = 1.0;
729  uv_coeff_np1 = 0.0;
730  tau = 1.0 / lsf_data.tau_lsf; // only applies to u,v LSF nudging
731 
732  lsf_data.get_forcing_time_coeffs(time, itime_curr, itime_next, uv_coeff_n, uv_coeff_np1);
733  u_nudge_n = lsf_data.u_int_lsf_d[itime_curr].dataPtr();
734  u_nudge_np1 = lsf_data.u_int_lsf_d[itime_next].dataPtr();
735  v_nudge_n = lsf_data.v_int_lsf_d[itime_curr].dataPtr();
736  v_nudge_np1 = lsf_data.v_int_lsf_d[itime_next].dataPtr();
737  }
738 
739  ParallelFor(tbx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
740  {
741  Real z = z_cc_arr(i,j,k);
742  if (l_lsf || (z >= u_z1 && z <= u_z2)) {
743  Real unudge = -((dptr_u_plane(k)/dptr_r_plane(k)) - (uv_coeff_n*u_nudge_n[k] + uv_coeff_np1*u_nudge_np1[k]));
744  xmom_src_arr(i, j, k) += tau * unudge * dptr_r_plane(k);
745  }
746  });
747 
748  ParallelFor(tby, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
749  {
750  Real z = z_cc_arr(i,j,k);
751  if (l_lsf || (z >= u_z1 && z <= u_z2)) {
752  Real vnudge = -((dptr_v_plane(k)/dptr_r_plane(k)) - (uv_coeff_n*v_nudge_n[k] + uv_coeff_np1*v_nudge_np1[k]));
753  ymom_src_arr(i, j, k) += tau * vnudge * dptr_r_plane(k);
754  }
755  });
756  }
757 
758  // *****************************************************************************
759  // 6. Add NUMERICAL DIFFUSION terms
760  // *****************************************************************************
761 #if 0
762  if (l_use_ndiff) {
763  const Array4<const Real>& mf_ux = mapfac[MapFacType::u_x]->const_array(mfi);
764  const Array4<const Real>& mf_uy = mapfac[MapFacType::u_y]->const_array(mfi);
765  const Array4<const Real>& mf_vx = mapfac[MapFacType::v_x]->const_array(mfi);
766  const Array4<const Real>& mf_vy = mapfac[MapFacType::v_y]->const_array(mfi);
767  NumericalDiffusion_Xmom(tbx, dt, solverChoice.num_diff_coeff,
768  u, cell_data, xmom_src_arr, mf_ux, mf_uy);
769  NumericalDiffusion_Ymom(tby, dt, solverChoice.num_diff_coeff,
770  v, cell_data, ymom_src_arr, mf_vx, mf_vy);
771  }
772 #endif
773 
774  // *****************************************************************************
775  // 7. Add SPONGING
776  // *****************************************************************************
777  if (is_slow_step) {
778  if (solverChoice.spongeChoice.sponge_type == SpongeType::Input_Sponge)
779  {
780  ApplySpongeZoneBCsForMom_ReadFromFile(solverChoice.spongeChoice, geom, tbx, tby, cell_data,
781  z_cc_arr, xmom_src_arr, ymom_src_arr,
782  rho_u, rho_v, d_sponge_ptrs_at_lev);
783  }
784  else if (solverChoice.spongeChoice.sponge_type == SpongeType::Standard)
785  {
786  ApplySpongeZoneBCsForMom(solverChoice.spongeChoice, geom, tbx, tby, tbz,
787  xmom_src_arr, ymom_src_arr, zmom_src_arr, rho_u, rho_v, rho_w,
788  r0, z_nd_arr, z_cc_arr);
789  }
790 
791  if(solverChoice.init_type == InitType::HindCast and solverChoice.hindcast_lateral_forcing){
792 
793  const Array4<const Real>& rho_u_forecast_state = (*forecast_state_at_lev)[IntVars::xmom].array(mfi);
794  const Array4<const Real>& rho_v_forecast_state = (*forecast_state_at_lev)[IntVars::ymom].array(mfi);
795  const Array4<const Real>& rho_w_forecast_state = (*forecast_state_at_lev)[IntVars::zmom].array(mfi);
796  const Array4<const Real>& cons_forecast_state = (*forecast_state_at_lev)[IntVars::cons].array(mfi);
797  ApplyBndryForcing_Forecast(solverChoice, geom, tbx, tby, tbz, z_nd_arr,
798  xmom_src_arr, ymom_src_arr, zmom_src_arr,
799  rho_u, rho_v, rho_w,
800  rho_u_forecast_state, rho_v_forecast_state, rho_w_forecast_state,
801  cons_forecast_state);
802  }
803  }
804 
805  // *****************************************************************************
806  // 8. Add CANOPY source terms
807  // The drag is the acceleration -C_d LAD u |U| (Forest.rst) times the
808  // density on the face, since these are sources of rho u, rho v, rho w.
809  // With erf.forest_substep = true, cell_data is the state the substep starts
810  // from (S_old for the first substep, the running substep state after), so
811  // the density follows the substeps; the velocity is the stage velocity.
812  // *****************************************************************************
813  if (solverChoice.do_forest_drag &&
814  ((is_slow_step && !use_canopy_fast) || (!is_slow_step && use_canopy_fast))) {
815  ParallelFor(tbx, tby, tbz,
816  [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
817  {
818  const Real ux = u(i, j, k);
819  const Real uy = fourth * ( v(i, j , k ) + v(i-1, j , k )
820  + v(i, j+1, k ) + v(i-1, j+1, k ) );
821  const Real uz = fourth * ( w(i, j , k ) + w(i-1, j , k )
822  + w(i, j , k+1) + w(i-1, j , k+1) );
823  const Real windspeed = std::sqrt(ux * ux + uy * uy + uz * uz);
824  const Real f_drag = myhalf * (f_drag_arr(i, j, k) + f_drag_arr(i-1, j, k));
825  const Real rho_on_u_face = myhalf * (cell_data(i, j, k, Rho_comp) + cell_data(i-1, j, k, Rho_comp));
826  xmom_src_arr(i, j, k) -= rho_on_u_face * f_drag * ux * windspeed;
827  },
828  [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
829  {
830  const Real ux = fourth * ( u(i , j , k ) + u(i , j-1, k )
831  + u(i+1, j , k ) + u(i+1, j-1, k ) );
832  const Real uy = v(i, j, k);
833  const Real uz = fourth * ( w(i , j , k ) + w(i , j-1, k )
834  + w(i , j , k+1) + w(i , j-1, k+1) );
835  const amrex::Real windspeed = std::sqrt(ux * ux + uy * uy + uz * uz);
836  const Real f_drag = myhalf * (f_drag_arr(i, j, k) + f_drag_arr(i, j-1, k));
837  const Real rho_on_v_face = myhalf * (cell_data(i, j, k, Rho_comp) + cell_data(i, j-1, k, Rho_comp));
838  ymom_src_arr(i, j, k) -= rho_on_v_face * f_drag * uy * windspeed;
839  },
840  [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
841  {
842  const amrex::Real ux = fourth * ( u(i , j , k ) + u(i+1, j , k )
843  + u(i , j , k-1) + u(i+1, j , k-1) );
844  const amrex::Real uy = fourth * ( v(i , j , k ) + v(i , j+1, k )
845  + v(i , j , k-1) + v(i , j+1, k-1) );
846  const amrex::Real uz = w(i, j, k);
847  const amrex::Real windspeed = std::sqrt(ux * ux + uy * uy + uz * uz);
848  const Real f_drag = myhalf * (f_drag_arr(i, j, k) + f_drag_arr(i, j, k-1));
849  const Real rho_on_w_face = myhalf * (cell_data(i, j, k, Rho_comp) + cell_data(i, j, k-1, Rho_comp));
850  zmom_src_arr(i, j, k) -= rho_on_w_face * f_drag * uz * windspeed;
851  });
852  }
853  // *****************************************************************************
854  // 9a. Add immersed source terms for terrain
855  // *****************************************************************************
856  // erf.if_implicit_projection: on a level that projects the drag of the solid is inside the
857  // projection; a FullyCompressible level keeps it in the source term
858  const bool l_if_drag_in_projection = solverChoice.if_drag_in_projection(level);
859  if (solverChoice.terrain_type == TerrainType::ImmersedForcing &&
860  ((is_slow_step && !use_ImmersedForcing_fast) || (!is_slow_step && use_ImmersedForcing_fast))) {
861 
862  if (solverChoice.if_fraction_stress) {
863  ImmersedForcingTerrain_Xmom_FractionStress(tbx, u, v, w, cell_data, t_blank_arr, t_blank_xface_arr,
864  xmom_src_arr, geom, solverChoice, dt, time, l_if_drag_in_projection);
865  ImmersedForcingTerrain_Ymom_FractionStress(tby, u, v, w, cell_data, t_blank_arr, t_blank_yface_arr,
866  ymom_src_arr, geom, solverChoice, dt, time, l_if_drag_in_projection);
867  } else {
868  ImmersedForcingTerrain_Xmom(tbx, u, v, w, cell_data, t_blank_arr, t_blank_xface_arr,
869  z_cc_arr, xmom_src_arr, geom, solverChoice, dt, l_if_drag_in_projection);
870  ImmersedForcingTerrain_Ymom(tby, u, v, w, cell_data, t_blank_arr, t_blank_yface_arr,
871  z_cc_arr, ymom_src_arr, geom, solverChoice, dt, l_if_drag_in_projection);
872  }
873  ImmersedForcingTerrain_Zmom(tbz, u, v, w, cell_data, t_blank_arr, t_blank_zface_arr,
874  z_cc_arr, zmom_src_arr, geom, solverChoice, dt, l_if_drag_in_projection);
875  }
876 
877  // *****************************************************************************
878  // 9b. Add immersed source terms for buildings
879  // *****************************************************************************
880  if (solverChoice.use_building_immersed_forcing(geom) &&
881  ((is_slow_step && !use_ImmersedForcing_fast) || (!is_slow_step && use_ImmersedForcing_fast))) {
882 
883  ImmersedForcingBuildings_Xmom(tbx, u, v, w, cell_data, t_blank_arr, t_blank_xface_arr,
884  z_cc_arr, xmom_src_arr, geom, solverChoice, dt, is_slow_step, l_if_drag_in_projection);
885  ImmersedForcingBuildings_Ymom(tby, u, v, w, cell_data, t_blank_arr, t_blank_yface_arr,
886  z_cc_arr, ymom_src_arr, geom, solverChoice, dt, is_slow_step, l_if_drag_in_projection);
887  ImmersedForcingBuildings_Zmom(tbz, u, v, w, cell_data, t_blank_arr, t_blank_zface_arr,
888  z_cc_arr, zmom_src_arr, geom, solverChoice, dt, is_slow_step, l_if_drag_in_projection);
889  }
890 
891  // *****************************************************************************
892  // 10. Enforce constant mass flux
893  // *****************************************************************************
894  if (is_slow_step && (enforce_massflux_x || enforce_massflux_y)) {
895  // The mass fluxes are integrated over the cross-section, so their difference is
896  // divided by its area to give a tendency of rho*u (rho*v) at each point.
897  // The divisor is never zero: an optimized build may divide before it tests
898  // the area, and 0/0 would then trip amrex.fpe_trap_invalid even when the
899  // result is discarded. For a positive area the divisor is the area itself.
900  Real tau_inv = one / solverChoice.const_massflux_tau;
901  constexpr Real tiny_area = std::numeric_limits<Real>::min();
902  Real dRUA = (massflux_area_x > zero) ? (rhoUA_target - rhoUA) / amrex::max(massflux_area_x, tiny_area) : zero;
903  Real dRVA = (massflux_area_y > zero) ? (rhoVA_target - rhoVA) / amrex::max(massflux_area_y, tiny_area) : zero;
904 
905  // Each direction is relaxed only if its own mass flux is prescribed; the other
906  // direction's target is zero and must not be imposed
907  if (enforce_massflux_x) {
908  ParallelFor(tbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept {
909  xmom_src_arr(i, j, k) += tau_inv * dRUA;
910  });
911  }
912  if (enforce_massflux_y) {
913  ParallelFor(tby, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept {
914  ymom_src_arr(i, j, k) += tau_inv * dRVA;
915  });
916  }
917  }
918 
919  } // mfi
920 }
void ApplyBndryForcing_Forecast(const SolverChoice &solverChoice, const Geometry geom, const Box &tbx, const Box &tby, const Box &tbz, const Array4< const Real > &z_phys_nd, const Array4< Real > &rho_u_rhs, const Array4< Real > &rho_v_rhs, const Array4< Real > &rho_w_rhs, const Array4< const Real > &rho_u, const Array4< const Real > &rho_v, const Array4< const Real > &rho_w, const Array4< const Real > &rho_u_initial_state, const Array4< const Real > &rho_v_initial_state, const Array4< const Real > &rho_w_initial_state, const Array4< const Real > &cons_initial_state)
Definition: ERF_ApplyBndryForcing_Forecast.cpp:8
void ApplySpongeZoneBCsForMom(const SpongeChoice &spongeChoice, const Geometry geom, const Box &tbx, const Box &tby, const Box &tbz, const Array4< Real > &rho_u_rhs, const Array4< Real > &rho_v_rhs, const Array4< Real > &rho_w_rhs, const Array4< const Real > &rho_u, const Array4< const Real > &rho_v, const Array4< const Real > &rho_w, const Array4< const Real > &r0, const Array4< const Real > &z_phys_nd, const Array4< const Real > &z_phys_cc)
Apply sponge zone damping to momentum variables.
Definition: ERF_ApplySpongeZoneBCs.cpp:211
void ApplySpongeZoneBCsForMom_ReadFromFile(const SpongeChoice &spongeChoice, const Geometry geom, const Box &tbx, const Box &tby, const Array4< const Real > &cell_data, const Array4< const Real > &z_phys_cc, const Array4< Real > &rho_u_rhs, const Array4< Real > &rho_v_rhs, const Array4< const Real > &rho_u, const Array4< const Real > &rho_v, const Vector< Real * > d_sponge_ptrs_at_lev)
Definition: ERF_ApplySpongeZoneBCs_ReadFromFile.cpp:23
@ ubar
Definition: ERF_DataStruct.H:235
@ wbar
Definition: ERF_DataStruct.H:235
@ vbar
Definition: ERF_DataStruct.H:235
@ v_x
Definition: ERF_DataStruct.H:40
@ u_y
Definition: ERF_DataStruct.H:41
@ v_y
Definition: ERF_DataStruct.H:41
@ u_x
Definition: ERF_DataStruct.H:40
DirectionSelector< 2 > ZDir
Definition: ERF_DirectionSelector.H:55
void ImmersedForcingTerrain_Ymom_FractionStress(const Box &tby, const Array4< const Real > &u, const Array4< const Real > &v, const Array4< const Real > &w, const Array4< const Real > &cell_data, const Array4< const Real > &t_blank_arr, const Array4< const Real > &t_blank_yface_arr, const Array4< Real > &ymom_src_arr, const Geometry &geom, const SolverChoice &solverChoice, const Real fac, const Real time, const bool l_drag_in_projection)
Definition: ERF_ImmersedForcing.cpp:511
void ImmersedForcingTerrain_Ymom(const Box &tby, const Array4< const Real > &u, const Array4< const Real > &v, const Array4< const Real > &w, const Array4< const Real > &cell_data, const Array4< const Real > &t_blank_arr, const Array4< const Real > &t_blank_yface_arr, const Array4< const Real > &z_cc_arr, const Array4< Real > &ymom_src_arr, const Geometry &geom, const SolverChoice &solverChoice, const Real fac, const bool l_drag_in_projection)
Definition: ERF_ImmersedForcing.cpp:209
void ImmersedForcingBuildings_Ymom(const Box &tby, const Array4< const Real > &u, const Array4< const Real > &v, const Array4< const Real > &w, const Array4< const Real > &cell_data, const Array4< const Real > &t_blank_arr, const Array4< const Real > &t_blank_yface_arr, const Array4< const Real > &z_cc_arr, const Array4< Real > &ymom_src_arr, const Geometry &geom, const SolverChoice &solverChoice, const Real fac, bool is_slow_step, const bool l_drag_in_projection)
Definition: ERF_ImmersedForcing.cpp:904
void ImmersedForcingBuildings_Xmom(const Box &tbx, const Array4< const Real > &u, const Array4< const Real > &v, const Array4< const Real > &w, const Array4< const Real > &cell_data, const Array4< const Real > &t_blank_arr, const Array4< const Real > &t_blank_xface_arr, const Array4< const Real > &z_cc_arr, const Array4< Real > &xmom_src_arr, const Geometry &geom, const SolverChoice &solverChoice, const Real fac, bool is_slow_step, const bool l_drag_in_projection)
Definition: ERF_ImmersedForcing.cpp:701
void ImmersedForcingBuildings_Zmom(const Box &tbz, const Array4< const Real > &u, const Array4< const Real > &v, const Array4< const Real > &w, const Array4< const Real > &cell_data, const Array4< const Real > &t_blank_arr, const Array4< const Real > &t_blank_zface_arr, const Array4< const Real > &z_cc_arr, const Array4< Real > &zmom_src_arr, const Geometry &geom, const SolverChoice &solverChoice, const Real fac, bool is_slow_step, const bool l_drag_in_projection)
Definition: ERF_ImmersedForcing.cpp:1105
void ImmersedForcingTerrain_Zmom(const Box &tbz, const Array4< const Real > &u, const Array4< const Real > &v, const Array4< const Real > &w, const Array4< const Real > &cell_data, const Array4< const Real > &t_blank_arr, const Array4< const Real > &t_blank_zface_arr, const Array4< const Real > &z_cc_arr, const Array4< Real > &zmom_src_arr, const Geometry &geom, const SolverChoice &solverChoice, const Real fac, const bool l_drag_in_projection)
Definition: ERF_ImmersedForcing.cpp:637
void ImmersedForcingTerrain_Xmom(const Box &tbx, const Array4< const Real > &u, const Array4< const Real > &v, const Array4< const Real > &w, const Array4< const Real > &cell_data, const Array4< const Real > &t_blank_arr, const Array4< const Real > &t_blank_xface_arr, const Array4< const Real > &z_cc_arr, const Array4< Real > &xmom_src_arr, const Geometry &geom, const SolverChoice &solverChoice, const Real fac, const bool l_drag_in_projection)
Definition: ERF_ImmersedForcing.cpp:83
void ImmersedForcingTerrain_Xmom_FractionStress(const Box &tbx, const Array4< const Real > &u, const Array4< const Real > &v, const Array4< const Real > &w, const Array4< const Real > &cell_data, const Array4< const Real > &t_blank_arr, const Array4< const Real > &t_blank_xface_arr, const Array4< Real > &xmom_src_arr, const Geometry &geom, const SolverChoice &solverChoice, const Real fac, const Real time, const bool l_drag_in_projection)
Definition: ERF_ImmersedForcing.cpp:493
#define Rho_comp
Definition: ERF_IndexDefines.H:61
amrex::GpuArray< Real, AMREX_SPACEDIM > dxInv
Definition: ERF_InitCustomPertVels_ParticleTests.H:16
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(axis >=0 &&axis< AMREX_SPACEDIM, "prob.axis must be 0, 1 or 2")
Real H
Definition: ERF_InitCustomPert_MovingTerrain.H:8
ParallelFor(fab_box, [=] AMREX_GPU_DEVICE(int i, int j, int k) { qrcuten_arr(i, j, k)=Real(0);qscuten_arr(i, j, k)=Real(0);qicuten_arr(i, j, k)=Real(0);})
constexpr amrex::Real one
Definition: ERF_NumericalConstants.H:37
constexpr amrex::Real fourth
Definition: ERF_NumericalConstants.H:42
constexpr amrex::Real zero
Definition: ERF_NumericalConstants.H:36
constexpr amrex::Real myhalf
Definition: ERF_NumericalConstants.H:41
constexpr amrex::Real PI
Definition: ERF_NumericalConstants.H:46
void NumericalDiffusion_Ymom(const Box &bx, const double dt, const Real num_diff_coeff, const Array4< const Real > &prim_data, const Array4< const Real > &cell_data, const Array4< Real > &rhs, const Array4< const Real > &mfx_arr, const Array4< const Real > &mfy_arr)
Definition: ERF_NumericalDiffusion.cpp:151
void NumericalDiffusion_Xmom(const Box &bx, const double dt, const Real num_diff_coeff, const Array4< const Real > &prim_data, const Array4< const Real > &cell_data, const Array4< Real > &rhs, const Array4< const Real > &mfx_arr, const Array4< const Real > &mfy_arr)
Definition: ERF_NumericalDiffusion.cpp:85
AMREX_FORCE_INLINE IntVect offset(const int face_dir, const int normal)
Definition: ERF_ReadBndryPlanes.cpp:36
Definition: ERF_PlaneAverage.H:14
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
@ r0_comp
Definition: ERF_IndexDefines.H:109
@ ymom
Definition: ERF_IndexDefines.H:251
@ cons
Definition: ERF_IndexDefines.H:249
@ zmom
Definition: ERF_IndexDefines.H:252
@ xmom
Definition: ERF_IndexDefines.H:250
@ nr
Definition: ERF_Morrison.H:48
@ xvel
Definition: ERF_IndexDefines.H:232
@ cons
Definition: ERF_IndexDefines.H:231
@ yvel
Definition: ERF_IndexDefines.H:233
@ ww
Definition: ERF_AdvanceWSM6.cpp:107
real(c_double), private k1
Definition: ERF_module_mp_morr_two_moment.F90:213
real(kind=kind_phys), parameter, private r0
Definition: ERF_module_mp_wdm6.F90:75
amrex::Real rayleigh_dampcoef
Rayleigh damping inverse time scale [1/s].
Definition: ERF_DampingStruct.H:103
bool rayleigh_damp_U
Whether Rayleigh damping is applied to x-momentum.
Definition: ERF_DampingStruct.H:99
bool rayleigh_damp_V
Whether Rayleigh damping is applied to y-momentum.
Definition: ERF_DampingStruct.H:100
bool rayleigh_damp_W
Whether Rayleigh damping is applied to vertical momentum.
Definition: ERF_DampingStruct.H:101
amrex::Vector< amrex::Real > input_sounding_time
Physical time associated with each input sounding file.
Definition: ERF_InputSoundingData.H:550
amrex::Vector< amrex::Gpu::DeviceVector< amrex::Real > > V_inp_sound_d
Device y velocity profiles.
Definition: ERF_InputSoundingData.H:568
amrex::Vector< amrex::Gpu::DeviceVector< amrex::Real > > U_inp_sound_d
Device x velocity profiles.
Definition: ERF_InputSoundingData.H:568
amrex::Real tau_nudging
Nudging time scale for relaxing toward input sounding data.
Definition: ERF_InputSoundingData.H:545
void get_forcing_time_coeffs(const amrex::Real &time, int &curr, int &next, amrex::Real &coeff_curr, amrex::Real &coeff_next) const
Determine interpolation coefficients to apply tendencies for the given time.
Definition: ERF_LargeScaleForcingData.H:337
amrex::Vector< amrex::Gpu::DeviceVector< amrex::Real > > v_int_lsf_d
Definition: ERF_LargeScaleForcingData.H:459
amrex::Real tau_lsf
Definition: ERF_LargeScaleForcingData.H:409
amrex::Vector< amrex::Gpu::DeviceVector< amrex::Real > > u_int_lsf_d
Definition: ERF_LargeScaleForcingData.H:459
amrex::Real const_massflux_v
Target constant mass flux in the y direction.
Definition: ERF_DataStruct.H:3552
static InitType init_type
Initial-condition source selected for the run.
Definition: ERF_DataStruct.H:2983
amrex::Real coriolis_factor
Twice the planetary rotation rate used for Coriolis forcing.
Definition: ERF_DataStruct.H:3157
bool if_drag_in_projection(int lev) const noexcept
Definition: ERF_DataStruct.H:3223
static bool flat_terrain
Permit fixed-index planar averages after validating a flat fitted mesh.
Definition: ERF_DataStruct.H:2992
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
bool rayleigh_fast_uvT(int lev) const noexcept
Definition: ERF_DataStruct.H:3248
bool variable_coriolis
Whether spatially varying Coriolis forcing is enabled.
Definition: ERF_DataStruct.H:3499
int ave_plane
Averaging plane index used by diagnostics.
Definition: ERF_DataStruct.H:3501
amrex::Real const_massflux_u
Target constant mass flux in the x direction.
Definition: ERF_DataStruct.H:3551
bool forest_fast(int lev) const noexcept
Definition: ERF_DataStruct.H:3297
bool use_building_immersed_forcing(const amrex::Geometry &geom) const noexcept
Definition: ERF_DataStruct.H:3231
bool rayleigh_implicit_w(int lev) const noexcept
Definition: ERF_DataStruct.H:3277
amrex::Real num_diff_coeff
Numerical diffusion coefficient after input scaling.
Definition: ERF_DataStruct.H:3430
bool hindcast_lateral_forcing
Whether hindcast lateral forcing is enabled.
Definition: ERF_DataStruct.H:3561
amrex::Real nudging_u_z2
Definition: ERF_DataStruct.H:3181
bool use_coriolis
Whether Coriolis forcing is enabled.
Definition: ERF_DataStruct.H:3096
static MeshType mesh_type
Vertical mesh representation.
Definition: ERF_DataStruct.H:3004
SpongeChoice spongeChoice
Sponge-layer options.
Definition: ERF_DataStruct.H:3019
bool have_geo_wind_profile
Whether a geostrophic wind profile has been configured.
Definition: ERF_DataStruct.H:3497
DampingChoice dampingChoice
Damping-related options.
Definition: ERF_DataStruct.H:3018
amrex::Real nudging_u_z1
Definition: ERF_DataStruct.H:3180
bool do_forest_drag
Whether forest canopy drag is enabled.
Definition: ERF_DataStruct.H:3541
int massflux_khi
Upper vertical index for constant-mass-flux forcing.
Definition: ERF_DataStruct.H:3557
bool large_scale_forcing
Definition: ERF_DataStruct.H:3191
static TerrainType terrain_type
Terrain or immersed-boundary representation.
Definition: ERF_DataStruct.H:2989
amrex::Real cosphi
Cosine of the latitude used for Coriolis forcing.
Definition: ERF_DataStruct.H:3158
bool immersed_forcing_fast(int lev) const noexcept
Definition: ERF_DataStruct.H:3303
bool rayleigh_fast_w(int lev) const noexcept
Definition: ERF_DataStruct.H:3256
int massflux_klo
Lower vertical index for constant-mass-flux forcing.
Definition: ERF_DataStruct.H:3556
amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > abl_geo_forcing
Applied geostrophic-wind forcing vector.
Definition: ERF_DataStruct.H:3495
bool custom_w_subsidence
Whether custom vertical subsidence is enabled.
Definition: ERF_DataStruct.H:3164
bool rayleigh_slow_implicit_w(int lev) const noexcept
Definition: ERF_DataStruct.H:3288
amrex::Real const_massflux_tau
Relaxation time scale for constant-mass-flux forcing.
Definition: ERF_DataStruct.H:3553
amrex::Real sinphi
Sine of the latitude used for Coriolis forcing.
Definition: ERF_DataStruct.H:3159
bool do_mom_advection
Whether custom vertical subsidence is applied to momentum.
Definition: ERF_DataStruct.H:3166
bool nudging_u
Definition: ERF_DataStruct.H:3187
bool nudging_from_input_sounding
Whether solution fields are nudged toward input sounding data.
Definition: ERF_DataStruct.H:3174
static SpongeType sponge_type
Selected sponge damping model.
Definition: ERF_SpongeStruct.H:100
Here is the call graph for this function: