Parallel Simulations Using Parfor: Parameter Sweep in Normal Mode

This example shows how you can run multiple Simulink® simulations of a Monte Carlo study in parallel by using Parallel Computing Toolbox™. This involves five main steps:

  1. Start MATLAB® workers on your machine

  2. Set up data required for the multiple simulations

  3. Run multiple simulations in a parfor (parallel for) loop

  4. Plot results of multiple simulations

  5. Close MATLAB workers

Model Overview

The model sldemo_suspn_3dof shown below simulates the vehicle dynamics based on the road - suspension interaction for different road profiles. The vehicle dynamics are captured in three degrees of freedom: vertical displacement, roll, and pitch. The road profile data for the left and right tires is imported into the Signal Builder block as different test groups. The Road-Suspension Interaction subsystem calculates the suspension forces on the vehicle at the four tire locations based on the road data and the current vehicle state. In the Body Dynamics subsystem these forces and the resulting pitch and roll moments are used to determine the vehicle motion in three degrees of freedom: vertical displacement, roll, and pitch.

In this Monte Carlo study, the damping coefficients of the front suspension system are varied to study their effect on the vehicle dynamics. Parallel Computing Toolbox is used to speed up these multiple simulations, as illustrated below.

mdl               = 'sldemo_suspn_3dof';
isModelOpen       = bdIsLoaded(mdl);

Step 1: Start MATLAB Workers on Your Machine

First configure and start a pool of MATLAB workers. Use the parpool command to check if a parallel pool is open, and then open a local pool on your multicore desktop. This requires Parallel Computing Toolbox.

apool = gcp('nocreate');
if isempty(apool)
    apool = parpool('local');
Starting parallel pool (parpool) using the 'local' profile ... connected to 12 workers.

Step 2: Set up Data Required for the Multiple Simulations

Create a parameter vector for the front suspension damping coefficient that will be used in the Monte Carlo study. Also preallocate an array of SimulationOutput objects in the Simulink package that will be used to collect the simulation results in step 3.

Cf_sweep            = Cf*(0.05:0.1:0.95);
iterations          = length(Cf_sweep);
simout(iterations)  = Simulink.SimulationOutput;

Step 3: Run Multiple Simulations in a parfor (Parallel for) Loop

Run the multiple simulations corresponding to the different parameters within a parfor loop. The sim command returns all the simulation data as a SimulationOutput object of the Simulink package. Note that the simulation results are collected in the simout variable, which was preallocated in step 2.

parfor idx = 1:iterations
    set_param([mdl '/Road-Suspension Interaction'],'MaskValues',...
    simout(idx) = sim(mdl,'SimulationMode','normal');

Step 4: Plot Results of Multiple Simulations

Plot the vertical vehicle displacement from the different simulations to see how varying the damping coefficient affected the vehicle dynamics. Use the get method of the SimulationOutput object to obtain the time and signal data contained in each element of simout.

legend_labels = cell(1,iterations);
for i = 1:iterations
        si = simout(i);
        ts = si.get('logsout').get('vertical_disp').Values;
        legend_labels{i} = ['Run ' num2str(i)];
        hold all
title('Response of a 3-DoF Suspension Model')
xlabel('Time (s)');
ylabel('Vehicle vertical displacement (m)');

Step 5: Close MATLAB Workers

Last, close the parallel pool and the model if they were not previously opened.

    close_system(mdl, 0);
Parallel pool using the 'local' profile is shutting down.
Was this topic helpful?