pythonmodelicafmi

Import FMU parameters using FMPy


I'm trying to import in Python a model created in Modelica with the FMU method. The library I'm currently using is FMPy and it is working. I can introduce the corresponding inputs and I get the same results as the original model.

The problem is that I can't change the parameters of the model before the simulation in python. There are some parameters that can be change in the model but since they are not established as inputs, I can't work with them in my script.

I've tried changing the library to PyFMI but I found FMIPy to work best. The following code is my best try to import the parameters:

parameters = [v for v in model_description.modelVariables if v.causality == 'parameter']
for v in model_description.modelVariables:             
    if v.causality == 'v':                    
        print('%-25s %s' % (variable.name, variable.start))

Solution

  • I read your post again and I see a small misstake in your Python code to extract all parameters. Try this code instead:

    parameters = [v for v in model_description.modelVariables if v.causality == 'parameter']
    for p in parameters:             
       print('%-25s %s' % (p.name, p.start))
    

    Note that you list both parameters of the numerical procedure as well as parameters and initial states of the model.

    When you do a simulation with FMPy you make a call like:

    sim_res = simulate_fmu(
       filename = fmu_model,
       start_time = 0,
       stop_time = 
       start_values = 
       output = 
    }
    

    and you give the simulation function the appropriate information where I have mainly blanks.

    start_values should be a dictionary that contains both parameters and states initial values.

    For example if you have a model m with parameter p that you want to be 1.0, the dictionary should be: start_values = {'m.p': 1.0}

    I suggest that you first make this dictionary 'start_values' manually to see that it all works. Later you can make some script that extract exactly the information from model_description that you want to address and vary in your simulations.