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

3

14

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

OpenModelica Model from OMEdit called myGain

Now I would like to run the model in Python. I can do this by using OMPython. After importing OMPython and loading the files I use the following command to run the simulation:

result = OMPython.execute("simulate(myGain, numberOfIntervals=2, outputFormat=\"mat\")")

The simulation now runs and the results are written to a file.

Now I would like to run the same model but with an different parameter for the constant block.

How can I do this?

Since the parameter is compiled into the model it should not be possible to change it. So what I need is a model like that:

myGain with a variable as parameter

Is it possible to call the model from Python and set the variable "a" to a specific value?

With the command OMPython.execute("simulate(...)") I can specify some environment variables like "numberOfIntervals" or "outputFormat" but not more.

Corrinnecorrival answered 7/1, 2014 at 7:49 Comment(1)
have you solved this problem?Hideaway
K
5

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.

Kinky answered 8/1, 2014 at 9:47 Comment(0)
I
3

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.

Incoordinate answered 21/7, 2014 at 17:58 Comment(1)
is there a command like this for modelica.blocks instead of parameter?Hideaway
U
2
  • 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.

Ungraceful answered 21/7, 2017 at 16:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.