Documentation Center

  • Trials
  • Product Updates

Build a Discrete-Event Model

Overview

This section describes how to build a new model representing a discrete-event system. The system is a simple queuing system in which "customers" — entities — arrive at a fixed deterministic rate, wait in a queue, and advance to a server that operates at a fixed deterministic rate. This type of system is known as a D/D/1 queuing system in queuing notation. The notation indicates a deterministic arrival rate, a deterministic service rate, and a single server.

Using the example system, this section shows you how to perform basic model-building tasks, such as:

  • Adding blocks to models

  • Configuring blocks using their parameter dialog boxes

The next section, Explore Simulations Using the Debugger and Plots, uses the same D/D/1 system to illustrate techniques more specific to discrete-event simulations, such as:

  • Using the SimEvents® debugger to examine the state of a server

  • Using plots to understand simulation behavior, including plots that show multiple values at a fixed time

To skip the model-building steps and open a completed version of the example model, enter simeventsdocex('doc_dd1')simeventsdocex('doc_dd1') in the MATLAB® Command Window. Save the model in your working folder as dd1.

    Note:   When you later create your own models, use the conversion blocks from the Gateways library (gateway blocks) to convert signals and function-calls from signal-based to event-based and vice versa.

Open a Model and Libraries

The first steps in building a model are to set up your environment, open a new model window, and open the libraries containing blocks.

Open a New Model Window

On the Home tab, select New > Simulink Model . An empty model window opens.

To name the model and save it as a file, select File > Save from the model window's menu. Save the model in your working folder under the file name dd1.

Open SimEvents Libraries

In the MATLAB Command Window, enter

simeventslib

The main SimEvents library window appears. This window contains an icon for each SimEvents library. To open a library and view the blocks it contains, double-click the icon that represents that library.

Main SimEvents library window

Open Simulink Libraries

In the MATLAB Command Window, enter

simulink

The Simulink® Library Browser opens, using a tree structure to display the available libraries and blocks. To view the blocks in a library listed in the left pane, select the library name, and the list of blocks appears in the right pane. The Library Browser provides access not only to Simulink blocks but also to SimEvents blocks. For details about the Library Browser, see Simulink Library Browser in the Simulink documentation.

Move Blocks into the Model Window

To move blocks from libraries into the model window, follow these steps:

  1. In the main SimEvents library window, double-click the Generators icon to open the Generators library. Then double-click the Entity Generators icon to open the Entity Generators sublibrary.

    Entity Generators sublibrary window

  2. Drag the Time-Based Entity Generator block from the library into the model window.

    This might cause an informational dialog box to open, with a brief description of the difference between entities and events.

    Time-Based Entity Generator block in the model window

  3. In the main SimEvents library window, double-click the Queues icon to open the Queues library.

    Queues library window

  4. Drag the FIFO Queue block from the library into the model window.

  5. In the main SimEvents library window, double-click the Servers icon to open the Servers library.

    Servers library window

  6. Drag the Single Server block from the library into the model window.

  7. In the main SimEvents library window, double-click the SimEvents Sinks icon to open the SimEvents Sinks library.

    SimEvents Sinks library window

  8. Drag the Signal Scope block and the Entity Sink block from the library into the model window.

As a result, the model window looks like the following figure. The model window contains blocks that represent the key processes in the simulation: blocks that generate entities, store entities in a queue, serve entities, and create a plot showing relevant data.

Model window containing all the blocks for the example

Configure Blocks

Configuring the blocks in dd1 means setting their parameters appropriately to represent the system being modeled. Each block has a dialog box that enables you to specify parameters for the block. Default parameter values might or might not be appropriate, depending on what you are modeling.

View Parameter Values

Two important parameters in this D/D/1 queuing system are the arrival rate and service rate. The reciprocals of these rates are the duration between successive entities and the duration of service for each entity. To examine these durations, do the following:

  1. Double-click the Time-Based Entity Generator block to open its dialog box. Observe that the Distribution parameter is set to Constant and that the Period parameter is set to 1. This means that the block generates a new entity every second.

  2. Double-click the Single Server block to open its dialog box. Observe that the Service time parameter is set to 1. This means that the server spends one second processing each entity that arrives at the block.

  3. Click Cancel in both dialog boxes to dismiss them without changing any parameters.

The Period and Service time parameters have the same value, which means that the server completes an entity's service at exactly the same time that a new entity is being created.

Change Parameter Values

