Simulink Design Verifier 1.5
Block Replacements for Unsupported Blocks
This demo shows how to use Simulink Design Verifier functions to replace unsupported blocks and to customize test vector generation for specific requirements.
Contents
Model with a Nonlinear Block
The demo model includes a Switch block whose output is controlled by a Math Function block that operates as a sqrt function. For each switch position, the output of the model is calculated by a Lookup Table block. For this model, we will concentrate on generating test cases that satisfy the following:
1. Achieve 100% lookup table coverage.
2. Test vectors demonstrate each Switch block position when the values of its first and third input ports differ.
open_system('sldvdemo_sqrt_blockrep');
Checking Model Compatibility
Because the sqrt function that the Math Function block specifies is nonlinear, this model is not compatible with Simulink Design Verifier.
[status,msg] = sldvcompat('sldvdemo_sqrt_blockrep')
Checking compatibility of model "sldvdemo_sqrt_blockrep"
Compiling model... done
Checking compatibility... done
'sldvdemo_sqrt_blockrep' is partially compatible with Simulink Design Verifi
er.
The model contains unsupported elements and cannot be
analyzed directly by Simulink Design Verifier.
You can analyze it by turning on the
AutomaticStubbing option.
status =
0
msg =
1x2 struct array with fields:
source
sourceFullName
objH
reportedBy
msg
Creating a Custom Block Replacement Rule to Work Around the Incompatibility
This model can be made compatible by replacing the Math Function block with a supported block that is functionally equivalent. The library block shown below constrains the input signal to the range [0 10000] and approximates a sqrt function by using a lookup table.
The lookup table data was calculated to match the values of sqrt with a maximum error of 0.2 in the range [0 10000]. Please refer to the mask initialization pane of the block Sqrt_Approx in the library sldvdemo_custom_blockreplib for the values of the lookup table data.
The replacement rule defined in the M-file, sldvdemo_custom_blkrep_rule_sqrt.m, checks the following conditions on every Math Function block before allowing replacement:
1. Its Function parameter must specify sqrt.
2. Simulink Design Verifier supports Lookup Table blocks only when the input signal data type is either double or single, and the input and output ports specify the same data type. Therefore, this rule ensures that these conditions are satisfied before allowing a block replacement. Otherwise, the model will remain incompatible after such replacements.
function rule = sldvdemo_custom_blkrep_rule_sqrt
rule = Sldv.xform.BlkRepRule;
rule.FileName = mfilename;rule.BlockType = 'Math';
rule.ReplacementPath = sprintf('sldvdemo_custom_blockreplib/Sqrt_Appr
ox');rule.ReplacementMode = 'Normal';
parameter.OutMin = '$original.OutMin$';
parameter.OutMax = '$original.OutMax$';
parameter.OutDataTypeStr = '$original.OutDataTypeStr$';
rule.ParameterMap = parameter;rule.IsReplaceableCallBack = @replacementTestFunction;
end
function out = replacementTestFunction(blockH)
out = strcmp(get_param(blockH,'Function'),'sqrt');
if out
out = false;
acceptedOutDataTypeStr = {'double','single',...
'Inherit: Inherit via back propagation',...
'Inherit: Same as input'};
I = strmatch(get_param(blockH,'OutDataTypeStr'),acceptedOutDa
taTypeStr,'exact');
if ~isempty(I)portDataTypes = get_param(blockH,'CompiledPortDataTypes') ;
out = any(strcmp(portDataTypes.Inport,{'double','sin
gle'})) && ...
strcmp(portDataTypes.Inport,portDataTypes.Outport);
end
endend
open_system('sldvdemo_custom_blockreplib'); open_system('sldvdemo_custom_blockreplib/Sqrt_Approx/Lookup Table');
Configuring Simulink Design Verifier Options for Block Replacement
We will run Simulink Design Verifier in test generation mode with block replacements enabled. In order to pass the compatibility test, we must use the custom replacement rule sldvdemo_custom_blkrep_rule_sqrt.m.
Since we are also interested in lookup table coverage, we need the built-in block replacement blkrep_rule_lookup_normal.m, which inserts test objectives for each interval and breakpoint value for a Lookup Table block. Moreover, we need the built-in rule blkrep_rule_switch_normal.m, which requires each switch position to be exercised when the values of the first and third input ports differ. Please refer to the ''Working With Block Replacements'' chapter of the Simulink Design Verifier documentation for a list of all built-in replacement rules.
The analysis will run for a maximum of 30 seconds and produce a harness model. Report generation is also enabled. Other Simulink Design Verifier options are set to their default values.
opts = sldvoptions; opts.Mode = 'TestGeneration'; opts.MaxProcessTime = 30; opts.BlockReplacement = 'on'; opts.BlockReplacementRulesList = ['sldvdemo_custom_blkrep_rule_sqrt.m,' ... 'blkrep_rule_lookup_normal.m,'... 'blkrep_rule_switch_normal.m']; opts.SaveHarnessModel = 'on'; opts.SaveReport = 'on'; get(opts)
Mode: 'TestGeneration'
MaxProcessTime: 30
DisplayUnsatisfiableObjectives: 'on'
AutomaticStubbing: 'off'
OutputDir: 'sldv_output/$ModelName$'
MakeOutputFilesUnique: 'on'
BlockReplacement: 'on'
BlockReplacementRulesList: 'sldvdemo_custom_blkrep_rule_sqrt.m,blkr
ep_rule_lookup_normal.m,blkrep_rule_switch_normal.m'
BlockReplacementModelFileName: '$ModelName$_replacement'
Parameters: 'on'
ParametersConfigFileName: 'sldv_params_template.m'
ModelCoverageObjectives: 'MCDC'
TestConditions: 'UseLocalSettings'
TestObjectives: 'UseLocalSettings'
MaxTestCaseSteps: 500
TestSuiteOptimization: 'CombinedObjectives'
Assertions: 'UseLocalSettings'
ProofAssumptions: 'UseLocalSettings'
ProvingStrategy: 'Prove'
MaxViolationSteps: 20
SaveDataFile: 'on'
DataFileName: '$ModelName$_sldvdata'
SaveExpectedOutput: 'off'
RandomizeNoEffectData: 'off'
SaveHarnessModel: 'on'
HarnessModelFileName: '$ModelName$_harness'
ModelReferenceHarness: 'off'
SaveSystemTestHarness: 'off'
SystemTestFileName: '$ModelName$_harness'
SaveReport: 'on'
ReportFileName: '$ModelName$_report'
ReportIncludeGraphics: 'off'
DisplayReport: 'on'
Executing Test Generation with Block Replacements
The sldvrun function runs Simulink Design Verifier on a model using the settings defined in a sldvoptions object opts.
Since we enabled block replacements in the opts object, the original model will be copied into a new model named sldvdemo_sqrt_blockrep_replacement. Replacements and analysis will be performed on this new model. In the end, a harness model will be created from the original model. The generated report also includes a chapter summarizing block replacements performed on the model.
[status,fileNames] = sldvrun('sldvdemo_sqrt_blockrep', opts, true)
status =
1
fileNames =
DataFile: [1x116 char]
HarnessModel: [1x115 char]
SystemTestFile: ''
Report: [1x116 char]
ExtractedModel: ''
BlockReplacementModel: [1x119 char]
Executing Tests in the Harness Model
Enable the lookup table coverage metric and then run the test cases using the harness model. You can also execute the suite of tests by clicking the Run all button on the Signal Builder dialog box after enabling lookup table coverage from the Tools > Coverage Settings menu.
The coverage report shown below indicates that we are able to reach 100% lookup table coverage with the shown test vectors that Simulink Design Verifier generated.
[harnessModelPath harnessModel] = fileparts(fileNames.HarnessModel); set_param(harnessModel,'covMetricSettings','dcmte'); sldvdemo_playall(harnessModel);
Clean Up
To complete the demo, close all models and remove the files that Simulink Design Verifier generated.
close_system('sldvdemo_custom_blockreplib'); close_system(fileNames.HarnessModel,0); close_system(fileNames.BlockReplacementModel,0); close_system('sldvdemo_sqrt_blockrep',0); delete(fileNames.HarnessModel); delete(fileNames.BlockReplacementModel); delete(fileNames.DataFile);
Store