%% Collect Coverage for Multiple Simulations by Using Design Studies
% This example shows how to collect coverage when running multiple
% simulations. To learn more about running multiple simulations, see
% <docid:simulink_doccenter#bvo3bik>.

% Copyright 2021 The MathWorks, Inc.

%%
% The |slvnvdemo_powerwindow_parsim| model contains a power window
% controller and a low-order plant model. A Stateflow&reg; chart implements
% the controller.
%

modelName = 'slvnvdemo_powerwindow_parsim';
open_system(modelName);

%% Load the Design Study
% Design studies allow you to run a series of simulations where each
% simulation changes one parameter at a time. You can collect coverage on
% the simulations to test different sets of model inputs without manually
% running each simulation. For more information about creating design
% studies, see <docid:simulink_ug#mw_ebacf42a-570d-495d-88e6-e1ddece1ea6b>.

%%
% To open the *Multiple Simulations* pane, on the *Simulation* tab, under
% *Prepare*, click *Multiple Simulations*.
%

%%
% To load the design study file, click the *Load file containing design
% studies* icon at the top of the *Multiple Simulations* pane. In the
% *Select File to Open* window, select
% |slvnvdemo_powerwindow_multisim.mldatx|.


%%
% This file contains two design studies:
%

%%
% The *Driver Scenario* design study contains two simulations. In both
% simulations, the Signal Editor block |Input| parameter *Active Scenario*
% is set to |Driver|. In the first simulation, the *Active Signal*
% parameter is set to |Driver Up|, and in the second simulation the
% parameter is set to |Driver Down|. These settings simulate the
% driver-side power window receiving input to raise and lower the window.

%%
% The *Passenger Scenario* design study contains two simulations. In both
% simulations, the Signal Editor block |Input| parameter *Active Scenario*
% is set to |Passenger|. In the first simulation, the *Active Signal*
% parameter is set to |Passenger Up|, and in the second simulation the
% parameter is set to |Passenger Down|. These settings simulate the
% passenger-side power window receiving input to raise and lower the
% window.

%% Enable Coverage Analysis
% Next, enable coverage for the model. On the *Modeling* tab, click *Model
% Settings*. In the left pane, click *Coverage* and then select *Enable
% coverage analysis*. Click *OK*.

blockHandle = [modelName,'/Input'];
scenarios = ["Driver"; "Driver"; "Passenger"; "Passenger"];
signals = ["Driver Up"; "Driver Down"; "Passenger Up"; "Passenger Down"];
for k = 4:-1:1
    simIn(k) = Simulink.SimulationInput(modelName);
    simIn(k) = setBlockParameter(simIn(k),blockHandle,"ActiveScenario",scenarios(k));
    simIn(k) = setBlockParameter(simIn(k),blockHandle,"ActiveSignal",signals(k));
    simIn(k) = setModelParameter(simIn(k),"CovEnable","on");
    simIn(k) = setModelParameter(simIn(k),"CovSaveSingleToWorkspaceVar","on");
    simIn(k) = setModelParameter(simIn(k),"CovSaveName","covData");
    simIn(k) = setModelParameter(simIn(k),"CovMetricStructuralLevel","MCDC");
end

%% Run Simulations and Review Coverage Results
% In the *Multiple Simulations* pane, select *Driver Scenario*. On the
% *Apps* tab, select *Coverage Analyzer*. In the *Coverage* tab, click
% *Analyze Coverage > Run All (Coverage)*

simOut = parsim(simIn,'ShowProgress','off');

%%
% After the simulations complete, the *Coverage Details* pane opens and
% displays the aggregated coverage results for the two simulations in the
% Driver Scenario.

aggCovData = simOut(1).covData;
for k = 2:4
    aggCovData = aggCovData + simOut(k).covData;
end
cvmodelview(aggCovData)

%%
% <<../driver-scenario-coverage-results.png>>

%%
% The coverage results for all of the simulations are populated into the
% Coverage Results Explorer. To open the Coverage Results Explorer, on the
% *Coverage* tab, click *Results Explorer*. 
% 

%%
% <<../driver-results-explorer.png>>

%% 
% In the left pane of the Coverage Results Explorer, you can see the two
% simulations for the Driver Scenario design study and the current
% cumulative results. In the left pane, click *Current Cumulative Data*. In
% the *Coverage Data* pane, the description lists the design study name and
% a list of the block and parameter names specified in the design study.
% The *Tag* field lists the scenario specified for each run.

%%
% In the Simulink&reg; window, in the *Multiple Simulations* pane, select
% *Passenger Scenario*. This action automatically clears the *Driver
% Scenario* design study because you can only select one design study at a
% time. On the *Coverage* tab, click *Analyze Coverage > Run All
% (Coverage)*.

%%
% The *Coverage Details* pane updates with aggregated data fromd the two
% additional simulations in the Passenger Scenario design study. Scroll to
% the *Aggregated Tests* section to see four simulations: two from the
% Driver Scenario design study, and two from the Passenger Scenario design
% study.

%%
% <<../two-scenario-coverage-results.png>>

%%
% In the Coverage Results Explorer, click *Current Cumulative Data* to see
% that the additional simulations improved the coverage results.

%%
% <<../two-scenario-results-explorer.png>>

%%
% Additionally, you can click each run to see that the aggregated
% coverage data displays more complete coverage than either of the
% individual runs. The different coverage results indicate that the
% different simulations satisfied different coverage outcomes.

%%
% To generate a standalone report for these results, in the
% Simulink&reg; window, on the *Coverage* tab, click *Generate Report*.
% Alternatively, in the Coverage Results Explorer, in the left pane, click
% *Current Cumulative Data* and then click *Generate report*.

cvhtml('multisimCovResults',aggCovData,'-sRT=0')