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 &domain, const int level, const double dt_d, const Array4< const int > &col_kext, 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 int >&, \
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:662

Function Documentation

◆ ImplicitDiffForMomLU_T()

template<int stagdir>
void ImplicitDiffForMomLU_T ( const Box &  bx,
const Box &  domain,
const int  level,
const double  dt_d,
const Array4< const int > &  col_kext,
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
321 {
322  BL_PROFILE_VAR("ImplicitDiffForMom_T()",ImplicitDiffForMom_T);
323 
324  Real dt = static_cast<Real>(dt_d);
325 
326  // setup quantities for getRhoAlphaAtFaces()
327  DiffChoice dc = solverChoice.diffChoice;
328  TurbChoice tc = solverChoice.turbChoice[level];
329  bool l_consA = (dc.molec_diff_type == MolecDiffType::ConstantAlpha);
330  bool l_turb = tc.use_kturb;
331  // The off-diagonal correction strains for u/v contain a factor of 1/2,
332  // while the diagonal correction strain for w does not.
333  constexpr Real molec_fac = (stagdir == 2) ? two : one;
334  Real mu_eff = (l_consA) ? molec_fac * dc.dynamic_viscosity / dc.rho0_trans
335  : molec_fac * dc.dynamic_viscosity;
336 
337  // g(S*) coefficient
338  // stagdir==0: tau_corr = myhalf * du/dz * mu_tot
339  // stagdir==1: tau_corr = myhalf * dv/dz * mu_tot
340  // stagdir==2: tau_corr = dw/dz * mu_tot
341  constexpr Real gfac = (stagdir == 2) ? two/three : one;
342 
343  // offsets used to average to faces
344  constexpr int ioff = (stagdir == 0) ? 1 : 0;
345  constexpr int joff = (stagdir == 1) ? 1 : 0;
346 
347  // Box bounds
348  int ilo = bx.smallEnd(0);
349  int ihi = bx.bigEnd(0);
350  int jlo = bx.smallEnd(1);
351  int jhi = bx.bigEnd(1);
352  int box_klo = bx.smallEnd(2);
353  int box_khi = bx.bigEnd(2);
354  amrex::ignore_unused(ilo, ihi, jlo, jhi);
355 
356  // Vertical staggering of this component, and the vertical extent of the domain for the
357  // faces we are solving on. A column whose top or bottom is not the domain boundary ends
358  // at a coarse/fine boundary instead, and must not be given a physical BC row.
359  constexpr int knodal = (stagdir == 2) ? 1 : 0;
360  const int dom_klo = domain.smallEnd(2);
361  const int dom_khi = domain.bigEnd(2) + knodal;
362 
363  // Temporary FABs for tridiagonal solve (allocated on column)
364  // A[k] * x[k-1] + B[k] * x[k] + C[k+1] = RHS[k]
365  amrex::FArrayBox RHS_fab, soln_fab, coeffG_fab;
366  RHS_fab.resize(bx,1, amrex::The_Async_Arena());
367  soln_fab.resize(bx,1, amrex::The_Async_Arena());
368  coeffG_fab.resize(bx,1, amrex::The_Async_Arena());
369  auto const& RHS_a = RHS_fab.array();
370  auto const& soln_a = soln_fab.array();
371  auto const& coeffG_a = coeffG_fab.array();
372 
373  Real dz_inv = cellSizeInv[2];
374 
375  int bc_comp = BCVars::xvel_bc + stagdir;
376  bool ext_dir_on_zlo = (bc_ptr[bc_comp].lo(2) == ERFBCType::ext_dir ||
377  bc_ptr[bc_comp].lo(2) == ERFBCType::ext_dir_prim);
378  bool ext_dir_on_zhi = (bc_ptr[bc_comp].hi(2) == ERFBCType::ext_dir ||
379  bc_ptr[bc_comp].hi(2) == ERFBCType::ext_dir_prim);
380  bool foextrap_on_zlo = (bc_ptr[bc_comp].lo(2) == ERFBCType::foextrap);
381  bool foextrap_on_zhi = (bc_ptr[bc_comp].hi(2) == ERFBCType::foextrap);
382  amrex::ignore_unused(foextrap_on_zlo,foextrap_on_zhi);
383 
384  AMREX_ASSERT_WITH_MESSAGE(foextrap_on_zlo || ext_dir_on_zlo || use_SurfLayer,
385  "Unexpected lower BC for momentum used with implicit vertical diffusion");
386  AMREX_ASSERT_WITH_MESSAGE(foextrap_on_zhi || ext_dir_on_zhi,
387  "Unexpected upper BC for momentum used with implicit vertical diffusion");
388 
389  Real Fact = implicit_fac * dt * dz_inv;
390 
391 #ifdef AMREX_USE_GPU
392  ParallelFor(makeSlab(bx,2,0), [=] AMREX_GPU_DEVICE (int i, int j, int)
393  {
394 #else
395  for (int j(jlo); j<=jhi; ++j) {
396  for (int i(ilo); i<=ihi; ++i) {
397 #endif
398 
399  // The face column at (i,j) is shared by the two cell columns on either side of it,
400  // and so by the box on either side of a grid seam. Solve only over the vertical
401  // range that both of those cell columns cover: the boxes on the two sides then
402  // build the same tridiagonal system and get the same answer, so the duplicated
403  // faces along the seam stay consistent. Faces outside that range sit on a lateral
404  // coarse/fine boundary and are set from the coarse level, so it is consistent to
405  // leave them to the explicit update.
406  const int klo = amrex::max(box_klo, col_kext(i-ioff,j-joff,0,0),
407  col_kext(i ,j ,0,0));
408  const int khi = amrex::min(box_khi, col_kext(i-ioff,j-joff,0,1)+knodal,
409  col_kext(i ,j ,0,1)+knodal);
410 
411  // Fewer than two faces in common -- nothing to invert, leave the column explicit
412 #ifdef AMREX_USE_GPU
413  if (khi <= klo) { return; }
414 #else
415  if (khi <= klo) { continue; }
416 #endif
417  // Notes:
418  //
419  // - In DiffusionSrcForMom (e.g., for x-mom)
420  //
421  // Real diffContrib = ...
422  // + (tau13(i,j,k+1) - tau13(i,j,k)) / dzinv
423  // rho_u_rhs(i,j,k) -= diffContrib; // note the negative sign
424  //
425  // - We need to scale the explicit _part_ of `tau13` (for x-mom) by (1 - implicit_fac)
426  // The part that needs to be scaled is stored in `tau_corr`.
427  // E.g., tau13 = myhalf * (du/dz + dw/dx)
428  // tau13_corr = myhalf * du/dz
429  //
430  // - The momentum (`face_data`) was set to `S_old + S_rhs * dt`
431  // prior to including "ERF_Implicit.H". Recall that S_rhs includes
432  // sources from advection and other forcings, not just diffusion.
433  //
434  // - To correct momentum, we need to subtract `implicit_fac * diffContrib_corr`
435  // from S_rhs to recover `(1 - implicit_fac) * diffContrib_corr`,
436  // where `diffContrib_corr = -d(tau_corr)/dz`. The negative sign
437  // comes from our convention for the RHS diffusion source.
438  //
439  // Subtracting a negative gives the += below; multiply by dt to
440  // get the intermediate momentum on the RHS of the tridiagonal
441  // system.
442  //
443  // - With a surface_layer BC, tau13/23 holds the vertical flux -d_z(k*u_i)
444  // directly. We must use tau at klo (not tau_corr) with SL BCs.
445  //
446  // - The detJ for divergence was multiplied through.
447  // Therefore, it doesn't show up in the A/B denominator,
448  // but it does modify B and the RHS.
449  //
450  // - Finally, the terms ~ RHS += (tau_corr_hi - tau_corr_lo) / dz (below)
451  // essentially undo the explicit diffusion update that will be
452  // handled here implicitly.
453 
454  // Bottom boundary coefficients and RHS for L decomp
455  //===================================================
456  Real rhoface, rhoAlpha_lo, rhoAlpha_hi;
457  Real detJface, met_h_zeta_lo, met_h_zeta_hi;
458  Real a_tmp, b_tmp, c_tmp, inv_b2_tmp;
459  {
460  detJface = myhalf * (detJ(i,j,klo) + detJ(i-ioff,j-joff,klo));
461  rhoface = myhalf * (cell_data(i,j,klo,Rho_comp) + cell_data(i-ioff,j-joff,klo,Rho_comp));
462  getRhoAlphaForFaces(i, j, klo, ioff, joff, rhoAlpha_lo, rhoAlpha_hi,
463  cell_data, mu_turb, mu_eff,
464  l_consA, l_turb);
465 
466  met_h_zeta_lo = myhalf * ( Compute_h_zeta_AtKface(i ,j ,klo ,cellSizeInv,z_nd)
467  + Compute_h_zeta_AtKface(i-ioff,j-joff,klo ,cellSizeInv,z_nd) );
468  met_h_zeta_hi = myhalf * ( Compute_h_zeta_AtKface(i ,j ,klo+1,cellSizeInv,z_nd)
469  + Compute_h_zeta_AtKface(i-ioff,j-joff,klo+1,cellSizeInv,z_nd) );
470 
471  a_tmp = zero;
472  c_tmp = -Fact * gfac * rhoAlpha_hi * dz_inv / met_h_zeta_hi;
473 
474  RHS_a(i,j,klo) = detJface * face_data(i,j,klo); // NOTE: this is momenta; solution is velocity
475 
476  // BCs: Dirichlet (u_i = val), slip wall (w = 0), or surface layer (w = 0) --
477  // but only where the bottom of this column really is the domain boundary
478  if (klo != dom_klo) {
479  // Coarse/fine (or grid) boundary: close the system with the known face value
480  // below instead of imposing a physical BC at an interior height
481  a_tmp = -Fact * rhoAlpha_lo * dz_inv / met_h_zeta_lo;
482  RHS_a(i,j,klo) += Fact * gfac * (tau_corr(i,j,klo+1) - tau_corr(i,j,klo));
483  const Real rho_below = myhalf * ( cell_data(i ,j ,klo-1,Rho_comp)
484  + cell_data(i-ioff,j-joff,klo-1,Rho_comp) );
485  RHS_a(i,j,klo) -= a_tmp * (face_data(i,j,klo-1) / rho_below);
486  } else if (ext_dir_on_zlo) {
487  RHS_a(i,j,klo) += Fact * gfac * (tau_corr(i,j,klo+1) - tau_corr(i,j,klo));
488  if (stagdir==2) {
489  c_tmp = zero;
490  RHS_a(i,j,klo) = zero;
491  } else {
492  // NOTE: wall is 1/2 dz away (2 dz_inv)
493  a_tmp = -two * Fact * rhoAlpha_lo * dz_inv / met_h_zeta_lo;
494  const Real rho_wall = myhalf * ( cell_data(i ,j ,klo-1,Rho_comp)
495  + cell_data(i-ioff,j-joff,klo-1,Rho_comp) );
496  const Real wall_velocity = face_data(i,j,klo-1) / rho_wall;
497  RHS_a(i,j,klo) -= a_tmp * wall_velocity;
498  }
499  } else if (use_SurfLayer) {
500  // NOTE: tau = -mu*d_z(u_i) w/ SL
501  RHS_a(i,j,klo) += Fact * gfac * (tau_corr(i,j,klo+1) - tau(i,j,klo));
502  RHS_a(i,j,klo) += Fact * tau(i,j,klo);
503  } else {
504  // NOTE: FOEXTRAP has zero lower flux (nothing to add to RHS)
505  RHS_a(i,j,klo) += Fact * gfac * (tau_corr(i,j,klo+1) - tau_corr(i,j,klo));
506  }
507 
508  // Add YSU momentum countergradient correction at bottom boundary.
509  // NOTE: As for the scalars, the lower face at klo carries no
510  // countergradient flux -- the surface stress is supplied by the
511  // surface layer model or the wall BC above. Only the upper face
512  // contributes here.
513  // NOTE: The sign matches the scalar path: tau_i3 = -rho*K*(du_i/dz - gamma_i),
514  // so the countergradient piece of the flux is +rho*K*gamma_i and its
515  // divergence enters the RHS with a minus sign.
516  if (use_ysu_mom_countergradient && stagdir < 2) {
517  const int hgam_comp = (stagdir == 0) ? EddyDiff::HGAMU_v : EddyDiff::HGAMV_v;
518  // Average HGAM* to the staggered face
519  const Real gam_klo = myhalf * (mu_turb(i,j,klo ,hgam_comp) + mu_turb(i-ioff,j-joff,klo ,hgam_comp));
520  const Real gam_kp1 = myhalf * (mu_turb(i,j,klo+1,hgam_comp) + mu_turb(i-ioff,j-joff,klo+1,hgam_comp));
521  const Real gam_hi = myhalf * (gam_klo + gam_kp1);
522  RHS_a(i,j,klo) -= Fact * rhoAlpha_hi * gam_hi;
523  }
524 
525  b_tmp = detJface * rhoface - a_tmp - c_tmp;
526  inv_b2_tmp = one;
527 
528  RHS_a(i,j,klo) /= b_tmp; // NOTE: this is now "rho"
529  coeffG_a(i,j,klo) = c_tmp / b_tmp; // NOTE: this is now "gamma"
530  }
531 
532  // Build the coefficients and RHS for L decomp
533  //===================================================
534  for (int k(klo+1); k < khi; k++) {
535  detJface = myhalf * (detJ(i,j,k) + detJ(i-ioff,j-joff,k));
536  rhoface = myhalf * (cell_data(i,j,k,Rho_comp) + cell_data(i-ioff,j-joff,k,Rho_comp));
537  getRhoAlphaForFaces(i, j, k, ioff, joff, rhoAlpha_lo, rhoAlpha_hi,
538  cell_data, mu_turb, mu_eff,
539  l_consA, l_turb);
540 
541  met_h_zeta_lo = myhalf * ( Compute_h_zeta_AtKface(i ,j ,k ,cellSizeInv,z_nd)
542  + Compute_h_zeta_AtKface(i-ioff,j-joff,k ,cellSizeInv,z_nd) );
543  met_h_zeta_hi = myhalf * ( Compute_h_zeta_AtKface(i ,j ,k+1,cellSizeInv,z_nd)
544  + Compute_h_zeta_AtKface(i-ioff,j-joff,k+1,cellSizeInv,z_nd) );
545 
546  a_tmp = -Fact * rhoAlpha_lo * dz_inv / met_h_zeta_lo;
547  c_tmp = -Fact * rhoAlpha_hi * dz_inv / met_h_zeta_hi;
548  b_tmp = detJface * rhoface - a_tmp - c_tmp;
549  inv_b2_tmp = one / (b_tmp - a_tmp * coeffG_a(i,j,k-1));
550 
551  RHS_a(i,j,k) = detJface * face_data(i,j,k); // NOTE: this is momenta; solution is velocity
552  RHS_a(i,j,k) += Fact * gfac * (tau_corr(i,j,k+1) - tau_corr(i,j,k));
553 
554  // Add YSU momentum countergradient correction
555  if (use_ysu_mom_countergradient && stagdir < 2) {
556  const int hgam_comp = (stagdir == 0) ? EddyDiff::HGAMU_v : EddyDiff::HGAMV_v;
557  // Average HGAM* to the staggered face
558  const Real gam_k = myhalf * (mu_turb(i,j,k ,hgam_comp) + mu_turb(i-ioff,j-joff,k ,hgam_comp));
559  const Real gam_km1 = myhalf * (mu_turb(i,j,k-1,hgam_comp) + mu_turb(i-ioff,j-joff,k-1,hgam_comp));
560  const Real gam_kp1 = myhalf * (mu_turb(i,j,k+1,hgam_comp) + mu_turb(i-ioff,j-joff,k+1,hgam_comp));
561  const Real gam_hi = myhalf * (gam_k + gam_kp1); // at k+1/2
562  const Real gam_lo = myhalf * (gam_k + gam_km1); // at k-1/2
563  RHS_a(i,j,k) -= Fact * (rhoAlpha_hi * gam_hi - rhoAlpha_lo * gam_lo);
564  }
565 
566  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"
567  coeffG_a(i,j,k) = c_tmp * inv_b2_tmp; // NOTE: this is now "gamma"
568  } // k
569 
570  // Top boundary coefficients and RHS for L decomp
571  //===================================================
572  {
573  detJface = myhalf * (detJ(i,j,khi) + detJ(i-ioff,j-joff,khi));
574  rhoface = myhalf * (cell_data(i,j,khi,Rho_comp) + cell_data(i-ioff,j-joff,khi,Rho_comp));
575  getRhoAlphaForFaces(i, j, khi, ioff, joff, rhoAlpha_lo, rhoAlpha_hi,
576  cell_data, mu_turb, mu_eff,
577  l_consA, l_turb);
578 
579  met_h_zeta_lo = myhalf * ( Compute_h_zeta_AtKface(i ,j ,khi ,cellSizeInv,z_nd)
580  + Compute_h_zeta_AtKface(i-ioff,j-joff,khi ,cellSizeInv,z_nd) );
581  met_h_zeta_hi = myhalf * ( Compute_h_zeta_AtKface(i ,j ,khi+1,cellSizeInv,z_nd)
582  + Compute_h_zeta_AtKface(i-ioff,j-joff,khi+1,cellSizeInv,z_nd) );
583 
584  a_tmp = -Fact * gfac * rhoAlpha_lo * dz_inv / met_h_zeta_lo;
585  c_tmp = zero;
586 
587  RHS_a(i,j,khi) = detJface * face_data(i,j,khi); // NOTE: this is momenta; solution is velocity
588  RHS_a(i,j,khi) += Fact * gfac * (tau_corr(i,j,khi+1) - tau_corr(i,j,khi));
589 
590  // BCs: Dirichlet (u_i = val), slip wall (w = 0) --
591  // but only where the top of this column really is the domain boundary
592  if (khi != dom_khi) {
593  // Coarse/fine (or grid) boundary: close the system with the known face value
594  // above instead of imposing a physical BC at an interior height
595  c_tmp = -Fact * rhoAlpha_hi * dz_inv / met_h_zeta_hi;
596  const Real rho_above = myhalf * ( cell_data(i ,j ,khi+1,Rho_comp)
597  + cell_data(i-ioff,j-joff,khi+1,Rho_comp) );
598  RHS_a(i,j,khi) -= c_tmp * (face_data(i,j,khi+1) / rho_above);
599  } else if (ext_dir_on_zhi) {
600  if (stagdir==2) {
601  a_tmp = zero;
602  RHS_a(i,j,khi) = zero;
603  } else {
604  // NOTE: wall is 1/2 dz away (2 dz_inv)
605  c_tmp = -two * Fact * rhoAlpha_hi * dz_inv / met_h_zeta_hi;
606  const Real rho_wall = myhalf * ( cell_data(i ,j ,khi+1,Rho_comp)
607  + cell_data(i-ioff,j-joff,khi+1,Rho_comp) );
608  const Real wall_velocity = face_data(i,j,khi+1) / rho_wall;
609  RHS_a(i,j,khi) -= c_tmp * wall_velocity;
610  }
611  }
612 
613  b_tmp = detJface * rhoface - a_tmp - c_tmp;
614  inv_b2_tmp = one / (b_tmp - a_tmp * coeffG_a(i,j,khi-1));
615 
616  // First solve
617  soln_a(i,j,khi) = (RHS_a(i,j,khi) - a_tmp * RHS_a(i,j,khi-1)) * inv_b2_tmp;
618  }
619 
620  // Back sweep the U decomp solution
621  //===================================================
622  for (int k(khi-1); k>=klo; --k) {
623  soln_a(i,j,k) = RHS_a(i,j,k) - coeffG_a(i,j,k) * soln_a(i,j,k+1);
624  }
625 
626  // Convert back to momenta
627  //===================================================
628  for (int k(klo); k<=khi; ++k) {
629  rhoface = myhalf * (cell_data(i,j,k,Rho_comp) + cell_data(i-ioff,j-joff,k,Rho_comp));
630  face_data(i,j,k) = rhoface * soln_a(i,j,k);
631  }
632 
633 #ifdef AMREX_USE_GPU
634  });
635 #else
636  } // i
637  } // j
638 #endif
639 }
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 klo
Definition: ERF_InitCustomPert_ABL.H:75
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);})
constexpr amrex::Real three
Definition: ERF_NumericalConstants.H:32
constexpr amrex::Real two
Definition: ERF_NumericalConstants.H:31
constexpr amrex::Real one
Definition: ERF_NumericalConstants.H:30
constexpr amrex::Real zero
Definition: ERF_NumericalConstants.H:29
constexpr amrex::Real myhalf
Definition: ERF_NumericalConstants.H:34
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:409
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:296
@ ext_dir
Definition: ERF_IndexDefines.H:297
@ ext_dir_prim
Definition: ERF_IndexDefines.H:300
@ 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:1974
DiffChoice diffChoice
Diffusion-related options.
Definition: ERF_DataStruct.H:1971
Definition: ERF_TurbStruct.H:115
bool use_kturb
Whether any turbulence model is active.
Definition: ERF_TurbStruct.H:835
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  // k-eqn RANS with a Dirichlet wall value of k: the first cell is held at
97  // the value set by the surface layer, so its row reduces to x(klo) = phi(klo)
98  // and the row above sees it as a Dirichlet neighbour.
99  // NOTE: init_bcs requires zlo.type = surface_layer whenever dirichlet_k is set,
100  // so this matches the l_dirichlet_k pin in erf_slow_rhs_post, which also
101  // requires a surface layer. Do not relax one without the other.
102  const bool pin_klo = (qty_index == RhoKE_comp) && (klo == domain.smallEnd(2)) &&
103  (solverChoice.turbChoice[level].rans_type == RANSType::kEqn) &&
104  solverChoice.turbChoice[level].dirichlet_k;
105 
106  // A column whose bottom or top is not the domain boundary ends at a coarse/fine boundary
107  // instead. There the system must be closed with the value on the other side of that
108  // boundary rather than given a physical BC row at an interior height, so that the answer
109  // does not depend on where the grid happens to end in the vertical.
110  const bool at_zlo = (klo == domain.smallEnd(2));
111  const bool at_zhi = (khi == domain.bigEnd(2));
112 
113  Real Fact = implicit_fac * dt * dz_inv;
114 
115 #ifdef AMREX_USE_GPU
116  ParallelFor(makeSlab(bx,2,0), [=] AMREX_GPU_DEVICE (int i, int j, int)
117  {
118 #else
119  for (int j(jlo); j<=jhi; ++j) {
120  for (int i(ilo); i<=ihi; ++i) {
121 #endif
122  // Notes: The detJ for divergence was multiplied through.
123  // Therefore, it doesn't show up in the A/B denominator,
124  // but it does modify B and the RHS.
125 
126  // Bottom boundary coefficients and RHS for L decomp
127  //===================================================
128  Real rhoAlpha_lo, rhoAlpha_hi;
129  Real met_h_zeta_lo, met_h_zeta_hi;
130  Real a_tmp, b_tmp, c_tmp, inv_b2_tmp;
131  {
132  getRhoAlpha(i, j, klo, rhoAlpha_lo, rhoAlpha_hi,
133  cell_data, mu_turb, d_alpha_eff, d_eddy_diff_idz,
134  prim_index, prim_scal_index, l_consA, l_turb);
135 
136  met_h_zeta_hi = Compute_h_zeta_AtKface(i,j,klo+1,cellSizeInv,z_nd);
137 
138  a_tmp = zero;
139  if (!at_zlo) {
140  met_h_zeta_lo = Compute_h_zeta_AtKface(i,j,klo,cellSizeInv,z_nd);
141  a_tmp = -Fact * rhoAlpha_lo * dz_inv / met_h_zeta_lo;
142  }
143  c_tmp = (pin_klo) ? zero : -Fact * rhoAlpha_hi * dz_inv / met_h_zeta_hi;
144  b_tmp = detJ(i,j,klo) * cell_data(i,j,klo,Rho_comp) - a_tmp - c_tmp;
145  inv_b2_tmp = one;
146 
147  RHS_a(i,j,klo) = detJ(i,j,klo) * cell_data(i,j,klo,n); // NOTE: this is rho*phi; solution is phi
148  if (!at_zlo) {
149  // Coarse/fine boundary: close the system with the known value below
150  RHS_a(i,j,klo) -= a_tmp * (cell_data(i,j,klo-1,n) / cell_data(i,j,klo-1,Rho_comp));
151  } else if (pin_klo) {
152  // Dirichlet row: no flux terms
153  } else if (use_SurfLayer && scalar_zflux) {
154  RHS_a(i,j,klo) += Fact * scalar_zflux(i,j,klo); // NOTE: scalar_zflux = -K*d_z(\phi)
155  } else if (neumann_on_zlo) {
156  RHS_a(i,j,klo) += -Fact * rhoAlpha_lo * bc_neumann_vals[2]; // NOTE: N_val = d_z(\phi)
157  }
158 
159  // Add countergradient correction to RHS at bottom boundary.
160  // NOTE: The lower face at klo carries no countergradient flux -- the
161  // total surface flux is supplied by the surface layer model or the
162  // Neumann BC, while gamma represents nonlocal transport interior to
163  // the PBL. Only the upper face contributes here.
164  if (use_mrf_countergradient && (n == RhoTheta_comp || n == RhoQ1_comp)) {
165  const int gam_comp = (n == RhoTheta_comp) ? EddyDiff::HGAMT_v : EddyDiff::HGAMQ_v;
166  const Real gam_hi = myhalf * (mu_turb(i, j, klo, gam_comp) + mu_turb(i, j, klo+1, gam_comp));
167  RHS_a(i,j,klo) -= Fact * rhoAlpha_hi * gam_hi;
168  if (!at_zlo) {
169  const Real gam_lo = myhalf * (mu_turb(i, j, klo, gam_comp) + mu_turb(i, j, klo-1, gam_comp));
170  RHS_a(i,j,klo) += Fact * rhoAlpha_lo * gam_lo;
171  }
172  }
173 
174  RHS_a(i,j,klo) /= b_tmp; // NOTE: this is now "rho"
175  coeffG_a(i,j,klo) = c_tmp / b_tmp; // NOTE: this is now "gamma"
176  }
177 
178  // Build the coefficients and RHS for L decomp
179  //===================================================
180  for (int k(klo+1); k < khi; k++) {
181  getRhoAlpha(i, j, k, 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  met_h_zeta_lo = Compute_h_zeta_AtKface(i,j,k ,cellSizeInv,z_nd);
186  met_h_zeta_hi = Compute_h_zeta_AtKface(i,j,k+1,cellSizeInv,z_nd);
187 
188  a_tmp = -Fact * rhoAlpha_lo * dz_inv / met_h_zeta_lo;
189  c_tmp = -Fact * rhoAlpha_hi * dz_inv / met_h_zeta_hi;
190  b_tmp = detJ(i,j,k) * cell_data(i,j,k,Rho_comp) - a_tmp - c_tmp;
191  inv_b2_tmp = one / (b_tmp - a_tmp * coeffG_a(i,j,k-1));
192 
193  RHS_a(i,j,k) = detJ(i,j,k) * cell_data(i,j,k,n); // NOTE: this is rho*phi; solution is phi
194 
195  // Add countergradient correction to RHS in interior
196  if (use_mrf_countergradient && (n == RhoTheta_comp || n == RhoQ1_comp)) {
197  const int gam_comp = (n == RhoTheta_comp) ? EddyDiff::HGAMT_v : EddyDiff::HGAMQ_v;
198  const Real gam_k = mu_turb(i, j, k, gam_comp);
199  const Real gam_km1 = mu_turb(i, j, k-1, gam_comp);
200  const Real gam_kp1 = mu_turb(i, j, k+1, gam_comp);
201  const Real gam_hi = myhalf * (gam_k + gam_kp1); // at k+½
202  const Real gam_lo = myhalf * (gam_k + gam_km1); // at k-½
203  // Countergradient flux divergence (implicit contribution to RHS):
204  // -Fact * [ρα_{k+½}·γ_{k+½} - ρα_{k-½}·γ_{k-½}]
205  // NOTE: no 1/met_h_zeta here. met_h_zeta appears in a_tmp/c_tmp only
206  // because the diffusive flux contains an inner vertical
207  // derivative d(phi)/dz ~ dz_inv/h_zeta * delta_phi. The
208  // countergradient piece contains no derivative, so it takes only
209  // the outer dz_inv already carried in Fact.
210  RHS_a(i,j,k) -= Fact * (rhoAlpha_hi * gam_hi - rhoAlpha_lo * gam_lo);
211  }
212 
213  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"
214  coeffG_a(i,j,k) = c_tmp * inv_b2_tmp; // NOTE: this is now "gamma"
215  } // k
216 
217  // Top boundary coefficients and RHS for L decomp
218  //===================================================
219  {
220  getRhoAlpha(i, j, khi, rhoAlpha_lo, rhoAlpha_hi,
221  cell_data, mu_turb, d_alpha_eff, d_eddy_diff_idz,
222  prim_index, prim_scal_index, l_consA, l_turb);
223 
224  // Lower-face metric shared with row khi-1.
225  met_h_zeta_lo = Compute_h_zeta_AtKface(i,j,khi ,cellSizeInv,z_nd);
226 
227  a_tmp = -Fact * rhoAlpha_lo * dz_inv / met_h_zeta_lo;
228  c_tmp = zero;
229  if (!at_zhi) {
230  met_h_zeta_hi = Compute_h_zeta_AtKface(i,j,khi+1,cellSizeInv,z_nd);
231  c_tmp = -Fact * rhoAlpha_hi * dz_inv / met_h_zeta_hi;
232  }
233  b_tmp = detJ(i,j,khi) * cell_data(i,j,khi,Rho_comp) - a_tmp - c_tmp;
234  inv_b2_tmp = one / (b_tmp - a_tmp * coeffG_a(i,j,khi-1));
235 
236  RHS_a(i,j,khi) = detJ(i,j,khi) * cell_data(i,j,khi,n); // NOTE: this is rho*phi; solution is phi
237  if (!at_zhi) {
238  // Coarse/fine boundary: close the system with the known value above. The
239  // countergradient flux then passes through both faces, as it does in the
240  // interior, rather than only through the lower one.
241  RHS_a(i,j,khi) -= c_tmp * (cell_data(i,j,khi+1,n) / cell_data(i,j,khi+1,Rho_comp));
242  if (use_mrf_countergradient && (n == RhoTheta_comp || n == RhoQ1_comp)) {
243  const int gam_comp = (n == RhoTheta_comp) ? EddyDiff::HGAMT_v : EddyDiff::HGAMQ_v;
244  const Real gam_hi = myhalf * (mu_turb(i, j, khi, gam_comp) + mu_turb(i, j, khi+1, gam_comp));
245  const Real gam_lo = myhalf * (mu_turb(i, j, khi, gam_comp) + mu_turb(i, j, khi-1, gam_comp));
246  RHS_a(i,j,khi) -= Fact * (rhoAlpha_hi * gam_hi - rhoAlpha_lo * gam_lo);
247  }
248  } else if (neumann_on_zhi) {
249  RHS_a(i,j,khi) -= -Fact * rhoAlpha_hi * bc_neumann_vals[5]; // NOTE: N_val = d_z(\phi)
250  }
251 
252  // First solve
253  soln_a(i,j,khi) = (RHS_a(i,j,khi) - a_tmp * RHS_a(i,j,khi-1)) * inv_b2_tmp;
254  }
255 
256  // Back sweep the U decomp solution
257  //===================================================
258  for (int k(khi-1); k>=klo; --k) {
259  soln_a(i,j,k) = RHS_a(i,j,k) - coeffG_a(i,j,k) * soln_a(i,j,k+1);
260  }
261 
262  // Convert back to rho*theta
263  //===================================================
264  for (int k(klo); k<=khi; ++k) {
265  cell_data(i,j,k,n) = cell_data(i,j,k,Rho_comp) * soln_a(i,j,k);
266  }
267 
268 #ifdef AMREX_USE_GPU
269  });
270 #else
271  } // i
272  } // j
273 #endif
274 }
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
#define RhoKE_comp
Definition: ERF_IndexDefines.H:41
@ neumann
Definition: ERF_IndexDefines.H:302
@ HGAMQ_v
Definition: ERF_IndexDefines.H:261
@ HGAMT_v
Definition: ERF_IndexDefines.H:260
Here is the call graph for this function: