From 290016c2c84af3b36121f6f60e0027ae11bdcd92 Mon Sep 17 00:00:00 2001 From: Maxime Rey <87315832+MaxJPRey@users.noreply.github.com> Date: Fri, 3 Dec 2021 15:12:11 +0100 Subject: [PATCH] Fix/issue 609 (#616) * 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. --- _unittest/test_09_VariableManager.py | 11 +++++++++++ pyaedt/application/Variables.py | 17 +++++++++-------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/_unittest/test_09_VariableManager.py b/_unittest/test_09_VariableManager.py index 7f95f8149d0..11fd11b8629 100644 --- a/_unittest/test_09_VariableManager.py +++ b/_unittest/test_09_VariableManager.py @@ -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 @@ -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") diff --git a/pyaedt/application/Variables.py b/pyaedt/application/Variables.py index 81b026375b7..d65a2ebea65 100644 --- a/pyaedt/application/Variables.py +++ b/pyaedt/application/Variables.py @@ -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 @@ -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