From 83b167a2e8257bceb3289402862f7c93b3458a2d Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Thu, 18 Aug 2022 20:49:10 +0530 Subject: [PATCH 01/21] Quantity (RealwithUnit) unit converter using pint module --- setup.py | 1 + src/ansys/fluent/core/quantity.py | 229 ++++++++++++++++++++++++++++++ tests/test_quantity.py | 146 +++++++++++++++++++ 3 files changed, 376 insertions(+) create mode 100644 src/ansys/fluent/core/quantity.py create mode 100644 tests/test_quantity.py diff --git a/setup.py b/setup.py index 831eb8b06ae5..cc40a6b66240 100644 --- a/setup.py +++ b/setup.py @@ -30,6 +30,7 @@ "appdirs>=1.4.0", "pandas>=1.1.5", "h5py>=3.7.0", + "pint>=0.19", ] diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py new file mode 100644 index 000000000000..754e3c632c54 --- /dev/null +++ b/src/ansys/fluent/core/quantity.py @@ -0,0 +1,229 @@ +import pint +from pint import Unit + +unit = pint.UnitRegistry(autoconvert_offset_to_baseunit=True, system="SI") +quantity = unit.Quantity + +restricted_conversions = ( + ("Hz", "rad/s"), + ("Hz", "radian/s"), + ("Hz", "rpm"), + ("Hz", "rps"), + ("Hz", "cps"), + ("hertz", "rad/s"), + ("hertz", "radian/s"), + ("hertz", "rpm"), + ("hertz", "rps"), + ("hertz", "cps"), + ("rpm", "Hz"), + ("rpm", "hertz"), + ("rpm", "rad/s"), + ("rpm", "radian/s"), + ("rpm", "rps"), + ("rpm", "cps"), + ("rps", "Hz"), + ("rps", "hertz"), + ("rps", "rad/s"), + ("rps", "radian/s"), + ("rps", "rpm"), + ("rps", "cps"), + ("cps", "Hz"), + ("cps", "hertz"), + ("cps", "rad/s"), + ("cps", "radian/s"), + ("cps", "rpm"), + ("cps", "rps"), + ("rad/s", "Hz"), + ("rad/s", "hertz"), + ("rad/s", "rpm"), + ("rad/s", "rps"), + ("rad/s", "cps"), + ("radian/s", "Hz"), + ("radian/s", "hertz"), + ("radian/s", "rpm"), + ("radian/s", "rps"), + ("radian/s", "cps"), +) + +restricted_unit_expansion = { + "Hz": [ + "yHz", + "zHz", + "aHz", + "fHz", + "pHz", + "nHz", + "uHz", + "cHz", + "dHz", + "daHz", + "hHz", + "mHz", + "kHz", + "MHz", + "GHz", + "THz", + "PHz", + "EHz", + "ZHz", + "YHz", + "Hz", + ], + "hertz": [ + "yhertz", + "zhertz", + "ahertz", + "fhertz", + "phertz", + "nhertz", + "uhertz", + "chertz", + "dhertz", + "dahertz", + "hhertz", + "mhertz", + "khertz", + "Mhertz", + "Ghertz", + "Thertz", + "Phertz", + "Ehertz", + "Zhertz", + "Yhertz", + "hertz", + ], + "rad/s": ["radian/s", "rad/s"], + "radian/s": ["rad/s", "radian/s"], + "rpm": ["revolutions_per_minute", "rpm"], + "rps": ["revolutions_per_second", "rps"], + "cps": ["counts_per_second", "cps"], +} + + +def build_restricted_conversions(conversions, unit_expansion): + + """This function generates required final restricted mappings.""" + + keys = set([i[0] for i in conversions]) + restricted_units_dict = {unit[0]: [] for unit in conversions} + + for key in keys: + temp_list = [unit_expansion[i] for i in keys.difference(set([key]))] + for temp in temp_list: + restricted_units_dict[key] += temp + for key in keys: + restricted_units_dict[key] = list(set(restricted_units_dict[key])) + return restricted_units_dict + + +restricted_units = build_restricted_conversions( + restricted_conversions, restricted_unit_expansion +) + + +class Quantity: + + """This class instantiates physical quantities using their real values and + units. + + All the instances of this class are converted to base SI units + system. Any conversion between Hz, hertz, rad/s, radian/s, + revolution/min, revolution/s, counts/s is restricted. + """ + + def __init__(self, real_value, units_string): + self._real_value = real_value + self._unit_string = units_string + self._quantity = quantity(self._real_value, self._unit_string) + self._base_si_quantity = self._quantity.to_base_units() + self._restricted_conversions = restricted_units + + def __float__(self): + return Quantity(self._real_value, self._unit_string) + + def __str__(self): + return f'({self._real_value}, "{self._unit_string}")' + + def __repr__(self): + return f'(Quantity ({self._real_value}, "{self._unit_string}"))' + + def __getitem__(self, unit): + + """This method checks the compatibility between current instance unit + and user provided unit, if both of them are compatible, then only it + performs required conversion otherwise raises a Value Error.""" + + if ( + self._unit_string in self._restricted_conversions.keys() + and unit in self._restricted_conversions[self._unit_string] + ): + raise ValueError( + f"Conversion between '{self._unit_string}' and '{unit}' is restricted." + ) + + user_unit = Unit(unit) + + if not self._quantity.is_compatible_with(user_unit): + raise ValueError("Units are not compatible") + + convert = self._quantity.to(unit) + + return Quantity(convert.magnitude, unit) + + def __mul__(self, other): + + if isinstance(other, Quantity): + temp = self._base_si_quantity * other._quantity + elif isinstance(other, int) or isinstance(other, float): + temp = quantity(self._base_si_quantity * other, self._unit_string) + return Quantity(temp.magnitude, temp.units) + + def __rmul__(self, other): + return self.__mul__(other) + + def __truediv__(self, other): + + if isinstance(other, Quantity): + temp = self._base_si_quantity / other._quantity + elif isinstance(other, int) or isinstance(other, float): + temp = quantity(self._base_si_quantity / other, self._unit_string) + return Quantity(temp.magnitude, temp.units) + + def __add__(self, other): + if self._unit_string in self._restricted_conversions.keys(): + raise ValueError("This arithmetic operation is retsricted") + + if isinstance(other, Quantity): + temp = self._base_si_quantity + other._quantity + elif isinstance(other, int) or isinstance(other, float): + temp = quantity( + self._base_si_quantity.magnitude + other, self._base_si_quantity.units + ) + return Quantity(temp.magnitude, temp.units) + + def __radd__(self, other): + return self.__add__(other) + + def __sub__(self, other): + if self._unit_string in self._restricted_conversions.keys(): + raise ValueError("This arithmetic operation is retsricted") + + if isinstance(other, Quantity): + temp = self._base_si_quantity - other._quantity + elif isinstance(other, int) or isinstance(other, float): + temp = quantity( + self._base_si_quantity.magnitude - other, self._base_si_quantity.units + ) + return Quantity(temp.magnitude, temp.units) + + def __rsub__(self, other): + if self._unit_string in self._restricted_conversions.keys(): + raise ValueError("This arithmetic operation is retsricted") + + if isinstance(other, Quantity): + temp = other._quantity - self._base_si_quantity + elif isinstance(other, int) or isinstance(other, float): + temp = quantity( + other - self._base_si_quantity.magnitude, self._base_si_quantity.units + ) + return Quantity(temp.magnitude, temp.units) diff --git a/tests/test_quantity.py b/tests/test_quantity.py new file mode 100644 index 000000000000..ee6780c4b492 --- /dev/null +++ b/tests/test_quantity.py @@ -0,0 +1,146 @@ +from pytest import approx + +import ansys.fluent.core.quantity as q + + +def test_viscosity(): + v = q.Quantity(1, "P") # poise + conversion_output = v["kg m^-1 s^-1"] + assert conversion_output._real_value == 0.1 + + +def test_volume(): + v = q.Quantity(1, "gal") + conversion_output = v["m^3"] + assert conversion_output._real_value == approx(0.00378541) + + +def test_youngs_modulus(): + ym = q.Quantity(1, "lbf ft^-2") + conversion_output = ym["N m^-2"] + assert conversion_output._real_value == approx(47.89, 0.1) + + +def test_temperature(): + tempC = q.Quantity(1, "degC") # celsius + conversion_output_1 = tempC["degK"] + assert conversion_output_1._real_value == 274.15 + conversion_output_2 = tempC["degR"] + assert conversion_output_2._real_value == approx(493.46, 0.1) + conversion_output_3 = tempC["degF"] + assert conversion_output_3._real_value == approx(33.79, 0.1) + + +def test_collision_rate(): + cr = q.Quantity(1, "ft^-3 s^-1") + conversion_output = cr["m^-3 s^-1"] + assert conversion_output._real_value == approx(35.3147, 0.001) + + +def test_area_inverse(): + in_sq_m = q.Quantity(1, "m^-2") + conversion_output = in_sq_m["cm^-2"] + assert conversion_output._real_value == approx(0.0001) + + +def test_area(): + in_sq_m = q.Quantity(1, "m^2") + conversion_output = in_sq_m["in^2"] + assert conversion_output._real_value == approx(1550, 0.1) + + +def test_angular_velocity(): + degps = q.Quantity(1, "deg/s") + conversion_output = degps["rad/s"] + assert conversion_output._real_value == approx(0.01745, 0.001) + + +if __name__ == "__main__": + v1 = q.Quantity(10.2, "m/s") # Instantiation + print(v1) + print(v1["cm/s"]) # conversion m/s to cm/s + print(v1["ft/s"]) # conversion m/s to ft/s + print(v1 + 15.7) # scalar addition + print(v1 - 5.9) # scalar subtraction + print(v1 * 2) # scalar multiplication + print(v1 / 2) # scalar division + + print("\n Quantity class instance arithmetic \n") + v2 = q.Quantity(15.9, "m/s") + print(v2 + v1) + print(v2 - v1) + print(v2 * v1) + print(v2 / v1) + + print("\n mass-flux \n") + rho1 = q.Quantity(1.225, "kg/m^3") + mass_flux1 = 0.2 * v1 * rho1 + print(mass_flux1) + + print("\n acceleration \n") + a1 = q.Quantity(1, "m/s^2") + print(a1) + print(a1["cm/s^2"]) # conversion m/s^-2 to cm/s^-2 + print(a1["ft/s^2"]) # conversion m/s^-2 to ft/s^-2 + + print("\n angle \n") + d1 = q.Quantity(1, "deg") + r1 = q.Quantity(1, "rad") + print(d1) + print(r1) + print(d1["rad"]) # conversion deg to rad + print(r1["deg"]) # conversion rad to deg + + print("\n angluar-velocity \n") + degps = q.Quantity(1, "deg/s") + radps = q.Quantity(1, "rad/s") + revpm = q.Quantity(1, "revolution/min") + print(radps["deg/s"]) + print(degps["rad/s"]) + + print("\n area \n") + sq_m = q.Quantity(1, "m^2") + print(sq_m) + print(sq_m["cm^2"]) + print(sq_m["mm^2"]) + print(sq_m["ft^2"]) + print(sq_m["in^2"]) + + print("\n area-inverse \n") + in_sq_m = q.Quantity(1, "m^-2") + print(in_sq_m) + print(in_sq_m["cm^-2"]) + print(in_sq_m["mm^-2"]) + print(in_sq_m["ft^-2"]) + print(in_sq_m["in^-2"]) + + print("\n collision-rate \n") + cr = q.Quantity(1, "ft^-3 s^-1") + print(cr) + print(cr["m^-3 s^-1"]) + + print("\n temperature \n") + tempC = q.Quantity(1, "degC") # celsius + print(tempC) + print(tempC["degK"]) # kelvin + print(tempC["degR"]) # rankine + print(tempC["degF"]) # fahrenheit + + print("\n youngs-modulus \n") + ym = q.Quantity(1, "lbf ft^-2") + print(ym) + print(ym["N m^-2"]) + + ym2 = q.Quantity(1, "dyn cm^-2") + print(ym2) + print(ym2["N m^-2"]) + + print("\n volume \n") + v = q.Quantity(1, "gal") + print(v) + print(v["m^3"]) + + print("\n viscosity \n") + v = q.Quantity(1, "P") # poise + print(v) + print(v["kg m^-1 s^-1"]) From efa94cf9b90f45ac7e3c853f56fdc4e78a7068c9 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Thu, 18 Aug 2022 21:09:24 +0530 Subject: [PATCH 02/21] Fixed a typo --- src/ansys/fluent/core/quantity.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 754e3c632c54..a347d9b7b0bd 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -191,7 +191,7 @@ def __truediv__(self, other): def __add__(self, other): if self._unit_string in self._restricted_conversions.keys(): - raise ValueError("This arithmetic operation is retsricted") + raise ValueError("This arithmetic operation is restricted") if isinstance(other, Quantity): temp = self._base_si_quantity + other._quantity @@ -206,7 +206,7 @@ def __radd__(self, other): def __sub__(self, other): if self._unit_string in self._restricted_conversions.keys(): - raise ValueError("This arithmetic operation is retsricted") + raise ValueError("This arithmetic operation is restricted") if isinstance(other, Quantity): temp = self._base_si_quantity - other._quantity @@ -218,7 +218,7 @@ def __sub__(self, other): def __rsub__(self, other): if self._unit_string in self._restricted_conversions.keys(): - raise ValueError("This arithmetic operation is retsricted") + raise ValueError("This arithmetic operation is restricted") if isinstance(other, Quantity): temp = other._quantity - self._base_si_quantity From 210524257baeb33ebbbbd96a15ae36284e764257 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Thu, 18 Aug 2022 21:31:07 +0530 Subject: [PATCH 03/21] Changed pint version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index cc40a6b66240..f1ca8107e156 100644 --- a/setup.py +++ b/setup.py @@ -30,7 +30,7 @@ "appdirs>=1.4.0", "pandas>=1.1.5", "h5py>=3.7.0", - "pint>=0.19", + "pint>=0.18", ] From 63c0ba929477f30f6023fc1af3d55f64864dd4bb Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Fri, 19 Aug 2022 12:27:45 +0530 Subject: [PATCH 04/21] Restricted conversions is generated as a list using standard list of prefixes --- src/ansys/fluent/core/quantity.py | 50 +++++-------------------------- 1 file changed, 8 insertions(+), 42 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index a347d9b7b0bd..96a58bd05e90 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -1,49 +1,15 @@ +from itertools import permutations + import pint from pint import Unit unit = pint.UnitRegistry(autoconvert_offset_to_baseunit=True, system="SI") quantity = unit.Quantity -restricted_conversions = ( - ("Hz", "rad/s"), - ("Hz", "radian/s"), - ("Hz", "rpm"), - ("Hz", "rps"), - ("Hz", "cps"), - ("hertz", "rad/s"), - ("hertz", "radian/s"), - ("hertz", "rpm"), - ("hertz", "rps"), - ("hertz", "cps"), - ("rpm", "Hz"), - ("rpm", "hertz"), - ("rpm", "rad/s"), - ("rpm", "radian/s"), - ("rpm", "rps"), - ("rpm", "cps"), - ("rps", "Hz"), - ("rps", "hertz"), - ("rps", "rad/s"), - ("rps", "radian/s"), - ("rps", "rpm"), - ("rps", "cps"), - ("cps", "Hz"), - ("cps", "hertz"), - ("cps", "rad/s"), - ("cps", "radian/s"), - ("cps", "rpm"), - ("cps", "rps"), - ("rad/s", "Hz"), - ("rad/s", "hertz"), - ("rad/s", "rpm"), - ("rad/s", "rps"), - ("rad/s", "cps"), - ("radian/s", "Hz"), - ("radian/s", "hertz"), - ("radian/s", "rpm"), - ("radian/s", "rps"), - ("radian/s", "cps"), -) + +restricted_units = ["Hz", "hertz", "rad/s", "radian/s", "rpm", "rps", "cps"] +restricted_conversions = list((permutations(restricted_units, 2))) + restricted_unit_expansion = { "Hz": [ @@ -127,8 +93,8 @@ class Quantity: units. All the instances of this class are converted to base SI units - system. Any conversion between Hz, hertz, rad/s, radian/s, - revolution/min, revolution/s, counts/s is restricted. + system. Any conversion between "Hz", "hertz", "rad/s", "radian/s", + "rpm", "rps", "cps" is restricted. """ def __init__(self, real_value, units_string): From 2ee93b9eea632f013835f1a90e18e3a270582359 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Fri, 19 Aug 2022 13:19:25 +0530 Subject: [PATCH 05/21] Checked test cases for restricted unit conversions --- tests/test_quantity.py | 270 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 261 insertions(+), 9 deletions(-) diff --git a/tests/test_quantity.py b/tests/test_quantity.py index ee6780c4b492..a1e478dc70b4 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -1,4 +1,4 @@ -from pytest import approx +import pytest import ansys.fluent.core.quantity as q @@ -12,13 +12,13 @@ def test_viscosity(): def test_volume(): v = q.Quantity(1, "gal") conversion_output = v["m^3"] - assert conversion_output._real_value == approx(0.00378541) + assert conversion_output._real_value == pytest.approx(0.00378541) def test_youngs_modulus(): ym = q.Quantity(1, "lbf ft^-2") conversion_output = ym["N m^-2"] - assert conversion_output._real_value == approx(47.89, 0.1) + assert conversion_output._real_value == pytest.approx(47.89, 0.1) def test_temperature(): @@ -26,33 +26,285 @@ def test_temperature(): conversion_output_1 = tempC["degK"] assert conversion_output_1._real_value == 274.15 conversion_output_2 = tempC["degR"] - assert conversion_output_2._real_value == approx(493.46, 0.1) + assert conversion_output_2._real_value == pytest.approx(493.46, 0.1) conversion_output_3 = tempC["degF"] - assert conversion_output_3._real_value == approx(33.79, 0.1) + assert conversion_output_3._real_value == pytest.approx(33.79, 0.1) def test_collision_rate(): cr = q.Quantity(1, "ft^-3 s^-1") conversion_output = cr["m^-3 s^-1"] - assert conversion_output._real_value == approx(35.3147, 0.001) + assert conversion_output._real_value == pytest.approx(35.3147, 0.001) def test_area_inverse(): in_sq_m = q.Quantity(1, "m^-2") conversion_output = in_sq_m["cm^-2"] - assert conversion_output._real_value == approx(0.0001) + assert conversion_output._real_value == pytest.approx(0.0001) def test_area(): in_sq_m = q.Quantity(1, "m^2") conversion_output = in_sq_m["in^2"] - assert conversion_output._real_value == approx(1550, 0.1) + assert conversion_output._real_value == pytest.approx(1550, 0.1) def test_angular_velocity(): degps = q.Quantity(1, "deg/s") conversion_output = degps["rad/s"] - assert conversion_output._real_value == approx(0.01745, 0.001) + assert conversion_output._real_value == pytest.approx(0.01745, 0.001) + + +def test_hz_hertz(): + f = q.Quantity(1, "Hz") + with pytest.raises(Exception) as e_info: + conversion_output = f["hertz"] + + +def test_hz_rad(): + f = q.Quantity(1, "Hz") + with pytest.raises(Exception) as e_info: + conversion_output = f["rad/s"] + + +def test_hz_radian(): + f = q.Quantity(1, "Hz") + with pytest.raises(Exception) as e_info: + conversion_output = f["radian/s"] + + +def test_hz_rpm(): + f = q.Quantity(1, "Hz") + with pytest.raises(Exception) as e_info: + conversion_output = f["rpm"] + + +def test_hz_rps(): + f = q.Quantity(1, "Hz") + with pytest.raises(Exception) as e_info: + conversion_output = f["rps"] + + +def test_hz_cps(): + f = q.Quantity(1, "Hz") + with pytest.raises(Exception) as e_info: + conversion_output = f["cps"] + + +def test_hertz_hz(): + f = q.Quantity(1, "hertz") + with pytest.raises(Exception) as e_info: + conversion_output = f["Hz"] + + +def test_hertz_rad(): + f = q.Quantity(1, "hertz") + with pytest.raises(Exception) as e_info: + conversion_output = f["rad/s"] + + +def test_hertz_radian(): + f = q.Quantity(1, "hertz") + with pytest.raises(Exception) as e_info: + conversion_output = f["radian/s"] + + +def test_hertz_rpm(): + f = q.Quantity(1, "hertz") + with pytest.raises(Exception) as e_info: + conversion_output = f["rpm"] + + +def test_hertz_rps(): + f = q.Quantity(1, "hertz") + with pytest.raises(Exception) as e_info: + conversion_output = f["rps"] + + +def test_hertz_cps(): + f = q.Quantity(1, "hertz") + with pytest.raises(Exception) as e_info: + conversion_output = f["cps"] + + +def test_rad_hz(): + f = q.Quantity(1, "rad/s") + with pytest.raises(Exception) as e_info: + conversion_output = f["Hz"] + + +def test_rad_hertz(): + f = q.Quantity(1, "rad/s") + with pytest.raises(Exception) as e_info: + conversion_output = f["hertz"] + + +def test_rad_radian(): + f = q.Quantity(1, "rad/s") + with pytest.raises(Exception) as e_info: + conversion_output = f["radian/s"] + + +def test_rad_rpm(): + f = q.Quantity(1, "rad/s") + with pytest.raises(Exception) as e_info: + conversion_output = f["rpm"] + + +def test_rad_rps(): + f = q.Quantity(1, "rad/s") + with pytest.raises(Exception) as e_info: + conversion_output = f["rps"] + + +def test_rad_cps(): + f = q.Quantity(1, "rad/s") + with pytest.raises(Exception) as e_info: + conversion_output = f["cps"] + + +def test_radian_hz(): + f = q.Quantity(1, "radian/s") + with pytest.raises(Exception) as e_info: + conversion_output = f["Hz"] + + +def test_radian_hertz(): + f = q.Quantity(1, "radian/s") + with pytest.raises(Exception) as e_info: + conversion_output = f["hertz"] + + +def test_radian_rad(): + f = q.Quantity(1, "radian/s") + with pytest.raises(Exception) as e_info: + conversion_output = f["rad/s"] + + +def test_radian_rpm(): + f = q.Quantity(1, "radian/s") + with pytest.raises(Exception) as e_info: + conversion_output = f["rpm"] + + +def test_radian_rps(): + f = q.Quantity(1, "radian/s") + with pytest.raises(Exception) as e_info: + conversion_output = f["rps"] + + +def test_radian_cps(): + f = q.Quantity(1, "radian/s") + with pytest.raises(Exception) as e_info: + conversion_output = f["cps"] + + +def test_rpm_hz(): + f = q.Quantity(1, "rpm") + with pytest.raises(Exception) as e_info: + conversion_output = f["Hz"] + + +def test_rpm_hertz(): + f = q.Quantity(1, "rpm") + with pytest.raises(Exception) as e_info: + conversion_output = f["hertz"] + + +def test_rpm_radian(): + f = q.Quantity(1, "rpm") + with pytest.raises(Exception) as e_info: + conversion_output = f["radian/s"] + + +def test_rpm_rad(): + f = q.Quantity(1, "rpm") + with pytest.raises(Exception) as e_info: + conversion_output = f["rad/s"] + + +def test_rpm_rps(): + f = q.Quantity(1, "rpm") + with pytest.raises(Exception) as e_info: + conversion_output = f["rps"] + + +def test_rpm_cps(): + f = q.Quantity(1, "rpm") + with pytest.raises(Exception) as e_info: + conversion_output = f["cps"] + + +def test_rps_hz(): + f = q.Quantity(1, "rps") + with pytest.raises(Exception) as e_info: + conversion_output = f["Hz"] + + +def test_rps_hertz(): + f = q.Quantity(1, "rps") + with pytest.raises(Exception) as e_info: + conversion_output = f["hertz"] + + +def test_rps_radian(): + f = q.Quantity(1, "rps") + with pytest.raises(Exception) as e_info: + conversion_output = f["radian/s"] + + +def test_rps_rad(): + f = q.Quantity(1, "rps") + with pytest.raises(Exception) as e_info: + conversion_output = f["rad/s"] + + +def test_rps_rpm(): + f = q.Quantity(1, "rps") + with pytest.raises(Exception) as e_info: + conversion_output = f["rpm"] + + +def test_rps_cps(): + f = q.Quantity(1, "rps") + with pytest.raises(Exception) as e_info: + conversion_output = f["cps"] + + +def test_cps_hz(): + f = q.Quantity(1, "cps") + with pytest.raises(Exception) as e_info: + conversion_output = f["Hz"] + + +def test_cps_hertz(): + f = q.Quantity(1, "rps") + with pytest.raises(Exception) as e_info: + conversion_output = f["hertz"] + + +def test_cps_radian(): + f = q.Quantity(1, "cps") + with pytest.raises(Exception) as e_info: + conversion_output = f["radian/s"] + + +def test_cps_rad(): + f = q.Quantity(1, "cps") + with pytest.raises(Exception) as e_info: + conversion_output = f["rad/s"] + + +def test_cps_rpm(): + f = q.Quantity(1, "cps") + with pytest.raises(Exception) as e_info: + conversion_output = f["rpm"] + + +def test_cps_rps(): + f = q.Quantity(1, "cps") + with pytest.raises(Exception) as e_info: + conversion_output = f["rps"] if __name__ == "__main__": From e5e6d87c3062ca88f37dad777f703a6ce3e6c249 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Fri, 19 Aug 2022 13:47:51 +0530 Subject: [PATCH 06/21] Added standard prefix list and generated combination of Hz and hertz units --- src/ansys/fluent/core/quantity.py | 73 ++++++++++++------------------- 1 file changed, 27 insertions(+), 46 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 96a58bd05e90..03624b37c034 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -11,53 +11,34 @@ restricted_conversions = list((permutations(restricted_units, 2))) +std_prefixes = [ + "y", + "z", + "a", + "f", + "p", + "n", + "u", + "c", + "d", + "da", + "h", + "m", + "k", + "M", + "G", + "T", + "P", + "E", + "Z", + "Y", + "", +] + + restricted_unit_expansion = { - "Hz": [ - "yHz", - "zHz", - "aHz", - "fHz", - "pHz", - "nHz", - "uHz", - "cHz", - "dHz", - "daHz", - "hHz", - "mHz", - "kHz", - "MHz", - "GHz", - "THz", - "PHz", - "EHz", - "ZHz", - "YHz", - "Hz", - ], - "hertz": [ - "yhertz", - "zhertz", - "ahertz", - "fhertz", - "phertz", - "nhertz", - "uhertz", - "chertz", - "dhertz", - "dahertz", - "hhertz", - "mhertz", - "khertz", - "Mhertz", - "Ghertz", - "Thertz", - "Phertz", - "Ehertz", - "Zhertz", - "Yhertz", - "hertz", - ], + "Hz": [prefix + "Hz" for prefix in std_prefixes], + "hertz": [prefix + "hertz" for prefix in std_prefixes], "rad/s": ["radian/s", "rad/s"], "radian/s": ["rad/s", "radian/s"], "rpm": ["revolutions_per_minute", "rpm"], From 432f5d2900596db7253502515949cfe4258fc564 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Fri, 19 Aug 2022 17:57:26 +0530 Subject: [PATCH 07/21] Index operator replaced with convert method and added more test cases including dynamic viscosity --- src/ansys/fluent/core/quantity.py | 36 ++--- tests/test_quantity.py | 238 +++++++++++++++++++----------- 2 files changed, 170 insertions(+), 104 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 03624b37c034..f22715485953 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -79,50 +79,50 @@ class Quantity: """ def __init__(self, real_value, units_string): - self._real_value = real_value - self._unit_string = units_string - self._quantity = quantity(self._real_value, self._unit_string) + self.value = real_value + self.unit = units_string + self._quantity = quantity(self.value, self.unit) self._base_si_quantity = self._quantity.to_base_units() self._restricted_conversions = restricted_units def __float__(self): - return Quantity(self._real_value, self._unit_string) + return Quantity(self.value, self.unit) def __str__(self): - return f'({self._real_value}, "{self._unit_string}")' + return f'({self.value}, "{self.unit}")' def __repr__(self): - return f'(Quantity ({self._real_value}, "{self._unit_string}"))' + return f'(Quantity ({self.value}, "{self.unit}"))' - def __getitem__(self, unit): + def convert(self, to_unit): """This method checks the compatibility between current instance unit and user provided unit, if both of them are compatible, then only it performs required conversion otherwise raises a Value Error.""" if ( - self._unit_string in self._restricted_conversions.keys() - and unit in self._restricted_conversions[self._unit_string] + self.unit in self._restricted_conversions.keys() + and to_unit in self._restricted_conversions[self.unit] ): raise ValueError( - f"Conversion between '{self._unit_string}' and '{unit}' is restricted." + f"Conversion between '{self.unit}' and '{to_unit}' is restricted." ) - user_unit = Unit(unit) + user_unit = Unit(to_unit) if not self._quantity.is_compatible_with(user_unit): raise ValueError("Units are not compatible") - convert = self._quantity.to(unit) + converted = self._quantity.to(to_unit) - return Quantity(convert.magnitude, unit) + return Quantity(converted.magnitude, to_unit) def __mul__(self, other): if isinstance(other, Quantity): temp = self._base_si_quantity * other._quantity elif isinstance(other, int) or isinstance(other, float): - temp = quantity(self._base_si_quantity * other, self._unit_string) + temp = quantity(self._base_si_quantity * other, self.unit) return Quantity(temp.magnitude, temp.units) def __rmul__(self, other): @@ -133,11 +133,11 @@ def __truediv__(self, other): if isinstance(other, Quantity): temp = self._base_si_quantity / other._quantity elif isinstance(other, int) or isinstance(other, float): - temp = quantity(self._base_si_quantity / other, self._unit_string) + temp = quantity(self._base_si_quantity / other, self.unit) return Quantity(temp.magnitude, temp.units) def __add__(self, other): - if self._unit_string in self._restricted_conversions.keys(): + if self.unit in self._restricted_conversions.keys(): raise ValueError("This arithmetic operation is restricted") if isinstance(other, Quantity): @@ -152,7 +152,7 @@ def __radd__(self, other): return self.__add__(other) def __sub__(self, other): - if self._unit_string in self._restricted_conversions.keys(): + if self.unit in self._restricted_conversions.keys(): raise ValueError("This arithmetic operation is restricted") if isinstance(other, Quantity): @@ -164,7 +164,7 @@ def __sub__(self, other): return Quantity(temp.magnitude, temp.units) def __rsub__(self, other): - if self._unit_string in self._restricted_conversions.keys(): + if self.unit in self._restricted_conversions.keys(): raise ValueError("This arithmetic operation is restricted") if isinstance(other, Quantity): diff --git a/tests/test_quantity.py b/tests/test_quantity.py index a1e478dc70b4..2c08719700e4 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -5,313 +5,379 @@ def test_viscosity(): v = q.Quantity(1, "P") # poise - conversion_output = v["kg m^-1 s^-1"] - assert conversion_output._real_value == 0.1 + conversion_output = v.convert("kg m^-1 s^-1") + assert conversion_output.value == 0.1 + + +def test_dynamic_viscosity(): + vd = q.Quantity(1, "Pa s") # poise + conversion_output = vd.convert("P") + assert conversion_output.value == 10.0 def test_volume(): v = q.Quantity(1, "gal") - conversion_output = v["m^3"] - assert conversion_output._real_value == pytest.approx(0.00378541) + conversion_output = v.convert("m^3") + assert conversion_output.value == pytest.approx(0.00378541) def test_youngs_modulus(): ym = q.Quantity(1, "lbf ft^-2") - conversion_output = ym["N m^-2"] - assert conversion_output._real_value == pytest.approx(47.89, 0.1) + conversion_output = ym.convert("N m^-2") + assert conversion_output.value == pytest.approx(47.89, 0.1) def test_temperature(): tempC = q.Quantity(1, "degC") # celsius - conversion_output_1 = tempC["degK"] - assert conversion_output_1._real_value == 274.15 - conversion_output_2 = tempC["degR"] - assert conversion_output_2._real_value == pytest.approx(493.46, 0.1) - conversion_output_3 = tempC["degF"] - assert conversion_output_3._real_value == pytest.approx(33.79, 0.1) + conversion_output_1 = tempC.convert("degK") + assert conversion_output_1.value == 274.15 + conversion_output_2 = tempC.convert("degR") + assert conversion_output_2.value == pytest.approx(493.46, 0.1) + conversion_output_3 = tempC.convert("degF") + assert conversion_output_3.value == pytest.approx(33.79, 0.1) def test_collision_rate(): cr = q.Quantity(1, "ft^-3 s^-1") - conversion_output = cr["m^-3 s^-1"] - assert conversion_output._real_value == pytest.approx(35.3147, 0.001) + conversion_output = cr.convert("m^-3 s^-1") + assert conversion_output.value == pytest.approx(35.3147, 0.001) def test_area_inverse(): in_sq_m = q.Quantity(1, "m^-2") - conversion_output = in_sq_m["cm^-2"] - assert conversion_output._real_value == pytest.approx(0.0001) + conversion_output = in_sq_m.convert("cm^-2") + assert conversion_output.value == pytest.approx(0.0001) def test_area(): in_sq_m = q.Quantity(1, "m^2") - conversion_output = in_sq_m["in^2"] - assert conversion_output._real_value == pytest.approx(1550, 0.1) + conversion_output = in_sq_m.convert("in^2") + assert conversion_output.value == pytest.approx(1550, 0.1) def test_angular_velocity(): degps = q.Quantity(1, "deg/s") - conversion_output = degps["rad/s"] - assert conversion_output._real_value == pytest.approx(0.01745, 0.001) + conversion_output = degps.convert("rad/s") + assert conversion_output.value == pytest.approx(0.01745, 0.001) def test_hz_hertz(): f = q.Quantity(1, "Hz") with pytest.raises(Exception) as e_info: - conversion_output = f["hertz"] + conversion_output = f.convert("hertz") def test_hz_rad(): f = q.Quantity(1, "Hz") with pytest.raises(Exception) as e_info: - conversion_output = f["rad/s"] + conversion_output = f.convert("rad/s") def test_hz_radian(): f = q.Quantity(1, "Hz") with pytest.raises(Exception) as e_info: - conversion_output = f["radian/s"] + conversion_output = f.convert("radian/s") def test_hz_rpm(): f = q.Quantity(1, "Hz") with pytest.raises(Exception) as e_info: - conversion_output = f["rpm"] + conversion_output = f.convert("rpm") def test_hz_rps(): f = q.Quantity(1, "Hz") with pytest.raises(Exception) as e_info: - conversion_output = f["rps"] + conversion_output = f.convert("rps") def test_hz_cps(): f = q.Quantity(1, "Hz") with pytest.raises(Exception) as e_info: - conversion_output = f["cps"] + conversion_output = f.convert("cps") def test_hertz_hz(): f = q.Quantity(1, "hertz") with pytest.raises(Exception) as e_info: - conversion_output = f["Hz"] + conversion_output = f.convert("Hz") def test_hertz_rad(): f = q.Quantity(1, "hertz") with pytest.raises(Exception) as e_info: - conversion_output = f["rad/s"] + conversion_output = f.convert("rad/s") def test_hertz_radian(): f = q.Quantity(1, "hertz") with pytest.raises(Exception) as e_info: - conversion_output = f["radian/s"] + conversion_output = f.convert("radian/s") def test_hertz_rpm(): f = q.Quantity(1, "hertz") with pytest.raises(Exception) as e_info: - conversion_output = f["rpm"] + conversion_output = f.convert("rpm") def test_hertz_rps(): f = q.Quantity(1, "hertz") with pytest.raises(Exception) as e_info: - conversion_output = f["rps"] + conversion_output = f.convert("rps") def test_hertz_cps(): f = q.Quantity(1, "hertz") with pytest.raises(Exception) as e_info: - conversion_output = f["cps"] + conversion_output = f.convert("cps") def test_rad_hz(): f = q.Quantity(1, "rad/s") with pytest.raises(Exception) as e_info: - conversion_output = f["Hz"] + conversion_output = f.convert("Hz") def test_rad_hertz(): f = q.Quantity(1, "rad/s") with pytest.raises(Exception) as e_info: - conversion_output = f["hertz"] + conversion_output = f.convert("hertz") def test_rad_radian(): f = q.Quantity(1, "rad/s") with pytest.raises(Exception) as e_info: - conversion_output = f["radian/s"] + conversion_output = f.convert("radian/s") def test_rad_rpm(): f = q.Quantity(1, "rad/s") with pytest.raises(Exception) as e_info: - conversion_output = f["rpm"] + conversion_output = f.convert("rpm") def test_rad_rps(): f = q.Quantity(1, "rad/s") with pytest.raises(Exception) as e_info: - conversion_output = f["rps"] + conversion_output = f.convert("rps") def test_rad_cps(): f = q.Quantity(1, "rad/s") with pytest.raises(Exception) as e_info: - conversion_output = f["cps"] + conversion_output = f.convert("cps") def test_radian_hz(): f = q.Quantity(1, "radian/s") with pytest.raises(Exception) as e_info: - conversion_output = f["Hz"] + conversion_output = f.convert("Hz") def test_radian_hertz(): f = q.Quantity(1, "radian/s") with pytest.raises(Exception) as e_info: - conversion_output = f["hertz"] + conversion_output = f.convert("hertz") def test_radian_rad(): f = q.Quantity(1, "radian/s") with pytest.raises(Exception) as e_info: - conversion_output = f["rad/s"] + conversion_output = f.convert("rad/s") def test_radian_rpm(): f = q.Quantity(1, "radian/s") with pytest.raises(Exception) as e_info: - conversion_output = f["rpm"] + conversion_output = f.convert("rpm") def test_radian_rps(): f = q.Quantity(1, "radian/s") with pytest.raises(Exception) as e_info: - conversion_output = f["rps"] + conversion_output = f.convert("rps") def test_radian_cps(): f = q.Quantity(1, "radian/s") with pytest.raises(Exception) as e_info: - conversion_output = f["cps"] + conversion_output = f.convert("cps") def test_rpm_hz(): f = q.Quantity(1, "rpm") with pytest.raises(Exception) as e_info: - conversion_output = f["Hz"] + conversion_output = f.convert("Hz") def test_rpm_hertz(): f = q.Quantity(1, "rpm") with pytest.raises(Exception) as e_info: - conversion_output = f["hertz"] + conversion_output = f.convert("hertz") def test_rpm_radian(): f = q.Quantity(1, "rpm") with pytest.raises(Exception) as e_info: - conversion_output = f["radian/s"] + conversion_output = f.convert("radian/s") def test_rpm_rad(): f = q.Quantity(1, "rpm") with pytest.raises(Exception) as e_info: - conversion_output = f["rad/s"] + conversion_output = f.convert("rad/s") def test_rpm_rps(): f = q.Quantity(1, "rpm") with pytest.raises(Exception) as e_info: - conversion_output = f["rps"] + conversion_output = f.convert("rps") def test_rpm_cps(): f = q.Quantity(1, "rpm") with pytest.raises(Exception) as e_info: - conversion_output = f["cps"] + conversion_output = f.convert("cps") def test_rps_hz(): f = q.Quantity(1, "rps") with pytest.raises(Exception) as e_info: - conversion_output = f["Hz"] + conversion_output = f.convert("Hz") def test_rps_hertz(): f = q.Quantity(1, "rps") with pytest.raises(Exception) as e_info: - conversion_output = f["hertz"] + conversion_output = f.convert("hertz") def test_rps_radian(): f = q.Quantity(1, "rps") with pytest.raises(Exception) as e_info: - conversion_output = f["radian/s"] + conversion_output = f.convert("radian/s") def test_rps_rad(): f = q.Quantity(1, "rps") with pytest.raises(Exception) as e_info: - conversion_output = f["rad/s"] + conversion_output = f.convert("rad/s") def test_rps_rpm(): f = q.Quantity(1, "rps") with pytest.raises(Exception) as e_info: - conversion_output = f["rpm"] + conversion_output = f.convert("rpm") def test_rps_cps(): f = q.Quantity(1, "rps") with pytest.raises(Exception) as e_info: - conversion_output = f["cps"] + conversion_output = f.convert("cps") def test_cps_hz(): f = q.Quantity(1, "cps") with pytest.raises(Exception) as e_info: - conversion_output = f["Hz"] + conversion_output = f.convert("Hz") def test_cps_hertz(): f = q.Quantity(1, "rps") with pytest.raises(Exception) as e_info: - conversion_output = f["hertz"] + conversion_output = f.convert("hertz") def test_cps_radian(): f = q.Quantity(1, "cps") with pytest.raises(Exception) as e_info: - conversion_output = f["radian/s"] + conversion_output = f.convert("radian/s") def test_cps_rad(): f = q.Quantity(1, "cps") with pytest.raises(Exception) as e_info: - conversion_output = f["rad/s"] + conversion_output = f.convert("rad/s") def test_cps_rpm(): f = q.Quantity(1, "cps") with pytest.raises(Exception) as e_info: - conversion_output = f["rpm"] + conversion_output = f.convert("rpm") def test_cps_rps(): f = q.Quantity(1, "cps") with pytest.raises(Exception) as e_info: - conversion_output = f["rps"] + conversion_output = f.convert("rps") + + +def test_dyne(): + x = q.Quantity(1, "dyn cm^-2") + conversion_output = x.convert("N m^-2") + assert conversion_output.value == pytest.approx(0.1) + + +def test_gal(): + x = q.Quantity(1, "gal^-1") + conversion_output = x.convert("m^-3") + assert conversion_output.value == pytest.approx(264.17, 0.002) + + +def test_mph(): + x = q.Quantity(1, "m s^-1") + conversion_output = x.convert("mph") + assert conversion_output.value == pytest.approx(2.23694, 0.00002) + + +def test_inches_water(): + x = q.Quantity(1, "inch_H2O_39F") + conversion_output = x.convert("Pa") + assert conversion_output.value == pytest.approx(249, 0.1) + + +def test_torr(): + x = q.Quantity(1, "torr") + conversion_output = x.convert("Pa") + assert conversion_output.value == pytest.approx(133.3220, 0.0003) + + +def test_psi(): + x = q.Quantity(1, "psi") + conversion_output = x.convert("Pa") + assert conversion_output.value == pytest.approx(6894.757, 0.0002) + + +def test_atm(): + x = q.Quantity(1, "atm") + conversion_output = x.convert("Pa") + assert conversion_output.value == pytest.approx(101325.0) + + +def test_mole_con_henry_const(): + x = q.Quantity(1, "atm m^3 kg mol^-1") + conversion_output = x.convert("Pa m^3 kg mol^-1") + assert conversion_output.value == pytest.approx(101325.0) + + +def test_pdl(): + x = q.Quantity(1, "pdl") + conversion_output = x.convert("N") + assert conversion_output.value == pytest.approx(0.13826, 0.0001) + + +def test_ozf(): + x = q.Quantity(1, "ozf") + conversion_output = x.convert("N") + assert conversion_output.value == pytest.approx(0.27802, 0.0001) if __name__ == "__main__": v1 = q.Quantity(10.2, "m/s") # Instantiation print(v1) - print(v1["cm/s"]) # conversion m/s to cm/s - print(v1["ft/s"]) # conversion m/s to ft/s + print(v1.convert("cm/s")) # conversion m/s to cm/s + print(v1.convert("ft/s")) # conversion m/s to ft/s print(v1 + 15.7) # scalar addition print(v1 - 5.9) # scalar subtraction print(v1 * 2) # scalar multiplication @@ -332,67 +398,67 @@ def test_cps_rps(): print("\n acceleration \n") a1 = q.Quantity(1, "m/s^2") print(a1) - print(a1["cm/s^2"]) # conversion m/s^-2 to cm/s^-2 - print(a1["ft/s^2"]) # conversion m/s^-2 to ft/s^-2 + print(a1.convert("cm/s^2")) # conversion m/s^-2 to cm/s^-2 + print(a1.convert("ft/s^2")) # conversion m/s^-2 to ft/s^-2 print("\n angle \n") d1 = q.Quantity(1, "deg") r1 = q.Quantity(1, "rad") print(d1) print(r1) - print(d1["rad"]) # conversion deg to rad - print(r1["deg"]) # conversion rad to deg + print(d1.convert("rad")) # conversion deg to rad + print(r1.convert("deg")) # conversion rad to deg print("\n angluar-velocity \n") degps = q.Quantity(1, "deg/s") radps = q.Quantity(1, "rad/s") revpm = q.Quantity(1, "revolution/min") - print(radps["deg/s"]) - print(degps["rad/s"]) + print(radps.convert("deg/s")) + print(degps.convert("rad/s")) print("\n area \n") sq_m = q.Quantity(1, "m^2") print(sq_m) - print(sq_m["cm^2"]) - print(sq_m["mm^2"]) - print(sq_m["ft^2"]) - print(sq_m["in^2"]) + print(sq_m.convert("cm^2")) + print(sq_m.convert("mm^2")) + print(sq_m.convert("ft^2")) + print(sq_m.convert("in^2")) print("\n area-inverse \n") in_sq_m = q.Quantity(1, "m^-2") print(in_sq_m) - print(in_sq_m["cm^-2"]) - print(in_sq_m["mm^-2"]) - print(in_sq_m["ft^-2"]) - print(in_sq_m["in^-2"]) + print(in_sq_m.convert("cm^-2")) + print(in_sq_m.convert("mm^-2")) + print(in_sq_m.convert("ft^-2")) + print(in_sq_m.convert("in^-2")) print("\n collision-rate \n") cr = q.Quantity(1, "ft^-3 s^-1") print(cr) - print(cr["m^-3 s^-1"]) + print(cr.convert("m^-3 s^-1")) print("\n temperature \n") tempC = q.Quantity(1, "degC") # celsius print(tempC) - print(tempC["degK"]) # kelvin - print(tempC["degR"]) # rankine - print(tempC["degF"]) # fahrenheit + print(tempC.convert("degK")) # kelvin + print(tempC.convert("degR")) # rankine + print(tempC.convert("degF")) # fahrenheit print("\n youngs-modulus \n") ym = q.Quantity(1, "lbf ft^-2") print(ym) - print(ym["N m^-2"]) + print(ym.convert("N m^-2")) ym2 = q.Quantity(1, "dyn cm^-2") print(ym2) - print(ym2["N m^-2"]) + print(ym2.convert("N m^-2")) print("\n volume \n") v = q.Quantity(1, "gal") print(v) - print(v["m^3"]) + print(v.convert("m^3")) print("\n viscosity \n") v = q.Quantity(1, "P") # poise print(v) - print(v["kg m^-1 s^-1"]) + print(v.convert("kg m^-1 s^-1")) From 6c277cd2a38c3c5d10f98044e916affbb36a2013 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Fri, 19 Aug 2022 18:01:54 +0530 Subject: [PATCH 08/21] Removed a comment --- tests/test_quantity.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_quantity.py b/tests/test_quantity.py index 2c08719700e4..5994f90a0979 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -10,7 +10,7 @@ def test_viscosity(): def test_dynamic_viscosity(): - vd = q.Quantity(1, "Pa s") # poise + vd = q.Quantity(1, "Pa s") conversion_output = vd.convert("P") assert conversion_output.value == 10.0 From e097763ee22f13213c91169d79efa5d7a97521cb Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Mon, 22 Aug 2022 14:20:24 +0530 Subject: [PATCH 09/21] Added test cases for remaining units --- tests/test_quantity.py | 98 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 97 insertions(+), 1 deletion(-) diff --git a/tests/test_quantity.py b/tests/test_quantity.py index 5994f90a0979..1f9edad0628f 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -15,6 +15,18 @@ def test_dynamic_viscosity(): assert conversion_output.value == 10.0 +def test_viscosity_slugs(): + vd = q.Quantity(1, "slugs ft^-1 s^-1") + conversion_output = vd.convert("kg m^-1 s^-1") + assert conversion_output.value == pytest.approx(47.880, 0.0002) + + +def test_viscosity_lb(): + vd = q.Quantity(1, "lb ft^-1 s^-1") + conversion_output = vd.convert("kg m^-1 s^-1") + assert conversion_output.value == pytest.approx(1.488164, 0.000001) + + def test_volume(): v = q.Quantity(1, "gal") conversion_output = v.convert("m^3") @@ -368,11 +380,95 @@ def test_pdl(): def test_ozf(): - x = q.Quantity(1, "ozf") + x = q.Quantity(1, "ozf") # force_ounce conversion_output = x.convert("N") assert conversion_output.value == pytest.approx(0.27802, 0.0001) +def test_lbf(): + x = q.Quantity(1, "lbf") # force_pound + conversion_output = x.convert("N") + assert conversion_output.value == pytest.approx(4.44820, 0.00002) + + +def test_kgf(): + x = q.Quantity(1, "kgf") + conversion_output = x.convert("N") + assert conversion_output.value == pytest.approx(9.806805, 0.0002) + + +def test_cal(): + x = q.Quantity(1, "cal") + conversion_output = x.convert("J") + assert conversion_output.value == pytest.approx(4.1868, 0.002) + + +def test_kcal(): + x = q.Quantity(1, "kcal") + conversion_output = x.convert("J") + assert conversion_output.value == pytest.approx(4186.8, 2) + + +def test_hp(): + x = q.Quantity(1, "hp") + conversion_output = x.convert("W") + assert conversion_output.value == pytest.approx(745.70, 0.01) + + +def test_ohm(): + x = q.Quantity(1, "ohm cm") + conversion_output = x.convert("ohm m") + assert conversion_output.value == 0.01 + + +def test_hp_h(): + x = q.Quantity(1, "hp h") # hp_hour + conversion_output = x.convert("J") + assert conversion_output.value == pytest.approx(2.6845 * 10**6, 19) + + +def test_erg(): + x = q.Quantity(1, "erg") + conversion_output = x.convert("J") + assert conversion_output.value == pytest.approx(1.0 * 10**-7) + + +def test_energy_density(): + x = q.Quantity(1, "BTU ft^-2") + conversion_output = x.convert("J m^-2") + assert conversion_output.value == pytest.approx(11356.36, 0.2) + + +def test_degree_rankine(): + x = q.Quantity(1, "BTU lb^-1 degR^-1") + conversion_output = x.convert("J kg^-1 degK^-1") + assert conversion_output.value == pytest.approx(4186.69, 0.2) + + +def test_degree_fahrenheit(): + x = q.Quantity(1, "BTU lb^-1 degF^-1") + conversion_output = x.convert("J kg^-1 degK^-1") + assert conversion_output.value == pytest.approx(4186.69, 0.2) + + +def test_degree_celsius(): + x = q.Quantity(1, "cal g^-1 degC^-1") + conversion_output = x.convert("J kg^-1 degK^-1") + assert conversion_output.value == pytest.approx(4186.69, 2) + + +def test_mol(): + x = q.Quantity(1, "lb mol ft^-3 s^-1") + conversion_output = x.convert("kg mol m^-3 s^-1") + assert conversion_output.value == pytest.approx(16.01846, 0.000003) + + +def test_rpm(): + x = q.Quantity(1, "rpm") + conversion_output = x.convert("rad s^-1") + assert conversion_output.value == pytest.approx(0.1047198, 0.000001) + + if __name__ == "__main__": v1 = q.Quantity(10.2, "m/s") # Instantiation print(v1) From 6847d6a3d7b3a998cfa46f2e955a82a681102751 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Mon, 22 Aug 2022 15:51:53 +0530 Subject: [PATCH 10/21] Fixed a typo --- src/ansys/fluent/core/quantity.py | 2 +- tests/test_quantity.py | 206 +++++++++++++++--------------- 2 files changed, 104 insertions(+), 104 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index f22715485953..0e04c2d9e26a 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -94,7 +94,7 @@ def __str__(self): def __repr__(self): return f'(Quantity ({self.value}, "{self.unit}"))' - def convert(self, to_unit): + def convertTo(self, to_unit): """This method checks the compatibility between current instance unit and user provided unit, if both of them are compatible, then only it diff --git a/tests/test_quantity.py b/tests/test_quantity.py index 1f9edad0628f..fef2d8194c22 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -5,475 +5,475 @@ def test_viscosity(): v = q.Quantity(1, "P") # poise - conversion_output = v.convert("kg m^-1 s^-1") + conversion_output = v.convertTo("kg m^-1 s^-1") assert conversion_output.value == 0.1 def test_dynamic_viscosity(): vd = q.Quantity(1, "Pa s") - conversion_output = vd.convert("P") + conversion_output = vd.convertTo("P") assert conversion_output.value == 10.0 def test_viscosity_slugs(): vd = q.Quantity(1, "slugs ft^-1 s^-1") - conversion_output = vd.convert("kg m^-1 s^-1") + conversion_output = vd.convertTo("kg m^-1 s^-1") assert conversion_output.value == pytest.approx(47.880, 0.0002) def test_viscosity_lb(): vd = q.Quantity(1, "lb ft^-1 s^-1") - conversion_output = vd.convert("kg m^-1 s^-1") + conversion_output = vd.convertTo("kg m^-1 s^-1") assert conversion_output.value == pytest.approx(1.488164, 0.000001) def test_volume(): v = q.Quantity(1, "gal") - conversion_output = v.convert("m^3") + conversion_output = v.convertTo("m^3") assert conversion_output.value == pytest.approx(0.00378541) def test_youngs_modulus(): ym = q.Quantity(1, "lbf ft^-2") - conversion_output = ym.convert("N m^-2") + conversion_output = ym.convertTo("N m^-2") assert conversion_output.value == pytest.approx(47.89, 0.1) def test_temperature(): tempC = q.Quantity(1, "degC") # celsius - conversion_output_1 = tempC.convert("degK") + conversion_output_1 = tempC.convertTo("degK") assert conversion_output_1.value == 274.15 - conversion_output_2 = tempC.convert("degR") + conversion_output_2 = tempC.convertTo("degR") assert conversion_output_2.value == pytest.approx(493.46, 0.1) - conversion_output_3 = tempC.convert("degF") + conversion_output_3 = tempC.convertTo("degF") assert conversion_output_3.value == pytest.approx(33.79, 0.1) def test_collision_rate(): cr = q.Quantity(1, "ft^-3 s^-1") - conversion_output = cr.convert("m^-3 s^-1") + conversion_output = cr.convertTo("m^-3 s^-1") assert conversion_output.value == pytest.approx(35.3147, 0.001) def test_area_inverse(): in_sq_m = q.Quantity(1, "m^-2") - conversion_output = in_sq_m.convert("cm^-2") + conversion_output = in_sq_m.convertTo("cm^-2") assert conversion_output.value == pytest.approx(0.0001) def test_area(): in_sq_m = q.Quantity(1, "m^2") - conversion_output = in_sq_m.convert("in^2") + conversion_output = in_sq_m.convertTo("in^2") assert conversion_output.value == pytest.approx(1550, 0.1) def test_angular_velocity(): degps = q.Quantity(1, "deg/s") - conversion_output = degps.convert("rad/s") + conversion_output = degps.convertTo("rad/s") assert conversion_output.value == pytest.approx(0.01745, 0.001) def test_hz_hertz(): f = q.Quantity(1, "Hz") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("hertz") + conversion_output = f.convertTo("hertz") def test_hz_rad(): f = q.Quantity(1, "Hz") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("rad/s") + conversion_output = f.convertTo("rad/s") def test_hz_radian(): f = q.Quantity(1, "Hz") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("radian/s") + conversion_output = f.convertTo("radian/s") def test_hz_rpm(): f = q.Quantity(1, "Hz") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("rpm") + conversion_output = f.convertTo("rpm") def test_hz_rps(): f = q.Quantity(1, "Hz") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("rps") + conversion_output = f.convertTo("rps") def test_hz_cps(): f = q.Quantity(1, "Hz") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("cps") + conversion_output = f.convertTo("cps") def test_hertz_hz(): f = q.Quantity(1, "hertz") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("Hz") + conversion_output = f.convertTo("Hz") def test_hertz_rad(): f = q.Quantity(1, "hertz") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("rad/s") + conversion_output = f.convertTo("rad/s") def test_hertz_radian(): f = q.Quantity(1, "hertz") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("radian/s") + conversion_output = f.convertTo("radian/s") def test_hertz_rpm(): f = q.Quantity(1, "hertz") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("rpm") + conversion_output = f.convertTo("rpm") def test_hertz_rps(): f = q.Quantity(1, "hertz") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("rps") + conversion_output = f.convertTo("rps") def test_hertz_cps(): f = q.Quantity(1, "hertz") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("cps") + conversion_output = f.convertTo("cps") def test_rad_hz(): f = q.Quantity(1, "rad/s") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("Hz") + conversion_output = f.convertTo("Hz") def test_rad_hertz(): f = q.Quantity(1, "rad/s") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("hertz") + conversion_output = f.convertTo("hertz") def test_rad_radian(): f = q.Quantity(1, "rad/s") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("radian/s") + conversion_output = f.convertTo("radian/s") def test_rad_rpm(): f = q.Quantity(1, "rad/s") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("rpm") + conversion_output = f.convertTo("rpm") def test_rad_rps(): f = q.Quantity(1, "rad/s") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("rps") + conversion_output = f.convertTo("rps") def test_rad_cps(): f = q.Quantity(1, "rad/s") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("cps") + conversion_output = f.convertTo("cps") def test_radian_hz(): f = q.Quantity(1, "radian/s") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("Hz") + conversion_output = f.convertTo("Hz") def test_radian_hertz(): f = q.Quantity(1, "radian/s") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("hertz") + conversion_output = f.convertTo("hertz") def test_radian_rad(): f = q.Quantity(1, "radian/s") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("rad/s") + conversion_output = f.convertTo("rad/s") def test_radian_rpm(): f = q.Quantity(1, "radian/s") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("rpm") + conversion_output = f.convertTo("rpm") def test_radian_rps(): f = q.Quantity(1, "radian/s") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("rps") + conversion_output = f.convertTo("rps") def test_radian_cps(): f = q.Quantity(1, "radian/s") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("cps") + conversion_output = f.convertTo("cps") def test_rpm_hz(): f = q.Quantity(1, "rpm") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("Hz") + conversion_output = f.convertTo("Hz") def test_rpm_hertz(): f = q.Quantity(1, "rpm") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("hertz") + conversion_output = f.convertTo("hertz") def test_rpm_radian(): f = q.Quantity(1, "rpm") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("radian/s") + conversion_output = f.convertTo("radian/s") def test_rpm_rad(): f = q.Quantity(1, "rpm") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("rad/s") + conversion_output = f.convertTo("rad/s") def test_rpm_rps(): f = q.Quantity(1, "rpm") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("rps") + conversion_output = f.convertTo("rps") def test_rpm_cps(): f = q.Quantity(1, "rpm") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("cps") + conversion_output = f.convertTo("cps") def test_rps_hz(): f = q.Quantity(1, "rps") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("Hz") + conversion_output = f.convertTo("Hz") def test_rps_hertz(): f = q.Quantity(1, "rps") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("hertz") + conversion_output = f.convertTo("hertz") def test_rps_radian(): f = q.Quantity(1, "rps") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("radian/s") + conversion_output = f.convertTo("radian/s") def test_rps_rad(): f = q.Quantity(1, "rps") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("rad/s") + conversion_output = f.convertTo("rad/s") def test_rps_rpm(): f = q.Quantity(1, "rps") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("rpm") + conversion_output = f.convertTo("rpm") def test_rps_cps(): f = q.Quantity(1, "rps") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("cps") + conversion_output = f.convertTo("cps") def test_cps_hz(): f = q.Quantity(1, "cps") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("Hz") + conversion_output = f.convertTo("Hz") def test_cps_hertz(): f = q.Quantity(1, "rps") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("hertz") + conversion_output = f.convertTo("hertz") def test_cps_radian(): f = q.Quantity(1, "cps") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("radian/s") + conversion_output = f.convertTo("radian/s") def test_cps_rad(): f = q.Quantity(1, "cps") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("rad/s") + conversion_output = f.convertTo("rad/s") def test_cps_rpm(): f = q.Quantity(1, "cps") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("rpm") + conversion_output = f.convertTo("rpm") def test_cps_rps(): f = q.Quantity(1, "cps") with pytest.raises(Exception) as e_info: - conversion_output = f.convert("rps") + conversion_output = f.convertTo("rps") def test_dyne(): x = q.Quantity(1, "dyn cm^-2") - conversion_output = x.convert("N m^-2") + conversion_output = x.convertTo("N m^-2") assert conversion_output.value == pytest.approx(0.1) def test_gal(): x = q.Quantity(1, "gal^-1") - conversion_output = x.convert("m^-3") + conversion_output = x.convertTo("m^-3") assert conversion_output.value == pytest.approx(264.17, 0.002) def test_mph(): x = q.Quantity(1, "m s^-1") - conversion_output = x.convert("mph") + conversion_output = x.convertTo("mph") assert conversion_output.value == pytest.approx(2.23694, 0.00002) def test_inches_water(): x = q.Quantity(1, "inch_H2O_39F") - conversion_output = x.convert("Pa") + conversion_output = x.convertTo("Pa") assert conversion_output.value == pytest.approx(249, 0.1) def test_torr(): x = q.Quantity(1, "torr") - conversion_output = x.convert("Pa") + conversion_output = x.convertTo("Pa") assert conversion_output.value == pytest.approx(133.3220, 0.0003) def test_psi(): x = q.Quantity(1, "psi") - conversion_output = x.convert("Pa") + conversion_output = x.convertTo("Pa") assert conversion_output.value == pytest.approx(6894.757, 0.0002) def test_atm(): x = q.Quantity(1, "atm") - conversion_output = x.convert("Pa") + conversion_output = x.convertTo("Pa") assert conversion_output.value == pytest.approx(101325.0) def test_mole_con_henry_const(): x = q.Quantity(1, "atm m^3 kg mol^-1") - conversion_output = x.convert("Pa m^3 kg mol^-1") + conversion_output = x.convertTo("Pa m^3 kg mol^-1") assert conversion_output.value == pytest.approx(101325.0) def test_pdl(): x = q.Quantity(1, "pdl") - conversion_output = x.convert("N") + conversion_output = x.convertTo("N") assert conversion_output.value == pytest.approx(0.13826, 0.0001) def test_ozf(): x = q.Quantity(1, "ozf") # force_ounce - conversion_output = x.convert("N") + conversion_output = x.convertTo("N") assert conversion_output.value == pytest.approx(0.27802, 0.0001) def test_lbf(): x = q.Quantity(1, "lbf") # force_pound - conversion_output = x.convert("N") + conversion_output = x.convertTo("N") assert conversion_output.value == pytest.approx(4.44820, 0.00002) def test_kgf(): x = q.Quantity(1, "kgf") - conversion_output = x.convert("N") + conversion_output = x.convertTo("N") assert conversion_output.value == pytest.approx(9.806805, 0.0002) def test_cal(): x = q.Quantity(1, "cal") - conversion_output = x.convert("J") + conversion_output = x.convertTo("J") assert conversion_output.value == pytest.approx(4.1868, 0.002) def test_kcal(): x = q.Quantity(1, "kcal") - conversion_output = x.convert("J") + conversion_output = x.convertTo("J") assert conversion_output.value == pytest.approx(4186.8, 2) def test_hp(): x = q.Quantity(1, "hp") - conversion_output = x.convert("W") + conversion_output = x.convertTo("W") assert conversion_output.value == pytest.approx(745.70, 0.01) def test_ohm(): x = q.Quantity(1, "ohm cm") - conversion_output = x.convert("ohm m") + conversion_output = x.convertTo("ohm m") assert conversion_output.value == 0.01 def test_hp_h(): x = q.Quantity(1, "hp h") # hp_hour - conversion_output = x.convert("J") + conversion_output = x.convertTo("J") assert conversion_output.value == pytest.approx(2.6845 * 10**6, 19) def test_erg(): x = q.Quantity(1, "erg") - conversion_output = x.convert("J") + conversion_output = x.convertTo("J") assert conversion_output.value == pytest.approx(1.0 * 10**-7) def test_energy_density(): x = q.Quantity(1, "BTU ft^-2") - conversion_output = x.convert("J m^-2") + conversion_output = x.convertTo("J m^-2") assert conversion_output.value == pytest.approx(11356.36, 0.2) def test_degree_rankine(): x = q.Quantity(1, "BTU lb^-1 degR^-1") - conversion_output = x.convert("J kg^-1 degK^-1") + conversion_output = x.convertTo("J kg^-1 degK^-1") assert conversion_output.value == pytest.approx(4186.69, 0.2) def test_degree_fahrenheit(): x = q.Quantity(1, "BTU lb^-1 degF^-1") - conversion_output = x.convert("J kg^-1 degK^-1") + conversion_output = x.convertTo("J kg^-1 degK^-1") assert conversion_output.value == pytest.approx(4186.69, 0.2) def test_degree_celsius(): x = q.Quantity(1, "cal g^-1 degC^-1") - conversion_output = x.convert("J kg^-1 degK^-1") + conversion_output = x.convertTo("J kg^-1 degK^-1") assert conversion_output.value == pytest.approx(4186.69, 2) def test_mol(): x = q.Quantity(1, "lb mol ft^-3 s^-1") - conversion_output = x.convert("kg mol m^-3 s^-1") + conversion_output = x.convertTo("kg mol m^-3 s^-1") assert conversion_output.value == pytest.approx(16.01846, 0.000003) def test_rpm(): x = q.Quantity(1, "rpm") - conversion_output = x.convert("rad s^-1") + conversion_output = x.convertTo("rad s^-1") assert conversion_output.value == pytest.approx(0.1047198, 0.000001) if __name__ == "__main__": v1 = q.Quantity(10.2, "m/s") # Instantiation print(v1) - print(v1.convert("cm/s")) # conversion m/s to cm/s - print(v1.convert("ft/s")) # conversion m/s to ft/s + print(v1.convertTo("cm/s")) # conversion m/s to cm/s + print(v1.convertTo("ft/s")) # conversion m/s to ft/s print(v1 + 15.7) # scalar addition print(v1 - 5.9) # scalar subtraction print(v1 * 2) # scalar multiplication @@ -494,67 +494,67 @@ def test_rpm(): print("\n acceleration \n") a1 = q.Quantity(1, "m/s^2") print(a1) - print(a1.convert("cm/s^2")) # conversion m/s^-2 to cm/s^-2 - print(a1.convert("ft/s^2")) # conversion m/s^-2 to ft/s^-2 + print(a1.convertTo("cm/s^2")) # conversion m/s^-2 to cm/s^-2 + print(a1.convertTo("ft/s^2")) # conversion m/s^-2 to ft/s^-2 print("\n angle \n") d1 = q.Quantity(1, "deg") r1 = q.Quantity(1, "rad") print(d1) print(r1) - print(d1.convert("rad")) # conversion deg to rad - print(r1.convert("deg")) # conversion rad to deg + print(d1.convertTo("rad")) # conversion deg to rad + print(r1.convertTo("deg")) # conversion rad to deg print("\n angluar-velocity \n") degps = q.Quantity(1, "deg/s") radps = q.Quantity(1, "rad/s") revpm = q.Quantity(1, "revolution/min") - print(radps.convert("deg/s")) - print(degps.convert("rad/s")) + print(radps.convertTo("deg/s")) + print(degps.convertTo("rad/s")) print("\n area \n") sq_m = q.Quantity(1, "m^2") print(sq_m) - print(sq_m.convert("cm^2")) - print(sq_m.convert("mm^2")) - print(sq_m.convert("ft^2")) - print(sq_m.convert("in^2")) + print(sq_m.convertTo("cm^2")) + print(sq_m.convertTo("mm^2")) + print(sq_m.convertTo("ft^2")) + print(sq_m.convertTo("in^2")) print("\n area-inverse \n") in_sq_m = q.Quantity(1, "m^-2") print(in_sq_m) - print(in_sq_m.convert("cm^-2")) - print(in_sq_m.convert("mm^-2")) - print(in_sq_m.convert("ft^-2")) - print(in_sq_m.convert("in^-2")) + print(in_sq_m.convertTo("cm^-2")) + print(in_sq_m.convertTo("mm^-2")) + print(in_sq_m.convertTo("ft^-2")) + print(in_sq_m.convertTo("in^-2")) print("\n collision-rate \n") cr = q.Quantity(1, "ft^-3 s^-1") print(cr) - print(cr.convert("m^-3 s^-1")) + print(cr.convertTo("m^-3 s^-1")) print("\n temperature \n") tempC = q.Quantity(1, "degC") # celsius print(tempC) - print(tempC.convert("degK")) # kelvin - print(tempC.convert("degR")) # rankine - print(tempC.convert("degF")) # fahrenheit + print(tempC.convertTo("degK")) # kelvin + print(tempC.convertTo("degR")) # rankine + print(tempC.convertTo("degF")) # fahrenheit print("\n youngs-modulus \n") ym = q.Quantity(1, "lbf ft^-2") print(ym) - print(ym.convert("N m^-2")) + print(ym.convertTo("N m^-2")) ym2 = q.Quantity(1, "dyn cm^-2") print(ym2) - print(ym2.convert("N m^-2")) + print(ym2.convertTo("N m^-2")) print("\n volume \n") v = q.Quantity(1, "gal") print(v) - print(v.convert("m^3")) + print(v.convertTo("m^3")) print("\n viscosity \n") v = q.Quantity(1, "P") # poise print(v) - print(v.convert("kg m^-1 s^-1")) + print(v.convertTo("kg m^-1 s^-1")) From 8e09456733a6fd8b3964773088afc5112393546d Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Mon, 22 Aug 2022 19:35:03 +0530 Subject: [PATCH 11/21] Defined new units and convertTo replaced with to --- src/ansys/fluent/core/quantity.py | 12 +- tests/test_quantity.py | 236 +++++++++++++++++------------- 2 files changed, 143 insertions(+), 105 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 0e04c2d9e26a..dee9efbb78db 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -3,7 +3,15 @@ import pint from pint import Unit -unit = pint.UnitRegistry(autoconvert_offset_to_baseunit=True, system="SI") +unit = pint.UnitRegistry(autoconvert_offset_to_baseunit=True) +unit.default_system = "SI" + +# Unit definitions +unit.define("1.e15-particles/kg = 1 kg^-1 = 1^15particles kg^-1") +unit.define("1.e15-particles m^-3 s^-1 = 1 m^-3 s^-1 = 1^15particles m^-3 s^-1") +unit.define("ton_force = 1 UK_force_ton = tonf") + + quantity = unit.Quantity @@ -94,7 +102,7 @@ def __str__(self): def __repr__(self): return f'(Quantity ({self.value}, "{self.unit}"))' - def convertTo(self, to_unit): + def to(self, to_unit): """This method checks the compatibility between current instance unit and user provided unit, if both of them are compatible, then only it diff --git a/tests/test_quantity.py b/tests/test_quantity.py index fef2d8194c22..6ae7401e4a3c 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -5,475 +5,505 @@ def test_viscosity(): v = q.Quantity(1, "P") # poise - conversion_output = v.convertTo("kg m^-1 s^-1") + conversion_output = v.to("kg m^-1 s^-1") assert conversion_output.value == 0.1 def test_dynamic_viscosity(): vd = q.Quantity(1, "Pa s") - conversion_output = vd.convertTo("P") + conversion_output = vd.to("P") assert conversion_output.value == 10.0 def test_viscosity_slugs(): vd = q.Quantity(1, "slugs ft^-1 s^-1") - conversion_output = vd.convertTo("kg m^-1 s^-1") + conversion_output = vd.to("kg m^-1 s^-1") assert conversion_output.value == pytest.approx(47.880, 0.0002) def test_viscosity_lb(): vd = q.Quantity(1, "lb ft^-1 s^-1") - conversion_output = vd.convertTo("kg m^-1 s^-1") + conversion_output = vd.to("kg m^-1 s^-1") assert conversion_output.value == pytest.approx(1.488164, 0.000001) def test_volume(): v = q.Quantity(1, "gal") - conversion_output = v.convertTo("m^3") + conversion_output = v.to("m^3") assert conversion_output.value == pytest.approx(0.00378541) def test_youngs_modulus(): ym = q.Quantity(1, "lbf ft^-2") - conversion_output = ym.convertTo("N m^-2") + conversion_output = ym.to("N m^-2") assert conversion_output.value == pytest.approx(47.89, 0.1) def test_temperature(): tempC = q.Quantity(1, "degC") # celsius - conversion_output_1 = tempC.convertTo("degK") + conversion_output_1 = tempC.to("degK") assert conversion_output_1.value == 274.15 - conversion_output_2 = tempC.convertTo("degR") + conversion_output_2 = tempC.to("degR") assert conversion_output_2.value == pytest.approx(493.46, 0.1) - conversion_output_3 = tempC.convertTo("degF") + conversion_output_3 = tempC.to("degF") assert conversion_output_3.value == pytest.approx(33.79, 0.1) def test_collision_rate(): cr = q.Quantity(1, "ft^-3 s^-1") - conversion_output = cr.convertTo("m^-3 s^-1") + conversion_output = cr.to("m^-3 s^-1") assert conversion_output.value == pytest.approx(35.3147, 0.001) def test_area_inverse(): in_sq_m = q.Quantity(1, "m^-2") - conversion_output = in_sq_m.convertTo("cm^-2") + conversion_output = in_sq_m.to("cm^-2") assert conversion_output.value == pytest.approx(0.0001) def test_area(): in_sq_m = q.Quantity(1, "m^2") - conversion_output = in_sq_m.convertTo("in^2") + conversion_output = in_sq_m.to("in^2") assert conversion_output.value == pytest.approx(1550, 0.1) def test_angular_velocity(): degps = q.Quantity(1, "deg/s") - conversion_output = degps.convertTo("rad/s") + conversion_output = degps.to("rad/s") assert conversion_output.value == pytest.approx(0.01745, 0.001) def test_hz_hertz(): f = q.Quantity(1, "Hz") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("hertz") + conversion_output = f.to("hertz") def test_hz_rad(): f = q.Quantity(1, "Hz") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("rad/s") + conversion_output = f.to("rad/s") def test_hz_radian(): f = q.Quantity(1, "Hz") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("radian/s") + conversion_output = f.to("radian/s") def test_hz_rpm(): f = q.Quantity(1, "Hz") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("rpm") + conversion_output = f.to("rpm") def test_hz_rps(): f = q.Quantity(1, "Hz") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("rps") + conversion_output = f.to("rps") def test_hz_cps(): f = q.Quantity(1, "Hz") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("cps") + conversion_output = f.to("cps") def test_hertz_hz(): f = q.Quantity(1, "hertz") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("Hz") + conversion_output = f.to("Hz") def test_hertz_rad(): f = q.Quantity(1, "hertz") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("rad/s") + conversion_output = f.to("rad/s") def test_hertz_radian(): f = q.Quantity(1, "hertz") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("radian/s") + conversion_output = f.to("radian/s") def test_hertz_rpm(): f = q.Quantity(1, "hertz") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("rpm") + conversion_output = f.to("rpm") def test_hertz_rps(): f = q.Quantity(1, "hertz") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("rps") + conversion_output = f.to("rps") def test_hertz_cps(): f = q.Quantity(1, "hertz") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("cps") + conversion_output = f.to("cps") def test_rad_hz(): f = q.Quantity(1, "rad/s") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("Hz") + conversion_output = f.to("Hz") def test_rad_hertz(): f = q.Quantity(1, "rad/s") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("hertz") + conversion_output = f.to("hertz") def test_rad_radian(): f = q.Quantity(1, "rad/s") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("radian/s") + conversion_output = f.to("radian/s") def test_rad_rpm(): f = q.Quantity(1, "rad/s") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("rpm") + conversion_output = f.to("rpm") def test_rad_rps(): f = q.Quantity(1, "rad/s") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("rps") + conversion_output = f.to("rps") def test_rad_cps(): f = q.Quantity(1, "rad/s") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("cps") + conversion_output = f.to("cps") def test_radian_hz(): f = q.Quantity(1, "radian/s") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("Hz") + conversion_output = f.to("Hz") def test_radian_hertz(): f = q.Quantity(1, "radian/s") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("hertz") + conversion_output = f.to("hertz") def test_radian_rad(): f = q.Quantity(1, "radian/s") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("rad/s") + conversion_output = f.to("rad/s") def test_radian_rpm(): f = q.Quantity(1, "radian/s") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("rpm") + conversion_output = f.to("rpm") def test_radian_rps(): f = q.Quantity(1, "radian/s") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("rps") + conversion_output = f.to("rps") def test_radian_cps(): f = q.Quantity(1, "radian/s") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("cps") + conversion_output = f.to("cps") def test_rpm_hz(): f = q.Quantity(1, "rpm") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("Hz") + conversion_output = f.to("Hz") def test_rpm_hertz(): f = q.Quantity(1, "rpm") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("hertz") + conversion_output = f.to("hertz") def test_rpm_radian(): f = q.Quantity(1, "rpm") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("radian/s") + conversion_output = f.to("radian/s") def test_rpm_rad(): f = q.Quantity(1, "rpm") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("rad/s") + conversion_output = f.to("rad/s") def test_rpm_rps(): f = q.Quantity(1, "rpm") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("rps") + conversion_output = f.to("rps") def test_rpm_cps(): f = q.Quantity(1, "rpm") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("cps") + conversion_output = f.to("cps") def test_rps_hz(): f = q.Quantity(1, "rps") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("Hz") + conversion_output = f.to("Hz") def test_rps_hertz(): f = q.Quantity(1, "rps") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("hertz") + conversion_output = f.to("hertz") def test_rps_radian(): f = q.Quantity(1, "rps") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("radian/s") + conversion_output = f.to("radian/s") def test_rps_rad(): f = q.Quantity(1, "rps") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("rad/s") + conversion_output = f.to("rad/s") def test_rps_rpm(): f = q.Quantity(1, "rps") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("rpm") + conversion_output = f.to("rpm") def test_rps_cps(): f = q.Quantity(1, "rps") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("cps") + conversion_output = f.to("cps") def test_cps_hz(): f = q.Quantity(1, "cps") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("Hz") + conversion_output = f.to("Hz") def test_cps_hertz(): f = q.Quantity(1, "rps") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("hertz") + conversion_output = f.to("hertz") def test_cps_radian(): f = q.Quantity(1, "cps") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("radian/s") + conversion_output = f.to("radian/s") def test_cps_rad(): f = q.Quantity(1, "cps") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("rad/s") + conversion_output = f.to("rad/s") def test_cps_rpm(): f = q.Quantity(1, "cps") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("rpm") + conversion_output = f.to("rpm") def test_cps_rps(): f = q.Quantity(1, "cps") with pytest.raises(Exception) as e_info: - conversion_output = f.convertTo("rps") + conversion_output = f.to("rps") def test_dyne(): x = q.Quantity(1, "dyn cm^-2") - conversion_output = x.convertTo("N m^-2") + conversion_output = x.to("N m^-2") assert conversion_output.value == pytest.approx(0.1) def test_gal(): x = q.Quantity(1, "gal^-1") - conversion_output = x.convertTo("m^-3") + conversion_output = x.to("m^-3") assert conversion_output.value == pytest.approx(264.17, 0.002) def test_mph(): x = q.Quantity(1, "m s^-1") - conversion_output = x.convertTo("mph") + conversion_output = x.to("mph") assert conversion_output.value == pytest.approx(2.23694, 0.00002) def test_inches_water(): x = q.Quantity(1, "inch_H2O_39F") - conversion_output = x.convertTo("Pa") + conversion_output = x.to("Pa") assert conversion_output.value == pytest.approx(249, 0.1) def test_torr(): x = q.Quantity(1, "torr") - conversion_output = x.convertTo("Pa") + conversion_output = x.to("Pa") assert conversion_output.value == pytest.approx(133.3220, 0.0003) def test_psi(): x = q.Quantity(1, "psi") - conversion_output = x.convertTo("Pa") + conversion_output = x.to("Pa") assert conversion_output.value == pytest.approx(6894.757, 0.0002) def test_atm(): x = q.Quantity(1, "atm") - conversion_output = x.convertTo("Pa") + conversion_output = x.to("Pa") assert conversion_output.value == pytest.approx(101325.0) def test_mole_con_henry_const(): x = q.Quantity(1, "atm m^3 kg mol^-1") - conversion_output = x.convertTo("Pa m^3 kg mol^-1") + conversion_output = x.to("Pa m^3 kg mol^-1") assert conversion_output.value == pytest.approx(101325.0) def test_pdl(): x = q.Quantity(1, "pdl") - conversion_output = x.convertTo("N") + conversion_output = x.to("N") assert conversion_output.value == pytest.approx(0.13826, 0.0001) def test_ozf(): x = q.Quantity(1, "ozf") # force_ounce - conversion_output = x.convertTo("N") + conversion_output = x.to("N") assert conversion_output.value == pytest.approx(0.27802, 0.0001) def test_lbf(): x = q.Quantity(1, "lbf") # force_pound - conversion_output = x.convertTo("N") + conversion_output = x.to("N") assert conversion_output.value == pytest.approx(4.44820, 0.00002) def test_kgf(): x = q.Quantity(1, "kgf") - conversion_output = x.convertTo("N") + conversion_output = x.to("N") assert conversion_output.value == pytest.approx(9.806805, 0.0002) +def test_tonf(): + x = q.Quantity(1, "tonf") + conversion_output = x.to("N") + assert conversion_output.value == pytest.approx(9964.12914, 0.1) + + def test_cal(): x = q.Quantity(1, "cal") - conversion_output = x.convertTo("J") + conversion_output = x.to("J") assert conversion_output.value == pytest.approx(4.1868, 0.002) def test_kcal(): x = q.Quantity(1, "kcal") - conversion_output = x.convertTo("J") + conversion_output = x.to("J") assert conversion_output.value == pytest.approx(4186.8, 2) def test_hp(): x = q.Quantity(1, "hp") - conversion_output = x.convertTo("W") + conversion_output = x.to("W") assert conversion_output.value == pytest.approx(745.70, 0.01) def test_ohm(): x = q.Quantity(1, "ohm cm") - conversion_output = x.convertTo("ohm m") + conversion_output = x.to("ohm m") assert conversion_output.value == 0.01 def test_hp_h(): x = q.Quantity(1, "hp h") # hp_hour - conversion_output = x.convertTo("J") + conversion_output = x.to("J") assert conversion_output.value == pytest.approx(2.6845 * 10**6, 19) def test_erg(): x = q.Quantity(1, "erg") - conversion_output = x.convertTo("J") + conversion_output = x.to("J") assert conversion_output.value == pytest.approx(1.0 * 10**-7) def test_energy_density(): x = q.Quantity(1, "BTU ft^-2") - conversion_output = x.convertTo("J m^-2") + conversion_output = x.to("J m^-2") assert conversion_output.value == pytest.approx(11356.36, 0.2) def test_degree_rankine(): x = q.Quantity(1, "BTU lb^-1 degR^-1") - conversion_output = x.convertTo("J kg^-1 degK^-1") + conversion_output = x.to("J kg^-1 degK^-1") assert conversion_output.value == pytest.approx(4186.69, 0.2) def test_degree_fahrenheit(): x = q.Quantity(1, "BTU lb^-1 degF^-1") - conversion_output = x.convertTo("J kg^-1 degK^-1") + conversion_output = x.to("J kg^-1 degK^-1") assert conversion_output.value == pytest.approx(4186.69, 0.2) def test_degree_celsius(): x = q.Quantity(1, "cal g^-1 degC^-1") - conversion_output = x.convertTo("J kg^-1 degK^-1") + conversion_output = x.to("J kg^-1 degK^-1") assert conversion_output.value == pytest.approx(4186.69, 2) def test_mol(): x = q.Quantity(1, "lb mol ft^-3 s^-1") - conversion_output = x.convertTo("kg mol m^-3 s^-1") + conversion_output = x.to("kg mol m^-3 s^-1") assert conversion_output.value == pytest.approx(16.01846, 0.000003) def test_rpm(): x = q.Quantity(1, "rpm") - conversion_output = x.convertTo("rad s^-1") + conversion_output = x.to("rad s^-1") assert conversion_output.value == pytest.approx(0.1047198, 0.000001) +def test_particles_per_gm(): + x = q.Quantity(1, "1^15particles g^-1") + conversion_output = x.to("1^15particles kg^-1") + assert conversion_output.value == 1000.0 + + +def test_particles_per_lb(): + x = q.Quantity(1, "1^15particles lb^-1") + conversion_output = x.to("1^15particles kg^-1") + assert conversion_output.value == pytest.approx(2.2046225, 0.000001) + + +def test_particles_rate_per_ft(): + x = q.Quantity(1, "1^15particles ft^-3 s^-1") + conversion_output = x.to("1^15particles m^-3 s^-1") + assert conversion_output.value == pytest.approx(35.314724828, 0.0001) + + +def test_particles_rate_per_cm(): + x = q.Quantity(1, "1^15particles cm^-3 s^-1") + conversion_output = x.to("1^15particles m^-3 s^-1") + assert conversion_output.value == pytest.approx(1000000.0, 0.1) + + if __name__ == "__main__": v1 = q.Quantity(10.2, "m/s") # Instantiation print(v1) - print(v1.convertTo("cm/s")) # conversion m/s to cm/s - print(v1.convertTo("ft/s")) # conversion m/s to ft/s + print(v1.to("cm/s")) # conversion m/s to cm/s + print(v1.to("ft/s")) # conversion m/s to ft/s print(v1 + 15.7) # scalar addition print(v1 - 5.9) # scalar subtraction print(v1 * 2) # scalar multiplication @@ -494,67 +524,67 @@ def test_rpm(): print("\n acceleration \n") a1 = q.Quantity(1, "m/s^2") print(a1) - print(a1.convertTo("cm/s^2")) # conversion m/s^-2 to cm/s^-2 - print(a1.convertTo("ft/s^2")) # conversion m/s^-2 to ft/s^-2 + print(a1.to("cm/s^2")) # conversion m/s^-2 to cm/s^-2 + print(a1.to("ft/s^2")) # conversion m/s^-2 to ft/s^-2 print("\n angle \n") d1 = q.Quantity(1, "deg") r1 = q.Quantity(1, "rad") print(d1) print(r1) - print(d1.convertTo("rad")) # conversion deg to rad - print(r1.convertTo("deg")) # conversion rad to deg + print(d1.to("rad")) # conversion deg to rad + print(r1.to("deg")) # conversion rad to deg print("\n angluar-velocity \n") degps = q.Quantity(1, "deg/s") radps = q.Quantity(1, "rad/s") revpm = q.Quantity(1, "revolution/min") - print(radps.convertTo("deg/s")) - print(degps.convertTo("rad/s")) + print(radps.to("deg/s")) + print(degps.to("rad/s")) print("\n area \n") sq_m = q.Quantity(1, "m^2") print(sq_m) - print(sq_m.convertTo("cm^2")) - print(sq_m.convertTo("mm^2")) - print(sq_m.convertTo("ft^2")) - print(sq_m.convertTo("in^2")) + print(sq_m.to("cm^2")) + print(sq_m.to("mm^2")) + print(sq_m.to("ft^2")) + print(sq_m.to("in^2")) print("\n area-inverse \n") in_sq_m = q.Quantity(1, "m^-2") print(in_sq_m) - print(in_sq_m.convertTo("cm^-2")) - print(in_sq_m.convertTo("mm^-2")) - print(in_sq_m.convertTo("ft^-2")) - print(in_sq_m.convertTo("in^-2")) + print(in_sq_m.to("cm^-2")) + print(in_sq_m.to("mm^-2")) + print(in_sq_m.to("ft^-2")) + print(in_sq_m.to("in^-2")) print("\n collision-rate \n") cr = q.Quantity(1, "ft^-3 s^-1") print(cr) - print(cr.convertTo("m^-3 s^-1")) + print(cr.to("m^-3 s^-1")) print("\n temperature \n") tempC = q.Quantity(1, "degC") # celsius print(tempC) - print(tempC.convertTo("degK")) # kelvin - print(tempC.convertTo("degR")) # rankine - print(tempC.convertTo("degF")) # fahrenheit + print(tempC.to("degK")) # kelvin + print(tempC.to("degR")) # rankine + print(tempC.to("degF")) # fahrenheit print("\n youngs-modulus \n") ym = q.Quantity(1, "lbf ft^-2") print(ym) - print(ym.convertTo("N m^-2")) + print(ym.to("N m^-2")) ym2 = q.Quantity(1, "dyn cm^-2") print(ym2) - print(ym2.convertTo("N m^-2")) + print(ym2.to("N m^-2")) print("\n volume \n") v = q.Quantity(1, "gal") print(v) - print(v.convertTo("m^3")) + print(v.to("m^3")) print("\n viscosity \n") v = q.Quantity(1, "P") # poise print(v) - print(v.convertTo("kg m^-1 s^-1")) + print(v.to("kg m^-1 s^-1")) From 538f36c40ec0cf29be1aadfacca8afc59c89d3a1 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Mon, 22 Aug 2022 19:44:43 +0530 Subject: [PATCH 12/21] Replaced units with base_units --- src/ansys/fluent/core/quantity.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index dee9efbb78db..c2197b7dcbc5 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -3,16 +3,16 @@ import pint from pint import Unit -unit = pint.UnitRegistry(autoconvert_offset_to_baseunit=True) -unit.default_system = "SI" +base_units = pint.UnitRegistry(autoconvert_offset_to_baseunit=True) +base_units.default_system = "SI" # Unit definitions -unit.define("1.e15-particles/kg = 1 kg^-1 = 1^15particles kg^-1") -unit.define("1.e15-particles m^-3 s^-1 = 1 m^-3 s^-1 = 1^15particles m^-3 s^-1") -unit.define("ton_force = 1 UK_force_ton = tonf") +base_units.define("1.e15-particles/kg = 1 kg^-1 = 1^15particles kg^-1") +base_units.define("1.e15-particles m^-3 s^-1 = 1 m^-3 s^-1 = 1^15particles m^-3 s^-1") +base_units.define("ton_force = 1 UK_force_ton = tonf") -quantity = unit.Quantity +quantity = base_units.Quantity restricted_units = ["Hz", "hertz", "rad/s", "radian/s", "rpm", "rps", "cps"] From 99f10c5008e8056dda8bc0f3e56de4ae9cffdbba Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Wed, 24 Aug 2022 13:39:47 +0530 Subject: [PATCH 13/21] Removed non-std units, added check for dimensionality and added description about restricted units --- src/ansys/fluent/core/quantity.py | 39 ++++++++++++++++++++----------- tests/test_quantity.py | 30 ------------------------ 2 files changed, 26 insertions(+), 43 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index c2197b7dcbc5..f6d7c349d907 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -6,11 +6,6 @@ base_units = pint.UnitRegistry(autoconvert_offset_to_baseunit=True) base_units.default_system = "SI" -# Unit definitions -base_units.define("1.e15-particles/kg = 1 kg^-1 = 1^15particles kg^-1") -base_units.define("1.e15-particles m^-3 s^-1 = 1 m^-3 s^-1 = 1^15particles m^-3 s^-1") -base_units.define("ton_force = 1 UK_force_ton = tonf") - quantity = base_units.Quantity @@ -79,11 +74,17 @@ def build_restricted_conversions(conversions, unit_expansion): class Quantity: """This class instantiates physical quantities using their real values and - units. - - All the instances of this class are converted to base SI units - system. Any conversion between "Hz", "hertz", "rad/s", "radian/s", - "rpm", "rps", "cps" is restricted. + units. All the instances of this class are converted to base SI units + system. Any conversion between "Hz", "hertz", "rad/s", "radian/s", "rpm", + "rps", "cps" is disallowed. + + Certain conversions allowed by pint are disallowed here because they + are not dimensionally consistent. For instance conversions between + Hz and rad/s are not allowed because the former represents the + number of times a dimensionless quantity is changing per unit time + and latter represents the amount by which an angular quantity + changes per unit time. A ValueError exception will be thrown in such + cases. """ def __init__(self, real_value, units_string): @@ -150,10 +151,14 @@ def __add__(self, other): if isinstance(other, Quantity): temp = self._base_si_quantity + other._quantity - elif isinstance(other, int) or isinstance(other, float): + elif self._base_si_quantity.dimensionless and ( + isinstance(other, int) or isinstance(other, float) + ): temp = quantity( self._base_si_quantity.magnitude + other, self._base_si_quantity.units ) + else: + raise ValueError(f"Quantity{(self.value, self.unit)} is not dimensionless.") return Quantity(temp.magnitude, temp.units) def __radd__(self, other): @@ -165,10 +170,14 @@ def __sub__(self, other): if isinstance(other, Quantity): temp = self._base_si_quantity - other._quantity - elif isinstance(other, int) or isinstance(other, float): + elif self._base_si_quantity.dimensionless and ( + isinstance(other, int) or isinstance(other, float) + ): temp = quantity( self._base_si_quantity.magnitude - other, self._base_si_quantity.units ) + else: + raise ValueError(f"Quantity{(self.value, self.unit)} is not dimensionless.") return Quantity(temp.magnitude, temp.units) def __rsub__(self, other): @@ -177,8 +186,12 @@ def __rsub__(self, other): if isinstance(other, Quantity): temp = other._quantity - self._base_si_quantity - elif isinstance(other, int) or isinstance(other, float): + elif self._base_si_quantity.dimensionless and ( + isinstance(other, int) or isinstance(other, float) + ): temp = quantity( other - self._base_si_quantity.magnitude, self._base_si_quantity.units ) + else: + raise ValueError(f"Quantity{(self.value, self.unit)} is not dimensionless.") return Quantity(temp.magnitude, temp.units) diff --git a/tests/test_quantity.py b/tests/test_quantity.py index 6ae7401e4a3c..c260e48ca058 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -397,12 +397,6 @@ def test_kgf(): assert conversion_output.value == pytest.approx(9.806805, 0.0002) -def test_tonf(): - x = q.Quantity(1, "tonf") - conversion_output = x.to("N") - assert conversion_output.value == pytest.approx(9964.12914, 0.1) - - def test_cal(): x = q.Quantity(1, "cal") conversion_output = x.to("J") @@ -475,30 +469,6 @@ def test_rpm(): assert conversion_output.value == pytest.approx(0.1047198, 0.000001) -def test_particles_per_gm(): - x = q.Quantity(1, "1^15particles g^-1") - conversion_output = x.to("1^15particles kg^-1") - assert conversion_output.value == 1000.0 - - -def test_particles_per_lb(): - x = q.Quantity(1, "1^15particles lb^-1") - conversion_output = x.to("1^15particles kg^-1") - assert conversion_output.value == pytest.approx(2.2046225, 0.000001) - - -def test_particles_rate_per_ft(): - x = q.Quantity(1, "1^15particles ft^-3 s^-1") - conversion_output = x.to("1^15particles m^-3 s^-1") - assert conversion_output.value == pytest.approx(35.314724828, 0.0001) - - -def test_particles_rate_per_cm(): - x = q.Quantity(1, "1^15particles cm^-3 s^-1") - conversion_output = x.to("1^15particles m^-3 s^-1") - assert conversion_output.value == pytest.approx(1000000.0, 0.1) - - if __name__ == "__main__": v1 = q.Quantity(10.2, "m/s") # Instantiation print(v1) From 2c79c7e6bf8239cd4ace0d115d9a76e4f3134baf Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Wed, 24 Aug 2022 13:48:51 +0530 Subject: [PATCH 14/21] Renamed base_units to ureg --- src/ansys/fluent/core/quantity.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index f6d7c349d907..aac66bc43e85 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -3,11 +3,11 @@ import pint from pint import Unit -base_units = pint.UnitRegistry(autoconvert_offset_to_baseunit=True) -base_units.default_system = "SI" +ureg = pint.UnitRegistry(autoconvert_offset_to_baseunit=True) +ureg.default_system = "SI" -quantity = base_units.Quantity +quantity = ureg.Quantity restricted_units = ["Hz", "hertz", "rad/s", "radian/s", "rpm", "rps", "cps"] From 47208e9679d9f8c0ee63bf2b42236475ffd5b239 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Wed, 24 Aug 2022 16:10:58 +0530 Subject: [PATCH 15/21] Quantity is derived from float --- src/ansys/fluent/core/quantity.py | 46 ++++++++++--------- tests/test_quantity.py | 74 +++++++++++++++---------------- 2 files changed, 61 insertions(+), 59 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index aac66bc43e85..703b714e78fd 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -6,14 +6,11 @@ ureg = pint.UnitRegistry(autoconvert_offset_to_baseunit=True) ureg.default_system = "SI" - quantity = ureg.Quantity - restricted_units = ["Hz", "hertz", "rad/s", "radian/s", "rpm", "rps", "cps"] restricted_conversions = list((permutations(restricted_units, 2))) - std_prefixes = [ "y", "z", @@ -38,7 +35,6 @@ "", ] - restricted_unit_expansion = { "Hz": [prefix + "Hz" for prefix in std_prefixes], "hertz": [prefix + "hertz" for prefix in std_prefixes], @@ -51,7 +47,6 @@ def build_restricted_conversions(conversions, unit_expansion): - """This function generates required final restricted mappings.""" keys = set([i[0] for i in conversions]) @@ -71,10 +66,11 @@ def build_restricted_conversions(conversions, unit_expansion): ) -class Quantity: - +class Quantity(float): """This class instantiates physical quantities using their real values and - units. All the instances of this class are converted to base SI units + units. + + All the instances of this class are converted to base SI units system. Any conversion between "Hz", "hertz", "rad/s", "radian/s", "rpm", "rps", "cps" is disallowed. @@ -87,21 +83,21 @@ class Quantity: cases. """ + def __new__(self, real_value, units_string): + return float.__new__(self, real_value) + def __init__(self, real_value, units_string): - self.value = real_value + float.__init__(real_value) self.unit = units_string - self._quantity = quantity(self.value, self.unit) + self._quantity = quantity(self.__float__(), self.unit) self._base_si_quantity = self._quantity.to_base_units() self._restricted_conversions = restricted_units - def __float__(self): - return Quantity(self.value, self.unit) - def __str__(self): - return f'({self.value}, "{self.unit}")' + return f'({self.__float__()}, "{self.unit}")' def __repr__(self): - return f'(Quantity ({self.value}, "{self.unit}"))' + return f'(Quantity ({self.__float__()}, "{self.unit}"))' def to(self, to_unit): @@ -120,7 +116,7 @@ def to(self, to_unit): user_unit = Unit(to_unit) if not self._quantity.is_compatible_with(user_unit): - raise ValueError("Units are not compatible") + raise ValueError("Units are not compatible.") converted = self._quantity.to(to_unit) @@ -147,7 +143,7 @@ def __truediv__(self, other): def __add__(self, other): if self.unit in self._restricted_conversions.keys(): - raise ValueError("This arithmetic operation is restricted") + raise ValueError("This arithmetic operation is restricted.") if isinstance(other, Quantity): temp = self._base_si_quantity + other._quantity @@ -158,7 +154,9 @@ def __add__(self, other): self._base_si_quantity.magnitude + other, self._base_si_quantity.units ) else: - raise ValueError(f"Quantity{(self.value, self.unit)} is not dimensionless.") + raise ValueError( + f"Quantity{(self.__float__(), self.unit)} is not dimensionless." + ) return Quantity(temp.magnitude, temp.units) def __radd__(self, other): @@ -166,7 +164,7 @@ def __radd__(self, other): def __sub__(self, other): if self.unit in self._restricted_conversions.keys(): - raise ValueError("This arithmetic operation is restricted") + raise ValueError("This arithmetic operation is restricted.") if isinstance(other, Quantity): temp = self._base_si_quantity - other._quantity @@ -177,12 +175,14 @@ def __sub__(self, other): self._base_si_quantity.magnitude - other, self._base_si_quantity.units ) else: - raise ValueError(f"Quantity{(self.value, self.unit)} is not dimensionless.") + raise ValueError( + f"Quantity{(self.__float__(), self.unit)} is not dimensionless." + ) return Quantity(temp.magnitude, temp.units) def __rsub__(self, other): if self.unit in self._restricted_conversions.keys(): - raise ValueError("This arithmetic operation is restricted") + raise ValueError("This arithmetic operation is restricted.") if isinstance(other, Quantity): temp = other._quantity - self._base_si_quantity @@ -193,5 +193,7 @@ def __rsub__(self, other): other - self._base_si_quantity.magnitude, self._base_si_quantity.units ) else: - raise ValueError(f"Quantity{(self.value, self.unit)} is not dimensionless.") + raise ValueError( + f"Quantity{(self.__float__(), self.unit)} is not dimensionless." + ) return Quantity(temp.magnitude, temp.units) diff --git a/tests/test_quantity.py b/tests/test_quantity.py index c260e48ca058..e610b15debb6 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -6,71 +6,71 @@ def test_viscosity(): v = q.Quantity(1, "P") # poise conversion_output = v.to("kg m^-1 s^-1") - assert conversion_output.value == 0.1 + assert conversion_output.__float__() == 0.1 def test_dynamic_viscosity(): vd = q.Quantity(1, "Pa s") conversion_output = vd.to("P") - assert conversion_output.value == 10.0 + assert conversion_output.__float__() == 10.0 def test_viscosity_slugs(): vd = q.Quantity(1, "slugs ft^-1 s^-1") conversion_output = vd.to("kg m^-1 s^-1") - assert conversion_output.value == pytest.approx(47.880, 0.0002) + assert conversion_output.__float__() == pytest.approx(47.880, 0.0002) def test_viscosity_lb(): vd = q.Quantity(1, "lb ft^-1 s^-1") conversion_output = vd.to("kg m^-1 s^-1") - assert conversion_output.value == pytest.approx(1.488164, 0.000001) + assert conversion_output.__float__() == pytest.approx(1.488164, 0.000001) def test_volume(): v = q.Quantity(1, "gal") conversion_output = v.to("m^3") - assert conversion_output.value == pytest.approx(0.00378541) + assert conversion_output.__float__() == pytest.approx(0.00378541) def test_youngs_modulus(): ym = q.Quantity(1, "lbf ft^-2") conversion_output = ym.to("N m^-2") - assert conversion_output.value == pytest.approx(47.89, 0.1) + assert conversion_output.__float__() == pytest.approx(47.89, 0.1) def test_temperature(): tempC = q.Quantity(1, "degC") # celsius conversion_output_1 = tempC.to("degK") - assert conversion_output_1.value == 274.15 + assert conversion_output_1.__float__() == 274.15 conversion_output_2 = tempC.to("degR") - assert conversion_output_2.value == pytest.approx(493.46, 0.1) + assert conversion_output_2.__float__() == pytest.approx(493.46, 0.1) conversion_output_3 = tempC.to("degF") - assert conversion_output_3.value == pytest.approx(33.79, 0.1) + assert conversion_output_3.__float__() == pytest.approx(33.79, 0.1) def test_collision_rate(): cr = q.Quantity(1, "ft^-3 s^-1") conversion_output = cr.to("m^-3 s^-1") - assert conversion_output.value == pytest.approx(35.3147, 0.001) + assert conversion_output.__float__() == pytest.approx(35.3147, 0.001) def test_area_inverse(): in_sq_m = q.Quantity(1, "m^-2") conversion_output = in_sq_m.to("cm^-2") - assert conversion_output.value == pytest.approx(0.0001) + assert conversion_output.__float__() == pytest.approx(0.0001) def test_area(): in_sq_m = q.Quantity(1, "m^2") conversion_output = in_sq_m.to("in^2") - assert conversion_output.value == pytest.approx(1550, 0.1) + assert conversion_output.__float__() == pytest.approx(1550, 0.1) def test_angular_velocity(): degps = q.Quantity(1, "deg/s") conversion_output = degps.to("rad/s") - assert conversion_output.value == pytest.approx(0.01745, 0.001) + assert conversion_output.__float__() == pytest.approx(0.01745, 0.001) def test_hz_hertz(): @@ -328,145 +328,145 @@ def test_cps_rps(): def test_dyne(): x = q.Quantity(1, "dyn cm^-2") conversion_output = x.to("N m^-2") - assert conversion_output.value == pytest.approx(0.1) + assert conversion_output.__float__() == pytest.approx(0.1) def test_gal(): x = q.Quantity(1, "gal^-1") conversion_output = x.to("m^-3") - assert conversion_output.value == pytest.approx(264.17, 0.002) + assert conversion_output.__float__() == pytest.approx(264.17, 0.002) def test_mph(): x = q.Quantity(1, "m s^-1") conversion_output = x.to("mph") - assert conversion_output.value == pytest.approx(2.23694, 0.00002) + assert conversion_output.__float__() == pytest.approx(2.23694, 0.00002) def test_inches_water(): x = q.Quantity(1, "inch_H2O_39F") conversion_output = x.to("Pa") - assert conversion_output.value == pytest.approx(249, 0.1) + assert conversion_output.__float__() == pytest.approx(249, 0.1) def test_torr(): x = q.Quantity(1, "torr") conversion_output = x.to("Pa") - assert conversion_output.value == pytest.approx(133.3220, 0.0003) + assert conversion_output.__float__() == pytest.approx(133.3220, 0.0003) def test_psi(): x = q.Quantity(1, "psi") conversion_output = x.to("Pa") - assert conversion_output.value == pytest.approx(6894.757, 0.0002) + assert conversion_output.__float__() == pytest.approx(6894.757, 0.0002) def test_atm(): x = q.Quantity(1, "atm") conversion_output = x.to("Pa") - assert conversion_output.value == pytest.approx(101325.0) + assert conversion_output.__float__() == pytest.approx(101325.0) def test_mole_con_henry_const(): x = q.Quantity(1, "atm m^3 kg mol^-1") conversion_output = x.to("Pa m^3 kg mol^-1") - assert conversion_output.value == pytest.approx(101325.0) + assert conversion_output.__float__() == pytest.approx(101325.0) def test_pdl(): x = q.Quantity(1, "pdl") conversion_output = x.to("N") - assert conversion_output.value == pytest.approx(0.13826, 0.0001) + assert conversion_output.__float__() == pytest.approx(0.13826, 0.0001) def test_ozf(): x = q.Quantity(1, "ozf") # force_ounce conversion_output = x.to("N") - assert conversion_output.value == pytest.approx(0.27802, 0.0001) + assert conversion_output.__float__() == pytest.approx(0.27802, 0.0001) def test_lbf(): x = q.Quantity(1, "lbf") # force_pound conversion_output = x.to("N") - assert conversion_output.value == pytest.approx(4.44820, 0.00002) + assert conversion_output.__float__() == pytest.approx(4.44820, 0.00002) def test_kgf(): x = q.Quantity(1, "kgf") conversion_output = x.to("N") - assert conversion_output.value == pytest.approx(9.806805, 0.0002) + assert conversion_output.__float__() == pytest.approx(9.806805, 0.0002) def test_cal(): x = q.Quantity(1, "cal") conversion_output = x.to("J") - assert conversion_output.value == pytest.approx(4.1868, 0.002) + assert conversion_output.__float__() == pytest.approx(4.1868, 0.002) def test_kcal(): x = q.Quantity(1, "kcal") conversion_output = x.to("J") - assert conversion_output.value == pytest.approx(4186.8, 2) + assert conversion_output.__float__() == pytest.approx(4186.8, 2) def test_hp(): x = q.Quantity(1, "hp") conversion_output = x.to("W") - assert conversion_output.value == pytest.approx(745.70, 0.01) + assert conversion_output.__float__() == pytest.approx(745.70, 0.01) def test_ohm(): x = q.Quantity(1, "ohm cm") conversion_output = x.to("ohm m") - assert conversion_output.value == 0.01 + assert conversion_output.__float__() == 0.01 def test_hp_h(): x = q.Quantity(1, "hp h") # hp_hour conversion_output = x.to("J") - assert conversion_output.value == pytest.approx(2.6845 * 10**6, 19) + assert conversion_output.__float__() == pytest.approx(2.6845 * 10**6, 19) def test_erg(): x = q.Quantity(1, "erg") conversion_output = x.to("J") - assert conversion_output.value == pytest.approx(1.0 * 10**-7) + assert conversion_output.__float__() == pytest.approx(1.0 * 10**-7) def test_energy_density(): x = q.Quantity(1, "BTU ft^-2") conversion_output = x.to("J m^-2") - assert conversion_output.value == pytest.approx(11356.36, 0.2) + assert conversion_output.__float__() == pytest.approx(11356.36, 0.2) def test_degree_rankine(): x = q.Quantity(1, "BTU lb^-1 degR^-1") conversion_output = x.to("J kg^-1 degK^-1") - assert conversion_output.value == pytest.approx(4186.69, 0.2) + assert conversion_output.__float__() == pytest.approx(4186.69, 0.2) def test_degree_fahrenheit(): x = q.Quantity(1, "BTU lb^-1 degF^-1") conversion_output = x.to("J kg^-1 degK^-1") - assert conversion_output.value == pytest.approx(4186.69, 0.2) + assert conversion_output.__float__() == pytest.approx(4186.69, 0.2) def test_degree_celsius(): x = q.Quantity(1, "cal g^-1 degC^-1") conversion_output = x.to("J kg^-1 degK^-1") - assert conversion_output.value == pytest.approx(4186.69, 2) + assert conversion_output.__float__() == pytest.approx(4186.69, 2) def test_mol(): x = q.Quantity(1, "lb mol ft^-3 s^-1") conversion_output = x.to("kg mol m^-3 s^-1") - assert conversion_output.value == pytest.approx(16.01846, 0.000003) + assert conversion_output.__float__() == pytest.approx(16.01846, 0.000003) def test_rpm(): x = q.Quantity(1, "rpm") conversion_output = x.to("rad s^-1") - assert conversion_output.value == pytest.approx(0.1047198, 0.000001) + assert conversion_output.__float__() == pytest.approx(0.1047198, 0.000001) if __name__ == "__main__": From 5465b7427da2ab3c91e5698cf1b29912707dcdd9 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Wed, 24 Aug 2022 16:46:47 +0530 Subject: [PATCH 16/21] Description added about pint functionalities --- src/ansys/fluent/core/quantity.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 703b714e78fd..8a46a3045fa9 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -68,11 +68,17 @@ def build_restricted_conversions(conversions, unit_expansion): class Quantity(float): """This class instantiates physical quantities using their real values and - units. + units. Attributes of every instance of this class are used to construct a + new quantity instance supported by unit registry of pint module. - All the instances of this class are converted to base SI units - system. Any conversion between "Hz", "hertz", "rad/s", "radian/s", "rpm", - "rps", "cps" is disallowed. + The pint module supports methods for unit conversions, unit compatibility and + dimensionality check. + + All the instances of this class are converted to base SI units system to have + consistency in all arithmetic operations. + + Any conversion between "Hz", "hertz", "rad/s", "radian/s", "rpm", + "rps", "cps" is restricted. Certain conversions allowed by pint are disallowed here because they are not dimensionally consistent. For instance conversions between From ce077a08d495ffae981ea3bd8ba590bf6df8eeb2 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Tue, 30 Aug 2022 17:58:43 +0530 Subject: [PATCH 17/21] Removed restricted code, related test cases and updated the documentation --- src/ansys/fluent/core/quantity.py | 102 ++---------- tests/test_quantity.py | 252 ------------------------------ 2 files changed, 17 insertions(+), 337 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 8a46a3045fa9..e9d7280b13e0 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -1,5 +1,3 @@ -from itertools import permutations - import pint from pint import Unit @@ -8,85 +6,34 @@ quantity = ureg.Quantity -restricted_units = ["Hz", "hertz", "rad/s", "radian/s", "rpm", "rps", "cps"] -restricted_conversions = list((permutations(restricted_units, 2))) - -std_prefixes = [ - "y", - "z", - "a", - "f", - "p", - "n", - "u", - "c", - "d", - "da", - "h", - "m", - "k", - "M", - "G", - "T", - "P", - "E", - "Z", - "Y", - "", -] - -restricted_unit_expansion = { - "Hz": [prefix + "Hz" for prefix in std_prefixes], - "hertz": [prefix + "hertz" for prefix in std_prefixes], - "rad/s": ["radian/s", "rad/s"], - "radian/s": ["rad/s", "radian/s"], - "rpm": ["revolutions_per_minute", "rpm"], - "rps": ["revolutions_per_second", "rps"], - "cps": ["counts_per_second", "cps"], -} - - -def build_restricted_conversions(conversions, unit_expansion): - """This function generates required final restricted mappings.""" - - keys = set([i[0] for i in conversions]) - restricted_units_dict = {unit[0]: [] for unit in conversions} - - for key in keys: - temp_list = [unit_expansion[i] for i in keys.difference(set([key]))] - for temp in temp_list: - restricted_units_dict[key] += temp - for key in keys: - restricted_units_dict[key] = list(set(restricted_units_dict[key])) - return restricted_units_dict - - -restricted_units = build_restricted_conversions( - restricted_conversions, restricted_unit_expansion -) - class Quantity(float): """This class instantiates physical quantities using their real values and units. Attributes of every instance of this class are used to construct a new quantity instance supported by unit registry of pint module. + Attributes + ---------- + float: 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. + The pint module supports methods for unit conversions, unit compatibility and dimensionality check. All the instances of this class are converted to base SI units system to have consistency in all arithmetic operations. - - Any conversion between "Hz", "hertz", "rad/s", "radian/s", "rpm", - "rps", "cps" is restricted. - - Certain conversions allowed by pint are disallowed here because they - are not dimensionally consistent. For instance conversions between - Hz and rad/s are not allowed because the former represents the - number of times a dimensionless quantity is changing per unit time - and latter represents the amount by which an angular quantity - changes per unit time. A ValueError exception will be thrown in such - cases. """ def __new__(self, real_value, units_string): @@ -97,7 +44,6 @@ def __init__(self, real_value, units_string): self.unit = units_string self._quantity = quantity(self.__float__(), self.unit) self._base_si_quantity = self._quantity.to_base_units() - self._restricted_conversions = restricted_units def __str__(self): return f'({self.__float__()}, "{self.unit}")' @@ -111,14 +57,6 @@ def to(self, to_unit): and user provided unit, if both of them are compatible, then only it performs required conversion otherwise raises a Value Error.""" - if ( - self.unit in self._restricted_conversions.keys() - and to_unit in self._restricted_conversions[self.unit] - ): - raise ValueError( - f"Conversion between '{self.unit}' and '{to_unit}' is restricted." - ) - user_unit = Unit(to_unit) if not self._quantity.is_compatible_with(user_unit): @@ -148,8 +86,6 @@ def __truediv__(self, other): return Quantity(temp.magnitude, temp.units) def __add__(self, other): - if self.unit in self._restricted_conversions.keys(): - raise ValueError("This arithmetic operation is restricted.") if isinstance(other, Quantity): temp = self._base_si_quantity + other._quantity @@ -169,8 +105,6 @@ def __radd__(self, other): return self.__add__(other) def __sub__(self, other): - if self.unit in self._restricted_conversions.keys(): - raise ValueError("This arithmetic operation is restricted.") if isinstance(other, Quantity): temp = self._base_si_quantity - other._quantity @@ -187,8 +121,6 @@ def __sub__(self, other): return Quantity(temp.magnitude, temp.units) def __rsub__(self, other): - if self.unit in self._restricted_conversions.keys(): - raise ValueError("This arithmetic operation is restricted.") if isinstance(other, Quantity): temp = other._quantity - self._base_si_quantity diff --git a/tests/test_quantity.py b/tests/test_quantity.py index e610b15debb6..681030d0be72 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -73,258 +73,6 @@ def test_angular_velocity(): assert conversion_output.__float__() == pytest.approx(0.01745, 0.001) -def test_hz_hertz(): - f = q.Quantity(1, "Hz") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("hertz") - - -def test_hz_rad(): - f = q.Quantity(1, "Hz") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("rad/s") - - -def test_hz_radian(): - f = q.Quantity(1, "Hz") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("radian/s") - - -def test_hz_rpm(): - f = q.Quantity(1, "Hz") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("rpm") - - -def test_hz_rps(): - f = q.Quantity(1, "Hz") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("rps") - - -def test_hz_cps(): - f = q.Quantity(1, "Hz") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("cps") - - -def test_hertz_hz(): - f = q.Quantity(1, "hertz") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("Hz") - - -def test_hertz_rad(): - f = q.Quantity(1, "hertz") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("rad/s") - - -def test_hertz_radian(): - f = q.Quantity(1, "hertz") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("radian/s") - - -def test_hertz_rpm(): - f = q.Quantity(1, "hertz") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("rpm") - - -def test_hertz_rps(): - f = q.Quantity(1, "hertz") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("rps") - - -def test_hertz_cps(): - f = q.Quantity(1, "hertz") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("cps") - - -def test_rad_hz(): - f = q.Quantity(1, "rad/s") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("Hz") - - -def test_rad_hertz(): - f = q.Quantity(1, "rad/s") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("hertz") - - -def test_rad_radian(): - f = q.Quantity(1, "rad/s") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("radian/s") - - -def test_rad_rpm(): - f = q.Quantity(1, "rad/s") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("rpm") - - -def test_rad_rps(): - f = q.Quantity(1, "rad/s") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("rps") - - -def test_rad_cps(): - f = q.Quantity(1, "rad/s") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("cps") - - -def test_radian_hz(): - f = q.Quantity(1, "radian/s") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("Hz") - - -def test_radian_hertz(): - f = q.Quantity(1, "radian/s") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("hertz") - - -def test_radian_rad(): - f = q.Quantity(1, "radian/s") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("rad/s") - - -def test_radian_rpm(): - f = q.Quantity(1, "radian/s") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("rpm") - - -def test_radian_rps(): - f = q.Quantity(1, "radian/s") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("rps") - - -def test_radian_cps(): - f = q.Quantity(1, "radian/s") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("cps") - - -def test_rpm_hz(): - f = q.Quantity(1, "rpm") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("Hz") - - -def test_rpm_hertz(): - f = q.Quantity(1, "rpm") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("hertz") - - -def test_rpm_radian(): - f = q.Quantity(1, "rpm") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("radian/s") - - -def test_rpm_rad(): - f = q.Quantity(1, "rpm") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("rad/s") - - -def test_rpm_rps(): - f = q.Quantity(1, "rpm") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("rps") - - -def test_rpm_cps(): - f = q.Quantity(1, "rpm") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("cps") - - -def test_rps_hz(): - f = q.Quantity(1, "rps") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("Hz") - - -def test_rps_hertz(): - f = q.Quantity(1, "rps") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("hertz") - - -def test_rps_radian(): - f = q.Quantity(1, "rps") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("radian/s") - - -def test_rps_rad(): - f = q.Quantity(1, "rps") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("rad/s") - - -def test_rps_rpm(): - f = q.Quantity(1, "rps") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("rpm") - - -def test_rps_cps(): - f = q.Quantity(1, "rps") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("cps") - - -def test_cps_hz(): - f = q.Quantity(1, "cps") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("Hz") - - -def test_cps_hertz(): - f = q.Quantity(1, "rps") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("hertz") - - -def test_cps_radian(): - f = q.Quantity(1, "cps") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("radian/s") - - -def test_cps_rad(): - f = q.Quantity(1, "cps") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("rad/s") - - -def test_cps_rpm(): - f = q.Quantity(1, "cps") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("rpm") - - -def test_cps_rps(): - f = q.Quantity(1, "cps") - with pytest.raises(Exception) as e_info: - conversion_output = f.to("rps") - - def test_dyne(): x = q.Quantity(1, "dyn cm^-2") conversion_output = x.to("N m^-2") From 01d8331bad6d9270635600b59063d713dd5d933f Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Tue, 30 Aug 2022 23:08:16 +0530 Subject: [PATCH 18/21] Added equal check method and related test case --- src/ansys/fluent/core/quantity.py | 5 ++ tests/test_quantity.py | 79 ++++++++++++++++--------------- 2 files changed, 47 insertions(+), 37 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index e9d7280b13e0..2bd8578f03be 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -135,3 +135,8 @@ def __rsub__(self, other): f"Quantity{(self.__float__(), self.unit)} is not dimensionless." ) return Quantity(temp.magnitude, temp.units) + + def __eq__(self, other): + if isinstance(other, Quantity): + return other._quantity == self._quantity + return False diff --git a/tests/test_quantity.py b/tests/test_quantity.py index 681030d0be72..886b25d16721 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -6,215 +6,220 @@ def test_viscosity(): v = q.Quantity(1, "P") # poise conversion_output = v.to("kg m^-1 s^-1") - assert conversion_output.__float__() == 0.1 + assert conversion_output.real == 0.1 def test_dynamic_viscosity(): vd = q.Quantity(1, "Pa s") conversion_output = vd.to("P") - assert conversion_output.__float__() == 10.0 + assert conversion_output.real == 10.0 + + +def test_dynamic_viscosity_equality(): + vd = q.Quantity(1, "Pa s") + assert vd == q.Quantity(1, "Pa s") def test_viscosity_slugs(): vd = q.Quantity(1, "slugs ft^-1 s^-1") conversion_output = vd.to("kg m^-1 s^-1") - assert conversion_output.__float__() == pytest.approx(47.880, 0.0002) + assert conversion_output.real == pytest.approx(47.880, 0.0002) def test_viscosity_lb(): vd = q.Quantity(1, "lb ft^-1 s^-1") conversion_output = vd.to("kg m^-1 s^-1") - assert conversion_output.__float__() == pytest.approx(1.488164, 0.000001) + assert conversion_output.real == pytest.approx(1.488164, 0.000001) def test_volume(): v = q.Quantity(1, "gal") conversion_output = v.to("m^3") - assert conversion_output.__float__() == pytest.approx(0.00378541) + assert conversion_output.real == pytest.approx(0.00378541) def test_youngs_modulus(): ym = q.Quantity(1, "lbf ft^-2") conversion_output = ym.to("N m^-2") - assert conversion_output.__float__() == pytest.approx(47.89, 0.1) + assert conversion_output.real == pytest.approx(47.89, 0.1) def test_temperature(): tempC = q.Quantity(1, "degC") # celsius conversion_output_1 = tempC.to("degK") - assert conversion_output_1.__float__() == 274.15 + assert conversion_output_1.real == 274.15 conversion_output_2 = tempC.to("degR") - assert conversion_output_2.__float__() == pytest.approx(493.46, 0.1) + assert conversion_output_2.real == pytest.approx(493.46, 0.1) conversion_output_3 = tempC.to("degF") - assert conversion_output_3.__float__() == pytest.approx(33.79, 0.1) + assert conversion_output_3.real == pytest.approx(33.79, 0.1) def test_collision_rate(): cr = q.Quantity(1, "ft^-3 s^-1") conversion_output = cr.to("m^-3 s^-1") - assert conversion_output.__float__() == pytest.approx(35.3147, 0.001) + assert conversion_output.real == pytest.approx(35.3147, 0.001) def test_area_inverse(): in_sq_m = q.Quantity(1, "m^-2") conversion_output = in_sq_m.to("cm^-2") - assert conversion_output.__float__() == pytest.approx(0.0001) + assert conversion_output.real == pytest.approx(0.0001) def test_area(): in_sq_m = q.Quantity(1, "m^2") conversion_output = in_sq_m.to("in^2") - assert conversion_output.__float__() == pytest.approx(1550, 0.1) + assert conversion_output.real == pytest.approx(1550, 0.1) def test_angular_velocity(): degps = q.Quantity(1, "deg/s") conversion_output = degps.to("rad/s") - assert conversion_output.__float__() == pytest.approx(0.01745, 0.001) + assert conversion_output.real == pytest.approx(0.01745, 0.001) def test_dyne(): x = q.Quantity(1, "dyn cm^-2") conversion_output = x.to("N m^-2") - assert conversion_output.__float__() == pytest.approx(0.1) + assert conversion_output.real == pytest.approx(0.1) def test_gal(): x = q.Quantity(1, "gal^-1") conversion_output = x.to("m^-3") - assert conversion_output.__float__() == pytest.approx(264.17, 0.002) + assert conversion_output.real == pytest.approx(264.17, 0.002) def test_mph(): x = q.Quantity(1, "m s^-1") conversion_output = x.to("mph") - assert conversion_output.__float__() == pytest.approx(2.23694, 0.00002) + assert conversion_output.real == pytest.approx(2.23694, 0.00002) def test_inches_water(): x = q.Quantity(1, "inch_H2O_39F") conversion_output = x.to("Pa") - assert conversion_output.__float__() == pytest.approx(249, 0.1) + assert conversion_output.real == pytest.approx(249, 0.1) def test_torr(): x = q.Quantity(1, "torr") conversion_output = x.to("Pa") - assert conversion_output.__float__() == pytest.approx(133.3220, 0.0003) + assert conversion_output.real == pytest.approx(133.3220, 0.0003) def test_psi(): x = q.Quantity(1, "psi") conversion_output = x.to("Pa") - assert conversion_output.__float__() == pytest.approx(6894.757, 0.0002) + assert conversion_output.real == pytest.approx(6894.757, 0.0002) def test_atm(): x = q.Quantity(1, "atm") conversion_output = x.to("Pa") - assert conversion_output.__float__() == pytest.approx(101325.0) + assert conversion_output.real == pytest.approx(101325.0) def test_mole_con_henry_const(): x = q.Quantity(1, "atm m^3 kg mol^-1") conversion_output = x.to("Pa m^3 kg mol^-1") - assert conversion_output.__float__() == pytest.approx(101325.0) + assert conversion_output.real == pytest.approx(101325.0) def test_pdl(): x = q.Quantity(1, "pdl") conversion_output = x.to("N") - assert conversion_output.__float__() == pytest.approx(0.13826, 0.0001) + assert conversion_output.real == pytest.approx(0.13826, 0.0001) def test_ozf(): x = q.Quantity(1, "ozf") # force_ounce conversion_output = x.to("N") - assert conversion_output.__float__() == pytest.approx(0.27802, 0.0001) + assert conversion_output.real == pytest.approx(0.27802, 0.0001) def test_lbf(): x = q.Quantity(1, "lbf") # force_pound conversion_output = x.to("N") - assert conversion_output.__float__() == pytest.approx(4.44820, 0.00002) + assert conversion_output.real == pytest.approx(4.44820, 0.00002) def test_kgf(): x = q.Quantity(1, "kgf") conversion_output = x.to("N") - assert conversion_output.__float__() == pytest.approx(9.806805, 0.0002) + assert conversion_output.real == pytest.approx(9.806805, 0.0002) def test_cal(): x = q.Quantity(1, "cal") conversion_output = x.to("J") - assert conversion_output.__float__() == pytest.approx(4.1868, 0.002) + assert conversion_output.real == pytest.approx(4.1868, 0.002) def test_kcal(): x = q.Quantity(1, "kcal") conversion_output = x.to("J") - assert conversion_output.__float__() == pytest.approx(4186.8, 2) + assert conversion_output.real == pytest.approx(4186.8, 2) def test_hp(): x = q.Quantity(1, "hp") conversion_output = x.to("W") - assert conversion_output.__float__() == pytest.approx(745.70, 0.01) + assert conversion_output.real == pytest.approx(745.70, 0.01) def test_ohm(): x = q.Quantity(1, "ohm cm") conversion_output = x.to("ohm m") - assert conversion_output.__float__() == 0.01 + assert conversion_output.real == 0.01 def test_hp_h(): x = q.Quantity(1, "hp h") # hp_hour conversion_output = x.to("J") - assert conversion_output.__float__() == pytest.approx(2.6845 * 10**6, 19) + assert conversion_output.real == pytest.approx(2.6845 * 10**6, 19) def test_erg(): x = q.Quantity(1, "erg") conversion_output = x.to("J") - assert conversion_output.__float__() == pytest.approx(1.0 * 10**-7) + assert conversion_output.real == pytest.approx(1.0 * 10**-7) def test_energy_density(): x = q.Quantity(1, "BTU ft^-2") conversion_output = x.to("J m^-2") - assert conversion_output.__float__() == pytest.approx(11356.36, 0.2) + assert conversion_output.real == pytest.approx(11356.36, 0.2) def test_degree_rankine(): x = q.Quantity(1, "BTU lb^-1 degR^-1") conversion_output = x.to("J kg^-1 degK^-1") - assert conversion_output.__float__() == pytest.approx(4186.69, 0.2) + assert conversion_output.real == pytest.approx(4186.69, 0.2) def test_degree_fahrenheit(): x = q.Quantity(1, "BTU lb^-1 degF^-1") conversion_output = x.to("J kg^-1 degK^-1") - assert conversion_output.__float__() == pytest.approx(4186.69, 0.2) + assert conversion_output.real == pytest.approx(4186.69, 0.2) def test_degree_celsius(): x = q.Quantity(1, "cal g^-1 degC^-1") conversion_output = x.to("J kg^-1 degK^-1") - assert conversion_output.__float__() == pytest.approx(4186.69, 2) + assert conversion_output.real == pytest.approx(4186.69, 2) def test_mol(): x = q.Quantity(1, "lb mol ft^-3 s^-1") conversion_output = x.to("kg mol m^-3 s^-1") - assert conversion_output.__float__() == pytest.approx(16.01846, 0.000003) + assert conversion_output.real == pytest.approx(16.01846, 0.000003) def test_rpm(): x = q.Quantity(1, "rpm") conversion_output = x.to("rad s^-1") - assert conversion_output.__float__() == pytest.approx(0.1047198, 0.000001) + assert conversion_output.real == pytest.approx(0.1047198, 0.000001) if __name__ == "__main__": From ee80774a65b31a6f5d5e264b904893257f2e6ba7 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Thu, 1 Sep 2022 10:04:28 +0530 Subject: [PATCH 19/21] Added self.value,updated test cases and updated the __eq__ method --- src/ansys/fluent/core/quantity.py | 7 ++-- tests/test_quantity.py | 68 +++++++++++++++---------------- 2 files changed, 38 insertions(+), 37 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index 2bd8578f03be..f8b61e759a6a 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -41,6 +41,7 @@ def __new__(self, real_value, units_string): def __init__(self, real_value, units_string): float.__init__(real_value) + self.value = self.__float__() self.unit = units_string self._quantity = quantity(self.__float__(), self.unit) self._base_si_quantity = self._quantity.to_base_units() @@ -137,6 +138,6 @@ def __rsub__(self, other): return Quantity(temp.magnitude, temp.units) def __eq__(self, other): - if isinstance(other, Quantity): - return other._quantity == self._quantity - return False + return ( + other._quantity == self._quantity if isinstance(other, Quantity) else False + ) diff --git a/tests/test_quantity.py b/tests/test_quantity.py index 886b25d16721..3d9be819cb75 100644 --- a/tests/test_quantity.py +++ b/tests/test_quantity.py @@ -6,13 +6,13 @@ def test_viscosity(): v = q.Quantity(1, "P") # poise conversion_output = v.to("kg m^-1 s^-1") - assert conversion_output.real == 0.1 + assert conversion_output.value == 0.1 def test_dynamic_viscosity(): vd = q.Quantity(1, "Pa s") conversion_output = vd.to("P") - assert conversion_output.real == 10.0 + assert conversion_output.value == 10.0 def test_dynamic_viscosity_equality(): @@ -23,25 +23,25 @@ def test_dynamic_viscosity_equality(): def test_viscosity_slugs(): vd = q.Quantity(1, "slugs ft^-1 s^-1") conversion_output = vd.to("kg m^-1 s^-1") - assert conversion_output.real == pytest.approx(47.880, 0.0002) + assert conversion_output.value == pytest.approx(47.880, 0.0002) def test_viscosity_lb(): vd = q.Quantity(1, "lb ft^-1 s^-1") conversion_output = vd.to("kg m^-1 s^-1") - assert conversion_output.real == pytest.approx(1.488164, 0.000001) + assert conversion_output.value == pytest.approx(1.488164, 0.000001) def test_volume(): v = q.Quantity(1, "gal") conversion_output = v.to("m^3") - assert conversion_output.real == pytest.approx(0.00378541) + assert conversion_output.value == pytest.approx(0.00378541) def test_youngs_modulus(): ym = q.Quantity(1, "lbf ft^-2") conversion_output = ym.to("N m^-2") - assert conversion_output.real == pytest.approx(47.89, 0.1) + assert conversion_output.value == pytest.approx(47.89, 0.1) def test_temperature(): @@ -57,169 +57,169 @@ def test_temperature(): def test_collision_rate(): cr = q.Quantity(1, "ft^-3 s^-1") conversion_output = cr.to("m^-3 s^-1") - assert conversion_output.real == pytest.approx(35.3147, 0.001) + assert conversion_output.value == pytest.approx(35.3147, 0.001) def test_area_inverse(): in_sq_m = q.Quantity(1, "m^-2") conversion_output = in_sq_m.to("cm^-2") - assert conversion_output.real == pytest.approx(0.0001) + assert conversion_output.value == pytest.approx(0.0001) def test_area(): in_sq_m = q.Quantity(1, "m^2") conversion_output = in_sq_m.to("in^2") - assert conversion_output.real == pytest.approx(1550, 0.1) + assert conversion_output.value == pytest.approx(1550, 0.1) def test_angular_velocity(): degps = q.Quantity(1, "deg/s") conversion_output = degps.to("rad/s") - assert conversion_output.real == pytest.approx(0.01745, 0.001) + assert conversion_output.value == pytest.approx(0.01745, 0.001) def test_dyne(): x = q.Quantity(1, "dyn cm^-2") conversion_output = x.to("N m^-2") - assert conversion_output.real == pytest.approx(0.1) + assert conversion_output.value == pytest.approx(0.1) def test_gal(): x = q.Quantity(1, "gal^-1") conversion_output = x.to("m^-3") - assert conversion_output.real == pytest.approx(264.17, 0.002) + assert conversion_output.value == pytest.approx(264.17, 0.002) def test_mph(): x = q.Quantity(1, "m s^-1") conversion_output = x.to("mph") - assert conversion_output.real == pytest.approx(2.23694, 0.00002) + assert conversion_output.value == pytest.approx(2.23694, 0.00002) def test_inches_water(): x = q.Quantity(1, "inch_H2O_39F") conversion_output = x.to("Pa") - assert conversion_output.real == pytest.approx(249, 0.1) + assert conversion_output.value == pytest.approx(249, 0.1) def test_torr(): x = q.Quantity(1, "torr") conversion_output = x.to("Pa") - assert conversion_output.real == pytest.approx(133.3220, 0.0003) + assert conversion_output.value == pytest.approx(133.3220, 0.0003) def test_psi(): x = q.Quantity(1, "psi") conversion_output = x.to("Pa") - assert conversion_output.real == pytest.approx(6894.757, 0.0002) + assert conversion_output.value == pytest.approx(6894.757, 0.0002) def test_atm(): x = q.Quantity(1, "atm") conversion_output = x.to("Pa") - assert conversion_output.real == pytest.approx(101325.0) + assert conversion_output.value == pytest.approx(101325.0) def test_mole_con_henry_const(): x = q.Quantity(1, "atm m^3 kg mol^-1") conversion_output = x.to("Pa m^3 kg mol^-1") - assert conversion_output.real == pytest.approx(101325.0) + assert conversion_output.value == pytest.approx(101325.0) def test_pdl(): x = q.Quantity(1, "pdl") conversion_output = x.to("N") - assert conversion_output.real == pytest.approx(0.13826, 0.0001) + assert conversion_output.value == pytest.approx(0.13826, 0.0001) def test_ozf(): x = q.Quantity(1, "ozf") # force_ounce conversion_output = x.to("N") - assert conversion_output.real == pytest.approx(0.27802, 0.0001) + assert conversion_output.value == pytest.approx(0.27802, 0.0001) def test_lbf(): x = q.Quantity(1, "lbf") # force_pound conversion_output = x.to("N") - assert conversion_output.real == pytest.approx(4.44820, 0.00002) + assert conversion_output.value == pytest.approx(4.44820, 0.00002) def test_kgf(): x = q.Quantity(1, "kgf") conversion_output = x.to("N") - assert conversion_output.real == pytest.approx(9.806805, 0.0002) + assert conversion_output.value == pytest.approx(9.806805, 0.0002) def test_cal(): x = q.Quantity(1, "cal") conversion_output = x.to("J") - assert conversion_output.real == pytest.approx(4.1868, 0.002) + assert conversion_output.value == pytest.approx(4.1868, 0.002) def test_kcal(): x = q.Quantity(1, "kcal") conversion_output = x.to("J") - assert conversion_output.real == pytest.approx(4186.8, 2) + assert conversion_output.value == pytest.approx(4186.8, 2) def test_hp(): x = q.Quantity(1, "hp") conversion_output = x.to("W") - assert conversion_output.real == pytest.approx(745.70, 0.01) + assert conversion_output.value == pytest.approx(745.70, 0.01) def test_ohm(): x = q.Quantity(1, "ohm cm") conversion_output = x.to("ohm m") - assert conversion_output.real == 0.01 + assert conversion_output.value == 0.01 def test_hp_h(): x = q.Quantity(1, "hp h") # hp_hour conversion_output = x.to("J") - assert conversion_output.real == pytest.approx(2.6845 * 10**6, 19) + assert conversion_output.value == pytest.approx(2.6845 * 10**6, 19) def test_erg(): x = q.Quantity(1, "erg") conversion_output = x.to("J") - assert conversion_output.real == pytest.approx(1.0 * 10**-7) + assert conversion_output.value == pytest.approx(1.0 * 10**-7) def test_energy_density(): x = q.Quantity(1, "BTU ft^-2") conversion_output = x.to("J m^-2") - assert conversion_output.real == pytest.approx(11356.36, 0.2) + assert conversion_output.value == pytest.approx(11356.36, 0.2) def test_degree_rankine(): x = q.Quantity(1, "BTU lb^-1 degR^-1") conversion_output = x.to("J kg^-1 degK^-1") - assert conversion_output.real == pytest.approx(4186.69, 0.2) + assert conversion_output.value == pytest.approx(4186.69, 0.2) def test_degree_fahrenheit(): x = q.Quantity(1, "BTU lb^-1 degF^-1") conversion_output = x.to("J kg^-1 degK^-1") - assert conversion_output.real == pytest.approx(4186.69, 0.2) + assert conversion_output.value == pytest.approx(4186.69, 0.2) def test_degree_celsius(): x = q.Quantity(1, "cal g^-1 degC^-1") conversion_output = x.to("J kg^-1 degK^-1") - assert conversion_output.real == pytest.approx(4186.69, 2) + assert conversion_output.value == pytest.approx(4186.69, 2) def test_mol(): x = q.Quantity(1, "lb mol ft^-3 s^-1") conversion_output = x.to("kg mol m^-3 s^-1") - assert conversion_output.real == pytest.approx(16.01846, 0.000003) + assert conversion_output.value == pytest.approx(16.01846, 0.000003) def test_rpm(): x = q.Quantity(1, "rpm") conversion_output = x.to("rad s^-1") - assert conversion_output.real == pytest.approx(0.1047198, 0.000001) + assert conversion_output.value == pytest.approx(0.1047198, 0.000001) if __name__ == "__main__": From c93cd70bccafda7b96d5456870857cef200e0eef Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Thu, 1 Sep 2022 10:14:31 +0530 Subject: [PATCH 20/21] Replaced self.__float__() with self.value in all methods --- src/ansys/fluent/core/quantity.py | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/ansys/fluent/core/quantity.py b/src/ansys/fluent/core/quantity.py index f8b61e759a6a..3b8b93b51f77 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -43,14 +43,14 @@ def __init__(self, real_value, units_string): float.__init__(real_value) self.value = self.__float__() self.unit = units_string - self._quantity = quantity(self.__float__(), self.unit) + self._quantity = quantity(self.value, self.unit) self._base_si_quantity = self._quantity.to_base_units() def __str__(self): - return f'({self.__float__()}, "{self.unit}")' + return f'({self.value}, "{self.unit}")' def __repr__(self): - return f'(Quantity ({self.__float__()}, "{self.unit}"))' + return f'(Quantity ({self.value}, "{self.unit}"))' def to(self, to_unit): @@ -97,9 +97,7 @@ def __add__(self, other): self._base_si_quantity.magnitude + other, self._base_si_quantity.units ) else: - raise ValueError( - f"Quantity{(self.__float__(), self.unit)} is not dimensionless." - ) + raise ValueError(f"Quantity{(self.value, self.unit)} is not dimensionless.") return Quantity(temp.magnitude, temp.units) def __radd__(self, other): @@ -116,9 +114,7 @@ def __sub__(self, other): self._base_si_quantity.magnitude - other, self._base_si_quantity.units ) else: - raise ValueError( - f"Quantity{(self.__float__(), self.unit)} is not dimensionless." - ) + raise ValueError(f"Quantity{(self.value, self.unit)} is not dimensionless.") return Quantity(temp.magnitude, temp.units) def __rsub__(self, other): @@ -132,9 +128,7 @@ def __rsub__(self, other): other - self._base_si_quantity.magnitude, self._base_si_quantity.units ) else: - raise ValueError( - f"Quantity{(self.__float__(), self.unit)} is not dimensionless." - ) + raise ValueError(f"Quantity{(self.value, self.unit)} is not dimensionless.") return Quantity(temp.magnitude, temp.units) def __eq__(self, other): From 10affdc52b859860bc3a6acdf7221cdc45d7fada Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Thu, 1 Sep 2022 17:08:51 +0530 Subject: [PATCH 21/21] Updated the documentation --- 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 3b8b93b51f77..cf0f3ab89f5b 100644 --- a/src/ansys/fluent/core/quantity.py +++ b/src/ansys/fluent/core/quantity.py @@ -14,7 +14,7 @@ class Quantity(float): Attributes ---------- - float: Real value + value: Real value Value of quantity is stored as float. unit: Unit string