Skip to content

Commit 8a3c304

Browse files
zhangzhanqunpyansys-ci-botpre-commit-ci[bot]
authored
fix: update launcher,add argument to define ansys version (#632)
Co-authored-by: pyansys-ci-bot <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 65a5d4f commit 8a3c304

File tree

4 files changed

+96
-11
lines changed

4 files changed

+96
-11
lines changed

doc/changelog/632.documentation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fix: update launcher,add argument to define ansys version

src/ansys/dyna/core/pre/launcher.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def launch_grpc(port=DYNAPRE_DEFAULT_PORT, ip=LOCALHOST, server_path="") -> tupl
136136
# threadserver.setDaemon(True)
137137
# threadserver.start()
138138
# env_path = get_virtualenv_path()
139-
process = subprocess.Popen("python kwserver.py", cwd=server_path, shell=True)
139+
process = subprocess.Popen(f"{sys.executable} kwserver.py", cwd=server_path, shell=True)
140140
waittime = 0
141141
while not DynaSolution.grpc_local_server_on():
142142
sleep(5)
@@ -284,7 +284,7 @@ def launch_dynapre(
284284

285285
if __name__ == "__main__":
286286
server_path = os.path.join(os.getcwd(), "Server")
287-
process = subprocess.Popen(["python", "kwserver.py"], cwd=server_path, shell=True)
287+
process = subprocess.Popen(f"{sys.executable} kwserver.py", cwd=server_path, shell=True)
288288
process.wait()
289289
process.terminate()
290290
print(process)

src/ansys/dyna/core/solver/dynasolver.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def __init__(self, hostname="localhost", port="5000", channel=None, server_path=
7070
self.port = port
7171
self.pim_client = None
7272
self.remote_instance = None
73+
self.working_dir = None
7374

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

220+
def working_dir(self, working_dir=None):
221+
"""Set current working directory.
222+
223+
Parameters
224+
----------
225+
working_dir : string
226+
Current working directory.
227+
"""
228+
if os.path.exists(working_dir):
229+
if not os.path.isdir(working_dir):
230+
working_dir = "."
231+
else:
232+
os.makedirs(working_dir)
233+
os.chdir(working_dir)
234+
self.working_dir = working_dir
235+
219236
def node(self, n):
220237
"""Get size information about a node in the model's working directory.
221238
@@ -292,8 +309,11 @@ def download(self, fname):
292309
fp.close()
293310
return fsize
294311

295-
def push(self, fname):
312+
def push(self, fname, workdir):
296313
"""Provide an alias for the ``upload` method for backward compatibility."""
314+
request = dynasolver_pb2.DynaSolverWorkDir()
315+
request.dirname = bytes(workdir, "utf-8")
316+
self.stub.set_working_directory(request)
297317
return self.upload(fname)
298318

299319
def upload(self, fname):

src/ansys/dyna/core/solver/launcher.py

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""Module for launching the pydyna solver service locally."""
22

33
import os
4-
import platform
54
import socket
65
import subprocess
6+
import sys
77
from time import sleep
88
from zipfile import ZipFile
99

@@ -16,11 +16,13 @@
1616
except ModuleNotFoundError: # pragma: no cover
1717
_HAS_PIM = False
1818

19+
from ansys.tools.path import get_available_ansys_installations, get_latest_ansys_installation
20+
1921
from ansys.dyna.core.solver import DynaSolver
2022

2123
LOCALHOST = "127.0.0.1"
2224
DYNA_DEFAULT_PORT = 5000
23-
SERVER_SOLVER_VERSION = "v0.4.12"
25+
SERVER_SOLVER_VERSION = "v0.4.13"
2426
MAX_MESSAGE_LENGTH = 8 * 1024**2
2527

2628

@@ -73,7 +75,32 @@ def port_in_use(port, host=LOCALHOST):
7375
return True
7476

7577

76-
def launch_grpc(port=DYNA_DEFAULT_PORT, ip=LOCALHOST, server_path="") -> tuple: # pragma: no cover
78+
def _check_minimal_versions(latest_installed_version: int) -> None:
79+
"""Check client is compatible with Ansys Products.
80+
81+
Check that at least V241 is installed.
82+
"""
83+
if abs(latest_installed_version) < 241:
84+
msg = (
85+
"PyAnsys Geometry is compatible with Ansys Products from version 24.1.0. "
86+
+ "Please install Ansys products 24.1.0 or later."
87+
)
88+
raise SystemError(msg)
89+
90+
91+
def _check_version_is_available(version: int, installations: dict[int, str]) -> None:
92+
"""Check that the requested version for launcher is installed."""
93+
if version not in installations:
94+
msg = (
95+
f"The requested Ansys product's version {version} is not available, "
96+
+ "please specify a different version."
97+
)
98+
raise SystemError(msg)
99+
100+
101+
def launch_grpc(
102+
port=DYNA_DEFAULT_PORT, ip=LOCALHOST, server_path="", product_version=None
103+
) -> tuple: # pragma: no cover
77104
"""
78105
Launch the solver service locally in gRPC mode.
79106
@@ -120,15 +147,41 @@ def launch_grpc(port=DYNA_DEFAULT_PORT, ip=LOCALHOST, server_path="") -> tuple:
120147
zipf.extractall(extractpath)
121148
server_path = server_package
122149
# os.environ["ANSYS_PYDYNA_SOLVER_SERVER_PATH"] = server_path
150+
151+
# Check Ansys version
152+
installations = get_available_ansys_installations()
153+
if product_version is not None:
154+
try:
155+
_check_version_is_available(product_version, installations)
156+
except SystemError as serr:
157+
# The user requested a version as a Student version...
158+
# Let's negate it and try again... if this works, we override the
159+
# product_version variable.
160+
try:
161+
_check_version_is_available(-product_version, installations)
162+
except SystemError:
163+
# The student version is not installed either... raise the original error.
164+
raise serr
165+
166+
product_version = -product_version
167+
else:
168+
product_version = get_latest_ansys_installation()[0]
169+
170+
# Verify that the minimum version is installed.
171+
_check_minimal_versions(product_version)
172+
123173
if os.path.isdir(server_path):
124174
# threadserver = ServerThread(1, port=port, ip=ip, server_path=server_path)
125175
# threadserver.run()
126176
# threadserver.setDaemon(True)
127177
# threadserver.start()
128-
if platform.system() == "Windows":
129-
process = subprocess.Popen("python server.py", cwd=server_path, shell=True)
130-
else:
131-
process = subprocess.Popen("python3 server.py", cwd=server_path, shell=True)
178+
179+
process = subprocess.Popen(f"{sys.executable} server.py {product_version}", cwd=server_path, shell=True)
180+
181+
# if platform.system() == "Windows":
182+
# process = subprocess.Popen("python server.py", cwd=server_path, shell=True)
183+
# else:
184+
# process = subprocess.Popen("python3 server.py", cwd=server_path, shell=True)
132185
waittime = 0
133186
while not DynaSolver.grpc_local_server_on():
134187
sleep(5)
@@ -202,13 +255,24 @@ def launch_remote_dyna(
202255

203256

204257
def launch_dyna(
258+
product_version: int = None,
205259
port=None,
206260
ip=None,
207261
) -> DynaSolver:
208262
"""Start DYNA locally.
209263
210264
Parameters
211265
----------
266+
product_version: int, optional
267+
The product version to be started. Goes from v20.1 to
268+
the latest. Default is ``None``.
269+
If a specific product version is requested but not installed locally,
270+
a SystemError will be raised.
271+
272+
**Ansys products versions and their corresponding int values:**
273+
274+
* ``241`` : Ansys 24R1
275+
* ``242`` : Ansys 24R2
212276
port : int
213277
Port to launch DYNA gRPC on. Final port will be the first
214278
port available after (or including) this port. Defaults to
@@ -243,7 +307,7 @@ def launch_dyna(
243307
LOG.info("Starting DYNA remotely. The startup configuration will be ignored.")
244308
return launch_remote_dyna()
245309

246-
launch_grpc(port=port, ip=ip)
310+
launch_grpc(port=port, ip=ip, product_version=product_version)
247311

248312
dyna = DynaSolver(
249313
hostname=ip,

0 commit comments

Comments
 (0)