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

2024-06-07 06:39:11 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我一直遵循的一个过程是使用FMPy.gui打开FMU,查看参数列表,然后在脚本中使用它们,但我想知道是否有更简单的方法,以便我可以在Jupyter笔记本中列出参数,并根据需要更改参数


Tags: 方法模型脚本列表参数过程gui用例
3条回答

对于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

在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']

相关问题 更多 >