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 MultiFab *r0, const MultiFab *pi0, Real 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 MultiFab *  r0,
const MultiFab *  pi0,
Real  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]mesh_typeDo we have constant dz?
[in]gravityMagnitude of gravity
[in]c_pCoefficient at constant pressure
[in]r0Reference (hydrostatically stratified) density
[in]pi0Reference (hydrostatically stratified) Exner function
[in]dtauFast time step
[in]beta_sCoefficient which determines how implicit vs explicit the solve is
39 {
40  BL_PROFILE_VAR("make_fast_coeffs()",make_fast_coeffs);
41 
42  Real beta_2 = myhalf * (one + beta_s); // multiplies implicit terms
43 
44  Real c_v = c_p - R_d;
45  Real RvOverRd = R_v / 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) ? detJ_cc->const_array(mfi) : Array4<const Real>{};
81 
82  const Array4<const Real>& r0_ca = r0->const_array(mfi);
83  const Array4<const Real>& pi0_ca = pi0->const_array(mfi);
84  const Array4<const Real>& pi_stage_ca = pi_stage.const_array(mfi);
85 
86  FArrayBox gam_fab; gam_fab.resize(surroundingNodes(bx,2),1,The_Async_Arena());
87 
88  auto const& coeffA_a = coeff_A_mf.array(mfi);
89  auto const& coeffB_a = coeff_B_mf.array(mfi);
90  auto const& coeffC_a = coeff_C_mf.array(mfi);
91  auto const& coeffP_a = coeff_P_mf.array(mfi);
92  auto const& coeffQ_a = coeff_Q_mf.array(mfi);
93  auto const& gam_a = gam_fab.array();
94 
95  // *********************************************************************
96  // *********************************************************************
97  // *********************************************************************
98 
99  Box bx_shrunk_in_k = bx;
100  int klo = tbz.smallEnd(2);
101  int khi = tbz.bigEnd(2);
102  bx_shrunk_in_k.setSmall(2,klo+1);
103  bx_shrunk_in_k.setBig(2,khi-1);
104 
105  // Note that the notes use "g" to mean the magnitude of gravity, so it is positive
106  // We set grav_gpu[2] to be the vector component which is negative
107  // We define halfg to match the notes (which is why we take the absolute value)
108  Real halfg = std::abs(myhalf * grav_gpu[2]);
109 
110  //Note we don't act on the bottom or top boundaries of the domain
111  if (mesh_type != MeshType::ConstantDz)
112  {
113  ParallelFor(bx_shrunk_in_k, [=] AMREX_GPU_DEVICE (int i, int j, int k)
114  {
115  Real rhobar_lo, rhobar_hi, pibar_lo, pibar_hi;
116  rhobar_lo = r0_ca(i,j,k-1);
117  rhobar_hi = r0_ca(i,j,k );
118  pibar_lo = pi0_ca(i,j,k-1);
119  pibar_hi = pi0_ca(i,j,k );
120 
121  Real pi_c = myhalf * (pi_stage_ca(i,j,k-1) + pi_stage_ca(i,j,k));
122 
123  Real detJ_on_kface = myhalf * (detJ(i,j,k) + detJ(i,j,k-1));
124  Real inv_detJ_on_kface = one / detJ_on_kface;
125 
126  Real qv_p = (l_use_moisture) ? prim(i,j,k ,PrimQ1_comp) : zero;
127  Real qv_q = (l_use_moisture) ? prim(i,j,k-1,PrimQ1_comp) : zero;
128 
129  Real coeff_P = -Gamma * R_d * dzi * inv_detJ_on_kface * pi_c * (one + RvOverRd*qv_p)
130  + halfg * R_d * rhobar_hi * pi_stage_ca(i,j,k) /
131  ( c_v * pibar_hi * stage_cons(i,j,k,RhoTheta_comp) );
132 
133  Real coeff_Q = Gamma * R_d * dzi * inv_detJ_on_kface * pi_c * (one + RvOverRd*qv_q)
134  + halfg * R_d * rhobar_lo * pi_stage_ca(i,j,k-1) /
135  ( c_v * pibar_lo * stage_cons(i,j,k-1,RhoTheta_comp) );
136 
137  coeffP_a(i,j,k) = coeff_P;
138  coeffQ_a(i,j,k) = coeff_Q;
139 
140  if (l_use_moisture) {
141  Real q = myhalf * ( prim(i,j,k,PrimQ1_comp) + prim(i,j,k-1,PrimQ1_comp)
142  + prim(i,j,k,PrimQ2_comp) + prim(i,j,k-1,PrimQ2_comp) );
143  coeff_P /= (one + q);
144  coeff_Q /= (one + q);
145  }
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 = dtau * dtau * beta_2 * beta_2 * dzi;
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 * (Real(1.0)/detJ(i,j,k) - Real(1.0)/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 rhobar_lo, rhobar_hi, pibar_lo, pibar_hi;
165  rhobar_lo = r0_ca(i,j,k-1);
166  rhobar_hi = r0_ca(i,j,k );
167  pibar_lo = pi0_ca(i,j,k-1);
168  pibar_hi = pi0_ca(i,j,k );
169 
170  Real pi_c = myhalf * (pi_stage_ca(i,j,k-1) + pi_stage_ca(i,j,k));
171 
172  Real qv_p = (l_use_moisture) ? prim(i,j,k ,PrimQ1_comp) : zero;
173  Real qv_q = (l_use_moisture) ? prim(i,j,k-1,PrimQ1_comp) : zero;
174 
175  Real coeff_P = -Gamma * R_d * dzi * pi_c * (one + RvOverRd*qv_p)
176  + halfg * R_d * rhobar_hi * pi_stage_ca(i,j,k) /
177  ( c_v * pibar_hi * stage_cons(i,j,k,RhoTheta_comp) );
178 
179  Real coeff_Q = Gamma * R_d * dzi * pi_c * (one + RvOverRd*qv_q)
180  + halfg * R_d * rhobar_lo * pi_stage_ca(i,j,k-1) /
181  ( c_v * pibar_lo * stage_cons(i,j,k-1,RhoTheta_comp) );
182 
183  coeffP_a(i,j,k) = coeff_P;
184  coeffQ_a(i,j,k) = coeff_Q;
185 
186  if (l_use_moisture) {
187  Real q = myhalf * ( prim(i,j,k,PrimQ1_comp) + prim(i,j,k-1,PrimQ1_comp)
188  + prim(i,j,k,PrimQ2_comp) + prim(i,j,k-1,PrimQ2_comp) );
189  coeff_P /= (one + q);
190  coeff_Q /= (one + q);
191  }
192 
193  Real theta_t_lo = myhalf * ( prim(i,j,k-2,PrimTheta_comp) + prim(i,j,k-1,PrimTheta_comp) );
194  Real theta_t_mid = myhalf * ( prim(i,j,k-1,PrimTheta_comp) + prim(i,j,k ,PrimTheta_comp) );
195  Real theta_t_hi = myhalf * ( prim(i,j,k ,PrimTheta_comp) + prim(i,j,k+1,PrimTheta_comp) );
196 
197  // LHS for tri-diagonal system
198  Real D = dtau * dtau * beta_2 * beta_2 * dzi;
199  coeffA_a(i,j,k) = D * ( halfg - coeff_Q * theta_t_lo );
200  coeffC_a(i,j,k) = D * (-halfg + coeff_P * theta_t_hi );
201 
202  coeffB_a(i,j,k) = one + D * (coeff_Q - coeff_P) * theta_t_mid;
203  });
204  }
205 
206  amrex::Box b2d = tbz; // Copy constructor
207  b2d.setRange(2,0);
208 
209  auto const lo = amrex::lbound(bx);
210  auto const hi = amrex::ubound(bx);
211 
212  auto const domhi = amrex::ubound(domain);
213 
214  {
215  BL_PROFILE("make_coeffs_b2d_loop");
216 #ifdef AMREX_USE_GPU
217  ParallelFor(b2d, [=] AMREX_GPU_DEVICE (int i, int j, int) {
218 
219  // If at the bottom of the grid, we will set w to a specified Dirichlet value
220  coeffA_a(i,j,lo.z) = zero;
221  coeffB_a(i,j,lo.z) = one;
222  coeffC_a(i,j,lo.z) = zero;
223 
224  // If at the top of the grid, we will set w to a specified Dirichlet value
225  coeffA_a(i,j,hi.z+1) = zero;
226  coeffB_a(i,j,hi.z+1) = one;
227  coeffC_a(i,j,hi.z+1) = zero;
228 
229  // UNLESS if at the top of the domain and the boundary is outflow,
230  // we will use a homogeneous Neumann condition
231  if ( (hi.z == domhi.z) &&
232  (phys_bc_type[5] == ERF_BC::outflow or phys_bc_type[5] == ERF_BC::ho_outflow) )
233  {
234  coeffA_a(i,j,hi.z+1) = -one;
235  }
236 
237  // w = specified Dirichlet value at k = lo.z
238  gam_a(i,j,lo.z) = coeffC_a(i,j,lo.z) / coeffB_a(i,j,lo.z);
239  for (int k = lo.z+1; k <= hi.z+1; k++) {
240  coeffB_a(i,j,k) = one / ( coeffB_a(i,j,k) - coeffA_a(i,j,k)*gam_a(i,j,k-1) );
241  gam_a(i,j,k) = coeffC_a(i,j,k) * coeffB_a(i,j,k);
242  }
243  });
244 #else
245  // If at the bottom of the grid, we will set w to a specified Dirichlet value
246  for (int j = lo.y; j <= hi.y; ++j) {
247  AMREX_PRAGMA_SIMD
248  for (int i = lo.x; i <= hi.x; ++i) {
249  coeffA_a(i,j,lo.z) = zero;
250  coeffB_a(i,j,lo.z) = one;
251  coeffC_a(i,j,lo.z) = zero;
252  gam_a(i,j,lo.z) = coeffC_a(i,j,lo.z) / coeffB_a(i,j,lo.z);
253  }
254  }
255  for (int j = lo.y; j <= hi.y; ++j) {
256  AMREX_PRAGMA_SIMD
257  for (int i = lo.x; i <= hi.x; ++i) {
258 
259  // If at the top of the grid, we will set w to a specified Dirichlet value
260  coeffA_a(i,j,hi.z+1) = zero;
261  coeffB_a(i,j,hi.z+1) = one;
262  coeffC_a(i,j,hi.z+1) = zero;
263 
264  // UNLESS if at the top of the domain and the boundary is outflow,
265  // we will use a homogeneous Neumann condition
266  if ( (hi.z == domhi.z) &&
267  (phys_bc_type[5] == ERF_BC::outflow or phys_bc_type[5] == ERF_BC::ho_outflow) )
268  {
269  coeffA_a(i,j,hi.z+1) = -one;
270  }
271  }
272  }
273  for (int k = lo.z+1; k <= hi.z+1; ++k) {
274  for (int j = lo.y; j <= hi.y; ++j) {
275  AMREX_PRAGMA_SIMD
276  for (int i = lo.x; i <= hi.x; ++i) {
277  coeffB_a(i,j,k) = one / ( coeffB_a(i,j,k) - coeffA_a(i,j,k)*gam_a(i,j,k-1) );
278  gam_a(i,j,k) = coeffC_a(i,j,k) * coeffB_a(i,j,k);
279  }
280  }
281  }
282 #endif
283  } // end profile
284  } // mfi
285  } // omp
286 }
constexpr amrex::Real R_v
Definition: ERF_Constants.H:30
constexpr amrex::Real one
Definition: ERF_Constants.H:7
constexpr amrex::Real zero
Definition: ERF_Constants.H:6
constexpr amrex::Real myhalf
Definition: ERF_Constants.H:11
constexpr amrex::Real R_d
Definition: ERF_Constants.H:29
constexpr amrex::Real Gamma
Definition: ERF_Constants.H:38
#define PrimQ1_comp
Definition: ERF_IndexDefines.H:58
#define PrimQ2_comp
Definition: ERF_IndexDefines.H:59
#define RhoTheta_comp
Definition: ERF_IndexDefines.H:37
#define PrimTheta_comp
Definition: ERF_IndexDefines.H:55
@ 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 MultiFab *r0, const MultiFab *pi0, Real dtau, Real beta_s, amrex::GpuArray< ERF_BC, AMREX_SPACEDIM *2 > &phys_bc_type)
Definition: ERF_MakeFastCoeffs.cpp:26
ParallelFor(grown_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:192
@ q
Definition: ERF_WSM6.H:169
real(kind=kind_phys), parameter, private r0
Definition: ERF_module_mp_wsm6.F90:21
Here is the call graph for this function: