Skip to content

Commit

Permalink
collection get_entries_indices (#1640)
Browse files Browse the repository at this point in the history
* get_entries_indices

* Apply suggestions from code review
  • Loading branch information
cbellot000 authored Jun 28, 2024
1 parent a36f1f8 commit 05399b1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/ansys/dpf/core/collection_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
)
from typing import Optional, Generic, TypeVar

from ansys.dpf.gate.integral_types import MutableListInt32

TYPE = TypeVar('TYPE')


Expand Down Expand Up @@ -270,6 +272,33 @@ def _get_entries(self, label_space_or_index):
self._api.collection_get_obj_by_index(self, label_space_or_index)
)

@version_requires("9.0")
def get_entries_indices(self, label_space):
"""Retrieve the indices of the entries corresponding a requested label space .
Notes
-----
Available starting with DPF 2025R1.
Parameters
----------
label_space : dict[str,int]
Label space or index. For example,
``{"time": 1, "complex": 0}`` or the index of the field.
Returns
-------
indices : list[int], list[Field], list[MeshedRegion]
Indices of the entries corresponding to the request.
"""
client_label_space = LabelSpace(
label_space=label_space, obj=self, server=self._server
)
num = self._api.collection_get_num_obj_for_label_space(self, client_label_space)
int_list = MutableListInt32(num)
self._api.collection_fill_obj_indeces_for_label_space(self, client_label_space, int_list)
return int_list.tolist()

def _get_entry(self, label_space_or_index) -> TYPE:
"""Retrieve the entry at a requested label space or index.
Expand Down
11 changes: 11 additions & 0 deletions src/ansys/dpf/gate/collection_grpcapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from ansys.dpf.gate.generated import collection_abstract_api
from ansys.dpf.gate import object_handler, data_processing_grpcapi, grpc_stream_helpers, errors
from ansys.dpf.gate.integral_types import MutableListInt32


# -------------------------------------------------------------------------------
Expand Down Expand Up @@ -129,6 +130,16 @@ def _list(collection):
def collection_get_num_obj_for_label_space(collection, space):
return len(CollectionGRPCAPI._collection_get_entries(collection, space))

@staticmethod
def collection_fill_obj_indeces_for_label_space(collection, space, indices: MutableListInt32):
from ansys.grpc.dpf import collection_pb2
request = collection_pb2.EntryRequest()
request.collection.CopyFrom(collection._internal_obj)
request.label_space.CopyFrom(space._internal_obj)

out = _get_stub(collection._server).GetEntriesIndices(request)
indices.set(out.indices.rep_int)

@staticmethod
def collection_get_obj_by_index_for_label_space(collection, space, index):
return data_processing_grpcapi.DataProcessingGRPCAPI.data_processing_duplicate_object_reference(
Expand Down
12 changes: 12 additions & 0 deletions tests/test_fieldscontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,3 +560,15 @@ def test_fields_container_empty_tf_support(server_type):
fields_container = dpf.FieldsContainer(server=server_type)

assert fields_container.time_freq_support == None


@conftest.raises_for_servers_version_under("9.0")
def test_get_entries_indices_fields_container(server_type):
fc = FieldsContainer(server=server_type)
fc.labels = ["time", "complex"]
for i in range(0, 20):
mscop = {"time": i + 1, "complex": 0}
fc.add_field(mscop, Field(nentities=i + 10, server=server_type))
assert np.allclose(fc.get_entries_indices({"time": 1, "complex": 0}), [0])
assert np.allclose(fc.get_entries_indices({"time": 2}), [1])
assert np.allclose(fc.get_entries_indices({"complex": 0}), range(0, 20))

0 comments on commit 05399b1

Please sign in to comment.