How can I call an OpenModelica model in Python with OMPython?

后端 未结 3 1823
逝去的感伤
逝去的感伤 2021-02-15 23:27

I have an OpenModelica model made with OMEdit. In order to get a concrete example I designed the following:

\"OpenMo

相关标签:
3条回答
  • 2021-02-16 00:12
    • Create one new folder in windows

    • In this folder put/create 2 new files file1.py and file2.bat

    • The file1.py content is:


    import os
    import sys
    sys.path.insert(0, "C:\OpenModelica1.11.0-32bit\share\omc\scripts\PythonInterface")
    from OMPython import OMCSession
    sys.path.insert(0, "C:\OpenModelica1.11.0-32bit\lib\python")
    os.environ['USER'] = 'stefanache'
    omc = OMCSession()
    omc.sendExpression("loadModel(Modelica)")
    omc.sendExpression("loadFile(getInstallationDirectoryPath() + \"/share/doc/omc/testmodels/BouncingBall.mo\")")
    omc.sendExpression("instantiateModel(BouncingBall)")
    omc.sendExpression("simulate(BouncingBall)")
    omc.sendExpression("plot(h)")`
    
    • the file2.bat content is:

    @echo off
    python file1.py
    pause
    
    • then click on file2.bat... and please be patient!

    The plotted result window will appear.

    0 讨论(0)
  • 2021-02-16 00:13

    You can send more flags to the simulate command. For example simflags to override parameters. See https://openmodelica.org/index.php/forum/topic?id=1011 for some details.

    You can also use the buildModel(...) command followed by system("./ModelName -overrideFile ...") to avoid re-translation and re-compilation or with some minor scripting parallel parameter sweeps. If you use Linux or OSX it should be easy to call OMPython to create the executable and then call it yourself. On Windows you need to setup some environment variables for it to work as expected.

    0 讨论(0)
  • I believe you are looking for the setParameterValue command. You can read about it here: https://openmodelica.org/download/OMC_API-HowTo.pdf

    Basically you would add a line similar to OMPython.execute("setParameterValue(myGain, a, 20)") to your python script before the line where you run the simulation, so long as a is a parameter in your model.

    0 讨论(0)
提交回复
热议问题