【发布时间】:2020-06-14 22:51:27
【问题描述】:
我有一个模型的 FMU,用例是更改 FMU 的参数值以查看对结果的影响。如果我无法访问 Modelica 模型,有没有办法使用 FMPy 或 pyFMI 列出 FMU 的顶级参数?
我一直在遵循的过程之一是使用 FMPy.gui 打开 FMU 并浏览参数列表,然后在脚本中使用它们,但我想知道是否有更简单的方法可以做到这一点我可以在 Jupyter 笔记本中列出并根据需要更改参数吗?
【问题讨论】:
我有一个模型的 FMU,用例是更改 FMU 的参数值以查看对结果的影响。如果我无法访问 Modelica 模型,有没有办法使用 FMPy 或 pyFMI 列出 FMU 的顶级参数?
我一直在遵循的过程之一是使用 FMPy.gui 打开 FMU 并浏览参数列表,然后在脚本中使用它们,但我想知道是否有更简单的方法可以做到这一点我可以在 Jupyter 笔记本中列出并根据需要更改参数吗?
【问题讨论】:
在 FMI 中,顶级参数和其他参数没有区别。使用 PyFMI (FMI 2.0) 列出模型中的所有可用参数:
from pyfmi import load_fmu
import pyfmi.fmi as fmi
model = load_fmu("MyModel.fmu")
params = model.get_model_variables(causality=fmi.FMI2_PARAMETER)
【讨论】:
使用 fmpy,您可以遍历模型描述中的 modelVariables,如下所示:
from fmpy import read_model_description
from fmpy.util import download_test_file
fmu_filename = 'CoupledClutches.fmu'
download_test_file('2.0', 'CoSimulation', 'MapleSim', '2016.2', 'CoupledClutches', fmu_filename)
model_description = read_model_description(fmu_filename)
parameters = [v for v in model_description.modelVariables if v.causality == 'parameter']
【讨论】:
对于 fmpy,您还可以检查这个 jupyter 笔记本:https://notebooks.azure.com/t-sommer/projects/CoupledClutches,其中包含以下行
model_description = read_model_description(filename) # read the model description
for variable in model_description.modelVariables: # iterate over the variables
if variable.causality == 'parameter': # and print the names of all parameters
print('%-25s %s' % (variable.name, variable.start)) # and the respective start values
【讨论】: