Units and unit conversions are BIG in engineering. Engineers solve the world's problems in teams. Any problem solved has to have a context. How heavy can a rocket be and still make it off the ground? What thickness bodypannels keep occupants save during a crash? In engineering, a number without a unit is like a fish without water. It just flops around hopelessly without context around it is useless.
How can we get help using units? Programming is one way. We are going to complete some uit conversion problmes using Python and Pint. Pint is a Python package used for unit conversions.
See the (Pint documentation) for more examples.
I recommend that undergraduate engineers use Python 3 (Python 2.7 is legacy python) and the Anaconda distribution. To use Pint, we need to install pint in our wroking version of Python. Open up the Anaconda Prompt:
> pip install pint
I am working on a Windows 10 machine. You can check your operating system and Python version using the code below:
import platform
print('Operating System: ' + platform.system() + platform.release())
print('Python Version: '+ platform.python_version())
Before we can complete a unit conversion with the Pint package, we need to import
the Pint module and instantiate a UnitRegistry
object. The new ureg
object contains all the units used in the examples below.
import pint
ureg = pint.UnitRegistry()
For our first problem, we will complete the following converison:
Convert 252 kW to Btu/day¶
We'll create a variable called power
with units of kilowatts (kW). To create the kW unit, we'll use our ureg
object.
power = 252*ureg.kW
print(power)
To convert power
to Btu/day, we use Pint's .to()
method. The .to()
method does not change the units of power
in place. We need to assign the output of the .to()
method to another variable power_in_Btu_per_day
power_in_Btu_per_day = power.to(ureg.Btu / ureg.day)
print(power_in_Btu_per_day)
Another probem:
Convert 722 MPa to ksi¶
stress = 722*ureg.MPa
stress_in_ksi = stress.to(ureg.ksi)
print(stress_in_ksi)
Next problem:
Convert 1.620 m/s2 to ft/min2¶
accel = 1.620 *ureg.m/(ureg.s**2)
print(accel)
This time we will use the .ito()
method. Using .ito()
will convert the units of accel
in place.
accel.ito(ureg.ft/(ureg.min**2))
print(accel)
Convert 14.31 x 108 kJ kg mm-3 to cal lbm / in3¶
quant = 14.31e8 * ureg.kJ * ureg.kg * ureg.mm**(-3)
print(quant)
quant.ito( ureg.cal*ureg.lb / (ureg.inch**3))
print(quant)