Simpact Cyan
Population based event driven simulation using mNRM
populationalgorithmadvanced.h
Go to the documentation of this file.
1 #ifndef POPULATIONALGORITHMADVANCED_H
2 
3 #define POPULATIONALGORITHMADVANCED_H
4 
9 #include "algorithm.h"
10 #include "mutex.h"
11 #include "personbase.h"
12 #include "populationinterfaces.h"
13 #include "populationevent.h"
14 #include "personaleventlist.h"
15 #include <assert.h>
16 
18 class PersonBase;
19 class PopulationEvent;
21 
83 {
84 public:
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  void lockEvent(PopulationEvent *pEvt) const;
101  void unlockEvent(PopulationEvent *pEvt) const;
102 
103  double getTime() const { return Algorithm::getTime(); }
105 
106  void setAboutToFireAction(PopulationAlgorithmAboutToFireInterface *pAction) { m_pOnAboutToFire = pAction; }
107 private:
108  bool_t initEventTimes() const;
109  bool_t getNextScheduledEvent(double &dt, EventBase **ppEvt);
110  void advanceEventTimes(EventBase *pScheduledEvent, double dt);
111  void onAboutToFire(EventBase *pEvt) { if (m_pOnAboutToFire) m_pOnAboutToFire->onAboutToFire(static_cast<PopulationEvent *>(pEvt)); }
112  PopulationEvent *getEarliestEvent(const std::vector<PersonBase *> &people);
113  PersonalEventList *personalEventList(PersonBase *pPerson);
114 
115  PopulationStateAdvanced &m_popState;
116  bool m_init;
117 
118 #ifdef STATE_SHOW_EVENTS
119  void showEvents(); // FOR DEBUGGING
120 #endif // STATE_SHOW_EVENTS
121  void onAlgorithmLoop();
122 
123  int64_t getNextEventID();
124 
125 #ifndef DISABLEOPENMP
126  Mutex m_eventsToRemoveMutex;
127 #endif // !DISABLEOPENMP
128  std::vector<EventBase *> m_eventsToRemove;
129 
130  // For the parallel version
131  bool m_parallel;
132 
133  int64_t m_nextEventID;
134 #ifndef DISABLEOPENMP
135  Mutex m_nextEventIDMutex;
136 #endif // !DISABLEOPENMP
137 
138  std::vector<PopulationEvent *> m_tmpEarliestEvents;
139  std::vector<double> m_tmpEarliestTimes;
140 
141 #ifndef DISABLEOPENMP
142  mutable std::vector<Mutex> m_eventMutexes;
143 #endif // !DISABLEOPENMP
144 
145  PopulationAlgorithmAboutToFireInterface *m_pOnAboutToFire;
146 };
147 
148 inline int64_t PopulationAlgorithmAdvanced::getNextEventID()
149 {
150 #ifndef DISABLEOPENMP
151  if (m_parallel)
152  m_nextEventIDMutex.lock();
153 #endif // !DISABLEOPENMP
154 
155  int64_t id = m_nextEventID++;
156 
157 #ifndef DISABLEOPENMP
158  if (m_parallel)
159  m_nextEventIDMutex.unlock();
160 #endif // !DISABLEOPENMP
161 
162  return id;
163 }
164 
165 inline PersonalEventList *PopulationAlgorithmAdvanced::personalEventList(PersonBase *pPerson)
166 {
167  assert(pPerson);
168  PersonalEventList *pEvtList = static_cast<PersonalEventList *>(pPerson->getAlgorithmInfo());
169  assert(pEvtList);
170  return pEvtList;
171 }
172 
173 #endif // POPULATIONALGORITHMADVANCED_H
Type to return true/false with error description.
Definition: booltype.h:25
virtual void onAboutToFire(PopulationEvent *pEvt)=0
If set using PopulationAlgorithmInterface::setAboutToFireAction, this function will be called right b...
This class provides functions for a population-based simulation using the modified Next Reaction Meth...
Definition: populationalgorithmadvanced.h:82
void setAboutToFireAction(PopulationAlgorithmAboutToFireInterface *pAction)
Allows you to set the action that needs to be performed before firing an event dynamically.
Definition: populationalgorithmadvanced.h:106
PopulationAlgorithmAdvanced(PopulationStateAdvanced &state, GslRandomNumberGenerator &rng, bool parallel)
Constructor of the class, indicating if a parallel version should be used, which random number genera...
Definition: populationalgorithmadvanced.cpp:27
double getTime() const
Must return the simulation tilme of the algorithm.
Definition: populationalgorithmadvanced.h:103
bool_t init()
Abstract function to initialize the implementation used.
Definition: populationalgorithmadvanced.cpp:40
GslRandomNumberGenerator * getRandomNumberGenerator() const
Must return the random number generator used by the algorithm.
Definition: populationalgorithmadvanced.h:104
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:140
GslRandomNumberGenerator * getRandomNumberGenerator() const
Returns the random number generator that was specified in the constructor.
Definition: algorithm.h:143
This class contains the core algorithm (as shown on the main page of the documentation) to execute th...
Definition: algorithm.h:66
This is the base class for events in population-based simulations.
Definition: populationevent.h:61
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
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: populationalgorithmadvanced.cpp:82
Population state to be used when simulating with the population based algorithm in PopulationAlgorith...
Definition: populationstateadvanced.h:22
void onNewEvent(PopulationEvent *pEvt)
When a new event has been created, it must be injected into the simulation using this function...
Definition: populationalgorithmadvanced.cpp:389
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
An interface for a population based mNRM algorithm.
Definition: populationinterfaces.h:101