Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModelicaSystem does not show plot window #144

Open
bilderbuchi opened this issue Aug 16, 2021 · 2 comments
Open

ModelicaSystem does not show plot window #144

bilderbuchi opened this issue Aug 16, 2021 · 2 comments

Comments

@bilderbuchi
Copy link

bilderbuchi commented Aug 16, 2021

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.

@arun3688
Copy link
Collaborator

@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")

@bilderbuchi
Copy link
Author

bilderbuchi commented Aug 16, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants