Documentation Center

  • Trial Software
  • Product Updates

Conditional Execution Behavior

What Is Conditional Execution Behavior?

To speed the simulation of a model, by default the Simulink® software avoids unnecessary execution of blocks connected to Switch, Multiport Switch, and of conditionally executed blocks. This behavior is conditional execution (CE) behavior. You can disable this behavior for all Switch and Multiport Switch blocks in a model, or for specific conditional subsystems. See Disabling Conditional Execution Behavior.

The following model illustrates conditional execution behavior.

The Scope block shows the simulation result:

This model:

  • Has the Display > Signals & Ports > Execution Context Indicator menu option enabled.

  • The Pulse Generator block has the following parameter settings:

    • Pulse typeSample based

    • Period100

    • Pulse width50

    • Phase delay50

    • Sample Time0.01

  • The Gain block's sorted order (1:2) is second (2) in the enabled subsystem's execution context (1).

  • The Enabled Subsystem block has the Propagate execution context across subsystem boundary parameter enabled.

  • In the enabled subsystem, the Out1 block has the following parameter settings:

    • Initial output[]

    • Output when disabledheld

The outputs of the Constant block and Gain blocks are computed only while the enabled subsystem is enabled (for example, at time steps 0.5 to 1.0 and 1.0 to 1.5). This behavior is necessary 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, the outputs of the Constant and Gain blocks are computed at every time step, regardless of whether the outputs are needed or change.

In this example, the enabled subsystem is regarded as defining an execution context for the Constant and Gain blocks. Although the blocks reside graphically in the root system of the model, the Simulink software invokes the block methods during simulation as if the blocks reside in the enabled subsystem. This is indicated 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 root level (id = 0) of the model. The Gain block's sorted order (1:2) is second (2) in the enabled subsystem's execution context (1).

Propagating Execution Contexts

In general, the Simulink software defines an execution context as a set of blocks to be executed as a unit. At model compilation time, the Simulink software 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, each block in the model is examined to determine whether it meets the following conditions:

  • The block output is required only by a conditional subsystem or the block input changes only as a result of the execution of a conditionally executed subsystem.

  • The execution context of the subsystem can propagate across the subsystem boundaries.

  • The output of the block is not a test point (see Test Points).

  • The block is allowed to inherit its conditional execution context.

    The Simulink software does not allow some built-in blocks, such as 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.

  • The block is not a multirate block.

  • The block sample time is set to inherited (-1).

If a block meets these conditions and execution context propagation is enabled for the associated conditional subsystem (see Disabling Conditional Execution Behavior), the Simulink software moves the block into the execution context of the subsystem. This ensures that the block methods execute during the simulation loop only when the corresponding conditional subsystem executes.

    Note:   Execution contexts are not propagated to blocks having a constant sample time.

Behavior of Switch Blocks

This behavior treats the input branches of a Switch or Multiport Switch block as invisible, conditional subsystems, each of which has its own execution context. This CE is enabled only when the control input of the switch 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, in the Simulink Editor, select Display > Blocks > Sorted Execution Order. The sorted order index for each block in the model is displayed 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 sorted order in the execution context of the subsystem. For example, 0:0 indicates that the block is the first block in the execution context of the root subsystem.

If a bus is connected to a block input, the block sorted order is displayed as s:B. For example, 0:B indicates that the block belongs to the execution context of the root system and has a bus connected to its input.

The sorted order index of conditional subsystems is expanded 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 root system of the model. The first 1 indicates that the enabled subsystem is the second block on the sorted list of the root system (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 fact that the Constant block has an index of 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 Optimization Pane: General). To disable conditional execution behavior for a specific conditional subsystem, clear the Propagate execution context across subsystem boundary check box on the subsystem parameter dialog box.

Even if this option is enabled, the execution context of the subsystem cannot propagate across its boundaries under either of the following circumstances:

  • The subsystem is a triggered subsystem with a latched input port.

  • The subsystem has one or more output ports that specify an initial condition other than []. In this case, a block connected to the subsystem output cannot inherit the execution context of the subsystem.

Displaying Execution Context Bars

Simulink can optionally display bars next to the ports of subsystems across which execution contexts cannot propagate. To display the bars, select Display > Signals & Ports > Execution Context Indicator.

For example, it displays bars on subsystems from which no block can inherit its execution context. In the following figure, the context bars appear next to the In1 and Out1 ports of the Enabled Subsystem block.

Was this topic helpful?