Main Content

Simulate Variant Subsystem with Startup Activation Using parsim

This example shows how you can simulate a Variant Subsystem with startup activation time using parsim. You can simulate multiple configurations in parallel with the Variant activation time set to startup using parsim. Each configuration will have one active variant.

Model

Open the model slexVariantSubsystemsWithParSim. The model contains a variant subsystem block Controller with two choices Linear Controller and Nonlinear Controller with the conditions VSS_MODE == 1 and VSS_MODE == 2 respectively. Set the Variant activation time to startup in the Block Parameters dialog.

open_system('slexVariantSubsystemsWithParSim.slx')

Step 1 - Setup the active variant selection for each variant choice

Setup the active variant selection for each variant choice and set the number of simulations to be equal to the number of variant choices. In this example model, we have two variant choices.

mdl = 'slexVariantSubsystemsWithParSim';
numSims = 2;
varControl = [1,2];

Step 2 - Create the SimulationInput object

Create the SimulationInput object for each simulation run and set the variant control value for each run.

in(1:numSims) = Simulink.SimulationInput(mdl);
for idx = 1:numSims
    in(idx) = in(idx).setModelParameter('SimulationMode', 'rapid', ...
                'RapidAcceleratorUpToDateCheck', 'on', ...
                'SaveTime', 'on', ...
                'SaveOutput', 'on');
    in(idx) = in(idx).setVariable('VSS_MODE',varControl(idx));
end

Note: If your model has variant configurations defined using Variant Manager for Simulink®, you can use the setVariantConfiguration function on the Simulink.SimulationInput object to set the variant configuration to be applied before simulation. With this workflow, there is no need to specify the set of variant control values individually using the setVariable method to activate a specific variant. For an example, see Run Simulations for Variant Models Using Variant Configurations.

Step 3 - Use parsim to simulate the model

Use parsim to simulate the model in parallel for each variant control value.

out = parsim(in, 'ShowProgress', 'on');
[10-Jun-2024 09:01:41] Checking for availability of parallel pool...
Starting parallel pool (parpool) using the 'Processes' profile ...
10-Jun-2024 09:02:59: Job Queued. Waiting for parallel pool job with ID 1 to start ...
10-Jun-2024 09:04:00: Job Queued. Waiting for parallel pool job with ID 1 to start ...
Connected to parallel pool with 6 workers.
[10-Jun-2024 09:04:33] Starting Simulink on parallel workers...
[10-Jun-2024 09:05:24] Configuring simulation cache folder on parallel workers...
[10-Jun-2024 09:05:25] Loading model on parallel workers...
[10-Jun-2024 09:05:44] Running simulations...
[10-Jun-2024 09:09:04] Completed 1 of 2 simulation runs
[10-Jun-2024 09:09:04] Received simulation output (size: 21.10 KB) for run 1 from parallel worker.
[10-Jun-2024 09:09:04] Completed 2 of 2 simulation runs
[10-Jun-2024 09:09:04] Received simulation output (size: 21.10 KB) for run 2 from parallel worker.
[10-Jun-2024 09:09:04] Cleaning up parallel workers...

You can simulate the model using parsim with SetupFcn. This is optional. If you run parsim without SetupFcn, set the RapidAcceleratorUpToDateCheck to on.

  out = parsim(in, 'ShowProgress', 'on', 'SetupFcn', @() slexVariantSubsystemsWithParSim_script_setup(mdl));

The setup script, slexVariantSubsystemsWithParSim_script_setup.m builds the rapid acceleration target for the model.

Step 4 - Plot the output values

Now plot the results from each run.

for i = 1:numSims
    simOut = out(i);
    t = simOut.tout;
    y = simOut.yout;
    plot(t, y)
    hold all
end