#!/usr/bin/env python # coding: utf-8 # ### GenerationOfFMUs (1) - without_linear_correction # [CO-SIMULATION USING THE OPEN-SOURCE PYTHON PACKAGE PYFMI](https://www.modelon.com/co-simulation-using-the-open-source-python-package-pyfmi/) に記述された手順にしたがって、MSLの # * Modelica.Mechanics.Rotational.Examples.Utilities.DirectInertia # * Modelica.Mechanics.Rotational.Examples.Utilities.InverseInertia # # の Co-Simulation モデルを作成してシミュレーションを実行します。 # # サブシステムモデルをコンパイルしてFMUを生成します。 # 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[4]: from pyfmi import load_fmu directInertia = load_fmu("DirectInertia.fmu") inverseInertia = load_fmu("InverseInertia.fmu") # パラメータを設定します。 # In[5]: directInertia.set("J", 1.1) inverseInertia.set("J", 2.2) # サブシステムモデルのリストと接続関係のリストを作成します。 # In[6]: models = [directInertia, inverseInertia] connections = [ (directInertia, "phi", inverseInertia, "phi"), (directInertia, "w", inverseInertia, "w"), (directInertia, "a", inverseInertia, "a"), (inverseInertia, "tau", directInertia, "tau")] # Co-Simulationモデルを作成します。 # In[7]: from pyfmi.master import Master coupled_simulation = Master(models, connections) # directional derivatives を計算する機能がサポートされていないというワーニングが出ています。この機能はモデルどうしの接続で代数ループがある場合は必要になるようです。 # # オプションを設定します。コミュニケーションステップサイズを設定し、linear_correction を False にします。 # In[8]: opts = coupled_simulation.simulate_options() opts["step_size"] = 0.01 opts["linear_correction"] = False # directInertia モデルの入力信号用オブジェクトを生成します。 # In[9]: import numpy as np f = lambda time: 10*np.sin(2*np.pi*2*time) input_object = ((directInertia, "tauDrive"), f) # Co-Simulation を実行します。 # In[10]: res = coupled_simulation.simulate(options=opts, input=input_object) # シミュレーション結果を抽出してプロットします。 # In[11]: inverseInertia_time = res[inverseInertia]["time"] inverseInertia_phi = res[inverseInertia]["inertia.flange_b.phi"] # In[12]: get_ipython().run_line_magic('matplotlib', 'inline') import matplotlib.pyplot as plt plt.figure(1) plt.plot(inverseInertia_time, inverseInertia_phi) # 計算結果は正しくありませんが、上記のリンクの資料と一致しています。 # In[ ]: