Parallel Simulations Using Parsim: Parameter Sweep in Normal Mode
This example shows how to run multiple simulations of a Monte Carlo study in parallel by using Parallel Computing Toolbox™. Parallel execution leverages the multiple cores of your host machine to run many simulations more quickly. These simulations could also be run in parallel on computer clusters using the MATLAB Parallel Server™. This example will work even if the Parallel Computing Toolbox™ or the MATLAB Parallel Server™ is not available, but the simulations will run in serial.
Explore Example Model
The model sldemo_suspn_3dof
simulates vehicle dynamics based on the interaction between road and suspension for different road profiles. The model captures vehicle dynamics in three degrees of freedom: vertical displacement, roll, and pitch. The Signal Editor block stores measured road profile data for the left and right tires 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. The Body Dynamics subsystem uses these forces and the resulting pitch and roll moments to calculate the vehicle motion in each of the three degrees of freedom.
In this Monte Carlo study, you inspect the impact of the front suspension coefficients on the vehicle dynamics. You run multiple simulations, each with a different coefficient value.
mdl = 'sldemo_suspn_3dof';
isModelOpen = bdIsLoaded(mdl);
open_system(mdl);
In the model, double-click the Road-Suspension Interaction block. The mask dialog box opens. The mask parameter Front susp. damping sets the value of the damping coefficient, 150
.
For the Body Dynamics block, find the signal that exits the Vertical disp
outport port. This signal represents the vertical vehicle displacement over time, which the suspension damping coefficient influences.
Right-click the signal and select Properties.
In the Signal Properties dialog box, the Logging and accessibility tab has Log signal data checked on, indicating that the signal is configured for logging. After the simulation finishes, you can use the specified logging name, vertical_disp
, to identify the signal and acquire the simulation output data from the SimulationOutput object.
Prepare Parameter Inputs
Calculate the sweep values for the coefficient as percentages of the design value ranging from 5% to 95% in increments of 10%. Store the values in a variable, Cf_sweep
, in the base workspace.
Cf_sweep = Cf*(0.05:0.1:0.95);
Determine the number of simulations to run, which is equal to the number of sweep values. Store the number in a variable, numSims
.
numSims = length(Cf_sweep);
Use a for
loop to:
Create
Simulink.SimulationInput
objects for the model. Create one object per simulation. Store the objects as an array in a variable,in
.Specify the sweep value for each simulation. Identify the target mask parameter by its underlying name,
Cf
.
for i = numSims:-1:1 in(i) = Simulink.SimulationInput(mdl); in(i) = setBlockParameter(in(i), [mdl '/Road-Suspension Interaction'], 'Cf', num2str(Cf_sweep(i))); end
Note that specifying the block parameter on the SimulationInput object does not apply it to the model immediately. The specified value will be applied during the simulation and reverted back to its original value, if possible, after the simulation finishes.
Run Simulations in Parallel Using Parsim
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. The output from the parsim
command is an array of Simulink.SimulationOutput
objects which is stored in the variable out
. Set the 'ShowProgress' option to 'on' to print a progress of the simulations on the MATLAB command window.
out = parsim(in, 'ShowProgress', 'on');
[20-Feb-2021 23:49:38] Checking for availability of parallel pool... Starting parallel pool (parpool) using the 'local' profile ... Connected to the parallel pool (number of workers: 6). [20-Feb-2021 23:50:23] Starting Simulink on parallel workers... [20-Feb-2021 23:51:11] Configuring simulation cache folder on parallel workers... [20-Feb-2021 23:51:13] Loading model on parallel workers... [20-Feb-2021 23:51:18] Running simulations... [20-Feb-2021 23:51:31] Completed 1 of 10 simulation runs [20-Feb-2021 23:51:31] Completed 2 of 10 simulation runs [20-Feb-2021 23:51:31] Completed 3 of 10 simulation runs [20-Feb-2021 23:51:31] Completed 4 of 10 simulation runs [20-Feb-2021 23:51:31] Completed 5 of 10 simulation runs [20-Feb-2021 23:51:31] Completed 6 of 10 simulation runs [20-Feb-2021 23:51:33] Completed 7 of 10 simulation runs [20-Feb-2021 23:51:33] Completed 8 of 10 simulation runs [20-Feb-2021 23:51:33] Completed 9 of 10 simulation runs [20-Feb-2021 23:51:34] Completed 10 of 10 simulation runs [20-Feb-2021 23:51:34] Cleaning up parallel workers...
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.
Plot Results
Plot the vertical vehicle displacement from the different simulations to see how varying the damping coefficient affects the vehicle dynamics. The signal is logged in the SimulationOutput object in the Dataset format. Use the get
method to obtain the timeseries object containing the time and signal data from each element of out
.
legend_labels = cell(1,numSims); for i = numSims:-1:1 simOut = out(i); ts = simOut.logsout.get('vertical_disp').Values; % 'ts' is a MATLAB 'timeseries' object that stores the time and % data values for the logged 'vertical_disp' signal. % Use the 'plot' method of the object to plot the data against the % time. plot(ts); legend_labels{i} = ['Run ' num2str(i)]; hold all end title('Response of a 3-DoF Suspension Model') xlabel('Time (s)'); ylabel('Vehicle vertical displacement (m)'); legend(legend_labels,'Location','NorthEastOutside');
Close MATLAB Workers
Last, close the parallel pool and the model if they were not previously opened.
if(~isModelOpen) close_system(mdl, 0); end delete(gcp('nocreate'));
Parallel pool using the 'local' profile is shutting down.
See Also
parsim
| Simulink.SimulationInput
| Simulink.Simulation.Variable