Control Variant Condition Propagation using Variant Start and Variant End Blocks
This example explains how to use Variant Start and Variant End blocks to create bounded regions in a model where variant conditions apply only to a specific set of blocks. Variant conditions are logical expressions that, when applied to components, enable you to activate or deactivate them during simulation or code generation. This enables switching between different model configurations without altering the model structure. When you apply variant conditions to bounded regions, you restrict the activation and deactivation to a specific set of blocks by controlling the propagation of variant conditions beyond the bounded region. This approach enables you to collectively manage the activation and deactivation of blocks and isolates these changes to a specific set of blocks so that they do not impact other parts of the model.
For more information on variant conditions, see Variant Control Modes in Variant Blocks.
In this example, you will learn how to create bounded regions to:
Apply the same variant condition to a set of blocks to control active components collectively.
Confine the propagation of variant conditions to a set of blocks to prevent unintended changes on other model elements.
Explore the Model
Open the slexVariantStartAndEnd
model, which represents a control system. The control system contains first-order and second-order systems that are placed in the bounded regions defined by the Variant Start and Variant End blocks. These blocks determine whether to activate the first-order or the second-order system based on the variant condition that evaluates to true
during simulation. The blocks outside this region are unconditional, and remain unaffected by the variant conditions.
model = "slexVariantStartAndEnd";
open_system(model)
Apply Same Variant Conditions to Control Active Choice Within Bounded Region
You can apply the same variant conditions to a group of blocks by defining a bounded region with Variant Start and Variant End blocks. Use this approach to control the active state of a set of blocks collectively, not individually, to enable different configurations within a single layer. Alternatively, you can control the activation of blocks in a single layer using Variant Source and Variant Sink blocks. However, this approach is effective for models where only specific signals or paths need to change between variants. For scenarios that require a defined region with variant conditions, specifying a bounded region using Variant Start and Variant End blocks is recommended.
In the first model of this example, the variant conditions on the Variant Start
block are set to systemOrder == 1
and systemOrder == 2
for the output ports connected to the first-order and second-order systems, respectively. The variant control variable, systemOrder
, is defined and set to 2
in the PostLoadFcn
callback of the model. During simulation, the variant conditions systemOrder == 1
and systemOrder == 2
from the Variant Start
block propagate to the blocks within the bounded region, making them conditional.
sim(model);
During simulation, the variant conditions propagate within the bounded region as follows. To visualize the propagated variant conditions, use the Variant Conditions Legend. For more information, see Visualize Propagated Variant Conditions in Variant Conditions Legend.
The variant condition
systemOrder == 1
applies to the blocks connected to the first port of theVariant Start
block. The variant conditionsystemOrder == 2
applies to the blocks connected to the second port of theVariant Start
block.
When
systemOrder == 1
evaluates totrue
, the first-order system is active.
When
systemOrder == 2
evaluates totrue
, the second-order system is active.
The
Gain2
andGain3
blocks are unconditional because theVariant Start
andVariant End
blocks prevent the variant condition propagation beyond their boundaries.
Confine Variant Conditions Within Bounded Region to Prevent Unintended Changes
You can confine the propagation of variant conditions to a set of blocks to prevent unintended changes to other model components by defining a bounded region using Variant Start and Variant End blocks. Alternatively, you can limit the propagation of variant conditions by using Variant Subsystem blocks. However, with Variant Subsystem blocks, you place variant components inside the subsystem, introducing a hierarchy. In contrast, Variant Start and Variant End blocks limit propagation within a single layer.
In the second model of this example, the variant conditions on the Variant Start1
block are set to systemOrder == 1
and systemOrder == 2
for the output ports connected to the first-order and second-order systems, respectively. The Variant Source and Variant Sink blocks within the bounded region have variant conditions isFirstOrderSystemConnected == true
and isSecondOrderSystemConnected == true
, respectively. The variant control variables, systemOrder
, isFirstOrderSystemConnected
, and isSecondOrderSystemConnected
are defined and set to 2
, true
, and true
, respectively, in the PostLoadFcn
callback of the model.
During simulation, the variant conditions propagate within the bounded region as follows. To visualize the propagated variant conditions, use the Variant Conditions Legend. For more information, see Visualize Propagated Variant Conditions in Variant Conditions Legend.
When
isFirstOrderSystemConnected == true
&&systemOrder == 1
evaluates totrue
, the first-order system is active.
When
isSecondOrderSystemConnected == true
&&systemOrder == 2
evaluates totrue
, the second-order system is active.
The
Gain1
andGain4
blocks are unconditional because theVariant Start1
andVariant End1
blocks prevent the variant condition propagation beyond their boundaries.
See Also
Propagate Variant Conditions to Define Variant Regions with Variant Blocks
Related Topics
- Generate Code for Variant Start and Variant End Blocks (Simulink Coder)