Skip to content

Commit

Permalink
Any from and to vec[int] (#1635)
Browse files Browse the repository at this point in the history
* any from and to vec int

* revert test

* Update generic_data_container.py
  • Loading branch information
cbellot000 authored Jun 26, 2024
1 parent 03c64e9 commit 9e31a5b
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 31 deletions.
20 changes: 19 additions & 1 deletion src/ansys/dpf/core/any.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
import traceback
import warnings

import numpy as np

import ansys.dpf.core.server_types
from ansys.dpf.core import server as server_module
from ansys.dpf.core import errors
from ansys.dpf.core.check_version import server_meet_version
from ansys.dpf.core.check_version import server_meet_version, server_meet_version_and_raise
from ansys.dpf.core.common import create_dpf_instance
from ansys.dpf.gate import any_abstract_api, integral_types
from ansys.dpf.gate import dpf_vector


class Any:
Expand Down Expand Up @@ -159,6 +162,11 @@ def _type_to_new_from_get_as_method(self):
self._api.any_new_from_any_collection,
self._api.any_get_as_any_collection,
),
(
dpf_vector.DPFVectorInt,
self._api.any_new_from_int_collection,
self._api.any_get_as_int_collection,
),
]

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

return any_dpf
if isinstance(obj, (list, np.ndarray)) and type_tuple[0]==dpf_vector.DPFVectorInt:
from ansys.dpf.core import collection
if server_meet_version_and_raise("9.0", inner_server, "Creating an Any from a list is only supported "
"with"
"server versions starting at 9.0"):
inpt = collection.CollectionBase.integral_collection(obj, inner_server)
any_dpf._internal_obj = type_tuple[1](inpt)
any_dpf._internal_type = type_tuple[0]
any_dpf._get_as_method = type_tuple[2]
return any_dpf

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

Expand Down
13 changes: 13 additions & 0 deletions src/ansys/dpf/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,20 @@ def __getitem__(self, item):
})


def type_to_special_dpf_constructors():
from ansys.dpf.gate.dpf_vector import DPFVectorInt
from ansys.dpf.core import collection_base
return {DPFVectorInt:
lambda obj, server: collection_base.IntCollection(
server=server, collection=obj
).get_integral_entries()
}


def create_dpf_instance(type, internal_obj, server):
spe_constructors = type_to_special_dpf_constructors()
if type in spe_constructors:
return spe_constructors[type](internal_obj, server)
# get current type's constructors' variable keyword for passing the internal_obj
internal_obj_keyword = type_to_internal_object_keyword()[type]

Expand Down
17 changes: 13 additions & 4 deletions src/ansys/dpf/core/generic_data_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
import builtins
from typing import Union, TYPE_CHECKING

import numpy as np

from ansys.dpf.core.check_version import server_meet_version
from ansys.dpf.gate import dpf_vector

