Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update launcher,add argument to define ansys version #632

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changelog/632.documentation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix: update launcher,add argument to define ansys version
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ classifiers = [
]

dependencies = ["ansys-dpf-core>=0.7.2",
"ansys-api-dyna==0.4.1",
"ansys-api-dyna==0.4.2",
"ansys-tools-path>=0.6.0",
"ansys-platform-instancemanagement~=1.0",
"pyvista>=0.43.4",
Expand Down
4 changes: 2 additions & 2 deletions src/ansys/dyna/core/pre/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def launch_grpc(port=DYNAPRE_DEFAULT_PORT, ip=LOCALHOST, server_path="") -> tupl
# threadserver.setDaemon(True)
# threadserver.start()
# env_path = get_virtualenv_path()
process = subprocess.Popen("python kwserver.py", cwd=server_path, shell=True)
process = subprocess.Popen(f"{sys.executable} kwserver.py", cwd=server_path, shell=True)
waittime = 0
while not DynaSolution.grpc_local_server_on():
sleep(5)
Expand Down Expand Up @@ -284,7 +284,7 @@ def launch_dynapre(

if __name__ == "__main__":
server_path = os.path.join(os.getcwd(), "Server")
process = subprocess.Popen(["python", "kwserver.py"], cwd=server_path, shell=True)
process = subprocess.Popen(f"{sys.executable} kwserver.py", cwd=server_path, shell=True)
process.wait()
process.terminate()
print(process)
22 changes: 21 additions & 1 deletion src/ansys/dyna/core/solver/dynasolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def __init__(self, hostname="localhost", port="5000", channel=None, server_path=
self.port = port
self.pim_client = None
self.remote_instance = None
self.working_dir = None

temp = hostname + ":" + str(port)
if channel is None:
Expand Down Expand Up @@ -216,6 +217,22 @@ def list_files(self, subname=None):
ret.append((str(response.name[i], "utf-8"), response.size[i]))
return ret

def working_dir(self, working_dir=None):
"""Set current working directory.

Parameters
----------
working_dir : string
Current working directory.
"""
if os.path.exists(working_dir):
if not os.path.isdir(working_dir):
working_dir = "."
else:
os.makedirs(working_dir)
os.chdir(working_dir)
self.working_dir = working_dir

def node(self, n):
"""Get size information about a node in the model's working directory.

Expand Down Expand Up @@ -292,8 +309,11 @@ def download(self, fname):
fp.close()
return fsize

def push(self, fname):
def push(self, fname, workdir):
"""Provide an alias for the ``upload` method for backward compatibility."""
request = dynasolver_pb2.DynaSolverWorkDir()
request.dirname = bytes(workdir, "utf-8")
self.stub.set_working_directory(request)
return self.upload(fname)

def upload(self, fname):
Expand Down
80 changes: 72 additions & 8 deletions src/ansys/dyna/core/solver/launcher.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""Module for launching the pydyna solver service locally."""

import os
import platform
import socket
import subprocess
import sys
from time import sleep
from zipfile import ZipFile

Expand All @@ -16,11 +16,13 @@
except ModuleNotFoundError: # pragma: no cover
_HAS_PIM = False

from ansys.tools.path import get_available_ansys_installations, get_latest_ansys_installation

from ansys.dyna.core.solver import DynaSolver

LOCALHOST = "127.0.0.1"
DYNA_DEFAULT_PORT = 5000
SERVER_SOLVER_VERSION = "v0.4.12"
SERVER_SOLVER_VERSION = "v0.4.13"
MAX_MESSAGE_LENGTH = 8 * 1024**2


Expand Down Expand Up @@ -73,7 +75,32 @@ def port_in_use(port, host=LOCALHOST):
return True


def launch_grpc(port=DYNA_DEFAULT_PORT, ip=LOCALHOST, server_path="") -> tuple: # pragma: no cover
def _check_minimal_versions(latest_installed_version: int) -> None:
"""Check client is compatible with Ansys Products.

Check that at least V241 is installed.
"""
if abs(latest_installed_version) < 241:
msg = (
"PyAnsys Geometry is compatible with Ansys Products from version 24.1.0. "
+ "Please install Ansys products 24.1.0 or later."
)
raise SystemError(msg)


def _check_version_is_available(version: int, installations: dict[int, str]) -> None:
"""Check that the requested version for launcher is installed."""
if version not in installations:
msg = (
f"The requested Ansys product's version {version} is not available, "
+ "please specify a different version."
)
raise SystemError(msg)


def launch_grpc(
port=DYNA_DEFAULT_PORT, ip=LOCALHOST, server_path="", product_version=None
) -> tuple: # pragma: no cover
"""
Launch the solver service locally in gRPC mode.

Expand Down Expand Up @@ -120,15 +147,41 @@ def launch_grpc(port=DYNA_DEFAULT_PORT, ip=LOCALHOST, server_path="") -> tuple:
zipf.extractall(extractpath)
server_path = server_package
# os.environ["ANSYS_PYDYNA_SOLVER_SERVER_PATH"] = server_path

# Check Ansys version
installations = get_available_ansys_installations()
if product_version is not None:
try:
_check_version_is_available(product_version, installations)
except SystemError as serr:
# The user requested a version as a Student version...
# Let's negate it and try again... if this works, we override the
# product_version variable.
try:
_check_version_is_available(-product_version, installations)
except SystemError:
# The student version is not installed either... raise the original error.
raise serr

product_version = -product_version
else:
product_version = get_latest_ansys_installation()[0]

# Verify that the minimum version is installed.
_check_minimal_versions(product_version)

if os.path.isdir(server_path):
# threadserver = ServerThread(1, port=port, ip=ip, server_path=server_path)
# threadserver.run()
# threadserver.setDaemon(True)
# threadserver.start()
if platform.system() == "Windows":
process = subprocess.Popen("python server.py", cwd=server_path, shell=True)
else:
process = subprocess.Popen("python3 server.py", cwd=server_path, shell=True)

process = subprocess.Popen(f"{sys.executable} server.py {product_version}", cwd=server_path, shell=True)

# if platform.system() == "Windows":
# process = subprocess.Popen("python server.py", cwd=server_path, shell=True)
# else:
# process = subprocess.Popen("python3 server.py", cwd=server_path, shell=True)
waittime = 0
while not DynaSolver.grpc_local_server_on():
sleep(5)
Expand Down Expand Up @@ -202,13 +255,24 @@ def launch_remote_dyna(


def launch_dyna(
product_version: int = None,
port=None,
ip=None,
) -> DynaSolver:
"""Start DYNA locally.

Parameters
----------
product_version: int, optional
The product version to be started. Goes from v20.1 to
the latest. Default is ``None``.
If a specific product version is requested but not installed locally,
a SystemError will be raised.

**Ansys products versions and their corresponding int values:**

* ``241`` : Ansys 24R1
* ``242`` : Ansys 24R2
port : int
Port to launch DYNA gRPC on. Final port will be the first
port available after (or including) this port. Defaults to
Expand Down Expand Up @@ -243,7 +307,7 @@ def launch_dyna(
LOG.info("Starting DYNA remotely. The startup configuration will be ignored.")
return launch_remote_dyna()

launch_grpc(port=port, ip=ip)
launch_grpc(port=port, ip=ip, product_version=product_version)

dyna = DynaSolver(
hostname=ip,
Expand Down
Loading