ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_Substep_NS.cpp File Reference
#include <ERF_TI_fast_headers.H>
#include "ERF_Constants.H"
Include dependency graph for ERF_Substep_NS.cpp:

Functions

void erf_substep_NS (int step, int nrk, int level, int finest_level, Vector< MultiFab > &S_slow_rhs, const Vector< MultiFab > &S_prev, Vector< MultiFab > &S_stage_data, const MultiFab &S_stage_prim, const MultiFab &qt, const MultiFab &pi_stage, const MultiFab &fast_coeffs, Vector< MultiFab > &S_data, MultiFab &lagged_delta_rt, MultiFab &avg_xmom, MultiFab &avg_ymom, MultiFab &avg_zmom, const MultiFab &cc_src, const MultiFab &xmom_src, const MultiFab &ymom_src, const MultiFab &zmom_src, const Geometry geom, const Real gravity, amrex::Gpu::DeviceVector< amrex::Real > &stretched_dz_d, const double dtau_d, const Real beta_s, const Real facinv, Vector< std::unique_ptr< MultiFab >> &mapfac, YAFluxRegister *fr_as_crse, YAFluxRegister *fr_as_fine, bool l_use_moisture, bool l_reflux, bool l_real_bc, const amrex::Real *sinesq_stag_d, const Real l_damp_coef)
 

Function Documentation

◆ erf_substep_NS()

void erf_substep_NS ( int  step,
int  nrk,
int  level,
int  finest_level,
Vector< MultiFab > &  S_slow_rhs,
const Vector< MultiFab > &  S_prev,
Vector< MultiFab > &  S_stage_data,
const MultiFab &  S_stage_prim,
const MultiFab &  qt,
const MultiFab &  pi_stage,
const MultiFab &  fast_coeffs,
Vector< MultiFab > &  S_data,
MultiFab &  lagged_delta_rt,
MultiFab &  avg_xmom,
MultiFab &  avg_ymom,
MultiFab &  avg_zmom,
const MultiFab &  cc_src,
const MultiFab &  xmom_src,
const MultiFab &  ymom_src,
const MultiFab &  zmom_src,
const Geometry  geom,
const Real  gravity,
amrex::Gpu::DeviceVector< amrex::Real > &  stretched_dz_d,
const double  dtau_d,
const Real  beta_s,
const Real  facinv,
Vector< std::unique_ptr< MultiFab >> &  mapfac,
YAFluxRegister *  fr_as_crse,
YAFluxRegister *  fr_as_fine,
bool  l_use_moisture,
bool  l_reflux,
bool  l_real_bc,
const amrex::Real sinesq_stag_d,
const Real  l_damp_coef 
)

Function for computing the fast RHS with no terrain and variable vertical spacing

