{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#3: X-ray Diffraction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Example number 3.1, Page number 3.9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wavelength of X-rays is 0.08496 nm\n",
      "answer varies due to rounding off errors\n",
      "when theta=90, maximum order of diffraction possible is 7\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "d=0.313;     #lattice spacing(m)\n",
    "theta=7+(48/60);    #angle(degrees)\n",
    "n=1;\n",
    "\n",
    "#Calculation\n",
    "theta=theta*math.pi/180;    #angle(radian)\n",
    "lamda=2*d*math.sin(theta)/n;    #wavelength of X-rays(nm)\n",
    "#when theta=90\n",
    "n=2*d/lamda;       #maximum order of diffraction possible\n",
    "\n",
    "#Result\n",
    "print \"wavelength of X-rays is\",round(lamda,5),\"nm\"\n",
    "print \"answer varies due to rounding off errors\"\n",
    "print \"when theta=90, maximum order of diffraction possible is\",int(n)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Example number 3.2, Page number 3.10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "interatomic spacing is 2.67 angstrom\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "lamda=1.5418;      #wavelength(angstrom)\n",
    "theta=30;      #angle(degrees)\n",
    "n=1;    #first order\n",
    "h=1;\n",
    "k=1;\n",
    "l=1;\n",
    "\n",
    "#Calculation\n",
    "theta=theta*math.pi/180;    #angle(radian)\n",
    "d=n*lamda/(2*math.sin(theta));     \n",
    "a=d*math.sqrt(h**2+k**2+l**2);    #interatomic spacing(angstrom)\n",
    "\n",
    "#Result\n",
    "print \"interatomic spacing is\",round(a,2),\"angstrom\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Example number 3.3, Page number 3.10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "glancing angle is 21 degrees\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "d100=0.28;    #spacing(nm)\n",
    "lamda=0.071;    #wavelength of X rays(nm)\n",
    "n=2;    #second order\n",
    "\n",
    "#Calculation\n",
    "d110=round(d100/math.sqrt(2),3);     #spacing(nm)\n",
    "x=n*lamda/(2*d110);\n",
    "theta=math.asin(x);    #glancing angle(radian)\n",
    "theta=theta*180/math.pi;     #glancing angle(degrees)\n",
    "\n",
    "#Result\n",
    "print \"glancing angle is\",int(theta),\"degrees\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Example number 3.4, Page number 3.11"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "distance between planes is 0.27 nm\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "a=0.38;     #lattice constant(nm)\n",
    "h=1;\n",
    "k=1;\n",
    "l=0;\n",
    "\n",
    "#Calculation\n",
    "d=a/math.sqrt(h**2+k**2+l**2);     #distance between planes(nm)\n",
    "\n",
    "#Result\n",
    "print \"distance between planes is\",round(d,2),\"nm\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Example number 3.5, Page number 3.11"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "glancing angle is 32.0 degrees\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "a=0.19;     #lattice constant(nm)\n",
    "h=1;\n",
    "k=1;\n",
    "l=1;\n",
    "lamda=0.058;    #wavelength of X rays(nm)\n",
    "n=2;    #second order\n",
    "\n",
    "#Calculation\n",
    "d=a/math.sqrt(h**2+k**2+l**2);     #distance between planes(nm)\n",
    "x=n*lamda/(2*d);\n",
    "theta=math.asin(x);    #glancing angle(radian)\n",
    "theta=theta*180/math.pi;     #glancing angle(degrees)\n",
    "\n",
    "#Result\n",
    "print \"glancing angle is\",round(theta),\"degrees\""
   ]
  }
 ],
 "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.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}