Skip to content

Commit

Permalink
feat: Avoid serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
mkundu1 committed Jan 17, 2025
1 parent 71513fd commit 6971c64
Showing 1 changed file with 44 additions and 31 deletions.
75 changes: 44 additions & 31 deletions src/ansys/fluent/core/services/field_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,20 @@ def get_solver_mesh_nodes(
responses = self._stub.GetSolverMeshNodesDouble(
request, metadata=self._metadata
)
nodes = []
nested_nodes = []
for response in responses:
nodes.append(response.nodes)
return [x for sublist in nodes for x in sublist]
nested_nodes.append(response.nodes)
return nested_nodes

def get_solver_mesh_elements(
self, request: FieldDataProtoModule.GetSolverMeshElementsRequest
):
"""GetSolverMeshElements RPC of FieldData service."""
responses = self._stub.GetSolverMeshElements(request, metadata=self._metadata)
elements = []
elementss = []
for response in responses:
elements.append(response.elements)
return [x for sublist in elements for x in sublist]
elementss.append(response.elements)
return elementss


class FieldInfo:
Expand Down Expand Up @@ -1479,46 +1479,59 @@ def get_mesh(self, zone: str | int) -> Mesh:
nodes_request = FieldDataProtoModule.GetSolverMeshNodesRequest(
domain_id=ROOT_DOMAIN_ID, thread_id=zone_info._id
)
nodes = self._service.get_solver_mesh_nodes(nodes_request)
nested_nodes = self._service.get_solver_mesh_nodes(nodes_request)
logger.info(f"Nodes data received in {time.time() - start_time} seconds")
logger.info(f"Getting elements for zone {zone_info._id}")
start_time = time.time()
elements_request = FieldDataProtoModule.GetSolverMeshElementsRequest(
domain_id=ROOT_DOMAIN_ID, thread_id=zone_info._id
)
elements_pb = self._service.get_solver_mesh_elements(elements_request)
elementss_pb = self._service.get_solver_mesh_elements(elements_request)
logger.info(f"Elements data received in {time.time() - start_time} seconds")
logger.info("Constructing nodes structure in PyFluent")
start_time = time.time()
nodes = [Node(_id=node.id, x=node.x, y=node.y, z=node.z) for node in nodes]
node_index_by_id = {node._id: index for index, node in enumerate(nodes)}
node_count = sum(len(nodes) for nodes in nested_nodes)
nodes = np.empty(node_count, dtype=Node)
node_index_by_id = {}
i = 0
for nodes_pb in nested_nodes:
for node_pb in nodes_pb:
nodes[i] = Node(_id=node_pb.id, x=node_pb.x, y=node_pb.y, z=node_pb.z)
node_index_by_id[node_pb.id] = i
i += 1
logger.info(
f"Nodes structure constructed in {time.time() - start_time} seconds"
)
logger.info("Constructing elements structure in PyFluent")
start_time = time.time()
elements = []
for element_pb in elements_pb:
element_type = CellElementType(element_pb.element_type)
if element_type == CellElementType.POLYHEDRON:
facets = []
for facet_pb in element_pb.facets:
facet = Facet(
node_indices=[node_index_by_id[id] for id in facet_pb.node]
element_count = sum(len(elements) for elements in elementss_pb)
elements = np.empty(element_count, dtype=Element)
i = 0
for elements_pb in elementss_pb:
for element_pb in elements_pb:
element_type = CellElementType(element_pb.element_type)
if element_type == CellElementType.POLYHEDRON:
facets = []
for facet_pb in element_pb.facets:
facet = Facet(
node_indices=[node_index_by_id[id] for id in facet_pb.node]
)
facets.append(facet)
element = Element(
_id=element_pb.id,
element_type=element_type,
facets=facets,
)
facets.append(facet)
element = Element(
_id=element_pb.id,
element_type=element_type,
facets=facets,
)
else:
element = Element(
_id=element_pb.id,
element_type=element_type,
node_indices=[node_index_by_id[id] for id in element_pb.node_ids],
)
elements.append(element)
else:
element = Element(
_id=element_pb.id,
element_type=element_type,
node_indices=[
node_index_by_id[id] for id in element_pb.node_ids
],
)
elements[i] = element
i += 1
logger.info(
f"Elements structure constructed in {time.time() - start_time} seconds"
)
Expand Down

0 comments on commit 6971c64

Please sign in to comment.