Skip to content

Connect to OpenModelica installed on WSL #268

Closed

Description

@Sonyoyo

Hello,

I would like to use OMPython, installed on Windows, to interact with OpenModelica, installed on WSL. Is it possible?

If I understand correctly, this possibility is not considered by the current implementation. I guess it should be possible, since I can launch OMC with Popen, for exemple with Popen(["wsl","omc", ...])...

Activity

  1. syntron commented on Jun 12, 2025

    @syntron
    Contributor

    @Sonyoyo could you please test PR #302 - this could work ...

  2. Sonyoyo commented on Jun 13, 2025

    @Sonyoyo
    Author

    Thanks for your support. However, it looks like it can't yet find the OpenModelica installation:

    >>> omc = OMCSessionZMQ()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Users\D24676\Documents\depots\OMPython\OMPython\OMCSession.py", line 293, in __init__
        omc_process = OMCProcessLocal(omhome=omhome, timeout=timeout)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\D24676\Documents\depots\OMPython\OMPython\OMCSession.py", line 542, in __init__
        self._omhome = self._omc_home_get(omhome=omhome)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\D24676\Documents\depots\OMPython\OMPython\OMCSession.py", line 564, in _omc_home_get
        raise OMCSessionException("Cannot find OpenModelica executable, please install from openmodelica.org")
    OMPython.OMCSession.OMCSessionException: Cannot find OpenModelica executable, please install from openmodelica.org
    >

    Maybe I have to provide some arguments for omc = OMCSessionZMQ() or set some specific environment variables?

  3. syntron commented on Jun 14, 2025

    @syntron
    Contributor

    @Sonyoyo it is not as simple ;-)

      import OMPython
      
      def run_wsl():
          omcp = OMPython.OMCProcessWSL()
          om = OMPython.OMCSessionZMQ(omc_process=omcp)
          print(om.sendExpression("getVersion()"))
      
      if __name__ == '__main__':
          run_wsl()
      
          print('DONE')
    
    • if needed provide the location of omc in WSL as wsl_omc: omcp = OMPython.OMCProcessWSL(wsl_cmd=...)
    • please keep in mind, that this will not allow you to use ModelicaSystem! - this would need local access to the files to read the content or to run the executable

    untested from my side; thus, your feedback is welcome!

  4. Sonyoyo commented on Jun 16, 2025

    @Sonyoyo
    Author

    I still have errors:

    >>> omcp = OMPython.OMCProcessWSL()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Users\D24676\Documents\depots\OMPython\OMPython\OMCSession.py", line 1000, in __init__
        self._omc_port = self._omc_port_get()
                         ^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\D24676\Documents\depots\OMPython\OMPython\OMCSession.py", line 1040, in _omc_port_get
        raise OMCSessionException(f"WSL based OMC Server did not start (timeout={self._timeout}). "
    OMPython.OMCSession.OMCSessionException: WSL based OMC Server did not start (timeout=10.0). Could not open port file None. Log-file says:

    Using the wsl_omc argument (not wsl_cmd 😉 ) with wsl omc as value (which is the command I can enter in the windows cmd to launch OpenModelica) changes nothing.

  5. syntron commented on Jun 16, 2025

    @syntron
    Contributor

    @Sonyoyo Lets go one step back and just test if it is possible to run OMC; could you please execute the following commands to check if it is working?

    These are the commands which would be executed by OMCSessionZMQ/OMCProcessWSL:

    (1) start OMC in WSL:

    wsl -- omc --locale=C --interactive=zmq -z=abcdefg123
    
    Created ZeroMQ Server.
    Dumped server port in file: /tmp/openmodelica.nobody.port.7c595220669d4c878d422624b3f2f910
    

    (2) Get the content of the port file:

    wsl -- cat <insert the filename from above>
    

    You should get something like:

    tcp://127.0.0.1:41517
    

    If this is working, then we can continue ;-) Else we should tweak these commands first to find working versions ...

    The error message indicates that step (1) fails, i.e. OMC is not started / the path to the port file is not available.

  6. added this to the 4.0.0 milestone on Jun 17, 2025
  7. syntron commented on Jun 17, 2025

    @syntron
    Contributor

    @Sonyoyo

    Just noted an important point:

    Using the wsl_omc argument (not wsl_cmd 😉 ) with wsl omc as value (which is the command I can enter in the windows cmd to launch OpenModelica) changes nothing.

    wsl_omc = 'omc' - the wsl part is added by the class!

  8. Sonyoyo commented on Jun 17, 2025

    @Sonyoyo
    Author

    Result of tests (quite good advances!):

    I had errors running OMC in wsl since the default user is root, which is not allowed by OMC:

    Error: You are trying to run OpenModelica as a server using the root user.
    This is a very bad idea:
    * The socket interface does not authenticate the user.
    * OpenModelica allows execution of arbitrary commands.
    Execution failed!
    

    I suppose this is a quite common setup for WSL, so a generic issue. I then create a new user (with its own home!):

    sudo useradd -d /home/USER_NAME USER_NAME
    

    Two solutions to use it then:

    • Change the default user for wsl &rarr, wsl --manage DISTRO_NAME --set-default-user USER_NAME
    • Or specify a user name in the wsl call → wsl --user USER_NAME -- omc --locale=C --interactive=zmq -z=abcdefg123

    I would prefer the second option, but I will go with the first one for the moment (waiting for the additional user argument in the class). 😉

    With that done, the tests pass and I get the tcp address.

    However I still had the original error message... but trying to change the timeout to 30 (indeed the wsl startup itself take some time (with a quite random delay...especially in an interactive python shell, it looks better once in a script)...so 10 seconds timeout looked to me quite short) it is now working and I get the OpenModelica version printed out.

    Wonderful!

    I am now getting some error while trying to load some libraries, but maybe this is my fault (I am not familiar with the whole OMPython):

    om.sendExpression("loadModel(Modelica)")
    Traceback (most recent call last):
      File "c:\Users\D24676\Documents\PYBIB\wsl_omc.py", line 21, in <module>
        run_wsl()
      File "c:\Users\D24676\Documents\PYBIB\wsl_omc.py", line 13, in run_wsl
        om.sendExpression("loadModel(Modelica)")
      File "C:\Users\D24676\Documents\depots\OMPython\OMPython\OMCSession.py", line 408, in sendExpression
        raise OMCSessionException(msg)
    OMPython.OMCSession.OMCSessionException: [OMC log for 'sendExpression(loadModel(Modelica), True)']: [runtime:error:-1] Curl error for URL https://libraries.openmodelica.org/index/v1/index.json: Timeout was reached
    om.sendExpression("loadFile('/mnt/c/Users/D24676/Documents/depots/ThermoSysPro/ThermoSysPro/package.mo')")
    Traceback (most recent call last):
      File "c:\Users\D24676\Documents\PYBIB\wsl_omc.py", line 22, in <module>
        run_wsl()
      File "c:\Users\D24676\Documents\PYBIB\wsl_omc.py", line 14, in run_wsl
        om.sendExpression("loadFile('/mnt/c/Users/D24676/Documents/depots/ThermoSysPro/ThermoSysPro/package.mo')")
      File "C:\Users\D24676\Documents\depots\OMPython\OMPython\OMCSession.py", line 408, in sendExpression
        raise OMCSessionException(msg)
    OMPython.OMCSession.OMCSessionException: [OMC log for 'sendExpression(loadFile('/mnt/c/Users/D24676/Documents/depots/ThermoSysPro/ThermoSysPro/package.mo'), True)']: [translation:error:3] Class loadFile not found in scope <global scope> (looking for a function or record).

    Here it does not recognize loadFile... I do not understand why...

  9. syntron commented on Jun 18, 2025

    @syntron
    Contributor

    @Sonyoyo user and distribution can be defined as option to OMCProcessWSL

     def __init__(
                self,
                timeout: float = 10.00,
                omhome: Optional[str] = None,
                wsl_distribution: Optional[str] = None,
                wsl_user: Optional[str] = None,
        ) -> None:
    

    perhaps, this should be enforced, i.e. a values != root must be defined?

    At the same point, you can increase the timeout as needed.

    The errors you get now seems to be related to internet connection (curl). I think that this could be more related to local WSL problems ... Is it working if you use OMC directly in WSL?

    Have you tested some simple OMC functionality (from test_ZMQ)?

        om.sendExpression("""loadString(\"model M
      Real r = time;
    end M;
    \")""")
        om.sendExpression('res:=simulate(M, stopTime=2.0)')
        om.sendExpression('res.resultFile')
    

    It should output you a file name ...

  10. Sonyoyo commented on Jun 19, 2025

    @Sonyoyo
    Author

    Thank you for pointing out that the wsl_user argument already exist. Excellent! 😄

    With your example, everything works fine. So I guess I just need to understand how to load existing libraries and models with OMPython.

  11. syntron commented on Jun 19, 2025

    @syntron
    Contributor

    @Sonyoyo thanks for the feedback - all further questions are perhaps related to OMPython but more focused on using it - WSL is working! as you confirmed this issue could be closed

    @adeas31 this means PR #302 is working ;-)

  12. adeas31 commented on Jun 25, 2025

    @adeas31
    Member

    @syntron I also got your example working with 2 different distros.

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