Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: exposing quantity types in PyDPF Core #1965

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
d1a7e88
feat: exposing quantity types in PyDPF Core
a-bouth Dec 12, 2024
0c06eab
feat: Update after PR Review
a-bouth Dec 13, 2024
adf46c5
feat: Update after PR Review (2)
a-bouth Dec 13, 2024
ba084f2
feat: Update after PR Review (3)
a-bouth Dec 13, 2024
bb05ddb
feat: Update after PR Review (4)
a-bouth Dec 13, 2024
8d009e9
Merge branch 'master' into feat/exposing_quantity_types
a-bouth Dec 16, 2024
dd7ed4f
feat: Added Fields Container to `any` cast
a-bouth Dec 18, 2024
799227f
Merge branch 'feat/exposing_quantity_types' of https://github.com/ans…
a-bouth Dec 18, 2024
2a1477d
feat: exposing quantity_type in the GRPC API
a-bouth Dec 18, 2024
418cb8d
Merge branch 'master' into feat/exposing_quantity_types
a-bouth Dec 18, 2024
0b18c4b
feat: using right name for quantity_types
a-bouth Dec 19, 2024
984d9dd
Merge branch 'feat/exposing_quantity_types' of https://github.com/ans…
a-bouth Dec 19, 2024
329f94a
feat: fixing exposure of quantity types
a-bouth Dec 19, 2024
58b007c
feat: Added gRPC methods for Any cast for FieldsContainer
a-bouth Dec 19, 2024
4e147e3
Merge branch 'master' into feat/exposing_quantity_types
a-bouth Dec 20, 2024
11e6e48
Merge branch 'master' into feat/exposing_quantity_types
a-bouth Jan 6, 2025
a9153eb
Merge branch 'master' into feat/exposing_quantity_types
a-bouth Jan 20, 2025
85c0130
feat: removed an useless comment
a-bouth Jan 22, 2025
856293d
Merge branch 'master' into feat/exposing_quantity_types
a-bouth Jan 22, 2025
a6749ac
feat: fixed style check
a-bouth Jan 22, 2025
8dc0755
Merge branch 'feat/exposing_quantity_types' of https://github.com/ans…
a-bouth Jan 22, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/ansys/dpf/core/any.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
def _type_to_new_from_get_as_method(self, obj):
from ansys.dpf.core import (
field,
fields_container,
property_field,
generic_data_container,
string_field,
Expand Down Expand Up @@ -156,6 +157,11 @@
self._api.any_new_from_property_field,
self._api.any_get_as_property_field,
)
elif issubclass(obj, fields_container.FieldsContainer):
return (

Check warning on line 161 in src/ansys/dpf/core/any.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/any.py#L161

Added line #L161 was not covered by tests
self._api.any_new_from_fields_container,
self._api.any_get_as_fields_container,
)
elif issubclass(obj, string_field.StringField):
return (
self._api.any_new_from_string_field,
Expand Down
53 changes: 53 additions & 0 deletions src/ansys/dpf/core/field_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,59 @@ def dimensionality(self):
self._api.csfield_definition_fill_dimensionality(self, dim, nature, dim.internal_size)
return Dimensionality(dim.tolist(), natures(int(nature)))

@property
def quantity_types(self):
"""Getter for Quantity Types.

Returns
-------
str
All quantity types of the elementary data for this FieldDefinition.
"""
quantity_types = []
for i in range(self.num_quantity_types()):
qt = self._api.csfield_definition_get_quantity_type(self, i)
quantity_types.append(str(qt))

return quantity_types

def add_quantity_type(self, quantity_type_to_add):
"""Add a new Quantity Type.

Parameters
----------
quantity_type_to_add: str
Quantity type to add
"""
self._api.csfield_definition_set_quantity_type(self, quantity_type_to_add)

def num_quantity_types(self):
"""Return number of available quantity types.

Returns
-------
num_quantity_types : int
Number of quantity types
"""
num_quantity_types = self._api.csfield_definition_get_num_available_quantity_types(self)
return num_quantity_types

def is_of_quantity_type(self, quantity_type):
"""Check if the field definition is of a given quantity type.

Parameters
----------
quantity_type: str
Quantity type to check

Returns
-------
is_of_quantity_type : bool
True if the field definition is of the given quantity type
"""
is_of_quantity_type = self._api.csfield_definition_is_of_quantity_type(self, quantity_type)
return is_of_quantity_type

@unit.setter
def unit(self, value):
self._api.csfield_definition_set_unit(self, value, None, 0, 0, 0)
Expand Down
10 changes: 10 additions & 0 deletions src/ansys/dpf/gate/any_grpcapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def _type_to_message_type():
from ansys.dpf.gate import dpf_vector
from ansys.dpf.core import (
field,
fields_container,
property_field,
generic_data_container,
string_field,
Expand All @@ -50,6 +51,7 @@ def _type_to_message_type():
(float, base_pb2.Type.DOUBLE),
(bytes, base_pb2.Type.STRING),
(field.Field, base_pb2.Type.FIELD),
(fields_container.FieldsContainer, base_pb2.Type.COLLECTION, base_pb2.Type.FIELD),
(property_field.PropertyField, base_pb2.Type.PROPERTY_FIELD),
(string_field.StringField, base_pb2.Type.STRING_FIELD),
(custom_type_field.CustomTypeField, base_pb2.Type.CUSTOM_TYPE_FIELD),
Expand Down Expand Up @@ -114,6 +116,10 @@ def any_get_as_field(any):
@staticmethod
def any_get_as_property_field(any):
return AnyGRPCAPI._get_as(any).field

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

@staticmethod
def any_get_as_string_field(any):
Expand Down Expand Up @@ -213,6 +219,10 @@ def any_new_from_field(any):
def any_new_from_property_field(any):
return AnyGRPCAPI._new_from(any, any._server)

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

@staticmethod
def any_new_from_string_field(any):
return AnyGRPCAPI._new_from(any, any._server)
Expand Down
23 changes: 21 additions & 2 deletions src/ansys/dpf/gate/field_definition_grpcapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,27 @@ def init_field_definition_environment(object):
def csfield_definition_fill_unit(fieldDef, symbol, size, homogeneity, factor, shift):
symbol.set_str(_get_stub(fieldDef._server).List(fieldDef._internal_obj).unit.symbol)


@staticmethod
def csfield_definition_get_quantity_type(fieldDef, index):
return _get_stub(fieldDef._server).List(fieldDef._internal_obj).quantity_types.quantity_types[index]

@staticmethod
def csfield_definition_set_quantity_type(fieldDef, quantityType):
FieldDefinitionGRPCAPI._modify_field_def(fieldDef, quantity_type=quantityType)

@staticmethod
def csfield_definition_get_num_available_quantity_types(fieldDef):
return len(_get_stub(fieldDef._server).List(fieldDef._internal_obj).quantity_types.quantity_types)

@staticmethod
def csfield_definition_is_of_quantity_type(fieldDef, quantityType):
return quantityType in _get_stub(fieldDef._server).List(fieldDef._internal_obj).quantity_types.quantity_types

@staticmethod
def csfield_definition_get_shell_layers(fieldDef):
return _get_stub(fieldDef._server).List(fieldDef._internal_obj).shell_layers - 1

@staticmethod
def csfield_definition_fill_location(fieldDef, location, size):
out = _get_stub(fieldDef._server).List(fieldDef._internal_obj)
Expand Down Expand Up @@ -73,7 +90,7 @@ def field_definition_new_on_client(client):

@staticmethod
def _modify_field_def(
fieldDef, unit=None, location=None, dimensionality=None, shell_layer=None, name=None
fieldDef, unit=None, location=None, dimensionality=None, shell_layer=None, name=None, quantity_type=None
):
from ansys.grpc.dpf import field_definition_pb2
request = field_definition_pb2.FieldDefinitionUpdateRequest()
Expand All @@ -89,5 +106,7 @@ def _modify_field_def(
request.shell_layers = shell_layer
if name != None:
request.name.string = name
if quantity_type != None:
request.quantity_types.quantity_types.append(quantity_type)

_get_stub(fieldDef._server).Update(request)
3 changes: 1 addition & 2 deletions src/ansys/dpf/gate/generated/field_definition_capi.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,5 +351,4 @@ def dimensionality_get_num_comp_for_object(api_to_use, nature, size, vsize):
res = capi.dll.Dimensionality_GetNumComp_for_object(api_to_use._internal_obj if api_to_use is not None else None, utils.to_int32(nature), utils.to_int32_ptr(size), utils.to_int32(vsize), ctypes.byref(utils.to_int32(errorSize)), ctypes.byref(sError))
if errorSize.value != 0:
raise errors.DPFServerException(sError.value)
return res

return res
12 changes: 12 additions & 0 deletions tests/test_any.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,15 @@ def test_cast_operator_any(server_type):
new_entity = any_dpf.cast()

assert entity.name == new_entity.name


@pytest.mark.skipif(
not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_10_0,
reason="any does not support operator below 10.0",
)
def test_cast_fields_container_any(server_type):
entity = dpf.FieldsContainer(server=server_type)
any_dpf = dpf.Any.new_from(entity)
new_entity = any_dpf.cast()

assert entity.name == new_entity.name
28 changes: 28 additions & 0 deletions tests/test_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,34 @@ def test_create_and_update_field_definition(server_type):
assert fieldDef.location == locations.nodal


def test_field_definition_quantity_type(server_type):
fieldDef = FieldDefinition(server=server_type)

# Testing the setter
qt = "my_quantity_type"
fieldDef.add_quantity_type(qt)

# Testing the getter
assert fieldDef.quantity_types[0] == qt

# Adding a second quantity type
qt2 = "another_quantity_type"
fieldDef.add_quantity_type(qt2)

# Testing the getter again
assert fieldDef.quantity_types[1] == qt2

# Testing the getter with an index out of range
with pytest.raises(Exception):
fieldDef.quantity_types[2]

# Getting the number of available quantity types
assert fieldDef.num_quantity_types() == 2

# Checking if the field definition is of a given quantity type
assert fieldDef.is_of_quantity_type(qt)


@conftest.raises_for_servers_version_under("4.0")
def test_create_and_set_get_name_field_definition(server_type):
fieldDef = FieldDefinition(server=server_type)
Expand Down
Loading