Enhance Model Coverage of Older Release Models
To enhance the model coverage of a model that you created in an older release, use a test generation workflow or a code generation workflow. You can leverage the latest release capabilities of Simulink® Design Verifier™ to generate the test cases for a Model-Based Design.
These workflows enhance model coverage.
Enhance Model Coverage by Generating Test Cases for Older Release Model
This example shows how to upgrade model coverage of a model created in R2015b. You use test generation for supported S-functions available in the latest release.
The example model sldvexSFunctionHandlingExample
contains the handwritten S-Function, which implements a lookup table algorithm. The handwritten S-Function is in the file sldvexSFunctionHandlingSFcn.c
. The user source code for the lookup table is in the file sldvexSFunctionHandlingSource.c
.
1. In MATLAB R2015b, open the sldvexSFunctionHandlingExample
model.
open_system('sldvexSFunctionHandlingExample');
2. To simulate the model and generate the coverage report, in the Simulink Editor, click the Run button. See View Coverage Results in Simulink Canvas (Simulink Coverage).
After the simulation, the coverage report indicates that full coverage is not achieved for sldvexSFunctionHandlingExample
model.
3. In MATLAB R2018b or later releases, open the sldvexSFunctionHandlingExample
model. The example model sldvexSFunctionHandlingExample
is available in R2015b and later releases, so you can use the same model for test generation workflow.
open_system('sldvexSFunctionHandlingExample');
To avoid any potential changes in the model, create a copy of the older release model in the current working folder, and then open the model in R2018b or later releases. To upgrade and improve models that you use in the current release, you can use the upgradeadvisor
function. See Programmatically Analyze and Upgrade Model.
4. Compile the S-function to be compatible with Simulink Design Verifier for test case generation by using slcovmex
(Simulink Coverage). For more information, see Configuring S-Function for Test Case Generation.
slcovmex('-sldv', ... '-output', 'sldvexSFunctionHandlingSFcn',... 'sldvexSFunctionHandlingSource.c','sldvexSFunctionHandlingSFcn.c');
mex /tmp/Bdoc24b_2679053_1509958/tp5384ec21_cbc3_4060_823a_10d73e23c468/tp548e285a_fc39_4da8_8809_6ce4a22d2aee.c /tmp/Bdoc24b_2679053_1509958/tp5384ec21_cbc3_4060_823a_10d73e23c468/tpac9f16cd_ddee_4360_bd6a_7e3f36ce5518.c -L/mathworks/devel/bat/filer/batfs2561-0/Bdoc24b.2679053/build/runnable/matlab/bin/glnxa64 -lmwsl_sfcn_cov_bridge -output sldvexSFunctionHandlingSFcn Building with 'gcc'. MEX completed successfully. mex sldvexSFunctionHandlingSource.c /tmp/Bdoc24b_2679053_1509958/tp5384ec21_cbc3_4060_823a_10d73e23c468/sldvexSFunctionHandlingSFcn.c /tmp/Bdoc24b_2679053_1509958/tp5384ec21_cbc3_4060_823a_10d73e23c468/tp62b9c08e_3417_453b_8b75_15be1e1ae9b0.c /tmp/Bdoc24b_2679053_1509958/tp5384ec21_cbc3_4060_823a_10d73e23c468/tpb656054d_5987_45ec_a592_49e892f4996d.c -L/mathworks/devel/bat/filer/batfs2561-0/Bdoc24b.2679053/build/runnable/matlab/bin/glnxa64 -lmwsl_sfcn_cov_bridge -output sldvexSFunctionHandlingSFcn Building with 'gcc'. MEX completed successfully.
5. Create an opts
option for the sldvexSFunctionHandlingExample
model.
opts = sldvoptions; opts.Mode = 'TestGeneration'; opts.ModelCoverageObjectives = 'ConditionDecision'; opts.SaveHarnessModel = 'off'; opts.SaveReport = 'off'; opts.SFcnSupport = 'on';
6. To generate test cases by using the specified opts
options, use sldvrun
to analyze the model.
[status, fileNames] = sldvrun('sldvexSFunctionHandlingExample', opts);
20-Jul-2024 19:51:18 Checking compatibility for test generation: model 'sldvexSFunctionHandlingExample' Compiling model...done Building model representation...done 20-Jul-2024 19:51:32 'sldvexSFunctionHandlingExample' is compatible for test generation with Simulink Design Verifier. Generating tests using model representation from 20-Jul-2024 19:51:32... Generating output files: 20-Jul-2024 19:51:44 Results generation completed. Data file: /tmp/Bdoc24b_2679053_1509958/tp72f6dff9/sldv-ex67693772/sldv_output/sldvexSFunctionHandlingExample/sldvexSFunctionHandlingExample_sldvdata.mat
After analysis, the software generates a Simulink Design Verifier data file and stores it in the default location <current_folder>\sldv_output\sldvexSFunctionHandlingExample_sldvdata.mat.
7. In R2015b, open the model.
open_system('sldvexSFunctionHandlingExample');
8. Load the sldvData
file created in R2018b or later releases.
a. On the Design Verifier tab, click Load Earlier Results and browse to the sldvData
MAT-file generated in R2018b or later releases.
b. Click Open.
9. In the Simulink Design Verifier Results Summary window, click Simulate tests and produce a model coverage report. The report indicates that 100% coverage is achieved for sldvexSFunctionHandlingExample
model.
For more information, see Manage Simulink Design Verifier Data Files and Simulate Tests and Produce Model Coverage Report.
Enhance Model Coverage by Using Generated Code from Older Release
This example shows how to upgrade the model coverage of a model created in R2015b by using code generation workflow.
For this workflow, you must have Simulink Coder™ and Embedded Coder®.
The example model sldvCrossReleaseExample
contains the handwritten
S-Function, which implements a relational boundary algorithm. The handwritten S-Function
is in the file rel_sfcn.c
. The user source code is in the file
rel_comp.c
.
To inline the S-function, use the rel_sfcn.tlc
file. For more
information, see Inline S-Functions with TLC (Embedded Coder).
Go to the examples folder from your directory and copy the example model
sldvCrossReleaseExample
and S-Function files,rel_sfcn.c
,rel_comp.c
, andrel_sfcn.tlc
in the current working folder. Copy the header filesrel_comp.h
into the current working folder. You use the example model and supporting files in R2015b for a Cross-Release Code Integration (Embedded Coder) workflow.Note
The example model
sldvCrossReleaseExample
is created for example purpose. To perform code generation workflow by using the example model, exportsldvCrossReleaseExample
model to 15b. Save the model assldvCrossReleaseExample_15b
in the current working folder. For more information, see Export Model to Previous Version of Simulink.In MATLAB R2015b, open
sldvCrossReleaseExample_15b
model from the current working folder.open_system('sldvCrossReleaseExample_15b');
Compile the S-function by using the function
legacy_code
.def = legacy_code('initialize'); def.SFunctionName = 'rel_sfcn'; def.OutputFcnSpec = 'uint8 y1 = relational_bound(uint8 u1)'; def.HeaderFiles = {'rel_comp.h'}; def.SourceFiles = {'rel_comp.c'}; def.IncPaths = {pwd}; def.SrcPaths = {pwd}; def.Options.supportCoverageAndDesignVerifier = true; legacy_code('sfcn_cmex_generate', def); legacy_code('compile', def);
To simulate the model and generate the coverage report, in the Simulink Editor, click the Run button. See View Coverage Results in Simulink Canvas (Simulink Coverage).
After the simulation, the coverage report indicates that 50% coverage is achieved for
sldvCrossReleaseExample_15b
model.To generate code using Embedded Coder, from the Apps tab, select Embedded Coder. For more information, see Generate Code Using Embedded Coder (Embedded Coder).
In the C Code tab, click Generate Code.
The model is preconfigured with these code generation settings.
set_param(sldvCrossReleaseExample_15b,'SystemTargetFile','ert.tlc'); set_param(sldvCrossReleaseExample_15b,'PortableWordSizes','on'); set_param(sldvCrossReleaseExample_15b,'SupportNonFinite','off'); set_param(sldvCrossReleaseExample_15b,'GenCodeOnly','on'); set_param(sldvCrossReleaseExample_15b,'SolverMode','SingleTasking'); set_param(sldvCrossReleaseExample_15b,'ProdEqTarget','on');
The software generates C code for the model and saves the files in the default folder location
<current_folder>\sldvCrossReleaseExample_15b_ert_rtw
.Save the configuration set of the model
sldvCrossReleaseExample_15b
to a MAT-file. ThisConfigSet
is used to set the configuration set of the model in R2018b and later releases.config_set = getActiveConfigSet('sldvCrossReleaseExample_15b'); copiedConfig = config_set.copy; save('copiedConfig.mat','copiedConfig');
In MATLAB R2018b or later releases, import the components exported from R2015b.
Before you import components in current release, rename or delete
rtwtypes.h
file available in the folder<current_folder>\sldvCrossReleaseExample_15b_ert_rtw
. During cross-release import, MATLAB tries to regenerate a file with same name. If you do not delete or rename the filertwtypes.h
, MATLAB displays an error.Import the generated component code from R2015b as software-in-the-loop (SIL) block.
crossReleaseImport('sldvCrossReleaseExample_15b_ert_rtw',... 'sldvCrossReleaseExample_15b', 'SimulationMode','SIL');
The
crossReleaseImport
function creates an untitled model that contains software-in-the-loop (SIL) blocksldvCrossReleaseExample_15b_R2015b_sil
.
Add Inport and Outport ports to the
sldvCrossReleaseExample_15b_R2015b_sil
block and save the model assldvCrossReleaseExample_sil_18b
.Apply the model configuration set similar to R2015b model.
load('copiedConfig.mat'); attachConfigSet('sldvCrossReleaseExample_sil_18b', copiedConfig, true); setActiveConfigSet('sldvCrossReleaseExample_sil_18b', copiedConfig.Name);
Set the simulation mode to
Software-in-the-Loop (SIL)
. To simulate the model, in the harness model, click the Run all button.To generate test cases for Embedded Coder generated code, on the Design Verifier tab, select Target > Code Generated as Top Model and click Generate Tests. For more information, see Generate Test Cases for Embedded Coder Generated Code.
After Simulink Design Verifier analysis, the software generates the test cases and saves the
sldvData
in folder at default location<current_folder>\sldv_output\sldvCrossReleaseExample_sil_18b
.In R2015b, open the model.
open_system('sldvCrossReleaseExample_15b');
Update the
sldvData.ModelInfomation.Name
field insldvData
same as the model name in older release. For example,sldvCrossReleaseExample_15b.slx
.Create a harness model by using the
sldvData
created in R2018b or later releases. This data consists of test cases generated from Embedded Coder generated code. In thedataFile
, type the location of thesldvData
generated forsldvCrossReleaseExample_sil_18b
model.sldvmakeharness('sldvCrossReleaseExample_15b.slx','dataFile')
To simulate the model by using all the test cases, click the Run all (Coverage) button in the harness model.
The software simulates all the test cases and generates a coverage report. The report indicates that 100% coverage is achieved for
sldvCrossReleaseExample_15b
model.