ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_TI_slow_rhs_pre.H
Go to the documentation of this file.
1 #include "ERF_SrcHeaders.H"
2 
3 #ifdef ERF_USE_FIRE
4 #include <ERF_FireCoupler.H>
5 #endif
6 
7 /**
8  * Wrapper for calling the routine that creates the slow RHS
9  */
10  auto slow_rhs_fun_pre = [&,zero_d=zero,one_d=one](Vector<MultiFab>& S_rhs,
11  Vector<MultiFab>& S_old,
12  Vector<MultiFab>& S_data,
13  const double old_step_time,
14  const double old_stage_time,
15  const double new_stage_time,
16  const int nrk)
17  {
18  BL_PROFILE("slow_rhs_fun_pre");
19  //
20  // Define primitive variables for all later RK stages
21  // (We have already done this for the first RK step)
22  // Note that it is essential this happen before the call to make_mom_sources
23  // because some of the buoyancy routines use the primitive variables
24  //
25  if (nrk > 0) {
26  int ng_cons = S_data[IntVars::cons].nGrow();
27  cons_to_prim(S_data[IntVars::cons], S_prim, ng_cons);
29  }
30 
31  if (verbose) Print() << std::setprecision(timeprecision)
32  << "Making slow rhs at time " << old_stage_time
33  << " for fast variables advancing from " << old_step_time
34  << " to " << new_stage_time << std::endl;
35 
36  double slow_dt = new_stage_time - old_step_time;
37 
38  const GpuArray<Real, AMREX_SPACEDIM> dxInv = fine_geom.InvCellSizeArray();
39 
40  // *************************************************************************
41  // Set up flux registers if using two_way coupling
42  // *************************************************************************
43  YAFluxRegister* fr_as_crse = nullptr;
44  YAFluxRegister* fr_as_fine = nullptr;
45  if (solverChoice.coupling_type == CouplingType::TwoWay && finest_level > 0) {
46  if (level < finest_level) {
47  fr_as_crse = getAdvFluxReg(level+1);
48  // Once per step: the RK2 integrator of anelastic and pseudo-incompressible levels
49  // registers fluxes at both stages
50  // (RefluxStageWeight)
51  if (nrk == 0) { fr_as_crse->reset(); }
52  }
53  if (level > 0) {
54  fr_as_fine = getAdvFluxReg(level);
55  }
56  }
57 
58  // *************************************************************************
59  // Get multifab pointers
60  // *************************************************************************
61 
62  // Canopy data for mom sources
63  MultiFab* forest_drag = (solverChoice.do_forest_drag) ?
64  m_forest_drag[level]->get_drag_field() : nullptr;
65  MultiFab* frontal_area = (solverChoice.do_forest_drag) ?
66  m_forest_drag[level]->get_frontal_area() : nullptr;
67 
68  // Immersed Forcing
69  MultiFab* terrain_blank = (solverChoice.terrain_type == TerrainType::ImmersedForcing ||
70  solverChoice.buildings_type == BuildingsType::ImmersedForcing) ?
71  terrain_blanking[level].get() : nullptr;
72  MultiFab* terrain_blank_xface = (solverChoice.terrain_type == TerrainType::ImmersedForcing ||
73  solverChoice.buildings_type == BuildingsType::ImmersedForcing) ?
74  terrain_blanking_xface[level].get() : nullptr;
75  MultiFab* terrain_blank_yface = (solverChoice.terrain_type == TerrainType::ImmersedForcing ||
76  solverChoice.buildings_type == BuildingsType::ImmersedForcing) ?
77  terrain_blanking_yface[level].get() : nullptr;
78  MultiFab* terrain_blank_zface = (solverChoice.terrain_type == TerrainType::ImmersedForcing ||
79  solverChoice.buildings_type == BuildingsType::ImmersedForcing) ?
80  terrain_blanking_zface[level].get() : nullptr;
81 
82  // Update the total moisture variable *before* computing sources since this is used in
83  // the buoyancy calculation
84  if (solverChoice.moisture_type != MoistureType::None) {
85  int n_qstate_into_total = micro->Get_Qstate_Moist_Size() - micro->Get_Qstate_Moist_NumConc_Size();
86  make_qt(S_data[IntVars::cons], qt, n_qstate_into_total);
87  }
88 
89  MultiFab p0_to_use, base_to_use;
90  MultiFab *zpn_to_use, *zpc_to_use, *ax_to_use, *ay_to_use, *az_to_use, *dJ_to_use;
91 
92  // Moving terrain
93  std::unique_ptr<MultiFab> z_t_pert;
94  if ( solverChoice.terrain_type == TerrainType::MovingFittedMesh )
95  {
96  z_t_pert = std::make_unique<MultiFab>(S_data[IntVars::zmom].boxArray(), S_data[IntVars::zmom].DistributionMap(), 1, 1);
97  update_terrain_stage(level, old_step_time, old_stage_time, new_stage_time, slow_dt);
98 
99  p0_to_use = MultiFab(base_state_new[level], make_alias, BaseState::p0_comp, 1);
100  base_to_use = MultiFab(base_state_new[level], make_alias, 0, BaseState::num_comps);
101  zpn_to_use = z_phys_nd_src[level].get();
102  zpc_to_use = z_phys_cc_src[level].get();
103  ax_to_use = ax_src[level].get();
104  ay_to_use = ay_src[level].get();
105  az_to_use = az_src[level].get();
106  dJ_to_use = detJ_cc_src[level].get();
107 
108  } else {
109  p0_to_use = MultiFab(base_state[level], make_alias, BaseState::p0_comp, 1);
110  base_to_use = MultiFab(base_state[level], make_alias, 0, BaseState::num_comps);
111  zpn_to_use = z_phys_nd[level].get();
112  zpc_to_use = z_phys_cc[level].get();
113  ax_to_use = ax[level].get();
114  ay_to_use = ay[level].get();
115  az_to_use = az[level].get();
116  dJ_to_use = detJ_cc[level].get();
117  }
118 
119  // Get planar averages from persistent storage for immersed forcing
120  bool l_use_IF = (solverChoice.terrain_type == TerrainType::ImmersedForcing ||
121  solverChoice.buildings_type == BuildingsType::ImmersedForcing);
122  Table1D<Real> r_avg_to_pass = l_use_IF ? r_plane_avg[level].table() : Table1D<Real>();
123  Table1D<Real> t_avg_to_pass = l_use_IF ? t_plane_avg[level].table() : Table1D<Real>();
124 
125  // *****************************************************************************
126  // Construct the source terms for the cell-centered (conserved) variables
127  // *****************************************************************************
128  make_sources(level, nrk, slow_dt, old_stage_time,
129  S_data, S_prim, cc_src, base_state[level], zpc_to_use,
130  xvel_new, yvel_new, zvel_new,
131  qheating_rates[level].get(),
132  terrain_blank, fine_geom, solverChoice,
133  mapfac[level],
134  rhotheta_src[level].get(), rhoqt_src[level].get(),
135  dptr_wbar_sub, d_rayleigh_ptrs_at_lev, d_sinesq_at_lev,
136  turbPert,
137  r_avg_to_pass, t_avg_to_pass,
138  forest_drag, &S_old[IntVars::cons],
139  true, ib_wall_hfx[level].get(), ib_wall_ktarget[level].get(), ib_wall_qfx[level].get());
140 
141 #ifdef ERF_USE_DUST
142  // *****************************************************************************
143  // The dust model's emission, settling and deposition, added AFTER
144  // make_sources, which resets cc_src at every RK stage
145  // *****************************************************************************
146  if (m_dust_model) {
147  m_dust_model->add_sources(level, cc_src, S_data[IntVars::cons], *dJ_to_use, fine_geom, slow_dt);
148  }
149 #endif
150 
151  // *****************************************************************************
152  // A localized source of gas (erf.mass_source), added AFTER make_sources, which
153  // resets cc_src at every RK stage. It goes into the slow RHS of rho and rho*theta,
154  // which the acoustic substeps apply; the substeps' own make_sources call does not see it.
155  // Its geometry (ground heights, composite volume) is redone only when the grids change.
156  // *****************************************************************************
157  if (m_mass_source) {
158  Vector<const MultiFab*> mf_mx(finest_level+1), mf_my(finest_level+1);
159  for (int lev = 0; lev <= finest_level; ++lev) {
160  mf_mx[lev] = mapfac[lev][MapFacType::m_x].get();
161  mf_my[lev] = mapfac[lev][MapFacType::m_y].get();
162  }
163  m_mass_source->update_geometry(finest_level, Geom(), grids, dmap, ref_ratio,
164  z_phys_nd, detJ_cc, mf_mx, mf_my,
165  [this] (int lev, MultiFab& slab) { fill_surface_nodes(lev, slab); });
166  m_mass_source->add_sources(level, cc_src, S_data[IntVars::cons], zpn_to_use,
167  fine_geom, old_stage_time);
168  }
169 
170  // *****************************************************************************
171  // Inject the fire-to-atmosphere heat flux AFTER make_sources, which resets
172  // cc_src at every RK stage. The lagged fire flux (FireCoupler) is constant
173  // across all RK stages and must be added here to survive the integrator.
174  // *****************************************************************************
175 #ifdef ERF_USE_FIRE
176  // on the level the fire grid refines; coarser levels get it by average-down.
177  // The heat and vapor with the WRF-Fire profile, then the smoke tracer,
178  // proportional to the fuel burnt (FireCoupler::add_sources).
179  if (m_fire_coupler && level == m_fire_coupler->level()) {
180  bool has_moisture = (solverChoice.moisture_type != MoistureType::None);
181  m_fire_coupler->add_sources(cc_src,
182  S_old[IntVars::cons],
183  *zpc_to_use,
184  zpn_to_use,
185  fine_geom,
186  has_moisture,
187  istep[level]);
188  }
189 #endif
190 
191  // Heat flux from the building faces (immersed-boundary surface energy
192  // balance), added to the freshly rebuilt source; no-op unless enabled.
193  if (ibseb_params.enable && ibseb_params.couple_heat &&
194  level < static_cast<int>(m_ibseb.size()) && m_ibseb[level]) {
195  m_ibseb[level]->add_heat_flux_to_source(cc_src, S_data[IntVars::cons], fine_geom,
196  solverChoice.c_p, solverChoice.rdOcp);
197  }
198 
199  // Nudging of potential temperature towards observations at stations
200  if (solverChoice.nudging_from_observations && obs_nudging) {
201  obs_nudging->add_theta_source(level, old_stage_time, static_cast<Real>(slow_dt), fine_geom,
202  S_data[IntVars::cons], cc_src, zpn_to_use, terrain_blank);
203  }
204 
205  // *****************************************************************************
206  // Add nudging terms on moist variables if we have a non-zero relaxation region
207  // *****************************************************************************
208 #if defined(ERF_USE_NETCDF)
209  if ( solverChoice.use_real_bcs && (level==0) &&
210  (solverChoice.moisture_type != MoistureType::None) )
211  {
212  Real bdy_factor = solverChoice.bdy_nudge_factor;
213  Real l_rdOcp = solverChoice.rdOcp;
214  Real l_c_p = solverChoice.c_p;
215  int moist_nudge_type = solverChoice.bdy_moist_nudge_type;
216  int num_q = micro->Get_Qstate_Moist_Size() - micro->Get_Qstate_Moist_NumConc_Size();
217  AMREX_ALWAYS_ASSERT( (solverChoice.bdy_moist_nudge_type != 2) ||
218  (solverChoice.moisture_type != MoistureType::Morrison_NoIce &&
219  solverChoice.moisture_type != MoistureType::SAM_NoIce) );
220  add_moist_nudging_terms(S_data[IntVars::cons], cc_src, num_q, slow_dt,
221  old_stage_time, start_time,
222  start_bdy_time, final_bdy_time, bdy_time_interval,
223  bdy_factor, real_width, geom[level],
224  bdy_data_xlo, bdy_data_xhi, bdy_data_ylo, bdy_data_yhi,
225  m_r2d, l_c_p, l_rdOcp,
226  solverChoice.moisture_indices,
227  solverChoice.use_wrf_bdy_density, moist_nudge_type);
228  }
229 #endif
230 
231  // Accumulate fixed-leaf-temperature canopy exchange in the staged
232  // source storage. The existing slow-pre and slow-post paths then
233  // apply rho-theta and rho-qv with their normal RK3 or RK2/MidPoint weighting.
234  if (solverChoice.do_forest_drag && solverChoice.forest_biophysics &&
235  solverChoice.forest_biophysics_heat && frontal_area) {
237  S_data[IntVars::cons],
238  xvel_new, yvel_new, zvel_new,
239  frontal_area, base_to_use,
240  solverChoice);
241  }
242 
243  // *****************************************************************************
244  // Define the pressure gradient
245  // *****************************************************************************
246  make_gradp_pert(level, solverChoice, fine_geom, S_data,
247  base_to_use, qt, *zpc_to_use,
248  mapfac[level],
249  get_eb(level), gradp[level]);
250 
251  // The stage pressure, in the stage whose update the step ends with, for the reflux of
252  // the pressure impulse on the coarse/fine faces (ERF_MomentumReflux.cpp)
253  if (IsRefluxStage(solverChoice, level, finest_level, nrk)) {
254  if (MultiFab* P_reflux = momentum_reflux_pressure(level)) {
255  momentum_reflux_add_slow(level, *P_reflux, S_data[IntVars::cons], p0_to_use, qt,
256  solverChoice.moisture_type != MoistureType::None,
257  RefluxStageWeight(solverChoice, level, finest_level, nrk) *
258  static_cast<Real>(slow_dt));
259  }
260  }
261 
262  // *****************************************************************************
263  // Define the buoyancy forcing term in the z-direction
264  // *****************************************************************************
265  int num_q = micro->Get_Qstate_Moist_Size() - micro->Get_Qstate_Moist_NumConc_Size();
266  make_buoyancy(level, S_data, S_prim, qt, buoyancy, fine_geom, solverChoice, base_to_use,
267  num_q, get_eb(level), solverChoice.compressibility_type[level] == CompressibilityType::Anelastic);
268 
269  // *****************************************************************************
270  // Make remaining (not gradp or buoyancy) momentum sources
271  // *****************************************************************************
272  make_mom_sources(level, old_stage_time, slow_dt,
273  S_data, zpn_to_use, zpc_to_use, stretched_dz_h[level],
274  xvel_new, yvel_new, zvel_new,
275  xmom_src, ymom_src, zmom_src,
276  base_to_use, forest_drag, terrain_blank,
277  terrain_blank_xface, terrain_blank_yface, terrain_blank_zface,
278  cosPhi_m[level].get(), sinPhi_m[level].get(), fine_geom, solverChoice,
279  mapfac[level],
280  (solverChoice.have_geo_wind_profile) ? d_u_geos[level].data(): nullptr,
281  (solverChoice.have_geo_wind_profile) ? d_v_geos[level].data(): nullptr,
282  dptr_wbar_sub, d_rayleigh_ptrs_at_lev, d_sinesq_at_lev, d_sinesq_stag_at_lev,
283  d_sponge_ptrs_at_lev,
284  (solverChoice.hindcast_lateral_forcing? &forecast_state_interp[level] : nullptr),
285  sounding_at_level(level), lsf_at_level(level), lsf_data[level],
286  get_eb(level), true);
287 
288 #ifdef ERF_USE_MOVING_BODIES
289  // The structures' force on the air (erf.structures.model), fixed over the step
290  if (m_structure_model && level == m_structure_model->level(finest_level)) {
291  m_structure_model->add_momentum_sources(level, xmom_src, ymom_src, zmom_src);
292  }
293  // On a coarser level, the model's source restricted onto it (ERF::make_structure_coarse_sources)
294  if (m_structure_model && level < m_structure_model->level(finest_level) &&
295  level < static_cast<int>(m_structure_crse_src.size()) &&
296  m_structure_crse_src[level][0].isDefined()) {
297  MultiFab::Add(xmom_src, m_structure_crse_src[level][0], 0, 0, 1, 0);
298  MultiFab::Add(ymom_src, m_structure_crse_src[level][1], 0, 0, 1, 0);
299  MultiFab::Add(zmom_src, m_structure_crse_src[level][2], 0, 0, 1, 0);
300  }
301 #endif
302 
303  // The impulse of the last momentum reflux onto this level, spread over its steps in
304  // this coarse step (erf.momentum_reflux, ERF_MomentumReflux.cpp)
305  if (momentum_reflux_enabled()) {
306  momentum_reflux_add_sources(level, xmom_src, ymom_src);
307  }
308 
309  // Nudging of the velocity towards observations at stations
310  if (solverChoice.nudging_from_observations && obs_nudging) {
311  obs_nudging->add_momentum_sources(level, old_stage_time, static_cast<Real>(slow_dt), fine_geom,
312  S_data[IntVars::cons], xvel_new, yvel_new, zvel_new,
313  xmom_src, ymom_src, zmom_src, zpn_to_use,
314  terrain_blank_xface, terrain_blank_yface,
315  terrain_blank_zface);
316  }
317 
318  // *****************************************************************************
319  // Add body sources if doing flow around a body
320  // *****************************************************************************
321  add_thin_body_sources(xmom_src, ymom_src, zmom_src,
322  xflux_imask[level], yflux_imask[level], zflux_imask[level],
323  thin_xforce[level], thin_yforce[level], thin_zforce[level]);
324 
325  // *****************************************************************************
326  // Pseudo-incompressible: advect the cell-centered scalars of this stage with the upwind
327  // direction of the projection that made S_data (kept for erf_slow_rhs_post, since the
328  // projection at the end of this stage overwrites pi_upw_dir)
329  // *****************************************************************************
330  Array<const MultiFab*,AMREX_SPACEDIM> upw_dir{AMREX_D_DECL(nullptr,nullptr,nullptr)};
331  const bool l_pi_src = (solverChoice.compressibility_type[level] == CompressibilityType::PseudoIncompressible &&
332  solverChoice.terrain_type != TerrainType::EB);
333  if (l_pi_src) {
334  for (auto* m : {&pi_adv_rt[level], &pi_src[level]}) {
335  if (!*m || (*m)->boxArray() != grids[level] || (*m)->DistributionMap() != dmap[level]) {
336  *m = std::make_unique<MultiFab>(grids[level], dmap[level], 1, 0);
337  (*m)->setVal(zero_d);
338  }
339  }
340  }
341  if (l_pi_src) {
342  define_pi_upw_dir(level);
343  for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
344  MultiFab::Copy(*pi_upw_dir_stage[level][idim], *pi_upw_dir[level][idim], 0, 0, 1, 0);
345  upw_dir[idim] = pi_upw_dir_stage[level][idim].get();
346  }
347  }
348 
349  // *****************************************************************************
350  // Define RHS for rho, rho_theta and momenta
351  // *****************************************************************************
352  erf_slow_rhs_pre(level, finest_level, nrk, slow_dt, S_rhs, S_old, S_data,
353  S_prim, qt, avg_xmom[level], avg_ymom[level], avg_zmom[level], upw_dir,
354  l_pi_src ? pi_adv_rt[level].get() : nullptr,
355  xvel_new, yvel_new, zvel_new,
356  z_t_rk[level], cc_src, xmom_src, ymom_src, zmom_src, buoyancy,
357  (level > 0) ? &zmom_crse_rhs[level] : nullptr,
358  Tau[level], Tau_corr[level], Tau_EB[level],
359  SmnSmn, eddyDiffs, Hfx1, Hfx2, Hfx3, Q1fx1, Q1fx2, Q1fx3, Q2fx3, Diss, mf_surface_fluxes,
360  Hfx3_EB, fine_geom, solverChoice, m_SurfaceLayer, domain_bcs_type_d, domain_bcs_type,
361  *zpn_to_use, *zpc_to_use, *ax_to_use, *ay_to_use, *az_to_use, *dJ_to_use,
362  stretched_dz_d[level], gradp[level],
363  mapfac[level], get_eb(level), physbnd_mask,
364  native_shoc_driver[level].get(),
365  fr_as_crse, fr_as_fine, &base_to_use,
366  cloud_chamber_config.active ? &cloud_chamber_config : nullptr,
367  cloud_chamber_budget.get(),
368  ib_wall_face13[level].get(), ib_wall_face23[level].get(),
369  ib_wall_face33[level].get(), ib_wall_hfx[level].get(),
370  walldist[level].get());
371 
372  // Anelastic and pseudo-incompressible coarse/fine faces in time (ERF_CoarseFineFaces.cpp): a coarse level adds
373  // this stage's mass fluxes to those of its step, a fine level registers the coarse
374  // momenta its coarse/fine faces are filled from in this stage
375  if (solverChoice.compressibility_type[level] != CompressibilityType::FullyCompressible) {
376  if (level < finest_level) { cf_anelastic_record(level, nrk); }
377  if (level > 0) { cf_anelastic_stage_registration(level, nrk); }
378  }
379 
380  if ((solverChoice.vert_implicit_fac[level][nrk] > zero_d) && solverChoice.implicit_before_substep) {
381  const Real stage_dt = static_cast<Real>(slow_dt);
382  /**
383  * @brief Temporary MultiFab for conserved variable updates.
384  */
385  MultiFab scratch(S_data[IntVars::cons].boxArray(),S_data[IntVars::cons].DistributionMap(), 2,
386  S_data[IntVars::cons].nGrowVect());
387  MultiFab::Copy(scratch, S_old[IntVars::cons], 0, 0, 2, S_data[IntVars::cons].nGrowVect()); // scratch := S_old (for rho, rhotheta)
388  MultiFab::Saxpy(scratch, stage_dt, S_rhs[IntVars::cons], 0, 0, 2, 0); // scratch := S_old + stage_dt*Src (for rho, rhotheta)
389  scratch.FillBoundary(geom[level].periodicity());
390 
391  /**
392  * @brief Temporary MultiFab for x-momentum updates.
393  */
394  MultiFab scratch_xmom(S_data[IntVars::xmom].boxArray(),
395  S_data[IntVars::xmom].DistributionMap(), 1,
396  S_data[IntVars::xmom].nGrowVect());
397  /**
398  * @brief Temporary MultiFab for y-momentum updates.
399  */
400  MultiFab scratch_ymom(S_data[IntVars::ymom].boxArray(),
401  S_data[IntVars::ymom].DistributionMap(), 1,
402  S_data[IntVars::ymom].nGrowVect());
403  if (solverChoice.implicit_momentum_diffusion) {
404  MultiFab::Copy(scratch_xmom, S_old[IntVars::xmom], 0, 0, 1, S_data[IntVars::xmom].nGrowVect()); // scratch := S_old
405  MultiFab::Saxpy(scratch_xmom, stage_dt, S_rhs[IntVars::xmom], 0, 0, 1, 0); // scratch := S_old + stage_dt*Src
406  scratch_xmom.FillBoundary(geom[level].periodicity());
407 
408  MultiFab::Copy(scratch_ymom, S_old[IntVars::ymom], 0, 0, 1, S_data[IntVars::ymom].nGrowVect()); // scratch := S_old
409  MultiFab::Saxpy(scratch_ymom, stage_dt, S_rhs[IntVars::ymom], 0, 0, 1, 0); // scratch := S_old + stage_dt*Src
410  scratch_ymom.FillBoundary(geom[level].periodicity());
411  }
412 
413  // The ghost cells of scratch still hold S_old; at a coarse/fine boundary fill them with
414  // the coarse data at the time we are advancing to, since a column that ends there is
415  // closed with the value on the other side
416  if (level > 0) {
417  const bool l_mom = solverChoice.implicit_momentum_diffusion;
418  Vector<MultiFab*> scratch_mom = {l_mom ? &scratch_xmom : nullptr,
419  l_mom ? &scratch_ymom : nullptr,
420  nullptr};
421  FillCoarseFineGhostsForImplicit(level, new_stage_time, scratch, {RhoTheta_comp}, scratch_mom);
422  }
423 
424 #include "ERF_ImplicitPre.H"
425 
426  // NOTE: the implicit part of the vertical (rho theta) diffusion is not added to the flux
427  // registers. (rho theta) is refluxed only on anelastic and pseudo-incompressible
428  // levels, and there no stage whose fluxes are registered solves implicitly: RK2
429  // turns the solve off, and
430  // MidPoint solves in stage 0 but registers stage 1 (see SolverChoice and
431  // StageFluxWeight).
432 
433  MultiFab::Saxpy(scratch, -one_d, S_old[IntVars::cons], 1, 1, 1, 0); // scratch := (S_new - S_old) (for rhotheta only)
434  scratch.mult(one_d / stage_dt); // scratch := (S_new - S_old) / stage_dt
435  MultiFab::Copy(S_rhs[IntVars::cons], scratch, 1, 1, 1, 0); // slow_rhs := (S_new - S_old) / stage_dt (for rhotheta only)
436 
437  if (solverChoice.implicit_momentum_diffusion) {
438  MultiFab::Saxpy(scratch_xmom, -one_d, S_old[IntVars::xmom], 0, 0, 1, 0); // scratch := (S_new - S_old)
439  scratch_xmom.mult(one_d / stage_dt); // scratch := (S_new - S_old) / stage_dt
440  MultiFab::Copy(S_rhs[IntVars::xmom], scratch_xmom, 0, 0, 1, 0); // slow_rhs := (S_new - S_old) / stage_dt
441 
442  MultiFab::Saxpy(scratch_ymom, -one_d, S_old[IntVars::ymom], 0, 0, 1, 0); // scratch := (S_new - S_old)
443  scratch_ymom.mult(one_d / stage_dt); // scratch := (S_new - S_old) / stage_dt
444  MultiFab::Copy(S_rhs[IntVars::ymom], scratch_ymom, 0, 0, 1, 0); // slow_rhs := (S_new - S_old) / stage_dt
445  }
446  }
447 
448  if (solverChoice.turbChoice[level].uses_native_shoc() && native_shoc_driver[level]) {
449  // Native SHOC applies its increment directly to state; ERF no
450  // longer adds a separate fast RHS contribution.
451  }
452 
453  // *****************************************************************************
454  // Pseudo-incompressible: the non-advective tendency of (rho theta) in this stage, the
455  // whole (rho theta) RHS (now including any implicit diffusion) less its advective part
456  // *****************************************************************************
457  if (l_pi_src) {
458  MultiFab::Copy (*pi_src[level], S_rhs[IntVars::cons], RhoTheta_comp, 0, 1, 0);
459  MultiFab::Subtract(*pi_src[level], *pi_adv_rt[level], 0, 0, 1, 0);
460  }
461 
462  // *****************************************************************************
463  // Update for moving terrain
464  // *****************************************************************************
465  if ( solverChoice.terrain_type == TerrainType::MovingFittedMesh )
466  {
467  // The column update below works on valid cells, reading only the cells above and below in the
468  // same column, so it needs no ghost-cell fill first. The ghost planes below and above the
469  // column keep the values the initialization gave them, which are finite (the base-state BCs
470  // leave them alone on a moving mesh, see ERFPhysBCFunct_base): the bottom-face flux is
471  // extrapolated from the three lowest cells, and the top-face flux is zero (z_t = 0 there).
472 
473  MultiFab r_hse_new (base_state_new[level], make_alias, BaseState::r0_comp, 1);
474  MultiFab p_hse_new (base_state_new[level], make_alias, BaseState::p0_comp, 1);
475  MultiFab pi_hse_new (base_state_new[level], make_alias, BaseState::pi0_comp, 1);
476  MultiFab th_hse_new (base_state_new[level], make_alias, BaseState::th0_comp, 1);
477 
478  MultiFab* r0_new = &r_hse_new;
479  MultiFab* p0_new = &p_hse_new;
480  MultiFab* pi0_new = &pi_hse_new;
481  MultiFab* th0_new = &th_hse_new;
482 
483  // We define and evolve (rho theta)_0 in order to re-create p_0 in a way that is consistent
484  // with our update of (rho theta) but does NOT maintain dp_0 / dz = -rho_0 g. This is why
485  // we no longer discretize the vertical pressure gradient in perturbational form.
486  MultiFab rt0(p0->boxArray(),p0->DistributionMap(),1,1);
487  MultiFab rt0_new(p0->boxArray(),p0->DistributionMap(),1,1);
488  MultiFab r0_temp(p0->boxArray(),p0->DistributionMap(),1,1);
489 
490  // Remember this does NOT maintain dp_0 / dz = -rho_0 g, so we can no longer
491  // discretize the vertical pressure gradient in perturbational form.
492  AMREX_ALWAYS_ASSERT(solverChoice.advChoice.dycore_horiz_adv_type == AdvType::Centered_2nd);
493  AMREX_ALWAYS_ASSERT(solverChoice.advChoice.dycore_vert_adv_type == AdvType::Centered_2nd);
494 
495  double dt_base = new_stage_time - old_step_time;
496 
497  const Real l_rdOcp = solverChoice.rdOcp;
498 
499 #ifdef _OPENMP
500 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
501 #endif
502  // Tile only in x and y: the second kernel reads r0_tmp and rt0_tmp at k-1 and k+1,
503  // which the first kernel fills tile by tile, so a tile boundary in z would read
504  // values another thread may not have written yet
505  for ( MFIter mfi(*p0,TileNoZ()); mfi.isValid(); ++mfi)
506  {
507  const Array4<Real > rt0_arr = rt0.array(mfi);
508  const Array4<Real > rt0_tmp_arr = rt0_new.array(mfi);
509 
510  const Array4<Real const> r0_arr = r0->const_array(mfi);
511  const Array4<Real > r0_new_arr = r0_new->array(mfi);
512  const Array4<Real > r0_tmp_arr = r0_temp.array(mfi);
513 
514  const Array4<Real const> p0_arr = p0->const_array(mfi);
515  const Array4<Real > p0_new_arr = p0_new->array(mfi);
516  const Array4<Real > pi0_new_arr = pi0_new->array(mfi);
517  const Array4<Real > th0_new_arr = th0_new->array(mfi);
518 
519  const Array4<Real >& z_t_arr = z_t_rk[level]->array(mfi);
520 
521  const Array4<Real const>& dJ_old_arr = detJ_cc[level]->const_array(mfi);
522  const Array4<Real const>& dJ_new_arr = detJ_cc_new[level]->const_array(mfi);
523  const Array4<Real const>& dJ_src_arr = detJ_cc_src[level]->const_array(mfi);
524 
525  // Valid cells and the planes above and below them, which the fluxes read
526  Box gbx = mfi.tilebox(); gbx.grow(2,1);
527  amrex::ParallelFor(gbx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
528  {
529  rt0_arr(i,j,k) = getRhoThetagivenP(p0_arr(i,j,k));
530  rt0_tmp_arr(i,j,k) = getRhoThetagivenP(p0_new_arr(i,j,k));
531  r0_tmp_arr(i,j,k) = r0_new_arr(i,j,k);
532  });
533 
534  const int klo_base = fine_geom.Domain().smallEnd(2);
535  // Valid cells only; the ghost cells are filled from them below. Every grid spans the
536  // full height of the domain (checked in ERF::ReadParameters), so the three lowest cells
537  // the bottom-face extrapolation reads are in this box
538  Box gbx2 = mfi.tilebox();
539  amrex::ParallelFor(gbx2, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
540  {
541  Real zflux_r_lo = -z_t_arr(i,j,k ) * myhalf * (r0_tmp_arr(i,j,k) + r0_tmp_arr(i,j,k-1));
542  Real zflux_r_hi = -z_t_arr(i,j,k+1) * myhalf * (r0_tmp_arr(i,j,k) + r0_tmp_arr(i,j,k+1));
543  if (k == klo_base) {
544  // The ghost cell below klo is not on the new mesh (the vertical ghost planes of the
545  // base state are not filled on a moving mesh), so the average above is not a
546  // bottom-face value; use the quadratic extrapolation to the bottom face from the
547  // three lowest cells, of equal spacing in the computational coordinate. At the
548  // top face z_t = 0, so the ghost cell above the column only has to be finite.
549  zflux_r_lo = -z_t_arr(i,j,k) *
550  ( Real(1.875)*r0_tmp_arr(i,j,k) - Real(1.25)*r0_tmp_arr(i,j,k+1)
551  + Real(0.375)*r0_tmp_arr(i,j,k+2) );
552  }
553 
554  Real zflux_rt_lo = zflux_r_lo * myhalf * (rt0_tmp_arr(i,j,k)/r0_tmp_arr(i,j,k) + rt0_tmp_arr(i,j,k-1)/r0_tmp_arr(i,j,k-1));
555  Real zflux_rt_hi = zflux_r_hi * myhalf * (rt0_tmp_arr(i,j,k)/r0_tmp_arr(i,j,k) + rt0_tmp_arr(i,j,k+1)/r0_tmp_arr(i,j,k+1));
556  if (k == klo_base) {
557  // theta_0 at the bottom face by the same extrapolation
558  zflux_rt_lo = zflux_r_lo *
559  ( Real(1.875)*(rt0_tmp_arr(i,j,k )/r0_tmp_arr(i,j,k ))
560  - Real(1.25)*(rt0_tmp_arr(i,j,k+1)/r0_tmp_arr(i,j,k+1))
561  + Real(0.375)*(rt0_tmp_arr(i,j,k+2)/r0_tmp_arr(i,j,k+2)) );
562  }
563 
564  Real invdetJ = one / dJ_src_arr(i,j,k);
565 
566  Real src_r = - invdetJ * ( zflux_r_hi - zflux_r_lo ) * dxInv[2];
567  Real src_rt = - invdetJ * ( zflux_rt_hi - zflux_rt_lo ) * dxInv[2];
568 
569  Real dt_b = static_cast<Real>(dt_base);
570  Real rho0_new = dJ_old_arr(i,j,k) * r0_arr(i,j,k) + dt_b * dJ_src_arr(i,j,k) * src_r;
571  Real rt0_tmp_new = dJ_old_arr(i,j,k) * rt0_arr(i,j,k) + dt_b * dJ_src_arr(i,j,k) * src_rt;
572 
573  r0_new_arr(i,j,k) = rho0_new / dJ_new_arr(i,j,k);
574  rt0_tmp_new /= dJ_new_arr(i,j,k);
575 
576  p0_new_arr(i,j,k) = getPgivenRTh(rt0_tmp_new);
577  pi0_new_arr(i,j,k) = getExnergivenRTh(rt0_tmp_new, l_rdOcp);
578  th0_new_arr(i,j,k) = rt0_tmp_new / r0_new_arr(i,j,k);
579  });
580  } // MFIter
581  // Lateral and periodic ghost cells of the updated base state, on the new mesh (the functor
582  // starts with the periodic and interior FillBoundary). The vertical
583  // ghost planes are again left alone: nothing that uses this base state on a moving mesh
584  // changes the solution through them. The pressure gradient, buoyancy and cell-centered
585  // sources read only cells inside the domain; the momentum sponge reads the plane below at
586  // the bottom face, where w is set by the boundary condition instead.
587  (*physbcs_base_new[level])(base_state_new[level],0,BaseState::num_comps,base_state_new[level].nGrowVect());
588  }
589 
590 #ifdef ERF_USE_NETCDF
591  // Populate RHS for relaxation zones if using real bcs
592  if (solverChoice.use_real_bcs && (level == 0)) {
593  const Real bdy_factor = solverChoice.bdy_nudge_factor;
594  if (real_width>0) {
595  //
596  // Note that old_stage_time is elapsed time; start_bdy_time and final_bdy_time
597  // are total time
598  //
599  Real l_rdOcp = solverChoice.rdOcp;
600  Real l_c_p = solverChoice.c_p;
601  realbdy_compute_interior_ghost_rhs(old_stage_time, start_time, slow_dt,
602  start_bdy_time, final_bdy_time, bdy_time_interval,
603  bdy_factor, real_width, fine_geom,
604  S_rhs, S_data,
605  bdy_data_xlo, bdy_data_xhi,
606  bdy_data_ylo, bdy_data_yhi,
607  m_r2d, l_c_p, l_rdOcp,
608  solverChoice.use_wrf_bdy_density,
609  solverChoice.bdy_rho_nudge_factor);
610  }
611  }
612 #endif
613  }; // end slow_rhs_fun_pre
void add_thin_body_sources(MultiFab &xmom_src, MultiFab &ymom_src, MultiFab &zmom_src, std::unique_ptr< iMultiFab > &xflux_imask_lev, std::unique_ptr< iMultiFab > &yflux_imask_lev, std::unique_ptr< iMultiFab > &zflux_imask_lev, std::unique_ptr< MultiFab > &thin_xforce_lev, std::unique_ptr< MultiFab > &thin_yforce_lev, std::unique_ptr< MultiFab > &thin_zforce_lev)
Definition: ERF_AddThinBodySources.cpp:27
AMREX_INLINE void AddCanopyBiophysicsHeatSources(amrex::MultiFab &cell_source, const amrex::MultiFab &S_data, const amrex::MultiFab &xvel, const amrex::MultiFab &yvel, const amrex::MultiFab &zvel, const amrex::MultiFab *frontal_area, const amrex::MultiFab &base_state, const SolverChoice &solver_choice)
Definition: ERF_CanopyBiophysics.H:28
@ m_y
Definition: ERF_DataStruct.H:41
@ m_x
Definition: ERF_DataStruct.H:40
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real getRhoThetagivenP(const amrex::Real p, const amrex::Real qv=amrex::Real(0))
Definition: ERF_EOS.H:172
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real getExnergivenRTh(const amrex::Real rhotheta, const amrex::Real rdOcp, const amrex::Real qv=amrex::Real(0))
Definition: ERF_EOS.H:156
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real getPgivenRTh(const amrex::Real rhotheta, const amrex::Real qv=amrex::Real(0))
Definition: ERF_EOS.H:81
The atmosphere side of the fire coupling, shared by every fire engine.
#define RhoTheta_comp
Definition: ERF_IndexDefines.H:62
@ Centered_2nd
pp_for_pert_vels get("prob_type", prob_type)
amrex::GpuArray< Real, AMREX_SPACEDIM > dxInv
Definition: ERF_InitCustomPertVels_ParticleTests.H:16
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
void realbdy_compute_interior_ghost_rhs(const double &elapsed_time, const double &start_time, const double &delta_t_d, const double &start_bdy_time, const double &final_bdy_time, const double &bdy_time_interval, const Real &nudge_factor, int width, const Geometry &geom, Vector< MultiFab > &S_rhs, Vector< MultiFab > &S_cur_data, Vector< Vector< FArrayBox >> &bdy_data_xlo, Vector< Vector< FArrayBox >> &bdy_data_xhi, Vector< Vector< FArrayBox >> &bdy_data_ylo, Vector< Vector< FArrayBox >> &bdy_data_yhi, std::unique_ptr< ReadBndryPlanes > &m_r2d, const Real &c_p, const Real &rdOcp, const bool use_wrf_bdy_density, const Real &bdy_rho_nudge_factor)
Definition: ERF_InteriorGhostCells.cpp:162
void make_buoyancy(int lev, const Vector< MultiFab > &S_data, const MultiFab &S_prim, const MultiFab &qt, MultiFab &buoyancy, const Geometry geom, const SolverChoice &solverChoice, const MultiFab &base_state, const int n_qstate, const eb_ &ebfact, const int anelastic)
Definition: ERF_MakeBuoyancy.cpp:36
void make_gradp_pert(int level, const SolverChoice &solverChoice, const Geometry &geom, Vector< MultiFab > &S_data, const MultiFab &base_state, const MultiFab &qt, const MultiFab &z_phys_cc, Vector< std::unique_ptr< MultiFab >> &mapfac, const eb_ &ebfact, Vector< MultiFab > &gradp)
Definition: ERF_MakeGradP.cpp:122
void make_mom_sources(int level, double time_d, double dt, 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 *terrain_blank_xface, MultiFab *terrain_blank_yface, MultiFab *terrain_blank_zface, 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, const InputSoundingData &input_sounding_data, const LargeScaleForcingData &lsf_data, std::unique_ptr< amrex::MultiFab > &lsf_tendencies, const eb_ &ebfact, bool is_slow_step)
Definition: ERF_MakeMomSources.cpp:41
void make_sources(int level, int, double dt, double time_d, 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 &zvel, const MultiFab *qheating_rates, MultiFab *terrain_blank, const Geometry geom, const SolverChoice &solverChoice, Vector< std::unique_ptr< MultiFab >> &mapfac, const MultiFab *rhotheta_src, const MultiFab *rhoqt_src, const Real *dptr_wbar_sub, const Vector< Real * > d_rayleigh_ptrs_at_lev, const Real *d_sinesq_at_lev, TurbulentPerturbation &turbPert, const Table1D< Real > r_plane_avg, const Table1D< Real > t_plane_avg, const MultiFab *forest_drag, const MultiFab *cons_old, bool is_slow_step, MultiFab *ib_wall_hfx, MultiFab *ib_wall_ktarget, MultiFab *ib_wall_qfx)
Definition: ERF_MakeSources.cpp:39
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 one
Definition: ERF_NumericalConstants.H:37
constexpr amrex::Real zero
Definition: ERF_NumericalConstants.H:36
constexpr amrex::Real myhalf
Definition: ERF_NumericalConstants.H:41
void erf_slow_rhs_pre(int level, int finest_level, int nrk, double dt, Vector< MultiFab > &S_rhs, Vector< MultiFab > &S_old, Vector< MultiFab > &S_data, const MultiFab &S_prim, const MultiFab &qt, MultiFab &avg_xmom, MultiFab &avg_ymom, MultiFab &avg_zmom, const Array< const MultiFab *, AMREX_SPACEDIM > &upw_dir, MultiFab *pi_adv_rt, const MultiFab &xvel, const MultiFab &yvel, const MultiFab &zvel, std::unique_ptr< MultiFab > &z_t_mf, const MultiFab &cc_src, const MultiFab &xmom_src, const MultiFab &ymom_src, const MultiFab &zmom_src, const MultiFab &buoyancy, const MultiFab *zmom_crse_rhs, Vector< std::unique_ptr< MultiFab >> &Tau_lev, Vector< std::unique_ptr< MultiFab >> &Tau_corr_lev, Vector< Vector< std::unique_ptr< MultiFab >>> &Tau_EB, MultiFab *SmnSmn, MultiFab *eddyDiffs, MultiFab *Hfx1, MultiFab *Hfx2, MultiFab *Hfx3, MultiFab *Q1fx1, MultiFab *Q1fx2, MultiFab *Q1fx3, MultiFab *Q2fx3, MultiFab *, MultiFab *mf_surface_fluxes, MultiFab *Hfx3_EB, const Geometry geom, const SolverChoice &solverChoice, const amrex::Vector< std::unique_ptr< SurfaceLayer >> &SurfLayer, const Gpu::DeviceVector< BCRec > &domain_bcs_type_d, const Vector< BCRec > &domain_bcs_type_h, const MultiFab &z_phys_nd, const MultiFab &z_phys_cc, const MultiFab &ax, const MultiFab &ay, const MultiFab &az, const MultiFab &detJ, Gpu::DeviceVector< Real > &stretched_dz_d, Vector< MultiFab > &gradp, Vector< std::unique_ptr< MultiFab >> &mapfac, const eb_ &ebfact, const Vector< iMultiFab > &physbnd_mask, ShocDriver *native_shoc_lev, YAFluxRegister *fr_as_crse, YAFluxRegister *fr_as_fine, const MultiFab *cloud_chamber_base_state, const erf_cloud_chamber::Config *cloud_chamber_config, CloudChamberBudget *cloud_budget, const MultiFab *ib_wall_face13, const MultiFab *ib_wall_face23, const MultiFab *ib_wall_face33, const MultiFab *ib_wall_hfx, const MultiFab *walldist)
Definition: ERF_SlowRhsPre.cpp:76
bool IsRefluxStage(const SolverChoice &solverChoice, int level, int finest_level, int nrk)
Definition: ERF_TI_slow_headers.H:91
amrex::Real RefluxStageWeight(const SolverChoice &solverChoice, int level, int finest_level, int nrk)
Definition: ERF_TI_slow_headers.H:78
auto slow_rhs_fun_pre
Definition: ERF_TI_slow_rhs_pre.H:10
auto make_pi_stage
Definition: ERF_TI_utils.H:4
auto update_terrain_stage
Definition: ERF_TI_utils.H:137
AMREX_FORCE_INLINE amrex::IntVect TileNoZ()
Definition: ERF_TileNoZ.H:11
void cons_to_prim(const MultiFab &cons_state, MultiFab &S_prim, int ng)
Definition: ERF_Utils.cpp:17
void make_qt(const MultiFab &cons_state, MultiFab &qt, int n_qstate_into_total)
Definition: ERF_Utils.cpp:112
@ num_comps
Definition: ERF_IndexDefines.H:114
@ pi0_comp
Definition: ERF_IndexDefines.H:111
@ p0_comp
Definition: ERF_IndexDefines.H:110
@ th0_comp
Definition: ERF_IndexDefines.H:112
@ r0_comp
Definition: ERF_IndexDefines.H:109
@ ymom
Definition: ERF_IndexDefines.H:251
@ cons
Definition: ERF_IndexDefines.H:249
@ zmom
Definition: ERF_IndexDefines.H:252
@ xmom
Definition: ERF_IndexDefines.H:250
@ None
Definition: ERF_LSM_Types.H:6
@ qt
Definition: ERF_Kessler.H:30
real(c_double), parameter p0
Definition: ERF_module_model_constants.F90:40
real(kind=kind_phys), parameter, private r0
Definition: ERF_module_mp_wdm6.F90:75
Definition: ERF_ObsNudging.cpp:22