accelerate, refine, and test hybrid dynamic system on host computer by using rsim system target file -凯发k8网页登录

main content

accelerate, refine, and test hybrid dynamic system on host computer by using rsim system target file

after you create a model, you can use the rapid simulation (rsim) system target file to characterize model behavior. the executable program that results from the build process is for non-real-time execution on your development computer. the executable program is highly optimized for simulating models of hybrid dynamic systems, including models that use variable-step solvers and zero-crossing detection. the speed of the generated code makes the rsim system target file ideal for building programs for batch or monte carlo simulations.

about rapid simulation

use the rsim target to generate an executable that runs fast, standalone simulations. you can repeat simulations with varying data sets, interactively or programmatically with scripts, without rebuilding the model. this can accelerate the characterization and tuning of model behavior and code generation testing.

using command-line options:

  • define parameter values and input signals in one or more mat-files that you can load and reload at the start of simulations without rebuilding your model.

  • redirect logging data to one or more mat-files that you can then analyze and compare.

  • control simulation time.

  • specify external mode options.

note

to run an rsim executable, configure your computer to run matlab® and have the matlab and simulink® installation folders accessible. to deploy a standalone host executable (that is, without matlab and simulink installed), consider using the host-based shared library target (ert_shrlib).

rapid simulation advantage

the advantage that you gain from rapid simulation varies. larger simulations achieve speed improvements of up to 10 times faster than standard simulink simulations. some models might not show noticeable improvement in simulation speed. to determine the speed difference for your model, time your standard simulink simulation and compare the results with a rapid simulation. in addition, test the model simulation in rapid accelerator simulation mode.

general rapid simulation workflow

like other stages of model-based design, characterization and tuning of model behavior is an iterative process, as shown in the general workflow diagram in the figure. tasks in the workflow are:

rapid simulation workflow

identify rapid simulation requirements

the first step to setting up a rapid simulation is to identify your simulation requirements.

question...for more information, see...
how long do you want simulations to run? configure and build model for rapid simulation
are there solver requirements? do you expect to use the same solver for which the model is configured for your rapid simulations?configure and build model for rapid simulation
do your rapid simulations need to accommodate flexible custom code interfacing? or, do your simulations need to retain storage class settings?configure and build model for rapid simulation
will you be running simulations with multiple data sets?set up rapid simulation input data
will the input data consist of global parameters, signals, or both?set up rapid simulation input data
what type of source blocks provide input data to the model — from file, inport, from workspace? set up rapid simulation input data
will the model's parameter vector (model_p) be used as input data?create a mat-file that includes a model parameter structure
what is the data type of the input parameters and signals?set up rapid simulation input data
will the source data consist of one variable or multiple variables?set up rapid simulation input data
does the input data include tunable parameters?create a mat-file that includes a model parameter structure
do you need to gain access to tunable parameter information — model checksum and parameter data types, identifiers, and complexity?create a mat-file that includes a model parameter structure
will you have a need to vary the simulation stop time for simulation runs?configure and build model for rapid simulation andoverride a model simulation stop time
do you want to set a time limit for the simulation? consider setting a time limit if your model experiences frequent zero crossings and has a small minor step size.set a clock time limit for a rapid simulation
do you need to preserve the output of each simulation run? specify a new output file name for a simulation and specify new output file names for to file blocks
do you expect to run the simulations interactively or in batch mode?scripts for batch and monte carlo simulations

configure inports to provide simulation source data

you can use inport blocks as a source of input data for rapid simulations. to do so, configure the blocks so that they can import data from external mat-files. by default, the inport block inherits parameter settings from downstream blocks. in most cases, to import data from an external mat-file, you must explicitly set these block parameters to match the source data in the mat-file.

  • main > interpolate data

  • signal attributes > port dimensions

  • signal attributes > data type

  • signal attributes > signal type

if you do not have control over the model content, you might need to modify the data in the mat-file to conform to what the model expects for input. input data characteristics and specifications of the inport block that receives the data must match.

for details on adjusting these parameters and on creating a mat-file for use with an inport block, see create a mat-file for an inport block. for descriptions of the preceding block parameters, see the block description of .

configure and build model for rapid simulation

after you identify your rapid simulation requirements, configure the model for rapid simulation.

  1. set model configuration parameter system target file to rsim.tlc (rapid simulation target).

  2. click rsim target to view the rsim target pane.

    rsim target pane in model configuration parameters dialog box

  3. set the rsim target configuration parameters for your rapid simulation requirements.

    if you want to... then...
    generate code that allows the rsim executable to load parameters from a mat-fileselect enable rsim executable to load parameters from a mat-file (default).
    let the target choose a solver based on the solver already configured for the model

    set solver selection to auto (default). the code generator uses a built-in solver if a fixed-step solver is specified on the solver pane or calls the simulink solver module (a shared library) if a variable-step solver is specified.

    explicitly instruct the target to use a fixed-step solverset solver selection to use fixed-step solvers. in the configuration parameters dialog box, on the solver pane, specify a fixed-step solver.
    explicitly instruct the target to use a variable-step solver

    set solver selection to use simulink solver module. in the configuration parameters dialog box, on the solver pane, specify a variable-step solver.

    force storage classes to auto for flexible custom code interfacingselect force storage classes to auto (default).
    retain storage class settings, such as exportedglobal or importedextern, due to application requirementsclear force storage classes to auto.
  4. set up data import and export options. on the data import/export pane, in the save to workspace section, select the time, states, outputs, and final states parameters, as they apply. by default, the code generator saves simulation logging results to a file named model.mat. for more information, see .

  5. if you are using external mode communication, set up the interface, using the code generation > interface pane. see for details.

  6. press ctrl b. the code generator builds a highly optimized executable program that you can run on your development computer with varying data, without rebuilding.

    for more information on compilers that are compatible with the simulink coder™ product, see and .

set up rapid simulation input data

about rapid simulation data setup

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 parameterscall the rsimgetrtp function to get the 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 a from file block or inport block exists in your model.

create a mat-file that includes a model parameter structure

to create a mat-file that includes a model 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.

get 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')
argumentdescription
modelthe model for which you are running the rapid simulations.

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 code generator 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 does not match the original checksum anymore. 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.

the parameter structure contains the following information.

fielddescription
datatypenamethe name of the parameter data type, for example, double
datatypeidinternal data type identifier used by the code generator
complexthe value 0 if real; 1 if complex
dttransidxinternal data index used by the code generator
valuesa vector of the parameter values associated with this structure
mapthis field contains the mapping information that correlates the 'values' to the tunable parameters of the model. this mapping information, in conjunction with rsimsetrtpparam, is useful for creating subsequent rtp structures without compiling the block diagram.

the code generator 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 example model 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]

save 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 run rapid simulations.

convert the parameter structure for running simulations on varying data sets.  to use rapid simulations to test changes to specific parameters, you can convert the model parameter structure to a cell array. you can then access a specific parameter set by using the @ operator to specify the index for a specific parameter set in the file.

to convert the structure to a cell array:

  1. use the function rsimgetrtp to get a structure containing parameter information for the example model rtwdemo_rsimtf. store the structure in a variable param_struct.

    param_struct = rsimgetrtp('rtwdemo_rsimtf');
    

    the parameters field of the structure is a substructure that contains parameter information. the values field of the parameters substructure contains the numeric values of the parameters that you can tune during execution of the simulation code.

  2. use the function rsimsetrtpparam to expand the structure so that it contains more parameter sets. in this case, create two more parameter sets (for a total of three sets).

    param_struct = rsimsetrtpparam(param_struct,3);

    the function converts the parameters field to a cell array with three elements. each element contains information for a single parameter set. by default, the function creates the second and third elements of the cell array by copying the first element. therefore, all of the parameter sets use the same parameter values.

  3. specify new values for the parameters in the second and third parameter sets.

    param_struct.parameters{2}.values = [-150 -5000 0 4950];
    param_struct.parameters{3}.values = [-170 -5500 0 5100];
  4. save the structure containing the parameter set information to a mat-file.

    save rtwdemo_rsimtf.mat param_struct;

