バックナンバーはこちら。
https://www.simulationroom999.com/blog/model-based-of-minimum-2-backnumber/
はじめに
前回は、時間軸、Ramp関数、入力用行列、入力オブジェクトの生成まで実施。
シミュレーションを実施する上での必要な準備が整った状態。
今回は実施にFMUを駆動させてシミュレーションを実施するところ。
登場人物
博識フクロウのフクさん

イラストACにて公開の「kino_k」さんのイラストを使用しています。
https://www.ac-illust.com/main/profile.php?id=iKciwKA9&area=1
エンジニア歴8年の太郎くん

イラストACにて公開の「しのみ」さんのイラストを使用しています。
https://www.ac-illust.com/main/profile.php?id=uCKphAW2&area=1
シミュレーション

前回のでシミュレーションの前準備が揃ったから
今回はついにシミュレーション実施だね。

そうだね。
まぁやることはシミュレーション用の関数を呼び出すだけだ。
res = model.simulate(start_time=0.0,final_time=2.0, input=input_object)

前回作った、入力オブジェクトをここで渡すんだね。
シミュレーション時間も開始を0秒、終了を2秒か。

そうそう。
これでシミュレーションとしては完了だ。
シミュレーション結果の取得

で、シミュレーションの結果はどう取得するの?
戻り値でresってのは受け取ってるけど?

そのresの中に連想配列で格納されてる。
取得方法としては以下になる。
voltage = res['voltage']
current = res['current']
speed = res['speed']
loadTorqueStep_tau = res['loadTorqueStep.tau']
t = res['time']

ほう。
パラメータ名をそのまま指定すれば取得できるのか!

FMILibraryの時は一旦変数オブジェクトを取得して、
そこからvalueReferenceがどうのってあったけど、
そこらへんは要らないんだね。

そうだね。
そこら辺はかなり扱いが楽になっていると言える。
シミュレーション結果の表示

次はグラフにするんだろうけど、
これは普通にmatplotlibを使えばいいんだろうね。

こんな感じかな。
plt.plot(t, voltage, label="voltage")
plt.plot(t, current, label="current")
plt.plot(t, speed, label="speed")
plt.plot(t, loadTorqueStep_tau, label="loadTorqueStep.tau")
plt.legend(loc='best')
plt.xlabel('time [sec]')
plt.grid(which='both')
plt.show()

うん。
それでOKだろう。
まとめ

まとめだよ。
- シミュレーション実施。
- 開始時刻、終了時刻、入力オブジェクトを渡すことで実施可能。
- シミュレーション結果取得。
- simulate関数の戻り値が連想配列になっている。
- voltage = res[‘voltage’]のような指定方法。
- シミュレーション結果のグラフ表示。
- matplotlibでプロットするのみ。
バックナンバーはこちら。
コメント