Simpact Cyan
Population based event driven simulation using mNRM
algorithm.h
Go to the documentation of this file.
1 #ifndef ALGORITHM_H
2 
3 #define ALGORITHM_H
4 
9 #include "booltype.h"
10 #include <stdint.h>
11 #include <vector>
12 
13 //#define ALGORITHM_SHOW_EVENTS
14 
15 //#define ALGORITHM_DEBUG_TIMER
16 
18 class EventBase;
19 class Algorithm;
20 class Mutex;
21 
23 class State
24 {
25 public:
26  State();
27  virtual ~State();
28 
35  double getTime() const { return m_time; }
36 
42  void setTime(double t) { m_time = t; }
43 
44  // Protected by a mutex
45  void setAbortAlgorithm(const std::string &reason) const;
46 
47  // Will only be called from the main simulation thread
48  void clearAbort();
49  bool shouldAbortAlgorithm() const { return m_abort; }
50  std::string getAbortReason() const;
51 private:
52  double m_time;
53 
54  mutable bool m_abort;
55  mutable Mutex *m_pAbortMutex;
56  mutable std::string m_abortReason;
57 };
58 
81 class Algorithm
82 {
83 public:
87  virtual ~Algorithm();
88 
152  bool_t evolve(double &tMax, int64_t &maxEvents, double startTime = 0, bool initEvents = true);
153 
155  double getTime() const { return m_time; }
156 
158  GslRandomNumberGenerator *getRandomNumberGenerator() const { return m_pRndGen; }
159 protected:
161  State *getState() const { return m_pState; }
162 
165  virtual bool_t initEventTimes() const;
166 
169  virtual bool_t getNextScheduledEvent(double &dt, EventBase **ppEvt);
170 
173  virtual void advanceEventTimes(EventBase *pScheduledEvent, double dt);
174 
176  virtual void onAboutToFire(EventBase *pEvt) { }
177 
179  virtual void onFiredEvent(EventBase *pEvt) { }
180 
183  virtual void onAlgorithmLoop(bool finished) { }
184 private:
185  mutable GslRandomNumberGenerator *m_pRndGen;
186  State *m_pState;
187  double m_time;
188 };
189 
190 #endif // ALGORITHM_H
191 
This class contains the core algorithm (as shown on the main page of the documentation) to execute th...
Definition: algorithm.h:82
virtual void onAboutToFire(EventBase *pEvt)
Called right before pEvt is fired.
Definition: algorithm.h:176
virtual void advanceEventTimes(EventBase *pScheduledEvent, double dt)
Advance the times of the necessary events to the time when dt has passed, ignoring pScheduledEvent si...
Definition: algorithm.cpp:181
virtual void onAlgorithmLoop(bool finished)
Called at the end of each algorithm loop, with finished set to true if the loop will be exited.
Definition: algorithm.h:183
GslRandomNumberGenerator * getRandomNumberGenerator() const
Returns the random number generator that was specified in the constructor.
Definition: algorithm.h:158
virtual bool_t initEventTimes() const
Generate the internal times for the events present in the algorithm (called by State::evolve dependin...
Definition: algorithm.cpp:171
State * getState() const
Returns the simulation state instance that was specified in the constructor.
Definition: algorithm.h:161
Algorithm(State &state, GslRandomNumberGenerator &rng)
Constructor of the class, to which the simulation state must be specified as well as the random numbe...
Definition: algorithm.cpp:47
double getTime() const
This function returns the current time of the simulation.
Definition: algorithm.h:155
virtual void onFiredEvent(EventBase *pEvt)
Called after pEvt is fired.
Definition: algorithm.h:179
bool_t evolve(double &tMax, int64_t &maxEvents, double startTime=0, bool initEvents=true)
This advances the simulation state specified in the constructor using the core mNRM.
Definition: algorithm.cpp:62
virtual bool_t getNextScheduledEvent(double &dt, EventBase **ppEvt)
Store the next event to be fired in ppEvt and store the real world time that will have passed until i...
Definition: algorithm.cpp:176
This is the base class for events in the mNRM algorithm.
Definition: eventbase.h:56
This class allows you to generate random numbers, and uses the GNU Scientific Library for this.
Definition: gslrandomnumbergenerator.h:17
This is a base class describing the simulation state of an mNRM algorithm.
Definition: algorithm.h:24
void setTime(double t)
Set the simulation time, which is normally only done by the Algorithm in use (but may be necessary wh...
Definition: algorithm.h:42
double getTime() const
Retrieve the current simulation time, which is stored by the Algorithm instance in use (note that unt...
Definition: algorithm.h:35
Type to return true/false with error description.
Definition: booltype.h:26