Simpact Cyan
Population based event driven simulation using mNRM
populationevent.h
Go to the documentation of this file.
1 #ifndef POPULATIONEVENT_H
2 
3 #define POPULATIONEVENT_H
4 
9 #include "eventbase.h"
10 #include "population.h"
11 #include <assert.h>
12 #include <stdlib.h>
13 #include <string>
14 #include <iostream>
15 
16 #define POPULATIONEVENT_MAXPERSONS 2
17 
18 class PersonBase;
19 
61 class PopulationEvent : public EventBase
62 {
63 public:
71 
73  PopulationEvent(PersonBase *pPerson);
74 
76  PopulationEvent(PersonBase *pPerson1, PersonBase *pPerson2);
77  ~PopulationEvent();
78 
79  // These are for internal use
80  void setGlobalEventPerson(PersonBase *pDummyPerson);
81  void setEventID(int64_t id) { assert(m_eventID < 0); assert(id >= 0); m_eventID = id; }
82  int64_t getEventID() const { return m_eventID; }
83 
88  int getNumberOfPersons() const { assert(m_numPersons >= 0 && m_numPersons <= POPULATIONEVENT_MAXPERSONS); return (int)m_numPersons; }
89 
92  PersonBase *getPerson(int idx) const;
93 
96  virtual bool isEveryoneAffected() const { return false; }
97 
100  virtual void markOtherAffectedPeople(const Population &population) { }
101 
104  virtual bool areGlobalEventsAffected() const { return false; }
105 
108  virtual std::string getDescription(double tNow) const { return std::string("No description given"); }
109 
110  // TODO: shield these from the user somehow? These functions are used internally
111  // by the algorithm and should not be called directly by the user. */
112  void setEventIndex(PersonBase *pPerson, int idx);
113  int getEventIndex(PersonBase *pPerson) const;
114 
115  void setScheduledForRemoval() { m_scheduledForRemoval = true; }
116  bool isScheduledForRemoval() const { return m_scheduledForRemoval; }
117 
118  bool isNoLongerUseful();
119 protected:
124  virtual bool isUseless() { return false; }
125 private:
126  void commonConstructor();
127 
128  PersonBase *m_pPersons[POPULATIONEVENT_MAXPERSONS];
129  int m_eventIndex[POPULATIONEVENT_MAXPERSONS];
130  int8_t m_numPersons;
131 
132  bool m_scheduledForRemoval;
133  int64_t m_eventID;
134 };
135 
136 inline void PopulationEvent::setEventIndex(PersonBase *pPerson, int idx)
137 {
138  assert(pPerson != 0);
139  int num = m_numPersons;
140 
141  for (int i = 0 ; i < num ; i++)
142  {
143  assert(m_pPersons[i] != 0);
144 
145  if (pPerson == m_pPersons[i])
146  {
147  m_eventIndex[i] = idx;
148  return;
149  }
150  }
151 
152  std::cerr << "PopulationEvent::setEventIndex: Consistency error: invalid Person object in setEventIndex" << std::endl;
153  abort();
154 }
155 
156 inline int PopulationEvent::getEventIndex(PersonBase *pPerson) const
157 {
158  assert(pPerson != 0);
159  int num = m_numPersons;
160 
161  for (int i = 0 ; i < num ; i++)
162  {
163  assert(m_pPersons[i] != 0);
164 
165  if (pPerson == m_pPersons[i])
166  return m_eventIndex[i];
167  }
168 
169  std::cerr << "PopulationEvent::getEventIndex: Consistency error: invalid Person object in getEventIndex" << std::endl;
170  abort();
171 }
172 
173 #ifdef NDEBUG
174 inline PersonBase *PopulationEvent::getPerson(int idx) const
175 {
176  return m_pPersons[idx];
177 }
178 #endif
179 
180 #endif // POPULATIONEVENT_H
virtual bool isUseless()
This function can be used to inform the algorithm that an event is no longer of any use and should be...
Definition: populationevent.h:124
int getNumberOfPersons() const
Returns the number of people specified during the creation of the event (will be one for global event...
Definition: populationevent.h:88
This class provides functions for a population-based simulation using the modified Next Reaction Meth...
Definition: population.h:83
PersonBase * getPerson(int idx) const
Returns a person that was specified when the event was constructed, where idx can range from 0 to Pop...
Definition: populationevent.cpp:83
PopulationEvent()
Constructs a 'global' event.
Definition: populationevent.cpp:27
virtual void markOtherAffectedPeople(const Population &population)
If other people than the one(s) mentioned in the constructor are also affected by this event...
Definition: populationevent.h:100
This is the base class for events in population-based simulations which use the Population class...
Definition: populationevent.h:61
virtual bool areGlobalEventsAffected() const
If global events (not referring to a particular person) are affected, this function can be overridden...
Definition: populationevent.h:104
This is the base class for a person in a population-based simulation that used the Population class...
Definition: personbase.h:23
virtual std::string getDescription(double tNow) const
Returns a short description of the event, can be useful for logging/debugging purposes.
Definition: populationevent.h:108
virtual bool isEveryoneAffected() const
If the entire population is affected by this event (should be avoided!), this function can be overrid...
Definition: populationevent.h:96
This is the base class for events in the mNRM algorithm.
Definition: eventbase.h:62