CO-SIMULATION USING THE OPEN-SOURCE PYTHON PACKAGE PYFMI の最後の方に記述された linear_correction = True にしたシミュレーションを試みます。
サブシステムモデルをコンパイルしてFMUを生成します。
from pymodelica import compile_fmu
fmu1 = compile_fmu('Modelica.Mechanics.Rotational.Examples.Utilities.DirectInertia',target='cs', version='2.0', compile_to='DirectInertia.fmu')
fmu2 = compile_fmu('Modelica.Mechanics.Rotational.Examples.Utilities.InverseInertia',target='cs',version='2.0', compile_to='InverseInertia.fmu')
サブシステムモデルをロードします。
from pyfmi import load_fmu
directInertia = load_fmu("DirectInertia.fmu")
inverseInertia = load_fmu("InverseInertia.fmu")
パラメータを設定します。
directInertia.set("J", 1.1)
inverseInertia.set("J", 2.2)
サブシステムモデルのリストと接続関係のリストを作成します。
models = [directInertia, inverseInertia]
connections = [
(directInertia, "phi", inverseInertia, "phi"),
(directInertia, "w", inverseInertia, "w"),
(directInertia, "a", inverseInertia, "a"),
(inverseInertia, "tau", directInertia, "tau")]
Co-Simulationモデルを作成します。
from pyfmi.master import Master
coupled_simulation = Master(models, connections)
directional derivatives を計算する機能がサポートされていないというワーニングが出ています。この機能はモデルどうしの接続で代数ループがある場合は必要になるようです。
オプションを設定します。コミュニケーションステップサイズを設定し、linear_correction を False にします。
opts = coupled_simulation.simulate_options()
opts["step_size"] = 0.01
opts["linear_correction"] = True
directInertia モデルの入力信号用オブジェクトを生成します。
import numpy as np
f = lambda time: 10*np.sin(2*np.pi*2*time)
input_object = ((directInertia, "tauDrive"), f)
Co-Simulation を実行します。
res = coupled_simulation.simulate(options=opts, input=input_object)
linear correction は、directional derivative が計算できる場合のみサポートされるという旨のワーニングがでます。 シミュレーション結果を抽出してプロットします。
inverseInertia_time = res[inverseInertia]["time"]
inverseInertia_phi = res[inverseInertia]["inertia.flange_b.phi"]
%matplotlib inline
import matplotlib.pyplot as plt
plt.figure(1)
plt.plot(inverseInertia_time, inverseInertia_phi)
結局、linear_correction が有効になりませんでした。