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 
17 #ifdef STATE_SHOW_EVENTS
18 #include <iostream>
19 #endif // STATE_SHOW_EVENTS
20 
22 class PersonBase;
23 class PopulationEvent;
25 
87 {
88 public:
94 
95  bool_t init();
96 
97  bool isParallel() const { return m_parallel; }
98  bool_t run(double &tMax, int64_t &maxEvents, double startTime = 0);
99  void onNewEvent(PopulationEvent *pEvt);
100 
101  // TODO: shield these from the user somehow? These functions should not be used
102  // directly by the user, they are used internally by the algorithm
103  void scheduleForRemoval(PopulationEvent *pEvt);
104  void lockEvent(PopulationEvent *pEvt) const;
105  void unlockEvent(PopulationEvent *pEvt) const;
106 
107  double getTime() const { return Algorithm::getTime(); }
109 
110  void setAboutToFireAction(PopulationAlgorithmAboutToFireInterface *pAction) { m_pOnAboutToFire = pAction; }
111 private:
112  bool_t initEventTimes() const;
113  bool_t getNextScheduledEvent(double &dt, EventBase **ppEvt);
114  void advanceEventTimes(EventBase *pScheduledEvent, double dt);
115  void onAboutToFire(EventBase *pEvt);
116  PopulationEvent *getEarliestEvent(const std::vector<PersonBase *> &people);
117  PersonalEventList *personalEventList(PersonBase *pPerson);
118 
119  PopulationStateAdvanced &m_popState;
120  bool m_init;
121 
122 #ifdef ALGORITHM_SHOW_EVENTS
123  void showEvents(); // FOR DEBUGGING
124 #endif // ALGORITHM_SHOW_EVENTS
125  void onAlgorithmLoop(bool finished);
126 
127  int64_t getNextEventID();
128 
129 #ifndef DISABLEOPENMP
130  Mutex m_eventsToRemoveMutex;
131 #endif // !DISABLEOPENMP
132  std::vector<EventBase *> m_eventsToRemove;
133 
134  // For the parallel version
135  bool m_parallel;
136 
137  int64_t m_nextEventID;
138 #ifndef DISABLEOPENMP
139  Mutex m_nextEventIDMutex;
140 #endif // !DISABLEOPENMP
141 
142  std::vector<PopulationEvent *> m_tmpEarliestEvents;
143  std::vector<double> m_tmpEarliestTimes;
144 
145 #ifndef DISABLEOPENMP
146  mutable std::vector<Mutex> m_eventMutexes;
147 #endif // !DISABLEOPENMP
148 
149  PopulationAlgorithmAboutToFireInterface *m_pOnAboutToFire;
150 };
151 
152 inline int64_t PopulationAlgorithmAdvanced::getNextEventID()
153 {
154 #ifndef DISABLEOPENMP
155  if (m_parallel)
156  m_nextEventIDMutex.lock();
157 #endif // !DISABLEOPENMP
158 
159  int64_t id = m_nextEventID++;
160 
161 #ifndef DISABLEOPENMP
162  if (m_parallel)
163  m_nextEventIDMutex.unlock();
164 #endif // !DISABLEOPENMP
165 
166  return id;
167 }
168 
169 inline PersonalEventList *PopulationAlgorithmAdvanced::personalEventList(PersonBase *pPerson)
170 {
171  assert(pPerson);
172  PersonalEventList *pEvtList = static_cast<PersonalEventList *>(pPerson->getAlgorithmInfo());
173  assert(pEvtList);
174  return pEvtList;
175 }
176 
177 inline void PopulationAlgorithmAdvanced::onAboutToFire(EventBase *pEvt)
178 {
179 #ifdef STATE_SHOW_EVENTS
180  std::cerr << getTime() << "\t" << static_cast<PopulationEvent *>(pEvt)->getDescription(getTime()) << std::endl;
181 #endif // STATE_SHOW_EVENTS
182 
183  if (m_pOnAboutToFire)
184  m_pOnAboutToFire->onAboutToFire(static_cast<PopulationEvent *>(pEvt));
185 }
186 
187 #endif // POPULATIONALGORITHMADVANCED_H
This class contains the core algorithm (as shown on the main page of the documentation) to execute th...
Definition: algorithm.h:82
GslRandomNumberGenerator * getRandomNumberGenerator() const
Returns the random number generator that was specified in the constructor.
Definition: algorithm.h:158
double getTime() const
This function returns the current time of the simulation.
Definition: algorithm.h:155
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 the base class for a person in a population-based simulation.
Definition: personbase.h:24
PersonAlgorithmInfo * getAlgorithmInfo() const
Returns what was stored using PersonBase::PersonAlgorithmInfo.
Definition: personbase.h:77
An interface to allow a member function PopulationAlgorithmAboutToFireInterface::onAboutToFire to be ...
Definition: populationinterfaces.h:90
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:87
bool_t init()
Abstract function to initialize the implementation used.
Definition: populationalgorithmadvanced.cpp:41
double getTime() const
Must return the simulation tilme of the algorithm.
Definition: populationalgorithmadvanced.h:107
GslRandomNumberGenerator * getRandomNumberGenerator() const
Must return the random number generator used by the algorithm.
Definition: populationalgorithmadvanced.h:108
void onNewEvent(PopulationEvent *pEvt)
When a new event has been created, it must be injected into the simulation using this function.
Definition: populationalgorithmadvanced.cpp:415
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:83
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:28
void setAboutToFireAction(PopulationAlgorithmAboutToFireInterface *pAction)
Allows you to set the action that needs to be performed before firing an event dynamically.
Definition: populationalgorithmadvanced.h:110
An interface for a population based mNRM algorithm.
Definition: populationinterfaces.h:102
This is the base class for events in population-based simulations.
Definition: populationevent.h:64
Population state to be used when simulating with the population based algorithm in PopulationAlgorith...
Definition: populationstateadvanced.h:23
Type to return true/false with error description.
Definition: booltype.h:26