本文介绍了如何使用 OMPython 在 Python 中调用 OpenModelica 模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 OMEdit 制作的 OpenModelica 模型.为了得到一个具体的例子,我设计了以下内容:

I have an OpenModelica model made with OMEdit. In order to get a concrete example I designed the following:

现在我想在 Python 中运行模型.我可以通过使用 OMPython 来做到这一点.导入 OMPython 并加载文件后,我使用以下命令运行模拟:

Now I would like to run the model in Python. I can do this by using OMPython. After importing OMPython and loading the files I use the following command to run the simulation:

result = OMPython.execute("simulate(myGain, numberOfIntervals=2, outputFormat=\"mat\")")

现在运行模拟并将结果写入文件.

The simulation now runs and the results are written to a file.

现在我想运行相同的模型,但常量块的参数不同.

Now I would like to run the same model but with an different parameter for the constant block.

我该怎么做?

由于参数已编译到模型中,因此无法更改它.所以我需要的是这样的模型:

Since the parameter is compiled into the model it should not be possible to change it. So what I need is a model like that:

是否可以从 Python 调用模型并将变量a"设置为特定值?

Is it possible to call the model from Python and set the variable "a" to a specific value?

使用命令 OMPython.execute("simulate(...)") 我可以指定一些环境变量,例如numberOfIntervals"或outputFormat",但不能指定更多.

With the command OMPython.execute("simulate(...)") I can specify some environment variables like "numberOfIntervals" or "outputFormat" but not more.

推荐答案

您可以向模拟命令发送更多标志.例如 simflags 来覆盖参数.请参阅 https://openmodelica.org/index.php/forum/topic?id=1011 了解详情.

You can send more flags to the simulate command. For example simflags to override parameters. See https://openmodelica.org/index.php/forum/topic?id=1011 for some details.

您也可以使用 buildModel(...) 命令后跟 system("./ModelName -overrideFile ...") 以避免重新翻译和重新编译或使用一些较小的脚本并行参数扫描.如果您使用 Linux 或 OSX,应该很容易调用 OMPython 来创建可执行文件,然后自己调用它.在 Windows 上,您需要设置一些环境变量才能使其按预期工作.

You can also use the buildModel(...) command followed by system("./ModelName -overrideFile ...") to avoid re-translation and re-compilation or with some minor scripting parallel parameter sweeps. If you use Linux or OSX it should be easy to call OMPython to create the executable and then call it yourself. On Windows you need to setup some environment variables for it to work as expected.

这篇关于如何使用 OMPython 在 Python 中调用 OpenModelica 模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:05