19. Using pints for units in python
Oct 9, 2023
It's critical to keep track of units in scientific computing. Fortunately, python has a few options for tracking and converting units. Here we show you a unit conversion library and the power pint library. We also introduce scipy constants and the molmass library to calculate elemental molar mass. This video has lots of examples of unit conversion for materials science.
00:00 why care about units?
1:30 unit_converter
3:30 pints library
7:20 combining units
9:15 scipy constants
12:15 molmass library
17:20 custom units
18:55 electrical conductivity
Hey everybody, it's Taylor.
Today we're gonna talk about using units in Python.
In scientific computing, it is really important
in many instances to keep track of units.
In fact, some of the great engineering disasters out there
have been related to units, believe it or not.
Take the Mars Climate Orbiter, right?
This was back in 1999.
It was a spacecraft, right, that was around Mars.
But when the people back at Mission Control
sent it a command, they sent it with the non-SI units
of pound-force-seconds, barbarians,
when it was expecting the units in SI units
of newton-seconds, and so it got the wrong amount of force,
and so it actually went too close to the planet,
and they think that it was either destroyed
in the atmosphere of Mars, or it got sent
into heliocentric space, leaving Mars' atmosphere.
So in either scenario, it was a disaster,
and it was because of units.
So we've gotta keep track of units.
And if you've used something like Wolfram Alpha,
you know it can do really cool stuff.
You can tell it to, hey, go from five centigrade
to five F, no problem, right?
And it can do that.
But it can do more complicated things, right?
If you use Wolfram Alpha, you can tell it
like 60 miles per hour per joule, right, if you want,
to kilometers per second per electron volt.
Technically, those are like nonsense units, right,
for some funky situation, but it can still give them to you.
It can still do these conversions, right?
So yeah, kilometers per second per electron volt joule,
right, so if you want to,
there are software programs out there
that can do these conversions.
But when we are writing our software,
our little code snippets,
we wanna be able to do them too, right?
So how do you do them in Python?
There's several different ways.
One way to do it, I found a library called unit conversion.
So from the library unit converter,
from unit underscore converter, that module,
we're going to import a dot conversion converter.
We're going to import convert.
So this is one way to convert things.
We'll call it just a simple conversion tool
for one-time conversion.
This might be what you're looking for, okay?
How it works is we're going to give it the command convert,
which we've just brought in,
and then it says quantity in a string format
to the desired unit in a string format.
So it just wants two strings, right?
And here they give an example, right?
2.78 DAN times millimeter squared to MN,
millinewtons towards micrometer squared, right?
It would convert that out.
Let's do a different one.
Let's do, yeah, we'll do 60 meters per second, right?
We want to convert that to, let's do kilometers per hour,
right, if we want to make that conversion.
So let's go ahead and run that.
If we run this whole thing, we get an error.
It doesn't know, it doesn't like the,
it doesn't like that we called it HR.
Let's just try H.
Okay, so it looks like it ran that.
Let's see what it does by just highlighting that
and hitting F9, and sure enough,
it spits out the conversion right there.
60 meters per second is equal to that number
in kilometers per hour, right?
So this is a pretty simple tool
that works for a lot of things,
but it's not the most powerful.
What we would really like is when we,
when you're writing programs, you've got X equals,
you know, it's some variable that equals five, maybe gallons.
So what we'd really like to do is store this
as five gallons, right?
So that we can use it in our code and come back to it
and pull out the five or change the gallons
to something else.
How do we do that, right?
To do that, we need to bring in a different library.
This one's called Pint.
We're gonna import Pint.
By the way, these are custom libraries
that did not come standard with this installation
of Anaconda, but it's really easy to install them.
You're just gonna go to your start
and you're gonna type Anaconda.
And when you get to this Anaconda prompt, right?
This allows you to do PIP installation.
So you could just do PIP install Pint, that's it.
That will install it.
When I hit run, first off, it's gonna tell me,
hey, you've already got it.
So it's gonna say requirement already satisfied.
Like you've already got PIP or Pint, I mean.
So I don't need to do anything.
And when you, if you don't like it,
if you install a library that you don't like,
you can just do PIP uninstall Pint.
And this will give me the option for uninstalling it.
Now, since I wanna use it today, I'm not gonna uninstall it.
So when it says, do you wanna proceed yes or no?
I'm gonna say no, I'm rid of it.
So I'm gonna keep it, right?
So that lets me to have Pint.
Now, Pint lets you do some really cool things.
Pint has what's called a unit registry.
So I'm gonna make a new variable called U.
So I don't have to type out this unit registry every time.
So U is equal to Pint.unitregistry, okay?
So this method allows me to do some really cool things.
It allows me to keep track of the quantity and the unit,
right?
The magnitude of something
and the units associated with it, right?
I'm gonna, we're gonna bring in one other thing.
We're gonna do Q equals U dot,
where U is now this Pint.unitregistry.
We're gonna bring in quantity function, okay?
Or the quantity method, okay?
We're gonna use both of those.
Now here's how you use them.
Let's say that we wanna store a value.
Oh, it's giving me an error.
That's cause this needs a parentheses right there.
Okay, let's try that again.
Great, now it's working.
So let's try and do the same conversion
that we did up here,
but we're going to do it using Pint, okay?
So let's go ahead and just make a variable called speed,
right?
So our speed is equal to,
we're going to use this Q command, right?
Which is really doing Pint.unitregistry.quantity, right?
We're just, it's making it very simple.
With the simple value of Q,
we now can type in two things.
We're gonna type in the values.
We're gonna do 60.
And then the second thing that it's gonna expect
is the units, so meters per second.
So as a string,
we're just gonna do meters divided by seconds, okay?
I don't think you have to type out meters.
Let's try that and see if it works for us, okay?
So let's test it now.
Let's come down here in our console
and type in speed.units, right?
And this is going to return the units of that.
Now, unfortunately,
I've got this dark background in Spyder
and this, when this library, by default,
writes the units in black text,
so it's hard to see,
but hopefully you can see that it says
meter per second there, so great.
It knew that M is meter, right?
It knew that notation.
Great.
Now, it's really simple to do a conversion.
We can just write Print.
Okay, we're gonna do speed and then .2, right?
So this method two allows us to make the conversion,
where we just type in the units that we want.
So it could be inches per hour, right?
If we want, let's run it.
So it's gonna,
it takes it in 60 meters per second
and it's gonna print it into inches per hour
and it's this number down there.
So how slick is that?
Really, really powerful tool.
Now, what if you don't want this whole string to get output
but you just want the number part of it?
Easy peasy.
We can do that too.
And we could just type speed, speed.magnitude.
And it just returns the number part,
not the units part, right?
The magnitude.
And again, it kept it in the original form.
So we did this speed two
and we converted it to a different form.
That did not re-save it over the initial speed variable.
It just did a inline conversion of it
from one unit to another, okay?
So that's the basics of using Pint.
Let's do some examples.
Okay, let's take two units, right?
Two variables that each have units
and try combining them,
either by multiplying or dividing.
Let's take this.
Let's take distance equals, I don't know.
We're gonna use our Q command, right?
That means that we're declaring a variable
that has a quantity and a unit associated with it.
So let's say that it's eight, we'll say meters, okay?
So we now have a distance.
Now let's take time where time is equal to,
again, we're gonna use Q
because it's gonna be a quantity.
Let's say 15 and then seconds, okay?
Now let's take speed is equal to distance divided by time
and run it and see what happens, right?
So we run it.
We've now overwritten speed.
Okay, so let's go ahead and grab this print column
and bring it down here and see what it does now.
So let's do it in meters per second.
Meter per seconds.
Run that and it should be eight divided by 15
or roughly half, 0.533 meters per second, great.
So how cool is that?
It's able, you can store these things, multiply them,
divide them, and it knows what to do with the units as well.
So that's something really great that Pine can do.
If we wanted to, we could re-save this
in our speed variable.
We can say speed equals speed.2
and then we could do, we could do unit.
Let's say kilometers divided by unit.hour, right?
If we wanna do that, we could.
When we come down here now,
let's go ahead and reprint it again after doing that.
So let's test it down here.
Now, when we go to it, we go to speed.magnitude
and sure enough, that is the value in kilometers per hour.
Let's make sure that that's the right unit.
We're gonna go to speed.units
and again, sorry for the black text,
but it's in kilometers per hour.
So pretty slick.
Pints is really pretty powerful.
Let's do another example.
Okay, for this one,
we're gonna bring in some other constants.
For that, we're gonna bring in another library.
We're gonna bring in SciPy.
So we're going to import SciPy.
That's for scientific Python.
We will do lots more of that soon
when we get to solving equations,
but from SciPy.constants,
we're going to import SciPy.constants as CNST.
We'll just call it CNST, okay?
So this allows us to bring in constants.
Well, what kind of constants?
Well, we could say that C, the speed of light,
is equal to CNST, that's SciPy.constants.C.
That is the default value for the speed of light.
So let's go ahead and run that.
If we come to our variable explorer,
okay, let's check it.
Over here in our value for C,
it's a float, and it is just this value.
So interestingly, with SciPy,
when you bring in this constant,
it doesn't have units.
It just gives us the value of that constant, right?
The value of the speed of light is 2.9979 e to the eighth.
All they have is the float and not the units.
So let's go ahead and take this constant
and let's give it units using pint.
So we're gonna combine these two.
We're gonna say that C, our speed of light,
is not just equal to the constant value,
that's just the quantity.
We're gonna use this quantity method from pint,
where the value needs to be the value
from the SciPy constant, but the units need to be,
we know that's in meters per second, right?
So let's go ahead and do that, run it.
Now, if you look at this, our C is no longer just a float,
it is now this quantity, which is a pint class, right?
It's the build quantity class, okay?
And if you look at it, we would find under magnitude,
gotta scroll all the way down
inside these fields of the class,
inside here in our magnitude,
you'd see there's the value, right, the magnitude.
And then if we looked under units,
we would be able to see the units.
So under units, we would be able to see
what type of units it has.
I think you have to click that open, okay?
All right, so let's go ahead and figure out
how far you could go in a light year.
What's the distance of a light year?
Well, it's the speed of light times one year.
All right, so we've already defined our constant
for the speed of light, and we've given it units.
Let's define a light year.
So a light year is equal to, it's gonna be C,
multiplied by, now we need to define one year.
We haven't made a variable for one year,
but we can do it right here in line.
We're gonna do the Q command, right,
our function from pint that creates a registry, right,
a quantity, so that's gonna be one,
and then the comma for the units will be year.
So now we've defined our light year,
so these are multiplied together.
Now we can go ahead and convert it.
We can go ahead and print.
We're gonna print the value of a light year,
and we're gonna do the to command.
We're gonna convert it to just,
this should just have the units of meters, right,
because the seconds and the years can cancel out
with the correct conversion factor,
and the to function knows how to do that.
So let's go ahead and print it,
and sure enough, we find that, yeah,
a light year is that far.
It's a very large number of meters, okay?
So that's a pretty great command that we can do.
Let's do another example.
For this one, we're gonna bring in chemical information.
So we need a new library that stores things
like elemental masses, right,
the molar mass of the different elements,
because a lot of the conversions that we do
in material science have to do with chemistry.
So from that, we need a new library,
and that library is called mole mass.
So we're going to import mole mass,
and from mole mass, actually,
we're going to import, it's called formula, capital K,
formula.
So this allows us to do some really cool things.
So let's come down here for this example.
Let's say that we want to ask the question,
what is the density of sodium chloride
if the lattice parameter, 563 nanometers, okay?
Well, we can go ahead and do this.
First off, we need to say, let's see, formulas per cell.
We're gonna make a variable.
Because this is the rock salt structure,
we know that there are four unit cells,
or four formulas per unit cell.
So that's gonna be four.
So now we need to know,
if we're gonna figure out the density, right,
density is equal to mass divided by volume, okay?
So we haven't defined mass or volume yet, but we can.
What's the mass going to be?
Well, the mass is going to be the weight of each formula,
and there's gonna be four of those, right,
divided by Avogadro's number
in order to get that in the right units.
So mass is going to be, what is the mass of NaCl?
Well, you could look it up,
or we can use this really great library, molmass.
And this formula command, let's go ahead and do it.
Let's say that NaCl, we're gonna have that equal to
formula.NaCl, and you can see in the help,
they tell us what they're looking for.
It's looking for a string that represents
chemical elements, isotopes, parentheses, numbers.
It's asking for us to put in the formula.
So for this one, we can just put in NaCl.
So we've stored that.
Now, once we do that, the mass is equal to NaCl.mass, okay?
So let's comment this line out for just a moment, okay?
So let's check it so far.
What do we have?
When we run this, let's come up here to our mass
is equal to a float,
and that will be the value in grams per mole.
Now, we probably wanna divide that by Avogadro's number
to get the number of grams, okay?
So we can do that.
So this is going to be divided by Avogadro's number.
Well, fortunately, that comes from SciPy.
So if we have our constant, right,
that we brought in from the SciPy library, constants,
then we're gonna type in Avogadro's.
Oh, great, it knows what we're looking for,
so it helps us finish it.
Now, we need to have the units here.
So let's go ahead and give this units as well.
So this first one, the mass is going to be in what?
That's going to be in grams per mole.
So let's use the quantity method, right?
The value is that the units are going to be grams per mole.
So that's our first quantity.
Now, the quantity for Avogadro's number is going to be Q,
again, where it's the value of Avogadro's number
we get from the constant.
Now, we need to give the units,
which is one divided by mole, okay?
All right, so now we have stored this mass, right?
So now let's go to do our volume.
How do we know what the volume is?
Well, it's rock salt, so it's a cubic crystal structure.
That makes the volume really easy.
The volume is just going to be equal to,
again, we're gonna do the Q command for quantity.
That allows us to work a number with units.
So this is equal to the lattice parameter, 0.563,
and the units for that are nanometers, okay?
This needs to be cubed, so we do that.
Multiply, multiply, that means take the exponent.
We're gonna cube that, that gives us volume,
and now we are ready to define our density term,
which is mass divided by volume.
Finally, let's go ahead and print our density dot two, okay?
We're gonna convert this
to, let's say, grams per centimeter cubed, okay?
Oh, this is capital volume,
and this was lowercase over here, so it wasn't liking that.
Let's try that again.
This time it's gonna print it out,
and if we did this correctly, something's not quite right.
Ah, what did we miss?
So this is not the right number.
Salt is heavier than that.
This would be salt that floats, and it doesn't.
What are we missing?
Well, under mass, so we have the,
here we have the mass of salt divided by Avogadro's number,
but we forgot to multiply it by the formulas per unit cell,
so we need to include that.
So let's grab this, and this needs to be multiplied by that.
Okay, let's try that one more time,
and it should print out the density of salt now.
Yeah, there we go, 2.17 grams per cc.
Let's check to see if that's right.
Density of NaCl, yeah, 2.16 grams per centimeter cubed.
So here you can see a really powerful tool
to bring in this chemical information for units,
bring in constants from SciPy,
bring in unit conversions from Pint,
and working together, you can get it
to do some pretty slick stuff for you.
All right, let's do two last examples.
One is we can define our own units.
If while you're working, you come up with,
you know, you wanna be able to work
with your own custom units, great.
There's the story of this guy at MIT, right?
Smoot, if you haven't looked this up,
it's kind of interesting, an MIT Smoot unit.
He was a kid, I think in like a fraternity,
and as a prank, they used him as a measurement of length,
like however long that person was,
and so they did this measurement out to the bridge.
In any case, he is now an official unit.
It's equal to 1.702 meters.
So we can define that as a custom unit in Python
if we want using Pint.
So we're going to do custom units, right?
So we're gonna do u.define as the function now,
and we're gonna call it a Smoot.
So one Smoot is equal to 1.702 meters,
and then you can even give it like a shorthand, right?
You can call it SM for Smoot, okay?
So now we've defined a unit in Smoot,
so we could take a quantity, right?
We could print the quantity.
Well, let's make a variable.
Our distance from before, right?
Eight meters, let's go and convert that to Smoots, right?
Great, so we can print.
We're gonna print distance.2,
and it's going to be in the units of Smoots, right?
So let's do that, and when we print it out,
it tells us that eight meters is equal to 4.7 Smoots, right?
So you can define your custom units.
Okay, last thing we want to cover
is an example of just another calculation.
Let's do one for electrical conductivity.
Electrical conductivity.
Now there's a formula where we know that sigma,
the electrical conductivity, is equal to n,
the carrier concentration,
multiplied by the mobility,
multiplied by the charge, right?
So let's go ahead and do that.
Let's define these things.
We'll say that n is equal to,
we're gonna use q because it's a quantity.
Let's say it's like one e to the 17th.
So you can use scientific notation here.
That's no problem.
And then we have to give that its units.
That has the units of centimeters to the negative three.
So that allows us to define our concentration.
Now let's define our mobility.
Now I looked this up ahead of time,
and the mobility, we'll call this mu,
is equal to q, it's 1.5 e to the fourth,
and that has units of centimeter squared
per volt per second, right?
So I looked that up for silicon.
That's the mobility of an electron in silicon
at this concentration of our carriers, right?
Now the last thing we need is our charge of an electron.
So we're gonna say e is our charge of an electron.
We're gonna do cnstconstants.e.
That is in the SciPy library.
That's one of the constants.
That's the charge of an electron, okay?
But we need to give that units.
So we're gonna do q, okay?
And then tell it the units, right?
And that is coulombs.
Great.
So now to go ahead and define our new value,
it's going to be sigma is equal to n times mu times e.
And now we can print our sigma,
and we can convert it to whatever we want.
So we're gonna do sigma.2.
Let's convert it to siemens per meter, right?
So this is siemens per meter.
We run that out, and we see that it's able to convert.
Oh, got an error.
Oh, it says siemens is not in the unit register.
Let's try siemen for real quick.
Okay, interestingly, it doesn't have
the unit for siemens built in, surprisingly.
That's okay.
We know that siemen is equal to one over an ohm, right?
So we can say ohms to the negative one divided by meter.
Or we could say times meters to the negative one, right?
It should be able to do that.
Great.
So yeah, and that's the right number.
24,000 is the one that I calculated ahead of time.
So this is just a quick primer on how you can use
some really cool tools for handling units,
for dealing with them in your scientific computing,
which is honestly probably gonna run into
as a material scientist is wanting to keep track of units.
And these are a couple really simple ways to do it.
I suggest learning how to use Pint.
I think it's the best.
And we will get into a lot more detail
of SciPy coming up soon.
Okay, stay tuned.
On to the next video.

