Verify Numerical Equivalence Between Two Modes of Execution of a Model
The following example describes configuring, executing, and comparing the results of the rtwdemo_cgv model in normal and software-in-the-loop (SIL) simulation modes.
Configure the Model
The first task for verifying numerical equivalence is to check the configuration of your model.
Open the
rtwdemo_cgv
model.cgvModel = 'rtwdemo_cgv'; load_system(cgvModel);
Save the model to a working directory.
save_system(cgvModel, fullfile(pwd, cgvModel)); close_system(cgvModel); % avoid original model shadowing saved model
Use the
cgv.Config
to create acgv.Config
object. Specify parameters that check and modify configuration parameter values and save the model for top-model SIL mode of execution.cgvCfg = cgv.Config('rtwdemo_cgv', 'connectivity', 'sil', 'SaveModel', 'on');
Use the
configModel
method to review your model configuration and to change the settings to configure your model for SIL. When'connectivity'
is set to'sil'
, the system target file is automatically set to'ert.tlc'
. If you specified the parameter/value pair, ('SaveModel', 'on'
) when you created thecgvCfg
object, thecgv.Config.configModel
method saves the model.Note
CGV runs on models that are open. If you modify a model without saving it, CGV can issue an error.
cgvCfg.configModel(); % Evaluate, change, and save your model for SIL
Display a report of the changes that
cgv.Config.configModel
makes to the model.cgvCfg.displayReport(); % In this example, this reports no changes
Execute the Model
Use the CGV API to execute the model in two modes. The two modes in this example are normal mode simulation and SIL mode. In each execution of the model, the CGV object for each mode captures the output data and writes the data to a file.
If you have not already done so, follow the steps described in Configure the Model.
Create a
cgv.CGV
object that specifies thertwdemo_cgv
model in normal mode simulation.cgvSim = cgv.CGV(cgvModel, 'connectivity', 'sim');
Note
When the top model is set to normal simulation mode, the CGV API sets referenced models in PIL mode to accelerator mode.
Provide the input file to the
cgvSim
object.cgvSim.addInputData(1, [cgvModel '_data']);
Before execution of the model, specify the MATLAB files to execute or MAT-files to load. This step is optional.
cgvSim.addPostLoadFiles({[cgvModel '_init.m']});
Specify a location where the object writes all output data and metadata files for execution. This step is optional.
cgvSim.setOutputDir('cgv_output');
Execute the model.
result1 = cgvSim.run();
Get the output data associated with the input data.
outputDataSim = cgvSim.getOutputData(1);
For the next mode of execution, SIL, repeat steps 2–7.
cgvSil = cgv.CGV( cgvModel, 'Connectivity', 'sil'); cgvSil.addInputData(1, [cgvModel '_data']); cgvSil.addPostLoadFiles({[cgvModel '_init.m']}); cgvSil.setOutputDir('cgv_output'); result2 = cgvSil.run();
Compare All Output Signals
After setting up and running the test, compare the outputs by doing the following:
If you have not already done so, configure and test the model, as described in Configure the Model and Execute the Model.
Test that the execution result of the model:
if ~result1 || ~result2 error('Execution of model failed.'); end
Use the
getOutputData
method to get the output data from the cgv.CGV objects.simData = cgvSim.getOutputData(1); silData = cgvSil.getOutputData(1);
Display a list of signals by name using the
getSavedSignals
method.cgvSim.getSavedSignals(simData);
Using the list of signals, build a list of signals in a cell array of character vectors. The signal list can contain a number of signals.
signalList = {'simData.getElement(4).Values.Data'};
Use the
createToleranceFile
method to create a file, in this example,'localtol'
, correlating tolerance information with output signal names.toleranceList = {{'absolute', 0.5}}; cgv.CGV.createToleranceFile('localtol', signalList, toleranceList);
Compare the output data signals. By default, the
compare
method looks at all signals which have a common name between both executions. If a tolerance file is present,cgv.CGV.compare
uses the associated tolerance for a specific signal during comparison; otherwise the tolerance is zero. In this example, the'Plot'
parameter is set to'mismatch'
. Therefore, only mismatched signals produce a plot.At the MATLAB command line, you see:[matchNames, ~, mismatchNames, ~] = ... cgv.CGV.compare(simData, silData, 'Plot', 'mismatch', ... 'Tolerancefile', 'localtol'); fprintf( '%d Signals match, %d Signals mismatch\n', ... length(matchNames), length(mismatchNames)); disp('Mismatched Signal Names:'); disp(mismatchNames);
14 Signals match, 1 Signals mismatch Mismatched Signal Names: 'simData.getElement(4).Values.Data'
A plot results from the signal mismatch.
The lower plot displays the numeric difference between the results.
Compare Individual Output Signals
After setting up and running the test, compare the outputs of individual signals by doing the following:
If you have not already done so, configure and test the model, as described in Configure the Model and Execute the Model.
Use the
getOutputData
method to get the output data from thecgv.CGV
objects.simData = cgvSim.getOutputData(1); silData = cgvSil.getOutputData(1);
Use the
getSavedSignals
method to display the output data signal names. Build a list of specific signal names in a cell array of character vectors. The signal list can contain a number of signals.cgv.CGV.getSavedSignals(simData); signalList = {'simData.getElement(3).Values.hi1.mid0.lo1.Data', ... 'simData.getElement(3).Values.hi1.mid0.lo2.Data', ... 'simData.getElement(2).Values.Data(:,3)'};
Use the specified signals as input to the
compare
method to compare the signals from separate runs.At the MATLAB command line, the result is:[matchNames, ~, mismatchNames, ~] = ... cgv.CGV.compare(simData, silData, 'Plot', 'mismatch', ... 'signals', signalList); fprintf( '%d Signals match, %d Signals mismatch\n', ... length(matchNames), length(mismatchNames)); if ~isempty(mismatchNames) disp( 'Mismatched Signal Names:'); disp(mismatchNames); end
3 Signals match, 0 Signals mismatch
Plot Output Signals
After setting up and running the test, use the plot
method to plot output
signals.
If you have not already done so, configure and test the model, as described in Configure the Model and Execute the Model.
Use the
getOutputData
method to get the output data from thecgv.CGV
objects.simData = cgvSim.getOutputData(1);
Use the
getSavedSignals
method to display the output data signal names. Build a list of specific signal names in a cell array of character vectors. The signal list can contain a number of signals.cgv.CGV.getSavedSignals(simData); signalList = {'simData.getElement(2).Values.Data(:,1)'};
Use the specified signal list as input to the
plot
method to compare the signals from separate runs.[signalNames, signalFigures] = cgv.CGV.plot(simData, ... 'Signals', signalList);