pythonjupyter-notebookfmi

How to bundle an FMU with a png-file that illustrates the system?


I use FMU a lot for simulation of Modelica models in a Python environment. It would be nice to bundle the obtained FMU from compilation with a png-file that shows the system. Is that possible?

And how to access that png-file and show it in a Jupyter notebook?


Solution

  • With the input from Christian and Dag and reading about package zipfile https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile.open we can handle the problem as follows for FMU (2.0):

    1. Create a file 'processDiagram.png' by for instance a screenshot of the Modelica-configuration.
    2. Unzip the FMU.
    3. Place the png-file in a folder called "documentation" at the top in parallell to the modelDescription-xml file. If the folder is not there you create one.
    4. Zip the FMU and name it for example: 'fmu_model.fmu'

    The person who receive this FMU can now do the following in Python to show the process_diagram.png

    import matplotlib.pyplot as plt
    import matplotlib.image as img
    import zipfile
    
    fmu_file = zipfile.ZipFile('fmu_model.fmu', 'r')
    process_diagram = fmu_file.open('documentation/processDiagram.png')
    plt.imshow(img.imread(process_diagram))
    plt.axis('off')
    plt.show()
    

    For a safe handling we need to also include a system with checksum or similar to avoid the possibility to corrupt the FMU with wrong png-files. Design of this safety handling I leave to the designers of PyFMI and FMPy. In fact FMPy has some general handling of checksum already for FMI 2.0, but to my knowledge no functionality to extract a process_diagram file.