JModelica.org には、Modelica Standard Library - Version 3.2.2 (MSL) が含まれています。
JModelica.org User Guide - Version 2.10 5.4.1 の手順に従ってシミュレーションを実行します。
必要なライブラリを import します。
%matplotlib notebook
import matplotlib.pyplot as plt
from pymodelica import compile_fmu
from pyfmi import load_fmu
モデルをコンパイルして FMU を作成し、ロードします。 MSL に含まれるクラスについては、compile_fmu の引数としてモデル名のみをフルネームで指定すればよく、ファイル名を指定する必要はありません。また、環境変数 MODELICAPATH の設定も不要です。
# Compile model
fmu_name = compile_fmu("Modelica.Mechanics.Rotational.Examples.First")
# Load model
model = load_fmu(fmu_name)
シミュレーションを実行します。
# Load result file
res = model.simulate(final_time=3.)
Final Run Statistics: --- Number of steps : 627 Number of function evaluations : 844 Number of Jacobian evaluations : 12 Number of function eval. due to Jacobian eval. : 48 Number of error test failures : 46 Number of nonlinear iterations : 840 Number of nonlinear convergence failures : 0 Solver options: Solver : CVode Linear multistep method : BDF Nonlinear solver : Newton Linear solver type : DENSE Maximal order : 5 Tolerances (absolute) : [ 1.00000000e-06 1.00000000e-06 1.00000000e-10 1.00000000e-06] Tolerances (relative) : 0.0001 Simulation interval : 0.0 - 3.0 seconds. Elapsed simulation time: 0.0512900352478 seconds.
シミュレーション結果をプロットします。
w1 = res['inertia1.w']
w2 = res['inertia2.w']
w3 = res['inertia3.w']
tau = res['torque.tau']
t = res['time']
plt.figure(1)
plt.subplot(2,1,1)
plt.plot(t,w1,t,w2,t,w3)
plt.grid(True)
plt.legend(['inertia1.w','inertia2.w','inertia3.w'])
plt.subplot(2,1,2)
plt.plot(t,tau)
plt.grid(True)
plt.legend(['tau'])
plt.xlabel('time [s]')
plt.show()