Skip to content

Commit

Permalink
Add PropertyField.name (#1469)
Browse files Browse the repository at this point in the history
* Add the FieldDefinition to the PropertyField and get/set the name through it.

Signed-off-by: paul.profizi <[email protected]>

* Add testing

Signed-off-by: paul.profizi <[email protected]>

* Fix retro-compatibility

Signed-off-by: paul.profizi <[email protected]>

* Fix retro-compatibility

Signed-off-by: paul.profizi <[email protected]>

* Add typehinting

Signed-off-by: paul.profizi <[email protected]>

* Add @version_requires decorators to new properties

Signed-off-by: paul.profizi <[email protected]>

---------

Signed-off-by: paul.profizi <[email protected]>
  • Loading branch information
PProfizi authored Apr 10, 2024
1 parent ecf0fa3 commit 9a99ff2
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/ansys/dpf/core/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def shell_layers(self, value):
fielddef.shell_layers = value
self.field_definition = fielddef

def get_entity_data(self, index):
def get_entity_data(self, index: int) -> dpf_array.DPFArray:
try:
vec = dpf_vector.DPFVectorDouble(client=self._server.client)
self._api.csfield_get_entity_data_for_dpf_vector(
Expand All @@ -353,7 +353,7 @@ def get_entity_data(self, index):
data.shape = (data.size // n_comp, n_comp)
return data

def get_entity_data_by_id(self, id):
def get_entity_data_by_id(self, id: int) -> dpf_array.DPFArray:
try:
vec = dpf_vector.DPFVectorDouble(client=self._server.client)
self._api.csfield_get_entity_data_by_id_for_dpf_vector(
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/dpf/core/fields_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def get_imaginary_field(self, timeid=None):

return super()._get_entry(label_space)

def __getitem__(self, key):
def __getitem__(self, key) -> field.Field:
"""Retrieve the field at a requested index.
Parameters
Expand Down
43 changes: 43 additions & 0 deletions src/ansys/dpf/core/property_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
"""

import numpy as np
from ansys.dpf.core.check_version import version_requires
from ansys.dpf.core.common import natures, locations, _get_size_of_list
from ansys.dpf.core import scoping, dimensionality
from ansys.dpf.core.field_base import _FieldBase, _LocalFieldBase
from ansys.dpf.core.check_version import meets_version
from ansys.dpf.core.field_definition import FieldDefinition
from ansys.dpf.gate import (
property_field_abstract_api,
property_field_capi,
Expand Down Expand Up @@ -69,6 +72,9 @@ def __init__(
field=property_field,
server=server,
)
self._field_definition = None
if meets_version(self._server.version, "8.1"):
self._field_definition = self._load_field_definition()

@property
def _api(self) -> property_field_abstract_api.PropertyFieldAbstractAPI:
Expand Down Expand Up @@ -101,6 +107,14 @@ def _field_create_internal_obj(
else:
return api.csproperty_field_new(nentities, nentities * dim.component_count)


@version_requires("8.1")
def _load_field_definition(self):
"""Attempt to load the field definition for this field."""
# try:
out = self._api.csproperty_field_get_shared_field_definition(self)
return FieldDefinition(out, self._server)

@property
def location(self):
"""Location of the property field.
Expand Down Expand Up @@ -299,6 +313,35 @@ def as_local_field(self):
"""
return _LocalPropertyField(self)

@property
@version_requires("8.1")
def name(self):
"""Name of the property field.
..note:
Available starting with DPF 2024.2.pre1.
"""
if self._field_definition:
return self._field_definition.name

@name.setter
@version_requires("8.1")
def name(self, value):
"""Change the name of the property field
Parameters
----------
value : str
Name of the property field.
..note:
Available starting with DPF 2024.2.pre1.
"""
if self._field_definition:
self._field_definition._api.csfield_definition_set_name(
self._field_definition, name=value
)


class _LocalPropertyField(_LocalFieldBase, PropertyField):
"""Caches the internal data of a field so that it can be modified locally.
Expand Down
4 changes: 3 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,9 @@ def return_ds(server=None):

return return_ds


SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_8_1 = meets_version(
get_server_version(core._global_server()), "8.1"
)
SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_8_0 = meets_version(
get_server_version(core._global_server()), "8.0"
)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_propertyfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ def test_set_get_data_property_field(server_type):
assert np.allclose(field.data, data)


@pytest.mark.skipif(not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_8_1,
reason="Available starting with DPF 8.1")
def test_set_get_name_property_field(server_type):
field = dpf.core.PropertyField(server=server_type)
field.name = "test"
assert field.name == "test"


def test_create_property_field_push_back(server_type):
f_vec = core.PropertyField(1, core.natures.vector, core.locations.nodal, server=server_type)
f_vec.append([1, 2, 4], 1)
Expand Down

0 comments on commit 9a99ff2

Please sign in to comment.