Search code examples
pythonmodelicaopenmodelica

How to run modelica model using OMPython with external library package (i.e. Buildings)


I am struggling to load my model with external library. RoomM is my model and Buildings is my library package exported externally to Modelica. Below is the code:

from OMPython import OMCSessionZMQ

from OMPython import ModelicaSystem

omc = OMCSessionZMQ()

omc.sendExpression("loadModel(Modelica)")

omc.sendExpression("loadModel(C:\OpenModelica1.18.1-64bit\lib\omlibrary\Buildings-9.1.0\Buildings-9.1.0\Buildings\Buildings.mo)")

model_path=omc.sendExpression("loadFile(\"C:\Modelica-python\Modelica-Model/RoomM.mo\")")

Solution

  • Use loadModel to load Modelica libraries that are located in a location OpenModelica knows about (OpenModelica installation directory, %HOME%/.openmodelica/libraries and locations from environment variable OPENMODELICALIBRARY).

    If you installed Buildings library with the package manager you can use

    import OMPython
    from OMPython import OMCSessionZMQ
    
    omc = OMCSessionZMQ()
    omc.sendExpression("loadModel(Buildings, {\"9.1.0\"})")
    

    False is returned in case omc can't find the specified library for the given version. You can use the package manager to install it.


    If you want to load the package.mo file instead you need to use loadFile:

    import OMPython
    from OMPython import OMCSessionZMQ
    
    omc = OMCSessionZMQ()
    omc.sendExpression("loadFile(\"C:\OpenModelica1.18.1-64bit\lib\omlibrary\Buildings-9.1.0\Buildings-9.1.0\Buildings\Buildings.mo\")")
    

    Remember to escape quoted strings with \" if the API function has string inputs.

    See also the OMPython section of OpenModelica User's Guide. It has an example on how to load Modelica libraries and files.