Skip to content

Commit

Permalink
Add unused variables delete (#3803)
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuelopez-ansys authored Oct 26, 2023
1 parent c8a3ace commit 103bdf1
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
20 changes: 18 additions & 2 deletions _unittest/test_09_VariableManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,9 @@ def test_13_postprocessing(self):
def test_14_intrinsics(self):
self.aedtapp["fc"] = "Freq"
assert self.aedtapp["fc"] == "Freq"
assert self.aedtapp.variable_manager.dependent_variables["fc"].numeric_value == 1.0
assert self.aedtapp.variable_manager.dependent_variables["fc"].units == "GHz"
assert self.aedtapp.variable_manager.dependent_variables["fc"].units == self.aedtapp.odesktop.GetDefaultUnit(
"Frequency"
)

def test_15_arrays(self):
self.aedtapp["arr_index"] = 0
Expand Down Expand Up @@ -658,3 +659,18 @@ def test_31_test_validator_float_type_invalidate(self, validation_float_input):
validation_errors = generate_validation_errors(property_names, expected_settings, actual_settings)

assert len(validation_errors) == 1

def test_32_delete_unused_variables(self):
self.aedtapp.insert_design("used_variables")
self.aedtapp["used_var"] = "1mm"
self.aedtapp["unused_var"] = "1mm"
self.aedtapp["$project_used_var"] = "1"
self.aedtapp.modeler.create_rectangle(0, ["used_var", "used_var", "used_var"], [10, 20])
mat1 = self.aedtapp.materials.add_material("new_copper2")
mat1.permittivity = "$project_used_var"
assert self.aedtapp.variable_manager.delete_variable("unused_var")
self.aedtapp["unused_var"] = "1mm"
number_of_variables = len(self.aedtapp.variable_manager.variable_names)
assert self.aedtapp.variable_manager.delete_unused_variables()
new_number_of_variables = len(self.aedtapp.variable_manager.variable_names)
assert number_of_variables != new_number_of_variables
11 changes: 11 additions & 0 deletions pyaedt/application/Design.py
Original file line number Diff line number Diff line change
Expand Up @@ -3196,6 +3196,17 @@ def delete_variable(self, sVarName):
"""
return self.variable_manager.delete_variable(sVarName)

@pyaedt_function_handler()
def delete_unused_variables(self):
"""Delete design and project unused variables.
Returns
-------
bool
``True`` when successful, ``False`` when failed.
"""
return self.variable_manager.delete_unused_variables()

@pyaedt_function_handler()
def insert_design(self, design_name=None, solution_type=None):
"""Add a design of a specified type.
Expand Down
75 changes: 75 additions & 0 deletions pyaedt/application/Variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,81 @@ def delete_variable(self, var_name):
return True
return False

@pyaedt_function_handler()
def is_used_variable(self, var_name):
"""Find if a variable is used.
Parameters
----------
var_name : str
Name of the variable.
Returns
-------
bool
``True`` when successful, ``False`` when failed.
"""
used = False
# Modeler
for obj in self._app.modeler.objects.values():
used = self._find_used_variable_history(obj.history(), var_name)
if used:
self._logger.warning("{} used in modeler.".format(var_name))
return used

# Material
for mat in self._app.materials.material_keys.values():
for _, v in mat._props.items():
if isinstance(v, str) and var_name in re.findall("[$a-zA-Z0-9_]+", v):
used = True
self._logger.warning("{} used in the material: {}.".format(var_name, mat.name))
return used
return used

def _find_used_variable_history(self, history, var_name):
"""Find if a variable is used.
Parameters
----------
history : :class:`pyaedt.modeler.cad.elements3d.BinaryTree`
Object history.
var_name : str
Name of the variable.
Returns
-------
bool
``True`` when successful, ``False`` when failed.
"""
used = False
for _, v in history.props.items():
if isinstance(v, str) and var_name in re.findall("[a-zA-Z0-9_]+", v):
return True
for el in history.children.values():
used = self._find_used_variable_history(el, var_name)
if used:
return True
return used

@pyaedt_function_handler()
def delete_unused_variables(self):
"""Delete unused design and project variables.
Returns
-------
bool
``True`` when successful, ``False`` when failed.
"""
var_list = self.variable_names

for var in var_list[:]:
if not self.is_used_variable(var):
self.delete_variable(var)
return True

@pyaedt_function_handler()
def _get_var_list_from_aedt(self, desktop_object):
var_list = []
Expand Down

0 comments on commit 103bdf1

Please sign in to comment.