Skip to content

ModelicaSystem does not show plot window #144

Closed

Description

@bilderbuchi

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.

Activity

  1. arun3688 commented on Aug 16, 2021

    @arun3688
    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")
    
  2. bilderbuchi commented on Aug 16, 2021

    @bilderbuchi
    Author

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

  3. syntron commented on Jan 29, 2026

    @syntron
    Contributor

    @adeas31 based on PR #352, I think this issue can be closed

  4. adeas31 commented on Jan 30, 2026

    @adeas31
    Member

    @syntron I will let @bilderbuchi test this and then we can close this issue.

    @bilderbuchi would you like to test out the new plot interface? Please check and close the issue.

  5. bilderbuchi commented on Apr 23, 2026

    @bilderbuchi
    Author

    @bilderbuchi would you like to test out the new plot interface? Please check and close the issue.

    Yeah, I would. However, I found that the current version on pypi (4.0.1) does not have this fix, yet (as could be expected a posterior :-P).
    Then I tried to install the version from my OM installation, but was unable to locate it.

    cd
    python -m pip install -U .

    but frustratingly do not tell you what OMPythonPath is..

    • The OM installation directory seemingly does not contain an OMPython package (anymore? I seem to remember that this was the case earlier?)

    PS C:\OpenModelica1.27.0-dev-64bit> Get-ChildItem -Directory -Recurse -Filter OMPython

    • I guess I'll clone the repo now :-|
  6. bilderbuchi commented on Apr 23, 2026

    @bilderbuchi
    Author

    OK, this fails, apparently the API of ModelicaSystem has changed in the meantime. A lot.
    Anyway, I managed to create a plot:

    from OMPython import OMCSessionZMQ
    omc = OMCSessionZMQ()
    model_path=omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels/"
    from OMPython import ModelicaSystem
    msys=ModelicaSystem()
    msys.model("BouncingBall", model_path + "BouncingBall.mo")
    msys.buildModel()
    msys.simulate()
    msys.plot('h')

    This can be closed, thank you for the fix!

  7. adeas31 commented on Apr 24, 2026

    @adeas31
    Member

    @bilderbuchi would you like to test out the new plot interface? Please check and close the issue.

    Yeah, I would. However, I found that the current version on pypi (4.0.1) does not have this fix, yet (as could be expected a posterior :-P). Then I tried to install the version from my OM installation, but was unable to locate it.

    cd
    python -m pip install -U .

    but frustratingly do not tell you what OMPythonPath is..

    • The OM installation directory seemingly does not contain an OMPython package (anymore? I seem to remember that this was the case earlier?)

    PS C:\OpenModelica1.27.0-dev-64bit> Get-ChildItem -Directory -Recurse -Filter OMPython

    • I guess I'll clone the repo now :-|

    Yeah OMPython is not part of OM since a long time now. You need to clone the repo and OMPythonPath means path where you cloned the repo.

    Perhaps you can propose what we can change in the README from a user's perspective.

  8. cbu-hw commented on Apr 24, 2026

    @cbu-hw

    Perhaps you can propose what we can change in the README from a user's perspective.

    Probably it's fine, and this is only a problem of an old experienced user...new people won't remember to search within OM. ;-)

  9. added this to the 4.0.0 milestone on Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions