Skip to content

Commit

Permalink
FIX: Throw value error for unsupported version of Mechanical (#917)
Browse files Browse the repository at this point in the history
Co-authored-by: pyansys-ci-bot <[email protected]>
  • Loading branch information
dipinknair and pyansys-ci-bot authored Sep 30, 2024
1 parent 498e216 commit 225b52a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 35 deletions.
1 change: 1 addition & 0 deletions doc/changelog.d/917.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Throw value error for unsupported version of Mechanical
6 changes: 2 additions & 4 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
68 changes: 37 additions & 31 deletions src/ansys/mechanical/core/embedding/initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""


Expand All @@ -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:
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 225b52a

Please sign in to comment.