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

I have an OpenModelica model created using OMEdit. To get a concrete example, I developed the following:

OpenModelica Model from OMEdit called myGain

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

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

Now the simulation is running and the results are written to a file.

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

How can i do this?

Since the parameter is compiled into a model, it cannot be changed. So I need a model like this:

myGain with a variable as parameter

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

Using the OMPython.execute ("simulate (...)") command, I can specify some environment variables, such as "numberOfIntervals" or "outputFormat", but no more.

+6
source share
3 answers

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 more details.

You can also use the buildModel(...) command followed by system("./ModelName -overrideFile ...") to avoid re-translation and re-compilation or with some small scripts for parallel parameterization of scripts. If you are using Linux or OSX, you should easily call OMPython to create the executable, and then call it yourself. On Windows, you need to set up some environment variables so that they work as expected.

+2
source

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 while a is a parameter in your model.

0
source
  • Create a new folder in Windows

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

  • The contents of file1.py:


 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 contents of file2.bat:

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

A window with a graphical result will appear.

0
source

All Articles