Contenuto principale

Define, Activate, and Manage Design Alternatives in Simulink Model

You can use variant mechanisms in variant blocks and variant parameters to define, activate, and manage design alternatives. These mechanisms include variant choice, variant control variables, variant activation time, variant condition propagation, and so on. By using these mechanisms, Simulink® models can automatically select and apply the appropriate configuration for any given scenario. This approach enables the models to adapt to changing requirements and streamlines workflows, eliminating the need for manual intervention.

Define Design Alternatives Using Variant Choices

A variant choice is one of the design alternatives or implementations within a variant block or variant parameter in Simulink that can be selected without altering the overall model structure.

In variant blocks, variant choices provide structural variation, with each choice representing a different design alternative. For variant parameters, variant choices enable variation in parameter value, with each choice specifying a different parameter value within a block. You can activate or deactivate each variant choice by using variant controls.

To define variant choices in variant blocks, see Working with Variant Choices. To define variant choices in variant parameters, see Create a Simple Variant Parameter Model.

Switch Between Variant Choices Using Variant Controls

Each variant choice in a variant block or variant parameter is associated with a variant control that determines which variant choice is active. You use the variant control to activate or deactivate a specific variant choice. While each variant choice is associated with a variant control, only one variant choice within a variant block or a variant parameter can be active at any given time. During simulation, Simulink uses only the active variant choice and ignores inactive ones.

For variant blocks, use the Variant control mode parameter to specify how Simulink determines the active variant choice. In expression mode, use Boolean expressions to activate a variant choice. In label mode, use user-defined labels to activate a variant choice. In sim codegen switching mode, use the sim and codegen keywords to activate the variant choice when performing simulation and code generation workflows, respectively.

For variant parameters, only the expression type of variant control mode is supported, so you use Boolean expressions to activate variant choices.

Variant control modeDescription
expression

Simulink uses Boolean expressions to determine the active variant choice. The value of the variant control variable specifies which variant becomes active. Simulink activates the selected variant at the simulation or code generation phase defined by Variant activation time. Use expression for applications such as controllers or situations where you need to retain multiple variants in the generated code.

label

Simulink uses descriptive, predefined strings, called labels, that you assign during model creation or editing to select the active variant choice. Use label for applications with a limited set of design alternatives that require predictable and easily maintained variations, such as in plant modeling.

sim codegen switching

Simulink automates variant selection between two variant choices based on whether you are running a simulation or generating code task. To control which variant is active, assign the keyword (sim) to the variant choice intended for simulation runs, and (codegen) to the variant choice intended for code generation tasks. When you simulate a model in normal, accelerator, or rapid accelerator mode, Simulink automatically selects the (sim) branch as the active variant choice. For software-in-the-loop (SIL), processor-in-the-loop (PIL) simulations, or when using external mode, Simulink automatically selects the (codegen) branch.

For more information on variant control modes, see Variant Control Modes in Variant Blocks and Variant Control Mode in Variant Parameters.

Select Active Choice Using Variant Control Variables

A variant control variable is a workspace variable used in Boolean variant control expressions of a variant block or variant parameter. To specify a variant control variable for a variant block, set Variant control mode to expression. For a variant parameter, expression is the only available variant control mode. The value of the variant control variable determines which variant control expression evaluates to true to activate the corresponding variant choice. By changing the values of variant control variables, you can switch between variant choices without altering the overall model structure. You can define the variant control variables in different workspaces, such as the base workspace, model workspace, or data dictionary based on your requirements. For more information, see Switch Between Choices Using Condition Expressions in Variant Blocks and Switch Between Choices Using Condition Expressions in Variant Parameters.

This model represents a vehicle system with a Variant Subsystem block Engine and a variant parameter kv. The Engine block has three variant choices Engine_2_cyl_petrol, Engine_4_cyl_petrol, and Engine_8_cyl_petrol which represent three possible engine configurations. You can switch between these subsystems based on the value of the variant control variable EngineType. The Engine_2_cyl_petrol subsystem is active when the variant condition EngineType == 1 evaluates to true, Engine_4_cyl_petrol is active when EngineType == 2 evaluates to true, and Engine_8_cyl_petrol is active when EngineType == 3 evaluates to true. The variant parameter kv stores multiple values of throttle, and you can switch between these values based on the value of the variant control variable nc. The throttle is 10 when nc == 1 evaluates to true, and the throttle is 20 when nc == 2 evaluates to ture.

