{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 7 : Diffusion in Solids"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 7.1 Page No : 207"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "time required for carburization in 142.8 min\n"
     ]
    }
   ],
   "source": [
    "\n",
    "import math \n",
    "from scipy.special import erfinv\n",
    "\n",
    "# Variables\n",
    "D = 1.28*10**(-11);\t\t\t#diffusion coefficient of carbon in given steel in m2/s\n",
    "c_s = 0.9;\t\t\t#Surface concentration of diffusion element in the surface\n",
    "c_o = 0.2;\t\t\t#Initial uniform concentration of the element in the solid\n",
    "c_x = 0.4;\t\t\t#Concentration of the diffusing element at a distance x from thesurface\n",
    "x = 0.5*10**(-3);\t\t\t#depth from the surface in m\n",
    "\n",
    "# Calculation\n",
    "#(c_s-c_x)/(c_s-c_o) = erf(x/(2*(D*t)**(1/2)))\n",
    "t = (x/(2*erfinv((c_s-c_x)/(c_s-c_o))*D**(1./2)))**2;\t\t\t#time required for carburization(in sec)\n",
    "\n",
    "# Results\n",
    "print 'time required for carburization in %.1f min'%(t/60)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 7.2 Page No : 208"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " C1  =  0.0\n",
      "time required to get a boron content of 1023 atoms per m3 at a depth of 2 micro meter is = 3845 sec\n"
     ]
    }
   ],
   "source": [
    "\t\t\t\n",
    "import math \n",
    "from scipy.special import erfinv\n",
    "\n",
    "# Variables\n",
    "D = 4*10**(-17);\t\t\t#diffusion coefficient of carbon in given steel in m2/s\n",
    "c_s = 3*10**26;\t\t\t#Surface concentration of boron atoms in the surface\n",
    "c_1 = 0;\t\t\t#Initial uniform concentration of the element in the solid\n",
    "c_x = 10**23;\t\t\t#Concentration of the diffusing element at a distance x from thesurface\n",
    "x = 2*10**(-6);\t\t\t#depth from the surface in m\n",
    "\n",
    "# Calculation and Results\n",
    "#(c_s-c_x)/(c_s-c_1) = erf(x/(2*(D*t)**(1/2)))\n",
    "a = (erfinv((c_s-c_x)/(c_s-c_1)));\n",
    "print ' C1  = ',a\n",
    "t = (x**2/(D*4*(2.55)**2));\t\t\t#time required to get a boron content of 1023 atoms per m3 at a depth of 2 micro meter\n",
    "print 'time required to get a boron content of 1023 atoms per m3 at a depth of 2 micro meter is = %.0f sec'%t\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 7.3 Page No : 208"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "activation energy = 2.93e-19 J\n",
      "constant of the equation = 2.68e-04 m2/s\n",
      "diffusion coefficient at 500°C = 3.27e-16 m2/s\n"
     ]
    }
   ],
   "source": [
    "\n",
    "import math \n",
    "\n",
    "# Variables\n",
    "t_1 = 736.;\t\t\t#Temperature in °C\n",
    "t_2 = 782.;\t\t\t#Temperature in °C\n",
    "T_1 = t_1+273;\t\t\t#Temperature in K\n",
    "T_2 = t_2+273;\t\t\t#Temperature in K\n",
    "D_1 = 2.*10**(-13);\t\t\t#Coefficient of diffusion at T_1 (in m2/s)\n",
    "D_2 = 5.*10**(-13);\t\t\t#Coefficient of diffusion at T_2 (in m2/s)\n",
    "k = 1.38*10**(-23);\t\t\t#in J/K\n",
    "\n",
    "# Calculation and Results\n",
    "#math.log(d_1) = math.log(d_o)-E/(k*T_1)\n",
    "#math.log(d_2) = math.log(d_o)-E/(k*T_2)\n",
    "E = (math.log(D_1)-math.log(D_2))/((1/(k*T_1))-(1/(k*T_2)));\t\t\t#\n",
    "print 'activation energy = %.2e J'%-E\n",
    "D_o = 2.*10**(-13)/math.exp(E/(k*T_1));\n",
    "print 'constant of the equation = %.2e m2/s'%D_o\n",
    "t_4 = 500.;\t\t\t#Temperature in °C\n",
    "T_4 = t_4+273;\t\t\t#Temperature in °K\n",
    "D_4 = D_o*math.exp(E/(k*T_4));\t\t\t#diffusion coefficient at 500°C\n",
    "print 'diffusion coefficient at 500°C = %.2e m2/s'%D_4\n",
    "\n",
    "# rounding off error"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 7.4 Page No : 210"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Time at 500*C that will produce the same diffusion as in 600*C in 110.4 Hours\n"
     ]
    }
   ],
   "source": [
    "\t\t\t\n",
    "import math \n",
    "\n",
    "# Variables\n",
    "D_500 = 4.8*10**(-14);\t\t\t#Diffusion coefficient for copper in aluminimum at 500*C(in m**2/s)\n",
    "D_600 = 5.3*10**(-13);\t\t\t#Diffusion coefficient for copper in aluminimum at 600*C(in m**2/s)\n",
    "t_600 = 10;\t\t\t#time of diffussion at 600*C(in Hours)\n",
    "\n",
    "# Calculation\n",
    "#D_500*t_500 = D_600*t_600\n",
    "t_500 = D_600*t_600/D_500;\t\t\t#time of diffussion at 500*C\n",
    "\n",
    "# Results\n",
    "print 'Time at 500*C that will produce the same diffusion as in 600*C in %.1f Hours'%t_500\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
