ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_Substep_MT.cpp File Reference
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 &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 Real dtau, 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)
 

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 &  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 Real  dtau,
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 
)

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]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]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
[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]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?
85 {
86  BL_PROFILE_REGION("erf_substep_MT()");
87 
88  Real beta_1 = 0.5 * (1.0 - beta_s); // multiplies explicit terms
89  Real beta_2 = 0.5 * (1.0 + beta_s); // multiplies implicit terms
90 
91  // How much do we project forward the (rho theta) that is used in the horizontal momentum equations
92  Real beta_d = 0.1;
93 
94  Real RvOverRd = R_v / R_d;
95 
96  const Real* dx = geom.CellSize();
97  const GpuArray<Real, AMREX_SPACEDIM> dxInv = geom.InvCellSizeArray();
98 
99  Real dxi = dxInv[0];
100  Real dyi = dxInv[1];
101  Real dzi = dxInv[2];
102 
103  MultiFab coeff_A_mf(fast_coeffs, make_alias, 0, 1);
104  MultiFab inv_coeff_B_mf(fast_coeffs, make_alias, 1, 1);
105  MultiFab coeff_C_mf(fast_coeffs, make_alias, 2, 1);
106  MultiFab coeff_P_mf(fast_coeffs, make_alias, 3, 1);
107  MultiFab coeff_Q_mf(fast_coeffs, make_alias, 4, 1);
108 
109  // *************************************************************************
110  // Set gravity as a vector
111  const Array<Real,AMREX_SPACEDIM> grav{0.0, 0.0, -gravity};
112  const GpuArray<Real,AMREX_SPACEDIM> grav_gpu{grav[0], grav[1], grav[2]};
113 
114  MultiFab extrap(S_data[IntVars::cons].boxArray(),S_data[IntVars::cons].DistributionMap(),1,1);
115 
116  MultiFab Omega(S_data[IntVars::zmom].boxArray(), S_data[IntVars::zmom].DistributionMap(), 1, 1);
117 
118  // *************************************************************************
119  // Define updates in the current RK stg
120  // *************************************************************************
121 #ifdef _OPENMP
122 #pragma omp parallel if (Gpu::notInLaunchRegion())
123 #endif
124  {
125  FArrayBox temp_rhs_fab;
126 
127  FArrayBox RHS_fab;
128  FArrayBox soln_fab;
129 
130  std::array<FArrayBox,AMREX_SPACEDIM> flux;
131 
132  // NOTE: we leave tiling off here for efficiency -- to make this loop work with tiling
133  // will require additional changes
134  for ( MFIter mfi(S_stg_data[IntVars::cons],false); mfi.isValid(); ++mfi)
135  {
136  Box bx = mfi.tilebox();
137  Box tbx = surroundingNodes(bx,0);
138  Box tby = surroundingNodes(bx,1);
139  Box tbz = surroundingNodes(bx,2);
140 
141  Box vbx = mfi.validbox();
142  const auto& vbx_hi = ubound(vbx);
143 
144  const Array4<Real const>& xmom_src_arr = xmom_src.const_array(mfi);
145  const Array4<Real const>& ymom_src_arr = ymom_src.const_array(mfi);
146  const Array4<Real const>& zmom_src_arr = zmom_src.const_array(mfi);
147  const Array4<Real const>& cc_src_arr = cc_src.const_array(mfi);
148 
149  const Array4<const Real> & stg_cons = S_stg_data[IntVars::cons].const_array(mfi);
150  const Array4<const Real> & stg_xmom = S_stg_data[IntVars::xmom].const_array(mfi);
151  const Array4<const Real> & stg_ymom = S_stg_data[IntVars::ymom].const_array(mfi);
152  const Array4<const Real> & stg_zmom = S_stg_data[IntVars::zmom].const_array(mfi);
153  const Array4<const Real> & prim = S_stg_prim.const_array(mfi);
154  const Array4<const Real> & qt_arr = qt.const_array(mfi);
155 
156  const Array4<const Real>& slow_rhs_cons = S_slow_rhs[IntVars::cons].const_array(mfi);
157  const Array4<const Real>& slow_rhs_rho_u = S_slow_rhs[IntVars::xmom].const_array(mfi);
158  const Array4<const Real>& slow_rhs_rho_v = S_slow_rhs[IntVars::ymom].const_array(mfi);
159  const Array4<const Real>& slow_rhs_rho_w = S_slow_rhs[IntVars::zmom].const_array(mfi);
160 
161  const Array4<Real>& cur_cons = S_data[IntVars::cons].array(mfi);
162  const Array4<Real>& cur_xmom = S_data[IntVars::xmom].array(mfi);
163  const Array4<Real>& cur_ymom = S_data[IntVars::ymom].array(mfi);
164  const Array4<Real>& cur_zmom = S_data[IntVars::zmom].array(mfi);
165 
166  const Array4<Real>& lagged = lagged_delta_rt.array(mfi);
167 
168  const Array4<const Real>& prev_cons = S_prev[IntVars::cons].const_array(mfi);
169  const Array4<const Real>& prev_xmom = S_prev[IntVars::xmom].const_array(mfi);
170  const Array4<const Real>& prev_ymom = S_prev[IntVars::ymom].const_array(mfi);
171  const Array4<const Real>& prev_zmom = S_prev[IntVars::zmom].const_array(mfi);
172 
173  // These store the advection momenta which we will use to update the slow variables
174  const Array4<Real>& avg_xmom_arr = avg_xmom.array(mfi);
175  const Array4<Real>& avg_ymom_arr = avg_ymom.array(mfi);
176  const Array4<Real>& avg_zmom_arr = avg_zmom.array(mfi);
177 
178  const Array4<const Real>& z_nd_old = z_phys_nd_old->const_array(mfi);
179  const Array4<const Real>& z_nd_new = z_phys_nd_new->const_array(mfi);
180  const Array4<const Real>& z_nd_stg = z_phys_nd_stg->const_array(mfi);
181  const Array4<const Real>& detJ_old = detJ_cc_old->const_array(mfi);
182  const Array4<const Real>& detJ_new = detJ_cc_new->const_array(mfi);
183  const Array4<const Real>& detJ_stg = detJ_cc_stg->const_array(mfi);
184 
185  const Array4<const Real>& z_t_arr = z_t_rk->const_array(mfi);
186  const Array4<const Real>& zp_t_arr = z_t_pert->const_array(mfi);
187 
188  const Array4< Real>& omega_arr = Omega.array(mfi);
189 
190  const Array4<const Real>& pi_stage_ca = pi_stage.const_array(mfi);
191 
192  const Array4<Real>& theta_extrap = extrap.array(mfi);
193 
194  // Map factors
195  const Array4<const Real>& mf_mx = mapfac[MapFacType::m_x]->const_array(mfi);
196  const Array4<const Real>& mf_my = mapfac[MapFacType::m_y]->const_array(mfi);
197  const Array4<const Real>& mf_ux = mapfac[MapFacType::u_x]->const_array(mfi);
198  const Array4<const Real>& mf_vy = mapfac[MapFacType::v_y]->const_array(mfi);
199 
200  // Note: it is important to grow the tilebox rather than use growntilebox because
201  // we need to fill the ghost cells of the tilebox so we can use them below
202  Box gbx = mfi.tilebox(); gbx.grow(1);
203  Box gtbx = mfi.nodaltilebox(0); gtbx.grow(1); gtbx.setSmall(2,0);
204  Box gtby = mfi.nodaltilebox(1); gtby.grow(1); gtby.setSmall(2,0);
205 
206  if (step == 0) {
207  ParallelFor(gbx,
208  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
209  cur_cons(i,j,k,Rho_comp) = prev_cons(i,j,k,Rho_comp);
210  cur_cons(i,j,k,RhoTheta_comp) = prev_cons(i,j,k,RhoTheta_comp);
211 
212  Real delta_rt = cur_cons(i,j,k,RhoTheta_comp) - stg_cons(i,j,k,RhoTheta_comp);
213  theta_extrap(i,j,k) = delta_rt;
214 
215  // NOTE: qv is not changing over the fast steps so we use the stage data
216  Real qv = (l_use_moisture) ? prim(i,j,k,PrimQ1_comp) : 0.0;
217  theta_extrap(i,j,k) *= (1.0 + RvOverRd*qv);
218 
219  // We define lagged_delta_rt for our next step as the current delta_rt
220  lagged(i,j,k) = delta_rt;
221  });
222  } else if (use_lagged_delta_rt) {
223  // This is the default for cases with no or static terrain
224  ParallelFor(gbx,
225  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
226  Real delta_rt = cur_cons(i,j,k,RhoTheta_comp) - stg_cons(i,j,k,RhoTheta_comp);
227  theta_extrap(i,j,k) = delta_rt + beta_d * (delta_rt - lagged(i,j,k));
228 
229  // NOTE: qv is not changing over the fast steps so we use the stage data
230  Real qv = (l_use_moisture) ? prim(i,j,k,PrimQ1_comp) : 0.0;
231  theta_extrap(i,j,k) *= (1.0 + RvOverRd*qv);
232 
233  // We define lagged_delta_rt for our next step as the current delta_rt
234  lagged(i,j,k) = delta_rt;
235  });
236  } else {
237  // For the moving wave problem, this choice seems more robust
238  ParallelFor(gbx,
239  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
240  theta_extrap(i,j,k) = cur_cons(i,j,k,RhoTheta_comp) - stg_cons(i,j,k,RhoTheta_comp);
241 
242  // NOTE: qv is not changing over the fast steps so we use the stage data
243  Real qv = (l_use_moisture) ? prim(i,j,k,PrimQ1_comp) : 0.0;
244  theta_extrap(i,j,k) *= (1.0 + RvOverRd*qv);
245  });
246  } // if step
247 
248  RHS_fab.resize (tbz,1, The_Async_Arena());
249  soln_fab.resize (tbz,1, The_Async_Arena());
250  temp_rhs_fab.resize(tbz,2, The_Async_Arena());
251 
252  auto const& RHS_a = RHS_fab.array();
253  auto const& soln_a = soln_fab.array();
254  auto const& temp_rhs_arr = temp_rhs_fab.array();
255 
256  auto const& coeffA_a = coeff_A_mf.array(mfi);
257  auto const& inv_coeffB_a = inv_coeff_B_mf.array(mfi);
258  auto const& coeffC_a = coeff_C_mf.array(mfi);
259  auto const& coeffP_a = coeff_P_mf.array(mfi);
260  auto const& coeffQ_a = coeff_Q_mf.array(mfi);
261 
262  // *********************************************************************
263  // Define updates in the RHS of {x, y, z}-momentum equations
264  // *********************************************************************
265  {
266  BL_PROFILE("substep_xymom_T");
267  ParallelFor(tbx, tby,
268  [=] AMREX_GPU_DEVICE (int i, int j, int k)
269  {
270  // Add (negative) gradient of (rho theta) multiplied by lagged "pi"
271  Real h_xi_old = Compute_h_xi_AtIface(i, j, k, dxInv, z_nd_old);
272  Real h_zeta_old = Compute_h_zeta_AtIface(i, j, k, dxInv, z_nd_old);
273  Real gp_xi = (theta_extrap(i,j,k) - theta_extrap(i-1,j,k)) * dxi;
274  Real gp_zeta_on_iface = (k == 0) ?
275  0.5 * dzi * ( theta_extrap(i-1,j,k+1) + theta_extrap(i,j,k+1)
276  -theta_extrap(i-1,j,k ) - theta_extrap(i,j,k ) ) :
277  0.25 * dzi * ( theta_extrap(i-1,j,k+1) + theta_extrap(i,j,k+1)
278  -theta_extrap(i-1,j,k-1) - theta_extrap(i,j,k-1) );
279  Real gpx = h_zeta_old * gp_xi - h_xi_old * gp_zeta_on_iface;
280  gpx *= mf_ux(i,j,0);
281 
282  Real q = (l_use_moisture) ? 0.5 * (qt_arr(i-1,j,k) + qt_arr(i,j,k)) : 0.0;
283 
284  Real pi_c = 0.5 * (pi_stage_ca(i-1,j,k,0) + pi_stage_ca(i ,j,k,0));
285  Real fast_rhs_rho_u = -Gamma * R_d * pi_c * gpx / (1.0 + q);
286 
287  // We have already scaled the source terms to have the extra factor of dJ
288  cur_xmom(i,j,k) = h_zeta_old * prev_xmom(i,j,k) + dtau * fast_rhs_rho_u
289  + dtau * slow_rhs_rho_u(i,j,k)
290  + dtau * xmom_src_arr(i,j,k);
291  },
292  [=] AMREX_GPU_DEVICE (int i, int j, int k)
293  {
294  // Add (negative) gradient of (rho theta) multiplied by lagged "pi"
295  Real h_eta_old = Compute_h_eta_AtJface(i, j, k, dxInv, z_nd_old);
296  Real h_zeta_old = Compute_h_zeta_AtJface(i, j, k, dxInv, z_nd_old);
297  Real gp_eta = (theta_extrap(i,j,k) -theta_extrap(i,j-1,k)) * dyi;
298  Real gp_zeta_on_jface = (k == 0) ?
299  0.5 * dzi * ( theta_extrap(i,j,k+1) + theta_extrap(i,j-1,k+1)
300  -theta_extrap(i,j,k ) - theta_extrap(i,j-1,k ) ) :
301  0.25 * dzi * ( theta_extrap(i,j,k+1) + theta_extrap(i,j-1,k+1)
302  -theta_extrap(i,j,k-1) - theta_extrap(i,j-1,k-1) );
303  Real gpy = h_zeta_old * gp_eta - h_eta_old * gp_zeta_on_jface;
304  gpy *= mf_vy(i,j,0);
305 
306  Real q = (l_use_moisture) ? 0.5 * (qt_arr(i,j-1,k) + qt_arr(i,j,k)) : 0.0;
307 
308  Real pi_c = 0.5 * (pi_stage_ca(i,j-1,k,0) + pi_stage_ca(i,j ,k,0));
309  Real fast_rhs_rho_v = -Gamma * R_d * pi_c * gpy / (1.0 + q);
310 
311  // We have already scaled the source terms to have the extra factor of dJ
312  cur_ymom(i, j, k) = h_zeta_old * prev_ymom(i,j,k) + dtau * fast_rhs_rho_v
313  + dtau * slow_rhs_rho_v(i,j,k)
314  + dtau * ymom_src_arr(i,j,k);
315  });
316  } // end profile
317 
318  // *************************************************************************
319  // Define flux arrays for use in advection
320  // *************************************************************************
321  for (int dir = 0; dir < AMREX_SPACEDIM; ++dir) {
322  flux[dir].resize(surroundingNodes(bx,dir),2,The_Async_Arena());
323  flux[dir].setVal<RunOn::Device>(0.);
324  }
325  const GpuArray<const Array4<Real>, AMREX_SPACEDIM>
326  flx_arr{{AMREX_D_DECL(flux[0].array(), flux[1].array(), flux[2].array())}};
327 
328  // *********************************************************************
329  {
330  BL_PROFILE("fast_T_making_rho_rhs");
331  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
332  {
333  Real h_zeta_stg_xlo = Compute_h_zeta_AtIface(i, j , k, dxInv, z_nd_stg);
334  Real h_zeta_stg_xhi = Compute_h_zeta_AtIface(i+1,j , k, dxInv, z_nd_stg);
335  Real xflux_lo = cur_xmom(i ,j,k) - stg_xmom(i ,j,k)*h_zeta_stg_xlo;
336  Real xflux_hi = cur_xmom(i+1,j,k) - stg_xmom(i+1,j,k)*h_zeta_stg_xhi;
337 
338  Real h_zeta_stg_yhi = Compute_h_zeta_AtJface(i, j+1, k, dxInv, z_nd_stg);
339  Real h_zeta_stg_ylo = Compute_h_zeta_AtJface(i, j , k, dxInv, z_nd_stg);
340  Real yflux_lo = cur_ymom(i,j ,k) - stg_ymom(i,j ,k)*h_zeta_stg_ylo;
341  Real yflux_hi = cur_ymom(i,j+1,k) - stg_ymom(i,j+1,k)*h_zeta_stg_yhi;
342 
343  // NOTE: we are saving the (1/J) weighting for later when we add this to rho and theta
344  temp_rhs_arr(i,j,k,0) = ( ( xflux_hi - xflux_lo ) * dxi + ( yflux_hi - yflux_lo ) * dyi );
345  temp_rhs_arr(i,j,k,1) = ( (( xflux_hi * (prim(i,j,k,0) + prim(i+1,j,k,0)) -
346  xflux_lo * (prim(i,j,k,0) + prim(i-1,j,k,0)) ) * dxi +
347  ( yflux_hi * (prim(i,j,k,0) + prim(i,j+1,k,0)) -
348  yflux_lo * (prim(i,j,k,0) + prim(i,j-1,k,0)) ) * dyi) * 0.5 );
349 
350  if (l_reflux) {
351  (flx_arr[0])(i,j,k,0) = xflux_lo;
352  (flx_arr[0])(i,j,k,1) = (flx_arr[0])(i ,j,k,0) * 0.5 * (prim(i,j,k,0) + prim(i-1,j,k,0));
353 
354  (flx_arr[1])(i,j,k,0) = yflux_lo;
355  (flx_arr[1])(i,j,k,1) = (flx_arr[1])(i,j ,k,0) * 0.5 * (prim(i,j,k,0) + prim(i,j-1,k,0));
356 
357  if (i == vbx_hi.x) {
358  (flx_arr[0])(i+1,j,k,0) = xflux_hi;
359  (flx_arr[0])(i+1,j,k,1) = (flx_arr[0])(i+1,j,k,0) * 0.5 * (prim(i,j,k,0) + prim(i+1,j,k,0));
360  }
361  if (j == vbx_hi.y) {
362  (flx_arr[1])(i,j+1,k,0) = yflux_hi;
363  (flx_arr[1])(i,j+1,k,1) = (flx_arr[1])(i,j+1,k,0) * 0.5 * (prim(i,j,k,0) + prim(i,j+1,k,0));
364  }
365  }
366  });
367  } // end profile
368 
369  // *********************************************************************
370  // This must be done before we set cur_xmom and cur_ymom, since those
371  // in fact point to the same array as prev_xmom and prev_ymom
372  // *********************************************************************
373  Box gbxo = mfi.nodaltilebox(2);
374  {
375  BL_PROFILE("fast_MT_making_omega");
376  Box gbxo_lo = gbxo; gbxo_lo.setBig(2,0);
377  ParallelFor(gbxo_lo, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
378  omega_arr(i,j,k) = 0.;
379  });
380  Box gbxo_hi = gbxo; gbxo_hi.setSmall(2,gbxo.bigEnd(2));
381  ParallelFor(gbxo_hi, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
382  omega_arr(i,j,k) = prev_zmom(i,j,k) - stg_zmom(i,j,k) - zp_t_arr(i,j,k);
383  });
384  Box gbxo_mid = gbxo; gbxo_mid.setSmall(2,1); gbxo_mid.setBig(2,gbxo.bigEnd(2)-1);
385  ParallelFor(gbxo_mid, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
386  omega_arr(i,j,k) =
387  ( OmegaFromW(i,j,k,prev_zmom(i,j,k),prev_xmom,prev_ymom,mf_ux,mf_vy,z_nd_old,dxInv)
388  -OmegaFromW(i,j,k, stg_zmom(i,j,k), stg_xmom, stg_ymom,mf_ux,mf_vy,z_nd_old,dxInv) )
389  - zp_t_arr(i,j,k);
390  });
391  } // end profile
392  // *********************************************************************
393 
394  ParallelFor(tbx, tby,
395  [=] AMREX_GPU_DEVICE (int i, int j, int k)
396  {
397  Real h_zeta_new = Compute_h_zeta_AtIface(i, j, k, dxInv, z_nd_new);
398  cur_xmom(i, j, k) /= h_zeta_new;
399  avg_xmom_arr(i,j,k) += facinv*(cur_xmom(i,j,k) - stg_xmom(i,j,k));
400  },
401  [=] AMREX_GPU_DEVICE (int i, int j, int k)
402  {
403  Real h_zeta_new = Compute_h_zeta_AtJface(i, j, k, dxInv, z_nd_new);
404  cur_ymom(i, j, k) /= h_zeta_new;
405  avg_ymom_arr(i,j,k) += facinv*(cur_ymom(i,j,k) - stg_ymom(i,j,k));
406  });
407 
408  Box bx_shrunk_in_k = bx;
409  int klo = tbz.smallEnd(2);
410  int khi = tbz.bigEnd(2);
411  bx_shrunk_in_k.setSmall(2,klo+1);
412  bx_shrunk_in_k.setBig(2,khi-1);
413 
414  // Note that the notes use "g" to mean the magnitude of gravity, so it is positive
415  // We set grav_gpu[2] to be the vector component which is negative
416  // We define halfg to match the notes (which is why we take the absolute value)
417  Real halfg = std::abs(0.5 * grav_gpu[2]);
418 
419  {
420  BL_PROFILE("fast_loop_on_shrunk_t");
421  //Note we don't act on the bottom or top boundaries of the domain
422  ParallelFor(bx_shrunk_in_k, [=] AMREX_GPU_DEVICE (int i, int j, int k)
423  {
424  Real q = (l_use_moisture) ? 0.5 * (qt_arr(i,j,k-1) + qt_arr(i,j,k)) : 0.0;
425 
426  Real coeff_P = coeffP_a(i,j,k) / (1.0 + q);
427  Real coeff_Q = coeffQ_a(i,j,k) / (1.0 + q);
428 
429  Real theta_t_lo = 0.5 * ( prim(i,j,k-2,PrimTheta_comp) + prim(i,j,k-1,PrimTheta_comp) );
430  Real theta_t_mid = 0.5 * ( prim(i,j,k-1,PrimTheta_comp) + prim(i,j,k ,PrimTheta_comp) );
431  Real theta_t_hi = 0.5 * ( prim(i,j,k ,PrimTheta_comp) + prim(i,j,k+1,PrimTheta_comp) );
432 
433  // line 2 last two terms (order dtau)
434  Real R0_tmp = coeff_P * cur_cons(i,j,k ,RhoTheta_comp)
435  + coeff_Q * cur_cons(i,j,k-1,RhoTheta_comp)
436  - coeff_P * stg_cons(i,j,k ,RhoTheta_comp)
437  - coeff_Q * stg_cons(i,j,k-1,RhoTheta_comp)
438  - halfg * ( cur_cons(i,j,k,Rho_comp) + cur_cons(i,j,k-1,Rho_comp) )
439  + halfg * ( stg_cons(i,j,k,Rho_comp) + stg_cons(i,j,k-1,Rho_comp) );
440 
441  // line 3 residuals (order dtau^2) 1.0 <-> beta_2
442  Real R1_tmp = - halfg * ( slow_rhs_cons(i,j,k ,Rho_comp)
443  + slow_rhs_cons(i,j,k-1,Rho_comp) )
444  + coeff_P * slow_rhs_cons(i,j,k ,RhoTheta_comp)
445  + coeff_Q * slow_rhs_cons(i,j,k-1,RhoTheta_comp);
446 
447  Real Omega_kp1 = omega_arr(i,j,k+1);
448  Real Omega_k = omega_arr(i,j,k );
449  Real Omega_km1 = omega_arr(i,j,k-1);
450 
451  Real detJdiff = (detJ_old(i,j,k) - detJ_old(i,j,k-1)) / (detJ_old(i,j,k)*detJ_old(i,j,k-1));
452 
453  // consolidate lines 4&5 (order dtau^2)
454  R1_tmp += halfg * ( beta_1 * dzi * (Omega_kp1/detJ_old(i,j,k) + detJdiff*Omega_k - Omega_km1/detJ_old(i,j,k-1))
455  + temp_rhs_arr(i,j,k,Rho_comp)/detJ_old(i,j,k) + temp_rhs_arr(i,j,k-1,Rho_comp)/detJ_old(i,j,k-1) );
456 
457  // consolidate lines 6&7 (order dtau^2)
458  R1_tmp += -( coeff_P/detJ_stg(i,j,k ) * ( beta_1 * dzi * (Omega_kp1*theta_t_hi - Omega_k*theta_t_mid) +
459  temp_rhs_arr(i,j,k ,RhoTheta_comp) )
460  + coeff_Q/detJ_stg(i,j,k-1) * ( beta_1 * dzi * (Omega_k*theta_t_mid - Omega_km1*theta_t_lo) +
461  temp_rhs_arr(i,j,k-1,RhoTheta_comp) ) );
462 
463  // line 1
464  Real detJ_half = 0.5 * (detJ_stg(i,j,k) + detJ_stg(i,j,k-1)); // TODO: THIS MAY NOT BE RIGHT
465  RHS_a(i,j,k) = prev_zmom(i,j,k) - stg_zmom(i,j,k)
466  + dtau * (slow_rhs_rho_w(i,j,k) + zmom_src_arr(i,j,k)) / detJ_half
467  + dtau * (R0_tmp + dtau*beta_2*R1_tmp);
468 
469  // We cannot use omega_arr here since that was built with old_rho_u and old_rho_v ...
470  Real UppVpp = OmegaFromW(i,j,k,0.,cur_xmom,cur_ymom,mf_ux,mf_vy,z_nd_new,dxInv)
471  - OmegaFromW(i,j,k,0.,stg_xmom,stg_ymom,mf_ux,mf_vy,z_nd_stg,dxInv);
472  RHS_a(i,j,k) += UppVpp;
473  });
474  } // end profile
475 
476  Box b2d = tbz; // Copy constructor
477  b2d.setRange(2,0);
478 
479  auto const lo = lbound(bx);
480  auto const hi = ubound(bx);
481 
482  {
483  BL_PROFILE("substep_b2d_loop_t");
484 
485 #ifdef AMREX_USE_GPU
486  ParallelFor(b2d, [=] AMREX_GPU_DEVICE (int i, int j, int)
487  {
488  // Moving terrain
489  Real rho_on_bdy = 0.5 * ( prev_cons(i,j,lo.z) + prev_cons(i,j,lo.z-1) );
490  RHS_a(i,j,lo.z) = rho_on_bdy * zp_t_arr(i,j,0);
491 
492  soln_a(i,j,lo.z) = RHS_a(i,j,lo.z) * inv_coeffB_a(i,j,lo.z);
493 
494  // w_khi = 0
495  RHS_a(i,j,hi.z+1) = 0.0;
496 
497  for (int k = lo.z+1; k <= hi.z+1; k++) {
498  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);
499  }
500 
501  for (int k = hi.z; k >= lo.z; k--) {
502  soln_a(i,j,k) -= ( coeffC_a(i,j,k) * inv_coeffB_a(i,j,k) ) * soln_a(i,j,k+1);
503  }
504 
505  // We assume that Omega == w at the top boundary and that changes in J there are irrelevant
506  cur_zmom(i,j,hi.z+1) = stg_zmom(i,j,hi.z+1) + soln_a(i,j,hi.z+1);
507  });
508 #else
509  for (int j = lo.y; j <= hi.y; ++j) {
510  AMREX_PRAGMA_SIMD
511  for (int i = lo.x; i <= hi.x; ++i) {
512 
513  Real rho_on_bdy = 0.5 * ( prev_cons(i,j,lo.z) + prev_cons(i,j,lo.z-1) );
514  RHS_a(i,j,lo.z) = rho_on_bdy * zp_t_arr(i,j,lo.z);
515 
516  soln_a(i,j,lo.z) = RHS_a(i,j,lo.z) * inv_coeffB_a(i,j,lo.z);
517  }
518  }
519 
520  for (int j = lo.y; j <= hi.y; ++j) {
521  AMREX_PRAGMA_SIMD
522  for (int i = lo.x; i <= hi.x; ++i) {
523  RHS_a (i,j,hi.z+1) = 0.0;
524  }
525  }
526  for (int k = lo.z+1; k <= hi.z+1; ++k) {
527  for (int j = lo.y; j <= hi.y; ++j) {
528  AMREX_PRAGMA_SIMD
529  for (int i = lo.x; i <= hi.x; ++i) {
530  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);
531  }
532  }
533  }
534  for (int k = hi.z; k >= lo.z; --k) {
535  for (int j = lo.y; j <= hi.y; ++j) {
536  AMREX_PRAGMA_SIMD
537  for (int i = lo.x; i <= hi.x; ++i) {
538  soln_a(i,j,k) -= ( coeffC_a(i,j,k) * inv_coeffB_a(i,j,k) ) * soln_a(i,j,k+1);
539  }
540  }
541  }
542 
543  // We assume that Omega == w at the top boundary and that changes in J there are irrelevant
544  for (int j = lo.y; j <= hi.y; ++j) {
545  AMREX_PRAGMA_SIMD
546  for (int i = lo.x; i <= hi.x; ++i) {
547  cur_zmom(i,j,hi.z+1) = stg_zmom(i,j,hi.z+1) + soln_a(i,j,hi.z+1);
548  }
549  }
550 #endif
551  } // end profile
552 
553  {
554  BL_PROFILE("substep_new_drhow");
555  tbz.setBig(2,hi.z);
556  ParallelFor(tbz, [=] AMREX_GPU_DEVICE (int i, int j, int k)
557  {
558  Real rho_on_face = 0.5 * (cur_cons(i,j,k,Rho_comp) + cur_cons(i,j,k-1,Rho_comp));
559 
560  if (k == lo.z) {
561  cur_zmom(i,j,k) = WFromOmega(i,j,k,rho_on_face*(z_t_arr(i,j,k)+zp_t_arr(i,j,k)),
562  cur_xmom,cur_ymom,mf_ux,mf_vy,z_nd_new,dxInv);
563 
564  // We need to set this here because it is used to define zflux_lo below
565  soln_a(i,j,k) = 0.;
566 
567  } else {
568 
569  Real UppVpp = WFromOmega(i,j,k,0.0,cur_xmom,cur_ymom,mf_ux,mf_vy,z_nd_new,dxInv)
570  - WFromOmega(i,j,k,0.0,stg_xmom,stg_ymom,mf_ux,mf_vy,z_nd_stg,dxInv);
571  Real wpp = soln_a(i,j,k) + UppVpp;
572  Real dJ_old_kface = 0.5 * (detJ_old(i,j,k) + detJ_old(i,j,k-1));
573  Real dJ_new_kface = 0.5 * (detJ_new(i,j,k) + detJ_new(i,j,k-1));
574 
575  cur_zmom(i,j,k) = dJ_old_kface * (stg_zmom(i,j,k) + wpp);
576  cur_zmom(i,j,k) /= dJ_new_kface;
577 
578  soln_a(i,j,k) = OmegaFromW(i,j,k,cur_zmom(i,j,k),cur_xmom,cur_ymom,mf_ux,mf_vy,z_nd_new,dxInv)
579  - OmegaFromW(i,j,k,stg_zmom(i,j,k),stg_xmom,stg_ymom,mf_ux,mf_vy,z_nd_stg,dxInv);
580  soln_a(i,j,k) -= rho_on_face * zp_t_arr(i,j,k);
581  }
582  });
583  } // end profile
584 
585  // **************************************************************************
586  // Define updates in the RHS of rho and (rho theta)
587  // **************************************************************************
588  {
589  BL_PROFILE("fast_rho_final_update");
590  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
591  {
592  Real zflux_lo = beta_2 * soln_a(i,j,k ) + beta_1 * omega_arr(i,j,k);
593  Real zflux_hi = beta_2 * soln_a(i,j,k+1) + beta_1 * omega_arr(i,j,k+1);
594 
595  // Note that in the solve we effectively impose new_drho_w(i,j,vbx_hi.z+1)=0
596  // so we don't update avg_zmom at k=vbx_hi.z+1
597  avg_zmom_arr(i,j,k) += facinv*zflux_lo / (mf_mx(i,j,0) * mf_my(i,j,0));
598  if (l_reflux) {
599  (flx_arr[2])(i,j,k,0) = zflux_lo / (mf_mx(i,j,0) * mf_my(i,j,0));
600  }
601 
602  // Note that the factor of (1/J) in the fast source term is canceled
603  // when we multiply old and new by detJ_old and detJ_new , respectively
604  // We have already scaled the slow source term to have the extra factor of dJ
605  Real fast_rhs_rho = -(temp_rhs_arr(i,j,k,0) + ( zflux_hi - zflux_lo ) * dzi);
606  Real temp_rho = detJ_old(i,j,k) * cur_cons(i,j,k,0) +
607  dtau * ( slow_rhs_cons(i,j,k,0)*detJ_stg(i,j,k) + fast_rhs_rho );
608  cur_cons(i,j,k,0) = temp_rho / detJ_new(i,j,k);
609 
610  // Note that the factor of (1/J) in the fast source term is canceled
611  // when we multiply old and new by detJ_old and detJ_new , respectively
612  // We have already scaled the slow source term to have the extra factor of dJ
613  Real fast_rhs_rhotheta = -( temp_rhs_arr(i,j,k,1) + 0.5 *
614  ( zflux_hi * (prim(i,j,k) + prim(i,j,k+1))
615  - zflux_lo * (prim(i,j,k) + prim(i,j,k-1)) ) * dzi );
616  Real temp_rth = detJ_old(i,j,k) * cur_cons(i,j,k,1) +
617  dtau * ( slow_rhs_cons(i,j,k,1)*detJ_stg(i,j,k) + fast_rhs_rhotheta );
618  cur_cons(i,j,k,1) = temp_rth / detJ_new(i,j,k);
619  if (l_reflux) {
620  (flx_arr[2])(i,j,k,1) = (flx_arr[2])(i,j,k,0) * 0.5 * (prim(i,j,k) + prim(i,j,k-1));
621  }
622 
623  if (k == vbx_hi.z) {
624  avg_zmom_arr(i,j,k+1) += facinv * zflux_hi / (mf_mx(i,j,0) * mf_my(i,j,0));
625  if (l_reflux) {
626  (flx_arr[2])(i,j,k+1,0) = zflux_hi / (mf_mx(i,j,0) * mf_my(i,j,0));
627  (flx_arr[2])(i,j,k+1,1) = (flx_arr[2])(i,j,k+1,0) * 0.5 * (prim(i,j,k) + prim(i,j,k+1));
628  }
629  }
630 
631  // add in source terms for cell-centered conserved variables
632  cur_cons(i,j,k,Rho_comp) += dtau * cc_src_arr(i,j,k,Rho_comp);
633  cur_cons(i,j,k,RhoTheta_comp) += dtau * cc_src_arr(i,j,k,RhoTheta_comp);
634  });
635  } // end profile
636 
637  // We only add to the flux registers in the final RK step
638  if (l_reflux) {
639  int strt_comp_reflux = 0;
640  // For now we don't reflux (rho theta) because it seems to create issues at c/f boundaries
641  int num_comp_reflux = 1;
642  if (level < finest_level) {
643  fr_as_crse->CrseAdd(mfi,
644  {{AMREX_D_DECL(&(flux[0]), &(flux[1]), &(flux[2]))}},
645  dx, dtau, strt_comp_reflux, strt_comp_reflux, num_comp_reflux, RunOn::Device);
646  }
647  if (level > 0) {
648  fr_as_fine->FineAdd(mfi,
649  {{AMREX_D_DECL(&(flux[0]), &(flux[1]), &(flux[2]))}},
650  dx, dtau, strt_comp_reflux, strt_comp_reflux, num_comp_reflux, RunOn::Device);
651  }
652 
653  // This is necessary here so we don't go on to the next FArrayBox without
654  // having finished copying the fluxes into the FluxRegisters (since the fluxes
655  // are stored in temporary FArrayBox's)
656  Gpu::streamSynchronize();
657 
658  } // two-way coupling
659 
660  } // mfi
661  } // OMP
662 }
constexpr amrex::Real R_v
Definition: ERF_Constants.H:11
constexpr amrex::Real R_d
Definition: ERF_Constants.H:10
constexpr amrex::Real Gamma
Definition: ERF_Constants.H:19
@ v_y
Definition: ERF_DataStruct.H:24
@ m_y
Definition: ERF_DataStruct.H:24
@ u_x
Definition: ERF_DataStruct.H:23
@ m_x
Definition: ERF_DataStruct.H:23
#define PrimQ1_comp
Definition: ERF_IndexDefines.H:53
#define Rho_comp
Definition: ERF_IndexDefines.H:36
#define RhoTheta_comp
Definition: ERF_IndexDefines.H:37
#define PrimTheta_comp
Definition: ERF_IndexDefines.H:50
amrex::Real Real
Definition: ERF_ShocInterface.H:19
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_u, const amrex::Array4< const amrex::Real > &mf_v, const amrex::Array4< const amrex::Real > &z_nd, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &dxInv)
Definition: ERF_TerrainMetrics.H:412
AMREX_GPU_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:115
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:102
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:142
AMREX_GPU_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:168
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_u, const amrex::Array4< const amrex::Real > &mf_v, const amrex::Array4< const amrex::Real > &z_nd, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &dxInv)
Definition: ERF_TerrainMetrics.H:462
@ gpy
Definition: ERF_IndexDefines.H:151
@ gpx
Definition: ERF_IndexDefines.H:150
@ ymom
Definition: ERF_IndexDefines.H:160
@ cons
Definition: ERF_IndexDefines.H:158
@ zmom
Definition: ERF_IndexDefines.H:161
@ xmom
Definition: ERF_IndexDefines.H:159
@ qt
Definition: ERF_Kessler.H:27
@ qv
Definition: ERF_Kessler.H:28
Here is the call graph for this function: