バックナンバーはこちら。
https://www.simulationroom999.com/blog/model-based-of-minimum-2-backnumber/
はじめに
前回はFMILibraryのサンプルをベースにボールがバウンドする様子のシミュレーションを確認した。
出力されたボールの高さ、ボールの速度をExcel上でグラフにすることで確認。
このシミュレーションの状況をもう少しカスタマイズできるといろいろ遊べそうである。
登場人物
博識フクロウのフクさん

イラスト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
fmi2_import_cs_testのシミュレーションstep

FMILibraryでボールのバウンドのシミュレーションは見れたけど、
結構サンプリングが荒いよねー。

そうだね。
100ms周期のサンプリングになってる。

あと、シミュレーション時間も2秒くらいなのかな?
もう少し長くても良い気がするねー。

じゃー、そこら辺を弄ってみよう。

え?
調整できるの?

まぁシミュレーションの時間管理はFMU側じゃなくてfmi2_import_cs_test側でやってるから
それほど難しくはないよ。
サンプリングとシミュレーション時間の調整

で、どこを調整すれば良いの?

fmi2_import_cs_test.cのソースコード内で実際にFMU側の演算処理をよびだしてるところがある。
関数名としてはfmi2_import_do_stepなんだけど、
whileルールで複数回呼び出すような実装になってる。
while (tcur < tend) {
fmi2_boolean_t newStep = fmi2_true;
fmistatus = fmi2_import_do_step(fmu, tcur, hstep, newStep);
for (k = 0; k < sizeof(compare_real_variables_vr)/sizeof(fmi2_value_reference_t); k++) {
fmi2_value_reference_t vr = compare_real_variables_vr[k];
fmi2_real_t rvalue;
fmistatus = fmi2_import_get_real(fmu, &vr, 1, &rvalue);
}
{
fmi2_real_t val[2];
fmi2_import_get_real(fmu, compare_real_variables_vr, 2, val);
printf("%10g %10g\n", val[0],val[1]);
}
tcur += hstep;
}

ほう!
ここが実際にシミュレーションを実施しているループか。

時間に関係する変数は以下3つ
- tcur:現在時間
- tend:終了時間
- hstep:ステップ時間

なんとなくわかってきたぞ。
今回の目的に合わせて修正するには、
tend:終了時間
と
hstep:ステップ時間
を調整すれば良いのか。

そうそう。
実際に修正する場所

で、実際の修正箇所だが、
test_simulate_cs関数の先頭で各種変数定義をしているところがあるのだが、
その変数定義の中の以下を修正すればよい。
fmi2_real_t hstep = 0.1;
fmi2_real_t tend = 2.0;

例えば、20ms周期で3秒間のシミュレーションをするなら以下。
fmi2_real_t hstep = 0.02;
fmi2_real_t tend = 3.0;

時間の単位は「秒」であることに注意が必要かな。

今回のは結構チョロい感じだったな。
まとめ

まとめだよ。
- シミュレーション時間とシミュレーションステップはFMUの外側の制御の仕方次第で確定する。
- FMU処理自体はfmi2_import_do_stepという関数の中で指定時間分実施する動き。
- サンプルプロジェクトfmi2_import_cs_testの場合はhstepとtendを調整すればOK。
- 時間は「秒」である点に注意。
バックナンバーはこちら。
コメント