Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
83b167a
Quantity (RealwithUnit) unit converter using pint module
hpohekar Aug 18, 2022
efa94cf
Fixed a typo
hpohekar Aug 18, 2022
2105242
Changed pint version
hpohekar Aug 18, 2022
63c0ba9
Restricted conversions is generated as a list using standard list of …
hpohekar Aug 19, 2022
2ee93b9
Checked test cases for restricted unit conversions
hpohekar Aug 19, 2022
e5e6d87
Added standard prefix list and generated combination of Hz and hertz …
hpohekar Aug 19, 2022
432f5d2
Index operator replaced with convert method and added more test cases…
hpohekar Aug 19, 2022
6c277cd
Removed a comment
hpohekar Aug 19, 2022
e097763
Added test cases for remaining units
hpohekar Aug 22, 2022
6847d6a
Fixed a typo
hpohekar Aug 22, 2022
8e09456
Defined new units and convertTo replaced with to
hpohekar Aug 22, 2022
538f36c
Replaced units with base_units
hpohekar Aug 22, 2022
99f10c5
Removed non-std units, added check for dimensionality and added descr…
hpohekar Aug 24, 2022
2c79c7e
Renamed base_units to ureg
hpohekar Aug 24, 2022
47208e9
Quantity is derived from float
hpohekar Aug 24, 2022
5465b74
Description added about pint functionalities
hpohekar Aug 24, 2022
ce077a0
Removed restricted code, related test cases and updated the documenta…
hpohekar Aug 30, 2022
01d8331
Added equal check method and related test case
hpohekar Aug 30, 2022
ee80774
Added self.value,updated test cases and updated the __eq__ method
hpohekar Sep 1, 2022
c93cd70
Replaced self.__float__() with self.value in all methods
hpohekar Sep 1, 2022
10affdc
Updated the documentation
hpohekar Sep 1, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"appdirs>=1.4.0",
"pandas>=1.1.5",
"h5py>=3.7.0",
"pint>=0.18",
]


Expand Down
137 changes: 137 additions & 0 deletions src/ansys/fluent/core/quantity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import pint
from pint import Unit

ureg = pint.UnitRegistry(autoconvert_offset_to_baseunit=True)
ureg.default_system = "SI"

quantity = ureg.Quantity


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
----------
value: Real value
Value of quantity is stored as float.

unit: Unit string
Unit of quantity is stored as string.

Methods
-------
to(to_unit)
Converts to given unit string.

Returns
-------
Quantity instance.

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.
"""

def __new__(self, real_value, units_string):
return float.__new__(self, real_value)

def __init__(self, real_value, units_string):
float.__init__(real_value)
self.value = self.__float__()
self.unit = units_string
self._quantity = quantity(self.value, self.unit)
self._base_si_quantity = self._quantity.to_base_units()

def __str__(self):
return f'({self.value}, "{self.unit}")'

def __repr__(self):
return f'(Quantity ({self.value}, "{self.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
performs required conversion otherwise raises a Value Error."""

user_unit = Unit(to_unit)

if not self._quantity.is_compatible_with(user_unit):
raise ValueError("Units are not compatible.")

converted = self._quantity.to(to_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)
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)
return Quantity(temp.magnitude, temp.units)

def __add__(self, other):

if isinstance(other, Quantity):
temp = self._base_si_quantity + other._quantity
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):
return self.__add__(other)

def __sub__(self, other):

if isinstance(other, Quantity):
temp = self._base_si_quantity - other._quantity
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):

if isinstance(other, Quantity):
temp = other._quantity - self._base_si_quantity
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)

def __eq__(self, other):
return (
other._quantity == self._quantity if isinstance(other, Quantity) else False
)
Loading