Setting Up Rapid Simulation Input Data

Introduction

The format and setup of input data for a rapid simulation depends on your requirements.

If the Input Data Source Is...Then...
The model's global parameter vector (model_P)Use the rsimgetrtp function to get the vector content and then save it to a MAT-file
The model's global parameter vector and you want a mapping between the vector and tunable parametersUse the rsimgetrtp function with the AddTunableParamInfo option to get the model's global parameter structure and then save it to a MAT-file
Provided by a From File blockCreate a MAT-file that a From File block can read
Provided by an Inport blockCreate a MAT-file that adheres to one of the three data file formats that the Inport block can read
Provided by a From Workspace blockCreate structure variables in the MATLAB® workspace

The RSim target requires that MAT-files used as input for From File and Inport blocks contain data. The grt target inserts MAT-file data directly into the generated code, which is then compiled and linked as an executable. In contrast, RSim allows you to replace data sets for each successive simulation. A MAT-file containing From File or Inport block data must be present if any From File or Inport blocks exist in your model.

Creating a MAT-File That Includes a Model's Parameter Structure

To create a MAT-file that includes a model's global parameter structure (model_P),

  1. Get the structure by calling the function rsimgetrtp.

  2. Save the parameter structure to a MAT-file.

If you want to run simulations over varying data sets, consider converting the parameter structure to a cell array and saving the parameter variations to a single MAT-file.

Getting the Parameter Structure for a Model

Get the global parameter structure (model_P) for a model by calling the function rsimgetrtp.

param_struct = rsimgetrtp('model', option)
ArgumentDescription
modelThe model for which you are running the rapid simulations.
optionThe parameter-value pair 'AddTunableParamInfo' 'value', where 'value' can be 'on' or 'off'. If you set the parameter to 'on', the Real-Time Workshop® software extracts tunable parameter information from the specified model and returns it to param_struct.

The rsimgetrtp function forces an update diagram action for the specified model and returns a structure that contains the following fields:

FieldDescription
modelChecksumA four-element vector that encodes the structure of the model. The Real-Time Workshop software uses the checksum to check whether the structure of the model has changed since the RSim executable was generated. If you delete or add a block, and then generate a new model_P vector, the new checksum no longer matches the original checksum. The RSim executable detects this incompatibility in parameter vectors and exits to avoid returning incorrect simulation results. If the model structure changes, you must regenerate the code for the model.
parametersA structure that contains the model's global parameters.

If you specify 'AddTunableParamInfo' 'on', the Real-Time Workshop build process creates and then deletes model.rtw from your current working directory and includes the following fields for each parameter in the parameter structure:

FieldDescription
dataTypeNameThe name of the parameter's data type, for example, double
dataTypeIDAn internal data type identifier used by the Real-Time Workshop software
complexThe value 0 if real and 1 if complex

To use the AddTunableParamInfo option, inline parameters must be enabled.

The Real-Time Workshop software reports a tunable fixed-point parameter according to its stored value. For example, an sfix(16) parameter value of 1.4 with a scaling of 2^-8 has a value of 358 as an int16.

In the following example, rsimgetrtp returns the parameter structure for the demo model rtwdemo_rsimtf to param_struct.

param_struct = rsimgetrtp('rtwdemo_rsimtf')

param_struct = 

    modelChecksum: [1.7165e+009 3.0726e+009 2.6061e+009 2.3064e+009]
       parameters: [1x1 struct]

Saving the Parameter Structure to a MAT-File

After you issue a call to rsimgetrtp, save the return value of the function call to a MAT-file. Using a command-line option, you can then specify that MAT-file as input for rapid simulations.

The following example saves the parameter structure returned for rtwdemo_rsimtf to the MAT-file myrsimdemo.mat.

save myrsimdemo.mat param_struct;

For information on using command-line options to specify required files, see Running Rapid Simulations.

Converting the Parameter Structure for Running Simulations on Varying Data Sets

If you might have a need to use rapid simulations to test changes to specific parameters, you can do so if you convert the model's parameter structure to a cell array. You can then access a specific parameter by using the @ operator to specify the index for a specific parameter in the file.

To convert the structure to a cell array,

  1. Save the parameters vector of the structure returned by rsimgetrtp to a temporary variable. The following example saves the parameter vector to temporary variable p.

    param_struct = rsimgetrtp('rtwdemo_rsimtf');
    p = param_struct.parameters;
  2. Convert the structure to a cell array.

    param_struct.parameters = [];
    
  3. Assign the saved contents of the temporary variable to the original structure name as an element of the cell array.

    param_struct.parameters{1} = p;
    param_struct.parameters{1}
    
    ans = 
    
        dataTypeName: 'double'
          dataTypeId: 0
             complex: 0
          dtTransIdx: 0
              values: [-140 -4900 0 4900]
  4. Make a copy of the cell array to preserve the original parameter values.

    param_struct.parameters{2} = param_struct.parameters{1};
    param_struct.parameters{2}
    
    ans = 
    
        dataTypeName: 'double'
          dataTypeId: 0
             complex: 0
          dtTransIdx: 0
              values: [-140 -4900 0 4900]

    For a subsequent data set, increment the array index.

  5. Modify any combination of the parameter values.

    param_struct.parameters{2}.values=[-150 -5000 0 4950];
  6. Repeat steps 4 and 5 for each parameter data set you want to use as input to a rapid simulation of the model.

  7. Save the cell array representing the parameter structure to a MAT-file.

    save rtwdemo_rsimtf.mat param_struct;

Changing Block Parameters for an RSim Simulation explains how to specify each data set when you run the simulations.

Creating a MAT-File for a From File Block

You can use a MAT-file as the input data source for a From File block. The format of the data in the MAT-file must match the matrix format expected by that block.

To create such a MAT-file,

  1. Create a matrix in the workspace that consists of two or more rows. The first row must contain monotonically increasing time points. Other rows contain data points that correspond to the time point in that column. The time and data points must be data of type double.

    For example:

    t=[0:0.1:2*pi]';
    Ina1=[2*sin(t) 2*cos(t)];
    Ina2=sin(2*t);
    Ina3=[0.5*sin(3*t) 0.5*cos(3*t)];
    var_matrix=[t Ina1 Ina2 Ina3]';

    For more information on setting up the input data, see the description of the From File block in the Simulink® documentation.

  2. Save the matrix to a MAT-file.

    The following example saves the matrix var_matrix to the MAT-file myrsimdemo.mat.

    save myrsimdemo.mat var_matrix;

    Using a command-line option, you can then specify that MAT-file as input for rapid simulations.

Creating a MAT-File for an Inport Block

You can use a MAT-file as the input data source for an Inport block.

The format of the data in the MAT-file must adhere to one of the three column-based formats listed in the following table. The table lists the formats in order from least flexible to most flexible.

FormatDescription
Single time/data matrix
  • Least flexible.

  • One variable.

  • Two or more columns. Number of columns must equal the sum of the dimensions of all root Inport blocks plus 1. First column must contain monotonically increasing time points. Other columns contain data points that correspond to the time point in a given row.

  • Data of type double.

For an example, see Single time/data matrix in step 4 below. For more information, see Importing Data Arrays in the Simulink documentation.

FormatDescription
Signal-and-time structure
  • More flexible than the single time/data matrix format.

  • One variable.

  • Must contain two top-level fields: time and signals. The time field contains a column vector of the simulation times. The signals field contains an array of substructures, each of which corresponds to an Inport block. The substructure index corresponds to the Inport block number. Each signals substructure must contain a field named values. The values field must contain an array of inputs for the corresponding Inport block, where each input corresponds to a time point specified by the time field.

  • If the time field is set to an empty value, clear the check box for the Inport block Interpolate data parameter.

  • No data type limitations, but must match Inport block settings.

For an example, see Signal-and-time structure in step 4 below. For more information on this format, see Importing Data Structures in the Simulink documentation.

FormatDescription
Per-port structure
  • Most flexible

  • Multiple variables. Number of variables must equal the number of Inport blocks.

  • Consists of a separate structure-with-time or structure-without-time for each Inport block. Each Inport block's data structure has only one signals field. To use this format, enter the names of the structures in the Input text field as a comma-separated list, in1, in2,..., inN, where in1 is the data for your model's first port, in2 for the second port, and so on.

  • Each variable can have a different time vector.

  • If the time field is set to an empty value, clear the check box for the Inport block Interpolate data parameter.

  • No data type limitations, but must match Inport block settings.

  • To save multiple variables to the same data file, you must save them in the order expected by the model, using the -append option.

