{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ising 1D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import random\n",
    "import math\n",
    "def ising1d():\n",
    "    ising1d=[]\n",
    "    n=int(input(\"enter number of electrons in the model\"))\n",
    "    T=int(input(\"input enter temperature in kelvin\"))\n",
    "    i=0\n",
    "\n",
    "#    for i in range(n):\n",
    "#        x=int(input(\"the spin of the lattice as -1 or 1\"))\n",
    "#        ising1d.append(x)\n",
    "#        i+=1\n",
    "\n",
    "    for i in range(n):\n",
    "        ising1d.append(random.randrange(-1,3,step=2))\n",
    "        \n",
    "    print(\"lattice under consideration \",ising1d)\n",
    "    j=random.randint(1,n)#randomly chosen lattice site\n",
    "    print(\"initial lattice site randomly chosen :\",j)\n",
    "    print(\"all sites after this point will be considered\")\n",
    "    delE=0\n",
    "    i=j-1\n",
    "    s=0\n",
    "    k=1.38054852*math.pow(10,23)\n",
    "    t=math.exp(-delE/(k*T))\n",
    "    while i<n :\n",
    "        print(\"lattice site under consideration= \",(i+1))\n",
    "        \"\"\"for i in range(n):\n",
    "            x=ising1d[i]\n",
    "            print(x)\n",
    "            s=s+x\"\"\"\n",
    "        if i==0 :\n",
    "            s=s+ising1d[i+1]#nearest neighbour of first electron\n",
    "        elif i==(n-1):\n",
    "            s=s+ising1d[i-1]\n",
    "        else :\n",
    "            s=s+ising1d[i-1]+ising1d[i+1]#nearest neighbours of the electron under consideration\n",
    "        delE=2*(i+1)*ising1d[i]*s\n",
    "        print(\"delta E=\",delE)\n",
    "        if delE<0:\n",
    "            ising1d[i]=-ising1d[i]\n",
    "        else:\n",
    "            r=random.uniform(0,1)\n",
    "            if(r<=t):\n",
    "                ising1d[i]=-ising1d[i]\n",
    "        print(\"the new Lattice after energy consideration and spin change is: \",ising1d)\n",
    "        s=0\n",
    "        i=i+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "enter number of electrons in the model4\n",
      "input enter temperature in kelvin2\n",
      "('lattice under consideration ', [1, 1, 1, -1])\n",
      "('initial lattice site randomly chosen :', 4)\n",
      "all sites after this point will be considered\n",
      "('lattice site under consideration= ', 4)\n",
      "('delta E=', -8)\n",
      "('the new Lattice after energy consideration and spin change is: ', [1, 1, 1, 1])\n"
     ]
    }
   ],
   "source": [
    "ising1d()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.randrange(-1,3,step=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 -1 1 1 1 -1 1 -1 -1 -1 -1 1 1 1 -1 1 -1 -1 -1 1 -1 -1 -1 1 1 -1 1 1 -1 -1 -1 -1 -1 1 -1 -1 1 -1 -1 1 -1 -1 1 -1 1 1 -1 -1 1 -1\n"
     ]
    }
   ],
   "source": [
    "for i in range(50):\n",
    "    print random.randrange(-1,3,step=2),"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "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.10"
  },
  "latex_envs": {
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 0
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
