Main Content

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);
    

Related Topics