Programmatically Resolve Unexpected Behavior in a Model with Model Slicer
In this example, you evaluate a Simulink® model, detect unexpected behavior, and use Model Slicer to programmatically isolate and resolve the unexpected behavior. When you plan to reuse your API commands and extend their use to other models, a programmatic approach is useful.
Be familiar with the behavior and purpose of Model Slicer and the functionality of the Model Slicer API. Highlight Functional Dependencies outlines how to use Model Slicer user interface to explore models. The
slslicertrace function reference pages contain the Model Slicer API command help.
Find the Area of the Model Responsible for Unexpected Behavior
The sldvSliceCruiseControlHarness test harness model contains a cruise controller subsystem sldvSliceCruiseControl and a block, TestCases, containing a test case for this subsystem. You first simulate the model to execute the test case. You then evaluate the behavior of the model to find and isolate areas of the model responsible for unexpected behavior:
1. Open the sldvSliceCruiseControlHarness test harness for the cruise control model.
Note: The Assertion block is set to Stop simulation when assertion fails when the actual operation mode is not the same as the expected operation mode.
The TestCases block contains several test inputs for sldvSliceCruiseControl.
2. In the TestCases Signal Editor click the Run all button to run all of the included test cases. You receive an error during the ResumeWO test case. The Assertion block halted simulation at 27 seconds, because the actual operation mode was not the same as the expected operation mode. Click OK to close this error message.
3. In the sldvSliceCruiseControlHarness model, double-click the Assertion block, clear Enable assertion, and click OK.
4. Set the Active Group of the TestCases Signal Editor block to the test case containing the error and run the simulation again.
set_param('sldvSliceCruiseControlHarness/TestCases',"ActiveScenario", "ResumeWO") sim('sldvSliceCruiseControlHarness')
The Scope block in the model contains three signals:
operation_mode - displays the actual operation mode of the subsystem.
expected_mode - displays the expected operation mode of the subsystem that the test case provides.
verify - displays a Boolean value comparing the operation mode and the expected mode.
The scope shows a disparity between the expected operation mode and the actual operation mode beginning at time 27. Now that you know the outport displaying the unexpected behavior and the time window containing the unexpected behavior, use Model Slicer to isolate and analyze the unexpected behavior.
Isolate the Area of the Model Responsible for Unexpected Behavior
1. Create a Model Slicer configuration object for the model using
slslicer. The Command Window displays the slice properties for this Model Slicer configuration.
obj = slslicer('sldvSliceCruiseControlHarness')
obj = SLSlicer with properties: Configuration: [1x1 SLSlicerAPI.SLSlicerConfig] ActiveConfig: 1 DisplayedConfig:  StorageOptions: [1x1 struct] AnalysisOptions: [1x1 struct] SliceOptions: [1x1 struct] InlineOptions: [1x1 struct] Contents of active configuration: Name: 'untitled' Description: '' Color: [0 1 1] SignalPropagation: 'upstream' StartingPoint: [1x0 struct] ExclusionPoint: [1x0 struct] Constraint: [1x0 struct] SliceComponent: [1x0 struct] UseTimeWindow: 0 CoverageFile: '' UseDeadLogic: 0 DeadLogicFile: ''
2. Activate the slice highlighting mode of Model Slicer to compile the model and prepare it for dependency analysis.
Consider turning on <a href="matlab:helpview(fullfile(docroot, 'simulink/ug/fast-restart-workflow.html#bugm2tp-3'))">Fast Restart</a> before launching Model Slicer for simulation based workflows. <a href="matlab:SlicerConfiguration.DisableFastRestartNotif">Do not show again.</a>
3. Add the operation_mode outport block as a starting point and highlight it.
The area of the model upstream of the starting point and active during simulation is highlighted.
4. Simulate the model within a restricted simulation time window (maximum 30 seconds) to highlight only the area of the model upstream of the starting point and active during the time window of interest.
Only the portion of the model upstream of the starting point and active during the simulation time window is highlighted.
5. You can further narrow the simulation time window by changing the start time to 20 seconds.
6. Create a sliced model sldvSliceCruiseControlHarness_sliced containing only the area of interest.
slicedModel = slice(obj,'sldvSliceCruiseControlHarness_sliced') open_system('sldvSliceCruiseControlHarness_sliced')
slicedModel = 'sldvSliceCruiseControlHarness_sliced'
The sliced model sldvSliceCruiseControlHarness_sliced now contains a simplified version of the source model sldvSliceCruiseControlHarness. The simplified standalone model contains only those parts of the model that are upstream of the specified starting point and active during the time window of interest.
Investigate the Sliced Model and Debug the Source Model
You can now debug the unexpected behavior in the simplified standalone model and then apply changes to the source model.
1. To enable editing the model again, terminate the Model Slicer mode.
2. Navigate to the area of the sliced model that contains the unexpected behavior.
The AND Logical Operator block in this subsystem has a truncated true constant attached to its second input port. This true constant indicates that the second input port is always true during the restricted time window for this sliced model, causing the cruise control system not to enter the "has canceled" state.
3. Navigate to the equivalent AND Logical Operator block in the source system by using
slslicertrace to view the blocks connected to the second input port.
h = slslicertrace('SOURCE',... 'sldvSliceCruiseControlHarness_sliced/Model/CruiseControlMode/opMode/resumeCondition/hasCanceled/LogicOp1') hilite_system(h)
h = 1.1050e+03
The OR Logical Operator block in this subsystem is always true in the current configuration. Changing the OR Logical Operator block to an AND Logical Operator block rectifies this error.
4. Before making edits, create new copies of the cruise control model and the test harness model.
5. Update the model reference in the test harness to refer to the newly saved model.
6. Use the block path of the erroneous Logical Operator block to fix the error.
7. Simulate the test harness for 45 seconds with the fixed model to confirm the corrected behavior.
The scope now shows that the expected operation mode is the same as the actual operation mode.
To complete the demo, save and close all models and remove the Model Slicer configuration object.
save_system('sldvSliceCruiseControl_fixed') save_system('sldvSliceCruiseControlHarness_fixed') close_system('sldvSliceCruiseControl_fixed') close_system('sldvSliceCruiseControlHarness_fixed') close_system('sldvSliceCruiseControlHarness_sliced') clear obj