diff --git a/src/ansys/fluent/core/solver/flobject.py b/src/ansys/fluent/core/solver/flobject.py index 7a22b6680a29..f8eddb3faa36 100644 --- a/src/ansys/fluent/core/solver/flobject.py +++ b/src/ansys/fluent/core/solver/flobject.py @@ -16,6 +16,7 @@ r.boundary_conditions.velocity_inlet['inlet'].vmag.constant = 20 """ import collections +import fnmatch import hashlib import importlib import keyword @@ -1214,3 +1215,41 @@ def get_root(flproxy, version: str = "") -> Group: root.set_flproxy(flproxy) root._setattr("_static_info", obj_info) return root + + +def find_children(obj, identifier="*"): + """Returns path of all the child objects matching an identifier. + + Parameters + ---------- + obj: Object + Object whose children need to be queried. + identifier: str + Identifier to find specific children. + + Returns + ------- + List + """ + list_of_children = [] + _list_children(obj.__class__, identifier, [], list_of_children) + return list_of_children + + +def _list_children(cls, identifier, path, list_of_children): + if issubclass(cls, (NamedObject, ListObject)): + if hasattr(cls.child_object_type, "child_names"): + _get_child_path(cls.child_object_type, path, identifier, list_of_children) + if issubclass(cls, Group): + _get_child_path(cls, path, identifier, list_of_children) + + +def _get_child_path(cls, path, identifier, list_of_children): + for name in cls.child_names: + path.append(name) + if fnmatch.fnmatch(name, identifier): + path_to_append = "/".join(path) + if path_to_append not in list_of_children: + list_of_children.append(path_to_append) + _list_children(getattr(cls, name), identifier, path, list_of_children) + path.pop() diff --git a/tests/test_flobject.py b/tests/test_flobject.py index 55f7e65f5ed3..aadd6cdc4ec6 100644 --- a/tests/test_flobject.py +++ b/tests/test_flobject.py @@ -2,6 +2,7 @@ # import codegen.settingsgen from collections.abc import MutableMapping import io +import os import weakref import pytest @@ -9,6 +10,9 @@ from ansys.fluent.core.examples import download_file from ansys.fluent.core.solver import flobject +from ansys.fluent.core.solver.flobject import find_children + +os.environ["PYFLUENT_FLUENT_ROOT"] = r"C:\ANSYSDev\ANSYSDev\vNNN\fluent" class Setting: @@ -727,6 +731,67 @@ def test_accessor_methods_on_settings_object_types(load_static_mixer_case): @pytest.mark.dev +@pytest.mark.fluent_231 +def test_find_children_from_settings_root(): + from ansys.fluent.core.solver.settings_231.setup import setup + + assert len(find_children(setup())) == 18514 + assert len(find_children(setup(), "gen*")) == 9 + assert find_children(setup(), "general*") == [ + "general", + "models/discrete_phase/general_settings", + "models/virtual_blade_model/disk/general", + ] + assert find_children(setup(), "general") == [ + "general", + "models/virtual_blade_model/disk/general", + ] + assert find_children(setup(), "*gen") == [ + "boundary_conditions/exhaust_fan/phase/p_backflow_spec_gen", + "boundary_conditions/exhaust_fan/p_backflow_spec_gen", + "boundary_conditions/outlet_vent/phase/p_backflow_spec_gen", + "boundary_conditions/outlet_vent/p_backflow_spec_gen", + "boundary_conditions/pressure_outlet/phase/p_backflow_spec_gen", + "boundary_conditions/pressure_outlet/p_backflow_spec_gen", + ] + + +@pytest.mark.dev +@pytest.mark.fluent_231 +def test_find_children_from_fluent_solver_session(load_static_mixer_case): + setup_children = find_children(load_static_mixer_case.setup) + + assert len(setup_children) == 18514 + + viscous = load_static_mixer_case.setup.models.viscous + assert find_children(viscous, "prod*") == [ + "options/production_kato_launder", + "turbulence_expert/production_limiter", + ] + + assert find_children( + load_static_mixer_case.setup.boundary_conditions.pressure_outlet, "*_dir_*" + ) == [ + "phase/geom_dir_spec", + "phase/geom_dir_x", + "phase/geom_dir_y", + "phase/geom_dir_z", + "geom_dir_spec", + "geom_dir_x", + "geom_dir_y", + "geom_dir_z", + ] + + assert find_children( + load_static_mixer_case.setup.materials.fluid["air"].density.piecewise_polynomial + ) == [ + "minimum", + "maximum", + "number_of_coefficients", + "coefficients", + ] + + @pytest.mark.fluent_232 def test_settings_matching_names(new_solver_session_no_transcript) -> None: solver = new_solver_session_no_transcript