Skip to content

Commit

Permalink
refactor: Refactor the Launcher Utilities. (#2546)
Browse files Browse the repository at this point in the history
* refactor: Refactor the Launcher Utilities.

* Refactoring.

* Refactor

* Fix import in test.

* Refactoring.

* Refactor.

* Revert create_launcher fix.

* fix doc formatting
  • Loading branch information
prmukherj authored Mar 8, 2024
1 parent 09b600c commit 96a5fa0
Show file tree
Hide file tree
Showing 14 changed files with 711 additions and 666 deletions.
2 changes: 1 addition & 1 deletion src/ansys/fluent/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
connect_to_fluent,
launch_fluent,
)
from ansys.fluent.core.launcher.launcher_utils import ( # noqa: F401
from ansys.fluent.core.launcher.pyfluent_enums import ( # noqa: F401
FluentLinuxGraphicsDriver,
FluentMode,
FluentUI,
Expand Down
10 changes: 6 additions & 4 deletions src/ansys/fluent/core/launcher/container_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
--------
>>> from ansys.fluent.core.launcher.launcher import create_launcher
>>> from ansys.fluent.core.launcher.launcher_utils import LaunchMode
>>> from ansys.fluent.core.launcher.pyfluent_enums import LaunchMode
>>> container_meshing_launcher = create_launcher(LaunchMode.CONTAINER, mode="meshing")
>>> container_meshing_session = container_meshing_launcher()
Expand All @@ -18,17 +18,19 @@
from typing import Any, Dict, Optional, Union

from ansys.fluent.core.fluent_connection import FluentConnection
from ansys.fluent.core.launcher.error_handler import _process_invalid_args
from ansys.fluent.core.launcher.fluent_container import (
configure_container_dict,
start_fluent_container,
)
from ansys.fluent.core.launcher.launcher_utils import (
from ansys.fluent.core.launcher.process_launch_string import (
_build_fluent_launch_args_string,
)
from ansys.fluent.core.launcher.pyfluent_enums import (
FluentLinuxGraphicsDriver,
FluentMode,
FluentUI,
FluentWindowsGraphicsDriver,
_build_fluent_launch_args_string,
_process_invalid_args,
)
import ansys.fluent.core.launcher.watchdog as watchdog
from ansys.fluent.core.utils.file_transfer_service import PimFileTransferService
Expand Down
126 changes: 126 additions & 0 deletions src/ansys/fluent/core/launcher/error_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
from ansys.fluent.core.exceptions import InvalidArgument
from ansys.fluent.core.launcher import launcher_utils
from ansys.fluent.core.launcher.pyfluent_enums import FluentUI, LaunchMode
from ansys.fluent.core.utils.fluent_version import FluentVersion


class InvalidPassword(ValueError):
"""Raised when password is invalid."""

def __init__(self):
super().__init__("Provide correct 'password'.")


class GPUSolverSupportError(ValueError):
"""Raised when an unsupported Fluent version is specified."""

def __init__(self):
super().__init__("Fluent GPU Solver is only supported for 3D.")


class IpPortNotProvided(ValueError):
"""Raised when IP address and port are not specified."""

def __init__(self):
super().__init__("Provide either 'ip' and 'port' or 'server_info_file_name'.")


class UnexpectedKeywordArgument(TypeError):
"""Raised when a valid keyword argument is not specified."""

pass


class DockerContainerLaunchNotSupported(SystemError):
"""Raised when Docker container launch is not supported."""

def __init__(self):
super().__init__("Python Docker SDK is unsupported on this system.")


# pylint: disable=missing-raises-doc
class LaunchFluentError(Exception):
"""Exception class representing launch errors."""

def __init__(self, launch_string):
"""__init__ method of LaunchFluentError class."""
details = "\n" + "Fluent Launch string: " + launch_string
super().__init__(details)


def _raise_non_gui_exception_in_windows(
ui: FluentUI, product_version: FluentVersion
) -> None:
"""Fluent user interface mode lower than ``FluentUI.HIDDEN_GUI`` is not supported in
Windows in Fluent versions earlier than 2024 R1."""
if (
launcher_utils.is_windows()
and ui < FluentUI.HIDDEN_GUI
and product_version < FluentVersion.v241
):
raise InvalidArgument(
f"'{ui}' supported in Windows only for Fluent version 24.1 or later."
)


def _process_kwargs(kwargs):
"""Verify whether keyword arguments are valid or not.
Parameters
----------
kwargs: Any
Keyword arguments.
Raises
------
UnexpectedKeywordArgument
If an unexpected keyword argument is provided.
"""
if kwargs:
if "meshing_mode" in kwargs:
raise UnexpectedKeywordArgument(
"Use 'launch_fluent(mode='meshing')' to launch Fluent in meshing mode."
)
else:
raise UnexpectedKeywordArgument(
f"launch_fluent() got an unexpected keyword argument {next(iter(kwargs))}"
)


def _process_invalid_args(dry_run, fluent_launch_mode, argvals):
"""Get invalid arguments.
Parameters
----------
dry_run: bool
Whether to dry run a container start.
If ``True``, the ``launch_fluent()`` will return the configured ``container_dict``.
fluent_launch_mode: LaunchMode
Fluent launch mode.
argvals: dict
Local arguments.
"""
if dry_run and fluent_launch_mode != LaunchMode.CONTAINER:
launcher_utils.logger.warning(
"'dry_run' argument for 'launch_fluent' currently is only "
"supported when starting containers."
)
if fluent_launch_mode != LaunchMode.STANDALONE:
arg_names = [
"env",
"cwd",
"topy",
"case_file_name",
"lightweight_mode",
"journal_file_names",
"case_data_file_name",
]
invalid_arg_names = list(
filter(lambda arg_name: argvals[arg_name] is not None, arg_names)
)
if len(invalid_arg_names) != 0:
invalid_str_names = ", ".join(invalid_arg_names)
launcher_utils.logger.warning(
f"These specified arguments are only supported when starting "
f"local standalone Fluent clients: {invalid_str_names}."
)
24 changes: 13 additions & 11 deletions src/ansys/fluent/core/launcher/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,27 @@
from ansys.fluent.core.exceptions import DisallowedValuesError
from ansys.fluent.core.fluent_connection import FluentConnection
from ansys.fluent.core.launcher.container_launcher import DockerLauncher
from ansys.fluent.core.launcher.error_handler import (
GPUSolverSupportError,
_process_invalid_args,
_process_kwargs,
)
from ansys.fluent.core.launcher.launcher_utils import (
_confirm_watchdog_start,
is_windows,
)
from ansys.fluent.core.launcher.pim_launcher import PIMLauncher
from ansys.fluent.core.launcher.pyfluent_enums import (
FluentLinuxGraphicsDriver,
FluentMode,
FluentUI,
FluentWindowsGraphicsDriver,
GPUSolverSupportError,
LaunchMode,
_confirm_watchdog_start,
_get_fluent_launch_mode,
_get_mode,
_get_running_session_mode,
_get_server_info,
_is_windows,
_process_invalid_args,
_process_kwargs,
)
from ansys.fluent.core.launcher.pim_launcher import PIMLauncher
from ansys.fluent.core.launcher.server_info import _get_server_info
from ansys.fluent.core.launcher.slurm_launcher import SlurmFuture, SlurmLauncher
from ansys.fluent.core.launcher.standalone_launcher import StandaloneLauncher
import ansys.fluent.core.launcher.watchdog as watchdog
Expand All @@ -51,12 +55,10 @@ def create_launcher(fluent_launch_mode: LaunchMode = None, **kwargs):
fluent_launch_mode: LaunchMode
Supported Fluent launch modes. Options are ``"LaunchMode.CONTAINER"``,
``"LaunchMode.PIM"``, ``"LaunchMode.SLURM"``, and ``"LaunchMode.STANDALONE"``.
Returns
-------
launcher: Union[DockerLauncher, PimLauncher, StandaloneLauncher]
Session launcher.
Raises
------
DisallowedValuesError
Expand Down Expand Up @@ -271,15 +273,15 @@ def launch_fluent(
if ui is None:
# Not using NO_GUI in windows as it opens a new cmd or
# shows Fluent output in the current cmd if start <launch_string> is not used
ui = FluentUI.HIDDEN_GUI if _is_windows() else FluentUI.NO_GUI
ui = FluentUI.HIDDEN_GUI if is_windows() else FluentUI.NO_GUI
if isinstance(ui, str):
ui = FluentUI(ui)
if graphics_driver is None:
graphics_driver = "auto"
graphics_driver = str(graphics_driver)
graphics_driver = (
FluentWindowsGraphicsDriver(graphics_driver)
if _is_windows()
if is_windows()
else FluentLinuxGraphicsDriver(graphics_driver)
)
fluent_launch_mode = _get_fluent_launch_mode(
Expand Down
Loading

0 comments on commit 96a5fa0

Please sign in to comment.