Parameters
[in]stepwhich fast time step within each Runge-Kutta step
[in]nrkwhich Runge-Kutta step
[in]levellevel of resolution
[in]finest_levelfinest level of resolution
[in]S_slow_rhsslow RHS computed in erf_slow_rhs_pre
[in]S_previf step == 0, this is S_old, else the previous fast solution
[in]S_stage_datasolution at previous RK stage
[in]S_stage_primprimitive variables at previous RK stage
[in]pi_stageExner function at previous RK stage
[in]fast_coeffscoefficients for the tridiagonal solve used in the fast integrator
[out]S_datacurrent solution
[in,out]lagged_delta_rt
[in,out]avg_xmomtime-averaged x-momentum to be used for updating slow variables
[in,out]avg_ymomtime-averaged y-momentum to be used for updating slow variables
[in,out]avg_zmomtime-averaged z-momentum to be used for updating slow variables
[in]cc_srcsource terms for conserved variables
[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]gravitymagnitude of gravity
[in]stretched_dz_d
[in]dtaufast time step
[in]beta_sCoefficient which determines how implicit vs explicit the solve is
[in]facinvinverse factor for time-averaging the momenta
[in]mapfacvector of map factors
[in,out]fr_as_crseYAFluxRegister at level l at level l / l+1 interface
[in,out]fr_as_fineYAFluxRegister at level l at level l-1 / l interface
[in]l_use_moisture
[in]l_refluxshould we add fluxes to the FluxRegisters?
74 {
75  //
76  // NOTE: for step > 0, S_data and S_prev point to the same MultiFab data!!
77  //
78 
79  BL_PROFILE_REGION("erf_substep_NS()");
80 
81  Real dtau = static_cast<Real>(dtau_d);
82 
83  const Box& domain = geom.Domain();
84  auto const domlo = lbound(domain);
85  auto const domhi = ubound(domain);
86 
87  int ilo = domlo.x;
88  int ihi = domhi.x + 1;
89  int jlo = domlo.y;
90  int jhi = domhi.y + 1;
91 
92  Real beta_1 = myhalf * (one - beta_s); // multiplies explicit terms
93  Real beta_2 = myhalf * (one + beta_s); // multiplies implicit terms
94 
95  // How much do we project forward the (rho theta) that is used in the horizontal momentum equations
96  Real beta_d = Real(0.1);
97 
98  bool l_rayleigh_impl_for_w = (sinesq_stag_d != nullptr);
99 
100  const Real* dx = geom.CellSize();
101  const GpuArray<Real, AMREX_SPACEDIM> dxInv = geom.InvCellSizeArray();
102 
103  Real dxi = dxInv[0];
104  Real dyi = dxInv[1];
105  Real dzi = dxInv[2];
106 
107  auto dz_ptr = stretched_dz_d.data();
108 
109  const auto& ba = S_stage_data[IntVars::cons].boxArray();
110  const auto& dm = S_stage_data[IntVars::cons].DistributionMap();
111 
112  MultiFab Delta_rho_theta( ba , dm, 1, 1);
113  MultiFab Delta_rho_w (convert(ba,IntVect(0,0,1)), dm, 1, IntVect(1,1,0));
114 
115  MultiFab coeff_A_mf(fast_coeffs, make_alias, 0, 1);
116  MultiFab inv_coeff_B_mf(fast_coeffs, make_alias, 1, 1);
117  MultiFab coeff_C_mf(fast_coeffs, make_alias, 2, 1);
118  MultiFab coeff_P_mf(fast_coeffs, make_alias, 3, 1);
119  MultiFab coeff_Q_mf(fast_coeffs, make_alias, 4, 1);
120 
121  // *************************************************************************
122  // Set gravity as a vector
123  const Array<Real,AMREX_SPACEDIM> grav{zero, zero, -gravity};
124  const GpuArray<Real,AMREX_SPACEDIM> grav_gpu{grav[0], grav[1], grav[2]};
125 
126  // This will hold theta extrapolated forward in time
127  MultiFab extrap(S_data[IntVars::cons].boxArray(),S_data[IntVars::cons].DistributionMap(),1,1);
128 
129  // This will hold the update for (rho) and (rho theta)
130  MultiFab temp_rhs(S_stage_data[IntVars::zmom].boxArray(),S_stage_data[IntVars::zmom].DistributionMap(),2,0);
131 
132  // This will hold the new x- and y-momenta temporarily (so that we don't overwrite values we need when tiling)
133  MultiFab temp_cur_xmom(S_stage_data[IntVars::xmom].boxArray(),S_stage_data[IntVars::xmom].DistributionMap(),1,0);
134  MultiFab temp_cur_ymom(S_stage_data[IntVars::ymom].boxArray(),S_stage_data[IntVars::ymom].DistributionMap(),1,0);
135 
136  // We assume that in the first step (nrk == 0) we are only doing one substep.
137  AMREX_ALWAYS_ASSERT(nrk > 0 || step == 0);
138 
139  // *************************************************************************
140  // First set up some arrays we'll need
141  // *************************************************************************
142 
143 #ifdef _OPENMP
144 #pragma omp parallel if (Gpu::notInLaunchRegion())
145 #endif
146  for ( MFIter mfi(S_stage_data[IntVars::cons],TilingIfNotGPU()); mfi.isValid(); ++mfi)
147  {
148  const Array4<const Real>& prev_cons = S_prev[IntVars::cons].const_array(mfi);
149  const Array4<const Real>& prev_zmom = S_prev[IntVars::zmom].const_array(mfi);
150 
151  const Array4<const Real>& stage_cons = S_stage_data[IntVars::cons].const_array(mfi);
152  const Array4<const Real>& stage_zmom = S_stage_data[IntVars::zmom].const_array(mfi);
153 
154  const Array4<Real>& prev_drho_w = Delta_rho_w.array(mfi);
155  const Array4<Real>& prev_drho_theta = Delta_rho_theta.array(mfi);
156  const Array4<Real>& lagged_arr = lagged_delta_rt.array(mfi);
157  const Array4<Real>& theta_extrap = extrap.array(mfi);
158  const Array4<const Real>& prim = S_stage_prim.const_array(mfi);
159 
160  Box gbx = mfi.growntilebox(1);
161  ParallelFor(gbx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
162  {
163  prev_drho_theta(i,j,k) = prev_cons(i,j,k,RhoTheta_comp) - stage_cons(i,j,k,RhoTheta_comp);
164 
165  if (step == 0) {
166  theta_extrap(i,j,k) = prev_drho_theta(i,j,k);
167  } else {
168  theta_extrap(i,j,k) = prev_drho_theta(i,j,k) + beta_d *
169  ( prev_drho_theta(i,j,k) - lagged_arr(i,j,k) );
170  }
171 
172  // NOTE: qv is not changing over the fast steps so we use the stage data
173  Real qv = (l_use_moisture) ? prim(i,j,k,PrimQ1_comp) : zero;
174  theta_extrap(i,j,k) *= (one + RvoRd*qv);
175 
176  // We define lagged_delta_rt for our next step as the current delta_rt
177  // (after using it above to extrapolate theta for this step)
178  lagged_arr(i,j,k) = prev_drho_theta(i,j,k);
179  });
180 
181  // NOTE: We must do this here because for step > 0, prev_zmom and cur_zmom both point to the same data,
182  // so by the time we would use prev_zmom to define zflux, it would have already been over-written.
183  //
184  // NOTE: grownnodaltilebox, not nodaltilebox(2) grown by hand. Growing by hand pushes
185  // every tile one cell past its own share of the grid in x and y, so once the grid
186  // is tiled two tiles write the same Delta_rho_w cells -- a data race under OpenMP,
187  // even though both store the same value. This is identical to the old expression
188  // when there is one tile per grid.
189  //
190  Box gtbz = mfi.grownnodaltilebox(2,IntVect(1,1,0));
191  ParallelFor(gtbz, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
192  prev_drho_w(i,j,k) = prev_zmom(i,j,k) - stage_zmom(i,j,k);
193  });
194  } // mfi
195 
196  // *************************************************************************
197  // Define updates in the current RK stage
198  // *************************************************************************
199 
200 #ifdef _OPENMP
201 #pragma omp parallel if (Gpu::notInLaunchRegion())
202 #endif
203  for ( MFIter mfi(S_stage_data[IntVars::cons],TilingIfNotGPU()); mfi.isValid(); ++mfi)
204  {
205  Box tbx = mfi.nodaltilebox(0);
206  Box tby = mfi.nodaltilebox(1);
207 
208  const Array4<Real const>& xmom_src_arr = xmom_src.const_array(mfi);
209  const Array4<Real const>& ymom_src_arr = ymom_src.const_array(mfi);
210 
211  const Array4<const Real> & stage_xmom = S_stage_data[IntVars::xmom].const_array(mfi);
212  const Array4<const Real> & stage_ymom = S_stage_data[IntVars::ymom].const_array(mfi);
213  const Array4<const Real> & qt_arr = qt.const_array(mfi);
214 
215  const Array4<const Real>& slow_rhs_rho_u = S_slow_rhs[IntVars::xmom].const_array(mfi);
216  const Array4<const Real>& slow_rhs_rho_v = S_slow_rhs[IntVars::ymom].const_array(mfi);
217 
218  const Array4<Real>& temp_cur_xmom_arr = temp_cur_xmom.array(mfi);
219  const Array4<Real>& temp_cur_ymom_arr = temp_cur_ymom.array(mfi);
220 
221  const Array4<const Real>& prev_xmom = S_prev[IntVars::xmom].const_array(mfi);
222  const Array4<const Real>& prev_ymom = S_prev[IntVars::ymom].const_array(mfi);
223 
224  // These store the advection momenta which we will use to update the slow variables
225  const Array4< Real>& avg_xmom_arr = avg_xmom.array(mfi);
226  const Array4< Real>& avg_ymom_arr = avg_ymom.array(mfi);
227 
228  const Array4<const Real>& pi_stage_ca = pi_stage.const_array(mfi);
229 
230  const Array4<Real>& theta_extrap = extrap.array(mfi);
231 
232  // Map factors
233  const Array4<const Real>& mf_ux = mapfac[MapFacType::u_x]->const_array(mfi);
234  const Array4<const Real>& mf_uy = mapfac[MapFacType::u_y]->const_array(mfi);
235  const Array4<const Real>& mf_vx = mapfac[MapFacType::v_x]->const_array(mfi);
236  const Array4<const Real>& mf_vy = mapfac[MapFacType::v_y]->const_array(mfi);
237 
238  // *********************************************************************
239  // Define updates in the RHS of {x, y, z}-momentum equations
240  //
241  // NOTE: avg_{x,y}mom is a *flux* that is later handed to the scalar
242  // advection in erf_slow_rhs_post, which applies (mfsq/detJ) to its
243  // divergence. So what we accumulate here must be in the same
244  // convention as the base value set in AdvectionSrcForRho, namely
245  // ax*rho_u/mf_uy (and ay*rho_v/mf_vx). Two weightings are needed:
246  //
247  // (1) the map factor, matching the density fluxes formed below;
248  //
249  // (2) h_zeta = ax = detJ, which for this (laterally homogeneous)
250  // mesh is just dz_ptr[k]/dz. Note this factor cancels out of
251  // the rho update we do here -- temp_rhs uses a bare dxi and no
252  // 1/detJ -- but it does NOT cancel for avg_{x,y}mom, because
253  // the consumer downstream re-applies the 1/detJ that we never
254  // applied. It is identically 1 for MeshType::ConstantDz.
255  // *********************************************************************
256  if (nrk == 0 and step == 0) { // prev == stage
257  ParallelFor(tbx, tby,
258  [=] AMREX_GPU_DEVICE (int i, int j, int k)
259  {
260  Real h_zeta = dz_ptr[k] * dzi;
261  Real new_drho_u = dtau * slow_rhs_rho_u(i,j,k) + dtau * xmom_src_arr(i,j,k);;
262  avg_xmom_arr(i,j,k) += facinv * new_drho_u * h_zeta / mf_uy(i,j,0);
263  temp_cur_xmom_arr(i,j,k) = stage_xmom(i,j,k) + new_drho_u;
264  },
265  [=] AMREX_GPU_DEVICE (int i, int j, int k)
266  {
267  Real h_zeta = dz_ptr[k] * dzi;
268  Real new_drho_v = dtau * slow_rhs_rho_v(i,j,k) + dtau * ymom_src_arr(i,j,k);
269  avg_ymom_arr(i,j,k) += facinv * new_drho_v * h_zeta / mf_vx(i,j,0);
270  temp_cur_ymom_arr(i,j,k) = stage_ymom(i,j,k) + new_drho_v;
271  });
272  } else {
273  ParallelFor(tbx, tby,
274  [=] AMREX_GPU_DEVICE (int i, int j, int k)
275  {
276  // Add (negative) gradient of (rho theta) multiplied by lagged "pi"
277  Real gpx = (l_real_bc && (level==0) && (i==ilo || i==ihi)) ? Real(0.) :
278  (theta_extrap(i,j,k) - theta_extrap(i-1,j,k))*dxi;
279  gpx *= mf_ux(i,j,0);
280 
281  Real q = (l_use_moisture) ? myhalf * (qt_arr(i,j,k) + qt_arr(i-1,j,k)) : zero;
282 
283  Real pi_c = myhalf * (pi_stage_ca(i-1,j,k,0) + pi_stage_ca(i,j,k,0));
284  Real fast_rhs_rho_u = -Gamma * R_d * pi_c * gpx / (one + q);
285 
286  Real new_drho_u = prev_xmom(i,j,k) - stage_xmom(i,j,k)
287  + dtau * fast_rhs_rho_u + dtau * slow_rhs_rho_u(i,j,k)
288  + dtau * xmom_src_arr(i,j,k);
289 
290  Real h_zeta = dz_ptr[k] * dzi;
291  avg_xmom_arr(i,j,k) += facinv * new_drho_u * h_zeta / mf_uy(i,j,0);
292 
293  temp_cur_xmom_arr(i,j,k) = stage_xmom(i,j,k) + new_drho_u;
294  },
295  [=] AMREX_GPU_DEVICE (int i, int j, int k)
296  {
297  // Add (negative) gradient of (rho theta) multiplied by lagged "pi"
298  Real gpy = (l_real_bc && (level==0) && (j==jlo || j==jhi)) ? Real(0.) :
299  (theta_extrap(i,j,k) - theta_extrap(i,j-1,k))*dyi;
300  gpy *= mf_vy(i,j,0);
301 
302  Real q = (l_use_moisture) ? myhalf * (qt_arr(i,j,k) + qt_arr(i,j-1,k)) : zero;
303 
304  Real pi_c = myhalf * (pi_stage_ca(i,j-1,k,0) + pi_stage_ca(i,j,k,0));
305  Real fast_rhs_rho_v = -Gamma * R_d * pi_c * gpy / (one + q);
306 
307  Real new_drho_v = prev_ymom(i,j,k) - stage_ymom(i,j,k)
308  + dtau * fast_rhs_rho_v + dtau * slow_rhs_rho_v(i,j,k)
309  + dtau * ymom_src_arr(i,j,k);
310 
311  Real h_zeta = dz_ptr[k] * dzi;
312  avg_ymom_arr(i,j,k) += facinv * new_drho_v * h_zeta / mf_vx(i,j,0);
313 
314  temp_cur_ymom_arr(i,j,k) = stage_ymom(i,j,k) + new_drho_v;
315  });
316  } // nrk > 0 and/or step > 0
317  } //mfi
318 
319 #ifdef _OPENMP
320 #pragma omp parallel if (Gpu::notInLaunchRegion())
321 #endif
322  {
323  std::array<FArrayBox,AMREX_SPACEDIM> flux;
324  for ( MFIter mfi(S_stage_data[IntVars::cons],TileNoZ()); mfi.isValid(); ++mfi)
325  {
326  Box bx = mfi.tilebox();
327  Box tbz = surroundingNodes(bx,2);
328 
329  Box vbx = mfi.validbox();
330  const auto& vbx_hi = ubound(vbx);
331 
332  const Array4<Real const>& zmom_src_arr = zmom_src.const_array(mfi);
333 
334  const Array4<const Real>& stage_xmom = S_stage_data[IntVars::xmom].const_array(mfi);
335  const Array4<const Real>& stage_ymom = S_stage_data[IntVars::ymom].const_array(mfi);
336  const Array4<const Real>& stage_zmom = S_stage_data[IntVars::zmom].const_array(mfi);
337  const Array4<const Real> & prim = S_stage_prim.const_array(mfi);
338 
339  const Array4<const Real>& prev_drho_theta = Delta_rho_theta.array(mfi);
340 
341  const Array4<const Real>& prev_cons = S_prev[IntVars::cons].const_array(mfi);
342  const Array4<const Real>& stage_cons = S_stage_data[IntVars::cons].const_array(mfi);
343 
344  const Array4<const Real>& slow_rhs_cons = S_slow_rhs[IntVars::cons].const_array(mfi);
345  const Array4<const Real>& slow_rhs_rho_w = S_slow_rhs[IntVars::zmom].const_array(mfi);
346 
347  const Array4<const Real>& prev_zmom = S_prev[IntVars::zmom].const_array(mfi);
348  const Array4< Real>& cur_zmom = S_data[IntVars::zmom].array(mfi);
349 
350  const Array4<Real>& temp_cur_xmom_arr = temp_cur_xmom.array(mfi);
351  const Array4<Real>& temp_cur_ymom_arr = temp_cur_ymom.array(mfi);
352 
353  // These store the advection momenta which we will use to update the slow variables
354  const Array4< Real>& avg_zmom_arr = avg_zmom.array(mfi);
355 
356  // Map factors
357  const Array4<const Real>& mf_mx = mapfac[MapFacType::m_x]->const_array(mfi);
358  const Array4<const Real>& mf_my = mapfac[MapFacType::m_y]->const_array(mfi);
359  const Array4<const Real>& mf_uy = mapfac[MapFacType::u_y]->const_array(mfi);
360  const Array4<const Real>& mf_vx = mapfac[MapFacType::v_x]->const_array(mfi);
361 
362  FArrayBox RHS_fab;
363  RHS_fab.resize(tbz,1, The_Async_Arena());
364 
365  FArrayBox soln_fab;
366  soln_fab.resize(tbz,1, The_Async_Arena());
367 
368  auto const& RHS_a = RHS_fab.array();
369  auto const& soln_a = soln_fab.array();
370 
371  auto const& temp_rhs_arr = temp_rhs.array(mfi);
372 
373  auto const& coeffA_a = coeff_A_mf.array(mfi);
374  auto const& inv_coeffB_a = inv_coeff_B_mf.array(mfi);
375  auto const& coeffC_a = coeff_C_mf.array(mfi);
376  auto const& coeffP_a = coeff_P_mf.array(mfi);
377  auto const& coeffQ_a = coeff_Q_mf.array(mfi);
378 
379  // *************************************************************************
380  // Define flux arrays for use in advection
381  // *************************************************************************
382  for (int dir = 0; dir < AMREX_SPACEDIM; ++dir) {
383  flux[dir].resize(surroundingNodes(bx,dir),2,The_Async_Arena());
384  flux[dir].setVal<RunOn::Device>(0);
385  }
386  const GpuArray<const Array4<Real>, AMREX_SPACEDIM>
387  flx_arr{{AMREX_D_DECL(flux[0].array(), flux[1].array(), flux[2].array())}};
388 
389  // *********************************************************************
390  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
391  Real xflux_lo = (temp_cur_xmom_arr(i ,j,k) - stage_xmom(i ,j,k)) / mf_uy(i ,j,0);
392  Real xflux_hi = (temp_cur_xmom_arr(i+1,j,k) - stage_xmom(i+1,j,k)) / mf_uy(i+1,j,0);
393  Real yflux_lo = (temp_cur_ymom_arr(i,j ,k) - stage_ymom(i,j ,k)) / mf_vx(i,j ,0);
394  Real yflux_hi = (temp_cur_ymom_arr(i,j+1,k) - stage_ymom(i,j+1,k)) / mf_vx(i,j+1,0);
395 
396  Real mfsq = mf_mx(i,j,0) * mf_my(i,j,0);
397 
398  temp_rhs_arr(i,j,k,Rho_comp ) = ( xflux_hi - xflux_lo ) * dxi * mfsq
399  + ( yflux_hi - yflux_lo ) * dyi * mfsq;
400  temp_rhs_arr(i,j,k,RhoTheta_comp) = (( xflux_hi * (prim(i,j,k,0) + prim(i+1,j,k,0)) -
401  xflux_lo * (prim(i,j,k,0) + prim(i-1,j,k,0)) ) * dxi * mfsq +
402  ( yflux_hi * (prim(i,j,k,0) + prim(i,j+1,k,0)) -
403  yflux_lo * (prim(i,j,k,0) + prim(i,j-1,k,0)) ) * dyi * mfsq) * myhalf;
404 
405  if (l_reflux) {
406  (flx_arr[0])(i,j,k,0) = xflux_lo;
407  (flx_arr[0])(i,j,k,1) = (flx_arr[0])(i ,j,k,0) * myhalf * (prim(i,j,k,0) + prim(i-1,j,k,0));
408 
409  (flx_arr[1])(i,j,k,0) = yflux_lo;
410  (flx_arr[1])(i,j,k,1) = (flx_arr[1])(i,j ,k,0) * myhalf * (prim(i,j,k,0) + prim(i,j-1,k,0));
411 
412  if (i == vbx_hi.x) {
413  (flx_arr[0])(i+1,j,k,0) = xflux_hi;
414  (flx_arr[0])(i+1,j,k,1) = (flx_arr[0])(i+1,j,k,0) * myhalf * (prim(i,j,k,0) + prim(i+1,j,k,0));
415  }
416  if (j == vbx_hi.y) {
417  (flx_arr[1])(i,j+1,k,0) = yflux_hi;
418  (flx_arr[1])(i,j+1,k,1) = (flx_arr[1])(i,j+1,k,0) * myhalf * (prim(i,j,k,0) + prim(i,j+1,k,0));
419  }
420  }
421  });
422 
423  Box bx_shrunk_in_k = bx;
424  int klo = tbz.smallEnd(2);
425  int khi = tbz.bigEnd(2);
426  bx_shrunk_in_k.setSmall(2,klo+1);
427  bx_shrunk_in_k.setBig(2,khi-1);
428 
429  // Note that the notes use "g" to mean the magnitude of gravity, so it is positive
430  // We set grav_gpu[2] to be the vector component which is negative
431  // We define halfg to match the notes (which is why we take the absolute value)
432  Real halfg = std::abs(myhalf * grav_gpu[2]);
433 
434  // *********************************************************************
435  // fast_loop_on_shrunk
436  // *********************************************************************
437  //Note we don't act on the bottom or top boundaries of the domain
438  ParallelFor(bx_shrunk_in_k, [=] AMREX_GPU_DEVICE (int i, int j, int k)
439  {
440  Real coeff_P = coeffP_a(i,j,k);
441  Real coeff_Q = coeffQ_a(i,j,k);
442 
443  Real theta_t_lo = myhalf * ( prim(i,j,k-2,PrimTheta_comp) + prim(i,j,k-1,PrimTheta_comp) );
444  Real theta_t_mid = myhalf * ( prim(i,j,k-1,PrimTheta_comp) + prim(i,j,k ,PrimTheta_comp) );
445  Real theta_t_hi = myhalf * ( prim(i,j,k ,PrimTheta_comp) + prim(i,j,k+1,PrimTheta_comp) );
446 
447  Real Omega_kp1 = prev_zmom(i,j,k+1) - stage_zmom(i,j,k+1);
448  Real Omega_k = prev_zmom(i,j,k ) - stage_zmom(i,j,k );
449  Real Omega_km1 = prev_zmom(i,j,k-1) - stage_zmom(i,j,k-1);
450 
451  // line 2 last two terms (order dtau)
452  Real old_drho_k = prev_cons(i,j,k ,Rho_comp) - stage_cons(i,j,k ,Rho_comp);
453  Real old_drho_km1 = prev_cons(i,j,k-1,Rho_comp) - stage_cons(i,j,k-1,Rho_comp);
454  Real R0_tmp = coeff_P * prev_drho_theta(i,j,k) + coeff_Q * prev_drho_theta(i,j,k-1)
455  - halfg * ( old_drho_k + old_drho_km1 );
456 
457  // lines 3-5 residuals (order dtau^2) one <-> beta_2
458  Real R1_tmp = halfg * (-slow_rhs_cons(i,j,k ,Rho_comp) - slow_rhs_cons(i,j,k-1,Rho_comp)
459  + temp_rhs_arr(i,j,k ,Rho_comp) + temp_rhs_arr(i,j,k-1,Rho_comp) )
460  + ( coeff_P * (slow_rhs_cons(i,j,k ,RhoTheta_comp) - temp_rhs_arr(i,j,k ,RhoTheta_comp)) +
461  coeff_Q * (slow_rhs_cons(i,j,k-1,RhoTheta_comp) - temp_rhs_arr(i,j,k-1,RhoTheta_comp)) );
462 
463  // lines 6&7 consolidated (reuse Omega & metrics) (order dtau^2)
464  R1_tmp += beta_1 * ( ( (Omega_kp1 - Omega_k) / dz_ptr[k] + (Omega_k - Omega_km1) / dz_ptr[k-1] ) * halfg
465  +(-(Omega_kp1*theta_t_hi - Omega_k *theta_t_mid) * coeff_P / dz_ptr[k]
466  -(Omega_k *theta_t_mid - Omega_km1*theta_t_lo ) * coeff_Q / dz_ptr[k-1]) );
467 
468  // line 1
469  RHS_a(i,j,k) = Omega_k + dtau * (slow_rhs_rho_w(i,j,k) + R0_tmp + dtau * beta_2 * R1_tmp + zmom_src_arr(i,j,k));
470 
471  }); // bx_shrunk_in_k
472 
473  Box b2d = tbz; // Copy constructor
474  b2d.setRange(2,0);
475 
476  auto const lo = lbound(bx);
477  auto const hi = ubound(bx);
478 
479  ParallelFor(b2d, [=] AMREX_GPU_DEVICE (int i, int j, int)
480  {
481  // w at bottom boundary of grid is 0 if at domain boundary, otherwise w = w_old + dtau * slow_rhs
482  RHS_a (i,j,lo.z) = prev_zmom(i,j,lo.z) - stage_zmom(i,j,lo.z)
483  + dtau * slow_rhs_rho_w(i,j,lo.z)
484  + dtau * zmom_src_arr(i,j,lo.z);
485 
486  // w at top boundary of grid is 0 if at domain boundary, otherwise w = w_old + dtau * slow_rhs
487  RHS_a (i,j,hi.z+1) = prev_zmom(i,j,hi.z+1) - stage_zmom(i,j,hi.z+1)
488  + dtau * slow_rhs_rho_w(i,j,hi.z+1)
489  + dtau * zmom_src_arr(i,j,hi.z+1);
490  }); // b2d
491 
492 #ifdef AMREX_USE_GPU
493  ParallelFor(b2d, [=] AMREX_GPU_DEVICE (int i, int j, int)
494  {
495  // w = specified Dirichlet value at k = lo.z
496  soln_a(i,j,lo.z) = RHS_a(i,j,lo.z) * inv_coeffB_a(i,j,lo.z);
497  cur_zmom(i,j,lo.z) = stage_zmom(i,j,lo.z) + soln_a(i,j,lo.z);
498 
499  for (int k = lo.z+1; k <= hi.z+1; k++) {
500  soln_a(i,j,k) = (RHS_a(i,j,k)-coeffA_a(i,j,k)*soln_a(i,j,k-1)) * inv_coeffB_a(i,j,k);
501  }
502 
503  cur_zmom(i,j,hi.z+1) = stage_zmom(i,j,hi.z+1) + soln_a(i,j,hi.z+1);
504 
505  for (int k = hi.z; k >= lo.z; k--) {
506  soln_a(i,j,k) -= ( coeffC_a(i,j,k) * inv_coeffB_a(i,j,k) ) *soln_a(i,j,k+1);
507  cur_zmom(i,j,k) = stage_zmom(i,j,k) + soln_a(i,j,k);
508  }
509  }); // b2d
510 #else
511  for (int j = lo.y; j <= hi.y; ++j) {
512  AMREX_PRAGMA_SIMD
513  for (int i = lo.x; i <= hi.x; ++i) {
514  soln_a(i,j,lo.z) = RHS_a(i,j,lo.z) * inv_coeffB_a(i,j,lo.z);
515  }
516  }
517  for (int k = lo.z+1; k <= hi.z+1; ++k) {
518  for (int j = lo.y; j <= hi.y; ++j) {
519  AMREX_PRAGMA_SIMD
520  for (int i = lo.x; i <= hi.x; ++i) {
521  soln_a(i,j,k) = (RHS_a(i,j,k)-coeffA_a(i,j,k)*soln_a(i,j,k-1)) * inv_coeffB_a(i,j,k);
522  }
523  }
524  }
525  for (int j = lo.y; j <= hi.y; ++j) {
526  AMREX_PRAGMA_SIMD
527  for (int i = lo.x; i <= hi.x; ++i) {
528  cur_zmom(i,j,hi.z+1) = stage_zmom(i,j,hi.z+1) + soln_a(i,j,hi.z+1);
529  }
530  }
531  for (int k = hi.z; k >= lo.z; --k) {
532  for (int j = lo.y; j <= hi.y; ++j) {
533  AMREX_PRAGMA_SIMD
534  for (int i = lo.x; i <= hi.x; ++i) {
535  soln_a(i,j,k) -= ( coeffC_a(i,j,k) * inv_coeffB_a(i,j,k) ) * soln_a(i,j,k+1);
536  cur_zmom(i,j,k) = stage_zmom(i,j,k) + soln_a(i,j,k);
537  }
538  }
539  }
540 #endif
541  if (l_rayleigh_impl_for_w) {
542  ParallelFor(bx_shrunk_in_k, [=] AMREX_GPU_DEVICE (int i, int j, int k)
543  {
544  Real damping_coeff = l_damp_coef * dtau * sinesq_stag_d[k];
545  cur_zmom(i,j,k) /= (one + damping_coeff);
546  });
547  }
548 
549  // **************************************************************************
550  // Define updates in the RHS of rho and (rho theta)
551  // **************************************************************************
552  const Array4<Real>& prev_drho_w = Delta_rho_w.array(mfi);
553  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
554  {
555  Real zflux_lo = beta_2 * soln_a(i,j,k ) + beta_1 * prev_drho_w(i,j,k );
556  Real zflux_hi = beta_2 * soln_a(i,j,k+1) + beta_1 * prev_drho_w(i,j,k+1);
557 
558  avg_zmom_arr(i,j,k) += facinv*zflux_lo / (mf_mx(i,j,0) * mf_my(i,j,0));
559  if (l_reflux) {
560  (flx_arr[2])(i,j,k,0) = zflux_lo / (mf_mx(i,j,0) * mf_my(i,j,0));
561  (flx_arr[2])(i,j,k,1) = (flx_arr[2])(i,j,k,0) * myhalf * (prim(i,j,k) + prim(i,j,k-1));
562  }
563 
564  if (k == vbx_hi.z) {
565  avg_zmom_arr(i,j,k+1) += facinv * zflux_hi / (mf_mx(i,j,0) * mf_my(i,j,0));
566  if (l_reflux) {
567  (flx_arr[2])(i,j,k+1,0) = zflux_hi / (mf_mx(i,j,0) * mf_my(i,j,0));
568  (flx_arr[2])(i,j,k+1,1) = (flx_arr[2])(i,j,k+1,0) * myhalf * (prim(i,j,k) + prim(i,j,k+1));
569  }
570  }
571 
572  Real dz_inv = one / dz_ptr[k];
573  temp_rhs_arr(i,j,k,Rho_comp ) += dz_inv * ( zflux_hi - zflux_lo );
574  temp_rhs_arr(i,j,k,RhoTheta_comp) += myhalf * dz_inv * ( zflux_hi * (prim(i,j,k) + prim(i,j,k+1))
575  - zflux_lo * (prim(i,j,k) + prim(i,j,k-1)) );
576  });
577 
578  // We only add to the flux registers in the final RK step
579  if (l_reflux) {
580  int strt_comp_reflux = 0;
581  // For now we don't reflux (rho theta) because it seems to create issues at c/f boundaries
582  int num_comp_reflux = 1;
583  if (level < finest_level) {
584  fr_as_crse->CrseAdd(mfi,
585  {{AMREX_D_DECL(&(flux[0]), &(flux[1]), &(flux[2]))}},
586  dx, dtau, strt_comp_reflux, strt_comp_reflux, num_comp_reflux, RunOn::Device);
587  }
588  if (level > 0) {
589  fr_as_fine->FineAdd(mfi,
590  {{AMREX_D_DECL(&(flux[0]), &(flux[1]), &(flux[2]))}},
591  dx, dtau, strt_comp_reflux, strt_comp_reflux, num_comp_reflux, RunOn::Device);
592  }
593 
594  // This is necessary here so we don't go on to the next FArrayBox without
595  // having finished copying the fluxes into the FluxRegisters (since the fluxes
596  // are stored in temporary FArrayBox's)
597  Gpu::streamSynchronize();
598 
599  } // two-way coupling
600  } // mfi
601  } // OMP
602 
603 #ifdef _OPENMP
604 #pragma omp parallel if (Gpu::notInLaunchRegion())
605 #endif
606  for ( MFIter mfi(S_stage_data[IntVars::cons],TilingIfNotGPU()); mfi.isValid(); ++mfi)
607  {
608  const Box& bx = mfi.tilebox();
609 
610  const Array4< Real>& cur_cons = S_data[IntVars::cons].array(mfi);
611  const Array4<const Real>& prev_cons = S_prev[IntVars::cons].const_array(mfi);
612  auto const& temp_rhs_arr = temp_rhs.const_array(mfi);
613  auto const& slow_rhs_cons = S_slow_rhs[IntVars::cons].const_array(mfi);
614  const Array4<Real const>& cc_src_arr = cc_src.const_array(mfi);
615 
616  if (step == 0) {
617  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
618  {
619  cur_cons(i,j,k,Rho_comp) = prev_cons(i,j,k,Rho_comp) +
620  dtau * (slow_rhs_cons(i,j,k,Rho_comp) - temp_rhs_arr(i,j,k,Rho_comp));
621  cur_cons(i,j,k,RhoTheta_comp) = prev_cons(i,j,k,RhoTheta_comp) +
622  dtau * (slow_rhs_cons(i,j,k,RhoTheta_comp) - temp_rhs_arr(i,j,k,RhoTheta_comp));
623 
624  // add in source terms for cell-centered conserved variables
625  cur_cons(i,j,k,Rho_comp) += dtau * cc_src_arr(i,j,k,Rho_comp);
626  cur_cons(i,j,k,RhoTheta_comp) += dtau * cc_src_arr(i,j,k,RhoTheta_comp);
627  });
628  } else {
629  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
630  {
631  //
632  // We didn't need to set cur_cons = prev_cons above because they point to the same data for step > 0
633  //
634  cur_cons(i,j,k,Rho_comp) += dtau * (slow_rhs_cons(i,j,k,Rho_comp) - temp_rhs_arr(i,j,k,Rho_comp));
635  cur_cons(i,j,k,RhoTheta_comp) += dtau * (slow_rhs_cons(i,j,k,RhoTheta_comp) - temp_rhs_arr(i,j,k,RhoTheta_comp));
636 
637  // add in source terms for cell-centered conserved variables
638  cur_cons(i,j,k,Rho_comp) += dtau * cc_src_arr(i,j,k,Rho_comp);
639  cur_cons(i,j,k,RhoTheta_comp) += dtau * cc_src_arr(i,j,k,RhoTheta_comp);
640  });
641  } // step = 0
642 
643  const Array4<Real>& cur_xmom = S_data[IntVars::xmom].array(mfi);
644  const Array4<Real>& cur_ymom = S_data[IntVars::ymom].array(mfi);
645 
646  const Array4<Real const>& temp_cur_xmom_arr = temp_cur_xmom.const_array(mfi);
647  const Array4<Real const>& temp_cur_ymom_arr = temp_cur_ymom.const_array(mfi);
648 
649  Box tbx = surroundingNodes(bx,0);
650  Box tby = surroundingNodes(bx,1);
651 
652  ParallelFor(tbx, tby,
653  [=] AMREX_GPU_DEVICE (int i, int j, int k)
654  {
655  cur_xmom(i,j,k) = temp_cur_xmom_arr(i,j,k);
656  },
657  [=] AMREX_GPU_DEVICE (int i, int j, int k)
658  {
659  cur_ymom(i,j,k) = temp_cur_ymom_arr(i,j,k);
660  });
661 
662  } // mfi
663 }
constexpr amrex::Real RvoRd
Definition: ERF_Constants.H:43
constexpr amrex::Real R_d
Definition: ERF_Constants.H:34
constexpr amrex::Real Gamma
Definition: ERF_Constants.H:54
@ v_x
Definition: ERF_DataStruct.H:29
@ u_y
Definition: ERF_DataStruct.H:30
@ v_y
Definition: ERF_DataStruct.H:30
@ m_y
Definition: ERF_DataStruct.H:30
@ u_x
Definition: ERF_DataStruct.H:29
@ m_x
Definition: ERF_DataStruct.H:29
#define PrimQ1_comp
Definition: ERF_IndexDefines.H:61
#define Rho_comp
Definition: ERF_IndexDefines.H:39
#define RhoTheta_comp
Definition: ERF_IndexDefines.H:40
#define PrimTheta_comp
Definition: ERF_IndexDefines.H:58
amrex::GpuArray< Real, AMREX_SPACEDIM > dxInv
Definition: ERF_InitCustomPertVels_ParticleTests.H:17
const int klo
Definition: ERF_InitCustomPert_ABL.H:75
const Real dx
Definition: ERF_InitCustomPert_ABL.H:44
const int khi
Definition: ERF_InitCustomPert_Bubble.H:21
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
ParallelFor(fab_box, [=] AMREX_GPU_DEVICE(int i, int j, int k) { qrcuten_arr(i, j, k)=Real(0);qscuten_arr(i, j, k)=Real(0);qicuten_arr(i, j, k)=Real(0);})
constexpr amrex::Real one
Definition: ERF_NumericalConstants.H:30
constexpr amrex::Real zero
Definition: ERF_NumericalConstants.H:29
constexpr amrex::Real myhalf
Definition: ERF_NumericalConstants.H:34
amrex::Real Real
Definition: ERF_ShocInterface.H:19
AMREX_FORCE_INLINE amrex::IntVect TileNoZ()
Definition: ERF_TileNoZ.H:11
@ gpy
Definition: ERF_IndexDefines.H:225
@ gpx
Definition: ERF_IndexDefines.H:224
@ ymom
Definition: ERF_IndexDefines.H:234
@ cons
Definition: ERF_IndexDefines.H:232
@ zmom
Definition: ERF_IndexDefines.H:235
@ xmom
Definition: ERF_IndexDefines.H:233
@ qt
Definition: ERF_Kessler.H:30
@ qv
Definition: ERF_Kessler.H:31
@ q
Definition: ERF_WSM6.H:273
Here is the call graph for this function: