From 5a0c83e74ca353fcc6270ab2af85b3aa1c8ba1b7 Mon Sep 17 00:00:00 2001 From: zhangzhanqun Date: Wed, 27 Nov 2024 15:29:32 +0800 Subject: [PATCH 1/6] update launcher,add argument to define ansys version --- pyproject.toml | 2 +- src/ansys/dyna/core/pre/launcher.py | 4 +- src/ansys/dyna/core/solver/dynasolver.py | 22 ++++++- src/ansys/dyna/core/solver/launcher.py | 73 +++++++++++++++++++++--- 4 files changed, 90 insertions(+), 11 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index a8b487bd3..1e6ac1b8e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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", diff --git a/src/ansys/dyna/core/pre/launcher.py b/src/ansys/dyna/core/pre/launcher.py index 2ad5a755d..2e138ef5d 100644 --- a/src/ansys/dyna/core/pre/launcher.py +++ b/src/ansys/dyna/core/pre/launcher.py @@ -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) @@ -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) diff --git a/src/ansys/dyna/core/solver/dynasolver.py b/src/ansys/dyna/core/solver/dynasolver.py index bc99fd2b9..a8440c25a 100644 --- a/src/ansys/dyna/core/solver/dynasolver.py +++ b/src/ansys/dyna/core/solver/dynasolver.py @@ -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: @@ -215,7 +216,23 @@ def list_files(self, subname=None): for i in range(n): 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. @@ -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): diff --git a/src/ansys/dyna/core/solver/launcher.py b/src/ansys/dyna/core/solver/launcher.py index fd3d189d2..691bb0bc4 100644 --- a/src/ansys/dyna/core/solver/launcher.py +++ b/src/ansys/dyna/core/solver/launcher.py @@ -4,6 +4,7 @@ import platform import socket import subprocess +import sys from time import sleep from zipfile import ZipFile @@ -17,10 +18,11 @@ _HAS_PIM = False from ansys.dyna.core.solver import DynaSolver +from ansys.tools.path import get_available_ansys_installations, get_latest_ansys_installation 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 @@ -72,8 +74,28 @@ def port_in_use(port, host=LOCALHOST): except: return True +def _check_minimal_versions(latest_installed_version: int) -> None: + """Check client is compatible with Ansys Products. -def launch_grpc(port=DYNA_DEFAULT_PORT, ip=LOCALHOST, server_path="") -> tuple: # pragma: no cover + 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. @@ -120,15 +142,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) @@ -202,6 +250,7 @@ def launch_remote_dyna( def launch_dyna( + product_version: int = None, port=None, ip=None, ) -> DynaSolver: @@ -209,6 +258,16 @@ def launch_dyna( 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 @@ -243,7 +302,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, From 144ad23993def134d6d4b71b79f73735e2586cab Mon Sep 17 00:00:00 2001 From: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Date: Fri, 29 Nov 2024 07:39:38 +0000 Subject: [PATCH 2/6] chore: adding changelog file 632.miscellaneous.md [dependabot-skip] --- doc/changelog/632.miscellaneous.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 doc/changelog/632.miscellaneous.md diff --git a/doc/changelog/632.miscellaneous.md b/doc/changelog/632.miscellaneous.md new file mode 100644 index 000000000..5aaf7e596 --- /dev/null +++ b/doc/changelog/632.miscellaneous.md @@ -0,0 +1 @@ +update launcher,add argument to define ansys version \ No newline at end of file From 4227c30bd3282243d2341e9371f1dfa8327afb6a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 29 Nov 2024 07:40:52 +0000 Subject: [PATCH 3/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/ansys/dyna/core/solver/dynasolver.py | 10 +++++----- src/ansys/dyna/core/solver/launcher.py | 18 ++++++++++++------ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/ansys/dyna/core/solver/dynasolver.py b/src/ansys/dyna/core/solver/dynasolver.py index a8440c25a..8e0ee1f73 100644 --- a/src/ansys/dyna/core/solver/dynasolver.py +++ b/src/ansys/dyna/core/solver/dynasolver.py @@ -216,8 +216,8 @@ def list_files(self, subname=None): for i in range(n): ret.append((str(response.name[i], "utf-8"), response.size[i])) return ret - - def working_dir(self, working_dir = None): + + def working_dir(self, working_dir=None): """Set current working directory. Parameters @@ -227,12 +227,12 @@ def working_dir(self, working_dir = None): """ if os.path.exists(working_dir): if not os.path.isdir(working_dir): - working_dir = '.' + working_dir = "." else: os.makedirs(working_dir) os.chdir(working_dir) - self.working_dir = working_dir - + self.working_dir = working_dir + def node(self, n): """Get size information about a node in the model's working directory. diff --git a/src/ansys/dyna/core/solver/launcher.py b/src/ansys/dyna/core/solver/launcher.py index 691bb0bc4..015b3c577 100644 --- a/src/ansys/dyna/core/solver/launcher.py +++ b/src/ansys/dyna/core/solver/launcher.py @@ -17,9 +17,10 @@ except ModuleNotFoundError: # pragma: no cover _HAS_PIM = False -from ansys.dyna.core.solver import DynaSolver 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.13" @@ -74,6 +75,7 @@ def port_in_use(port, host=LOCALHOST): except: return True + def _check_minimal_versions(latest_installed_version: int) -> None: """Check client is compatible with Ansys Products. @@ -86,6 +88,7 @@ def _check_minimal_versions(latest_installed_version: int) -> None: ) 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: @@ -94,8 +97,11 @@ def _check_version_is_available(version: int, installations: dict[int, str]) -> + "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 + + +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. @@ -142,8 +148,8 @@ def launch_grpc(port=DYNA_DEFAULT_PORT, ip=LOCALHOST, server_path="", product_ve zipf.extractall(extractpath) server_path = server_package # os.environ["ANSYS_PYDYNA_SOLVER_SERVER_PATH"] = server_path - - #Check Ansys version + + # Check Ansys version installations = get_available_ansys_installations() if product_version is not None: try: @@ -302,7 +308,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,product_version = product_version) + launch_grpc(port=port, ip=ip, product_version=product_version) dyna = DynaSolver( hostname=ip, From 6762e60dd963870aa3c5d584c8130e68dbf7abe4 Mon Sep 17 00:00:00 2001 From: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Date: Fri, 29 Nov 2024 07:41:31 +0000 Subject: [PATCH 4/6] chore: adding changelog file 632.documentation.md [dependabot-skip] --- doc/changelog/{632.miscellaneous.md => 632.documentation.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename doc/changelog/{632.miscellaneous.md => 632.documentation.md} (100%) diff --git a/doc/changelog/632.miscellaneous.md b/doc/changelog/632.documentation.md similarity index 100% rename from doc/changelog/632.miscellaneous.md rename to doc/changelog/632.documentation.md From 588aaccced2fd18e4940e3c2d2388620c38eca25 Mon Sep 17 00:00:00 2001 From: zhangzhanqun <99698565+zhangzhanqun@users.noreply.github.com> Date: Fri, 29 Nov 2024 15:50:59 +0800 Subject: [PATCH 5/6] Update launcher.py --- src/ansys/dyna/core/solver/launcher.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ansys/dyna/core/solver/launcher.py b/src/ansys/dyna/core/solver/launcher.py index 015b3c577..e23817066 100644 --- a/src/ansys/dyna/core/solver/launcher.py +++ b/src/ansys/dyna/core/solver/launcher.py @@ -1,7 +1,6 @@ """Module for launching the pydyna solver service locally.""" import os -import platform import socket import subprocess import sys From c3580590e6c8dd73a1094aba44998670f004c1bc Mon Sep 17 00:00:00 2001 From: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Date: Fri, 29 Nov 2024 07:51:30 +0000 Subject: [PATCH 6/6] chore: adding changelog file 632.documentation.md [dependabot-skip] --- doc/changelog/632.documentation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/changelog/632.documentation.md b/doc/changelog/632.documentation.md index 5aaf7e596..1f782e86d 100644 --- a/doc/changelog/632.documentation.md +++ b/doc/changelog/632.documentation.md @@ -1 +1 @@ -update launcher,add argument to define ansys version \ No newline at end of file +fix: update launcher,add argument to define ansys version \ No newline at end of file