For an example, see Per-port structure in step 4 below. For more information, see Importing Data Structures in the Simulink documentation.

The supported formats and the following procedure are illustrated in rtwdemo_rsim_i.

To create a MAT-file for an Inport block,

  1. Choose one of the preceding data file formats.

  2. If necessary, update Inport block parameter settings and specifications to match specifications of the data you expect to be supplied by the MAT-file.

    By default, the Inport block inherits parameter settings from downstream blocks. In most cases, to import data from an external MAT-file, you need to explicitly set the following parameters to match the source data in the MAT-file.

    If you choose to use a structure format for workspace variables and the time field is empty, you must clear Interpolate data or modify the field such that it is set to a nonempty value. Interpolation requires time data.

    For descriptions of the preceding block parameters, see the description of the Inport block in the Simulink documentation.

  3. Build an RSim executable for the model. The Real-Time Workshop build process creates and calculates a structural checksum for the model and embeds it in the generated executable. The RSim target uses the checksum to verify that data being passed into the model is consistent with what model's executable expects.

  4. Create the MAT-file that is to provide the source data for the rapid simulations. Generally, you can create the MAT-file from a workspace variable. Using the specifications in the format comparison table above, create the workspace variables for your simulations.

    An example of each format follows:

    Single time/data matrix

    t=[0:0.1:2*pi]';
    Ina1=[2*sin(t) 2*cos(t)];
    Ina2=sin(2*t);
    Ina3=[0.5*sin(3*t) 0.5*cos(3*t)];
    var_matrix=[t Ina1 Ina2 Ina3];

    Signal-and-time structure

    t=[0:0.1:2*pi]';
    var_single_struct.time=t;
    var_single_struct.signals(1).values(:,1)=2*sin(t);
    var_single_struct.signals(1).values(:,2)=2*cos(t);
    var_single_struct.signals(2).values=sin(2*t);
    var_single_struct.signals(3).values(:,1)=0.5*sin(3*t);
    var_single_struct.signals(3).values(:,2)=0.5*cos(3*t);
    v=[var_single_struct.signals(1).values...
    var_single_struct.signals(2).values...
    var_single_struct.signals(3).values];

    Per-port structure

    t=[0:0.1:2*pi]';
    Inb1.time=t;
    Inb1.signals.values(:,1)=2*sin(t);
    Inb1.signals.values(:,2)=2*cos(t);
    t=[0:0.2:2*pi]';
    Inb2.time=t;
    Inb2.signals.values(:,1)=sin(2*t);
    t=[0:0.1:2*pi]';
    Inb3.time=t;
    Inb3.signals.values(:,1)=0.5*sin(3*t);
    Inb3.signals.values(:,2)=0.5*cos(3*t);
    
  5. Save the workspace variables to a MAT-file.

    Single time/data matrix

    The following example saves the workspace variable var_matrix to the MAT-file rsim_i_matrix.mat.

    save rsim_i_matrix.mat var_matrix;

    Signal-and-time structure

    The following example saves the workspace structure variable var_single_struct to the MAT-file rsim_i_single_struct.mat.

    save rsim_i_single_struct.mat var_single_struct;

    Per-port structure

    To ensure correct ordering of data when saving per-port structure variables to a single MAT-file, use the save command's -append option. Be sure to append the data in the order that the model expects it.

    The following example saves the workspace variables Inb1, Inb2, and Inb3 to MAT-file rsim_i_multi_struct.mat.

    save rsim_i_multi_struct.mat Inb1;
    save rsim_i_multi_struct.mat Inb2 -append;
    save rsim_i_multi_struct.mat Inb3 -append;

    The save command does not preserve the order in which you specify your workspace variables in the command line when saving data to a MAT-file. For example, if you specify the variables v1, v2, and v3, in that order, the order of the variables in the MAT-file could be v2 v1 v3.

    Using a command-line option, you can then specify the MAT-files as input for rapid simulations.

  


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