Skip to content

Commit 05399b1

Browse files
authored
collection get_entries_indices (#1640)
* get_entries_indices * Apply suggestions from code review
1 parent a36f1f8 commit 05399b1

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

src/ansys/dpf/core/collection_base.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
)
2727
from typing import Optional, Generic, TypeVar
2828

29+
from ansys.dpf.gate.integral_types import MutableListInt32
30+
2931
TYPE = TypeVar('TYPE')
3032

3133

@@ -270,6 +272,33 @@ def _get_entries(self, label_space_or_index):
270272
self._api.collection_get_obj_by_index(self, label_space_or_index)
271273
)
272274

275+
@version_requires("9.0")
276+
def get_entries_indices(self, label_space):
277+
"""Retrieve the indices of the entries corresponding a requested label space .
278+
279+
Notes
280+
-----
281+
Available starting with DPF 2025R1.
282+
283+
Parameters
284+
----------
285+
label_space : dict[str,int]
286+
Label space or index. For example,
287+
``{"time": 1, "complex": 0}`` or the index of the field.
288+
289+
Returns
290+
-------
291+
indices : list[int], list[Field], list[MeshedRegion]
292+
Indices of the entries corresponding to the request.
293+
"""
294+
client_label_space = LabelSpace(
295+
label_space=label_space, obj=self, server=self._server
296+
)
297+
num = self._api.collection_get_num_obj_for_label_space(self, client_label_space)
298+
int_list = MutableListInt32(num)
299+
self._api.collection_fill_obj_indeces_for_label_space(self, client_label_space, int_list)
300+
return int_list.tolist()
301+
273302
def _get_entry(self, label_space_or_index) -> TYPE:
274303
"""Retrieve the entry at a requested label space or index.
275304

src/ansys/dpf/gate/collection_grpcapi.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from ansys.dpf.gate.generated import collection_abstract_api
66
from ansys.dpf.gate import object_handler, data_processing_grpcapi, grpc_stream_helpers, errors
7+
from ansys.dpf.gate.integral_types import MutableListInt32
78

89

910
# -------------------------------------------------------------------------------
@@ -129,6 +130,16 @@ def _list(collection):
129130
def collection_get_num_obj_for_label_space(collection, space):
130131
return len(CollectionGRPCAPI._collection_get_entries(collection, space))
131132

133+
@staticmethod
134+
def collection_fill_obj_indeces_for_label_space(collection, space, indices: MutableListInt32):
135+
from ansys.grpc.dpf import collection_pb2
136+
request = collection_pb2.EntryRequest()
137+
request.collection.CopyFrom(collection._internal_obj)
138+
request.label_space.CopyFrom(space._internal_obj)
139+
140+
out = _get_stub(collection._server).GetEntriesIndices(request)
141+
indices.set(out.indices.rep_int)
142+
132143
@staticmethod
133144
def collection_get_obj_by_index_for_label_space(collection, space, index):
134145
return data_processing_grpcapi.DataProcessingGRPCAPI.data_processing_duplicate_object_reference(

tests/test_fieldscontainer.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,3 +560,15 @@ def test_fields_container_empty_tf_support(server_type):
560560
fields_container = dpf.FieldsContainer(server=server_type)
561561

562562
assert fields_container.time_freq_support == None
563+
564+
565+
@conftest.raises_for_servers_version_under("9.0")
566+
def test_get_entries_indices_fields_container(server_type):
567+
fc = FieldsContainer(server=server_type)
568+
fc.labels = ["time", "complex"]
569+
for i in range(0, 20):
570+
mscop = {"time": i + 1, "complex": 0}
571+
fc.add_field(mscop, Field(nentities=i + 10, server=server_type))
572+
assert np.allclose(fc.get_entries_indices({"time": 1, "complex": 0}), [0])
573+
assert np.allclose(fc.get_entries_indices({"time": 2}), [1])
574+
assert np.allclose(fc.get_entries_indices({"complex": 0}), range(0, 20))

0 commit comments

Comments
 (0)