Simpact Cyan
Population based event driven simulation using mNRM
Public Member Functions | Protected Member Functions | List of all members
State Class Reference

This class both describes the simulation state and contains the core algorithm (as shown on the main page of the documentation) to execute the modified next reaction method (mNRM). More...

#include <state.h>

Inheritance diagram for State:
Inheritance graph
[legend]
Collaboration diagram for State:
Collaboration graph
[legend]

Public Member Functions

 State (GslRandomNumberGenerator *pRng)
 Constructor of the class, to which the random number generator to be used internally must be specified. More...
 
bool evolve (double &tMax, int64_t &maxEvents, double startTime=0, bool initEvents=true)
 This advances the simulation state using the core mNRM. More...
 
double getTime () const
 This function returns the current time of the simulation. More...
 
GslRandomNumberGeneratorgetRandomNumberGenerator () const
 Returns the random number generator that was specified in the constructor. More...
 
- Public Member Functions inherited from errut::ErrorBase
 ErrorBase ()
 Creates an instance without an explicit object name. More...
 
 ErrorBase (const std::string &objName)
 Creates an instance with the object name set to objName. More...
 
std::string getObjectName () const
 Returns the stored object name. More...
 
std::string getErrorString () const
 Returns the currently stored error message. More...
 

Protected Member Functions

virtual bool initEventTimes () const
 Generate the internal times for the events present in the algorithm (called by State::evolve depending on the value of the initEvents parameter). More...
 
virtual EventBasegetNextScheduledEvent (double &dt)
 Return the next event to be fired and store the real world time that will have passed until it fires in dt. More...
 
virtual void advanceEventTimes (EventBase *pScheduledEvent, double dt)
 Advance the times of the necessary events to the time when dt has passed, ignoring pScheduledEvent since this is the one we will be firing. More...
 
virtual void onAboutToFire (EventBase *pEvt)
 Called right before pEvt is fired. More...
 
virtual void onFiredEvent (EventBase *pEvt)
 Called after pEvt is fired. More...
 
virtual void onAlgorithmLoop (bool finished)
 Called at the end of each algorithm loop, with finished set to true if the loop will be exited. More...
 
- Protected Member Functions inherited from errut::ErrorBase
void setErrorString (const std::string &str) const
 Derived classes can use this member function to store an error message. More...
 

Detailed Description

This class both describes the simulation state and contains the core algorithm (as shown on the main page of the documentation) to execute the modified next reaction method (mNRM).

Using this class alone will not work however, since it does not contain an implementation of several necessary functions. At leest three functions must be implemented in a subclass:

A very straightforward implementation is available in the SimpleState class, where no attempt is made to avoid unnecessary recalculations.

Constructor & Destructor Documentation

State::State ( GslRandomNumberGenerator pRng)

Constructor of the class, to which the random number generator to be used internally must be specified.

Member Function Documentation

void State::advanceEventTimes ( EventBase pScheduledEvent,
double  dt 
)
protectedvirtual

Advance the times of the necessary events to the time when dt has passed, ignoring pScheduledEvent since this is the one we will be firing.

bool State::evolve ( double &  tMax,
int64_t &  maxEvents,
double  startTime = 0,
bool  initEvents = true 
)

This advances the simulation state using the core mNRM.

Parameters
tMaxStop the simulation if the simulation time exceeds the specified time. Upon completion of the function, this variable will contain the actual simulation time stopped.
maxEventsIf positive, the simulation will stop if this many events have been executed. Set to a negative value to disable this limit. At the end of the simulation, this variable will contain the number of events executed.
startTimeThe start time of the simulation, can be used to continue where a previous call to this function left off.
initEventsIf set to true, the State::initEvents function will be called before entering the algorithm loop.

The algorithm executed is the following:

if (initEvents)
bool done = false;
int64_t eventCount = 0;
m_time = startTime;
while (!done)
{
double dtMin = -1;
EventBase *pNextScheduledEvent = getNextScheduledEvent(dtMin);
if (pNextScheduledEvent == 0)
return false;
advanceEventTimes(pNextScheduledEvent, dtMin);
m_time += dtMin;
onAboutToFire(pNextScheduledEvent);
pNextScheduledEvent->fire(this, m_time);
// If the event is still being used (the default) we'll need a new random number
if (!pNextScheduledEvent->willBeRemoved())
pNextScheduledEvent->generateNewInternalTimeDifference(m_pRndGen, this);
eventCount++;
if (m_time > tMax || (maxEvents > 0 && eventCount >= maxEvents))
done = true;
onFiredEvent(pNextScheduledEvent);
}
tMax = m_time;
maxEvents = eventCount;
return true;

Apart from the core functions State::initEventTimes, State::getNextScheduledEvent and State::advanceEventTimes which need to be provided by an implementation to get a working algorithm, a few extra functions can come in handy as well:

  • onAboutToFire: called right before an event will fire
  • onFiredEvent: called right after an event fired
  • onAboutToFire: called when the algoritm is going to loop
EventBase * State::getNextScheduledEvent ( double &  dt)
protectedvirtual

Return the next event to be fired and store the real world time that will have passed until it fires in dt.

GslRandomNumberGenerator* State::getRandomNumberGenerator ( ) const
inline

Returns the random number generator that was specified in the constructor.

double State::getTime ( ) const
inline

This function returns the current time of the simulation.

bool State::initEventTimes ( ) const
protectedvirtual

Generate the internal times for the events present in the algorithm (called by State::evolve depending on the value of the initEvents parameter).

virtual void State::onAboutToFire ( EventBase pEvt)
inlineprotectedvirtual

Called right before pEvt is fired.

virtual void State::onAlgorithmLoop ( bool  finished)
inlineprotectedvirtual

Called at the end of each algorithm loop, with finished set to true if the loop will be exited.

virtual void State::onFiredEvent ( EventBase pEvt)
inlineprotectedvirtual

Called after pEvt is fired.


The documentation for this class was generated from the following files: