Main Content

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

cross_release_model.png

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.

coverage_report_15a.png

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_2725827_13574/tp52003417_6574_4271_b8ec_bd1fb2cc856c/tp14ca096f_4f6b_473f_8cf5_7457964f0760.c /tmp/Bdoc24b_2725827_13574/tp52003417_6574_4271_b8ec_bd1fb2cc856c/tpd9a4f934_bc7b_4269_bb1d_336c8002747f.c -L/mathworks/devel/bat/filer/batfs2566-0/Bdoc24b.2725827/build/runnable/matlab/bin/glnxa64 -lmwsl_sfcn_cov_bridge -output sldvexSFunctionHandlingSFcn
Building with 'gcc'.
MEX completed successfully.
mex sldvexSFunctionHandlingSource.c /tmp/Bdoc24b_2725827_13574/tp52003417_6574_4271_b8ec_bd1fb2cc856c/sldvexSFunctionHandlingSFcn.c /tmp/Bdoc24b_2725827_13574/tp52003417_6574_4271_b8ec_bd1fb2cc856c/tpb600d8d0_4a30_4a5f_b5e8_e064a6fdbe71.c /tmp/Bdoc24b_2725827_13574/tp52003417_6574_4271_b8ec_bd1fb2cc856c/tp7cdf8f26_3428_4e6c_9b0f_4924616f409c.c -L/mathworks/devel/bat/filer/batfs2566-0/Bdoc24b.2725827/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);
06-Sep-2024 05:09:55
Checking compatibility for test generation: model 'sldvexSFunctionHandlingExample'
Compiling model...done
Building model representation...done

06-Sep-2024 05:10:10

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

Generating tests using model representation from 06-Sep-2024 05:10:10...


Generating output files:

06-Sep-2024 05:10:22
Results generation completed.

    Data file:
    /tmp/Bdoc24b_2725827_13574/tpa7db8267/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.

cross_release_sldv_results_summary.png

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.

coverage_report_15a_full.png

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).

  1. Go to the examples folder from your directory and copy the example model sldvCrossReleaseExample and S-Function files, rel_sfcn.c, rel_comp.c, and rel_sfcn.tlc in the current working folder. Copy the header files rel_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, export sldvCrossReleaseExample model to 15b. Save the model as sldvCrossReleaseExample_15b in the current working folder. For more information, see Export Model to Previous Version of Simulink.

  2. In MATLAB R2015b, open sldvCrossReleaseExample_15b model from the current working folder.

    open_system('sldvCrossReleaseExample_15b');
    

  3. 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);
  4. 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.

  5. 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.

  6. Save the configuration set of the model sldvCrossReleaseExample_15b to a MAT-file. This ConfigSet 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');
            
  7. In MATLAB R2018b or later releases, import the components exported from R2015b.

    1. 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 file rtwtypes.h, MATLAB displays an error.

    2. 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) block sldvCrossReleaseExample_15b_R2015b_sil.

  8. Add Inport and Outport ports to the sldvCrossReleaseExample_15b_R2015b_sil block and save the model as sldvCrossReleaseExample_sil_18b.

  9. Apply the model configuration set similar to R2015b model.

    load('copiedConfig.mat');
    attachConfigSet('sldvCrossReleaseExample_sil_18b', copiedConfig, true);
    setActiveConfigSet('sldvCrossReleaseExample_sil_18b', copiedConfig.Name);
  10. Set the simulation mode to Software-in-the-Loop (SIL). To simulate the model, in the harness model, click the Run all button.

  11. 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.

  12. In R2015b, open the model.

    open_system('sldvCrossReleaseExample_15b');
  13. Update the sldvData.ModelInfomation.Name field in sldvData same as the model name in older release. For example, sldvCrossReleaseExample_15b.slx.

  14. 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 the dataFile, type the location of the sldvData generated for sldvCrossReleaseExample_sil_18b model.

    sldvmakeharness('sldvCrossReleaseExample_15b.slx','dataFile')
  15. 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.

Related Topics