ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ERF_SlowRhsPost.cpp File Reference
#include <AMReX.H>
#include <ERF_SrcHeaders.H>
#include <ERF_TI_slow_headers.H>
#include <ERF_EBAdvection.H>
#include <ERF_EBRedistribute.H>
Include dependency graph for ERF_SlowRhsPost.cpp:

Functions

void erf_slow_rhs_post (int level, int finest_level, int nrk, Real dt, int n_qstate, Vector< MultiFab > &S_rhs, Vector< MultiFab > &S_old, Vector< MultiFab > &S_new, Vector< MultiFab > &S_data, const MultiFab &S_prim, Vector< MultiFab > &S_scratch, const MultiFab &xvel, const MultiFab &yvel, const MultiFab &, const MultiFab &source, const MultiFab *SmnSmn, const MultiFab *eddyDiffs, MultiFab *Hfx1, MultiFab *Hfx2, MultiFab *Hfx3, MultiFab *Q1fx1, MultiFab *Q1fx2, MultiFab *Q1fx3, MultiFab *Q2fx3, MultiFab *Diss, const Geometry geom, const SolverChoice &solverChoice, std::unique_ptr< SurfaceLayer > &SurfLayer, const Gpu::DeviceVector< BCRec > &domain_bcs_type_d, const Vector< BCRec > &domain_bcs_type_h, std::unique_ptr< MultiFab > &z_phys_nd, std::unique_ptr< MultiFab > &z_phys_cc, std::unique_ptr< MultiFab > &ax, std::unique_ptr< MultiFab > &ay, std::unique_ptr< MultiFab > &az, std::unique_ptr< MultiFab > &detJ, MultiFab *detJ_new, Gpu::DeviceVector< Real > &stretched_dz_d, Vector< std::unique_ptr< MultiFab >> &mapfac, amrex::EBFArrayBoxFactory const &ebfact, YAFluxRegister *fr_as_crse, YAFluxRegister *fr_as_fine)
 

Function Documentation

◆ erf_slow_rhs_post()

void erf_slow_rhs_post ( int  level,
int  finest_level,
int  nrk,
Real  dt,
int  n_qstate,
Vector< MultiFab > &  S_rhs,
Vector< MultiFab > &  S_old,
Vector< MultiFab > &  S_new,
Vector< MultiFab > &  S_data,
const MultiFab &  S_prim,
Vector< MultiFab > &  S_scratch,
const MultiFab &  xvel,
const MultiFab &  yvel,
const MultiFab &  ,
const MultiFab &  source,
const MultiFab *  SmnSmn,
const MultiFab *  eddyDiffs,
MultiFab *  Hfx1,
MultiFab *  Hfx2,
MultiFab *  Hfx3,
MultiFab *  Q1fx1,
MultiFab *  Q1fx2,
MultiFab *  Q1fx3,
MultiFab *  Q2fx3,
MultiFab *  Diss,
const Geometry  geom,
const SolverChoice solverChoice,
std::unique_ptr< SurfaceLayer > &  SurfLayer,
const Gpu::DeviceVector< BCRec > &  domain_bcs_type_d,
const Vector< BCRec > &  domain_bcs_type_h,
std::unique_ptr< MultiFab > &  z_phys_nd,
std::unique_ptr< MultiFab > &  z_phys_cc,
std::unique_ptr< MultiFab > &  ax,
std::unique_ptr< MultiFab > &  ay,
std::unique_ptr< MultiFab > &  az,
std::unique_ptr< MultiFab > &  detJ,
MultiFab *  detJ_new,
Gpu::DeviceVector< Real > &  stretched_dz_d,
Vector< std::unique_ptr< MultiFab >> &  mapfac,
amrex::EBFArrayBoxFactory const &  ebfact,
YAFluxRegister *  fr_as_crse,
YAFluxRegister *  fr_as_fine 
)

Function for computing the slow RHS for the evolution equations for the scalars other than density or potential temperature

