Products & Services Solutions Academia Support User Community Company

Learn more about SimEvents   

Example: Choices of Values for Event Priorities

Overview of Example

This example (open model) shows how you can vary the processing sequence for simultaneous events by varying their event priorities. The example creates race conditions at a switch and illustrates multiple ways to resolve the race conditions.

At T=1, 2, 3,... the Repeating Sequence Stair block changes its value from 1 to 2 or from 2 to 1. The change causes the following events to occur, not necessarily in this sequence:

Both entity generators are configured so that if a generated entity cannot depart immediately, the generator holds the entity and temporarily suspends the generation of additional entities.

In the model, the two Set Attribute blocks assign a Source attribute to each entity. The attribute value is 1 or 2 depending on which entity generator generated the entity. The Attribute Scope block plots the Source attribute values to indicate the source of each entity that departs from the switch.

Arbitrary Resolution of Signal Updates

If the two entity generators and the switch all have the Resolve simultaneous signal updates according to event priority option turned off, then you cannot necessarily predict the sequence in which the blocks schedule their reactions to changes in the output signal from the Repeating Sequence Stair block.

The rest of this example assumes that the two entity generators and the switch all use the Resolve simultaneous signal updates according to event priority option, for greater control over the sequencing of simultaneous events.

Selecting a Port First

Suppose the two entity generators and the switch have the explicit event priorities shown.

Event TypeEvent Priority
Generation event at top entity generator300
Generation event at bottom entity generator310
Port selection event at switch200

The following describes what happens at T=1, 2, 3 using messages from the simulation log of the SimEvents debugger. To learn more about the debugger, see Overview of the SimEvents Debugger.

Behavior at T=1

Behavior at T=2

Behavior at T=3

Evidence from Plots and Signals

The plot of entities' Source attribute values shows an alternating pattern of dots, as does the plot of the port selection signal p. The list of times and values of the entities' Source attribute, as recorded in the Source_attr variable in the MATLAB workspace, shows that two entities from the same entity generator reach the scope at T=2, 3, 4, etc.

Port Selection Signal

Switch Departures When Port Selection Is Processed First

[Source_attr.time, Source_attr.signals.values]

ans =

     1     2
     2     1
     2     1
     3     2
     3     2
     4     1
     4     1
     5     2
     5     2
     6     1
     6     1
     7     2
     7     2
     8     1
     8     1
     9     2
     9     2
    10     1
    10     1

Generating Entities First

Suppose the two entity generators and the switch have the explicit event priorities shown below.

Event TypeEvent Priority
Generation event at top entity generator300
Generation event at bottom entity generator310
Port selection event at switch4000

At the beginning of the simulation, the port selection signal, p, is 1.

Behavior at T=1

Behavior at T=2

Plots and Signals

The plot of entities' Source attribute values shows that two entities from different entity generators depart from the switch every second.

Switch Departures When Entity Generations Are Processed First

[Source_attr.time, Source_attr.signals.values]

ans =

     1     1
     1     2
     2     2
     2     1
     3     1
     3     2
     4     2
     4     1
     5     1
     5     2
     6     2
     6     1
     7     1
     7     2
     8     2
     8     1
     9     1
     9     2
    10     2
    10     1

Randomly Selecting a Sequence

Suppose the two entity generators and the switch have equal event priorities. By default, the application uses an arbitrary processing sequence for the entity-generation events and the port-selection events, which might or might not be appropriate in an application. To avoid bias by randomly determining the processing sequence for these events, set Execution order to Randomized in the model's Configuration Parameters dialog box.

Sample attribute values and the corresponding plot are below, but your results might vary depending on the specific random numbers.

Switch Departures When Processing Sequence is Random

[Source_attr.time, Source_attr.signals.values]

ans =

     1     2
     2     2
     2     1
     3     2
     4     1
     4     1
     5     1
     5     2
     5     2
     6     1
     7     1
     7     2
     8     2
     8     1
     9     2
    10     1
    10     1
  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

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