Skip to content

Commit

Permalink
feat: Store the original scheme name of aliases (#3509)
Browse files Browse the repository at this point in the history
* feat: Store the original scheme name of aliases

* fix: test
  • Loading branch information
mkundu1 authored Nov 24, 2024
1 parent 90d5818 commit d6447cc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/ansys/fluent/core/codegen/settingsgen_old.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ def _populate_classes(parent_dir):
child_aliases = getattr(cls, "_child_aliases", None)
if child_aliases:
f.write(f"{istr1}_child_aliases = dict(\n")
f.writelines([f'{istr2}{k}="{v}",\n' for k, v in child_aliases.items()])
f.writelines([f"{istr2}{k}={v!r},\n" for k, v in child_aliases.items()])
f.write(f"{istr1})\n\n")

# write object type
Expand Down
15 changes: 12 additions & 3 deletions src/ansys/fluent/core/solver/flobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ def _unalias(self, cls, value):
outer_set_states = []
for k, v in value.items():
if hasattr(cls, "_child_aliases") and k in cls._child_aliases:
alias = cls._child_aliases[k]
alias, _ = cls._child_aliases[k]
comps = alias.split("/")
if comps[0] == "..":
outer_obj = self
Expand Down Expand Up @@ -1122,6 +1122,7 @@ def __getattribute__(self, name):
raise InactiveObjectError(self.python_path)
alias = super().__getattribute__("_child_aliases").get(name)
if alias:
alias = alias[0]
alias_obj = self._child_alias_objs.get(name)
if alias_obj is None:
obj = self.find_object(alias)
Expand Down Expand Up @@ -1457,6 +1458,7 @@ def get(self, name: str) -> ChildTypeT:
def __getattr__(self, name: str):
alias = self._child_aliases.get(name)
if alias:
alias = alias[0]
alias_obj = self._child_alias_objs.get(name)
if alias_obj is None:
obj = self.find_object(alias)
Expand Down Expand Up @@ -1574,6 +1576,7 @@ def __setitem__(self, index: int, value):
def __getattr__(self, name: str):
alias = self._child_aliases.get(name)
if alias:
alias = alias[0]
alias_obj = self._child_alias_objs.get(name)
if alias_obj is None:
obj = self.find_object(alias)
Expand Down Expand Up @@ -1664,6 +1667,7 @@ def get_completer_info(self, prefix="", excluded=None) -> List[List[str]]:
def __getattr__(self, name: str):
alias = self._child_aliases.get(name)
if alias:
alias = alias[0]
alias_obj = self._child_alias_objs.get(name)
if alias_obj is None:
obj = self.find_object(alias)
Expand Down Expand Up @@ -2153,8 +2157,13 @@ def _process_cls_names(info_dict, names, write_doc=False):
for k, v in (
child_aliases | command_aliases | query_aliases | argument_aliases
).items():
cls._child_aliases[to_python_name(k)] = "/".join(
x if x == ".." else to_python_name(x) for x in v.split("/")
# Storing the original name as we don't have any other way
# to recover it at runtime.
cls._child_aliases[to_python_name(k)] = (
"/".join(
x if x == ".." else to_python_name(x) for x in v.split("/")
),
k,
)

except Exception:
Expand Down
29 changes: 16 additions & 13 deletions tests/test_settings_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,13 @@ def test_deprecated_settings_with_custom_aliases(new_solver_session):
solver = new_solver_session
case_path = download_file("mixing_elbow.cas.h5", "pyfluent/mixing_elbow")
download_file("mixing_elbow.dat.h5", "pyfluent/mixing_elbow")
solver.file._setattr("_child_aliases", {"rcd": "read_case_data"})
solver.file._setattr("_child_aliases", {"rcd": ("read_case_data", "rcd")})
with pytest.warns(DeprecatedSettingWarning):
solver.file.rcd(file_name=case_path)

solver.setup.boundary_conditions.velocity_inlet.child_object_type._child_aliases[
"mom"
] = "momentum"
] = ("momentum", "mom")
with pytest.warns(DeprecatedSettingWarning):
solver.setup.boundary_conditions.velocity_inlet["hot-inlet"].mom.velocity = 20
assert (
Expand Down Expand Up @@ -261,12 +261,9 @@ def test_deprecated_settings_with_custom_aliases(new_solver_session):
)
> 0
)
assert (
solver.setup.boundary_conditions.wall[
"wall-inlet"
].thermal.temperature._child_aliases["constant"]
== "value"
)
assert solver.setup.boundary_conditions.wall[
"wall-inlet"
].thermal.temperature._child_aliases["constant"] == ("value", "constant")
with pytest.warns(DeprecatedSettingWarning):
solver.setup.boundary_conditions.wall[
"wall-inlet"
Expand Down Expand Up @@ -298,7 +295,7 @@ def test_deprecated_settings_with_custom_aliases(new_solver_session):
== 410
)

solver.setup.boundary_conditions._setattr("_child_aliases", {"w": "wall"})
solver.setup.boundary_conditions._setattr("_child_aliases", {"w": ("wall", "w")})
with pytest.warns(DeprecatedSettingWarning):
solver.setup.boundary_conditions.w["wall-inlet"].thermal.temperature.value = 420

Expand All @@ -307,7 +304,7 @@ def test_deprecated_settings_with_custom_aliases(new_solver_session):
== 420
)

solver.setup._setattr("_child_aliases", {"bc": "boundary_conditions"})
solver.setup._setattr("_child_aliases", {"bc": ("boundary_conditions", "bc")})
with pytest.warns(DeprecatedSettingWarning):
solver.setup.bc.wall["wall-inlet"].thermal.temperature.value = 430

Expand All @@ -326,7 +323,7 @@ def test_deprecated_settings_with_custom_aliases(new_solver_session):
== 400
)

solver.results._setattr("_child_aliases", {"gr": "graphics"})
solver.results._setattr("_child_aliases", {"gr": ("graphics", "gr")})
with pytest.warns(DeprecatedSettingWarning):
solver.results.gr.contour.create("c1")

Expand All @@ -342,7 +339,10 @@ def test_deprecated_settings_with_custom_aliases(new_solver_session):

solver.setup.boundary_conditions.velocity_inlet[
"hot-inlet"
].momentum.velocity._child_aliases["hd"] = "../../turbulence/hydraulic_diameter"
].momentum.velocity._child_aliases["hd"] = (
"../../turbulence/hydraulic_diameter",
"hd",
)
with pytest.warns(DeprecatedSettingWarning):
solver.setup.boundary_conditions.velocity_inlet[
"hot-inlet"
Expand Down Expand Up @@ -481,7 +481,10 @@ def test_child_alias_with_parent_path(mixing_elbow_settings_session):
solver.settings.solution.initialization.hybrid_initialize()
assert (
solver.settings.setup.models.discrete_phase.numerics.node_based_averaging.kernel._child_aliases
== {"gaussian_factor": "../gaussian_factor", "option": "../kernel_type"}
== {
"gaussian_factor": ("../gaussian_factor", "gaussian-factor"),
"option": ("../kernel_type", "option"),
}
)
solver.settings.setup.models.discrete_phase.numerics.node_based_averaging.enabled = (
True
Expand Down

0 comments on commit d6447cc

Please sign in to comment.