ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_MakeFastCoeffs.cpp File Reference
#include <AMReX.H>
#include <ERF_TI_fast_headers.H>
Include dependency graph for ERF_MakeFastCoeffs.cpp:

Functions

void make_fast_coeffs (int, MultiFab &fast_coeffs, Vector< MultiFab > &S_stage_data, const MultiFab &S_stage_prim, const MultiFab &pi_stage, const amrex::Geometry geom, bool l_use_moisture, MeshType mesh_type, Real gravity, Real c_p, std::unique_ptr< MultiFab > &detJ_cc, const double dtau, Real beta_s, amrex::GpuArray< ERF_BC, AMREX_SPACEDIM *2 > &phys_bc_type)
 

Function Documentation

◆ make_fast_coeffs()

void make_fast_coeffs ( int  ,
MultiFab &  fast_coeffs,
Vector< MultiFab > &  S_stage_data,
const MultiFab &  S_stage_prim,
const MultiFab &  pi_stage,
const amrex::Geometry  geom,
bool  l_use_moisture,
MeshType  mesh_type,
Real  gravity,
Real  c_p,
std::unique_ptr< MultiFab > &  detJ_cc,
const double  dtau,
Real  beta_s,
amrex::GpuArray< ERF_BC, AMREX_SPACEDIM *2 > &  phys_bc_type 
)

Function for computing the coefficients for the tridiagonal solver used in the fast integrator (the acoustic substepping).

Parameters
[in]levellevel of refinement
[out]fast_coeffsthe coefficients for the tridiagonal solver computed here
[in]S_stage_datasolution at the last stage
[in]S_stage_primprimitive variables (i.e. conserved variables divided by density) at the last stage
[in]pi_stageExner function at the last stage
[in]geomContainer for geometric information
[in]l_use_moistureAre we evolving moisture?
[in]mesh_typeDo we have constant dz?
[in]gravityMagnitude of gravity
[in]c_pCoefficient at constant pressure
[in]detJ_ccJacobian of the metric transformation at cell centers
[in]dtauFast time step
[in]beta_sCoefficient which determines how implicit vs explicit the solve is
[in]phys_bc_typePhysical boundary condition types
40 {
41  BL_PROFILE_VAR("make_fast_coeffs()",make_fast_coeffs);
42 
43  Real beta_2 = myhalf * (one + beta_s); // multiplies implicit terms
44 
45  Real c_v = c_p - R_d;
46 
47  const GpuArray<Real, AMREX_SPACEDIM> dxInv = geom.InvCellSizeArray();
48  Real dzi = dxInv[2];
49 
50  const Box &domain = geom.Domain();
51 
52  MultiFab coeff_A_mf(fast_coeffs, amrex::make_alias, 0, 1);
53  MultiFab coeff_B_mf(fast_coeffs, amrex::make_alias, 1, 1);
54  MultiFab coeff_C_mf(fast_coeffs, amrex::make_alias, 2, 1);
55  MultiFab coeff_P_mf(fast_coeffs, amrex::make_alias, 3, 1);
56  MultiFab coeff_Q_mf(fast_coeffs, amrex::make_alias, 4, 1);
57 
58 
59  // *************************************************************************
60  // Set gravity as a vector
61  const Array<Real,AMREX_SPACEDIM> grav{zero, zero, -gravity};
62  const GpuArray<Real,AMREX_SPACEDIM> grav_gpu{grav[0], grav[1], grav[2]};
63 
64  // *************************************************************************
65  // Define updates in the current RK stage
66  // *************************************************************************
67 #ifdef _OPENMP
68 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
69 #endif
70  {
71 
72  for ( MFIter mfi(S_stage_data[IntVars::cons],TileNoZ()); mfi.isValid(); ++mfi)
73  {
74  Box bx = mfi.tilebox();
75  Box tbz = surroundingNodes(bx,2);
76 
77  const Array4<const Real> & stage_cons = S_stage_data[IntVars::cons].const_array(mfi);
78  const Array4<const Real> & prim = S_stage_prim.const_array(mfi);
79 
80  const Array4<const Real>& detJ = (mesh_type != MeshType::ConstantDz) ?
81  detJ_cc->const_array(mfi) : Array4<const Real>{};
82 
83  const Array4<const Real>& pi_stage_ca = pi_stage.const_array(mfi);
84 
85  FArrayBox gam_fab; gam_fab.resize(surroundingNodes(bx,2),1,The_Async_Arena());
86 
87  auto const& coeffA_a = coeff_A_mf.array(mfi);
88  auto const& coeffB_a = coeff_B_mf.array(mfi);
89  auto const& coeffC_a = coeff_C_mf.array(mfi);
90  auto const& coeffP_a = coeff_P_mf.array(mfi);
91  auto const& coeffQ_a = coeff_Q_mf.array(mfi);
92  auto const& gam_a = gam_fab.array();
93 
94  // *********************************************************************
95  // *********************************************************************
96  // *********************************************************************
97 
98  Box bx_shrunk_in_k = bx;
99  int klo = tbz.smallEnd(2);
100  int khi = tbz.bigEnd(2);
101  bx_shrunk_in_k.setSmall(2,klo+1);
102  bx_shrunk_in_k.setBig(2,khi-1);
103 
104  // Note that the notes use "g" to mean the magnitude of gravity, so it is positive
105  // We set grav_gpu[2] to be the vector component which is negative
106  // We define halfg to match the notes (which is why we take the absolute value)
107  Real halfg = std::abs(myhalf * grav_gpu[2]);
108 
109  //Note we don't act on the bottom or top boundaries of the domain
110  if (mesh_type != MeshType::ConstantDz)
111  {
112  ParallelFor(bx_shrunk_in_k, [=] AMREX_GPU_DEVICE (int i, int j, int k)
113  {
114  Real pi_c = myhalf * (pi_stage_ca(i,j,k-1) + pi_stage_ca(i,j,k));
115 
116  Real detJ_on_kface = myhalf * (detJ(i,j,k) + detJ(i,j,k-1));
117  Real inv_detJ_on_kface = one / detJ_on_kface;
118 
119  Real qv_p = (l_use_moisture) ? prim(i,j,k ,PrimQ1_comp) : zero;
120  Real qv_q = (l_use_moisture) ? prim(i,j,k-1,PrimQ1_comp) : zero;
121 
122  Real Thm_hi = stage_cons(i,j,k ,RhoTheta_comp) * (one + RvoRd*qv_p);
123  Real Thm_lo = stage_cons(i,j,k-1,RhoTheta_comp) * (one + RvoRd*qv_q);
124  Real Thm_grad = dzi * inv_detJ_on_kface * ( Thm_hi - Thm_lo );
125 
126  Real coeff_P = -Gamma * R_d * dzi * inv_detJ_on_kface * pi_c * (one + RvoRd*qv_p)
127  - Gamma * R_d * R_d * Thm_grad * myhalf * pi_stage_ca(i,j,k ) /
128  ( c_v * stage_cons(i,j,k ,RhoTheta_comp) );
129 
130  Real coeff_Q = Gamma * R_d * dzi * inv_detJ_on_kface * pi_c * (one + RvoRd*qv_q)
131  - Gamma * R_d * R_d * Thm_grad * myhalf * pi_stage_ca(i,j,k-1) /
132  ( c_v * stage_cons(i,j,k-1,RhoTheta_comp) );
133 
134  if (l_use_moisture) {
135  Real q = myhalf * ( prim(i,j,k,PrimQ1_comp) + prim(i,j,k-1,PrimQ1_comp)
136  + prim(i,j,k,PrimQ2_comp) + prim(i,j,k-1,PrimQ2_comp) );
137  coeff_P /= (one + q);
138  coeff_Q /= (one + q);
139  }
140 
141  // NOTE: we store the moisture-normalized coefficients so that the explicit
142  // RHS assembled in erf_substep_* uses exactly the same vertical fast
143  // pressure gradient as the implicit tridiagonal system built below
144  coeffP_a(i,j,k) = coeff_P;
145  coeffQ_a(i,j,k) = coeff_Q;
146 
147  Real theta_t_lo = myhalf * ( prim(i,j,k-2,PrimTheta_comp) + prim(i,j,k-1,PrimTheta_comp) );
148  Real theta_t_mid = myhalf * ( prim(i,j,k-1,PrimTheta_comp) + prim(i,j,k ,PrimTheta_comp) );
149  Real theta_t_hi = myhalf * ( prim(i,j,k ,PrimTheta_comp) + prim(i,j,k+1,PrimTheta_comp) );
150 
151  // LHS for tri-diagonal system
152  Real D = beta_2 * beta_2 * dzi * static_cast<Real>(dtau * dtau);
153  coeffA_a(i,j,k) = D * (one/detJ(i,j,k-1)) * ( halfg - coeff_Q * theta_t_lo );
154  coeffC_a(i,j,k) = D * (one/detJ(i,j,k )) * (-halfg + coeff_P * theta_t_hi );
155 
156  coeffB_a(i,j,k) = one + D * ( (coeff_Q/detJ(i,j,k-1) - coeff_P/detJ(i,j,k)) * theta_t_mid
157  + halfg * (one/detJ(i,j,k) - one/detJ(i,j,k-1)) );
158  });
159 
160  } else {
161 
162  ParallelFor(bx_shrunk_in_k, [=] AMREX_GPU_DEVICE (int i, int j, int k)
163  {
164  Real pi_c = myhalf * (pi_stage_ca(i,j,k-1) + pi_stage_ca(i,j,k));
165 
166  Real qv_p = (l_use_moisture) ? prim(i,j,k ,PrimQ1_comp) : zero;
167  Real qv_q = (l_use_moisture) ? prim(i,j,k-1,PrimQ1_comp) : zero;
168 
169  Real Thm_hi = stage_cons(i,j,k ,RhoTheta_comp) * (one + RvoRd*qv_p);
170  Real Thm_lo = stage_cons(i,j,k-1,RhoTheta_comp) * (one + RvoRd*qv_q);
171  Real Thm_grad = dzi * ( Thm_hi - Thm_lo );
172 
173  Real coeff_P = -Gamma * R_d * dzi * pi_c * (one + RvoRd*qv_p)
174  - Gamma * R_d * R_d * Thm_grad * myhalf * pi_stage_ca(i,j,k ) /
175  ( c_v * stage_cons(i,j,k ,RhoTheta_comp) );
176 
177  Real coeff_Q = Gamma * R_d * dzi * pi_c * (one + RvoRd*qv_q)
178  - Gamma * R_d * R_d * Thm_grad * myhalf * pi_stage_ca(i,j,k-1) /
179  ( c_v * stage_cons(i,j,k-1,RhoTheta_comp) );
180 
181  if (l_use_moisture) {
182  Real q = myhalf * ( prim(i,j,k,PrimQ1_comp) + prim(i,j,k-1,PrimQ1_comp)
183  + prim(i,j,k,PrimQ2_comp) + prim(i,j,k-1,PrimQ2_comp) );
184  coeff_P /= (one + q);
185  coeff_Q /= (one + q);
186  }
187 
188  // NOTE: we store the moisture-normalized coefficients so that the explicit
189  // RHS assembled in erf_substep_* uses exactly the same vertical fast
190  // pressure gradient as the implicit tridiagonal system built below
191  coeffP_a(i,j,k) = coeff_P;
192  coeffQ_a(i,j,k) = coeff_Q;
193 
194  Real theta_t_lo = myhalf * ( prim(i,j,k-2,PrimTheta_comp) + prim(i,j,k-1,PrimTheta_comp) );
195  Real theta_t_mid = myhalf * ( prim(i,j,k-1,PrimTheta_comp) + prim(i,j,k ,PrimTheta_comp) );
196  Real theta_t_hi = myhalf * ( prim(i,j,k ,PrimTheta_comp) + prim(i,j,k+1,PrimTheta_comp) );
197 
198  // LHS for tri-diagonal system
199  Real D = beta_2 * beta_2 * dzi * static_cast<Real>(dtau * dtau);
200  coeffA_a(i,j,k) = D * ( halfg - coeff_Q * theta_t_lo );
201  coeffC_a(i,j,k) = D * (-halfg + coeff_P * theta_t_hi );
202 
203  coeffB_a(i,j,k) = one + D * (coeff_Q - coeff_P) * theta_t_mid;
204  });
205  }
206 
207  amrex::Box b2d = tbz; // Copy constructor
208  b2d.setRange(2,0);
209 
210  auto const lo = amrex::lbound(bx);
211  auto const hi = amrex::ubound(bx);
212 
213  auto const domhi = amrex::ubound(domain);
214 
215  {
216  BL_PROFILE("make_coeffs_b2d_loop");
217 #ifdef AMREX_USE_GPU
218  ParallelFor(b2d, [=] AMREX_GPU_DEVICE (int i, int j, int) {
219 
220  // If at the bottom of the grid, we will set w to a specified Dirichlet value
221  coeffA_a(i,j,lo.z) = zero;
222  coeffB_a(i,j,lo.z) = one;
223  coeffC_a(i,j,lo.z) = zero;
224 
225  // If at the top of the grid, we will set w to a specified Dirichlet value
226  coeffA_a(i,j,hi.z+1) = zero;
227  coeffB_a(i,j,hi.z+1) = one;
228  coeffC_a(i,j,hi.z+1) = zero;
229 
230  // UNLESS if at the top of the domain and the boundary is outflow,
231  // we will use a homogeneous Neumann condition
232  if ( (hi.z == domhi.z) &&
233  (phys_bc_type[5] == ERF_BC::outflow or phys_bc_type[5] == ERF_BC::ho_outflow) )
234  {
235  coeffA_a(i,j,hi.z+1) = -one;
236  }
237 
238  // w = specified Dirichlet value at k = lo.z
239  gam_a(i,j,lo.z) = coeffC_a(i,j,lo.z) / coeffB_a(i,j,lo.z);
240  for (int k = lo.z+1; k <= hi.z+1; k++) {
241  coeffB_a(i,j,k) = one / ( coeffB_a(i,j,k) - coeffA_a(i,j,k)*gam_a(i,j,k-1) );
242  gam_a(i,j,k) = coeffC_a(i,j,k) * coeffB_a(i,j,k);
243  }
244  });
245 #else
246  // If at the bottom of the grid, we will set w to a specified Dirichlet value
247  for (int j = lo.y; j <= hi.y; ++j) {
248  AMREX_PRAGMA_SIMD
249  for (int i = lo.x; i <= hi.x; ++i) {
250  coeffA_a(i,j,lo.z) = zero;
251  coeffB_a(i,j,lo.z) = one;
252  coeffC_a(i,j,lo.z) = zero;
253  gam_a(i,j,lo.z) = coeffC_a(i,j,lo.z) / coeffB_a(i,j,lo.z);
254  }
255  }
256  for (int j = lo.y; j <= hi.y; ++j) {
257  AMREX_PRAGMA_SIMD
258  for (int i = lo.x; i <= hi.x; ++i) {
259 
260  // If at the top of the grid, we will set w to a specified Dirichlet value
261  coeffA_a(i,j,hi.z+1) = zero;
262  coeffB_a(i,j,hi.z+1) = one;
263  coeffC_a(i,j,hi.z+1) = zero;
264 
265  // UNLESS if at the top of the domain and the boundary is outflow,
266  // we will use a homogeneous Neumann condition
267  if ( (hi.z == domhi.z) &&
268  (phys_bc_type[5] == ERF_BC::outflow or phys_bc_type[5] == ERF_BC::ho_outflow) )
269  {
270  coeffA_a(i,j,hi.z+1) = -one;
271  }
272  }
273  }
274  for (int k = lo.z+1; k <= hi.z+1; ++k) {
275  for (int j = lo.y; j <= hi.y; ++j) {
276  AMREX_PRAGMA_SIMD
277  for (int i = lo.x; i <= hi.x; ++i) {
278  coeffB_a(i,j,k) = one / ( coeffB_a(i,j,k) - coeffA_a(i,j,k)*gam_a(i,j,k-1) );
279  gam_a(i,j,k) = coeffC_a(i,j,k) * coeffB_a(i,j,k);
280  }
281  }
282  }
283 #endif
284  } // end profile
285  } // mfi
286  } // omp
287 }
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
constexpr amrex::Real RvoRd
Definition: ERF_Constants.H:56
constexpr amrex::Real R_d
Definition: ERF_Constants.H:47
constexpr amrex::Real Gamma
Definition: ERF_Constants.H:62
#define PrimQ1_comp
Definition: ERF_IndexDefines.H:61
#define PrimQ2_comp
Definition: ERF_IndexDefines.H:62
#define RhoTheta_comp
Definition: ERF_IndexDefines.H:40
#define PrimTheta_comp
Definition: ERF_IndexDefines.H:58
@ ho_outflow
amrex::GpuArray< Real, AMREX_SPACEDIM > dxInv
Definition: ERF_InitCustomPertVels_ParticleTests.H:17
const int khi
Definition: ERF_InitCustomPert_Bubble.H:21
void make_fast_coeffs(int, MultiFab &fast_coeffs, Vector< MultiFab > &S_stage_data, const MultiFab &S_stage_prim, const MultiFab &pi_stage, const amrex::Geometry geom, bool l_use_moisture, MeshType mesh_type, Real gravity, Real c_p, std::unique_ptr< MultiFab > &detJ_cc, const double dtau, Real beta_s, amrex::GpuArray< ERF_BC, AMREX_SPACEDIM *2 > &phys_bc_type)
Definition: ERF_MakeFastCoeffs.cpp:27
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::Real Real
Definition: ERF_ShocInterface.H:19
AMREX_FORCE_INLINE amrex::IntVect TileNoZ()
Definition: ERF_TileNoZ.H:11
@ cons
Definition: ERF_IndexDefines.H:232
@ q
Definition: ERF_WSM6.H:184
Here is the call graph for this function: