【问题标题】:Is there a way to list of parameters of FMU (or of submodel in FMU) using the python libraries FMPy or pyFMI?有没有办法使用 python 库 FMPy 或 pyFMI 列出 FMU(或 FMU 中的子模型)的参数?
【发布时间】:2020-06-14 22:51:27
【问题描述】:

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

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

【问题讨论】:

    标签: python modelica fmi pyfmi


    【解决方案1】:

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

    【讨论】:

    • 那行得通。是否有 pyfmi 的所有此类功能的列表?我在看jmodelica.org/pyfmi/tutorial.html 但是我找不到这样的附加功能。您的解决方案绝对有帮助。我想我可以编写额外的脚本来过滤掉 MyModel.MySubModel 中的参数。如果您有任何建议,请告诉我。
    • 目前没有这样的列表。一般的经验法则是可以使用高级方法检索所有内容。为了获取有关变量的数据,我建议使用模型对象上的交互式帮助并检查例如方法:model.get_variable_*
    【解决方案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']
    

    【讨论】:

      【解决方案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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多