環境はWindowsでWSLは使っていない。
mainはOMCSessionZMQを使用する。
main2はModelicaSystemを使用する。
from OMPython import ModelicaSystem
from OMPython import OMCSessionZMQ
import pandas as pd
from matplotlib import pyplot as plt
def main():
"""
<https://openmodelica.org/doc/OpenModelicaUsersGuide/latest/ompython.html#ompython>
"""
omc = OMCSessionZMQ()
print(omc.sendExpression("getVersion()"))
omc.sendExpression("loadModel(Modelica)")
omc.sendExpression("loadFile(\\"sample.mo\\")")
result = omc.sendExpression("simulate(myPackage.ex1.myModel, stopTime=10.0, outputFormat=\\"csv\\")")
print(result)
df = pd.read_csv("myPackage.ex1.myModel_res.csv", index_col=0)
plt.figure()
plt.plot(df.index, df["a"])
plt.show()
def main2():
mod=ModelicaSystem("sample.mo","myPackage.ex1.myModel", ["Modelica"])
print(mod.getParameters())
mod.setSimulationOptions(["stopTime=10.0","outputFormat=\\"csv\\""])
mod.simulate()
df = pd.read_csv("myPackage.ex1.myModel_res.csv", index_col=0)
plt.figure()
plt.plot(df.index, df["a"])
plt.show()
if __name__ == "__main__":
main()
print("-----")
main2()
実行ログ
OpenModelica v1.26.1 (64-bit)
[OMC log for 'sendExpression(simulate(myPackage.ex1.myModel, stopTime=10.0, outputFormat="csv"), True)']: [translation:warning:496] The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->Show additional information from the initialization process, in OMNotebook call setCommandLineOptions("-d=initialization").
{'resultFile': 'C:/Users/mmer547/Documents/openmodelica/myPackage.ex1.myModel_res.csv', 'simulationOptions': "startTime = 0.0, stopTime = 10.0, numberOfIntervals = 500, tolerance = 1e-6, method = 'dassl', fileNamePrefix = 'myPackage.ex1.myModel', options = '', outputFormat = 'csv', variableFilter = '.*', cflags = '', simflags = ''", 'messages': 'LOG_SUCCESS | info | The initialization finished successfully without homotopy method.\\nLOG_SUCCESS | info | The simulation finished successfully.\\n', 'timeFrontend': 0.1111182, 'timeBackend': 0.0028818, 'timeSimCode': 0.0006119, 'timeTemplates': 0.0104471, 'timeCompile': 0.917712, 'timeSimulation': 0.0777868, 'timeTotal': 1.1208512}
-----
[OMC log for 'sendExpression(buildModel(myPackage.ex1.myModel, variableFilter=".*"), True)']: [translation:warning:496] The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->Show additional information from the initialization process, in OMNotebook call setCommandLineOptions("-d=initialization").
{'k': '10.0', 'm': '1.0'}
c:\\Users\\mmer547\\Documents\\openmodelica\\run_python_to_modelica.py:28: DeprecationWarning: The definition of values to set should use a dictionary, i.e. {'key1': 'val1', 'key2': 'val2', ...}. Please convert all cases which use a string ('key=val') or list ['key1=val1', 'key2=val2', ...]
mod.setParameters("m=2.0")
{'k': '10.0', 'm': '2.0'}
c:\\Users\\mmer547\\Documents\\openmodelica\\run_python_to_modelica.py:30: DeprecationWarning: The definition of values to set should use a dictionary, i.e. {'key1': 'val1', 'key2': 'val2', ...}. Please convert all cases which use a string ('key=val') or list ['key1=val1', 'key2=val2', ...]
mod.setSimulationOptions(["stopTime=10.0","outputFormat=\\"csv\\""])
mainの実行で出力されるグラフ

main2の実行で出力されるグラフ

main2は実行されておらず、mainの結果ファイルが読み込まれている。
WindowsでOMPythonを実行するときはOMCSessionZMQ形式を使う必要がある。