Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 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
a2bb52e
Merge branch 'master' into feat/exposing_quantity_types
a-bouth Jan 24, 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
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 @@
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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe having just one property which returns the quantity_types would be easier too use, what do you think?

"""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
Loading