ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_ImplicitDiff_S.cpp File Reference
Include dependency graph for ERF_ImplicitDiff_S.cpp:

Macros

#define INSTANTIATE_IMPLICIT_DIFF_FOR_MOM_LU(STAGDIR)
 

Functions

void ImplicitDiffForStateLU_S (const Box &bx, const Box &domain, const int level, const int n, const double dt_d, const GpuArray< Real, AMREX_SPACEDIM *2 > &bc_neumann_vals, const Array4< Real > &cell_data, const Gpu::DeviceVector< Real > &stretched_dz_d, const Array4< const Real > &scalar_zflux, const Array4< const Real > &mu_turb, const SolverChoice &solverChoice, const BCRec *bc_ptr, const bool use_SurfLayer, const Real implicit_fac, const bool use_mrf_countergradient)
 
template<int stagdir>
void ImplicitDiffForMomLU_S (const Box &bx, const Box &, const int level, const double dt_d, const Array4< const Real > &cell_data, const Array4< Real > &face_data, const Array4< const Real > &tau, const Array4< const Real > &tau_corr, const Gpu::DeviceVector< Real > &stretched_dz_d, const Array4< const Real > &mu_turb, const SolverChoice &solverChoice, const BCRec *bc_ptr, const bool use_SurfLayer, const Real implicit_fac, const bool use_ysu_mom_countergradient)
 

Macro Definition Documentation

◆ INSTANTIATE_IMPLICIT_DIFF_FOR_MOM_LU

#define INSTANTIATE_IMPLICIT_DIFF_FOR_MOM_LU (   STAGDIR)
Value:
template void ImplicitDiffForMomLU_S<STAGDIR> ( \
const Box&, \
const Box&, \
const int, \
const double, \
const Array4<const Real>&, \
const Array4< Real>&, \
const Array4<const Real>&, \
const Array4<const Real>&, \
const Gpu::DeviceVector<Real>&, \
const Array4<const Real>&, \
const SolverChoice&, \
const BCRec*, \
const bool, \
const Real, \
const bool);
amrex::Real Real
Definition: ERF_ShocInterface.H:19
Definition: ERF_DataStruct.H:634

Function Documentation

◆ ImplicitDiffForMomLU_S()

template<int stagdir>
void ImplicitDiffForMomLU_S ( const Box &  bx,
const Box &  ,
const int  level,
const double  dt_d,
const Array4< const Real > &  cell_data,
const Array4< Real > &  face_data,
const Array4< const Real > &  tau,
const Array4< const Real > &  tau_corr,
const Gpu::DeviceVector< Real > &  stretched_dz_d,
const Array4< const Real > &  mu_turb,
const SolverChoice solverChoice,
const BCRec *  bc_ptr,
const bool  use_SurfLayer,
const Real  implicit_fac,
const bool  use_ysu_mom_countergradient 
)

Function for computing the implicit contribution to the vertical diffusion of momentum, with a vertically stretched grid over flat terrain.

This function (explicitly instantiated below) handles staggering in x, y, or z through the template parameter, stagdir.

Parameters
[in]bxcell-centered box to loop over
[in]levelAMR level
[in]dt_dtime step
[in]cell_dataconserved cell-centered rho
[in,out]face_dataconserved momentum
[in]taustress contribution to momentum
[in]tau_corrstress contribution to momentum that will be corrected by the implicit solve
[in]stretched_dz_darray over z of dz[k]
[in]mu_turbturbulent viscosity
[in]solverChoicecontainer of parameters
[in]bc_ptrcontainer with boundary conditions
[in]use_SurfLayerwhether we have turned on subgrid diffusion
[in]implicit_facif 1 then fully implicit; if 0 then fully explicit
[in]use_ysu_mom_countergradientwhether to include YSU momentum countergradient correction
268 {
269  BL_PROFILE_VAR("ImplicitDiffForMom_S()",ImplicitDiffForMom_S);
270 
271  Real dt = static_cast<Real>(dt_d);
272 
273  // setup quantities for getRhoAlphaAtFaces()
274  DiffChoice dc = solverChoice.diffChoice;
275  TurbChoice tc = solverChoice.turbChoice[level];
276  bool l_consA = (dc.molec_diff_type == MolecDiffType::ConstantAlpha);
277  bool l_turb = tc.use_kturb;
278  // The off-diagonal correction strains for u/v contain a factor of 1/2,
279  // while the diagonal correction strain for w does not.
280  constexpr Real molec_fac = (stagdir == 2) ? two : one;
281  Real mu_eff = (l_consA) ? molec_fac * dc.dynamic_viscosity / dc.rho0_trans
282  : molec_fac * dc.dynamic_viscosity;
283 
284  // g(S*) coefficient
285  // stagdir==0: tau_corr = myhalf * du/dz * mu_tot
286  // stagdir==1: tau_corr = myhalf * dv/dz * mu_tot
287  // stagdir==2: tau_corr = dw/dz * mu_tot
288  constexpr Real gfac = (stagdir == 2) ? two/three : one;
289 
290  // offsets used to average to faces
291  constexpr int ioff = (stagdir == 0) ? 1 : 0;
292  constexpr int joff = (stagdir == 1) ? 1 : 0;
293 
294  // Box bounds
295  int ilo = bx.smallEnd(0);
296  int ihi = bx.bigEnd(0);
297  int jlo = bx.smallEnd(1);
298  int jhi = bx.bigEnd(1);
299  int klo = bx.smallEnd(2);
300  int khi = bx.bigEnd(2);
301  amrex::ignore_unused(ilo, ihi, jlo, jhi);
302 
303  // Temporary FABs for tridiagonal solve (allocated on column)
304  // A[k] * x[k-1] + B[k] * x[k] + C[k+1] = RHS[k]
305  amrex::FArrayBox RHS_fab, soln_fab, coeffG_fab;
306  RHS_fab.resize(bx,1, amrex::The_Async_Arena());
307  soln_fab.resize(bx,1, amrex::The_Async_Arena());
308  coeffG_fab.resize(bx,1, amrex::The_Async_Arena());
309  auto const& RHS_a = RHS_fab.array();
310  auto const& soln_a = soln_fab.array();
311  auto const& coeffG_a = coeffG_fab.array();
312 
313  auto dz_ptr = stretched_dz_d.data();
314 
315  int bc_comp = BCVars::xvel_bc + stagdir;
316  bool ext_dir_on_zlo = (bc_ptr[bc_comp].lo(2) == ERFBCType::ext_dir ||
317  bc_ptr[bc_comp].lo(2) == ERFBCType::ext_dir_prim);
318  bool ext_dir_on_zhi = (bc_ptr[bc_comp].hi(2) == ERFBCType::ext_dir ||
319  bc_ptr[bc_comp].hi(2) == ERFBCType::ext_dir_prim);
320  bool foextrap_on_zlo = (bc_ptr[bc_comp].lo(2) == ERFBCType::foextrap);
321  bool foextrap_on_zhi = (bc_ptr[bc_comp].hi(2) == ERFBCType::foextrap);
322  amrex::ignore_unused(foextrap_on_zlo,foextrap_on_zhi);
323 
324  AMREX_ASSERT_WITH_MESSAGE(foextrap_on_zlo || ext_dir_on_zlo || use_SurfLayer,
325  "Unexpected lower BC for momentum used with implicit vertical diffusion");
326  AMREX_ASSERT_WITH_MESSAGE(foextrap_on_zhi || ext_dir_on_zhi,
327  "Unexpected upper BC for momentum used with implicit vertical diffusion");
328 
329  Real Fact = implicit_fac * dt;
330 
331 #ifdef AMREX_USE_GPU
332  ParallelFor(makeSlab(bx,2,0), [=] AMREX_GPU_DEVICE (int i, int j, int)
333  {
334 #else
335  for (int j(jlo); j<=jhi; ++j) {
336  for (int i(ilo); i<=ihi; ++i) {
337 #endif
338  // Notes:
339  //
340  // - In DiffusionSrcForMom (e.g., for x-mom)
341  //
342  // Real diffContrib = ...
343  // + (tau13(i,j,k+1) - tau13(i,j,k)) / dzinv
344  // rho_u_rhs(i,j,k) -= diffContrib; // note the negative sign
345  //
346  // - We need to scale the explicit _part_ of `tau13` (for x-mom) by (1 - implicit_fac)
347  // The part that needs to be scaled is stored in `tau_corr`.
348  // E.g., tau13 = myhalf * (du/dz + dw/dx)
349  // tau13_corr = myhalf * du/dz
350  //
351  // - The momentum (`face_data`) was set to `S_old + S_rhs * dt`
352  // prior to including "ERF_Implicit.H". Recall that S_rhs includes
353  // sources from advection and other forcings, not just diffusion.
354  //
355  // - To correct momentum, we need to subtract `implicit_fac * diffContrib_corr`
356  // from S_rhs to recover `(1 - implicit_fac) * diffContrib_corr`,
357  // where `diffContrib_corr = -d(tau_corr)/dz`. The negative sign
358  // comes from our convention for the RHS diffusion source.
359  //
360  // Subtracting a negative gives the += below; multiply by dt to
361  // get the intermediate momentum on the RHS of the tridiagonal
362  // system.
363  //
364  // - With a surface_layer BC, tau13/23 holds the vertical flux -d_z(k*u_i)
365  // directly. We must use tau at klo (not tau_corr) with SL BCs.
366  //
367  // - Finally, the terms ~ RHS += (tau_corr_hi - tau_corr_lo) / dz (below)
368  // essentially undo the explicit diffusion update that will be
369  // handled here implicitly.
370 
371  // Bottom boundary coefficients and RHS for L decomp
372  //===================================================
373  Real rhoface, rhoAlpha_lo, rhoAlpha_hi;
374  Real dz_inv, dz_inv_lo, dz_inv_hi;
375  Real a_tmp, b_tmp, c_tmp, inv_b2_tmp;
376  {
377  rhoface = myhalf * (cell_data(i,j,klo,Rho_comp) + cell_data(i-ioff,j-joff,klo,Rho_comp));
378  getRhoAlphaForFaces(i, j, klo, ioff, joff, rhoAlpha_lo, rhoAlpha_hi,
379  cell_data, mu_turb, mu_eff,
380  l_consA, l_turb);
381 
382  dz_inv = one / dz_ptr[klo];
383  dz_inv_lo = dz_inv;
384  dz_inv_hi = two / (dz_ptr[klo] + dz_ptr[klo+1]);
385 
386  a_tmp = zero;
387  c_tmp = -Fact * gfac * rhoAlpha_hi * dz_inv_hi * dz_inv;
388 
389  RHS_a(i,j,klo) = face_data(i,j,klo); // NOTE: this is momenta; solution is velocity
390 
391  // BCs: Dirichlet (u_i = val), slip wall (w = 0), or surface layer (w = 0)
392  if (ext_dir_on_zlo) {
393  RHS_a(i,j,klo) += Fact * gfac * (tau_corr(i,j,klo+1) - tau_corr(i,j,klo)) * dz_inv;
394  if (stagdir==2) {
395  c_tmp = 0.;
396  RHS_a(i,j,klo) = 0.;
397  } else {
398  // NOTE: wall is 1/2 dz away (2 dz_inv)
399  a_tmp = -two * Fact * rhoAlpha_lo * dz_inv_lo * dz_inv;
400  const Real rho_wall = myhalf * ( cell_data(i ,j ,klo-1,Rho_comp)
401  + cell_data(i-ioff,j-joff,klo-1,Rho_comp) );
402  const Real wall_velocity = face_data(i,j,klo-1) / rho_wall;
403  RHS_a(i,j,klo) -= a_tmp * wall_velocity;
404  }
405  } else if (use_SurfLayer) {
406  // NOTE: tau = -mu*d_z(u_i) w/ SL
407  RHS_a(i,j,klo) += Fact * gfac * (tau_corr(i,j,klo+1) - tau(i,j,klo)) * dz_inv;
408  RHS_a(i,j,klo) += Fact * dz_inv * tau(i,j,klo);
409  } else {
410  // NOTE: FOEXTRAP has zero lower flux (nothing to add to RHS)
411  RHS_a(i,j,klo) += Fact * gfac * (tau_corr(i,j,klo+1) - tau_corr(i,j,klo)) * dz_inv;
412  }
413 
414  // Add YSU momentum countergradient correction at bottom boundary.
415  // NOTE: As for the scalars, the lower face at klo carries no
416  // countergradient flux -- the surface stress is supplied by the
417  // surface layer model or the wall BC above. Only the upper face
418  // contributes here.
419  // NOTE: The sign matches the scalar path: tau_i3 = -rho*K*(du_i/dz - gamma_i),
420  // so the countergradient piece of the flux is +rho*K*gamma_i and its
421  // divergence enters the RHS with a minus sign.
422  if (use_ysu_mom_countergradient && stagdir < 2) {
423  const int hgam_comp = (stagdir == 0) ? EddyDiff::HGAMU_v : EddyDiff::HGAMV_v;
424  // Average HGAM* to the staggered face
425  const Real gam_klo = myhalf * (mu_turb(i,j,klo ,hgam_comp) + mu_turb(i-ioff,j-joff,klo ,hgam_comp));
426  const Real gam_kp1 = myhalf * (mu_turb(i,j,klo+1,hgam_comp) + mu_turb(i-ioff,j-joff,klo+1,hgam_comp));
427  const Real gam_hi = myhalf * (gam_klo + gam_kp1);
428  RHS_a(i,j,klo) -= Fact * dz_inv * rhoAlpha_hi * gam_hi;
429  }
430 
431  b_tmp = rhoface - a_tmp - c_tmp;
432  inv_b2_tmp = one;
433 
434  RHS_a(i,j,klo) /= b_tmp; // NOTE: this is now "rho"
435  coeffG_a(i,j,klo) = c_tmp / b_tmp; // NOTE: this is now "gamma"
436  }
437 
438  // Build the coefficients and RHS for L decomp
439  //===================================================
440  for (int k(klo+1); k < khi; k++) {
441  rhoface = myhalf * (cell_data(i,j,k,Rho_comp) + cell_data(i-ioff,j-joff,k,Rho_comp));
442  getRhoAlphaForFaces(i, j, k, ioff, joff, rhoAlpha_lo, rhoAlpha_hi,
443  cell_data, mu_turb, mu_eff,
444  l_consA, l_turb);
445 
446  dz_inv = one / dz_ptr[k];
447  dz_inv_lo = two / (dz_ptr[k] + dz_ptr[k-1]);
448  dz_inv_hi = two / (dz_ptr[k] + dz_ptr[k+1]);
449 
450  a_tmp = -Fact * rhoAlpha_lo * dz_inv_lo * dz_inv;
451  c_tmp = -Fact * rhoAlpha_hi * dz_inv_hi * dz_inv;
452  b_tmp = rhoface - a_tmp - c_tmp;
453  inv_b2_tmp = one / (b_tmp - a_tmp * coeffG_a(i,j,k-1));
454 
455  RHS_a(i,j,k) = face_data(i,j,k); // NOTE: this is momenta; solution is velocity
456  RHS_a(i,j,k) += Fact * gfac * (tau_corr(i,j,k+1) - tau_corr(i,j,k)) * dz_inv;
457 
458  // Add YSU momentum countergradient correction
459  if (use_ysu_mom_countergradient && stagdir < 2) {
460  const int hgam_comp = (stagdir == 0) ? EddyDiff::HGAMU_v : EddyDiff::HGAMV_v;
461  // AverageHGAM* to the staggered face
462  const Real gam_k = myhalf * (mu_turb(i,j,k ,hgam_comp) + mu_turb(i-ioff,j-joff,k ,hgam_comp));
463  const Real gam_km1 = myhalf * (mu_turb(i,j,k-1,hgam_comp) + mu_turb(i-ioff,j-joff,k-1,hgam_comp));
464  const Real gam_kp1 = myhalf * (mu_turb(i,j,k+1,hgam_comp) + mu_turb(i-ioff,j-joff,k+1,hgam_comp));
465  const Real gam_hi = myhalf * (gam_k + gam_kp1); // at k+1/2
466  const Real gam_lo = myhalf * (gam_k + gam_km1); // at k-1/2
467  RHS_a(i,j,k) -= Fact * dz_inv * (rhoAlpha_hi * gam_hi - rhoAlpha_lo * gam_lo);
468  }
469 
470  RHS_a(i,j,k) = (RHS_a(i,j,k) - a_tmp * RHS_a(i,j,k-1)) * inv_b2_tmp; // NOTE: This is now "rho"
471  coeffG_a(i,j,k) = c_tmp * inv_b2_tmp; // NOTE: this is now "gamma"
472  } // k
473 
474  // Top boundary coefficients and RHS for L decomp
475  //===================================================
476  {
477  rhoface = myhalf * (cell_data(i,j,khi,Rho_comp) + cell_data(i-ioff,j-joff,khi,Rho_comp));
478  getRhoAlphaForFaces(i, j, khi, ioff, joff, rhoAlpha_lo, rhoAlpha_hi,
479  cell_data, mu_turb, mu_eff,
480  l_consA, l_turb);
481 
482  dz_inv = one / dz_ptr[khi];
483  dz_inv_lo = two / (dz_ptr[khi] + dz_ptr[khi-1]);
484  dz_inv_hi = dz_inv;
485 
486  a_tmp = -Fact * gfac * rhoAlpha_lo * dz_inv_lo * dz_inv;
487  c_tmp = zero;
488 
489  RHS_a(i,j,khi) = face_data(i,j,khi); // NOTE: this is momenta; solution is velocity
490  RHS_a(i,j,khi) += Fact * gfac * (tau_corr(i,j,khi+1) - tau_corr(i,j,khi)) * dz_inv;
491 
492  // BCs: Dirichlet (u_i = val), slip wall (w = 0)
493  if (ext_dir_on_zhi) {
494  if (stagdir==2) {
495  a_tmp = zero;
496  RHS_a(i,j,khi) = zero;
497  } else {
498  // NOTE: wall is 1/2 dz away (2 dz_inv)
499  c_tmp = -two * Fact * rhoAlpha_hi * dz_inv_hi * dz_inv;
500  const Real rho_wall = myhalf * ( cell_data(i ,j ,khi+1,Rho_comp)
501  + cell_data(i-ioff,j-joff,khi+1,Rho_comp) );
502  const Real wall_velocity = face_data(i,j,khi+1) / rho_wall;
503  RHS_a(i,j,khi) -= c_tmp * wall_velocity;
504  }
505  }
506 
507  b_tmp = rhoface - a_tmp - c_tmp;
508  inv_b2_tmp = one / (b_tmp - a_tmp * coeffG_a(i,j,khi-1));
509 
510  // First solve
511  soln_a(i,j,khi) = (RHS_a(i,j,khi) - a_tmp * RHS_a(i,j,khi-1)) * inv_b2_tmp;
512  }
513 
514  // Back sweep the U decomp solution
515  //===================================================
516  for (int k(khi-1); k>=klo; --k) {
517  soln_a(i,j,k) = RHS_a(i,j,k) - coeffG_a(i,j,k) * soln_a(i,j,k+1);
518  }
519 
520  // Convert back to momenta
521  //===================================================
522  for (int k(klo); k<=khi; ++k) {
523  rhoface = myhalf * (cell_data(i,j,k,Rho_comp) + cell_data(i-ioff,j-joff,k,Rho_comp));
524  face_data(i,j,k) = rhoface * soln_a(i,j,k);
525  }
526 
527 #ifdef AMREX_USE_GPU
528  });
529 #else
530  } // i
531  } // j
532 #endif
533 }
constexpr amrex::Real three
Definition: ERF_Constants.H:11
constexpr amrex::Real two
Definition: ERF_Constants.H:10
constexpr amrex::Real one
Definition: ERF_Constants.H:9
constexpr amrex::Real zero
Definition: ERF_Constants.H:8
constexpr amrex::Real myhalf
Definition: ERF_Constants.H:13
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void getRhoAlphaForFaces(int i, int j, int k, int ioff, int joff, amrex::Real &rhoAlpha_lo, amrex::Real &rhoAlpha_hi, const amrex::Array4< const amrex::Real > &cell_data, const amrex::Array4< const amrex::Real > &mu_turb, const amrex::Real mu_eff, bool l_consA, bool l_turb)
Definition: ERF_GetRhoAlphaForFaces.H:22
#define Rho_comp
Definition: ERF_IndexDefines.H:39
const int khi
Definition: ERF_InitCustomPert_Bubble.H:21
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);})
AMREX_ASSERT_WITH_MESSAGE(wbar_cutoff_min > wbar_cutoff_max, "ERROR: wbar_cutoff_min < wbar_cutoff_max")
@ xvel_bc
Definition: ERF_IndexDefines.H:105
@ foextrap
Definition: ERF_IndexDefines.H:291
@ ext_dir
Definition: ERF_IndexDefines.H:292
@ ext_dir_prim
Definition: ERF_IndexDefines.H:295
@ HGAMU_v
Definition: ERF_IndexDefines.H:262
@ HGAMV_v
Definition: ERF_IndexDefines.H:263
Definition: ERF_DiffStruct.H:22
MolecDiffType molec_diff_type
Selected molecular transport model.
Definition: ERF_DiffStruct.H:94
amrex::Real dynamic_viscosity
Dynamic viscosity for momentum diffusion [kg/(m-s)].
Definition: ERF_DiffStruct.H:106
amrex::Real rho0_trans
Reference density used to compute dynamic diffusion coefficients [kg/m3].
Definition: ERF_DiffStruct.H:101
amrex::Vector< TurbChoice > turbChoice
Turbulence options for each AMR level.
Definition: ERF_DataStruct.H:1864
DiffChoice diffChoice
Diffusion-related options.
Definition: ERF_DataStruct.H:1861
Definition: ERF_TurbStruct.H:114
bool use_kturb
Whether any turbulence model is active.
Definition: ERF_TurbStruct.H:665
Here is the call graph for this function:

