Skip to content

Commit 2d4d5bb

Browse files
feat: Check if fluent exe exists in AWP_ROOT path while determining the Fluent version to launch (#4024)
This is to handle the scenario when a later version of Ansys is installed without Fluent (#4023) --------- Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent cfd117b commit 2d4d5bb

File tree

8 files changed

+76
-58
lines changed

8 files changed

+76
-58
lines changed

doc/changelog.d/4024.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Check if fluent exe exists in AWP_ROOT path while determining the Fluent version to launch

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ tests = [
4747
"pytest-cov==6.1.1",
4848
"pytest-mock==3.14.0",
4949
"pytest-xdist==3.6.1",
50+
"pyfakefs==5.8.0"
5051
]
5152
docs = [
5253
"Sphinx==8.1.3",

src/ansys/fluent/core/codegen/tuigen.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
)
5959
from ansys.fluent.core.utils.fix_doc import escape_wildcards
6060
from ansys.fluent.core.utils.fluent_version import (
61-
AnsysVersionNotFound,
6261
FluentVersion,
6362
get_version_for_file_name,
6463
)
@@ -130,7 +129,7 @@ def _copy_tui_help_xml_file(version: str):
130129
shutil.copy(str(xml_source), _XML_HELP_FILE)
131130
else:
132131
logger.warning("fluent_gui_help.xml is not found.")
133-
except AnsysVersionNotFound:
132+
except FileNotFoundError:
134133
logger.warning("fluent_gui_help.xml is not found.")
135134

136135

src/ansys/fluent/core/launcher/launcher.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ def launch_fluent(
184184
Version of Ansys Fluent to launch. To use Fluent version 2025 R1, pass
185185
any of ``FluentVersion.v251``, ``"25.1.0"``, ``"25.1"``, ``25.1``or ``251``.
186186
The default is ``None``, in which case the newest installed version is used.
187+
PyFluent uses the ``AWP_ROOT<ver>`` environment variable to locate the Fluent
188+
installation, where ``<ver>`` is the Ansys release number such as ``251``.
189+
The ``AWP_ROOT<ver>`` environment variable is automatically configured on Windows
190+
system when Fluent is installed. On Linux systems, ``AWP_ROOT<ver>`` must be
191+
configured to point to the absolute path of an Ansys installation such as
192+
``/apps/ansys_inc/v251``.
187193
dimension : Dimension or int, optional
188194
Geometric dimensionality of the Fluent simulation. The default is ``None``,
189195
in which case ``Dimension.THREE`` is used. Options are either the values of the

src/ansys/fluent/core/launcher/process_launch_string.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,6 @@ def get_fluent_exe_path(**launch_argvals) -> Path:
150150
Fluent executable path
151151
"""
152152

153-
def get_fluent_root(version: FluentVersion) -> Path:
154-
awp_root = os.environ[version.awp_var]
155-
return Path(awp_root) / "fluent"
156-
157153
def get_exe_path(fluent_root: Path) -> Path:
158154
if launcher_utils.is_windows():
159155
return fluent_root / "ntbin" / "win64" / "fluent.exe"
@@ -171,12 +167,12 @@ def get_exe_path(fluent_root: Path) -> Path:
171167
# 2. product_version parameter passed with launch_fluent
172168
product_version = launch_argvals.get("product_version")
173169
if product_version:
174-
return get_exe_path(get_fluent_root(FluentVersion(product_version)))
170+
return FluentVersion(product_version).get_fluent_exe_path()
175171

176172
# (DEV) "PYFLUENT_FLUENT_ROOT" environment variable
177173
fluent_root = os.getenv("PYFLUENT_FLUENT_ROOT")
178174
if fluent_root:
179175
return get_exe_path(Path(fluent_root))
180176

181177
# 3. the latest ANSYS version from AWP_ROOT environment variables
182-
return get_exe_path(get_fluent_root(FluentVersion.get_latest_installed()))
178+
return FluentVersion.get_latest_installed().get_fluent_exe_path()

src/ansys/fluent/core/utils/fluent_version.py

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,29 @@
2525
from enum import Enum
2626
from functools import total_ordering
2727
import os
28+
from pathlib import Path
29+
import platform
30+
from typing import Any
2831

2932
import ansys.fluent.core as pyfluent
3033

3134

3235
class AnsysVersionNotFound(RuntimeError):
3336
"""Raised when Ansys version is not found."""
3437

35-
pass
38+
def __init__(self, version: Any):
39+
"""Initialize VersionNotFound.
40+
41+
Parameters
42+
----------
43+
version : str
44+
Version that was not found.
45+
"""
46+
super().__init__(
47+
f"The specified version '{version}' is not supported."
48+
+ " Supported versions are: "
49+
+ ", ".join([member.value for member in FluentVersion][::-1])
50+
)
3651

3752

3853
class ComparisonError(RuntimeError):
@@ -84,7 +99,7 @@ class FluentVersion(Enum):
8499
v222 = "22.2.0"
85100

86101
@classmethod
87-
def _missing_(cls, version):
102+
def _missing_(cls, version: Any):
88103
if isinstance(version, (int, float, str)):
89104
version = str(version)
90105
if len(version) == 3:
@@ -93,11 +108,8 @@ def _missing_(cls, version):
93108
for member in cls:
94109
if version == member.value:
95110
return member
96-
raise AnsysVersionNotFound(
97-
f"The specified version '{version[:-2]}' is not supported."
98-
+ " Supported versions are: "
99-
+ ", ".join([member.value for member in cls][::-1])
100-
)
111+
112+
raise AnsysVersionNotFound(version[:-2])
101113

102114
@classmethod
103115
def get_latest_installed(cls):
@@ -111,15 +123,33 @@ def get_latest_installed(cls):
111123
112124
Raises
113125
------
114-
AnsysVersionNotFound
126+
FileNotFoundError
115127
If an Ansys version cannot be found.
116128
"""
117129
for member in cls:
118-
if member.awp_var in os.environ:
130+
if member.awp_var in os.environ and member.get_fluent_exe_path().exists():
119131
return member
120132

121-
raise AnsysVersionNotFound(
122-
"Verify the value of the 'AWP_ROOT' environment variable."
133+
raise FileNotFoundError(
134+
"Unable to locate a compatible Ansys Fluent installation. "
135+
"Ensure that an environment variable like 'AWP_ROOT242' or 'AWP_ROOT251' "
136+
"points to a supported Ansys version, and that Fluent is included in the installation."
137+
)
138+
139+
def get_fluent_exe_path(self) -> Path:
140+
"""Get the path for the Fluent executable file.
141+
142+
Returns
143+
-------
144+
Path
145+
Fluent executable path.
146+
"""
147+
awp_root = os.environ[self.awp_var]
148+
fluent_root = Path(awp_root) / "fluent"
149+
return (
150+
fluent_root / "ntbin" / "win64" / "fluent.exe"
151+
if platform.system() == "Windows"
152+
else fluent_root / "bin" / "fluent"
123153
)
124154

125155
@classmethod

tests/test_fluent_version.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ def test_version_not_found():
4747
FluentVersion(22)
4848

4949

50-
def test_get_latest_installed(helpers):
50+
def test_get_latest_installed(helpers, fs):
5151
helpers.mock_awp_vars()
52+
with pytest.raises(FileNotFoundError):
53+
assert FluentVersion.get_latest_installed() == FluentVersion.current_release()
54+
fs.create_file(FluentVersion.current_release().get_fluent_exe_path())
5255
assert FluentVersion.get_latest_installed() == FluentVersion.current_release()
5356

5457

tests/test_launcher.py

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
LaunchMode,
5656
UIMode,
5757
)
58-
from ansys.fluent.core.utils.fluent_version import AnsysVersionNotFound, FluentVersion
58+
from ansys.fluent.core.utils.fluent_version import FluentVersion
5959
import ansys.platform.instancemanagement as pypim
6060

6161

@@ -256,49 +256,31 @@ def test_gpu_launch_arg_additional_arg():
256256

257257
def test_get_fluent_exe_path_when_nothing_is_set(helpers):
258258
helpers.delete_all_awp_vars()
259-
with pytest.raises(AnsysVersionNotFound):
259+
with pytest.raises(FileNotFoundError):
260260
get_fluent_exe_path()
261-
with pytest.raises(AnsysVersionNotFound):
261+
with pytest.raises(FileNotFoundError):
262262
FluentVersion.get_latest_installed()
263263

264264

265-
def test_get_fluent_exe_path_from_awp_root_222(helpers):
266-
helpers.mock_awp_vars(version="222")
267-
if platform.system() == "Windows":
268-
expected_path = Path("ansys_inc/v222/fluent") / "ntbin" / "win64" / "fluent.exe"
269-
else:
270-
expected_path = Path("ansys_inc/v222/fluent") / "bin" / "fluent"
271-
assert FluentVersion.get_latest_installed() == FluentVersion.v222
272-
assert get_fluent_exe_path() == expected_path
273-
274-
275-
def test_get_fluent_exe_path_from_awp_root_231(helpers):
276-
helpers.mock_awp_vars(version="231")
277-
if platform.system() == "Windows":
278-
expected_path = Path("ansys_inc/v231/fluent") / "ntbin" / "win64" / "fluent.exe"
279-
else:
280-
expected_path = Path("ansys_inc/v231/fluent") / "bin" / "fluent"
281-
assert FluentVersion.get_latest_installed() == FluentVersion.v231
282-
assert get_fluent_exe_path() == expected_path
283-
284-
285-
def test_get_fluent_exe_path_from_awp_root_232(helpers):
286-
helpers.mock_awp_vars(version="232")
287-
if platform.system() == "Windows":
288-
expected_path = Path("ansys_inc/v232/fluent") / "ntbin" / "win64" / "fluent.exe"
289-
else:
290-
expected_path = Path("ansys_inc/v232/fluent") / "bin" / "fluent"
291-
assert FluentVersion.get_latest_installed() == FluentVersion.v232
292-
assert get_fluent_exe_path() == expected_path
293-
294-
295-
def test_get_fluent_exe_path_from_awp_root_241(helpers):
296-
helpers.mock_awp_vars(version="241")
265+
@pytest.mark.parametrize(
266+
"fluent_version",
267+
[version for version in FluentVersion],
268+
)
269+
def test_get_fluent_exe_path_from_awp_root(fluent_version, helpers, fs):
270+
helpers.mock_awp_vars(version=str(fluent_version.number))
271+
fs.create_file(fluent_version.get_fluent_exe_path())
297272
if platform.system() == "Windows":
298-
expected_path = Path("ansys_inc/v241/fluent") / "ntbin" / "win64" / "fluent.exe"
273+
expected_path = (
274+
Path(f"ansys_inc/v{fluent_version.number}/fluent")
275+
/ "ntbin"
276+
/ "win64"
277+
/ "fluent.exe"
278+
)
299279
else:
300-
expected_path = Path("ansys_inc/v241/fluent") / "bin" / "fluent"
301-
assert FluentVersion.get_latest_installed() == FluentVersion.v241
280+
expected_path = (
281+
Path(f"ansys_inc/v{fluent_version.number}/fluent") / "bin" / "fluent"
282+
)
283+
assert FluentVersion.get_latest_installed() == fluent_version
302284
assert get_fluent_exe_path() == expected_path
303285

304286

0 commit comments

Comments
 (0)