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_ParmParse.H>
7 #include <AMReX_IntegratorBase.H>
8 
9 #include <ERF_TI_slow_headers.H>
10 #include <ERF_TI_fast_headers.H>
11 
12 #include <functional>
13 
14 template<class T>
16 {
17 private:
18  /**
19  * \brief rhs is the right-hand-side function the integrator will use.
20  */
21  std::function<void(T&, const T&, const amrex::Real, const amrex::Real )> rhs;
22  std::function<void(T&, T&, T&, const amrex::Real, const amrex::Real, const amrex::Real, const int)> slow_rhs_pre;
23  std::function<void(T&, T&, T&, T&, const amrex::Real, const amrex::Real, const amrex::Real, const int )> slow_rhs_post;
24  std::function<void(int, int, int, T&, const T&, T&, T&, const amrex::Real, const amrex::Real,
26 
27  /**
28  * \brief Integrator timestep size (Real)
29  */
31 
32  /**
33  * \brief The ratio of slow timestep size / fast timestep size (int)
34  */
36 
37  /**
38  * \brief Should we not do acoustic substepping
39  */
41 
42  /**
43  * \brief Should we use the anelastic integrator
44  */
45  int anelastic;
46 
47  /**
48  * \brief How many components in the cell-centered MultiFab
49  */
51 
52  /**
53  * \brief Do we follow the recommendation to only perform a single substep in the first RK stage
54  */
56 
57  /**
58  * \brief The no_substep function is called when we have no acoustic substepping
59  */
60  std::function<void (T&, T&, T&, amrex::Real, amrex::Real, int)> no_substep;
61 
62 
63  amrex::Vector<std::unique_ptr<T> > T_store;
64  T* S_sum;
66 
67  void initialize_data (const T& S_data)
68  {
69  // TODO: We can optimize memory by making the cell-centered part of S_sum
70  // have only 2 components, not ncomp_cons components
71  const bool include_ghost = true;
72  amrex::IntegratorOps<T>::CreateLike(T_store, S_data, include_ghost);
73  S_sum = T_store[0].get();
74  amrex::IntegratorOps<T>::CreateLike(T_store, S_data, include_ghost);
75  F_slow = T_store[1].get();
76  }
77 
78 public:
79  MRISplitIntegrator () = default;
80 
81  MRISplitIntegrator (const T& S_data)
82  {
83  initialize_data(S_data);
84  }
85 
86  void initialize (const T& S_data)
87  {
88  initialize_data(S_data);
89  }
90 
91  ~MRISplitIntegrator () = default;
92 
93  // Declare a default move constructor so we ensure the destructor is
94  // not called when we return an object of this class by value
95  MRISplitIntegrator(MRISplitIntegrator&&) noexcept = default;
96 
97  // Declare a default move assignment operator
98  MRISplitIntegrator& operator=(MRISplitIntegrator&& other) noexcept = default;
99 
100  // Delete the copy constructor and copy assignment operators because
101  // the integrator allocates internal memory that is best initialized
102  // from scratch when needed instead of making a copy.
103 
104  // Delete the copy constructor
105  MRISplitIntegrator(const MRISplitIntegrator& other) = delete;
106  //
107  // Delete the copy assignment operator
108  MRISplitIntegrator& operator=(const MRISplitIntegrator& other) = delete;
109 
110  void setNcompCons(int _ncomp_cons)
111  {
112  ncomp_cons = _ncomp_cons;
113  }
114 
115  void setAnelastic(int _anelastic)
116  {
117  anelastic = _anelastic;
118  }
119 
120  void setNoSubstepping(int _no_substepping)
121  {
122  no_substepping = _no_substepping;
123  }
124 
125  void setForceFirstStageSingleSubstep(int _force_stage1_single_substep)
126  {
127  force_stage1_single_substep = _force_stage1_single_substep;
128  }
129 
130  void set_slow_rhs_pre (std::function<void(T&, T&, T&, const amrex::Real, const amrex::Real, const amrex::Real, const int)> F)
131  {
132  slow_rhs_pre = F;
133  }
134  void set_slow_rhs_post (std::function<void(T&, T&, T&, T&, const amrex::Real, const amrex::Real, const amrex::Real, const int)> F)
135  {
136  slow_rhs_post = F;
137  }
138 
139  void set_fast_rhs (std::function<void(int, int, int, T&, const T&, T&, T&,
140  const amrex::Real, const amrex::Real,
141  const amrex::Real, const amrex::Real)> F)
142  {
143  fast_rhs = F;
144  }
145 
146  void set_slow_fast_timestep_ratio (const int timestep_ratio = 1)
147  {
148  slow_fast_timestep_ratio = timestep_ratio;
149  }
150 
152  {
154  }
155 
156  void set_no_substep (std::function<void (T&, T&, T&, amrex::Real, amrex::Real, int)> F)
157  {
158  no_substep = F;
159  }
160 
161  std::function<void(T&, const T&, const amrex::Real, int)> get_rhs ()
162  {
163  return rhs;
164  }
165 
166  amrex::Real advance (T& S_old, T& S_new, amrex::Real time, const amrex::Real time_step)
167  {
168  BL_PROFILE_REGION("MRI_advance");
169  using namespace amrex;
170 
171  // *******************************************************************************
172  // !no_substepping: we only update the fast variables every fast timestep, then update
173  // the slow variables after the acoustic sub-stepping. This has
174  // two calls to slow_rhs so that we can update the slow variables
175  // with the velocity field after the acoustic substepping using
176  // the time-averaged velocity from the substepping
177  // no_substepping: we don't do any acoustic subcyling so we only make one call per RK
178  // stage to slow_rhs
179  // *******************************************************************************
180  timestep = time_step;
181 
182  const int substep_ratio = get_slow_fast_timestep_ratio();
183 
184  if (!no_substepping) {
185  AMREX_ALWAYS_ASSERT(substep_ratio > 1 && substep_ratio % 2 == 0);
186  }
187 
188  // Assume before advance() that S_old is valid data at the current time ("time" argument)
189  // And that if data is a MultiFab, both S_old and S_new contain ghost cells for evaluating a stencil based RHS
190  // We need this from S_old. This is convenient for S_new to have so we can use it
191  // as scratch space for stage values without creating a new scratch MultiFab with ghost cells.
192 
193  // NOTE: In the following, we use S_new to hold S*, S**, and finally, S^(n+1) at the new time
194  // DEFINITIONS:
195  // S_old = S^n
196  // S_sum = S(t)
197  // F_slow = F(S_stage)
198 
199  int n_data = IntVars::NumTypes;
200 
201  /**********************************************/
202  /* RK3 Integration with Acoustic Sub-stepping */
203  /**********************************************/
204  Vector<int> num_vars = {ncomp_cons, 1, 1, 1};
205  for (int i(0); i<n_data; ++i)
206  {
207  // Copy old -> new
208  MultiFab::Copy(S_new[i],S_old[i],0,0,num_vars[i],S_old[i].nGrowVect());
209  }
210 
211  // Timestep taken by the fast integrator
212  amrex::Real dtau;
213 
214  // How many timesteps taken by the fast integrator
215  int nsubsteps;
216 
217  // This is the final time of the full timestep (also the 3rd RK stage)
218  // Real new_time = time + timestep;
219 
220  amrex::Real time_stage = time;
221  amrex::Real old_time_stage;
222 
223  const amrex::Real sub_timestep = timestep / substep_ratio;
224 
225  if (!anelastic) {
226  // RK3 for compressible integrator
227  for (int nrk = 0; nrk < 3; nrk++)
228  {
229  // Capture the time we got to in the previous RK step
230  old_time_stage = time_stage;
231 
232  if (nrk == 0) {
234  nsubsteps = 1; dtau = timestep / 3.0;
235  } else {
236  nsubsteps = substep_ratio/3; dtau = sub_timestep ;
237  }
238  time_stage = time + timestep / 3.0;
239  }
240  if (nrk == 1) {
241  if (no_substepping) {
242  nsubsteps = 1; dtau = 0.5 * timestep;
243  } else {
244  nsubsteps = substep_ratio/2; dtau = sub_timestep;
245  }
246  time_stage = time + timestep / 2.0;
247  }
248  if (nrk == 2) {
249  if (no_substepping) {
250  nsubsteps = 1; dtau = timestep;
251  } else {
252  nsubsteps = substep_ratio; dtau = sub_timestep;
253 
254  // STRT HACK -- this hack can be used to approximate the no-substepping algorithm
255  // nsubsteps = 1; dtau = timestep;
256  // END HACK
257  }
258  time_stage = time + timestep;
259  }
260 
261  // step 1 starts with S_stage = S^n and we always start substepping at the old time
262  // step 2 starts with S_stage = S^* and we always start substepping at the old time
263  // step 3 starts with S_stage = S^** and we always start substepping at the old time
264 
265  slow_rhs_pre(*F_slow, S_old, S_new, time, old_time_stage, time_stage, nrk);
266 
267  amrex::Real inv_fac = 1.0 / static_cast<amrex::Real>(nsubsteps);
268 
269  // ****************************************************
270  // Acoustic substepping
271  // ****************************************************
272  if (!no_substepping)
273  {
274  // *******************************************************************************
275  // Update the fast variables
276  // *******************************************************************************
277  for (int ks = 0; ks < nsubsteps; ++ks)
278  {
279  fast_rhs(ks, nsubsteps, nrk, *F_slow, S_old, S_new, *S_sum, dtau, inv_fac,
280  time + ks*dtau, time + (ks+1) * dtau);
281 
282  } // ks
283 
284  } else {
285  no_substep(*S_sum, S_old, *F_slow, time + nsubsteps*dtau, nsubsteps*dtau, nrk);
286  }
287 
288  // ****************************************************
289  // Evaluate F_slow(S_stage) only for the slow variables
290  // Note that we are using the current stage versions (in S_new) of the slow variables
291  // (because we didn't update the slow variables in the substepping)
292  // but we are using the "new" versions (in S_sum) of the velocities
293  // (because we did update the fast variables in the substepping)
294  // ****************************************************
295  slow_rhs_post(*F_slow, S_old, S_new, *S_sum, time, old_time_stage, time_stage, nrk);
296  } // nrk
297 
298  } else {
299  // RK2 for anelastic integrator
300  for (int nrk = 0; nrk < 2; nrk++)
301  {
302  // Capture the time we got to in the previous RK step
303  old_time_stage = time_stage;
304 
305  if (nrk == 0) { nsubsteps = 1; dtau = timestep; time_stage = time + timestep; }
306  if (nrk == 1) { nsubsteps = 1; dtau = timestep; time_stage = time + timestep; }
307 
308  slow_rhs_pre(*F_slow, S_old, S_new, time, old_time_stage, time_stage, nrk);
309 
310  no_substep(*S_sum, S_old, *F_slow, time + nsubsteps*dtau, nsubsteps*dtau, nrk);
311 
312  // ****************************************************
313  // Evaluate F_slow(S_stage) only for the slow variables
314  // Note that we are using the current stage versions (in S_new) of the slow variables
315  // (because we didn't update the slow variables in the substepping)
316  // but we are using the "new" versions (in S_sum) of the velocities
317  // (because we did update the fast variables in the substepping)
318  // ****************************************************
319  slow_rhs_post(*F_slow, S_old, S_new, *S_sum, time, old_time_stage, time_stage, nrk);
320  } // nrk
321  }
322 
323  // Return timestep
324  return timestep;
325  }
326 
327  void map_data (std::function<void(T&)> Map)
328  {
329  for (auto& F : T_store) {
330  Map(*F);
331  }
332  }
333 };
334 
335 #endif
amrex::Real Real
Definition: ERF_ShocInterface.H:19
Definition: ERF_MRI.H:16
T * F_slow
Definition: ERF_MRI.H:65
std::function< void(T &, T &, T &, T &, const amrex::Real, const amrex::Real, const amrex::Real, const int)> slow_rhs_post
Definition: ERF_MRI.H:23
amrex::Vector< std::unique_ptr< T > > T_store
Definition: ERF_MRI.H:63
void map_data(std::function< void(T &)> Map)
Definition: ERF_MRI.H:327
void set_no_substep(std::function< void(T &, T &, T &, amrex::Real, amrex::Real, int)> F)
Definition: ERF_MRI.H:156
int anelastic
Should we use the anelastic integrator.
Definition: ERF_MRI.H:45
void setNcompCons(int _ncomp_cons)
Definition: ERF_MRI.H:110
std::function< void(T &, T &, T &, const amrex::Real, const amrex::Real, const amrex::Real, const int)> slow_rhs_pre
Definition: ERF_MRI.H:22
amrex::Real timestep
Integrator timestep size (Real)
Definition: ERF_MRI.H:30
MRISplitIntegrator()=default
void set_fast_rhs(std::function< void(int, int, int, T &, const T &, T &, T &, const amrex::Real, const amrex::Real, const amrex::Real, const amrex::Real)> F)
Definition: ERF_MRI.H:139
void setForceFirstStageSingleSubstep(int _force_stage1_single_substep)
Definition: ERF_MRI.H:125
std::function< void(int, int, int, T &, const T &, T &, T &, const amrex::Real, const amrex::Real, const amrex::Real, const amrex::Real)> fast_rhs
Definition: ERF_MRI.H:25
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:55
int ncomp_cons
How many components in the cell-centered MultiFab.
Definition: ERF_MRI.H:50
void setNoSubstepping(int _no_substepping)
Definition: ERF_MRI.H:120
void initialize(const T &S_data)
Definition: ERF_MRI.H:86
MRISplitIntegrator(MRISplitIntegrator &&) noexcept=default
void initialize_data(const T &S_data)
Definition: ERF_MRI.H:67
MRISplitIntegrator(const T &S_data)
Definition: ERF_MRI.H:81
std::function< void(T &, const T &, const amrex::Real, int)> get_rhs()
Definition: ERF_MRI.H:161
std::function< void(T &, const T &, const amrex::Real, const amrex::Real)> rhs
rhs is the right-hand-side function the integrator will use.
Definition: ERF_MRI.H:21
void setAnelastic(int _anelastic)
Definition: ERF_MRI.H:115
int get_slow_fast_timestep_ratio()
Definition: ERF_MRI.H:151
std::function< void(T &, T &, T &, amrex::Real, amrex::Real, int)> no_substep
The no_substep function is called when we have no acoustic substepping.
Definition: ERF_MRI.H:60
void set_slow_rhs_post(std::function< void(T &, T &, T &, T &, const amrex::Real, const amrex::Real, const amrex::Real, const int)> F)
Definition: ERF_MRI.H:134
int slow_fast_timestep_ratio
The ratio of slow timestep size / fast timestep size (int)
Definition: ERF_MRI.H:35
void set_slow_rhs_pre(std::function< void(T &, T &, T &, const amrex::Real, const amrex::Real, const amrex::Real, const int)> F)
Definition: ERF_MRI.H:130
~MRISplitIntegrator()=default
void set_slow_fast_timestep_ratio(const int timestep_ratio=1)
Definition: ERF_MRI.H:146
T * S_sum
Definition: ERF_MRI.H:64
amrex::Real advance(T &S_old, T &S_new, amrex::Real time, const amrex::Real time_step)
Definition: ERF_MRI.H:166
int no_substepping
Should we not do acoustic substepping.
Definition: ERF_MRI.H:40
@ NumTypes
Definition: ERF_IndexDefines.H:162
@ T
Definition: ERF_IndexDefines.H:110
Definition: ERF_ConsoleIO.cpp:12