ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_MRI.H
Go to the documentation of this file.
1 #ifndef ERF_MRI_H
2 #define ERF_MRI_H
3 
4 #include <AMReX_REAL.H>
5 #include <AMReX_Vector.H>
6 #include <AMReX_IntegratorBase.H>
7 
8 #include <ERF_TI_slow_headers.H>
9 #include <ERF_TI_fast_headers.H>
10 
11 #include <algorithm>
12 #include <functional>
13 
14 /**
15  * @brief Split integrator for MRI simulations handling slow and fast timescales.
16  * @tparam T State type.
17  */
18 template<class T>
20 {
21 private:
22  /**
23  * \brief rhs is the right-hand-side function the integrator will use.
24  */
25  std::function<void(T&, const T&, const double, const double)> rhs;
26  std::function<void(T&, T&, T&, const double, const double, const double, const int)> slow_rhs_pre;
27  std::function<void(T&, T&, T&, T&, const double, const double, const double, const int)> slow_rhs_post;
28  std::function<void(int, int, int, T&, const T&, T&, T&, const double, const double,
29  const amrex::Real, const double,
30  const double)> acoustic_substepping;
31 
32  /**
33  * \brief Integrator timestep size (Real)
34  */
35  double timestep = 0.0;
36 
37  /**
38  * \brief The ratio of slow timestep size / fast timestep size (int)
39  */
41 
42  /**
43  * \brief Should we not do acoustic substepping (-1 until setNoSubstepping is called)
44  */
45  int no_substepping = -1;
46 
47  /**
48  * \brief Should we use the two-stage integrator of anelastic and pseudo-incompressible levels
49  * (-1 until setAnelastic is called)
50  */
51  int anelastic = -1;
52 
53  /**
54  * \brief Which two-stage scheme the anelastic / pseudo-incompressible integrator uses (RK2 or MidPoint)
55  */
56  TimesteppingType timestepping_type = TimesteppingType::RK2;
57 
58  /**
59  * \brief How many components in the cell-centered MultiFab (-1 until setNcompCons is called)
60  */
61  int ncomp_cons = -1;
62 
63  /**
64  * \brief Do we follow the recommendation to only perform a single substep in the first RK stage
65  * (-1 until setForceFirstStageSingleSubstep is called)
66  */
68 
69  /**
70  * \brief The no_substep function is called when we have no acoustic substepping
71  */
72  std::function<void (T&, T&, T&, const double, const double, int)> no_substep;
73 
74 
75  amrex::Vector<std::unique_ptr<T> > T_store;
76  T* S_sum = nullptr;
77  T* F_slow = nullptr;
78 
79  /**
80  * @brief Allocate internal storage for integrator variables.
81  * @param[in] S_data Reference state used to determine storage size.
82  */
83  void initialize_data (const T& S_data)
84  {
85  // TODO: We can optimize memory by making the cell-centered part of S_sum
86  // have only 2 components, not ncomp_cons components
87  const bool include_ghost = true;
88  // CreateLike appends to T_store, so drop any storage from a previous
89  // initialization before S_sum and F_slow are taken from entries 0 and 1
90  T_store.clear();
91  amrex::IntegratorOps<T>::CreateLike(T_store, S_data, include_ghost);
92  S_sum = T_store[0].get();
93  amrex::IntegratorOps<T>::CreateLike(T_store, S_data, include_ghost);
94  F_slow = T_store[1].get();
95  // initializing to zero
96  for (long idx = 0; idx < S_sum->size(); idx++) { (*S_sum)[idx].setVal(0); }
97  for (long idx = 0; idx < F_slow->size(); idx++) { (*F_slow)[idx].setVal(0); }
98  }
99 
100 public:
101  MRISplitIntegrator () = default;
102 
103  MRISplitIntegrator (const T& S_data)
104  {
105  initialize_data(S_data);
106  }
107 
108  /**
109  * @brief Initialize integrator storage.
110  * @param[in] S_data Reference state used to determine storage size.
111  */
112  void initialize (const T& S_data)
113  {
114  initialize_data(S_data);
115  }
116 
117  ~MRISplitIntegrator () = default;
118 
119  // Declare a default move constructor so we ensure the destructor is
120  // not called when we return an object of this class by value
121  MRISplitIntegrator (MRISplitIntegrator&&) noexcept = default;
122 
123  // Declare a default move assignment operator
124  MRISplitIntegrator& operator= (MRISplitIntegrator&& other) noexcept = default;
125 
126  // Delete the copy constructor and copy assignment operators because
127  // the integrator allocates internal memory that is best initialized
128  // from scratch when needed instead of making a copy.
129 
130  // Delete the copy constructor
131  MRISplitIntegrator (const MRISplitIntegrator& other) = delete;
132  //
133  // Delete the copy assignment operator
134  MRISplitIntegrator& operator= (const MRISplitIntegrator& other) = delete;
135 
136  /**
137  * @brief Set the number of conservative components.
138  * @param[in] _ncomp_cons Number of conservative components.
139  */
140  void setNcompCons (int _ncomp_cons)
141  {
142  ncomp_cons = _ncomp_cons;
143  }
144 
145  /**
146  * @brief Set whether to use the anelastic / pseudo-incompressible integrator.
147  * @param[in] _anelastic Integer flag (1 for anelastic or pseudo-incompressible, 0 for compressible).
148  */
149  void setAnelastic (int _anelastic)
150  {
151  anelastic = _anelastic;
152  }
153 
154  /**
155  * @brief Set the level's time integrator; the anelastic / pseudo-incompressible integrator uses
156  * RK2 or MidPoint.
157  * @param[in] _timestepping_type The level's TimesteppingType.
158  */
159  void setTimesteppingType (TimesteppingType _timestepping_type = TimesteppingType::RK2)
160  {
161  timestepping_type = _timestepping_type;
162  }
163 
164  /**
165  * @brief Set whether acoustic substepping is disabled.
166  * @param[in] _no_substepping Integer flag (1 to disable substepping).
167  */
168  void setNoSubstepping (int _no_substepping)
169  {
170  no_substepping = _no_substepping;
171  }
172 
173  /**
174  * @brief Force the first RK stage to perform only a single substep.
175  * @param[in] _force_stage1_single_substep Integer flag to enable this behavior.
176  */
177  void setForceFirstStageSingleSubstep (int _force_stage1_single_substep)
178  {
179  force_stage1_single_substep = _force_stage1_single_substep;
180  }
181 
182  /**
183  * @brief Set the pre-substepping slow RHS function.
184  * @param[in] F Function to compute the pre-substepping slow RHS.
185  */
186  void set_slow_rhs_pre (std::function<void(T&, T&, T&, const double, const double, const double, const int)> F)
187  {
188  slow_rhs_pre = F;
189  }
190  void set_slow_rhs_post (std::function<void(T&, T&, T&, T&, const double, const double, const double, const int)> F)
191  {
192  slow_rhs_post = F;
193  }
194 
195  /**
196  * @brief Set the acoustic substepping function.
197  * @param[in] F Function to perform the acoustic substepping update.
198  */
199  void set_acoustic_substepping (std::function<void(int, int, int, T&, const T&, T&, T&,
200  const double, const double,
201  const amrex::Real, const double,
202  const double)> F)
203  {
205  }
206 
207  /**
208  * @brief Set the ratio of slow to fast timestep sizes.
209  * @param[in] timestep_ratio Ratio of slow/fast timesteps.
210  */
211  void set_slow_fast_timestep_ratio (const int timestep_ratio = 1)
212  {
213  slow_fast_timestep_ratio = timestep_ratio;
214  }
215 
216  /**
217  * @brief Get the current slow-to-fast timestep ratio.
218  * @return The slow/fast timestep ratio.
219  */
221  {
223  }
224 
225  /**
226  * @brief Set the function to be called when acoustic substepping is disabled.
227  * @param[in] F Function to use in place of substepping.
228  */
229  void set_no_substep (std::function<void (T&, T&, T&, const double, const double, int)> F)
230  {
231  no_substep = F;
232  }
233 
234  std::function<void(T&, const T&, const double, const double)> get_rhs ()
235  {
236  return rhs;
237  }
238 
239  /**
240  * @brief Advance the state from time to time + time_step.
241  * @param[in,out] S_old Current state.
242  * @param[in,out] S_new State to be updated.
243  * @param[in] time Current simulation time.
244  * @param[in] time_step Timestep size.
245  * @return The actual timestep taken.
246  */
247  double advance (T& S_old, T& S_new, double time, const double time_step)
248  {
249  BL_PROFILE_REGION("MRI_advance");
250  using namespace amrex;
251 
252  // *******************************************************************************
253  // !no_substepping: we only update the fast variables every fast timestep, then update
254  // the slow variables after the acoustic sub-stepping. This has
255  // 2 calls to slow_rhs so that we can update the slow variables
256  // with the velocity field after the acoustic substepping using
257  // the time-averaged velocity from the substepping
258  // no_substepping: we don't do any acoustic subcycling so we only make one call per RK
259  // stage to slow_rhs
260  // *******************************************************************************
263  "MRISplitIntegrator::advance: setNoSubstepping, setAnelastic, "
264  "setForceFirstStageSingleSubstep and setNcompCons must be called before advance");
265  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(S_sum != nullptr && F_slow != nullptr,
266  "MRISplitIntegrator::advance: integrator storage must be initialized before advance");
267 
268  timestep = time_step;
269 
270  const int substep_ratio = get_slow_fast_timestep_ratio();
271 
272  if (!no_substepping) {
273  AMREX_ALWAYS_ASSERT(substep_ratio > 1 && substep_ratio % 2 == 0);
274  }
275 
276  // Assume before advance() that S_old is valid data at the current time ("time" argument)
277  // And that if data is a MultiFab, both S_old and S_new contain ghost cells for evaluating a stencil based RHS
278  // We need this from S_old. This is convenient for S_new to have so we can use it
279  // as scratch space for stage values without creating a new scratch MultiFab with ghost cells.
280 
281  // NOTE: In the following, we use S_new to hold S*, S**, and finally, S^(n+1) at the new time
282  // DEFINITIONS:
283  // S_old = S^n
284  // S_sum = S(t)
285  // F_slow = F(S_stage)
286 
287  int n_data = IntVars::NumTypes;
288 
289  /**********************************************/
290  /* RK3 Integration with Acoustic Sub-stepping */
291  /**********************************************/
292  Vector<int> num_vars = {ncomp_cons, 1, 1, 1};
293  for (int i(0); i<n_data; ++i)
294  {
295  // Copy old -> new
296  MultiFab::Copy(S_new[i],S_old[i],0,0,num_vars[i],S_old[i].nGrowVect());
297  }
298 
299  // Timestep taken by the fast integrator
300  double dtau;
301 
302  // How many timesteps taken by the fast integrator
303  int nsubsteps;
304 
305  // This is the final time of the full timestep (also the 3rd RK stage)
306  // Real new_time = time + timestep;
307 
308  double time_stage = time;
309  double old_time_stage;
310 
311  if (!anelastic) {
312  // RK3 for compressible integrator
313  for (int nrk = 0; nrk < 3; nrk++)
314  {
315  // Capture the time we got to in the previous RK step
316  old_time_stage = time_stage;
317 
318  if (nrk == 0) {
320  nsubsteps = 1;
321  dtau = timestep / three;
322  } else {
323  // Clamp to 1: substep_ratio is only required to be even, so a ratio of 2
324  // would otherwise give zero substeps and leave S_sum stale in this stage
325  nsubsteps = std::max(substep_ratio/3, 1);
326  dtau = (timestep / three) / static_cast<double>(nsubsteps);
327  }
328  time_stage = time + timestep / three;
329  }
330  if (nrk == 1) {
331  if (no_substepping) {
332  nsubsteps = 1;
333  dtau = myhalf * timestep;
334  } else {
335  nsubsteps = std::max(substep_ratio/2, 1);
336  dtau = (myhalf * timestep) / static_cast<double>(nsubsteps);
337  }
338  time_stage = time + timestep / two;
339  }
340  if (nrk == 2) {
341  if (no_substepping) {
342  nsubsteps = 1;
343  dtau = timestep;
344  } else {
345  nsubsteps = substep_ratio;
346  dtau = timestep / static_cast<double>(nsubsteps);
347  }
348  time_stage = time + timestep;
349  }
350 
351  // step 1 starts with S_stage = S^n and we always start substepping at the old time
352  // step 2 starts with S_stage = S^* and we always start substepping at the old time
353  // step 3 starts with S_stage = S^** and we always start substepping at the old time
354 
355  slow_rhs_pre(*F_slow, S_old, S_new, time, old_time_stage, time_stage, nrk);
356 
357  amrex::Real inv_fac = one / static_cast<amrex::Real>(nsubsteps);
358 
359  // ****************************************************
360  // Acoustic substepping
361  // ****************************************************
362  if (!no_substepping)
363  {
364  // *******************************************************************************
365  // Update the fast variables
366  // *******************************************************************************
367  for (int ks = 0; ks < nsubsteps; ++ks)
368  {
369  acoustic_substepping(ks, nsubsteps, nrk, *F_slow, S_old, S_new, *S_sum, dtau, timestep, inv_fac,
370  time + ks*dtau, time + (ks+1) * dtau);
371 
372  } // ks
373 
374  } else {
375  no_substep(*S_sum, S_old, *F_slow, time + nsubsteps*dtau, nsubsteps*dtau, nrk);
376  }
377 
378  // ****************************************************
379  // Evaluate F_slow(S_stage) only for the slow variables
380  // Note that we are using the current stage versions (in S_new) of the slow variables
381  // (because we didn't update the slow variables in the substepping)
382  // and the momenta slow_rhs_pre set from the same stage, plus the average of
383  // the acoustic substep updates when substepping, to advect them
384  // (the mass fluxes that advance rho over the stage)
385  // ****************************************************
386  slow_rhs_post(*F_slow, S_old, S_new, *S_sum, time, old_time_stage, time_stage, nrk);
387  } // nrk
388 
389  } else {
390  // Two-stage integrator for the anelastic and pseudo-incompressible equations, which get
391  // their pressure from a projection at the end of each stage.
392  //
393  // SSP-RK2 (Heun): both stages advance a full timestep from S^n, and the
394  // second stage averages the slow source with the one from the first,
395  // which erf_slow_rhs_{pre,post} do when (timestepping_type == RK2 && nrk == 1).
396  //
397  // MidPoint: the first stage advances only a half timestep,
398  // S^* = S^n + (dt/2) F(S^n)
399  // S^n+1 = S^n + dt F(S^*)
400  // and the second stage takes no average. The vertical diffusion
401  // is second order in time with a tridiagonal solve in the first
402  // stage only and none in the second.
403  const bool l_midpoint = (timestepping_type == TimesteppingType::MidPoint);
404 
405  for (int nrk = 0; nrk < 2; nrk++)
406  {
407  // Capture the time we got to in the previous RK step
408  old_time_stage = time_stage;
409 
410  // Set the timestep for this stage -- both stages advance from the old time
411  nsubsteps = 1;
412  dtau = (l_midpoint && (nrk == 0)) ? myhalf * timestep : timestep;
413  time_stage = time + nsubsteps * dtau;
414 
415  slow_rhs_pre(*F_slow, S_old, S_new, time, old_time_stage, time_stage, nrk);
416 
417  no_substep(*S_sum, S_old, *F_slow, time + nsubsteps*dtau, nsubsteps*dtau, nrk);
418 
419  // ****************************************************
420  // Evaluate F_slow(S_stage) only for the slow variables
421  // Note that we are using the current stage versions (in S_new) of the slow variables
422  // and the momenta slow_rhs_pre took from the same stage to advect them;
423  // S_sum holds the projected state at the end of the stage
424  // ****************************************************
425  slow_rhs_post(*F_slow, S_old, S_new, *S_sum, time, old_time_stage, time_stage, nrk);
426  } // nrk
427  }
428 
429  // Return timestep
430  return timestep;
431  }
432 
433  /**
434  * @brief Apply a mapping function to all internal stored data.
435  * @param[in] Map Mapping function to apply to each stored state object.
436  */
437  void map_data (std::function<void(T&)> Map)
438  {
439  for (auto& F : T_store) {
440  Map(*F);
441  }
442  }
443 };
444 
445 #endif
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(axis >=0 &&axis< AMREX_SPACEDIM, "prob.axis must be 0, 1 or 2")
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int idx(int i, int j, int k, int nx, int ny)
Definition: ERF_InitForEnsemble.cpp:404
constexpr amrex::Real three
Definition: ERF_NumericalConstants.H:39
constexpr amrex::Real two
Definition: ERF_NumericalConstants.H:38
constexpr amrex::Real one
Definition: ERF_NumericalConstants.H:37
constexpr amrex::Real myhalf
Definition: ERF_NumericalConstants.H:41
Split integrator for MRI simulations handling slow and fast timescales.
Definition: ERF_MRI.H:20
T * F_slow
Definition: ERF_MRI.H:77
std::function< void(T &, const T &, const double, const double)> get_rhs()
Definition: ERF_MRI.H:234
amrex::Vector< std::unique_ptr< T > > T_store
Definition: ERF_MRI.H:75
void set_slow_rhs_post(std::function< void(T &, T &, T &, T &, const double, const double, const double, const int)> F)
Definition: ERF_MRI.H:190
void map_data(std::function< void(T &)> Map)
Apply a mapping function to all internal stored data.
Definition: ERF_MRI.H:437
void set_slow_rhs_pre(std::function< void(T &, T &, T &, const double, const double, const double, const int)> F)
Set the pre-substepping slow RHS function.
Definition: ERF_MRI.H:186
int anelastic
Should we use the two-stage integrator of anelastic and pseudo-incompressible levels (-1 until setAne...
Definition: ERF_MRI.H:51
void setNcompCons(int _ncomp_cons)
Set the number of conservative components.
Definition: ERF_MRI.H:140
MRISplitIntegrator()=default
std::function< void(T &, const T &, const double, const double)> rhs
rhs is the right-hand-side function the integrator will use.
Definition: ERF_MRI.H:25
void setForceFirstStageSingleSubstep(int _force_stage1_single_substep)
Force the first RK stage to perform only a single substep.
Definition: ERF_MRI.H:177
int force_stage1_single_substep
Do we follow the recommendation to only perform a single substep in the first RK stage (-1 until setF...
Definition: ERF_MRI.H:67
int ncomp_cons
How many components in the cell-centered MultiFab (-1 until setNcompCons is called)
Definition: ERF_MRI.H:61
void setNoSubstepping(int _no_substepping)
Set whether acoustic substepping is disabled.
Definition: ERF_MRI.H:168
double timestep
Integrator timestep size (Real)
Definition: ERF_MRI.H:35
TimesteppingType timestepping_type
Which two-stage scheme the anelastic / pseudo-incompressible integrator uses (RK2 or MidPoint)
Definition: ERF_MRI.H:56
std::function< void(T &, T &, T &, const double, const double, const double, const int)> slow_rhs_pre
Definition: ERF_MRI.H:26
void set_no_substep(std::function< void(T &, T &, T &, const double, const double, int)> F)
Set the function to be called when acoustic substepping is disabled.
Definition: ERF_MRI.H:229
void initialize(const T &S_data)
Initialize integrator storage.
Definition: ERF_MRI.H:112
MRISplitIntegrator(MRISplitIntegrator &&) noexcept=default
void setTimesteppingType(TimesteppingType _timestepping_type=TimesteppingType::RK2)
Set the level's time integrator; the anelastic / pseudo-incompressible integrator uses RK2 or MidPoin...
Definition: ERF_MRI.H:159
void initialize_data(const T &S_data)
Allocate internal storage for integrator variables.
Definition: ERF_MRI.H:83
MRISplitIntegrator(const T &S_data)
Definition: ERF_MRI.H:103
std::function< void(T &, T &, T &, T &, const double, const double, const double, const int)> slow_rhs_post
Definition: ERF_MRI.H:27
void setAnelastic(int _anelastic)
Set whether to use the anelastic / pseudo-incompressible integrator.
Definition: ERF_MRI.H:149
std::function< void(T &, T &, T &, const double, const double, int)> no_substep
The no_substep function is called when we have no acoustic substepping.
Definition: ERF_MRI.H:72
int get_slow_fast_timestep_ratio()
Get the current slow-to-fast timestep ratio.
Definition: ERF_MRI.H:220
double advance(T &S_old, T &S_new, double time, const double time_step)
Advance the state from time to time + time_step.
Definition: ERF_MRI.H:247
int slow_fast_timestep_ratio
The ratio of slow timestep size / fast timestep size (int)
Definition: ERF_MRI.H:40
void set_acoustic_substepping(std::function< void(int, int, int, T &, const T &, T &, T &, const double, const double, const amrex::Real, const double, const double)> F)
Set the acoustic substepping function.
Definition: ERF_MRI.H:199
~MRISplitIntegrator()=default
void set_slow_fast_timestep_ratio(const int timestep_ratio=1)
Set the ratio of slow to fast timestep sizes.
Definition: ERF_MRI.H:211
T * S_sum
Definition: ERF_MRI.H:76
std::function< void(int, int, int, T &, const T &, T &, T &, const double, const double, const amrex::Real, const double, const double)> acoustic_substepping
Definition: ERF_MRI.H:30
int no_substepping
Should we not do acoustic substepping (-1 until setNoSubstepping is called)
Definition: ERF_MRI.H:45
@ NumTypes
Definition: ERF_IndexDefines.H:253
@ ks
Definition: ERF_SLM.H:76
@ T
Definition: ERF_IndexDefines.H:180
Definition: ERF_ConsoleIO.cpp:17