Examples Using Discrete Event Subsystem Blocks

Example: Comparing the Lengths of Two Queues

In a model containing two queues, a logical comparison of the lengths of the queues changes when either queue has an arrival or departure. A queue block's #n output signal is updated after each arrival if the queue is nonempty, and after each departure. By contrast, the Relational Operator block is a time-based block. The model below performs the comparison inside a discrete event subsystem whose Discrete Event Inport blocks both have Type of signal-based event set to Sample time hit. This way, the comparison occurs whenever either #n signal is updated. If both queues update their #n values at the same time on the simulation clock, then the discrete event subsystem is called twice at that time.

Top-Level Model

Subsystem Contents

Example: Normalizing a Statistic to Use for Routing

Suppose you want to make a routing decision based on an output signal from a SimEvents® block, but you must manipulate or normalize the statistic so that the routing block receives a value in the correct range. In the model shown below, the util output signal from the Single Server block assumes real values between 0 and 1, while the Output Switch block expects values of 1 or 2 in the attribute of each arriving entity. The discrete event subsystem adds 1 to the rounded utilization value.

The Discrete Event Inport block inside the subsystem has Type of signal-based event set to Sample time hit so that the computation occurs whenever the server block updates the value of the utilization signal. The subsystem's configuration and the presence of the Delay of Length Zero block ensure that the attribute assignment and the routing decision always use the most up-to-date value of the util signal.

At a time instant at which an entity departs from the Single Server block, the sequence of operations is as follows:

  1. The entity advances from the Single Server block to the block labeled Delay of Length Zero, which is merely a single server whose service time is zero.

  2. The Single Server block updates its util output signal.

  3. The subsystem reacts to the updated value of util by computing an updated value at the A1 input port of the Set Attribute block.

  4. The entity advances from the Delay of Length Zero block to the Set Attribute block, which uses an up-to-date signal to determine the attribute value.

    Ensuring that the A1 signal is up-to-date before the entity arrives is the reason for the Delay of Length Zero block. See Interleaving of Block Operations for details.

  5. The entity advances from the Set Attribute block to the Output Switch block, which uses the entity's attribute value to determine the routing behavior.

Top-Level Model

Subsystem Contents

Example: Using Event-Based Timing for a Statistical Computation

This example performs the computation described in Timing for a Statistical Computation, to find the total length of time during the simulation that a queue's length equals or exceeds a threshold value.

In the model shown below, the threshold is three entities. The Compare To Constant block produces an indicator function for that threshold. The Discrete Time Integrator block, configured to have an inherited sample time rather than an explicit discrete sample time, computes the area under the curve of the indicator function, that is, the total amount of time that the input to the subsystem exceeds the threshold. The Discrete Event Inport block inside the subsystem has Type of signal-based event set to Sample time hit so that the computation occurs whenever the queue block updates the value of the queue length signal. The subsystem's configuration ensures that the comparison block does not miss any asynchronous discontinuities in the queue length signal and that the integrator processes the correct indicator function.

Top-Level Model

Subsystem Contents

Example: Ending the Simulation Upon an Event

This example ends the simulation as described in Timing for the End of the Simulation, precisely when the number of entities in a queue first equals or exceeds a threshold. In the model shown below, the Compare To Constant and Stop Simulation blocks are in a discrete event subsystem. The Discrete Event Inport block inside the subsystem has Type of signal-based event set to Sample time hit so that the subsystem executes at exactly those times when the FIFO Queue block updates the value of the queue length signal.

Top-Level Model

Subsystem Contents

Example: Sending Unrepeated Data to the MATLAB® Workspace

Suppose you want to log statistics to the MATLAB® workspace, but you want to save simulation time and memory by capturing only values that are relevant to you. You might want to suppress repeated values, for example, or capture only values that represent an increase from the previous value.

In the model shown below, the discrete event subsystem contains a To Workspace block whose Save format parameter is set to Structure With Time. The Discrete Event Inport block inside the subsystem has Type of signal-based event set to Change in signal and Type of change in signal value set to Either, so that the MATLAB workspace variable tells you when the Output Switch block selects an entity output port that differs from the previously selected one. If, for example, the switch is configured to select the first port that is not blocked, then a change in the port selection indicates a change in the state of the simulation (that is, a previously blocked port has become unblocked, or a port becomes blocked that previously was not).

Top-Level Model

Subsystem Contents

Example: Focusing on Events, Not Values

The example below counts the number of times a signal changes its value, ignoring times at which the signal might be updated with the same value. The discrete event subsystem contains a Counter Free-Running block with an inherited sample time. Because the Counter Free-Running counts starting from 0, the subsystem also adds 1 to the counter output.

The Discrete Event Inport block inside the subsystem has Type of signal-based event set to Change in signal and Type of change in signal value set to Either, so that the subsystem is executed each time the input signal changes its value. In contrast to other subsystem examples, this subsystem does not use the signal's specific values for computations; the input signal is connected to a Terminator block inside the subsystem. The counter's value is what the subsystem sends to the MATLAB workspace.

In this example, avoiding extraneous calls to the subsystem is not merely a time-saver or memory-saver, but rather a strategy for producing the correct result.

Top-Level Model

Subsystem Contents

Example: Detecting Changes from Empty to Nonempty

The example below executes a subsystem only when an N-server changes from empty to nonempty, or vice versa, but not when the number of entities in the server remains constant or changes between two nonzero values. In the model, the N-Server block produces a #n signal that indicates the number of entities in the server. The server is empty if and only if the #n signal is zero. Connected to the #n signal is a Discrete Event Inport block inside the subsystem that has Type of signal-based event set to Trigger and Trigger type set to Either, so that the subsystem is executed each time the #n signal changes from zero to one or from one to zero. Connected to the w signal is another Discrete Event Inport block inside the subsystem; this block has Execute subsystem upon signal-based events cleared so that this signal does not cause additional calls to the subsystem; the subsystem merely uses the most recent value of the w signal whenever the #n signal exhibits a trigger edge.

If the server changes instantaneously from empty to nonempty and back to empty, then the subsystem is called exactly twice in the same time instant, once for the rising edge and once for the subsequent falling edge. The Triggered Subsystem block might not detect the edges that the zero-duration value of 1 creates, and thus might not call the subsystem at that time instant. This is why the Discrete Event Subsystem block is more appropriate for this application.

Example: Logging Data About the First Entity on a Path

Suppose your model includes a particular entity path that entities rarely use, and you want to record certain attributes of the first entity that takes that path during the simulation. You can send the attribute values to the MATLAB workspace by using a To Workspace block inside a discrete event subsystem.

In the example below, the #d output signal from the Get Attribute block indicates how many entities have departed from the block. The other outputs from that block are the attribute values that you want to send to the workspace. Connected to the #d signal is a Discrete Event Inport block inside the subsystem that has Type of signal-based event set to Trigger, so that the subsystem is executed each time the #d signal changes from zero to one. Connected to the A1 and A2 signals are additional Discrete Event Inport blocks inside the subsystem. These blocks have Execute subsystem upon signal-based events cleared so that the attribute signals do not cause additional calls to the subsystem; the subsystem merely uses the most recent value of the A1 and A2 signals whenever the #d signal exhibits a trigger edge.

The To Workspace block inside the subsystem does not actually create the variables in the workspace until the simulation ends, but the variable contents are correct because the timing of the subsystem corresponds to the time of the #d signal's first positive value.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS