next up previous contents
Next: Methods the application provides Up: The TimeWarp class Previous: The TimeWarp class

Defining the state of an object

The state of a simulation object is the data that is required at any simulation time to determine the object's actions at the next simulation time, given an event to process. Normally, the only data that needs to be in an object's state is data that can change each simulation cycle.

For example, in our ping pong application, the number of times the object has received a ball and the number of times the object has hit the ball will vary between simulation cycles. The total number of times that the object is supposed to hit the ball is constant throughout the simulation. Although this data could be put into the state, it is not necessary to do so, and will make the simulation less efficient. (Due to the way the TimeWarp algorithm works, an object's state will be copied a significant number of times throughout the simulation.) A better choice would be to make the maximum number of hits for an object part of its class data, and the number of balls sent and received part of the definition of state. The following code fragment shows how state is defined for the PingObject class:

#include "BasicState.hh"

class PingState : public BasicState {
public:
  int numSent;
  int numReceived;

  PingState(){};
  ~PingState(){};

  void initialize(){
    numSent = 0;
    numReceived = 0;
  }
};

Notice that the default constructor in the example is empty. It was previously noted that the TimeWarp algorithm calls for the state to be copied a significant number of times. Likewise, a significant number of states are ``newed'' and ``deleted'', so to reduce the overhead of this memory allocation, the default constructors and destructors should be left empty. The method initialize will be called explicitly on the first state the object instantiates, and after that, the data from an old state will immediately be copied over the uninitialized memory of the new state. It should be noted that if there are any pointers defined in an object's state, proper copy constructors must be written to ensure the copying is done correctly.


next up previous contents
Next: Methods the application provides Up: The TimeWarp class Previous: The TimeWarp class
Philip A. Wilsey
1/26/1998