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)
/usr/local/lib/python2.7/dist-packages/ipykernel_launcher.py:2: UserWarning: The model, DirectInertia, does not support directional derivatives which is necessary in-case of an algebraic loop. The simulation might become unstable... /usr/local/lib/python2.7/dist-packages/ipykernel_launcher.py:2: UserWarning: The model, InverseInertia, does not support directional derivatives which is necessary in-case of an algebraic loop. The simulation might become unstable...
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)
/usr/local/lib/python2.7/dist-packages/ipykernel_launcher.py:1: UserWarning: Linear correction only supported if directional derivatives are available. """Entry point for launching an IPython kernel.
Elapsed initialization time: 0.00193905830383 seconds. Master Algorithm options: Algorithm : Jacobi (fixed-step) Execution : Serial Extrapolation Order : 0 Step-size : 0.01 Algebraic loop : True Linear Correction : False Statistics: Number of global steps : 100 Simulation interval : 0.0 - 1.0 seconds. Elapsed simulation time : 0.0547690391541 seconds. 0.002795 seconds spent in DirectInertia. 0.000860 seconds spent in InverseInertia. 0.042885 seconds spent saving simulation result.
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)
[<matplotlib.lines.Line2D at 0x7f728c326850>]
結局、linear_correction が有効になりませんでした。