Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 16 additions & 1 deletion src/ansys/dpf/core/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,9 +620,15 @@ def unit(self):
def unit(self, value):
"""Change the unit for the field.

If the value is a single string, then it will be interpreted to a physical meaning (homogeneity).

If the value is a tuple, then it must contain an homogeneity and a string.
If homogeneity is "dimensionless", then the unit string is kept as is.
Otherwise, homogeneity is ignored, and unit string is interpreted to a physical meaning.

Parameters
----------
value : str
value : str | tuple(str, str)
Units for the field.

Examples
Expand All @@ -635,6 +641,15 @@ def unit(self, value):
>>> my_field.unit
'm'


Named dimensionless unit.

>>> from ansys.dpf import core as dpf
>>> my_field = dpf.Field(10)
>>> my_field.unit = ("dimensionless", "dollars")
>>> print(my_field.unit)
'dollars'

"""
fielddef = self.field_definition
fielddef.unit = value
Expand Down
11 changes: 10 additions & 1 deletion src/ansys/dpf/core/field_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,16 @@ def is_of_quantity_type(self, quantity_type):

@unit.setter
def unit(self, value):
self._api.csfield_definition_set_unit(self, value, None, 0, 0, 0)
# setter with explicit homogeneity: homogeneity is taken into account if it is dimensionless
if isinstance(value, tuple):
if value[0] == "dimensionless":
# 117 corresponds to dimensionless
self._api.csfield_definition_set_unit(self, value[1], None, 117, 0, 0)
else:
self._api.csfield_definition_set_unit(self, value[1], None, 0, 0, 0)
# standard unit setter, using string interpreter
else:
self._api.csfield_definition_set_unit(self, value, None, 0, 0, 0)

@location.setter
def location(self, value):
Expand Down
3 changes: 3 additions & 0 deletions tests/entry/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ def _get_test_files_directory():
"/tmp/test_files"
)

SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_10_0 = meets_version(
get_server_version(core._global_server()), "10.0"
)
SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_8_1 = meets_version(
get_server_version(core._global_server()), "8.1"
)
Expand Down
22 changes: 21 additions & 1 deletion tests/test_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@
from ansys.dpf.core.check_version import server_meet_version
from ansys.dpf.core.common import locations, shell_layers
import conftest
from conftest import SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_8_0, running_docker
from conftest import (
SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_8_0,
SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_10_0,
running_docker,
)


@pytest.fixture()
Expand Down Expand Up @@ -1417,3 +1421,19 @@ def test_deep_copy_big_field_remote(server_type, server_type_remote_process):

out = dpf.core.core._deep_copy(field_a, server_type_remote_process)
assert np.allclose(out.data, data)


@pytest.mark.skipif(
not SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_10_0, reason="Available for servers >=10.0"
)
def test_set_units():
data = np.random.random(100)
field = dpf.core.field_from_array(data)
field.unit = "m"
assert field.unit == "m"

field.unit = ("dimensionless", "sones")
assert field.unit == "sones"

with pytest.raises(Exception):
field.unit = "sones"
Loading