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

3

6

I have a FMU of a model and the use case is to change parameter values of the FMU to see the impact on the results. Is there a way to list top level parameters of the FMU using either FMPy or pyFMI if I dont' have access to the Modelica model?

One of the process I have been following is to open the FMU using FMPy.gui and go through the list of parameters and then use them in the script but I would like to know if there an easier way of doing it so that I can list then in the Jupyter notebook and change the parameters as needed?

Heavyhearted answered 2/3, 2020 at 15:46 Comment(0)
A
2

With fmpy you can loop over the modelVariables in the model description as follows:

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']
Automaton answered 3/3, 2020 at 8:30 Comment(0)
T
4

In FMI there is no distinction between top level parameters and other parameters. To list all available parameters in the model using 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)
Turnip answered 2/3, 2020 at 19:10 Comment(2)
That worked. Is there list of all such functionalities of pyfmi? I was looking at jmodelica.org/pyfmi/tutorial.html however I couldn't find such additional functions. Your solution definitely helps. I guess I can write additional script to filter out parameters from MyModel.MySubModel. Let me know if you have any suggestions.Heavyhearted
There is no such list at the moment. A general rule of thumb is that everything can be retrieved using high-level methods. For getting data on variables I'd recommend to use the interactive help on the model object and check for instance the methods: model.get_variable_*Turnip
A
2

With fmpy you can loop over the modelVariables in the model description as follows:

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']
Automaton answered 3/3, 2020 at 8:30 Comment(0)
R
2

For fmpy you can also inspect this jupyter notebook: https://notebooks.azure.com/t-sommer/projects/CoupledClutches, which contains the lines

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
Reaves answered 3/3, 2020 at 20:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.