Integrate Modelica and Python on Windows

This is the method of linking Modelica and Python introduced in "I want to easily build a model-based development environment".

Purpose

The purpose of this time is to perform the following operations.

-** Create a physical model with Modelica. ** ** -** Compile the physical model into FMU format with Python's PyModelica module (JModelica). ** ** -** Calculate the FMU file with Python's PyFMI module (JModelica). ** ** -** Display the calculation result as a graph with Python's Matplotlib module. ** **

Environmental setting

Execution environment

Installation of Anaconda2

Anaconda_Logo.png

Build a Python execution environment in Anaconda as a coupled simulation master. Since JModelica described later supports only Python2 32bit, Anaconda also uses the one that supports it. (Even if another Python environment is already installed, I think that there is no problem if you install it additionally.)

Download the "Python 2.7 version (32bit)" version of the installer from the following page. Download Anaconda Now! | Continuum

Run the downloaded installer and basically keep the defaults. Set the installation folder to C: \ Anaconda2.

Install JModelica

Install JModelica to use the Python modules PyModelica and PyFMI.

JM_1.PNG

Download the latest installer from the page below. Download | JModelica.org

When installing the Python module, select the installation destination to be C: \ Anaconda2.

Create a Modelica model

This time, we will use the Modelica.Thermal.HeatTransfer.Exsamples.Motor introduced in" Using OpenModelica on Windows" saved in Modelica format. ..

I saved it in Motor.mo of" ozawaat / FMI_Motor ", so please use this.

Save Motor.mo in a suitable location.

Compile the Modelica model into FMU format

Launch Python from JModelica

Manipulate Python in the folder where Motor.mo is stored. Use one of the following to start Python.

  1. Open IPython from [Windows mark at the bottom left of the screen] → [JModelica.org-1.17] and go to the console. `` `! Cd C: \ [folder where Motor.mo is stored] \ ``` Enter to move the folder.

  2. Copy ʻIPython.bat from C: \ JModelica.org-1.17to the same folder asMotor.mo. Double-click on this ʻIPython.bat to open IPython.

IPython.png

Compile the Modelica file into FMU

Save the following files in the same folder as Motor.mo.

Motor_Compile.py


from pymodelica import compile_fmu
model_name = 'Motor'
mo_file = 'Motor.mo'
my_fmu = compile_fmu(model_name, mo_file, target='cs')

From the IPython console

run Motor_Compile.py


 Enter to compile `Motor.mo` and create` Motor.fmu`.



# Calculate FMU in Python and display it on the graph

 Save the following files in the same folder as `Motor.mo`.


#### **`Motor.py`**
```python

import matplotlib.pyplot as plt
from pyfmi import load_fmu

model = load_fmu('Motor.fmu')
opts = model.simulate_options()
opts["ncp"] = 1000

res = model.simulate(final_time=100000, options=opts)

T1 = res['Twinding.T']
T2 = res['Tcore.T']
t = res['time']

plt.plot(t, T1, label="Twinding.T")
plt.plot(t, T2, label="Tcore.T")
plt.legend(loc='best')
plt.xlabel('time [sec]')
plt.ylabel('Temperature [K]')
plt.show()

From the IPython console

run Motor.py


 If you enter, `Motor.fmu` will be calculated on Python and the graph below will be displayed.

 ![Motor_plot.png](https://qiita-image-store.s3.amazonaws.com/0/139905/09bbad72-3ebb-a814-4ccb-a8a7bf229cfe.png)


Recommended Posts

Integrate Modelica and Python on Windows
Python 3.6 on Windows ... and to Xamarin.
Python on Windows
python basic on windows ②
Install python on windows
Install and run Python3.5 + NumPy + SciPy on Windows 10
(Windows) Causes and workarounds for UnicodeEncodeError on Python 3
Notes on installing Python3 and using pip on Windows7
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
[Note] Installing Python 3.6 + α on Windows and RHEL
Install ZIP version Python and pip on Windows 10
Set-enable Python virtualenv on Windows
Run Openpose on Python (Windows)
Install watchdog on Windows + Python 3.3
Python on Ruby and angry Ruby on Python
Install Python and Flask (Windows 10)
Python + Kivy development on Windows
Sphinx-autobuild (0.5.2) on Windows7, Python 3.5.1, Sphinx 1.3.5
Fastest Python installation on Windows
Build Python environment on Windows
Build python environment on windows
I ran python on windows
[Python] [Chainer] [Windows] Install Chainer on Windows
Use Python on Windows (PyCharm)
[Windows] [Python3] Install python3 and Jupyter Notebook (formerly ipython notebook) on Windows
Create a decent shell and python environment on Windows
Python environment construction memo on Windows 10
Until you create Python Virtualenv on Windows and launch Jupyter
Installing Kivy on Windows10 64bit Python3.5
Install python2.7 on windows 32bit environment
Install xgboost (python version) on Windows
Install Python on Windows + pip + virtualenv
Install Pytorch on Blender 2.90 python on Windows
Installing Kivy-Designer on Windows10 64bit Python3.5
Install python and Visual Studio Code on windows10 (April 2020 version)
Mecab / Cabocha / KNP on Python + Windows
Mastering pip and wheel on windows
Install Python development environment on Windows 10
Python CGI file created on Windows
Getting started with Python 3.8 on Windows
Reproduce One-Touch Search on Python 3.7.3. (Windows 10)
Install easy_install and pip on windows
Build a 64-bit Python 2.7 environment with TDM-GCC and MinGW-w64 on Windows 7
Notes for using OpenCV on Windows10 Python 3.8.3.
[UE4] Build DedicatedServer on Windows and Linux
Python and Bash on Cisco Catalyst IOS-XE
Run servo with Python on ESP32 (Windows)
Python 2.7, 3.4, 3.5 extension module build environment on Windows
docker and PyCharm under proxy on windows
Build Python3 and OpenCV environment on Ubuntu 18.04
[Kivy] How to install Kivy on Windows [Python]
Install wsl2 and master linux on windows
Python virtual environment and packages on Ubuntu
A memo with Python2.7 and Python3 on CentOS
Virtualenv does not work on Python3.5 (Windows)
Creating a python virtual environment on Windows
Notes on building Python and pyenv on Mac
Use without installing python 2.x on Windows
[Beginner] Installing Python and running programs (Windows)
See file and folder information on python
Run py.test on Windows Anaconda and MinGW