◆ ImplicitDiffForStateLU_S()

void ImplicitDiffForStateLU_S ( const Box &  bx,
const Box &  domain,
const int  level,
const int  n,
const double  dt_d,
const GpuArray< Real, AMREX_SPACEDIM *2 > &  bc_neumann_vals,
const Array4< Real > &  cell_data,
const Gpu::DeviceVector< Real > &  stretched_dz_d,
const Array4< const Real > &  scalar_zflux,
const Array4< const Real > &  mu_turb,
const SolverChoice solverChoice,
const BCRec *  bc_ptr,
const bool  use_SurfLayer,
const Real  implicit_fac,
const bool  use_mrf_countergradient 
)

Function for computing the implicit contribution to the vertical diffusion of theta, with a vertically stretched grid over flat terrain.

Parameters
[in]bxcell-centered box to loop over
[in]domainbox of the whole domain
[in]levelAMR level
[in]nconserved component index
[in]dt_dtime step
[in]bc_neumann_valsvalues of derivatives if bc_type == Neumann
[in,out]cell_dataconserved cell-centered rho, rho theta
[in]stretched_dz_darray over z of dz[k]
[in]scalar_zfluxscalar vertical flux in z-dir
[in]mu_turbturbulent viscosity
[in]solverChoicecontainer of parameters
[in]bc_ptrcontainer with boundary conditions
[in]use_SurfLayerwhether we have turned on subgrid diffusion
[in]implicit_facif 1 then fully implicit; if 0 then fully explicit
[in]use_mrf_countergradientwhether to include MRF countergradient correction
45 {
46  BL_PROFILE_VAR("ImplicitDiffForState_S()",ImplicitDiffForState_S);
47 
48  Real dt = static_cast<Real>(dt_d);
49 
50  // setup quantities for getRhoAlpha()
51 #include "ERF_SetupVertDiff.H"
52  const int qty_index = n;
53  const int prim_index = qty_index - 1;
54  const int prim_scal_index = (qty_index >= RhoScalar_comp && qty_index < RhoScalar_comp+NSCALARS) ? PrimScalar_comp : prim_index;
55 
56  // Box bounds
57  int ilo = bx.smallEnd(0);
58  int ihi = bx.bigEnd(0);
59  int jlo = bx.smallEnd(1);
60  int jhi = bx.bigEnd(1);
61  int klo = bx.smallEnd(2);
62  int khi = bx.bigEnd(2);
63  amrex::ignore_unused(ilo, ihi, jlo, jhi);
64 
65  // Temporary FABs for tridiagonal solve (allocated on column)
66  // A[k] * x[k-1] + B[k] * x[k] + C[k+1] = RHS[k]
67 
68  // With LU decomposition, M * x = r is written as L * U * x = r with U * x = rho
69  // We then first have L * rho = r and U * x = rho
70  amrex::FArrayBox RHS_fab, soln_fab, coeffG_fab;
71  RHS_fab.resize(bx,1, amrex::The_Async_Arena());
72  soln_fab.resize(bx,1, amrex::The_Async_Arena());
73  coeffG_fab.resize(bx,1, amrex::The_Async_Arena());
74  auto const& RHS_a = RHS_fab.array();
75  auto const& soln_a = soln_fab.array();
76  auto const& coeffG_a = coeffG_fab.array();
77 
78  auto dz_ptr = stretched_dz_d.data();
79 
80  int bc_comp = qty_index;
81  bool foextrap_on_zlo = (bc_ptr[bc_comp].lo(2) == ERFBCType::foextrap);
82  bool foextrap_on_zhi = (bc_ptr[bc_comp].hi(2) == ERFBCType::foextrap);
83  bool neumann_on_zlo = (bc_ptr[bc_comp].lo(2) == ERFBCType::neumann);
84  bool neumann_on_zhi = (bc_ptr[bc_comp].hi(2) == ERFBCType::neumann);
85  amrex::ignore_unused(foextrap_on_zlo, foextrap_on_zhi);
86 
87  AMREX_ASSERT_WITH_MESSAGE(foextrap_on_zlo || neumann_on_zlo || use_SurfLayer,
88  "Unexpected lower BC for scalars used with implicit vertical diffusion");
89  AMREX_ASSERT_WITH_MESSAGE(foextrap_on_zhi || neumann_on_zhi,
90  "Unexpected upper BC for scalars used with implicit vertical diffusion");
91 
92  Real Fact = implicit_fac * dt;
93 
94 #ifdef AMREX_USE_GPU
95  ParallelFor(makeSlab(bx,2,0), [=] AMREX_GPU_DEVICE (int i, int j, int)
96  {
97 #else
98  for (int j(jlo); j<=jhi; ++j) {
99  for (int i(ilo); i<=ihi; ++i) {
100 #endif
101  // Bottom boundary coefficients and RHS for L decomp
102  //===================================================
103  Real rhoAlpha_lo, rhoAlpha_hi;
104  Real dz_inv, dz_inv_lo, dz_inv_hi;
105  Real a_tmp, b_tmp, c_tmp, inv_b2_tmp;
106  {
107  getRhoAlpha(i, j, klo, rhoAlpha_lo, rhoAlpha_hi,
108  cell_data, mu_turb, d_alpha_eff, d_eddy_diff_idz,
109  prim_index, prim_scal_index, l_consA, l_turb);
110 
111  dz_inv = one / dz_ptr[klo];
112  dz_inv_lo = dz_inv;
113  dz_inv_hi = two / (dz_ptr[klo] + dz_ptr[klo+1]);
114 
115  a_tmp = zero;
116  c_tmp = -Fact * rhoAlpha_hi * dz_inv_hi * dz_inv;
117  b_tmp = cell_data(i,j,klo,Rho_comp) - a_tmp - c_tmp;
118  inv_b2_tmp = one;
119 
120  RHS_a(i,j,klo) = cell_data(i,j,klo,n); // NOTE: this is rho*phi; solution is phi
121  if (use_SurfLayer && scalar_zflux) {
122  RHS_a(i,j,klo) += Fact * dz_inv * scalar_zflux(i,j,klo); // NOTE: scalar_zflux = -K*d_z(\phi)
123  } else if (neumann_on_zlo) {
124  RHS_a(i,j,klo) += -Fact * dz_inv * rhoAlpha_lo * bc_neumann_vals[2]; // NOTE: N_val = d_z(\phi)
125  }
126 
127  // Add countergradient correction to RHS at bottom boundary.
128  // NOTE: The lower face at klo carries no countergradient flux -- the
129  // total surface flux is supplied by the surface layer model or the
130  // Neumann BC, while gamma represents nonlocal transport interior to
131  // the PBL. Only the upper face contributes here.
132  if (use_mrf_countergradient && (n == RhoTheta_comp || n == RhoQ1_comp)) {
133  const int gam_comp = (n == RhoTheta_comp) ? EddyDiff::HGAMT_v : EddyDiff::HGAMQ_v;
134  const Real gam_hi = myhalf * (mu_turb(i, j, klo, gam_comp) + mu_turb(i, j, klo+1, gam_comp));
135  // rhoAlpha*gam is already a flux, so its divergence over cell klo
136  // is scaled by the *cell* spacing, not the face spacing
137  RHS_a(i,j,klo) -= Fact * dz_inv * rhoAlpha_hi * gam_hi;
138  }
139 
140  RHS_a(i,j,klo) /= b_tmp; // NOTE: this is now "rho"
141  coeffG_a(i,j,klo) = c_tmp / b_tmp; // NOTE: this is now "gamma"
142  }
143 
144  // Build the coefficients and RHS for L decomp
145  //===================================================
146  for (int k(klo+1); k < khi; k++) {
147  getRhoAlpha(i, j, k, rhoAlpha_lo, rhoAlpha_hi,
148  cell_data, mu_turb, d_alpha_eff, d_eddy_diff_idz,
149  prim_index, prim_scal_index, l_consA, l_turb);
150 
151  dz_inv = one / dz_ptr[k];
152  dz_inv_lo = two / (dz_ptr[k] + dz_ptr[k-1]);
153  dz_inv_hi = two / (dz_ptr[k] + dz_ptr[k+1]);
154 
155  a_tmp = -Fact * rhoAlpha_lo * dz_inv_lo * dz_inv;
156  c_tmp = -Fact * rhoAlpha_hi * dz_inv_hi * dz_inv;
157  b_tmp = cell_data(i,j,k,Rho_comp) - a_tmp - c_tmp;
158  inv_b2_tmp = one / (b_tmp - a_tmp * coeffG_a(i,j,k-1));
159 
160  RHS_a(i,j,k) = cell_data(i,j,k,n); // NOTE: this is rho*phi; solution is phi
161 
162  // Add countergradient correction to RHS in interior
163  if (use_mrf_countergradient && (n == RhoTheta_comp || n == RhoQ1_comp)) {
164  const int gam_comp = (n == RhoTheta_comp) ? EddyDiff::HGAMT_v : EddyDiff::HGAMQ_v;
165  const Real gam_k = mu_turb(i, j, k, gam_comp);
166  const Real gam_km1 = mu_turb(i, j, k-1, gam_comp);
167  const Real gam_kp1 = mu_turb(i, j, k+1, gam_comp);
168  const Real gam_hi = myhalf * (gam_k + gam_kp1); // at k+½
169  const Real gam_lo = myhalf * (gam_k + gam_km1); // at k-½
170  // Countergradient flux divergence (implicit contribution to RHS):
171  // -Fact * dz_inv * [ρα_{k+½}·γ_{k+½} - ρα_{k-½}·γ_{k-½}]
172  // ρα·γ is already a flux, so differencing it across the cell is
173  // scaled by the *cell* spacing dz_inv. The face spacings
174  // dz_inv_hi/dz_inv_lo belong to the gradient in the diffusion
175  // coefficients above, not here; using them would leave a spurious
176  // tendency for a constant countergradient flux on a stretched grid.
177  RHS_a(i,j,k) -= Fact * dz_inv * (rhoAlpha_hi * gam_hi - rhoAlpha_lo * gam_lo);
178  }
179 
180  RHS_a(i,j,k) = (RHS_a(i,j,k) - a_tmp * RHS_a(i,j,k-1)) * inv_b2_tmp; // NOTE: This is now "rho"
181  coeffG_a(i,j,k) = c_tmp * inv_b2_tmp; // NOTE: this is now "gamma"
182  } // k
183 
184  // Top boundary coefficients and RHS for L decomp
185  //===================================================
186  {
187  getRhoAlpha(i, j, khi, rhoAlpha_lo, rhoAlpha_hi,
188  cell_data, mu_turb, d_alpha_eff, d_eddy_diff_idz,
189  prim_index, prim_scal_index, l_consA, l_turb);
190 
191  dz_inv = one / dz_ptr[khi];
192  dz_inv_lo = two / (dz_ptr[khi] + dz_ptr[khi-1]);
193  dz_inv_hi = dz_inv;
194 
195  a_tmp = -Fact * rhoAlpha_lo * dz_inv_lo * dz_inv;
196  c_tmp = zero;
197  b_tmp = cell_data(i,j,khi,Rho_comp) - a_tmp - c_tmp;
198  inv_b2_tmp = one / (b_tmp - a_tmp * coeffG_a(i,j,khi-1));
199 
200  RHS_a(i,j,khi) = cell_data(i,j,khi,n); // NOTE: this is rho*phi; solution is phi
201  if (neumann_on_zhi) {
202  RHS_a(i,j,khi) -= -Fact * dz_inv * rhoAlpha_hi * bc_neumann_vals[5]; // NOTE: N_val = d_z(\phi)
203  }
204 
205  // First solve
206  soln_a(i,j,khi) = (RHS_a(i,j,khi) - a_tmp * RHS_a(i,j,khi-1)) * inv_b2_tmp;
207  }
208 
209  // Back sweep the U decomp solution
210  //===================================================
211  for (int k(khi-1); k>=klo; --k) {
212  soln_a(i,j,k) = RHS_a(i,j,k) - coeffG_a(i,j,k) * soln_a(i,j,k+1);
213  }
214 
215  // Convert back to rho*theta
216  //===================================================
217  for (int k(klo); k<=khi; ++k) {
218  cell_data(i,j,k,n) = cell_data(i,j,k,Rho_comp) * soln_a(i,j,k);
219  }
220 
221 #ifdef AMREX_USE_GPU
222  });
223 #else
224  } // i
225  } // j
226 #endif
227 }
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void getRhoAlpha(int i, int j, int k, amrex::Real &rhoAlpha_lo, amrex::Real &rhoAlpha_hi, const amrex::Array4< const amrex::Real > &cell_data, const amrex::Array4< const amrex::Real > &mu_turb, const amrex::Real *d_alpha_eff, const int *d_eddy_diff_idz, int prim_index, int prim_scal_index, bool l_consA, bool l_turb)
Definition: ERF_GetRhoAlpha.H:20
#define RhoScalar_comp
Definition: ERF_IndexDefines.H:43
#define RhoTheta_comp
Definition: ERF_IndexDefines.H:40
#define NSCALARS
Definition: ERF_IndexDefines.H:16
#define RhoQ1_comp
Definition: ERF_IndexDefines.H:45
#define PrimScalar_comp
Definition: ERF_IndexDefines.H:60
@ neumann
Definition: ERF_IndexDefines.H:297
@ HGAMQ_v
Definition: ERF_IndexDefines.H:261
@ HGAMT_v
Definition: ERF_IndexDefines.H:260
Here is the call graph for this function: