Skip to content

Commit c554151

Browse files
committed
Merged PR 293415: fixes
fixes Related work items: #295948
1 parent ecf30dc commit c554151

File tree

3 files changed

+78
-39
lines changed

3 files changed

+78
-39
lines changed

ansys/dpf/core/collection.py

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
66
"""
77
import numpy as np
8+
from typing import NamedTuple
9+
810
from ansys import dpf
911
from ansys.grpc.dpf import collection_pb2, collection_pb2_grpc
1012
from ansys.dpf.core.core import base_pb2
@@ -197,6 +199,26 @@ def _get_entries(self, label_space_or_index):
197199
entries : list[Scoping], list[Field], list[MeshedRegion]
198200
Entries corresponding to the request.
199201
"""
202+
entries = self._get_entries_tuple(label_space_or_index)
203+
if isinstance(entries, list):
204+
return [entry.entry for entry in entries]
205+
else:
206+
return entries
207+
208+
def _get_entries_tuple(self, label_space_or_index):
209+
"""Retrieve the entries at a requested label space or index.
210+
211+
Parameters
212+
----------
213+
label_space_or_index : dict[str,int]
214+
Label space or index. For example,
215+
``{"time": 1, "complex": 0}`` or the index of the field.
216+
217+
Returns
218+
-------
219+
entries : list[_CollectionEntry]
220+
Entries corresponding to the request.
221+
"""
200222
request = collection_pb2.EntryRequest()
201223
request.collection.CopyFrom(self._message)
202224

@@ -209,21 +231,34 @@ def _get_entries(self, label_space_or_index):
209231
out = self._stub.GetEntries(request)
210232
list_out = []
211233
for obj in out.entries:
234+
label_space = {}
235+
if obj.HasField("label_space"):
236+
for key in obj.label_space.label_space:
237+
label_space[key] = obj.label_space.label_space[key]
212238
if obj.HasField("dpf_type"):
213239
if self._type == types.scoping:
214240
unpacked_msg = scoping_pb2.Scoping()
215241
obj.dpf_type.Unpack(unpacked_msg)
216-
list_out.append(Scoping(scoping=unpacked_msg, server=self._server))
242+
list_out.append(
243+
_CollectionEntry(
244+
label_space=label_space,
245+
entry=Scoping(scoping=unpacked_msg, server=self._server)))
217246
elif self._type == types.field:
218247
unpacked_msg = field_pb2.Field()
219248
obj.dpf_type.Unpack(unpacked_msg)
220-
list_out.append(Field(field=unpacked_msg, server=self._server))
249+
list_out.append(
250+
_CollectionEntry(
251+
label_space=label_space,
252+
entry=Field(field=unpacked_msg, server=self._server)))
221253
elif self._type == types.meshed_region:
222254
unpacked_msg = meshed_region_pb2.MeshedRegion()
223255
obj.dpf_type.Unpack(unpacked_msg)
224256
list_out.append(
225-
MeshedRegion(mesh=unpacked_msg, server=self._server)
257+
_CollectionEntry(
258+
label_space=label_space,
259+
entry=MeshedRegion(mesh=unpacked_msg, server=self._server))
226260
)
261+
227262
if len(list_out) == 0:
228263
list_out = None
229264
return list_out
@@ -265,16 +300,8 @@ def get_label_space(self, index):
265300
Scoping of the requested entry. For example,
266301
``{"time": 1, "complex": 0}``.
267302
"""
268-
request = collection_pb2.EntryRequest()
269-
request.collection.CopyFrom(self._message)
270-
request.index = index
271-
out = self._stub.GetEntries(request).entries
272-
out = out[0].label_space.label_space
273-
dictOut = {}
274-
for key in out:
275-
dictOut[key] = out[key]
276-
277-
return dictOut
303+
entries = self._get_entries_tuple(index)
304+
return entries[0].label_space
278305

279306
def get_available_ids_for_label(self, label="time"):
280307
"""Retrieve the IDs assigned to an input label.
@@ -456,3 +483,7 @@ def __del__(self):
456483
def __iter__(self):
457484
for i in range(len(self)):
458485
yield self[i]
486+
487+
class _CollectionEntry(NamedTuple):
488+
label_space: dict
489+
entry: object

ansys/dpf/core/field.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ def __add__(self, field_b):
499499

