This notebook contains material from chemeng316_fluids; content is available on Github.
pint
¶This is a condensed excerpt from the previous notebook that can be used as a starting point for implementing pint
in any project or assignment.
!pip install pint
Requirement already satisfied: pint in /Users/nathanmurray/opt/anaconda3/lib/python3.9/site-packages (0.20.1)
The core concept in pint is to work with a unit registry
, which is created as follows
from pint import UnitRegistry
ur = UnitRegistry()
The unit registry provides a simple means to assign units using the multiplication operator. For example, here's how to compute the molarity of a sodium chloride solution in 58.44 grams of $NaCl$ (mw = 58.44) has been dissolved in water to form 3 liters of solution.
# problem data
V = 3.0 * ur.liters
m = 58.44 * ur.grams
mw = 58.44 * ur.grams/ur.mol
# compute molarity
C = m/(mw*V)
print(C)
0.3333333333333333 mole / liter
Each variable with units has to()
and ito()
methods for converting the quantity to a desired set of units. The to()
method is used to create a new variable by converting an existing variable to the indicated units.
x = 0.5 * ur.kilograms/ur.gallon
y = x.to(ur.grams/ur.liter)
print(x)
print(y)
0.5 kilogram / gallon 132.08602617907428 gram / liter
The ito()
method converts an existing variable 'in-place'.
x = 0.5 * ur.kilograms/ur.gallon
x.ito(ur.grams/ur.liter)
print(x)
132.08602617907428 gram / liter
For example, here's how to compute the molarity of a sodium chloride solution in which 0.5 pounds of NaCl (mw = 58.44) has been dissolved in water to form 2 gallons of solution.
# problem data
V = 3.0 * ur.gallons
m = 0.5 * ur.lbs
mw = 58.44 * ur.grams/ur.mol
# compute concentration
C = m/(mw*V)
print(C)
# convert to desired units and print
C = C.to(ur.mol/ur.liter)
print(C)
# convert to moles per gallon
C.ito(ur.mol/ur.gallon)
print(C)
0.0028519279032626055 mole * pound / gallon / gram 0.34173633161332617 mole / liter 1.293612736710016 mole / gallon
Muratic Acid is concentrated hydrochloric acid (31.5% by weight) sold by the gallon in home improvement centers for cleaning brick and masonary surfaces. The density of the solution is typically about 1.15 grams/ml. What is the molar concentration of HCl?
# molecular weight
mwHCl = 36.46 * ur.grams/ur.mol
# problem data
rho = 1.15 * ur.grams/ur.ml
wHCl = 0.315 * ur.grams/ur.gram
# calculations
massHCl = wHCl * rho # mass of HCl per volume
moleHCl = massHCl/mwHCl # gmols of HCl per volume
print(moleHCl)
# convert to desired units
moleHCl.ito(ur.mol/ur.liter)
print(moleHCl)
0.009935545803620405 mole / milliliter 9.935545803620407 mole / liter
# molar weights
mwBen = 78.11 * ur.grams/ur.liter
mwTol = 92.14 * ur.grams/ur.liter
xBen = 0.4 * ur.mol/ur.mol
xTol = 0.6 * ur.mol/ur.mol
wBen = mwBen*xBen / (mwBen*xBen + mwTol*xTol)
print(wBen)
0.36108542899408286 dimensionless
mwBen = 78.11 * ur.kg/ur.kmol
print(mwBen)
mwBen.ito_base_units()
print(mwBen)
78.11 kilogram / kilomole 0.07811 kilogram / mole
Examples of non-multiplicative units are degrees Celsius, degrees Fahrenheit, and gauge pressure. For these units the zero of the measurement scale is offset from absolute zero of the underlying physical quantity. In these cases the units are assigned using the Quantity()
function of the UnitRegistry
module.
T = ur.Quantity(25,ur.degC)
print(T, " = ", T.to(ur.degF))
25 degree_Celsius = 76.99999999999993 degree_Fahrenheit
There is an embedded ambiguity in working with non-multiplicative units. For example, given temperatures Ta = 10 and Tdelta = 25 in degrees C, should Ta + Tdelta give an answer of 35 degrees C, or the sum of the absolute temperatures which would correspond to 308.15 degrees C?
Pint uses unit type called delta_degC
for this situation.
Ta = ur.Quantity(10.0, ur.degC)
Tdelta = 25.0 * ur.delta_degC
print(Ta + Tdelta)
35.0 degree_Celsius
Tb = ur.Quantity(100.0, ur.degC)
Tf = ur.Quantity(32.0, ur.degF)
print("{0:6.3f}".format(Tb-Tf))
print("{0:6.3f}".format((Tb-Tf).to(ur.delta_degF)))
100.000 delta_degree_Celsius 180.000 delta_degree_Fahrenheit
P1 = (12.5 + 14.696) * ur.psi
T1 = ur.Quantity(70, ur.degF).to(ur.degR)
T2 = ur.Quantity(40, ur.degF).to(ur.degR)
P2 = (T2/T1)*P1 - 14.696 * ur.psi
P2
import math
pi = math.pi
# constants
R = 8.314 * ur.joule/(ur.mol*ur.degK)
mw = 28.966 * ur.grams/ur.mol
# problem data
P = 1.0 * ur.atm
T = ur.Quantity(25,ur.degC).to(ur.degK)
r = 10 * ur.cm
# calculations
V = (4.0/3.0)*pi*r**3
print("Volume = ", V)
m = mw*P*V/(R*T)
print("Mass of Air = ", m.to(ur.grams))
Volume = 4188.790204786391 centimeter ** 3 Mass of Air = 4.95962584191983 gram