OpenModelica 生成的 C 代码:我需要在不同的机器上进行参数化运行

发布于 2025-01-20 22:26:43 字数 146 浏览 4 评论 0原文

我在OpenModelica上有一个简单的模型。我生成了模型的C代码(使用导出FMU功能),并想调整代码,以便对参数进行参数化研究并打印输出。换句话说,我希望在运行可执行文件时,将参数传递给包含特定参数值的可执行文件,并在文件中打印模型的某些输出的值。

有可能吗?

I have a simple model on OpenModelica. I generated a C-code (using export FMU functionality) of the model and I want to adjust the code so as to make a parametrised study of a parameter and print the output. In other words, I want that when I run the executable, to pass an argument to the executable which contains the value of a certain parameter, and print the value of some output of the model in a file.

Is that possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可是我不能没有你 2025-01-27 22:26:43

是的,这是可能的。
您提到了FMU导出,所以我会坚持使用此信息,这可能是最简单的方法。

FMU内的二进制文件是编译包含所有内部运行时和内容的库,以模拟您的导出模型。
这些库通常由FMU导入工具来处理,例如 Omsimulator a href =“ https://fmpy.readthedocs.io/en/latest/” rel =“ nofollow noreferrer”> fmpy 。

我不会推荐直接更改生成的C代码。这很困难,您无需这样做。

在您的FMU导入工具中,您通常可以在运行仿真之前通过FMI2Setxxx函数更改Varaibles和参数的值。大多数工具将接口调用的工具包装到某些函数中以更改值。看看工具的文档。


因此,让我们看一个例子。我正在使用OpenModelica导出Helloworld和Python的Omsigulator,但请检查您的工具并使用预先使用的东西。

model helloWorld
  parameter Real a = 1;
  Real x(start=1,fixed=true);
equation
 der(x) = a*x;
end helloWorld

用file-> fort-> fmu或右键单击模型和导出 - > fmu的file-> fmu中的OMEDIT中的2.0模型交换FMU导出。
您可以在工具 - > fmi中找到导出设置。

现在,我拥有helloworld.fmu,并且可以使用Omsimulator进行仿真。我想将参数的值Helloworld.a更改为2.0,并使用setReal来做到这一点。

>>> from OMSimulator import OMSimulator
>>> oms = OMSimulator()
>>> oms.newModel("helloWorld")
>>> oms.addSystem("helloWorld.root", oms.system_sc)
>>> oms.addSubModel("helloWorld.root.fmu", "helloWorld.fmu")
>>> oms.instantiate("helloWorld")
>>> oms.setReal("helloWorld.root.fmu.a", 2.0)
>>> oms.initialize("helloWorld")
info:    maximum step size for 'helloWorld.root': 0.001000
info:    Result file: helloWorld_res.mat (bufferSize=10)
>>> oms.simulate("helloWorld")
>>> oms.terminate("helloWorld")
info:    Final Statistics for 'helloWorld.root':
         NumSteps = 1001 NumRhsEvals  = 1002 NumLinSolvSetups = 51
         NumNonlinSolvIters = 1001 NumNonlinSolvConvFails = 0 NumErrTestFails = 0
>>> oms.delete("helloWorld")

现在,在此添加一个循环以迭代参数的不同值,并执行您想要的后处理。

Yes that is possible.
You mentioned FMU export, so I'll stick to this and it is probably the easiest way.

The binaries inside a FMU are compiled libraries containing all internal runtimes and stuff to simulate your exported model.
These libraries are usually handled by an FMU importing tool, e.g. OMSimulator (part of OpenModelica) or FMPy.

I wouldn't recomment changing the generated C code directly. It is difficult and you don't need to do it.

In your FMU import tool you can usually change values of varaibles and parameters via the fmi2SetXXX functions before runing a simulation. Most tools wrap that interface call into some function to change values. Have a look at the documentation of your tool.


So let's look at an example. I'm using OpenModelica to export helloWorld and OMSimulator from Python, but check your tool and use what you prefere.

model helloWorld
  parameter Real a = 1;
  Real x(start=1,fixed=true);
equation
 der(x) = a*x;
end helloWorld

Export as 2.0 Model Exchange FMU in OMEdit with File->Export->FMU or right-click on your model and Export->FMU.
You can find your export settings in Tools->Options->FMI.

Now I have helloWorld.fmu and can simulate it with OMSimulator. I want to change the value of parameter helloWorld.a to 2.0 and use setReal to do so.

>>> from OMSimulator import OMSimulator
>>> oms = OMSimulator()
>>> oms.newModel("helloWorld")
>>> oms.addSystem("helloWorld.root", oms.system_sc)
>>> oms.addSubModel("helloWorld.root.fmu", "helloWorld.fmu")
>>> oms.instantiate("helloWorld")
>>> oms.setReal("helloWorld.root.fmu.a", 2.0)
>>> oms.initialize("helloWorld")
info:    maximum step size for 'helloWorld.root': 0.001000
info:    Result file: helloWorld_res.mat (bufferSize=10)
>>> oms.simulate("helloWorld")
>>> oms.terminate("helloWorld")
info:    Final Statistics for 'helloWorld.root':
         NumSteps = 1001 NumRhsEvals  = 1002 NumLinSolvSetups = 51
         NumNonlinSolvIters = 1001 NumNonlinSolvConvFails = 0 NumErrTestFails = 0
>>> oms.delete("helloWorld")

Now add a loop to this to iterate over different values of your parameter and do whatever post-processing you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文