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

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

#include <algorithm.h>

Inheritance diagram for Algorithm:
Inheritance graph
[legend]

Public Member Functions

 Algorithm (State &state, GslRandomNumberGenerator &rng)
 Constructor of the class, to which the simulation state must be specified as well as the random number generator to be used internally.
 
bool_t evolve (double &tMax, int64_t &maxEvents, double startTime=0, bool initEvents=true)
 This advances the simulation state specified in the constructor using the core mNRM. More...
 
double getTime () const
 This function returns the current time of the simulation.
 
GslRandomNumberGeneratorgetRandomNumberGenerator () const
 Returns the random number generator that was specified in the constructor.
 

Protected Member Functions

StategetState () const
 Returns the simulation state instance that was specified in the constructor.
 
virtual bool_t 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).
 
virtual bool_t getNextScheduledEvent (double &dt, EventBase **ppEvt)
 Store the next event to be fired in ppEvt and store the real world time that will have passed until it fires in dt.
 
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.
 
virtual void onAboutToFire (EventBase *pEvt)
 Called right before pEvt is fired.
 
virtual void onFiredEvent (EventBase *pEvt)
 Called after pEvt is fired.
 
virtual void onAlgorithmLoop (bool finished)
 Called at the end of each algorithm loop, with finished set to true if the loop will be exited.
 

Detailed Description

This class 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 SimpleAlgorithm class, where no attempt is made to avoid unnecessary recalculations.

Member Function Documentation

◆ evolve()

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

This advances the simulation state specified in the constructor 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 Algorithm::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_pState, 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;
virtual void onAboutToFire(EventBase *pEvt)
Called right before pEvt is fired.
Definition: algorithm.h:176
virtual void advanceEventTimes(EventBase *pScheduledEvent, double dt)
Advance the times of the necessary events to the time when dt has passed, ignoring pScheduledEvent si...
Definition: algorithm.cpp:181
virtual void onAlgorithmLoop(bool finished)
Called at the end of each algorithm loop, with finished set to true if the loop will be exited.
Definition: algorithm.h:183
virtual bool_t initEventTimes() const
Generate the internal times for the events present in the algorithm (called by State::evolve dependin...
Definition: algorithm.cpp:171
virtual void onFiredEvent(EventBase *pEvt)
Called after pEvt is fired.
Definition: algorithm.h:179
virtual bool_t getNextScheduledEvent(double &dt, EventBase **ppEvt)
Store the next event to be fired in ppEvt and store the real world time that will have passed until i...
Definition: algorithm.cpp:176
This is the base class for events in the mNRM algorithm.
Definition: eventbase.h:56
virtual void fire(Algorithm *pAlgorithm, State *pState, double t)
This function will be called when the event fires, so this should most likely be re-implemented in yo...
Definition: eventbase.cpp:57
bool willBeRemoved() const
Check if the event has been marked for deletion, can avoid a call to the random number generator to c...
Definition: eventbase.h:102

Apart from the core functions Algorithm::initEventTimes, Algorithm::getNextScheduledEvent and Algorithm::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

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