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 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
54 changes: 54 additions & 0 deletions src/ansys/dpf/core/field_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,60 @@ 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_type(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)
print(qt)
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):
"""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
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
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_type[0] == qt

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

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

# Testing the getter with an index out of range
with pytest.raises(Exception):
fieldDef.quantity_type[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