From 08f0504ae4d32193c16d44df0a61cbca6af6c425 Mon Sep 17 00:00:00 2001 From: "paul.profizi" Date: Fri, 29 Mar 2024 17:29:11 +0100 Subject: [PATCH 1/5] Add FluidSimulation.zone_mesh() --- src/ansys/dpf/post/fluid_simulation.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/ansys/dpf/post/fluid_simulation.py b/src/ansys/dpf/post/fluid_simulation.py index a3466cc96..d30ad67b3 100644 --- a/src/ansys/dpf/post/fluid_simulation.py +++ b/src/ansys/dpf/post/fluid_simulation.py @@ -12,6 +12,7 @@ from ansys.dpf import core as dpf from ansys.dpf.post import locations from ansys.dpf.post.dataframe import DataFrame +from ansys.dpf.post.mesh import Mesh from ansys.dpf.post.mesh_info import FluidMeshInfo from ansys.dpf.post.phase import PhasesDict from ansys.dpf.post.selection import Selection @@ -177,6 +178,15 @@ def mesh_info(self) -> FluidMeshInfo: self._mesh_info = FluidMeshInfo(self._model.metadata.mesh_info) return self._mesh_info + def zone_mesh(self, zone_id: int) -> Mesh: + """Return the mesh of the given zone.""" + if zone_id not in self.cell_zones.keys(): + if zone_id not in self.face_zones.keys(): + raise ValueError(f"'{zone_id}' is not a valid zone ID.") + mesh_provider = self._model.metadata.mesh_provider + mesh_provider.inputs.region_scoping(zone_id) + return Mesh(mesh_provider.eval()) + @property def cell_zones(self) -> dict: """Return a dictionary of the cell zones in the simulation.""" From bc4c2fa83292fe4e935a09b5f07a532c7ceb66d4 Mon Sep 17 00:00:00 2001 From: "paul.profizi" Date: Fri, 29 Mar 2024 17:36:12 +0100 Subject: [PATCH 2/5] Add FluidSimulation.zone_mesh() test --- tests/test_fluid_simulation.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/test_fluid_simulation.py b/tests/test_fluid_simulation.py index 4a5e60c39..ceedb3ca8 100644 --- a/tests/test_fluid_simulation.py +++ b/tests/test_fluid_simulation.py @@ -473,3 +473,15 @@ def test_plot_result_on_zones(self, fluent_simulation): qualifiers={"zone": list(fluent_simulation.face_zones.keys())} ) temperature.plot() + + def test_fluid_simulation_zone_mesh(self, fluent_simulation): + # Cell zone mesh + cell_zone_mesh = fluent_simulation.zone_mesh(zone_id=13) + assert cell_zone_mesh.num_elements == 6080 + assert cell_zone_mesh.num_nodes == 7293 + assert cell_zone_mesh.num_faces == 19388 + # Face zone mesh + face_zone_mesh = fluent_simulation.zone_mesh(zone_id=4) + assert face_zone_mesh.num_elements == 0 + assert face_zone_mesh.num_nodes == 429 + assert face_zone_mesh.num_faces == 380 From 65ad0d01abc299fdd293fd702cd48cdb1652e561 Mon Sep 17 00:00:00 2001 From: "paul.profizi" Date: Fri, 29 Mar 2024 17:40:28 +0100 Subject: [PATCH 3/5] Update 00-explore-fluid-simulation.py --- .../00-explore-fluid-simulation.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/examples/04-Fluid-Examples/00-explore-fluid-simulation.py b/examples/04-Fluid-Examples/00-explore-fluid-simulation.py index c3f72c83e..626dd4082 100644 --- a/examples/04-Fluid-Examples/00-explore-fluid-simulation.py +++ b/examples/04-Fluid-Examples/00-explore-fluid-simulation.py @@ -49,6 +49,23 @@ # Check the available phases print(simulation.phases) +############################################################################### +# Extract the mesh +# ---------------- +############################################################################### +# Extract the full mesh +print(simulation.mesh) +simulation.mesh.plot() +############################################################################### +# Extract the mesh for a given zone +cell_zone_mesh = simulation.zone_mesh(zone_id=2) +print(cell_zone_mesh) +cell_zone_mesh.plot() + +face_zone_mesh = simulation.zone_mesh(zone_id=9) +print(face_zone_mesh) +face_zone_mesh.plot() + ############################################################################### # Extract a result From 9cb8fa4306457cc8380efab07ad22b85627f4761 Mon Sep 17 00:00:00 2001 From: "paul.profizi" Date: Fri, 3 May 2024 10:35:27 +0200 Subject: [PATCH 4/5] Add possibility to get zone mesh by name --- src/ansys/dpf/post/fluid_simulation.py | 25 +++++++++++++++++++------ tests/test_fluid_simulation.py | 21 +++++++++++++++++++-- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/ansys/dpf/post/fluid_simulation.py b/src/ansys/dpf/post/fluid_simulation.py index d30ad67b3..04d24d826 100644 --- a/src/ansys/dpf/post/fluid_simulation.py +++ b/src/ansys/dpf/post/fluid_simulation.py @@ -178,13 +178,26 @@ def mesh_info(self) -> FluidMeshInfo: self._mesh_info = FluidMeshInfo(self._model.metadata.mesh_info) return self._mesh_info - def zone_mesh(self, zone_id: int) -> Mesh: - """Return the mesh of the given zone.""" - if zone_id not in self.cell_zones.keys(): - if zone_id not in self.face_zones.keys(): - raise ValueError(f"'{zone_id}' is not a valid zone ID.") + def zone_mesh(self, zone: Union[int, str]) -> Mesh: + """Return the mesh of the given zone (ID or name).""" + if isinstance(zone, int): + if zone not in self.cell_zones.keys(): + if zone not in self.face_zones.keys(): + raise ValueError(f"'{zone}' is not a valid zone ID.") + elif isinstance(zone, str): + zone_id_found = None + for zone_id, zone_name in self.cell_zones.items(): + if zone == zone_name: + zone_id_found = zone_id + if zone_id_found is None: + for zone_id, zone_name in self.face_zones.items(): + if zone == zone_name: + zone_id_found = zone_id + if zone_id_found is None: + raise ValueError(f"'{zone}' is not a valid zone name.") + zone = int(zone_id_found) mesh_provider = self._model.metadata.mesh_provider - mesh_provider.inputs.region_scoping(zone_id) + mesh_provider.inputs.region_scoping(zone) return Mesh(mesh_provider.eval()) @property diff --git a/tests/test_fluid_simulation.py b/tests/test_fluid_simulation.py index ceedb3ca8..68c2114af 100644 --- a/tests/test_fluid_simulation.py +++ b/tests/test_fluid_simulation.py @@ -476,12 +476,29 @@ def test_plot_result_on_zones(self, fluent_simulation): def test_fluid_simulation_zone_mesh(self, fluent_simulation): # Cell zone mesh - cell_zone_mesh = fluent_simulation.zone_mesh(zone_id=13) + cell_zone_mesh = fluent_simulation.zone_mesh(zone=13) assert cell_zone_mesh.num_elements == 6080 assert cell_zone_mesh.num_nodes == 7293 assert cell_zone_mesh.num_faces == 19388 + # by name + cell_zone_mesh = fluent_simulation.zone_mesh(zone="fluid-rotor") + assert cell_zone_mesh.num_elements == 6080 + assert cell_zone_mesh.num_nodes == 7293 + assert cell_zone_mesh.num_faces == 19388 + # Face zone mesh - face_zone_mesh = fluent_simulation.zone_mesh(zone_id=4) + face_zone_mesh = fluent_simulation.zone_mesh(zone=4) assert face_zone_mesh.num_elements == 0 assert face_zone_mesh.num_nodes == 429 assert face_zone_mesh.num_faces == 380 + # by name + face_zone_mesh = fluent_simulation.zone_mesh(zone="rotor-shroud") + assert face_zone_mesh.num_elements == 0 + assert face_zone_mesh.num_nodes == 429 + assert face_zone_mesh.num_faces == 380 + + # raise + with pytest.raises(ValueError, match="is not a valid zone name"): + _ = fluent_simulation.zone_mesh(zone="test") + with pytest.raises(ValueError, match="is not a valid zone ID"): + _ = fluent_simulation.zone_mesh(zone=999) From d37062bd9d56d845f2a5e3da2ccc0ec87fefced4 Mon Sep 17 00:00:00 2001 From: "paul.profizi" Date: Fri, 3 May 2024 10:44:03 +0200 Subject: [PATCH 5/5] Update example --- .../04-Fluid-Examples/00-explore-fluid-simulation.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/examples/04-Fluid-Examples/00-explore-fluid-simulation.py b/examples/04-Fluid-Examples/00-explore-fluid-simulation.py index 626dd4082..086acebd7 100644 --- a/examples/04-Fluid-Examples/00-explore-fluid-simulation.py +++ b/examples/04-Fluid-Examples/00-explore-fluid-simulation.py @@ -57,14 +57,17 @@ print(simulation.mesh) simulation.mesh.plot() ############################################################################### -# Extract the mesh for a given zone -cell_zone_mesh = simulation.zone_mesh(zone_id=2) +# Extract the mesh for a given zone by its ID +cell_zone_mesh = simulation.zone_mesh(zone=2) print(cell_zone_mesh) cell_zone_mesh.plot() +# or by its name +cell_zone_mesh = simulation.zone_mesh(zone="heater") -face_zone_mesh = simulation.zone_mesh(zone_id=9) +face_zone_mesh = simulation.zone_mesh(zone=9) print(face_zone_mesh) face_zone_mesh.plot() +face_zone_mesh = simulation.zone_mesh(zone="outflow") ###############################################################################