Simpact Cyan
Population based event driven simulation using mNRM
populationalgorithmtesting.h
Go to the documentation of this file.
1 #ifndef POPULATIONALGORITHMTESTING_H
2 
3 #define POPULATIONALGORITHMTESTING_H
4 
9 #include "algorithm.h"
10 #include "mutex.h"
11 #include "personbase.h"
12 #include "populationinterfaces.h"
13 #include "populationevent.h"
14 #include "personaleventlisttesting.h"
15 #include <assert.h>
16 
18 class PersonBase;
19 class PopulationEvent;
20 class PopulationStateTesting;
21 
83 {
84 public:
88  PopulationAlgorithmTesting(PopulationStateTesting &state, GslRandomNumberGenerator &rng, bool parallel);
90 
91  bool_t init();
92 
93  bool isParallel() const { return m_parallel; }
94  bool_t run(double &tMax, int64_t &maxEvents, double startTime = 0);
95  void onNewEvent(PopulationEvent *pEvt);
96 
97  // TODO: shield these from the user somehow? These functions should not be used
98  // directly by the user, they are used internally by the algorithm
99  void scheduleForRemoval(PopulationEvent *pEvt);
100 
101  double getTime() const { return Algorithm::getTime(); }
103 
104  void setAboutToFireAction(PopulationAlgorithmAboutToFireInterface *pAction) { m_pOnAboutToFire = pAction; }
105 private:
106  bool_t initEventTimes() const;
107  bool_t getNextScheduledEvent(double &dt, EventBase **ppEvt);
108  void advanceEventTimes(EventBase *pScheduledEvent, double dt);
109  void onAboutToFire(EventBase *pEvt) { if (m_pOnAboutToFire) m_pOnAboutToFire->onAboutToFire(static_cast<PopulationEvent *>(pEvt)); }
110  PopulationEvent *getEarliestEvent(const std::vector<PersonBase *> &people);
111  PersonalEventListTesting *personalEventList(PersonBase *pPerson);
112 
113  PopulationStateTesting &m_popState;
114  bool m_init;
115 
116 #ifdef ALGORITHM_SHOW_EVENTS
117  void showEvents(); // FOR DEBUGGING
118 #endif // ALGORITHM_SHOW_EVENTS
119  void onAlgorithmLoop(bool finished);
120 
121  int64_t getNextEventID();
122 
123  std::vector<EventBase *> m_eventsToRemove;
124 
125  // For the parallel version
126  bool m_parallel;
127 
128  int64_t m_nextEventID;
129 
130  PopulationAlgorithmAboutToFireInterface *m_pOnAboutToFire;
131 };
132 
133 inline int64_t PopulationAlgorithmTesting::getNextEventID()
134 {
135  int64_t id = m_nextEventID++;
136  return id;
137 }
138 
139 inline PersonalEventListTesting *PopulationAlgorithmTesting::personalEventList(PersonBase *pPerson)
140 {
141  assert(pPerson);
142  PersonalEventListTesting *pEvtList = static_cast<PersonalEventListTesting *>(pPerson->getAlgorithmInfo());
143  assert(pEvtList);
144  return pEvtList;
145 }
146 
147 #endif // POPULATIONALGORITHMTESTING_H
Type to return true/false with error description.
Definition: booltype.h:25
bool_t init()
Abstract function to initialize the implementation used.
Definition: populationalgorithmtesting.cpp:38
virtual void onAboutToFire(PopulationEvent *pEvt)=0
If set using PopulationAlgorithmInterface::setAboutToFireAction, this function will be called right b...
GslRandomNumberGenerator * getRandomNumberGenerator() const
Must return the random number generator used by the algorithm.
Definition: populationalgorithmtesting.h:102
bool_t run(double &tMax, int64_t &maxEvents, double startTime=0)
This should be called to actually start the simulation, do not call Algorithm::evolve for this...
Definition: populationalgorithmtesting.cpp:63
double getTime() const
Must return the simulation tilme of the algorithm.
Definition: populationalgorithmtesting.h:101
PopulationAlgorithmTesting(PopulationStateTesting &state, GslRandomNumberGenerator &rng, bool parallel)
Constructor of the class, indicating if a parallel version should be used, which random number genera...
Definition: populationalgorithmtesting.cpp:25
This class allows you to generate random numbers, and uses the GNU Scientific Library for this...
Definition: gslrandomnumbergenerator.h:16
double getTime() const
This function returns the current time of the simulation.
Definition: algorithm.h:155
GslRandomNumberGenerator * getRandomNumberGenerator() const
Returns the random number generator that was specified in the constructor.
Definition: algorithm.h:158
This class contains the core algorithm (as shown on the main page of the documentation) to execute th...
Definition: algorithm.h:81
This is the base class for events in population-based simulations.
Definition: populationevent.h:63
An interface to allow a member function PopulationAlgorithmAboutToFireInterface::onAboutToFire to be ...
Definition: populationinterfaces.h:89
This is the base class for a person in a population-based simulation.
Definition: personbase.h:23
This is the base class for events in the mNRM algorithm.
Definition: eventbase.h:55
PersonAlgorithmInfo * getAlgorithmInfo() const
Returns what was stored using PersonBase::PersonAlgorithmInfo.
Definition: personbase.h:77
void setAboutToFireAction(PopulationAlgorithmAboutToFireInterface *pAction)
Allows you to set the action that needs to be performed before firing an event dynamically.
Definition: populationalgorithmtesting.h:104
An interface for a population based mNRM algorithm.
Definition: populationinterfaces.h:101
void onNewEvent(PopulationEvent *pEvt)
When a new event has been created, it must be injected into the simulation using this function...
Definition: populationalgorithmtesting.cpp:288
This class provides functions for a population-based simulation using the modified Next Reaction Meth...
Definition: populationalgorithmtesting.h:82