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.
Setting the active choice at intermediate stages improves the speed of simulation and code generation workflows by enabling you to reuse the artifacts from the previous run. It also enables you to analyze variant choices for incompatibilities, such as datatype and dimension mismatches, prior to simulation and code generation.
For example, the code that you generate with code compile
activation time contains all the choices. Prior to the code compilation, you can specify
the value of the variant control variable. Based on the value of the variant control
variable, Simulink sets the active choice, and the code is conditionally compiled only for
the active choice. You can reuse the same code every time you change the value of the
variant control variable. The code conditionally compiles for the given active
choice.
Similarly, the code that you generate with startup
activation time
also contains all the choices. Prior to running the executable of the code, you can
specify the active choice by changing the value of the variant control variable. You can
reuse the same executable every time you change the value of the variant control
variable. The executable conditionally runs for the given active choice. Additionally,
startup
activation time enables you to skip recompilation and
directly start simulating the model. Recompiling the model is not required for accurate
results.
Note
Only one choice can be active any given stage of the simulation and code generation workflow.
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 Workflow | Description | Variant Activation Time |
---|---|---|
model compile | This 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-loop | 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 Workflow | Description | Variant Activation Time |
---|---|---|
model compile | This 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 compile | The C code is translated to machine code. | code compile |
model startup | The central processing unit executes the model’s machine code instructions. | startup |
Types of Variant Activation Time in Variant 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 with Same and Different Dimensions.
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 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:
Perform a fast restart simulation on your model and change the active choice prior to the simulation-loop stage without the need to recompile the model each time. This improves the Simulink Test™ workflow with fast restart. For more information, see Run Iterative Simulations Without Recompiling Model for Variant Systems Using Fast Restart, Simulate Variant Subsystem with Startup Activation Using parsim, and Verify and Validate Variant Models with Startup Activation Time.
Perform an accelerator or rapid accelerator simulation on your models. You can simulate a model incrementally with different variant configurations by changing the active variant each time without the need to regenerate code when there are no structural changes in the model. For more information, see Simulate Variant Blocks in Accelerator and Rapid Accelerator Modes with Startup Activation Time and Run Executable for Different Variant Parameter Values Without Recompiling Code.
Collect and aggregate coverage results from multiple simulations of Variant Subsystem, Variant Source, and Variant Sink blocks. With activation time set to
startup
, the coverage report displays all variant choices, regardless of whether they activate during the simulation. If a variant is not active during a simulation, the coverage analysis reports 0% coverage for that variant choice. For more information, see Model Coverage for Variant Blocks (Simulink Coverage).Simulate and create real-time applications from Simulink models with Variant blocks and run them on Speedgoat® target computer hardware connected to your physical systems. To create real time models, you must change the Variants to
atomic
and set the variant parameter asSimulink.Parameter
. For more information, see Create Real-Time Applications Using Variants and Simulink Real-Time.Create standalone executables outside of Simulink with different variant configurations and simulate the standalone executable by changing the active variant via an input to the executable. For more information see, Create Standalone Executables with Variants Using Simulink Compiler.
Generate HDL code that contains both active and inactive choices of a Variant Subsystem block. In the generated HDL code, the variant control variable is a tunable port. You can set the active choice by providing the value of the variant control variable at the model startup. For more information, see Variant Subsystem: Using Variant Subsystems for HDL Code Generation (HDL Coder).
Export and import models with inline variant blocks, and variant subsystems to a standalone Functional Mock-up Unit (FMU). The Variant activation time of the variant blocks must be set to
startup
. You can also import an existing FMU by using the FMU block. For more information, see Export and Import Function Mockup Unit (FMU) from Model with Variant Subsystem Blocks.
For information on limitations with the startup
activation time, see Known Limitations of startup variant activation time.
inherit from Simulink.VariantControl
The variant block inherits the activation time from its variant control
variable of type Simulink.VariantControl
. Inheriting the activation time from
variant control variables help you switch variant elements such as blocks and
parameters coherently. If a variant block has at least one variant control
variable of type Simulink.VariantControl
, then setting the
activation time of the block to inherit from
Simulink.VariantControl
is recommended, so that the block
inherits the activation time from the variant control variable.
Note
If a variant block has multiple variant control variables of type
Simulink.VariantControl
, then all those variables
must have the same activation time.
For information on how to use the inherit from
Simulink.VariantControl
activation time in variant blocks, see
Variant activation time. For an example, see Simulink.VariantControl Variables for Coherent Switching of Choices in Variant Blocks.
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 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.
Supported Variant Activation Time in Variant Blocks
This section lists the variant activation time supported by each type of variant blocks:
Hierarchical variant blocks and inline variant blocks
Variant Subsystem, Variant Model, Variant Source, Variant Sink— The availability of activation times depends on the type of the variant control mode that you specify for the block. For information on variant control modes, see Introduction to Variant Controls.
Variant Connector (Simscape), Manual Variant Source, Manual Variant Sink— The variant activation time is by default set to
update diagram
. You cannot set it to any other activation time.
Event-based variant blocks
Simulink Function— The block by default inherits the variant activation time from the corresponding Function Caller block. You can overwrite the inherited activation time to
code compile
or toupdate diagram
by selecting or clearing the Generate preprocessor conditionals parameter on its block parameter dialog box, respectively.Initialize Function, Reset Function, and Terminate Function— The variant activation time is by default set to
update diagram
. You can change it tocode compile
by selecting the Generate preprocessor conditionals parameter on its block parameter box.
This table lists the variant activation time supported by variant blocks in different control modes.
Variant activation time | |||||
---|---|---|---|---|---|
Variant blocks | Variant control mode | update diagram | update diagram analyze all choices | code compile | startup |
Variant Subsystem, Variant Model, Variant Source, Variant Sink | expression | ✓ | ✓ | ✓ | ✓ |
label | ✓ | x | x | x | |
sim codegen switching | ✓ | ✓ | x | x | |
Variant Connector (Simscape), Manual Variant Source, Manual Variant Sink | expression | ✓ | x | x | x |
label | not supported | ||||
sim codegen switching | not supported | ||||
Simulink Function, Initialize Function, Reset Function, and Terminate Function | expression | ✓ | ✓ | ✓ | x |
label | not supported | ||||
sim codegen switching | not supported |
Note
The operators that you can use to form a variant condition expression in variant blocks depend on its variant activation time. See Types of Operators in Variant Blocks for Different Activation Times.
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 to Define Variant Regions Outside Variant Subsystems.
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.
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.
Note
The operators that you can use to form a variant condition expression in variant parameters are listed in Types of Operators in Variant Parameters.
Variant control mode | Variant activation time | |||
---|---|---|---|---|
update diagram | update diagram analyze all choices | code compile | startup | |
expression | ✓ | ✓ | ✓ | ✓ |
label | not supported | |||
sim codegen switching | not 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.
This table lists the built-in storage classes supported for different activation times to generate code using Simulink Coder and Embedded Coder.
Built-in Storage Class Variant activation time code compile
startup
ExportedGlobal (Simulink Coder) x ✓ Model Default (Simulink Coder) x ✓ ImportedExtern, ImportedExternPointer (Simulink Coder) x ✓ Auto (Simulink Coder) x x This table lists the custom storage classes supported for different activation times to generate code using Embedded Coder.
Custom Storage Class Variant activation time code compile
startup
GetSet (Embedded Coder) x ✓ Define, ImportedDefine (Embedded Coder) ✓ x CompilerFlag (Embedded Coder) ✓ x Const and ConstVolatile. See, Const, Volatile, and ConstVolatile (Embedded Coder) x x Bitfield (Embedded Coder) x ✓ Struct (Embedded Coder) x ✓ ExportToFile (Embedded Coder) x ✓ ImportFromFile (Embedded Coder) x ✓ Volatile. See, Const, Volatile, and ConstVolatile (Embedded Coder) x ✓
If the activation time of a variant control variable is set to
startup
, then specifying its storage class to any of these is not supported:Storage classes such as
Define
that generates macros in the code.Storage classes such as
Const
that generates constant qualified variables in the code.
To use scalar MATLAB® variables as variant control variables in variant blocks without any storage class for code generation, select
Tunable
as the Default parameter behavior from the Model Settings under Code Generation > Optimization. This criteria is not applicable to AUTOSAR post build variants.