Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update arguments behaviour. #3332

Merged
merged 5 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/ansys/fluent/core/services/datamodel_se.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,11 @@ def fix_state(self, rules, path) -> None:
)

def update_dict(
self, rules: str, path: str, dict_state: dict[str, _TValue]
self,
rules: str,
path: str,
dict_state: dict[str, _TValue],
recursive=False,
) -> None:
"""Update the dict."""
request = DataModelProtoModule.UpdateDictRequest(
Expand Down Expand Up @@ -1392,7 +1396,7 @@ class PyDictionary(PyParameter):
to dict.update semantics (same as update_dict(dict_state))]
"""

def update_dict(self, dict_state: dict[str, Any]) -> None:
def update_dict(self, dict_state: dict[str, Any], recursive=False) -> None:
"""Update the state of the current object if the current object is a Dict in the
data model, else throws RuntimeError (currently not showing up in Python).
Update is executed according to dict.update semantics.
Expand All @@ -1402,6 +1406,9 @@ def update_dict(self, dict_state: dict[str, Any]) -> None:
dict_state : dict[str, Any]
Incoming dict state

recursive: bool
Flag to update the nested dictionary structure.

Raises
------
ReadOnlyObjectError
Expand All @@ -1410,7 +1417,7 @@ def update_dict(self, dict_state: dict[str, Any]) -> None:
if self.get_attr(Attribute.IS_READ_ONLY.value):
raise ReadOnlyObjectError(type(self).__name__)
self.service.update_dict(
self.rules, convert_path_to_se_path(self.path), dict_state
self.rules, convert_path_to_se_path(self.path), dict_state, recursive
)

updateDict = update_dict
Expand Down
32 changes: 25 additions & 7 deletions src/ansys/fluent/core/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ def __setattr__(self, attr, value):
logger.debug(f"BaseTask.__setattr__({attr}, {value})")
if attr in self.__dict__:
self.__dict__[attr] = value
elif attr in self.arguments():
elif attr in self.arguments() or attr == "arguments":
getattr(self, attr).set_state(value)
else:
setattr(self._task, attr, value)
Expand Down Expand Up @@ -763,9 +763,31 @@ def _assign(self, args: dict, fn) -> None:
# TODO: Figure out proper way to implement "add_child".
if "add_child" in args:
self._snake_to_camel_map["add_child"] = "AddChild"

cmd_args = self._task._command_arguments
for key, val in args.items():
camel_args[self._snake_to_camel_map[key] if key.islower() else key] = val
getattr(self._task.Arguments, fn)(camel_args)
camel_arg = self._snake_to_camel_map[key] if key.islower() else key
# TODO: Implement enhanced meshing workflow to hide away internal info.
if isinstance(
getattr(cmd_args, camel_arg), PySingletonCommandArgumentsSubItem
):
updated_dict = {}
for attr, attr_val in val.items():
camel_attr = snake_to_camel_case(
str(attr),
getattr(
self, camel_to_snake_case(key)
)._get_camel_case_arg_keys()
or [],
)
updated_dict[camel_attr] = attr_val
camel_args[camel_arg] = updated_dict
else:
camel_args[camel_arg] = val
if fn == "update_dict":
self._task.Arguments.update_dict(camel_args, recursive=True)
else:
getattr(self._task.Arguments, fn)(camel_args)
try:
self._refresh_command_after_changing_args(old_state)
except Exception as ex:
Expand Down Expand Up @@ -895,10 +917,6 @@ def __setattr__(self, attr, value):
if attr in self.__dict__:
self.__dict__[attr] = value
else:
camel_attr = snake_to_camel_case(
str(attr), self._get_camel_case_arg_keys() or []
)
attr = camel_attr or attr
self.set_state({attr: value})

def __dir__(self):
Expand Down
18 changes: 13 additions & 5 deletions tests/test_new_meshing_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,18 +597,26 @@ def test_updating_state_in_new_meshing_workflow(new_meshing_session):
"mixing_elbow.pmdb", "pyfluent/mixing_elbow"
)
watertight = new_meshing_session.watertight()
watertight.import_geometry.file_name.set_state(import_file_name)
assert watertight.import_geometry.length_unit() == "mm"
watertight.import_geometry.length_unit = "in"
assert watertight.import_geometry.length_unit.get_state() == "in"
assert watertight.import_geometry.cad_import_options.feature_angle() == 40.0
watertight.import_geometry.cad_import_options.feature_angle.set_state(25.0)
assert watertight.import_geometry.cad_import_options.feature_angle() == 25.0
assert (
watertight.import_geometry.cad_import_options.one_zone_per.allowed_values()
== ["body", "face", "object"]
)
assert watertight.import_geometry.cad_import_options.one_zone_per() == "body"
watertight.import_geometry.arguments = {
"file_name": import_file_name,
"length_unit": "in",
"cad_import_options": {"feature_angle": 35, "one_zone_per": "object"},
}
assert watertight.import_geometry.cad_import_options.feature_angle() == 35.0
assert (
watertight.import_geometry.cad_import_options.one_zone_per.get_state()
== "object"
)
assert watertight.import_geometry.length_unit.get_state() == "in"
watertight.import_geometry.cad_import_options.feature_angle = 25.0
assert watertight.import_geometry.cad_import_options.feature_angle() == 25.0
watertight.import_geometry.cad_import_options.one_zone_per = "face"
assert watertight.import_geometry.cad_import_options.one_zone_per() == "face"
watertight.import_geometry()
Expand Down
Loading