Main Content

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 modelsldemo_suspn_3dof基于道路和悬架之间的不同道路剖面的相互作用,模拟车辆动力学。该模型以三个自由度捕获车辆动力学:垂直位移,滚动和音高。信号编辑器块存储了左右轮胎作为不同测试组的道路剖面数据。道路悬浮互动子系统根据道路数据和当前车辆状态在四个轮胎位置计算车辆上的悬架力。身体动力学子系统使用这些力以及最终的螺距和滚动矩来计算三个自由度中的每个度中的车辆运动。

在这项蒙特卡洛研究中,您可以检查前悬架系数对车辆动力学的影响。您运行多个模拟,每个模拟都具有不同的系数值。

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

在模型中,双击道路悬架相互作用块。打开“蒙版”对话框。蒙版参数Front susp. dampingsets the value of the damping coefficient,150

对于身体动力块,找到退出的信号垂直分配outport port. This signal represents the vertical vehicle displacement over time, which the suspension damping coefficient influences.

Right-click the signal and selectProperties

In the Signal Properties dialog box, the记录和可访问性标签有Log signal data签入,表明该信号已配置用于记录。模拟完成后,您可以使用指定的记录名称,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_weep,,,,in the base workspace.

cf_weep = 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 =长度(cf_weep);

Use aforloop to:

  1. CreateSimulink.SimulationInput模型的对象。每个仿真创建一个对象。将对象作为数组存储在变量中,in

  2. Specify the sweep value for each simulation. Identify the target mask parameter by its underlying name,CF

fori = numsims:-1:1 in(i)= s金宝appimulink.simulationInput(mdl);在(i)= setBlockParameter(在(i)中,[mdl“/道路悬浮互动”],'cf',num2str(cf_weep(i)));end

注意,指定块Si的参数mulationInput 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.

并行运行模拟

使用Parsim函数以并行执行模拟。模拟对象的数组,in,,,,created in the last step is passed into theParsim功能是第一个参数。来自Parsimcommand is an array of金宝appsimulink.simulationOutputobjects which is stored in the variableout。Set the 'ShowProgress' option to 'on' to print a progress of the simulations on the MATLAB command window.

out = parsim(在“表演”,,,,'上');
[20-FEB-2021 23:49:38]检查并行池的可用性...使用“本地”配置文件开始并行池(PARPOOL)...连接到并行池(工人数量:6)。[20-FEB-2021 23:50:23]在平行工人上启动Si金宝appmulink ... [20-FEB-2021 23:51:11]在并行工人上配置模拟缓存文件夹... [20-FEB-2021 23:51:13]在平行工人上加载模型... [20-FEB-2021 23:51:18]运行模拟... [20-FEB-2021 23:51:31]完成了10个模拟运行中的1个[20-2月23日23:51:31]完成10次模拟运行中的2次[20-FEB-2021 23:51:31]完成了10次模拟运行中的3台[20-FEB-2021 23:51:31]完成了10个模拟跑步[20-FEB-2021 23:51:31]完成了10次模拟运行中的5次[20-FEB-2021 23:51:31]完成了10次模拟运行中的6个[20-FEB-2021 23:51:33]10次​​模拟运行中的7台[20-FEB-2021 23:51:33]完成了10次模拟运行中的8台[20-FEB-2021 23:51:33]完成了10次模拟运行中的9台[20-FEB-2021 23:51:34]完成10个模拟运行中的10台[20-FEB-2021 23:51:34]清理平行工人...

每个仿真图对象都包含已记录的信号以及仿真的信号。在运行多个模拟时使用Parsim,捕获错误,以便随后的模拟可以继续运行。任何错误都会在ErrorMessage模拟图对象的属性。

情节结果

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 theget从每个元素中获取包含时间和信号数据的时间表对象的方法out

legend_labels =单元格(1,numsims);fori = numSims:-1:1 simOut = out(i); ts = simOut.logsout.get('Vertical_disp')。% 'ts' is a MATLAB 'timeseries' object that stores the time and已记录的“ vertical_disp”信号的数据值。% Use the 'plot' method of the object to plot the data against the% time.plot(ts); legend_labels{i} = ['跑 'num2str(i)]; holdallendtitle(“ 3-DOF悬架模型的响应”) xlabel('Time (s)');ylabel(“车辆垂直位移(M)”);legend(legend_labels,'Location',,,,'NorthEastOutside');

关闭MATLAB工人

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

if(~isModelOpen) close_system(mdl, 0);enddelete(gcp('nocreate'));
Parallel pool using the 'local' profile is shutting down.

也可以看看

||

Related Topics