OpenModelicaの使い方 User’s Guide解読_7

OpenModelica

引き続きUser’s Guideを読んでいきます。
22ページからの内容となります。
前回の記事はこちら

様々なモデルを操作するAPI(Application Programming Interface)について解説があります。
例が示してありますが、詳しくはOpenModelica System Documentationを見てねという感じでしょうか。
一通り使うのにはここまでの知識はなくてもいいかもしれません。
自分は軽く読んで終わりました。
それぞれの関数をOpen Modelica System Documentationで検索すると解説があります。(たぶん)
例はBouncingBall.moを使っています。

>> loadFile(getInstallationDirectoryPath() + "/share/doc/omc/testmodels/BouncingBall.mo");

>> list(BouncingBall)
"model BouncingBall
  parameter Real e = 0.7 \"coefficient of restitution\";
  parameter Real g = 9.81 \"gravity acceleration\";
  Real h(fixed = true, start = 1) \"height of ball\";
  Real v(fixed = true) \"velocity of ball\";
  Boolean flying(fixed = true, start = true) \"true, if ball is flying\";
  Boolean impact;
  Real v_new(fixed = true);
  Integer foo;
equation
  impact = h <= 0.0;
  foo = if impact then 1 else 2;
  der(v) = if flying then -g else 0;
  der(h) = v;
  when {h <= 0.0 and v <= 0.0, impact} then
    v_new = if edge(impact) then -e*pre(v) else 0;
    flying = v_new > 0;
    reinit(v, v_new);
  end when;
end BouncingBall;"

>> getClassRestriction(BouncingBall)
"model"

>> getClassInformation(BouncingBall)
("model","",false,false,false,"C:/Program Files/OpenModelica1.22.1-64bit/share/doc/omc/testmodels/BouncingBall.mo",false,1,1,23,17,{},false,false,"","",false,"","","","","")

>> isFunction(BouncingBall)
false

>> existClass(BouncingBall)
true

>> getComponents(BouncingBall)
{{Real, e, "coefficient of restitution", "public", false, false, false, false, "parameter", "none", "unspecified", {}},{Real, g, "gravity acceleration", "public", false, false, false, false, "parameter", "none", "unspecified", {}},{Real, h, "height of ball", "public", false, false, false, false, "unspecified", "none", "unspecified", {}},{Real, v, "velocity of ball", "public", false, false, false, false, "unspecified", "none", "unspecified", {}},{Boolean, flying, "true, if ball is flying", "public", false, false, false, false, "unspecified", "none", "unspecified", {}},{Boolean, impact, "", "public", false, false, false, false, "unspecified", "none", "unspecified", {}},{Real, v_new, "", "public", false, false, false, false, "unspecified", "none", "unspecified", {}},{Integer, foo, "", "public", false, false, false, false, "unspecified", "none", "unspecified", {}}}

>> getConnectionCount(BouncingBall)
0

>> getInheritanceCount(BouncingBall)

0

>>  getComponentModifierValue(BouncingBall,e)
"0.7"

>> getComponentModifierNames(BouncingBall,"e")

{}

>> getVersion() 
"OpenModelica v1.22.1 (64-bit)"

XML形式やMATLAB mファイル形式でモデルの中身をファイルとして出力する機能がありそうです。
このあたり詳しくないので認識違っていたら教えてください。
dumpXMLDAE, exportDAEtoMatlabというコマンドです。

>> exportDAEtoMatlab(BouncingBall)
"The equation system was dumped to Matlab file:BouncingBall_imatrix.m"

出力されたBouncngBall_imatrix.mが下記のようになります。
C:\Users\[ユーザ名]\AppData\Local\Temp\OpenModelicaというフォルダに出力されています。
ファイルの出力先は設定できるのかな。。。
cdでディレクトリを変えて実行すればよさそうな気もします。

% Adjacency Matrix
% ====================================
% number of rows: 6
IM={{3,6},{1,{'if', 'true','==' {3},{},}},{{'if', 'true','==' {4},{},}},{5},{2,{'if', 'edge(impact)' {3},{5},}},{4,2}};
VL = {'foo','v_new','impact','flying','v','h'};


EqStr = {'impact = h <= 0.0;','foo = if impact then 1 else 2;','der(v) = if flying then -g else 0.0;','der(h) = v;','when {h <= 0.0 and v <= 0.0, impact} then v_new = if edge(impact) then (-e) * pre(v) else 0.0; end when;','when {h <= 0.0 and v <= 0.0, impact} then flying = v_new > 0.0; end when;'};


OldEqStr={'class BouncingBall','  parameter Real e = 0.7 "coefficient of restitution";','  parameter Real g = 9.81 "gravity acceleration";','  Real h(start = 1.0, fixed = true) "height of ball";','  Real v(fixed = true) "velocity of ball";','  Boolean flying(start = true, fixed = true) "true, if ball is flying";','  Boolean impact;','  Real v_new(fixed = true);','  Integer foo;','equation','  impact = h <= 0.0;','  foo = if impact then 1 else 2;','  der(v) = if flying then -g else 0.0;','  der(h) = v;','  when {h <= 0.0 and v <= 0.0, impact} then','    v_new = if edge(impact) then -e * pre(v) else 0.0;','    flying = v_new > 0.0;','    reinit(v, v_new);','  end when;','end BouncingBall;',''};

今回はここまでにします。

コメント

タイトルとURLをコピーしました