Configure blocks to create a plot that shows when each entity departs from the server, and to make the queue have an infinite capacity. Do this as follows:

  1. Double-click the Single Server block to open its dialog box.

  2. Click the Statistics tab to view parameters related to the statistical reporting of the block.

  3. Select the Number of entities departed check box.

    Then click OK. The Single Server block acquires a signal output port labeled #d. During the simulation, the block will produce an output signal at this #d port; the signal's value is the running count of entities that have completed their service and departed from the server.

  4. Double-click the FIFO Queue block to open its dialog box.

  5. Set the Capacity parameter to Inf and click OK.

Connect Blocks

Now that the model window for dd1 contains blocks that represent the key processes, connect the blocks as shown in the following graphic.

To connect blocks, do one of the following:

  • With the mouse, drag from the output port of the source block to the input port of the destination block.

  • Select the source block. Ctrl+click the destination block.

In both cases, SimEvents connects the source block to the destination block. If necessary, the software also routes the connecting line around intervening blocks or lines.

    Note:   The following section about inserting blocks is general information that does not apply to the dd1 model. When you have connected the blocks as shown in the preceding graphic, the dd1 model is ready for simulation as described in Run the Simulation.

Run the Simulation

Save the dd1 model you have created. Then start the simulation by choosing Simulation > Run from the model window's menu.

Resolve Solver Warnings

When you first simulate the model in this example, you will see warning messages in the MATLAB Command Window about continuous states and the maximum step size. These messages appear because certain default parameters for a Simulink model are inappropriate for this particular example model, which is completely event-based and contains no blocks with continuous states. The application overrides the inappropriate parameters and alerts you to that fact.

If you want to prevent these warnings when you simulate event-based models in the future, you need to set the solver type and step size parameters to appropriate values. To do this, with your model open in the model editor, open Simulation > Configuration Parameters > Solver. Under the Solver options section, for the Type: parameter select the Variable-step option in the drop-down list. For the Solver: parameter, select Discrete in the drop-down list and in the field for the Max step size: parameter, type inf. Click OK and save your model.

Results of the Simulation

When the simulation runs, the Signal Scope block opens a window containing a plot. The horizontal axis represents the times at which entities depart from the server, while the vertical axis represents the total number of entities that have departed from the server.

After an entity departs from the Single Server block, the block updates its output signal at the #d port. The updated values are reflected in the plot and highlighted with plotting markers. From the plot, you can make these observations:

  • Until T=1, no entities depart from the server. This is because it takes one second for the server to process the first entity.

  • Starting at T=1, the plot is a stairstep plot. The stairs have height 1 because the server processes one entity at a time, so entities depart one at a time. The stairs have width equal to the constant service time, which is one second.

Insert Blocks

You can insert a block in an existing line, if the block that you want to insert:

  • Has only one input and one output port.

  • Has connection port types (i.e. entity ports or signal ports) that correspond with the data on the existing line. For example, in an existing entity line, you can insert only a block that accepts and outputs entities. You cannot insert a block that accepts and outputs event-based signals.

To insert a block in a line:

  1. Drag the block over the line.

  2. Release the mouse button. SimEvents inserts the block in the line.

Build a Model Using Model Construction Commands

This example shows how to use model construction commands to add blocks to a model and connect them.

Suppose you want to add a Time-Based Entity Generator block and a FIFO Queue block to a model and connect them:

This procedure shows you how to add and position the two blocks in a model, for example, MyModel.

Add the Time-Based Entity Generator block and position it.

add_block(['simeventslib/Sources/', ...
'Time-Based Entity Generator'], 'MyModel/Time-Based Entity Generator'); set_param('MyModel/Time-Based Entity Generator','position',[65 63 150 117]);

The position parameter specifies the top left (x,y) and lower right (x+block width, y+block height) corners of the block.

Add the FIFO Queue block and position it.

add_block('simeventslib/Queueing/FIFO Queue','MyModel/FIFO Queue');
set_param('MyModel/FIFO Queue','position',[195 63 280 117]);

Connect the blocks.

add_line('MyModel','Time-Based Entity Generator/1','FIFO Queue/1','autorouting','on');

Port indices, such as Time-Based Entity Generator/1, correspond to the top-down order of connection ports when you look at the block in the Simulink Editor. The autorouting feature routes lines around any intervening blocks or other lines, as needed.

    Note:   If you want to connect blocks that are inside a subsystem, use the full path to the subsystem as the first argument of the add_line function:

    add_line('MyModel/MySubsystem', 'Time-Based Entity Generator/1',... )

Was this topic helpful?