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

时间:2020-03-02 15:46:19

标签: python modelica fmi pyfmi

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

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

3 个答案:

答案 0 :(得分:4)

在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)

答案 1 :(得分:2)

使用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']

答案 2 :(得分:2)

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