Main Content

Multiple Simulations Workflow Tips

此示例的目的是为使用并行仿真工具运行多个模拟提供有用的提示。即使并行计算Toolbox™不可用,此示例也将起作用,但是模拟将以串行运行。我们将使用模型sldemo_suspn_3dof

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

初始化simulink的数组。金宝app

Typically you will construct an array of Simulink.SimulationInput objects in order to run multiple simulations. There are several ways to initialize the array before populating it with data.

numsims = 5;cf_weep = cf*linspace(.05,.95,numsims);

方法1:在循环之前初始化数组

在(numsims)= si金宝appmulink.simulationInput;为了idx = 1:numsims%需要填充模型名称,因为默认情况下我们会获得任何空数组在(idx).modelName ='sldemo_suspn_3dof';在(idx)= in(idx).setVariable('cf',cf_weep(idx));结尾

方法2:初始化循环中的数组

Note that the loop variableIDXstarts from the largest value so that the entire array is pre-allocated.

为了idx = numsims:-1:1% Since we are indexing from 5 to 1, the first iteration will%初始化数组。在(idx)= si金宝appmulink.simulationInput('sldemo_suspn_3dof');在(idx)= in(idx).setVariable('cf',cf_weep(idx));结尾

Setting Model and Block Parameters

setModelParameterandsetBlockParameter方法使用相同的参数值对语法set_paramAPI使用。这意味着您传递给这些方法的大多数值应该是字符数组,而不是其字面价值。

为了idx = numsims:-1:1在(idx)= si金宝appmulink.simulationInput('sldemo_suspn_3dof');%不正确在(idx)= in(idx).setModelParameter('开始时间',5);% Correct在(idx)= in(idx).setModelParameter('开始时间',,,,'3');结尾

设置变量

setVaria方法期望您将要分配的文字价值传递给变量。这个想法是,这密切反映了assignin句法。

为了idx = numsims:-1:1在(idx)= si金宝appmulink.simulationInput('sldemo_suspn_3dof');%不正确,CF预计将是双重,而不是字符数组在(idx)= in(idx).setVariable('cf',,,,'2500');%正确,CF是标量双重在(idx)= in(idx).setVariable('cf',,,,2500);结尾

Diagnosing Runtime Errors

假设您已经意外地设置了一个模拟对象,该对象具有不正确的值。金宝app

mb_weep = linspace(0,1500,numsims);为了idx = numsims:-1:1在(idx)= si金宝appmulink.simulationInput('sldemo_suspn_3dof');%在第一次迭代中意外将质量设置为0在(idx)= in(idx).setVariable('Mb',mb_weep(idx));% Shorten the stop time在(idx)= in(idx).setModelParameter(“停止时间”,,,,'1');结尾

模拟这些会导致运行时错误

out = sim(in);
[09-Dec-2021 11:45:05] Running simulations... [09-Dec-2021 11:45:26] Completed 1 of 5 simulation runs. Run 1 has errors. [09-Dec-2021 11:45:36] Completed 2 of 5 simulation runs [09-Dec-2021 11:45:36] Completed 3 of 5 simulation runs [09-Dec-2021 11:45:37] Completed 4 of 5 simulation runs [09-Dec-2021 11:45:46] Completed 5 of 5 simulation runs Warning: Simulation(s) with indices listed below completed with errors. Please inspect the corresponding SimulationOutput to get more details about the error: [1]

Fortunately, you can inspect the Simulink.SimulationOutput object to see any error messages that come from a simulation.

出去(1).errormessage
ans = 'Derivative of state '1' in block 'sldemo_suspn_3dof/Body Dynamics/Vertical (Z) dynamics/Zdot' at time 0.0 is not finite. The simulation will be stopped. There may be a singularity in the solution. If not, try reducing the step size (either by reducing the fixed step size or by tightening the error tolerances)'

这也可以在平行工人上调试问题。

为了idx = numsims:-1:1在(idx)= si金宝appmulink.simulationInput('sldemo_suspn_3dof');%在第一次迭代中意外将质量设置为0在(idx)= in(idx).setVariable('Mb',mb_weep(idx));% Shorten the stop time在(idx)= in(idx).setModelParameter(“停止时间”,,,,'1');结尾out = parsim(in);
(09 - 12月- 2021 11:45:47)检查可用性啊f parallel pool... Starting parallel pool (parpool) using the 'local' profile ... Connected to the parallel pool (number of workers: 6). [09-Dec-2021 11:47:08] Starting Simulink on parallel workers... [09-Dec-2021 11:47:45] Configuring simulation cache folder on parallel workers... [09-Dec-2021 11:47:45] Loading model on parallel workers... [09-Dec-2021 11:48:06] Running simulations... [09-Dec-2021 11:48:18] Completed 1 of 5 simulation runs. Run 1 has errors. [09-Dec-2021 11:48:18] Completed 2 of 5 simulation runs [09-Dec-2021 11:48:18] Completed 3 of 5 simulation runs [09-Dec-2021 11:48:18] Completed 4 of 5 simulation runs [09-Dec-2021 11:48:18] Completed 5 of 5 simulation runs Warning: Simulation(s) with indices listed below completed with errors. Please inspect the corresponding SimulationOutput to get more details about the error: [1] [09-Dec-2021 11:48:18] Cleaning up parallel workers...

Inspecting the Simulink.SimulationOutput reveals a non-finite derivative error.

出去(1).errormessage
ans = 'Derivative of state '1' in block 'sldemo_suspn_3dof/Body Dynamics/Vertical (Z) dynamics/Zdot' at time 0.0 is not finite. The simulation will be stopped. There may be a singularity in the solution. If not, try reducing the step size (either by reducing the fixed step size or by tightening the error tolerances)'

ApplyTomodelmethod will configure your model with the settings on the SimulationInput so you can debug the problem locally.

在(1).applytomodel;

注意变量的值Mb在the base workspace changes to 0 to reflect the value that was used in the simulation corresponding to the first SimulationInput object in

关闭MATLAB工人

最后,如果以前没有打开,请关闭平行池和模型。

如果(~isModelOpen) close_system(mdl, 0);结尾删除(GCP(GCP)('nocreate');
Parallel pool using the 'local' profile is shutting down.

也可以看看

|

Related Topics