使用OMpython库,并参照如下官网
OpenModelica Python Interface and PySimulator
详细安装步骤如下:
To install OMPython follow the instructions at:
OMPython
因为我所使用的模型都基于VehicleInterfaces库,并且模型abc_vehicle03位于VehicleInterfaces.Examples下。
因此,ModelicaSystem函数中的fileName一定要指向VehicleInterfaces 2.0.0/package.mo文件,modelName指向我们要导入的模型文件"VehicleInterfaces.Examples.abc_vehicle03"
from OMPython import OMCSessionZMQ, ModelicaSystem
omc = OMCSessionZMQ()
#VehicleInterfaces模型库的路径
lib_path = omc.sendexpression("getInstallationDirectoryPath()") + "/lib/omlibrary/VehicleInterfaces 2.0.0/package.mo"
#加载VehicleInterfaces模型库,并选择导入的model
mod = ModelicaSystem(fileName=lib_path,
modelName="VehicleInterfaces.Examples.abc_vehicle03")
#BuildModel
mod.buildModel()
#仿真求解设置
mod.setSimulationOptions(["stopTime=1200.0", "stepSize=0.01"])
#开始计算
mod.simulate()



