Skip to content

Commit

Permalink
fix: issues in workflow and case reader (#2422)
Browse files Browse the repository at this point in the history
* Fix issues with case reader.

* Remove read-only behaviour of args.

* Update tests.

* Update test.

* Update test marker.

* Update attr error.
  • Loading branch information
prmukherj authored Feb 9, 2024
1 parent d5dbfe4 commit 72493e9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
12 changes: 5 additions & 7 deletions src/ansys/fluent/core/filereader/case_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,11 +634,9 @@ def _get_case_file_name_from_flprj(flprj_file):
# If the project file name begins with a digit then the node to find will be prepended
# with "_". Rather than making any assumptions that this is a hard rule, or what
# the scope of the rule is, simply retry with the name prepended:
find_folder = lambda retry=False: root.find(("_" if retry else "") + folder_name)
return (
(find_folder() or find_folder(retry=True))
.find("Input")
.find("Case")
.find("Target")
.get("value")
folder_obj = (
root.find(folder_name)
if len(root.find(folder_name)) > 0
else root.find("_" + folder_name)
)
return folder_obj.find("Input").find("Case").find("Target").get("value")
18 changes: 15 additions & 3 deletions src/ansys/fluent/core/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,11 @@ def __init__(self, task: BaseTask) -> None:
task : BaseTask
The task holding these arguments.
"""
self._task = task
self.__dict__.update(
dict(
_task=task,
)
)

def set_state(self, args: dict) -> None:
"""Overwrite arguments.
Expand Down Expand Up @@ -441,6 +445,14 @@ def get_state(self, explicit_only=False) -> dict:
def __getattr__(self, attr):
return getattr(self._task._command_arguments, attr)

def __setattr__(self, key, value):
try:
getattr(self, key).set_state(value)
except AttributeError:
raise AttributeError(
f"No attribute named '{key}' in '{self._task.name()}'."
)

def __setitem__(self, key, value):
self._task._command_arguments.__setitem__(key, value)

Expand Down Expand Up @@ -540,15 +552,15 @@ def _refreshed_command(self) -> ReadOnlyObject:
cmd = self._command()
if task_arg_state:
cmd.set_state(task_arg_state)
return ReadOnlyObject(self._cmd_sub_items_read_only(cmd, cmd()))
return self._cmd_sub_items_read_only(cmd, cmd())

def _cmd_sub_items_read_only(self, cmd, cmd_state):
for key, value in cmd_state.items():
if type(value) == dict:
setattr(
cmd, key, self._cmd_sub_items_read_only(getattr(cmd, key), value)
)
setattr(cmd, key, ReadOnlyObject(getattr(cmd, key)))
setattr(cmd, key, getattr(cmd, key))
return cmd

def _command(self):
Expand Down
34 changes: 30 additions & 4 deletions tests/test_meshing_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def test_attribute_query_list_types(new_mesh_session):
assert ["CAD", "Mesh"] == igt.arguments.FileFormat.getAttribValue("allowedValues")


@pytest.mark.fluent_version(">=23.1")
@pytest.mark.fluent_version(">=23.2")
@pytest.mark.codegen_required
def test_accessors_for_argument_sub_items(new_mesh_session):
session_new = new_mesh_session
Expand All @@ -234,10 +234,35 @@ def test_accessors_for_argument_sub_items(new_mesh_session):
w.InitializeWorkflow(WorkflowType="Watertight Geometry")
import_geom = w.task("Import Geometry")
assert import_geom.arguments.LengthUnit.default_value() == "mm"
assert import_geom.arguments.MeshUnit.is_read_only()
assert import_geom.arguments.LengthUnit.allowed_values() == [
"m",
"cm",
"mm",
"in",
"ft",
"um",
"nm",
]
assert import_geom.arguments.LengthUnit() == "mm"
import_geom.arguments.LengthUnit.set_state("cm")
assert import_geom.arguments.LengthUnit.get_state() == "cm"
import_geom.arguments.LengthUnit = "in"
assert import_geom.arguments.LengthUnit() == "in"

assert not import_geom.arguments.MeshUnit.is_read_only()
assert import_geom.arguments.LengthUnit.is_active()
assert import_geom.arguments.FileName.is_read_only()
assert import_geom.arguments.CadImportOptions.OneZonePer.is_read_only()
assert not import_geom.arguments.FileName.is_read_only()
assert not import_geom.arguments.FileName()
import_geom.arguments.FileName = "xyz.txt"
assert import_geom.arguments.FileName() == "xyz.txt"
with pytest.raises(AttributeError) as msg:
import_geom.arguments.File = "sample.txt"
assert msg.value.args[0] == "No attribute named 'File' in 'Import Geometry'."
assert not import_geom.arguments.CadImportOptions.OneZonePer.is_read_only()

assert import_geom.arguments.CadImportOptions.OneZonePer() == "body"
import_geom.arguments.CadImportOptions.OneZonePer.set_state("face")
assert import_geom.arguments.CadImportOptions.OneZonePer() == "face"

volume_mesh_gen = w.task("Generate the Volume Mesh")
assert (
Expand Down Expand Up @@ -280,6 +305,7 @@ def test_accessors_for_argument_sub_items(new_mesh_session):
)


@pytest.mark.skip("Wait for later implementation.")
@pytest.mark.fluent_version(">=23.1")
@pytest.mark.codegen_required
def test_read_only_behaviour_of_command_arguments(new_mesh_session):
Expand Down

0 comments on commit 72493e9

Please sign in to comment.