Record Coverage in Parallel Simulations by Using Parsim - MATLAB & Simulink (original) (raw)
Main Content
This example shows how to record coverage in multiple parallel Simulink® simulations corresponding to different test cases by using SimulationInput
objects and the parsim
command. If Parallel Computing Toolbox is installed on your system, the parsim
command runs simulations in parallel. Otherwise, the simulations are run in serial.
Model Overview
The [slvnvdemo_powerwindow_parsim](https://mdsite.deno.dev/matlab:open%5Fsystem%28'slvnvdemo%5Fpowerwindow%5Fparsim'%29)
model contains a power window controller and a low-order plant model. The component [slvnvdemo_powerwindow_parsim/power_window_control_system/control](https://mdsite.deno.dev/matlab:open%5Fsystem%28'slvnvdemo%5Fpowerwindow%5Fparsim/power%5Fwindow%5Fcontrol%5Fsystem/control'%29)
is a Model block that references the model [slvnvdemo_powerwindow_controller](https://mdsite.deno.dev/matlab:open%5Fsystem%28'slvnvdemo%5Fpowerwindow%5Fcontroller'%29)
, which implements the controller with a Stateflow® chart.
mdl = "slvnvdemo_powerwindow_parsim"; isModelOpen = bdIsLoaded(mdl); open_system(mdl);
Set Up Data for Multiple Simulations
Determine the number of test cases in the Signal Editor block by using the NumberOfScenarios
parameter. The number of test cases determines the number of iterations to run.
sigEditBlk = mdl + "/Input"; numCases = str2double(get_param(sigEditBlk,"NumberOfScenarios"));
Create an array of [Simulink.SimulationInput](../../simulink/slref/simulink.simulationinput.html)
objects to define the set of simulations to run. Each SimulationInput
object corresponds to one simulation and is stored in array simIn
. For each simulation, set these parameters:
ActiveScenario
to indicate which scenario of the Signal Editor block to executeCovEnable
to turn on coverage analysisCovSaveSingleToWokspaceVar
to save the coverage results to a workspace variableCovSaveName
to specify the name of the variable.
for idx = numCases:-1:1 simIn(idx) = Simulink.SimulationInput(mdl); simIn(idx) = setBlockParameter(simIn(idx),... sigEditBlk,"ActiveScenario",idx); simIn(idx) = setModelParameter(simIn(idx),... "CovEnable","on"); simIn(idx) = setModelParameter(simIn(idx),... "CovIncludeTopModel","off"); simIn(idx) = setModelParameter(simIn(idx),... "CovSaveSingleToWorkspaceVar","on"); simIn(idx) = setModelParameter(simIn(idx),... "CovSaveName","covdata"); end
Run Simulations in Parallel by Using Parsim
Use the [parsim](../../simulink/slref/parsim.html)
function to execute the simulations in parallel. Pass the array of SimulationInput
objects, simIn
, into the parsim
function as the first argument. Set the ShowProgress
option to on
to display the progress of the simulations in the MATLAB Command Window. The output from the parsim
command is simOut
, an array of Simulink.SimulationOutput
objects.
simOut = parsim(simIn,ShowProgress="on");
[01-Feb-2025 15:46:46] Checking for availability of parallel pool... [01-Feb-2025 15:46:47] Running simulations... [01-Feb-2025 15:47:00] Completed 1 of 2 simulation runs [01-Feb-2025 15:47:03] Completed 2 of 2 simulation runs
Each [Simulink.SimulationInput](../../simulink/slref/simulink.simulationinput.html)
object contains logged coverage results stored as [cv.cvdatagroup](../ref/cv.cvdatagroup-class.html) objects
. These coverage results are stored in a field named covdata
, as previously specified by the CovSaveName
parameter. Using parsim
to run multiple simulations means that errors are captured so that subsequent simulations can continue to run. Any errors are recorded in the ErrorMessage
property of the SimulationOutput
object.
covdata
references a file containing the coverage results. The coverage data from the referenced file is automatically loaded into memory when covdata
is used by a coverage function.
ans = ... cvdata file: /tmp/Bdoc25a_2864802_2301199/tp1ce04949/slcoverage-ex16619798/slcov_output/slvnvdemo_powerwindow_parsim/slvnvdemo_powerwindow_parsim_cvdata_1.cvt date: 01-Feb-2025 15:47:00
Compute Cumulative Coverage
Obtain the coverage data from each element of simOut
and cumulate the results.
coverageData = simOut(1).covdata; for i = 2 : numCases coverageData = coverageData + simOut(i).covdata; end
View the cumulative coverage results on the model by using coverage highlighting.
open(Simulink.BlockPath(... "slvnvdemo_powerwindow_parsim/power_window_control_system/control")); cvmodelview(coverageData);
Generate a cumulative coverage report.
cvhtml("cumulativeCovReport",coverageData);