Chombo + EB  3.0
Scheduler.H
Go to the documentation of this file.
1 #ifdef CH_LANG_CC
2 /*
3  * _______ __
4  * / ___/ / ___ __ _ / / ___
5  * / /__/ _ \/ _ \/ V \/ _ \/ _ \
6  * \___/_//_/\___/_/_/_/_.__/\___/
7  * Please refer to Copyright.txt, in Chombo's root directory.
8  */
9 #endif
10 
11 #ifndef _SCHEDULER_H_
12 #define _SCHEDULER_H_
13 
14 #include <map>
15 #include "CH_assert.H"
16 #include "REAL.H"
17 #include "RefCountedPtr.H"
18 #include "NamespaceHeader.H"
19 
20 // Forward declaration of AMR class.
21 class AMR;
22 
23 //! \class Scheduler
24 //! This class executes functions at periodic intervals during an
25 //! AMR simulation.
26 class Scheduler
27 {
28  public:
29 
30  //! \class PeriodicFunction
31  //! This function is called periodically by the Scheduler. Its
32  //! call operator must be overridden to define its behavior.
34  {
35  public:
36 
37  //! Default construction.
39 
40  //! Destructor.
41  virtual ~PeriodicFunction();
42 
43  //! Override this method to prepare the periodic function to interact
44  //! with the given AMR object when set to an interval in time steps.
45  //! By default this does nothing.
46  //! \param a_AMR The AMR object with which this function will interact
47  //! during periodic function calls.
48  //! \param a_interval The interval (in steps) at which the periodic function
49  //! is to be called.
50  virtual void setUp(AMR& a_AMR, int a_interval);
51 
52  //! Override this method to prepare the periodic function to interact
53  //! with the given AMR object when set to an interval in time units.
54  //! By default this does nothing.
55  //! \param a_AMR The AMR object with which this function will interact
56  //! during periodic function calls.
57  //! \param a_interval The interval (in simulation time units) at which
58  //! the periodic function is to be called.
59  virtual void setUp(AMR& a_AMR, Real a_interval);
60 
61  //! Override this operator to define the behavior of the periodic function.
62  //! \param a_step The step at which the function is called.
63  //! \param a_time The simulation time at which the function is called.
64  virtual void operator()(int a_step, Real a_time) = 0;
65 
66  //! Override this operator to perform a task at the conclusion of a
67  //! simulation. This is called by the conclude() method of AMR and
68  //! is fed the final step and simulation time. By default, this does
69  //! nothing.
70  //! \param a_step The step at which the function is called.
71  //! \param a_time The simulation time at which the function is called.
72  virtual void conclude(int a_step, Real a_time);
73 
74  private:
75 
78  };
79 
80  //! Ordering operator for pointers to periodic functions.
82  {
84  const RefCountedPtr<PeriodicFunction>& a_rhs) const
85  {
86  CH_assert(!a_lhs.isNull() && !a_rhs.isNull());
87  // Just use the pointer's address to order the two.
88  return (&*a_lhs < &*a_rhs);
89  }
90  };
91 
92  //! Default constructor. Makes an empty schedule.
93  Scheduler();
94 
95  //! Destructor.
96  virtual ~Scheduler();
97 
98  //! Add a periodic function that is called every \a a_interval steps.
99  //! \param a_function The function to be called periodically.
100  //! \param a_interval The number of steps that elapse between calls to \a a_function.
102  int a_interval);
103 
104  //! Add a periodic function that is called every \a a_interval time units.
105  //! \param a_function The function to be called periodically.
106  //! \param a_interval The number of simulation time units that elapse between calls to \a a_function.
108  Real a_interval);
109 
110  //! Execute the schedule on the given step and at the given time.
111  //! \param a_step The step in the simulation at which the schedule is executed.
112  //! \param a_time The simulation time at which the schedule is executed.
113  void execute(int a_step, Real a_time) const;
114 
115  //! This function is called by the associated AMR object to set up
116  //! interactions between itself and the periodic functions within the scheduler.
117  void setUp(AMR& a_AMR);
118 
119  //! This function is called by the associated AMR object upon the conclusion
120  //! of a simulation.
121  //! \param a_step The step in the simulation at which the schedule is executed.
122  //! \param a_time The simulation time at which the schedule is executed.
123  void conclude(int a_step, Real a_time) const;
124 
125  private:
126 
127  // Step-triggered functions.
128  mutable std::map<RefCountedPtr<PeriodicFunction>, int, PeriodicFunctionLessThan> m_stepTriggeredFunctions;
129 
130  // Time-triggered functions.
131  mutable std::map<RefCountedPtr<PeriodicFunction>, std::pair<Real, Real>, PeriodicFunctionLessThan> m_timeTriggeredFunctions;
132 
133  // Forbidden.
134  Scheduler(const Scheduler&);
135  Scheduler& operator=(const Scheduler&);
136 };
137 
138 //! \class PlotterPeriodicFunction
139 //! This placebo allows one to enable periodic plots using the Scheduler
140 //! mechanism.
142 {
143  public:
144 
145  explicit PlotterPeriodicFunction(const std::string& a_prefix);
146  void operator()(int a_step, Real a_time);
147 
148  void setUp(AMR& a_AMR, int a_interval);
149  void setUp(AMR& a_AMR, Real a_interval);
150  void conclude(int a_step, Real a_time);
151 
152  private:
153 
154  std::string m_prefix;
155 };
156 
157 //! \class CheckpointPeriodicFunction
158 //! This placebo allows one to enable periodic checkpoints using the Scheduler
159 //! mechanism.
161 {
162  public:
163 
164  explicit CheckpointPeriodicFunction(const std::string& a_prefix);
165 
166  void setUp(AMR& a_AMR, int a_interval);
167  void operator()(int a_step, Real a_time);
168 
169  private:
170 
171  std::string m_prefix;
172 };
173 
174 #include "NamespaceFooter.H"
175 #endif
Definition: Scheduler.H:141
bool operator()(const RefCountedPtr< PeriodicFunction > &a_lhs, const RefCountedPtr< PeriodicFunction > &a_rhs) const
Definition: Scheduler.H:83
Definition: Scheduler.H:26
A reference-counting handle class.
Definition: RefCountedPtr.H:66
PeriodicFunction()
Default construction.
#define CH_assert(cond)
Definition: CHArray.H:37
void setUp(AMR &a_AMR, int a_interval)
std::map< RefCountedPtr< PeriodicFunction >, std::pair< Real, Real >, PeriodicFunctionLessThan > m_timeTriggeredFunctions
Definition: Scheduler.H:131
std::string m_prefix
Definition: Scheduler.H:171
virtual void setUp(AMR &a_AMR, int a_interval)
Scheduler & operator=(const Scheduler &)
CheckpointPeriodicFunction(const std::string &a_prefix)
Ordering operator for pointers to periodic functions.
Definition: Scheduler.H:81
void setUp(AMR &a_AMR, int a_interval)
virtual void conclude(int a_step, Real a_time)
Definition: Scheduler.H:33
void conclude(int a_step, Real a_time) const
PlotterPeriodicFunction(const std::string &a_prefix)
virtual void operator()(int a_step, Real a_time)=0
Definition: Scheduler.H:160
double Real
Definition: REAL.H:33
Scheduler()
Default constructor. Makes an empty schedule.
virtual ~Scheduler()
Destructor.
virtual ~PeriodicFunction()
Destructor.
Framework for Berger-Oliger timestepping for AMR.
Definition: AMR.H:52
void setUp(AMR &a_AMR)
void conclude(int a_step, Real a_time)
bool isNull() const
Definition: RefCountedPtr.H:223
void schedule(RefCountedPtr< PeriodicFunction > a_function, int a_interval)
void operator()(int a_step, Real a_time)
std::map< RefCountedPtr< PeriodicFunction >, int, PeriodicFunctionLessThan > m_stepTriggeredFunctions
Definition: Scheduler.H:128
std::string m_prefix
Definition: Scheduler.H:154
void execute(int a_step, Real a_time) const
void operator()(int a_step, Real a_time)
PeriodicFunction & operator=(const PeriodicFunction &)