alternatively, you can modify the block parameters in the model, and use rsimgetrtp to create multiple parameter sets:

  1. use the function rsimgetrtp to get a structure containing parameter information for the example model rtwdemo_rsimtf. store the structure in a variable param_struct.

    param_struct = rsimgetrtp('rtwdemo_rsimtf');
    
  2. use the function rsimsetrtpparam to expand the structure so that it contains more parameter sets. in this case, create two more parameter sets (for a total of three sets).

    param_struct = rsimsetrtpparam(param_struct,3);

    the function converts the parameters field to a cell array with three elements. each element contains information for a single parameter set. by default, the function creates the second and third elements of the cell array by copying the first element. therefore, all of the parameter sets use the same parameter values.

  3. change the values of block parameters or workspace variables. for example, change the value of the variable w from 70 to 72.

    w = 72;
  4. use rimsgetrtp to get another structure containing parameter information. store the structure in a temporary variable rtp_temp.

    rtp_temp = rsimgetrtp('rtwdemo_rsimtf');
  5. assign the value of the parameters field of rtp_temp to the structure param_struct as a second parameter set.

    param_struct.parameters{2} = rtp_temp.parameters;
  6. change the value of the variable w from 72 to 75.

    w = 75;
  7. use rimsgetrtp to get another structure containing parameter information. then, assign the value of the parameters field to param_struct as a third parameter set.

    rtp_temp = rsimgetrtp('rtwdemo_rsimtf');
    param_struct.parameters{3} = rtp_temp.parameters;
  8. save the structure containing the parameter set information to a mat-file.

    save rtwdemo_rsimtf.mat param_struct;

for more information on how to specify each parameter set when you run the simulations, see change block parameters for an rsim simulation.

create a mat-file for a from file block

you can use a mat-file as the input data source for a block. the format of the data in the mat-file must match the data format expected by that block. for example, if you are using a matrix as an input for the mat file, this cannot be different from the matrix size for the executable.

to create a mat-file for a from file block:

  1. for array format data, in the workspace create a matrix 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 other supported data types, such as int16 or fixed-point, the time data points must be of type double, just as for array format data. however, the sample data can be of any dimension.

    for more information on setting up the input data, see the block description of .

  2. save the matrix to a mat-file.

    the following example saves the matrix var_matrix to the mat-file myrsimdemo.mat in version 7.3 format.

    save '-v7.3' myrsimdemo.mat var_matrix;

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

create 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 the following procedure, step 4. for more information, see .

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 block interpolate data parameter.

  • data type must match inport block settings.

for an example, see signal-and-time structure in the following procedure, step 4. for more information on this format, see .

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 data structure has only one signals field. to use this format, in the input text field, enter the names of the structures 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 block interpolate data parameter.

  • data type 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 the following procedure, step 4. for more information, see .

the supported formats and the following procedure are illustrated in .

to create a mat-file for an inport block:

  1. choose one of the preceding data file formats.

  2. update inport block parameter settings and specifications to match specifications of the data to be supplied by the mat-file.

    by default, the inport block inherits parameter settings from downstream blocks. to import data from an external mat-file, explicitly set the following parameters to match the source data in the mat-file.

    • main > interpolate data

    • signal attributes > port dimensions

    • signal attributes > data type

    • signal attributes > signal type

    if you choose to use a structure format for workspace variables and the time field is empty,clear block parameter interpolate data or modify the parameter so that it is set to a nonempty value. interpolation requires time data.

    for descriptions of the preceding block parameters, see the block description of .

  3. build an rsim executable program for the model. the 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 the model executable expects.

  4. create the mat-file that provides the source data for the rapid simulations. you can create the mat-file from a workspace variable. using the specifications in the preceding format comparison table, 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 order 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.

scripts for batch and monte carlo simulations

the rsim target is for batch simulations in which parameters and input signals vary for multiple simulations. new output file names allow you to run new simulations without overwriting prior simulation results. you can set up a series of simulations to run by creating a .bat file for use on a microsoft® windows® platform.

create a file for the windows platform with a text editor and execute it by typing the file name, for example, mybatch, where the name of the text file is mybatch.bat.

rtwdemo_rsimtf -f rtwdemo_rsimtf.mat=run1.mat -o results1.mat -tf 10.0
rtwdemo_rsimtf -f rtwdemo_rsimtf.mat=run2.mat -o results2.mat -tf 10.0
rtwdemo_rsimtf -f rtwdemo_rsimtf.mat=run3.mat -o results3.mat -tf 10.0
rtwdemo_rsimtf -f rtwdemo_rsimtf.mat=run4.mat -o results4.mat -tf 10.0

in this case, batch simulations run using four sets of input data in files run1.mat, run2.mat, and so on. the rsim executable saves the data to the files specified with the -o option.

the variable names containing simulation results in each of the files are identical. therefore, loading consecutive sets of data without renaming the data once it is in the matlab workspace results in overwriting the prior workspace variable with new data. to avoid overwriting, you can copy the result to a new matlab variable before loading the next set of data.

you can also write matlab scripts to create new signals and new parameter structures, as well as to save data and perform batch runs using the bang command (!).

for details on running simulations and available command-line options, see run rapid simulations. for an example of a rapid simulation batch script, see the example .

run rapid simulations

rapid simulations

