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

Functions

void erf_substep_MT (int step, int, int level, int finest_level, Vector< MultiFab > &S_slow_rhs, const Vector< MultiFab > &S_prev, Vector< MultiFab > &S_stg_data, const MultiFab &S_stg_prim, const MultiFab &qt, const MultiFab &pi_stage, const MultiFab &p_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, const bool use_lagged_delta_rt, std::unique_ptr< MultiFab > &z_t_rk, const MultiFab *z_t_pert, std::unique_ptr< MultiFab > &z_phys_nd_old, std::unique_ptr< MultiFab > &z_phys_nd_new, std::unique_ptr< MultiFab > &z_phys_nd_stg, std::unique_ptr< MultiFab > &detJ_cc_old, std::unique_ptr< MultiFab > &detJ_cc_new, std::unique_ptr< MultiFab > &detJ_cc_stg, const double dtau_d, 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, const Real *sinesq_stag_d, const Real l_damp_coef, const Real beta_d, const GpuArray< bool, 4 > &lateral_walls)
 

Function Documentation

◆ erf_substep_MT()

void erf_substep_MT ( int  step,
int  ,
int  level,
int  finest_level,
Vector< MultiFab > &  S_slow_rhs,
const Vector< MultiFab > &  S_prev,
Vector< MultiFab > &  S_stg_data,
const MultiFab &  S_stg_prim,
const MultiFab &  qt,
const MultiFab &  pi_stage,
const MultiFab &  p_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,
const bool  use_lagged_delta_rt,
std::unique_ptr< MultiFab > &  z_t_rk,
const MultiFab *  z_t_pert,
std::unique_ptr< MultiFab > &  z_phys_nd_old,
std::unique_ptr< MultiFab > &  z_phys_nd_new,
std::unique_ptr< MultiFab > &  z_phys_nd_stg,
std::unique_ptr< MultiFab > &  detJ_cc_old,
std::unique_ptr< MultiFab > &  detJ_cc_new,
std::unique_ptr< MultiFab > &  detJ_cc_stg,
const double  dtau_d,
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  ,
const Real *  sinesq_stag_d,
const Real  l_damp_coef,
const Real  beta_d,
const GpuArray< bool, 4 > &  lateral_walls 
)

