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.
SeeStages 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 asInitFcn
,PreLoadFcn
, andPostLoadFcn
。For more information on callbacks, seeModel 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 withcode 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 withstartup
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
只能激活一个选择t的任何阶段he 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 | The 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, seeSimulation 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 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.
代码生成工作流simula一样tion 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 theupdate diagram
activation time in variant blocks and in variant parameters, seeVariant activation timeandActivationTime。
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.
代码生成工作流simula一样tion 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 theupdate diagram analyze all choices
activation time in variant blocks and in variant parameters, seeVariant activation timeandActivationTime。
code compile
The simulation workflow is same asupdate 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
Incode compile
:
You must installSimulink 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 thecode compile
activation time in variant blocks and in variant parameters, seeVariant activation timeandCompile 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 regularif
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 theif
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
Instartup
, you must install eitherSimulink Coderor Embedded Coder to generate code for active and inactive choices in the model.
Withstartup
激活时间,您可以:
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, seeRun Iterative Simulations Without Recompiling Model for Variant Systems Using Fast Restart,Simulate Variant Subsystem with Startup Activation Using parsim, andVerify 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 is no structural changes in the model. For more information, seeSimulate Variant Blocks in Accelerator and Rapid Accelerator Modes with Startup Activation TimeandRun 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, seeModel 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
and disableExternal Mode
。For more information, seeCreate Real-Time Applications Using Variants and Simulink Real-Time
For information on limitations with thestartup
activation time, seeKnown 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 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 SubsystemVSS1
has two potential variants,Linear
andNonlinear
。与相关的变异条件表达式theLinear
isV == 1
。与相关的变异条件表达式theNonlinear
isV == 2
。On the dialog box ofVSS1
, theVariant activation timeparameter is specified ascode compile
。
When you generate code from this model, the generated code contains all variant choices ofVSS1
。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 forV
as an input to the compiler. Upon compiling the code, the compiler accepts the value ofV
to evaluate each preprocessor conditional statement. Based on the condition that evaluates totrue
, 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 as1
, the executable contains only theLinear
variant choice.
To generate an executable forNonlinear
variant choice, change the input value to compiler to2
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, seeIntroduction 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 theGenerate preprocessor conditionalsparameter on its block parameter dialog box, respectively.Initialize Function,Reset Function, andTerminate Function— The variant activation time is by default set to
update diagram
。You can change it tocode compile
by selecting theGenerate preprocessor conditionalsparameter 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, andTerminate 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. SeeTypes 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, seePropagate Variant Conditions Outside Variant Subsystems。
Propagating conditions through cascading blocks results in compounding conditions. Simulink does not support compounding conditions from variant blocks withstartup
andcode compile
variant activation times. For more information, seeConsiderations 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. TheGainparameter of theGainblock is a variant parameter with its value set toK
。The variableK
has two values:3.5
and8.5
。The Variant activation time ofK
is specified ascode compile
using theSimulink.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 ofK
。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 ofV
as input to evaluate each preprocessor conditional statement. Based on the condition that evaluates totrue
, 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 as1
, the executable is built forK
set to3.5
。
To generate an executable withK
set to8.5
, change the input value to compiler to2
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 inTypes 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, seeChoose 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 withSimulink Coderor 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 usingSimulink Coderand 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®变量变异控制变量变异blocks without any storage class for code generation, select
Tunable
as theDefault parameter behaviorfrom theModel SettingsunderCode Generation>Optimization。This criteria is not applicable to AUTOSAR post build variants.