diff --git a/hands_on/HandsOn-16/HandsOn-16-Sol.ipynb b/hands_on/HandsOn-16/HandsOn-16-Sol.ipynb new file mode 100644 index 0000000..9052917 --- /dev/null +++ b/hands_on/HandsOn-16/HandsOn-16-Sol.ipynb @@ -0,0 +1,188 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:eda61121733f1facb114934c5dd943d52ec00371f7920beabf140b38f69f79c2" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy as np\n", + "import matplotlib as mpl; import matplotlib.pyplot as plt\n", + "%matplotlib inline\n", + "import os; import pickle" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Modelo de Potts Est\u00e1ndar orden 5" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def pottenergy(state1,state2):\n", + " if (state1==state2):\n", + " return -1.\n", + " else:\n", + " return 0.\n", + "def near_energy(nx,ny):\n", + " global lattice\n", + " global N\n", + " return pottenergy(lattice[nx,ny],lattice[(nx-1)%N,ny])+\\\n", + "pottenergy(lattice[nx,ny],lattice[(nx+1)%N,ny]) + \\\n", + "pottenergy(lattice[nx,ny],lattice[nx,(ny+1)%N]) + \\\n", + "pottenergy(lattice[nx,ny],lattice[nx,(ny-1)%N])\n", + "def changeone():\n", + " nx, ny = np.random.randint(0,N), np.random.randint(0,N)\n", + " spin=lattice[nx,ny]\n", + " if spin==-2:\n", + " newspin=np.random.choice(np.array([-1,0,1,2]))\n", + " elif spin==-1:\n", + " newspin=np.random.choice(np.array([-2,0,1,2]))\n", + " elif spin==0:\n", + " newspin=np.random.choice(np.array([-2,-1,1,2]))\n", + " elif spin==1:\n", + " newspin=np.random.choice(np.array([-2,-1,0,2]))\n", + " elif spin==2:\n", + " newspin=np.random.choice(np.array([-2,-1,0,1]))\n", + " en_i = near_energy(nx,ny)\n", + " # Make the change\n", + " lattice[nx,ny]=newspin\n", + " # Calculate the new energy\n", + " en_f = near_energy(nx,ny)\n", + " if (en_f>en_i) and (np.random.random()>min(1,np.exp(-beta*(en_f-en_i)))):\n", + " lattice[nx,ny]=spin # change it back\n", + "def magnetization():\n", + " global lattice\n", + " m=(float(N**2)-np.count_nonzero(lattice))/N**2\n", + " return (5.*m-1.)/4" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "N=32\n", + "lattice=np.random.randint(-2,3,(N,N))" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Animaci\u00f3n\n", + "%matplotlib osx\n", + "from matplotlib import animation\n", + "T=0.8\n", + "beta=1./T\n", + "fig=plt.figure(figsize=(10,10))\n", + "tplot=plt.imshow(lattice,cmap='gist_heat',vmin=-2.,vmax=2.,interpolation='None')\n", + "#plt.xlim(xmin+dx,xmax-dx)\n", + "#plt.ylim(ymin+dy,ymax-dy)\n", + "\n", + "def animate(i):\n", + " global lattice\n", + " global T\n", + " for __ in range(100):\n", + " changeone()\n", + " tplot.set_array(lattice)\n", + " return 0\n", + " #plt.title(\"T=\"+str(T)+\" M=\"+str(round(magnetization()/N**2,3)))\n", + "\n", + "animacion = animation.FuncAnimation(fig, animate, np.arange(2,100000),interval=1, blit=False)\n", + "plt.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "plt.figure(figsize=(10,10))\n", + "plt.imshow(lattice,cmap='gist_heat',vmin=-2.,vmax=2.,interpolation='None')\n", + "plt.savefig(\"/Users/juan/Documents/Trabajo/Andes/2015-V/MetodosComputacionales/hands_on/figures/potttiling.png\",dpi=100)\n", + "plt.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 12 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "N=32\n", + "#lattice=np.random.randint(-2,3,(N,N))\n", + "lattice=np.zeros((N,N))\n", + "numIter=1000*N**2\n", + "T=0.1\n", + "magnetizaciones=[]\n", + "for temp in np.linspace(0.6,1.1,15):\n", + " print temp,\n", + " T=temp\n", + " beta=1./T\n", + " mag=[]\n", + " for i in range(numIter):\n", + " changeone()\n", + " if i>numIter-100:\n", + " mag.append(magnetization())\n", + " magnetizaciones.append([T,np.mean(mag)])\n", + "magnetizaciones=np.array(magnetizaciones)" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "plt.figure(figsize=(10,6))\n", + "plt.plot(magnetizaciones[:,0],magnetizaciones[:,1],\"o-\")\n", + "plt.axvline(1./np.log(1.+np.sqrt(5)), color='red')\n", + "plt.axhline(0, color='black')\n", + "plt.xlim(0.6,1.1)\n", + "plt.ylim(-0.1,1)\n", + "plt.title(\"Modelo de Potts q=5\\n Red de 32 X 32. Iteraciones en cada temperatura = 1e6 \")\n", + "plt.xlabel(\"T\")\n", + "plt.ylabel(\"r\")\n", + "plt.text(1.0,0.6,r\"$r=\\frac{5m-1}{4}$\",fontsize=20)\n", + "plt.text(0.995,0.55,u\"Par\u00e1metro de orden.\",fontsize=10)\n", + "#plt.savefig(\"/Users/juan/Documents/Trabajo/Andes/2015-V/MetodosComputacionalesLaboratorio/2015-V/actividades/talleres/Taller10/potts5.png\")\n", + "plt.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file