GenerationOfFMUs (2) - with_linear_correction

CO-SIMULATION USING THE OPEN-SOURCE PYTHON PACKAGE PYFMI の最後の方に記述された linear_correction = True にしたシミュレーションを試みます。


In [1]:
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')


In [2]:
from pyfmi import load_fmu
directInertia = load_fmu("DirectInertia.fmu")
inverseInertia = load_fmu("InverseInertia.fmu")


In [3]:
directInertia.set("J", 1.1)
inverseInertia.set("J", 2.2)


In [4]:
models = [directInertia, inverseInertia]
connections = [
    (directInertia, "phi", inverseInertia, "phi"),
    (directInertia, "w", inverseInertia, "w"),
    (directInertia, "a", inverseInertia, "a"),
    (inverseInertia, "tau", directInertia, "tau")]


In [5]:
from pyfmi.master import Master
coupled_simulation = Master(models, connections)
/usr/local/lib/python2.7/dist-packages/ 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/ 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 にします。

In [6]:
opts = coupled_simulation.simulate_options()
opts["step_size"] = 0.01
opts["linear_correction"] = True

directInertia モデルの入力信号用オブジェクトを生成します。

In [7]:
import numpy as np
f = lambda time: 10*np.sin(2*np.pi*2*time)
input_object = ((directInertia, "tauDrive"), f)

Co-Simulation を実行します。

In [8]:
res = coupled_simulation.simulate(options=opts, input=input_object)
/usr/local/lib/python2.7/dist-packages/ 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

 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 が計算できる場合のみサポートされるという旨のワーニングがでます。 シミュレーション結果を抽出してプロットします。

In [9]:
inverseInertia_time = res[inverseInertia]["time"]
inverseInertia_phi = res[inverseInertia]["inertia.flange_b.phi"]
In [10]:
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot(inverseInertia_time, inverseInertia_phi)
[<matplotlib.lines.Line2D at 0x7f728c326850>]

結局、linear_correction が有効になりませんでした。

In [ ]: