モデルをコンパイルしてFMUを作成します。
from pymodelica import compile_fmu
fmu_name = compile_fmu("Modelica.Fluid.Examples.Tanks.EmptyTanks")
FMUをロードします。
from pyfmi import load_fmu
model = load_fmu(fmu_name)
オプションでソルバーを LSODAR に変えてシミュレーションを実行します。 オプションの詳細は help(opts) で確認できます。
opts = model.simulate_options()
opts['solver'] = "LSODAR"
res = model.simulate(options=opts)
Final Run Statistics: --- Number of steps : 74 Number of function evaluations : 203 Number of Jacobian evaluations : 10 Number of state function evaluations : 145 Number of state events : 3 Solver options: Solver : LSODAR Absolute tolerances : [ 1.00000000e-06 3.00000000e-04 1.00000000e-06 3.00000000e-04] Relative tolerances : 0.0001 Starter : classical Simulation interval : 0.0 - 50.0 seconds. Elapsed simulation time: 0.0151219367981 seconds.
シミュレーション結果を取得します。
level1 = res['tank1.level']
level2 = res['tank2.level']
mflow = res['pipe.port_a.m_flow']
t = res['time']
プロットします。
%matplotlib notebook
import matplotlib.pyplot as plt
plt.figure(1)
plt.subplot(2,1,1)
plt.plot(t, level1, t, level2)
plt.ylabel('Tank Level (m)')
plt.legend(['Tank1 Level','Tank2 Level'])
plt.subplot(2,1,2)
plt.plot(t,mflow)
plt.ylabel('mass flow rate (kg/s)')
plt.legend(['Pipe Mass Flow Rate'])
plt.xlabel('Time (s)')
plt.show()