This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

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.

The parameters gain2 and gain3 have been specified as tunable parameters so that they can be modified later using the utility function Simulink.BlockDiagram.modifyTunableParameters. To learn how to select tunable parameters and set their properties graphically, read the help page concerning the Model Parameter Configuration Dialog Box.

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 build 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_parsim_paramsweep_suspn_raccel_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.

The utility function Simulink.BlockDiagram.modifyTunableParameters is a convenient way to build the rtp structure with different parameter 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.

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));
[06-Feb-2018 21:34:34] Checking for availability of parallel pool...
Starting parallel pool (parpool) using the 'local' profile ...
connected to 12 workers.
[06-Feb-2018 21:34:56] Loading Simulink on parallel workers...
Analyzing and transferring files to the workers ...done.
[06-Feb-2018 21:35:20] Configuring simulation cache folder on parallel workers...
[06-Feb-2018 21:35:21] Running SetupFcn on parallel workers...
[06-Feb-2018 21:35:58] Loading model on parallel workers...
[06-Feb-2018 21:35:58] Running simulations...
[06-Feb-2018 21:36:04] Completed 1 of 8 simulation runs
[06-Feb-2018 21:36:04] Completed 2 of 8 simulation runs
[06-Feb-2018 21:36:04] Completed 3 of 8 simulation runs
[06-Feb-2018 21:36:04] Completed 4 of 8 simulation runs
[06-Feb-2018 21:36:04] Completed 5 of 8 simulation runs
[06-Feb-2018 21:36:04] Completed 6 of 8 simulation runs
[06-Feb-2018 21:36:04] Completed 7 of 8 simulation runs
[06-Feb-2018 21:36:04] Completed 8 of 8 simulation runs
[06-Feb-2018 21:36:04] 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 acan be accessed from the SimulationOuput 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.
Was this topic helpful?