Function for computing the fast RHS with moving terrain

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_prevprevious solution
[in]S_stg_datasolution at previous RK stage
[in]S_stg_primprimitive variables at previous RK stage
[in]pi_stageExner function at previous RK stage
[in]p_stagepressure at previous RK stage
[in]fast_coeffscoefficients for the tridiagonal solve used in the fast integrator, and beta_s on the w-faces (component 5)
[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]use_lagged_delta_rtdefine lagged_delta_rt for our next step
[in]z_t_rkrate of change of grid height – only relevant for moving terrain
[in]z_t_pertrate of change of grid height – interpolated between RK stages
[in]z_phys_nd_oldheight coordinate at nodes at old time, also used for the metric terms of the lateral pressure gradients
[in]z_phys_nd_newheight coordinate at nodes at new time
[in]z_phys_nd_stgheight coordinate at nodes at previous stage
[in]detJ_cc_oldJacobian of the metric transformation at old time
[in]detJ_cc_newJacobian of the metric transformation at new time
[in]detJ_cc_stgJacobian of the metric transformation at previous stage
[in]dtaufast time step
[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?
[in]l_damp_coef
[in]beta_ddivergence damping coefficient: how far (rho theta) is projected forward
[in]lateral_wallswhether the xlo, xhi, ylo and yhi faces are walls (zero normal velocity)
97 {
98  BL_PROFILE_REGION("erf_substep_MT()");
99 
100  Real dtau = static_cast<Real>(dtau_d);
101 
102  bool l_rayleigh_impl_for_w = (sinesq_stag_d != nullptr);
103 
104  const Real* dx = geom.CellSize();
105  const GpuArray<Real, AMREX_SPACEDIM> dxInv = geom.InvCellSizeArray();
106  const int domlo_z = geom.Domain().smallEnd(2);
107  const int domhi_z = geom.Domain().bigEnd(2);
108 
109  // The substeps keep the normal momentum on a lateral wall face (slip, no-slip, symmetry or
110  // surface layer) at its stage value, zero on all but a symmetry face, as erf_substep_T does.
111  // Over terrain that slopes normal to the wall the terrain terms of the pressure gradient are
112  // not zero on the wall face, and updating the face would carry mass through the wall until
113  // the boundary fill reset it after the substep. (Across a symmetry face the terrain is
114  // mirrored, so those terms are zero there.) The face then carries no density flux, which
115  // is the difference from the stage momentum times h_zeta at the stage
116  const int ilo = geom.Domain().smallEnd(0);
117  const int ihi = geom.Domain().bigEnd(0) + 1;
118  const int jlo = geom.Domain().smallEnd(1);
119  const int jhi = geom.Domain().bigEnd(1) + 1;
120  const bool wall_xlo = lateral_walls[0];
121  const bool wall_xhi = lateral_walls[1];
122  const bool wall_ylo = lateral_walls[2];
123  const bool wall_yhi = lateral_walls[3];
124 
125  Real dxi = dxInv[0];
126  Real dyi = dxInv[1];
127  Real dzi = dxInv[2];
128 
129  MultiFab coeff_A_mf(fast_coeffs, make_alias, 0, 1);
130  MultiFab inv_coeff_B_mf(fast_coeffs, make_alias, 1, 1);
131  MultiFab coeff_C_mf(fast_coeffs, make_alias, 2, 1);
132  MultiFab coeff_P_mf(fast_coeffs, make_alias, 3, 1);
133  MultiFab coeff_Q_mf(fast_coeffs, make_alias, 4, 1);
134 
135  // beta_s on the w-faces, from which
136  // beta_1 = myhalf * (one - beta_s) multiplies explicit terms
137  // beta_2 = myhalf * (one + beta_s) multiplies implicit terms
138  MultiFab beta_s_mf(fast_coeffs, make_alias, 5, 1);
139 
140  // *************************************************************************
141  // Set gravity as a vector
142  const Array<Real,AMREX_SPACEDIM> grav{zero, zero, -gravity};
143  const GpuArray<Real,AMREX_SPACEDIM> grav_gpu{grav[0], grav[1], grav[2]};
144 
145  MultiFab extrap(S_data[IntVars::cons].boxArray(),S_data[IntVars::cons].DistributionMap(),1,1);
146 
147  MultiFab Omega(S_data[IntVars::zmom].boxArray(), S_data[IntVars::zmom].DistributionMap(), 1, 1);
148 
149  // *************************************************************************
150  // Define updates in the current RK stg
151  // *************************************************************************
152 #ifdef _OPENMP
153 #pragma omp parallel if (Gpu::notInLaunchRegion())
154 #endif
155  {
156  FArrayBox temp_rhs_fab;
157 
158  FArrayBox RHS_fab;
159  FArrayBox soln_fab;
160 
161  std::array<FArrayBox,AMREX_SPACEDIM> flux;
162 
163  // NOTE: we leave tiling off here for efficiency -- to make this loop work with tiling
164  // will require additional changes
165  for ( MFIter mfi(S_stg_data[IntVars::cons],false); mfi.isValid(); ++mfi)
166  {
167  Box bx = mfi.tilebox();
168  Box tbx = surroundingNodes(bx,0);
169  Box tby = surroundingNodes(bx,1);
170  Box tbz = surroundingNodes(bx,2);
171 
172  Box vbx = mfi.validbox();
173  const auto& vbx_hi = ubound(vbx);
174 
175  // The vertical solve below takes the bottom of the box as the moving terrain and the
176  // top of the box as the top of the domain, so a grid that is split in z (level 0 with
177  // max_grid_size_z below the domain height) or a refined grid with c/f faces in z
178  // would be solved for with the wrong boundary rows
179  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(vbx.smallEnd(2) == domlo_z && vbx.bigEnd(2) == domhi_z,
180  "erf_substep_MT: MovingFittedMesh requires every grid to span the full height of the domain");
181 
182  const Array4<Real const>& xmom_src_arr = xmom_src.const_array(mfi);
183  const Array4<Real const>& ymom_src_arr = ymom_src.const_array(mfi);
184  const Array4<Real const>& zmom_src_arr = zmom_src.const_array(mfi);
185  const Array4<Real const>& cc_src_arr = cc_src.const_array(mfi);
186 
187  const Array4<const Real> & stg_cons = S_stg_data[IntVars::cons].const_array(mfi);
188  const Array4<const Real> & stg_xmom = S_stg_data[IntVars::xmom].const_array(mfi);
189  const Array4<const Real> & stg_ymom = S_stg_data[IntVars::ymom].const_array(mfi);
190  const Array4<const Real> & stg_zmom = S_stg_data[IntVars::zmom].const_array(mfi);
191  const Array4<const Real> & prim = S_stg_prim.const_array(mfi);
192  const Array4<const Real> & qt_arr = qt.const_array(mfi);
193 
194  const Array4<const Real>& slow_rhs_cons = S_slow_rhs[IntVars::cons].const_array(mfi);
195  const Array4<const Real>& slow_rhs_rho_u = S_slow_rhs[IntVars::xmom].const_array(mfi);
196  const Array4<const Real>& slow_rhs_rho_v = S_slow_rhs[IntVars::ymom].const_array(mfi);
197  const Array4<const Real>& slow_rhs_rho_w = S_slow_rhs[IntVars::zmom].const_array(mfi);
198 
199  const Array4<Real>& cur_cons = S_data[IntVars::cons].array(mfi);
200  const Array4<Real>& cur_xmom = S_data[IntVars::xmom].array(mfi);
201  const Array4<Real>& cur_ymom = S_data[IntVars::ymom].array(mfi);
202  const Array4<Real>& cur_zmom = S_data[IntVars::zmom].array(mfi);
203 
204  const Array4<Real>& lagged = lagged_delta_rt.array(mfi);
205 
206  const Array4<const Real>& prev_cons = S_prev[IntVars::cons].const_array(mfi);
207  const Array4<const Real>& prev_xmom = S_prev[IntVars::xmom].const_array(mfi);
208  const Array4<const Real>& prev_ymom = S_prev[IntVars::ymom].const_array(mfi);
209  const Array4<const Real>& prev_zmom = S_prev[IntVars::zmom].const_array(mfi);
210 
211  // These store the advection momenta which we will use to update the slow variables
212  const Array4<Real>& avg_xmom_arr = avg_xmom.array(mfi);
213  const Array4<Real>& avg_ymom_arr = avg_ymom.array(mfi);
214  const Array4<Real>& avg_zmom_arr = avg_zmom.array(mfi);
215 
216  // The lateral pressure gradients take their metric terms from the mesh at the sub-step start
217  const Array4<const Real>& z_nd_old = z_phys_nd_old->const_array(mfi);
218  const Array4<const Real>& z_nd_new = z_phys_nd_new->const_array(mfi);
219  const Array4<const Real>& z_nd_stg = z_phys_nd_stg->const_array(mfi);
220  const Array4<const Real>& detJ_old = detJ_cc_old->const_array(mfi);
221  const Array4<const Real>& detJ_new = detJ_cc_new->const_array(mfi);
222  const Array4<const Real>& detJ_stg = detJ_cc_stg->const_array(mfi);
223 
224  const Array4<const Real>& z_t_arr = z_t_rk->const_array(mfi);
225  const Array4<const Real>& zp_t_arr = z_t_pert->const_array(mfi);
226 
227  const Array4< Real>& omega_arr = Omega.array(mfi);
228 
229  // Map factors
230  const Array4<const Real>& mf_mx = mapfac[MapFacType::m_x]->const_array(mfi);
231  const Array4<const Real>& mf_my = mapfac[MapFacType::m_y]->const_array(mfi);
232  const Array4<const Real>& mf_ux = mapfac[MapFacType::u_x]->const_array(mfi);
233  const Array4<const Real>& mf_uy = mapfac[MapFacType::u_y]->const_array(mfi);
234  const Array4<const Real>& mf_vx = mapfac[MapFacType::v_x]->const_array(mfi);
235  const Array4<const Real>& mf_vy = mapfac[MapFacType::v_y]->const_array(mfi);
236 
237  // *********************************************************************
238  // This must be done before we set cur_xmom and cur_ymom, since those
239  // in fact point to the same array as prev_xmom and prev_ymom
240  // *********************************************************************
241  Box gbxo = mfi.nodaltilebox(2);
242  {
243  BL_PROFILE("fast_MT_making_omega");
244  Box gbxo_lo = gbxo; gbxo_lo.setBig(2,0);
245  ParallelFor(gbxo_lo, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
246  omega_arr(i,j,k) = zero;
247  });
248  Box gbxo_hi = gbxo; gbxo_hi.setSmall(2,gbxo.bigEnd(2));
249  ParallelFor(gbxo_hi, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
250  // omega is the perturbation of the mass flux Omega - rho z_t through the moving face, with
251  // z_t = z_t_rk + z_t'': (Omega - rho z_t) - (Omega_stg - rho_stg z_t_rk)
252  Real rho_on_face = myhalf * (prev_cons(i,j,k,Rho_comp) + prev_cons(i,j,k-1,Rho_comp));
253  Real rho_stg_face = myhalf * (stg_cons(i,j,k,Rho_comp) + stg_cons(i,j,k-1,Rho_comp));
254  omega_arr(i,j,k) = prev_zmom(i,j,k) - stg_zmom(i,j,k) - rho_on_face * zp_t_arr(i,j,k)
255  - (rho_on_face - rho_stg_face) * z_t_arr(i,j,k);
256  });
257 
258  // gbxo_lo covers k<=0 and gbxo_hi covers the box's top face, so the mid box is
259  // [max(smallEnd,1), bigEnd-1]. Using max() never EXPANDS the box past the FAB,
260  // which is what setSmall(2,1) does for a box that starts above k=1.
261  Box gbxo_mid = gbxo;
262  gbxo_mid.setSmall(2, std::max(gbxo.smallEnd(2), 1));
263  gbxo_mid.setBig (2, gbxo.bigEnd(2)-1);
264 
265  ParallelFor(gbxo_mid, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
266  Real rho_on_face = myhalf * (prev_cons(i,j,k,Rho_comp) + prev_cons(i,j,k-1,Rho_comp));
267  Real rho_stg_face = myhalf * (stg_cons(i,j,k,Rho_comp) + stg_cons(i,j,k-1,Rho_comp));
268  // Each state on its own mesh: the stage flux was formed on the stage mesh. The grid-velocity
269  // part is rho z_t - rho_stg z_t_rk, so every density multiplying the grid velocity is the
270  // current one, as in the volume-change term of the update
271  omega_arr(i,j,k) =
272  ( OmegaFromW(i,j,k,prev_zmom(i,j,k),prev_xmom,prev_ymom,mf_ux,mf_vy,z_nd_old,dxInv)
273  -OmegaFromW(i,j,k, stg_zmom(i,j,k), stg_xmom, stg_ymom,mf_ux,mf_vy,z_nd_stg,dxInv) )
274  - rho_on_face * zp_t_arr(i,j,k)
275  - (rho_on_face - rho_stg_face) * z_t_arr(i,j,k);
276  });
277  } // end profile
278  // *********************************************************************
279 
280  const Array4<const Real>& pi_stage_ca = pi_stage.const_array(mfi);
281 
282  const Array4<Real>& theta_extrap = extrap.array(mfi);
283 
284  // Note: it is important to grow the tilebox rather than use growntilebox because
285  // we need to fill the ghost cells of the tilebox so we can use them below
286  Box gbx = mfi.tilebox(); gbx.grow(1);
287  Box gtbx = mfi.nodaltilebox(0); gtbx.grow(1); gtbx.setSmall(2,0);
288  Box gtby = mfi.nodaltilebox(1); gtby.grow(1); gtby.setSmall(2,0);
289 
290  if (step == 0) {
291  ParallelFor(gbx,
292  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
293  cur_cons(i,j,k,Rho_comp) = prev_cons(i,j,k,Rho_comp);
294  cur_cons(i,j,k,RhoTheta_comp) = prev_cons(i,j,k,RhoTheta_comp);
295 
296  Real delta_rt = cur_cons(i,j,k,RhoTheta_comp) - stg_cons(i,j,k,RhoTheta_comp);
297  theta_extrap(i,j,k) = delta_rt;
298 
299  // NOTE: qv is not changing over the fast steps so we use the stage data
300  Real qv = (l_use_moisture) ? prim(i,j,k,PrimQ1_comp) : zero;
301  theta_extrap(i,j,k) *= (one + RvoRd*qv);
302 
303  // We define lagged_delta_rt for our next step as the current delta_rt
304  lagged(i,j,k) = delta_rt;
305  });
306  } else if (use_lagged_delta_rt) {
307  // This is the default for cases with no or static terrain
308  ParallelFor(gbx,
309  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
310  Real delta_rt = cur_cons(i,j,k,RhoTheta_comp) - stg_cons(i,j,k,RhoTheta_comp);
311  theta_extrap(i,j,k) = delta_rt + beta_d * (delta_rt - lagged(i,j,k));
312 
313  // NOTE: qv is not changing over the fast steps so we use the stage data
314  Real qv = (l_use_moisture) ? prim(i,j,k,PrimQ1_comp) : zero;
315  theta_extrap(i,j,k) *= (one + RvoRd*qv);
316 
317  // We define lagged_delta_rt for our next step as the current delta_rt
318  lagged(i,j,k) = delta_rt;
319  });
320  } else {
321  // For the moving wave problem, this choice seems more robust
322  ParallelFor(gbx,
323  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
324  theta_extrap(i,j,k) = cur_cons(i,j,k,RhoTheta_comp) - stg_cons(i,j,k,RhoTheta_comp);
325 
326  // NOTE: qv is not changing over the fast steps so we use the stage data
327  Real qv = (l_use_moisture) ? prim(i,j,k,PrimQ1_comp) : zero;
328  theta_extrap(i,j,k) *= (one + RvoRd*qv);
329  });
330  } // if step
331 
332  RHS_fab.resize (tbz,1, The_Async_Arena());
333  soln_fab.resize (tbz,1, The_Async_Arena());
334  temp_rhs_fab.resize(tbz,2, The_Async_Arena());
335 
336  auto const& RHS_a = RHS_fab.array();
337  auto const& soln_a = soln_fab.array();
338  auto const& temp_rhs_arr = temp_rhs_fab.array();
339 
340  auto const& coeffA_a = coeff_A_mf.array(mfi);
341  auto const& inv_coeffB_a = inv_coeff_B_mf.array(mfi);
342  auto const& coeffC_a = coeff_C_mf.array(mfi);
343  auto const& coeffP_a = coeff_P_mf.array(mfi);
344  auto const& coeffQ_a = coeff_Q_mf.array(mfi);
345  auto const& beta_s_a = beta_s_mf.const_array(mfi);
346 
347  // *********************************************************************
348  // Horizontal pressure gradient with each state on its own mesh. The fast term is the gradient of
349  // the pressure of the current state on the mesh of the sub-step start minus the gradient of the
350  // pressure of the stage state on the stage mesh,
351  // [h_zeta d_xi - h_xi d_zeta]_old (p_stg + p'') - [h_zeta d_xi - h_xi d_zeta]_stg p_stg
352  // This is the gradient of the perturbation p'' with the metric terms of the sub-step start, plus
353  // the change of the metric terms since the stage time applied to the stage pressure.
354  // Without the second part a state at rest in hydrostatic balance does not stay at rest: the height
355  // of each cell changes, so p'' = -rho g (z - z_stg), and the gradient of this p'' along the sloping
356  // coordinate surface is not balanced.
357  // p_stage is the complete pressure of the stage state, made one time for the stage in make_pi_stage.
358  // The slow tendency has the gradient on the stage mesh (make_gradp_pert)
359  // *********************************************************************
360  const Array4<const Real>& pstg_a = p_stage.const_array(mfi);
361 
362  // *********************************************************************
363  // Define updates in the RHS of {x, y, z}-momentum equations
364  // *********************************************************************
365  {
366  BL_PROFILE("substep_xymom_T");
367  ParallelFor(tbx, tby,
368  [=] AMREX_GPU_DEVICE (int i, int j, int k)
369  {
370  // Add (negative) gradient of (rho theta) multiplied by lagged "pi"
371  // Metric terms and cell volume at the sub-step start
372  Real h_xi_old = Compute_h_xi_AtIface(i, j, k, dxInv, z_nd_old);
373  Real h_zeta_old = Compute_h_zeta_AtIface(i, j, k, dxInv, z_nd_old);
374  Real gp_xi = (theta_extrap(i,j,k) - theta_extrap(i-1,j,k)) * dxi;
375  Real gp_zeta_on_iface = (k == 0) ?
376  myhalf * dzi * ( theta_extrap(i-1,j,k+1) + theta_extrap(i,j,k+1)
377  - theta_extrap(i-1,j,k ) - theta_extrap(i,j,k ) ) :
378  fourth * dzi * ( theta_extrap(i-1,j,k+1) + theta_extrap(i,j,k+1)
379  - theta_extrap(i-1,j,k-1) - theta_extrap(i,j,k-1) );
380  Real gpx = h_zeta_old * gp_xi - h_xi_old * gp_zeta_on_iface;
381  gpx *= mf_ux(i,j,0);
382 
383  Real q = (l_use_moisture) ? myhalf * (qt_arr(i-1,j,k) + qt_arr(i,j,k)) : zero;
384 
385  Real pi_c = myhalf * (pi_stage_ca(i-1,j,k) + pi_stage_ca(i ,j,k));
386  Real fast_rhs_rho_u = -Gamma * R_d * pi_c * gpx / (one + q);
387 
388  // Change of the metric terms since the stage time, applied to the stage pressure
389  // (the same differences as for the perturbation above)
390  Real dh_xi = h_xi_old - Compute_h_xi_AtIface (i, j, k, dxInv, z_nd_stg);
391  Real dh_zeta = h_zeta_old - Compute_h_zeta_AtIface(i, j, k, dxInv, z_nd_stg);
392  Real ps_xi = (pstg_a(i,j,k) - pstg_a(i-1,j,k)) * dxi;
393  Real ps_zeta_on_iface = (k == 0) ?
394  myhalf * dzi * ( pstg_a(i-1,j,k+1) + pstg_a(i,j,k+1)
395  - pstg_a(i-1,j,k ) - pstg_a(i,j,k ) ) :
396  fourth * dzi * ( pstg_a(i-1,j,k+1) + pstg_a(i,j,k+1)
397  - pstg_a(i-1,j,k-1) - pstg_a(i,j,k-1) );
398  fast_rhs_rho_u -= (dh_zeta * ps_xi - dh_xi * ps_zeta_on_iface) * mf_ux(i,j,0) / (one + q);
399 
400  // We have already scaled the source terms to have the extra factor of dJ
401  cur_xmom(i,j,k) = h_zeta_old * prev_xmom(i,j,k) + dtau * fast_rhs_rho_u
402  + dtau * slow_rhs_rho_u(i,j,k)
403  + dtau * xmom_src_arr(i,j,k);
404  if ((wall_xlo && i == ilo) || (wall_xhi && i == ihi)) {
405  cur_xmom(i,j,k) = stg_xmom(i,j,k) * Compute_h_zeta_AtIface(i, j, k, dxInv, z_nd_stg);
406  }
407  },
408  [=] AMREX_GPU_DEVICE (int i, int j, int k)
409  {
410  // Add (negative) gradient of (rho theta) multiplied by lagged "pi"
411  // Metric terms and cell volume at the sub-step start
412  Real h_eta_old = Compute_h_eta_AtJface(i, j, k, dxInv, z_nd_old);
413  Real h_zeta_old = Compute_h_zeta_AtJface(i, j, k, dxInv, z_nd_old);
414  Real gp_eta = (theta_extrap(i,j,k) -theta_extrap(i,j-1,k)) * dyi;
415  Real gp_zeta_on_jface = (k == 0) ?
416  myhalf * dzi * ( theta_extrap(i,j,k+1) + theta_extrap(i,j-1,k+1)
417  - theta_extrap(i,j,k ) - theta_extrap(i,j-1,k ) ) :
418  fourth * dzi * ( theta_extrap(i,j,k+1) + theta_extrap(i,j-1,k+1)
419  - theta_extrap(i,j,k-1) - theta_extrap(i,j-1,k-1) );
420  Real gpy = h_zeta_old * gp_eta - h_eta_old * gp_zeta_on_jface;
421  gpy *= mf_vy(i,j,0);
422 
423  Real q = (l_use_moisture) ? myhalf * (qt_arr(i,j-1,k) + qt_arr(i,j,k)) : zero;
424 
425  Real pi_c = myhalf * (pi_stage_ca(i,j-1,k) + pi_stage_ca(i,j ,k));
426  Real fast_rhs_rho_v = -Gamma * R_d * pi_c * gpy / (one + q);
427 
428  // Change of the metric terms since the stage time, applied to the stage pressure
429  Real dh_eta = h_eta_old - Compute_h_eta_AtJface (i, j, k, dxInv, z_nd_stg);
430  Real dh_zeta = h_zeta_old - Compute_h_zeta_AtJface(i, j, k, dxInv, z_nd_stg);
431  Real ps_eta = (pstg_a(i,j,k) - pstg_a(i,j-1,k)) * dyi;
432  Real ps_zeta_on_jface = (k == 0) ?
433  myhalf * dzi * ( pstg_a(i,j,k+1) + pstg_a(i,j-1,k+1)
434  - pstg_a(i,j,k ) - pstg_a(i,j-1,k ) ) :
435  fourth * dzi * ( pstg_a(i,j,k+1) + pstg_a(i,j-1,k+1)
436  - pstg_a(i,j,k-1) - pstg_a(i,j-1,k-1) );
437  fast_rhs_rho_v -= (dh_zeta * ps_eta - dh_eta * ps_zeta_on_jface) * mf_vy(i,j,0) / (one + q);
438 
439  // We have already scaled the source terms to have the extra factor of dJ
440  cur_ymom(i, j, k) = h_zeta_old * prev_ymom(i,j,k) + dtau * fast_rhs_rho_v
441  + dtau * slow_rhs_rho_v(i,j,k)
442  + dtau * ymom_src_arr(i,j,k);
443  if ((wall_ylo && j == jlo) || (wall_yhi && j == jhi)) {
444  cur_ymom(i,j,k) = stg_ymom(i,j,k) * Compute_h_zeta_AtJface(i, j, k, dxInv, z_nd_stg);
445  }
446  });
447  } // end profile
448 
449  // *************************************************************************
450  // Define flux arrays for use in advection
451  // *************************************************************************
452  for (int dir = 0; dir < AMREX_SPACEDIM; ++dir) {
453  flux[dir].resize(surroundingNodes(bx,dir),2,The_Async_Arena());
454  flux[dir].setVal<RunOn::Device>(0);
455  }
456  const GpuArray<const Array4<Real>, AMREX_SPACEDIM>
457  flx_arr{{AMREX_D_DECL(flux[0].array(), flux[1].array(), flux[2].array())}};
458 
459  // *********************************************************************
460  {
461  BL_PROFILE("fast_T_making_rho_rhs");
462  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
463  {
464  Real h_zeta_stg_xlo = Compute_h_zeta_AtIface(i, j , k, dxInv, z_nd_stg);
465  Real h_zeta_stg_xhi = Compute_h_zeta_AtIface(i+1,j , k, dxInv, z_nd_stg);
466  Real xflux_lo = (cur_xmom(i ,j,k) - stg_xmom(i ,j,k)*h_zeta_stg_xlo) / mf_uy(i ,j,0);
467  Real xflux_hi = (cur_xmom(i+1,j,k) - stg_xmom(i+1,j,k)*h_zeta_stg_xhi) / mf_uy(i+1,j,0);
468 
469  Real h_zeta_stg_yhi = Compute_h_zeta_AtJface(i, j+1, k, dxInv, z_nd_stg);
470  Real h_zeta_stg_ylo = Compute_h_zeta_AtJface(i, j , k, dxInv, z_nd_stg);
471  Real yflux_lo = (cur_ymom(i,j ,k) - stg_ymom(i,j ,k)*h_zeta_stg_ylo) / mf_vx(i,j ,0);
472  Real yflux_hi = (cur_ymom(i,j+1,k) - stg_ymom(i,j+1,k)*h_zeta_stg_yhi) / mf_vx(i,j+1,0);
473 
474  Real mfsq = mf_mx(i,j,0) * mf_my(i,j,0);
475 
476  // NOTE: we are saving the (1/J) weighting for later when we add this to rho and theta
477  temp_rhs_arr(i,j,k,0) = ( xflux_hi - xflux_lo ) * dxi * mfsq + ( yflux_hi - yflux_lo ) * dyi * mfsq;
478  temp_rhs_arr(i,j,k,1) = (( xflux_hi * (prim(i,j,k,0) + prim(i+1,j,k,0)) -
479  xflux_lo * (prim(i,j,k,0) + prim(i-1,j,k,0)) ) * dxi * mfsq +
480  ( yflux_hi * (prim(i,j,k,0) + prim(i,j+1,k,0)) -
481  yflux_lo * (prim(i,j,k,0) + prim(i,j-1,k,0)) ) * dyi * mfsq) * myhalf;
482 
483  if (l_reflux) {
484  (flx_arr[0])(i,j,k,0) = xflux_lo;
485  (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));
486 
487  (flx_arr[1])(i,j,k,0) = yflux_lo;
488  (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));
489 
490  if (i == vbx_hi.x) {
491  (flx_arr[0])(i+1,j,k,0) = xflux_hi;
492  (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));
493  }
494  if (j == vbx_hi.y) {
495  (flx_arr[1])(i,j+1,k,0) = yflux_hi;
496  (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));
497  }
498  }
499  });
500  } // end profile
501 
502  ParallelFor(tbx, tby,
503  [=] AMREX_GPU_DEVICE (int i, int j, int k)
504  {
505  Real h_zeta_new = Compute_h_zeta_AtIface(i, j, k, dxInv, z_nd_new);
506  cur_xmom(i, j, k) /= h_zeta_new;
507  avg_xmom_arr(i,j,k) += facinv*(cur_xmom(i,j,k) - stg_xmom(i,j,k)) / mf_uy(i,j,0);
508  },
509  [=] AMREX_GPU_DEVICE (int i, int j, int k)
510  {
511  Real h_zeta_new = Compute_h_zeta_AtJface(i, j, k, dxInv, z_nd_new);
512  cur_ymom(i, j, k) /= h_zeta_new;
513  avg_ymom_arr(i,j,k) += facinv*(cur_ymom(i,j,k) - stg_ymom(i,j,k)) / mf_vx(i,j,0);
514  });
515 
516  Box bx_shrunk_in_k = bx;
517  int klo = tbz.smallEnd(2);
518  int khi = tbz.bigEnd(2);
519  bx_shrunk_in_k.setSmall(2,klo+1);
520  bx_shrunk_in_k.setBig(2,khi-1);
521 
522  // Note that the notes use "g" to mean the magnitude of gravity, so it is positive
523  // We set grav_gpu[2] to be the vector component which is negative
524  // We define halfg to match the notes (which is why we take the absolute value)
525  Real halfg = std::abs(myhalf * grav_gpu[2]);
526 
527  {
528  BL_PROFILE("fast_loop_on_shrunk_t");
529  //Note we don't act on the bottom or top boundaries of the domain
530  ParallelFor(bx_shrunk_in_k, [=] AMREX_GPU_DEVICE (int i, int j, int k)
531  {
532  // The w equation is conservative in the face volume and the row is divided by the face
533  // volume at the end of the sub-step. The volume dividing the vertical flux divergence of rho
534  // and (rho theta) is the new one, the volume the conservative update below divides by, and
535  // the face metric of the vertical pressure gradient (in coeff_P and coeff_Q, from
536  // make_fast_coeffs) is that of the new mesh
537  Real dJ_new_k = detJ_new(i,j,k );
538  Real dJ_new_km1 = detJ_new(i,j,k-1);
539 
540  // Face volumes at the sub-step ends and at the stage
541  Real dJ_old_kface = myhalf * (detJ_old(i,j,k) + detJ_old(i,j,k-1));
542  Real dJ_new_kface = myhalf * (detJ_new(i,j,k) + detJ_new(i,j,k-1));
543  Real dJ_stg_kface = myhalf * (detJ_stg(i,j,k) + detJ_stg(i,j,k-1));
544 
545  // Face volume ratios a = J_old/J_new and r = J_stg/J_new
546  Real a_kface = dJ_old_kface / dJ_new_kface;
547  Real r_kface = dJ_stg_kface / dJ_new_kface;
548 
549  Real coeff_P = coeffP_a(i,j,k);
550  Real coeff_Q = coeffQ_a(i,j,k);
551 
552  Real theta_t_lo = myhalf * ( prim(i,j,k-2,PrimTheta_comp) + prim(i,j,k-1,PrimTheta_comp) );
553  Real theta_t_mid = myhalf * ( prim(i,j,k-1,PrimTheta_comp) + prim(i,j,k ,PrimTheta_comp) );
554  Real theta_t_hi = myhalf * ( prim(i,j,k ,PrimTheta_comp) + prim(i,j,k+1,PrimTheta_comp) );
555 
556  // line 2 last two terms (order dtau). The pressure perturbation is pointwise, so the stage
557  // pressure has no volume ratio; gravity acts on the mass, so it carries the face volume
558  // ratio of its own time
559  Real R0_tmp = coeff_P * ( cur_cons(i,j,k ,RhoTheta_comp) - stg_cons(i,j,k ,RhoTheta_comp) )
560  + coeff_Q * ( cur_cons(i,j,k-1,RhoTheta_comp) - stg_cons(i,j,k-1,RhoTheta_comp) )
561  - halfg * ( cur_cons(i,j,k,Rho_comp) + cur_cons(i,j,k-1,Rho_comp) ) * a_kface
562  + halfg * ( stg_cons(i,j,k,Rho_comp) + stg_cons(i,j,k-1,Rho_comp) ) * r_kface;
563 
564  // line 3 residuals (order dtau^2) one <-> beta_2
565  // The slow tendency enters the conservative update with the stage volume:
566  // J_new q_new = J_old q_old + dtau (J_stg S_q + ...), as in the final update below
567  Real sw_k = detJ_stg(i,j,k ) / detJ_new(i,j,k );
568  Real sw_km1 = detJ_stg(i,j,k-1) / detJ_new(i,j,k-1);
569  Real R1_tmp = - halfg * ( sw_k *slow_rhs_cons(i,j,k ,Rho_comp)
570  + sw_km1*slow_rhs_cons(i,j,k-1,Rho_comp))
571  + coeff_P * sw_k *slow_rhs_cons(i,j,k ,RhoTheta_comp)
572  + coeff_Q * sw_km1*slow_rhs_cons(i,j,k-1,RhoTheta_comp);
573 
574  // Volume-change tendency -q (J_new - J_old)/(J_new dtau) of the conservative update; without it the
575  // implicit solve sees the change of the cell volume as a pressure change. Gravity acts on the
576  // face mass J_new rho_new, so the change of the face volume at fixed density, -g j_face rho_face,
577  // is added; it largely offsets the cell terms, since a change of volume alone moves no mass
578  Real jr_k = (detJ_new(i,j,k ) - detJ_old(i,j,k )) / (detJ_new(i,j,k ) * dtau);
579  Real jr_km1 = (detJ_new(i,j,k-1) - detJ_old(i,j,k-1)) / (detJ_new(i,j,k-1) * dtau);
580  Real jr_kface = (one - a_kface) / dtau;
581  R1_tmp += halfg * ( cur_cons(i,j,k,Rho_comp) * jr_k + cur_cons(i,j,k-1,Rho_comp) * jr_km1 )
582  - halfg * ( cur_cons(i,j,k,Rho_comp) + cur_cons(i,j,k-1,Rho_comp) ) * jr_kface
583  - coeff_P * cur_cons(i,j,k ,RhoTheta_comp) * jr_k
584  - coeff_Q * cur_cons(i,j,k-1,RhoTheta_comp) * jr_km1;
585 
586  Real Omega_kp1 = omega_arr(i,j,k+1);
587  Real Omega_k = omega_arr(i,j,k );
588  Real Omega_km1 = omega_arr(i,j,k-1);
589 
590  // Explicit part of the vertical flux on each face is beta_1 * Omega. beta_1 on face k
591  // is factored out, and dbeta_1 is how much larger beta_1 is on the neighboring faces,
592  // which is exactly zero (so the roundoff is that of a scalar beta_1) for uniform beta_s
593  Real beta_1 = myhalf * (one - beta_s_a(i,j,k));
594  Real dbeta_1_kp1 = myhalf * (one - beta_s_a(i,j,k+1)) - beta_1;
595  Real dbeta_1_km1 = myhalf * (one - beta_s_a(i,j,k-1)) - beta_1;
596 
597  Real detJdiff = (dJ_new_k - dJ_new_km1) / (dJ_new_k*dJ_new_km1);
598 
599  // consolidate lines 4&5 (order dtau^2)
600  R1_tmp += halfg * ( beta_1 * dzi * (Omega_kp1/dJ_new_k + detJdiff*Omega_k - Omega_km1/dJ_new_km1)
601  + dzi * (dbeta_1_kp1*Omega_kp1/dJ_new_k - dbeta_1_km1*Omega_km1/dJ_new_km1)
602  + temp_rhs_arr(i,j,k,Rho_comp)/dJ_new_k + temp_rhs_arr(i,j,k-1,Rho_comp)/dJ_new_km1 );
603 
604  // consolidate lines 6&7 (order dtau^2)
605  R1_tmp += -(
606  coeff_P/dJ_new_k * ( beta_1 * dzi * (Omega_kp1*theta_t_hi - Omega_k*theta_t_mid)
607  + dzi * dbeta_1_kp1*Omega_kp1*theta_t_hi
608  + temp_rhs_arr(i,j,k ,RhoTheta_comp) ) +
609  coeff_Q/dJ_new_km1 * ( beta_1 * dzi * (Omega_k*theta_t_mid - Omega_km1*theta_t_lo)
610  - dzi * dbeta_1_km1*Omega_km1*theta_t_lo
611  + temp_rhs_arr(i,j,k-1,RhoTheta_comp) ) );
612 
613  // The new vertical flux is beta_2 (soln - rho z_t'' - (rho - rho_stg) z_t_rk) + beta_1 omega; the
614  // matrix has beta_2 soln and lines 4-7 have beta_1 omega, so add the beta_2 part of the
615  // grid-velocity terms here. As for beta_1 above,
616  // beta_2 of face k is factored out and dbeta_2 (exactly zero for uniform beta_s) carries the
617  // difference of the neighboring faces
618  Real beta_2 = myhalf * (one + beta_s_a(i,j,k));
619  Real dbeta_2_kp1 = myhalf * (one + beta_s_a(i,j,k+1)) - beta_2;
620  Real dbeta_2_km1 = myhalf * (one + beta_s_a(i,j,k-1)) - beta_2;
621 
622  Real rf_kp1 = myhalf * (cur_cons(i,j,k+1,Rho_comp) + cur_cons(i,j,k ,Rho_comp));
623  Real rf_k = myhalf * (cur_cons(i,j,k ,Rho_comp) + cur_cons(i,j,k-1,Rho_comp));
624  Real rf_km1 = myhalf * (cur_cons(i,j,k-1,Rho_comp) + cur_cons(i,j,k-2,Rho_comp));
625  Real rs_kp1 = myhalf * (stg_cons(i,j,k+1,Rho_comp) + stg_cons(i,j,k ,Rho_comp));
626  Real rs_k = myhalf * (stg_cons(i,j,k ,Rho_comp) + stg_cons(i,j,k-1,Rho_comp));
627  Real rs_km1 = myhalf * (stg_cons(i,j,k-1,Rho_comp) + stg_cons(i,j,k-2,Rho_comp));
628  Real Z_kp1 = -rf_kp1 * zp_t_arr(i,j,k+1) - (rf_kp1 - rs_kp1) * z_t_arr(i,j,k+1);
629  Real Z_k = -rf_k * zp_t_arr(i,j,k ) - (rf_k - rs_k ) * z_t_arr(i,j,k );
630  Real Z_km1 = -rf_km1 * zp_t_arr(i,j,k-1) - (rf_km1 - rs_km1) * z_t_arr(i,j,k-1);
631  R1_tmp += halfg * beta_2 * dzi * (Z_kp1/dJ_new_k + detJdiff*Z_k - Z_km1/dJ_new_km1);
632  R1_tmp += halfg * dzi * (dbeta_2_kp1*Z_kp1/dJ_new_k - dbeta_2_km1*Z_km1/dJ_new_km1);
633  R1_tmp += -( coeff_P/dJ_new_k * beta_2 * dzi * (Z_kp1*theta_t_hi - Z_k *theta_t_mid)
634  + coeff_Q/dJ_new_km1 * beta_2 * dzi * (Z_k *theta_t_mid - Z_km1*theta_t_lo ) );
635  R1_tmp += -( coeff_P/dJ_new_k * dzi * dbeta_2_kp1*Z_kp1*theta_t_hi
636  - coeff_Q/dJ_new_km1 * dzi * dbeta_2_km1*Z_km1*theta_t_lo );
637 
638  // line 2 (the slow w tendency carries the stage face volume; the row is divided by dJ_new_kface)
639  RHS_a(i,j,k) = dtau * slow_rhs_rho_w(i,j,k) / dJ_new_kface
640  + dtau * zmom_src_arr(i,j,k) * a_kface;
641 
642  RHS_a(i,j,k) += dtau * R0_tmp;
643 
644  RHS_a(i,j,k) += dtau * dtau*beta_2*R1_tmp;
645 
646  // line 1. The unknown of the solve is the flux perturbation X = Omega_new - Omega_stg, which
647  // the rho and (rho theta) update below uses, so that the rho and (rho theta) the solve predicts
648  // are the ones stored. With W_new = X + Omega_stg + (h_xi U + h_eta V)_new the known part moves
649  // to the right: a W_old - Omega_stg - (h_xi U + h_eta V)_new, each on its own mesh.
650  // We cannot use omega_arr here since that was built with old_rho_u and old_rho_v ...
651  Real Omega_stg = OmegaFromW(i,j,k,stg_zmom(i,j,k),stg_xmom,stg_ymom,mf_ux,mf_vy,z_nd_stg,dxInv);
652  RHS_a(i,j,k) += a_kface * prev_zmom(i,j,k) - Omega_stg
653  + OmegaFromW(i,j,k,zero,cur_xmom,cur_ymom,mf_ux,mf_vy,z_nd_new,dxInv);
654  });
655  } // end profile
656 
657  Box b2d = tbz; // Copy constructor
658  b2d.setRange(2,0);
659 
660  auto const lo = lbound(bx);
661  auto const hi = ubound(bx);
662 
663  {
664  BL_PROFILE("substep_b2d_loop_t");
665 
666 #ifdef AMREX_USE_GPU
667  ParallelFor(b2d, [=] AMREX_GPU_DEVICE (int i, int j, int)
668  {
669  // Moving terrain
670  // No flux through the moving surface: Omega = rho z_t there, so
671  // Omega'' = rho z_t'' + (rho - rho_stg) z_t_rk
672  Real rho_on_bdy = myhalf * ( prev_cons(i,j,lo.z) + prev_cons(i,j,lo.z-1) );
673  Real rho_stg_bdy = myhalf * ( stg_cons(i,j,lo.z) + stg_cons(i,j,lo.z-1) );
674  RHS_a(i,j,lo.z) = rho_on_bdy * zp_t_arr(i,j,lo.z) + (rho_on_bdy - rho_stg_bdy) * z_t_arr(i,j,lo.z);
675 
676  soln_a(i,j,lo.z) = RHS_a(i,j,lo.z) * inv_coeffB_a(i,j,lo.z);
677 
678  // Zero at the domain top: Omega'' = 0 at a wall, and Omega''_top = Omega''_top-1
679  // for outflow, the matrix row set in make_fast_coeffs deciding which
680  RHS_a(i,j,hi.z+1) = (hi.z == domhi_z)
681  ? zero
682  : dtau * (slow_rhs_rho_w(i,j,hi.z+1) + zmom_src_arr(i,j,hi.z+1));
683 
684  for (int k = lo.z+1; k <= hi.z+1; k++) {
685  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);
686  }
687 
688  for (int k = hi.z; k >= lo.z; k--) {
689  soln_a(i,j,k) -= ( coeffC_a(i,j,k) * inv_coeffB_a(i,j,k) ) * soln_a(i,j,k+1);
690  }
691 
692  // We assume that Omega == w at the top boundary and that changes in J there are irrelevant
693  cur_zmom(i,j,hi.z+1) = stg_zmom(i,j,hi.z+1) + soln_a(i,j,hi.z+1);
694  });
695 #else
696  for (int j = lo.y; j <= hi.y; ++j) {
697  AMREX_PRAGMA_SIMD
698  for (int i = lo.x; i <= hi.x; ++i) {
699 
700  Real rho_on_bdy = myhalf * ( prev_cons(i,j,lo.z) + prev_cons(i,j,lo.z-1) );
701  Real rho_stg_bdy = myhalf * ( stg_cons(i,j,lo.z) + stg_cons(i,j,lo.z-1) );
702  RHS_a(i,j,lo.z) = rho_on_bdy * zp_t_arr(i,j,lo.z) + (rho_on_bdy - rho_stg_bdy) * z_t_arr(i,j,lo.z);
703 
704  soln_a(i,j,lo.z) = RHS_a(i,j,lo.z) * inv_coeffB_a(i,j,lo.z);
705  }
706  }
707 
708  for (int j = lo.y; j <= hi.y; ++j) {
709  AMREX_PRAGMA_SIMD
710  for (int i = lo.x; i <= hi.x; ++i) {
711  // Zero at the domain top: Omega'' = 0 at a wall, and Omega''_top = Omega''_top-1
712  // for outflow, the matrix row set in make_fast_coeffs deciding which
713  RHS_a(i,j,hi.z+1) = (hi.z == domhi_z)
714  ? zero
715  : dtau * (slow_rhs_rho_w(i,j,hi.z+1) + zmom_src_arr(i,j,hi.z+1));
716  }
717  }
718  for (int k = lo.z+1; k <= hi.z+1; ++k) {
719  for (int j = lo.y; j <= hi.y; ++j) {
720  AMREX_PRAGMA_SIMD
721  for (int i = lo.x; i <= hi.x; ++i) {
722  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);
723  }
724  }
725  }
726  for (int k = hi.z; k >= lo.z; --k) {
727  for (int j = lo.y; j <= hi.y; ++j) {
728  AMREX_PRAGMA_SIMD
729  for (int i = lo.x; i <= hi.x; ++i) {
730  soln_a(i,j,k) -= ( coeffC_a(i,j,k) * inv_coeffB_a(i,j,k) ) * soln_a(i,j,k+1);
731  }
732  }
733  }
734 
735  // We assume that Omega == w at the top boundary and that changes in J there are irrelevant
736  for (int j = lo.y; j <= hi.y; ++j) {
737  AMREX_PRAGMA_SIMD
738  for (int i = lo.x; i <= hi.x; ++i) {
739  cur_zmom(i,j,hi.z+1) = stg_zmom(i,j,hi.z+1) + soln_a(i,j,hi.z+1);
740  }
741  }
742 #endif
743  } // end profile
744 
745  {
746  BL_PROFILE("substep_new_drhow");
747  tbz.setBig(2,hi.z);
748  ParallelFor(tbz, [=] AMREX_GPU_DEVICE (int i, int j, int k)
749  {
750  Real rho_on_face = myhalf * (cur_cons(i,j,k,Rho_comp) + cur_cons(i,j,k-1,Rho_comp));
751 
752  if (k == lo.z) {
753  cur_zmom(i,j,k) = WFromOmega(i,j,k,rho_on_face*(z_t_arr(i,j,k)+zp_t_arr(i,j,k)),
754  cur_xmom,cur_ymom,mf_ux,mf_vy,z_nd_new,dxInv);
755 
756  // We need to set this here because it is used to define zflux_lo below
757  soln_a(i,j,k) = zero;
758 
759  } else {
760 
761  // soln is the flux perturbation Omega_new - Omega_stg (see the assembly of the RHS), so
762  // Omega_new and the new horizontal momenta give W_new; the update of rho and (rho theta)
763  // below takes soln itself as the flux, as the solve did
764  Real Omega_stg = OmegaFromW(i,j,k,stg_zmom(i,j,k),stg_xmom,stg_ymom,mf_ux,mf_vy,z_nd_stg,dxInv);
765  Real Omega_new = soln_a(i,j,k) + Omega_stg;
766  cur_zmom(i,j,k) = WFromOmega(i,j,k,Omega_new,cur_xmom,cur_ymom,mf_ux,mf_vy,z_nd_new,dxInv);
767 
768  Real rho_stg_face = myhalf * (stg_cons(i,j,k,Rho_comp) + stg_cons(i,j,k-1,Rho_comp));
769  soln_a(i,j,k) -= rho_on_face * zp_t_arr(i,j,k) + (rho_on_face - rho_stg_face) * z_t_arr(i,j,k);
770  }
771 
772  if (l_rayleigh_impl_for_w && k > 0) {
773  Real damping_coeff = l_damp_coef * dtau * sinesq_stag_d[k];
774  cur_zmom(i,j,k) /= (one + damping_coeff);
775  }
776  });
777  } // end profile
778 
779  // **************************************************************************
780  // Define updates in the RHS of rho and (rho theta)
781  // **************************************************************************
782  {
783  BL_PROFILE("fast_rho_final_update");
784  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
785  {
786  Real zflux_lo = myhalf * (one + beta_s_a(i,j,k )) * soln_a(i,j,k )
787  + myhalf * (one - beta_s_a(i,j,k )) * omega_arr(i,j,k );
788  Real zflux_hi = myhalf * (one + beta_s_a(i,j,k+1)) * soln_a(i,j,k+1)
789  + myhalf * (one - beta_s_a(i,j,k+1)) * omega_arr(i,j,k+1);
790 
791  // Note that in the solve we effectively impose new_drho_w(i,j,vbx_hi.z+1)=0
792  // so we don't update avg_zmom at k=vbx_hi.z+1
793  avg_zmom_arr(i,j,k) += facinv*zflux_lo / (mf_mx(i,j,0) * mf_my(i,j,0));
794  if (l_reflux) {
795  (flx_arr[2])(i,j,k,0) = zflux_lo / (mf_mx(i,j,0) * mf_my(i,j,0));
796  }
797 
798  // Note that the factor of (1/J) in the fast source term is canceled
799  // when we multiply old and new by detJ_old and detJ_new , respectively
800  // We have already scaled the slow source term to have the extra factor of dJ
801  Real fast_rhs_rho = -(temp_rhs_arr(i,j,k,0) + ( zflux_hi - zflux_lo ) * dzi);
802  Real fast_rhs_rhotheta = -( temp_rhs_arr(i,j,k,1) + myhalf *
803  ( zflux_hi * (prim(i,j,k) + prim(i,j,k+1))
804  - zflux_lo * (prim(i,j,k) + prim(i,j,k-1)) ) * dzi );
805 
806  cur_cons(i,j,k,0) *= (detJ_old(i,j,k)/detJ_new(i,j,k));
807  cur_cons(i,j,k,1) *= (detJ_old(i,j,k)/detJ_new(i,j,k));
808 
809  const Real sw = detJ_stg(i,j,k) / detJ_new(i,j,k);
810  cur_cons(i,j,k,0) += dtau * ( sw*slow_rhs_cons(i,j,k,0) + fast_rhs_rho / detJ_new(i,j,k));
811  cur_cons(i,j,k,1) += dtau * ( sw*slow_rhs_cons(i,j,k,1) + fast_rhs_rhotheta / detJ_new(i,j,k));
812 
813  if (l_reflux) {
814  (flx_arr[2])(i,j,k,1) = (flx_arr[2])(i,j,k,0) * myhalf * (prim(i,j,k) + prim(i,j,k-1));
815  }
816 
817  if (k == vbx_hi.z) {
818  avg_zmom_arr(i,j,k+1) += facinv * zflux_hi / (mf_mx(i,j,0) * mf_my(i,j,0));
819  if (l_reflux) {
820  (flx_arr[2])(i,j,k+1,0) = zflux_hi / (mf_mx(i,j,0) * mf_my(i,j,0));
821  (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));
822  }
823  }
824 
825  // add in source terms for cell-centered conserved variables
826  cur_cons(i,j,k,Rho_comp) += dtau * cc_src_arr(i,j,k,Rho_comp);
827  cur_cons(i,j,k,RhoTheta_comp) += dtau * cc_src_arr(i,j,k,RhoTheta_comp);
828  });
829  } // end profile
830 
831  // We only add to the flux registers in the final RK step
832  if (l_reflux) {
833  int strt_comp_reflux = 0;
834  // rho and (rho theta); only (rho theta) is refluxed (see ERF::post_timestep), since the
835  // coarse/fine faces of a fine level carry the coarse mass flux (CFFaceFluxes)
836  int num_comp_reflux = 2;
837  if (level < finest_level) {
838  fr_as_crse->CrseAdd(mfi,
839  {{AMREX_D_DECL(&(flux[0]), &(flux[1]), &(flux[2]))}},
840  dx, dtau, strt_comp_reflux, strt_comp_reflux, num_comp_reflux, RunOn::Device);
841  }
842  if (level > 0) {
843  fr_as_fine->FineAdd(mfi,
844  {{AMREX_D_DECL(&(flux[0]), &(flux[1]), &(flux[2]))}},
845  dx, dtau, strt_comp_reflux, strt_comp_reflux, num_comp_reflux, RunOn::Device);
846  }
847 
848  // This is necessary here so we don't go on to the next FArrayBox without
849  // having finished copying the fluxes into the FluxRegisters (since the fluxes
850  // are stored in temporary FArrayBox's)
851  Gpu::streamSynchronize();
852 
853  } // two-way coupling
854 
855  } // mfi
856  } // OMP
857 }
GpuArray< Real, AMREX_SPACEDIM > dx
Definition: ERF_ActuatorSpreading.cpp:131
constexpr amrex::Real RvoRd
Definition: ERF_Constants.H:59
constexpr amrex::Real R_d
Definition: ERF_Constants.H:49
constexpr amrex::Real Gamma
Definition: ERF_Constants.H:70
@ v_x
Definition: ERF_DataStruct.H:40
@ u_y
Definition: ERF_DataStruct.H:41
@ v_y
Definition: ERF_DataStruct.H:41
@ m_y
Definition: ERF_DataStruct.H:41
@ u_x
Definition: ERF_DataStruct.H:40
@ m_x
Definition: ERF_DataStruct.H:40
#define PrimQ1_comp
Definition: ERF_IndexDefines.H:94
#define Rho_comp
Definition: ERF_IndexDefines.H:61
#define RhoTheta_comp
Definition: ERF_IndexDefines.H:62
#define PrimTheta_comp
Definition: ERF_IndexDefines.H:91
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")
const int khi
Definition: ERF_InitCustomPert_Bubble.H:25
const int klo
Definition: ERF_InitCustomPert_KE.H:9
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
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real Compute_h_eta_AtJface(const int &i, const int &j, const int &k, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &cellSizeInv, const amrex::Array4< const amrex::Real > &z_nd)
Definition: ERF_TerrainMetrics.H:397
AMREX_GPU_DEVICE AMREX_FORCE_INLINE amrex::Real WFromOmega(int &i, int &j, int &k, amrex::Real omega, const amrex::Array4< const amrex::Real > &u_arr, const amrex::Array4< const amrex::Real > &v_arr, const amrex::Array4< const amrex::Real > &mf_ux, const amrex::Array4< const amrex::Real > &mf_vy, const amrex::Array4< const amrex::Real > &z_nd, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &dxInv)
Definition: ERF_TerrainMetrics.H:867
AMREX_GPU_DEVICE AMREX_FORCE_INLINE amrex::Real Compute_h_zeta_AtIface(const int &i, const int &j, const int &k, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &cellSizeInv, const amrex::Array4< const amrex::Real > &z_nd)
Definition: ERF_TerrainMetrics.H:281
AMREX_GPU_DEVICE AMREX_FORCE_INLINE amrex::Real OmegaFromW(int &i, int &j, int &k, amrex::Real w, const amrex::Array4< const amrex::Real > &u_arr, const amrex::Array4< const amrex::Real > &v_arr, const amrex::Array4< const amrex::Real > &mf_ux, const amrex::Array4< const amrex::Real > &mf_vy, const amrex::Array4< const amrex::Real > &z_nd, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &dxInv)
Definition: ERF_TerrainMetrics.H:802
AMREX_GPU_DEVICE AMREX_FORCE_INLINE amrex::Real Compute_h_zeta_AtJface(const int &i, const int &j, const int &k, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &cellSizeInv, const amrex::Array4< const amrex::Real > &z_nd)
Definition: ERF_TerrainMetrics.H:351
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real Compute_h_xi_AtIface(const int &i, const int &j, const int &k, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &cellSizeInv, const amrex::Array4< const amrex::Real > &z_nd)
Definition: ERF_TerrainMetrics.H:304
@ gpy
Definition: ERF_IndexDefines.H:242
@ gpx
Definition: ERF_IndexDefines.H:241
@ ymom
Definition: ERF_IndexDefines.H:251
@ cons
Definition: ERF_IndexDefines.H:249
@ zmom
Definition: ERF_IndexDefines.H:252
@ xmom
Definition: ERF_IndexDefines.H:250
@ qt
Definition: ERF_Kessler.H:30
@ qv
Definition: ERF_Kessler.H:31
@ q
Definition: ERF_WSM6.H:296
Here is the call graph for this function: