Skip to content

Commit 9e31a5b

Browse files
authored
Any from and to vec[int] (#1635)
* any from and to vec int * revert test * Update generic_data_container.py
1 parent 03c64e9 commit 9e31a5b

File tree

6 files changed

+75
-31
lines changed

6 files changed

+75
-31
lines changed

src/ansys/dpf/core/any.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
import traceback
88
import warnings
99

10+
import numpy as np
11+
1012
import ansys.dpf.core.server_types
1113
from ansys.dpf.core import server as server_module
1214
from ansys.dpf.core import errors
13-
from ansys.dpf.core.check_version import server_meet_version
15+
from ansys.dpf.core.check_version import server_meet_version, server_meet_version_and_raise
1416
from ansys.dpf.core.common import create_dpf_instance
1517
from ansys.dpf.gate import any_abstract_api, integral_types
18+
from ansys.dpf.gate import dpf_vector
1619

1720

1821
class Any:
@@ -159,6 +162,11 @@ def _type_to_new_from_get_as_method(self):
159162
self._api.any_new_from_any_collection,
160163
self._api.any_get_as_any_collection,
161164
),
165+
(
166+
dpf_vector.DPFVectorInt,
167+
self._api.any_new_from_int_collection,
168+
self._api.any_get_as_int_collection,
169+
),
162170
]
163171

164172
@staticmethod
@@ -196,6 +204,16 @@ def new_from(obj, server=None):
196204
any_dpf._get_as_method = type_tuple[2]
197205

198206
return any_dpf
207+
if isinstance(obj, (list, np.ndarray)) and type_tuple[0]==dpf_vector.DPFVectorInt:
208+
from ansys.dpf.core import collection
209+
if server_meet_version_and_raise("9.0", inner_server, "Creating an Any from a list is only supported "
210+
"with"
211+
"server versions starting at 9.0"):
212+
inpt = collection.CollectionBase.integral_collection(obj, inner_server)
213+
any_dpf._internal_obj = type_tuple[1](inpt)
214+
any_dpf._internal_type = type_tuple[0]
215+
any_dpf._get_as_method = type_tuple[2]
216+
return any_dpf
199217

200218
raise TypeError(f"{obj.__class__} is not currently supported by the Any class.")
201219

src/ansys/dpf/core/common.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,20 @@ def __getitem__(self, item):
379379
})
380380

381381

382+
def type_to_special_dpf_constructors():
383+
from ansys.dpf.gate.dpf_vector import DPFVectorInt
384+
from ansys.dpf.core import collection_base
385+
return {DPFVectorInt:
386+
lambda obj, server: collection_base.IntCollection(
387+
server=server, collection=obj
388+
).get_integral_entries()
389+
}
390+
391+
382392
def create_dpf_instance(type, internal_obj, server):
393+
spe_constructors = type_to_special_dpf_constructors()
394+
if type in spe_constructors:
395+
return spe_constructors[type](internal_obj, server)
383396
# get current type's constructors' variable keyword for passing the internal_obj
384397
internal_obj_keyword = type_to_internal_object_keyword()[type]
385398

src/ansys/dpf/core/generic_data_container.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
import builtins
1111
from typing import Union, TYPE_CHECKING
1212

13+
import numpy as np
14+
1315
from ansys.dpf.core.check_version import server_meet_version
16+
from ansys.dpf.gate import dpf_vector
1417

1518
if TYPE_CHECKING: # pragma: no cover
1619
from ansys.dpf.core import Field, Scoping, StringField, GenericDataContainer
@@ -111,7 +114,7 @@ def set_property(
111114
Property object.
112115
"""
113116

114-
if not isinstance(prop, (int, float, str, bytes)) and server_meet_version("8.1", self._server):
117+
if not isinstance(prop, (int, float, str, bytes, list, np.ndarray)) and server_meet_version("8.1", self._server):
115118
self._api.generic_data_container_set_property_dpf_type(self, property_name, prop)
116119
else:
117120
any_dpf = Any.new_from(prop, self._server)
@@ -146,8 +149,10 @@ def get_property(self, property_name, output_type: Union[None, type, types] = No
146149
class_ = getattr(builtins, output_type, None)
147150
if class_ is None:
148151
from ansys.dpf import core
149-
150-
class_ = getattr(core, output_type)
152+
if hasattr(dpf_vector, output_type):
153+
class_ = getattr(dpf_vector, output_type)
154+
else:
155+
class_ = getattr(core, output_type)
151156

152157
return any_dpf.cast(class_)
153158

@@ -174,7 +179,11 @@ def get_property_description(self):
174179

175180
python_property_types = []
176181
for _, property_type in enumerate(property_types):
177-
python_property_types.append(map_types_to_python[property_type])
182+
if property_type == "vector<int32>":
183+
python_type = dpf_vector.DPFVectorInt.__name__
184+
else:
185+
python_type = map_types_to_python[property_type]
186+
python_property_types.append(python_type)
178187

179188
return dict(zip(property_names, python_property_types))
180189

src/ansys/dpf/gate/any_grpcapi.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def init_any_environment(object):
3131
@staticmethod
3232
def _type_to_message_type():
3333
from ansys.grpc.dpf import base_pb2
34+
from ansys.dpf.gate import dpf_vector
3435
from ansys.dpf.core import (
3536
field,
3637
property_field,
@@ -39,7 +40,7 @@ def _type_to_message_type():
3940
scoping,
4041
data_tree,
4142
custom_type_field,
42-
collection,
43+
collection_base,
4344
)
4445

4546
return [(int, base_pb2.Type.INT),
@@ -53,7 +54,8 @@ def _type_to_message_type():
5354
(generic_data_container.GenericDataContainer, base_pb2.Type.GENERIC_DATA_CONTAINER),
5455
(scoping.Scoping, base_pb2.Type.SCOPING),
5556
(data_tree.DataTree, base_pb2.Type.DATA_TREE),
56-
(collection.Collection, base_pb2.Type.COLLECTION, base_pb2.Type.ANY),
57+
(collection_base.CollectionBase, base_pb2.Type.COLLECTION, base_pb2.Type.ANY),
58+
(dpf_vector.DPFVectorInt, base_pb2.Type.COLLECTION, base_pb2.Type.INT),
5759
]
5860

5961
@staticmethod
@@ -133,6 +135,10 @@ def any_get_as_data_tree(any):
133135
def any_get_as_any_collection(any):
134136
return AnyGRPCAPI._get_as(any).collection
135137

138+
@staticmethod
139+
def any_get_as_int_collection(any):
140+
return AnyGRPCAPI._get_as(any).collection
141+
136142
@staticmethod
137143
def _new_from(any, client=None):
138144
from ansys.grpc.dpf import dpf_any_pb2
@@ -183,6 +189,10 @@ def any_new_from_string_with_size_on_client(client, any, size):
183189
def any_new_from_double_on_client(client, any):
184190
return AnyGRPCAPI._new_from(any, client)
185191

192+
@staticmethod
193+
def any_new_from_int_collection(any):
194+
return AnyGRPCAPI._new_from(any, any._server)
195+
186196
@staticmethod
187197
def any_new_from_field(any):
188198
return AnyGRPCAPI._new_from(any, any._server)

tests/test_generic_data_container.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1+
import numpy as np
2+
13
from ansys.dpf import core as dpf
24
from conftest import (
35
SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_7_0,
46
SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_8_0,
57
raises_for_servers_version_under,
68
)
79
import pytest
10+
import conftest
811

912

10-
@pytest.mark.skipif(
11-
not SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_7_0, reason="Available for servers >=7.0"
12-
)
13+
@conftest.raises_for_servers_version_under("7.0")
1314
def test_create_generic_data_container(server_type):
1415
gdc = dpf.GenericDataContainer(server=server_type)
1516
assert gdc._internal_obj is not None
1617

1718

18-
@pytest.mark.skipif(
19-
not SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_7_0, reason="Available for servers >=7.0"
20-
)
19+
@conftest.raises_for_servers_version_under("7.0")
2120
def test_set_get_property_generic_data_container(server_type):
2221
gdc = dpf.GenericDataContainer(server=server_type)
2322
entity = dpf.Field(location="phase", nature=dpf.natures.scalar, server=server_type)
@@ -26,9 +25,7 @@ def test_set_get_property_generic_data_container(server_type):
2625
assert entity.location == new_entity.location
2726

2827

29-
@pytest.mark.skipif(
30-
not SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_7_0, reason="Available for servers >=7.0"
31-
)
28+
@conftest.raises_for_servers_version_under("7.0")
3229
def test_set_get_data_tree_generic_data_container(server_type):
3330
gdc = dpf.GenericDataContainer(server=server_type)
3431
entity = dpf.DataTree(server=server_type)
@@ -38,9 +35,7 @@ def test_set_get_data_tree_generic_data_container(server_type):
3835
assert new_entity.get_as("name") == "john"
3936

4037

41-
@pytest.mark.skipif(
42-
not SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_7_0, reason="Available for servers >=7.0"
43-
)
38+
@conftest.raises_for_servers_version_under("7.0")
4439
def test_get_property_description_generic_data_container(server_type):
4540
gdc = dpf.GenericDataContainer(server=server_type)
4641
entity = 42
@@ -72,9 +67,7 @@ def test_get_property_description_generic_data_container(server_type):
7267
}
7368

7469

75-
@pytest.mark.skipif(
76-
not SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_7_0, reason="Available for servers >=7.0"
77-
)
70+
@conftest.raises_for_servers_version_under("7.0")
7871
def test_get_by_type_generic_data_container(server_type):
7972
gdc = dpf.GenericDataContainer(server=server_type)
8073
entity = 42
@@ -147,4 +140,13 @@ def test_set_collection_generic_data_container(server_type):
147140
gdc = dpf.GenericDataContainer(server=server_type)
148141
coll.labels = ["body", "time"]
149142
gdc.set_property("coll", coll)
150-
assert gdc.get_property("coll", dpf.GenericDataContainersCollection).labels == ["body", "time"]
143+
assert gdc.get_property("coll", dpf.GenericDataContainersCollection).labels == ["body", "time"]
144+
145+
146+
@raises_for_servers_version_under("9.0")
147+
def test_set_int_vec_generic_data_container(server_type):
148+
gdc = dpf.GenericDataContainer(server=server_type)
149+
gdc.set_property("vec", [1, 2, 3])
150+
gdc.set_property("nparray", np.array([1, 2, 3], dtype=np.int32))
151+
assert np.allclose(gdc.get_property("vec"), [1, 2, 3])
152+
assert np.allclose(gdc.get_property("nparray"), [1, 2, 3])

tests/test_operator.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -774,14 +774,6 @@ def test_connect_get_output_int_list_operator(server_type):
774774
assert np.allclose(d, d_out)
775775

776776

777-
@conftest.raises_for_servers_version_under("3.0")
778-
def test_connect_get_output_double_list_operator(server_type):
779-
d = list(np.ones(100000))
780-
op = dpf.core.operators.utility.forward(d, server=server_type)
781-
d_out = op.get_output(0, dpf.core.types.vec_double)
782-
assert np.allclose(d, d_out)
783-
784-
785777
@conftest.raises_for_servers_version_under("5.0")
786778
def test_connect_get_output_string_list_operator(server_clayer):
787779
d = ["hello", "bye"]

0 commit comments

Comments
 (0)