Main Content

Run Batch Simulations Without Recompiling Generated Code

You can run batch simulations without recompiling the generated code. The example modifies input signal data and model parameters by reading data from a MAT-file. In the first part (steps 1-5), ten parameter sets are created from the model by changing the transfer function damping factor. The ten parameter sets are saved to a MAT-file, and the RSim executable reads the specified parameter set from the file. In the second part (step 6-7) of this example, five sets of signal data chirps are created with increasingly high frequencies. In both parts, the RSim executable runs the set of simulations and creates output MAT-files containing the specific simulation result. Finally, a composite of runs appears in a MATLAB® figure.

To quickly run multiple simulations in the Simulink environment, consider using rapid accelerator instead of RSim. See What Is Acceleration?.

  1. Open your model and configure it to use the RSim target. For more information on doing this graphically and setting up other RSim target related options, see Configure a System Target File.

  2. Build the RSim executable program for the model. During the build process, a structural checksum is calculated for the model and embedded into the generated executable program. This checksum is used to check that a parameter set passed to the executable is compatible with it.

  3. Get the default parameter set and create parameter sets. For example:

    for i=1:10
      % Extract current rtP structure using new damping factor.
      [rtpstruct]=evalin('base','rsimgetrtp(''rapidsim'');');
      savestr = strcat('save params',num2str(i),'.mat rtpstruct');
      eval(savestr);
      evalin('base','theta = theta - .1;');
    end
  4. Run RSim simulations, using the new parameter sets, and plot the results. For example:

    for i=1:10
      % Bang out and run a simulation using new parameter data
      runstr = ['.', filesep, 'rapidsim -p params',num2str(i),'.mat', ' -v'];
      [status, result] = system(runstr);
      if status ~= 0, error(result); end
      % Load simulation data into MATLAB for plotting.
      load rapidsim.mat;
      axis([0 1 0 2]);
      plot(rt_tout, rt_yout)
      hold on
    end
  5. Set up a time vector and an initial frequency vector. This example code sets up a time vector that has 4096 points in the event to do windowing and spectral analysis on of the simulation results.

    dt = .001;
    nn = [0:1:4095];
    t = dt*nn; [m,n] = size(t);
    wlo = 1; whi = 4;
    omega = [wlo:((whi-wlo)/n):whi - (whi-wlo)/n];
  6. Create sets of signal data in MAT files. This example code shows how to create MAT files with chirp data for a sequence of five plots. Each plot shows an input chirp signal of certain frequency range.

    for i = 1:5
      wlo = whi; whi = 3*whi; % keep increasing frequencies
      omega = [wlo:((whi-wlo)/n):whi - (whi-wlo)/n];
      % In a real application we recommend shaping the chirp using
      % a windowing function (hamming or hanning window, etc.)
      % This example does not use a windowing function.
      u      = sin(omega.*t);
      tudata = [t;u];
      % At each pass, save one more set of tudata to the next
      % .mat file.
      savestr = strcat('save sweep',num2str(i),'.mat tudata');
      eval(savestr);
      % Display each chirp. Note that this is only input data.
      % Simulations have not been run yet.
      plotstr = strcat('subplot(5,1,',num2str(i),');');
      eval(plotstr);
      plot(t,u)
      pause(0.3)
    end
  7. Run the RSim compiled simulation using new signal data. Replace the original signal data in other MAT files. For example:

    for i = 1:5
      % Bang out and run the next set of data with RSim
      runstr = ['.', filesep, 'rapidsim -f rapidsim.mat=sweep', ...
                num2str(i),'.mat -v -tf 4.096'];
      [status, result] = system(runstr);
      if status ~= 0, error(result); end
      % Load the data to MATLAB and plot the results.
      load rapidsim.mat
      plotstr = strcat('subplot(5,1,',num2str(i),');');
      eval(plotstr);
      plot(rt_tout, rt_yout); axis([0 4.1 -3 3]);
    end
    zoom on
    % cleanup
    evalin('base','clear w theta')
    close_system(mdlName, 0);