modelon-community / PyFMI

PyFMI is a package for loading and interacting with Functional Mock-Up Units (FMUs) both for Model Exchange and Co-Simulation, which are compiled dynamic models compliant with the Functional Mock-Up Interface (FMI)
http://jmodelica.org/pyfmi
GNU Lesser General Public License v3.0
151 stars 38 forks source link

Is PyFMI capable to measure the performance of FMI model? #230

Closed michael-MB17 closed 1 week ago

michael-MB17 commented 2 months ago

Hi all,

I would like to ask you if it is possible to somehow measure the performance of FMI model and compare it with others. Is there any integrated function inside the library which provides an overview about the time and memory consumed for calculations? Alternatively, it could be measured by python functions. Would it make sense?

Thank you in advance for any suggestion.

Michael

PeterMeisrimelModelon commented 2 months ago

Hi Michael,

the question is fairly general, but I'll try to provide an overview.

With ME FMUs, PyFMI uses the Assimulo solvers. Their memory consumption simply scales with the problem size (number of states) and is essentially as low as required.

Simulation performance can be gathered by simulation time & generated run statistics, which provide a breakdown over the various function calls. There is also the dynamic_diagnosticsoption which accumulates additional diagnostics information from the solvers and can be very useful for diagnosing e.g., slow simulations. Usage of the option itself can however significantly slow down simulations.

With CS FMUs and generally all computations within the FMU, the FMU is responsible for its own memory management and computations. There is no standardized way of accessing this. It is possible that such information might be saved in some variables of the model and/or log, but this ultimately becomes a question for the FMU generating tool.

Additionally a result object has the additional attribute detailed_timings, which provides a breakdown on where time was spent. This can look like this:

{'initializing_fmu': 0.0031871999999992795, 'initializing_result': 0.0049546000000013635, 'storing_result': 0.08350810000005993, 'computing_solution': 0.47022099999993827, 'returning_result': 0.00799040000000062, 'other': 0.5537398000000024, 'total': 1.123601100000002}

/Peter

michael-MB17 commented 2 months ago

Hi Peter,

Thank you for providing me the overview. I will try it out!

Michael

michael-MB17 commented 1 month ago

Hi Peter,

May I have follow-up question regarding this topic? I setup now the environment of PyFMI. I wish to run the dynamic_diagnostics and detailed_timingscommands and get the same results as you showed. However, I didn't find exact syntax in documentation. How to implement it in this code?

from pyfmi import load_fmu

model = load_fmu('BouncingBall.fmu')
res = model.simulate(final_time=5)

Additionally, I would like to ask you if it's possible to also somehow get average CPU and memory load consumed by .fmu model on device where it is executed in Python script using this library.

Thank you in advance for possible support.

Michael

PeterMeisrimelModelon commented 1 month ago

Hi Michael,

model = load_fmu(<your model>)

opts = model.simulate_options()
opts['dynamic_diagnostics'] = True

res = model.simulate(final_time = 5, options = opts)
print(res.detailed_timings)

dynamic_diagnostics will generate a lot of extra variables in res with an @Diagnostics prefix.

There is no specific functionality in PyFMI to monitor CPU & memory load from the FMU. An FMU may contain such functionality, but how to access it is a question for the FMU generating tool you used.

/Peter

michael-MB17 commented 1 month ago

Hi Peter,

Thank you for clarification and providing me the code. Should I also import something specific?

image image

Thank you in advance for clarifying also this.

Michael

PeterMeisrimelModelon commented 1 month ago

Hi Michael,

no specific imports required. I forgot to mention that dynamic_diagnostics is for ModelExchange FMUs only. The error you are seeing in the first picture is due to the fact that you have a CoSimulation FMU.

With CoSimulation FMUs the solvers are part of the FMU, so any diagnostics information has to come from the FMU or its outputs, such as logging information.

/Peter

michael-MB17 commented 1 month ago

Hi Peter,

I see and will obtain ModelExchange FMU and try it out.

Thank you for clarifying this!

Michael