OMPython icon indicating copy to clipboard operation
OMPython copied to clipboard

ModelicaSystem does not show plot window

Open bilderbuchi opened this issue 3 years ago • 2 comments

I am trying to plot results using the ModelicaSystem API, but the plot window is not shown. This is on Windows, using OMPython master and a recent OM nightly(OpenModelica v1.18.0-dev.beta1 (64-bit)).

Based on examples from the documentation, using the omc API works, it shows a plot window (the sleep is just there so that the window does not immediately disappear when the script is finished):

from OMPython import OMCSessionZMQ
omc = OMCSessionZMQ()
cmds = [
  'loadFile(getInstallationDirectoryPath() + "/share/doc/omc/testmodels/BouncingBall.mo")',
  "simulate(BouncingBall)",
  "plot(h)"
  ]
for cmd in cmds:
  answer = omc.sendExpression(cmd)
  print("\n{}:\n{}".format(cmd, answer))
from time import sleep
sleep(5)

Doing the same thing (I hope?!) with the ModelicaSystem API does not show a window:

from time import sleep

from OMPython import OMCSessionZMQ
omc = OMCSessionZMQ()
model_path=omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels/"
from OMPython import ModelicaSystem
mod=ModelicaSystem(model_path + "BouncingBall.mo","BouncingBall")
mod.simulate()
print('now plot ')
mod.sendExpression('plot(h)')
print(mod.requestApi('getErrorString'))
print('done, now sleep')
sleep(5)

output (notice: nothing in the error string):

LOG_SUCCESS       | info    | The initialization finished successfully without homotopy method.
LOG_SUCCESS       | info    | The simulation finished successfully.
now plot

done, now sleep

I tried to find out what's going wrong, but couldn't find anything.

bilderbuchi avatar Aug 16 '21 15:08 bilderbuchi

@bilderbuchi the ModelicaSystem() API uses a different approach to produce the simulation results, it basically builds the model first and then simulate() API uses the simulation executable to produce the result file and the result file is not stored insendExpression() when using ModelicaSystem() API, so inorder to use the plot() API with ModelicaSystem() we have to explicitly specify the result file to the plot() API some thing like below

mod=ModelicaSystem(model_path + "BouncingBall.mo","BouncingBall")
mod.simulate()
print('now plot ')
mod.resultfile ### this will show you the resultfile location
mod.sendExpression("plot(h, fileName=\"BouncingBall_res.mat\")")

The above will work

And We can also add a new API, plot() to ModelicaSystem() API which automatically loads the the result file and plots the result in OMPlot and you can directly call mod.plot() something like below

mod=ModelicaSystem(model_path + "BouncingBall.mo","BouncingBall")
mod.simulate()
mod.plot("h")

arun3688 avatar Aug 16 '21 16:08 arun3688

ah, so there is some hidden state created when using the omc simulate() function, that plot() needs? I did not know that. In any case, I find it useful that ModelicaSystem uses the exe, this way one can easily re-simulate/override without rebuilding.

Thank you for your suggestion, this works! Sorry, this was not obvious to me.

By the way, you can mix ' and " in Python strings which is useful in case you need to have strings with quotations marks, you don't need escapes:

mod.sendExpression('plot(h, fileName="{}")'.format(mod.resultfile))

Yeah, a new method plot() that mirrors the OMC plot signature for flexibility, and just uses the resultfile if no fileName argument is given, could be useful.

Same actually for a getError() method -- that would be quite useful because you have to do that sooo often when interacting with omc. 😀 This also could reduce a lot of duplication in the OMPython ModelicaSystem code, there's lots of ..."getErrorString"... strewn around...

bilderbuchi avatar Aug 16 '21 17:08 bilderbuchi