Fundamental Unitsο
units.py module
The units module provides a robust framework for handling physical quantities with automatic SI prefix resolution and CODATA-precise conversions.
The units converters are defined as classes: PhysicalQuantity, Energy, Pressure, Length, Area, Volume, Mass, MolarMass, Density.
Quick Startο
Initializationο
Users must first import the appropriate class from tools4pyPhysChem, e.g.:
from pyphyschemtools import Pressure
There are two ways to initialize quantities: you can create objects using standard initialization or the human-readable `parse` method.
Manual Initialization: Pass a value and a unit string.
String Parsing: Use
.parse()for scientific strings like"13.6 eV"or"1.672e2 nm".
# Standard way: (value, unit)
p1 = Pressure(1, 'atm')
# Scientific string parsing:
p2 = Pressure.parse("760 torr")
# They are equivalent:
print(p1.to('Pa') == p2.to('Pa')) # True
Smart Arithmeticο
Basic operationsο
Addition and subtraction operations are performed in absolute SI space, but the result is automatically returned in the unit of the first operand to maintain consistency in your calculations
e1 = Energy(3, 'eV')
e2 = Energy(1.602177e-19, 'J')
# Result will be in 'eV' because e1 is the first variable
ep = e1 + e2
print(ep) # 4 eV
em = e1 + e2
print(em) # 2 eV
Arithmetic Rulesο
The units module enforces strict physical laws to ensure your calculations remain dimensionally sound:
Additive Operations (+, -): These are allowed only between compatible quantities (e.g., Mass + Mass). The result is always expressed in the unit of the first operand.
Scalar Scaling (*, /): You can multiply or divide any physical quantity by a scalar (integer, float, or NumPy array). This is useful for scaling measurements or applying coefficients.
Dimensional Protection: Multiplying or dividing two quantities (e.g., Pressure * Pressure) is forbidden. This prevents the accidental creation of complex derived units (like Pa2) that the specific classes are not designed to handle
In summary, arithmetic is allowed between compatible quantities or with scalars, while operations that would result in complex derived units are restricted.
Operation |
Syntax |
Allowed? |
Resulting Type |
Note |
|---|---|---|---|---|
Addition |
|
β |
|
Performed in SI, returned in the unit of the first operand. |
Scalar Addition |
|
β |
|
The scalar is assumed to share the objectβs unit. |
Scaling |
|
β |
|
Doubling or halving the magnitude. |
Division |
|
β |
|
Reducing the magnitude. |
Dimensional Error |
|
β |
|
Forbidden to prevent uncontrolled unit exponentiation (\(g^2\)). |
Inversion Error |
|
β |
|
Forbidden to prevent unit inversion (\(g^{-1}\)). |
Incompatibility |
|
β |
|
Strictly forbidden (cannot add apples to oranges). |
Discovering Supported Units of each classο
Each class knows its own supported base units. You can inspect them alongside global SI prefixes, with the command:
Volume.show_available_units()
CODATA Traceability & Metadataο
Transparency is key in Physical Chemistry and in numerical calculations. You can inspect the exact physical constants (Planck, Boltzmann, etc.) used for internal calculations, sourced from the latest CODATA values via scipy.constants. It can be done either from a class associated to a physical quantity (Energy, etcβ¦) of from the base PhysicalQuantity class.
from pyphyschemtools import Energy
Energy.show_constants_metadata()
or
from pyphyschemtools import PhysicalQuantity
PhysicalQuantity.show_constants_metadata()
Universal Prefix Supportο
The recursive prefix manager allows for extreme physical scales. It treats prefixes like da (deca) or y (yocto) as dynamic multipliers applicable to (almost) any base unit. Supported SI prefixes range from yocto, \(10^{-30}\), to quetta, \(10^{30}\).
print(Energy.parse("4000 ym-1").to('GeV'))
print(Energy.parse("4000 ym-1").to('ZJ/mol'))
The list of prefixes can also be obtained either from a class associated to a physical quantity (Energy, etcβ¦) of from the base PhysicalQuantity class:
from pyphyschemtools import Energy
Energy.show_available_prefixes()
or
from pyphyschemtools import PhysicalQuantity
PhysicalQuantity.show_available_prefixes()
Accessing Numerical Dataο
The primary way to extract numerical values from a unit object, for example when you need to perform external calculations or pass the data to a plotting function (like Matplotlib) while keeping the original unitβs scale is to address it with :
from pyphyschemtools import Pressure
p = Pressure.parse("10.45 MPa").to('bar')
print(p) # 104.5000 bar
print(type(p)) # <class 'pyphyschemtools.units.Pressure'>
print(p.value) # 104.5
print(type(p.value)) # <class 'float'>
print(2*p) #
print(2*p.value) #
Batch Processing (Vectorization)ο
All classes are fully compatible with NumPy. You can pass a list or array of values to perform batch conversions. Letβs consider the energy levels of the Hydrogen atom:
K = 13.59844
Etab_eV = [-K, -K/4, -K/9, -K/16, -K/25]
# Convert the entire list from eV to hartree
Etab_hartree = Energy(Etab_eV, 'eV').to('hartree')
print(Etab_hartree) # Formatted output: Energy Array (hartree, shape=(5,))
print(Etab_hartree.value) # Formatted output: [-0.49973345 -0.12493336 -0.05552594 -0.03123334 -0.01998934]
Energyο
Mathematical relationshipsο
The Energy class is a high-level physical chemistry utility designed to handle the complex conversions between energy, wavelength, temperature, and molar quantities. It features a recursive prefix manager and native support for different energy contexts:
Photon Energy: Relates energy to wavelength (\(\lambda\)) or frequency (\(\nu\)):
Wavenumbers: Handles reciprocal length units (\(\text{cm}^{-1}\)):
Thermal Energy: Relates temperature (\(T\)) to energy via the Boltzmann constant (\(k_B\)):
Molar Quantities: Bridges single-particle energy (\(E_{particle}\)) and macroscopic thermodynamic properties using the Avogadro constant (\(N_A\)):
To use the energy unit conversion engine, import the Energy class as follows:
from pyphyschemtools import Energy
Spectroscopic & Molar Conversionsο
The class automatically detects whether a unit is an energy, a length (wavelength), or a reciprocal length (wavenumber). It also handles the conversion from single-particle energy to molar energy.
print(Energy.parse("4000 cm-1").to('eV')) # Wavenumber to Energy
print(Energy.parse("1 MeV").to('kJ/mol')) # Nuclear Energy to Molar Energy
print(Energy.parse("656 nm").to('eV')) # Wavelength to Energy
print(Energy.parse("1 kcal/mol").to('kJ/mol')) # Thermochemical conversion
Supported unitsο
Symbol |
Name of the Unit |
|---|---|
|
Joule (SI Base Unit) |
|
Electronvolt |
|
Hartree |
|
Calorie (thermochemical) |
|
Erg |
|
British Thermal Unit |
|
Kelvin |
|
Meter |
|
Γ ngstrΓΆm |
|
Reciprocal Centimeter |
|
Any reciprocal |
|
Molar units |
Pressureο
Standard & Industrial Unitsο
The Pressure class manages conversions between the SI unit (Pascal), common laboratory units (atm, torr), and industrial standards (bar, psi). It uses exact CODATA values from scipy.constants to ensure that even small pressure differences are captured accurately.
Supported Unitsο
The following units are handled natively:
Symbol |
Name of the Unit |
|---|---|
|
Pascal (SI Base Unit) |
|
Bar |
|
Standard Atmosphere |
|
Torr (mmHg) |
|
Pound per Square Inch |
Common Conversionsο
Whether you are working with vacuum systems (mTorr) or high-pressure reactors (MPa), the class resolves prefixes automatically.
from pyphyschemtools import Pressure
# Vacuum science: Convert mTorr to Pascal
p_vac = Pressure.parse("10 mtorr").to('Pa')
print(p_vac)
# Industrial monitoring: Convert MPa to bar
p_gauge = Pressure.parse("2.5 MPa").to('bar')
print(p_gauge)
# Diving/Geology: Convert psi to atm
p_deep = Pressure(3000, 'psi').to('atm')
print(p_deep)
Lengthο
From Atomic to Macroscopic Scalesο
The Length class handles spatial dimensions across a vast range of magnitudes. It is specifically optimized for physical chemists who frequently switch between spectroscopic units (Γ
ngstrΓΆms) and standard SI or Imperial units.
Supported Unitsο
The following units are handled natively:
Symbol |
Name of the Unit |
|---|---|
|
Meter (SI Base Unit) |
|
Γ ngstrΓΆm |
|
Inch |
|
Foot |
|
Yard |
|
Mile |
|
Nautical Mile |
Common Conversionsο
Whether you are defining a bond length or the dimensions of a chromatography column, the Length class ensures dimensional accuracy.
from pyphyschemtools import Length
# Structural Chemistry: Convert Γ
ngstrΓΆms to nm
bond_len = Length(1.54, 'Γ
').to('nm')
print(bond_len)
# Lab Equipment: Convert inches to centimeters
tube_dia = Length.parse("0.25 inch").to('cm')
print(tube_dia)
# Geographical scale: Convert kilometers to miles
dist = Length(10, 'km').to('mi')
print(dist)
Areaο
Surface and Interface Dimensionsο
The Area class handles two-dimensional spatial measurements. In physical chemistry, this is critical for calculating cross-sectional areas of molecular beams, the active surface of electrodes, or the footprint of Langmuir monolayers.
Supported Unitsο
The following units are handled natively:
Symbol |
Name of the Unit |
|---|---|
|
Square Meter (SI Base Unit) |
|
Square Centimeter |
|
Square Millimeter |
|
Square Γ ngstrΓΆm |
|
Hectare |
|
Are |
|
Square Inch |
|
Square Foot |
|
Acre |
|
Square Mile |
Common Conversionsο
Whether you are calculating the area of a microscope slide or the cross-section of a capillary, the Area class simplifies the math.
from pyphyschemtools import Area
# Microscopy: Convert square inches to square centimeters
slide_area = Area(1, 'sq_inch').to('cm2')
print(slide_area)
# Surface Science: Convert Γ
Β² (atomic scale) to nmΒ²
site_area = Area(20, 'Γ
2').to('nm2')
print(site_area)
# Agriculture/Environment: Convert hectares to acres
field = Area.parse("2.5 ha").to('acre')
print(field)
Volumeο
Three-Dimensional Capacityο
The Volume class manages three-dimensional space measurements. It is essential for stoichiometry, titrations, and chemical engineering, bridging the gap between metric laboratory glassware (L, mL) and industrial containers (gallons, barrels).
Supported Unitsο
The following units are handled natively:
Symbol |
Name of the Unit |
|---|---|
|
Cubic Meter (SI Base Unit) |
|
Cubic Centimeter (mL) |
|
Liter |
|
US Liquid Gallon |
|
US Liquid Quart |
|
US Liquid Pint |
|
US Cup |
|
Teaspoon |
|
Tablespoon |
|
US Fluid Ounce |
|
Oil Barrel |
Common Conversionsο
Whether you are preparing a buffer or scaling a pilot plant reactor, the Volume class ensures precise fluid management.
from pyphyschemtools import Volume
# Lab Preparation: Convert Liters to Milliliters
v_flask = Volume(0.25, 'L').to('mL')
print(v_flask)
# US Standards: Convert fluid ounces to mL
v_sample = Volume.parse("8 oz").to('mL')
print(v_sample)
# Bulk Chemistry: Convert Gallons to Liters
v_tank = Volume(55, 'gal').to('L')
print(v_tank)
Massο
From Subatomic to Industrial Scalesο
The Mass class manages the physical quantity of matter. It is designed to handle the extreme scales encountered in physical chemistry, from the mass of a single proton (atomic mass units) to the mass of large-scale chemical reactors (metric tons).
Supported Unitsο
The following units are handled natively:
Symbol |
Name of the Unit |
|---|---|
|
Kilogram (SI Base Unit) |
|
Gram |
|
Metric Ton |
|
Unified Atomic Mass Unit |
|
Carat |
|
Pound |
|
Ounce |
|
Grain |
Common Conversionsο
Whether you are weighing a catalyst on a microbalance or ordering bulk solvents, the Mass class ensures CODATA precision.
from pyphyschemtools import Mass
# Analytical Chemistry: Convert grams to milligrams
reagent = Mass(0.005, 'g').to('mg')
print(reagent)
# Particle Physics: Convert atomic mass units to grams
proton_mass = Mass(1.007, 'u').to('g')
print(proton_mass)
# Logistics: Convert kilograms to pounds
shipping_wt = Mass(25, 'kg').to('lb')
print(shipping_wt)
Molar Massο
The Stoichiometric Bridgeο
The MolarMass class (or Molecular Weight) is the fundamental scaling factor in chemical calculations. It relates the mass of a substance to the amount of substance (moles), bridging the gap between single molecules and bulk materials using the Avogadro constant (\(N_A\)).
Supported Unitsο
The following units are handled natively:
Symbol |
Name of the Unit |
|---|---|
|
Kilogram per mole (SI Base Unit) |
|
Gram per mole |
|
Dalton |
|
Unified atomic mass unit |
Common Conversionsο
Whether you are calculating the molecular weight of a small organic molecule or a massive protein, the MolarMass class ensures consistent dimensionality.
from pyphyschemtools import MolarMass
# Organic Synthesis: Convert g/mol to kg/mol for industrial scaling
mw_glucose = MolarMass(180.16, 'g/mol').to('kg/mol')
print(mw_glucose)
# Biochemistry: Working with Daltons for proteins
bsa_mw = MolarMass(66463, 'Da').to('g/mol')
print(bsa_mw)
# High-Precision Physics: Convert unified atomic mass units
c12_mass = MolarMass(12.000, 'u').to('g/mol')
print(c12_mass)
Densityο
Mass per Volume Relationshipsο
The Density class manages the concentration of matter within a specific space (\(\rho = m/V\)). It is a vital property for solvent preparation, determining the concentration of solutions, and identifying unknown substances in the laboratory.
Supported Unitsο
The following units are handled natively:
Symbol |
Name of the Unit |
|---|---|
|
Kilogram per Cubic Meter (SI Base Unit) |
|
Gram per Cubic Centimeter |
|
Gram per Milliliter |
|
Gram per Liter |
|
Pound per Cubic Foot |
Common Conversionsο
Whether you are converting from high-density materials (gold) to industrial fluids, the Density class ensures accuracy.
from pyphyschemtools import Density
# Materials Characterization: Convert g/cm3 to SI units
rho_au = Density(19.3, 'g/cm3').to('kg/m3')
print(rho_au)
# Industrial Standards: Convert pounds per cubic foot to g/cm3
rho_gasoline = Density.parse("45 lb/ft3").to('g/cm3')
print(rho_gasoline)
# Solution Prep: Convert g/L (common in biology) to g/mL
rho_sol = Density(150, 'g/L').to('g/mL')
print(rho_sol)
Temperatureο
Absolute and Relative Scalesο
The Temperature class handles conversions between thermodynamic temperature (Kelvin) and common relative scales (Celsius, Fahrenheit). Unlike other physical quantities, temperature conversions involve both a scaling factor and a numerical offset.
Supported Unitsο
The following units are handled natively by the Temperature class:
Symbol |
Name of the Unit |
|---|---|
|
Kelvin (SI Base Unit) |
|
Degree Celsius |
|
Degree Fahrenheit |
|
Degree Rankine |
Common Conversionsο
The Temperature class ensures that the delicate offsets (like the 273.15 constant for Celsius) are applied with scientific precision.
from pyphyschemtools import Temperature
# Laboratory Standard: Convert Celsius to Kelvin
t_room = Temperature(25, 'Β°C').to('K')
print(t_room)
# US Engineering: Convert Fahrenheit to Celsius
t_body = Temperature(98.6, 'Β°F').to('Β°C')
print(t_body)
# Cryogenics: Convert Kelvin to Fahrenheit
t_liquid_N2 = Temperature(77, 'K').to('Β°C')
print(t_liquid_N2)