英文:
OpenModelica on Python and external library using mod=ModelicaSystem
问题
I can help you with the translation. Here's the translated code part:
我可以帮你翻译以下代码部分(Building 是外部库,RoomM 是模型),但我想要使用 mod=ModelicaSystem... 来能够编辑参数。
from OMPython import OMCSessionZMQ
from OMPython import ModelicaSystem
omc = OMCSessionZMQ()
omc.sendExpression("loadModel(Modelica)")
omc.sendExpression("loadFile(\"C:\\OpenModelica1.18.1-64bit\\lib\\omlibrary\\Buildings-9.1.0\\Buildings-9.1.0\\Buildings\\package.mo\")")
omc.sendExpression("loadFile(getInstallationDirectoryPath() + \"/share/doc/omc/testmodels/RoomM.mo\")")
omc.sendExpression("instantiateModel(RoomM)")
omc.sendExpression("simulate(RoomM, stopTime=1.0)")
...以下是失败的代码部分:
mod=ModelicaSystem("C:/OpenModelica1.18.1-64bit/share/doc/omc/testmodels/RoomM.mo","RoomM", ["C:\\OpenModelica1.18.1-64bit\\lib\\omlibrary\\Buildings-9.1.0\\Buildings-9.1.0\\Buildings\\package.mo"])
Notification: Skipped loading package Buildings (9.1.0,default) using MODELICAPATH C:/OpenModelica1.18.1-64bit/lib/omlibrary (uses-annotation may be wrong).
Please note that I've translated the code portion and excluded the request for translation as you requested.
英文:
I can run the simulation using below code (Building is external library and RoomM is the model), but I want to use mod=ModelicaSystem....to be able to edit parameters.
from OMPython import OMCSessionZMQ
from OMPython import ModelicaSystem
omc = OMCSessionZMQ()
omc.sendExpression("loadModel(Modelica)")
omc.sendExpression("loadFile(\"C:\OpenModelica1.18.1-
64bit\lib\omlibrary\Buildings-9.1.0\Buildings-
9.1.0\Buildings\package.mo\")")
omc.sendExpression("loadFile(getInstallationDirectoryPath() +
\"/share/doc/omc/testmodels/RoomM.mo\")")
omc.sendExpression("instantiateModel(RoomM)")
omc.sendExpression("simulate(RoomM, stopTime=1.0)")
...below is the code that fails:
mod=ModelicaSystem("C:/OpenModelica1.18.1-
64bit/share/doc/omc/testmodels/RoomM.mo","RoomM",
["C:\OpenModelica1.18.1-64bit\lib\omlibrary\Buildings-9.1.0\Buildings-
9.1.0\Buildings\package.mo"])
Notification: Skipped loading package Buildings (9.1.0,default) using
MODELICAPATH C:/OpenModelica1.18.1-64bit/lib/omlibrary (uses-
annotation may be wrong).
答案1
得分: 1
以下是您要求的翻译内容:
对于以下的Modelica模型
NeedBuildings.mo
model NeedBuildings
extends Buildings.Examples.Tutorial.SpaceCooling.System1
annotation(uses(Buildings, version="9.1"));
end NeedBuildings;
你可以使用以下代码初始化一个ModelicaSystem:
from OMPython import ModelicaSystem
mod = ModelicaSystem("NeedBuildings.mo", "NeedBuildings", ["Buildings"])
重要的是,所需的库位于默认位置,并且可以被OpenModelica找到。
如果您的模型RoomM
有正确的uses
注释,甚至无需指定要加载哪些库,它将自动加载。
所以只需调用:
mod = ModelicaSystem("NeedBuildings.mo", "NeedBuildings")
- 第一个输入参数必须是一个字符串,其中包含Modelica代码的文件名,具有Modelica文件扩展名".mo"。如果Modelica文件不在Python的当前目录中,则必须包括文件路径。
- 第二个输入参数必须是一个字符串,其中包含Modelica模型的名称,包括命名空间,如果模型包含在Modelica包内。
- 第三个输入参数(可选)用于指定依赖库或依赖的Modelica文件列表。
- 第四个输入参数(可选)是一个关键字参数,用于设置命令行选项。
英文:
For the following Modelica Model
NeedBuildings.mo
model NeedBuildings
extends Buildings.Examples.Tutorial.SpaceCooling.System1
annotation(uses(Buildings, version="9.1"));
end NeedBuildings;
you can initialize a ModelicaSystem with:
from OMPython import ModelicaSystem
mod=ModelicaSystem("NeedBuildings.mo","NeedBuildings", ["Buildings"])
The important part is, that the needed libraries are in a default location and can be found by OpenModelica.
If your model RoomM
has a correct uses
annotation you don't even need to specify what libraries should be loaded, it will be loaded automatically.
So it's enough to call:
mod=ModelicaSystem("NeedBuildings.mo","NeedBuildings")
From the OpenModelica User's Guide:
> - The first input argument must be a string with the file name of the Modelica code, with Modelica file extension ".mo". If the Modelica file is not in the current directory of Python, then the file path must also be included.
> - The second input argument must be a string with the name of the Modelica model including the namespace if the model is wrapped within a Modelica package.
> - The third input argument (optional) is used to specify the list of dependent libraries or dependent Modelica files.
> - The fourth input argument (optional), is a keyword argument which is used to set the command line options.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论