Main Content

Activate Variant During Different Stages of Simulation and Code Generation Workflow

Using variant activation time, Simulink® sets active choices in variant blocks and variant parameters during different stages of simulation and code generation workflows.

The active choice is set:

  • During the model compile stage, or the simulation-loop stage of the simulation workflow.

  • During the model compile stage, the code compile stage, or the model startup stage of the code generation workflow.

See Stages to Set Active Choices in Variant Blocks and Variant Parameters.

To set the active choice, Simulink evaluates the variant controls that you specify in the workspace browser, at the command line, or in the callbacks, such as InitFcn, PreLoadFcn, and PostLoadFcn. For more information on callbacks, see Model Callbacks.

When you set the active choices at intermediate stages, you can include only the active choice or both the active and inactive choices in the simulation and code generation workflow. Including both active and inactive choices with code compile activation time improves the speed of code generation workflows because you do not need to regenerate the model every time you change the active choice. Similarly, the startup activation time improves the speed of the iterative simulation and code generation workflows because you do not need to recompile the model every time you change the active choice. You run a model only once. You can then change the active choice and generate the output using the artifacts generated in the previous iteration. The variant activation time also allows you to specify whether to analyze all variants for incompatibilities in signal attributes in the model, such as dimension and data type.

Note

Only one choice can be active any given stage of the simulation and code generation workflow.

Overview of variant activation time during simulation and code generation

Stages to Set Active Choices in Variant Blocks and Variant Parameters

These tables describe the stages at which Simulink sets the active choice of variant blocks and variant parameters during simulation and code generation workflow.

Stages of Simulation WorkflowDescriptionVariant Activation Time
model compileThis is the first stage of the simulation workflow. In this stage, the block parameter expressions and callback functions of the model are evaluated, signal attributes, sample time attributes, and block execution order is determined, and the block is optimized to improve the simulation speed.
simulation-loopThe Simulink successively computes the states and outputs of the system at intervals from the simulation start time to the finish time.startup

For more information on stages in the simulation workflow, see Simulation Phases in Dynamic Systems.

Stages of Code Generation WorkflowDescriptionVariant Activation Time
model compileThis is the first stage of the simulation workflow. In this stage, the block parameter expressions and callback functions of the model are evaluated, signal attributes, sample time attributes, and block execution order is determined, and the block is optimized to improve the simulation speed.
code compileThe C code is translated to machine code.code compile
model startupThe central processing unit executes the model’s machine code instructions. startup

Types of Variant Activation Time in Variants Blocks and Variant Parameters

Simulink sets the active choice of variant blocks and variant parameters during different stages of simulation and code generation workflow using these variant activation times:

update diagram

During simulation, Simulink sets and retains the active choice at the start of the model compilation stage before the signal attributes are propagated to the blocks. The inactive choices are removed from the simulation workflow. Signal attributes are then propagated only to the active choice, so only the active choice participates in the subsequent stages of simulation. The signal attributes are not analyzed for consistency across the model.

The code generation workflow is same as the simulation workflow. Since only active choices are retained and inactive choices are removed in the model compilation stage, the code that is generated in the codegen stage contains only the active choice.

For information on how to use the update diagram activation time in variant blocks and in variant parameters, see Variant activation time and ActivationTime.

update diagram analyze all choices

During simulation, Simulink propagates signal attributes to all variant choices. The signal attributes in the choices are analyzed to check if each block can accept the signals connected to its inputs. Simulink then sets and retains the active choice late in the model compilation stage. The inactive choices are removed from the simulation workflow, so only active choice participates in the subsequent stages of simulation.

The code generation workflow is same as the simulation workflow. Since only active choices are retained and inactive choices are removed in the model compilation stage, the code that is generated in the codegen stage contains only the active choice.

For information on how to use the update diagram analyze all choices activation time in variant blocks and in variant parameters, see Variant activation time and ActivationTime.

code compile

The simulation workflow is same as update diagram analyze all choices.

During code generation, Simulink propagates signal attributes to all variant choices. The signal attributes in all the choices are analyzed to check if each block can accept the signals connected to its inputs. All the choices participate in the codegen stage of the workflow. Thus, the generated code includes all the choices. In the code, the choices are enclosed in the C preprocessor conditionals #if and #else.

Simulink sets the active choice only when you compile the code to generate an executable from it. In other words, when you compile the code, the compiler accepts the active choice as input. The compiler compiles the preprocessor conditionals for the active choice and generates an executable for only the active choice. This option does not require you to regenerate the code next time you specify a different active choice. You generate the code only once, and you can then specify a different active choice prior to code compile and build the executable for that active choice.

Note

In code compile:

  • You must install Simulink Coder™ to generate code for only active choices in the model.

  • You must install Embedded Coder® to generate code for active and inactive choices in the model.

For information on how to use the code compile activation time in variant blocks and in variant parameters, see Variant activation time and Compile Code Conditionally for all Values of Variant Parameters.

startup

During simulation, Simulink propagates signal attributes to all variant choices. The signal attributes in all the choices are analyzed to check if each block can accept the signals connected to its inputs. Simulink then sets and retains the active choice and removes the inactive choices in the simulation-loop stage. Only the active choice participates in the simulation-loop stage. This option does not require you to compile the model next time you change the active choice. You compile the model only once, and you can then specify a different active choice prior to the simulation-loop stage and simulate the model. Simulink successively computes the states and outputs of the model for that active choice.

During code generation, Simulink propagates signal attributes to all the variants choices. The signal attributes in all the choices are analyzed to check if each block can accept the signals connected to its inputs. All the choices participate in the codegen stage of the workflow and hence, the generated code includes all the choices. In the code, the choices are enclosed in regular if conditions.

Simulink sets the active choice only when you run the executable that is built from the generated code. In other words, when you run the executable, the central processing unit accepts the active choice as input. The processor executes the if conditions for the active choice and generates the output for only the active choice. The advantage of using this option is that you are not required to regenerate the executable the next time you specify a different active choice. You generate the executable only once. You can then specify a different active choice prior to the running the executable and then generate the output for that active choice.

Note

In startup, you must install either Simulink Coder or Embedded Coder to generate code for active and inactive choices in the model.

With startup activation time, you can:

For information on limitations with the startup activation time, see Known Limitations of startup variant activation time.

Illustration of Different Variant Activation Times

This figure shows the stages at which Simulink sets the active choice for different variant activation times.

Variant activation time during different stages of simulation and code generation in variant blocks and in variant parameters

Variant Activation Time for Variant Blocks

This section explains how to specify the variant activation time, the supported variant control modes, and the variant condition propagation for different activation times in variant blocks.

Use Variant Activation Time in Variant Blocks

This example explains how to specify active choice using variant activation time in variant blocks.

Consider this model. The Variant Subsystem VSS1 has two potential variants, Linear and Nonlinear. The variant condition expression associated with the Linear is V == 1. The variant condition expression associated with the Nonlinear is V == 2. On the dialog box of VSS1, the Variant activation time parameter is specified as code compile.

When you generate code from this model, the generated code contains all variant choices of VSS1. In the code, the choices are enclosed in the C preprocessor conditional statements #if and #elif. Before you compile the code to generate an executable, you can specify a value for V as an input to the compiler. Upon compiling the code, the compiler accepts the value of V to evaluate each preprocessor conditional statement. Based on the condition that evaluates to true, the compiler sets the active choice and then generates the executable only for the active choice. For example, if you provide the input to the compiler as 1, the executable contains only the Linear variant choice.

To generate an executable for Nonlinear variant choice, change the input value to compiler to 2 and recompile the generated code.

An example of how to use variant activation time in variant blocks

Supported Variant Activation Time in Variant Blocks

The availability of activation times depends on the type of the variant control mode that you specify for the block. This table lists the variant activation time supported by each variant control mode. For information on variant control modes, see Introduction to Variant Controls.

Variant activation time
Variant control mode update diagramupdate diagram analyze all choicescode compilestartup
expression
labelxxx
sim codegen switchingxx

Note

The variant activation time of the Variant Connector (Simscape) block and event-based variant blocks such as Simulink Function block, Initialize Function block, Reset Function block, and Terminate Function block is by default set to update diagram. You cannot set it to any other activation time.

Variant Condition Propagation for Different Activation Times in Variant Blocks

Variant condition propagation allows you to assign condition expressions outside the variant blocks to the connecting blocks to improve performance. When you propagate conditions outside the Variant block, the inactive choices do not run during simulation, which can improve performance, particularly in large models. The propagation of variant condition outside the variant block varies for different activation times. For information, see Propagate Variant Conditions Outside a Variant Subsystem Block.

Propagating conditions through cascading blocks results in compounding conditions. Simulink does not support compounding conditions from variant blocks with startup and code compile variant activation times. For more information, see Considerations and Limitations for startup Variant Activation Time.

Variant Activation Time in Variant Parameters

This section explains how to specify the variant activation time, and the supported variant control modes in variant parameters.

Use Variant Activation Time in Variant parameters

This example explains how to specify active choice using variant activation time in variant parameters.

Consider this model. The Gain parameter of the Gain block is a variant parameter with its value set to K. The variable K has two values: 3.5 and 8.5. The Variant activation time of K is specified as code compile using the Simulink.VariantControl. This setting means that you can specify the active choice before code compilation starts.

An example of how to use variant activation time in variant parameters

V = Simulink.VariantControl('Value', 1, 'ActivationTime', code compile')
K = Simulink.VariantVariable('Choices', {'V==1', 3.5, 'V==2', 8.5})

When you generate code from this model, the generated code contains both active and inactive choices of K. In the code, the choices are enclosed in the C preprocessor conditional statements #if and #elif. When you compile this code to generate an executable, the compiler accepts the value of V as input to evaluate each preprocessor conditional statement. Based on the condition that evaluates to true, the compiler sets the active choice and then generates the executable only for the active choice. For example, if you provide the input to the compiler as 1, the executable is built for K set to 3.5.

To generate an executable with K set to 8.5, change the input value to compiler to 2 and compile the generated code again. You do not need to regenerate the code to generate an executable for different variant values.

Supported Variant Activation Time in Variant Parameters

This table lists the variant activation times available for variant parameters.

Variant control modeVariant activation time
 update diagramupdate diagram analyze all choicescode compilestartup
expression
labelnot supported
sim codegen switchingnot supported

Storage Classes for Different Variant Activation Times

When you specify a condition expression to determine the active choice, each variant control variable of the expression has a storage class associated with it. Use storage classes to control the appearance and placement of the variant control variables in the generated code. Storage classes also prevent the optimizations such as elimination of the storage for variant control variables in the generated code. For more information, see Choose Storage Class for Controlling Data Representation in Generated Code (Simulink Coder).

The variables used in the variant condition expressions must satisfy these criteria to generate code with Simulink Coder or Embedded Coder.

  • The variant control variables in variant condition expressions must have these storage classes.

  • If you write your own custom storage class, check that it does not generate a macro or a const.

  • To use scalar variables without any storage class for code generation, select Tunable as the Default parameter behavior from the Model Settings under Code Generation > Optimization.

Related Topics