Skip to content

Commit

Permalink
refactor: Update field data output. (#3329)
Browse files Browse the repository at this point in the history
* refactor: Update field data output.

* Update tests.

* Fix import.

* Update.

* Refactor and update test.

* Update docs.

* Update test.

* Update faces_connectivity.

* Update File Session.

* Update file_session.

* Added deprecated field data interface.

* Add test.

* Update 'get_surface_data' interface.

* Refactor.

* Update docs.

* Delete old test.

* Update 'get_surface_data' interface.

* Revert reduction changes.

* Update pathlines output.

* Update docs.

* Update the deprecated class.

* Revert pathlines change.

* Update get surface data.
  • Loading branch information
prmukherj authored Oct 17, 2024
1 parent d8903dc commit 702436e
Show file tree
Hide file tree
Showing 10 changed files with 968 additions and 603 deletions.
101 changes: 45 additions & 56 deletions doc/source/user_guide/fields/field_data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,54 +43,47 @@ Here are the methods for requesting each type of field:

Get surface data
~~~~~~~~~~~~~~~~
You can request surface vertices for a given ``surface_name`` by calling
the ``get_surface_data`` method and specifying ``Vertices`` for ``data_type``.
You can request surface vertices for a given surface name by calling
the ``get_surface_data`` method and specifying ``Vertices`` for ``data_types``.

.. code-block:: python
>>> from ansys.fluent.core.services.field_data import SurfaceDataType
>>> vertices_data = field_data.get_surface_data(surface_name="cold-inlet", data_type=SurfaceDataType.Vertices)
>>> vertices_data.size
241
>>> vertices_data.surface_id
3
>>> vertices_data[5].x
-0.2
>>> vertices_data[5].y
-0.10167995
>>> vertices_data[5].z
0.0036200774
>>> vertices_data = field_data.get_surface_data(surfaces=["cold-inlet"], data_types=[SurfaceDataType.Vertices])
>>> vertices_data["cold-inlet"][SurfaceDataType.Vertices].shape
(241, 3)
>>> vertices_data["cold-inlet"][SurfaceDataType.Vertices][5]
array([-0.2 , -0.10167995, 0.00362008], dtype=float32)
You can call the same method to get the corresponding surface face normals and centroids.
For ``data_type``, specifying ``FacesNormal`` and ``FacesCentroid`` respectively.
For ``data_types``, specifying ``FacesNormal`` and ``FacesCentroid`` respectively.

.. code-block:: python
>>> faces_normal_data = field_data.get_surface_data(
>>> data_type=SurfaceDataType.FacesNormal, surface_name="cold-inlet"
>>> faces_normal_and_centroid_data = field_data.get_surface_data(
>>> data_types=[SurfaceDataType.FacesNormal, SurfaceDataType.FacesCentroid], surfaces=["cold-inlet"]
>>> )
>>> faces_centroid_data = field_data.get_surface_data(
>>> data_type=SurfaceDataType.FacesCentroid, surface_name="cold-inlet"
>>> )
>>> faces_normal_and_centroid_data["cold-inlet"][SurfaceDataType.FacesNormal].shape
(152, 3)
>>> faces_normal_and_centroid_data["cold-inlet"][SurfaceDataType.FacesCentroid][15]
array([-0.2 , -0.11418786, 0.03345207], dtype=float32)
You can request face connectivity data for a given ``surface_name`` by calling
the ``get_surface_data`` method and specifying ``FacesConnectivity`` for ``data_type``.
You can request face connectivity data for given ``surfaces`` by calling
the ``get_surface_data`` method and specifying ``FacesConnectivity`` for ``data_types``.

.. code-block:: python
>>> faces_connectivity_data = field_data.get_surface_data(
>>> data_type=SurfaceDataType.FacesConnectivity, surface_name="cold-inlet"
>>> data_types=[SurfaceDataType.FacesConnectivity], surfaces=["cold-inlet"]
>>> )
>>> faces_connectivity_data[5].node_count
4
>>> faces_connectivity_data[5].node_indices
[12, 13, 17, 16]
>>> faces_connectivity_data["cold-inlet"][SurfaceDataType.FacesConnectivity][5]
array([12, 13, 17, 16])
If a surface name is provided as input, the response contains face vertices, connectivity data, and normal or centroid data.
If surface IDs are provided as input, the response is a dictionary containing a map of surface IDs to face
If a single surface is provided as input, the response contains face vertices, connectivity data, and normal or centroid data.
If multiple surfaces are provided as input, the response is a dictionary containing a map of surface IDs to face
vertices, connectivity data, and normal or centroid data.

Get scalar field data
Expand All @@ -99,50 +92,46 @@ You can call the ``get_scalar_field_data`` method to get scalar field data, such

.. code-block:: python
>>> abs_press_data = field_data.get_scalar_field_data(field_name="absolute-pressure", surface_name="cold-inlet")
>>> abs_press_data.size
241
>>> abs_press_data[120].scalar_data
>>> abs_press_data = field_data.get_scalar_field_data(field_name="absolute-pressure", surfaces=["cold-inlet"])
>>> abs_press_data["cold-inlet"].shape
(241,)
>>> abs_press_data["cold-inlet"][120]
101325.0
If a surface name is provided as input, scalar field data is returned.
If surface IDs are provided as input, a dictionary containing a map of surface IDs to scalar field data is returned.
If a single surface is provided as input, scalar field data is returned.
If multiple surfaces are provided as input, a dictionary containing a map of surface IDs to scalar field data is returned.

Get vector field data
~~~~~~~~~~~~~~~~~~~~~
You can call the ``get_vector_field_data`` method to get vector field data.

.. code-block:: python
>>> velocity_vector_data = field_data.get_vector_field_data(field_name="velocity", surface_name="cold-inlet")
>>> velocity_vector_data.size
152
>>> velocity_vector_data.scale
1.0
>>> velocity_vector_data = field_data.get_vector_field_data(field_name="velocity", surfaces=["cold-inlet"])
>>> velocity_vector_data["cold-inlet"].shape
(152, 3)
If a surface name is provided as input, vector field data is returned.
If surface IDs are provided as input, a dictionary containing a map of surface IDs to vector field data is returned.
If a single surface is provided as input, vector field data is returned.
If multiple surfaces are provided as input, a dictionary containing a map of surface IDs to vector field data is returned.

Get pathlines field data
~~~~~~~~~~~~~~~~~~~~~~~~
You can call the ``get_pathlines_field_data`` method to get pathlines field data.

.. code-block:: python
>>> path_lines_data = field_data.get_pathlines_field_data(field_name="velocity", surface_name="cold-inlet")
>>> path_lines_data["vertices"].size
76152
>>> path_lines_data["lines"].size
>>> path_lines_data = field_data.get_pathlines_field_data(field_name="velocity", surfaces=["cold-inlet"])
>>> path_lines_data["cold-inlet"]["vertices"].shape
(76152, 3)
>>> len(path_lines_data["cold-inlet"]["lines"])
76000
>>> path_lines_data["velocity"].size
76152
>>> path_lines_data["lines"][100].node_count
2
>>> path_lines_data["lines"][100].node_indices
>>> path_lines_data["cold-inlet"]["velocity"].shape
(76152, )
>>> path_lines_data["cold-inlet"]["lines"][100]
array([100, 101])
Dictionary containing a map of surface IDs to the path-line data is returned.
or example, pathlines connectivity, vertices, and field.
For example, pathlines connectivity, vertices, and field.


.. note::
Expand Down Expand Up @@ -172,16 +161,16 @@ Following code demonstrate adding multiple requests to a single transaction.
.. code-block::
>>> transaction.add_surfaces_request(
>>> surface_ids=[1], provide_vertices=True, provide_faces=False, provide_faces_centroid=True
>>> surfaces=[1], data_types = [SurfaceDataType.Vertices, SurfaceDataType.FacesCentroid],
>>> )
>>> transaction.add_surfaces_request(
>>> surface_ids=[2], provide_vertices=True, provide_faces=True
>>> surfaces=[2], data_types = [SurfaceDataType.Vertices, SurfaceDataType.FacesConnectivity],
>>> )
>>> transaction.add_scalar_fields_request(
>>> surface_ids=[1,2], field_name="temperature", node_value=True, boundary_value=True
>>> surfaces=[1,2], field_name="pressure", node_value=True, boundary_value=True
>>> )
>>> transaction.add_vector_fields_request(surface_ids=[1,2], field_name="velocity")
>>> transaction.add_pathlines_fields_request(surface_ids=[1,2], field_name="temperature")
>>> transaction.add_vector_fields_request(surfaces=[1,2], field_name="velocity")
>>> transaction.add_pathlines_fields_request(surfaces=[1,2], field_name="temperature")
You can call the ``get_fields`` method to get the data for all these requests. This call also
Expand Down
186 changes: 55 additions & 131 deletions src/ansys/fluent/core/file_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@
from ansys.fluent.core import PyFluentDeprecationWarning
from ansys.fluent.core.filereader.case_file import CaseFile
from ansys.fluent.core.filereader.data_file import DataFile
from ansys.fluent.core.services.field_data import (
FacesConnectivity,
ScalarFieldData,
SurfaceDataType,
VectorFieldData,
Vertices,
)
from ansys.fluent.core.services.field_data import SurfaceDataType
from ansys.fluent.core.utils.deprecate import deprecate_argument, deprecate_arguments


Expand Down Expand Up @@ -411,48 +405,32 @@ def get_surface_data(
)

if SurfaceDataType.Vertices in data_types:
if len(surfaces) == 1 and isinstance(surfaces[0], str):
surface_ids = self._field_info.get_surfaces_info()[surfaces[0]][
"surface_id"
]
return Vertices(
surface_ids[0],
self._file_session._case_file.get_mesh().get_vertices(
surface_ids[0]
),
)
else:
return {
surface_id: Vertices(
surface_id,
self._file_session._case_file.get_mesh().get_vertices(
surface_id
),
)
for surface_id in surface_ids
}
return {
surface: self._file_session._case_file.get_mesh()
.get_vertices(surface_ids[count])
.reshape(-1, 3)
for count, surface in enumerate(surfaces)
}

if SurfaceDataType.FacesConnectivity in data_types:
if len(surfaces) == 1 and isinstance(surfaces[0], str):
surface_ids = self._field_info.get_surfaces_info()[surfaces[0]][
"surface_id"
]
return FacesConnectivity(
surface_ids[0],
return {
surface: self._get_faces_connectivity_data(
self._file_session._case_file.get_mesh().get_connectivity(
surface_ids[0]
),
)
else:
return {
surface_id: FacesConnectivity(
surface_id,
self._file_session._case_file.get_mesh().get_connectivity(
surface_id
),
surface_ids[count]
)
for surface_id in surface_ids
}
)
for count, surface in enumerate(surfaces)
}

@staticmethod
def _get_faces_connectivity_data(data):
faces_data = []
i = 0
while i < len(data):
end = i + 1 + data[i]
faces_data.append(data[i + 1 : end])
i = end
return faces_data

@deprecate_argument(
old_arg="surface_name",
Expand Down Expand Up @@ -504,53 +482,24 @@ def get_scalar_field_data(
field_info=self._field_info,
surfaces=surfaces,
)
if len(surfaces) == 1 and isinstance(surfaces[0], str):
surface_ids = self._field_info.get_surfaces_info()[surfaces[0]][
"surface_id"
]
if len(self._file_session._data_file.get_phases()) > 1:
if not field_name.startswith("phase-"):
raise InvalidMultiPhaseFieldName()
return ScalarFieldData(
surface_ids[0],
self._file_session._data_file.get_face_scalar_field_data(
field_name.split(":")[0],
field_name.split(":")[1],
surface_ids[0],
),
)
else:
return ScalarFieldData(
surface_ids[0],
self._file_session._data_file.get_face_scalar_field_data(
"phase-1", field_name, surface_ids[0]
),
if len(self._file_session._data_file.get_phases()) > 1:
if not field_name.startswith("phase-"):
raise InvalidMultiPhaseFieldName()
return {
surface: self._file_session._data_file.get_face_scalar_field_data(
field_name.split(":")[0],
field_name.split(":")[1],
surface_ids[count],
)
for count, surface in enumerate(surfaces)
}
else:
if len(self._file_session._data_file.get_phases()) > 1:
if not field_name.startswith("phase-"):
raise InvalidMultiPhaseFieldName()
return {
surface_id: ScalarFieldData(
surface_id,
self._file_session._data_file.get_face_scalar_field_data(
field_name.split(":")[0],
field_name.split(":")[1],
surface_id,
),
)
for surface_id in surface_ids
}
else:
return {
surface_id: ScalarFieldData(
surface_id,
self._file_session._data_file.get_face_scalar_field_data(
"phase-1", field_name, surface_id
),
)
for surface_id in surface_ids
}
return {
surface: self._file_session._data_file.get_face_scalar_field_data(
"phase-1", field_name, surface_ids[count]
)
for count, surface in enumerate(surfaces)
}

@deprecate_argument(
old_arg="surface_name",
Expand Down Expand Up @@ -602,47 +551,22 @@ def get_vector_field_data(
):
raise InvalidFieldName()

if len(surfaces) == 1 and isinstance(surfaces[0], str):
surface_ids = self._field_info.get_surfaces_info()[surfaces[0]][
"surface_id"
]
if len(self._file_session._data_file.get_phases()) > 1:
if not field_name.startswith("phase-"):
raise InvalidMultiPhaseFieldName()
vector_data = self._file_session._data_file.get_face_vector_field_data(
field_name.split(":")[0], surface_ids[0]
)
else:
vector_data = self._file_session._data_file.get_face_vector_field_data(
"phase-1", surface_ids[0]
)

return VectorFieldData(surface_ids[0], vector_data, scale=1.0)
if len(self._file_session._data_file.get_phases()) > 1:
if not field_name.startswith("phase-"):
raise InvalidMultiPhaseFieldName()
return {
surface: self._file_session._data_file.get_face_vector_field_data(
field_name.split(":")[0], surface_ids[count]
).reshape(-1, 3)
for count, surface in enumerate(surfaces)
}
else:
if len(self._file_session._data_file.get_phases()) > 1:
if not field_name.startswith("phase-"):
raise InvalidMultiPhaseFieldName()
return {
surface_id: VectorFieldData(
surface_id,
self._file_session._data_file.get_face_vector_field_data(
field_name.split(":")[0], surface_id
),
scale=1.0,
)
for surface_id in surface_ids
}
else:
return {
surface_id: VectorFieldData(
surface_id,
self._file_session._data_file.get_face_vector_field_data(
"phase-1", surface_id
),
scale=1.0,
)
for surface_id in surface_ids
}
return {
surface: self._file_session._data_file.get_face_vector_field_data(
"phase-1", surface_ids[count]
).reshape(-1, 3)
for count, surface in enumerate(surfaces)
}

@deprecate_argument(
old_arg="surface_name",
Expand Down Expand Up @@ -871,4 +795,4 @@ def _get_surface_ids(
surface_ids.extend(field_info.get_surfaces_info()[surf]["surface_id"])
else:
surface_ids.append(surf)
return list(set(surface_ids))
return surface_ids
Loading

0 comments on commit 702436e

Please sign in to comment.