using the rsim target, you can build a model once and run multiple simulations to study effects of varying parameter settings and input signals. you can run a simulation directly from your operating system command line, redirect the command from the matlab command line by using the bang (!) character, or execute commands from a script.

from the operating system command line, use

rtwdemo_rsimtf

from the matlab command line, use

!rtwdemo_rsimtf

the following table lists ways you can use rsim target command-line options to control a simulation.

to...use...
read input data for a from file block from a mat-file other than the mat-file used for the previous simulation

model -f oldfilename.mat=newfilename.mat

print a summary of the options for rsim executable targetsexecutable filename -h
read input data for an inport block from a mat-file model -i filename.mat
time out after n clock time seconds, where n is a positive integer value model -l n
write mat-file logging data to file filename.mat model -o filename.mat
read a parameter vector from file filename.mat model -p filename.mat
override the default tcp port (17725) for external mode model -port tcpport
write mat-file logging data to a mat-file other than the mat-file used for the previous simulation model -t oldfilename.mat=newfilename.mat
run the simulation until the time value stoptime is reached model -tf stoptime
run in verbose mode model -v
wait for the simulink engine to start the model in external mode model -w

the following sections use the example model in examples to illustrate some of these command-line options. in each case, the example assumes you have already done the following:

  • created or changed to a working folder.

  • opened the example model.

  • copied the data file matlabroot/toolbox/rtw/rtwdemos/rsimdemos/rsim_tfdata.mat to your working folder. you can perform this operation using the command:

    copyfile(fullfile(matlabroot,'toolbox','rtw','rtwdemos',...
    'rsimdemos','rsim_tfdata.mat'),pwd);

requirements for running rapid simulations

the following requirements apply to both fixed and variable step executables.

  • you must run the rsim executable on a computer configured to run matlab. also, the rsim.exe file must be able to access the matlab and simulink installation folders on this machine. to obtain that access, your path environment variable must include /bin and /bin/($arch), where ($arch) represents your operating system architecture. for example, for a personal computer running on a windows platform, ($arch) is “win64”, whereas for a linux machine, ($arch) is “glnxa64”.

  • on gnu® linux® platforms, to run an rsim executable, define the ld_library_path environment variable to provide the path to the matlab installation folder, as follows:

    % setenv ld_library_path /matlab/sys/os/glnx64:$ld_library_path
  • on the apple macintosh os x platform, to run rsim target executables, you must define the environment variable dyld_library_path to include the folders bin/mac and sys/os/mac under the matlab installation folder. for example, if your matlab installation is under /matlab, add /matlab/bin/mac and /matlab/sys/os/mac to the definition for dyld_library_path.

set a clock time limit for a rapid simulation

if a model experiences frequent zero crossings and the model's minor step size is small, consider setting a time limit for a rapid simulation. to set a time limit, specify the -l option with a positive integer value. the simulation aborts after running for the specified amount of clock time (not simulation time). for example,

  !rtwdemo_rsimtf -l 20

based on your clock, after the executable runs for 20 seconds, the program terminates. you see a message similar to one of the following:

  • on a microsoft windows platform,

    exiting program, time limit exceeded
    logging available data ...
  • on the open group unix® platform,

    ** received sigalrm (alarm) signal @ fri jul 25 15:43:23 2003
    ** exiting model  'rtwdemo_rsimtf' @ fri jul 25 15:43:23 2003

you do not need to do anything to your model or to its configuration to use this option.

override a model simulation stop time

by default, a rapid simulation runs until the simulation time reaches the time specified the configuration parameters dialog box, on the solver pane. you can override the model simulation stop time by using the -tf option. for example, the following simulation runs until the time reaches 6.0 seconds.

!rtwdemo_rsimtf -tf 6.0

the rsim target stops and logs output data using mat-file data logging rules.

if the model includes a from file block, the end of the simulation is regulated by the stop time setting specified in the configuration parameters dialog box, on the solver pane, or with the rsim target option -tf. the values in the block time vector are ignored. however, if the simulation time exceeds the endpoints of the time and signal matrix (if the final time is greater than the final time value of the data matrix), the signal data is extrapolated to the final time value.

read the parameter vector into a rapid simulation

to read the model parameter vector into a rapid simulation, you must first create a mat-file that includes the parameter structure as described in create a mat-file that includes a model parameter structure. you can then specify the mat-file in the command line with the -p option.

