diff --git a/doc/changelog.d/917.fixed.md b/doc/changelog.d/917.fixed.md new file mode 100644 index 000000000..1bda1ba72 --- /dev/null +++ b/doc/changelog.d/917.fixed.md @@ -0,0 +1 @@ +Throw value error for unsupported version of Mechanical \ No newline at end of file diff --git a/doc/source/conf.py b/doc/source/conf.py index 9a0c6450d..14b7c3c7b 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -16,9 +16,7 @@ from sphinx_gallery.sorting import FileNameSortKey import ansys.mechanical.core as pymechanical -from ansys.mechanical.core.embedding.initializer import ( - SUPPORTED_MECHANICAL_EMBEDDING_VERSIONS_WINDOWS, -) +from ansys.mechanical.core.embedding.initializer import SUPPORTED_MECHANICAL_EMBEDDING_VERSIONS # necessary when building the sphinx gallery pymechanical.BUILDING_GALLERY = True @@ -141,7 +139,7 @@ with open("links.rst") as f: rst_epilog += f.read() -current_mechanical_version = next(iter(SUPPORTED_MECHANICAL_EMBEDDING_VERSIONS_WINDOWS.keys())) +current_mechanical_version = next(iter(SUPPORTED_MECHANICAL_EMBEDDING_VERSIONS.keys())) rst_epilog = rst_epilog.replace("%%VERSION%%", f"v{current_mechanical_version}") # The name of the Pygments (syntax highlighting) style to use. pygments_style = "sphinx" diff --git a/src/ansys/mechanical/core/embedding/initializer.py b/src/ansys/mechanical/core/embedding/initializer.py index 2a1b0be22..ddd28350d 100644 --- a/src/ansys/mechanical/core/embedding/initializer.py +++ b/src/ansys/mechanical/core/embedding/initializer.py @@ -28,15 +28,13 @@ import sys import warnings -import ansys.tools.path as atp - from ansys.mechanical.core.embedding.loader import load_clr from ansys.mechanical.core.embedding.resolver import resolve INITIALIZED_VERSION = None """Constant for the initialized version.""" -SUPPORTED_MECHANICAL_EMBEDDING_VERSIONS_WINDOWS = {242: "2024R2", 241: "2024R1", 232: "2023R2"} +SUPPORTED_MECHANICAL_EMBEDDING_VERSIONS = {242: "2024R2", 241: "2024R1", 232: "2023R2"} """Supported Mechanical embedding versions on Windows.""" @@ -59,40 +57,46 @@ def __workaround_material_server(version: int) -> None: os.environ["ENGRDATA_SERVER_SERIAL"] = "1" -def _get_default_linux_version() -> int: - """Try to get the active linux version from the environment. +def __check_for_supported_version(version) -> None: + """Check if Mechanical version is supported with current version of PyMechanical. - On linux, embedding is only possible by setting environment variables before starting python. - The version will then be fixed to a specific version, based on those env vars. - The documented way to set those variables is to run python using the ``mechanical-env`` script, - which can be used after installing the ``ansys-mechanical-env`` package with this command: - ``pip install ansys-mechanical-env``. The script takes user input of a version. If the user - does not provide a version, the ``find_mechanical()`` function from the ``ansys-tools-path`` - package is used to find a version of Mechanical. + If specific environment variable is enabled, then users can overwrite the supported versions. + However, using unsupported versions may cause issues. """ - supported_versions = [232, 241, 242] - awp_roots = {ver: os.environ.get(f"AWP_ROOT{ver}", "") for ver in supported_versions} - installed_versions = { - ver: path for ver, path in awp_roots.items() if path and os.path.isdir(path) - } - assert len(installed_versions) == 1, "multiple AWP_ROOT environment variables found!" - return next(iter(installed_versions)) + allow_old_version = os.getenv("ANSYS_MECHANICAL_EMBEDDING_SUPPORT_OLD_VERSIONS") == "1" + # Check if the version is supported + if not allow_old_version and version < min(SUPPORTED_MECHANICAL_EMBEDDING_VERSIONS): + raise ValueError(f"Mechanical version {version} is not supported.") -def _get_default_version() -> int: - if os.name == "posix": - return _get_default_linux_version() + return version - if os.name != "nt": # pragma: no cover - raise Exception("Unexpected platform!") - _, version = atp.find_mechanical( - supported_versions=SUPPORTED_MECHANICAL_EMBEDDING_VERSIONS_WINDOWS - ) +def _get_latest_default_version() -> int: + """Try to get the latest Mechanical version from the environment. - # version is of the form 23.2 - int_version = int(str(version).replace(".", "")) - return int_version + Checks if multiple versions of Mechanical found in system. + For Linux it will be only one since ``mechanical-env`` takes care of that. + If multiple versions are detected, select the latest one, as no specific version is provided. + """ + awp_roots = [value for key, value in os.environ.items() if key.startswith("AWP_ROOT")] + + if not awp_roots: + raise Exception("No Mechanical installations found.") + + versions_found = [] + for path in awp_roots: + folder = os.path.basename(os.path.normpath(path)) + version = folder.split("v")[-1] + versions_found.append(int(version)) + latest_version = max(versions_found) + + if len(awp_roots) > 1: + raise Warning( + f"Multiple versions of Mechanical found! Using latest version {latest_version} ..." + ) + + return latest_version def __check_python_interpreter_architecture() -> None: @@ -165,7 +169,9 @@ def initialize(version: int = None): return if version == None: - version = _get_default_version() + version = _get_latest_default_version() + + version = __check_for_supported_version(version=version) INITIALIZED_VERSION = version