有没有办法使用 python 库 FMPy 或 pyFMI 列出 FMU(或 FMU 中的子模型)的参数?

Is there a way to list of parameters of FMU (or of submodel in FMU) using the python libraries FMPy or pyFMI?

我有一个模型的 FMU,用例是更改 FMU 的参数值以查看对结果的影响。如果我无法访问 Modelica 模型,是否可以使用 FMPy 或 pyFMI 列出 FMU 的顶级参数?

我一直遵循的过程之一是使用 FMPy.gui 打开 FMU 并浏览参数列表,然后在脚本中使用它们,但我想知道是否有更简单的方法这样做是为了让我可以在 Jupyter notebook 中列出并根据需要更改参数?

在 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 notebook: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