Test Two Simulations for Equivalence
Verify model equivalence in normal and SIL simulation mode.
This example shows how to test for equivalence between two models using test harnesses and the test manager. One model runs in normal mode, and a test harness model created from a subsystem runs in software-in-the-loop (SIL) mode.
The equivalence test case in the test manager compares signal output between two simulations to determine equivalence. Signals from the main model and the test harness are set up for logging in this example. The logged signals are used as the equivalence criteria between normal and SIL mode.
Configure the Model
mdl = 'sltestNormalSILEquivalenceExample'; harnessOwner = 'sltestNormalSILEquivalenceExample/Controller'; open_system(mdl);
Turn on signal logging in the model.
Mark the Controller subsystem output and input signals for logging.
ph_controller_in = get_param('sltestNormalSILEquivalenceExample/Controller/In1','PortHandles'); ph_controller_out = get_param('sltestNormalSILEquivalenceExample/Controller','PortHandles'); set_param(ph_controller_in.Outport(1),'DataLogging','on'); set_param(ph_controller_out.Outport(1),'DataLogging','on'); clear ph_controller_in ph_controller_out;
Simulate the model and output the logged signals. The signal data is used as input for the test harness.
out = sim(mdl);
Get the logged signal data.
out_data = out.get('SIL_signals'); control_in1 = out_data.get(2);
Create a Test Harness for SIL Verification.
The command to create the harness will generate code. Switch to a directory with write permissions.
origDir = pwd; dirName = tempname; mkdir(dirName); cd(dirName); cleanup = onCleanup(@()cd(origDir)); sltest.harness.create(harnessOwner,'Name','SIL_Harness','VerificationMode','SIL');
### Starting build procedure for: Controller ### Successful completion of build procedure for: Controller ### Creating SIL block ... Building with 'gcc'. MEX completed successfully. Build Summary Top model targets built: Model Action Rebuild Reason ============================================================================================ Controller Code generated and compiled. Code generation information file does not exist. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 42.627s
Open the test harness.
Set Up Logging in the Test Harness
Turn on signal logging in the test harness.
Mark the test harness outport for signal logging to use in the equivalence test case.
ph_harness_out = get_param('SIL_Harness/Controller','PortHandles'); set_param(ph_harness_out.Outport(1),'DataLogging','on'); clear ph_harness_out;
Assign the input data from the simulation to the test harness.
Create an Equivalence Test Case in the Test Manager
Open the test manager by opening the Apps tab, clicking Simulink Test in the Model Verification, Validation, and Test section, and then clicking *Simulink Test Manager" in the *Tests" tab. Alternately, use the command
Create an equivalence test case.
From the test manager toolstrip, click the New arrow and select Test File > Blank Test File.
Specify the test file as
testHarnessEquivalence.mldatx. The test manager creates the test file with a new test suite and baseline test case by default.
In the Test Browser pane, select the baseline test case,
New Test Case 1, and click Delete.
New Test Suite 1.
From the toolstrip, click the New arrow and select Equivalence Test.
In the Test Browser pane, right-click the new equivalence test case and select
Rename. Name the new equivalence test case
SIL Equivalence Test.
Assign the test harness to the equivalence test case Simulation 1.
Expand Simulation 1 and System Under Test.
Click the Use current model button to assign
Expand Test Harness.
Click the Refresh button to get an up-to-date list of available test harnesses.
Select SIL_Harness from the Harness menu to use as the System Under Test.
sltestNormalSILEquivalenceExample model as Simulation 2.
Collapse Simulation 1.
Expand Simulation 2 and System Under Test.
Click the Use current model button to assign
Collapse Simulation 2.
Capture the equivalence criteria. Under Equivalence Criteria, click Capture to run the test harness in Simulation 1 and identify the equivalence signal.
Run the Test Case and View the Results
SIL Equivalence Test in the Test Browser pane and click Run in the toolstrip. The test manager switches to the Results and Artifacts pane and runs the equivalence test case. The test case passes because the signal comparison between the model and the test harness matches. Expand the results set and select the
Controller:1 option button to plot the signal comparison.
close_system(mdl, 0); clear mdl harnessOwner cleanup control_in1 origDir out out_data;