{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Heat Bath Algorithm" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import random, math" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "L = 6\n", "N = L * L\n", "nbr = {i : ((i // L) * L + (i + 1) % L, (i + L) % N,\n", " (i // L) * L + (i - 1) % L, (i - L) % N) for i in range(N)}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "nsteps = 1000\n", "beta = 1.0\n", "S = [random.choice([-1, 1]) for site in range(N)]\n", "\n", "E = -0.5 * sum(S[k] * sum(S[nn] for nn in nbr[k]) \\\n", " for k in range(N))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "E_tot, E2_tot = 0.0, 0.0\n", "random.seed('123456')\n", "for step in range(nsteps):\n", " k = random.randint(0, N - 1)\n", " Upsilon = random.uniform(0.0, 1.0)\n", " h = sum(S[nn] for nn in nbr[k])\n", " Sk_old = S[k]\n", " S[k] = -1\n", " if Upsilon < 1.0 / (1.0 + math.exp(-2.0 * beta * h)):\n", " S[k] = 1\n", " if S[k] != Sk_old:\n", " E -= 2.0 * h * S[k]\n", " E_tot += E\n", " E2_tot += E ** 2\n", "E_av = E_tot / float(nsteps)\n", "E2_av = E2_tot / float(nsteps)\n", "c_V = beta ** 2 * (E2_av - E_av ** 2) / float(N)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(-55.988, 7.750662666666661)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "E_av,c_V" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }