1.5. Unit Conversions#
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.
How can we get help using units? Programming is one way. We are going to complete some unit 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 the Anaconda distribution of Python. To use Pint, you need to install pint in your wroking version of Python. Open up the Anaconda Prompt and type:
> conda install -c conda-forge pint
If you are not using the Anaconda distribution of Python, the pint package can also be installed with pip the Python package manager.
$ 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())
Operating System: Linux6.5.0-1021-azure
Python Version: 3.11.9
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)
252 kilowatt
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)
20636629.714441698 british_thermal_unit / day
Another probem:
Convert 722 MPa to ksi#
stress = 722*ureg.MPa
stress_in_ksi = stress.to(ureg.ksi)
print(stress_in_ksi)
104.71724664121105 kip_per_square_inch
Next problem:
Convert 1.620 m/s2 to ft/min2#
accel = 1.620 *ureg.m/(ureg.s**2)
print(accel)
1.62 meter / second ** 2
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)
19133.85826771654 foot / minute ** 2
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)
1431000000.0 kilogram * kilojoule / millimeter ** 3
quant.ito( ureg.cal*ureg.lb / (ureg.inch**3))
print(quant)
1.2356155557389996e+16 calorie * pound / inch ** 3