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