Main Content

Run Simulations for Variant Models Using Variant Configurations

Since R2024a

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 slexVariantReducer model has two named variant configurations created using Variant Manager. The configurations are stored in a variant configuration data object, slexVariantReducer_config, that is associated with the model.

Open the model:

modelName = "slexVariantReducer";

View the variant configurations in slexVariantReducer_config:

vcd = Simulink.VariantManager.getConfigurationData(modelName);
cellfun(@(name)(fprintf("%s\n", name)),{vcd.Configurations(:).Name})

Run Single Simulation

To simulate the model using a named configuration in slexVariantReducer_config, 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,'config1');
simOut = sim(simInp,"ShowProgress","on");

Run Multiple Parallel Simulations

To run multiple simulations using several named configurations present in slexVariantReducer_config, 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,{'config1','config2'});
simOuts = parsim(simInputs,"ShowProgress","on");

See Also



Related Topics