Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

OMPython questions

OMPython questions

Hi,
I'm trying to do scripting with OMPython, through the command OMCSession.sendExpression(). I wonder about the command getErrorString, see https://openmodelica.org/doc/OpenModeli … _api.html. According to the API this should return the error message and the user can choose whether or not to include warnings. However, what I get is all messages: Notifications, Errors and Warnings - even without setting warningsAsErrors = true. Have i misunderstood something or is this a bug?

What I am trying to achieve is to seperate warnings, errors and notifications - can someone advice me on how to accomplish this?

Best Regards!

Edited by: rJonatan - Feb-10-17 12:42:46

Re: OMPython questions

You have misunderstood the documentation. You always get all messages. What warningsAsErrors does is similar to -Werror in gcc (it makes warnings into errors) if you want a more pedantic mode (we use this when developing OM). You can use countMessages or perhaps even getMessagesStringInternal if you want to do something similar to what OMEdit uses.

Re: OMPython questions

Ok, thank you sjoelund.se for the clarification!

Re: OMPython questions

I have another question about using OMPython. I use the command translateModelFMU, which according to the API returns the full path of the generated FMU. Does anyone know how I access this filename through OMPython? If I do

answer = omc.sendExpression('translateModelFMU(my_model)'),

I get a string:

SimCode: The model my_model has been translated to FMU.

But how do I get the path?

Re: OMPython questions

Yeah, we should return the filename, not that message.
Basically it should be my_model.fmu in the current directory (you can get that with cd() command).

Re: OMPython questions

I opened a ticket about this:
https://trac.openmodelica.org/OpenModelica/ticket/4262
and we should fix it.

Re: OMPython questions

OK, thank you ardpo!

Re: OMPython questions

I updated to OpenModelica 1.11.0-32bit and now I have new problems with OMPython. First I got an ImportError: No module named omniORB. This I managed to bypass by adding to path "C:\OpenModelica1.11.0-32bit\lib\python". Now however I don't manage to run commands via OMCSession.sendExpression(). The example I run 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'] = 'jonatan'
omc = OMCSession()

answer = omc.sendExpression("loadModel(Modelica)")
print answer

The error I get is

  File "C:/Users/jonat/workspace/sandbox/test_ompython.py", line 22, in <module>
    answer = omc.sendExpression(cmd)
  File "C:\OpenModelica1.11.0-32bit\share\omc\scripts\PythonInterface\OMPython\__init__.py", line 245, in sendExpression
    answer = OMTypedParser.parseString(result)
  File "C:\OpenModelica1.11.0-32bit\share\omc\scripts\PythonInterface\OMPython\OMTypedParser.py", line 82, in parseString
    return omcGrammar.parseString(string)[0]
  File "C:\OCT-1.0rc1\Python27\lib\site-packages\pyparsing.py", line 1162, in parseString
    raise exc
pyparsing.ParseException: Expected {quoted string, starting with " ending with " | Combine:({["-"] {"0" | W:(1234...,0123...)} [{"." W:(0123...)}] [{W:(eE) W:(0123...,0123...)}]}) | Forward: ... | Group:({Suppress:("{") [Forward: ... [, Forward: ...]...] Suppress:("}")}) | Group:({Suppress:("(") [Forward: ... [, Forward: ...]...] Suppress:(")")}) | {Suppress:("SOME") Suppress:("(") Forward: ... Suppress:(")")} | "true" | "false" | {"NONE" Suppress:("(") Suppress:(")")} | Combine:(Forward: ...)} (at char 1), (line:2, col:1)

Before this command and similar was working fine, can someone help me with what changed?

By the way, I also saw that there are now new features of OMPython: https://openmodelica.org/doc/OpenModeli … interface. Does this perhaps mean that the old syntax is incompatible with new versions of OpenModelica?

Best regards,
Jonatan

Re: OMPython questions

Have you installed OMPython after updating?

Code:


cd C:/OpenModelica1.11.0-32bit/share/omc/scripts/PythonInterface
python setup.py install

