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