%% 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.
% 
% Copyright 2021 The MathWorks, Inc.

%% 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
%% 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');
%% 
%
% 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