Parsim を使ったラピッド アクセラレータ シミュレーション
この例では、入力値とパラメーター値の範囲で並列シミュレーションの実行を必要とするアプリケーションでラピッド アクセラレータを使用する方法を示します。
この例では、エンジンのアイドル回転数をシミュレーションするモデルを使用します。モデルの入力はバイパス エア バルブの電圧です。出力はアイドル回転数です。
parsim
を使用して、2 セットのバルブ電圧で並列シミュレーションを実行します。2 つの値の範囲で 3 つの伝達関数ゲイン パラメーターのうち 2 つを独立して変化させます。次の表に、8 つのシミュレーションをパラメーター値と共に示します。手順 2 で、外部入力 inpSets
を作成します。変数 gain2
と変数 gain3
は 2 つのゲイン パラメーターに対応します。
Run 1 inpSets(1) gain2 = 25 gain3 = 20
Run 2 inpSets(1) gain2 = 25 gain3 = 30
Run 3 inpSets(1) gain2 = 35 gain3 = 20
Run 4 inpSets(1) gain2 = 35 gain3 = 30
Run 5 inpSets(2) gain2 = 25 gain3 = 20
Run 6 inpSets(2) gain2 = 25 gain3 = 30
Run 7 inpSets(2) gain2 = 35 gain3 = 20
Run 8 inpSets(2) gain2 = 35 gain3 = 30
手順 1: 準備
最初にモデルを開きます。シミュレーション モードはラピッド アクセラレータに設定されています。既定の入力データと必須パラメーターは、ベース ワークスペースに事前に読み込まれています。
次のようにしてモデルを開きます。
mdl = 'sldemo_raccel_engine_idle_speed';
open_system(mdl);
手順 2: 入力セットの作成
既定の入力値ベクトルに摂動を与え、新しい入力値ベクトルを取得します。
inpSets(1) = timeseries(inpData, time); rndPertb = 0.5 + rand(length(time), 1); inpSets(2) = timeseries(inpData.*rndPertb, time); numInpSets = length(inpSets);
手順 3: パラメーター セットの作成
次に、パラメーター gain2
および gain3
のさまざまな値に対してアイドル回転数がどのように変化するか調べます。Simulink.SimulationInput
オブジェクトの配列を作成し、各シミュレーションに異なるパラメーター値と外部入力を指定します。SimulationInput オブジェクトの配列は、パフォーマンスを改善するために事前割り当てされます。外部入力は、モデル パラメーターを使用するのではなく、SimulationInput
オブジェクトに直接指定できる点に注目してください。
gain2_vals = 25:10:35; gain3_vals = 20:10:30; num_gain2_vals = length(gain2_vals); num_gain3_vals = length(gain3_vals); numSims = num_gain2_vals*num_gain3_vals*numInpSets; in(1:numSims) = Simulink.SimulationInput(mdl); idx = 1; for iG2 = 1:num_gain2_vals for iG3 = 1:num_gain3_vals for inpSetsIdx = 1:numInpSets in(idx) = in(idx).setModelParameter('SimulationMode', 'rapid', ... 'RapidAcceleratorUpToDateCheck', 'off', ... 'SaveTime', 'on', ... 'SaveOutput', 'on'); % Use setVariable to specify a new value for a variable during % simulations in(idx) = in(idx).setVariable('gain2', gain2_vals(iG2)); in(idx) = in(idx).setVariable('gain3', gain3_vals(iG3)); in(idx).ExternalInput = inpSets(inpSetsIdx); idx = idx + 1; end end end
メモ: この例では、ラピッド アクセラレータ モードでシミュレーションを実行してログを有効にするように、SimulationInput
オブジェクトの setModelParameter
メソッドを使用してモデル パラメーターを設定します。ラピッド アクセラレータ ターゲットのビルドには SetupFcn
を使用しています。ラピッド アクセラレータ ターゲットを 1 回ビルドし、モデルのコンパイルにかかる時間を短縮するために以降のすべてのシミュレーションで使用します。SetupFcn のコードは次のとおりです。
function sldemo_parallel_rapid_accel_sims_script_setup(mdl) % Temporarily change the current folder on the workers to an empty % folder so that any existing slprj folder on the client does not % interfere in the build process. currentFolder = pwd; tempDir = tempname; mkdir(tempDir); cd (tempDir); oc = onCleanup(@() cd (currentFolder)); Simulink.BlockDiagram.buildRapidAcceleratorTarget(mdl); end
手順 4: シミュレーションの実行
関数parsim
を使用してシミュレーションを並列実行します。最後の手順で作成された SimulationInput
オブジェクトの配列 in
は、最初の引数として関数 parsim
に渡されます。シミュレーション出力データを、値が Simulink.SimulationOutput
オブジェクトの配列である変数 out
に保存します。各 SimulationOutput
オブジェクトには、ログ信号に加え、SimulationMetadata
が含まれています。parsim
を使用して複数のシミュレーションを実行すると、後続のシミュレーションを継続して実行できるようにエラーがキャプチャされます。エラーがあれば SimulationOutput
オブジェクトの ErrorMessage
プロパティに表示されます。
out = parsim(in, 'ShowProgress', 'on', ... '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...
手順 5: 結果のプロット
さまざまなパラメーター値と入力で、時間に対するエンジンのアイドル回転数をプロットします。出力は配列形式に記録され、SimulationOuput オブジェクトからアクセスできます。
for i = 1:numSims simOut = out(i); t = simOut.tout; y = simOut.yout; plot(t, y) hold all end
手順 6: MATLAB ワーカーを閉じる
delete(gcp('nocreate'))