Skip to content

Commit

Permalink
Feat/local params (#47)
Browse files Browse the repository at this point in the history
* local para

* PoC

* local para

* cons fix

* refactor

* test

* refactor

* spread values into runtime study

* spread values into runtime study

* spread values into runtime study

* in parallel = tricky

* fix for class attributes

* sync fix

* fix by using base dp expicitly

* nice refactor

* nice refactor

* nice refactor

* nice refactor

* nice refactor

* nice refactor

* nice refactor

* nice refactor

* nice refactor

* h5py

* h5py

* h5py

* h5py

* test simplified

* refac

* refac

* refac

* refac

* refac

* test simplified

* testing

* name fix

* tidy ups

* tidy ups

* tidy up

* tidy ups

* tidy ups

* tidy ups

* tidy ups

* extend case reader

* conditional import

* review comments

* undo test changes

* .h5 checks

* .h5 checks

* redo test changes so that tests don't fail

* case reader

* Update conf.py
  • Loading branch information
seanpearsonuk authored Jul 23, 2022
1 parent 69a32f4 commit 539f31b
Show file tree
Hide file tree
Showing 13 changed files with 1,130 additions and 28 deletions.
2 changes: 1 addition & 1 deletion doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
# Intersphinx mapping
intersphinx_mapping = {
"python": ("https://docs.python.org/dev", None),
"scipy": ("https://docs.scipy.org/doc/scipy/reference", None),
"scipy": ("https://docs.scipy.org/doc/scipy", None),
"numpy": ("https://numpy.org/devdocs", None),
"matplotlib": ("https://matplotlib.org/stable", None),
"pandas": ("https://pandas.pydata.org/pandas-docs/stable", None),
Expand Down
2 changes: 2 additions & 0 deletions examples/00-parametric/parametric_static_mixer_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@
session.solver.tui.define.boundary_conditions.set.velocity_inlet(
"inlet1", (), "vmag", "yes", "inlet1_vel", 1, "quit"
)

session.solver.tui.define.boundary_conditions.set.velocity_inlet(
"inlet1", (), "temperature", "yes", "inlet1_temp", 300, "quit"
)

session.solver.tui.define.boundary_conditions.set.velocity_inlet(
"inlet2", (), "vmag", "yes", "no", "inlet2_vel", 1, "quit"
)

session.solver.tui.define.boundary_conditions.set.velocity_inlet(
"inlet2", (), "temperature", "yes", "no", "inlet2_temp", 350, "quit"
)
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ packages = [
python = ">=3.7,<4.0"
importlib-metadata = {version = "^4.0", python = "<3.8"}
ansys-fluent-core = "~=0.10"
h5py = ">=3.7.0"

[tool.black]
line-length = 88
Expand Down
71 changes: 47 additions & 24 deletions src/ansys/fluent/parametric/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,33 +185,33 @@ class ParametricStudy:
Update a list of design points.
"""

_all_studies: Dict[int, "ParametricStudy"] = {}
current_study_name = None

def __init__(
self,
parametric_studies,
session=None,
name: Optional[str] = None,
design_points: Dict[str, DesignPoint] = None,
):
self._parametric_studies = parametric_studies
self.session = (
session if session is not None else (_shared_parametric_study_registry())
)
self.name = name
self.design_points = {}
if design_points is not None:
self.design_points = design_points
self.project_filepath = None
ParametricStudy._all_studies[id(self)] = self
self.session.register_study(self)

@classmethod
def get_all_studies(cls) -> Dict[str, "ParametricStudy"]:
def get_all_studies(self) -> Dict[str, "ParametricStudy"]:
"""Get all currently active studies.
Returns
-------
Dict[str, "ParametricStudy"]
currently active studies
"""
return {v.name: v for _, v in cls._all_studies.items()}
return {v.name: v for _, v in self.session._all_studies.items()}

def initialize(self) -> "ParametricStudy":
"""Initialize parametric study."""
Expand All @@ -235,7 +235,7 @@ def initialize(self) -> "ParametricStudy":
self._parametric_studies[self.name].design_points[BASE_DP_NAME],
)
self.design_points = {BASE_DP_NAME: base_design_point}
ParametricStudy.current_study_name = self.name
self.session.current_study_name = self.name
return self
else:
LOG.error("initialize is not available")
Expand All @@ -258,13 +258,13 @@ def rename(self, new_name: str) -> None:
@property
def is_current(self) -> bool:
"""Whether the parametric study is the current parametric study."""
return ParametricStudy.current_study_name == self.name
return self.session.current_study_name == self.name

def set_as_current(self) -> None:
"""Set the parametric study as the current parametric study."""
if not self.is_current:
self._parametric_studies.set_as_current(self.name)
ParametricStudy.current_study_name = self.name
self.session.current_study_name = self.name

def duplicate(self, copy_design_points: bool = True) -> "ParametricStudy":
"""Duplicate the current study.
Expand All @@ -283,9 +283,7 @@ def duplicate(self, copy_design_points: bool = True) -> "ParametricStudy":
self._parametric_studies.duplicate(copy_design_points=copy_design_points)
new_study_names = self._parametric_studies.get_object_names()
clone_name = set(new_study_names).difference(set(old_study_names)).pop()
current_study = ParametricStudy.get_all_studies()[
ParametricStudy.current_study_name
]
current_study = self.get_all_studies()[self.session.current_study_name]
if copy_design_points:
clone_design_points = {
k: DesignPoint(k, self._parametric_studies[clone_name].design_points[k])
Expand All @@ -298,9 +296,9 @@ def duplicate(self, copy_design_points: bool = True) -> "ParametricStudy":
)
clone_design_points = {BASE_DP_NAME: base_design_point}
clone = ParametricStudy(
self._parametric_studies, clone_name, clone_design_points
self._parametric_studies, self.session, clone_name, clone_design_points
)
ParametricStudy.current_study_name = clone.name
self.session.current_study_name = clone.name
return clone

def delete(self) -> None:
Expand All @@ -309,7 +307,7 @@ def delete(self) -> None:
LOG.error("Cannot delete the current study %s", self.name)
else:
del self._parametric_studies[self.name]
ParametricStudy._all_studies.pop(id(self))
self.session._all_studies.pop(id(self))
del self

def use_base_data(self) -> None:
Expand Down Expand Up @@ -508,11 +506,15 @@ def __init__(
parametric_project,
parametric_studies,
project_filepath: str,
session=None,
open_project: bool = True,
):
self._parametric_project = parametric_project
self._parametric_studies = parametric_studies
self.project_filepath = project_filepath
self.session = (
session if session is not None else (_shared_parametric_study_registry())
)
if open_project:
self.open(project_filepath=project_filepath)

Expand All @@ -534,7 +536,7 @@ def open(
)
self.project_filepath = project_filepath
for study_name in self._parametric_studies.get_object_names():
study = ParametricStudy(self._parametric_studies, study_name)
study = ParametricStudy(self._parametric_studies, self.session, study_name)
dps_settings = self._parametric_studies[study_name].design_points
for dp_name in dps_settings.get_object_names():
study.design_points[dp_name] = DesignPoint(
Expand Down Expand Up @@ -600,7 +602,16 @@ def __call__(self):
return pyfluent.launch_fluent(*self._args, **self._kwargs)


class ParametricSession:
class ParametricStudyRegistry:
def __init__(self):
self._all_studies: Dict[int, "ParametricStudy"] = {}
self.current_study_name = None

def register_study(self, study):
self._all_studies[id(study)] = study


class ParametricSession(ParametricStudyRegistry):
"""ParametricSession class which encapsulates studies and project.
Attributes
Expand Down Expand Up @@ -645,42 +656,45 @@ def __init__(
Whether to start streaming of Fluent transcript, by default
False.
"""
super().__init__()
self.studies = {}
self.project = None
self._session = launcher()
self.scheme_eval = self._session.scheme_eval.scheme_eval
self.scheme_eval(
"(set parametric-study-dependents-manager " "save-project-at-exit? #f)"
)
if start_transcript:
self.start_transcript()
if not start_transcript:
self.stop_transcript()
self._root = self._session.solver.root
if case_filepath is not None:
self._root.file.read(file_name=case_filepath, file_type="case")
study = ParametricStudy(self._root.parametric_studies).initialize()
study = ParametricStudy(self._root.parametric_studies, self).initialize()
self.studies[study.name] = study
self.project = ParametricProject(
parametric_project=self._root.file.parametric_project,
parametric_studies=self._root.parametric_studies,
project_filepath=str(study.project_filepath),
open_project=False,
session=self._session,
)
elif project_filepath is not None:
self.project = ParametricProject(
parametric_project=self._root.file.parametric_project,
parametric_studies=self._root.parametric_studies,
project_filepath=project_filepath,
session=self._session,
)
studies_settings = self._root.parametric_studies
for study_name in studies_settings.get_object_names():
study = ParametricStudy(studies_settings, study_name)
study = ParametricStudy(studies_settings, self, study_name)
dps_settings = studies_settings[study_name].design_points
for dp_name in dps_settings.get_object_names():
study.design_points[dp_name] = DesignPoint(
dp_name, dps_settings[dp_name]
)
self.studies[study_name] = study
ParametricStudy.current_study_name = self._root.current_parametric_study()
self.current_study_name = self._root.current_parametric_study()

def new_study(self) -> ParametricStudy:
"""Create new study.
Expand All @@ -690,7 +704,7 @@ def new_study(self) -> ParametricStudy:
ParametricStudy
New study.
"""
study = self.studies[ParametricStudy.current_study_name].duplicate()
study = self.studies[self.current_study_name].duplicate()
self.studies[study.name] = study
return study

Expand Down Expand Up @@ -741,4 +755,13 @@ def stop_transcript(self) -> None:
self._session.stop_transcript()


def _shared_parametric_study_registry():
if _shared_parametric_study_registry.instance is None:
_shared_parametric_study_registry.instance = ParametricStudyRegistry()
return _shared_parametric_study_registry.instance


_shared_parametric_study_registry.instance = None


from ansys.fluent.parametric.parameters import InputParameters, OutputParameters
Loading

0 comments on commit 539f31b

Please sign in to comment.