| Using Simulink | ![]() |
Conditional Execution Behavior
To speed simulation of a model, Simulink by default avoids unnecessary execution of blocks connected to Switch, Multiport Switch, and conditionally executed blocks, a behavior called conditional execution (CE) behavior. You can disable this behavior for all Switch and Multiport Switch blocks in a model or for specific conditionally executed subsystems (see Disabling Conditional Execution Behavior).
The following model illustrates conditional execution behavior.
Simulink computes the outputs of the Constant block and Gain block only while the enabled subsystem is enabled (i.e., at time steps 0.5 to 1.0, 1.5 to 2.0, and so on). This is because the output of the Constant block is required and the input of the Gain block changes only while the enabled subsystem is enabled. When CE behavior is off, Simulink computes the outputs of the Constant and Gain blocks at every time step, regardless of whether the outputs are needed or change.
In this example, Simulink regards the enabled subsystem as defining an execution context for the Constant and Gain blocks. Although the blocks reside graphically in the model's root system, Simulink invokes the blocks' methods during simulation as if the blocks reside in the enabled subsystem. Simulink indicates this in the sorted order labels displayed on the diagram for the Constant and Gain blocks. The notations list the subsystem's (id = 1) as the execution context for the blocks even though the blocks exist graphically at the model's root level (id = 0).
Propagating Execution Contexts
In general, Simulink defines an execution context as a set of blocks to be executed as a unit. At model compilation time, Simulink associates an execution context with the model's root system and with each of its nonvirtual subsystems. Initially, the execution context of the root system and each nonvirtual subsystem is simply the blocks that it contains.
When compiling a model, Simulink examines each block in the model to determine whether it meets the following conditions:
Simulink does not allow some built-in blocks, e.g., the Delay block, ever to inherit their execution context. Also, S-Function blocks can inherit their execution context only if they specify the SS_OPTION_CAN_BE_CALLED_CONDITIONALLY option.
If a block meets these conditions and execution context propagation is enabled for the associated conditionally executed subsystem (see Disabling Conditional Execution Behavior), Simulink moves the block into the execution context of the subsystem. This ensures that the block's methods are executed during the simulation loop only when the corresponding conditionally executed subsystem executes.
Behavior for Switch Blocks
This behavior treats the input branches of a Switch or Multiport Switch block as invisible, conditionally executed subsystems, each of which has its own execution context that is enabled only when the switch's control input selects the corresponding data input. As a result, switch branches execute only when selected by switch control inputs.
Displaying Execution Contexts
To determine the execution context to which a block belongs, select Sorted order from the model window's Format menu. Simulink displays the sorted order index for each block in the model in the upper right corner of the block. The index has the format s:b, where s specifies the subsystem to whose execution context the block belongs and b is an index that indicates the block's sorted order in the subsystem's execution context, e.g., 0:0 indicates that the block is the first block in the root subsystem's execution context.
If a bus is connected to the block's input (see Bus-Capable Blocks), Simulink displays the block's sorted order as s:B, e.g., 0:B indicates that the block belongs to the root system's execution context and has a bus connected to its input.
Simulink expands the sorted order index of conditionally executed subsystems to include the system ID of the subsystem itself in curly brackets as illustrated in the following figure.
In this example, the sorted order index of the enabled subsystem is 0:1{1}. The 0 indicates that the enabled subsystem resides in the model's root system. The first 1 indicates that the enabled subsystem is the second block on the root system's sorted list (zero-based indexing). The 1 in curly brackets indicates that the system index of the enabled subsystem itself is 1. Thus any block whose system index is 1 belongs to the execution context of the enabled subsystem and hence executes when it does. For example, the Constant block's index, 1:0, indicates that it is the first block on the sorted list of the enabled subsystem, even though it resides in the root system.
Disabling Conditional Execution Behavior
To disable conditional execution behavior for all Switch and Multiport Switch blocks in a model, turn off the Conditional input branch execution optimization on the Optimization pane of the Configuration Parameters dialog box (see The Optimization Pane). To disable conditional execution behavor for a specific conditionally executed subsystem, uncheck the Propagate execution context across subsystem boundary option on the subsystem's parameter dialog box.
Even if this option is enabled, a subsystem's execution context cannot propagate across its boundaries under either of the following circumstances:
[]. In this case, a block connected to the subsystem's output cannot inherit the subsystem's execution context.
Displaying Execution Context Bars
Simulink can optionally display bars next to the ports of subsystems across which execution contexts cannot propagate, i.e., on subsystems from which no block can inherit its execution context.
To display the bars, select Execution context indicator from the model editor's Format -> Block Displays menu.
| Triggered and Enabled Subsystems | Referencing Models | ![]() |
© 1994-2005 The MathWorks, Inc.