ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_TI_substep_fun.H
Go to the documentation of this file.
1 #include "ERF_SrcHeaders.H"
2 
3 /**
4  * Wrapper for calling the routine that creates the fast RHS
5  */
6 auto acoustic_substepping_fun = [&](int fast_step, int n_sub, int nrk,
7  Vector<MultiFab>& S_slow_rhs,
8  const Vector<MultiFab>& S_old,
9  Vector<MultiFab>& S_stage,
10  Vector<MultiFab>& S_data,
11  const Real dtau,
12  const Real slow_dt,
13  const Real inv_fac,
14  const Real old_substep_time,
15  const Real new_substep_time)
16 {
17  BL_PROFILE("acoustic_substepping_fun");
18  if (verbose) amrex::Print() << "Fast time integration at level " << level
19  << std::setprecision(timeprecision)
20  << " from " << old_substep_time << " to " << new_substep_time
21  << " with dt = " << dtau << std::endl;
22 
23  // Define beta_s here so that it is consistent between where we make the fast coefficients
24  // and where we use them
25  // Per p2902 of Klemp-Skamarock-Dudhia-2007
26  // beta_s = -1.0 : fully explicit
27  // beta_s = 1.0 : fully implicit
28  Real beta_s;
29  if (solverChoice.substepping_type[level] == SubsteppingType::Implicit) {
30  beta_s = solverChoice.beta_s;
31  } else { // Fully explicit
32  beta_s = -1.0;
33  }
34 
35  const GpuArray<Real, AMREX_SPACEDIM> dxInv = fine_geom.InvCellSizeArray();
36 
37  // Canopy data for mom sources
38  MultiFab* forest_drag = (solverChoice.do_forest_drag) ?
39  m_forest_drag[level]->get_drag_field() : nullptr;
40 
41  // Immersed Forcing
42  MultiFab* terrain_blank = (solverChoice.terrain_type == TerrainType::ImmersedForcing ||
43  solverChoice.buildings_type == BuildingsType::ImmersedForcing ) ?
44  terrain_blanking[level].get() : nullptr;
45 
46  Vector<MultiFab> Svec_to_use;
47  if (fast_step == 0) {
48  // If this is the first substep we pass in S_old as the previous step's solution
49  Svec_to_use.push_back(MultiFab(S_old[IntVars::cons],make_alias,0,S_old[IntVars::cons].nComp()));
50  Svec_to_use.push_back(MultiFab(S_old[IntVars::xmom],make_alias,0,1));
51  Svec_to_use.push_back(MultiFab(S_old[IntVars::ymom],make_alias,0,1));
52  Svec_to_use.push_back(MultiFab(S_old[IntVars::zmom],make_alias,0,1));
53  } else {
54  // If this is not the first substep we pass in S_data as the previous step's solution
55  Svec_to_use.push_back(MultiFab(S_data[IntVars::cons],make_alias,0,S_data[IntVars::cons].nComp()));
56  Svec_to_use.push_back(MultiFab(S_data[IntVars::xmom],make_alias,0,1));
57  Svec_to_use.push_back(MultiFab(S_data[IntVars::ymom],make_alias,0,1));
58  Svec_to_use.push_back(MultiFab(S_data[IntVars::zmom],make_alias,0,1));
59  }
60 
61  make_sources(level, nrk, dtau, old_substep_time,
62  Svec_to_use, S_prim, cc_src, base_state[level], z_phys_cc[level].get(),
63  xvel_new, yvel_new,
64  qheating_rates[level].get(),
65  terrain_blank, fine_geom, solverChoice,
66  mapfac[level],
67  dptr_rhotheta_src, dptr_rhoqt_src,
68  dptr_wbar_sub, d_rayleigh_ptrs_at_lev,
69  d_sinesq_at_lev,
70  input_sounding_data, turbPert, false);
71 
72  // *************************************************************************
73  // Set up flux registers if using two_way coupling
74  // *************************************************************************
75  const bool l_reflux = ( (solverChoice.coupling_type == CouplingType::TwoWay) && (nrk == 2) && (finest_level > 0) );
76 
77  YAFluxRegister* fr_as_crse = nullptr;
78  YAFluxRegister* fr_as_fine = nullptr;
79  if (l_reflux) {
80  if (level < finest_level) {
81  fr_as_crse = getAdvFluxReg(level+1);
82  }
83  if (level > 0) {
84  fr_as_fine = getAdvFluxReg(level);
85  }
86  }
87 
88  MultiFab base_to_use;
89  if ( solverChoice.terrain_type == TerrainType::MovingFittedMesh ) {
90  base_to_use = MultiFab(base_state_new[level], make_alias, 0, BaseState::num_comps);
91  } else {
92  base_to_use = MultiFab(base_state[level], make_alias, 0, BaseState::num_comps);
93  }
94 
95  make_mom_sources(old_substep_time, dtau, Svec_to_use,
96  z_phys_nd[level].get(), z_phys_cc[level].get(), stretched_dz_h[level],
97  xvel_new, yvel_new, zvel_new,
98  xmom_src, ymom_src, zmom_src,
99  base_to_use, forest_drag, terrain_blank,
100  cosPhi_m[level].get(), sinPhi_m[level].get(), fine_geom, solverChoice,
101  mapfac[level],
102  (solverChoice.have_geo_wind_profile) ? d_u_geos[level].data() : nullptr,
103  (solverChoice.have_geo_wind_profile) ? d_v_geos[level].data() : nullptr,
104  dptr_wbar_sub, d_rayleigh_ptrs_at_lev,
105  d_sinesq_at_lev, d_sinesq_stag_at_lev, d_sponge_ptrs_at_lev,
106  (solverChoice.hindcast_lateral_forcing? &forecast_state_interp[level] : nullptr),
107  input_sounding_data, false);
108 
109  // Moving terrain
110  std::unique_ptr<MultiFab> z_t_pert;
111  if ( solverChoice.terrain_type == TerrainType::MovingFittedMesh )
112  {
113  z_t_pert = std::make_unique<MultiFab>(S_data[IntVars::zmom].boxArray(), S_data[IntVars::zmom].DistributionMap(), 1, 1);
114  update_terrain_substep(level, old_substep_time, new_substep_time, dtau, S_data, z_t_pert);
115  }
116 
117  bool l_use_moisture = ( solverChoice.moisture_type != MoistureType::None );
118 
119  if ( (fast_step == 0) || (solverChoice.terrain_type == TerrainType::MovingFittedMesh) )
120  {
121  make_fast_coeffs(level, fast_coeffs, S_stage, S_prim, pi_stage, fine_geom,
122  l_use_moisture, SolverChoice::mesh_type, solverChoice.gravity, solverChoice.c_p,
123  detJ_cc[level], r0, pi0, dtau, beta_s, phys_bc_type);
124  }
125 
126  bool l_rayleigh_implicit = (solverChoice.dampingChoice.rayleigh_damping_type == RayleighDampingType::FastImplicit);
127  Real l_damp_coef = solverChoice.dampingChoice.rayleigh_dampcoef;
128 
129  if ( solverChoice.terrain_type == TerrainType::MovingFittedMesh )
130  {
131  erf_substep_MT(fast_step, nrk, level, finest_level,
132  S_slow_rhs, Svec_to_use, S_stage, S_prim, qt, pi_stage, fast_coeffs,
133  S_data, lagged_delta_rt[level], avg_xmom[level], avg_ymom[level], avg_zmom[level],
134  cc_src, xmom_src, ymom_src, zmom_src,
135  fine_geom,
136  solverChoice.gravity, solverChoice.use_lagged_delta_rt,
137  z_t_rk[level], z_t_pert.get(),
138  z_phys_nd[level], z_phys_nd_new[level], z_phys_nd_src[level],
139  detJ_cc[level], detJ_cc_new[level], detJ_cc_src[level],
140  dtau, beta_s, inv_fac, mapfac[level],
141  fr_as_crse, fr_as_fine, l_use_moisture, l_reflux);
142 
143  } else if (solverChoice.mesh_type == MeshType::VariableDz) {
144 
145  erf_substep_T(fast_step, nrk, level, finest_level,
146  S_slow_rhs, Svec_to_use, S_stage, S_prim, qt, pi_stage, fast_coeffs,
147  S_data, lagged_delta_rt[level], avg_xmom[level], avg_ymom[level], avg_zmom[level],
148  cc_src, xmom_src, ymom_src, zmom_src,
149  fine_geom, solverChoice.gravity,
150  z_phys_nd[level], detJ_cc[level], dtau, beta_s, inv_fac,
151  mapfac[level],
152  fr_as_crse, fr_as_fine, l_use_moisture, l_reflux,
153  (l_rayleigh_implicit) ? d_sinesq_ptrs[level].data() : nullptr, l_damp_coef);
154 
155  } else {
156 
157  erf_substep_NS(fast_step, nrk, level, finest_level,
158  S_slow_rhs, Svec_to_use, S_stage, S_prim, qt, pi_stage, fast_coeffs,
159  S_data, lagged_delta_rt[level], avg_xmom[level], avg_ymom[level], avg_zmom[level],
160  cc_src, xmom_src, ymom_src, zmom_src,
161  fine_geom, solverChoice.gravity, stretched_dz_d[level],
162  dtau, beta_s, inv_fac, mapfac[level],
163  fr_as_crse, fr_as_fine, l_use_moisture, l_reflux,
164  (l_rayleigh_implicit) ? d_sinesq_ptrs[level].data() : nullptr, l_damp_coef);
165  }
166 
167  // Even if we update all the conserved variables we don't need
168  // to fillpatch the slow ones every acoustic substep
169 
170  if ( (solverChoice.vert_implicit_fac[nrk] > 0.) && !solverChoice.implicit_before_substep &&
171  (fast_step == n_sub-1) )
172  {
173  MultiFab scratch(S_data[IntVars::cons], make_alias, 0, 2);
174  MultiFab scratch_xmom(S_data[IntVars::xmom], make_alias, 0, 1);
175  MultiFab scratch_ymom(S_data[IntVars::ymom], make_alias, 0, 1);
176 #ifdef ERF_IMPLICIT_W
177  MultiFab scratch_zmom(S_data[IntVars::zmom], make_alias, 0, 1);
178 #endif
179 #include "ERF_Implicit.H"
180  }
181 
182  // Apply vertical velocity damping at the end of the timestep
183  if ( (solverChoice.dampingChoice.w_damping) &&
184  (nrk == 2) &&
185  (fast_step == n_sub-1))
186  {
187  amrex::Real w_damping_const = solverChoice.dampingChoice.w_damping_const;
188  amrex::Real w_damping_coeff = solverChoice.dampingChoice.w_damping_coeff;
189  amrex::Real cflw_lim = solverChoice.dampingChoice.w_damping_cfl;
190 
191  for ( MFIter mfi(S_data[IntVars::cons]); mfi.isValid(); ++mfi)
192  {
193  Box tbz = mfi.nodaltilebox(2);
194 
195  const Array4<const Real>& z_nd_arr = z_phys_nd[level]->const_array(mfi);
196 
197  const Array4<const Real>& cell_data = S_data[IntVars::cons].const_array(mfi);
198  const Array4< Real>& rho_w = S_data[IntVars::zmom].array(mfi);
199 
200  ParallelFor(tbz, [=] AMREX_GPU_DEVICE (int i, int j, int k)
201  {
202  Real dzInv;
203  if (z_nd_arr) {
204  // average z_nd to face then diff
205  if (k == domain.smallEnd(2)) {
206  dzInv = 4.0 / (
207  z_nd_arr(i ,j ,k+1) - z_nd_arr(i ,j ,k)
208  + z_nd_arr(i+1,j ,k+1) - z_nd_arr(i+1,j ,k)
209  + z_nd_arr(i ,j+1,k+1) - z_nd_arr(i ,j+1,k)
210  + z_nd_arr(i+1,j+1,k+1) - z_nd_arr(i+1,j+1,k) );
211  } else if (k == domain.bigEnd(2)+1) {
212  dzInv = 4.0 / (
213  z_nd_arr(i ,j ,k) - z_nd_arr(i ,j ,k-1)
214  + z_nd_arr(i+1,j ,k) - z_nd_arr(i+1,j ,k-1)
215  + z_nd_arr(i ,j+1,k) - z_nd_arr(i ,j+1,k-1)
216  + z_nd_arr(i+1,j+1,k) - z_nd_arr(i+1,j+1,k-1) );
217  } else {
218  // dz = 0.5 * (dz[i,j,k] + dz[i,j,k+1])
219  // = 0.5 * (z_nd_face[i,j,k+1] - z_nd_face[i,j,k-1])
220  dzInv = 8.0 / (
221  z_nd_arr(i ,j ,k+1) - z_nd_arr(i ,j ,k-1)
222  + z_nd_arr(i+1,j ,k+1) - z_nd_arr(i+1,j ,k-1)
223  + z_nd_arr(i ,j+1,k+1) - z_nd_arr(i ,j+1,k-1)
224  + z_nd_arr(i+1,j+1,k+1) - z_nd_arr(i+1,j+1,k-1) );
225  }
226  } else {
227  dzInv = dxInv[2];
228  }
229 
230  Real dampcoef = (w_damping_const > 0)
231  ? w_damping_const
232  : w_damping_coeff / (dzInv * dt_advance * dt_advance);
233 
234  Real rho_on_w_face = 0.5 * ( cell_data(i,j,k,Rho_comp) + cell_data(i,j,k-1,Rho_comp) );
235  Real wmag = std::abs(rho_w(i,j,k) / rho_on_w_face);
236 
237  // This is the dynamics timestep, not the slow or fast dt
238  Real cflw = wmag * dt_advance * dzInv;
239 
240  if (cflw > cflw_lim) {
241  Real sgn_w = (rho_w(i,j,k) > 0) ? 1.0 : -1.0;
242  rho_w(i, j, k) -= rho_on_w_face * sgn_w * dampcoef * (cflw - cflw_lim) * dt_advance;
243  // Note: These prints are not immediately flushed to screen
244 #ifdef AMREX_USE_GPU
245  AMREX_DEVICE_PRINTF("t=%f w-damping applied at (%d,%d,%d) for w-CFL = %f > %f : %f --> %f\n",
246  old_time+dt_advance, i,j,k, cflw, cflw_lim, sgn_w*wmag, rho_w(i,j,k)/rho_on_w_face);
247 #else
248  printf("t=%f w-damping applied at (%d,%d,%d) for w-CFL = %f > %f : %f --> %f\n",
249  old_time+dt_advance, i,j,k, cflw, cflw_lim, sgn_w*wmag, rho_w(i,j,k)/rho_on_w_face);
250 #endif
251  }
252  });
253  }
254  }
255 
256  // NOTE: Numerical diffusion is tested on in FillPatchIntermediate and dictates the size of the
257  // box over which VelocityToMomentum is computed. V2M requires one more ghost cell be
258  // filled for rho than velocity. This logical condition ensures we fill enough ghost cells
259  // when use_num_diff is true.
260  int ng_cons = S_data[IntVars::cons].nGrowVect().max() - 1;
261  int ng_vel = S_data[IntVars::xmom].nGrowVect().max();
262  if (!solverChoice.use_num_diff) {
263  ng_cons = 1;
264  ng_vel = 1;
265  }
266  apply_bcs(S_data, new_substep_time, ng_cons, ng_vel, fast_only=true, vel_and_mom_synced=false);
267 };
if(l_use_mynn &&start_comp<=RhoKE_comp &&end_comp >=RhoKE_comp)
Definition: ERF_AddQKESources.H:2
#define Rho_comp
Definition: ERF_IndexDefines.H:36
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
void make_mom_sources(Real time, Real, const Vector< MultiFab > &S_data, const MultiFab *z_phys_nd, const MultiFab *z_phys_cc, Vector< Real > &stretched_dz_h, const MultiFab &xvel, const MultiFab &yvel, const MultiFab &wvel, MultiFab &xmom_src, MultiFab &ymom_src, MultiFab &zmom_src, const MultiFab &base_state, MultiFab *forest_drag, MultiFab *terrain_blank, MultiFab *cosPhi_mf, MultiFab *sinPhi_mf, const Geometry geom, const SolverChoice &solverChoice, Vector< std::unique_ptr< MultiFab >> &, const Real *dptr_u_geos, const Real *dptr_v_geos, const Real *dptr_wbar_sub, const Vector< Real * > d_rayleigh_ptrs_at_lev, const amrex::Real *d_sinesq_at_lev, const amrex::Real *d_sinesq_stag_at_lev, const Vector< Real * > d_sponge_ptrs_at_lev, const Vector< MultiFab > *forecast_state_at_lev, InputSoundingData &input_sounding_data, bool is_slow_step)
Definition: ERF_MakeMomSources.cpp:37
void make_sources(int level, int, Real dt, Real time, const Vector< MultiFab > &S_data, const MultiFab &S_prim, MultiFab &source, const MultiFab &base_state, const MultiFab *z_phys_cc, const MultiFab &xvel, const MultiFab &yvel, const MultiFab *qheating_rates, MultiFab *terrain_blank, const Geometry geom, const SolverChoice &solverChoice, Vector< std::unique_ptr< MultiFab >> &mapfac, const Real *dptr_rhotheta_src, const Real *dptr_rhoqt_src, const Real *dptr_wbar_sub, const Vector< Real * > d_rayleigh_ptrs_at_lev, const Real *d_sinesq_at_lev, InputSoundingData &input_sounding_data, TurbulentPerturbation &turbPert, bool is_slow_step)
Definition: ERF_MakeSources.cpp:33
amrex::Real Real
Definition: ERF_ShocInterface.H:19
void erf_substep_MT(int step, int, int level, int finest_level, Vector< MultiFab > &S_slow_rhs, const Vector< MultiFab > &S_prev, Vector< MultiFab > &S_stg_data, const MultiFab &S_stg_prim, const MultiFab &qt, const MultiFab &pi_stage, const MultiFab &fast_coeffs, Vector< MultiFab > &S_data, MultiFab &lagged_delta_rt, MultiFab &avg_xmom, MultiFab &avg_ymom, MultiFab &avg_zmom, const MultiFab &cc_src, const MultiFab &xmom_src, const MultiFab &ymom_src, const MultiFab &zmom_src, const Geometry geom, const Real gravity, const bool use_lagged_delta_rt, std::unique_ptr< MultiFab > &z_t_rk, const MultiFab *z_t_pert, std::unique_ptr< MultiFab > &z_phys_nd_old, std::unique_ptr< MultiFab > &z_phys_nd_new, std::unique_ptr< MultiFab > &z_phys_nd_stg, std::unique_ptr< MultiFab > &detJ_cc_old, std::unique_ptr< MultiFab > &detJ_cc_new, std::unique_ptr< MultiFab > &detJ_cc_stg, const Real dtau, const Real beta_s, const Real facinv, Vector< std::unique_ptr< MultiFab >> &mapfac, YAFluxRegister *fr_as_crse, YAFluxRegister *fr_as_fine, bool l_use_moisture, bool l_reflux)
Definition: ERF_Substep_MT.cpp:49
void erf_substep_NS(int step, int nrk, int level, int finest_level, Vector< MultiFab > &S_slow_rhs, const Vector< MultiFab > &S_prev, Vector< MultiFab > &S_stage_data, const MultiFab &S_stage_prim, const MultiFab &qt, const MultiFab &pi_stage, const MultiFab &fast_coeffs, Vector< MultiFab > &S_data, MultiFab &lagged_delta_rt, MultiFab &avg_xmom, MultiFab &avg_ymom, MultiFab &avg_zmom, const MultiFab &cc_src, const MultiFab &xmom_src, const MultiFab &ymom_src, const MultiFab &zmom_src, const Geometry geom, const Real gravity, amrex::Gpu::DeviceVector< amrex::Real > &stretched_dz_d, const Real dtau, const Real beta_s, const Real facinv, Vector< std::unique_ptr< MultiFab >> &mapfac, YAFluxRegister *fr_as_crse, YAFluxRegister *fr_as_fine, bool l_use_moisture, bool l_reflux, const amrex::Real *sinesq_stag_d, const Real l_damp_coef)
Definition: ERF_Substep_NS.cpp:42
void erf_substep_T(int step, int, int level, int finest_level, Vector< MultiFab > &S_slow_rhs, const Vector< MultiFab > &S_prev, Vector< MultiFab > &S_stage_data, const MultiFab &S_stage_prim, const MultiFab &qt, const MultiFab &pi_stage, const MultiFab &fast_coeffs, Vector< MultiFab > &S_data, MultiFab &lagged_delta_rt, MultiFab &avg_xmom, MultiFab &avg_ymom, MultiFab &avg_zmom, const MultiFab &cc_src, const MultiFab &xmom_src, const MultiFab &ymom_src, const MultiFab &zmom_src, const Geometry geom, const Real gravity, std::unique_ptr< MultiFab > &z_phys_nd, std::unique_ptr< MultiFab > &detJ_cc, const Real dtau, const Real beta_s, const Real facinv, Vector< std::unique_ptr< MultiFab >> &mapfac, YAFluxRegister *fr_as_crse, YAFluxRegister *fr_as_fine, bool l_use_moisture, bool l_reflux, const Real *sinesq_stag_d, const Real l_damp_coef)
Definition: ERF_Substep_T.cpp:43
auto acoustic_substepping_fun
Definition: ERF_TI_substep_fun.H:6
auto apply_bcs
Definition: ERF_TI_utils.H:73
auto update_terrain_substep
Definition: ERF_TI_utils.H:238
@ num_comps
Definition: ERF_IndexDefines.H:68
@ ymom
Definition: ERF_IndexDefines.H:160
@ cons
Definition: ERF_IndexDefines.H:158
@ zmom
Definition: ERF_IndexDefines.H:161
@ xmom
Definition: ERF_IndexDefines.H:159
@ qt
Definition: ERF_Kessler.H:27
static MeshType mesh_type
Definition: ERF_DataStruct.H:983