Skip to Main Content Skip to Search
Product Documentation

Programmatic Code Generation Verification

Code Generation Verification API Overview

When you execute a model in different modes of execution, you can use the Code Generation Verification (CGV) API to verify the numerical equivalence of results. CGV supports executing the model in simulation, Software-In-the-Loop (SIL), and Processor-In-the-Loop (PIL). For more information about SIL and PIL, see Numerical Equivalence Checking. The CGV demo, rtwdemo_cgv_script, shows CGV configuration, execution, and comparison support.

Verify Numerical Equivalence with CGV

Before verifying numerical equivalence:

To verify numerical equivalence:

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 simulation and SIL modes.

This example contains the following tasks:

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 cgv.Config.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 might 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, any referenced models set to PIL mode will be changed 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();
    *** handling PostLoad file rtwdemo_cgv_init.m
    Start CGV execution of model rtwdemo_cgv, ComponentType topmodel, ... 
    	connectivity sim, InputData rtwdemo_cgv_data.mat 
    End CGV execution: status completed
  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();

    At the MATLAB command line, the result is:

    *** handling PostLoad file rtwdemo_cgv_init.m
    Start CGV execution of model rtwdemo_cgv, ComponentType topmodel, ...
         connectivity sil, InputData rtwdemo_cgv_data.mat 
    
    ### Starting build procedure for model: rtwdemo_cgv
    ### Successful completion of build procedure for ...
        model: rtwdemo_cgv
    ### Preparing to start SIL simulation ...
    ### Starting SIL simulation for model: rtwdemo_cgv
    ### Stopping SIL simulation for model: rtwdemo_cgv
    End CGV execution: status completed

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 cgv.CGV.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 cgv.CGV.getSavedSignals method.

    cgvSim.getSavedSignals(simData);
    

    At the MATLAB command line, the result it:

    simData.hi0.Data(:,1)                      
    simData.hi0.Data(:,2)                      
    simData.Vector.Data(:,1)                   
    simData.Vector.Data(:,2)                   
    simData.Vector.Data(:,3)                   
    simData.Vector.Data(:,4)                   
    simData.BusOutputs.hi0.Data(:,1)           
    simData.BusOutputs.hi0.Data(:,2)           
    simData.BusOutputs.hi1.mid0.lo0.Data(1,1,:)
    simData.BusOutputs.hi1.mid0.lo0.Data(1,2,:)
    simData.BusOutputs.hi1.mid0.lo0.Data(2,1,:)
    simData.BusOutputs.hi1.mid0.lo0.Data(2,2,:)
    simData.BusOutputs.hi1.mid0.lo1.Data       
    simData.BusOutputs.hi1.mid0.lo2.Data       
    simData.BusOutputs.hi1.mid1.Data(:,1)      
    simData.BusOutputs.hi1.mid1.Data(:,2)      
    simData.ErrorsInjected.Data                
  5. Using the list of signals, build a list of signals in a cell array of strings. The signal list can contain any number of signals.

    signalList = {'simData.ErrorsInjected.Data'};
    
  6. Use the cgv.CGV.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 cgv.CGV.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, the result is:

    14 Signals match, 1 Signals mismatch
    Mismatched Signal Names:
        'simData.ErrorsInjected.Data'

    A plot results from the mismatch on signal simData.ErrorsInjected.Data.

    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 cgv.CGV.getOutputData method to get the output data from the cgv.CGV objects.

    simData = cgvSim.getOutputData(1);
    silData = cgvSil.getOutputData(1);
  3. Use the cgv.CGV.getSavedSignals method to display the output data signal names. Build a list of specific signal names in a cell array of strings. The signal list can contain any number of signals.

    cgv.CGV.getSavedSignals(simData);
    signalList = {'simData.BusOutputs.hi1.mid0.lo1.Data', ...
    'simData.BusOutputs.hi1.mid0.lo2.Data', 'simData.Vector.Data(:,3)'};
  4. Use the specified signals as input to the cgv.CGV.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 cgv.CGV.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 cgv.CGV.getOutputData method to get the output data from the cgv.CGV objects.

    simData = cgvSim.getOutputData(1);
  3. Use the cgv.CGV.getSavedSignals method to display the output data signal names. Build a list of specific signal names in a cell array of strings. The signal list can contain any number of signals.

    cgv.CGV.getSavedSignals(simData);
    signalList = {'simData.Vector.Data(:,1)'};
  4. Use the specified signal list as input to the cgv.CGV.plot method to compare the signals from separate runs.

    [signalNames, signalFigures] = cgv.CGV.plot(simData, ...
         'Signals', {'simData.Vector.Data(:,1)'});
    

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS