Parallel Simulations Using Parfor: Test-Case Sweep

This example shows how you can run multiple Simulink® simulations corresponding to different test cases in the Signal Builder block, in parallel by using the Parallel Computing Toolbox™. This involves five main steps:

  1. Start MATLAB® workers on your machine

  2. Set up data required for the multiple simulations


  4. Plot results of multiple simulations

  5. Close MATLAB workers

Model Overview


The suspension model is simulated using different road profiles to determine if the design meets desired performance goals. 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


apool = gcp;
if isempty(apool)
    apool = parpool;

Step 2: Set up Data Required for the Multiple Simulations

Determine the number of cases in the Signal Builder block using the signalbuildersignalbuilder function. The number of cases is used to determine the number of iterations to run in step 3. The number of cases is also used to preallocate an array of SimulationOutput objects in the Simulink package that will be used to collect the simulation results in step 3.

[~,~,~,cases]     = signalbuilder([mdl '/Road Profiles']);
numCases          = numel(cases);
simout(numCases)  = Simulink.SimulationOutput;


Run multiple simulations corresponding to the different test cases in the Signal Builder block within a parfor loop. The signalbuilder function is used again here but this time to set the active signal in each iteration. The simsim 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:numCases
    signalbuilder([mdl '/Road Profiles'],'activegroup',idx);

Step 4: Plot Results of Multiple Simulations

Plot the vertical vehicle displacement from the different simulations to see how the vehicle performed to the different road profiles. Use the get method of the SimulationOutput object to obtain the timeseries contained in each element of simout. The time and and signal data are contained within the timeseries object.

legend_labels = cell(1,numCases);
for i = 1:numCases
        si = simout(i);
        hold all
xlabel('Time (s)');
ylabel('Vehicle vertical displacement (m)');

Step 5: Close MATLAB Workers


    close_system(mdl, 0);