Skip to content

Commit

Permalink
feat: exposing quantity types in PyDPF Core
Browse files Browse the repository at this point in the history
  • Loading branch information
a-bouth committed Dec 12, 2024
1 parent 60140b1 commit d1a7e88
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
55 changes: 55 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,61 @@ def dimensionality(self):
self._api.csfield_definition_fill_dimensionality(self, dim, nature, dim.internal_size)
return Dimensionality(dim.tolist(), natures(int(nature)))

def get_quantity_type(self, index=0):
"""Getter for Quantity Type
Parameters
----------
index: Index of the quantity type to return
Returns
-------
quantity_type : Quantity Type
Quantity type of the elementary data at a given index.
"""
if index < 0:
raise ValueError("Index must be greater than or equal to 0")

Check warning on line 169 in src/ansys/dpf/core/field_definition.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/field_definition.py#L168-L169

Added lines #L168 - L169 were not covered by tests

quantity_type = self._api.csfield_definition_get_quantity_type(self, index)
return str(quantity_type)

Check warning on line 172 in src/ansys/dpf/core/field_definition.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/field_definition.py#L171-L172

Added lines #L171 - L172 were not covered by tests

def set_quantity_type(self, quantity_type):
"""Setter for Quantity Type
Parameters
----------
quantity_type: Quantity Type
Quantity type to set
"""
self._api.csfield_definition_set_quantity_type(self, quantity_type)

Check warning on line 182 in src/ansys/dpf/core/field_definition.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/field_definition.py#L182

Added line #L182 was not covered by tests

def get_num_available_quantity_types(self):
"""Getter for the 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

Check warning on line 193 in src/ansys/dpf/core/field_definition.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/field_definition.py#L192-L193

Added lines #L192 - L193 were not covered by tests

def is_of_quantity_type(self, quantity_type):
"""Check if the field definition is of a given quantity type
Parameters
----------
quantity_type: Quantity Type
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

Check warning on line 209 in src/ansys/dpf/core/field_definition.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/field_definition.py#L208-L209

Added lines #L208 - L209 were not covered by tests

@unit.setter
def unit(self, value):
self._api.csfield_definition_set_unit(self, value, None, 0, 0, 0)
Expand Down
38 changes: 38 additions & 0 deletions src/ansys/dpf/gate/generated/field_definition_capi.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,35 @@ def csfield_definition_get_shell_layers(res):
if errorSize.value != 0:
raise errors.DPFServerException(sError.value)
return res

@staticmethod
def csfield_definition_get_quantity_type(res, index):
errorSize = ctypes.c_int(0)
sError = ctypes.c_wchar_p()
res = capi.dll.CSFieldDefinition_GetQuantityType(res._internal_obj if res is not None else None, index, ctypes.byref(utils.to_int32(errorSize)), ctypes.byref(sError))
if errorSize.value != 0:
raise errors.DPFServerException(sError.value)
newres = ctypes.cast(res, ctypes.c_char_p).value.decode("utf-8") if res else None
capi.dll.DataProcessing_String_post_event(res, ctypes.byref(errorSize), ctypes.byref(sError))
return newres

@staticmethod
def csfield_definition_get_num_available_quantity_types(fieldDef):
errorSize = ctypes.c_int(0)
sError = ctypes.c_wchar_p()
res = capi.dll.CSFieldDefinition_GetNumAvailableQuantityTypes(fieldDef._internal_obj if fieldDef is not None else None, ctypes.byref(utils.to_int32(errorSize)), ctypes.byref(sError))
if errorSize.value != 0:
raise errors.DPFServerException(sError.value)
return res

@staticmethod
def csfield_definition_is_of_quantity_type(fieldDef, quantityType):
errorSize = ctypes.c_int(0)
sError = ctypes.c_wchar_p()
res = capi.dll.CSFieldDefinition_GetNumAvailableQuantityTypes(fieldDef._internal_obj if fieldDef is not None else None, utils.to_char_ptr(quantityType), ctypes.byref(utils.to_int32(errorSize)), ctypes.byref(sError))
if errorSize.value != 0:
raise errors.DPFServerException(sError.value)
return res

@staticmethod
def csfield_definition_get_location(res):
Expand Down Expand Up @@ -241,6 +270,15 @@ def csfield_definition_set_shell_layers(fieldDef, shellLayers):
raise errors.DPFServerException(sError.value)
return res

@staticmethod
def csfield_definition_set_quantity_type(fieldDef, quantityType):
errorSize = ctypes.c_int(0)
sError = ctypes.c_wchar_p()
res = capi.dll.CSFieldDefinition_SetQuantityType(fieldDef._internal_obj if fieldDef is not None else None, utils.to_char_ptr(quantityType), ctypes.byref(utils.to_int32(errorSize)), ctypes.byref(sError))
if errorSize.value != 0:
raise errors.DPFServerException(sError.value)
return res

@staticmethod
def csfield_definition_set_location(fieldDef, location):
errorSize = ctypes.c_int(0)
Expand Down
31 changes: 31 additions & 0 deletions tests/test_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,37 @@ 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.set_quantity_type(qt)

# Testing the getter
assert fieldDef.get_quantity_type(0) == qt

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

# Testing the getter again
assert fieldDef.get_quantity_type(1) == qt2

# Testing the getter with an index out of range
assert fieldDef.get_quantity_type(2) == None

# Testing the getter with a negative index
with pytest.raises(Exception):
fieldDef.get_quantity_type(-1)

# Getting the number of available quantity types
assert fieldDef.get_num_available_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

0 comments on commit d1a7e88

Please sign in to comment.