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 "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 (Real time, Real, 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 *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 MultiFab *surface_state_at_lev, InputSoundingData &input_sounding_data, const eb_ &ebfact, bool is_slow_step)
 

Function Documentation

◆ make_mom_sources()

void make_mom_sources ( Real  time,
Real  ,
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 *  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 MultiFab *  surface_state_at_lev,
InputSoundingData input_sounding_data,
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]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
69 {
70  BL_PROFILE_REGION("erf_make_mom_sources()");
71 
72  Box domain(geom.Domain());
73  const GpuArray<Real, AMREX_SPACEDIM> dxInv = geom.InvCellSizeArray();
74 
75  // Initialize sources to zero each time we may use them
76  xmom_src.setVal(0.0);
77  ymom_src.setVal(0.0);
78  zmom_src.setVal(0.0);
79 
80  MultiFab r_hse (base_state, make_alias, BaseState::r0_comp , 1);
81 
82  // flags to apply certain source terms in substep call only
83  bool use_Rayleigh_fast_uv = ( (solverChoice.dampingChoice.rayleigh_damping_type == RayleighDampingType::FastExplicit) ||
85  bool use_Rayleigh_fast_w = (solverChoice.dampingChoice.rayleigh_damping_type == RayleighDampingType::FastExplicit);
86  bool use_canopy_fast = solverChoice.forest_substep;
87  bool use_ImmersedForcing_fast = solverChoice.immersed_forcing_substep;
88 
89  // *****************************************************************************
90  // Define source term for all three components of momenta from
91  // one Coriolis forcing for (xmom,ymom,zmom)
92  // two Rayleigh damping for (xmom,ymom,zmom)
93  // three Constant / height-dependent geostrophic forcing
94  // Real(4.) Subsidence
95  // Real(5.) Nudging towards input sounding data
96  // Real(6.) Numerical diffusion for (xmom,ymom,zmom)
97  // Real(7.) Sponge
98  // Real(8.) Forest canopy
99  // 9a. Immersed forcing for terrain
100  // 9b. Immersed forcing for buildings
101  // Real(10.) Constant mass flux
102  // *****************************************************************************
103  // NOTE: buoyancy is now computed in a separate routine - it should not appear here
104  // *****************************************************************************
105  //const bool l_use_ndiff = solverChoice.use_num_diff;
106 
107  if (solverChoice.terrain_type == TerrainType::ImmersedForcing) {
108  if (solverChoice.do_forest_drag) {
109  amrex::Error(" Currently forest canopy cannot be used with immersed forcing");
110  }
111  }
112 
113 
114  // *****************************************************************************
115  // Data for Coriolis forcing
116  // *****************************************************************************
117  auto use_coriolis = solverChoice.use_coriolis;
118  auto coriolis_factor = solverChoice.coriolis_factor;
119  auto cosphi = solverChoice.cosphi;
120  auto sinphi = solverChoice.sinphi;
121  auto var_coriolis = solverChoice.variable_coriolis;
122 
123  // *****************************************************************************
124  // Flag for Geostrophic forcing
125  // *****************************************************************************
126  auto abl_geo_forcing = solverChoice.abl_geo_forcing;
127  auto geo_wind_profile = solverChoice.have_geo_wind_profile;
128 
129  // *****************************************************************************
130  // Data for Rayleigh damping
131  // *****************************************************************************
132  auto rayleigh_damp_U = solverChoice.dampingChoice.rayleigh_damp_U;
133  auto rayleigh_damp_V = solverChoice.dampingChoice.rayleigh_damp_V;
134  auto rayleigh_damp_W = solverChoice.dampingChoice.rayleigh_damp_W;
135 
136  Real* ubar = d_rayleigh_ptrs_at_lev[Rayleigh::ubar];
137  Real* vbar = d_rayleigh_ptrs_at_lev[Rayleigh::vbar];
138  Real* wbar = d_rayleigh_ptrs_at_lev[Rayleigh::wbar];
139 
140  // *****************************************************************************
141  // Data for constant mass flux
142  // *****************************************************************************
143  bool enforce_massflux_x = (solverChoice.const_massflux_u != 0);
144  bool enforce_massflux_y = (solverChoice.const_massflux_v != 0);
145  Real U_target = solverChoice.const_massflux_u;
146  Real V_target = solverChoice.const_massflux_v;
147  int massflux_klo = solverChoice.massflux_klo;
148  int massflux_khi = solverChoice.massflux_khi;
149 
150  // These will be updated by integrating through the planar average profiles
151  Real rhoUA_target{0};
152  Real rhoVA_target{0};
153  Real rhoUA{0};
154  Real rhoVA{0};
155 
156  // *****************************************************************************
157  // Planar averages for subsidence, nudging, or constant mass flux
158  // *****************************************************************************
159  Table1D<Real> dptr_r_plane, dptr_u_plane, dptr_v_plane;
160  TableData<Real, 1> r_plane_tab, u_plane_tab, v_plane_tab;
161 
162  if (is_slow_step && (dptr_wbar_sub || solverChoice.nudging_from_input_sounding ||
163  enforce_massflux_x || enforce_massflux_y))
164  {
165  const int offset = 1;
166  const int u_offset = 1;
167  const int v_offset = 1;
168 
169  //
170  // We use the alias here to control ncomp inside the PlaneAverage
171  //
172  MultiFab cons(S_data[IntVars::cons], make_alias, 0, 1);
173 
174  IntVect ng_c = S_data[IntVars::cons].nGrowVect(); ng_c[2] = offset;
175  PlaneAverage r_ave(&cons, geom, solverChoice.ave_plane, ng_c);
176  r_ave.compute_averages(ZDir(), r_ave.field());
177 
178  int ncell = r_ave.ncell_line();
179  Gpu::HostVector< Real> r_plane_h(ncell);
180  Gpu::DeviceVector< Real> r_plane_d(ncell);
181 
182  r_ave.line_average(Rho_comp, r_plane_h);
183 
184  Gpu::copyAsync(Gpu::hostToDevice, r_plane_h.begin(), r_plane_h.end(), r_plane_d.begin());
185 
186  Real* dptr_r = r_plane_d.data();
187 
188  Box tdomain = domain; tdomain.grow(2,ng_c[2]);
189  r_plane_tab.resize({tdomain.smallEnd(2)}, {tdomain.bigEnd(2)});
190 
191  dptr_r_plane = r_plane_tab.table();
192  ParallelFor(ncell, [=] AMREX_GPU_DEVICE (int k) noexcept
193  {
194  dptr_r_plane(k-offset) = dptr_r[k];
195  });
196 
197  // U and V momentum
198  IntVect ng_u = S_data[IntVars::xmom].nGrowVect(); ng_u[2] = u_offset;
199  PlaneAverage u_ave(&(S_data[IntVars::xmom]), geom, solverChoice.ave_plane, ng_u);
200 
201  IntVect ng_v = S_data[IntVars::ymom].nGrowVect(); ng_v[2] = v_offset;
202  PlaneAverage v_ave(&(S_data[IntVars::ymom]), geom, solverChoice.ave_plane, ng_v);
203 
204  u_ave.compute_averages(ZDir(), u_ave.field());
205  v_ave.compute_averages(ZDir(), v_ave.field());
206 
207  int u_ncell = u_ave.ncell_line();
208  int v_ncell = v_ave.ncell_line();
209  Gpu::HostVector< Real> u_plane_h(u_ncell), v_plane_h(v_ncell);
210  Gpu::DeviceVector< Real> u_plane_d(u_ncell), v_plane_d(v_ncell);
211 
212  u_ave.line_average(0, u_plane_h);
213  v_ave.line_average(0, v_plane_h);
214 
215  Gpu::copyAsync(Gpu::hostToDevice, u_plane_h.begin(), u_plane_h.end(), u_plane_d.begin());
216  Gpu::copyAsync(Gpu::hostToDevice, v_plane_h.begin(), v_plane_h.end(), v_plane_d.begin());
217 
218  Real* dptr_u = u_plane_d.data();
219  Real* dptr_v = v_plane_d.data();
220 
221  Box udomain = domain; udomain.grow(2,ng_u[2]);
222  Box vdomain = domain; vdomain.grow(2,ng_v[2]);
223  u_plane_tab.resize({udomain.smallEnd(2)}, {udomain.bigEnd(2)});
224  v_plane_tab.resize({vdomain.smallEnd(2)}, {vdomain.bigEnd(2)});
225 
226  dptr_u_plane = u_plane_tab.table();
227  ParallelFor(u_ncell, [=] AMREX_GPU_DEVICE (int k) noexcept
228  {
229  dptr_u_plane(k-u_offset) = dptr_u[k];
230  });
231 
232  dptr_v_plane = v_plane_tab.table();
233  ParallelFor(v_ncell, [=] AMREX_GPU_DEVICE (int k) noexcept
234  {
235  dptr_v_plane(k-v_offset) = dptr_v[k];
236  });
237 
238  // sum in z for massflux adjustment
239  if (enforce_massflux_x || enforce_massflux_y) {
240  Real Lx = geom.ProbHi(0) - geom.ProbLo(0);
241  Real Ly = geom.ProbHi(1) - geom.ProbLo(1);
242 
243  if (solverChoice.mesh_type == MeshType::ConstantDz) {
244  // note: massflux_khi corresponds to unstaggered indices in this case
245  rhoUA = std::accumulate(u_plane_h.begin() + u_offset + massflux_klo,
246  u_plane_h.begin() + u_offset + massflux_khi+1, zero);
247  rhoVA = std::accumulate(v_plane_h.begin() + v_offset + massflux_klo,
248  v_plane_h.begin() + v_offset + massflux_khi+1, zero);
249  rhoUA_target = std::accumulate(r_plane_h.begin() + offset + massflux_klo,
250  r_plane_h.begin() + offset + massflux_khi+1, zero);
251  rhoVA_target = rhoUA_target;
252 
253  rhoUA *= geom.CellSize(2) * Ly;
254  rhoVA *= geom.CellSize(2) * Lx;
255  rhoUA_target *= geom.CellSize(2) * Ly;
256  rhoVA_target *= geom.CellSize(2) * Lx;
257 
258  } else if (solverChoice.mesh_type == MeshType::StretchedDz) {
259  // note: massflux_khi corresponds to staggered indices in this case
260  for (int k=massflux_klo; k < massflux_khi; ++k) {
261  rhoUA += u_plane_h[k + u_offset] * stretched_dz_h[k];
262  rhoVA += v_plane_h[k + v_offset] * stretched_dz_h[k];
263  rhoUA_target += r_plane_h[k + offset] * stretched_dz_h[k];
264  }
265  rhoVA_target = rhoUA_target;
266 
267  rhoUA *= Ly;
268  rhoVA *= Lx;
269  rhoUA_target *= Ly;
270  rhoVA_target *= Lx;
271  }
272 
273  // at this point, this is integrated rho*dA
274  rhoUA_target *= U_target;
275  rhoVA_target *= V_target;
276 
277  Print() << "Integrated mass flux : " << rhoUA << " " << rhoVA
278  << " (target: " << rhoUA_target << " " << rhoVA_target << ")"
279  << std::endl;
280  }
281  }
282 
283  // *****************************************************************************
284  // Add all the other forcings
285  // *****************************************************************************
286  for ( MFIter mfi(S_data[IntVars::cons]); mfi.isValid(); ++mfi)
287  {
288  Box tbx = mfi.nodaltilebox(0);
289  Box tby = mfi.nodaltilebox(1);
290  Box tbz = mfi.nodaltilebox(2);
291  if (tbz.bigEnd(2) == domain.bigEnd(2)+1) tbz.growHi(2,-1);
292 
293  const Array4<const Real>& cell_data = S_data[IntVars::cons].array(mfi);
294  const Array4<const Real>& rho_u = S_data[IntVars::xmom].array(mfi);
295  const Array4<const Real>& rho_v = S_data[IntVars::ymom].array(mfi);
296  const Array4<const Real>& rho_w = S_data[IntVars::zmom].array(mfi);
297 
298  const Array4<const Real>& u = xvel.array(mfi);
299  const Array4<const Real>& v = yvel.array(mfi);
300  const Array4<const Real>& w = wvel.array(mfi);
301 
302  const Array4< Real>& xmom_src_arr = xmom_src.array(mfi);
303  const Array4< Real>& ymom_src_arr = ymom_src.array(mfi);
304  const Array4< Real>& zmom_src_arr = zmom_src.array(mfi);
305 
306  const Array4<const Real>& r0 = r_hse.const_array(mfi);
307 
308  const Array4<const Real>& f_drag_arr = (forest_drag) ? forest_drag->const_array(mfi) :
309  Array4<const Real>{};
310  const Array4<const Real>& t_blank_arr = (terrain_blank) ? terrain_blank->const_array(mfi) :
311  Array4<const Real>{};
312 
313  const Array4<const Real>& cphi_arr = (cosPhi_mf) ? cosPhi_mf->const_array(mfi) :
314  Array4<const Real>{};
315  const Array4<const Real>& sphi_arr = (sinPhi_mf) ? sinPhi_mf->const_array(mfi) :
316  Array4<const Real>{};
317 
318  const Array4<const Real>& z_nd_arr = z_phys_nd->const_array(mfi);
319  const Array4<const Real>& z_cc_arr = z_phys_cc->const_array(mfi);
320 
321 
322  // *****************************************************************************
323  // one Add CORIOLIS forcing (this assumes east is +x, north is +y)
324  // *****************************************************************************
325  if (use_coriolis && is_slow_step) {
326  if(solverChoice.init_type == InitType::HindCast) {
327  const Array4<const Real>& latlon_arr = (*forecast_state_at_lev)[4].array(mfi);
328  ParallelFor(tbx, tby, tbz,
329  [=] AMREX_GPU_DEVICE (int i, int j, int k)
330  {
331  Real rho_v_loc = fourth * (rho_v(i,j+1,k) + rho_v(i,j,k) + rho_v(i-1,j+1,k) + rho_v(i-1,j,k));
332  Real rho_w_loc = fourth * (rho_w(i,j,k+1) + rho_w(i,j,k) + rho_w(i,j-1,k+1) + rho_w(i,j-1,k));
333  Real latitude = latlon_arr(i,j,k,0);
334  Real sphi_loc = std::sin(latitude*PI/Real(180.0));
335  Real cphi_loc = std::cos(latitude*PI/Real(180.0));
336  xmom_src_arr(i, j, k) += coriolis_factor * (rho_v_loc * sphi_loc - rho_w_loc * cphi_loc);
337  },
338  [=] AMREX_GPU_DEVICE (int i, int j, int k) {
339  Real rho_u_loc = fourth * (rho_u(i+1,j,k) + rho_u(i,j,k) + rho_u(i+1,j-1,k) + rho_u(i,j-1,k));
340  Real latitude = latlon_arr(i,j,k,0);
341  Real sphi_loc = std::sin(latitude*PI/Real(180.0));
342  ymom_src_arr(i, j, k) += -coriolis_factor * rho_u_loc * sphi_loc;
343  },
344  [=] AMREX_GPU_DEVICE (int i, int j, int k) {
345  Real rho_u_loc = fourth * (rho_u(i+1,j,k) + rho_u(i,j,k) + rho_u(i+1,j,k-1) + rho_u(i,j,k-1));
346  Real latitude = latlon_arr(i,j,k,0);
347  Real cphi_loc = std::cos(latitude*PI/Real(180.0));
348  zmom_src_arr(i, j, k) += coriolis_factor * rho_u_loc * cphi_loc;
349  });
350  }
351  else if (var_coriolis && (sinPhi_mf) && (cosPhi_mf)) {
352  ParallelFor(tbx, tby, tbz,
353  [=] AMREX_GPU_DEVICE (int i, int j, int k)
354  {
355  Real rho_v_loc = fourth * (rho_v(i,j+1,k) + rho_v(i,j,k) + rho_v(i-1,j+1,k) + rho_v(i-1,j,k));
356  Real rho_w_loc = fourth * (rho_w(i,j,k+1) + rho_w(i,j,k) + rho_w(i-1,j,k+1) + rho_w(i-1,j,k));
357  Real sphi_loc = myhalf * (sphi_arr(i,j,0) + sphi_arr(i-1,j,0));
358  Real cphi_loc = myhalf * (cphi_arr(i,j,0) + cphi_arr(i-1,j,0));
359  xmom_src_arr(i, j, k) += coriolis_factor * (rho_v_loc * sphi_loc - rho_w_loc * cphi_loc);
360  },
361  [=] AMREX_GPU_DEVICE (int i, int j, int k) {
362  Real rho_u_loc = fourth * (rho_u(i+1,j,k) + rho_u(i,j,k) + rho_u(i+1,j-1,k) + rho_u(i,j-1,k));
363  Real sphi_loc = myhalf * (sphi_arr(i,j,0) + sphi_arr(i,j-1,0));
364  ymom_src_arr(i, j, k) += -coriolis_factor * rho_u_loc * sphi_loc;
365  },
366  [=] AMREX_GPU_DEVICE (int i, int j, int k) {
367  Real rho_u_loc = fourth * (rho_u(i+1,j,k) + rho_u(i,j,k) + rho_u(i+1,j,k-1) + rho_u(i,j,k-1));
368  zmom_src_arr(i, j, k) += coriolis_factor * rho_u_loc * cphi_arr(i,j,0);
369  });
370  } else {
371  if (solverChoice.terrain_type == TerrainType::EB) {
372  Array4<const Real> u_volfrac = (ebfact.get_u_const_factory())->getVolFrac().const_array(mfi);
373  Array4<const Real> v_volfrac = (ebfact.get_v_const_factory())->getVolFrac().const_array(mfi);
374  Array4<const Real> w_volfrac = (ebfact.get_w_const_factory())->getVolFrac().const_array(mfi);
375  ParallelFor(tbx, [=] AMREX_GPU_DEVICE (int i, int j, int k) {
376  Real rho_v_loc = 0.0;
377  Real rho_w_loc = 0.0;
378  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);
379  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);
380  if (v_vol > 0.0) {
381  rho_v_loc = ( v_volfrac(i,j+1,k) * rho_v(i,j+1,k) + v_volfrac(i,j,k) * rho_v(i,j,k)
382  + v_volfrac(i-1,j+1,k) * rho_v(i-1,j+1,k) + v_volfrac(i-1,j,k) * rho_v(i-1,j,k)) / v_vol;
383  }
384  if (w_vol > 0.0) {
385  rho_w_loc = ( w_volfrac(i,j,k+1) * rho_w(i,j,k+1) + w_volfrac(i,j,k) * rho_w(i,j,k)
386  + w_volfrac(i-1,j,k+1) * rho_w(i-1,j,k+1) + w_volfrac(i-1,j,k) * rho_w(i-1,j,k)) / w_vol;
387  }
388  xmom_src_arr(i, j, k) += coriolis_factor * (rho_v_loc * sinphi - rho_w_loc * cosphi);
389  });
390  ParallelFor(tby, [=] AMREX_GPU_DEVICE (int i, int j, int k) {
391  Real rho_u_loc = 0.0;
392  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);
393  if (u_vol > 0.0) {
394  rho_u_loc = ( u_volfrac(i+1,j,k) * rho_u(i+1,j,k) + u_volfrac(i,j,k) * rho_u(i,j,k)
395  + u_volfrac(i+1,j-1,k) * rho_u(i+1,j-1,k) + u_volfrac(i,j-1,k) * rho_u(i,j-1,k)) / u_vol;
396  }
397  ymom_src_arr(i, j, k) += -coriolis_factor * rho_u_loc * sinphi;
398  });
399  ParallelFor(tbz, [=] AMREX_GPU_DEVICE (int i, int j, int k) {
400  Real rho_u_loc = 0.0;
401  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);
402  if (u_vol > 0.0) {
403  rho_u_loc = ( u_volfrac(i+1,j,k) * rho_u(i+1,j,k) + u_volfrac(i,j,k) * rho_u(i,j,k)
404  + u_volfrac(i+1,j,k-1) * rho_u(i+1,j,k-1) + u_volfrac(i,j,k-1) * rho_u(i,j,k-1)) / u_vol;
405  }
406  zmom_src_arr(i, j, k) += coriolis_factor * rho_u_loc * cosphi;
407  });
408  } else {
409  ParallelFor(tbx, tby, tbz,
410  [=] AMREX_GPU_DEVICE (int i, int j, int k)
411  {
412  Real rho_v_loc = 0.25 * (rho_v(i,j+1,k) + rho_v(i,j,k) + rho_v(i-1,j+1,k) + rho_v(i-1,j,k));
413  Real rho_w_loc = 0.25 * (rho_w(i,j,k+1) + rho_w(i,j,k) + rho_w(i-1,j,k+1) + rho_w(i-1,j,k));
414  xmom_src_arr(i, j, k) += coriolis_factor * (rho_v_loc * sinphi - rho_w_loc * cosphi);
415  },
416  [=] AMREX_GPU_DEVICE (int i, int j, int k) {
417  Real rho_u_loc = 0.25 * (rho_u(i+1,j,k) + rho_u(i,j,k) + rho_u(i+1,j-1,k) + rho_u(i,j-1,k));
418  ymom_src_arr(i, j, k) += -coriolis_factor * rho_u_loc * sinphi;
419  },
420  [=] AMREX_GPU_DEVICE (int i, int j, int k) {
421  Real rho_u_loc = 0.25 * (rho_u(i+1,j,k) + rho_u(i,j,k) + rho_u(i+1,j,k-1) + rho_u(i,j,k-1));
422  zmom_src_arr(i, j, k) += coriolis_factor * rho_u_loc * cosphi;
423  });
424  }
425  } // var_coriolis
426  } // use_coriolis
427 
428  // *****************************************************************************
429  // two Add RAYLEIGH damping
430  // *****************************************************************************
431  Real dampcoef = solverChoice.dampingChoice.rayleigh_dampcoef;
432 
433  if ( (is_slow_step && !use_Rayleigh_fast_uv) || (!is_slow_step && use_Rayleigh_fast_uv)) {
434  if (rayleigh_damp_U) {
435  ParallelFor(tbx, [=] AMREX_GPU_DEVICE (int i, int j, int k)
436  {
437  Real rho_on_u_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i-1,j,k,Rho_comp) );
438  Real uu = rho_u(i,j,k) / rho_on_u_face;
439  Real sinesq = d_sinesq_at_lev[k];
440  xmom_src_arr(i, j, k) -= dampcoef*sinesq * (uu - ubar[k]) * rho_on_u_face;
441  });
442  }
443 
444  if (rayleigh_damp_V) {
445  ParallelFor(tby, [=] AMREX_GPU_DEVICE (int i, int j, int k)
446  {
447  Real rho_on_v_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i,j-1,k,Rho_comp) );
448  Real vv = rho_v(i,j,k) / rho_on_v_face;
449  Real sinesq = d_sinesq_at_lev[k];
450  ymom_src_arr(i, j, k) -= dampcoef*sinesq * (vv - vbar[k]) * rho_on_v_face;
451  });
452  }
453  } // fast or slow step
454 
455  if ( (is_slow_step && !use_Rayleigh_fast_w) || (!is_slow_step && use_Rayleigh_fast_w)) {
456  if (rayleigh_damp_W) {
457  ParallelFor(tbz, [=] AMREX_GPU_DEVICE (int i, int j, int k)
458  {
459  Real rho_on_w_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i,j,k-1,Rho_comp) );
460  Real ww = rho_w(i,j,k) / rho_on_w_face;
461  Real sinesq = d_sinesq_stag_at_lev[k];
462  zmom_src_arr(i, j, k) -= dampcoef*sinesq * (ww - wbar[k]) * rho_on_w_face;
463  });
464  }
465  } // fast or slow step
466 
467  // *****************************************************************************
468  // 3a. Add constant GEOSTROPHIC forcing
469  // *****************************************************************************
470  if (is_slow_step) {
471  ParallelFor(tbx, tby, tbz,
472  [=] AMREX_GPU_DEVICE (int i, int j, int k)
473  {
474  Real rho_on_u_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i-1,j,k,Rho_comp) );
475  xmom_src_arr(i, j, k) += rho_on_u_face * abl_geo_forcing[0];
476  },
477  [=] AMREX_GPU_DEVICE (int i, int j, int k)
478  {
479  Real rho_on_v_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i,j-1,k,Rho_comp) );
480  ymom_src_arr(i, j, k) += rho_on_v_face * abl_geo_forcing[1];
481  },
482  [=] AMREX_GPU_DEVICE (int i, int j, int k)
483  {
484  Real rho_on_w_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i,j,k-1,Rho_comp) );
485  zmom_src_arr(i, j, k) += rho_on_w_face * abl_geo_forcing[2];
486  });
487  }
488 
489  // *****************************************************************************
490  // 3b. Add height-dependent GEOSTROPHIC forcing
491  // *****************************************************************************
492  if (geo_wind_profile && is_slow_step) {
493  ParallelFor(tbx, tby,
494  [=] AMREX_GPU_DEVICE (int i, int j, int k)
495  {
496  Real rho_on_u_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i-1,j,k,Rho_comp) );
497  xmom_src_arr(i, j, k) -= coriolis_factor * rho_on_u_face * dptr_v_geos[k] * sinphi;
498  },
499  [=] AMREX_GPU_DEVICE (int i, int j, int k)
500  {
501  Real rho_on_v_face = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i,j-1,k,Rho_comp) );
502  ymom_src_arr(i, j, k) += coriolis_factor * rho_on_v_face * dptr_u_geos[k] * sinphi;
503  });
504  } // geo_wind_profile
505 
506  // *****************************************************************************
507  // Real(4.) Add custom SUBSIDENCE terms
508  // *****************************************************************************
509  if (solverChoice.custom_w_subsidence && is_slow_step && solverChoice.do_mom_advection) {
510  if (solverChoice.custom_forcing_prim_vars) {
511  const int nr = Rho_comp;
512  ParallelFor(tbx, tby,
513  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
514  {
515  Real dzInv = myhalf*dxInv[2];
516  if (z_nd_arr) {
517  Real z_xf_lo = fourth * ( z_nd_arr(i,j,k ) + z_nd_arr(i,j+1,k )
518  + z_nd_arr(i,j,k-1) + z_nd_arr(i,j+1,k-1) );
519  Real z_xf_hi = fourth * ( z_nd_arr(i,j,k+1) + z_nd_arr(i,j+1,k+1)
520  + z_nd_arr(i,j,k+2) + z_nd_arr(i,j+1,k+2) );
521  dzInv = one / (z_xf_hi - z_xf_lo);
522  }
523  Real rho_on_u_face = myhalf * ( cell_data(i,j,k,nr) + cell_data(i-1,j,k,nr) );
524  Real U_hi = dptr_u_plane(k+1) / dptr_r_plane(k+1);
525  Real U_lo = dptr_u_plane(k-1) / dptr_r_plane(k-1);
526  Real wbar_xf = myhalf * (dptr_wbar_sub[k] + dptr_wbar_sub[k+1]);
527  xmom_src_arr(i, j, k) -= rho_on_u_face * wbar_xf * (U_hi - U_lo) * dzInv;
528  },
529  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
530  {
531  Real dzInv = myhalf*dxInv[2];
532  if (z_nd_arr) {
533  Real z_yf_lo = fourth * ( z_nd_arr(i,j,k ) + z_nd_arr(i+1,j,k )
534  + z_nd_arr(i,j,k-1) + z_nd_arr(i+1,j,k-1) );
535  Real z_yf_hi = fourth * ( z_nd_arr(i,j,k+1) + z_nd_arr(i+1,j,k+1)
536  + z_nd_arr(i,j,k+2) + z_nd_arr(i+1,j,k+2) );
537  dzInv = one / (z_yf_hi - z_yf_lo);
538  }
539  Real rho_on_v_face = myhalf * ( cell_data(i,j,k,nr) + cell_data(i,j-1,k,nr) );
540  Real V_hi = dptr_v_plane(k+1) / dptr_r_plane(k+1);
541  Real V_lo = dptr_v_plane(k-1) / dptr_r_plane(k-1);
542  Real wbar_yf = myhalf * (dptr_wbar_sub[k] + dptr_wbar_sub[k+1]);
543  ymom_src_arr(i, j, k) -= rho_on_v_face * wbar_yf * (V_hi - V_lo) * dzInv;
544  });
545  } else {
546  ParallelFor(tbx, tby,
547  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
548  {
549  Real dzInv = myhalf*dxInv[2];
550  if (z_nd_arr) {
551  Real z_xf_lo = fourth * ( z_nd_arr(i,j,k ) + z_nd_arr(i,j+1,k )
552  + z_nd_arr(i,j,k-1) + z_nd_arr(i,j+1,k-1) );
553  Real z_xf_hi = fourth * ( z_nd_arr(i,j,k+1) + z_nd_arr(i,j+1,k+1)
554  + z_nd_arr(i,j,k+2) + z_nd_arr(i,j+1,k+2) );
555  dzInv = one / (z_xf_hi - z_xf_lo);
556  }
557  Real U_hi = dptr_u_plane(k+1) / dptr_r_plane(k+1);
558  Real U_lo = dptr_u_plane(k-1) / dptr_r_plane(k-1);
559  Real wbar_xf = myhalf * (dptr_wbar_sub[k] + dptr_wbar_sub[k+1]);
560  xmom_src_arr(i, j, k) -= wbar_xf * (U_hi - U_lo) * dzInv;
561  },
562  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
563  {
564  Real dzInv = myhalf*dxInv[2];
565  if (z_nd_arr) {
566  Real z_yf_lo = fourth * ( z_nd_arr(i,j,k ) + z_nd_arr(i+1,j,k )
567  + z_nd_arr(i,j,k-1) + z_nd_arr(i+1,j,k-1) );
568  Real z_yf_hi = fourth * ( z_nd_arr(i,j,k+1) + z_nd_arr(i+1,j,k+1)
569  + z_nd_arr(i,j,k+2) + z_nd_arr(i+1,j,k+2) );
570  dzInv = one / (z_yf_hi - z_yf_lo);
571  }
572  Real V_hi = dptr_v_plane(k+1) / dptr_r_plane(k+1);
573  Real V_lo = dptr_v_plane(k-1) / dptr_r_plane(k-1);
574  Real wbar_yf = myhalf * (dptr_wbar_sub[k] + dptr_wbar_sub[k+1]);
575  ymom_src_arr(i, j, k) -= wbar_yf * (V_hi - V_lo) * dzInv;
576  });
577  }
578  }
579 
580  // *************************************************************************************
581  // Real(5.) Add nudging towards value specified in input sounding
582  // *************************************************************************************
583  if (solverChoice.nudging_from_input_sounding && is_slow_step)
584  {
585  int itime_n = 0;
586  int itime_np1 = 0;
587  Real coeff_n = one;
588  Real coeff_np1 = zero;
589 
590  Real tau_inv = one / input_sounding_data.tau_nudging;
591 
592  int n_sounding_times = input_sounding_data.input_sounding_time.size();
593 
594  for (int nt = 1; nt < n_sounding_times; nt++) {
595  if (time > input_sounding_data.input_sounding_time[nt]) itime_n = nt;
596  }
597  if (itime_n == n_sounding_times-1) {
598  itime_np1 = itime_n;
599  } else {
600  itime_np1 = itime_n+1;
601  coeff_np1 = (time - input_sounding_data.input_sounding_time[itime_n]) /
602  (input_sounding_data.input_sounding_time[itime_np1] - input_sounding_data.input_sounding_time[itime_n]);
603  coeff_n = one - coeff_np1;
604  }
605 
606  int nr = Rho_comp;
607 
608  const Real* u_inp_sound_n = input_sounding_data.U_inp_sound_d[itime_n].dataPtr();
609  const Real* u_inp_sound_np1 = input_sounding_data.U_inp_sound_d[itime_np1].dataPtr();
610  const Real* v_inp_sound_n = input_sounding_data.V_inp_sound_d[itime_n].dataPtr();
611  const Real* v_inp_sound_np1 = input_sounding_data.V_inp_sound_d[itime_np1].dataPtr();
612  ParallelFor(tbx, tby,
613  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
614  {
615  Real nudge_u = (coeff_n*u_inp_sound_n[k] + coeff_np1*u_inp_sound_np1[k]) - (dptr_u_plane(k)/dptr_r_plane(k));
616  nudge_u *= tau_inv;
617  xmom_src_arr(i, j, k) += cell_data(i, j, k, nr) * nudge_u;
618  },
619  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
620  {
621  Real nudge_v = (coeff_n*v_inp_sound_n[k] + coeff_np1*v_inp_sound_np1[k]) - (dptr_v_plane(k)/dptr_r_plane(k));
622  nudge_v *= tau_inv;
623  ymom_src_arr(i, j, k) += cell_data(i, j, k, nr) * nudge_v;
624  });
625  }
626 
627  // *****************************************************************************
628  // Real(6.) Add NUMERICAL DIFFUSION terms
629  // *****************************************************************************
630 #if 0
631  if (l_use_ndiff) {
632  const Array4<const Real>& mf_ux = mapfac[MapFac::ux]->const_array(mfi);
633  const Array4<const Real>& mf_uy = mapfac[MapFac::uy]->const_array(mfi);
634  const Array4<const Real>& mf_vx = mapfac[MapFac::vx]->const_array(mfi);
635  const Array4<const Real>& mf_vy = mapfac[MapFac::vy]->const_array(mfi);
636  NumericalDiffusion_Xmom(tbx, dt, solverChoice.num_diff_coeff,
637  u, cell_data, xmom_src_arr, mf_ux, mf_uy);
638  NumericalDiffusion_Ymom(tby, dt, solverChoice.num_diff_coeff,
639  v, cell_data, ymom_src_arr, mf_vx, mf_vy);
640  }
641 #endif
642 
643  // *****************************************************************************
644  // Real(7.) Add SPONGING
645  // *****************************************************************************
646  if (is_slow_step) {
647  if (solverChoice.spongeChoice.sponge_type == "input_sponge")
648  {
649  ApplySpongeZoneBCsForMom_ReadFromFile(solverChoice.spongeChoice, geom, tbx, tby, cell_data,
650  z_cc_arr, xmom_src_arr, ymom_src_arr,
651  rho_u, rho_v, d_sponge_ptrs_at_lev);
652  }
653  else
654  {
655  ApplySpongeZoneBCsForMom(solverChoice.spongeChoice, geom, tbx, tby, tbz,
656  xmom_src_arr, ymom_src_arr, zmom_src_arr, rho_u, rho_v, rho_w,
657  r0, z_nd_arr, z_cc_arr);
658  }
659 
660  if(solverChoice.init_type == InitType::HindCast and solverChoice.hindcast_lateral_forcing){
661 
662  const Array4<const Real>& rho_u_forecast_state = (*forecast_state_at_lev)[IntVars::xmom].array(mfi);
663  const Array4<const Real>& rho_v_forecast_state = (*forecast_state_at_lev)[IntVars::ymom].array(mfi);
664  const Array4<const Real>& rho_w_forecast_state = (*forecast_state_at_lev)[IntVars::zmom].array(mfi);
665  const Array4<const Real>& cons_forecast_state = (*forecast_state_at_lev)[IntVars::cons].array(mfi);
666  ApplyBndryForcing_Forecast(solverChoice, geom, tbx, tby, tbz, z_nd_arr,
667  xmom_src_arr, ymom_src_arr, zmom_src_arr,
668  rho_u, rho_v, rho_w,
669  rho_u_forecast_state, rho_v_forecast_state, rho_w_forecast_state,
670  cons_forecast_state);
671  }
672  if(solverChoice.init_type == InitType::HindCast and solverChoice.hindcast_surface_bcs) {
673  const Array4<const Real>& surface_state_arr = (*surface_state_at_lev).array(mfi);
675  xmom_src_arr, ymom_src_arr,
676  rho_u, rho_v,
677  cell_data, z_nd_arr,
678  surface_state_arr);
679  }
680  }
681 
682  // *****************************************************************************
683  // Real(8.) Add CANOPY source terms
684  // *****************************************************************************
685  if (solverChoice.do_forest_drag &&
686  ((is_slow_step && !use_canopy_fast) || (!is_slow_step && use_canopy_fast))) {
687  ParallelFor(tbx, tby, tbz,
688  [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
689  {
690  const Real ux = u(i, j, k);
691  const Real uy = fourth * ( v(i, j , k ) + v(i-1, j , k )
692  + v(i, j+1, k ) + v(i-1, j+1, k ) );
693  const Real uz = fourth * ( w(i, j , k ) + w(i-1, j , k )
694  + w(i, j , k+1) + w(i-1, j , k+1) );
695  const Real windspeed = std::sqrt(ux * ux + uy * uy + uz * uz);
696  const Real f_drag = myhalf * (f_drag_arr(i, j, k) + f_drag_arr(i-1, j, k));
697  xmom_src_arr(i, j, k) -= f_drag * ux * windspeed;
698  },
699  [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
700  {
701  const Real ux = fourth * ( u(i , j , k ) + u(i , j-1, k )
702  + u(i+1, j , k ) + u(i+1, j-1, k ) );
703  const Real uy = v(i, j, k);
704  const Real uz = fourth * ( w(i , j , k ) + w(i , j-1, k )
705  + w(i , j , k+1) + w(i , j-1, k+1) );
706  const amrex::Real windspeed = std::sqrt(ux * ux + uy * uy + uz * uz);
707  const Real f_drag = myhalf * (f_drag_arr(i, j, k) + f_drag_arr(i, j-1, k));
708  ymom_src_arr(i, j, k) -= f_drag * uy * windspeed;
709  },
710  [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
711  {
712  const amrex::Real ux = fourth * ( u(i , j , k ) + u(i+1, j , k )
713  + u(i , j , k-1) + u(i+1, j , k-1) );
714  const amrex::Real uy = fourth * ( v(i , j , k ) + v(i , j+1, k )
715  + v(i , j , k-1) + v(i , j+1, k-1) );
716  const amrex::Real uz = w(i, j, k);
717  const amrex::Real windspeed = std::sqrt(ux * ux + uy * uy + uz * uz);
718  const Real f_drag = myhalf * (f_drag_arr(i, j, k) + f_drag_arr(i, j, k-1));
719  zmom_src_arr(i, j, k) -= f_drag * uz * windspeed;
720  });
721  }
722  // *****************************************************************************
723  // 9a. Add immersed source terms for terrain
724  // *****************************************************************************
725  if (solverChoice.terrain_type == TerrainType::ImmersedForcing &&
726  ((is_slow_step && !use_ImmersedForcing_fast) || (!is_slow_step && use_ImmersedForcing_fast))) {
727  // geometric properties
728  const Real* dx_arr = geom.CellSize();
729  const Real dx_x = dx_arr[0];
730  const Real dx_y = dx_arr[1];
731  const Real dx_z = dx_arr[2];
732 
733  const Real alpha_m = solverChoice.if_Cd_momentum;
734  const Real drag_coefficient = alpha_m / std::pow(dx_x*dx_y*dx_z, one/three);
736  const Real U_s = one; // unit velocity scale
737 
738  // MOST parameters
739  similarity_funs sfuns;
740  const Real ggg = CONST_GRAV;
741  const Real kappa = KAPPA;
742  const Real z0 = solverChoice.if_z0;
743  const Real tflux_in = solverChoice.if_surf_temp_flux;
744  const Real Olen_in = solverChoice.if_Olen_in;
745  const bool l_use_most = solverChoice.if_use_most;
746 
747  ParallelFor(tbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
748  {
749  const Real ux = u(i, j, k);
750  const Real uy = fourth * ( v(i, j , k ) + v(i-1, j , k )
751  + v(i, j+1, k ) + v(i-1, j+1, k ) );
752  const Real uz = fourth * ( w(i, j , k ) + w(i-1, j , k )
753  + w(i, j , k+1) + w(i-1, j , k+1) );
754  const Real windspeed = std::sqrt(ux * ux + uy * uy + uz * uz);
755  const Real t_blank = myhalf * (t_blank_arr(i, j, k) + t_blank_arr(i-1, j, k));
756  const Real t_blank_above = myhalf * (t_blank_arr(i, j, k+1) + t_blank_arr(i-1, j, k+1));
757  const Real CdM = std::min(drag_coefficient / (windspeed + tiny), Real(1000.0));
758  const Real rho_xface = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i-1,j,k,Rho_comp) );
759 
760  if ((t_blank > 0 && (t_blank_above == zero)) && l_use_most) { // force to MOST value
761  // calculate tangential velocity one cell above
762  const Real ux2r = u(i, j, k+1) ;
763  const Real uy2r = fourth * ( v(i, j , k+1) + v(i-1, j , k+1)
764  + v(i, j+1, k+1) + v(i-1, j+1, k+1) ) ;
765  const Real h_windspeed2r = std::sqrt(ux2r * ux2r + uy2r * uy2r);
766 
767  // MOST
768  const Real theta_xface = (myhalf * (cell_data(i,j,k ,RhoTheta_comp) + cell_data(i-1,j,k, RhoTheta_comp))) / rho_xface;
769  const Real rho_xface_below = myhalf * ( cell_data(i,j,k-1,Rho_comp) + cell_data(i-1,j,k-1,Rho_comp) );
770  const Real theta_xface_below = (myhalf * (cell_data(i,j,k-1,RhoTheta_comp) + cell_data(i-1,j,k-1, RhoTheta_comp))) / rho_xface_below;
771  const Real theta_surf = theta_xface_below;
772 
773  Real psi_m = zero;
774  Real psi_h = zero;
775  Real ustar = h_windspeed2r * kappa / (std::log(Real(1.5) * dx_z / z0) - psi_m); // calculated from bottom of cell. Maintains flexibility for different Vf values
776  Real tflux = (tflux_in != 1e-8) ? tflux_in : -(theta_xface - theta_surf) * ustar * kappa / (std::log(Real(1.5) * dx_z / z0) - psi_h);
777  Real Olen = (Olen_in != 1e-8) ? Olen_in : -ustar * ustar * ustar * theta_xface / (kappa * ggg * tflux + tiny);
778  Real zeta = Real(1.5) * dx_z / Olen;
779 
780  // similarity functions
781  psi_m = sfuns.calc_psi_m(zeta);
782  psi_h = sfuns.calc_psi_h(zeta);
783  ustar = h_windspeed2r * kappa / (std::log(Real(1.5) * dx_z / z0) - psi_m);
784 
785  // prevent some unphysical math
786  if (!(ustar > zero && !std::isnan(ustar))) { ustar = zero; }
787  if (!(ustar < two && !std::isnan(ustar))) { ustar = two; }
788  if (psi_m > std::log(myhalf * dx_z / z0)) { psi_m = std::log(myhalf * dx_z / z0); }
789 
790  // determine target velocity
791  const Real uTarget = ustar / kappa * (std::log(myhalf * dx_z / z0) - psi_m);
792  Real uxTarget = uTarget * ux2r / (tiny + h_windspeed2r);
793  const Real bc_forcing_x = -(uxTarget - ux); // BC forcing pushes nonrelative velocity toward target velocity
794  xmom_src_arr(i, j, k) -= (1-t_blank) * rho_xface * CdM * U_s * bc_forcing_x; // if Vf low, force more strongly to MOST. If high, less forcing.
795  } else {
796  xmom_src_arr(i, j, k) -= t_blank * rho_xface * CdM * ux * windspeed;
797  }
798  });
799  ParallelFor(tby, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
800  {
801  const Real ux = fourth * ( u(i , j , k ) + u(i , j-1, k )
802  + u(i+1, j , k ) + u(i+1, j-1, k ) );
803  const Real uy = v(i, j, k);
804  const Real uz = fourth * ( w(i , j , k ) + w(i , j-1, k )
805  + w(i , j , k+1) + w(i , j-1, k+1) );
806  const Real windspeed = std::sqrt(ux * ux + uy * uy + uz * uz);
807  const Real t_blank = myhalf * (t_blank_arr(i, j, k) + t_blank_arr(i, j-1, k));
808  const Real t_blank_above = myhalf * (t_blank_arr(i, j, k+1) + t_blank_arr(i, j-1, k+1));
809  const Real CdM = std::min(drag_coefficient / (windspeed + tiny), Real(1000.0));
810  const Real rho_yface = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i,j-1,k,Rho_comp) );
811 
812  if ((t_blank > 0 && (t_blank_above == zero)) && l_use_most) { // force to MOST value
813  // calculate tangential velocity one cell above
814  const Real ux2r = fourth * ( u(i , j , k+1) + u(i , j-1, k+1)
815  + u(i+1, j , k+1) + u(i+1, j-1, k+1) );
816  const Real uy2r = v(i, j, k+1) ;
817  const Real h_windspeed2r = std::sqrt(ux2r * ux2r + uy2r * uy2r);
818 
819  // MOST
820  const Real theta_yface = (myhalf * (cell_data(i,j,k ,RhoTheta_comp) + cell_data(i,j-1,k, RhoTheta_comp))) / rho_yface;
821  const Real rho_yface_below = myhalf * ( cell_data(i,j,k-1,Rho_comp) + cell_data(i,j-1,k-1,Rho_comp) );
822  const Real theta_yface_below = (myhalf * (cell_data(i,j,k-1,RhoTheta_comp) + cell_data(i,j-1,k-1, RhoTheta_comp))) / rho_yface_below;
823  const Real theta_surf = theta_yface_below;
824 
825  Real psi_m = zero;
826  Real psi_h = zero;
827  Real ustar = h_windspeed2r * kappa / (std::log(Real(1.5) * dx_z / z0) - psi_m); // calculated from bottom of cell. Maintains flexibility for different Vf values
828  Real tflux = (tflux_in != 1e-8) ? tflux_in : -(theta_yface - theta_surf) * ustar * kappa / (std::log(Real(1.5) * dx_z / z0) - psi_h);
829  Real Olen = (Olen_in != 1e-8) ? Olen_in : -ustar * ustar * ustar * theta_yface / (kappa * ggg * tflux + tiny);
830  Real zeta = Real(1.5) * dx_z / Olen;
831 
832  // similarity functions
833  psi_m = sfuns.calc_psi_m(zeta);
834  psi_h = sfuns.calc_psi_h(zeta);
835  ustar = h_windspeed2r * kappa / (std::log(Real(1.5) * dx_z / z0) - psi_m);
836 
837  // prevent some unphysical math
838  if (!(ustar > zero && !std::isnan(ustar))) { ustar = zero; }
839  if (!(ustar < two && !std::isnan(ustar))) { ustar = two; }
840  if (psi_m > std::log(myhalf * dx_z / z0)) { psi_m = std::log(myhalf * dx_z / z0); }
841 
842  // determine target velocity
843  const Real uTarget = ustar / kappa * (std::log(myhalf * dx_z / z0) - psi_m);
844  Real uyTarget = uTarget * uy2r / (tiny + h_windspeed2r);
845  const Real bc_forcing_y = -(uyTarget - uy); // BC forcing pushes nonrelative velocity toward target velocity
846  ymom_src_arr(i, j, k) -= (1 - t_blank) * rho_yface * CdM * U_s * bc_forcing_y; // if Vf low, force more strongly to MOST. If high, less forcing.
847  } else {
848  ymom_src_arr(i, j, k) -= t_blank * rho_yface * CdM * uy * windspeed;
849  }
850  });
851  ParallelFor(tbz, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
852  {
853  const Real ux = fourth * ( u(i , j , k ) + u(i+1, j , k )
854  + u(i , j , k-1) + u(i+1, j , k-1) );
855  const Real uy = fourth * ( v(i , j , k ) + v(i , j+1, k )
856  + v(i , j , k-1) + v(i , j+1, k-1) );
857  const Real uz = w(i, j, k);
858  const Real windspeed = std::sqrt(ux * ux + uy * uy + uz * uz);
859  const Real t_blank = myhalf * (t_blank_arr(i, j, k) + t_blank_arr(i, j, k-1));
860  const Real CdM = std::min(drag_coefficient / (windspeed + tiny), Real(1000.0));
861  const Real rho_zface = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i,j,k-1,Rho_comp) );
862  zmom_src_arr(i, j, k) -= t_blank * rho_zface * CdM * uz * windspeed;
863  });
864  }
865 
866  // *****************************************************************************
867  // 9b. Add immersed source terms for buildings
868  // *****************************************************************************
869  if ((solverChoice.buildings_type == BuildingsType::ImmersedForcing ) &&
870  ((is_slow_step && !use_ImmersedForcing_fast) || (!is_slow_step && use_ImmersedForcing_fast)))
871  {
872  // geometric properties
873  const Real* dx_arr = geom.CellSize();
874  const Real dx_x = dx_arr[0];
875  const Real dx_y = dx_arr[1];
876 
877  const Real alpha_m = solverChoice.if_Cd_momentum;
879  const Real min_t_blank = Real(0.005); // threshold for where immersed forcing acts
880 
881  ParallelFor(tbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
882  {
883  const Real ux = u(i, j, k);
884  const Real uy = fourth * ( v(i, j , k ) + v(i-1, j , k )
885  + v(i, j+1, k ) + v(i-1, j+1, k ) );
886  const Real uz = fourth * ( w(i, j , k ) + w(i-1, j , k )
887  + w(i, j , k+1) + w(i-1, j , k+1) );
888  const Real windspeed = std::sqrt(ux * ux + uy * uy + uz * uz);
889 
890  Real t_blank = myhalf * (t_blank_arr(i, j, k) + t_blank_arr(i-1, j, k));
891  if (t_blank < min_t_blank) { t_blank = zero; }
892  const Real dx_z = (z_cc_arr) ? (z_cc_arr(i,j,k) - z_cc_arr(i,j,k-1)) : dx_arr[2];
893  const Real drag_coefficient = alpha_m / std::pow(dx_x*dx_y*dx_z, one/three);
894  const Real CdM = std::min(drag_coefficient / (windspeed + tiny), Real(1000.0));
895  const Real rho_xface = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i-1,j,k,Rho_comp) );
896  xmom_src_arr(i, j, k) -= t_blank * rho_xface * CdM * ux * windspeed;
897  });
898  ParallelFor(tby, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
899  {
900  const Real ux = fourth * ( u(i , j , k ) + u(i , j-1, k )
901  + u(i+1, j , k ) + u(i+1, j-1, k ) );
902  const Real uy = v(i, j, k);
903  const Real uz = fourth * ( w(i , j , k ) + w(i , j-1, k )
904  + w(i , j , k+1) + w(i , j-1, k+1) );
905  const Real windspeed = std::sqrt(ux * ux + uy * uy + uz * uz);
906 
907  Real t_blank = myhalf * (t_blank_arr(i, j, k) + t_blank_arr(i, j-1, k));
908  if (t_blank < min_t_blank) { t_blank = zero; }
909  const Real dx_z = (z_cc_arr) ? (z_cc_arr(i,j,k) - z_cc_arr(i,j,k-1)) : dx_arr[2];
910  const Real drag_coefficient = alpha_m / std::pow(dx_x*dx_y*dx_z, one/three);
911  const Real CdM = std::min(drag_coefficient / (windspeed + tiny), Real(1000.0));
912  const Real rho_yface = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i,j-1,k,Rho_comp) );
913  ymom_src_arr(i, j, k) -= t_blank * rho_yface * CdM * uy * windspeed;
914  });
915  ParallelFor(tbz, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
916  {
917  const Real ux = fourth * ( u(i , j , k ) + u(i+1, j , k )
918  + u(i , j , k-1) + u(i+1, j , k-1) );
919  const Real uy = fourth * ( v(i , j , k ) + v(i , j+1, k )
920  + v(i , j , k-1) + v(i , j+1, k-1) );
921  const Real uz = w(i, j, k);
922  const Real windspeed = std::sqrt(ux * ux + uy * uy + uz * uz);
923 
924  Real t_blank = myhalf * (t_blank_arr(i, j, k) + t_blank_arr(i, j, k-1));
925  if (t_blank < min_t_blank) { t_blank = zero; }
926  const Real dx_z = (z_nd_arr) ? (z_nd_arr(i,j,k) - z_nd_arr(i,j,k-1)) : dx_arr[2]; // ASW double check
927  const Real drag_coefficient = alpha_m / std::pow(dx_x*dx_y*dx_z, one/three);
928  const Real CdM = std::min(drag_coefficient / (windspeed + tiny), Real(1000.0));
929  const Real rho_zface = myhalf * ( cell_data(i,j,k,Rho_comp) + cell_data(i,j,k-1,Rho_comp) );
930  zmom_src_arr(i, j, k) -= t_blank * rho_zface * CdM * uz * windspeed;
931  });
932  }
933 
934  // *****************************************************************************
935  // Real(10.) Enforce constant mass flux
936  // *****************************************************************************
937  if (is_slow_step && (enforce_massflux_x || enforce_massflux_y)) {
938  Real tau_inv = one / solverChoice.const_massflux_tau;
939 
940  ParallelFor(tbx, tby,
941  [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept {
942  xmom_src_arr(i, j, k) += tau_inv * (rhoUA_target - rhoUA);
943  },
944  [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept {
945  ymom_src_arr(i, j, k) += tau_inv * (rhoVA_target - rhoVA);
946  });
947  }
948 
949  } // mfi
950 }
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)
Definition: ERF_ApplySpongeZoneBCs.cpp:169
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:8
void ApplySurfaceTreatment_BulkCoeff_Mom(const Box &tbx, const Box &tby, 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 Array4< const Real > &cons_state, const Array4< const Real > &z_phys_nd, const Array4< const Real > &surface_state_arr)
Definition: ERF_ApplySurfaceTreatment_BulkCoeff.cpp:8
constexpr amrex::Real three
Definition: ERF_Constants.H:9
constexpr amrex::Real KAPPA
Definition: ERF_Constants.H:30
constexpr amrex::Real two
Definition: ERF_Constants.H:8
constexpr amrex::Real one
Definition: ERF_Constants.H:7
constexpr amrex::Real fourth
Definition: ERF_Constants.H:12
constexpr amrex::Real zero
Definition: ERF_Constants.H:6
constexpr amrex::Real myhalf
Definition: ERF_Constants.H:11
constexpr amrex::Real PI
Definition: ERF_Constants.H:16
constexpr amrex::Real CONST_GRAV
Definition: ERF_Constants.H:31
@ ubar
Definition: ERF_DataStruct.H:98
@ wbar
Definition: ERF_DataStruct.H:98
@ vbar
Definition: ERF_DataStruct.H:98
DirectionSelector< 2 > ZDir
Definition: ERF_DirectionSelector.H:38
#define Rho_comp
Definition: ERF_IndexDefines.H:36
#define RhoTheta_comp
Definition: ERF_IndexDefines.H:37
amrex::GpuArray< Real, AMREX_SPACEDIM > dxInv
Definition: ERF_InitCustomPertVels_ParticleTests.H:17
Real z0
Definition: ERF_InitCustomPertVels_ScalarAdvDiff.H:8
ParallelFor(bx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept { const auto prob_lo=geomdata.ProbLo();const auto dx=geomdata.CellSize();const Real x=(prob_lo[0]+(i+myhalf) *dx[0])/mf_m(i, j, 0);const Real z=z_cc(i, j, k);Real L=std::sqrt(std::pow((x - x_c)/x_r, 2)+std::pow((z - z_c)/z_r, 2));if(L<=one) { Real dT=T_pert *(std::cos(PI *L)+one)/two;Real Tbar_hse=p_hse(i, j, k)/(R_d *r_hse(i, j, k));Real theta_perturbed=(Tbar_hse+dT) *std::pow(p_0/p_hse(i, j, k), rdOcp);Real theta_0=(Tbar_hse) *std::pow(p_0/p_hse(i, j, k), rdOcp);if(const_rho) { state_pert(i, j, k, RhoTheta_comp)=r_hse(i, j, k) *(theta_perturbed - theta_0);} else { state_pert(i, j, k, Rho_comp)=getRhoThetagivenP(p_hse(i, j, k))/theta_perturbed - r_hse(i, j, k);} } })
void NumericalDiffusion_Ymom(const Box &bx, const Real 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 Real 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:28
amrex::Real Real
Definition: ERF_ShocInterface.H:19
Definition: ERF_PlaneAverage.H:14
eb_aux_ const * get_w_const_factory() const noexcept
Definition: ERF_EB.H:52
eb_aux_ const * get_v_const_factory() const noexcept
Definition: ERF_EB.H:51
eb_aux_ const * get_u_const_factory() const noexcept
Definition: ERF_EB.H:50
@ r0_comp
Definition: ERF_IndexDefines.H:63
@ ymom
Definition: ERF_IndexDefines.H:178
@ cons
Definition: ERF_IndexDefines.H:176
@ zmom
Definition: ERF_IndexDefines.H:179
@ xmom
Definition: ERF_IndexDefines.H:177
@ nr
Definition: ERF_Morrison.H:45
@ xvel
Definition: ERF_IndexDefines.H:159
@ cons
Definition: ERF_IndexDefines.H:158
@ yvel
Definition: ERF_IndexDefines.H:160
real(c_double), parameter epsilon
Definition: ERF_module_model_constants.F90:12
real(kind=kind_phys), parameter, private r0
Definition: ERF_module_mp_wsm6.F90:21
bool rayleigh_damp_V
Definition: ERF_DampingStruct.H:85
amrex::Real rayleigh_dampcoef
Definition: ERF_DampingStruct.H:88
bool rayleigh_damp_W
Definition: ERF_DampingStruct.H:86
RayleighDampingType rayleigh_damping_type
Definition: ERF_DampingStruct.H:101
bool rayleigh_damp_U
Definition: ERF_DampingStruct.H:84
amrex::Vector< amrex::Gpu::DeviceVector< amrex::Real > > V_inp_sound_d
Definition: ERF_InputSoundingData.H:407
amrex::Vector< amrex::Real > input_sounding_time
Definition: ERF_InputSoundingData.H:394
amrex::Real tau_nudging
Definition: ERF_InputSoundingData.H:391
amrex::Vector< amrex::Gpu::DeviceVector< amrex::Real > > U_inp_sound_d
Definition: ERF_InputSoundingData.H:407
bool do_mom_advection
Definition: ERF_DataStruct.H:1173
amrex::Real coriolis_factor
Definition: ERF_DataStruct.H:1164
static MeshType mesh_type
Definition: ERF_DataStruct.H:1079
amrex::Real if_surf_temp_flux
Definition: ERF_DataStruct.H:1144
bool if_use_most
Definition: ERF_DataStruct.H:1148
DampingChoice dampingChoice
Definition: ERF_DataStruct.H:1089
amrex::Real const_massflux_v
Definition: ERF_DataStruct.H:1242
amrex::Real if_z0
Definition: ERF_DataStruct.H:1143
amrex::Real cosphi
Definition: ERF_DataStruct.H:1165
amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > abl_geo_forcing
Definition: ERF_DataStruct.H:1214
bool hindcast_lateral_forcing
Definition: ERF_DataStruct.H:1251
int massflux_klo
Definition: ERF_DataStruct.H:1246
bool custom_w_subsidence
Definition: ERF_DataStruct.H:1171
bool nudging_from_input_sounding
Definition: ERF_DataStruct.H:1181
bool immersed_forcing_substep
Definition: ERF_DataStruct.H:1136
amrex::Real sinphi
Definition: ERF_DataStruct.H:1166
bool have_geo_wind_profile
Definition: ERF_DataStruct.H:1216
amrex::Real const_massflux_u
Definition: ERF_DataStruct.H:1241
amrex::Real if_Olen_in
Definition: ERF_DataStruct.H:1147
bool use_coriolis
Definition: ERF_DataStruct.H:1130
amrex::Real num_diff_coeff
Definition: ERF_DataStruct.H:1200
bool variable_coriolis
Definition: ERF_DataStruct.H:1218
amrex::Real if_Cd_momentum
Definition: ERF_DataStruct.H:1141
bool custom_forcing_prim_vars
Definition: ERF_DataStruct.H:1175
static BuildingsType buildings_type
Definition: ERF_DataStruct.H:1070
static TerrainType terrain_type
Definition: ERF_DataStruct.H:1067
SpongeChoice spongeChoice
Definition: ERF_DataStruct.H:1090
static InitType init_type
Definition: ERF_DataStruct.H:1061
bool hindcast_surface_bcs
Definition: ERF_DataStruct.H:1252
bool do_forest_drag
Definition: ERF_DataStruct.H:1238
amrex::Real const_massflux_tau
Definition: ERF_DataStruct.H:1243
int massflux_khi
Definition: ERF_DataStruct.H:1247
bool forest_substep
Definition: ERF_DataStruct.H:1137
int ave_plane
Definition: ERF_DataStruct.H:1220
std::string sponge_type
Definition: ERF_SpongeStruct.H:60
Definition: ERF_MOSTStress.H:40
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real calc_psi_m(amrex::Real zeta) const
Definition: ERF_MOSTStress.H:90
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real calc_psi_h(amrex::Real zeta) const
Definition: ERF_MOSTStress.H:104
Here is the call graph for this function: