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

MeshInfo.parts helper #1374

Merged
merged 4 commits into from
Jan 25, 2024
Merged
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
16 changes: 12 additions & 4 deletions src/ansys/dpf/core/generic_data_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
GenericDataContainer
====================
"""
from __future__ import annotations
import traceback
import warnings
import builtins
from typing import Union, TYPE_CHECKING
if TYPE_CHECKING: # pragma: no cover
from ansys.dpf.core import Field, Scoping, StringField, GenericDataContainer

from ansys.dpf.core import server as server_module
from ansys.dpf.core import errors
Expand Down Expand Up @@ -82,15 +86,19 @@ def __str__(self):

return _description(self._internal_obj, self._server)

def set_property(self, property_name, prop):
def set_property(
self,
property_name: str,
prop: Union[int, float, str, Field, StringField, GenericDataContainer, Scoping]
):
"""Register given property with the given name.

Parameters
----------
property_name : str
property_name:
Property name.
prop : Int, String, Float, Field, StringField, GenericDataContainer, Scoping
object instance.
prop:
Property object.
"""

any_dpf = Any.new_from(prop, self._server)
Expand Down
24 changes: 24 additions & 0 deletions src/ansys/dpf/core/mesh_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
raise ValueError(
"Arguments generic_data_container and mesh_info are mutually exclusive."
)
self._part_map = None
self._zone_map = None
self._cell_zone_map = None
self._face_zone_map = None
Expand Down Expand Up @@ -225,6 +226,29 @@
else:
return None

@property
def parts(self) -> dict:
"""Dictionary of available part IDs to part names.

Returns
-------
parts:
Map of part IDs to part names.

.. warning:
Currently unavailable for LegacyGrpc servers.
"""
if self._part_map:
return self._part_map

Check warning on line 242 in src/ansys/dpf/core/mesh_info.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/mesh_info.py#L242

Added line #L242 was not covered by tests
part_names = self.part_names
part_map = {}
if part_names:
names = part_names.data
for i, key in enumerate(part_names.scoping.ids):
part_map[str(key)] = names[i]
self._part_map = part_map
return self._part_map

@property
def part_scoping(self):
"""
Expand Down
3 changes: 2 additions & 1 deletion src/ansys/dpf/core/string_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
dpf_vector,
integral_types,
)
from typing import List


class StringField(_FieldBase):
Expand Down Expand Up @@ -198,7 +199,7 @@ def get_entity_data_by_id(self, id):
data = self.get_entity_data(index)
return data

def append(self, data, scopingid):
def append(self, data: List[str], scopingid: int):
string_list = integral_types.MutableListString(data)
self._api.csstring_field_push_back(self, scopingid, _get_size_of_list(data), string_list)

Expand Down
18 changes: 18 additions & 0 deletions tests/test_mesh_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,21 @@ def test_mesh_info_zones(fluent_multi_species, server_clayer):
'7': 'velocity-inlet-7'
}
assert mesh_info.face_zones == ref_face_zones


@pytest.mark.skipif(
not SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_7_0, reason="Available for servers >=7.0"
)
def test_mesh_info_parts(server_type):
parts = ["part_1", "part_2"]
part_ids = list(range(1, len(parts)+1))
part_names = dpf.StringField(nentities=len(part_ids))
for part_id in part_ids:
part_names.append(data=[parts[part_id-1]], scopingid=part_id)
part_scoping = dpf.Scoping(location="part", ids=part_ids)
gdc = dpf.GenericDataContainer()
gdc.set_property(property_name="part_names", prop=part_names)
gdc.set_property(property_name="part_scoping", prop=part_scoping)
mesh_info = dpf.MeshInfo(generic_data_container=gdc, server=server_type)
ref = """{'1': 'part_1', '2': 'part_2'}"""
assert str(mesh_info.parts) == ref