During simulation, the value of throttle is provided as an input to the engine and differs for each engine configuration. If the value of EngineType is 1 and the value of nc is 2, a throttle value of 20 is provided as an input to the Engine_2_cyl_petrol subsystem.

Model with variant controls in variant block and in variant parameter

Automatically Identify Variant Paths Using Variant Condition Propagation

Simulink determines which model components are active during model compilation through a process called variant condition propagation. This process evaluates the variant controls specified on variant blocks and automatically propagates the variant conditions to the connecting blocks. Variant conditions can propagate through signal lines, buses, and function calls. This process deactivates the model components associated with the inactive variant choices, and they are excluded from simulation. See Propagate Variant Conditions from Variant Blocks Upstream and Downstream.

Control Signal Flow with Variant Logic Using Variant Source and Variant Sink Blocks

During simulation, the Variant Source block evaluates the variant conditions specified on its input ports and propagates these conditions to the connected blocks. Only the input port whose variant conditions evaluate to true becomes active, enabling the Variant Source block to pass through the corresponding input signal to the connected blocks. Similarly, the Variant Sink block receives a single input signal and routes it to one of several output ports, depending on which output port has a variant condition that evaluates to true. For more information, see Propagate Variant Conditions to Define Variant Regions Using Variant Source and Variant Sink Blocks.

This diagram shows how the variant conditions V == 1 and V == 2 defined on a Variant Source block automatically propagate to the connected blocks, so that each block is activated only when its corresponding variant condition evaluates to true. The first output port of the Variant Source carries the condition V == 1, and the second port carries V == 2. These variant conditions propagate to the blocks connected to each respective port. The blocks that share both ports display a combined condition V == 1 || V == 2, indicating that they remain active regardless of which port is selected.

Variant conditions V == 1 and V == 2 propagating from Variant Source block to connected blocks

Apply and Propagate Variant Logic Within and Beyond Subsystems

In Variant Subsystem blocks, variant conditions are applied to the subsystem choices and the referenced model choices within the block. The subsystem with a variant condition that evaluates to true becomes active during simulation. By default, these variant conditions do not propagate to blocks outside the Variant Subsystem block. To propagate the variant conditions outside the Variant Subsystem block, you must select the Propagate conditions outside of variant subsystem parameter as described in Propagate Variant Conditions to Define Variant Regions Outside Variant Subsystems to Promote Consistency and Reduce Errors.

Define Start and End Boundaries of Variant Logic Propagation

To further control variant condition propagation, you can define variant regions using Variant Start and Variant End blocks. These blocks allow you to specify where the variant condition propagation must begin and end, providing control over which parts of your model are affected by variant logic. For more information, see Control Variant Condition Propagation using Variant Start and Variant End Blocks. In addition, other variant blocks also support variant condition propagation. For a complete list of blocks that support propagation, see Variant Implementation Techniques in Simulink.

Selectively Check Signal Consistency and Include Variants in Generated Code Using Activation Time

Variant activation time determines in which phase of simulation and code generation Simulink selects the active choice for variant blocks and variant parameters. It also specifies when consistency checks across variant paths are performed, and which variant choices are analyzed when switching between the alternatives. These checks validate ports, signal dimensions, data types, sample times, and bus hierarchy to verify that switching between active and inactive variants does not introduce incompatibilities.

Variant Condition Evaluation Stages in Simulation and Code Generation

In the simulation workflow, the phases include model compile and simulation-loop. In the code generation workflow, the phases are model compile, code compile, and model startup. The orange dashed markers indicate the specific stages within each phase where Simulink evaluates variant conditions to determine the active variant choice. For example, when you set the variant activation time is to update diagram, the active variant choice is determined early during the model compile phase in simulation.

Depending on the variant activation time, Simulink uses variant condition propagation to optimize which blocks to compile or execute. For example, using the update diagram activation time improves compilation speed by excluding blocks that are not needed, such as those connected to hardware or external libraries that are not used during simulation. Similarly, using the code compile activation time allows you to switch between variant choices before generating code, enabling thorough testing of multiple design alternatives. By selecting the most appropriate activation time, you can enhance model performance, flexibility, or reliability according to your requirements. For more information on variant activation time, see Activate Variant During Different Stages of Simulation and Code Generation Workflow.

Evaluation of variant controls for each activation time during different stages of simulation and code generation

How Variant Activation Time Checks Consistency and Improves Simulation Efficiency

During simulation, variant activation time determines when Simulink evaluates active and inactive variant paths for consistency. Simulink checks that each variant path has the same number of ports, compatible signal dimensions, matching data types, and other relevant properties to confirm that switching between variants does not introduce errors due to incompatibilities.

For example, when you set the variant activation time to startup or runtime, Simulink compiles every variant path so that you can switch between them dynamically during simulation without errors. Once this compilation is complete, fast restart enables Simulink to reuse the compiled model for subsequent runs, provided the model structure remains unchanged. As a result, iterative simulations, parameter sweeps, or tests involving only input changes can run much faster since the overhead of recompiling the model is eliminated. In contrast, when you set the variant activation time to update diagram, Simulink compiles only the active variant choice. Consequently, consistency checks across all variant paths are not required, which reduces compilation overhead and results in even faster simulation cycles for scenarios where dynamic switching is not necessary.

This table describes the variant activation times when you perform simulation workflows for your model. For examples of when to use different types of variant activation times for various applications, see Optimal Activation of Variations During Simulation.

Variant activation timeEvaluation stageSimulation behavior
update diagramWhen you update the model
  • Checks signal attributes only within the active variant choice, ignoring inactive variant choices

  • Compiles only the active variant choice, minimizing compilation overhead and accelerating simulation

update diagram analyze all choicesDuring mode update with analysis
  • Checks signal attributes across variant choices to detect mismatches early

  • Compiles only the active variant choice, minimizing compilation overhead and accelerating simulation

startupAt model initialization
  • Checks signal attributes across variant choices to detect mismatches early

  • Compiles variant choices into a single compiled model so that you can use fast restart to switch between variant choices before each simulation run without recompilation

runtimeDuring simulation
  • Checks signal attributes across variant choices to detect mismatches early

  • Compiles variant choices into a single compiled model so that you can use fast restart to switch between variant choices during simulation without recompilation

    Note

    If your variant choices include blocks with states, use State Reader and State Writer blocks to transfer or reset states when you switch between variant choices at run time.

Note

When the Variant activation time parameter of a variant block is set to update diagram, no compiled information is available for its inactive variant choices. Any commands used to extract the compiled information from these inactive blocks return empty results. In contrast, for any activation time other than update diagram, compiled information for active and inactive variant choices is available and can be retrieved using the commands described in Programmatically Specify Block Parameters and Properties.

How Variant Activation Time Optimizes Code Generation and Run-Time Adaptability

During code generation, the variant activation time controls when the code generator evaluates active and inactive variant paths to determine which design alternatives are included in the generated code. For example, with the code compile activation time, inactive variant paths are excluded during compilation, producing a smaller and more efficient executable. With the runtime activation time, variant choices are included in the executable, allowing you to switch between the variant choices in real time on your hardware.

This table describes the variant activation times to choose when you perform code generation workflows for your model. For examples of when to use different types of variant activation times for various applications, see Optimal Inclusion of Variations in Generated Code .

Variant activation timeEvaluation stageSimulation behavior
code compileDuring code generationWith an Embedded Coder® license, generates code for variant choices enclosed within #if and #elif preprocessor statements. Although all variant choices appear in the generated code, the compiler excludes inactive paths when building the executable, resulting in a smaller code size and improved run-time performance.
startupAt model initializationGenerates code for variant choices enclosed within if statements so that every variant choice is included in the executable. The active variant choice remains fixed throughout each simulation step. This approach saves time because you do not need to regenerate code each time you switch variant choice on your hardware.
runtimeDuring code executionGenerates code for variant choices enclosed within if statements so that every variant choice is included in the executable. You can change the active variant choice in real-time during execution using predefined logic. This approach saves time because you do not need to regenerate code each time you switch variants on your hardware.