Retrieving FMU co-simulation results when using Master in PyFMI

169 views Asked by At

I am trying to co-simulate two Matlab-Simulink FMUs using the Master from PyFMI:

from pyfmi import Master
    """ Loading FMUs, establishing connection between models, defining Master simulator and simulation options """
    dummy_model = load_fmu('dummy.fmu', log_level = 4)
    batt_model = load_fmu('battery.fmu', log_level = 4)
    
    models = [dummy_model, batt_model]
    connection = [(dummy_model, 'P_to_batt_fmu', batt_model, 'P_to_batt_fmu')]
    
    mastersim = Master(models, connection)
    opts = mastersim.simulate_options()
    opts['step_size'] = 1000
    opts['result_handling'] = 'memory'

    """ Simulating """
    res = mastersim.simulate(final_time = 1000, options = opts)

Everything until here runs as expected. The problem comes when trying to retrieve the results from res, since I cannot call the names of the output variables in my Simulink models. These have names like 'Ibatt', 'SoC' or 'Qbatt' (see image below), and I would expect them to be the keys of res. Output variable names in Simulink

But they aren't. In fact, res appears to be a python dictionary with very strange keys and values, and until now I have been unable to make heads or tails of it:

{0: <pyfmi.common.algorithm_drivers.AssimuloSimResult object at 0x000002B3AC3CCF10>,
 <pyfmi.fmi.FMUModelCS2 object at 0x000002B3AB8BC800>: <pyfmi.common.algorithm_drivers.AssimuloSimResult object at 0x000002B3AC3CCF10>,
 1: <pyfmi.common.algorithm_drivers.AssimuloSimResult object at 0x000002B3AC3CE1A0>,
 <pyfmi.fmi.FMUModelCS2 object at 0x000002B3AB889670>: <pyfmi.common.algorithm_drivers.AssimuloSimResult object at 0x000002B3AC3CE1A0>}

As I said before, I would expect res to be a dictionary with very straightforward keys, which is what happens when simulating just one of the FMUs:


batt_model = load_fmu('battery.fmu', log_level = 4)

opts = batt_model.simulate_options()

opts['ncp'] = 1000

res = batt_model.simulate(final_time=1000, options = opts)

If I run the code above, then res.keys() are just the names of my Simulink output variables.

My questions are: How can I find my variables in res when using Master? How can I work with them in an easy way?

So far I have been able to work with a results.txt file by stating opts['result_handling'] = 'file', however I would much prefer storing and working with the results in 'memory'

0

There are 0 answers