Skip to content

Commit

Permalink
feat: Create option for PyPIM to be installed separately (#1060)
Browse files Browse the repository at this point in the history
Co-authored-by: pyansys-ci-bot <[email protected]>
  • Loading branch information
klmcadams and pyansys-ci-bot authored Jan 24, 2025
1 parent 976b288 commit 868afe8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
1 change: 1 addition & 0 deletions doc/changelog.d/1060.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Create option for PyPIM to be installed separately
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ dependencies = [
"ansys-api-mechanical==0.1.2",
"ansys-mechanical-env==0.1.9",
"ansys-mechanical-stubs==0.1.6",
"ansys-platform-instancemanagement>=1.0.1",
"ansys-pythonnet>=3.1.0rc2",
"ansys-tools-path>=0.3.1",
"appdirs>=1.4.0",
Expand Down Expand Up @@ -87,6 +86,9 @@ viz = [
"ansys-tools-visualization-interface>=0.2.6",
"usd-core==24.11",
]
pim = [
"ansys-platform-instancemanagement>=1.0.1",
]

[project.scripts]
ansys-mechanical = "ansys.mechanical.core.run:cli"
Expand Down
29 changes: 24 additions & 5 deletions src/ansys/mechanical/core/mechanical.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
import socket
import threading
import time
from typing import Optional
import warnings
import weakref

import ansys.api.mechanical.v0.mechanical_pb2 as mechanical_pb2
import ansys.api.mechanical.v0.mechanical_pb2_grpc as mechanical_pb2_grpc
import ansys.platform.instancemanagement as pypim
from ansys.platform.instancemanagement import Instance
import ansys.tools.path as atp
import grpc

Expand All @@ -57,6 +57,16 @@
threaded,
)

# Check if PyPIM is installed
try:
import ansys.platform.instancemanagement as pypim # pragma: nocover noqa: F401

_HAS_ANSYS_PIM = True
"""Whether or not PyPIM exists."""
except ImportError:
_HAS_ANSYS_PIM = False


# Checking if tqdm is installed.
# If it is, the default value for progress_bar is true.
try:
Expand Down Expand Up @@ -1932,7 +1942,9 @@ def launch_grpc(
return port


def launch_remote_mechanical(version=None) -> (grpc.Channel, Instance): # pragma: no cover
def launch_remote_mechanical(
version=None,
) -> (grpc.Channel, Optional["Instance"]): # pragma: no cover
"""Start Mechanical remotely using the Product Instance Management (PIM) API.
When calling this method, you must ensure that you are in an environment
Expand All @@ -1951,6 +1963,13 @@ def launch_remote_mechanical(version=None) -> (grpc.Channel, Instance): # pragm
-------
Tuple containing channel, remote_instance.
"""
# Display warning if PyPIM is not installed
if not _HAS_ANSYS_PIM:
warnings.warn(
"Installation of pim option required! Use ``pip install ansys-mechanical-core[pim]``."
)
return

pim = pypim.connect()
instance = pim.create_instance(product_name="mechanical", product_version=version)

Expand Down Expand Up @@ -2062,7 +2081,7 @@ def launch_mechanical(
version : str, optional
Mechanical version to run in the three-digit format. For example, ``"251"``
for 2025 R1. The default is ``None``, in which case the server runs the
latest installed version. If PyPIM is configured and ``exce_file=None``,
latest installed version. If PyPIM is configured and ``exec_file=None``,
PyPIM launches Mechanical using its ``version`` parameter.
keep_connection_alive : bool, optional
Whether to keep the gRPC connection alive by running a background thread
Expand Down Expand Up @@ -2099,7 +2118,7 @@ def launch_mechanical(
"""
# Start Mechanical with PyPIM if the environment is configured for it
# and a directive on how to launch Mechanical was not passed.
if pypim.is_configured() and exec_file is None: # pragma: no cover
if _HAS_ANSYS_PIM and pypim.is_configured() and exec_file is None: # pragma: no cover
LOG.info("Starting Mechanical remotely. The startup configuration will be ignored.")
channel, remote_instance = launch_remote_mechanical(version=version)
return Mechanical(
Expand Down
4 changes: 2 additions & 2 deletions src/ansys/mechanical/core/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
import time
import warnings

import ansys.platform.instancemanagement as pypim
from ansys.tools.path import version_from_path

from ansys.mechanical.core.errors import VersionError
from ansys.mechanical.core.mechanical import (
_HAS_ANSYS_PIM,
_HAS_TQDM,
LOG,
MECHANICAL_DEFAULT_PORT,
Expand Down Expand Up @@ -175,7 +175,7 @@ def __init__(
if "exec_file" in kwargs:
exec_file = kwargs["exec_file"]
else:
if pypim.is_configured(): # pragma: no cover
if _HAS_ANSYS_PIM and pypim.is_configured(): # pragma: no cover
if "version" in kwargs:
version = kwargs["version"]
self._remote = True
Expand Down

0 comments on commit 868afe8

Please sign in to comment.