for example:

  1. build an rsim executable for the example model .

  2. modify parameters in your model and save the parameter structure.

    param_struct = rsimgetrtp('rtwdemo_rsimtf');
    save myrsimdata.mat param_struct
  3. run the executable with the new parameter set.

    !rtwdemo_rsimtf -p myrsimdata.mat
    
    ** starting model 'rtwdemo_rsimtf' @ tue dec 27 12:30:16 2005 
    ** created rtwdemo_rsimtf.mat **
  4. load workspace variables and plot the simulation results by entering the following commands:

    load rtwdemo_rsimf.mat
    plot(rt_yout)

specify new signal data file for a from file block

if your model's input data source is a block, you can feed the block with input data during simulation from a single mat-file or you can change the mat-file from one simulation to the next. each mat-file must adhere to the format described in create a mat-file for a from file block.

to change the mat-file after an initial simulation, you specify the executable with the -f option and an oldfile.mat=newfile.mat parameter, as shown in the following example.

  1. set some parameters in the matlab workspace. for example:

    w = 100;
    theta = 0.5;
  2. build an rsim executable for the example model .

  3. run the executable.

    !rtwdemo_rsimtf

    the rsim executable runs a set of simulations and creates output mat-files containing the specific simulation result.

  4. load the workspace variables and plot the simulation results by entering the following commands:

    load rtwdemo_rsimtf.mat
    plot(rt_yout)

    the resulting plot shows simulation results based on default input data.

    plot that shows simulation results based on default input data

  5. create a new data file, newfrom.mat, that includes the following data:

    t=[0:.001:1];
    u=sin(100*t.*t);
    tu=[t;u];
    save newfrom.mat tu;
  6. run a rapid simulation with the new data by using the -f option to replace the original file, rsim_tfdata.mat, with newfrom.mat.

    !rtwdemo_rsimtf -f rsim_tfdata.mat=newfrom.mat
  7. load the data and plot the new results by entering the following commands:

    load rtwdemo_rsimtf.mat
    plot(rt_yout)

    the next figure shows the resulting plot.

    plot that shows results based on loaded data

from file blocks require input data of type double. if you need to import signal data of a data type other than double, use an inport block (see create a mat-file for an inport block) or a from workspace block with the data specified as a structure.

workspace data must be in the format:

variable.time
variable.signals.values

if you have more than one signal, use the following format:

variable.time
variable.signals(1).values
variable.signals(2).values

specify signal data file for an inport block

if your model's input data source is an inport block, you can feed the block with input data during simulation from a single mat-file or you can change the mat-file from one simulation to the next. each mat-file must adhere to one of the three formats described in create a mat-file for an inport block.

to specify the mat-file after a simulation, you specify the executable with the -i option and the name of the mat-file that contains the input data. for example:

  1. open the model .

  2. check the inport block parameter settings. the following inport block data parameter settings and specifications that you specify for the workspace variables must match settings in the mat-file, as indicated in configure inports to provide simulation source data:

    • main > interpolate data

    • signal attributes > port dimensions

    • signal attributes > data type

    • signal attributes > signal type

  3. build the model.

  4. set up the input signals. for example:

    t=[0:0.01:2*pi]';
    s1=[2*sin(t) 2*cos(t)];
    s2=sin(2*t);
    s3=[0.5*sin(3*t) 0.5*cos(3*t)];
    plot(t, [s1 s2 s3])

    plot that shows input signals

  5. prepare the mat-file by using one of the three available file formats described in create a mat-file for an inport block. the following example defines a signal-and-time structure in the workspace and names it var_single_struct.

    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];
  6. save the workspace variable var_single_struct to mat-file rsim_i_single_struct.

    save rsim_i_single_struct.mat var_single_struct;
  7. run a rapid simulation with the input data by using the -i option. load and plot the results.

    !rtwdemo_rsim_i -i rsim_i_single_struct.mat
    
    ** starting model 'rtwdemo_rsim_i' @ tue aug 19 10:26:53 2014 
     *** rsim_i_single_struct.mat is successfully loaded! *** 
    ** created rtwdemo_rsim_i.mat ** 
     
    ** execution time = 0.02024185130718954s
  8. load and plot the results.

    load rtwdemo_rsim_i.mat
    plot(rt_tout, rt_yout);

    plot that shows loaded results

change block parameters for an rsim simulation

as described in create a mat-file that includes a model parameter structure, after you alter one or more parameters in a simulink block diagram, you can extract the parameter vector, model_p, for the entire model. you can then save the parameter vector, along with a model checksum, to a mat-file. this mat-file can be read directly by the standalone rsim executable, allowing you to replace the entire parameter vector or individual parameter values, for running studies of variations of parameter values representing coefficients, new data for input signals, and so on.

