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.

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 compileactivation 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 withstartupactivation 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,startupactivation 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.

Evaluation of variant controls for each activation time during different stages of 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 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 diagramactivation 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 choicesactivation 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#ifand#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 compileactivation 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 regularifconditions.

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 theifconditions 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激活时间,您可以:

For information on limitations with thestartupactivation 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.

Detailed view of setting active and inactive choices in variant blocks and in variant parameters during different stages of simulation and code generation for each activation time

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 SubsystemVSS1has two potential variants,LinearandNonlinear。与相关的变异条件表达式theLinearisV == 1。与相关的变异条件表达式theNonlinearisV == 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#ifand#elif。Before you compile the code to generate an executable, you can specify a value forVas an input to the compiler. Upon compiling the code, the compiler accepts the value ofVto 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 theLinearvariant choice.

To generate an executable forNonlinearvariant choice, change the input value to compiler to2and recompile the generated code.

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

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

  • 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 tocode compileor toupdate diagramby 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 toupdate diagram。You can change it tocode compileby 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 withstartupandcode compilevariant 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 variableKhas two values:3.5and8.5。The Variant activation time ofKis specified ascode compileusing theSimulink.VariantControl。This setting means that you can specify the active choice before code compilation starts.

An example of how to set 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 ofK。In the code, the choices are enclosed in the C preprocessor conditional statements#ifand#elif。When you compile this code to generate an executable, the compiler accepts the value ofVas 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 forKset to3.5

To generate an executable withKset to8.5, change the input value to compiler to2and 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.

  • If the activation time of a variant control variable is set tostartup, then specifying its storage class to any of these is not supported:

    • Storage classes such asDefinethat generates macros in the code.

    • Storage classes such asConstthat generates constant qualified variables in the code.

  • To use scalar MATLAB®变量变异控制变量变异blocks without any storage class for code generation, selectTunableas theDefault parameter behaviorfrom theModel SettingsunderCode Generation>Optimization。This criteria is not applicable to AUTOSAR post build variants.

Related Topics