Documentation Center

  • Trial Software
  • Product Updates

Regulate Simulation Length


When you gather statistics from a simulation, ending the simulation at the right time is more important than if you are only observing behavior qualitatively. Typical criteria for ending a discrete-event simulation include the following:

  • A fixed amount of time passes

  • The simulation processes a fixed number of packets, parts, customers, or other items that entities represent

  • The simulation achieves a particular state, such as an overflow or a machine failure

Set a Fixed Simulation Stop Time

To run a simulation interactively with a fixed stop time, do the following:

  1. Open the configuration parameters dialog box by choosing Simulation > Model Configuration Parameters in the menu of the model window.

  2. In the dialog box, set Stop time to the desired stop time.

  3. Run the simulation by choosing Simulation > Run.

To fix the stop time when running a simulation programmatically, use syntax like


where model is the name of the model and timespan is the desired stop time.

Stop Simulation Upon a Specified Entity Count

By counting entities, you can stop the simulation when the simulation processes a fixed number of entities. The basic procedure for stopping a simulation based on the total number of entity departures from a block is:

  1. Find the parameter of the block that enables the departure counter as a signal output. Most blocks call the parameter Number of entities departed. Exceptions are in the following table.

    Entity Departure CounterWrite count to signal port #d
    Entity SinkNumber of entities arrived

  2. Set the check box. This setting causes the block to have a signal output port corresponding to the entity count.

  3. Connect the new signal output port to an Atomic Subsystem block.

  4. Double-click the subsystem block to open the subsystem it represents.

  5. Delete the Outport block labeled Out.

  6. Connect the Inport block labeled In to a Compare To Constant block.

  7. In the Compare To Constant block,

    • Set Operator to >=.

    • Set Constant value to the desired number of entity departures.

    • Set Output data type mode to boolean.

  8. Connect the Compare To Constant block to a Stop Simulation block. The result should look like the following, except that your SimEvents® block might be a block other than Entity Departure Counter.

    Top-Level Model

    Subsystem Contents

See the considerations discussed in Tips for Using State-Based Stopping Conditions below. They are relevant if you are stopping the simulation based on an entity count, where "desired state" means the entity-count threshold.

Stop Simulation Upon Reaching a Particular State

Suppose you want the simulation to end when it achieves a particular state, such as an overflow or a machine failure. The state might be the only criterion for ending the simulation, or the state might be one of multiple criteria, each of which is sufficient reason to end the simulation. An example that uses multiple criteria is a military simulation that ends when all identified targets are destroyed or all resources (ammunition, aircraft, etc.) are depleted, whichever occurs first.

Once you have identified a state that is relevant for ending the simulation, you typically create a Boolean signal that queries the state and connect the signal to a Stop Simulation block. Typical ways to create a Boolean signal that queries a state include the following:

  • Connect a signal to a logic block to determine whether the signal satisfies some condition. See the blocks in the Simulink® Logic and Bit Operations library. The following figure below illustrates one possibility.

    Top-Level Model

    Subsystem Contents

  • Use a Get Attribute block to query an attribute and a logic block to determine whether the attribute value satisfies some condition. The next figure illustrates one possibility.

    Top-Level Model

    Subsystem Contents

  • To end the simulation whenever an entity reaches a particular entity path, you can end that path with an Entity Sink block, enable that block's output signal to count entities, and check whether the output signal is greater than zero.

    Top-Level Model

    Subsystem Contents

  • Logically combine multiple tests using logic blocks to build the final Boolean signal that connects to a Stop Simulation block. (A logical OR operation is implied if your model contains an independent Stop Simulation block for each of the multiple tests, meaning that the simulation ends when the first such block processes an input signal whose value is true.) The figure below illustrates one possibility using the exclusive-OR of two tests, one of which is in turn the logical AND of two tests.

    Top-Level Model

    Subsystem Contents

Tips for Using State-Based Stopping Conditions

When using a state rather than a time to determine when the simulation ends, keep in mind the following considerations:

  • If the model has a finite stop time, then the simulation might end before reaching the desired state. Depending on your needs, this might be a desirable or undesirable outcome. If it is important that the simulation not stop too early, then you can follow the instructions in Set a Fixed Simulation Stop Time and use Inf as the Stop time parameter.

  • If you set the Stop time parameter to Inf, then you should ensure that the simulation actually stops. For example, if you want to stop based on an entity count but the simulation either reaches a deadlock or sends most entities on a path not involving the block whose departure count is the stopping criterion, then the simulation might not end.

  • Checking for the desired state throughout the simulation might make the simulation run more slowly than if you used a fixed stop time.

Was this topic helpful?