Adeel.

Re: OMPython questions

Hi Adeel,

Thank you for the advice. I had tried installing OMPython via pip install, after the update of OpenModelica, but that didn't help me. I also now tried the setup.py install command that you suggested, but I still get the same error. The problem is not with creating an OMCSession object, the problem comes once I want to start executing commands. Any other ideas?

Re: OMPython questions

That's strange because I can run the commands without any issue,

Code:


>>> omc = OMCSession()
2017-02-23 15:58:58,647 - OMCSession - INFO - OMC Server is up and running at file:///c:/users/adeas31/appdata/local/temp/openmodelica.objid.2b4416f5aed74c6a8b907ae7b35b3aab
>>> omc.sendExpression("getVersion()")
'v1.12.0-dev-230-g40f70ce (64-bit)'

However, I am using the 64-bit version but I don't think that is the problem.

Can you post the output of setup.py and the commands above?

Adeel.

Re: OMPython questions

Thank you very much for the feedback. I tried what you suggested and it worked as it should. I also got it to work in my script, perhaps I had to restart my IDE after doing setup.py in order for it to work.

Now I have another error though, so I may post again once I've looked into it. current/smile

Re: OMPython questions

Hi again,

My new error comes when using the command translateModelFMU() via OMPython. Again this was something that ran succesfully before I updated the OpenModelica vesion. Now I get the following warning if I work through the OMShell:

translateModelFMU(x, fileNamePrefix="model")
"SimCode: The model x has been translated to FMU"
Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").

In this case the getErrorString() is empty. If I instead work through OMPython I get some information from getErrorString():

omc.sendExpression('translateModelFMU(x, fileNamePrefix="model")')
'SimCode: The model x has been translated to FMU'
>>> omc.sendExpression('countMessages()')
(4, 0, 4)
>>> omc.sendExpression('getErrorString()')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "OMPython\__init__.py", line 245, in sendExpression
    answer = OMTypedParser.parseString(result)
  File "OMPython\OMTypedParser.py", line 82, in parseString
    return omcGrammar.parseString(string)[0]
  File "C:\OCT-1.0rc1\Python27\lib\site-packages\pyparsing.py", line 1162, in parseString
    raise exc
pyparsing.ParseException: Expected end of text (at char 192), (line:1, col:193)

Anyone who knows what may be causing this problem? And do you know why I get slightly different feedback depending on if I use OMPython or if I work directly in the OMShell? I've experienced this apparent inconsistency several times.

Best greetings!

Edited by: rJonatan - Feb-23-17 16:00:11

Re: OMPython questions

Note that  1.11.0 32bit OMC has issues when building models.
The bug was fixed recently in 12.0-dev:
https://trac.openmodelica.org/OpenModelica/ticket/3894
We should port these fixes to 1.11.1 or so but it will take a while.

There are two alternatives for you:
1. use 64 bit 1.11.0
2. upgrade to 1.12.0-dev 32bit nigthly-builds:
    https://build.openmodelica.org/omc/buil … lds/32bit/

Re: OMPython questions

You get different output because OMShell always call getErrorString() with every command.

The output of translateModelFMU(x, fileNamePrefix="model") is,

"SimCode: The model x has been translated to FMU"

The output of getErrorString() (in this case) is,

"Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization"). "

If you call following commands from OMPython you should get the same output,

Code:


omc.sendExpression('translateModelFMU(x, fileNamePrefix="model")')
omc.sendExpression('getErrorString()')

Adeel.

Re: OMPython questions

as example you can see this sample for Windows

-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!

must apear the plotted result window

Re: OMPython questions

Hi !

I am actually scipting using OM Python, however when I run ModelicaSystem method on a model that I created it generates an empty model with this error one the screen :

model_path = omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels/"
mod = ModelicaSystem(model_path + "CircuitRC.mo", "CircuitRC")
....
...
...
loadFile Error: Notification: Automatically loaded package Modelica 3.2.1 due to uses annotation.

Thanks for your help

There are 0 guests and 0 other users also viewing this topic