rsim can read the mat-file and replace the entire model_p structure whenever you change one or more parameters, without recompiling the entire model.

for example, assume that you changed one or more parameters in your model, generated the new model_p vector, and saved model_p to a new mat-file called mymatfile.mat. to run the same rtwdemo_rsimtf model and use these new parameter values, use the -p option, as shown in the following example:

!rtwdemo_rsimtf -p mymatfile.mat
load rtwdemo_rsimtf 
plot(rt_yout)

if you have converted the parameter structure to a cell array for running simulations on varying data sets, as described in convert the parameter structure for running simulations on varying data sets, you must add an @n suffix to the mat-file specification. n is the element of the cell array that contains the specific input that you want to use for the simulation.

the following example converts param_struct to a cell array, changes parameter values, saves the changes to mat-file mymatfile.mat, and then runs the executable using the parameter values in the second element of the cell array as input.

param_struct = rsimgetrtp('rtwdemo_rsimtf');
param_struct = rsimsetrtpparam(param_struct,2);
param_struct.parameters{1}
ans = 
       datatypename: 'double'
         datatypeid: 0
            complex: 0
         dttransidx: 0
             values: [-140 -4900 0 4900]
                map: []
    structparaminfo: []
param_struct.parameters{2}.values=[-150 -5000 0 4950];
save mymatfile.mat param_struct;
!rtwdemo_rsimtf -p mymatfile.mat@2 -o rsim2.mat

specify a new output file name for a simulation

if you have specified one or more of the save to workspace parameters — time, states, outputs, or final states — in the configuration parameters dialog box, on the data import/export pane, the default is to save simulation logging results to the file model.mat. for example, the example model rtwdemo_rsimtf normally saves data to rtwdemo_rsimtf.mat, as follows:

!rtwdemo_rsimtf
created rtwdemo_rsimtf.mat

you can specify a new output file name for data logging by using the -o option when you run an executable.

!rtwdemo_rsimtf -o rsim1.mat

in this case, the set of parameters provided at the time of code generation, including from file block data parameters, is run.

specify new output file names for to file blocks

in much the same way as you can specify a new system output file name, you can also provide new output file names for data saved from one or more to file blocks. to do this, specify the original file name at the time of code generation with a new name, as shown in the following example:

!rtwdemo_rsimtf -t rtwdemo_rsimtf_data.mat=mynewrsimdata.mat

in this case, assume that the original model wrote data to the output file rtwdemo_rsimtf_data.mat. specifying a new file name forces rsim to write to the file mynewrsimdata.mat. with this technique, you can avoid overwriting an existing simulation run.

tune parameters interactively during rapid simulation

the rsim system target file was designed to let you run batch simulations at the fastest possible speed. using variable-step or fixed-step solvers with that system target file and a tunable parameter data structure, whether you set default parameter behavior to tunable or to inlined, you can create multiple parameter sets. you can run the data sets with the generated executable program (.exe on windows). each invocation of the executable program allows specification of the file name to use for results.

open the example model rsimparamtuning.

open_system('rsimparamtuning.slx'); 

for this model, default parameter behavior is set to inlined. the model declares workspace variables as tunable parameters. to use the rsim system target file with default parameter behavior set to tunable, and without explicitly declaring tunable parameters, see .

this model uses the rsim system target file and the rsimgetrtp function to allow a non real time executable to be passed new data without the need to recompile the simulink model. this feature allows you to get a map of the tunable parameters declared in a model and save it in a mat-file. you can then create your own matlab graphical user interface (gui) or a standalone gui (independent of matlab) to read and write the mat-file and rerun the executable program to produce new output files.

in the model, double-click the buttons sequentially to run the example.

to review the code used to create the matlab and standalone guis, double-click the view matlab programs button.

for more information, see .

rapid simulation target limitations

the rsim target has the following limitations:

  • does not support algebraic loops.

  • does not support blocks.

  • does not support noninlined matlab language or fortran s-functions.

  • if an rsim build includes referenced models (by using model blocks), set up these models to use fixed-step solvers to generate code for them. the top model, however, can use a variable-step solver as long as the blocks in the referenced models are discrete.

  • in certain cases, changing block parameters can result in structural changes to your model that change the model checksum. an example of such a change is changing the number of delays in a dsp simulation. in such cases, you must regenerate the code for the model.

related topics

    网站地图