Skip to content

Commit 53cf486

Browse files
committed
feat: allow giving a name to a dimensionless unit (eg: sones)
1 parent bbc5f42 commit 53cf486

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

src/ansys/dpf/core/field.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,29 @@ def unit(self, value):
640640
fielddef.unit = value
641641
self.field_definition = fielddef
642642

643+
def set_named_dimensionless_unit(self, value):
644+
"""Set a named dimensionless unit for the field.
645+
646+
Parameters
647+
----------
648+
value : str
649+
Units for the field. This unit must be homogeneous to no physical quantity
650+
651+
Examples
652+
--------
653+
Units for a psychoacoustics field.
654+
655+
>>> from ansys.dpf import core as dpf
656+
>>> my_field = dpf.Field(10)
657+
>>> my_field.set_named_dimensionless_unit("sones")
658+
>>> print(my_field.unit)
659+
sones
660+
661+
"""
662+
fielddef = self.field_definition
663+
fielddef.set_named_dimensionless_unit(value)
664+
self.field_definition = fielddef
665+
643666
@property
644667
def dimensionality(self):
645668
"""Dimensionality represents the shape of the elementary data contained in the field.

src/ansys/dpf/core/field_definition.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,17 @@ def is_of_quantity_type(self, quantity_type):
207207
def unit(self, value):
208208
self._api.csfield_definition_set_unit(self, value, None, 0, 0, 0)
209209

210+
def set_named_dimensionless_unit(self, value):
211+
"""Set a named dimensionless unit for the field.
212+
213+
Parameters
214+
----------
215+
value : str
216+
Units for the field. This unit must be homogeneous to no physical quantity
217+
"""
218+
# 117 corresponds to dimensionless
219+
self._api.csfield_definition_set_unit(self, value, None, 117, 0, 0)
220+
210221
@location.setter
211222
def location(self, value):
212223
self._api.csfield_definition_set_location(self, value)

tests/test_field.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@
3232
from ansys.dpf.core.check_version import server_meet_version
3333
from ansys.dpf.core.common import locations, shell_layers
3434
import conftest
35-
from conftest import SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_8_0, running_docker
35+
from conftest import (
36+
SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_8_0,
37+
SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_10_0,
38+
running_docker,
39+
)
3640

3741

3842
@pytest.fixture()
@@ -1417,3 +1421,16 @@ def test_deep_copy_big_field_remote(server_type, server_type_remote_process):
14171421

14181422
out = dpf.core.core._deep_copy(field_a, server_type_remote_process)
14191423
assert np.allclose(out.data, data)
1424+
1425+
1426+
@pytest.mark.skipif(
1427+
not SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_10_0, reason="Available for servers >=10.0"
1428+
)
1429+
def test_set_named_dimensionless_units():
1430+
data = np.random.random(100)
1431+
field = dpf.core.field_from_array(data)
1432+
field.unit = "m"
1433+
assert field.unit == "m"
1434+
1435+
field.set_named_dimensionless_unit("sones")
1436+
assert field.unit == "sones"

0 commit comments

Comments
 (0)