ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_TI_no_substep_fun.H
Go to the documentation of this file.
1 /**
2  * Wrapper for advancing the solution with the slow RHS in the absence of acoustic substepping
3  */
4  auto no_substep_fun = [&,bogus_large_value_d=bogus_large_value](Vector<MultiFab>& S_sum,
5  Vector<MultiFab>& S_old,
6  Vector<MultiFab>& F_slow,
7  const double time_for_fp, const double slow_dt_d,
8  const int nrk)
9  {
10  BL_PROFILE("no_substep_fun");
11  amrex::ignore_unused(nrk);
12  int n_data = IntVars::NumTypes;
13 
14  amrex::Real slow_dt = static_cast<Real>(slow_dt_d);
15 
16  const auto& dxInv = fine_geom.InvCellSizeArray();
17 
18  const amrex::GpuArray<int, IntVars::NumTypes> scomp_fast = {0,0,0,0};
19  const amrex::GpuArray<int, IntVars::NumTypes> ncomp_fast = {2,1,1,1};
20 
21  if (verbose) amrex::Print() << " No-substepping time integration at level " << level
22  << std::setprecision(timeprecision)
23  << " to " << time_for_fp
24  << " with dt = " << slow_dt << std::endl;
25 
26  // Update S_sum = S_stage only for the fast variables
27 #ifdef _OPENMP
28 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
29 #endif
30  {
31  //
32  // NOTE: we must not tile if the terrain is moving because the update of zmom at
33  // k = 0 below calls WFromOmega, which reads the x- and y-momenta at
34  // (i+1,j) and (i,j+1). Those faces are written on nodaltilebox(0) and
35  // nodaltilebox(1), which exclude a tile's upper faces, so with tiling
36  // they would still hold the values from the previous stage -- and, under
37  // OpenMP, would be written by another thread at the same time.
38  // (The substepping version in ERF_Substep_MT.cpp leaves tiling off for
39  // this same reason.)
40  //
41  const bool do_tiling = (solverChoice.terrain_type == TerrainType::MovingFittedMesh)
42  ? false : TilingIfNotGPU();
43 
44  for ( MFIter mfi(S_sum[IntVars::cons],do_tiling); mfi.isValid(); ++mfi)
45  {
46  const Box bx = mfi.tilebox();
47  Box tbx = mfi.nodaltilebox(0);
48  Box tby = mfi.nodaltilebox(1);
49  Box tbz = mfi.nodaltilebox(2);
50 
51  Vector<Array4<Real> > ssum_h(n_data);
52  Vector<Array4<Real> > sold_h(n_data);
53  Vector<Array4<Real> > fslow_h(n_data);
54 
55  for (int i = 0; i < n_data; ++i) {
56  ssum_h[i] = S_sum[i].array(mfi);
57  sold_h[i] = S_old[i].array(mfi);
58  fslow_h[i] = F_slow[i].array(mfi);
59  }
60 
61  Gpu::AsyncVector<Array4<Real> > sold_d(n_data);
62  Gpu::AsyncVector<Array4<Real> > ssum_d(n_data);
63  Gpu::AsyncVector<Array4<Real> > fslow_d(n_data);
64 
65  Gpu::copy(Gpu::hostToDevice, sold_h.begin(), sold_h.end(), sold_d.begin());
66  Gpu::copy(Gpu::hostToDevice, ssum_h.begin(), ssum_h.end(), ssum_d.begin());
67  Gpu::copy(Gpu::hostToDevice, fslow_h.begin(), fslow_h.end(), fslow_d.begin());
68 
69  Array4<Real>* sold = sold_d.dataPtr();
70  Array4<Real>* ssum = ssum_d.dataPtr();
71  Array4<Real>* fslow = fslow_d.dataPtr();
72 
73  // Moving terrain
74  if ( solverChoice.terrain_type == TerrainType::MovingFittedMesh )
75  {
76  const Array4<const Real>& dJ_old = detJ_cc[level]->const_array(mfi);
77  const Array4<const Real>& dJ_new = detJ_cc_new[level]->const_array(mfi);
78  const Array4<const Real>& dJ_stg = detJ_cc_src[level]->const_array(mfi);
79 
80  const Array4<const Real>& z_nd_old = z_phys_nd[level]->const_array(mfi);
81  const Array4<const Real>& z_nd_new = z_phys_nd_new[level]->const_array(mfi);
82 
83  const Array4<const Real>& mf_u = mapfac[level][MapFacType::u_x]->const_array(mfi);
84  const Array4<const Real>& mf_v = mapfac[level][MapFacType::v_y]->const_array(mfi);
85 
86  const Array4<Real >& z_t_arr = z_t_rk[level]->array(mfi);
87 
88  // We have already scaled the slow source term to have the extra factor of dJ
89  ParallelFor(bx, ncomp_fast[IntVars::cons],
90  [=] AMREX_GPU_DEVICE (int i, int j, int k, int nn) {
91  const int n = scomp_fast[IntVars::cons] + nn;
92  ssum[IntVars::cons](i,j,k,n) = dJ_old(i,j,k) * sold[IntVars::cons](i,j,k,n)
93  + slow_dt * dJ_stg(i,j,k) * fslow[IntVars::cons](i,j,k,n);
94  ssum[IntVars::cons](i,j,k,n) /= dJ_new(i,j,k);
95  });
96 
97  // We have already scaled the slow source term to have the extra factor of dJ
98  ParallelFor(tbx, tby,
99  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
100  Real h_zeta_old = Compute_h_zeta_AtIface(i, j, k, dxInv, z_nd_old);
101  Real h_zeta_new = Compute_h_zeta_AtIface(i, j, k, dxInv, z_nd_new);
102  ssum[IntVars::xmom](i,j,k) = ( h_zeta_old * sold[IntVars::xmom](i,j,k)
103  + slow_dt * fslow[IntVars::xmom](i,j,k) ) / h_zeta_new;
104  },
105  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
106  Real h_zeta_old = Compute_h_zeta_AtJface(i, j, k, dxInv, z_nd_old);
107  Real h_zeta_new = Compute_h_zeta_AtJface(i, j, k, dxInv, z_nd_new);
108  ssum[IntVars::ymom](i,j,k) = ( h_zeta_old * sold[IntVars::ymom](i,j,k)
109  + slow_dt * fslow[IntVars::ymom](i,j,k) ) / h_zeta_new;
110  });
111  ParallelFor(tbz,
112  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
113  if (k == 0) {
114  // Here we take advantage of the fact that moving terrain has a slip wall
115  // so we can just use the new value at (i,j,0).
116  Real rho_on_face = ssum[IntVars::cons](i,j,k,Rho_comp);
117  ssum[IntVars::zmom](i,j,k) = WFromOmega(i,j,k,rho_on_face*z_t_arr(i,j,k),
118  ssum[IntVars::xmom], ssum[IntVars::ymom],
119  mf_u, mf_v, z_nd_new,dxInv);
120  } else {
121  Real dJ_old_kface = myhalf * (dJ_old(i,j,k) + dJ_old(i,j,k-1));
122  Real dJ_new_kface = myhalf * (dJ_new(i,j,k) + dJ_new(i,j,k-1));
123  ssum[IntVars::zmom](i,j,k) = ( dJ_old_kface * sold[IntVars::zmom](i,j,k)
124  + slow_dt * fslow[IntVars::zmom](i,j,k) ) / dJ_new_kface;
125  }
126  });
127 
128  } else if (solverChoice.terrain_type != TerrainType::EB) { // Fixed or no terrain
129  const Array4<const Real>& dJ_old = detJ_cc[level]->const_array(mfi);
130  ParallelFor(bx, ncomp_fast[IntVars::cons],
131  [=] AMREX_GPU_DEVICE (int i, int j, int k, int nn) {
132  const int n = scomp_fast[IntVars::cons] + nn;
133  if (dJ_old(i,j,k) > zero) {
134  ssum[IntVars::cons](i,j,k,n) = sold[IntVars::cons](i,j,k,n) + slow_dt *
135  ( fslow[IntVars::cons](i,j,k,n) );
136  } else {
137  ssum[IntVars::cons](i,j,k,n) = sold[IntVars::cons](i,j,k,n);
138  }
139  });
140 
141  ParallelFor(tbx, tby, tbz,
142  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
143  ssum[IntVars::xmom](i,j,k) = sold[IntVars::xmom](i,j,k)
144  + slow_dt * fslow[IntVars::xmom](i,j,k);
145  },
146  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
147  ssum[IntVars::ymom](i,j,k) = sold[IntVars::ymom](i,j,k)
148  + slow_dt * fslow[IntVars::ymom](i,j,k);
149  },
150  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
151  ssum[IntVars::zmom](i,j,k) = sold[IntVars::zmom](i,j,k)
152  + slow_dt * fslow[IntVars::zmom](i,j,k);
153  });
154  } // not moving terrain (with EB, S_sum is set after the redistribution below)
155  } // mfi
156  } // omp
157 
158  // Redistribution performs MPI communication and manages its own OpenMP regions.
159  if (solverChoice.terrain_type == TerrainType::EB)
160  {
161  BL_PROFILE("no_substep_fun_redistribute");
162  // Redistribute cons states (cell-centered)
163 
164  Vector<MultiFab> dUdt(IntVars::NumTypes);
165  dUdt[IntVars::cons].define(ba, dm, F_slow[IntVars::cons].nComp(), F_slow[IntVars::cons].nGrow(), MFInfo(), EBFactory(level));
166  for (int i = 1; i <= AMREX_SPACEDIM; ++i) {
167  dUdt[i].define(F_slow[i].boxArray(), F_slow[i].DistributionMap(), F_slow[i].nComp(), F_slow[i].nGrow(), MFInfo());
168  }
169  for (int i = 0; i <= AMREX_SPACEDIM; ++i) {
170  // Copy F_slow to dUdt, INCLUDING GHOST CELLS
171  // Ghost cells are required by redistribute_term which operates on grown boxes
172  MultiFab::Copy(dUdt[i], F_slow[i], 0, 0, F_slow[i].nComp(), F_slow[i].nGrow());
173  // redistribute_term fills the interior and periodic ghost cells
174  dUdt[i].setDomainBndry(bogus_large_value_d, 0, ncomp_fast[i], fine_geom);
175  }
176 
177  BCRec const* bc_ptr_d = domain_bcs_type_d.data();
178  IntVect ngv_pert = S_old[IntVars::cons].nGrowVect();
179 
180  MultiFab S_pert_cons(ba, dm, S_old[IntVars::cons].nComp(), ngv_pert, MFInfo(), EBFactory(level));
181  MultiFab rt0(ba, dm, 1, ngv_pert, MFInfo(), EBFactory(level));
182 
183  // Copy S_old into S_pert
184  MultiFab::Copy(S_pert_cons, S_old[IntVars::cons],0,0,S_pert_cons.nComp(), ngv_pert);
185 
186  // Copy theta0 into rt0
187  MultiFab::Copy(rt0,base_state[level],BaseState::th0_comp,0,1,ngv_pert);
188 
189  // Multiply theta0 by rho0 in rt0
190  MultiFab::Multiply(rt0,base_state[level],BaseState::r0_comp,0,1,ngv_pert);
191 
192  MultiFab::Subtract(S_pert_cons, base_state[level], BaseState::r0_comp,0,1,ngv_pert);
193  MultiFab::Subtract(S_pert_cons, rt0 , 0,1,1,ngv_pert);
194 
195  // Update F_slow for perturbational fast quantities (rho - rho0) and (rho_theta - rho_theta_0)
196  redistribute_term(ncomp_fast[IntVars::cons], fine_geom, F_slow[IntVars::cons], dUdt[IntVars::cons],
197  S_pert_cons, EBFactory(level), bc_ptr_d, slow_dt);
198 
199  // Update F_slow for momenta
200  redistribute_term(ncomp_fast[IntVars::xmom], fine_geom, F_slow[IntVars::xmom], dUdt[IntVars::xmom],
201  S_old[IntVars::xmom], *(get_eb(level).get_u_const_factory()), bc_ptr_d, slow_dt, IntVars::xmom);
202  redistribute_term(ncomp_fast[IntVars::ymom], fine_geom, F_slow[IntVars::ymom], dUdt[IntVars::ymom],
203  S_old[IntVars::ymom], *(get_eb(level).get_v_const_factory()), bc_ptr_d, slow_dt, IntVars::ymom);
204  redistribute_term(ncomp_fast[IntVars::zmom], fine_geom, F_slow[IntVars::zmom], dUdt[IntVars::zmom],
205  S_old[IntVars::zmom], *(get_eb(level).get_w_const_factory()), bc_ptr_d, slow_dt, IntVars::zmom);
206 
207 
208  // Update state using the updated F_slow; covered cells and faces keep S_old
209 #ifdef _OPENMP
210 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
211 #endif
212  for ( MFIter mfi(S_sum[IntVars::cons],TilingIfNotGPU()); mfi.isValid(); ++mfi)
213  {
214  const Box bx = mfi.tilebox();
215  Box tbx = mfi.nodaltilebox(0);
216  Box tby = mfi.nodaltilebox(1);
217  Box tbz = mfi.nodaltilebox(2);
218 
219  const Array4<Real >& ssum_c = S_sum[IntVars::cons].array(mfi);
220  const Array4<Real >& ssum_u = S_sum[IntVars::xmom].array(mfi);
221  const Array4<Real >& ssum_v = S_sum[IntVars::ymom].array(mfi);
222  const Array4<Real >& ssum_w = S_sum[IntVars::zmom].array(mfi);
223  const Array4<const Real>& sold_c = S_old[IntVars::cons].const_array(mfi);
224  const Array4<const Real>& sold_u = S_old[IntVars::xmom].const_array(mfi);
225  const Array4<const Real>& sold_v = S_old[IntVars::ymom].const_array(mfi);
226  const Array4<const Real>& sold_w = S_old[IntVars::zmom].const_array(mfi);
227  const Array4<const Real>& fslow_c = F_slow[IntVars::cons].const_array(mfi);
228  const Array4<const Real>& fslow_u = F_slow[IntVars::xmom].const_array(mfi);
229  const Array4<const Real>& fslow_v = F_slow[IntVars::ymom].const_array(mfi);
230  const Array4<const Real>& fslow_w = F_slow[IntVars::zmom].const_array(mfi);
231 
232  const Array4<const Real>& vfrac_c = (get_eb(level).get_const_factory())->getVolFrac().const_array(mfi);
233  const Array4<const Real>& vfrac_u = (get_eb(level).get_u_const_factory())->getVolFrac().const_array(mfi);
234  const Array4<const Real>& vfrac_v = (get_eb(level).get_v_const_factory())->getVolFrac().const_array(mfi);
235  const Array4<const Real>& vfrac_w = (get_eb(level).get_w_const_factory())->getVolFrac().const_array(mfi);
236 
237  ParallelFor(bx, ncomp_fast[IntVars::cons], [=] AMREX_GPU_DEVICE (int i, int j, int k, int nn)
238  {
239  const int n = scomp_fast[IntVars::cons] + nn;
240  ssum_c(i,j,k,n) = (vfrac_c(i,j,k) > zero) ? sold_c(i,j,k,n) + slow_dt * fslow_c(i,j,k,n)
241  : sold_c(i,j,k,n);
242  });
243 
244  ParallelFor(tbx, tby, tbz,
245  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
246  ssum_u(i,j,k) = (vfrac_u(i,j,k) > zero) ? sold_u(i,j,k) + slow_dt * fslow_u(i,j,k)
247  : sold_u(i,j,k);
248  },
249  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
250  ssum_v(i,j,k) = (vfrac_v(i,j,k) > zero) ? sold_v(i,j,k) + slow_dt * fslow_v(i,j,k)
251  : sold_v(i,j,k);
252  },
253  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
254  ssum_w(i,j,k) = (vfrac_w(i,j,k) > zero) ? sold_w(i,j,k) + slow_dt * fslow_w(i,j,k)
255  : sold_w(i,j,k);
256  });
257  } // MFIter
258  } // EB
259 
260  // Implicit Rayleigh damping of w (erf.rayleigh_damping_type = SlowImplicit). The slow
261  // source leaves it out (make_mom_sources), and with (rho w)* the update above
262  // (rho w)^new = ( (rho w)* + dt c s^2 rho wbar ) / ( 1 + dt c s^2 ),
263  // which is stable for any dt; the explicit term is not once c s^2 dt reaches 2 (RK2).
264  // The ground and the top keep the values the boundary conditions set.
265  if (solverChoice.rayleigh_slow_implicit_w(level))
266  {
267  const Real dampcoef = solverChoice.dampingChoice.rayleigh_dampcoef;
268  const Real* sinesq_stag = d_sinesq_stag_at_lev;
269  const Real* wbar = d_rayleigh_ptrs_at_lev[Rayleigh::wbar];
270  const int klo = fine_geom.Domain().smallEnd(2);
271  const int khi = fine_geom.Domain().bigEnd(2);
272  const bool is_eb = (solverChoice.terrain_type == TerrainType::EB);
273 #ifdef _OPENMP
274 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
275 #endif
276  for (MFIter mfi(S_sum[IntVars::zmom],TilingIfNotGPU()); mfi.isValid(); ++mfi)
277  {
278  const Box tbz = mfi.tilebox();
279  const Array4<Real >& rho_w = S_sum[IntVars::zmom].array(mfi);
280  const Array4<const Real>& cons = S_old[IntVars::cons].const_array(mfi);
281  const Array4<const Real> vfrac = is_eb ?
282  (get_eb(level).get_w_const_factory())->getVolFrac().const_array(mfi) : Array4<const Real>{};
283  ParallelFor(tbz, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
284  {
285  if (k <= klo || k > khi) { return; }
286  if (is_eb && vfrac(i,j,k) <= zero) { return; }
287  const Real a = slow_dt * dampcoef * sinesq_stag[k];
288  const Real rho_on_w_face = myhalf * (cons(i,j,k,Rho_comp) + cons(i,j,k-1,Rho_comp));
289  rho_w(i,j,k) = (rho_w(i,j,k) + a * rho_on_w_face * wbar[k]) / (one + a);
290  });
291  }
292  }
293 
294  // Even if we update all the conserved variables we don't need
295  // to fillpatch the slow ones every acoustic substep
296  apply_bcs(S_sum, time_for_fp, S_sum[IntVars::cons].nGrow(), S_sum[IntVars::xmom].nGrow(),
297  fast_only=true, vel_and_mom_synced=false);
298 
299  if (solverChoice.compressibility_type[level] != CompressibilityType::FullyCompressible) {
300  bool have_tb = (thin_xforce[level] || thin_yforce[level] || thin_zforce[level]);
301  if (have_tb) {
302  project_velocity_tb(level, slow_dt, S_sum);
303  } else {
304  project_momenta(level, time_for_fp, slow_dt, S_sum, true);
305  }
306  }
307  };
constexpr amrex::Real bogus_large_value
Definition: ERF_Constants.H:19
@ wbar
Definition: ERF_DataStruct.H:235
@ v_y
Definition: ERF_DataStruct.H:41
@ u_x
Definition: ERF_DataStruct.H:40
void redistribute_term(int ncomp, const Geometry &geom, MultiFab &result, MultiFab &result_tmp, MultiFab const &state, EBFactType const &ebfact, BCRec const *bc, double local_dt_d, int const igrid)
Apply EB state redistribution to result_tmp and write the redistributed result.
Definition: ERF_EBRedistribute.cpp:21
#define Rho_comp
Definition: ERF_IndexDefines.H:61
amrex::GpuArray< Real, AMREX_SPACEDIM > dxInv
Definition: ERF_InitCustomPertVels_ParticleTests.H:16
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 zero
Definition: ERF_NumericalConstants.H:36
constexpr amrex::Real myhalf
Definition: ERF_NumericalConstants.H:41
auto no_substep_fun
Definition: ERF_TI_no_substep_fun.H:4
auto apply_bcs
Definition: ERF_TI_utils.H:46
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 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
@ th0_comp
Definition: ERF_IndexDefines.H:112
@ r0_comp
Definition: ERF_IndexDefines.H:109
@ NumTypes
Definition: ERF_IndexDefines.H:253
@ ymom
Definition: ERF_IndexDefines.H:251
@ cons
Definition: ERF_IndexDefines.H:249
@ zmom
Definition: ERF_IndexDefines.H:252
@ xmom
Definition: ERF_IndexDefines.H:250
@ nn
Definition: ERF_WDM6.H:33
@ cons
Definition: ERF_IndexDefines.H:231