Skip to content
This repository was archived by the owner on May 8, 2025. It is now read-only.

use dymola python bridge to run framework on windows#3

Open
Honigmelone wants to merge 1 commit intoRWTH-EBC:masterfrom
Honigmelone:devel_dymola_python_interface
Open

use dymola python bridge to run framework on windows#3
Honigmelone wants to merge 1 commit intoRWTH-EBC:masterfrom
Honigmelone:devel_dymola_python_interface

Conversation

@Honigmelone
Copy link
Copy Markdown

Hi,

thanks for updating this repository for python 3. I wanted to try the code on my machine and was experiencing trouble with the automated simulation execution, due to it only being implemented for linux. In this pull request I want to show a possibility to use this in windows as well. The proposed solution, might also be favourable for runtime optimization.

why

  • the current setup executes a *.mos file directly with the dymola executable.
  • this loads all user interface libraries see stackoverflow
  • the approach is unflexible, since the specific command to launch the dymola executable varies depending on the used operating system
  • the approach is doesn't allow treatment of errors during execution

how

tasks left

  • clean the code. This is just a working example.

I would be happy if you would consider such a change.

Copy link
Copy Markdown

@FWuellhorst FWuellhorst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for implementing this and thus contributing to this fork. :)
Please check if you are calling the add_dymola_to_active_environment.py. I could not find it anywhere.

Comment on lines +8 to +19
dymola_search_path1 = os.path.join('C:\\', 'Program Files',
'Dymola 2018 FD01',
'Modelica',
'Library',
'python_interface',
'dymola.egg')
dymola_search_path2 = os.path.join('C:\\', 'Program Files',
'Dymola 2018',
'Modelica',
'Library',
'python_interface',
'dymola.egg')
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will work for Dymola 2018 only.
I'm currently using the following function to dynamically find Dymola installations.
Feel free to adapt it to pathlib :)
Note that I tested it for Windows and Linux, not for mac so far.

import sys
import os


def get_dymola_install_path(basedir=None):
    """
    Function to get the path of the newest dymola installment
    on the used machine. Supported platforms are:
    * Windows
    * Linux
    * Mac OS X
    If multiple installation of Dymola are found, the newest version will be returned.
    This assumes the names are sortable, e.g. Dymola 2020, Dymola 2019 etc.

    :param str basedir:
        The base-directory to search for the dymola-installation.
        The default value depends on the platform one is using.
        On Windows it is "C://Program Files" or "C://Program Files (x86)" (for 64 bit)
        On Linux it is "/opt" (based on our ci-Docker configuration
        On Mac OS X "/Application" (based on the default)
    :return: str
        Path to the dymola-installation
    """

    if basedir is None:
        if "linux" in sys.platform:
            basedir = os.path.normpath("/opt")
        elif "win" in sys.platform:
            basedir = os.path.normpath("C:/Program Files")
        elif "darwin" in sys.platform:
            basedir = os.path.normpath("/Applications")
        else:
            raise OSError(f"Your operating system ({sys.platform})does not support "
                          f"a default basedir. Please provide one.")

    syspaths = [basedir]
    # Check if 64bit is installed (Windows only)
    systempath_64 = os.path.normpath("C://Program Files (x86)")
    if os.path.exists(systempath_64):
        syspaths.append(systempath_64)
    # Get all folders in both path's
    temp_list = []
    for systempath in syspaths:
        temp_list += os.listdir(systempath)
    # Filter programs that are not Dymola
    dym_versions = []
    for folder_name in temp_list:
        # Catch both Dymola and dymola folder-names
        if "dymola" in folder_name.lower():
            dym_versions.append(folder_name)
    del temp_list
    # Find the newest version and return the egg-file
    # This sorting only works with a good Folder structure, eg. Dymola 2020, Dymola 2019 etc.
    dym_versions.sort()
    for dym_version in reversed(dym_versions):
        for system_path in syspaths:
            full_path = os.path.join(system_path, dym_version)
            if os.path.isdir(full_path):
                return full_path
    # If still inside the function, no interface was found

try:
os.mkdir(WORKING_DIR)
except OSError:
print("Creation of the directory %s failed" % WORKING_DIR)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the creation fails, isn't the whole script going to fail as well?



# dymola.ExecuteCommand("Advanced.CompileWith64=2")
result = dymola.ExecuteCommand('Modelica.Utilities.System.setWorkDirectory("' + Path(WORKING_DIR).as_posix() + '")')
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this achieve something different than simple dymola.cd(Path(WORKING_DIR).as_posix())?

Comment on lines +86 to +88
# dymola.plot(["J1.w", "J2.w", "J3.w", "J4.w"])
#dymola.plot(['STABLE_MOTION_G2_PRT.frame_a.f[1]','STABLE_MOTION_G2_PRT.frame_a.f[2]', 'STABLE_MOTION_G2_PRT.frame_a.f[3]'])
#dymola.ExportPlotAsImage(os.path.join(dir_result, 'tmp.png'))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not work so far? If so, delete. If it works, add an options at the start, e.g. PLOT_RESULTS = True.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants