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

1. Open the `rtwdemo_cgv` model.

```cgvModel = 'rtwdemo_cgv'; load_system(cgvModel);```
2. Save the model to a working directory.

```save_system(cgvModel, fullfile(pwd, cgvModel)); close_system(cgvModel); % avoid original model shadowing saved model ```

3. Use the `cgv.Config` to create a `cgv.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');`

4. 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 the `cgvCfg` object, the `cgv.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`

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

1. If you have not already done so, follow the steps described in Configure the Model.

2. Create a `cgv.CGV` object that specifies the `rtwdemo_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.

3. Provide the input file to the `cgvSim` object.

`cgvSim.addInputData(1, [cgvModel '_data']);`

4. 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']});`
5. Specify a location where the object writes all output data and metadata files for execution. This step is optional.

`cgvSim.setOutputDir('cgv_output');`

6. Execute the model.

`result1 = cgvSim.run();`

7. Get the output data associated with the input data.

`outputDataSim = cgvSim.getOutputData(1);`

8. 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:

1. If you have not already done so, configure and test the model, as described in Configure the Model and Execute the Model.

2. Test that the execution result of the model:

```if ~result1 || ~result2 error('Execution of model failed.'); end```

3. Use the `getOutputData` method to get the output data from the cgv.CGV objects.

```simData = cgvSim.getOutputData(1); silData = cgvSil.getOutputData(1);```

4. Display a list of signals by name using the `getSavedSignals` method.

```cgvSim.getSavedSignals(simData); ```

5. 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'};`

6. 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);```

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

```[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);```
At the MATLAB command line, you see:
```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:

1. If you have not already done so, configure and test the model, as described in Configure the Model and Execute the Model.

2. Use the `getOutputData` method to get the output data from the `cgv.CGV` objects.

```simData = cgvSim.getOutputData(1); silData = cgvSil.getOutputData(1);```

3. 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)'}; ```

4. Use the specified signals as input to the `compare` method to compare the signals from separate runs.

```[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```
At the MATLAB command line, the result is:
`3 Signals match, 0 Signals mismatch`

### Plot Output Signals

After setting up and running the test, use the `plot` method to plot output signals.

1. If you have not already done so, configure and test the model, as described in Configure the Model and Execute the Model.

2. Use the `getOutputData` method to get the output data from the `cgv.CGV` objects.

`simData = cgvSim.getOutputData(1);`

3. 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)'};```

4. 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); ```
{"ft_lang_label":"__label__en","ft_lang_prob":0.6092581,"math_prob":0.8899853,"size":6720,"snap":"2021-31-2021-39","text_gpt3_token_len":1667,"char_repetition_ratio":0.14681359,"word_repetition_ratio":0.26349893,"special_character_ratio":0.22693452,"punctuation_ratio":0.21615385,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.969244,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,1,null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-07-27T11:12:12Z\",\"WARC-Record-ID\":\"<urn:uuid:21be96a7-7176-4a50-9f87-48b6d6a87ff5>\",\"Content-Length\":\"81097\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:c654a74e-67d7-41e0-ae60-4ee7568ad0fa>\",\"WARC-Concurrent-To\":\"<urn:uuid:633d77cd-b937-4c74-910f-9dba01431447>\",\"WARC-IP-Address\":\"23.219.220.114\",\"WARC-Target-URI\":\"https://es.mathworks.com/help/ecoder/ug/verify-numerical-equivalence-between-two-modes-of-execution-of-a-model.html\",\"WARC-Payload-Digest\":\"sha1:NQJ342XO7UZZHRRRUOXD2O436PSFKS4F\",\"WARC-Block-Digest\":\"sha1:WJQIPTTI272I4E5I2OWUPQPTD3XSPRRS\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046153391.5_warc_CC-MAIN-20210727103626-20210727133626-00634.warc.gz\"}"}