From c2483876afdb9e6ad7cf93a2cfb1e1f71390d88b Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Fri, 21 Oct 2022 15:34:47 +0530 Subject: [PATCH 01/40] Initial base interface and representation --- src/ansys/fluent/core/quantity.py | 38 +++++++++++++++++++++++++++++++ tests/test_quantity.py | 29 +++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 src/ansys/fluent/core/quantity.py create mode 100644 tests/test_quantity.py diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py new file mode 100644 index 000000000000..04b6ce8c6c0c --- /dev/null +++ b/src/ansys/fluent/core/quantity.py @@ -0,0 +1,38 @@ +class Quantity(float): + """This class instantiates physical quantities using their real values and + units. + + Attributes + ---------- + value: Real value + Value of quantity is stored as float. + + unit: Unit string + Unit of quantity is stored as string. + + Methods + ------- + to(to_unit) + Converts to given unit string. + + Returns + ------- + Quantity instance. + + All the instances of this class are converted to base SI units system to have + consistency in all arithmetic operations. + """ + + def __new__(self, real_value, units_string): + return float.__new__(self, real_value) + + def __init__(self, real_value, units_string): + float.__init__(real_value) + self.value = self.__float__() + self.unit = units_string + + def __str__(self): + return f'({self.value}, "{self.unit}")' + + def __repr__(self): + return f'(Quantity ({self.value}, "{self.unit}"))' diff --git a/tests/test_quantity.py b/tests/test_quantity.py new file mode 100644 index 000000000000..b78baa8c14a5 --- /dev/null +++ b/tests/test_quantity.py @@ -0,0 +1,29 @@ +import pytest + +import ansys.fluent.core.quantity as q + +DELTA = 1.0e-5 + + +def test_value_unit_1(): + v = q.Quantity(1, "m s^-1") + assert v.value == 1 + assert v.unit == "m s^-1" + + +def test_value_unit_2(): + v = q.Quantity(10.6, "m") + assert v.value == 10.6 + assert v.unit == "m" + + +def test_value_unit_3(): + v = q.Quantity(99.85, "radian") + assert v.value == pytest.approx(99.85, DELTA) + assert v.unit == "radian" + + +def test_value_unit_4(): + v = q.Quantity(5.7, "") + assert v.value == pytest.approx(5.7, DELTA) + assert v.unit == "" From c18bfc3e1637c6141edc447539574aae500a9641 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Fri, 21 Oct 2022 16:11:14 +0530 Subject: [PATCH 02/40] Added required method description --- src/ansys/fluent/core/quantity.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 04b6ce8c6c0c..64f48bc39096 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -15,6 +15,18 @@ class Quantity(float): to(to_unit) Converts to given unit string. + toSystem(baseSystem) + Converts to given base system. + + getDimensions(unit) + Extracts dimensions from unit. + + isDimensionless() + Determines type of quantity. + + checkDimensionsMatch(quantity1, quantity2) + Compares dimensions of 2 quantities. + Returns ------- Quantity instance. From caf695c1930a07c3dac46bf8cda39eebb977fbe4 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Fri, 21 Oct 2022 18:04:08 +0530 Subject: [PATCH 03/40] Replaced self with cls on __new__ method --- src/ansys/fluent/core/quantity.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 64f48bc39096..cf8c766e2176 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -35,8 +35,8 @@ class Quantity(float): consistency in all arithmetic operations. """ - def __new__(self, real_value, units_string): - return float.__new__(self, real_value) + def __new__(cls, real_value, units_string): + return float.__new__(cls, real_value) def __init__(self, real_value, units_string): float.__init__(real_value) From 377b1cdac6cc44b7645272c73ded2a546cb784ca Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Fri, 28 Oct 2022 18:08:18 +0530 Subject: [PATCH 04/40] Real Quantity with Units Module --- src/ansys/fluent/core/quantity.py | 530 +++++++++++++++++++++++++++++- tests/test_quantity.py | 408 +++++++++++++++++++++++ 2 files changed, 929 insertions(+), 9 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index cf8c766e2176..a00089629895 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -1,3 +1,390 @@ +from collections import OrderedDict + + +class UnitsTable(object): + _fundamental_units = { + "kg": "M", + "g": "M", + "lb": "M", + "lbm": "M", + "slug": "M", + "m": "L", + "cm": "L", + "ft": "L", + "in": "L", + "s": "T", + "A": "A", + "K": "K", + "C": "K", + "F": "K", + "R": "K", + "mol": "mol", + "cd": "cd", + "sr": "sr", + "radian": "Angle", + "degree": "Angle", + } + + _derived_units = { + "N": "kg m s^-2", + "Pa": "N m^-2", + "W": "N m s^-1", + "J": "N m", + "V": "A ohm", + "F": "N m V^-2", + "H": "N m A^-2", + "S": "ohm^-1", + "Wb": "N m A^-1", + "T": "Wb m^-2", + "dyne": "g cm s^-2", + "erg": "dyne cm", + "pdl": "lbm ft s^-2", + "BTU": "J", + "psi": "lbf in^-2", + "lbf": "slug ft s^-2", + "psf": "lbf ft^-2", + "ohm": "kg m^2 s^-3 A^-2", + "Hz": "s^-1", + } + + _derived_units_with_conversion_factor = { + "l": (0.001, "m^3"), + "gal": (0.0037854117839999993, "m^3"), + "BTU": (1055.056, "J"), + } + + _multipliers = { + "d": 10**-1, + "c": 10**-2, + "m": 10**-3, + "μ": 10**-6, + "n": 10**-9, + "p": 10**-12, + "f": 10**-15, + "a": 10**-18, + "z": 10**-21, + "y": 10**-24, + "da": 10**1, + "h": 10**2, + "k": 10**3, + "M": 10**6, + "G": 10**9, + "T": 10**12, + "P": 10**15, + "E": 10**18, + "Z": 10**21, + "Y": 10**24, + } + + _dimension_order = OrderedDict( + [ + ("Mass", "M"), + ("Length", "L"), + ("Time", "T"), + ("Temperature", "K"), + ("Angle", "Angle"), + ("ChemicalAmount", "mol"), + ("Light", "cd"), + ("Current", "A"), + ("SolidAngle", "sr"), + ("", ""), + ] + ) + + _conversion_map = { + "kg": 1, + "g": 0.001, + "lb": 0.45359237, + "lbm": 0.45359237, + "slug": 14.59390293720637, + "m": 1, + "cm": 0.01, + "ft": 0.30479999999999996, + "in": 0.0254, + "s": 1, + "A": 1, + "mol": 1, + "cd": 1, + "sr": 1, + "radian": 1, + "degree": 0.017453292519943295, + "K": 1, + "C": 1, + "F": 0.5555555555555556, + "R": 0.5555555555555556, + } + + _si_map = { + "kg": "kg", + "g": "kg", + "lb": "kg", + "lbm": "kg", + "slug": "kg", + "m": "m", + "cm": "m", + "ft": "m", + "in": "m", + "s": "s", + "A": "A", + "mol": "mol", + "cd": "cd", + "sr": "sr", + "radian": "radian", + "degree": "radian", + "K": "K", + "C": "K", + "F": "K", + "R": "K", + "": "", + } + + _offset_conversions = { + "K": 1, + "C": 274.15, + "F": 255.92777777777778, + "R": 0.5555555555555556, + } + + _offset_dict = { + "K": {"C": 274.15, "F": 255.92777777777778, "R": 0.5555555555555556}, + "C": {"K": -272.15, "F": -17.2222222222222, "R": -272.59444444444443}, + "F": {"C": 33.79999999999993, "K": -457.87, "R": -458.67}, + "R": {"C": 493.4699999999999, "F": 460.66999999999996, "K": 1.7999999999999998}, + } + + +def get_si_conversion_factor(unit_str): + return ( + UnitsTable._conversion_map[unit_str] + if unit_str in UnitsTable._conversion_map.keys() + else 1 + ) + + +def filter_multiplier(unit_str, predicate=None): + result = False + matched = "" + for item in UnitsTable._multipliers.keys(): + result = predicate(item, unit_str) if predicate else item == unit_str + if result: + matched = item + result = True + temp_unit_str = unit_str[len(item) :] + if ( + temp_unit_str in UnitsTable._fundamental_units + or temp_unit_str in UnitsTable._derived_units + ): + break + else: + result = False + continue + return result, matched + + +def remove_multiplier(unit_str): + has_multiplier, prefix = filter_multiplier( + unit_str, lambda item, unit: len(unit) > 1 and unit.startswith(item) + ) + if has_multiplier: + unit_str = unit_str[len(prefix) :] + return unit_str + + +class Unit(object): + def __init__(self, unit_str): + self._unit = unit_str + self._si_multiplier = 1 + self._si_offset = 0 + self._si_unit = "" + self._compute_multipliers_and_offsets(unit_str, 1) + self._reduce_to_si_unit(self._si_unit) + + @property + def user_unit(self): + return self._unit + + @property + def si_factor(self): + return self._si_multiplier + + @property + def si_unit(self): + return self._si_unit + + def __call__(self): + return self.user_unit + + def _reduce_to_si_unit(self, unit_str): + term_power_dict = OrderedDict() + unit_split = unit_str.split(" ") + + for term in unit_split: + if "^" in term: + term_split = term.split("^") + if term_split[0] in term_power_dict.keys(): + term_power_dict[term_split[0]] += int(term_split[1]) + else: + term_power_dict[term_split[0]] = int(term_split[1]) + else: + if term in term_power_dict.keys(): + term_power_dict[term] += 1 + else: + term_power_dict[term] = 1 + + self._si_unit = "" + + for key, power in term_power_dict.items(): + spacechar = " " if len(self._si_unit) > 0 else "" + if power > 1 or power < 0: + self._si_unit += spacechar + key + "^" + str(power) + else: + self._si_unit += spacechar + key + + def _compute_multipliers_and_offsets(self, unit_str, power): + if len(unit_str) == 0: + return + + unit_list = unit_str.split(" ") + + for term in unit_list: + unit_str = term + term_power = 1 + + if "^" in unit_str: + unit_str, term_power = unit_str[: unit_str.index("^")], int( + unit_str[unit_str.index("^") + 1 :] + ) + + term_power *= power + has_multiplier = not ( + unit_str in UnitsTable._fundamental_units + or unit_str in UnitsTable._derived_units + ) + + if has_multiplier: + _, prefix = filter_multiplier( + unit_str, lambda item, unit: len(unit) > 1 and unit.startswith(item) + ) + + if len(prefix): + self._si_multiplier *= UnitsTable._multipliers[prefix] ** term_power + + unit_str = remove_multiplier(unit_str) + + if unit_str in UnitsTable._fundamental_units: + spacechar = " " if len(self._si_unit) > 0 else "" + + if term_power > 1 or term_power < 0: + self._si_unit += ( + spacechar + UnitsTable._si_map[unit_str] + "^" + str(term_power) + ) + else: + self._si_unit += spacechar + UnitsTable._si_map[unit_str] + + self._si_multiplier *= get_si_conversion_factor(unit_str) ** term_power + + elif unit_str in UnitsTable._derived_units_with_conversion_factor: + ( + conversion_factor, + unit_str, + ) = UnitsTable._derived_units_with_conversion_factor[unit_str] + self._si_multiplier *= conversion_factor**term_power + self._compute_multipliers_and_offsets(unit_str, term_power) + elif unit_str in UnitsTable._derived_units: + self._compute_multipliers_and_offsets( + UnitsTable._derived_units[unit_str], term_power + ) + + +class Dimension(object): + def __init__(self, unit_str): + self._dimensions = { + "M": 0, + "L": 0, + "T": 0, + "K": 0, + "A": 0, + "mol": 0, + "cd": 0, + "Angle": 0, + "sr": 0, + "": 0, + } + self._parser(unit_str) + + def _add_or_update_dimension(self, dim_dict): + for key in dim_dict.keys(): + self._dimensions[key] += dim_dict[key] + + def _parser(self, unit, power=1): + if len(unit) == 0: + return + + unit_list = unit.split(" ") + for term in unit_list: + unit_dim = "" + has_multiplier = not ( + term in UnitsTable._fundamental_units + or term in UnitsTable._derived_units + ) + unit_str = remove_multiplier(term) if has_multiplier else term + term_power = 1 + + if "^" in term: + unit_str, term_power = term[: term.index("^")], int( + term[term.index("^") + 1 :] + ) + has_multiplier = not ( + unit_str in UnitsTable._fundamental_units + or unit_str in UnitsTable._derived_units + ) + unit_str = remove_multiplier(unit_str) if has_multiplier else unit_str + + term_power *= power + unit_dim = self._get_dim(unit_str, term_power) + + if unit_dim != None: + self._add_or_update_dimension(unit_dim) + + def get_dimensions_dict(self): + return self._dimensions + + def _get_dim(self, unit_str, power): + if unit_str in UnitsTable._fundamental_units: + return {UnitsTable._fundamental_units[unit_str]: power} + elif unit_str in UnitsTable._derived_units: + self._parser(UnitsTable._derived_units[unit_str], power) + elif unit_str in UnitsTable._derived_units_with_conversion_factor: + _, unit_str = UnitsTable._derived_units_with_conversion_factor[unit_str] + self._parser(unit_str, power) + else: + raise ValueError("Not implemented") + + +def get_si_unit_from_dim(dim_list): + si_unit = "" + dim_to_unit_map = { + "M": "kg", + "L": "m", + "T": "s", + "K": "K", + "A": "A", + "mol": "mol", + "cd": "cd", + "Angle": "radian", + "sr": "sr", + "": "", + } + for key, power in zip(UnitsTable._dimension_order.values(), dim_list): + unit_str = dim_to_unit_map[key] + spacechar = " " if len(si_unit) > 0 else "" + if power > 1 or power < 0: + si_unit += spacechar + unit_str + "^" + str(power) + elif power == 1: + si_unit += spacechar + unit_str + return si_unit + + class Quantity(float): """This class instantiates physical quantities using their real values and units. @@ -15,18 +402,12 @@ class Quantity(float): to(to_unit) Converts to given unit string. - toSystem(baseSystem) - Converts to given base system. - getDimensions(unit) Extracts dimensions from unit. isDimensionless() Determines type of quantity. - checkDimensionsMatch(quantity1, quantity2) - Compares dimensions of 2 quantities. - Returns ------- Quantity instance. @@ -38,13 +419,144 @@ class Quantity(float): def __new__(cls, real_value, units_string): return float.__new__(cls, real_value) - def __init__(self, real_value, units_string): + def __init__(self, real_value, unit_str): float.__init__(real_value) - self.value = self.__float__() - self.unit = units_string + self._value = self.__float__() + self._unit = Unit(unit_str) + self._dimension = Dimension(unit_str) + + @property + def value(self): + return self._value + + @property + def unit(self): + return self._unit.user_unit + + @value.setter + def value(self, val): + self._value = val + + @property + def dimension(self): + return self._dimension + + @property + def si_value(self): + return self._unit.si_factor * self._value + + @property + def si_unit(self): + return self._unit.si_unit + + def is_dimension_less(self): + return all([value == 0 for value in self.get_dimensions_list()]) + + def get_dimensions_list(self): + dims = self._dimension.get_dimensions_dict() + return [ + dims[UnitsTable._dimension_order[key]] + for key in UnitsTable._dimension_order.keys() + ] + + def to(self, to_unit_str): + temp_quantity = Quantity(1, to_unit_str) + + curr_unit_dim_obj = self.dimension + to_unit_dim_obj = temp_quantity.dimension + + if ( + curr_unit_dim_obj.get_dimensions_dict() + != to_unit_dim_obj.get_dimensions_dict() + ): + raise ValueError( + f"Incompatible conversion from {self.unit} : to {to_unit_str}" + ) + else: + pass + + curr_unit_obj = self._unit + to_unit_obj = temp_quantity._unit + temp_quantity.value = ( + curr_unit_obj.si_factor / to_unit_obj.si_factor + ) * self.value + + return temp_quantity + + def _get_si_unit(self, other, func): + curr_dim = self.get_dimensions_list() + other_dim = other.get_dimensions_list() + temp_dim = [func(curr_dim[i], other_dim[i]) for i in range(len(other_dim))] + temp_unit = get_si_unit_from_dim(temp_dim) + return temp_unit + + def pow(self, exponent): + new_dims = list( + map(lambda x: x * exponent if x != 0 else x, self.get_dimensions_list()) + ) + new_si_unit = get_si_unit_from_dim(new_dims) + new_si_value = pow(self.si_value, exponent) + return Quantity(new_si_value, new_si_unit) def __str__(self): return f'({self.value}, "{self.unit}")' def __repr__(self): return f'(Quantity ({self.value}, "{self.unit}"))' + + def __mul__(self, other): + if isinstance(other, Quantity): + temp_value = self.si_value * other.si_value + temp_unit = self._get_si_unit(other, lambda x, y: x + y) + return Quantity(temp_value, temp_unit) + elif isinstance(other, int) or isinstance(other, float): + temp = Quantity(self.si_value * other, self.si_unit) + return temp + + def __rmul__(self, other): + return self.__mul__(other) + + def __truediv__(self, other): + if isinstance(other, Quantity): + temp_value = self.si_value / other.si_value + temp_unit = self._get_si_unit(other, lambda x, y: x - y) + return Quantity(temp_value, temp_unit) + elif isinstance(other, int) or isinstance(other, float): + temp = Quantity(self.si_value / other, self.si_unit) + return temp + + def __add__(self, other): + if isinstance(other, Quantity): + temp_value = self.si_value + other.si_value + elif self.is_dimension_less() and ( + isinstance(other, int) or isinstance(other, float) + ): + temp_value = self.si_value + other + else: + raise ValueError(f"Quantity{(self.value, self.unit)} is not dimensionless.") + return Quantity(temp_value, self.si_unit) + + def __radd__(self, other): + return self.__add__(other) + + def __sub__(self, other): + if isinstance(other, Quantity): + temp_value = self.si_value - other.si_value + elif self.is_dimension_less() and ( + isinstance(other, int) or isinstance(other, float) + ): + temp_value = self.si_value - other + else: + raise ValueError(f"Quantity{(self.value, self.unit)} is not dimensionless.") + return Quantity(temp_value, self.si_unit) + + def __rsub__(self, other): + if isinstance(other, Quantity): + temp_value = other.si_value - self.si_value + elif self.is_dimension_less() and ( + isinstance(other, int) or isinstance(other, float) + ): + temp_value = other - self.si_value + else: + raise ValueError(f"Quantity{(self.value, self.unit)} is not dimensionless.") + return Quantity(temp_value, self.si_unit) diff --git a/tests/test_quantity.py b/tests/test_quantity.py index b78baa8c14a5..98daf06f23a1 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -27,3 +27,411 @@ def test_value_unit_4(): v = q.Quantity(5.7, "") assert v.value == pytest.approx(5.7, DELTA) assert v.unit == "" + + +def test_dims_5(): + v = q.Quantity(1.0, "kPa") + assert v.get_dimensions_list() == [1, -1, -2, 0, 0, 0, 0, 0, 0, 0] + + +def test_dims_6(): + v = q.Quantity(1.0, "ft") + assert v.get_dimensions_list() == [0, 1, 0, 0, 0, 0, 0, 0, 0, 0] + + +def test_dims_7(): + v = q.Quantity(1.0, "m m^-1") + assert v.get_dimensions_list() == [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + + +def test_to_8(): + v = q.Quantity(1.0, "m") + convert = v.to("ft") + assert convert.value == pytest.approx(3.2808398, DELTA) + assert convert.unit == "ft" + + +def test_to_9(): + v = q.Quantity(1.0, "m") + convert = v.to("mm") + assert convert.value == 1000 + assert convert.unit == "mm" + + +def test_to_10(): + v = q.Quantity(100000.0, "Pa") + convert = v.to("kPa") + assert convert.value == 100.0 + assert convert.unit == "kPa" + + +def test_to_11(): + v = q.Quantity(1.0, "dm^3") + convert = v.to("m^3") + assert convert.value == pytest.approx(0.001, DELTA) + assert convert.unit == "m^3" + + +def test_to_12(): + v = q.Quantity(1.0, "radian") + convert = v.to("degree") + assert convert.value == pytest.approx(57.295779, DELTA) + assert convert.unit == "degree" + + +def test_to_13(): + v = q.Quantity(1.0, "degree") + convert = v.to("radian") + assert convert.value == pytest.approx(0.01745329251, DELTA) + assert convert.unit == "radian" + + +def test_to_14(): + v = q.Quantity(1.0, "Pa s") + convert = v.to("dyne cm^-2 s") + assert convert.value == pytest.approx(10.0, DELTA) + assert convert.unit == "dyne cm^-2 s" + + +def test_to_15(): + v = q.Quantity(1.0, "kg m^-1 s^-1") + convert = v.to("dyne cm^-2 s") + assert convert.value == pytest.approx(10.0, DELTA) + assert convert.unit == "dyne cm^-2 s" + + +def test_to_16(): + v = q.Quantity(1.0, "Pa s") + convert = v.to("slug in^-1 s^-1") + assert convert.value == pytest.approx(0.00174045320, DELTA) + assert convert.unit == "slug in^-1 s^-1" + + +def test_to_17(): + v = q.Quantity(1.0, "kg m^-1 s^-1") + convert = v.to("slug in^-1 s^-1") + assert convert.value == pytest.approx(0.00174045320, DELTA) + assert convert.unit == "slug in^-1 s^-1" + + +def test_to_18(): + v = q.Quantity(1.0, "lb ft^-1 s^-1") + convert = v.to("Pa s") + assert convert.value == pytest.approx(1.488164, DELTA) + assert convert.unit == "Pa s" + + +def test_to_19(): + v = q.Quantity(1.0, "lb ft^-1 s^-1") + convert = v.to("kg m^-1 s^-1") + assert convert.value == pytest.approx(1.488164, DELTA) + assert convert.unit == "kg m^-1 s^-1" + + +def test_to_20(): + v = q.Quantity(1.0, "Hz") + with pytest.raises(ValueError) as e_info: + convert = v.to("radian s^-1") + + +def test_to_21(): + v = q.Quantity(1.0, "radian s^-1") + with pytest.raises(ValueError) as e_info: + convert = v.to("Hz") + + +def test_to_22(): + v = q.Quantity(1.0, "lbf ft^-2") + convert = v.to("N m^-2") + assert convert.value == pytest.approx(47.88024159, DELTA) + assert convert.unit == "N m^-2" + + +def test_to_23(): + v = q.Quantity(1.0, "ft^-3 s^-1") + convert = v.to("m^-3 s^-1") + assert convert.value == pytest.approx(35.3146667, DELTA) + assert convert.unit == "m^-3 s^-1" + + +def test_to_24(): + v = q.Quantity(1.0, "m^-2") + convert = v.to("cm^-2") + assert convert.value == pytest.approx(0.0001, DELTA) + assert convert.unit == "cm^-2" + + +def test_to_25(): + v = q.Quantity(1.0, "m^2") + convert = v.to("in^2") + assert convert.value == pytest.approx(1550.0031, DELTA) + assert convert.unit == "in^2" + + +def test_to_26(): + v = q.Quantity(1.0, "radian s^-1") + convert = v.to("degree s^-1") + assert convert.value == pytest.approx(57.295779, DELTA) + assert convert.unit == "degree s^-1" + + +def test_to_27(): + v = q.Quantity(1.0, "degree s^-1") + convert = v.to("radian s^-1") + assert convert.value == pytest.approx(0.01745329251, DELTA) + assert convert.unit == "radian s^-1" + + +def test_to_28(): + v = q.Quantity(1.0, "dyne cm^-2") + convert = v.to("N m^-2") + assert convert.value == pytest.approx(0.1, DELTA) + assert convert.unit == "N m^-2" + + +def test_to_29(): + v = q.Quantity(1.0, "psi") + convert = v.to("Pa") + assert convert.value == pytest.approx(6894.76, DELTA) + assert convert.unit == "Pa" + + +def test_to_30(): + v = q.Quantity(1.0, "pdl") + convert = v.to("N") + assert convert.value == pytest.approx(0.138254999, DELTA) + assert convert.unit == "N" + + +def test_to_31(): + v = q.Quantity(1.0, "ohm cm") + convert = v.to("ohm m") + assert convert.value == pytest.approx(0.01, DELTA) + assert convert.unit == "ohm m" + + +def test_to_32(): + v = q.Quantity(1.0, "erg") + convert = v.to("J") + assert convert.value == pytest.approx(1.0e-7, DELTA) + assert convert.unit == "J" + + +def test_to_33(): + v = q.Quantity(1.0, "BTU") + convert = v.to("J") + assert convert.value == pytest.approx(1055.056, DELTA) + assert convert.unit == "J" + + +def test_to_34(): + v = q.Quantity(1.0, "gal") + convert = v.to("m^3") + assert convert.value == pytest.approx(0.00378541, DELTA) + assert convert.unit == "m^3" + + +def test_to_35(): + v = q.Quantity(1.0, "l") + convert = v.to("m^3") + assert convert.value == pytest.approx(0.001, DELTA) + assert convert.unit == "m^3" + + +def test_to_36(): + v = q.Quantity(1.0, "BTU lb^-1 R^-1") + convert = v.to("J kg^-1 K^-1") + assert convert.value == pytest.approx(4186.8161854, DELTA) + assert convert.unit == "J kg^-1 K^-1" + + +def test_to_37(): + v = q.Quantity(1.0, "BTU lb^-1 F^-1") + convert = v.to("J kg^-1 K^-1") + assert convert.value == pytest.approx(4186.8161854, DELTA) + assert convert.unit == "J kg^-1 K^-1" + + +def test_to_38(): + v = q.Quantity(1.0, "gal^-1") + convert = v.to("m^-3") + assert convert.value == pytest.approx(264.172, DELTA) + assert convert.unit == "m^-3" + + +def test_to_39(): + v = q.Quantity(1.0, "BTU ft^-2") + convert = v.to("J m^-2") + assert convert.value == pytest.approx(11356.5713242, DELTA) + assert convert.unit == "J m^-2" + + +def testing_dimensions(): + print(f"{'*' * 25} {testing_dimensions.__name__} {'*' * 25}") + + def dim_test(unit_str, dim_list): + qt = q.Quantity(10, unit_str) + print(f"{unit_str} : {qt.get_dimensions_list()}") + assert qt.get_dimensions_list() == dim_list + + dim_test("m", [0, 1, 0, 0, 0, 0, 0, 0, 0, 0]) + dim_test("m s^-1", [0, 1, -1, 0, 0, 0, 0, 0, 0, 0]) + dim_test("kg m s^-2 m^-2", [1, -1, -2, 0, 0, 0, 0, 0, 0, 0]) + dim_test("Pa", [1, -1, -2, 0, 0, 0, 0, 0, 0, 0]) + dim_test("kPa", [1, -1, -2, 0, 0, 0, 0, 0, 0, 0]) + dim_test("Pa^2", [2, -2, -4, 0, 0, 0, 0, 0, 0, 0]) + dim_test("daPa", [1, -1, -2, 0, 0, 0, 0, 0, 0, 0]) + dim_test("MPa", [1, -1, -2, 0, 0, 0, 0, 0, 0, 0]) + dim_test("kPa^2", [2, -2, -4, 0, 0, 0, 0, 0, 0, 0]) + dim_test("slug in^-1 s^-1", [1, -1, -1, 0, 0, 0, 0, 0, 0, 0]) + dim_test("radian", [0, 0, 0, 0, 1, 0, 0, 0, 0, 0]) + dim_test("ohm", [1, 2, -3, 0, 0, 0, 0, -2, 0, 0]) + dim_test("lb cm s^-2", [1, 1, -2, 0, 0, 0, 0, 0, 0, 0]) + print("-" * 75) + + +def testing_to_systems(): + print(f"{'*' * 25} {testing_to_systems.__name__} {'*' * 25}") + test = q.Quantity(90, "lb cm s^-2") + print(f"test : {test.get_dimensions_list()}") # [1 1 -2 0 -2 0 0 0 0] + + test.to("kg m s^-2") + test.to("g m s^-2") + test.to("g cm s^-2") + test.to("g in s^-2") + test.to("g ft s^-2") + test.to("kg ft s^-2") + test.to("kg in s^-1 s^-1") + try: + test.to("ft s^-2") + except ValueError as ve: + print(ve) + + print("-" * 75) + + +def testing_multipliers(): + print(f"{'*' * 25} {testing_multipliers.__name__} {'*' * 25}") + + def from_to(from_str, to_str): + qt = q.Quantity(1, from_str) + to = qt.to(to_str) + print(f"from {qt} -> to {to}") + + from_to("mm", "cm") + from_to("m", "ft") + from_to("dm^3", "m^3") + from_to("m s^-1", "cm s^-1") + from_to("N", "dyne") + from_to("m^2", "in^2") + from_to("degree s^-1", "radian s^-1") + from_to("radian s^-1", "degree s^-1") + from_to("Pa", "lb m s^-2 ft^-2") + from_to("lb m s^-2 ft^-2", "Pa") + + from_to("J kg^-1 K^-1", "J kg^-1 C^-1") + from_to("J kg^-1 K^-1", "J kg^-1 R^-1") + from_to("J kg^-1 K^-1", "J kg^-1 F^-1") + + from_to("K", "C") + from_to("K", "R") + from_to("K", "F") + + print("-" * 75) + + +def testing_arithmetic_operators(): + print(f"{'*' * 25} {testing_arithmetic_operators.__name__} {'*' * 25}") + + qt1 = q.Quantity(10, "m s^-1") + qt2 = q.Quantity(5, "m s^-1") + + qt3 = qt1 * qt2 + + print(f"{qt1} * {qt2} = {qt3}") + assert qt3.value == 50 + assert qt3.unit == "m^2 s^-2" + + result = qt1 * 2 + print(f"{qt1} * {2} = {result}") + assert result.value == 20 + assert result.unit == "m s^-1" + + result1 = 2 * qt1 + print(f"{2} * {qt1} = {result1}") + assert result1.value == 20 + assert result1.unit == "m s^-1" + + q3 = qt1 / qt2 + + print(f"{qt1} / {qt2} = {q3}") + assert q3.value == 2 + assert q3.unit == "" + + result3 = qt1 / 2 + print(f"{qt1} / {2} = {qt1 / 2}") + assert result3.value == 5 + assert result3.unit == "m s^-1" + + qa3 = qt1 + qt2 + + print(f"{qt1} + {qt2} = {qa3}") + assert qa3.value == 15 + assert qa3.unit == "m s^-1" + + try: + result5 = qt1 + 2 + print(f"{qt1} + {2} = {result5}") + except ValueError as ve: + print(ve) + + try: + result6 = 2 + qt1 + print(f"{2} + {qt1} = {result6}") + except ValueError as ve: + print(ve) + + qs3 = qt1 - qt2 + + print(f"{qt1} - {qt2} = {qs3}") + assert qs3.value == 5 + assert qs3.unit == "m s^-1" + + try: + result7 = qt1 - 2 + print(f"{qt1} - {2} = {result7}") + except ValueError as ve: + print(ve) + + try: + result8 = 2 - qt1 + print(f"{2} - {qt1} = {result8}") + except ValueError as ve: + print(ve) + + +def testing_properties(): + print(f"{'*' * 25} {testing_properties.__name__} {'*' * 25}") + + v = q.Quantity(1, "cm s^-1") + print(f"value = {v.value}") + print(f"unit = {v.unit}") + print(f"si value = {v.si_value}") + print(f"si unit = {v.si_unit}") + print(f"is dimensionless? = {v.is_dimension_less()}") + print(f"dimensions = {v.get_dimensions_list()}") + + qt1 = q.Quantity(10, "m s^-1") + qt2 = q.Quantity(5, "m s^-1") + + +# if __name__ == "__main__": + +# testing_dimensions() +# testing_multipliers() +# testing_to_systems() +# testing_arithmetic_operators() +# testing_properties +# +# x = q.Quantity(1, "ft") +# print(f"User unit: {x._unit.user_unit}, multiplier: {x._unit.si_factor}, reduced_si_unit: {x._unit.si_unit}, si_value: {x.si_value}") From b0f2f790224debcf557123de44b4dbd4bc60b655 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Fri, 28 Oct 2022 20:24:43 +0530 Subject: [PATCH 05/40] Updated __rsub__ and __pow__method --- src/ansys/fluent/core/quantity.py | 12 ++---------- tests/test_quantity.py | 16 ++++++++-------- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index a00089629895..0312eefc1f26 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -490,7 +490,7 @@ def _get_si_unit(self, other, func): temp_unit = get_si_unit_from_dim(temp_dim) return temp_unit - def pow(self, exponent): + def __pow__(self, exponent): new_dims = list( map(lambda x: x * exponent if x != 0 else x, self.get_dimensions_list()) ) @@ -551,12 +551,4 @@ def __sub__(self, other): return Quantity(temp_value, self.si_unit) def __rsub__(self, other): - if isinstance(other, Quantity): - temp_value = other.si_value - self.si_value - elif self.is_dimension_less() and ( - isinstance(other, int) or isinstance(other, float) - ): - temp_value = other - self.si_value - else: - raise ValueError(f"Quantity{(self.value, self.unit)} is not dimensionless.") - return Quantity(temp_value, self.si_unit) + return self.__sub__(other) diff --git a/tests/test_quantity.py b/tests/test_quantity.py index 98daf06f23a1..387e6305c38f 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -426,12 +426,12 @@ def testing_properties(): # if __name__ == "__main__": - -# testing_dimensions() -# testing_multipliers() -# testing_to_systems() -# testing_arithmetic_operators() -# testing_properties # -# x = q.Quantity(1, "ft") -# print(f"User unit: {x._unit.user_unit}, multiplier: {x._unit.si_factor}, reduced_si_unit: {x._unit.si_unit}, si_value: {x.si_value}") +# testing_dimensions() +# testing_multipliers() +# testing_to_systems() +# testing_arithmetic_operators() +# testing_properties +# +# x = q.Quantity(1, "ft") +# print(f"User unit: {x._unit.user_unit}, multiplier: {x._unit.si_factor}, reduced_si_unit: {x._unit.si_unit}, si_value: {x.si_value}") From b31b755cb3dcfae3df80de557478b009f2f46693 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Fri, 28 Oct 2022 21:23:15 +0530 Subject: [PATCH 06/40] Updated __rsub__ method --- src/ansys/fluent/core/quantity.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 0312eefc1f26..5ecc92b66c4d 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -551,4 +551,4 @@ def __sub__(self, other): return Quantity(temp_value, self.si_unit) def __rsub__(self, other): - return self.__sub__(other) + return Quantity(other, "") - self From 55a34185a7dd0670f83d5937fb6f2f081ed65eb2 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Mon, 31 Oct 2022 13:53:57 +0530 Subject: [PATCH 07/40] Dimensions as float --- src/ansys/fluent/core/quantity.py | 28 +++++----- tests/test_quantity.py | 89 ++++++++++++++++++++----------- 2 files changed, 71 insertions(+), 46 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 5ecc92b66c4d..92130d33d0d4 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -222,9 +222,9 @@ def _reduce_to_si_unit(self, unit_str): if "^" in term: term_split = term.split("^") if term_split[0] in term_power_dict.keys(): - term_power_dict[term_split[0]] += int(term_split[1]) + term_power_dict[term_split[0]] += float(term_split[1]) else: - term_power_dict[term_split[0]] = int(term_split[1]) + term_power_dict[term_split[0]] = float(term_split[1]) else: if term in term_power_dict.keys(): term_power_dict[term] += 1 @@ -251,7 +251,7 @@ def _compute_multipliers_and_offsets(self, unit_str, power): term_power = 1 if "^" in unit_str: - unit_str, term_power = unit_str[: unit_str.index("^")], int( + unit_str, term_power = unit_str[: unit_str.index("^")], float( unit_str[unit_str.index("^") + 1 :] ) @@ -299,16 +299,16 @@ def _compute_multipliers_and_offsets(self, unit_str, power): class Dimension(object): def __init__(self, unit_str): self._dimensions = { - "M": 0, - "L": 0, - "T": 0, - "K": 0, - "A": 0, - "mol": 0, - "cd": 0, - "Angle": 0, - "sr": 0, - "": 0, + "M": 0.0, + "L": 0.0, + "T": 0.0, + "K": 0.0, + "A": 0.0, + "mol": 0.0, + "cd": 0.0, + "Angle": 0.0, + "sr": 0.0, + "": 0.0, } self._parser(unit_str) @@ -331,7 +331,7 @@ def _parser(self, unit, power=1): term_power = 1 if "^" in term: - unit_str, term_power = term[: term.index("^")], int( + unit_str, term_power = term[: term.index("^")], float( term[term.index("^") + 1 :] ) has_multiplier = not ( diff --git a/tests/test_quantity.py b/tests/test_quantity.py index 387e6305c38f..06521dd8fed5 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -1,3 +1,5 @@ +import math + import pytest import ansys.fluent.core.quantity as q @@ -31,17 +33,28 @@ def test_value_unit_4(): def test_dims_5(): v = q.Quantity(1.0, "kPa") - assert v.get_dimensions_list() == [1, -1, -2, 0, 0, 0, 0, 0, 0, 0] + assert v.get_dimensions_list() == [ + 1.0, + -1.0, + -2.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + ] def test_dims_6(): v = q.Quantity(1.0, "ft") - assert v.get_dimensions_list() == [0, 1, 0, 0, 0, 0, 0, 0, 0, 0] + assert v.get_dimensions_list() == [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] def test_dims_7(): v = q.Quantity(1.0, "m m^-1") - assert v.get_dimensions_list() == [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + assert v.get_dimensions_list() == [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] def test_to_8(): @@ -274,19 +287,19 @@ def dim_test(unit_str, dim_list): print(f"{unit_str} : {qt.get_dimensions_list()}") assert qt.get_dimensions_list() == dim_list - dim_test("m", [0, 1, 0, 0, 0, 0, 0, 0, 0, 0]) - dim_test("m s^-1", [0, 1, -1, 0, 0, 0, 0, 0, 0, 0]) - dim_test("kg m s^-2 m^-2", [1, -1, -2, 0, 0, 0, 0, 0, 0, 0]) - dim_test("Pa", [1, -1, -2, 0, 0, 0, 0, 0, 0, 0]) - dim_test("kPa", [1, -1, -2, 0, 0, 0, 0, 0, 0, 0]) - dim_test("Pa^2", [2, -2, -4, 0, 0, 0, 0, 0, 0, 0]) - dim_test("daPa", [1, -1, -2, 0, 0, 0, 0, 0, 0, 0]) - dim_test("MPa", [1, -1, -2, 0, 0, 0, 0, 0, 0, 0]) - dim_test("kPa^2", [2, -2, -4, 0, 0, 0, 0, 0, 0, 0]) - dim_test("slug in^-1 s^-1", [1, -1, -1, 0, 0, 0, 0, 0, 0, 0]) - dim_test("radian", [0, 0, 0, 0, 1, 0, 0, 0, 0, 0]) - dim_test("ohm", [1, 2, -3, 0, 0, 0, 0, -2, 0, 0]) - dim_test("lb cm s^-2", [1, 1, -2, 0, 0, 0, 0, 0, 0, 0]) + dim_test("m", [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]) + dim_test("m s^-1", [0.0, 1.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]) + dim_test("kg m s^-2 m^-2", [1.0, -1.0, -2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]) + dim_test("Pa", [1.0, -1.0, -2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]) + dim_test("kPa", [1.0, -1.0, -2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]) + dim_test("Pa^2", [2.0, -2.0, -4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]) + dim_test("daPa", [1.0, -1.0, -2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]) + dim_test("MPa", [1.0, -1.0, -2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]) + dim_test("kPa^2", [2.0, -2.0, -4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]) + dim_test("slug in^-1 s^-1", [1.0, -1.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]) + dim_test("radian", [0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0]) + dim_test("ohm", [1.0, 2.0, -3.0, 0.0, 0.0, 0.0, 0.0, -2.0, 0.0, 0.0]) + dim_test("lb cm s^-2", [1.0, 1.0, -2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]) print("-" * 75) @@ -350,17 +363,17 @@ def testing_arithmetic_operators(): print(f"{qt1} * {qt2} = {qt3}") assert qt3.value == 50 - assert qt3.unit == "m^2 s^-2" + assert qt3.unit == "m^2.0 s^-2.0" result = qt1 * 2 print(f"{qt1} * {2} = {result}") assert result.value == 20 - assert result.unit == "m s^-1" + assert result.unit == "m s^-1.0" result1 = 2 * qt1 print(f"{2} * {qt1} = {result1}") assert result1.value == 20 - assert result1.unit == "m s^-1" + assert result1.unit == "m s^-1.0" q3 = qt1 / qt2 @@ -371,13 +384,13 @@ def testing_arithmetic_operators(): result3 = qt1 / 2 print(f"{qt1} / {2} = {qt1 / 2}") assert result3.value == 5 - assert result3.unit == "m s^-1" + assert result3.unit == "m s^-1.0" qa3 = qt1 + qt2 print(f"{qt1} + {qt2} = {qa3}") assert qa3.value == 15 - assert qa3.unit == "m s^-1" + assert qa3.unit == "m s^-1.0" try: result5 = qt1 + 2 @@ -395,7 +408,7 @@ def testing_arithmetic_operators(): print(f"{qt1} - {qt2} = {qs3}") assert qs3.value == 5 - assert qs3.unit == "m s^-1" + assert qs3.unit == "m s^-1.0" try: result7 = qt1 - 2 @@ -425,13 +438,25 @@ def testing_properties(): qt2 = q.Quantity(5, "m s^-1") -# if __name__ == "__main__": -# -# testing_dimensions() -# testing_multipliers() -# testing_to_systems() -# testing_arithmetic_operators() -# testing_properties -# -# x = q.Quantity(1, "ft") -# print(f"User unit: {x._unit.user_unit}, multiplier: {x._unit.si_factor}, reduced_si_unit: {x._unit.si_unit}, si_value: {x.si_value}") +if __name__ == "__main__": + + testing_dimensions() + testing_multipliers() + testing_to_systems() + testing_arithmetic_operators() + testing_properties + + x = q.Quantity(1, "ft") + print( + f"User unit: {x._unit.user_unit}, multiplier: {x._unit.si_factor}, reduced_si_unit: {x._unit.si_unit}, si_value: {x.si_value}" + ) + + deg = q.Quantity(90, "degree") + rad = q.Quantity(math.pi / 2, "radian") + + print(math.sin(deg)) + print(math.sin(rad)) + + print(deg.to("radian")) + print(math.sin(deg.to("radian"))) + print(math.sin(1.5707963267948966)) From c8c8abf6ae139994d4916111a00c93918177169e Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Mon, 31 Oct 2022 14:33:22 +0530 Subject: [PATCH 08/40] Added test for proportionality constants --- tests/test_quantity.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/tests/test_quantity.py b/tests/test_quantity.py index 06521dd8fed5..ec16875746f2 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -279,6 +279,15 @@ def test_to_39(): assert convert.unit == "J m^-2" +def test_prop_constants_40(): + c = q.Quantity(10.5, "s^-0.5") + x = q.Quantity(5.7, "m") + t = q.Quantity(4.8, "s") + + y = c * x * t + assert y == 287.28 + + def testing_dimensions(): print(f"{'*' * 25} {testing_dimensions.__name__} {'*' * 25}") @@ -454,9 +463,5 @@ def testing_properties(): deg = q.Quantity(90, "degree") rad = q.Quantity(math.pi / 2, "radian") - print(math.sin(deg)) - print(math.sin(rad)) - - print(deg.to("radian")) - print(math.sin(deg.to("radian"))) - print(math.sin(1.5707963267948966)) + print(math.sin(deg.si_value)) # prints 1.0 + print(math.sin(rad)) # prints 1.0 From b0b0c49f6b9259aff673f261bffed03239ee1f8b Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Mon, 31 Oct 2022 15:08:57 +0530 Subject: [PATCH 09/40] Used standard symbols for each quantity --- src/ansys/fluent/core/quantity.py | 46 +++++++++++++++---------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 92130d33d0d4..b53c33f6485e 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -13,14 +13,14 @@ class UnitsTable(object): "ft": "L", "in": "L", "s": "T", - "A": "A", - "K": "K", - "C": "K", - "F": "K", - "R": "K", - "mol": "mol", - "cd": "cd", - "sr": "sr", + "A": "I", + "K": "Temp", + "C": "Temp", + "F": "Temp", + "R": "Temp", + "mol": "n", + "cd": "Iv", + "sr": "SAngle", "radian": "Angle", "degree": "Angle", } @@ -81,12 +81,12 @@ class UnitsTable(object): ("Mass", "M"), ("Length", "L"), ("Time", "T"), - ("Temperature", "K"), + ("Temperature", "Temp"), ("Angle", "Angle"), - ("ChemicalAmount", "mol"), - ("Light", "cd"), - ("Current", "A"), - ("SolidAngle", "sr"), + ("ChemicalAmount", "n"), + ("Light", "Iv"), + ("Current", "I"), + ("SolidAngle", "SAngle"), ("", ""), ] ) @@ -302,12 +302,12 @@ def __init__(self, unit_str): "M": 0.0, "L": 0.0, "T": 0.0, - "K": 0.0, - "A": 0.0, - "mol": 0.0, - "cd": 0.0, + "Temp": 0.0, + "I": 0.0, + "n": 0.0, + "Iv": 0.0, "Angle": 0.0, - "sr": 0.0, + "SAngle": 0.0, "": 0.0, } self._parser(unit_str) @@ -367,12 +367,12 @@ def get_si_unit_from_dim(dim_list): "M": "kg", "L": "m", "T": "s", - "K": "K", - "A": "A", - "mol": "mol", - "cd": "cd", + "Temp": "K", + "I": "A", + "n": "mol", + "Iv": "cd", "Angle": "radian", - "sr": "sr", + "SAngle": "sr", "": "", } for key, power in zip(UnitsTable._dimension_order.values(), dim_list): From c73937f9b2198b185c6a0acd1ea9aebdeb80447e Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Tue, 1 Nov 2022 10:13:31 +0530 Subject: [PATCH 10/40] Changed N and J symbols --- src/ansys/fluent/core/quantity.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index b53c33f6485e..b45ab0c30af3 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -18,8 +18,8 @@ class UnitsTable(object): "C": "Temp", "F": "Temp", "R": "Temp", - "mol": "n", - "cd": "Iv", + "mol": "N", + "cd": "J", "sr": "SAngle", "radian": "Angle", "degree": "Angle", @@ -83,8 +83,8 @@ class UnitsTable(object): ("Time", "T"), ("Temperature", "Temp"), ("Angle", "Angle"), - ("ChemicalAmount", "n"), - ("Light", "Iv"), + ("ChemicalAmount", "N"), + ("Light", "J"), ("Current", "I"), ("SolidAngle", "SAngle"), ("", ""), @@ -304,8 +304,8 @@ def __init__(self, unit_str): "T": 0.0, "Temp": 0.0, "I": 0.0, - "n": 0.0, - "Iv": 0.0, + "N": 0.0, + "J": 0.0, "Angle": 0.0, "SAngle": 0.0, "": 0.0, @@ -369,8 +369,8 @@ def get_si_unit_from_dim(dim_list): "T": "s", "Temp": "K", "I": "A", - "n": "mol", - "Iv": "cd", + "N": "mol", + "J": "cd", "Angle": "radian", "SAngle": "sr", "": "", From eb49add7143867bc4b99a67c20076ca36bcb7b71 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Tue, 1 Nov 2022 10:21:41 +0530 Subject: [PATCH 11/40] Renamed UnitTable to _UnitTable --- src/ansys/fluent/core/quantity.py | 84 +++++++++++++++---------------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index b45ab0c30af3..326e2a75d4aa 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -1,8 +1,8 @@ from collections import OrderedDict -class UnitsTable(object): - _fundamental_units = { +class _UnitsTable(object): + fundamental_units = { "kg": "M", "g": "M", "lb": "M", @@ -25,7 +25,7 @@ class UnitsTable(object): "degree": "Angle", } - _derived_units = { + derived_units = { "N": "kg m s^-2", "Pa": "N m^-2", "W": "N m s^-1", @@ -47,13 +47,13 @@ class UnitsTable(object): "Hz": "s^-1", } - _derived_units_with_conversion_factor = { + derived_units_with_conversion_factor = { "l": (0.001, "m^3"), "gal": (0.0037854117839999993, "m^3"), "BTU": (1055.056, "J"), } - _multipliers = { + multipliers = { "d": 10**-1, "c": 10**-2, "m": 10**-3, @@ -76,7 +76,7 @@ class UnitsTable(object): "Y": 10**24, } - _dimension_order = OrderedDict( + dimension_order = OrderedDict( [ ("Mass", "M"), ("Length", "L"), @@ -91,7 +91,7 @@ class UnitsTable(object): ] ) - _conversion_map = { + conversion_map = { "kg": 1, "g": 0.001, "lb": 0.45359237, @@ -114,7 +114,7 @@ class UnitsTable(object): "R": 0.5555555555555556, } - _si_map = { + si_map = { "kg": "kg", "g": "kg", "lb": "kg", @@ -138,14 +138,14 @@ class UnitsTable(object): "": "", } - _offset_conversions = { + offset_conversions = { "K": 1, "C": 274.15, "F": 255.92777777777778, "R": 0.5555555555555556, } - _offset_dict = { + offset_dict = { "K": {"C": 274.15, "F": 255.92777777777778, "R": 0.5555555555555556}, "C": {"K": -272.15, "F": -17.2222222222222, "R": -272.59444444444443}, "F": {"C": 33.79999999999993, "K": -457.87, "R": -458.67}, @@ -155,8 +155,8 @@ class UnitsTable(object): def get_si_conversion_factor(unit_str): return ( - UnitsTable._conversion_map[unit_str] - if unit_str in UnitsTable._conversion_map.keys() + _UnitsTable.conversion_map[unit_str] + if unit_str in _UnitsTable.conversion_map.keys() else 1 ) @@ -164,15 +164,15 @@ def get_si_conversion_factor(unit_str): def filter_multiplier(unit_str, predicate=None): result = False matched = "" - for item in UnitsTable._multipliers.keys(): + for item in _UnitsTable.multipliers.keys(): result = predicate(item, unit_str) if predicate else item == unit_str if result: matched = item result = True temp_unit_str = unit_str[len(item) :] if ( - temp_unit_str in UnitsTable._fundamental_units - or temp_unit_str in UnitsTable._derived_units + temp_unit_str in _UnitsTable.fundamental_units + or temp_unit_str in _UnitsTable.derived_units ): break else: @@ -196,7 +196,7 @@ def __init__(self, unit_str): self._si_multiplier = 1 self._si_offset = 0 self._si_unit = "" - self._compute_multipliers_and_offsets(unit_str, 1) + self._computemultipliers_and_offsets(unit_str, 1) self._reduce_to_si_unit(self._si_unit) @property @@ -240,7 +240,7 @@ def _reduce_to_si_unit(self, unit_str): else: self._si_unit += spacechar + key - def _compute_multipliers_and_offsets(self, unit_str, power): + def _computemultipliers_and_offsets(self, unit_str, power): if len(unit_str) == 0: return @@ -257,8 +257,8 @@ def _compute_multipliers_and_offsets(self, unit_str, power): term_power *= power has_multiplier = not ( - unit_str in UnitsTable._fundamental_units - or unit_str in UnitsTable._derived_units + unit_str in _UnitsTable.fundamental_units + or unit_str in _UnitsTable.derived_units ) if has_multiplier: @@ -267,32 +267,32 @@ def _compute_multipliers_and_offsets(self, unit_str, power): ) if len(prefix): - self._si_multiplier *= UnitsTable._multipliers[prefix] ** term_power + self._si_multiplier *= _UnitsTable.multipliers[prefix] ** term_power unit_str = remove_multiplier(unit_str) - if unit_str in UnitsTable._fundamental_units: + if unit_str in _UnitsTable.fundamental_units: spacechar = " " if len(self._si_unit) > 0 else "" if term_power > 1 or term_power < 0: self._si_unit += ( - spacechar + UnitsTable._si_map[unit_str] + "^" + str(term_power) + spacechar + _UnitsTable.si_map[unit_str] + "^" + str(term_power) ) else: - self._si_unit += spacechar + UnitsTable._si_map[unit_str] + self._si_unit += spacechar + _UnitsTable.si_map[unit_str] self._si_multiplier *= get_si_conversion_factor(unit_str) ** term_power - elif unit_str in UnitsTable._derived_units_with_conversion_factor: + elif unit_str in _UnitsTable.derived_units_with_conversion_factor: ( conversion_factor, unit_str, - ) = UnitsTable._derived_units_with_conversion_factor[unit_str] + ) = _UnitsTable.derived_units_with_conversion_factor[unit_str] self._si_multiplier *= conversion_factor**term_power - self._compute_multipliers_and_offsets(unit_str, term_power) - elif unit_str in UnitsTable._derived_units: - self._compute_multipliers_and_offsets( - UnitsTable._derived_units[unit_str], term_power + self._computemultipliers_and_offsets(unit_str, term_power) + elif unit_str in _UnitsTable.derived_units: + self._computemultipliers_and_offsets( + _UnitsTable.derived_units[unit_str], term_power ) @@ -324,8 +324,8 @@ def _parser(self, unit, power=1): for term in unit_list: unit_dim = "" has_multiplier = not ( - term in UnitsTable._fundamental_units - or term in UnitsTable._derived_units + term in _UnitsTable.fundamental_units + or term in _UnitsTable.derived_units ) unit_str = remove_multiplier(term) if has_multiplier else term term_power = 1 @@ -335,8 +335,8 @@ def _parser(self, unit, power=1): term[term.index("^") + 1 :] ) has_multiplier = not ( - unit_str in UnitsTable._fundamental_units - or unit_str in UnitsTable._derived_units + unit_str in _UnitsTable.fundamental_units + or unit_str in _UnitsTable.derived_units ) unit_str = remove_multiplier(unit_str) if has_multiplier else unit_str @@ -350,12 +350,12 @@ def get_dimensions_dict(self): return self._dimensions def _get_dim(self, unit_str, power): - if unit_str in UnitsTable._fundamental_units: - return {UnitsTable._fundamental_units[unit_str]: power} - elif unit_str in UnitsTable._derived_units: - self._parser(UnitsTable._derived_units[unit_str], power) - elif unit_str in UnitsTable._derived_units_with_conversion_factor: - _, unit_str = UnitsTable._derived_units_with_conversion_factor[unit_str] + if unit_str in _UnitsTable.fundamental_units: + return {_UnitsTable.fundamental_units[unit_str]: power} + elif unit_str in _UnitsTable.derived_units: + self._parser(_UnitsTable.derived_units[unit_str], power) + elif unit_str in _UnitsTable.derived_units_with_conversion_factor: + _, unit_str = _UnitsTable.derived_units_with_conversion_factor[unit_str] self._parser(unit_str, power) else: raise ValueError("Not implemented") @@ -375,7 +375,7 @@ def get_si_unit_from_dim(dim_list): "SAngle": "sr", "": "", } - for key, power in zip(UnitsTable._dimension_order.values(), dim_list): + for key, power in zip(_UnitsTable.dimension_order.values(), dim_list): unit_str = dim_to_unit_map[key] spacechar = " " if len(si_unit) > 0 else "" if power > 1 or power < 0: @@ -455,8 +455,8 @@ def is_dimension_less(self): def get_dimensions_list(self): dims = self._dimension.get_dimensions_dict() return [ - dims[UnitsTable._dimension_order[key]] - for key in UnitsTable._dimension_order.keys() + dims[_UnitsTable.dimension_order[key]] + for key in _UnitsTable.dimension_order.keys() ] def to(self, to_unit_str): From bb35453c22a35e5ed9c41497aadb7027e55e5f47 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Tue, 1 Nov 2022 10:40:14 +0530 Subject: [PATCH 12/40] Overloaded float operator --- src/ansys/fluent/core/quantity.py | 11 +++++------ tests/test_quantity.py | 13 ++++++++++++- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 326e2a75d4aa..9ccb831fb0a9 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -385,7 +385,7 @@ def get_si_unit_from_dim(dim_list): return si_unit -class Quantity(float): +class Quantity: """This class instantiates physical quantities using their real values and units. @@ -416,12 +416,8 @@ class Quantity(float): consistency in all arithmetic operations. """ - def __new__(cls, real_value, units_string): - return float.__new__(cls, real_value) - def __init__(self, real_value, unit_str): - float.__init__(real_value) - self._value = self.__float__() + self._value = float(real_value) self._unit = Unit(unit_str) self._dimension = Dimension(unit_str) @@ -498,6 +494,9 @@ def __pow__(self, exponent): new_si_value = pow(self.si_value, exponent) return Quantity(new_si_value, new_si_unit) + def __float__(self): + return float(self.si_value) + def __str__(self): return f'({self.value}, "{self.unit}")' diff --git a/tests/test_quantity.py b/tests/test_quantity.py index ec16875746f2..4e7498fe0b72 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -285,7 +285,18 @@ def test_prop_constants_40(): t = q.Quantity(4.8, "s") y = c * x * t - assert y == 287.28 + assert float(y) == 287.28 + + +def test_math_fun(): + deg = q.Quantity(90, "degree") + assert math.sin(deg) == 1.0 + + rad = q.Quantity(math.pi / 2, "radian") + assert math.sin(rad) == 1.0 + + root = q.Quantity(100.0, "") + assert math.sqrt(root) == 10.0 def testing_dimensions(): From 64ebfeea498f96ba040ce2ce0d51809268db14fd Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Tue, 1 Nov 2022 13:03:25 +0530 Subject: [PATCH 13/40] Derived from float and updated __repr__ method --- src/ansys/fluent/core/quantity.py | 12 +++++++----- tests/test_quantity.py | 8 +------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 9ccb831fb0a9..d025889045f7 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -385,7 +385,7 @@ def get_si_unit_from_dim(dim_list): return si_unit -class Quantity: +class Quantity(float): """This class instantiates physical quantities using their real values and units. @@ -420,6 +420,11 @@ def __init__(self, real_value, unit_str): self._value = float(real_value) self._unit = Unit(unit_str) self._dimension = Dimension(unit_str) + float.__init__(self.si_value) + + def __new__(cls, real_value, unit_str): + _unit = Unit(unit_str) + return float.__new__(cls, _unit.si_factor * real_value) @property def value(self): @@ -494,14 +499,11 @@ def __pow__(self, exponent): new_si_value = pow(self.si_value, exponent) return Quantity(new_si_value, new_si_unit) - def __float__(self): - return float(self.si_value) - def __str__(self): return f'({self.value}, "{self.unit}")' def __repr__(self): - return f'(Quantity ({self.value}, "{self.unit}"))' + return f'Quantity ({self.value}, "{self.unit}")' def __mul__(self, other): if isinstance(other, Quantity): diff --git a/tests/test_quantity.py b/tests/test_quantity.py index 4e7498fe0b72..600a3313c371 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -459,7 +459,7 @@ def testing_properties(): if __name__ == "__main__": - + test_value_unit_1() testing_dimensions() testing_multipliers() testing_to_systems() @@ -470,9 +470,3 @@ def testing_properties(): print( f"User unit: {x._unit.user_unit}, multiplier: {x._unit.si_factor}, reduced_si_unit: {x._unit.si_unit}, si_value: {x.si_value}" ) - - deg = q.Quantity(90, "degree") - rad = q.Quantity(math.pi / 2, "radian") - - print(math.sin(deg.si_value)) # prints 1.0 - print(math.sin(rad)) # prints 1.0 From e7806fa36a69d18ac399234c0455648d1d74d447 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Tue, 1 Nov 2022 13:14:41 +0530 Subject: [PATCH 14/40] Renamed get_dimensions_dict() to as_dict() --- src/ansys/fluent/core/quantity.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index d025889045f7..8c657c752d6a 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -346,7 +346,7 @@ def _parser(self, unit, power=1): if unit_dim != None: self._add_or_update_dimension(unit_dim) - def get_dimensions_dict(self): + def as_dict(self): return self._dimensions def _get_dim(self, unit_str, power): @@ -454,7 +454,7 @@ def is_dimension_less(self): return all([value == 0 for value in self.get_dimensions_list()]) def get_dimensions_list(self): - dims = self._dimension.get_dimensions_dict() + dims = self._dimension.as_dict() return [ dims[_UnitsTable.dimension_order[key]] for key in _UnitsTable.dimension_order.keys() @@ -466,10 +466,7 @@ def to(self, to_unit_str): curr_unit_dim_obj = self.dimension to_unit_dim_obj = temp_quantity.dimension - if ( - curr_unit_dim_obj.get_dimensions_dict() - != to_unit_dim_obj.get_dimensions_dict() - ): + if curr_unit_dim_obj.as_dict() != to_unit_dim_obj.as_dict(): raise ValueError( f"Incompatible conversion from {self.unit} : to {to_unit_str}" ) From 44fd971cd81c510a1a78a0b443a104f44701004f Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Tue, 1 Nov 2022 13:31:16 +0530 Subject: [PATCH 15/40] Now si_value and si_unit is private --- src/ansys/fluent/core/quantity.py | 28 ++++++++++++++-------------- tests/test_quantity.py | 6 +++--- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 8c657c752d6a..ca766da816ba 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -420,7 +420,7 @@ def __init__(self, real_value, unit_str): self._value = float(real_value) self._unit = Unit(unit_str) self._dimension = Dimension(unit_str) - float.__init__(self.si_value) + float.__init__(self._si_value) def __new__(cls, real_value, unit_str): _unit = Unit(unit_str) @@ -443,11 +443,11 @@ def dimension(self): return self._dimension @property - def si_value(self): + def _si_value(self): return self._unit.si_factor * self._value @property - def si_unit(self): + def _si_unit(self): return self._unit.si_unit def is_dimension_less(self): @@ -493,7 +493,7 @@ def __pow__(self, exponent): map(lambda x: x * exponent if x != 0 else x, self.get_dimensions_list()) ) new_si_unit = get_si_unit_from_dim(new_dims) - new_si_value = pow(self.si_value, exponent) + new_si_value = pow(self._si_value, exponent) return Quantity(new_si_value, new_si_unit) def __str__(self): @@ -504,11 +504,11 @@ def __repr__(self): def __mul__(self, other): if isinstance(other, Quantity): - temp_value = self.si_value * other.si_value + temp_value = self._si_value * other._si_value temp_unit = self._get_si_unit(other, lambda x, y: x + y) return Quantity(temp_value, temp_unit) elif isinstance(other, int) or isinstance(other, float): - temp = Quantity(self.si_value * other, self.si_unit) + temp = Quantity(self._si_value * other, self._si_unit) return temp def __rmul__(self, other): @@ -516,37 +516,37 @@ def __rmul__(self, other): def __truediv__(self, other): if isinstance(other, Quantity): - temp_value = self.si_value / other.si_value + temp_value = self._si_value / other._si_value temp_unit = self._get_si_unit(other, lambda x, y: x - y) return Quantity(temp_value, temp_unit) elif isinstance(other, int) or isinstance(other, float): - temp = Quantity(self.si_value / other, self.si_unit) + temp = Quantity(self._si_value / other, self._si_unit) return temp def __add__(self, other): if isinstance(other, Quantity): - temp_value = self.si_value + other.si_value + temp_value = self._si_value + other._si_value elif self.is_dimension_less() and ( isinstance(other, int) or isinstance(other, float) ): - temp_value = self.si_value + other + temp_value = self._si_value + other else: raise ValueError(f"Quantity{(self.value, self.unit)} is not dimensionless.") - return Quantity(temp_value, self.si_unit) + return Quantity(temp_value, self._si_unit) def __radd__(self, other): return self.__add__(other) def __sub__(self, other): if isinstance(other, Quantity): - temp_value = self.si_value - other.si_value + temp_value = self._si_value - other._si_value elif self.is_dimension_less() and ( isinstance(other, int) or isinstance(other, float) ): - temp_value = self.si_value - other + temp_value = self._si_value - other else: raise ValueError(f"Quantity{(self.value, self.unit)} is not dimensionless.") - return Quantity(temp_value, self.si_unit) + return Quantity(temp_value, self._si_unit) def __rsub__(self, other): return Quantity(other, "") - self diff --git a/tests/test_quantity.py b/tests/test_quantity.py index 600a3313c371..81705e947573 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -449,8 +449,8 @@ def testing_properties(): v = q.Quantity(1, "cm s^-1") print(f"value = {v.value}") print(f"unit = {v.unit}") - print(f"si value = {v.si_value}") - print(f"si unit = {v.si_unit}") + print(f"si value = {v._si_value}") + print(f"si unit = {v._si_unit}") print(f"is dimensionless? = {v.is_dimension_less()}") print(f"dimensions = {v.get_dimensions_list()}") @@ -468,5 +468,5 @@ def testing_properties(): x = q.Quantity(1, "ft") print( - f"User unit: {x._unit.user_unit}, multiplier: {x._unit.si_factor}, reduced_si_unit: {x._unit.si_unit}, si_value: {x.si_value}" + f"User unit: {x._unit.user_unit}, multiplier: {x._unit.si_factor}, reduced_si_unit: {x._unit.si_unit}, si_value: {x._si_value}" ) From 6c3c7b23178515460641c458e56e9212c98dab7b Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Tue, 1 Nov 2022 13:49:15 +0530 Subject: [PATCH 16/40] Updated __add__ and __sub__ method --- src/ansys/fluent/core/quantity.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index ca766da816ba..7458fbf5057f 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -524,29 +524,33 @@ def __truediv__(self, other): return temp def __add__(self, other): - if isinstance(other, Quantity): + if isinstance(other, Quantity) and ( + self.get_dimensions_list() == other.get_dimensions_list() + ): temp_value = self._si_value + other._si_value elif self.is_dimension_less() and ( isinstance(other, int) or isinstance(other, float) ): temp_value = self._si_value + other else: - raise ValueError(f"Quantity{(self.value, self.unit)} is not dimensionless.") + raise ValueError("Incompatible dimensions.") return Quantity(temp_value, self._si_unit) def __radd__(self, other): return self.__add__(other) def __sub__(self, other): - if isinstance(other, Quantity): + if isinstance(other, Quantity) and ( + self.get_dimensions_list() == other.get_dimensions_list() + ): temp_value = self._si_value - other._si_value elif self.is_dimension_less() and ( isinstance(other, int) or isinstance(other, float) ): temp_value = self._si_value - other else: - raise ValueError(f"Quantity{(self.value, self.unit)} is not dimensionless.") + raise ValueError("Incompatible dimensions.") return Quantity(temp_value, self._si_unit) def __rsub__(self, other): - return Quantity(other, "") - self + return self.__sub__(other) From 61647829f789f903213cb55de3f98748187585f7 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Tue, 1 Nov 2022 14:37:35 +0530 Subject: [PATCH 17/40] si_value and si_unit is now private members --- src/ansys/fluent/core/quantity.py | 13 +++---------- tests/test_quantity.py | 12 ++++++++++++ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 7458fbf5057f..83aa54f79f76 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -420,6 +420,8 @@ def __init__(self, real_value, unit_str): self._value = float(real_value) self._unit = Unit(unit_str) self._dimension = Dimension(unit_str) + self._si_value = self._unit.si_factor * self._value + self._si_unit = self._unit.si_unit float.__init__(self._si_value) def __new__(cls, real_value, unit_str): @@ -442,14 +444,6 @@ def value(self, val): def dimension(self): return self._dimension - @property - def _si_value(self): - return self._unit.si_factor * self._value - - @property - def _si_unit(self): - return self._unit.si_unit - def is_dimension_less(self): return all([value == 0 for value in self.get_dimensions_list()]) @@ -508,8 +502,7 @@ def __mul__(self, other): temp_unit = self._get_si_unit(other, lambda x, y: x + y) return Quantity(temp_value, temp_unit) elif isinstance(other, int) or isinstance(other, float): - temp = Quantity(self._si_value * other, self._si_unit) - return temp + return Quantity(self._si_value * other, self._si_unit) def __rmul__(self, other): return self.__mul__(other) diff --git a/tests/test_quantity.py b/tests/test_quantity.py index 81705e947573..78c7a21a0021 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -470,3 +470,15 @@ def testing_properties(): print( f"User unit: {x._unit.user_unit}, multiplier: {x._unit.si_factor}, reduced_si_unit: {x._unit.si_unit}, si_value: {x._si_value}" ) + + g = q.Quantity(1, "g") + m = q.Quantity(1, "m") + print(f"{g} * {m} = {g * m}") + print(" ") + print(f"{m} * 2 = {m * 2}") + print(" ") + + l = q.Quantity(1, "cm") + r = q.Quantity(1, "m") + print(f"{l} + {r} = {l + r}") + print(" ") From 19d10cf67f50d1a0644cc82c2d2498414f767bea Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Tue, 1 Nov 2022 19:09:36 +0530 Subject: [PATCH 18/40] Handled unit error --- src/ansys/fluent/core/quantity.py | 13 +++++---- tests/test_quantity.py | 46 +++++++++++++++++-------------- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 83aa54f79f76..07369cf046de 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -183,7 +183,7 @@ def filter_multiplier(unit_str, predicate=None): def remove_multiplier(unit_str): has_multiplier, prefix = filter_multiplier( - unit_str, lambda item, unit: len(unit) > 1 and unit.startswith(item) + unit_str, lambda item, unit: len(unit) > 0.0 and unit.startswith(item) ) if has_multiplier: unit_str = unit_str[len(prefix) :] @@ -235,7 +235,7 @@ def _reduce_to_si_unit(self, unit_str): for key, power in term_power_dict.items(): spacechar = " " if len(self._si_unit) > 0 else "" - if power > 1 or power < 0: + if power > 0.0 or power < 0.0: self._si_unit += spacechar + key + "^" + str(power) else: self._si_unit += spacechar + key @@ -263,7 +263,8 @@ def _computemultipliers_and_offsets(self, unit_str, power): if has_multiplier: _, prefix = filter_multiplier( - unit_str, lambda item, unit: len(unit) > 1 and unit.startswith(item) + unit_str, + lambda item, unit: len(unit) > 0.0 and unit.startswith(item), ) if len(prefix): @@ -274,7 +275,7 @@ def _computemultipliers_and_offsets(self, unit_str, power): if unit_str in _UnitsTable.fundamental_units: spacechar = " " if len(self._si_unit) > 0 else "" - if term_power > 1 or term_power < 0: + if term_power > 0.0 or term_power < 0.0: self._si_unit += ( spacechar + _UnitsTable.si_map[unit_str] + "^" + str(term_power) ) @@ -378,9 +379,9 @@ def get_si_unit_from_dim(dim_list): for key, power in zip(_UnitsTable.dimension_order.values(), dim_list): unit_str = dim_to_unit_map[key] spacechar = " " if len(si_unit) > 0 else "" - if power > 1 or power < 0: + if power > 0.0 or power < 0.0: si_unit += spacechar + unit_str + "^" + str(power) - elif power == 1: + elif power == 1.0: si_unit += spacechar + unit_str return si_unit diff --git a/tests/test_quantity.py b/tests/test_quantity.py index 78c7a21a0021..d68701f97e28 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -285,6 +285,7 @@ def test_prop_constants_40(): t = q.Quantity(4.8, "s") y = c * x * t + print(y) assert float(y) == 287.28 @@ -388,12 +389,12 @@ def testing_arithmetic_operators(): result = qt1 * 2 print(f"{qt1} * {2} = {result}") assert result.value == 20 - assert result.unit == "m s^-1.0" + assert result.unit == "m^1.0 s^-1.0" result1 = 2 * qt1 print(f"{2} * {qt1} = {result1}") assert result1.value == 20 - assert result1.unit == "m s^-1.0" + assert result1.unit == "m^1.0 s^-1.0" q3 = qt1 / qt2 @@ -404,13 +405,13 @@ def testing_arithmetic_operators(): result3 = qt1 / 2 print(f"{qt1} / {2} = {qt1 / 2}") assert result3.value == 5 - assert result3.unit == "m s^-1.0" + assert result3.unit == "m^1.0 s^-1.0" qa3 = qt1 + qt2 print(f"{qt1} + {qt2} = {qa3}") assert qa3.value == 15 - assert qa3.unit == "m s^-1.0" + assert qa3.unit == "m^1.0 s^-1.0" try: result5 = qt1 + 2 @@ -428,7 +429,7 @@ def testing_arithmetic_operators(): print(f"{qt1} - {qt2} = {qs3}") assert qs3.value == 5 - assert qs3.unit == "m s^-1.0" + assert qs3.unit == "m^1.0 s^-1.0" try: result7 = qt1 - 2 @@ -466,19 +467,22 @@ def testing_properties(): testing_arithmetic_operators() testing_properties - x = q.Quantity(1, "ft") - print( - f"User unit: {x._unit.user_unit}, multiplier: {x._unit.si_factor}, reduced_si_unit: {x._unit.si_unit}, si_value: {x._si_value}" - ) - - g = q.Quantity(1, "g") - m = q.Quantity(1, "m") - print(f"{g} * {m} = {g * m}") - print(" ") - print(f"{m} * 2 = {m * 2}") - print(" ") - - l = q.Quantity(1, "cm") - r = q.Quantity(1, "m") - print(f"{l} + {r} = {l + r}") - print(" ") + # x = q.Quantity(1, "ft") + # print( + # f"User unit: {x._unit.user_unit}, multiplier: {x._unit.si_factor}, reduced_si_unit: {x._unit.si_unit}, si_value: {x._si_value}" + # ) + # + # g = q.Quantity(1, "g") + # m = q.Quantity(1, "m") + # print(f"{g} * {m} = {g * m}") + # print(" ") + # print(f"{m} * 2 = {m * 2}") + # print(" ") + # + # l = q.Quantity(1, "cm") + # r = q.Quantity(1, "kg m s") + # print(f"{l} + {r} = {l + r}") + # print(" ") + + # cgs_unit = q.get_unit_from_dim(r.get_dimensions_list()) + # print(cgs_unit) From be20ad54c1467bb05b8553a52657c9dc71effe2d Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Tue, 1 Nov 2022 20:58:40 +0530 Subject: [PATCH 19/40] Implemented UnitSystem class and __eq__ method --- src/ansys/fluent/core/quantity.py | 48 ++++++++++++++++++++ tests/test_quantity.py | 75 ++++++++++++++++++++----------- 2 files changed, 96 insertions(+), 27 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 07369cf046de..86d6b67ff37d 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -386,6 +386,51 @@ def get_si_unit_from_dim(dim_list): return si_unit +class UnitSystem: + + _dim_to_unit_sys_map = { + "M": {"SI": "kg", "CGS": "g", "BTU": "slug"}, + "L": {"SI": "m", "CGS": "cm", "BTU": "ft"}, + "T": {"SI": "s", "CGS": "s", "BTU": "s"}, + "Temp": {"SI": "K", "CGS": "K", "BTU": "R"}, + "I": {"SI": "A", "CGS": "A", "BTU": "A"}, + "N": {"SI": "mol", "CGS": "mol", "BTU": "slugmol"}, + "J": {"SI": "cd", "CGS": "cd", "BTU": "cd"}, + "Angle": {"SI": "radian", "CGS": "radian", "BTU": "radian"}, + "SAngle": {"SI": "sr", "CGS": "sr", "BTU": "sr"}, + "": {"SI": "", "CGS": "", "BTU": ""}, + } + + _supported_unit_sys = ("SI", "CGS", "BTU") + + def __init__(self, unit_sys): + self._unit_system = unit_sys.upper() + + if self._unit_system not in UnitSystem._supported_unit_sys: + raise ValueError( + "Unsupported unit system, only 'SI', 'CGS', 'BTU' is allowed." + ) + + def _get_unit_from_dim(self, dim_list): + unit = "" + + for key, power in zip(_UnitsTable.dimension_order.values(), dim_list): + unit_str = UnitSystem._dim_to_unit_sys_map[key][self._unit_system] + spacechar = " " if len(unit) > 0 else "" + if power > 0.0 or power < 0.0: + unit += spacechar + unit_str + "^" + str(power) + elif power == 1: + unit += spacechar + unit_str + return unit + + def convert(self, quantity): + if not isinstance(quantity, Quantity): + raise TypeError("Not instance of Quantity.") + dims = quantity.get_dimensions_list() + unit_str = self._get_unit_from_dim(dims) + return quantity.to(unit_str) + + class Quantity(float): """This class instantiates physical quantities using their real values and units. @@ -548,3 +593,6 @@ def __sub__(self, other): def __rsub__(self, other): return self.__sub__(other) + + def __eq__(self, other): + return self._si_value == other._si_value and self._si_unit == other._si_unit diff --git a/tests/test_quantity.py b/tests/test_quantity.py index d68701f97e28..1384f37b91f6 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -289,6 +289,30 @@ def test_prop_constants_40(): assert float(y) == 287.28 +def test_unit_system_41(): + unitSysSI = q.UnitSystem("SI") + myVel = q.Quantity(3.0, "ft s^-1") + siVel = unitSysSI.convert(myVel) # Returns velocity in m/s + assert siVel.value == pytest.approx(0.9143999, DELTA) + assert siVel.unit == "m^1.0 s^-1.0" + + unitSysCGS = q.UnitSystem("CGS") + cgsVel = unitSysCGS.convert(myVel) # Returns velocity in cm/s + assert cgsVel.value == 91.44 + assert cgsVel.unit == "cm^1.0 s^-1.0" + + unitSysBT = q.UnitSystem("BTU") + btuVel = unitSysBT.convert(myVel) # Returns velocity in ft/s + assert btuVel.value == 3.0 + assert btuVel.unit == "ft^1.0 s^-1.0" + + +def test_equality_42(): + qt1 = q.Quantity(5.0, "m s^-1") + qt2 = q.Quantity(5.0, "m s^-1") + assert qt1 == qt2 + + def test_math_fun(): deg = q.Quantity(90, "degree") assert math.sin(deg) == 1.0 @@ -459,30 +483,27 @@ def testing_properties(): qt2 = q.Quantity(5, "m s^-1") -if __name__ == "__main__": - test_value_unit_1() - testing_dimensions() - testing_multipliers() - testing_to_systems() - testing_arithmetic_operators() - testing_properties - - # x = q.Quantity(1, "ft") - # print( - # f"User unit: {x._unit.user_unit}, multiplier: {x._unit.si_factor}, reduced_si_unit: {x._unit.si_unit}, si_value: {x._si_value}" - # ) - # - # g = q.Quantity(1, "g") - # m = q.Quantity(1, "m") - # print(f"{g} * {m} = {g * m}") - # print(" ") - # print(f"{m} * 2 = {m * 2}") - # print(" ") - # - # l = q.Quantity(1, "cm") - # r = q.Quantity(1, "kg m s") - # print(f"{l} + {r} = {l + r}") - # print(" ") - - # cgs_unit = q.get_unit_from_dim(r.get_dimensions_list()) - # print(cgs_unit) +# if __name__ == "__main__": +# test_value_unit_1() +# testing_dimensions() +# testing_multipliers() +# testing_to_systems() +# testing_arithmetic_operators() +# testing_properties + +# x = q.Quantity(1, "ft") +# print( +# f"User unit: {x._unit.user_unit}, multiplier: {x._unit.si_factor}, reduced_si_unit: {x._unit.si_unit}, si_value: {x._si_value}" +# ) +# +# g = q.Quantity(1, "g") +# m = q.Quantity(1, "m") +# print(f"{g} * {m} = {g * m}") +# print(" ") +# print(f"{m} * 2 = {m * 2}") +# print(" ") +# +# l = q.Quantity(1, "cm") +# r = q.Quantity(1, "") +# print(f"{l} + {r} = {l + r}") +# print(" ") From f609c765909d3ef8a595e80847c0402708109560 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Tue, 1 Nov 2022 21:27:18 +0530 Subject: [PATCH 20/40] Supported slugmol unit --- src/ansys/fluent/core/quantity.py | 3 +++ tests/test_quantity.py | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 86d6b67ff37d..80e943f338b3 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -19,6 +19,7 @@ class _UnitsTable(object): "F": "Temp", "R": "Temp", "mol": "N", + "slugmol": "N", "cd": "J", "sr": "SAngle", "radian": "Angle", @@ -104,6 +105,7 @@ class _UnitsTable(object): "s": 1, "A": 1, "mol": 1, + "slugmol": 14593.9, "cd": 1, "sr": 1, "radian": 1, @@ -127,6 +129,7 @@ class _UnitsTable(object): "s": "s", "A": "A", "mol": "mol", + "slugmol": "mol", "cd": "cd", "sr": "sr", "radian": "radian", diff --git a/tests/test_quantity.py b/tests/test_quantity.py index 1384f37b91f6..480d103d16cc 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -489,13 +489,13 @@ def testing_properties(): # testing_multipliers() # testing_to_systems() # testing_arithmetic_operators() -# testing_properties - +# testing_properties() +# # x = q.Quantity(1, "ft") # print( # f"User unit: {x._unit.user_unit}, multiplier: {x._unit.si_factor}, reduced_si_unit: {x._unit.si_unit}, si_value: {x._si_value}" # ) -# + # g = q.Quantity(1, "g") # m = q.Quantity(1, "m") # print(f"{g} * {m} = {g * m}") From 4d9f43c8e314e5b4f7c6ee9300c229c8d151a4c4 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Wed, 2 Nov 2022 10:39:02 +0530 Subject: [PATCH 21/40] Added line to handle import error and updated __rsub__ method --- src/ansys/fluent/core/quantity.py | 7 ++-- tests/test_quantity.py | 53 +++++++++++++++++-------------- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 80e943f338b3..7f9fbfc6d20f 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -1,3 +1,4 @@ +# -*- coding: latin-1 -*- from collections import OrderedDict @@ -52,13 +53,15 @@ class _UnitsTable(object): "l": (0.001, "m^3"), "gal": (0.0037854117839999993, "m^3"), "BTU": (1055.056, "J"), + "C": (lambda c: c + 273.15, "K"), + "F": (lambda f: (f - 32) * (5 / 9) + 273.15, "K"), } multipliers = { "d": 10**-1, "c": 10**-2, "m": 10**-3, - "μ": 10**-6, + "?": 10**-6, "n": 10**-9, "p": 10**-12, "f": 10**-15, @@ -595,7 +598,7 @@ def __sub__(self, other): return Quantity(temp_value, self._si_unit) def __rsub__(self, other): - return self.__sub__(other) + return Quantity(other, "") - self def __eq__(self, other): return self._si_value == other._si_value and self._si_unit == other._si_unit diff --git a/tests/test_quantity.py b/tests/test_quantity.py index 480d103d16cc..ac1c801911e8 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -483,27 +483,32 @@ def testing_properties(): qt2 = q.Quantity(5, "m s^-1") -# if __name__ == "__main__": -# test_value_unit_1() -# testing_dimensions() -# testing_multipliers() -# testing_to_systems() -# testing_arithmetic_operators() -# testing_properties() -# -# x = q.Quantity(1, "ft") -# print( -# f"User unit: {x._unit.user_unit}, multiplier: {x._unit.si_factor}, reduced_si_unit: {x._unit.si_unit}, si_value: {x._si_value}" -# ) - -# g = q.Quantity(1, "g") -# m = q.Quantity(1, "m") -# print(f"{g} * {m} = {g * m}") -# print(" ") -# print(f"{m} * 2 = {m * 2}") -# print(" ") -# -# l = q.Quantity(1, "cm") -# r = q.Quantity(1, "") -# print(f"{l} + {r} = {l + r}") -# print(" ") +if __name__ == "__main__": + # test_value_unit_1() + # testing_dimensions() + # testing_multipliers() + # testing_to_systems() + # testing_arithmetic_operators() + # testing_properties() + # + # x = q.Quantity(1, "ft") + # print( + # f"User unit: {x._unit.user_unit}, multiplier: {x._unit.si_factor}, reduced_si_unit: {x._unit.si_unit}, si_value: {x._si_value}" + # ) + + # g = q.Quantity(1, "g") + # m = q.Quantity(1, "m") + # print(f"{g} * {m} = {g * m}") + # print(" ") + # print(f"{m} * 2 = {m * 2}") + # print(" ") + # + # l = q.Quantity(1, "cm") + # r = q.Quantity(1, "") + # print(f"{l} + {r} = {l + r}") + # print(" ") + tk = q.Quantity(300.0, "K") + tr = q.Quantity(150.0, "R") + + print(tk.to("R")) + print(tr.to("K")) From 6fcf8fa7ec70887b7a4e09a7cb05ccca15e3c9d4 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Wed, 2 Nov 2022 14:00:18 +0530 Subject: [PATCH 22/40] Updated __rsub__ method --- src/ansys/fluent/core/quantity.py | 17 +++++-- tests/test_quantity.py | 80 ++++++++++++++++++++----------- 2 files changed, 66 insertions(+), 31 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 7f9fbfc6d20f..3629281c8a2a 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -281,7 +281,9 @@ def _computemultipliers_and_offsets(self, unit_str, power): if unit_str in _UnitsTable.fundamental_units: spacechar = " " if len(self._si_unit) > 0 else "" - if term_power > 0.0 or term_power < 0.0: + if term_power == 1.0: + self._si_unit += spacechar + _UnitsTable.si_map[unit_str] + elif term_power > 0.0 or term_power < 0.0: self._si_unit += ( spacechar + _UnitsTable.si_map[unit_str] + "^" + str(term_power) ) @@ -389,6 +391,13 @@ def get_si_unit_from_dim(dim_list): si_unit += spacechar + unit_str + "^" + str(power) elif power == 1.0: si_unit += spacechar + unit_str + elif power > 0.0 or power < 0.0: + si_unit += ( + spacechar + + unit_str + + "^" + + str(int(power) if power.is_integer() else power) + ) return si_unit @@ -589,8 +598,10 @@ def __sub__(self, other): self.get_dimensions_list() == other.get_dimensions_list() ): temp_value = self._si_value - other._si_value - elif self.is_dimension_less() and ( - isinstance(other, int) or isinstance(other, float) + elif ( + not isinstance(other, Quantity) + and self.is_dimension_less() + and (isinstance(other, int) or isinstance(other, float)) ): temp_value = self._si_value - other else: diff --git a/tests/test_quantity.py b/tests/test_quantity.py index ac1c801911e8..f6282953ae6e 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -413,12 +413,12 @@ def testing_arithmetic_operators(): result = qt1 * 2 print(f"{qt1} * {2} = {result}") assert result.value == 20 - assert result.unit == "m^1.0 s^-1.0" + assert result.unit == "m^1 s^-1.0" result1 = 2 * qt1 print(f"{2} * {qt1} = {result1}") assert result1.value == 20 - assert result1.unit == "m^1.0 s^-1.0" + assert result1.unit == "m^1 s^-1.0" q3 = qt1 / qt2 @@ -429,13 +429,13 @@ def testing_arithmetic_operators(): result3 = qt1 / 2 print(f"{qt1} / {2} = {qt1 / 2}") assert result3.value == 5 - assert result3.unit == "m^1.0 s^-1.0" + assert result3.unit == "m^1 s^-1.0" qa3 = qt1 + qt2 print(f"{qt1} + {qt2} = {qa3}") assert qa3.value == 15 - assert qa3.unit == "m^1.0 s^-1.0" + assert qa3.unit == "m^1 s^-1.0" try: result5 = qt1 + 2 @@ -453,7 +453,7 @@ def testing_arithmetic_operators(): print(f"{qt1} - {qt2} = {qs3}") assert qs3.value == 5 - assert qs3.unit == "m^1.0 s^-1.0" + assert qs3.unit == "m^1 s^-1.0" try: result7 = qt1 - 2 @@ -484,31 +484,55 @@ def testing_properties(): if __name__ == "__main__": - # test_value_unit_1() - # testing_dimensions() - # testing_multipliers() - # testing_to_systems() - # testing_arithmetic_operators() - # testing_properties() - # - # x = q.Quantity(1, "ft") - # print( - # f"User unit: {x._unit.user_unit}, multiplier: {x._unit.si_factor}, reduced_si_unit: {x._unit.si_unit}, si_value: {x._si_value}" - # ) - - # g = q.Quantity(1, "g") - # m = q.Quantity(1, "m") - # print(f"{g} * {m} = {g * m}") - # print(" ") - # print(f"{m} * 2 = {m * 2}") - # print(" ") - # + test_value_unit_1() + testing_dimensions() + testing_multipliers() + testing_to_systems() + testing_arithmetic_operators() + testing_properties() + + x = q.Quantity(1, "ft") + print( + f"User unit: {x._unit.user_unit}, multiplier: {x._unit.si_factor}, reduced_si_unit: {x._unit.si_unit}, si_value: {x._si_value}" + ) + + g = q.Quantity(1, "g") + m = q.Quantity(1, "m") + print(f"{g} * {m} = {g * m}") + print("here ") + print(f"{m} * 2 = {m * 2}") + print(" ") + # l = q.Quantity(1, "cm") # r = q.Quantity(1, "") # print(f"{l} + {r} = {l + r}") # print(" ") - tk = q.Quantity(300.0, "K") - tr = q.Quantity(150.0, "R") + # tk = q.Quantity(300.0, "K") + # tr = q.Quantity(150.0, "R") + # + # print(tk.to("R")) + # print(tr.to("K")) + + # b = q.Quantity(10.5, "m") + # print(b) + + q1 = q.Quantity(10.0, "m s^-1") + q2 = q.Quantity(5.0, "m s^-1") + + print(f"{q1} - {q2} = {q1 - q2}") + print("") + + print(f"{q2} - {q1} = {q2 - q1}") + print("") + + print(f"{float(q1)} - 2.0 = {float(q1) - 2.0}") + print("") + + print(f"2.0 - {float(q1)} = {2.0 - float(q1)}") + print("") + + print(f"{float(q1)} - 3 = {float(q1) - 3}") + print("") - print(tk.to("R")) - print(tr.to("K")) + print(f"3 - {float(q1)} = {3 - float(q1)}") + print("") From 7bd42ac6b6e8518519033d1cc7a3329786182b9f Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Wed, 2 Nov 2022 14:12:15 +0530 Subject: [PATCH 23/40] Updated __pow__ method --- src/ansys/fluent/core/quantity.py | 4 +--- tests/test_quantity.py | 3 +++ 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 3629281c8a2a..0cd26533891c 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -544,9 +544,7 @@ def _get_si_unit(self, other, func): return temp_unit def __pow__(self, exponent): - new_dims = list( - map(lambda x: x * exponent if x != 0 else x, self.get_dimensions_list()) - ) + new_dims = list(map(lambda x: x * exponent, self.get_dimensions_list())) new_si_unit = get_si_unit_from_dim(new_dims) new_si_value = pow(self._si_value, exponent) return Quantity(new_si_value, new_si_unit) diff --git a/tests/test_quantity.py b/tests/test_quantity.py index f6282953ae6e..78141b653ae1 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -536,3 +536,6 @@ def testing_properties(): print(f"3 - {float(q1)} = {3 - float(q1)}") print("") + + print(q1**2) + print(q2**2) From c0917aef05378282a542b2be03b3a18358484e49 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Wed, 2 Nov 2022 14:24:43 +0530 Subject: [PATCH 24/40] Updated __pow__ method --- src/ansys/fluent/core/quantity.py | 9 ++++++++- tests/test_quantity.py | 8 ++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 0cd26533891c..531964609b53 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -610,4 +610,11 @@ def __rsub__(self, other): return Quantity(other, "") - self def __eq__(self, other): - return self._si_value == other._si_value and self._si_unit == other._si_unit + if isinstance(other, Quantity): + return self._si_value == other._si_value and self._si_unit == other._si_unit + elif ( + self.is_dimension_less() + and not isinstance(other, Quantity) + and isinstance(other, float) + ): + return float(self) == other diff --git a/tests/test_quantity.py b/tests/test_quantity.py index 78141b653ae1..fcd48c6ec545 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -539,3 +539,11 @@ def testing_properties(): print(q1**2) print(q2**2) + + q3 = q.Quantity(10.0, "m s^-1") + q4 = q.Quantity(10.0, "") + + print(q1 == q2) + print(q1 == q3) + print(float(q1) == 10.0) + print(q4 == 10.0) From 7d4e032337af980cd9bbfdd5f5c3fa9357ff1cd8 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Wed, 2 Nov 2022 14:34:25 +0530 Subject: [PATCH 25/40] Updated __eq__ method and added test for subtraction methods --- tests/test_quantity.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/test_quantity.py b/tests/test_quantity.py index fcd48c6ec545..2e3cfd2492c9 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -313,7 +313,7 @@ def test_equality_42(): assert qt1 == qt2 -def test_math_fun(): +def test_math_fun_43(): deg = q.Quantity(90, "degree") assert math.sin(deg) == 1.0 @@ -324,6 +324,18 @@ def test_math_fun(): assert math.sqrt(root) == 10.0 +def test_subtraction_44(): + q1 = q.Quantity(10.0, "m s^-1") + q2 = q.Quantity(5.0, "m s^-1") + + assert float(q1 - q2) == 5.0 + assert float(q2 - q1) == -5.0 + assert float(q1) - 2.0 == 8.0 + assert 2.0 - float(q1) == -8.0 + assert float(q1) - 3 == 7.0 + assert 3 - float(q1) == -7.0 + + def testing_dimensions(): print(f"{'*' * 25} {testing_dimensions.__name__} {'*' * 25}") From 98dcaae9c5a4a078c00f6302417a3c68ab8958c1 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Wed, 2 Nov 2022 14:43:28 +0530 Subject: [PATCH 26/40] Added test for __pow__ and __eq__ methods --- tests/test_quantity.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/test_quantity.py b/tests/test_quantity.py index 2e3cfd2492c9..034f733f4a4c 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -336,6 +336,29 @@ def test_subtraction_44(): assert 3 - float(q1) == -7.0 +def test_pow_45(): + q1 = q.Quantity(10.0, "m s^-1") + q2 = q.Quantity(5.0, "m s^-1") + + q1_sq = q1**2 + assert q1_sq.unit == "m^2.0 s^-2.0" + + assert float(q1) ** 2 == 100.0 + assert float(q2) ** 2 == 25.0 + + +def test_eq_46(): + q1 = q.Quantity(10.0, "m s^-1") + q2 = q.Quantity(5.0, "m s^-1") + q3 = q.Quantity(10.0, "m s^-1") + q4 = q.Quantity(10.0, "") + + assert q1 != q2 + assert q1 == q3 + assert float(q1) == 10.0 + assert q4 == 10.0 + + def testing_dimensions(): print(f"{'*' * 25} {testing_dimensions.__name__} {'*' * 25}") From 5fb5e2979a11b89e88e7e96bd85ff826fa909a6d Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Wed, 2 Nov 2022 15:24:42 +0530 Subject: [PATCH 27/40] Updated mu and ohm symbol --- src/ansys/fluent/core/quantity.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 531964609b53..1af36d6be6ed 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -22,7 +22,7 @@ class _UnitsTable(object): "mol": "N", "slugmol": "N", "cd": "J", - "sr": "SAngle", + "sr": "?", "radian": "Angle", "degree": "Angle", } @@ -90,7 +90,7 @@ class _UnitsTable(object): ("ChemicalAmount", "N"), ("Light", "J"), ("Current", "I"), - ("SolidAngle", "SAngle"), + ("SolidAngle", "?"), ("", ""), ] ) @@ -316,7 +316,7 @@ def __init__(self, unit_str): "N": 0.0, "J": 0.0, "Angle": 0.0, - "SAngle": 0.0, + "?": 0.0, "": 0.0, } self._parser(unit_str) @@ -381,7 +381,7 @@ def get_si_unit_from_dim(dim_list): "N": "mol", "J": "cd", "Angle": "radian", - "SAngle": "sr", + "?": "sr", "": "", } for key, power in zip(_UnitsTable.dimension_order.values(), dim_list): @@ -412,7 +412,7 @@ class UnitSystem: "N": {"SI": "mol", "CGS": "mol", "BTU": "slugmol"}, "J": {"SI": "cd", "CGS": "cd", "BTU": "cd"}, "Angle": {"SI": "radian", "CGS": "radian", "BTU": "radian"}, - "SAngle": {"SI": "sr", "CGS": "sr", "BTU": "sr"}, + "?": {"SI": "sr", "CGS": "sr", "BTU": "sr"}, "": {"SI": "", "CGS": "", "BTU": ""}, } From 5f048a8121c3df859418a3c405c26c1de50a8e9a Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Wed, 2 Nov 2022 16:31:39 +0530 Subject: [PATCH 28/40] Updated theta symbol --- src/ansys/fluent/core/quantity.py | 16 ++++---- tests/test_quantity.py | 63 ++++++++++++++++--------------- 2 files changed, 41 insertions(+), 38 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 1af36d6be6ed..68da58374c6d 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -15,10 +15,10 @@ class _UnitsTable(object): "in": "L", "s": "T", "A": "I", - "K": "Temp", - "C": "Temp", - "F": "Temp", - "R": "Temp", + "K": "?", + "C": "?", + "F": "?", + "R": "?", "mol": "N", "slugmol": "N", "cd": "J", @@ -85,7 +85,7 @@ class _UnitsTable(object): ("Mass", "M"), ("Length", "L"), ("Time", "T"), - ("Temperature", "Temp"), + ("Temperature", "?"), ("Angle", "Angle"), ("ChemicalAmount", "N"), ("Light", "J"), @@ -311,7 +311,7 @@ def __init__(self, unit_str): "M": 0.0, "L": 0.0, "T": 0.0, - "Temp": 0.0, + "?": 0.0, "I": 0.0, "N": 0.0, "J": 0.0, @@ -376,7 +376,7 @@ def get_si_unit_from_dim(dim_list): "M": "kg", "L": "m", "T": "s", - "Temp": "K", + "?": "K", "I": "A", "N": "mol", "J": "cd", @@ -407,7 +407,7 @@ class UnitSystem: "M": {"SI": "kg", "CGS": "g", "BTU": "slug"}, "L": {"SI": "m", "CGS": "cm", "BTU": "ft"}, "T": {"SI": "s", "CGS": "s", "BTU": "s"}, - "Temp": {"SI": "K", "CGS": "K", "BTU": "R"}, + "?": {"SI": "K", "CGS": "K", "BTU": "R"}, "I": {"SI": "A", "CGS": "A", "BTU": "A"}, "N": {"SI": "mol", "CGS": "mol", "BTU": "slugmol"}, "J": {"SI": "cd", "CGS": "cd", "BTU": "cd"}, diff --git a/tests/test_quantity.py b/tests/test_quantity.py index 034f733f4a4c..7fb287aad0f3 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -551,34 +551,37 @@ def testing_properties(): # b = q.Quantity(10.5, "m") # print(b) - q1 = q.Quantity(10.0, "m s^-1") - q2 = q.Quantity(5.0, "m s^-1") - - print(f"{q1} - {q2} = {q1 - q2}") - print("") - - print(f"{q2} - {q1} = {q2 - q1}") - print("") - - print(f"{float(q1)} - 2.0 = {float(q1) - 2.0}") - print("") - - print(f"2.0 - {float(q1)} = {2.0 - float(q1)}") - print("") - - print(f"{float(q1)} - 3 = {float(q1) - 3}") - print("") - - print(f"3 - {float(q1)} = {3 - float(q1)}") - print("") - - print(q1**2) - print(q2**2) - - q3 = q.Quantity(10.0, "m s^-1") - q4 = q.Quantity(10.0, "") + # q1 = q.Quantity(10.0, "m s^-1") + # q2 = q.Quantity(5.0, "m s^-1") + # + # print(f"{q1} - {q2} = {q1 - q2}") + # print("") + # + # print(f"{q2} - {q1} = {q2 - q1}") + # print("") + # + # print(f"{float(q1)} - 2.0 = {float(q1) - 2.0}") + # print("") + # + # print(f"2.0 - {float(q1)} = {2.0 - float(q1)}") + # print("") + # + # print(f"{float(q1)} - 3 = {float(q1) - 3}") + # print("") + # + # print(f"3 - {float(q1)} = {3 - float(q1)}") + # print("") + # + # print(q1**2) + # print(q2**2) + # + # q3 = q.Quantity(10.0, "m s^-1") + # q4 = q.Quantity(10.0, "") + # + # print(q1 == q2) + # print(q1 == q3) + # print(float(q1) == 10.0) + # print(q4 == 10.0) - print(q1 == q2) - print(q1 == q3) - print(float(q1) == 10.0) - print(q4 == 10.0) + rankine = q.Quantity(1.0, "R") + print(rankine.to("K")) From f41e42ef5d82d2d6d369264dc0124b69f7a1f773 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Wed, 2 Nov 2022 17:51:38 +0530 Subject: [PATCH 29/40] Added __rdiv__ method and it's test --- src/ansys/fluent/core/quantity.py | 6 ++++++ tests/test_quantity.py | 20 ++++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 68da58374c6d..e89b21d3e1df 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -575,6 +575,12 @@ def __truediv__(self, other): temp = Quantity(self._si_value / other, self._si_unit) return temp + def __rtruediv__(self, other): + if not isinstance(other, Quantity) and isinstance(other, float): + return Quantity(other / self._si_value, self._si_unit) + else: + return other / self + def __add__(self, other): if isinstance(other, Quantity) and ( self.get_dimensions_list() == other.get_dimensions_list() diff --git a/tests/test_quantity.py b/tests/test_quantity.py index 7fb287aad0f3..6c705e8be23b 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -359,6 +359,16 @@ def test_eq_46(): assert q4 == 10.0 +def test_rdiv_47(): + q1 = q.Quantity(10.0, "m s^-1") + q2 = q.Quantity(5.0, "m s^-1") + + assert float(q1) / float(q2) == 2.0 + assert float(q2) / float(q1) == 0.5 + assert float(q1) / 2 == 5.0 + assert 2.0 / float(q1) == 0.2 + + def testing_dimensions(): print(f"{'*' * 25} {testing_dimensions.__name__} {'*' * 25}") @@ -551,8 +561,8 @@ def testing_properties(): # b = q.Quantity(10.5, "m") # print(b) - # q1 = q.Quantity(10.0, "m s^-1") - # q2 = q.Quantity(5.0, "m s^-1") + q1 = q.Quantity(10.0, "m s^-1") + q2 = q.Quantity(5.0, "m s^-1") # # print(f"{q1} - {q2} = {q1 - q2}") # print("") @@ -583,5 +593,7 @@ def testing_properties(): # print(float(q1) == 10.0) # print(q4 == 10.0) - rankine = q.Quantity(1.0, "R") - print(rankine.to("K")) + print(q1 / q2) + print(q2 / q1) + print(q1 / 2) + print(2.0 / q1) From 6b07e005a01db1883d7d406ffa94e6f8974a16fa Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Wed, 2 Nov 2022 19:14:02 +0530 Subject: [PATCH 30/40] Updated UnitSystem class --- src/ansys/fluent/core/quantity.py | 78 ++++++++++++++++++++----------- tests/test_quantity.py | 6 +-- 2 files changed, 54 insertions(+), 30 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index e89b21d3e1df..b8d81a2d1d07 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -372,18 +372,8 @@ def _get_dim(self, unit_str, power): def get_si_unit_from_dim(dim_list): si_unit = "" - dim_to_unit_map = { - "M": "kg", - "L": "m", - "T": "s", - "?": "K", - "I": "A", - "N": "mol", - "J": "cd", - "Angle": "radian", - "?": "sr", - "": "", - } + dim_to_unit_map = UnitSystem("SI").base_units() + for key, power in zip(_UnitsTable.dimension_order.values(), dim_list): unit_str = dim_to_unit_map[key] spacechar = " " if len(si_unit) > 0 else "" @@ -404,16 +394,42 @@ def get_si_unit_from_dim(dim_list): class UnitSystem: _dim_to_unit_sys_map = { - "M": {"SI": "kg", "CGS": "g", "BTU": "slug"}, - "L": {"SI": "m", "CGS": "cm", "BTU": "ft"}, - "T": {"SI": "s", "CGS": "s", "BTU": "s"}, - "?": {"SI": "K", "CGS": "K", "BTU": "R"}, - "I": {"SI": "A", "CGS": "A", "BTU": "A"}, - "N": {"SI": "mol", "CGS": "mol", "BTU": "slugmol"}, - "J": {"SI": "cd", "CGS": "cd", "BTU": "cd"}, - "Angle": {"SI": "radian", "CGS": "radian", "BTU": "radian"}, - "?": {"SI": "sr", "CGS": "sr", "BTU": "sr"}, - "": {"SI": "", "CGS": "", "BTU": ""}, + "SI": { + "M": "kg", + "L": "m", + "T": "s", + "?": "K", + "I": "A", + "N": "mol", + "J": "cd", + "Angle": "radian", + "?": "sr", + "": "", + }, + "CGS": { + "M": "g", + "L": "cm", + "T": "s", + "?": "K", + "I": "A", + "N": "mol", + "J": "cd", + "Angle": "radian", + "?": "sr", + "": "", + }, + "BTU": { + "M": "slug", + "L": "ft", + "T": "s", + "?": "R", + "I": "A", + "N": "slugmol", + "J": "cd", + "Angle": "radian", + "?": "sr", + "": "", + }, } _supported_unit_sys = ("SI", "CGS", "BTU") @@ -429,13 +445,18 @@ def __init__(self, unit_sys): def _get_unit_from_dim(self, dim_list): unit = "" + base_units = UnitSystem._dim_to_unit_sys_map[self._unit_system] for key, power in zip(_UnitsTable.dimension_order.values(), dim_list): - unit_str = UnitSystem._dim_to_unit_sys_map[key][self._unit_system] spacechar = " " if len(unit) > 0 else "" - if power > 0.0 or power < 0.0: - unit += spacechar + unit_str + "^" + str(power) - elif power == 1: - unit += spacechar + unit_str + if power == 1: + unit += spacechar + base_units[key] + elif power != 0: + unit += ( + spacechar + + base_units[key] + + "^" + + str(int(power) if power.is_integer() else power) + ) return unit def convert(self, quantity): @@ -445,6 +466,9 @@ def convert(self, quantity): unit_str = self._get_unit_from_dim(dims) return quantity.to(unit_str) + def base_units(self): + return UnitSystem._dim_to_unit_sys_map[self._unit_system] + class Quantity(float): """This class instantiates physical quantities using their real values and diff --git a/tests/test_quantity.py b/tests/test_quantity.py index 6c705e8be23b..f4e19dc386ed 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -294,17 +294,17 @@ def test_unit_system_41(): myVel = q.Quantity(3.0, "ft s^-1") siVel = unitSysSI.convert(myVel) # Returns velocity in m/s assert siVel.value == pytest.approx(0.9143999, DELTA) - assert siVel.unit == "m^1.0 s^-1.0" + assert siVel.unit == "m s^-1" unitSysCGS = q.UnitSystem("CGS") cgsVel = unitSysCGS.convert(myVel) # Returns velocity in cm/s assert cgsVel.value == 91.44 - assert cgsVel.unit == "cm^1.0 s^-1.0" + assert cgsVel.unit == "cm s^-1" unitSysBT = q.UnitSystem("BTU") btuVel = unitSysBT.convert(myVel) # Returns velocity in ft/s assert btuVel.value == 3.0 - assert btuVel.unit == "ft^1.0 s^-1.0" + assert btuVel.unit == "ft s^-1" def test_equality_42(): From e5dfa6125a97245a3c547af41e45edf3a743a00e Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Wed, 2 Nov 2022 19:23:42 +0530 Subject: [PATCH 31/40] Updated get_si_unit_from_dim function --- src/ansys/fluent/core/quantity.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index b8d81a2d1d07..89b003a40d6e 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -377,17 +377,10 @@ def get_si_unit_from_dim(dim_list): for key, power in zip(_UnitsTable.dimension_order.values(), dim_list): unit_str = dim_to_unit_map[key] spacechar = " " if len(si_unit) > 0 else "" - if power > 0.0 or power < 0.0: - si_unit += spacechar + unit_str + "^" + str(power) - elif power == 1.0: + if power == 1.0: si_unit += spacechar + unit_str - elif power > 0.0 or power < 0.0: - si_unit += ( - spacechar - + unit_str - + "^" - + str(int(power) if power.is_integer() else power) - ) + elif power != 0.0: + si_unit += spacechar + unit_str + "^" + str(power) return si_unit From 9ea6fa61470a13ab40a29048423f735f77a09615 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Wed, 2 Nov 2022 19:46:19 +0530 Subject: [PATCH 32/40] Updated get_si_unit_from_dim function again --- src/ansys/fluent/core/quantity.py | 7 ++++++- tests/test_quantity.py | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 89b003a40d6e..5e4e90240a26 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -380,7 +380,12 @@ def get_si_unit_from_dim(dim_list): if power == 1.0: si_unit += spacechar + unit_str elif power != 0.0: - si_unit += spacechar + unit_str + "^" + str(power) + si_unit += ( + spacechar + + unit_str + + "^" + + str(int(power) if power.is_integer() else power) + ) return si_unit diff --git a/tests/test_quantity.py b/tests/test_quantity.py index f4e19dc386ed..d36312e882c3 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -341,7 +341,7 @@ def test_pow_45(): q2 = q.Quantity(5.0, "m s^-1") q1_sq = q1**2 - assert q1_sq.unit == "m^2.0 s^-2.0" + assert q1_sq.unit == "m^2 s^-2" assert float(q1) ** 2 == 100.0 assert float(q2) ** 2 == 25.0 @@ -453,7 +453,7 @@ def testing_arithmetic_operators(): print(f"{qt1} * {qt2} = {qt3}") assert qt3.value == 50 - assert qt3.unit == "m^2.0 s^-2.0" + assert qt3.unit == "m^2 s^-2" result = qt1 * 2 print(f"{qt1} * {2} = {result}") From 3ea2ea445a100a7be918e67cb096dc3dc6f1f1f3 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Thu, 3 Nov 2022 11:35:48 +0530 Subject: [PATCH 33/40] Temprature support, power issue resolved, replaced symbols --- src/ansys/fluent/core/quantity.py | 161 +++++++++++++++++++++--------- tests/test_quantity.py | 55 +++++++--- 2 files changed, 152 insertions(+), 64 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 5e4e90240a26..71ee348277bd 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -1,4 +1,3 @@ -# -*- coding: latin-1 -*- from collections import OrderedDict @@ -15,14 +14,14 @@ class _UnitsTable(object): "in": "L", "s": "T", "A": "I", - "K": "?", - "C": "?", - "F": "?", - "R": "?", + "K": "Temp", + "C": "Temp", + "F": "Temp", + "R": "Temp", "mol": "N", "slugmol": "N", - "cd": "J", - "sr": "?", + "cd": "Iv", + "sr": "SAngle", "radian": "Angle", "degree": "Angle", } @@ -53,15 +52,13 @@ class _UnitsTable(object): "l": (0.001, "m^3"), "gal": (0.0037854117839999993, "m^3"), "BTU": (1055.056, "J"), - "C": (lambda c: c + 273.15, "K"), - "F": (lambda f: (f - 32) * (5 / 9) + 273.15, "K"), } multipliers = { "d": 10**-1, "c": 10**-2, "m": 10**-3, - "?": 10**-6, + "u": 10**-6, "n": 10**-9, "p": 10**-12, "f": 10**-15, @@ -85,12 +82,12 @@ class _UnitsTable(object): ("Mass", "M"), ("Length", "L"), ("Time", "T"), - ("Temperature", "?"), + ("Temperature", "Temp"), ("Angle", "Angle"), ("ChemicalAmount", "N"), - ("Light", "J"), + ("Light", "Iv"), ("Current", "I"), - ("SolidAngle", "?"), + ("SolidAngle", "SAngle"), ("", ""), ] ) @@ -144,18 +141,32 @@ class _UnitsTable(object): "": "", } - offset_conversions = { - "K": 1, - "C": 274.15, - "F": 255.92777777777778, - "R": 0.5555555555555556, - } - - offset_dict = { - "K": {"C": 274.15, "F": 255.92777777777778, "R": 0.5555555555555556}, - "C": {"K": -272.15, "F": -17.2222222222222, "R": -272.59444444444443}, - "F": {"C": 33.79999999999993, "K": -457.87, "R": -458.67}, - "R": {"C": 493.4699999999999, "F": 460.66999999999996, "K": 1.7999999999999998}, + temperature_conversions = { + # from : { to : formula } + "C": { + "K": lambda c: c + 273.15, + "R": lambda c: (c + 273.15) * 1.8, + "F": lambda c: c * 9 / 5 + 32, + "C": lambda c: c, + }, + "F": { + "K": lambda f: (f - 32) * 5 / 9 + 273.15, + "R": lambda f: f + 459.67, + "F": lambda f: f, + "C": lambda f: (f - 32) * 5 / 9, + }, + "R": { + "K": lambda r: r / 1.8, + "R": lambda r: r, + "F": lambda r: r - 459.67, + "C": lambda r: (r - 491.67) / 1.8, + }, + "K": { + "K": lambda k: k, + "R": lambda k: k * 1.8, + "F": lambda k: k * 1.8 - 459.67, + "C": lambda k: k - 273.15, + }, } @@ -196,6 +207,12 @@ def remove_multiplier(unit_str): return unit_str +def is_temperature_quantity(dim_obj): + temp_dim = dim_obj["Temp"] + total_dims_sum = sum([abs(val) for val in dim_obj.values()]) + return temp_dim == 1 and total_dims_sum == temp_dim + + class Unit(object): def __init__(self, unit_str): self._unit = unit_str @@ -233,16 +250,24 @@ def _reduce_to_si_unit(self, unit_str): term_power_dict[term_split[0]] = float(term_split[1]) else: if term in term_power_dict.keys(): - term_power_dict[term] += 1 + term_power_dict[term] += 1.0 else: - term_power_dict[term] = 1 + term_power_dict[term] = 1.0 self._si_unit = "" for key, power in term_power_dict.items(): spacechar = " " if len(self._si_unit) > 0 else "" - if power > 0.0 or power < 0.0: - self._si_unit += spacechar + key + "^" + str(power) + + if power == 1.0: + self._si_unit += spacechar + key + elif power != 0.0: + self._si_unit += ( + spacechar + + key + + "^" + + str(int(power) if power.is_integer() else power) + ) else: self._si_unit += spacechar + key @@ -283,10 +308,12 @@ def _computemultipliers_and_offsets(self, unit_str, power): if term_power == 1.0: self._si_unit += spacechar + _UnitsTable.si_map[unit_str] - elif term_power > 0.0 or term_power < 0.0: + elif term_power != 0.0: self._si_unit += ( spacechar + _UnitsTable.si_map[unit_str] + "^" + str(term_power) ) + elif term_power == 0.0: + self._si_unit += "" else: self._si_unit += spacechar + _UnitsTable.si_map[unit_str] @@ -311,12 +338,12 @@ def __init__(self, unit_str): "M": 0.0, "L": 0.0, "T": 0.0, - "?": 0.0, + "Temp": 0.0, "I": 0.0, "N": 0.0, - "J": 0.0, + "Iv": 0.0, "Angle": 0.0, - "?": 0.0, + "SAngle": 0.0, "": 0.0, } self._parser(unit_str) @@ -396,36 +423,36 @@ class UnitSystem: "M": "kg", "L": "m", "T": "s", - "?": "K", + "Temp": "K", "I": "A", "N": "mol", - "J": "cd", + "Iv": "cd", "Angle": "radian", - "?": "sr", + "SAngle": "sr", "": "", }, "CGS": { "M": "g", "L": "cm", "T": "s", - "?": "K", + "Temp": "K", "I": "A", "N": "mol", - "J": "cd", + "Iv": "cd", "Angle": "radian", - "?": "sr", + "SAngle": "sr", "": "", }, "BTU": { "M": "slug", "L": "ft", "T": "s", - "?": "R", + "Temp": "R", "I": "A", "N": "slugmol", - "J": "cd", + "Iv": "cd", "Angle": "radian", - "?": "sr", + "SAngle": "sr", "": "", }, } @@ -550,13 +577,49 @@ def to(self, to_unit_str): else: pass - curr_unit_obj = self._unit - to_unit_obj = temp_quantity._unit - temp_quantity.value = ( - curr_unit_obj.si_factor / to_unit_obj.si_factor - ) * self.value - - return temp_quantity + if is_temperature_quantity(curr_unit_dim_obj.as_dict()): + return self._get_converted_temperature(to_unit_str) + else: + curr_unit_obj = self._unit + to_unit_obj = temp_quantity._unit + temp_quantity.value = ( + curr_unit_obj.si_factor / to_unit_obj.si_factor + ) * self.value + return temp_quantity + + def _get_converted_temperature(self, to_unit_str): + from_unit_str = self.unit + + if "^" in from_unit_str: + from_unit_str = from_unit_str.split("^")[0] + + if "^" in to_unit_str: + to_unit_str = to_unit_str.split("^")[0] + + _, prefix = filter_multiplier( + from_unit_str, lambda item, unit: len(unit) > 0.0 and unit.startswith(item) + ) + from_unit_multiplier = ( + _UnitsTable.multipliers[prefix] if len(prefix) != 0 else 1.0 + ) + + _, prefix = filter_multiplier( + to_unit_str, lambda item, unit: len(unit) > 0.0 and unit.startswith(item) + ) + to_unit_multiplier = ( + _UnitsTable.multipliers[prefix] if len(prefix) != 0 else 1.0 + ) + + from_key = remove_multiplier(from_unit_str) + to_key = remove_multiplier(to_unit_str) + + return Quantity( + _UnitsTable.temperature_conversions[from_key][to_key]( + from_unit_multiplier * self.value + ) + / to_unit_multiplier, + to_unit_str, + ) def _get_si_unit(self, other, func): curr_dim = self.get_dimensions_list() diff --git a/tests/test_quantity.py b/tests/test_quantity.py index d36312e882c3..3a506d3b23b1 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -369,6 +369,28 @@ def test_rdiv_47(): assert 2.0 / float(q1) == 0.2 +def test_tempK_48(): + k = q.Quantity(-40, "K") + + kc = k.to("C") + assert kc.value == -313.15 + assert kc.unit == "C" + + kc = k.to("R") + assert kc.value == -72.0 + assert kc.unit == "R" + + kc = k.to("F") + assert kc.value == pytest.approx(-531.67, DELTA) + assert kc.unit == "F" + + +def test_temp_49(): + mk = q.Quantity(-40_000, "mK") + uc = mk.to("uC^1") + assert uc.value == -3.13150000e08 + + def testing_dimensions(): print(f"{'*' * 25} {testing_dimensions.__name__} {'*' * 25}") @@ -458,12 +480,12 @@ def testing_arithmetic_operators(): result = qt1 * 2 print(f"{qt1} * {2} = {result}") assert result.value == 20 - assert result.unit == "m^1 s^-1.0" + assert result.unit == "m s^-1" result1 = 2 * qt1 print(f"{2} * {qt1} = {result1}") assert result1.value == 20 - assert result1.unit == "m^1 s^-1.0" + assert result1.unit == "m s^-1" q3 = qt1 / qt2 @@ -474,13 +496,13 @@ def testing_arithmetic_operators(): result3 = qt1 / 2 print(f"{qt1} / {2} = {qt1 / 2}") assert result3.value == 5 - assert result3.unit == "m^1 s^-1.0" + assert result3.unit == "m s^-1" qa3 = qt1 + qt2 print(f"{qt1} + {qt2} = {qa3}") assert qa3.value == 15 - assert qa3.unit == "m^1 s^-1.0" + assert qa3.unit == "m s^-1" try: result5 = qt1 + 2 @@ -498,7 +520,7 @@ def testing_arithmetic_operators(): print(f"{qt1} - {qt2} = {qs3}") assert qs3.value == 5 - assert qs3.unit == "m^1 s^-1.0" + assert qs3.unit == "m s^-1" try: result7 = qt1 - 2 @@ -529,12 +551,12 @@ def testing_properties(): if __name__ == "__main__": - test_value_unit_1() - testing_dimensions() - testing_multipliers() - testing_to_systems() - testing_arithmetic_operators() - testing_properties() + # test_value_unit_1() + # testing_dimensions() + # testing_multipliers() + # testing_to_systems() + # testing_arithmetic_operators() + # testing_properties() x = q.Quantity(1, "ft") print( @@ -593,7 +615,10 @@ def testing_properties(): # print(float(q1) == 10.0) # print(q4 == 10.0) - print(q1 / q2) - print(q2 / q1) - print(q1 / 2) - print(2.0 / q1) + # print(q1 / q2) + # print(q2 / q1) + # print(q1 / 2) + # print(2.0 / q1) + + q = q.Quantity(5, "m^0") + print(f"{q} * 2 = {q * 2}") From 01a0e680b6133c203ae3eaef8b521a08458a04f1 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Thu, 3 Nov 2022 12:53:08 +0530 Subject: [PATCH 34/40] Added more tests for temprature and power --- tests/test_quantity.py | 95 ++++++++++++++++++++++++++++++++---------- 1 file changed, 74 insertions(+), 21 deletions(-) diff --git a/tests/test_quantity.py b/tests/test_quantity.py index 3a506d3b23b1..a8fef7952043 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -391,6 +391,62 @@ def test_temp_49(): assert uc.value == -3.13150000e08 +def test_temp_50(): + k = q.Quantity(1.0, "K") + + f = k.to("F") + r = k.to("R") + c = k.to("C") + + assert f.value == -457.87 + assert r.value == 1.8 + assert c.value == -272.15 + + +def test_temp_51(): + c = q.Quantity(1.0, "C") + + f = c.to("F") + r = c.to("R") + k = c.to("K") + + assert f.value == 33.80 + assert r.value == pytest.approx(493.469, DELTA) + assert k.value == 274.15 + + +def test_temp_52(): + r = q.Quantity(1.0, "R") + + f = r.to("F") + c = r.to("C") + k = r.to("K") + + assert f.value == pytest.approx(-458.6699, DELTA) + assert c.value == pytest.approx(-272.5944, DELTA) + assert k.value == pytest.approx(0.555556, DELTA) + + +def test_temp_53(): + f = q.Quantity(1.0, "F") + + c = f.to("C") + r = f.to("R") + k = f.to("K") + + assert c.value == pytest.approx(-17.2222, DELTA) + assert r.value == pytest.approx(460.670, DELTA) + assert k.value == pytest.approx(255.927, DELTA) + + +def test_power_54(): + qt = q.Quantity(5.0, "m^0") + qtm = qt * 2 + + assert qtm.value == 10.0 + assert qtm.unit == "" + + def testing_dimensions(): print(f"{'*' * 25} {testing_dimensions.__name__} {'*' * 25}") @@ -551,24 +607,24 @@ def testing_properties(): if __name__ == "__main__": - # test_value_unit_1() - # testing_dimensions() - # testing_multipliers() - # testing_to_systems() - # testing_arithmetic_operators() - # testing_properties() - - x = q.Quantity(1, "ft") - print( - f"User unit: {x._unit.user_unit}, multiplier: {x._unit.si_factor}, reduced_si_unit: {x._unit.si_unit}, si_value: {x._si_value}" - ) - - g = q.Quantity(1, "g") - m = q.Quantity(1, "m") - print(f"{g} * {m} = {g * m}") - print("here ") - print(f"{m} * 2 = {m * 2}") - print(" ") + test_value_unit_1() + testing_dimensions() + testing_multipliers() + testing_to_systems() + testing_arithmetic_operators() + testing_properties() + # + # x = q.Quantity(1, "ft") + # print( + # f"User unit: {x._unit.user_unit}, multiplier: {x._unit.si_factor}, reduced_si_unit: {x._unit.si_unit}, si_value: {x._si_value}" + # ) + # + # g = q.Quantity(1, "g") + # m = q.Quantity(1, "m") + # print(f"{g} * {m} = {g * m}") + # print("here ") + # print(f"{m} * 2 = {m * 2}") + # print(" ") # l = q.Quantity(1, "cm") # r = q.Quantity(1, "") @@ -619,6 +675,3 @@ def testing_properties(): # print(q2 / q1) # print(q1 / 2) # print(2.0 / q1) - - q = q.Quantity(5, "m^0") - print(f"{q} * 2 = {q * 2}") From 2087a49e999ac879850e9d62d9d3742bf4d9cd69 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Thu, 3 Nov 2022 13:05:39 +0530 Subject: [PATCH 35/40] Updated __radd__ method --- src/ansys/fluent/core/quantity.py | 2 +- tests/test_quantity.py | 62 +++---------------------------- 2 files changed, 6 insertions(+), 58 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 71ee348277bd..bbf21a595529 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -680,7 +680,7 @@ def __add__(self, other): return Quantity(temp_value, self._si_unit) def __radd__(self, other): - return self.__add__(other) + return super().__add__(other) def __sub__(self, other): if isinstance(other, Quantity) and ( diff --git a/tests/test_quantity.py b/tests/test_quantity.py index a8fef7952043..e9eafeea2180 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -613,65 +613,13 @@ def testing_properties(): testing_to_systems() testing_arithmetic_operators() testing_properties() - # + # x = q.Quantity(1, "ft") # print( # f"User unit: {x._unit.user_unit}, multiplier: {x._unit.si_factor}, reduced_si_unit: {x._unit.si_unit}, si_value: {x._si_value}" # ) - # - # g = q.Quantity(1, "g") - # m = q.Quantity(1, "m") - # print(f"{g} * {m} = {g * m}") - # print("here ") - # print(f"{m} * 2 = {m * 2}") - # print(" ") - - # l = q.Quantity(1, "cm") - # r = q.Quantity(1, "") - # print(f"{l} + {r} = {l + r}") - # print(" ") - # tk = q.Quantity(300.0, "K") - # tr = q.Quantity(150.0, "R") - # - # print(tk.to("R")) - # print(tr.to("K")) - - # b = q.Quantity(10.5, "m") - # print(b) - q1 = q.Quantity(10.0, "m s^-1") - q2 = q.Quantity(5.0, "m s^-1") - # - # print(f"{q1} - {q2} = {q1 - q2}") - # print("") - # - # print(f"{q2} - {q1} = {q2 - q1}") - # print("") - # - # print(f"{float(q1)} - 2.0 = {float(q1) - 2.0}") - # print("") - # - # print(f"2.0 - {float(q1)} = {2.0 - float(q1)}") - # print("") - # - # print(f"{float(q1)} - 3 = {float(q1) - 3}") - # print("") - # - # print(f"3 - {float(q1)} = {3 - float(q1)}") - # print("") - # - # print(q1**2) - # print(q2**2) - # - # q3 = q.Quantity(10.0, "m s^-1") - # q4 = q.Quantity(10.0, "") - # - # print(q1 == q2) - # print(q1 == q3) - # print(float(q1) == 10.0) - # print(q4 == 10.0) - - # print(q1 / q2) - # print(q2 / q1) - # print(q1 / 2) - # print(2.0 / q1) + a = q.Quantity(3.0, "m") + c = q.Quantity(4.0, "") + # print(1 + "a") + print(f"{c} + 2 = {c + 2}") From 3161303c97ffb4175634c71c0ef3179ca24f05c6 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Thu, 3 Nov 2022 13:06:27 +0530 Subject: [PATCH 36/40] Updated __radd__ method --- tests/test_quantity.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_quantity.py b/tests/test_quantity.py index e9eafeea2180..318d305e52b9 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -623,3 +623,5 @@ def testing_properties(): c = q.Quantity(4.0, "") # print(1 + "a") print(f"{c} + 2 = {c + 2}") + print("") + print(f"2 + {c} = {2 + c}") From b0a4a178315feb67eecb2d299472cf556f5e5d69 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Thu, 3 Nov 2022 13:36:07 +0530 Subject: [PATCH 37/40] Added __neg__ method --- src/ansys/fluent/core/quantity.py | 3 +++ tests/test_quantity.py | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index bbf21a595529..62e47ecdd2fc 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -709,3 +709,6 @@ def __eq__(self, other): and isinstance(other, float) ): return float(self) == other + + def __neg__(self): + return Quantity(-self.value, self.unit) diff --git a/tests/test_quantity.py b/tests/test_quantity.py index 318d305e52b9..083a3f57c5f2 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -621,7 +621,9 @@ def testing_properties(): a = q.Quantity(3.0, "m") c = q.Quantity(4.0, "") - # print(1 + "a") + # print(a + "a") print(f"{c} + 2 = {c + 2}") print("") print(f"2 + {c} = {2 + c}") + + print(f"-{a} = {-a}") From 4894c3de85aa14cf9270fe285ebeb5ea03383639 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Thu, 3 Nov 2022 14:12:46 +0530 Subject: [PATCH 38/40] Updated __add__ method --- src/ansys/fluent/core/quantity.py | 20 +++++++++++--------- tests/test_quantity.py | 12 ++++++------ 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 62e47ecdd2fc..00c85ef05f1c 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -667,16 +667,18 @@ def __rtruediv__(self, other): return other / self def __add__(self, other): - if isinstance(other, Quantity) and ( - self.get_dimensions_list() == other.get_dimensions_list() - ): - temp_value = self._si_value + other._si_value - elif self.is_dimension_less() and ( - isinstance(other, int) or isinstance(other, float) - ): - temp_value = self._si_value + other + if isinstance(other, Quantity): + if self.get_dimensions_list() == other.get_dimensions_list(): + temp_value = float(self) + float(other) + else: + raise ValueError("Incompatible dimensions.") + elif isinstance(other, (float, int)): + if self.is_dimension_less(): + temp_value = self._si_value + other + else: + raise ValueError("Incompatible dimensions.") else: - raise ValueError("Incompatible dimensions.") + return super().__add__(other) return Quantity(temp_value, self._si_unit) def __radd__(self, other): diff --git a/tests/test_quantity.py b/tests/test_quantity.py index 083a3f57c5f2..12b702bda6b5 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -621,9 +621,9 @@ def testing_properties(): a = q.Quantity(3.0, "m") c = q.Quantity(4.0, "") - # print(a + "a") - print(f"{c} + 2 = {c + 2}") - print("") - print(f"2 + {c} = {2 + c}") - - print(f"-{a} = {-a}") + print(a + "a") + # print(f"{c} + 2 = {c + 2}") + # print("") + # print(f"2 + {c} = {2 + c}") + # + # print(f"-{a} = {-a}") From ea5ecd116487edbe3151f093c40eab67a8c0a35d Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Thu, 3 Nov 2022 16:01:33 +0530 Subject: [PATCH 39/40] Added more complex tests for temprature --- src/ansys/fluent/core/quantity.py | 16 +++++++ tests/test_quantity.py | 76 +++++++++++++++++++++++++------ 2 files changed, 77 insertions(+), 15 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 00c85ef05f1c..9ebc5bda89ae 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -46,12 +46,14 @@ class _UnitsTable(object): "psf": "lbf ft^-2", "ohm": "kg m^2 s^-3 A^-2", "Hz": "s^-1", + "cal": "J", } derived_units_with_conversion_factor = { "l": (0.001, "m^3"), "gal": (0.0037854117839999993, "m^3"), "BTU": (1055.056, "J"), + "cal": (4.184, "J"), } multipliers = { @@ -714,3 +716,17 @@ def __eq__(self, other): def __neg__(self): return Quantity(-self.value, self.unit) + + def __ge__(self, other): + if ( + isinstance(other, Quantity) + and self.get_dimensions_list() == other.get_dimensions_list() + and self._si_unit == other._si_unit + ): + return float(self) > float(other) + elif ( + self.is_dimension_less() + and (not isinstance(other, Quantity)) + and isinstance(other, (float, int)) + ): + return float(self) > other diff --git a/tests/test_quantity.py b/tests/test_quantity.py index 12b702bda6b5..69171f31b911 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -439,7 +439,46 @@ def test_temp_53(): assert k.value == pytest.approx(255.927, DELTA) -def test_power_54(): +def test_temp_54(): + hc = q.Quantity(1.0, "J g^-1 K^-1") + + hcto1 = hc.to("kJ kg^-1 K^-1") + + assert hcto1.value == pytest.approx(1.0, DELTA) + assert hcto1.unit == "kJ kg^-1 K^-1" + + hcto2 = hc.to("J kg^-1 C^-1") + + assert hcto2.value == pytest.approx(1000.0, DELTA) + assert hcto2.unit == "J kg^-1 C^-1" + + hcto3 = hc.to("kJ kg^-1 C^-1") + + assert hcto3.value == pytest.approx(1.0, DELTA) + assert hcto3.unit == "kJ kg^-1 C^-1" + + hcto4 = hc.to("cal g^-1 C^-1") + + assert hcto4.value == pytest.approx(0.2390057, DELTA) + assert hcto4.unit == "cal g^-1 C^-1" + + hcto5 = hc.to("cal kg^-1 C^-1") + + assert hcto5.value == pytest.approx(239.0057, DELTA) + assert hcto5.unit == "cal kg^-1 C^-1" + + hcto6 = hc.to("kcal kg^-1 C^-1") + + assert hcto6.value == pytest.approx(0.2390057, DELTA) + assert hcto6.unit == "kcal kg^-1 C^-1" + + hcto7 = hc.to("BTU lb^-1 F^-1") + + assert hcto7.value == pytest.approx(0.238845, DELTA) + assert hcto7.unit == "BTU lb^-1 F^-1" + + +def test_power_55(): qt = q.Quantity(5.0, "m^0") qtm = qt * 2 @@ -607,23 +646,30 @@ def testing_properties(): if __name__ == "__main__": - test_value_unit_1() - testing_dimensions() - testing_multipliers() - testing_to_systems() - testing_arithmetic_operators() - testing_properties() + # test_value_unit_1() + # testing_dimensions() + # testing_multipliers() + # testing_to_systems() + # testing_arithmetic_operators() + # testing_properties() # x = q.Quantity(1, "ft") # print( # f"User unit: {x._unit.user_unit}, multiplier: {x._unit.si_factor}, reduced_si_unit: {x._unit.si_unit}, si_value: {x._si_value}" # ) - a = q.Quantity(3.0, "m") - c = q.Quantity(4.0, "") - print(a + "a") - # print(f"{c} + 2 = {c + 2}") - # print("") - # print(f"2 + {c} = {2 + c}") - # - # print(f"-{a} = {-a}") + hc = q.Quantity(1.0, "J g^-1 K^-1") + + print(hc.to("kJ kg^-1 K^-1")) + + print(hc.to("J kg^-1 C^-1")) + + print(hc.to("kJ kg^-1 C^-1")) + + print(hc.to("cal g^-1 C^-1")) + + print(hc.to("cal kg^-1 C^-1")) + + print(hc.to("kcal kg^-1 C^-1")) + + print(hc.to("BTU lb^-1 F^-1")) From c14de81949271a15bc27f516f210a5d0535b239b Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Thu, 3 Nov 2022 18:41:28 +0530 Subject: [PATCH 40/40] Added tests for K^2 temprature cases --- tests/test_quantity.py | 74 ++++++++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 28 deletions(-) diff --git a/tests/test_quantity.py b/tests/test_quantity.py index 69171f31b911..bff828af6735 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -478,7 +478,41 @@ def test_temp_54(): assert hcto7.unit == "BTU lb^-1 F^-1" -def test_power_55(): +def test_temp_54(): + temp_var = q.Quantity(1.0, "kg m^-3 s^-1 K^2") + + temp_varto1 = temp_var.to("g cm^-3 s^-1 K^2") + + assert temp_varto1.value == pytest.approx(0.001, DELTA) + assert temp_varto1.unit == "g cm^-3 s^-1 K^2" + + temp_varto2 = temp_var.to("kg mm^-3 s^-1 K^2") + + assert temp_varto2.value == pytest.approx(1e-09, DELTA) + assert temp_varto2.unit == "kg mm^-3 s^-1 K^2" + + temp_varto3 = temp_var.to("kg um^-3 s^-1 K^2") + + assert temp_varto3.value == pytest.approx(9.999999999999999e-19, DELTA) + assert temp_varto3.unit == "kg um^-3 s^-1 K^2" + + temp_varto4 = temp_var.to("mg mm^-3 ms^-1 K^2") + + assert temp_varto4.value == pytest.approx(1.0000000000000002e-06, DELTA) + assert temp_varto4.unit == "mg mm^-3 ms^-1 K^2" + + temp_varto5 = temp_var.to("g cm^-3 us^-1 K^2") + + assert temp_varto5.value == pytest.approx(1e-09, DELTA) + assert temp_varto5.unit == "g cm^-3 us^-1 K^2" + + temp_varto6 = temp_var.to("pg um^-3 ms^-1 K^2") + + assert temp_varto6.value == pytest.approx(9.999999999999997e-07, DELTA) + assert temp_varto6.unit == "pg um^-3 ms^-1 K^2" + + +def test_power_56(): qt = q.Quantity(5.0, "m^0") qtm = qt * 2 @@ -645,31 +679,15 @@ def testing_properties(): qt2 = q.Quantity(5, "m s^-1") -if __name__ == "__main__": - # test_value_unit_1() - # testing_dimensions() - # testing_multipliers() - # testing_to_systems() - # testing_arithmetic_operators() - # testing_properties() - - # x = q.Quantity(1, "ft") - # print( - # f"User unit: {x._unit.user_unit}, multiplier: {x._unit.si_factor}, reduced_si_unit: {x._unit.si_unit}, si_value: {x._si_value}" - # ) - - hc = q.Quantity(1.0, "J g^-1 K^-1") - - print(hc.to("kJ kg^-1 K^-1")) - - print(hc.to("J kg^-1 C^-1")) - - print(hc.to("kJ kg^-1 C^-1")) - - print(hc.to("cal g^-1 C^-1")) - - print(hc.to("cal kg^-1 C^-1")) - - print(hc.to("kcal kg^-1 C^-1")) +# if __name__ == "__main__": +# test_value_unit_1() +# testing_dimensions() +# testing_multipliers() +# testing_to_systems() +# testing_arithmetic_operators() +# testing_properties() - print(hc.to("BTU lb^-1 F^-1")) +# x = q.Quantity(1, "ft") +# print( +# f"User unit: {x._unit.user_unit}, multiplier: {x._unit.si_factor}, reduced_si_unit: {x._unit.si_unit}, si_value: {x._si_value}" +# )