parsim
以并行或串行方式进行多次动态系统仿真
说明
使用 simOut
= parsim(in
)SimulationInput
对象 in 中指定的输入对模型进行仿真。parsim
函数使用 SimulationInput
对象数组作为输入来运行多个仿真。
使用在 simOut
= parsim(in
,Name=Value
)SimulationInput
对象中指定的输入和使用一个或多个名称-值参量指定的选项以并行方式对模型进行仿真。
parsim
函数需要 Parallel Computing Toolbox™ 许可证才能并行运行仿真。如果无法创建并行池或未使用 Parallel Computing Toolbox,则 parsim
函数会以串行方式运行仿真。
示例
在快速加速模式下使用 parsim
在快速加速模式下仿真模型 vdp
。
加载模型。
openExample('simulink_general/VanDerPolOscillatorExample') mdl = "vdp"; load_system(mdl)
此步骤构建快速加速目标
Simulink.BlockDiagram.buildRapidAcceleratorTarget(mdl);
创建一个 SimulationInput
对象并使用 setModelParameter
函数将 RapidAcceleratorUpToDateCheck
设置为 "off"
。
in = Simulink.SimulationInput(mdl); in = setModelParameter(in,SimulationMode="rapid-accelerator"); in = setModelParameter(in,RapidAcceleratorUpToDateCheck="off");
对模型进行仿真。
out = parsim(in);
使用 parsim 以并行方式仿真模型
通过扫描变量以并行方式仿真模型 CSTR。执行扫描时,使用了 SimulationInput 对象的数组。
指定扫描值。
FeedTempSweep = 250:10:300;
创建 SimulationInput 对象的数组。
for i = length(FeedTempSweep):-1:1 in(i) = Simulink.SimulationInput('CSTR'); in(i) = in(i).setVariable('FeedTemp0',FeedTempSweep(i)); end
以并行方式仿真模型。
out = parsim(in, 'ShowSimulationManager', 'on')
[29-Jun-2022 15:05:52] Checking for availability of parallel pool... Starting parallel pool (parpool) using the 'Processes' profile ... Connected to the parallel pool (number of workers: 6). [29-Jun-2022 15:07:01] Starting Simulink on parallel workers... [29-Jun-2022 15:07:34] Configuring simulation cache folder on parallel workers... [29-Jun-2022 15:07:35] Loading model on parallel workers... [29-Jun-2022 15:07:49] Running simulations... [29-Jun-2022 15:08:24] Completed 1 of 6 simulation runs [29-Jun-2022 15:08:24] Completed 2 of 6 simulation runs [29-Jun-2022 15:08:24] Completed 3 of 6 simulation runs [29-Jun-2022 15:08:27] Completed 4 of 6 simulation runs [29-Jun-2022 15:08:28] Completed 5 of 6 simulation runs [29-Jun-2022 15:08:28] Completed 6 of 6 simulation runs [29-Jun-2022 15:08:28] Cleaning up parallel workers... out = 1x6 Simulink.SimulationOutput array
输入参数
in
— 并行仿真的输入和配置
Simulink.SimulationInput
对象的数组
并行仿真的输入和配置,指定为 Simulink.SimulationInput
对象数组。
名称-值参数
将可选的参量对组指定为 Name1=Value1,...,NameN=ValueN
,其中 Name
是参量名称,Value
是对应的值。名称-值参量必须出现在其他参量后,但参量对组的顺序无关紧要。
在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name
引起来。
示例: ShowProgress="on"
AttachedFiles
— 要附加到并行池的文件
字符向量元胞数组
要附加到并行池的其他文件,指定为字符向量元胞数组。parsim
函数不支持使用 From File 模块从子目录中的 MAT 文件加载数据。
ShowProgress
— 显示仿真进度的选项
"on"
(默认值) | "off"
显示仿真进度的选项,指定为 "on"
或 "off"
。设置为 "on"
,以在 MATLAB® 命令行窗口中查看仿真的进度。当设置为 "off"
时,进度将隐藏。
注意
当显示进度时,可能会在最后几次仿真完成之前显示 'Cleaning up parallel workers..'
消息。此消息出现与否并不取决于仿真是否完成。只有当仿真从 future
获取到输出结果时,仿真才真正完成。有关详细信息,请参阅 Simulink.Simulation.Future
。
RunInBackground
— 在后台运行仿真的选项
"off"
(默认) | "on"
在后台运行仿真的选项,指定为 "off"
或 "on"
。设置为 "on"
将以异步方式运行仿真,从而保持 MATLAB 命令提示符可用。要终止在后台运行的仿真,请使用 Simulink.Simulation.Future
对象的 cancel
函数。
当您将 'RunInBackground'
参量指定为 "on"
时,parsim
函数将返回 Simulink.Simulation.Future
对象。使用 Simulink.Simulation.Future
对象访问 SimulationOutput
对象。
SetupFcn
— 在开始仿真之前对每个工作进程运行一次的函数
函数句柄
在开始仿真之前对每个工作进程运行一次的函数,指定为函数句柄。
示例: SetupFcn=@()openProject("Modelex/Model_example.prj")
注意
当在 SetupFcn
中使用 buildRapidAcceleratorTarget
并且对模型指定了外部输入时,请将 LoadExternalInput
设置为 "off"
或确保指定的外部输入在工作进程上可用,以防出现编译错误。
CleanupFcn
— 在运行仿真后对每个工作进程运行一次的函数
函数句柄
在运行仿真后对每个工作进程运行一次的函数,指定为函数句柄。
ManageDependencies
— 管理模型依存关系的选项
"on"
(默认) | "off"
管理模型依存关系的选项,指定为 "on"
或 "off"
。当 ManageDependencies
设置为 "on"
时,会在需要时自动向并行工作进程发送模型依存关系。如果将 ManageDependencies
设置为 "off"
,则将模型依存关系显式附加到并行池。
UseFastRestart
— 使用快速重启的选项
"off"
(默认) | "on"
使用快速重启的选项,指定为 "off"
或 "on"
。当 UseFastRestart
设置为 "on"
时,仿真使用快速重启在工作进程上运行。
当执行会更改 Simscape™ 组件属性(如 Simscape Multibody™ 中 Solid 模块的质量和惯量)的参数扫描时,请将 Simscape 模块参数指定为可在运行时进行配置。有关详细信息,请参阅About Simscape Run-Time Parameters (Simscape)。
注意
parsim
函数不支持包含 To File 模块的模型的快速重启。
TransferBaseWorkspaceVariables
— 将变量传递给并行工作进程的选项
"off"
(默认) | "on"
将变量传输给并行工作进程的选项,指定为 "off"
或 "on"
。当 TransferBaseWorkspaceVariables
设置为 "on"
时,在模型中使用的变量和在基础工作区中定义的所有内容将传输给并行工作进程。
对于大规模仿真,不推荐使用 TransferBaseWorkspaceVariables
选项。传输不必要的基础工作区数据会无谓地占用网络和内存使用量。在这种情况下,请考虑使用工程启动脚本、模型回调函数或 SetupFcn
名称-值参量来定义基础工作区数据。
ShowSimulationManager
— 打开仿真管理器的选项
"off"
(默认) | "on"
打开仿真管理器的选项,指定为 "off"
或 "on"
。当 ShowSimulationManager
设置为 "on"
时,可以使用仿真管理器来监视仿真。
StopOnError
— 出现仿真错误时跳过其余仿真的选项
"off"
(默认) | "on"
出现仿真错误时跳过其余仿真的选项,指定为 "off"
或 "on"
。如果将 StopOnError
设置为 "on"
,则在发生仿真错误时会停止执行仿真。
输出参量
simOut
— 仿真结果
Simulink.SimulationOutput
对象的数组
仿真结果,以 Simulink.SimulationOutput
对象数组形式返回,每个对象包含一个仿真的所有记录数据。数组的大小等于 Simulink.SimulationInput
对象的数组的大小。
在单个 Simulink.SimulationOutput
对象中返回所有仿真输出(记录的时间、状态和信号)。使用“配置参数”对话框的数据导入/导出窗格指定要记录的数据。您可以使用 To Workspace 和 Scope 等模块记录信号。信号和波形管理器可以直接记录信号。
扩展功能
自动并行支持
通过使用 Parallel Computing Toolbox™ 自动运行并行计算来加快代码执行。
如果安装了 Parallel Computing Toolbox,则在您使用 parsim
时,MATLAB 会自动在本地计算机上打开工作进程的并行池。MATLAB 在可用的工作进程上运行仿真。使用并行预设项控制并行行为,包括扩展到集群。
有关详细信息,请参阅Running Multiple Simulations。
版本历史记录
在 R2017a 中推出
另请参阅
对象
工具
函数
applyToModel
|setBlockParameter
|setModelParameter
|setInitialState
|setExternalInput
|setVariable
|validate
|setPreSimFcn
|setPostSimFcn
|cancel
|fetchNext
|fetchOutputs
|wait
主题
- Rapid Accelerator Simulations Using Parsim
- Parallel Simulations Using Parsim: Test-Case Sweep
- Parallel Simulations Using Parsim: Parameter Sweep in Normal Mode
- Parallel Simulations Using Parsim: Parameter Sweep in Rapid Accelerator Mode
- Running Multiple Simulations
- Run Parallel Simulations for a Thermal Model of a House Using parsim
- 运行并行仿真
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)