Functional Mock-up Interface は、様々なダイナミックシステムのシミュレーションツール間でモデル交換やCo-Simulationを行うための規格(satandard)です。規格書は、https://fmi-standard.org/downloads/ からダウンロードできます。
JModelica.org を使用すると、
に従った Functional Mock-up Unit (FMU) を生成することと、FMUを読み込んでシミュレーションをの実行することができます。デフォルトは ME20 です。ここでは、これらの FMU の生成とシミュレーションを行います。
モデルは、FMI1.0 FMI for Co-Simulationについて P.22 で例題として作成したもので、元は Modelica by Exampleの跳ね返るボールのモデル Bouncing Ball を参考にして、反発係数 e を入力信号とし、y=h を出力信号としたものです。これに対して上記の4種類の FMU を生成し、各FMUについて、入力信号が無い場合(e=0.8)と、入力信号 e が1秒で1.0から0.7に変化する場合の2ケースのシミュレーションを実行します。
モデルのソースコードを作成します。
%%writefile BouncingBall.mo
model BouncingBall
import SI = Modelica.SIunits;
Modelica.Blocks.Interfaces.RealInput e(start=0.8);
Modelica.Blocks.Interfaces.RealOutput y;
SI.Height h;
SI.Velocity v;
parameter SI.Acceleration g = Modelica.Constants.g_n;
parameter SI.Height h0 = 1.0;
initial equation
h = h0;
equation
v = der(h);
der(v) = -g;
y = h;
when h < 0 then
reinit(v, -e * pre(v));
end when;
end BouncingBall;
Overwriting BouncingBall.mo
ソースコードをコンパイルして、FMIの各規格に従った4種類の FMU を作成します。
from pymodelica import compile_fmu
fmu = compile_fmu("BouncingBall","BouncingBall.mo", version='1.0', target='me',compile_to = "BouncingBallME10.fmu")
fmu = compile_fmu("BouncingBall","BouncingBall.mo", version='2.0', target='me',compile_to = "BouncingBallME20.fmu")
fmu = compile_fmu("BouncingBall","BouncingBall.mo", version='1.0', target='cs',compile_to = "BouncingBallCS10.fmu")
fmu = compile_fmu("BouncingBall","BouncingBall.mo", version='2.0', target='cs',compile_to = "BouncingBallCS20.fmu")
入力信号を表す関数を作成します。
def stepf(t):
startTime = 1.0
if t < startTime:
return 1.0
else:
return 0.7
図化のために Matplotlib をインポートします。
%matplotlib inline
import matplotlib.pyplot as plt
from pyfmi import load_fmu
4種類のFMUについて、入力信号がない場合とある場合の2ケースのシミュレーションを実行し、結果の入力信号eと出力信号yをプロットします。
names = ["BouncingBallME10.fmu","BouncingBallME20.fmu","BouncingBallCS10.fmu","BouncingBallCS20.fmu"]
i = 0
for name in names:
i += 1
print('\n')
print('%d %s' % (i, name))
# Simulate without input signal
model = load_fmu(name)
opts = model.simulate_options()
opts["ncp"]=500
res1 = model.simulate(final_time = 3, options=opts)
# Simulate with input signal
model = load_fmu(name)
opts = model.simulate_options()
opts["ncp"]=500
res2 = model.simulate(final_time = 3, options=opts, input=("e",stepf))
# Plot simulation results
plt.figure(i)
plt.subplot(2,1,1)
plt.title(name)
plt.plot(res1["time"], res1["e"],res1["time"],res1["y"])
plt.legend(["e","y"])
plt.subplot(2,1,2)
plt.plot(res2["time"], res2["e"],res2["time"],res2["y"])
plt.legend(["e","y"])
1 BouncingBallME10.fmu Final Run Statistics: BouncingBallME10 Number of steps : 106 Number of function evaluations : 164 Number of Jacobian evaluations : 13 Number of function eval. due to Jacobian eval. : 26 Number of error test failures : 0 Number of nonlinear iterations : 112 Number of nonlinear convergence failures : 0 Number of state function evaluations : 231 Number of state events : 12 Solver options: Solver : CVode Linear multistep method : BDF Nonlinear solver : Newton Linear solver type : DENSE Maximal order : 5 Tolerances (absolute) : 1e-06 Tolerances (relative) : 0.0001 Simulation interval : 0.0 - 3.0 seconds. Elapsed simulation time: 0.0217440128326 seconds. Final Run Statistics: BouncingBallME10 Number of steps : 106 Number of function evaluations : 166 Number of Jacobian evaluations : 13 Number of function eval. due to Jacobian eval. : 26 Number of error test failures : 1 Number of nonlinear iterations : 114 Number of nonlinear convergence failures : 0 Number of state function evaluations : 239 Number of state events : 12 Solver options: Solver : CVode Linear multistep method : BDF Nonlinear solver : Newton Linear solver type : DENSE Maximal order : 5 Tolerances (absolute) : 1e-06 Tolerances (relative) : 0.0001 Simulation interval : 0.0 - 3.0 seconds. Elapsed simulation time: 0.0366840362549 seconds. 2 BouncingBallME20.fmu Final Run Statistics: --- Number of steps : 106 Number of function evaluations : 164 Number of Jacobian evaluations : 13 Number of function eval. due to Jacobian eval. : 26 Number of error test failures : 0 Number of nonlinear iterations : 112 Number of nonlinear convergence failures : 0 Number of state function evaluations : 231 Number of state events : 12 Solver options: Solver : CVode Linear multistep method : BDF Nonlinear solver : Newton Linear solver type : DENSE Maximal order : 5 Tolerances (absolute) : 1e-06 Tolerances (relative) : 0.0001 Simulation interval : 0.0 - 3.0 seconds. Elapsed simulation time: 0.0213289260864 seconds. Final Run Statistics: --- Number of steps : 106 Number of function evaluations : 166 Number of Jacobian evaluations : 13 Number of function eval. due to Jacobian eval. : 26 Number of error test failures : 1 Number of nonlinear iterations : 114 Number of nonlinear convergence failures : 0 Number of state function evaluations : 239 Number of state events : 12 Solver options: Solver : CVode Linear multistep method : BDF Nonlinear solver : Newton Linear solver type : DENSE Maximal order : 5 Tolerances (absolute) : 1e-06 Tolerances (relative) : 0.0001 Simulation interval : 0.0 - 3.0 seconds. Elapsed simulation time: 0.0345189571381 seconds. 3 BouncingBallCS10.fmu Simulation interval : 0.0 - 3.0 seconds. Elapsed simulation time: 0.0159890651703 seconds. Simulation interval : 0.0 - 3.0 seconds. Elapsed simulation time: 0.0230710506439 seconds. 4 BouncingBallCS20.fmu Simulation interval : 0.0 - 3.0 seconds. Elapsed simulation time: 0.0138819217682 seconds. Simulation interval : 0.0 - 3.0 seconds. Elapsed simulation time: 0.0238161087036 seconds.