MATLAB Examples

Rapid Accelerator Simulations Using Parsim

This example shows the use of Rapid Accelerator in applications that require running parallel simulations for a range of input and parameter values.

We use the engine idle speed model which simulates the idle speed of an engine. The input of this model is the voltage of the bypass air valve and the output is the idle speed.

We run parallel simulations using parsim with two sets of valve voltages and by independently varying two of the three gain parameters of the transfer functions over a range of two values. Hence, in total, we will be running eight different sets of simulations.

It is easy to customize this example for your own application by modifying the script file used to build this example. Click the link in the top left corner of this page to edit the script file. Click the link in the top right corner to run this example from MATLAB®.


Step 1: Preparation

First we open the model where the simulation mode has been set to Rapid Accelerator. The default input data and the required parameters are preloaded in the models workspace.

We copy the default input and time data to a variable so that we can later modify them and pass them to the SIM command.

Open model:

mdl = 'sldemo_raccel_engine_idle_speed';

% Copy input data
inpData = evalin('base', 'inpData');
tData = evalin('base', 'time');

Step 2: Setup to Build the Rapid Accelerator Target

The Rapid Accelerator executable for the model is built in the SetupFcn call using the buildRapidAcceleratorTarget function from the Simulink.BlockDiagram package. The buildRapidAcceleratorTarget function returns the default run-time parameter set which is assigned to a global variable, RTP, in the SetupFcn and is used in the next step to modify the parameter values. Open sldemo_parallel_rapid_accel_sims_script_setup in the editor to look at the code. Note that the build process is optimized so that if the build files are already present and compatible with the model and machine architecture then it returns early.

Step 4: Create Input Sets

Here we perturb the default input values vector to obtain a new input values vector.

In this example, we will be plotting the engine idle speed as a function of the valve voltage for different parameter values.

inpSets{1} = inpData;
rndPertb = 0.5 + rand(length(tData), 1);
inpSets{2} = inpSets{1}.*rndPertb;
numInpSets  = length(inpSets);

Step 3: Create Parameter Sets

Using the default rtp structure from step 2, we build a new structure with different values for the tunable variables in the model. We want to see how the idle speed changes for different values of parameters gain2 and gain3. Therefore, we generate different parameter sets with different values of gain2 and gain3 and leave the other tunable variables at their default values.

Create an array of Simulink.SimulationInput objects to specify the different parameter values and external input for each simulation. The array of SimulationInput objects is preallocated for better performance. Also note that the external input can be directly specified on the SimulationInput object instead of using a model parameter.

The utility function Simulink.BlockDiagram.modifyTunableParameters is a convenient way to build the rtp structure with different parameter values. Use the PreSimFcn, sldemo_parallel_rapid_accel_sims_script_presim on the SimulationInput object to access the RTP and create parameter sets.

gain2_vals = 25:10:35;
gain3_vals = 20:10:30;

num_gain2_vals = length(gain2_vals);
num_gain3_vals = length(gain3_vals);

numSims = num_gain2_vals*num_gain3_vals*numInpSets;
in(numSims) = Simulink.SimulationInput(mdl);

idx = 1;
for iG2 = 1:num_gain2_vals
    for iG3 = 1:num_gain3_vals
        for inpSetsIdx = 1:numInpSets
            in(idx).ModelName = mdl;
            in(idx) = in(idx).setModelParameter('SimulationMode', 'rapid', ...
                'RapidAcceleratorUpToDateCheck', 'off', ...
                'SaveTime', 'on', ...
                'SaveOutput', 'on');
            in(idx).PreSimFcn = @(x) sldemo_parallel_rapid_accel_sims_script_presim(x, gain2_vals(iG2), gain3_vals(iG3));
            in(idx).ExternalInput = [tData, inpSets{inpSetsIdx}];
            idx = idx + 1;

Step 4: Execute Simulations

Use the parsim function to execute the simulations in parallel. The array of SimulationInput objects, in, created in the last step is passed into the parsim function as the first argument. Store the simulation output data in a variable, out, whose value is an array of Simulink.SimulationOutput objects. Each SimulationOutput object contains the logged signal along with the SimulationMetadata. When running multiple simulations using parsim, errors are captured so that subsequent simulations can continue to run. Any errors would show up in the ErrorMessage property of the SimulationOutput object.

out = parsim(in, 'ShowProgress', 'on', ...
    'SetupFcn', @() sldemo_parallel_rapid_accel_sims_script_setup(mdl));
[28-Feb-2018 11:00:04] Checking for availability of parallel pool...
[28-Feb-2018 11:00:04] Loading Simulink on parallel workers...
Analyzing and transferring files to the workers ...done.
[28-Feb-2018 11:00:04] Configuring simulation cache folder on parallel workers...
[28-Feb-2018 11:00:04] Running SetupFcn on parallel workers...
[28-Feb-2018 11:00:33] Loading model on parallel workers...
[28-Feb-2018 11:00:38] Running simulations...
[28-Feb-2018 11:00:41] Completed 1 of 8 simulation runs
[28-Feb-2018 11:00:41] Completed 2 of 8 simulation runs
[28-Feb-2018 11:00:42] Completed 3 of 8 simulation runs
[28-Feb-2018 11:00:42] Completed 4 of 8 simulation runs
[28-Feb-2018 11:00:42] Completed 5 of 8 simulation runs
[28-Feb-2018 11:00:42] Completed 6 of 8 simulation runs
[28-Feb-2018 11:00:43] Completed 7 of 8 simulation runs
[28-Feb-2018 11:00:43] Completed 8 of 8 simulation runs
[28-Feb-2018 11:00:44] Cleaning up parallel workers...

Step 5: Plot Results

Plot the engine idle speed with respect to time for different parameter values and inputs. The output is logged in the array format and can be accessed from the SimulationOutput object.

for i = 1:numSims
        simOut = out(i);
        t = simOut.tout;
        y = simOut.yout;
        plot(t, y)
        hold all

Step 6: Close MATLAB Workers

Parallel pool using the 'local' profile is shutting down.