500500
def __pow__(self, value):
501501
if value != 2:
502-
raise ValueError('Only the value "2" is suppported.')
502+
raise ValueError('Only the value "2" is supported.')
503503
from ansys.dpf.core import dpf_operator, operators
504504

505505
if hasattr(operators, "math") and hasattr(operators.math, "sqr"):

ansys/dpf/core/time_freq_support.py

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,6 @@ def __check_if_field_id(self, field):
346346
else:
347347
return field.id.id != 0
348348

349-
@protect_grpc
350349
def _get_frequencies(self, cplx=False):
351350
"""Retrieves a field of all the frequencies in the model
352351
(complex or real).
@@ -361,17 +360,13 @@ def _get_frequencies(self, cplx=False):
361360
field : dpf.core.Field
362361
Field of all the frequencies in the model (complex or real).
363362
"""
364-
request = time_freq_support_pb2.ListRequest()
365-
request.time_freq_support.CopyFrom(self._message)
366363

367-
list_response = self._stub.List(request)
368-
if cplx is True and self.__check_if_field_id(list_response.freq_complex):
369-
return dpf.core.Field(server=self._server, field=list_response.freq_complex)
370-
elif cplx is False and self.__check_if_field_id(list_response.freq_real):
371-
return dpf.core.Field(server=self._server, field=list_response.freq_real)
372-
return None
364+
attributes_list = self._get_attributes_list()
365+
if cplx and "freq_complex" in attributes_list:
366+
return attributes_list["freq_complex"]
367+
elif cplx!=True and "freq_real" in attributes_list:
368+
return attributes_list["freq_real"]
373369

374-
@protect_grpc
375370
def _get_rpms(self):
376371
"""Retrieves a field of all the RPMs in the model.
377372
@@ -380,15 +375,10 @@ def _get_rpms(self):
380375
field : dpf.core.Field
381376
Field of all the RPMs in the model (complex or real).
382377
"""
383-
request = time_freq_support_pb2.ListRequest()
384-
request.time_freq_support.CopyFrom(self._message)
378+
attributes_list = self._get_attributes_list()
379+
if "rpm" in attributes_list:
380+
return attributes_list["rpm"]
385381

386-
list_response = self._stub.List(request)
387-
if self.__check_if_field_id(list_response.rpm):
388-
return dpf.core.Field(server=self._server, field=list_response.rpm)
389-
return None
390-
391-
@protect_grpc
392382
def _get_harmonic_indices(self, stage_num=0):
393383
"""Retrieves a field of all the harmonic indices in the model.
394384
@@ -400,16 +390,34 @@ def _get_harmonic_indices(self, stage_num=0):
400390
stage_num: int, optional, default = 0
401391
Targeted stage number.
402392
"""
393+
attributes_list = self._get_attributes_list(stage_num)
394+
if "cyc_harmonic_index" in attributes_list:
395+
return attributes_list["cyc_harmonic_index"]
396+
397+
@protect_grpc
398+
def _get_attributes_list(self, stage_num=None):
403399
request = time_freq_support_pb2.ListRequest()
404400
request.time_freq_support.CopyFrom(self._message)
405-
request.cyclic_stage_num = stage_num
406-
401+
if stage_num:
402+
request.cyclic_stage_num = stage_num
407403
list_response = self._stub.List(request)
408-
if self.__check_if_field_id(list_response.cyc_harmonic_index):
409-
return dpf.core.Field(
410-
server=self._server, field=list_response.cyc_harmonic_index
411-
)
412-
return None
404+
out = {}
405+
if list_response.HasField("freq_real"):
406+
out["freq_real"] = dpf.core.Field(
407+
server=self._server, field=list_response.freq_real)
408+
if list_response.HasField("freq_complex"):
409+
out["freq_complex"] = dpf.core.Field(
410+
server=self._server, field=list_response.freq_complex)
411+
if list_response.HasField("rpm"):
412+
out["rpm"] = dpf.core.Field(
413+
server=self._server, field=list_response.rpm)
414+
if list_response.HasField("cyc_harmonic_index"):
415+
out["cyc_harmonic_index"] = dpf.core.Field(
416+
server=self._server, field=list_response.cyc_harmonic_index)
417+
if list_response.HasField("cyclic_harmonic_index_scoping"):
418+
out["cyclic_harmonic_index_scoping"] = dpf.core.Scoping(
419+
server=self._server, scoping=list_response.cyclic_harmonic_index_scoping)
420+
return out
413421

414422
def append_step(
415423
self,

0 commit comments

Comments
 (0)