For stand-alone execution of simulation models, adevs provides the devssim class. This class is a wrapper around the basic Parallel DEVS simulation objects (see the pdevs.h header) and provides some simple options for driving your simulation. The most direct means of starting a simulation is to create a devssim object, provided with a model via the constructor, and then call its run method.
int main() { myModel model; devssim sim(&model); sim.run(); return 0; }
This code snippet will simulate the model until its time advance is INFINITY. That is, until the model is passive. Optionally, and end time could be passed to the run method which would cause the simulation to halt when the time of next event is larger than the requested termination time.
The devvsim class can be sub-classed to provide specialized behavior. The class has two virtual member functions that can be overridden. The halt() method is called at the end of every simulation cycle. It returns true if the simulation has reached a stopping condition and false otherwise. When the halt() method returns true, the simulation stops and the run() method will return. The other virtual method is the reset() member function. The default implementation causes each models init() function to be called with the time t0. Simulation classes derived from devssim should always call the base devssim reset() method as part of any override of the reset() method. An example of a specialized simulation driver is shown below.
class sim: public devssim { public: sim(myModel* model, int count): devssim(model), model_(model), count_(count), cycles_(0) {} // Overrides the halt method from devssim. bool halt() { return (model_->stopSimulation() || cycles_++ > count_; } // Resets the simulator. void reset(stime_t t0 = 0) { devssim::reset(t0); cycles_ = 0; } ~sim(){} private: myModel* model_; int count_; int cycles_; };
This sim class will run until the model indicates that it has reached a stopping point or the number of simulation cycles exceeds some count. The next snippet shows how this new simulation class might be used. The only significant change from the use of the devssim class is in the constructor call.
int main() { myModel model; sim sim_(&model,1000); sim_.run(); return 0; }