Run Simulations for Variant Models Using Variant Configurations
This example shows how to programmatically run single or multiple simulations for a variant model by specifying variant configurations as input to the Simulink.SimulationInput
object.
Variant elements in Simulink®, including variant blocks and variant parameters, enable you to represent multiple design alternatives within a single model. For such a model, you can create variant configurations to represent combinations of variant choices across the model hierarchy. These configurations comprise a set of variant control variables and their corresponding values, allowing you to activate specific variants within the model hierarchy.
Note: You must install the Variant Manager for Simulink support package using Add-On Explorer to create variant configurations for a model.
Explore Example Model
The slexVariantManagement
model has multiple named variant configurations created using Variant Manager. The configurations are stored in a variant configuration data object, vcd
, defined in the data dictionary associated with the model.
Open the model:
modelName = "slexVariantManagement";
open_system(modelName);
View the variant configurations in vcd
:
vcd = Simulink.VariantManager.getConfigurationData(modelName);
cellfun(@(name)(fprintf("%s\n", name)),{vcd.Configurations(:).Name});
LinInterExpNoNoise LinInterExpWithNoise LinInterStd NonLinExterLowFid NonLinExterHighFid SmartAIExterHighFid LinExterHighFid
Run Single Simulation
To simulate the model using a named configuration in vcd
, create a Simulink.SimulationInput
object, set the VariantConfiguration
property in the object to the named configuration, and run the simulation. This variant configuration is temporarily applied to the model, activating only the corresponding variant path in the model hierarchy before simulation.
simInp = Simulink.SimulationInput(modelName); simInp = setVariantConfiguration(simInp,'LinInterExpWithNoise'); simOut = sim(simInp,"ShowProgress","on");
[22-Apr-2024 16:05:15] Running simulations... [22-Apr-2024 16:05:32] Completed 1 of 1 simulation runs
Run Multiple Parallel Simulations
To run multiple simulations using several named configurations present in vcd
, create an array of Simulink.SimulationInput
objects and set the required configurations. You can use the parsim
or batchsim
functions to run the simulations in parallel or in batch mode.
simInputs(1:2) = Simulink.SimulationInput(modelName); simInputs = setVariantConfiguration... (simInputs,{'LinInterExpWithNoise','NonLinExterLowFid'}); simOuts = parsim(simInputs,"ShowProgress","on");
[22-Apr-2024 16:05:37] Checking for availability of parallel pool... Starting parallel pool (parpool) using the 'Processes' profile ... 22-Apr-2024 16:06:47: Job Queued. Waiting for parallel pool job with ID 1 to start ... Connected to parallel pool with 8 workers. [22-Apr-2024 16:07:35] Starting Simulink on parallel workers... [22-Apr-2024 16:08:21] Configuring simulation cache folder on parallel workers... [22-Apr-2024 16:08:22] Loading model on parallel workers... [22-Apr-2024 16:08:40] Running simulations... [22-Apr-2024 16:08:50] Completed 1 of 2 simulation runs [22-Apr-2024 16:08:50] Completed 2 of 2 simulation runs [22-Apr-2024 16:08:50] Cleaning up parallel workers...