<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 I was working through a diffusion problem and thought that Python and a package for dealing with units and unit conversions called pint would be usefull.
I'm using the Anaconda distribution of Python, which comes with the Anaconda Prompt already installed. For help installing Anaconda, see a previous blog post
Step1:  Let's start with the temperature, T = 300 &deg;C.
Temperature units in &deg;F and &deg;C are relative units with an off-set scale. &deg;C and &deg;F are not multiplicative units.  Non-multiplicatve units are handled by pint a little differently compared to regular multiplicative units.  
To create a variable including a unit of degrees C, we instantiate a Quantity object and pass in the temperature in &deg;C along with the unit (u.degC). We can convert the temperature to Kelvin (K) using the .ito method. 
Since we want to do some mulipication, division and exponentiation with our temperature, we need to convert the temperature to a multiplicative unit. Pint has two versions of the temperature unit in Kelvin (K). There is the non-multiplicative type degK and the multiplicative type kelvin. 
We convert the temperature variable T to the multiplicative type kelvin by pulling out the magnitude (the number part without the degK unit) from the T variable and multiplying it by the kevlin unit from pint.
Step2:  Next we'll create variables for $Q_n = 37,600 \frac{J}{mol}$ and the universal gas contant $R=8.31 \frac{J}{mol-K}$
Step3:  Our first nitrogen pressure is 0.10 MPa and our second nitrogen pressure is 5.0 MPa, we'll make variables for both
Step4:  Now we can calculate the two nitrogren concentrations in wt% using the equation
Step5:  These values CN1 and CN2 are in units of wt% N in an iron-nitrogen "alloy" where almost all of the alloy is iron with only a small amount of nitrogen. To use Fick's First Law of Diffusion
Step6:  Now we'll determine how many kg of nitrogen there are in 1 kg of iron given our concentrations CN1 and CN2 in wt%.  Note that we have to multiply CN1 and CN2 by 0.01 because CN1 and CN2 are in units of %.  
When we divide the mass of nitrogen by the volume of iron, we get a concentration of nitrogen in iron in units of kg/m<sup>3</sup>, which is the concentration units we need to use the Fick's First Law of Diffusion.
Step7:  Back to Fick's Fist Law of Diffusion
Step8:  Next we need to find the diffusion coefficient $D$. To do this, we need the pre-exponential term $D_0$ and the activating envery of diffusion $Q_d$.
From the beginning of the problem
Step9:  To calculate diffusion constant $D$, we use the equation which relates diffusion coefficient, $D$ to temperature, $T$ according to
Step10:  Now that we have $D$, $dC$ and $dx$, we can finally calculate diffusion flux, $J$ through the plate using Fick's First Law of Diffusion
<ASSISTANT_TASK:>
Python Code:
import pint
from math import exp, sqrt
u = pint.UnitRegistry()
Explanation: I was working through a diffusion problem and thought that Python and a package for dealing with units and unit conversions called pint would be usefull.
I'm using the Anaconda distribution of Python, which comes with the Anaconda Prompt already installed. For help installing Anaconda, see a previous blog post: Installing Anaconda on Windows 10. 
To use the pint package, I needed to install pint using the Anaconda Prompt:
```
pip install pint
```
The problem I'm working on involes the diffusion of nitrogen gas (N<sub>2</sub>) into a thin plate of iron. 
Given:
When &alpha;-iron is put in a nitrogen atmosphere, the concentration of nitrogen in the &alpha;-iron, $C_{N}$ (in units of wt%) is a function of the nitrogen pressure $P_{N_2}$ and temperature $T$ accoding to the relationship:
$$C_{N} = 4.9 \times 10^{-6} \sqrt{P_{N_2}} exp{\frac{Q_n}{RT}} $$
Where:
$Q_n = 37,600 \frac{J}{mol}$
$R=8.31 \frac{J}{mol-K}$
$T$ is the temperature in Kelvin.
At 300 &deg;C the nitrogen gas pressure on one side of an iron plate is 0.10 MPa. On the other side of the iron plate, the nitrogen gas pressure is 5.0 MPa. The iron plate is a 1.5 mm thick.  Assume the pre-exponential term $D_0$ and the activation energy of diffusion of nitrogen in carbon, $Q_d$ are equal to the values below:
$D_0 = 5 \times 10^{-7} \frac{m^2}{s}$
$Q_d = 77,000 \frac{J}{mol} $
Find:
Calculate the diffusion flux, J through the plate using Fick's First Law of Diffusion:
$$ J = -D \frac{dC}{dx} $$
Solution:
We have a couple different quantities and a couple of different units to handle to solve this problem. We'll start out importing pint and creating a UnitRegistry object. We'll also need the exp (e raised to a power) and sqrt (square root) functions from the math module, part of the Python standard library.
End of explanation
Q_ = u.Quantity
T = Q_(300, u.degC)
print('T = {}'.format(T))
T.ito('degK')
print('T = {}'.format(T))
T = T.magnitude * u.kelvin
print(T)
Explanation: Let's start with the temperature, T = 300 &deg;C.
Temperature units in &deg;F and &deg;C are relative units with an off-set scale. &deg;C and &deg;F are not multiplicative units.  Non-multiplicatve units are handled by pint a little differently compared to regular multiplicative units.  
To create a variable including a unit of degrees C, we instantiate a Quantity object and pass in the temperature in &deg;C along with the unit (u.degC). We can convert the temperature to Kelvin (K) using the .ito method. 
Since we want to do some mulipication, division and exponentiation with our temperature, we need to convert the temperature to a multiplicative unit. Pint has two versions of the temperature unit in Kelvin (K). There is the non-multiplicative type degK and the multiplicative type kelvin. 
We convert the temperature variable T to the multiplicative type kelvin by pulling out the magnitude (the number part without the degK unit) from the T variable and multiplying it by the kevlin unit from pint.
End of explanation
Qn = 37600 * u.J/u.mol
R = 8.31 * u.J/(u.mol*u.kelvin)
Explanation: Next we'll create variables for $Q_n = 37,600 \frac{J}{mol}$ and the universal gas contant $R=8.31 \frac{J}{mol-K}$
End of explanation
PN1 = 0.10
PN2 = 5.0
Explanation: Our first nitrogen pressure is 0.10 MPa and our second nitrogen pressure is 5.0 MPa, we'll make variables for both:
End of explanation
CN1 = (4.9e-3)*sqrt(PN1)*exp(-Qn/(R*T))
print(CN1)
CN2 = (4.9e-3)*sqrt(PN2)*exp(-Qn/(R*T))
print(CN2)
Explanation: Now we can calculate the two nitrogren concentrations in wt% using the equation:
$$C_{N} = 4.9 \times 10^{-6} \sqrt{P_{N_2}} exp{\frac{Q_n}{RT}}$$
where $P_{N_2}$ = 0.10 for one side of the iron plate and $P_{N_2}$ = 5.0 for the other side of the iron plate
End of explanation
p=7.874*u.g/u.cm**3
p.ito(u.kg/u.m**3)
mFe = 1*u.kg
vFe = mFe/p
Explanation: These values CN1 and CN2 are in units of wt% N in an iron-nitrogen "alloy" where almost all of the alloy is iron with only a small amount of nitrogen. To use Fick's First Law of Diffusion:
$$ J = -D \frac{dC}{dx} $$
We need a concentration gradient $dC$ in units of mass per unit volume like kg/m<sup>3</sup> or g/cm<sup>3</sup> not in units of wt %. Therefore we need to convert the two concentrations of nitrogen in iron, CN1 and CN2 from units of wt% to units of kg/m<sup>3</sup>.
To make the conversion between wt% and mass per unit volume we have to pick a sample mass of iron. This mass of iron will contain a mass of nitrogen (based on wt%). We can divide this mass of nitrogen by the volume of iron that corresponds to the mass of iron we picked. As long as we divide the mass of nitrogen by the volume of iron that contains that mass of nitrogen, we will end up with a unit conversion from wt% to kg/m<sup>3</sup> that works. So let's pick 1 kilogram of iron, and use the density of iron as 7.874 g/cm<sup>3</sup>.
We set a variable p to equal the density of iron in g/cm<sup>3</sup> and use the .ito() method to convert the density to units of kg/m<sup>3</sup>. Then we divide the mass of iron that we picked (1 kg) and convert it to volume of iron using the density p. This will give use the volume of 1kg of iron in units of m<sup>3</sup>.
End of explanation
mN1 = mFe*CN1*0.01
CN1 = mN1/vFe
print(CN1)
mN2 = mFe*CN2*0.01
CN2 = mN2/vFe
print(CN2)
Explanation: Now we'll determine how many kg of nitrogen there are in 1 kg of iron given our concentrations CN1 and CN2 in wt%.  Note that we have to multiply CN1 and CN2 by 0.01 because CN1 and CN2 are in units of %.  
When we divide the mass of nitrogen by the volume of iron, we get a concentration of nitrogen in iron in units of kg/m<sup>3</sup>, which is the concentration units we need to use the Fick's First Law of Diffusion.
End of explanation
dC = CN2-CN1
dx = 1.5 *u.mm
dx.ito(u.m)
Explanation: Back to Fick's Fist Law of Diffusion:
$$ J = -D \frac{dC}{dx} $$
The difference in concentration $dC$, is just the difference between the two concentrations CN1 and CN2 now that they are both in units of kg/m<sup>3</sup>. $dx$, the change in distance is the thickness of the plate, 1.5 mm. We'll convert the change in distance, $dx$ to units of meters using the ito() method.
End of explanation
D0 = 5e-7 * u.m**2/u.s
Qd = 77000 * u.J/u.mol
Explanation: Next we need to find the diffusion coefficient $D$. To do this, we need the pre-exponential term $D_0$ and the activating envery of diffusion $Q_d$.
From the beginning of the problem:
$D_0 = 5 \times 10^{-7} \frac{m^2}{s}$
$Q_d = 77,000 \frac{J}{mol} $
Let's assign these to variables with the applicable units.
End of explanation
D = D0 * exp(-Qd/(R*T))
print(D)
Explanation: To calculate diffusion constant $D$, we use the equation which relates diffusion coefficient, $D$ to temperature, $T$ according to:
$$ D = D_0e^{\frac{-Q_d}{RT}} $$
End of explanation
J = -D*(dC/dx)
J
Explanation: Now that we have $D$, $dC$ and $dx$, we can finally calculate diffusion flux, $J$ through the plate using Fick's First Law of Diffusion:
$$ J = -D \frac{dC}{dx} $$
End of explanation
<END_TASK>