00001 #ifdef CH_LANG_CC 00002 /* 00003 * _______ __ 00004 * / ___/ / ___ __ _ / / ___ 00005 * / /__/ _ \/ _ \/ V \/ _ \/ _ \ 00006 * \___/_//_/\___/_/_/_/_.__/\___/ 00007 * Please refer to Copyright.txt, in Chombo's root directory. 00008 */ 00009 #endif 00010 00011 #ifndef _SCHEDULER_H_ 00012 #define _SCHEDULER_H_ 00013 00014 #include <map> 00015 #include "CH_assert.H" 00016 #include "REAL.H" 00017 #include "RefCountedPtr.H" 00018 #include "NamespaceHeader.H" 00019 00020 // Forward declaration of AMR class. 00021 class AMR; 00022 00023 //! \class Scheduler 00024 //! This class executes functions at periodic intervals during an 00025 //! AMR simulation. 00026 class Scheduler 00027 { 00028 public: 00029 00030 //! \class PeriodicFunction 00031 //! This function is called periodically by the Scheduler. Its 00032 //! call operator must be overridden to define its behavior. 00033 class PeriodicFunction 00034 { 00035 public: 00036 00037 //! Default construction. 00038 PeriodicFunction(); 00039 00040 //! Destructor. 00041 virtual ~PeriodicFunction(); 00042 00043 //! Override this method to prepare the periodic function to interact 00044 //! with the given AMR object when set to an interval in time steps. 00045 //! By default this does nothing. 00046 //! \param a_AMR The AMR object with which this function will interact 00047 //! during periodic function calls. 00048 //! \param a_interval The interval (in steps) at which the periodic function 00049 //! is to be called. 00050 virtual void setUp(AMR& a_AMR, int a_interval); 00051 00052 //! Override this method to prepare the periodic function to interact 00053 //! with the given AMR object when set to an interval in time units. 00054 //! By default this does nothing. 00055 //! \param a_AMR The AMR object with which this function will interact 00056 //! during periodic function calls. 00057 //! \param a_interval The interval (in simulation time units) at which 00058 //! the periodic function is to be called. 00059 virtual void setUp(AMR& a_AMR, Real a_interval); 00060 00061 //! Override this operator to define the behavior of the periodic function. 00062 //! \param a_step The step at which the function is called. 00063 //! \param a_time The simulation time at which the function is called. 00064 virtual void operator()(int a_step, Real a_time) = 0; 00065 00066 //! Override this operator to perform a task at the conclusion of a 00067 //! simulation. This is called by the conclude() method of AMR and 00068 //! is fed the final step and simulation time. By default, this does 00069 //! nothing. 00070 //! \param a_step The step at which the function is called. 00071 //! \param a_time The simulation time at which the function is called. 00072 virtual void conclude(int a_step, Real a_time); 00073 00074 private: 00075 00076 PeriodicFunction(const PeriodicFunction&); 00077 PeriodicFunction& operator=(const PeriodicFunction&); 00078 }; 00079 00080 //! Ordering operator for pointers to periodic functions. 00081 struct PeriodicFunctionLessThan 00082 { 00083 bool operator()(const RefCountedPtr<PeriodicFunction>& a_lhs, 00084 const RefCountedPtr<PeriodicFunction>& a_rhs) const 00085 { 00086 CH_assert(!a_lhs.isNull() && !a_rhs.isNull()); 00087 // Just use the pointer's address to order the two. 00088 return (&*a_lhs < &*a_rhs); 00089 } 00090 }; 00091 00092 //! Default constructor. Makes an empty schedule. 00093 Scheduler(); 00094 00095 //! Destructor. 00096 virtual ~Scheduler(); 00097 00098 //! Add a periodic function that is called every \a a_interval steps. 00099 //! \param a_function The function to be called periodically. 00100 //! \param a_interval The number of steps that elapse between calls to \a a_function. 00101 void schedule(RefCountedPtr<PeriodicFunction> a_function, 00102 int a_interval); 00103 00104 //! Add a periodic function that is called every \a a_interval time units. 00105 //! \param a_function The function to be called periodically. 00106 //! \param a_interval The number of simulation time units that elapse between calls to \a a_function. 00107 void schedule(RefCountedPtr<PeriodicFunction> a_function, 00108 Real a_interval); 00109 00110 //! Execute the schedule on the given step and at the given time. 00111 //! \param a_step The step in the simulation at which the schedule is executed. 00112 //! \param a_time The simulation time at which the schedule is executed. 00113 void execute(int a_step, Real a_time) const; 00114 00115 //! This function is called by the associated AMR object to set up 00116 //! interactions between itself and the periodic functions within the scheduler. 00117 void setUp(AMR& a_AMR); 00118 00119 //! This function is called by the associated AMR object upon the conclusion 00120 //! of a simulation. 00121 //! \param a_step The step in the simulation at which the schedule is executed. 00122 //! \param a_time The simulation time at which the schedule is executed. 00123 void conclude(int a_step, Real a_time) const; 00124 00125 private: 00126 00127 // Step-triggered functions. 00128 mutable std::map<RefCountedPtr<PeriodicFunction>, int, PeriodicFunctionLessThan> m_stepTriggeredFunctions; 00129 00130 // Time-triggered functions. 00131 mutable std::map<RefCountedPtr<PeriodicFunction>, std::pair<Real, Real>, PeriodicFunctionLessThan> m_timeTriggeredFunctions; 00132 00133 // Forbidden. 00134 Scheduler(const Scheduler&); 00135 Scheduler& operator=(const Scheduler&); 00136 }; 00137 00138 //! \class PlotterPeriodicFunction 00139 //! This placebo allows one to enable periodic plots using the Scheduler 00140 //! mechanism. 00141 class PlotterPeriodicFunction: public Scheduler::PeriodicFunction 00142 { 00143 public: 00144 00145 explicit PlotterPeriodicFunction(const std::string& a_prefix); 00146 void operator()(int a_step, Real a_time); 00147 00148 void setUp(AMR& a_AMR, int a_interval); 00149 void setUp(AMR& a_AMR, Real a_interval); 00150 void conclude(int a_step, Real a_time); 00151 00152 private: 00153 00154 std::string m_prefix; 00155 }; 00156 00157 //! \class CheckpointPeriodicFunction 00158 //! This placebo allows one to enable periodic checkpoints using the Scheduler 00159 //! mechanism. 00160 class CheckpointPeriodicFunction: public Scheduler::PeriodicFunction 00161 { 00162 public: 00163 00164 explicit CheckpointPeriodicFunction(const std::string& a_prefix); 00165 00166 void setUp(AMR& a_AMR, int a_interval); 00167 void operator()(int a_step, Real a_time); 00168 00169 private: 00170 00171 std::string m_prefix; 00172 }; 00173 00174 #include "NamespaceFooter.H" 00175 #endif