Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FluidSimulation.zone_mesh() to get the mesh of a single zone #615

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions examples/04-Fluid-Examples/00-explore-fluid-simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@
# 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 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=9)
print(face_zone_mesh)
face_zone_mesh.plot()
face_zone_mesh = simulation.zone_mesh(zone="outflow")


###############################################################################
# Extract a result
Expand Down
23 changes: 23 additions & 0 deletions src/ansys/dpf/post/fluid_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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.result_workflows._component_helper import (
Expand Down Expand Up @@ -187,6 +188,28 @@
self._mesh_info = FluidMeshInfo(self._model.metadata.mesh_info)
return self._mesh_info

def zone_mesh(self, zone: Union[int, str]) -> Mesh:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PProfizi don't you think we should create a mesh api container here ? for extensibility of our APIs I think it can be useful. We could need to add other APIs like: zone_meshes(zones), merged_mesh_for_zones()...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've marked this as a global feature request for mesh manipulation in PyDPF-Post.
This one though was a specific request so I'll merge it on its own for now.
To me this method will still be there, just improved (for example allow to select several zones at once and get a single 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)
return Mesh(mesh_provider.eval())

Check warning on line 211 in src/ansys/dpf/post/fluid_simulation.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/post/fluid_simulation.py#L193-L211

Added lines #L193 - L211 were not covered by tests

@property
def cell_zones(self) -> dict:
"""Return a dictionary of the cell zones in the simulation."""
Expand Down
29 changes: 29 additions & 0 deletions tests/test_fluid_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,3 +473,32 @@ 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=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=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)
Loading