To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

Commit 6e2338b0 authored by Valerio's avatar Valerio
Browse files

solutions ex7

parent e655603f
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from timeit import default_timer as timer\n",
"import random\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Running this notebook locally on your laptop may take long time. Order of 1h."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"#perform a Wolff cluster update step on sys of size H*L, with anisotropic couplings\n",
"#absolute value of return is the cluster size, twice the return is the change in magnetisation\n",
"def Wolff_step_aniso(sys,H,L,probability_horizontal, probability_vertical):\n",
" flipped = 1\n",
" #choose random spin\n",
" spin_v = random.randint(0,H-1)\n",
" spin_h = random.randint(0,L-1)\n",
" #flip the spin\n",
" sys[spin_v][spin_h] *= -1\n",
" #keep track of what we do to the magnetisation\n",
" flip_sign = sys[spin_v][spin_h]\n",
" #check neighbours recursively\n",
" spins_to_check = [[spin_v,spin_h]]\n",
" while(len(spins_to_check) > 0):\n",
" indices = spins_to_check.pop()\n",
" spin_v = indices[0]\n",
" spin_h = indices[1]\n",
" current_spin = sys[spin_v][spin_h]\n",
" #add neighbours to cluster and schedule them with correct probability\n",
" if(current_spin * sys[(spin_v+1)%H][spin_h] == -1 and random.random() < probability_vertical):\n",
" spins_to_check.append([(spin_v+1)%H,spin_h])\n",
" sys[(spin_v+1)%H][spin_h] *= -1\n",
" flipped += 1\n",
"\n",
" if(current_spin * sys[(spin_v-1)%H][spin_h] == -1 and random.random() < probability_vertical):\n",
" spins_to_check.append([(spin_v-1)%H,spin_h])\n",
" sys[(spin_v-1)%H][spin_h] *= -1\n",
" flipped += 1\n",
"\n",
" if(current_spin * sys[spin_v][(spin_h+1)%L] == -1 and random.random() < probability_horizontal):\n",
" spins_to_check.append([spin_v,(spin_h+1)%L])\n",
" sys[spin_v][(spin_h+1)%L] *= -1\n",
" flipped += 1\n",
"\n",
" if(current_spin * sys[spin_v][(spin_h-1)%L] == -1 and random.random() < probability_horizontal):\n",
" spins_to_check.append([spin_v,(spin_h-1)%L])\n",
" sys[spin_v][(spin_h-1)%L] *= -1\n",
" flipped += 1\n",
" return flipped*flip_sign"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"#returns the magnetisation of sys\n",
"def magnetisation(sys):\n",
" magn = 0\n",
" L = len(sys[0])\n",
" H = len(sys)\n",
" for a in sys:\n",
" for s in a:\n",
" magn += s\n",
" return float(magn)/float(H*L)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"class data_cl:\n",
" def __init__(self,L):\n",
" self.magnetisations = []\n",
" self.magnetisationsq = []\n",
" self.binders2 = []\n",
" self.binders4 = []\n",
" self.chis = []\n",
" self.L = L"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#run a simulation using cluster updates with anisotropy\n",
"def run_aniso_cluster_simulation(Jmin=0.5,Jmax=2.5,meas=8192*2,equil=1000*2):\n",
"\n",
" #######################\n",
" ###\tInitialisation\n",
" #######################\n",
"\n",
" Js = np.linspace(Jmin,Jmax,30)\n",
" delta = 0.05\n",
" gamma = 1.\n",
"\n",
" datas = {8: data_cl(8), 12: data_cl(12), 16: data_cl(16), 20: data_cl(20), 24: data_cl(24), 28: data_cl(28)}\n",
"\n",
" \n",
" #define system parameters\n",
" for i in reversed([8,12,16,20,24,28]):\n",
" d = datas[i]\n",
" L = d.L\n",
" H = d.L\n",
" sys = []\n",
" for i in range(H):\n",
" a = []\n",
" for j in range(L):\n",
" a.append(1)\n",
" sys.append(a)\n",
" #initialise the magnetisation\n",
" total_magnetisation = L*H\n",
"\n",
" #######################\n",
" ###\tSimulation\n",
" #######################\n",
" #simulate for the specified temperatures\n",
" for J in Js:\n",
" print(\"{} {}\".format(L,J))\n",
" #arrays to store all samples\n",
" #magdata = [] #magnetisation\n",
" #magqdata = [] #magnetisation squared\n",
" #magqqdata = [] #magnetisation **4\n",
" mag = 0.\n",
" magq = 0.\n",
" magqq = 0.\n",
" prob_h = 1. - np.exp(-2.*delta*J)\n",
" prob_v = 1. - delta*gamma\n",
"\n",
" e_s = equil\n",
" m_s = meas\n",
" if(J < 1.5):\n",
" e_s = int(8*equil)\n",
" m_s = int(8*meas)\n",
" else:\n",
" e_s = int(equil*2.)\n",
" m_s = int(meas*2.)\n",
"\n",
" #equilibrate\n",
" for i in range(e_s):\n",
" total_magnetisation += 2*Wolff_step_aniso(sys,H,L,prob_h,prob_v)\n",
" #measure\n",
" for i in range(m_s):\n",
" wolff_return = Wolff_step_aniso(sys,H,L,prob_h,prob_v)\n",
" m = np.abs(wolff_return)/float(L*H)\n",
" total_magnetisation += 2*wolff_return\n",
" #magdata.append(m)\n",
" #magdata.append(np.abs(total_magnetisation)/float(L*H))\n",
" #magqdata.append((total_magnetisation/float(L*H))**2)\n",
" #magqqdata.append((total_magnetisation/float(L*H))**4)\n",
" mag += m\n",
" magq += (total_magnetisation/float(L*H))**2\n",
" magqq += (total_magnetisation/float(L*H))**4\n",
"\n",
" #extract means and their standard deviations\n",
" #d.magnetisations.append(np.mean(magdata))\n",
" #d.magnetisationsq.append(np.mean(magqdata))\n",
" #d.binders2.append(np.mean(magqdata)/np.mean(magdata)**2)\n",
" #d.binders4.append(np.mean(magqqdata)/np.mean(magqdata)**2)\n",
" #d.chis.append(np.mean(magqdata)-np.mean(magdata)**2)\n",
" magex = float(mag)/float(m_s)\n",
" magqex = float(magq)/float(m_s)\n",
" magqqex = float(magqq)/float(m_s)\n",
" d.magnetisations.append(magex)\n",
" d.magnetisationsq.append(magqqex)\n",
" d.binders2.append(magqex/magex**2)\n",
" d.binders4.append(magqqex/magqex**2)\n",
" d.chis.append(magqex-magex**2)\n",
" \n",
"\n",
"\n",
"\n",
"\n",
" #######################\n",
" ###\tPlotting\n",
" #######################\n",
"\n",
" #plot magnetisation\n",
" leg = []\n",
" for i in [8,12,16,20,24,28]:\n",
" plt.plot(Js,datas[i].magnetisations,'o-',ms=1)\n",
" leg.append(\"L=\" + str(i))\n",
" plt.legend(leg)\n",
" plt.title(\"Magnetisation\")\n",
" plt.xlabel(\"J/$\\Gamma$\")\n",
" plt.ylabel(\"Magnetisation\")\n",
" #plt.show()\n",
" plt.savefig(\"M.pdf\")\n",
" plt.clf()\n",
"\n",
" #plot second order Binder\n",
" leg = []\n",
" for i in [8,12,16,20,24,28]:\n",
" plt.plot(Js,datas[i].binders2,'o-',ms=1)\n",
" leg.append(\"L=\" + str(i))\n",
" plt.legend(leg)\n",
" plt.title(\"Second order Binder cumulant\")\n",
" plt.xlabel(\"J/$\\Gamma$\")\n",
" plt.ylabel(\"$U_2$\")\n",
" plt.savefig(\"U2.pdf\")\n",
" plt.clf()\n",
"\n",
" #plot fourth order Binder\n",
" leg = []\n",
" for i in [8,12,16,20,24,28]:\n",
" plt.plot(Js,datas[i].binders4,'o-',ms=1)\n",
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment