如何使用 JModelica 编译和模拟作为包一部分的 modelica 模型?

How to compile and simulate a modelica model that is part of a package with JModelica?

我的问题与question of janpeter. I study the ebook by Tiller and try to simulate the example 'Architecture Driven Approach' with OpenModelica and JModelica类似。我在 OpenModelica 中尝试了最小示例 'BaseSystem',它工作正常。但是对于 JModelica 1.14 版,我在编译过程中遇到错误并且我的脚本失败了。我的 python 脚本是:

import matplotlib.pyplot as plt
from pymodelica import compile_fmu
from pyfmi import load_fmu

# Variables: modelName, modelFile, extraLibPath
modelName = 'BaseSystem'
modelFile = 'BaseSystem.mo'
extraLibPath = 'C:\Users\Tom\Desktop\Tiller2015a\ModelicaByExample\Architectures'
compilerOption = {'extra_lib_dirs':[extraLibPath]}

# Compile model
fmuName = compile_fmu( modelName, modelFile, compiler_options=compilerOption)

# Load model
model = load_fmu( fmuName)

# Simulate model
res = model.simulate( start_time=0.0, final_time=5.0)

# Extract interesting values
res_w = res['sensor.w']
res_y = res['setpoint.y']
tSim = res['time']

# Visualize results
fig = plt.figure(1)
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
ax1.plot(tSim, res_w, 'g-')
ax2.plot(tSim, res_y, 'b-')
ax1.set_xlabel('t (s)')
ax1.set_ylabel('w (???)', color='g')
ax2.set_ylabel('y (???)', color='b')
plt.title('BaseSystem')
plt.legend()
plt.grid(True)
plt.show()

我的问题是如何编译和模拟作为包一部分的模型?

我不是 jModelica 用户,但我认为我在您的脚本中看到了一些混乱。您写道:

modelName = 'BaseSystem'
modelFile = 'BaseSystem.mo'
extraLibPath = 'C:\Users\Tom\Desktop\Tiller2015a\ModelicaByExample\Architectures'

但这意味着(对我而言)编译器应该打开存储在 C:\Users\Tom\Desktop\Tiller2015a\ModelicaByExample\Architectures 的包。但是顶层包是ModelicaByExample,你要的型号是Architectures.BaseSystem。所以我认为这样的事情可能更合适:

modelName = 'Architectures.BaseSystem'
modelFile = 'package.mo'
extraLibPath = 'C:\Users\Tom\Desktop\Tiller2015a\ModelicaByExample'

这里的重点是您应该打开 ModelicaByExample(具体来说,ModelicaByExample 目录中的 package.mo 文件)。这将打开 ModelicaByExample 包。您需要打开此包,因为它是顶级包。您不能只加载一个子包(这就是您尝试做的事情)。然后,一旦你加载了 ModelicaByExample,你可以要求编译器专门编译 Architectures.BaseSystem.

我怀疑 OpenModelica 是 "helping" 你打开顶层包,即使你要求它打开子包。

但同样,我对 jModelica 不是很了解,我绝对没有测试过任何这些建议。

谢谢迈克尔·蒂勒。在您的支持下,我找到了解决方案。

首先,modelName 必须是完全合格的。其次,正如您提到的,extraLibPath 应该在库 ModelicaByExample 的顶级目录结束。但是后来我遇到了错误,JModelica 找不到属于 Modelica 标准库 (MSL).

的组件或声明

所以我在 MSL 中添加了 modelicaLibPath,但错误消息仍然相同。经过多次尝试,我以管理员权限启动命令行,所有错误都消失了。

这是可执行的 python 脚本:BaseSystem.py

###  Attention!
# The script and/or the command line must be
# started with administrator privileges

import matplotlib.pyplot as plt
from pymodelica import compile_fmu
from pyfmi import load_fmu

# Variables: modelName, modelFile, extraLibPath
modelName = 'Architectures.SensorComparison.Examples.BaseSystem'
modelFile = ''
extraLibPath = 'C:\Users\Tom\Desktop\Tiller2015a\ModelicaByExample'
modelicaLibPath = 'C:\OpenModelica1.9.2\lib\omlibrary\Modelica 3.2.1'
compileToPath = 'C:\Users\Tom\Desktop\Tiller2015a'

# Set the compiler options
compilerOptions = {'extra_lib_dirs':[modelicaLibPath, extraLibPath]}

# Compile model
fmuName = compile_fmu( modelName, modelFile, compiler_options=compilerOptions, compile_to=compileToPath)

# Load model
model = load_fmu( fmuName)

# Simulate model
res = model.simulate( start_time=0.0, final_time=5.0)

# Extract interesting values
res_w = res['sensor.w']
res_y = res['setpoint.y']
tSim = res['time']

# Visualize results
fig = plt.figure(1)
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
ax1.plot(tSim, res_w, 'g-')
ax2.plot(tSim, res_y, 'b-')
ax1.set_xlabel('t (s)')
ax1.set_ylabel('sensor.w (rad/s)', color='g')
ax2.set_ylabel('setpoint.y (rad/s)', color='b')
plt.title('BaseSystem')
plt.legend()
plt.grid(True)
plt.show()