ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_TI_utils.H
Go to the documentation of this file.
1 /**
2  * Define the Exner pressure at this RK stage, and the pressure for the acoustic sub-step on a moving mesh
3  */
4  auto make_pi_stage = [&](const MultiFab& cons_state)
5  {
6  BL_PROFILE("make_pi_stage()");
7 
8 #ifdef _OPENMP
9 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
10 #endif
11  for (MFIter mfi(cons_state,TilingIfNotGPU()); mfi.isValid(); ++mfi)
12  {
13  //
14  // We only use one ghost cell of pi_stage so we only fill one here
15  //
16  const Box& gbx1 = mfi.growntilebox(1);
17 
18  const Array4<const Real>& cons_arr = cons_state.const_array(mfi);
19  const Array4< Real>& pi_stage_arr = pi_stage.array(mfi);
20  const Real rdOcp = solverChoice.rdOcp;
21  const bool l_use_moisture = (solverChoice.moisture_type != MoistureType::None);
22 
23  amrex::ParallelFor(gbx1, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
24  {
25  auto qv_for_p = (l_use_moisture) ? cons_arr(i,j,k,RhoQ1_comp)/cons_arr(i,j,k,Rho_comp) : zero;
26  pi_stage_arr(i,j,k) = getExnergivenRTh(cons_arr(i,j,k,RhoTheta_comp), rdOcp, qv_for_p);
27  });
28 
29  // The complete pressure of the stage state, from the function that make_gradp_pert uses for the
30  // slow tendency. The stage state does not change during the sub-steps, so erf_substep_MT takes
31  // it from here
32  if (l_need_p_stage) {
33  const Array4<Real>& p_stage_arr = p_stage.array(mfi);
34  amrex::ParallelFor(gbx1, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
35  {
36  auto qv_for_p = (l_use_moisture) ? cons_arr(i,j,k,RhoQ1_comp)/cons_arr(i,j,k,Rho_comp) : zero;
37  p_stage_arr(i,j,k) = getPgivenRTh(cons_arr(i,j,k,RhoTheta_comp), qv_for_p);
38  });
39  }
40  } // mfi
41  };
42 
43 /**
44  * This routine is called after the scalars are updated for each RK stage
45  */
46  auto apply_bcs = [&](Vector<MultiFab>& S_data,
47  const double time_for_fp, int ng_cons, int ng_vel,
48  bool fast_only, bool vel_and_mom_synced)
49  {
50  BL_PROFILE("apply_bcs()");
51 
52  int scomp_cons;
53  int ncomp_cons;
54  bool cons_only;
55 
56  int ng_cons_to_use;
57 
58  // **********************************************************************************
59  // Because momentum is updated in the time-stepping routine, but boundary conditions
60  // are imposed on velocities, we must first update the velocity from the momentum
61  // before applying bcs.
62  // **********************************************************************************
63  if (!vel_and_mom_synced) {
64 
65  // **********************************************************************************
66  // Call FillPatch routines for the density only because we need it to convert between
67  // momentum and velocity
68  // This fills ghost cells/faces from
69  // 1) coarser level if lev > 0
70  // 2) physical boundaries
71  // 3) other grids at the same level
72  // **********************************************************************************
73 
74  // We must have at least one ghost cell of density to convert from momentum to velocity
75  // on the valid region
76  AMREX_ALWAYS_ASSERT (ng_cons >= 1);
77 
78  // We must have at least one extra ghost cell of density to convert from velocity to momentum
79  // on the valid region
80  ng_cons_to_use = std::max(ng_cons, ng_vel+1);
81 
82  scomp_cons = 0;
83  ncomp_cons = 1;
84  cons_only = true;
85  // **********************************************************************************
86  // Fill ghost cells of density only
87  // **********************************************************************************
88  FillIntermediatePatch(level, time_for_fp,
89  {&S_data[IntVars::cons], &xvel_new, &yvel_new, &zvel_new},
90  {&S_data[IntVars::cons], &S_data[IntVars::xmom],
91  &S_data[IntVars::ymom], &S_data[IntVars::zmom]},
92  ng_cons_to_use, 0, cons_only, scomp_cons, ncomp_cons);
93  }
94 
95  // ***************************************************************************************
96  // Call FillPatch routines for all data except rho which was filled above
97  // This fills ghost cells/faces from
98  // 1) coarser level if lev > 0
99  // 2) physical boundaries
100  // 3) other grids at the same level
101  // ***************************************************************************************
102  if (vel_and_mom_synced) {
103  if (fast_only) {
104  scomp_cons = 0;
105  ncomp_cons = 2; // rho and (rho theta) only
106  } else {
107  scomp_cons = 0;
108  ncomp_cons = S_data[IntVars::cons].nComp();
109  }
110  // We must have at least one extra ghost cell of density to convert from velocity to momentum
111  // on the valid region
112  ng_cons_to_use = std::max(ng_cons, ng_vel+1);
113 
114  } else {
115  if (fast_only) {
116  scomp_cons = 1;
117  ncomp_cons = 1; // (rho theta) only since we filled rho above
118  } else {
119  scomp_cons = 1;
120  ncomp_cons = S_data[IntVars::cons].nComp()-1; // since we filled rho above
121  }
122  ng_cons_to_use = ng_cons;
123  }
124 
125  // **********************************************************************************
126  // NOTE: FillIntermediatePatch takes momenta at the new time, and returns
127  // BOTH updated velocities and momenta
128  // **********************************************************************************
129  cons_only = false;
130  FillIntermediatePatch(level, time_for_fp,
131  {&S_data[IntVars::cons], &xvel_new, &yvel_new, &zvel_new},
132  {&S_data[IntVars::cons], &S_data[IntVars::xmom],
133  &S_data[IntVars::ymom], &S_data[IntVars::zmom]},
134  ng_cons_to_use, ng_vel, cons_only, scomp_cons, ncomp_cons);
135  };
136 
137  auto update_terrain_stage = [&,one_d=one](int lev, const double old_step_time,
138  const double old_stage_time,
139  const double new_stage_time,
140  const double slow_dt)
141  {
142  // Note that the "old" and "new" metric terms correspond to
143  // t^n and the RK stage (either t^*, t^** or t^{n+1} that this source
144  // will be used to advance to
145 
146  // The "src" metric terms correspond to the time at which we are evaluating the source here,
147  // aka old_stage_time
148 
149  if (verbose) Print() << "Re-making old geometry at old time : " << old_step_time << std::endl;
150 
151  //
152  // Make a temporary MF to fill the terrain -- on this level's domain: one made from
153  // the level 0 domain would cover only part of a fine level
154  //
155  Box terrain_bx(surroundingNodes(fine_geom.Domain())); terrain_bx.grow(z_phys_nd[lev]->nGrow());
156  FArrayBox terrain_fab_old(makeSlab(terrain_bx,2,0),1);
157  FArrayBox terrain_fab_src(makeSlab(terrain_bx,2,0),1);
158  FArrayBox terrain_fab_new(makeSlab(terrain_bx,2,0),1);
159 
160  prob->init_terrain_surface(fine_geom,terrain_fab_old,old_step_time);
161  prob->init_terrain_surface(fine_geom,terrain_fab_src,old_stage_time);
162  prob->init_terrain_surface(fine_geom,terrain_fab_new,new_stage_time);
163 
164  // Copy on intersection
165  for (MFIter mfi(*z_phys_nd[lev],false); mfi.isValid(); ++mfi)
166  {
167  Box isect = terrain_fab_old.box() & (*z_phys_nd[lev])[mfi].box();
168  ( *z_phys_nd[lev])[mfi].template copy<RunOn::Device>(terrain_fab_old,isect,0,isect,0,1);
169  (*z_phys_nd_src[lev])[mfi].template copy<RunOn::Device>(terrain_fab_src,isect,0,isect,0,1);
170  (*z_phys_nd_new[lev])[mfi].template copy<RunOn::Device>(terrain_fab_new,isect,0,isect,0,1);
171  }
172 
173  make_terrain_fitted_coords(lev,fine_geom,*z_phys_nd[lev], zlevels_stag[lev], phys_bc_type,
174  FineTerrain::None, nullptr, /*moving_mesh=*/true);
175  make_J (fine_geom, *z_phys_nd[lev], *detJ_cc[lev]);
176  make_areas (fine_geom, *z_phys_nd[lev], *ax[lev], *ay[lev], *az[lev]);
177 
178  if (verbose) Print() << "Making src geometry at old_stage_time: "
179  << std::setprecision(timeprecision) << old_stage_time << std::endl;
180  make_terrain_fitted_coords(lev,fine_geom,*z_phys_nd_src[lev], zlevels_stag[lev], phys_bc_type,
181  FineTerrain::None, nullptr, /*moving_mesh=*/true);
182  make_J (fine_geom, *z_phys_nd_src[lev], *detJ_cc_src[lev]);
183  make_areas (fine_geom, *z_phys_nd_src[lev], *ax_src[lev], *ay_src[lev], *az_src[lev]);
184  make_zcc (fine_geom, *z_phys_nd_src[lev], *z_phys_cc_src[lev]);
185 
186  if (verbose) Print() << "Making new geometry at new_stage_time: "
187  << std::setprecision(timeprecision) << new_stage_time << std::endl;
188  make_terrain_fitted_coords(lev,fine_geom,*z_phys_nd_new[lev], zlevels_stag[lev], phys_bc_type,
189  FineTerrain::None, nullptr, /*moving_mesh=*/true);
190  make_J (fine_geom, *z_phys_nd_new[lev], *detJ_cc_new[lev]);
191 
192  Real inv_dt = one_d/static_cast<Real>(slow_dt);
193 
194 #ifdef _OPENMP
195 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
196 #endif
197  for (MFIter mfi(*z_t_rk[lev],TilingIfNotGPU()); mfi.isValid(); ++mfi)
198  {
199  Box gbx = mfi.growntilebox(IntVect(1,1,0));
200 
201  const Array4<Real >& z_t_arr = z_t_rk[lev]->array(mfi);
202  const Array4<Real const>& z_nd_new_arr = z_phys_nd_new[lev]->const_array(mfi);
203  const Array4<Real const>& z_nd_old_arr = z_phys_nd[lev]->const_array(mfi);
204 
205  // Loop over horizontal plane
206  amrex::ParallelFor(gbx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
207  {
208  // Evaluate between RK stages assuming the geometry is linear between old and new time
209  z_t_arr(i,j,k) = fourth * inv_dt * (z_nd_new_arr(i+1,j+1,k) - z_nd_old_arr(i+1,j+1,k)
210  +z_nd_new_arr(i ,j+1,k) - z_nd_old_arr( i,j+1,k)
211  +z_nd_new_arr(i+1,j ,k) - z_nd_old_arr(i+1,j ,k)
212  +z_nd_new_arr(i ,j ,k) - z_nd_old_arr(i ,j ,k));
213  });
214 
215  } // mfi
216  };
217 
218  auto update_terrain_substep = [&,one_d=one](int lev, const double old_substep_time, const double new_substep_time, const double dtau,
219  Vector<MultiFab>& S_data, std::unique_ptr<MultiFab>& z_t_pert)
220  {
221  // Make "old" fast geom -- store in z_phys_nd for convenience
222  Box terrain_bx(surroundingNodes(fine_geom.Domain())); terrain_bx.grow(z_phys_nd[lev]->nGrow());
223 
224  if (verbose) Print() << "Making geometry at start of substep time: "
225  << std::setprecision(timeprecision) << old_substep_time << std::endl;
226  FArrayBox terrain_fab_old(makeSlab(terrain_bx,2,0),1);
227  prob->init_terrain_surface(fine_geom,terrain_fab_old,old_substep_time);
228 
229  // Make "new" fast geom
230  if (verbose) Print() << "Making geometry for end of substep time :"
231  << std::setprecision(timeprecision) << new_substep_time << std::endl;
232  FArrayBox terrain_fab_new(makeSlab(terrain_bx,2,0),1);
233  prob->init_terrain_surface(fine_geom,terrain_fab_new,new_substep_time);
234 
235  // Copy on intersection
236  for (MFIter mfi(*z_phys_nd[lev],false); mfi.isValid(); ++mfi)
237  {
238  Box isect = terrain_fab_old.box() & (*z_phys_nd[lev])[mfi].box();
239  ( *z_phys_nd[lev])[mfi].template copy<RunOn::Device>(terrain_fab_old,isect,0,isect,0,1);
240  (*z_phys_nd_new[lev])[mfi].template copy<RunOn::Device>(terrain_fab_new,isect,0,isect,0,1);
241  }
242 
243  make_terrain_fitted_coords(lev,fine_geom,*z_phys_nd[lev], zlevels_stag[lev], phys_bc_type,
244  FineTerrain::None, nullptr, /*moving_mesh=*/true);
245  make_J (fine_geom,*z_phys_nd[lev], *detJ_cc[lev]);
246 
247  make_terrain_fitted_coords(lev,fine_geom,*z_phys_nd_new[lev], zlevels_stag[lev], phys_bc_type,
248  FineTerrain::None, nullptr, /*moving_mesh=*/true);
249  make_J (fine_geom,*z_phys_nd_new[lev], *detJ_cc_new[lev]);
250 
251  Real inv_dt = one_d/static_cast<Real>(dtau);
252 
253  z_t_pert = std::make_unique<MultiFab>(S_data[IntVars::zmom].boxArray(), S_data[IntVars::zmom].DistributionMap(), 1, 1);
254 
255  for (MFIter mfi(*z_t_rk[lev],TilingIfNotGPU()); mfi.isValid(); ++mfi)
256  {
257  Box gbx = mfi.growntilebox(IntVect(1,1,0));
258 
259  const Array4<Real >& z_t_arr = z_t_rk[lev]->array(mfi);
260  const Array4<Real >& zp_t_arr = z_t_pert->array(mfi);
261 
262  const Array4<Real const>& z_nd_new_arr = z_phys_nd_new[lev]->const_array(mfi);
263  const Array4<Real const>& z_nd_old_arr = z_phys_nd[lev]->const_array(mfi);
264 
265  // Loop over horizontal plane
266  amrex::ParallelFor(gbx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
267  {
268  // Evaluate between RK stages assuming the geometry is linear between old and new time
269  zp_t_arr(i,j,k) = fourth * inv_dt * (z_nd_new_arr(i+1,j+1,k) - z_nd_old_arr(i+1,j+1,k)
270  +z_nd_new_arr(i ,j+1,k) - z_nd_old_arr( i,j+1,k)
271  +z_nd_new_arr(i+1,j ,k) - z_nd_old_arr(i+1,j ,k)
272  +z_nd_new_arr(i ,j ,k) - z_nd_old_arr(i ,j ,k));
273  // Convert to perturbation: z"_t(t) = z_t(t) - z_t^{RK}
274  zp_t_arr(i,j,k) -= z_t_arr(i,j,k);
275  });
276  } // mfi
277  };
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
#define Rho_comp
Definition: ERF_IndexDefines.H:61
#define RhoTheta_comp
Definition: ERF_IndexDefines.H:62
#define RhoQ1_comp
Definition: ERF_IndexDefines.H:78
const Real rdOcp
Definition: ERF_InitCustomPert_ABL.H:128
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
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 fourth
Definition: ERF_NumericalConstants.H:42
constexpr amrex::Real zero
Definition: ERF_NumericalConstants.H:36
auto make_pi_stage
Definition: ERF_TI_utils.H:4
auto apply_bcs
Definition: ERF_TI_utils.H:46
auto update_terrain_substep
Definition: ERF_TI_utils.H:218
auto update_terrain_stage
Definition: ERF_TI_utils.H:137
@ None
caller has not prepared a fine mesh from the coarse level
void make_areas(const Geometry &geom, MultiFab &z_phys_nd, MultiFab &ax, MultiFab &ay, MultiFab &az)
Definition: ERF_TerrainMetrics.cpp:1191
void make_J(const Geometry &geom, MultiFab &z_phys_nd, MultiFab &detJ_cc)
Definition: ERF_TerrainMetrics.cpp:1153
void make_zcc(const Geometry &geom, MultiFab &z_phys_nd, MultiFab &z_phys_cc)
Definition: ERF_TerrainMetrics.cpp:1258
void make_terrain_fitted_coords(int lev, const Geometry &geom, MultiFab &z_phys_nd, Vector< Real > const &z_levels_h, GpuArray< ERF_BC, AMREX_SPACEDIM *2 > &phys_bc_type, FineTerrain fine_terrain, MultiFab const *z_phys_interp, bool moving_mesh)
Definition: ERF_TerrainMetrics.cpp:324
@ 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