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

Macros

#define INSTANTIATE_IMPLICIT_DIFF_FOR_MOM_LU(STAGDIR)
 

Functions

void ImplicitDiffForStateLU_T (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 Array4< const Real > &z_nd, const Array4< const Real > &detJ, const GpuArray< Real, AMREX_SPACEDIM > &cellSizeInv, 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_T (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 Array4< const Real > &z_nd, const Array4< const Real > &detJ, const GpuArray< Real, AMREX_SPACEDIM > &cellSizeInv, 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_T<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 Array4<const Real>&, \
const Array4<const Real>&, \
const GpuArray<Real, AMREX_SPACEDIM>&, \
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:241

Function Documentation

◆ ImplicitDiffForMomLU_T()

template<int stagdir>
void ImplicitDiffForMomLU_T ( 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 Array4< const Real > &  z_nd,
const Array4< const Real > &  detJ,
const GpuArray< Real, AMREX_SPACEDIM > &  cellSizeInv,
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, over terrain.

This function (explicitly instantiated below) handles staggering in x, y, or z through the template parameter, stagdir. NOTE: implicit diffusion of w has remains an experimental feature and has not been tested yet with terrain.

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

◆ ImplicitDiffForStateLU_T()

void ImplicitDiffForStateLU_T ( 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 Array4< const Real > &  z_nd,
const Array4< const Real > &  detJ,
const GpuArray< Real, AMREX_SPACEDIM > &  cellSizeInv,
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 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]z_ndnodal array of z
[in]detJJacobian determinant
[in]cellSizeInvinverse cell size array
[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
49 {
50  BL_PROFILE_VAR("ImplicitDiffForState_T()",ImplicitDiffForState_T);
51 
52  Real dt = static_cast<Real>(dt_d);
53 
54  // setup quantities for getRhoAlpha()
55 #include "ERF_SetupVertDiff.H"
56  const int qty_index = n;
57  const int prim_index = qty_index - 1;
58  const int prim_scal_index = (qty_index >= RhoScalar_comp && qty_index < RhoScalar_comp+NSCALARS) ? PrimScalar_comp : prim_index;
59 
60  // Box bounds
61  int ilo = bx.smallEnd(0);
62  int ihi = bx.bigEnd(0);
63  int jlo = bx.smallEnd(1);
64  int jhi = bx.bigEnd(1);
65  int klo = bx.smallEnd(2);
66  int khi = bx.bigEnd(2);
67  amrex::ignore_unused(ilo, ihi, jlo, jhi);
68 
69  // Temporary FABs for tridiagonal solve (allocated on column)
70  // A[k] * x[k-1] + B[k] * x[k] + C[k+1] = RHS[k]
71 
72  // With LU decomposition, M * x = r is written as L * U * x = r with U * x = rho
73  // We then first have L * rho = r and U * x = rho
74  amrex::FArrayBox RHS_fab, soln_fab, coeffG_fab;
75  RHS_fab.resize(bx,1, amrex::The_Async_Arena());
76  soln_fab.resize(bx,1, amrex::The_Async_Arena());
77  coeffG_fab.resize(bx,1, amrex::The_Async_Arena());
78  auto const& RHS_a = RHS_fab.array();
79  auto const& soln_a = soln_fab.array();
80  auto const& coeffG_a = coeffG_fab.array();
81 
82  Real dz_inv = cellSizeInv[2];
83 
84  int bc_comp = qty_index;
85  bool foextrap_on_zlo = (bc_ptr[bc_comp].lo(2) == ERFBCType::foextrap);
86  bool foextrap_on_zhi = (bc_ptr[bc_comp].hi(2) == ERFBCType::foextrap);
87  bool neumann_on_zlo = (bc_ptr[bc_comp].lo(2) == ERFBCType::neumann);
88  bool neumann_on_zhi = (bc_ptr[bc_comp].hi(2) == ERFBCType::neumann);
89  amrex::ignore_unused(foextrap_on_zlo, foextrap_on_zhi);
90 
91  AMREX_ASSERT_WITH_MESSAGE(foextrap_on_zlo || neumann_on_zlo || use_SurfLayer,
92  "Unexpected lower BC for scalars used with implicit vertical diffusion");
93  AMREX_ASSERT_WITH_MESSAGE(foextrap_on_zhi || neumann_on_zhi,
94  "Unexpected upper BC for scalars used with implicit vertical diffusion");
95 
96  Real Fact = implicit_fac * dt * dz_inv;
97 
98 #ifdef AMREX_USE_GPU
99  ParallelFor(makeSlab(bx,2,0), [=] AMREX_GPU_DEVICE (int i, int j, int)
100  {
101 #else
102  for (int j(jlo); j<=jhi; ++j) {
103  for (int i(ilo); i<=ihi; ++i) {
104 #endif
105  // Notes: The detJ for divergence was multiplied through.
106  // Therefore, it doesn't show up in the A/B denominator,
107  // but it does modify B and the RHS.
108 
109  // Bottom boundary coefficients and RHS for L decomp
110  //===================================================
111  Real rhoAlpha_lo, rhoAlpha_hi;
112  Real met_h_zeta_lo, met_h_zeta_hi;
113  Real a_tmp, b_tmp, c_tmp, inv_b2_tmp;
114  {
115  getRhoAlpha(i, j, klo, rhoAlpha_lo, rhoAlpha_hi,
116  cell_data, mu_turb, d_alpha_eff, d_eddy_diff_idz,
117  prim_index, prim_scal_index, l_consA, l_turb);
118 
119  met_h_zeta_hi = Compute_h_zeta_AtKface(i,j,klo+1,cellSizeInv,z_nd);
120 
121  a_tmp = zero;
122  c_tmp = -Fact * rhoAlpha_hi * dz_inv / met_h_zeta_hi;
123  b_tmp = detJ(i,j,klo) * cell_data(i,j,klo,Rho_comp) - a_tmp - c_tmp;
124  inv_b2_tmp = one;
125 
126  RHS_a(i,j,klo) = detJ(i,j,klo) * cell_data(i,j,klo,n); // NOTE: this is rho*phi; solution is phi
127  if (use_SurfLayer && scalar_zflux) {
128  RHS_a(i,j,klo) += Fact * scalar_zflux(i,j,klo); // NOTE: scalar_zflux = -K*d_z(\phi)
129  } else if (neumann_on_zlo) {
130  RHS_a(i,j,klo) += -Fact * rhoAlpha_lo * bc_neumann_vals[2]; // NOTE: N_val = d_z(\phi)
131  }
132 
133  // Add countergradient correction to RHS at bottom boundary
134  if (use_mrf_countergradient && (n == RhoTheta_comp || n == RhoQ1_comp)) {
135  const int gam_comp = (n == RhoTheta_comp) ? EddyDiff::HGAMT_v : EddyDiff::HGAMQ_v;
136  const Real gam_hi = myhalf * (mu_turb(i, j, klo, gam_comp) + mu_turb(i, j, klo+1, gam_comp));
137  RHS_a(i,j,klo) -= Fact * 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  met_h_zeta_lo = Compute_h_zeta_AtKface(i,j,k ,cellSizeInv,z_nd);
152  met_h_zeta_hi = Compute_h_zeta_AtKface(i,j,k+1,cellSizeInv,z_nd);
153 
154  a_tmp = -Fact * rhoAlpha_lo * dz_inv / met_h_zeta_lo;
155  c_tmp = -Fact * rhoAlpha_hi * dz_inv / met_h_zeta_hi;
156  b_tmp = detJ(i,j,k) * cell_data(i,j,k,Rho_comp) - a_tmp - c_tmp;
157  inv_b2_tmp = one / (b_tmp - a_tmp * coeffG_a(i,j,k-1));
158 
159  RHS_a(i,j,k) = detJ(i,j,k) * cell_data(i,j,k,n); // NOTE: this is rho*phi; solution is phi
160 
161  // Add countergradient correction to RHS in interior
162  if (use_mrf_countergradient && (n == RhoTheta_comp || n == RhoQ1_comp)) {
163  const int gam_comp = (n == RhoTheta_comp) ? EddyDiff::HGAMT_v : EddyDiff::HGAMQ_v;
164  const Real gam_k = mu_turb(i, j, k, gam_comp);
165  const Real gam_km1 = mu_turb(i, j, k-1, gam_comp);
166  const Real gam_kp1 = mu_turb(i, j, k+1, gam_comp);
167  const Real gam_hi = myhalf * (gam_k + gam_kp1); // at k+½
168  const Real gam_lo = myhalf * (gam_k + gam_km1); // at k-½
169  // Countergradient flux divergence (implicit contribution to RHS):
170  // -Fact * [ρα_{k+½}·γ_{k+½} - ρα_{k-½}·γ_{k-½}]
171  RHS_a(i,j,k) -= Fact * (rhoAlpha_hi * gam_hi - rhoAlpha_lo * gam_lo);
172  }
173 
174  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"
175  coeffG_a(i,j,k) = c_tmp * inv_b2_tmp; // NOTE: this is now "gamma"
176  } // k
177 
178  // Top boundary coefficients and RHS for L decomp
179  //===================================================
180  {
181  getRhoAlpha(i, j, khi, rhoAlpha_lo, rhoAlpha_hi,
182  cell_data, mu_turb, d_alpha_eff, d_eddy_diff_idz,
183  prim_index, prim_scal_index, l_consA, l_turb);
184 
185  // Lower-face metric shared with row khi-1.
186  met_h_zeta_lo = Compute_h_zeta_AtKface(i,j,khi ,cellSizeInv,z_nd);
187 
188  a_tmp = -Fact * rhoAlpha_lo * dz_inv / met_h_zeta_lo;
189  c_tmp = zero;
190  b_tmp = detJ(i,j,khi) * cell_data(i,j,khi,Rho_comp) - a_tmp - c_tmp;
191  inv_b2_tmp = one / (b_tmp - a_tmp * coeffG_a(i,j,khi-1));
192 
193  RHS_a(i,j,khi) = detJ(i,j,khi) * cell_data(i,j,khi,n); // NOTE: this is rho*phi; solution is phi
194  if (neumann_on_zhi) {
195  RHS_a(i,j,khi) -= -Fact * rhoAlpha_hi * bc_neumann_vals[5]; // NOTE: N_val = d_z(\phi)
196  }
197 
198  // First solve
199  soln_a(i,j,khi) = (RHS_a(i,j,khi) - a_tmp * RHS_a(i,j,khi-1)) * inv_b2_tmp;
200  }
201 
202  // Back sweep the U decomp solution
203  //===================================================
204  for (int k(khi-1); k>=klo; --k) {
205  soln_a(i,j,k) = RHS_a(i,j,k) - coeffG_a(i,j,k) * soln_a(i,j,k+1);
206  }
207 
208  // Convert back to rho*theta
209  //===================================================
210  for (int k(klo); k<=khi; ++k) {
211  cell_data(i,j,k,n) = cell_data(i,j,k,Rho_comp) * soln_a(i,j,k);
212  }
213 
214 #ifdef AMREX_USE_GPU
215  });
216 #else
217  } // i
218  } // j
219 #endif
220 }
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:40
#define RhoTheta_comp
Definition: ERF_IndexDefines.H:37
#define NSCALARS
Definition: ERF_IndexDefines.H:16
#define RhoQ1_comp
Definition: ERF_IndexDefines.H:42
#define PrimScalar_comp
Definition: ERF_IndexDefines.H:57
@ neumann
Definition: ERF_IndexDefines.H:254
@ HGAMQ_v
Definition: ERF_IndexDefines.H:218
@ HGAMT_v
Definition: ERF_IndexDefines.H:217
Here is the call graph for this function: