Skip to content

Commit

Permalink
feat: Builtin settings objects from Setup section of Fluent's outline…
Browse files Browse the repository at this point in the history
… tree (#3127)

feat: Initial implementation of settings objects
  • Loading branch information
mkundu1 authored Sep 16, 2024
1 parent 0ac70bf commit a3fbbb9
Show file tree
Hide file tree
Showing 7 changed files with 626 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ classifiers = [
include = [
{ path = "src/ansys/fluent/core/generated/fluent_version_*.py", format = ["sdist", "wheel"] },
{ path = "src/ansys/fluent/core/generated/meshing/tui_*.py", format = ["sdist", "wheel"] },
{ path = "src/ansys/fluent/core/generated/solver/tui_*.py", format = ["sdist", "wheel"] },
{ path = "src/ansys/fluent/core/generated/solver/*.py", format = ["sdist", "wheel"] },
{ path = "src/ansys/fluent/core/generated/datamodel_*/*.py", format = ["sdist", "wheel"] },
{ path = "src/ansys/fluent/core/generated/solver/settings_*/*.py", format = ["sdist", "wheel"] },
{ path = "src/ansys/fluent/core/generated/solver/settings_*/*.pyi", format = ["sdist", "wheel"] },
Expand Down
5 changes: 5 additions & 0 deletions src/ansys/fluent/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
warning,
)

try:
from ansys.fluent.core.generated.solver.settings_builtin import * # noqa: F401, F403
except (ImportError, AttributeError):
pass

_VERSION_INFO = None
"""Global variable indicating the version of the PyFluent package - Empty by default"""

Expand Down
8 changes: 7 additions & 1 deletion src/ansys/fluent/core/codegen/allapigen.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
from pathlib import Path
import pickle

from ansys.fluent.core.codegen import datamodelgen, settingsgen, tuigen
from ansys.fluent.core.codegen import (
builtin_settingsgen,
datamodelgen,
settingsgen,
tuigen,
)
from ansys.fluent.core.search import get_api_tree_file_name


Expand All @@ -22,6 +27,7 @@ def generate(version: str, static_infos: dict):
Path(api_tree_file).parent.mkdir(parents=True, exist_ok=True)
with open(api_tree_file, "wb") as f:
pickle.dump(api_tree, f)
builtin_settingsgen.generate()


if __name__ == "__main__":
Expand Down
46 changes: 46 additions & 0 deletions src/ansys/fluent/core/codegen/builtin_settingsgen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""Generate builtin setting classes."""

from ansys.fluent.core import CODEGEN_OUTDIR, FluentVersion
from ansys.fluent.core.solver.settings_builtin_data import DATA

_PY_FILE = CODEGEN_OUTDIR / "solver" / "settings_builtin.py"
_PYI_FILE = CODEGEN_OUTDIR / "solver" / "settings_builtin.pyi"


def generate():
"""Generate builtin setting classes."""
CODEGEN_OUTDIR.mkdir(exist_ok=True)
with open(_PY_FILE, "w") as f:
f.write('"""Solver settings."""\n\n')
f.write(
"from ansys.fluent.core.solver.settings_builtin_bases import _SingletonSetting, _NamedObjectSetting\n\n\n"
)
f.write("__all__ = [\n")
for name, _ in DATA.items():
f.write(f' "{name}",\n')
f.write("]\n\n")
for name, v in DATA.items():
kind, path = v
f.write(f"class {name}(_{kind}Setting):\n")
f.write(f' """{name} setting."""\n\n')

with open(_PYI_FILE, "w") as f:
for version in FluentVersion:
f.write(
f"from ansys.fluent.core.generated.solver.settings_{version.number} import root as settings_root_{version.number}\n"
)
f.write("\n\n")
for name, v in DATA.items():
kind, path = v
f.write(f"class {name}(\n")
if isinstance(path, str):
path = {v: path for v in FluentVersion}
for v, p in path.items():
if kind == "NamedObject":
p = f"{p}.child_object_type"
f.write(f" type(settings_root_{v.number}.{p}),\n")
f.write("): ...\n\n")


if __name__ == "__main__":
generate()
26 changes: 26 additions & 0 deletions src/ansys/fluent/core/solver/settings_builtin_bases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Base classes for builtin setting classes."""

from ansys.fluent.core.session_solver import Solver
from ansys.fluent.core.solver.settings_builtin_data import DATA


class _SingletonSetting:
def __new__(cls, solver: Solver):
obj = solver.settings
path = DATA[cls.__name__][1]
if isinstance(path, dict):
version = solver.get_fluent_version()
path = path.get(version)
if path is None:
raise RuntimeError(
f"{cls.__name__} is not supported in Fluent version {version}."
)
for comp in path.split("."):
obj = getattr(obj, comp)
return obj


class _NamedObjectSetting(_SingletonSetting):
def __new__(cls, solver: Solver, name: str):
obj = super().__new__(cls, solver)
return obj[name]
Loading

0 comments on commit a3fbbb9

Please sign in to comment.