Parameters
[in]levellevel of resolution
[in]finest_levelfinest level of resolution
[in]nrkwhich RK stage
[in]dtslow time step
[out]S_rhsRHS computed here
[in]S_oldsolution at start of time step
[in]S_newsolution at end of current RK stage
[in]S_datacurrent solution
[in]S_primprimitive variables (i.e. conserved variables divided by density)
[in]S_scratchscratch space
[in]xvelx-component of velocity
[in]yvely-component of velocity
[in]zvelz-component of velocity
[in]sourcesource terms for conserved variables
[in]SmnSmnstrain rate magnitude
[in]eddyDiffsdiffusion coefficients for LES turbulence models
[in]Hfx3heat flux in z-dir
[in]Dissdissipation of turbulent kinetic energy
[in]geomContainer for geometric information
[in]solverChoiceContainer for solver parameters
[in]SurfLayerPointer to SurfaceLayer class for Monin-Obukhov Similarity Theory boundary condition
[in]domain_bcs_type_ddevice vector for domain boundary conditions
[in]z_phys_ndheight coordinate at nodes
[in]axarea fractions on x-faces
[in]ayarea fractions on y-faces
[in]azarea fractions on z-faces
[in]detJJacobian of the metric transformation at start of time step (= 1 if use_terrain is false)
[in]detJ_newJacobian of the metric transformation at new RK stage time (= 1 if use_terrain is false)
[in]mapfacmap 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
98 {
99  BL_PROFILE_REGION("erf_slow_rhs_post()");
100 
101  const BCRec* bc_ptr_d = domain_bcs_type_d.data();
102  const BCRec* bc_ptr_h = domain_bcs_type_h.data();
103 
104  AdvChoice ac = solverChoice.advChoice;
105  DiffChoice dc = solverChoice.diffChoice;
106  TurbChoice tc = solverChoice.turbChoice[level];
107 
108  const MultiFab* t_mean_mf = nullptr;
109  if (SurfLayer) { t_mean_mf = SurfLayer->get_mac_avg(level,2); }
110 
111  const bool l_use_terrain = (solverChoice.mesh_type != MeshType::ConstantDz);
112  const bool l_moving_terrain = (solverChoice.terrain_type == TerrainType::MovingFittedMesh);
113  const bool l_reflux = ( (solverChoice.coupling_type == CouplingType::TwoWay) && (nrk == 2) && (finest_level > 0) );
114  if (l_moving_terrain) AMREX_ALWAYS_ASSERT(l_use_terrain);
115 
116  const bool l_anelastic = solverChoice.anelastic[level];
117 
118  const bool l_use_mono_adv = solverChoice.use_mono_adv;
119  const bool l_use_KE = ( tc.use_tke );
120  const bool l_need_SmnSmn = ( tc.les_type == LESType::Deardorff ||
121  tc.rans_type == RANSType::kEqn );
122  const bool l_advect_KE = ( tc.use_tke && tc.advect_tke );
123  const bool l_use_diff = ((dc.molec_diff_type != MolecDiffType::None) ||
124  (tc.les_type != LESType::None) ||
125  (tc.rans_type != RANSType::None) ||
126  (tc.pbl_type != PBLType::None) );
127  const bool l_use_turb = ( tc.les_type == LESType::Smagorinsky ||
128  tc.les_type == LESType::Deardorff ||
129  tc.rans_type == RANSType::kEqn ||
130  tc.pbl_type == PBLType::MYNN25 ||
131  tc.pbl_type == PBLType::MYNNEDMF ||
132  tc.pbl_type == PBLType::YSU ||
133  tc.pbl_type == PBLType::MRF );
134  const bool l_rotate = (solverChoice.use_rotate_surface_flux);
135 
136  const Box& domain = geom.Domain();
137 
138  const GpuArray<Real, AMREX_SPACEDIM> dxInv = geom.InvCellSizeArray();
139  const Real* dx = geom.CellSize();
140 
141  // *************************************************************************
142  // Set gravity as a vector
143  // *************************************************************************
144  const Array<Real,AMREX_SPACEDIM> grav{0.0, 0.0, -solverChoice.gravity};
145  const GpuArray<Real,AMREX_SPACEDIM> grav_gpu{grav[0], grav[1], grav[2]};
146 
147  // *************************************************************************
148  // Pre-computed quantities
149  // *************************************************************************
150  int nvars = S_data[IntVars::cons].nComp();
151  const BoxArray& ba = S_data[IntVars::cons].boxArray();
152  const DistributionMapping& dm = S_data[IntVars::cons].DistributionMap();
153 
154  std::unique_ptr<MultiFab> dflux_x;
155  std::unique_ptr<MultiFab> dflux_y;
156  std::unique_ptr<MultiFab> dflux_z;
157 
158  if (l_use_diff) {
159  dflux_x = std::make_unique<MultiFab>(convert(ba,IntVect(1,0,0)), dm, 1, 0);
160  dflux_y = std::make_unique<MultiFab>(convert(ba,IntVect(0,1,0)), dm, 1, 0);
161  dflux_z = std::make_unique<MultiFab>(convert(ba,IntVect(0,0,1)), dm, 1, 0);
162  } else {
163  dflux_x = nullptr;
164  dflux_y = nullptr;
165  dflux_z = nullptr;
166  }
167 
168  // Valid vars
169  Vector<int> is_valid_slow_var; is_valid_slow_var.resize(RhoQ1_comp+1,0);
170  if (l_use_KE) {is_valid_slow_var[ RhoKE_comp] = 1;}
171  is_valid_slow_var[RhoScalar_comp] = 1;
172  if (solverChoice.moisture_type != MoistureType::None) {
173  is_valid_slow_var[RhoQ1_comp] = 1;
174  }
175 
176  // *****************************************************************************
177  // Monotonic advection for scalars
178  // *****************************************************************************
179  int nvar = S_new[IntVars::cons].nComp();
180  Vector<Real> max_scal(nvar, 1.0e34); Gpu::DeviceVector<Real> max_scal_d(nvar);
181  Vector<Real> min_scal(nvar,-1.0e34); Gpu::DeviceVector<Real> min_scal_d(nvar);
182  if (l_use_mono_adv) {
183  auto const& ma_s_arr = S_new[IntVars::cons].const_arrays();
184  for (int ivar(RhoKE_comp); ivar<nvar; ++ivar) {
185  GpuTuple<Real,Real> mm = ParReduce(TypeList<ReduceOpMax,ReduceOpMin>{},
186  TypeList<Real, Real>{},
187  S_new[IntVars::cons], IntVect(0),
188  [=] AMREX_GPU_DEVICE (int box_no, int i, int j, int k) noexcept
189  -> GpuTuple<Real,Real>
190  {
191  return { ma_s_arr[box_no](i,j,k,ivar), ma_s_arr[box_no](i,j,k,ivar) };
192  });
193  max_scal[ivar] = get<0>(mm);
194  min_scal[ivar] = get<1>(mm);
195  }
196  }
197  Gpu::copy(Gpu::hostToDevice, max_scal.begin(), max_scal.end(), max_scal_d.begin());
198  Gpu::copy(Gpu::hostToDevice, min_scal.begin(), min_scal.end(), min_scal_d.begin());
199  Real* max_s_ptr = max_scal_d.data();
200  Real* min_s_ptr = min_scal_d.data();
201 
202  // *************************************************************************
203  // Calculate cell-centered eddy viscosity & diffusivities
204  //
205  // Notes -- we fill all the data in ghost cells before calling this so
206  // that we can fill the eddy viscosity in the ghost regions and
207  // not have to call a boundary filler on this data itself
208  //
209  // LES - updates both horizontal and vertical eddy viscosityS_tmp components
210  // PBL - only updates vertical eddy viscosity components so horizontal
211  // components come from the LES model or are left as zero.
212  // *************************************************************************
213 
214  // *************************************************************************
215  // Define updates and fluxes in the current RK stage
216  // *************************************************************************
217 #ifdef _OPENMP
218 #pragma omp parallel if (Gpu::notInLaunchRegion())
219 #endif
220  {
221  std::array<FArrayBox,AMREX_SPACEDIM> flux;
222  std::array<FArrayBox,AMREX_SPACEDIM> flux_tmp;
223 
224  int start_comp;
225  int num_comp;
226 
227  // Cell-centered masks for EB (used for flux interpolation)
228  iMultiFab physbnd_mask;
229  bool already_on_centroids = false;
230  if (solverChoice.terrain_type == TerrainType::EB) {
231  physbnd_mask.define(S_data[IntVars::cons].boxArray(), S_data[IntVars::cons].DistributionMap(), 1, 1);
232  physbnd_mask.BuildMask(geom.Domain(), geom.periodicity(), 1, 1, 0, 1);
233  }
234 
235  for (MFIter mfi(S_data[IntVars::cons],TilingIfNotGPU()); mfi.isValid(); ++mfi) {
236 
237  Box tbx = mfi.tilebox();
238 
239  // *************************************************************************
240  // Define flux arrays for use in advection
241  // *************************************************************************
242  for (int dir = 0; dir < AMREX_SPACEDIM; ++dir) {
243  if (solverChoice.terrain_type != TerrainType::EB) {
244  flux[dir].resize(surroundingNodes(tbx,dir),nvars);
245  } else {
246  flux[dir].resize(surroundingNodes(tbx,dir).grow(1),nvars);
247  }
248  flux[dir].setVal<RunOn::Device>(0.);
249  if (l_use_mono_adv) {
250  flux_tmp[dir].resize(surroundingNodes(tbx,dir),1);
251  flux_tmp[dir].setVal<RunOn::Device>(0.);
252  }
253  }
254  const GpuArray<const Array4<Real>, AMREX_SPACEDIM>
255  flx_arr{{AMREX_D_DECL(flux[0].array(), flux[1].array(), flux[2].array())}};
256  Array4<Real> tmpx = (l_use_mono_adv) ? flux_tmp[0].array() : Array4<Real>{};
257  Array4<Real> tmpy = (l_use_mono_adv) ? flux_tmp[1].array() : Array4<Real>{};
258  Array4<Real> tmpz = (l_use_mono_adv) ? flux_tmp[2].array() : Array4<Real>{};
259  const GpuArray<Array4<Real>, AMREX_SPACEDIM> flx_tmp_arr{{AMREX_D_DECL(tmpx,tmpy,tmpz)}};
260 
261  // *************************************************************************
262  // Define Array4's
263  // *************************************************************************
264  const Array4<const Real> & old_cons = S_old[IntVars::cons].array(mfi);
265  const Array4< Real> & cell_rhs = S_rhs[IntVars::cons].array(mfi);
266 
267  const Array4< Real> & new_cons = S_new[IntVars::cons].array(mfi);
268  const Array4< Real> & new_xmom = S_new[IntVars::xmom].array(mfi);
269  const Array4< Real> & new_ymom = S_new[IntVars::ymom].array(mfi);
270  const Array4< Real> & new_zmom = S_new[IntVars::zmom].array(mfi);
271 
272  const Array4< Real> & cur_cons = S_data[IntVars::cons].array(mfi);
273  const Array4<const Real> & cur_prim = S_prim.array(mfi);
274  const Array4< Real> & cur_xmom = S_data[IntVars::xmom].array(mfi);
275  const Array4< Real> & cur_ymom = S_data[IntVars::ymom].array(mfi);
276  const Array4< Real> & cur_zmom = S_data[IntVars::zmom].array(mfi);
277 
278  Array4<Real> avg_xmom = S_scratch[IntVars::xmom].array(mfi);
279  Array4<Real> avg_ymom = S_scratch[IntVars::ymom].array(mfi);
280  Array4<Real> avg_zmom = S_scratch[IntVars::zmom].array(mfi);
281 
282  const Array4<const Real> & u = xvel.array(mfi);
283  const Array4<const Real> & v = yvel.array(mfi);
284 
285  const Array4<Real const>& mu_turb = l_use_turb ? eddyDiffs->const_array(mfi) : Array4<const Real>{};
286 
287  const Array4<const Real>& z_nd = z_phys_nd->const_array(mfi);
288  const Array4<const Real>& z_cc = z_phys_cc->const_array(mfi);
289  const Array4<const Real>& detJ_new_arr = l_moving_terrain ? detJ_new->const_array(mfi) : Array4<const Real>{};
290 
291  // Map factors
292  const Array4<const Real>& mf_mx = mapfac[MapFacType::m_x]->const_array(mfi);
293  const Array4<const Real>& mf_ux = mapfac[MapFacType::u_x]->const_array(mfi);
294  const Array4<const Real>& mf_vx = mapfac[MapFacType::v_x]->const_array(mfi);
295  const Array4<const Real>& mf_my = mapfac[MapFacType::m_y]->const_array(mfi);
296  const Array4<const Real>& mf_uy = mapfac[MapFacType::u_y]->const_array(mfi);
297  const Array4<const Real>& mf_vy = mapfac[MapFacType::v_y]->const_array(mfi);
298 
299  // SmnSmn for KE src with Deardorff or k-eqn RANS
300  const Array4<const Real>& SmnSmn_a = l_need_SmnSmn ? SmnSmn->const_array(mfi) : Array4<const Real>{};
301 
302  // **************************************************************************
303  // Here we fill the "current" data with "new" data because that is the result of the previous RK stage
304  // **************************************************************************
305  int nsv = S_old[IntVars::cons].nComp() - 2;
306  const GpuArray<int, IntVars::NumTypes> scomp_slow = { 2,0,0,0};
307  const GpuArray<int, IntVars::NumTypes> ncomp_slow = {nsv,0,0,0};
308 
309  // **************************************************************************
310  // Note that here we do copy only the "slow" variables, not (rho) or (rho theta)
311  // **************************************************************************
312  ParallelFor(tbx, ncomp_slow[IntVars::cons],
313  [=] AMREX_GPU_DEVICE (int i, int j, int k, int nn) {
314  const int n = scomp_slow[IntVars::cons] + nn;
315  cur_cons(i,j,k,n) = new_cons(i,j,k,n);
316  });
317 
318  // We have projected the velocities stored in S_data but we will use
319  // the velocities stored in S_scratch to update the scalars, so
320  // we need to copy from S_data (projected) into S_scratch
321  if (l_anelastic) {
322  Box tbx_inc = mfi.nodaltilebox(0);
323  Box tby_inc = mfi.nodaltilebox(1);
324  Box tbz_inc = mfi.nodaltilebox(2);
325 
326  ParallelFor(tbx_inc, tby_inc, tbz_inc,
327  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
328  avg_xmom(i,j,k) = cur_xmom(i,j,k);
329  },
330  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
331  avg_ymom(i,j,k) = cur_ymom(i,j,k);
332  },
333  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
334  avg_zmom(i,j,k) = cur_zmom(i,j,k);
335  });
336  }
337 
338  // **************************************************************************
339  // Define updates in the RHS of continuity, temperature, and scalar equations
340  // **************************************************************************
341  Array4<const int> mask_arr{};
342  Array4<const EBCellFlag> cfg_arr{};
343  Array4<const Real> ax_arr{};
344  Array4<const Real> ay_arr{};
345  Array4<const Real> az_arr{};
346  Array4<const Real> fcx_arr{};
347  Array4<const Real> fcy_arr{};
348  Array4<const Real> fcz_arr{};
349  Array4<const Real> detJ_arr{};
350  if (solverChoice.terrain_type == TerrainType::EB) {
351  EBCellFlagFab const& cfg = ebfact.getMultiEBCellFlagFab()[mfi];
352  cfg_arr = cfg.const_array();
353  ax_arr = ebfact.getAreaFrac()[0]->const_array(mfi);
354  ay_arr = ebfact.getAreaFrac()[1]->const_array(mfi);
355  az_arr = ebfact.getAreaFrac()[2]->const_array(mfi);
356  fcx_arr = ebfact.getFaceCent()[0]->const_array(mfi);
357  fcy_arr = ebfact.getFaceCent()[1]->const_array(mfi);
358  fcz_arr = ebfact.getFaceCent()[2]->const_array(mfi);
359  detJ_arr = ebfact.getVolFrac().const_array(mfi);
360  // if (!already_on_centroids) {mask_arr = physbnd_mask.const_array(mfi);}
361  mask_arr = physbnd_mask.const_array(mfi);
362  } else {
363  ax_arr = ax->const_array(mfi);
364  ay_arr = ay->const_array(mfi);
365  az_arr = az->const_array(mfi);
366  detJ_arr = detJ->const_array(mfi);
367  }
368 
369  AdvType horiz_adv_type, vert_adv_type;
370  Real horiz_upw_frac, vert_upw_frac;
371 
372  Array4<Real> diffflux_x, diffflux_y, diffflux_z;
373  Array4<Real> hfx_x, hfx_y, hfx_z, diss;
374  Array4<Real> q1fx_x, q1fx_y, q1fx_z, q2fx_z;
375  const bool use_SurfLayer = (SurfLayer != nullptr);
376 
377  if (l_use_diff) {
378  diffflux_x = dflux_x->array(mfi);
379  diffflux_y = dflux_y->array(mfi);
380  diffflux_z = dflux_z->array(mfi);
381 
382  hfx_x = Hfx1->array(mfi);
383  hfx_y = Hfx2->array(mfi);
384  hfx_z = Hfx3->array(mfi);
385  diss = Diss->array(mfi);
386 
387  if (Q1fx1) q1fx_x = Q1fx1->array(mfi);
388  if (Q1fx2) q1fx_y = Q1fx2->array(mfi);
389  if (Q1fx3) q1fx_z = Q1fx3->array(mfi);
390  if (Q2fx3) q2fx_z = Q2fx3->array(mfi);
391  }
392 
393  //
394  // Note that we either advect and diffuse all or none of the moisture variables
395  //
396  for (int ivar(RhoKE_comp); ivar<= RhoQ1_comp; ++ivar)
397  {
398  if (is_valid_slow_var[ivar])
399  {
400  start_comp = ivar;
401  num_comp = 1;
402 
403  if (ivar == RhoQ1_comp) {
404  horiz_adv_type = ac.moistscal_horiz_adv_type;
405  vert_adv_type = ac.moistscal_vert_adv_type;
406  horiz_upw_frac = ac.moistscal_horiz_upw_frac;
407  vert_upw_frac = ac.moistscal_vert_upw_frac;
408 
409  if (ac.use_efficient_advection){
410  horiz_adv_type = EfficientAdvType(nrk,ac.moistscal_horiz_adv_type);
411  vert_adv_type = EfficientAdvType(nrk,ac.moistscal_vert_adv_type);
412  }
413 
414  num_comp = n_qstate;
415 
416  } else {
417  horiz_adv_type = ac.dryscal_horiz_adv_type;
418  vert_adv_type = ac.dryscal_vert_adv_type;
419  horiz_upw_frac = ac.dryscal_horiz_upw_frac;
420  vert_upw_frac = ac.dryscal_vert_upw_frac;
421 
422  if (ac.use_efficient_advection){
423  horiz_adv_type = EfficientAdvType(nrk,ac.dryscal_horiz_adv_type);
424  vert_adv_type = EfficientAdvType(nrk,ac.dryscal_vert_adv_type);
425  }
426 
427  if (ivar == RhoScalar_comp) {
428  num_comp = NSCALARS;
429  }
430  }
431 
432  if (( ivar != RhoKE_comp ) ||
433  ((ivar == RhoKE_comp) && l_advect_KE))
434  {
435  if (solverChoice.terrain_type != TerrainType::EB){
436  AdvectionSrcForScalars(dt, tbx, start_comp, num_comp, avg_xmom, avg_ymom, avg_zmom,
437  cur_cons, cur_prim, cell_rhs,
438  l_use_mono_adv, max_s_ptr, min_s_ptr,
439  detJ_arr, dxInv, mf_mx, mf_my,
440  horiz_adv_type, vert_adv_type,
441  horiz_upw_frac, vert_upw_frac,
442  flx_arr, flx_tmp_arr, domain, bc_ptr_h);
443  } else {
444  EBAdvectionSrcForScalars(tbx, start_comp, num_comp,
445  avg_xmom, avg_ymom, avg_zmom,
446  cur_prim, cell_rhs,
447  mask_arr, cfg_arr, ax_arr, ay_arr, az_arr,
448  fcx_arr, fcy_arr, fcz_arr,
449  detJ_arr, dxInv, mf_mx, mf_my,
450  horiz_adv_type, vert_adv_type,
451  horiz_upw_frac, vert_upw_frac,
452  flx_arr, domain, bc_ptr_h,
453  already_on_centroids);
454  }
455  }
456 
457  if (l_use_diff) {
458  const Array4<const Real> tm_arr = t_mean_mf ? t_mean_mf->const_array(mfi) : Array4<const Real>{};
459  if (solverChoice.mesh_type == MeshType::StretchedDz && solverChoice.terrain_type != TerrainType::EB) {
460  DiffusionSrcForState_S(tbx, domain, start_comp, num_comp, l_rotate, u, v,
461  new_cons, cur_prim, cell_rhs,
462  diffflux_x, diffflux_y, diffflux_z,
463  stretched_dz_d, dxInv, SmnSmn_a,
464  mf_mx, mf_ux, mf_vx,
465  mf_my, mf_uy, mf_vy,
466  hfx_x, hfx_y, hfx_z, q1fx_x, q1fx_y, q1fx_z,q2fx_z, diss,
467  mu_turb, solverChoice, level,
468  tm_arr, grav_gpu, bc_ptr_d, use_SurfLayer);
469  } else if (l_use_terrain) {
470  DiffusionSrcForState_T(tbx, domain, start_comp, num_comp, l_rotate, u, v,
471  new_cons, cur_prim, cell_rhs,
472  diffflux_x, diffflux_y, diffflux_z,
473  z_nd, z_cc, ax_arr, ay_arr, az_arr,
474  detJ_arr, dxInv, SmnSmn_a,
475  mf_mx, mf_ux, mf_vx,
476  mf_my, mf_uy, mf_vy,
477  hfx_x, hfx_y, hfx_z, q1fx_x, q1fx_y, q1fx_z,q2fx_z, diss,
478  mu_turb, solverChoice, level,
479  tm_arr, grav_gpu, bc_ptr_d, use_SurfLayer);
480  } else {
481  DiffusionSrcForState_N(tbx, domain, start_comp, num_comp, u, v,
482  new_cons, cur_prim, cell_rhs,
483  diffflux_x, diffflux_y, diffflux_z, dxInv, SmnSmn_a,
484  mf_mx, mf_ux, mf_vx,
485  mf_my, mf_uy, mf_vy,
486  hfx_z, q1fx_z, q2fx_z, diss,
487  mu_turb, solverChoice, level,
488  tm_arr, grav_gpu, bc_ptr_d, use_SurfLayer);
489  }
490  } // use_diff
491  } // valid slow var
492  } // loop ivar
493 
494 #if defined(ERF_USE_NETCDF)
495  if (moist_set_rhs_bool)
496  {
497  const Array4<const Real> & old_cons_const = S_old[IntVars::cons].const_array(mfi);
498  const Array4<const Real> & new_cons_const = S_new[IntVars::cons].const_array(mfi);
499  moist_set_rhs(tbx, old_cons_const, new_cons_const, cell_rhs, bdy_time_interval,
500  start_bdy_time, new_stage_time, dt, width, set_width, domain,
501  bdy_data_xlo, bdy_data_xhi, bdy_data_ylo, bdy_data_yhi);
502  }
503 #endif
504 
505  // This updates just the "slow" conserved variables
506  {
507  BL_PROFILE("rhs_post_8");
508 
509  const Real eps = std::numeric_limits<Real>::epsilon();
510 
511  auto const& src_arr = source.const_array(mfi);
512 
513  for (int ivar(RhoKE_comp); ivar<= RhoQ1_comp; ++ivar)
514  {
515  if (is_valid_slow_var[ivar])
516  {
517  start_comp = ivar;
518  num_comp = 1;
519  if (ivar == RhoQ1_comp) {
520  num_comp = nvars - RhoQ1_comp;
521  } else if (ivar == RhoScalar_comp) {
522  num_comp = NSCALARS;
523  }
524 
525  if (l_moving_terrain)
526  {
527  ParallelFor(tbx, num_comp,
528  [=] AMREX_GPU_DEVICE (int i, int j, int k, int nn) noexcept {
529  const int n = start_comp + nn;
530  cell_rhs(i,j,k,n) += src_arr(i,j,k,n);
531  Real temp_val = detJ_arr(i,j,k) * old_cons(i,j,k,n) + dt * detJ_arr(i,j,k) * cell_rhs(i,j,k,n);
532  cur_cons(i,j,k,n) = temp_val / detJ_new_arr(i,j,k);
533  if (ivar == RhoKE_comp) {
534  cur_cons(i,j,k,n) = amrex::max(cur_cons(i,j,k,n), eps);
535  }
536  });
537 
538  } else if (l_anelastic && (nrk == 1)) { // not moving and ( (anelastic) and second RK stage) )
539 
540  ParallelFor(tbx, num_comp,
541  [=] AMREX_GPU_DEVICE (int i, int j, int k, int nn) noexcept {
542  const int n = start_comp + nn;
543  cell_rhs(i,j,k,n) += src_arr(i,j,k,n);
544 
545  // Re-construct the cell_rhs used in the first RK stage
546  Real dt_times_old_cell_rhs = cur_cons(i,j,k,n) - old_cons(i,j,k,n);
547 
548  // Add the time-averaged RHS to the old state
549  cur_cons(i,j,k,n) = old_cons(i,j,k,n) + 0.5 * (dt_times_old_cell_rhs + dt * cell_rhs(i,j,k,n));
550 
551  if (ivar == RhoKE_comp) {
552  cur_cons(i,j,k,n) = amrex::max(cur_cons(i,j,k,n), eps);
553  } else if (ivar >= RhoQ1_comp) {
554  cur_cons(i,j,k,n) = amrex::max(cur_cons(i,j,k,n), 0.0);
555  }
556  });
557 
558  } else { // not moving and ( (not anelastic) or (first RK stage) )
559 
560  ParallelFor(tbx, num_comp,
561  [=] AMREX_GPU_DEVICE (int i, int j, int k, int nn) noexcept {
562  const int n = start_comp + nn;
563  cell_rhs(i,j,k,n) += src_arr(i,j,k,n);
564  cur_cons(i,j,k,n) = old_cons(i,j,k,n) + dt * cell_rhs(i,j,k,n);
565  if (ivar == RhoKE_comp) {
566  cur_cons(i,j,k,n) = amrex::max(cur_cons(i,j,k,n), eps);
567  } else if (ivar >= RhoQ1_comp) {
568  cur_cons(i,j,k,n) = amrex::max(cur_cons(i,j,k,n), 0.0);
569  }
570  });
571 
572  } // moving, anelastic or neither?
573 
574  } // is_valid
575  } // ivar
576  } // profile
577 
578  {
579  BL_PROFILE("rhs_post_9");
580  // This updates all the conserved variables (not just the "slow" ones)
581  int num_comp_all = S_data[IntVars::cons].nComp();
582  ParallelFor(tbx, num_comp_all,
583  [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) noexcept {
584  new_cons(i,j,k,n) = cur_cons(i,j,k,n);
585  });
586  } // end profile
587 
588  Box xtbx = mfi.nodaltilebox(0);
589  Box ytbx = mfi.nodaltilebox(1);
590  Box ztbx = mfi.nodaltilebox(2);
591 
592  {
593  BL_PROFILE("rhs_post_10()");
594  ParallelFor(xtbx, ytbx, ztbx,
595  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
596  new_xmom(i,j,k) = cur_xmom(i,j,k);
597  },
598  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
599  new_ymom(i,j,k) = cur_ymom(i,j,k);
600  },
601  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
602  new_zmom(i,j,k) = cur_zmom(i,j,k);
603  });
604  } // end profile
605 
606  {
607  BL_PROFILE("rhs_post_10");
608  // We only add to the flux registers in the final RK step
609  if (l_reflux) {
610  int strt_comp_reflux = RhoTheta_comp + 1;
611  int num_comp_reflux = nvars - strt_comp_reflux;
612  if (level < finest_level) {
613  fr_as_crse->CrseAdd(mfi,
614  {{AMREX_D_DECL(&(flux[0]), &(flux[1]), &(flux[2]))}},
615  dx, dt, strt_comp_reflux, strt_comp_reflux, num_comp_reflux, RunOn::Device);
616  }
617  if (level > 0) {
618  fr_as_fine->FineAdd(mfi,
619  {{AMREX_D_DECL(&(flux[0]), &(flux[1]), &(flux[2]))}},
620  dx, dt, strt_comp_reflux, strt_comp_reflux, num_comp_reflux, RunOn::Device);
621  }
622 
623  // This is necessary here so we don't go on to the next FArrayBox without
624  // having finished copying the fluxes into the FluxRegisters (since the fluxes
625  // are stored in temporary FArrayBox's)
626  Gpu::streamSynchronize();
627 
628  } // two-way coupling
629  } // end profile
630  } // mfi
631 
632  if (solverChoice.terrain_type == TerrainType::EB)
633  {
634  // start_comp and num_comp
635  for (int ivar(RhoKE_comp); ivar<= RhoQ1_comp; ++ivar)
636  {
637  if (is_valid_slow_var[ivar])
638  {
639  start_comp = ivar;
640  num_comp = 1;
641  if (ivar == RhoQ1_comp) {
642  num_comp = nvars - RhoQ1_comp;
643  } else if (ivar == RhoScalar_comp) {
644  num_comp = NSCALARS;
645  }
646  }
647  }
648 
649  // Redistribute cons states (cell-centered)
650  const int num_comp_total{S_rhs[IntVars::cons].nComp()};
651  MultiFab dUdt_tmp(ba, dm, num_comp_total, S_rhs[IntVars::cons].nGrow(), MFInfo(), ebfact);
652  dUdt_tmp.setVal(0.);
653  MultiFab::Copy(dUdt_tmp, S_rhs[IntVars::cons], start_comp, start_comp, num_comp, S_rhs[IntVars::cons].nGrow());
654  dUdt_tmp.FillBoundary(geom.periodicity());
655  dUdt_tmp.setDomainBndry(1.234e10, 0, num_comp_total, geom);
656 
657  S_old[IntVars::cons].FillBoundary(geom.periodicity());
658  S_old[IntVars::cons].setDomainBndry(1.234e10, 0, num_comp_total, geom);
659 
660  // Update S_rhs by Redistribution.
661  // To-do: Currently, redistributing all the scalar variables.
662  // This needs to be redistributed only for num_comp variables starting from ivar, for efficiency.
663  redistribute_term ( num_comp_total, geom, S_rhs[IntVars::cons], dUdt_tmp,
664  S_old[IntVars::cons], ebfact, bc_ptr_d, dt);
665 
666  // Update state using the updated S_rhs. (NOTE: redistribute_term returns RHS not state variables.)
667  for ( MFIter mfi(S_new[IntVars::cons],TilingIfNotGPU()); mfi.isValid(); ++mfi)
668  {
669  Box tbx = mfi.tilebox();
670  const Array4<Real>& snew = S_new[IntVars::cons].array(mfi);
671  const Array4<Real>& sold = S_old[IntVars::cons].array(mfi);
672  const Array4<Real>& srhs = S_rhs[IntVars::cons].array(mfi);
673  Array4<const Real> detJ_arr = ebfact.getVolFrac().const_array(mfi);
674 
675  ParallelFor(tbx, num_comp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int nn)
676  {
677  if (detJ_arr(i,j,k) > 0.0) {
678  const int n = start_comp + nn;
679  snew(i,j,k,n) = sold(i,j,k,n) + dt * srhs(i,j,k,n);
680  }
681  });
682  }
683 
684  // Redistribute momentum states (face-centered) will be added here.
685  } // EB
686 
687  } // OMP
688 }
void AdvectionSrcForScalars(const amrex::Real &dt, const amrex::Box &bx, const int icomp, const int ncomp, const amrex::Array4< const amrex::Real > &avg_xmom, const amrex::Array4< const amrex::Real > &avg_ymom, const amrex::Array4< const amrex::Real > &avg_zmom, const amrex::Array4< const amrex::Real > &cur_cons, const amrex::Array4< const amrex::Real > &cell_prim, const amrex::Array4< amrex::Real > &src, const bool &use_mono_adv, amrex::Real *max_s_ptr, amrex::Real *min_s_ptr, const amrex::Array4< const amrex::Real > &vf_arr, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &cellSizeInv, const amrex::Array4< const amrex::Real > &mf_mx, const amrex::Array4< const amrex::Real > &mf_my, const AdvType horiz_adv_type, const AdvType vert_adv_type, const amrex::Real horiz_upw_frac, const amrex::Real vert_upw_frac, const amrex::GpuArray< const amrex::Array4< amrex::Real >, AMREX_SPACEDIM > &flx_arr, const amrex::GpuArray< amrex::Array4< amrex::Real >, AMREX_SPACEDIM > &flx_tmp_arr, const amrex::Box &domain, const amrex::BCRec *bc_ptr_h)
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE AdvType EfficientAdvType(int nrk, AdvType adv_type)
Definition: ERF_Advection.H:287
@ nvars
Definition: ERF_DataStruct.H:87
@ v_x
Definition: ERF_DataStruct.H:22
@ u_y
Definition: ERF_DataStruct.H:23
@ v_y
Definition: ERF_DataStruct.H:23
@ m_y
Definition: ERF_DataStruct.H:23
@ u_x
Definition: ERF_DataStruct.H:22
@ m_x
Definition: ERF_DataStruct.H:22
void DiffusionSrcForState_S(const amrex::Box &bx, const amrex::Box &domain, int start_comp, int num_comp, const bool &rotate, const amrex::Array4< const amrex::Real > &u, const amrex::Array4< const amrex::Real > &v, const amrex::Array4< const amrex::Real > &cell_data, const amrex::Array4< const amrex::Real > &cell_prim, const amrex::Array4< amrex::Real > &cell_rhs, const amrex::Array4< amrex::Real > &xflux, const amrex::Array4< amrex::Real > &yflux, const amrex::Array4< amrex::Real > &zflux, const amrex::Gpu::DeviceVector< amrex::Real > &stretched_dz_d, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &dxInv, const amrex::Array4< const amrex::Real > &SmnSmn_a, const amrex::Array4< const amrex::Real > &mf_mx, const amrex::Array4< const amrex::Real > &mf_ux, const amrex::Array4< const amrex::Real > &mf_vx, const amrex::Array4< const amrex::Real > &mf_my, const amrex::Array4< const amrex::Real > &mf_uy, const amrex::Array4< const amrex::Real > &mf_vy, amrex::Array4< amrex::Real > &hfx_x, amrex::Array4< amrex::Real > &hfx_y, amrex::Array4< amrex::Real > &hfx_z, amrex::Array4< amrex::Real > &qfx1_x, amrex::Array4< amrex::Real > &qfx1_y, amrex::Array4< amrex::Real > &qfx1_z, amrex::Array4< amrex::Real > &qfx2_z, amrex::Array4< amrex::Real > &diss, const amrex::Array4< const amrex::Real > &mu_turb, const SolverChoice &solverChoice, const int level, const amrex::Array4< const amrex::Real > &tm_arr, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > grav_gpu, const amrex::BCRec *bc_ptr, const bool use_SurfLayer)
void DiffusionSrcForState_N(const amrex::Box &bx, const amrex::Box &domain, int start_comp, int num_comp, const amrex::Array4< const amrex::Real > &u, const amrex::Array4< const amrex::Real > &v, const amrex::Array4< const amrex::Real > &cell_data, const amrex::Array4< const amrex::Real > &cell_prim, const amrex::Array4< amrex::Real > &cell_rhs, const amrex::Array4< amrex::Real > &xflux, const amrex::Array4< amrex::Real > &yflux, const amrex::Array4< amrex::Real > &zflux, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &cellSizeInv, const amrex::Array4< const amrex::Real > &SmnSmn_a, const amrex::Array4< const amrex::Real > &mf_mx, const amrex::Array4< const amrex::Real > &mf_ux, const amrex::Array4< const amrex::Real > &mf_vx, const amrex::Array4< const amrex::Real > &mf_my, const amrex::Array4< const amrex::Real > &mf_uy, const amrex::Array4< const amrex::Real > &mf_vy, amrex::Array4< amrex::Real > &hfx_z, amrex::Array4< amrex::Real > &qfx1_z, amrex::Array4< amrex::Real > &qfx2_z, amrex::Array4< amrex::Real > &diss, const amrex::Array4< const amrex::Real > &mu_turb, const SolverChoice &solverChoice, const int level, const amrex::Array4< const amrex::Real > &tm_arr, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > grav_gpu, const amrex::BCRec *bc_ptr, const bool use_SurfLayer)
void DiffusionSrcForState_T(const amrex::Box &bx, const amrex::Box &domain, int start_comp, int num_comp, const bool &rotate, const amrex::Array4< const amrex::Real > &u, const amrex::Array4< const amrex::Real > &v, const amrex::Array4< const amrex::Real > &cell_data, const amrex::Array4< const amrex::Real > &cell_prim, const amrex::Array4< amrex::Real > &cell_rhs, const amrex::Array4< amrex::Real > &xflux, const amrex::Array4< amrex::Real > &yflux, const amrex::Array4< amrex::Real > &zflux, const amrex::Array4< const amrex::Real > &z_nd, const amrex::Array4< const amrex::Real > &z_cc, const amrex::Array4< const amrex::Real > &ax, const amrex::Array4< const amrex::Real > &ay, const amrex::Array4< const amrex::Real > &az, const amrex::Array4< const amrex::Real > &detJ, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &dxInv, const amrex::Array4< const amrex::Real > &SmnSmn_a, const amrex::Array4< const amrex::Real > &mf_mx, const amrex::Array4< const amrex::Real > &mf_ux, const amrex::Array4< const amrex::Real > &mf_vx, const amrex::Array4< const amrex::Real > &mf_my, const amrex::Array4< const amrex::Real > &mf_uy, const amrex::Array4< const amrex::Real > &mf_vy, amrex::Array4< amrex::Real > &hfx_x, amrex::Array4< amrex::Real > &hfx_y, amrex::Array4< amrex::Real > &hfx_z, amrex::Array4< amrex::Real > &qfx1_x, amrex::Array4< amrex::Real > &qfx1_y, amrex::Array4< amrex::Real > &qfx1_z, amrex::Array4< amrex::Real > &qfx2_z, amrex::Array4< amrex::Real > &diss, const amrex::Array4< const amrex::Real > &mu_turb, const SolverChoice &solverChoice, const int level, const amrex::Array4< const amrex::Real > &tm_arr, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > grav_gpu, const amrex::BCRec *bc_ptr, const bool use_SurfLayer)
void EBAdvectionSrcForScalars(const amrex::Box &bx, const int icomp, const int ncomp, const amrex::Array4< const amrex::Real > &avg_xmom, const amrex::Array4< const amrex::Real > &avg_ymom, const amrex::Array4< const amrex::Real > &avg_zmom, const amrex::Array4< const amrex::Real > &cell_prim, const amrex::Array4< amrex::Real > &src, const amrex::Array4< const int > &mask_arr, const amrex::Array4< const amrex::EBCellFlag > &cfg_arr, const amrex::Array4< const amrex::Real > &ax_arr, const amrex::Array4< const amrex::Real > &ay_arr, const amrex::Array4< const amrex::Real > &az_arr, const amrex::Array4< const amrex::Real > &fcx_arr, const amrex::Array4< const amrex::Real > &fcy_arr, const amrex::Array4< const amrex::Real > &fcz_arr, const amrex::Array4< const amrex::Real > &vf_arr, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &cellSizeInv, const amrex::Array4< const amrex::Real > &mf_mx, const amrex::Array4< const amrex::Real > &mf_my, const AdvType horiz_adv_type, const AdvType vert_adv_type, const amrex::Real horiz_upw_frac, const amrex::Real vert_upw_frac, const amrex::GpuArray< const amrex::Array4< amrex::Real >, AMREX_SPACEDIM > &flx_arr, const amrex::Box &domain, const amrex::BCRec *bc_ptr_h, bool already_on_centroids)
void redistribute_term(int ncomp, const Geometry &geom, MultiFab &result, MultiFab &result_tmp, MultiFab const &state, EBFArrayBoxFactory const &ebfact, BCRec const *bc, Real const local_dt)
Definition: ERF_EBRedistribute.cpp:13
#define RhoScalar_comp
Definition: ERF_IndexDefines.H:40
#define RhoTheta_comp
Definition: ERF_IndexDefines.H:37
#define NSCALARS
Definition: ERF_IndexDefines.H:16
#define RhoQ1_comp
Definition: ERF_IndexDefines.H:42
AdvType
Definition: ERF_IndexDefines.H:221
#define RhoKE_comp
Definition: ERF_IndexDefines.H:38
@ ymom
Definition: ERF_IndexDefines.H:160
@ cons
Definition: ERF_IndexDefines.H:158
@ zmom
Definition: ERF_IndexDefines.H:161
@ xmom
Definition: ERF_IndexDefines.H:159
@ xvel
Definition: ERF_IndexDefines.H:141
@ yvel
Definition: ERF_IndexDefines.H:142
real(c_double), parameter epsilon
Definition: ERF_module_model_constants.F90:12
real(c_double), private ac
Definition: ERF_module_mp_morr_two_moment.F90:180
Definition: ERF_AdvStruct.H:19
Definition: ERF_DiffStruct.H:19
MolecDiffType molec_diff_type
Definition: ERF_DiffStruct.H:81
static MeshType mesh_type
Definition: ERF_DataStruct.H:708
bool use_mono_adv
Definition: ERF_DataStruct.H:798
DiffChoice diffChoice
Definition: ERF_DataStruct.H:717
amrex::Real gravity
Definition: ERF_DataStruct.H:757
amrex::Vector< TurbChoice > turbChoice
Definition: ERF_DataStruct.H:719
amrex::Vector< int > anelastic
Definition: ERF_DataStruct.H:724
AdvChoice advChoice
Definition: ERF_DataStruct.H:716
MoistureType moisture_type
Definition: ERF_DataStruct.H:804
static TerrainType terrain_type
Definition: ERF_DataStruct.H:702
bool use_rotate_surface_flux
Definition: ERF_DataStruct.H:785
CouplingType coupling_type
Definition: ERF_DataStruct.H:803
Definition: ERF_TurbStruct.H:39
PBLType pbl_type
Definition: ERF_TurbStruct.H:354
RANSType rans_type
Definition: ERF_TurbStruct.H:351
bool advect_tke
Definition: ERF_TurbStruct.H:394
bool use_tke
Definition: ERF_TurbStruct.H:366
LESType les_type
Definition: ERF_TurbStruct.H:314
Here is the call graph for this function: