diff --git a/codegen/tuigen.py b/codegen/tuigen.py index 317de7b9ee0..703502bd191 100644 --- a/codegen/tuigen.py +++ b/codegen/tuigen.py @@ -100,7 +100,7 @@ def _copy_tui_help_xml_file(): client = docker.from_env() container = client.containers.create(_FLUENT_IMAGE_NAME) - xml_source = f"/ansys_inc/v{FLUENT_VERSION.replace('.', '')}/commonfiles/help/en-us/fluent_gui_help/fluent_gui_help.xml" + xml_source = f"/ansys_inc/v{FLUENT_VERSION[0].replace('.', '')}/commonfiles/help/en-us/fluent_gui_help/fluent_gui_help.xml" is_linux = platform.system() == "Linux" subprocess.run( f"docker cp {container.name}:{xml_source} {_XML_HELP_FILE}", shell=is_linux @@ -342,7 +342,7 @@ def generate(self) -> None: def generate(): # pyfluent.set_log_level("WARNING") - if FLUENT_VERSION > "22.2": + if FLUENT_VERSION[0] > "22.2": _copy_tui_help_xml_file() _populate_xml_helpstrings() TUIGenerator(meshing=True).generate() diff --git a/src/ansys/fluent/core/__init__.py b/src/ansys/fluent/core/__init__.py index 95cbeb0a582..6f0a5b4660c 100644 --- a/src/ansys/fluent/core/__init__.py +++ b/src/ansys/fluent/core/__init__.py @@ -6,7 +6,13 @@ import appdirs from ansys.fluent.core._version import __version__ # noqa: F401 -from ansys.fluent.core.launcher.launcher import LaunchModes, launch_fluent # noqa: F401 +from ansys.fluent.core.launcher.launcher import ( # noqa: F401 + FluentVersion, + LaunchModes, + launch_fluent, + set_ansys_version, + set_fluent_path, +) from ansys.fluent.core.session import BaseSession as Fluent # noqa: F401 from ansys.fluent.core.utils.logging import LOG diff --git a/src/ansys/fluent/core/launcher/launcher.py b/src/ansys/fluent/core/launcher/launcher.py index 1d4136602a3..bc778abac03 100644 --- a/src/ansys/fluent/core/launcher/launcher.py +++ b/src/ansys/fluent/core/launcher/launcher.py @@ -25,8 +25,53 @@ _THIS_DIR = os.path.dirname(__file__) _OPTIONS_FILE = os.path.join(_THIS_DIR, "fluent_launcher_options.json") -FLUENT_VERSION = "22.2" -PIM_FLUENT_PRODUCT_VERSION = FLUENT_VERSION.replace(".", "") +FLUENT_VERSION = ["22.2"] +PIM_FLUENT_PRODUCT_VERSION = [FLUENT_VERSION[0].replace(".", "")] +FLUENT_EXE_PATH = [] + + +class FluentVersion(Enum): + """Contains the standard ansys / fluent release.""" + + version_22R2 = "22.2" + version_23R1 = "23.1" + + @staticmethod + def get_version(version: str) -> "FluentVersion": + """Returns the available versions based on the version in string + format.""" + for v in FluentVersion: + if version == v.value: + return v + else: + raise RuntimeError(f"The passed version '{version}' does not exist.") + + +def set_fluent_path(fluent_exe_path: Union[str, Path]) -> None: + """Lets the user set the fluent installation path manually. + + This supersedes the fluent path set in the environment variable + """ + if Path(fluent_exe_path).exists() and Path(fluent_exe_path).name == "fluent.exe": + FLUENT_EXE_PATH.append(str(fluent_exe_path)) + else: + raise RuntimeError( + f"The passed path '{fluent_exe_path}' does not contain a valid fluent executable file." + ) + + +def set_ansys_version(version: Union[str, float, FluentVersion]) -> None: + """Lets the user set the fluent version manually. + + Only works if the provided ansys version is installed and the + environment variables are updated properly. This supersedes the + fluent path set in the environment variable + """ + if type(version) in [float, str]: + version = FluentVersion.get_version(str(version)) + if version in FluentVersion or str(version) in FluentVersion.value: + FLUENT_VERSION[0] = version.value + PIM_FLUENT_PRODUCT_VERSION[0] = FLUENT_VERSION[0].replace(".", "") class LaunchModes(Enum): @@ -62,7 +107,7 @@ def get_fluent_path() -> Path: path = os.environ["PYFLUENT_FLUENT_ROOT"] return Path(path) else: - path = os.environ["AWP_ROOT" + "".join(FLUENT_VERSION.split("."))] + path = os.environ["AWP_ROOT" + "".join(FLUENT_VERSION[0].split("."))] return Path(path) / "fluent" @@ -315,7 +360,10 @@ def launch_fluent( ) ) if start_instance: - exe_path = _get_fluent_exe_path() + if FLUENT_EXE_PATH: + exe_path = FLUENT_EXE_PATH[0] + else: + exe_path = _get_fluent_exe_path() launch_string = exe_path launch_string += _build_fluent_launch_args_string(**argvals) if meshing_mode: @@ -362,7 +410,7 @@ def launch_fluent( "Starting Fluent remotely. The startup configuration will be ignored." ) return launch_remote_fluent( - product_version=PIM_FLUENT_PRODUCT_VERSION, + product_version=PIM_FLUENT_PRODUCT_VERSION[0], cleanup_on_exit=cleanup_on_exit, meshing_mode=meshing_mode, dimensionality=version, diff --git a/tests/test_launcher.py b/tests/test_launcher.py new file mode 100644 index 00000000000..beed913d933 --- /dev/null +++ b/tests/test_launcher.py @@ -0,0 +1,29 @@ +import pytest + +import ansys.fluent.core as pyfluent +from ansys.fluent.core.launcher.launcher import FLUENT_VERSION + + +def test_manual_fluent_version_setting(): + """Test case for setting up the Ansys / Fluent version via program""" + pyfluent.set_ansys_version("23.1") + assert FLUENT_VERSION[0] == "23.1" + + pyfluent.set_ansys_version(22.2) + assert FLUENT_VERSION[0] == "22.2" + + pyfluent.set_ansys_version(version=pyfluent.FluentVersion.version_23R1) + assert FLUENT_VERSION[0] == "23.1" + + # version does not exist + with pytest.raises(RuntimeError): + pyfluent.set_ansys_version(22.1) + + +def test_manual_fluent_path_setting(): + """Test case for setting up the path to fluent.exe via program""" + with pytest.raises(RuntimeError): + pyfluent.set_fluent_path("X:/dir_1/dir2/xxx.exe") + + with pytest.raises(RuntimeError): + pyfluent.set_fluent_path("X:/dir_1/dir2/fluent.bat") diff --git a/tests/test_launcher_remote.py b/tests/test_launcher_remote.py index 36bdcba87ab..72967f2777d 100644 --- a/tests/test_launcher_remote.py +++ b/tests/test_launcher_remote.py @@ -48,7 +48,7 @@ def test_launch_remote_instance(monkeypatch, new_solver_session): assert mock_is_configured.called assert mock_connect.called mock_client.create_instance.assert_called_with( - "fluent-3ddp", product_version=launcher.PIM_FLUENT_PRODUCT_VERSION + "fluent-3ddp", product_version=launcher.PIM_FLUENT_PRODUCT_VERSION[0] ) assert mock_instance.wait_for_ready.called mock_instance.build_grpc_channel.assert_called_with()