バックナンバーはこちら。
https://www.simulationroom999.com/blog/model-based-of-minimum-2-backnumber/
はじめに
前回グラフィックエディタで作成したModelicaモデルのソースコードを確認してみた。
凡その構成を把握したところで、ソースコードに手を入れる割と簡単なパターンを出してみる。
これを知ってるとシミュレーションの試行回数を一気に増やすことができ作業効率化ができる。
かも?
今回はこのちょっとしたテクニックの紹介になる。
登場人物
博識フクロウのフクさん

イラスト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
Modelicaのソースコード確認だけではちょっと勿体ない

さて、前回Modelicaモデルのソースコードを確認したわけだが、
確認ってだけじゃつまらなないよね?

いや、別にソースコード弄らなくてもいいんだけど・・・。

いろいろと効率化される可能性がある割には結構一撃なテクニックなんだけどなー。

え?
結構チョロいやつ?

チョロいチョロい。

じゃー教えてー!

(チョロいな)
massモデルの修正

とりあえず、Modelicaコードを修正した後の差分が以下。
diff U3B masstest.mo masstest2.mo
--- masstest.mo
+++ masstest2.mo
@@ -1,9 +1,11 @@
-model masstest
+model masstest2
+ parameter Real Force1 = -1;
+ parameter Real Force2 = 2;
Modelica.Mechanics.Translational.Components.Mass mass(L = 0.1, m = 1, s(fixed = true, start = 0), v(fixed = true, start = 0)) annotation(
Placement(visible = true, transformation(extent = {{-20, -10}, {0, 10}}, rotation = 0)));
- Modelica.Mechanics.Translational.Sources.ConstantForce constantForce2(f_constant = 2) annotation(
+ Modelica.Mechanics.Translational.Sources.ConstantForce constantForce2(f_constant = Force2) annotation(
Placement(visible = true, transformation(origin = {32, 0}, extent = {{10, -10}, {-10, 10}}, rotation = 0)));
- Modelica.Mechanics.Translational.Sources.ConstantForce constantForce1(f_constant = -1) annotation(
+ Modelica.Mechanics.Translational.Sources.ConstantForce constantForce1(f_constant = Force1) annotation(
Placement(visible = true, transformation(origin = {-48, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
equation
connect(constantForce1.flange, mass.flange_a) annotation(
@@ -12,4 +14,4 @@
Line(points = {{22, 0}, {0, 0}}, color = {0, 127, 0}));
annotation(
uses(Modelica(version = "3.2.3")));
-end masstest;
+end masstest2;

パッと見の差分としては・・・。
parameterってのが増えて、
そこのparameterを元に作った変数なのかな?
Force1、Force2をconstantForceの引数に渡ってる感じ?

そうだね。
元々constantForce1とconstantForce2には-1と2の即値が入っていた。
それをparameterで定義した変数で渡すようにした。

うーん、これって意味あるの?
動作としては全く変わらないような・・・。

まぁこのままだと意味はないかもね。
間に変数を挟んだことで、ソースコードとしてのパラメータを調整し易くなったとかはあると思うけど。

うーん、Modelicaコードでパラメータを弄るのと
OpenModelica Connection Editorでパラメータを弄るのでは
どっちが楽ってのもなさそうだなー。

そうだろうね。
どっちもどっちって感じだ。

じゃー、意味ないじゃん!!

このparameterを間に挟んだ効能は・・・。

効能は?

次回説明だ!

(まじか・・・。)
まとめ

まとめだよ。
- Modelicaコードを弄ることでいろいろと効率化される可能性がある。
- 実際にparameterキーワードを使って変数を定義。そして、それをconstantForceに設定。
- 今のままでは動作は何も変わらない。
- ソースコード上でパラメータ調整をし易くなったくらいの効能しかない。
- 即値で調整するのでは労力に差はさなそう。
バックナンバーはこちら。
コメント