Main Content

使用PARSIM快速加速器模拟

此示例显示了在需要对一系列输入和参数值的运行并行仿真的应用中使用快速加速器。

该示例使用模拟发动机空闲速度的模型。模型输入是旁路气阀的电压。输出是空闲速度。

UseParsimto run parallel simulations with two sets of valve voltages. Independently vary two of the three transfer function gain parameters over a range of two values. The following table lists the eight simulations, along with the parameter values. In Step 2, create the external inputs,inpsets。变量增益2and增益3对应于两个增益参数。

运行1个inpsets(1)增益2 = 25 Gain3 = 20

Run 2 inpSets(1) gain2 = 25 gain3 = 30

运行3个inpsets(1)Gain2 = 35 Gain3 = 20

运行4个inpsets(1)Gain2 = 35 Gain3 = 30

运行5个inpsets(2)Gain2 = 25 Gain3 = 20

运行6个inpsets(2)Gain2 = 25 Gain3 = 30

运行7个inpsets(2)Gain2 = 35 Gain3 = 20

运行8个inpsets(2)Gain2 = 35 Gain3 = 30

步骤1:准备

首先,打开模型。仿真模式设置为快速加速器。默认输入数据和所需参数已预加载在基本工作区中。

开放模型:

mdl ='sldemo_raccel_engine_idle_speed';Open_System(MDL);

步骤2:创建输入集

Perturb the default input values vector to obtain a new input values vector.

inpsets(1) = timeseries(inpData, time); rndPertb = 0.5 + rand(length(time), 1); inpSets(2) = timeseries(inpData.*rndPertb, time); numInpSets = length(inpSets);

步骤3:创建参数集

Next, examine how the idle speed changes for different values of parameters增益2and增益3。创建一个阵列金宝appSimulink.SimulationInputobjects to specify the different parameter values and external input for each simulation. The array of SimulationInput objects is preallocated for better performance. Note that you can directly specify the external input on theSimulationInputobject instead of using a model parameter.

GAIN2_VALS = 25:10:35;GAIN3_VALS = 20:10:30;num_gain2_vals =长度(gain2_vals);num_gain3_vals =长度(gain3_vals);numsims = num_gain2_vals*num_gain3_vals*numinpsets;在(1:numsims)= si金宝appmulink.simulationInput(MDL);idx = 1;为了Ig2 = 1:num_gain2_vals为了Ig3 = 1:num_gain3_vals为了inpsetsIdx = 1:numInpSets in(idx) = in(idx).setModelParameter('simulationmode',,,,'迅速的',,,,...“ RapidAcceleratoruptoptatecheck”,,,,'off',,,,...'SaveTime',,,,'在',,,,...“ saveoutput”,,,,'在');%使用setVaria可以在变量期间指定变量的新值% simulations在(idx)= in(idx).setVariable('GAIN2',gain2_vals(ig2));在(idx)= in(idx).setVariable('gain3',gain3_vals(ig3));在(idx).externalInput = inpsets(inpetSIDX);IDX = IDX + 1;结尾结尾结尾

Note: This example uses thesetModelParameter方法的方法SimulationInput对象设置模型参数以快速加速器模式运行模拟并启用记录。快速加速器目标是使用设置。快速加速器目标是构建一次,并由所有后续模拟使用,从而节省了模型汇编所需的时间。这是设置的代码

functionsldemo_parallel_rapid_accel_sims_script_setup(mdl)%将工人上的当前文件夹暂时更改为空%文件夹,因此客户端上的任何现有SLPRJ文件夹都不%干预构建过程。currentFolder = pwd; tempDir = tempname; mkdir(tempDir); cd (tempDir); oc = onCleanup(@() cd (currentFolder)); Simulink.BlockDiagram.buildRapidAcceleratorTarget(mdl);结尾

步骤4:执行模拟

使用Parsimfunction to execute the simulations in parallel. The array ofSimulationInput对象,,,,,created in the last step is passed into theParsim功能是第一个参数。将仿真输出数据存储在变量中,出去,其价值是金宝appsimulink.simulationOutput对象。每个仿真图object contains the logged signal along with the仿真metadata。在运行多个模拟时使用Parsim,捕获错误,以便随后的模拟可以继续运行。任何错误都会在ErrorMessageproperty of the仿真图object.

out = parsim(在“表演”,,,,'在',,,,...'SetupFcn', @()sldemo_parallel_rapid_accel_sims_script_setup(mdl));
[21-May-2021 17:46:47] Checking for availability of parallel pool... Starting parallel pool (parpool) using the 'local' profile ... Connected to the parallel pool (number of workers: 6). [21-May-2021 17:47:51] Starting Simulink on parallel workers... Analyzing and transferring files to the workers ...done. [21-May-2021 17:48:54] Configuring simulation cache folder on parallel workers... [21-May-2021 17:48:55] Running SetupFcn on parallel workers... [21-May-2021 17:50:14] Loading model on parallel workers... [21-May-2021 17:50:22] Running simulations... [21-May-2021 17:50:29] Completed 1 of 8 simulation runs [21-May-2021 17:50:29] Completed 2 of 8 simulation runs [21-May-2021 17:50:29] Completed 3 of 8 simulation runs [21-May-2021 17:50:29] Completed 4 of 8 simulation runs [21-May-2021 17:50:29] Completed 5 of 8 simulation runs [21-May-2021 17:50:29] Completed 6 of 8 simulation runs [21-May-2021 17:50:36] Completed 7 of 8 simulation runs [21-May-2021 17:50:36] Completed 8 of 8 simulation runs [21-May-2021 17:50:36] Cleaning up parallel workers...

Step 5: Plot Results

绘制引擎空闲速度相对于不同的参数值和输入的时间。输出以数组格式记录,并且可以从SimulationOutPut对象访问。

为了i = 1:numsims simout = out(i);t = simout.tout;y = simout.yout;情节(t,y)握住all结尾

第六步:关闭MATLAB工人

删除(GCP(GCP)('nocreate'))

也可以看看

Related Topics