|
1 | 1 | """Module for launching the pydyna solver service locally."""
|
2 | 2 |
|
3 | 3 | import os
|
4 |
| -import platform |
5 | 4 | import socket
|
6 | 5 | import subprocess
|
| 6 | +import sys |
7 | 7 | from time import sleep
|
8 | 8 | from zipfile import ZipFile
|
9 | 9 |
|
|
16 | 16 | except ModuleNotFoundError: # pragma: no cover
|
17 | 17 | _HAS_PIM = False
|
18 | 18 |
|
| 19 | +from ansys.tools.path import get_available_ansys_installations, get_latest_ansys_installation |
| 20 | + |
19 | 21 | from ansys.dyna.core.solver import DynaSolver
|
20 | 22 |
|
21 | 23 | LOCALHOST = "127.0.0.1"
|
22 | 24 | DYNA_DEFAULT_PORT = 5000
|
23 |
| -SERVER_SOLVER_VERSION = "v0.4.12" |
| 25 | +SERVER_SOLVER_VERSION = "v0.4.13" |
24 | 26 | MAX_MESSAGE_LENGTH = 8 * 1024**2
|
25 | 27 |
|
26 | 28 |
|
@@ -73,7 +75,32 @@ def port_in_use(port, host=LOCALHOST):
|
73 | 75 | return True
|
74 | 76 |
|
75 | 77 |
|
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 |
77 | 104 | """
|
78 | 105 | Launch the solver service locally in gRPC mode.
|
79 | 106 |
|
@@ -120,15 +147,41 @@ def launch_grpc(port=DYNA_DEFAULT_PORT, ip=LOCALHOST, server_path="") -> tuple:
|
120 | 147 | zipf.extractall(extractpath)
|
121 | 148 | server_path = server_package
|
122 | 149 | # 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 | + |
123 | 173 | if os.path.isdir(server_path):
|
124 | 174 | # threadserver = ServerThread(1, port=port, ip=ip, server_path=server_path)
|
125 | 175 | # threadserver.run()
|
126 | 176 | # threadserver.setDaemon(True)
|
127 | 177 | # 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) |
132 | 185 | waittime = 0
|
133 | 186 | while not DynaSolver.grpc_local_server_on():
|
134 | 187 | sleep(5)
|
@@ -202,13 +255,24 @@ def launch_remote_dyna(
|
202 | 255 |
|
203 | 256 |
|
204 | 257 | def launch_dyna(
|
| 258 | + product_version: int = None, |
205 | 259 | port=None,
|
206 | 260 | ip=None,
|
207 | 261 | ) -> DynaSolver:
|
208 | 262 | """Start DYNA locally.
|
209 | 263 |
|
210 | 264 | Parameters
|
211 | 265 | ----------
|
| 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 |
212 | 276 | port : int
|
213 | 277 | Port to launch DYNA gRPC on. Final port will be the first
|
214 | 278 | port available after (or including) this port. Defaults to
|
@@ -243,7 +307,7 @@ def launch_dyna(
|
243 | 307 | LOG.info("Starting DYNA remotely. The startup configuration will be ignored.")
|
244 | 308 | return launch_remote_dyna()
|
245 | 309 |
|
246 |
| - launch_grpc(port=port, ip=ip) |
| 310 | + launch_grpc(port=port, ip=ip, product_version=product_version) |
247 | 311 |
|
248 | 312 | dyna = DynaSolver(
|
249 | 313 | hostname=ip,
|
|
0 commit comments