Skip to content

Commit

Permalink
feat: Retrieve mesh data in solution mode (#3618)
Browse files Browse the repository at this point in the history
* feat: Retrieve mesh data in solution mode

* feat: Retrieve mesh data in solution mode

* build: update ansys-api-fluent version

* test: mesh data

* feat: unsort element data

* test: fix

* test: fix
  • Loading branch information
mkundu1 authored Jan 9, 2025
1 parent 9d0248d commit 6918389
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ classifiers = [
"Operating System :: OS Independent",
]
dependencies = [
"ansys-api-fluent>=0.3.30",
"ansys-api-fluent>=0.3.31",
"ansys-platform-instancemanagement~=1.0",
"ansys-tools-filetransfer>=0.1,<0.3",
"ansys-units>=0.3.3,<0.5",
Expand Down
150 changes: 150 additions & 0 deletions src/ansys/fluent/core/services/field_data.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Wrappers over FieldData gRPC service of Fluent."""

from dataclasses import dataclass
from enum import Enum
from functools import reduce
import io
from typing import Callable, Dict, List, Tuple

import grpc
Expand Down Expand Up @@ -82,6 +84,36 @@ def get_fields(self, request):
)
return chunk_iterator

def get_solver_mesh_nodes(
self, request: FieldDataProtoModule.GetSolverMeshNodesRequest
):
"""GetSolverMeshNodesDouble RPC of FieldData service."""
chuncked_responses = self._stub.GetSolverMeshNodesDouble(
request, metadata=self._metadata
)
buffer = io.BytesIO()
for chuncked_response in chuncked_responses:
buffer.write(chuncked_response.chunk)
serialized_response = buffer.getvalue()
response = FieldDataProtoModule.GetSolverMeshNodesDoubleResponse()
response.ParseFromString(serialized_response)
return response

def get_solver_mesh_elements(
self, request: FieldDataProtoModule.GetSolverMeshElementsRequest
):
"""GetSolverMeshElements RPC of FieldData service."""
chuncked_responses = self._stub.GetSolverMeshElements(
request, metadata=self._metadata
)
buffer = io.BytesIO()
for chuncked_response in chuncked_responses:
buffer.write(chuncked_response.chunk)
serialized_response = buffer.getvalue()
response = FieldDataProtoModule.GetSolverMeshElementsResponse()
response.ParseFromString(serialized_response)
return response


class FieldInfo:
"""Provides access to Fluent field information.
Expand Down Expand Up @@ -922,6 +954,89 @@ def _extract_field(field_datatype, field_size, chunk_iterator):
return fields_data


@dataclass
class Node:
"""Node class for mesh.
Attributes:
-----------
x : float
x-coordinate of the node.
y : float
y-coordinate of the node.
z : float
z-coordinate of the node.
"""

_id: int
x: float
y: float
z: float


class CellElementType(Enum):
"""Element types for a cell element."""

# 3 nodes, 3 faces
TRIANGLE = 1
# 4 nodes, 4 faces
TETRAHEDRON = 2
# 4 nodes, 4 faces
QUADRILATERAL = 3
# 8 nodes, 6 faces
HEXAHEDRON = 4
# 5 nodes, 5 faces
PYRAMID = 5
# 6 nodes, 5 faces
WEDGE = 6
# Arbitrary number of nodes and faces
POLYHEDRON = 7
# 2 nodes, 1 face (only in 2D)
GHOST = 8
# 10 nodes, 4 faces
QUADRATIC_TETRAHEDRON = 9
# 20 nodes, 6 faces
QUADRATIC_HEXAHEDRON = 10
# 13 nodes, 5 faces
QUADRATIC_PYRAMID = 11
# 15 nodes, 5 faces
QUADRATIC_WEDGE = 12


@dataclass
class Element:
"""Element class for mesh.
Attributes:
-----------
element_type : CellElementType
Element type of the element.
node_indices : list[int]
0-based node indices of the element.
"""

_id: int
element_type: CellElementType
node_indices: list[int]


@dataclass
class Mesh:
"""Mesh class for Fluent field data.
Attributes:
-----------
nodes : list[Node]
List of nodes in the mesh. Sorted by Fluent's node ID.
elements : list[Element]
List of elements in the mesh. Unsorted as we want to correlate with the
solution data retrieved via svar service.
"""

nodes: list[Node]
elements: list[Element]


class FieldData:
"""Provides access to Fluent field data on surfaces."""

Expand Down Expand Up @@ -1285,3 +1400,38 @@ def get_pathlines_field_data(
field_name: pathlines_data[surface_ids[count]][field_name],
}
return path_lines_dict

def get_mesh(self, zone_id: int) -> Mesh:
"""Get mesh for a zone.
Parameters
----------
zone_id : int
Zone ID.
Returns
-------
Mesh
Mesh object containing nodes and elements.
"""
request = FieldDataProtoModule.GetSolverMeshNodesRequest(
domain_id=1, thread_id=zone_id
)
response = self._service.get_solver_mesh_nodes(request)
nodes = response.nodes
nodes = [Node(_id=node.id, x=node.x, y=node.y, z=node.z) for node in nodes]
nodes = sorted(nodes, key=lambda x: x._id)
request = FieldDataProtoModule.GetSolverMeshElementsRequest(
domain_id=1, thread_id=zone_id
)
response = self._service.get_solver_mesh_elements(request)
elements = response.elements
elements = [
Element(
_id=element.id,
element_type=CellElementType(element.element_type),
node_indices=[(id - 1) for id in element.node_ids],
)
for element in elements
]
return Mesh(nodes=nodes, elements=elements)
23 changes: 22 additions & 1 deletion tests/test_field_data.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import numpy as np
import pytest
from test_utils import pytest_approx

from ansys.fluent.core import examples
from ansys.fluent.core.examples.downloads import download_file
from ansys.fluent.core.exceptions import DisallowedValuesError
from ansys.fluent.core.services.field_data import FieldUnavailable, SurfaceDataType
from ansys.fluent.core.services.field_data import (
CellElementType,
FieldUnavailable,
SurfaceDataType,
)

HOT_INLET_TEMPERATURE = 313.15

Expand Down Expand Up @@ -464,3 +469,19 @@ def plot_mesh(index, field_name, data):
assert len(mesh_data[5]["faces"]) == 80

assert list(mesh_data[12].keys()) == ["vertices", "faces"]


@pytest.mark.fluent_version(">=25.2")
def test_mesh_data(static_mixer_case_session):
solver = static_mixer_case_session
mesh = solver.fields.field_data.get_mesh(zone_id=97)
assert len(mesh.nodes) == 82247
assert len(mesh.elements) == 22771
assert mesh.elements[0].element_type == CellElementType.POLYHEDRON
assert len(mesh.elements[0].node_indices) > 0
assert min(mesh.nodes, key=lambda x: x.x).x == pytest_approx(-1.999075e-03)
assert max(mesh.nodes, key=lambda x: x.x).x == pytest_approx(1.999125e-03)
assert min(mesh.nodes, key=lambda x: x.y).y == pytest_approx(-3.000000e-03)
assert max(mesh.nodes, key=lambda x: x.y).y == pytest_approx(3.000000e-03)
assert min(mesh.nodes, key=lambda x: x.z).z == pytest_approx(-2.000000e-03)
assert max(mesh.nodes, key=lambda x: x.z).z == pytest_approx(2.500000e-03)

0 comments on commit 6918389

Please sign in to comment.