Skip to content

Commit 20adc62

Browse files
prmukherjhpohekar
andauthored
refactor: Remove reinitialize from enhanced meshing workflow. (#2880)
* 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]>
1 parent 2316f7a commit 20adc62

File tree

3 files changed

+62
-53
lines changed

3 files changed

+62
-53
lines changed

src/ansys/fluent/core/meshing/meshing_workflow.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,9 @@ def __init__(
7272
self._unsubscribe_root_affected_callback()
7373
self._new_workflow(name=self._name)
7474

75-
def reinitialize(self) -> None:
76-
"""Reinitialize the same workflow."""
77-
self._new_workflow(name=self._name)
78-
7975
def __getattribute__(self, item: str):
8076
if (
81-
item not in ["reinitialize"]
82-
and not item.startswith("_")
77+
not item.startswith("_")
8378
and not getattr(self._meshing.GlobalSettings, self._identifier)()
8479
):
8580
raise RuntimeError(

src/ansys/fluent/core/workflow.py

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,16 @@ def _refresh_task_accessors(obj):
131131
_refresh_task_accessors(task)
132132

133133

134+
def _call_refresh_task_accessors(obj):
135+
"""This layer handles exception for PyConsole."""
136+
try:
137+
_refresh_task_accessors(obj)
138+
except Exception:
139+
# Is there a more specific Exception derived class
140+
# for which we know it is correct to pass?
141+
pass
142+
143+
134144
def _convert_task_list_to_display_names(workflow_root, task_list):
135145
if workflow_root.service.cache is not None:
136146
workflow_state = workflow_root.service.cache.get_state(
@@ -1280,37 +1290,41 @@ def __init__(
12801290
command_source : PyMenuGeneric
12811291
The application root for commanding.
12821292
"""
1283-
self._workflow = workflow
1284-
self._command_source = command_source
1285-
self._python_task_names = []
1286-
self._lock = threading.RLock()
1287-
self._refreshing = False
1288-
self._dynamic_python_names = False
1289-
self._refresh_count = 0
1290-
self._ordered_children = []
1291-
self._task_list = []
1292-
self._getattr_recurse_depth = 0
1293-
self._main_thread_ident = None
1294-
self._task_objects = {}
1295-
self._dynamic_interface = False
1296-
self._python_name_command_id_map = {}
1297-
self._python_name_display_id_map = {}
1298-
self._python_name_display_text_map = {}
1299-
self._repeated_task_python_name_display_text_map = {}
1300-
self._initial_task_python_names_map = {}
1301-
self._unwanted_attrs = {
1302-
"reset_workflow",
1303-
"initialize_workflow",
1304-
"load_workflow",
1305-
"insert_new_task",
1306-
"create_composite_task",
1307-
"create_new_workflow",
1308-
"rules",
1309-
"service",
1310-
"task_object",
1311-
"workflow",
1312-
}
1313-
self._fluent_version = fluent_version
1293+
self.__dict__.update(
1294+
dict(
1295+
_workflow=workflow,
1296+
_command_source=command_source,
1297+
_python_task_names=[],
1298+
_lock=threading.RLock(),
1299+
_refreshing=False,
1300+
_dynamic_python_names=False,
1301+
_refresh_count=0,
1302+
_ordered_children=[],
1303+
_task_list=[],
1304+
_getattr_recurse_depth=0,
1305+
_main_thread_ident=None,
1306+
_task_objects={},
1307+
_dynamic_interface=False,
1308+
_python_name_command_id_map={},
1309+
_python_name_display_id_map={},
1310+
_python_name_display_text_map={},
1311+
_repeated_task_python_name_display_text_map={},
1312+
_initial_task_python_names_map={},
1313+
_unwanted_attrs={
1314+
"reset_workflow",
1315+
"initialize_workflow",
1316+
"load_workflow",
1317+
"insert_new_task",
1318+
"create_composite_task",
1319+
"create_new_workflow",
1320+
"rules",
1321+
"service",
1322+
"task_object",
1323+
"workflow",
1324+
},
1325+
_fluent_version=fluent_version,
1326+
)
1327+
)
13141328

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

1431+
def __setattr__(self, attr, value):
1432+
if attr in self.__dict__:
1433+
self.__dict__[attr] = value
1434+
elif attr in self._task_objects:
1435+
self._task_objects[attr].set_state(value)
1436+
else:
1437+
super().__setattr__(attr, value)
1438+
14171439
def __dir__(self):
14181440
"""Override the behavior of ``dir`` to include attributes in the
14191441
``WorkflowWrapper`` class and the underlying workflow."""
@@ -1467,7 +1489,6 @@ def _activate_dynamic_interface(self, dynamic_interface: bool):
14671489
self._initialize_methods(dynamic_interface=dynamic_interface)
14681490

14691491
def _unsubscribe_root_affected_callback(self):
1470-
# if the same workflow is not being reinitialized, unsubscribe the root affected callback
14711492
if self._workflow.service in self._root_affected_cb_by_server:
14721493
self._root_affected_cb_by_server[self._workflow.service].unsubscribe()
14731494
self._root_affected_cb_by_server.pop(self._workflow.service)
@@ -1557,12 +1578,7 @@ def refresh_after_sleep(_):
15571578
logger.debug("Already _refreshing, ...")
15581579
self._refreshing = True
15591580
logger.debug("Call _refresh_task_accessors")
1560-
try:
1561-
_refresh_task_accessors(self)
1562-
except Exception:
1563-
# Is there a more specific Exception derived class
1564-
# for which we know it is correct to pass?
1565-
pass
1581+
_call_refresh_task_accessors(self)
15661582
self._refresh_count += 1
15671583
self._refreshing = False
15681584

tests/test_new_meshing_workflow.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,9 +1101,8 @@ def test_attrs_in_watertight_meshing_workflow(new_mesh_session):
11011101

11021102
assert watertight.import_geometry.file_name()
11031103
# Reinitialize the workflow:
1104-
watertight.reinitialize()
1105-
# Failing randomly in CI.
1106-
# assert not watertight.import_geometry.file_name()
1104+
watertight = new_mesh_session.watertight()
1105+
assert not watertight.import_geometry.file_name()
11071106

11081107

11091108
@pytest.mark.codegen_required
@@ -1147,8 +1146,7 @@ def test_attrs_in_fault_tolerant_meshing_workflow(new_mesh_session):
11471146

11481147
assert fault_tolerant.import_cad_and_part_management.fmd_file_name()
11491148
# Reinitialize the workflow:
1150-
fault_tolerant.reinitialize()
1151-
1149+
fault_tolerant = new_mesh_session.fault_tolerant()
11521150
assert not fault_tolerant.import_cad_and_part_management.fmd_file_name()
11531151

11541152

@@ -1177,7 +1175,7 @@ def test_switch_between_workflows(new_mesh_session):
11771175
watertight.import_geometry.arguments()
11781176

11791177
# Re-initialize watertight
1180-
watertight.reinitialize()
1178+
watertight = meshing.watertight()
11811179

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

12031201
# Re-initialize fault-tolerant
1204-
fault_tolerant.reinitialize()
1202+
fault_tolerant = meshing.fault_tolerant()
12051203
assert fault_tolerant.import_cad_and_part_management.arguments()
12061204

12071205

12081206
@pytest.mark.codegen_required
1209-
@pytest.mark.fluent_version(">=24.1")
1207+
@pytest.mark.fluent_version(">=24.2")
12101208
def test_new_meshing_workflow_without_dm_caching(
12111209
disable_datamodel_cache, new_mesh_session
12121210
):
@@ -1265,7 +1263,7 @@ def test_new_meshing_workflow_switching_without_dm_caching(
12651263
watertight.import_geometry.arguments()
12661264
assert fault_tolerant.import_cad_and_part_management.arguments()
12671265

1268-
watertight.reinitialize()
1266+
watertight = new_mesh_session.watertight()
12691267
with pytest.raises(RuntimeError):
12701268
fault_tolerant.import_cad_and_part_management.arguments()
12711269
assert watertight.import_geometry.arguments()

0 commit comments

Comments
 (0)