Skip to content

Commit

Permalink
Fix/issue 609 (#616)
Browse files Browse the repository at this point in the history
* Handle units withs exponant such as m2 or m2kg2.

* Refactor the way to get the units.

* Add the return value description and call find_units_in_dependent_variables.

* Add a unit test for the decompose_variable_value.

* Some units are still missing from the AEDT dictionary.
  • Loading branch information
MaxJPRey authored Dec 3, 2021
1 parent d06c4db commit 290016c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
11 changes: 11 additions & 0 deletions _unittest/test_09_VariableManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import math

from pyaedt.application.Variables import Variable
from pyaedt.application.Variables import decompose_variable_value
from pyaedt.generic.filesystem import Scratch
from pyaedt.generic.general_methods import isclose
from pyaedt.hfss import Hfss
Expand Down Expand Up @@ -333,3 +334,13 @@ def test_10_division(self):

def test_11_delete_variable(self):
assert self.aedtapp.variable_manager.delete_variable("Var1")

def test_12_decompose_variable_value(self):
assert decompose_variable_value("3.123456m") == (3.123456, "m")
assert decompose_variable_value("3m") == (3, "m")
assert decompose_variable_value("3") == (3, "")
assert decompose_variable_value("3.") == (3., "")
assert decompose_variable_value("3.123456m2") == (3.123456, "m2")
assert decompose_variable_value("3.123456Nm-2") == (3.123456, "Nm-2")
assert decompose_variable_value("3.123456kg2m2") == (3.123456, "kg2m2")
assert decompose_variable_value("3.123456kgm2") == (3.123456, "kgm2")
17 changes: 9 additions & 8 deletions pyaedt/application/Variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,11 @@ def decompose_variable_value(variable_value, full_variables={}):
----------
variable_value : float
full_variables : dict
Returns
-------
tuples
tuples made of the float value of the variable and the units exposed as a string.
"""
# set default return values - then check for valid units
float_value = variable_value
Expand All @@ -267,18 +269,17 @@ def decompose_variable_value(variable_value, full_variables={}):
float_value = float(variable_value)
except ValueError:
# search for a valid units string at the end of the variable_value
loc = re.search("[a-z_A-Z]+$", variable_value)
loc = re.search("[a-z_A-Z]+", variable_value)
units = _find_units_in_dependent_variables(variable_value, full_variables)

if loc:
loc_units = loc.span()[0]
extract_units = variable_value[loc_units:]
if unit_system(extract_units):
try:
float_value = float(variable_value[0:loc_units])
units = extract_units
except ValueError:
float_value = variable_value
try:
float_value = float(variable_value[0:loc_units])
units = extract_units
except ValueError:
float_value = variable_value

return float_value, units

Expand Down

0 comments on commit 290016c

Please sign in to comment.