Skip to content

Commit

Permalink
refactor: Remove reinitialize from enhanced meshing workflow. (#2880)
Browse files Browse the repository at this point in the history
* refactor: Remove reinitialize from enhanced meshing workflow.

* Refresh task list before accessing attributes.

* Update test marker.

* __setattr__ in Workflow

* Remove logger.

* revert task initialization at each set_state

---------

Co-authored-by: Harshal Pohekar <[email protected]>
  • Loading branch information
prmukherj and hpohekar authored Jun 13, 2024
1 parent 2316f7a commit 20adc62
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 53 deletions.
7 changes: 1 addition & 6 deletions src/ansys/fluent/core/meshing/meshing_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,9 @@ def __init__(
self._unsubscribe_root_affected_callback()
self._new_workflow(name=self._name)

def reinitialize(self) -> None:
"""Reinitialize the same workflow."""
self._new_workflow(name=self._name)

def __getattribute__(self, item: str):
if (
item not in ["reinitialize"]
and not item.startswith("_")
not item.startswith("_")
and not getattr(self._meshing.GlobalSettings, self._identifier)()
):
raise RuntimeError(
Expand Down
92 changes: 54 additions & 38 deletions src/ansys/fluent/core/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ def _refresh_task_accessors(obj):
_refresh_task_accessors(task)


def _call_refresh_task_accessors(obj):
"""This layer handles exception for PyConsole."""
try:
_refresh_task_accessors(obj)
except Exception:
# Is there a more specific Exception derived class
# for which we know it is correct to pass?
pass


def _convert_task_list_to_display_names(workflow_root, task_list):
if workflow_root.service.cache is not None:
workflow_state = workflow_root.service.cache.get_state(
Expand Down Expand Up @@ -1280,37 +1290,41 @@ def __init__(
command_source : PyMenuGeneric
The application root for commanding.
"""
self._workflow = workflow
self._command_source = command_source
self._python_task_names = []
self._lock = threading.RLock()
self._refreshing = False
self._dynamic_python_names = False
self._refresh_count = 0
self._ordered_children = []
self._task_list = []
self._getattr_recurse_depth = 0
self._main_thread_ident = None
self._task_objects = {}
self._dynamic_interface = False
self._python_name_command_id_map = {}
self._python_name_display_id_map = {}
self._python_name_display_text_map = {}
self._repeated_task_python_name_display_text_map = {}
self._initial_task_python_names_map = {}
self._unwanted_attrs = {
"reset_workflow",
"initialize_workflow",
"load_workflow",
"insert_new_task",
"create_composite_task",
"create_new_workflow",
"rules",
"service",
"task_object",
"workflow",
}
self._fluent_version = fluent_version
self.__dict__.update(
dict(
_workflow=workflow,
_command_source=command_source,
_python_task_names=[],
_lock=threading.RLock(),
_refreshing=False,
_dynamic_python_names=False,
_refresh_count=0,
_ordered_children=[],
_task_list=[],
_getattr_recurse_depth=0,
_main_thread_ident=None,
_task_objects={},
_dynamic_interface=False,
_python_name_command_id_map={},
_python_name_display_id_map={},
_python_name_display_text_map={},
_repeated_task_python_name_display_text_map={},
_initial_task_python_names_map={},
_unwanted_attrs={
"reset_workflow",
"initialize_workflow",
"load_workflow",
"insert_new_task",
"create_composite_task",
"create_new_workflow",
"rules",
"service",
"task_object",
"workflow",
},
_fluent_version=fluent_version,
)
)

def task(self, name: str) -> BaseTask:
"""Get a TaskObject by name, in a ``BaseTask`` wrapper.
Expand Down Expand Up @@ -1414,6 +1428,14 @@ def __getattr__(self, attr):
return obj
return super().__getattribute__(attr)

def __setattr__(self, attr, value):
if attr in self.__dict__:
self.__dict__[attr] = value
elif attr in self._task_objects:
self._task_objects[attr].set_state(value)
else:
super().__setattr__(attr, value)

def __dir__(self):
"""Override the behavior of ``dir`` to include attributes in the
``WorkflowWrapper`` class and the underlying workflow."""
Expand Down Expand Up @@ -1467,7 +1489,6 @@ def _activate_dynamic_interface(self, dynamic_interface: bool):
self._initialize_methods(dynamic_interface=dynamic_interface)

def _unsubscribe_root_affected_callback(self):
# if the same workflow is not being reinitialized, unsubscribe the root affected callback
if self._workflow.service in self._root_affected_cb_by_server:
self._root_affected_cb_by_server[self._workflow.service].unsubscribe()
self._root_affected_cb_by_server.pop(self._workflow.service)
Expand Down Expand Up @@ -1557,12 +1578,7 @@ def refresh_after_sleep(_):
logger.debug("Already _refreshing, ...")
self._refreshing = True
logger.debug("Call _refresh_task_accessors")
try:
_refresh_task_accessors(self)
except Exception:
# Is there a more specific Exception derived class
# for which we know it is correct to pass?
pass
_call_refresh_task_accessors(self)
self._refresh_count += 1
self._refreshing = False

Expand Down
16 changes: 7 additions & 9 deletions tests/test_new_meshing_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1101,9 +1101,8 @@ def test_attrs_in_watertight_meshing_workflow(new_mesh_session):

assert watertight.import_geometry.file_name()
# Reinitialize the workflow:
watertight.reinitialize()
# Failing randomly in CI.
# assert not watertight.import_geometry.file_name()
watertight = new_mesh_session.watertight()
assert not watertight.import_geometry.file_name()


@pytest.mark.codegen_required
Expand Down Expand Up @@ -1147,8 +1146,7 @@ def test_attrs_in_fault_tolerant_meshing_workflow(new_mesh_session):

assert fault_tolerant.import_cad_and_part_management.fmd_file_name()
# Reinitialize the workflow:
fault_tolerant.reinitialize()

fault_tolerant = new_mesh_session.fault_tolerant()
assert not fault_tolerant.import_cad_and_part_management.fmd_file_name()


Expand Down Expand Up @@ -1177,7 +1175,7 @@ def test_switch_between_workflows(new_mesh_session):
watertight.import_geometry.arguments()

# Re-initialize watertight
watertight.reinitialize()
watertight = meshing.watertight()

# 'import_cad_and_part_management' is a fault-tolerant workflow command which is not
# available now since we have changed to watertight in the backend.
Expand All @@ -1201,12 +1199,12 @@ def test_switch_between_workflows(new_mesh_session):
fault_tolerant.import_cad_and_part_management.arguments()

# Re-initialize fault-tolerant
fault_tolerant.reinitialize()
fault_tolerant = meshing.fault_tolerant()
assert fault_tolerant.import_cad_and_part_management.arguments()


@pytest.mark.codegen_required
@pytest.mark.fluent_version(">=24.1")
@pytest.mark.fluent_version(">=24.2")
def test_new_meshing_workflow_without_dm_caching(
disable_datamodel_cache, new_mesh_session
):
Expand Down Expand Up @@ -1265,7 +1263,7 @@ def test_new_meshing_workflow_switching_without_dm_caching(
watertight.import_geometry.arguments()
assert fault_tolerant.import_cad_and_part_management.arguments()

watertight.reinitialize()
watertight = new_mesh_session.watertight()
with pytest.raises(RuntimeError):
fault_tolerant.import_cad_and_part_management.arguments()
assert watertight.import_geometry.arguments()
Expand Down

0 comments on commit 20adc62

Please sign in to comment.