|
22 | 22 |
|
23 | 23 | """FieldDefinition.""" |
24 | 24 |
|
| 25 | +from __future__ import annotations |
| 26 | + |
25 | 27 | import traceback |
26 | 28 | import warnings |
27 | 29 |
|
28 | 30 | from ansys.dpf.core import server as server_module |
29 | | -from ansys.dpf.core.check_version import version_requires |
| 31 | +from ansys.dpf.core.available_result import Homogeneity |
| 32 | +from ansys.dpf.core.check_version import server_meet_version_and_raise, version_requires |
30 | 33 | from ansys.dpf.core.common import natures, shell_layers |
31 | 34 | from ansys.dpf.core.dimensionality import Dimensionality |
32 | 35 | from ansys.dpf.gate import ( |
@@ -204,8 +207,44 @@ def is_of_quantity_type(self, quantity_type): |
204 | 207 | return is_of_quantity_type |
205 | 208 |
|
206 | 209 | @unit.setter |
207 | | - def unit(self, value): |
208 | | - self._api.csfield_definition_set_unit(self, value, None, 0, 0, 0) |
| 210 | + def unit(self, value: str | tuple[Homogeneity, str]): |
| 211 | + """Change the unit for the field definition. |
| 212 | +
|
| 213 | + A single string is interpreted as a known physical unit with an associated homogeneity. |
| 214 | +
|
| 215 | + For DPF 11.0 (2026 R1) and above: A tuple of two strings is interpreted as a homogeneity and a unit name. |
| 216 | + If the homogeneity is :py:attr:`Homogeneity.dimensionless`, then the unit string is kept as a name. |
| 217 | + Otherwise, the homogeneity is ignored, and the unit string interpreted as a known physical unit with an associated homogeneity. |
| 218 | +
|
| 219 | + Parameters |
| 220 | + ---------- |
| 221 | + value: |
| 222 | + Units for the field. |
| 223 | +
|
| 224 | + Notes |
| 225 | + ----- |
| 226 | + Setting a named dimensionless unit requires DPF 11.0 (2026 R1) or above. |
| 227 | +
|
| 228 | + """ |
| 229 | + # setter with explicit homogeneity: homogeneity is taken into account if it is dimensionless |
| 230 | + if ( |
| 231 | + isinstance(value, tuple) |
| 232 | + and len(value) == 2 |
| 233 | + and isinstance(value[0], Homogeneity) |
| 234 | + and isinstance(value[1], str) |
| 235 | + ): |
| 236 | + server_meet_version_and_raise( |
| 237 | + required_version="11.0", |
| 238 | + server=self._server, |
| 239 | + msg="Setting a named dimensionless unit requires DPF 11.0 (2026 R1) or above.", |
| 240 | + ) |
| 241 | + # csfield_definition_set_unit will ignore the homogeneity if it is not dimensionless |
| 242 | + self._api.csfield_definition_set_unit(self, value[1], None, value[0].value, 0, 0) |
| 243 | + # standard unit setter, using string interpreter |
| 244 | + elif isinstance(value, str): |
| 245 | + self._api.csfield_definition_set_unit(self, value, None, 0, 0, 0) |
| 246 | + else: |
| 247 | + raise ValueError("Unit setter supports either string or tuple(Homogeneity, str)") |
209 | 248 |
|
210 | 249 | @location.setter |
211 | 250 | def location(self, value): |
|
0 commit comments