Skip to content

Commit

Permalink
refactor: Include 'version' and 'precision' into enums. (#2913)
Browse files Browse the repository at this point in the history
* refactor: Include 'version' and 'precision' into enums.

* Refactor for testing.

* version -> dimension.

* fix version.

* Clean up.

* Update src/ansys/fluent/core/launcher/pyfluent_enums.py

* Refactor.

* Update test.

* Update logic.

* Update docstring.

* Update tests/test_launcher.py

* Update tests/test_launcher.py

* Update tests/util/fixture_fluent.py

* Update tests/util/fixture_fluent.py

* Update dimension check for gpu launch.

* Support int in dimension.

* Update documentation.

* Update docstrings.

* Update tests.

* Update deprecated arg string.

* Update docstrings.

---------

Co-authored-by: Harshal Pohekar <[email protected]>
  • Loading branch information
prmukherj and hpohekar authored Jun 8, 2024
1 parent 964e77b commit edaf800
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 82 deletions.
14 changes: 7 additions & 7 deletions doc/source/user_guide/launching_ansys_fluent.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ modeling dimension:

.. code:: python
solver = pyfluent.launch_fluent(precision="double", version="2d", mode="solver")
solver = pyfluent.launch_fluent(precision="double", dimension=2, mode="solver")
Local parallel
~~~~~~~~~~~~~~
Expand All @@ -71,7 +71,7 @@ number of processors for local parallel execution:
.. code:: python
solver = pyfluent.launch_fluent(
precision="double", version="2d", processor_count=2, mode="solver"
precision="double", dimension=2, processor_count=2, mode="solver"
)
Distributed parallel
Expand All @@ -83,7 +83,7 @@ distributed across more than one machine:
solver = pyfluent.launch_fluent(
precision="double",
version="3d",
dimension=3,
processor_count=16,
mode="solver",
additional_arguments="-cnf=m1:8,m2:8",
Expand Down Expand Up @@ -153,15 +153,15 @@ machines and cores:

.. code:: python
solver = pyfluent.launch_fluent(precision="double", version="3d", mode="solver")
solver = pyfluent.launch_fluent(precision="double", dimension=3, mode="solver")
If you want to clamp the number of cores that Fluent is launched on, you can
pass the ``processor_count`` parameter:

.. code:: python
solver = pyfluent.launch_fluent(
precision="double", version="3d", processor_count=16, mode="solver"
precision="double", dimension=3, processor_count=16, mode="solver"
)
Passing the ``processor_count`` parameter like this forces execution of Fluent on 16
Expand All @@ -179,7 +179,7 @@ argument:
.. code:: python
solver = pyfluent.launch_fluent(
precision="double", version="3d", mode="solver", additional_arguments="-t16"
precision="double", dimension=3, mode="solver", additional_arguments="-t16"
)
For distributed parallel processing, you usually pass both parameters:
Expand All @@ -188,7 +188,7 @@ For distributed parallel processing, you usually pass both parameters:
solver = pyfluent.launch_fluent(
precision="double",
version="3d",
dimension=3,
mode="solver",
additional_arguments="-t16 -cnf=m1:8,m2:8",
)
Expand Down
2 changes: 2 additions & 0 deletions src/ansys/fluent/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
launch_fluent,
)
from ansys.fluent.core.launcher.pyfluent_enums import ( # noqa: F401
Dimension,
FluentLinuxGraphicsDriver,
FluentMode,
FluentWindowsGraphicsDriver,
Precision,
UIMode,
)
from ansys.fluent.core.services.batch_ops import BatchOps # noqa: F401
Expand Down
18 changes: 11 additions & 7 deletions src/ansys/fluent/core/launcher/container_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
_build_fluent_launch_args_string,
)
from ansys.fluent.core.launcher.pyfluent_enums import (
Dimension,
FluentLinuxGraphicsDriver,
FluentMode,
FluentWindowsGraphicsDriver,
Precision,
UIMode,
_get_argvals_and_session,
)
Expand All @@ -51,8 +53,8 @@ def __init__(
FluentWindowsGraphicsDriver, FluentLinuxGraphicsDriver, str, None
] = None,
product_version: Union[FluentVersion, str, float, int] = None,
version: Optional[str] = None,
precision: Optional[str] = None,
dimension: Union[Dimension, int, None] = None,
precision: Union[Precision, str, None] = None,
processor_count: Optional[int] = None,
start_timeout: int = 60,
additional_arguments: Optional[str] = "",
Expand Down Expand Up @@ -81,12 +83,14 @@ def __init__(
Version of Ansys Fluent to launch. To use Fluent version 2024 R2, pass
any of ``FluentVersion.v242``, ``"24.2.0"``, ``"24.2"``, ``24.2``, or ``242``.
The default is ``None``, in which case the newest installed version is used.
version : str, optional
dimension : Dimension or int, optional
Geometric dimensionality of the Fluent simulation. The default is ``None``,
in which case ``"3d"`` is used. Options are ``"3d"`` and ``"2d"``.
precision : str, optional
Floating point precision. The default is ``None``, in which case ``"double"``
is used. Options are ``"double"`` and ``"single"``.
in which case ``Dimension.THREE`` is used. Options are either the values of the
``Dimension`` enum (``Dimension.TWO`` or ``Dimension.THREE``) or any of ``2`` and ``3``.
precision : Precision or str, optional
Floating point precision. The default is ``None``, in which case ``Precision.DOUBLE``
is used. Options are either the values of the ``Precision`` enum (``Precision.SINGLE``
or ``Precision.DOUBLE``) or any of ``"double"`` and ``"single"``.
processor_count : int, optional
Number of processors. The default is ``None``, in which case ``1``
processor is used. In job scheduler environments the total number of
Expand Down
24 changes: 0 additions & 24 deletions src/ansys/fluent/core/launcher/fluent_launcher_options.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,4 @@
{
"version": {
"type": "str",
"allowed_values": [
"3d",
"2d"
],
"default": "3d",
"fluent_required": true,
"fluent_format": " {}"
},
"precision": {
"type": "str",
"allowed_values": [
"single",
"double"
],
"default": "double",
"fluent_required": true,
"fluent_map": {
"single": "",
"double": "dp"
},
"fluent_format": "{}"
},
"fluent_debug": {
"type": "bool",
"fluent_map": {
Expand Down
33 changes: 26 additions & 7 deletions src/ansys/fluent/core/launcher/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
from ansys.fluent.core.launcher.launcher_utils import _confirm_watchdog_start
from ansys.fluent.core.launcher.pim_launcher import PIMLauncher
from ansys.fluent.core.launcher.pyfluent_enums import (
Dimension,
FluentLinuxGraphicsDriver,
FluentMode,
FluentWindowsGraphicsDriver,
LaunchMode,
Precision,
UIMode,
_get_fluent_launch_mode,
_get_running_session_mode,
Expand Down Expand Up @@ -68,17 +70,32 @@ def create_launcher(fluent_launch_mode: LaunchMode = None, **kwargs):
return SlurmLauncher(**kwargs)


def _version_to_dimension(old_arg_val):
if old_arg_val == "2d":
return Dimension.TWO
elif old_arg_val == "3d":
return Dimension.THREE
else:
return None


# pylint: disable=unused-argument
@deprecate_argument(
old_arg="show_gui",
new_arg="ui_mode",
converter=lambda old_arg_val: UIMode.GUI if old_arg_val is True else None,
deprecation_class=PyFluentDeprecationWarning,
)
@deprecate_argument(
old_arg="version",
new_arg="dimension",
converter=_version_to_dimension,
deprecation_class=PyFluentDeprecationWarning,
)
def launch_fluent(
product_version: Union[FluentVersion, str, float, int, None] = None,
version: Optional[str] = None,
precision: Optional[str] = None,
dimension: Union[Dimension, int, None] = None,
precision: Union[Precision, str, None] = None,
processor_count: Optional[int] = None,
journal_file_names: Union[None, str, list[str]] = None,
start_timeout: Optional[int] = None,
Expand Down Expand Up @@ -114,12 +131,14 @@ def launch_fluent(
Version of Ansys Fluent to launch. To use Fluent version 2024 R2, pass
any of ``FluentVersion.v242``, ``"24.2.0"``, ``"24.2"``, ``24.2``or ``242``.
The default is ``None``, in which case the newest installed version is used.
version : str, optional
dimension : Dimension or int, optional
Geometric dimensionality of the Fluent simulation. The default is ``None``,
in which case ``"3d"`` is used. Options are ``"3d"`` and ``"2d"``.
precision : str, optional
Floating point precision. The default is ``None``, in which case ``"double"``
is used. Options are ``"double"`` and ``"single"``.
in which case ``Dimension.THREE`` is used. Options are either the values of the
``Dimension`` enum (``Dimension.TWO`` or ``Dimension.THREE``) or any of ``2`` and ``3``.
precision : Precision or str, optional
Floating point precision. The default is ``None``, in which case ``Precision.DOUBLE``
is used. Options are either the values of the ``Precision`` enum (``Precision.SINGLE``
or ``Precision.DOUBLE``) or any of ``"double"`` and ``"single"``.
processor_count : int, optional
Number of processors. The default is ``None``, in which case ``1``
processor is used. In job scheduler environments the total number of
Expand Down
20 changes: 12 additions & 8 deletions src/ansys/fluent/core/launcher/pim_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@

from ansys.fluent.core.fluent_connection import FluentConnection
from ansys.fluent.core.launcher.pyfluent_enums import (
Dimension,
FluentLinuxGraphicsDriver,
FluentMode,
FluentWindowsGraphicsDriver,
Precision,
UIMode,
_get_argvals_and_session,
)
Expand Down Expand Up @@ -49,8 +51,8 @@ def __init__(
FluentWindowsGraphicsDriver, FluentLinuxGraphicsDriver, str, None
] = None,
product_version: Union[FluentVersion, str, float, int] = None,
version: Optional[str] = None,
precision: Optional[str] = None,
dimension: Union[Dimension, int, None] = None,
precision: Union[Precision, str, None] = None,
processor_count: Optional[int] = None,
start_timeout: int = 60,
additional_arguments: Optional[str] = "",
Expand All @@ -77,12 +79,14 @@ def __init__(
Version of Ansys Fluent to launch. To use Fluent version 2024 R2, pass
``FluentVersion.v242``, ``"24.2.0"``, ``"24.2"``, ``24.2``, or ``242``.
The default is ``None``, in which case the newest installed version is used.
version : str, optional
dimension : Dimension or int, optional
Geometric dimensionality of the Fluent simulation. The default is ``None``,
in which case ``"3d"`` is used. Options are ``"3d"`` and ``"2d"``.
precision : str, optional
Floating point precision. The default is ``None``, in which case ``"double"``
is used. Options are ``"double"`` and ``"single"``.
in which case ``Dimension.THREE`` is used. Options are either the values of the
``Dimension`` enum (``Dimension.TWO`` or ``Dimension.THREE``) or any of ``2`` and ``3``.
precision : Precision or str, optional
Floating point precision. The default is ``None``, in which case ``Precision.DOUBLE``
is used. Options are either the values of the ``Precision`` enum (``Precision.SINGLE``
or ``Precision.DOUBLE``) or any of ``"double"`` and ``"single"``.
processor_count : int, optional
Number of processors. The default is ``None``, in which case ``1``
processor is used. In job scheduler environments the total number of
Expand Down Expand Up @@ -166,7 +170,7 @@ def __call__(self):
product_version=fluent_product_version,
cleanup_on_exit=self.argvals["cleanup_on_exit"],
mode=self.argvals["mode"],
dimensionality=self.argvals["version"],
dimensionality=self.argvals["dimension"],
launcher_args=self.argvals,
file_transfer_service=self.file_transfer_service,
)
Expand Down
14 changes: 13 additions & 1 deletion src/ansys/fluent/core/launcher/process_launch_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@

import ansys.fluent.core as pyfluent
from ansys.fluent.core.launcher import launcher_utils
from ansys.fluent.core.launcher.pyfluent_enums import FluentMode, UIMode
from ansys.fluent.core.launcher.pyfluent_enums import (
Dimension,
FluentMode,
Precision,
UIMode,
)
from ansys.fluent.core.scheduler import build_parallel_options, load_machines
from ansys.fluent.core.utils.fluent_version import FluentVersion

Expand All @@ -26,6 +31,13 @@ def _build_fluent_launch_args_string(**kwargs) -> str:
with open(_OPTIONS_FILE, encoding="utf-8") as fp:
all_options = json.load(fp)
launch_args_string = ""
dimension = kwargs.get("dimension")
launch_args_string += f" {Dimension(dimension).value[0]}"
precision = kwargs.get("precision")
if precision is None:
launch_args_string += f"{Precision.DOUBLE.value[0]}"
else:
launch_args_string += f"{Precision(precision).value[0]}"
for k, v in all_options.items():
argval = kwargs.get(k)
default = v.get("default")
Expand Down
34 changes: 30 additions & 4 deletions src/ansys/fluent/core/launcher/pyfluent_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,32 @@ class UIMode(FluentEnum):
GUI = ("",)


class Dimension(FluentEnum):
"""Geometric dimensionality of the Fluent simulation."""

TWO = ("2d",)
THREE = ("3d",)

@classmethod
def _missing_(cls, value: int):
if value is None:
return cls.THREE
for member in cls:
if int(member.value[0][0]) == value:
return member
raise ValueError(
f"The specified value '{value}' is not a supported value of {cls.__name__}."
f""" The supported values are: '{"', '".join((member.value[0][0]) for member in cls)}'."""
)


class Precision(FluentEnum):
"""Floating point precision."""

SINGLE = ("",)
DOUBLE = ("dp",)


class FluentWindowsGraphicsDriver(FluentEnum):
"""Provides supported graphics driver of Fluent in Windows."""

Expand Down Expand Up @@ -275,22 +301,22 @@ def _get_ui_mode(
return ui_mode


def _validate_gpu(gpu: Union[bool, list], version: str):
def _validate_gpu(gpu: Union[bool, list], dimension: int):
"""Raise an exception if the GPU Solver is unsupported.
Parameters
----------
gpu : bool or list, optional
This option will start Fluent with the GPU Solver.
version : str, optional
dimension : int, optional
Geometric dimensionality of the Fluent simulation.
"""
if version == "2d" and gpu:
if Dimension(dimension) == Dimension.TWO and gpu:
raise exceptions.GPUSolverSupportError()


def _get_argvals_and_session(argvals):
_validate_gpu(argvals["gpu"], argvals["version"])
_validate_gpu(argvals["gpu"], argvals["dimension"])
argvals["graphics_driver"] = _get_graphics_driver(argvals["graphics_driver"])
argvals["mode"] = _get_mode(argvals["mode"])
del argvals["self"]
Expand Down
18 changes: 11 additions & 7 deletions src/ansys/fluent/core/launcher/slurm_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@
)
from ansys.fluent.core.launcher.process_launch_string import _generate_launch_string
from ansys.fluent.core.launcher.pyfluent_enums import (
Dimension,
FluentLinuxGraphicsDriver,
FluentMode,
FluentWindowsGraphicsDriver,
Precision,
UIMode,
_get_argvals_and_session,
_get_ui_mode,
Expand Down Expand Up @@ -209,8 +211,8 @@ def __init__(
FluentWindowsGraphicsDriver, FluentLinuxGraphicsDriver, str, None
] = None,
product_version: Union[FluentVersion, str, float, int, None] = None,
version: Optional[str] = None,
precision: Optional[str] = None,
dimension: Union[Dimension, int, None] = None,
precision: Union[Precision, str, None] = None,
processor_count: Optional[int] = None,
journal_file_names: Union[None, str, list[str]] = None,
start_timeout: int = -1,
Expand Down Expand Up @@ -245,12 +247,14 @@ def __init__(
Version of Ansys Fluent to launch. To use Fluent version 2024 R2, pass
``FluentVersion.v242``, ``"24.2.0"``, ``"24.2"``, ``24.2``, or ``242``.
The default is ``None``, in which case the newest installed version is used.
version : str, optional
dimension : Dimension or int, optional
Geometric dimensionality of the Fluent simulation. The default is ``None``,
in which case ``"3d"`` is used. Options are ``"3d"`` and ``"2d"``.
precision : str, optional
Floating point precision. The default is ``None``, in which case ``"double"``
is used. Options are ``"double"`` and ``"single"``.
in which case ``Dimension.THREE`` is used. Options are either the values of the
``Dimension`` enum (``Dimension.TWO`` or ``Dimension.THREE``) or any of ``2`` and ``3``.
precision : Precision or str, optional
Floating point precision. The default is ``None``, in which case ``Precision.DOUBLE``
is used. Options are either the values of the ``Precision`` enum (``Precision.SINGLE``
or ``Precision.DOUBLE``) or any of ``"double"`` and ``"single"``.
processor_count : int, optional
Number of processors. The default is ``None``, in which case ``1``
processor is used. In job scheduler environments the total number of
Expand Down
Loading

0 comments on commit edaf800

Please sign in to comment.