Skip to content

Commit 654deb4

Browse files
committed
Update launchers typing to be more specific
1 parent 90d69a0 commit 654deb4

File tree

6 files changed

+676
-233
lines changed

6 files changed

+676
-233
lines changed

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

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
import logging
4040
import os
4141
import time
42-
from typing import Any
42+
from typing import Any, TypedDict, Unpack
4343

4444
from ansys.fluent.core.fluent_connection import FluentConnection
4545
from ansys.fluent.core.launcher.fluent_container import (
@@ -64,6 +64,33 @@
6464
from ansys.fluent.core.session import _parse_server_info_file
6565
from ansys.fluent.core.utils.fluent_version import FluentVersion
6666

67+
68+
class ContainerArgsWithoutDryRun(TypedDict, total=False):
69+
ui_mode: UIMode | str | None
70+
graphics_driver: (
71+
FluentWindowsGraphicsDriver | FluentLinuxGraphicsDriver | str | None
72+
)
73+
product_version: FluentVersion | str | float | int | None
74+
dimension: Dimension | int | None
75+
precision: Precision | str | None
76+
processor_count: int | None
77+
start_timeout: int
78+
additional_arguments: str
79+
container_dict: dict[str, Any] | None
80+
cleanup_on_exit: bool
81+
start_transcript: bool
82+
py: bool | None
83+
gpu: bool | None
84+
start_watchdog: bool | None
85+
file_transfer_service: Any | None
86+
use_docker_compose: bool | None
87+
use_podman_compose: bool | None
88+
89+
90+
class ContainerArgs(ContainerArgsWithoutDryRun, total=False):
91+
dry_run: bool
92+
93+
6794
_THIS_DIR = os.path.dirname(__file__)
6895
_OPTIONS_FILE = os.path.join(_THIS_DIR, "fluent_launcher_options.json")
6996
logger = logging.getLogger("pyfluent.launcher")
@@ -89,27 +116,8 @@ class DockerLauncher:
89116

90117
def __init__(
91118
self,
92-
mode: FluentMode | str | None = None,
93-
ui_mode: UIMode | str | None = None,
94-
graphics_driver: (
95-
FluentWindowsGraphicsDriver | FluentLinuxGraphicsDriver | str | None
96-
) = None,
97-
product_version: FluentVersion | str | float | int | None = None,
98-
dimension: Dimension | int | None = None,
99-
precision: Precision | str | None = None,
100-
processor_count: int | None = None,
101-
start_timeout: int = 60,
102-
additional_arguments: str = "",
103-
container_dict: dict | None = None,
104-
dry_run: bool = False,
105-
cleanup_on_exit: bool = True,
106-
start_transcript: bool = True,
107-
py: bool | None = None,
108-
gpu: bool | None = None,
109-
start_watchdog: bool | None = None,
110-
file_transfer_service: Any | None = None,
111-
use_docker_compose: bool | None = None,
112-
use_podman_compose: bool | None = None,
119+
mode: FluentMode | str,
120+
**kwargs: Unpack[ContainerArgs],
113121
):
114122
"""
115123
Launch a Fluent session in container mode.
@@ -184,31 +192,28 @@ def __init__(
184192
In job scheduler environments (e.g., SLURM, LSF, PBS), resources and compute nodes are allocated,
185193
and core counts are queried from these environments before being passed to Fluent.
186194
"""
187-
locals_ = locals().copy()
188-
argvals = {
189-
arg: locals_.get(arg)
190-
for arg in inspect.getargvalues(inspect.currentframe()).args
191-
}
192-
self.argvals, self.new_session = _get_argvals_and_session(argvals)
193-
if self.argvals["start_timeout"] is None:
195+
self.argvals, self.new_session = _get_argvals_and_session({**kwargs, mode: mode})
196+
if self.argvals.get("start_timeout") is None:
194197
self.argvals["start_timeout"] = 60
195-
self.file_transfer_service = file_transfer_service
198+
self.file_transfer_service = kwargs.get("file_transfer_service")
196199
if self.argvals["mode"] == FluentMode.SOLVER_ICING:
197200
self.argvals["fluent_icing"] = True
198-
if self.argvals["container_dict"] is None:
201+
if self.argvals.get("container_dict") is None:
199202
self.argvals["container_dict"] = {}
200-
if self.argvals["product_version"]:
203+
if "product_version" in self.argvals:
201204
self.argvals["container_dict"][
202205
"image_tag"
203206
] = f"v{FluentVersion(self.argvals['product_version']).value}"
204207

205208
self._args = _build_fluent_launch_args_string(**self.argvals).split()
206209
if FluentMode.is_meshing(self.argvals["mode"]):
207210
self._args.append(" -meshing")
211+
212+
use_docker_compose = kwargs.get("use_docker_compose")
213+
use_podman_compose = kwargs.get("use_podman_compose")
208214
self._compose_config = ComposeConfig(use_docker_compose, use_podman_compose)
209215

210216
def __call__(self):
211-
212217
if self.argvals["dry_run"]:
213218
config_dict, *_ = configure_container_dict(
214219
self._args,

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

Lines changed: 114 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
import inspect
3030
import logging
3131
import os
32-
from typing import Any, Dict
32+
from typing import Any, Literal, TypedDict, Unpack, final, overload
33+
34+
from typing_extensions import Required
3335

3436
import ansys.fluent.core as pyfluent
3537
from ansys.fluent.core._types import PathType
@@ -58,6 +60,7 @@
5860
from ansys.fluent.core.session_meshing import Meshing
5961
from ansys.fluent.core.session_pure_meshing import PureMeshing
6062
from ansys.fluent.core.session_solver import Solver
63+
from ansys.fluent.core.session_solver_aero import SolverAero
6164
from ansys.fluent.core.session_solver_icing import SolverIcing
6265
from ansys.fluent.core.utils.deprecate import deprecate_arguments
6366
from ansys.fluent.core.utils.fluent_version import FluentVersion
@@ -127,6 +130,94 @@ def _version_to_dimension(old_arg_val):
127130
return None
128131

129132

133+
class LaunchFluentArgs(TypedDict, total=False):
134+
product_version: FluentVersion | str | float | int | None
135+
dimension: Dimension | int
136+
precision: Precision | str
137+
processor_count: int | None
138+
journal_file_names: None | str | list[str]
139+
start_timeout: int
140+
additional_arguments: str
141+
env: dict[str, Any] | None
142+
start_container: bool | None
143+
container_dict: dict[str, Any] | None
144+
cleanup_on_exit: bool
145+
start_transcript: bool
146+
ui_mode: UIMode | str | None
147+
graphics_driver: (
148+
FluentWindowsGraphicsDriver | FluentLinuxGraphicsDriver | str | None
149+
)
150+
case_file_name: str | None
151+
case_data_file_name: str | None
152+
lightweight_mode: bool | None
153+
py: bool | None
154+
gpu: bool | list[int] | None
155+
cwd: str | None
156+
fluent_path: str | None
157+
topy: str | list | None
158+
start_watchdog: bool | None
159+
file_transfer_service: Any | None
160+
use_docker_compose: bool
161+
use_podman_compose: bool
162+
163+
164+
class SlurmSchedulerOptions(TypedDict, total=False):
165+
scheduler: Required[Literal["slurm"]]
166+
scheduler_headnode: str
167+
scheduler_queue: str
168+
scheduler_account: str
169+
170+
171+
@overload
172+
def launch_fluent(
173+
*,
174+
dry_run: Literal[False] = False,
175+
mode: Literal[FluentMode.MESHING, "meshing"],
176+
**kwargs: Unpack[LaunchFluentArgs],
177+
) -> Meshing: ...
178+
@overload
179+
def launch_fluent(
180+
*,
181+
dry_run: Literal[False] = False,
182+
mode: Literal[FluentMode.PURE_MESHING, "pure_meshing"],
183+
**kwargs: Unpack[LaunchFluentArgs],
184+
) -> PureMeshing: ...
185+
@overload
186+
def launch_fluent(
187+
*,
188+
dry_run: Literal[False] = False,
189+
mode: Literal[FluentMode.SOLVER, "solver"] = FluentMode.SOLVER,
190+
**kwargs: Unpack[LaunchFluentArgs],
191+
) -> Solver: ...
192+
@overload
193+
def launch_fluent(
194+
*,
195+
dry_run: Literal[False] = False,
196+
mode: Literal[FluentMode.SOLVER_ICING, "solver_icing"],
197+
**kwargs: Unpack[LaunchFluentArgs],
198+
) -> SolverIcing: ...
199+
@overload
200+
def launch_fluent(
201+
*,
202+
dry_run: Literal[False] = False,
203+
mode: Literal[FluentMode.SOLVER_AERO, "solver_aero"] = ...,
204+
**kwargs: Unpack[LaunchFluentArgs],
205+
) -> SolverAero: ...
206+
@overload
207+
def launch_fluent(
208+
*,
209+
dry_run: Literal[False] = False,
210+
scheduler_options: SlurmSchedulerOptions,
211+
mode: FluentMode | str = FluentMode.SOLVER,
212+
**kwargs: Unpack[LaunchFluentArgs],
213+
) -> SlurmFuture: ...
214+
@overload
215+
def launch_fluent(
216+
*,
217+
dry_run: Literal[True],
218+
**kwargs: Unpack[LaunchFluentArgs],
219+
) -> dict[str, Any]: ...
220+
130221
def _custom_converter_gui(kwargs):
131222
old_val = kwargs.pop("show_gui", None)
132223
kwargs["ui_mode"] = _show_gui_to_ui_mode(old_val, **kwargs)
@@ -152,14 +243,15 @@ def _custom_converter_dimension(kwargs):
152243
converter=_custom_converter_dimension,
153244
)
154245
def launch_fluent(
246+
*,
155247
product_version: FluentVersion | str | float | int | None = None,
156-
dimension: Dimension | int | None = None,
157-
precision: Precision | str | None = None,
248+
dimension: Dimension | int = Dimension.THREE,
249+
precision: Precision | str = Precision.DOUBLE,
158250
processor_count: int | None = None,
159251
journal_file_names: None | str | list[str] = None,
160-
start_timeout: int = None,
252+
start_timeout: int | None = None,
161253
additional_arguments: str = "",
162-
env: Dict[str, Any] | None = None,
254+
env: dict[str, Any] | None = None,
163255
start_container: bool | None = None,
164256
container_dict: dict | None = None,
165257
dry_run: bool = False,
@@ -172,18 +264,26 @@ def launch_fluent(
172264
case_file_name: "PathType | None" = None,
173265
case_data_file_name: "PathType | None" = None,
174266
lightweight_mode: bool | None = None,
175-
mode: FluentMode | str | None = None,
267+
mode: FluentMode | str = FluentMode.SOLVER,
176268
py: bool | None = None,
177269
gpu: bool | list[int] | None = None,
178270
cwd: "PathType | None" = None,
179271
fluent_path: "PathType | None" = None,
180272
topy: str | list | None = None,
181273
start_watchdog: bool | None = None,
182-
scheduler_options: dict | None = None,
274+
scheduler_options: SlurmSchedulerOptions | None = None,
183275
file_transfer_service: Any | None = None,
184-
use_docker_compose: bool | None = None,
185-
use_podman_compose: bool | None = None,
186-
) -> Meshing | PureMeshing | Solver | SolverIcing | SlurmFuture | dict:
276+
use_docker_compose: bool = False,
277+
use_podman_compose: bool = False,
278+
) -> (
279+
Meshing
280+
| PureMeshing
281+
| Solver
282+
| SolverIcing
283+
| SolverAero
284+
| SlurmFuture
285+
| dict[Any, Any]
286+
):
187287
"""Launch Fluent locally in server mode or connect to a running Fluent server
188288
instance.
189289
@@ -204,8 +304,7 @@ def launch_fluent(
204304
in which case ``Dimension.THREE`` is used. Options are either the values of the
205305
``Dimension`` enum (``Dimension.TWO`` or ``Dimension.THREE``) or any of ``2`` and ``3``.
206306
precision : Precision or str, optional
207-
Floating point precision. The default is ``None``, in which case ``Precision.DOUBLE``
208-
is used. Options are either the values of the ``Precision`` enum (``Precision.SINGLE``
307+
Floating point precision. Options are either the values of the ``Precision`` enum (``Precision.SINGLE``
209308
or ``Precision.DOUBLE``) or any of ``"double"`` and ``"single"``.
210309
processor_count : int, optional
211310
Number of processors. The default is ``None``, in which case ``1``
@@ -270,7 +369,7 @@ def launch_fluent(
270369
This parameter is used only when ``case_file_name`` is provided. The default is ``False``.
271370
mode : FluentMode or str or None, optional
272371
Launch mode of Fluent to point to a specific session type. Can be a
273-
``FluentMode`` enum member or a string. The default value is ``None``.
372+
``FluentMode`` enum member or a string. The default value is ``SOLVER``.
274373
Valid string options include ``"meshing"``, ``"pure-meshing"``, and
275374
``"solver"``.
276375
py : bool, optional
@@ -372,6 +471,7 @@ def _mode_to_launcher_type(fluent_launch_mode: LaunchMode):
372471

373472

374473
def connect_to_fluent(
474+
*,
375475
ip: str | None = None,
376476
port: int | None = None,
377477
cleanup_on_exit: bool = False,
@@ -380,7 +480,7 @@ def connect_to_fluent(
380480
password: str | None = None,
381481
start_watchdog: bool | None = None,
382482
file_transfer_service: Any | None = None,
383-
) -> Meshing | PureMeshing | Solver | SolverIcing:
483+
) -> Meshing | PureMeshing | Solver | SolverIcing | SolverAero:
384484
"""Connect to an existing Fluent server instance.
385485
386486
Parameters

0 commit comments

Comments
 (0)