Main Content

Simulate Harness Model with Signal Editor Inputs Block

This example shows how to generate model coverage report by simulating the test harness model with the Signal Editor Inputs block. You can simulate a single test case or counterexample by selecting the active scenario in the Signal Editor dialog box. For more information see, Simulate Harness Model by Using the Signal Editor Inputs Block.

To simulate all the test cases and measure their combined model coverage, use the cvsim or the parsim command.

In this example, you generate a harness model by selecting the Signal Editor as the harness source. The Signal Editor scenarios consists of signal sources that are associated with the test cases or counterexamples. Then, to generate combined model coverage report, you simulate all the scenarios by using the cvsim or parsim function.

1. Open the model and configure harness options

Create a harness model for the sldvdemo_cruise_control model by using the sldvharnessopts options. Set the HarnessSource option to Signal Editor.

model = 'sldvdemo_cruise_control';
open_system(model);
opts = sldvoptions;
opts.Mode = 'TestGeneration';
opts.SaveHarnessModel = 'on';
opts.HarnessSource = 'Signal Editor';
opts.HarnessModelFileName = 'sldvdemo_cruise_control_harness';
opts.SaveReport = 'off';

2. Generate test cases

Analyze the model by using the sldvrun function and sldvoptions.

sldvrun('sldvdemo_cruise_control', opts);
save_system('sldvdemo_cruise_control_harness');
17-Oct-2023 14:45:30
Checking compatibility for test generation: model 'sldvdemo_cruise_control'
Compiling model...done
Building model representation...done

17-Oct-2023 14:45:32

'sldvdemo_cruise_control' is compatible for test generation with Simulink Design Verifier.

Generating tests using model representation from 17-Oct-2023 14:45:32...

........................
17-Oct-2023 14:46:01

Completed normally.

Generating output files:

    Harness model:
    C:\Users\vshukla\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\vshukla.Bdoc24a.j2397882\sldv-ex99648832\sldv_output\sldvdemo_cruise_control\sldvdemo_cruise_control_harness.slx

17-Oct-2023 14:46:09
Results generation completed.

    Data file:
    C:\Users\vshukla\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\vshukla.Bdoc24a.j2397882\sldv-ex99648832\sldv_output\sldvdemo_cruise_control\sldvdemo_cruise_control_sldvdata.mat

3. Generate combined model coverage report

Simulink Design Verifier automatically configures Signal Editor harness in multiple simulation mode. To simulate the generated test cases and gather coverage for Test Unit, click Run all (Coverage) button on Simulation toolstrip menu.

Alternatively, after the analysis generates the harness model, you can use this code that uses cvtest and cvsim functions to generate the combined model coverage report.

signalEditorBlock = 'sldvdemo_cruise_control_harness/Inputs';
numOfScenarios = str2double(get_param(signalEditorBlock,'NumberOfScenarios'));
harnessModel = 'sldvdemo_cruise_control_harness';
test = cvtest(harnessModel);
test.modelRefSettings.enable = 'On';
test.modelRefSettings.excludeTopModel = 1;
covData = [];
for id = 1:numOfScenarios
set_param(signalEditorBlock,'ActiveScenario',id);
aCovData = cvsim(harnessModel);
if isempty(covData)
covData = aCovData;
else
covData = covData + aCovData;
end
end
save_system('sldvdemo_cruise_control_harness');
cvhtml('Coverage_Harness',covData);

Optionally, you can use this code that uses the parsim function to generate the combined model coverage report.

signalEditorBlock = 'sldvdemo_cruise_control_harness/Inputs';
numOfScenarios = str2double(get_param(signalEditorBlock,'NumberOfScenarios'));
harnessModel = 'sldvdemo_cruise_control_harness';

simIn  = Simulink.SimulationInput.empty(0,numOfScenarios);
for id = 1:numOfScenarios
    simIn(id) = Simulink.SimulationInput(harnessModel);
    simIn(id) = simIn(id).setBlockParameter(signalEditorBlock,'ActiveScenario', id);
    simIn(id) = simIn(id).setModelParameter('CovEnable', 'on');
    simIn(id) = simIn(id).setModelParameter('CovSaveSingleToWorkspaceVar', 'on');
end

simOut = parsim(simIn);
cvhtml('Coverage_Harness',simOut.covdata);
[17-Oct-2023 14:46:17] Checking for availability of parallel pool...
Starting parallel pool (parpool) using the 'Processes' profile ...
Connected to parallel pool with 4 workers.
[17-Oct-2023 14:47:09] Starting Simulink on parallel workers...
[17-Oct-2023 14:47:45] Configuring simulation cache folder on parallel workers...
[17-Oct-2023 14:47:45] Loading model on parallel workers...
[17-Oct-2023 14:48:04] Running simulations...
[17-Oct-2023 14:48:25] Completed 1 of 3 simulation runs
[17-Oct-2023 14:48:25] Completed 2 of 3 simulation runs
[17-Oct-2023 14:48:25] Completed 3 of 3 simulation runs
[17-Oct-2023 14:48:26] Cleaning up parallel workers...

The coverage report indicates that 100% coverage is achieved by simulating all the test cases for sldvdemo_cruise_control_model.

5. Clean Up

% To complete this example, close the models.
close_system('sldvdemo_cruise_control_harness', 0);
close_system('sldvdemo_cruise_control', 0);