Skip to content

Commit 7cb7a46

Browse files
committed
feat: use Homogeneity enum. Rework documentation
1 parent ea3a856 commit 7cb7a46

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

src/ansys/dpf/core/field.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -620,15 +620,15 @@ def unit(self):
620620
def unit(self, value):
621621
"""Change the unit for the field.
622622
623-
If the value is a single string, then it will be interpreted to a physical meaning (homogeneity).
623+
A single string is interpreted as a known physical unit with an associated homogeneity.
624624
625-
If the value is a tuple, then it must contain an homogeneity and a string.
626-
If homogeneity is "dimensionless", then the unit string is kept as is.
627-
Otherwise, homogeneity is ignored, and unit string is interpreted to a physical meaning.
625+
A tuple of two strings is interpreted as a homogeneity and a unit name.
626+
If the homogeneity is Homogeneity.dimensionless, then the unit string is kept as a name.
627+
Otherwise, the homogeneity is ignored, and the unit string is interpreted as a known physical unit with an associated homogeneity.
628628
629629
Parameters
630630
----------
631-
value : str | tuple(str, str)
631+
value : str | tuple(Homogeneity, str)
632632
Units for the field.
633633
634634
Examples
@@ -645,8 +645,9 @@ def unit(self, value):
645645
Named dimensionless unit.
646646
647647
>>> from ansys.dpf import core as dpf
648+
>>> from ansys.dpf.core.available_result import Homogeneity
648649
>>> my_field = dpf.Field(10)
649-
>>> my_field.unit = ("dimensionless", "dollars")
650+
>>> my_field.unit = (Homogeneity.dimensionless, "dollars")
650651
>>> print(my_field.unit)
651652
'dollars'
652653

src/ansys/dpf/core/field_definition.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222

2323
"""FieldDefinition."""
2424

25+
from argparse import ArgumentError
2526
import traceback
2627
import warnings
2728

2829
from ansys.dpf.core import server as server_module
30+
from ansys.dpf.core.available_result import Homogeneity
2931
from ansys.dpf.core.check_version import version_requires
3032
from ansys.dpf.core.common import natures, shell_layers
3133
from ansys.dpf.core.dimensionality import Dimensionality
@@ -206,15 +208,21 @@ def is_of_quantity_type(self, quantity_type):
206208
@unit.setter
207209
def unit(self, value):
208210
# setter with explicit homogeneity: homogeneity is taken into account if it is dimensionless
209-
if isinstance(value, tuple):
210-
if value[0] == "dimensionless":
211-
# 117 corresponds to dimensionless
212-
self._api.csfield_definition_set_unit(self, value[1], None, 117, 0, 0)
213-
else:
214-
self._api.csfield_definition_set_unit(self, value[1], None, 0, 0, 0)
211+
if (
212+
isinstance(value, tuple)
213+
and len(value) == 2
214+
and isinstance(value[0], Homogeneity)
215+
and isinstance(value[1], str)
216+
):
217+
# csfield_definition_set_unit will ignore the homogeneity if it is not dimensionless
218+
self._api.csfield_definition_set_unit(self, value[1], None, value[0].value, 0, 0)
215219
# standard unit setter, using string interpreter
216-
else:
220+
elif isinstance(value, str):
217221
self._api.csfield_definition_set_unit(self, value, None, 0, 0, 0)
222+
else:
223+
raise ArgumentError(
224+
None, message="Unit setter supports either string or tuple(Homogeneity, str)"
225+
)
218226

219227
@location.setter
220228
def location(self, value):

tests/test_field.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
# SOFTWARE.
2222

23+
from argparse import ArgumentError
2324
import copy
2425
import gc
2526

@@ -29,8 +30,10 @@
2930
from ansys import dpf
3031
from ansys.dpf import core
3132
from ansys.dpf.core import FieldDefinition, operators as ops
33+
from ansys.dpf.core.available_result import Homogeneity
3234
from ansys.dpf.core.check_version import server_meet_version
3335
from ansys.dpf.core.common import locations, shell_layers
36+
from ansys.dpf.gate.errors import DPFServerException
3437
import conftest
3538
from conftest import (
3639
SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_8_0,
@@ -1429,11 +1432,18 @@ def test_deep_copy_big_field_remote(server_type, server_type_remote_process):
14291432
def test_set_units():
14301433
data = np.random.random(100)
14311434
field = dpf.core.field_from_array(data)
1435+
# use string setter with recognized string
14321436
field.unit = "m"
14331437
assert field.unit == "m"
14341438

1435-
field.unit = ("dimensionless", "sones")
1439+
# use tuple(Homogeneity, string) setter
1440+
field.unit = (Homogeneity.dimensionless, "sones")
14361441
assert field.unit == "sones"
14371442

1438-
with pytest.raises(Exception):
1443+
# use unrecognized string
1444+
with pytest.raises(DPFServerException):
14391445
field.unit = "sones"
1446+
1447+
# use wrong type of arguments
1448+
with pytest.raises(ArgumentError):
1449+
field.unit = 1.0

0 commit comments

Comments
 (0)