if TYPE_CHECKING: # pragma: no cover
from ansys.dpf.core import Field, Scoping, StringField, GenericDataContainer
Expand Down Expand Up @@ -111,7 +114,7 @@ def set_property(
Property object.
"""

if not isinstance(prop, (int, float, str, bytes)) and server_meet_version("8.1", self._server):
if not isinstance(prop, (int, float, str, bytes, list, np.ndarray)) and server_meet_version("8.1", self._server):
self._api.generic_data_container_set_property_dpf_type(self, property_name, prop)
else:
any_dpf = Any.new_from(prop, self._server)
Expand Down Expand Up @@ -146,8 +149,10 @@ def get_property(self, property_name, output_type: Union[None, type, types] = No
class_ = getattr(builtins, output_type, None)
if class_ is None:
from ansys.dpf import core

class_ = getattr(core, output_type)
if hasattr(dpf_vector, output_type):
class_ = getattr(dpf_vector, output_type)
else:
class_ = getattr(core, output_type)

return any_dpf.cast(class_)

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

python_property_types = []
for _, property_type in enumerate(property_types):
python_property_types.append(map_types_to_python[property_type])
if property_type == "vector<int32>":
python_type = dpf_vector.DPFVectorInt.__name__
else:
python_type = map_types_to_python[property_type]
python_property_types.append(python_type)

return dict(zip(property_names, python_property_types))

Expand Down
14 changes: 12 additions & 2 deletions src/ansys/dpf/gate/any_grpcapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def init_any_environment(object):
@staticmethod
def _type_to_message_type():
from ansys.grpc.dpf import base_pb2
from ansys.dpf.gate import dpf_vector
from ansys.dpf.core import (
field,
property_field,
Expand All @@ -39,7 +40,7 @@ def _type_to_message_type():
scoping,
data_tree,
custom_type_field,
collection,
collection_base,
)

return [(int, base_pb2.Type.INT),
Expand All @@ -53,7 +54,8 @@ def _type_to_message_type():
(generic_data_container.GenericDataContainer, base_pb2.Type.GENERIC_DATA_CONTAINER),
(scoping.Scoping, base_pb2.Type.SCOPING),
(data_tree.DataTree, base_pb2.Type.DATA_TREE),
(collection.Collection, base_pb2.Type.COLLECTION, base_pb2.Type.ANY),
(collection_base.CollectionBase, base_pb2.Type.COLLECTION, base_pb2.Type.ANY),
(dpf_vector.DPFVectorInt, base_pb2.Type.COLLECTION, base_pb2.Type.INT),
]

@staticmethod
Expand Down Expand Up @@ -133,6 +135,10 @@ def any_get_as_data_tree(any):
def any_get_as_any_collection(any):
return AnyGRPCAPI._get_as(any).collection

@staticmethod
def any_get_as_int_collection(any):
return AnyGRPCAPI._get_as(any).collection

@staticmethod
def _new_from(any, client=None):
from ansys.grpc.dpf import dpf_any_pb2
Expand Down Expand Up @@ -183,6 +189,10 @@ def any_new_from_string_with_size_on_client(client, any, size):
def any_new_from_double_on_client(client, any):
return AnyGRPCAPI._new_from(any, client)

@staticmethod
def any_new_from_int_collection(any):
return AnyGRPCAPI._new_from(any, any._server)

@staticmethod
def any_new_from_field(any):
return AnyGRPCAPI._new_from(any, any._server)
Expand Down
34 changes: 18 additions & 16 deletions tests/test_generic_data_container.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import numpy as np

from ansys.dpf import core as dpf
from conftest import (
SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_7_0,
SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_8_0,
raises_for_servers_version_under,
)
import pytest
import conftest


@pytest.mark.skipif(
not SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_7_0, reason="Available for servers >=7.0"
)
@conftest.raises_for_servers_version_under("7.0")
def test_create_generic_data_container(server_type):
gdc = dpf.GenericDataContainer(server=server_type)
assert gdc._internal_obj is not None


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


@pytest.mark.skipif(
not SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_7_0, reason="Available for servers >=7.0"
)
@conftest.raises_for_servers_version_under("7.0")
def test_set_get_data_tree_generic_data_container(server_type):
gdc = dpf.GenericDataContainer(server=server_type)
entity = dpf.DataTree(server=server_type)
Expand All @@ -38,9 +35,7 @@ def test_set_get_data_tree_generic_data_container(server_type):
assert new_entity.get_as("name") == "john"


@pytest.mark.skipif(
not SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_7_0, reason="Available for servers >=7.0"
)
@conftest.raises_for_servers_version_under("7.0")
def test_get_property_description_generic_data_container(server_type):
gdc = dpf.GenericDataContainer(server=server_type)
entity = 42
Expand Down Expand Up @@ -72,9 +67,7 @@ def test_get_property_description_generic_data_container(server_type):
}


@pytest.mark.skipif(
not SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_7_0, reason="Available for servers >=7.0"
)
@conftest.raises_for_servers_version_under("7.0")
def test_get_by_type_generic_data_container(server_type):
gdc = dpf.GenericDataContainer(server=server_type)
entity = 42
Expand Down Expand Up @@ -147,4 +140,13 @@ def test_set_collection_generic_data_container(server_type):
gdc = dpf.GenericDataContainer(server=server_type)
coll.labels = ["body", "time"]
gdc.set_property("coll", coll)
assert gdc.get_property("coll", dpf.GenericDataContainersCollection).labels == ["body", "time"]
assert gdc.get_property("coll", dpf.GenericDataContainersCollection).labels == ["body", "time"]


@raises_for_servers_version_under("9.0")
def test_set_int_vec_generic_data_container(server_type):
gdc = dpf.GenericDataContainer(server=server_type)
gdc.set_property("vec", [1, 2, 3])
gdc.set_property("nparray", np.array([1, 2, 3], dtype=np.int32))
assert np.allclose(gdc.get_property("vec"), [1, 2, 3])
assert np.allclose(gdc.get_property("nparray"), [1, 2, 3])
8 changes: 0 additions & 8 deletions tests/test_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,14 +774,6 @@ def test_connect_get_output_int_list_operator(server_type):
assert np.allclose(d, d_out)


@conftest.raises_for_servers_version_under("3.0")
def test_connect_get_output_double_list_operator(server_type):
d = list(np.ones(100000))
op = dpf.core.operators.utility.forward(d, server=server_type)
d_out = op.get_output(0, dpf.core.types.vec_double)
assert np.allclose(d, d_out)


@conftest.raises_for_servers_version_under("5.0")
def test_connect_get_output_string_list_operator(server_clayer):
d = ["hello", "bye"]
Expand Down

0 comments on commit 9e31a5b

Please sign in to comment.