06: Fetch week 6
This commit is contained in:
693
Exercise sheet 6/exercise_sheet_06.ipynb
Normal file
693
Exercise sheet 6/exercise_sheet_06.ipynb
Normal file
@ -0,0 +1,693 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "968e63dc",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Exercise sheet\n",
|
||||
"\n",
|
||||
"Some general remarks about the exercises:\n",
|
||||
"* For your convenience functions from the lecture are included below. Feel free to reuse them without copying to the exercise solution box.\n",
|
||||
"* For each part of the exercise a solution box has been added, but you may insert additional boxes. Do not hesitate to add Markdown boxes for textual or LaTeX answers (via `Cell > Cell Type > Markdown`). But make sure to replace any part that says `YOUR CODE HERE` or `YOUR ANSWER HERE` and remove the `raise NotImplementedError()`.\n",
|
||||
"* Please make your code readable by humans (and not just by the Python interpreter): choose informative function and variable names and use consistent formatting. Feel free to check the [PEP 8 Style Guide for Python](https://www.python.org/dev/peps/pep-0008/) for the widely adopted coding conventions or [this guide for explanation](https://realpython.com/python-pep8/).\n",
|
||||
"* Make sure that the full notebook runs without errors before submitting your work. This you can do by selecting `Kernel > Restart & Run All` in the jupyter menu.\n",
|
||||
"* For some exercises test cases have been provided in a separate cell in the form of `assert` statements. When run, a successful test will give no output, whereas a failed test will display an error message.\n",
|
||||
"* Each sheet has 100 points worth of exercises. Note that only the grades of sheets number 2, 4, 6, 8 count towards the course examination. Submitting sheets 1, 3, 5, 7 & 9 is voluntary and their grades are just for feedback.\n",
|
||||
"\n",
|
||||
"Please fill in your name here:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "b388fbcf",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"NAME = \"\"\n",
|
||||
"NAMES_OF_COLLABORATORS = \"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "8a01a93a",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"---"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "41d26cde",
|
||||
"metadata": {
|
||||
"deletable": false,
|
||||
"editable": false,
|
||||
"nbgrader": {
|
||||
"cell_type": "markdown",
|
||||
"checksum": "3239a87542ad0d212698947a7b60cbb3",
|
||||
"grade": false,
|
||||
"grade_id": "cell-34662207849ac9d6",
|
||||
"locked": true,
|
||||
"schema_version": 3,
|
||||
"solution": false,
|
||||
"task": false
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"**Exercise sheet 6**\n",
|
||||
"\n",
|
||||
"Code from the lecture"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "cb41d2a1",
|
||||
"metadata": {
|
||||
"deletable": false,
|
||||
"editable": false,
|
||||
"nbgrader": {
|
||||
"cell_type": "code",
|
||||
"checksum": "34c958a728224ac04d230ccc781dba94",
|
||||
"grade": false,
|
||||
"grade_id": "cell-be52f3b5932e9c30",
|
||||
"locked": true,
|
||||
"schema_version": 3,
|
||||
"solution": false,
|
||||
"task": false
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pylab as plt\n",
|
||||
"\n",
|
||||
"rng = np.random.default_rng()\n",
|
||||
"%matplotlib inline\n",
|
||||
"\n",
|
||||
"def aligned_init_config(width):\n",
|
||||
" '''Produce an all +1 configuration.'''\n",
|
||||
" return np.ones((width,width),dtype=int)\n",
|
||||
"\n",
|
||||
"def plot_ising(config,ax,title):\n",
|
||||
" '''Plot the configuration.'''\n",
|
||||
" ax.matshow(config, vmin=-1, vmax=1, cmap=plt.cm.binary)\n",
|
||||
" ax.title.set_text(title)\n",
|
||||
" ax.set_yticklabels([])\n",
|
||||
" ax.set_xticklabels([])\n",
|
||||
" ax.set_yticks([])\n",
|
||||
" ax.set_xticks([])\n",
|
||||
"\n",
|
||||
"from collections import deque\n",
|
||||
"\n",
|
||||
"def neighboring_sites(s,w):\n",
|
||||
" '''Return the coordinates of the 4 sites adjacent to s on an w*w lattice.'''\n",
|
||||
" return [((s[0]+1)%w,s[1]),((s[0]-1)%w,s[1]),(s[0],(s[1]+1)%w),(s[0],(s[1]-1)%w)]\n",
|
||||
"\n",
|
||||
"def cluster_flip(state,seed,p_add):\n",
|
||||
" '''Perform a single Wolff cluster move with specified seed on the state with parameter p_add.'''\n",
|
||||
" w = len(state)\n",
|
||||
" spin = state[seed]\n",
|
||||
" state[seed] = -spin \n",
|
||||
" cluster_size = 1\n",
|
||||
" unvisited = deque([seed]) # use a deque to efficiently track the unvisited cluster sites\n",
|
||||
" while unvisited: # while unvisited sites remain\n",
|
||||
" site = unvisited.pop() # take one and remove from the unvisited list\n",
|
||||
" for nbr in neighboring_sites(site,w):\n",
|
||||
" if state[nbr] == spin and rng.uniform() < p_add:\n",
|
||||
" state[nbr] = -spin\n",
|
||||
" unvisited.appendleft(nbr)\n",
|
||||
" cluster_size += 1\n",
|
||||
" return cluster_size\n",
|
||||
"\n",
|
||||
"def wolff_cluster_move(state,p_add):\n",
|
||||
" '''Perform a single Wolff cluster move on the state with addition probability p_add.'''\n",
|
||||
" seed = tuple(rng.integers(0,len(state),2))\n",
|
||||
" return cluster_flip(state,seed,p_add)\n",
|
||||
"\n",
|
||||
"def compute_magnetization(config):\n",
|
||||
" '''Compute the magnetization M(s) of the state config.'''\n",
|
||||
" return np.sum(config)\n",
|
||||
"\n",
|
||||
"def run_ising_wolff_mcmc(state,p_add,n):\n",
|
||||
" '''Run n Wolff moves on state and return total number of spins flipped.'''\n",
|
||||
" total = 0\n",
|
||||
" for _ in range(n):\n",
|
||||
" total += wolff_cluster_move(state,p_add)\n",
|
||||
" return total\n",
|
||||
"\n",
|
||||
"def sample_autocovariance(x,tmax):\n",
|
||||
" '''Compute the autocorrelation of the time series x for t = 0,1,...,tmax-1.'''\n",
|
||||
" x_shifted = x - np.mean(x)\n",
|
||||
" return np.array([np.dot(x_shifted[:len(x)-t],x_shifted[t:])/len(x) \n",
|
||||
" for t in range(tmax)])\n",
|
||||
"\n",
|
||||
"def find_correlation_time(autocov):\n",
|
||||
" '''Return the index of the first entry that is smaller than \n",
|
||||
" autocov[0]/e or the length of autocov if none are smaller.'''\n",
|
||||
" smaller = np.where(autocov < np.exp(-1)*autocov[0])[0]\n",
|
||||
" return smaller[0] if len(smaller) > 0 else len(autocov)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3317e002",
|
||||
"metadata": {
|
||||
"deletable": false,
|
||||
"editable": false,
|
||||
"nbgrader": {
|
||||
"cell_type": "markdown",
|
||||
"checksum": "5fc7aaa40ff9892f4cefca6bedcc5fd1",
|
||||
"grade": false,
|
||||
"grade_id": "cell-03072464d602c105",
|
||||
"locked": true,
|
||||
"schema_version": 3,
|
||||
"solution": false,
|
||||
"task": false
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"## MCMC simulation of the XY model\n",
|
||||
"\n",
|
||||
"**(100 Points)**\n",
|
||||
"\n",
|
||||
"_Goal of this exercise_: Practice implementing MCMC simulation of the XY spin model using an appropriate cluster algorithm and analyzing the numerical effectiveness.\n",
|
||||
"\n",
|
||||
"The **XY model** is a relative of the Ising model in which the discrete $\\pm 1$ spin at each lattice site is replaced by a continuous $2$-dimensional spin on the unit circle \n",
|
||||
"\n",
|
||||
"$$\n",
|
||||
"S_1 = \\{(x,y)\\in\\mathbb{R}^2: x^2+y^2=1\\}.\n",
|
||||
"$$\n",
|
||||
"\n",
|
||||
"To be precise, we consider a $w\\times w$ lattice with periodic boundary conditions and a XY configuration $s = (s_1,\\ldots,s_N) \\in \\Gamma = S_1^N$, $N=w^2$, with Hamiltonian that is very similar to the Ising model,\n",
|
||||
"\n",
|
||||
"$$\n",
|
||||
"H_{XY}(s) = - J \\sum_{i\\sim j} s_i \\cdot s_j.\n",
|
||||
"$$\n",
|
||||
"\n",
|
||||
"Here, as in the Ising model, the sum runs over nearest neighbor pairs $i$ and $j$ and $s_i \\cdot s_j$ is the usual Euclidean inner product of the vectors $s_i,s_j \\in S_1$. We will only consider the ferromagnetic XY model and set $J=1$ in the remainder of the exercise. Note that nowhere in the definition the $x$- and $y$-components of the spins are related to the two directions of the lattice (one could also have studied the XY model on a one-dimensional or three-dimensional lattice and the spins would still have two components). As usual we are interested in sampling configurations $s\\in \\Gamma$ with distribution $\\pi(s)$ given by the Boltzmann distribution\n",
|
||||
"\n",
|
||||
"$$ \n",
|
||||
"\\pi(s) = \\frac{1}{Z_{XY}} e^{-\\beta H_{XY}(s)}, \\qquad \\beta = 1/T.\n",
|
||||
"$$\n",
|
||||
"\n",
|
||||
"The XY model admits a (local) **cluster algorithm** that is very similar to the Wolff algorithm of the Ising model. It amounts to the following recipe:\n",
|
||||
"1. Sample a uniform seed site $i_{\\text{seed}}$ in $1,\\ldots,N$ and an independent uniform unit vector $\\hat{n} \\in S_1$.\n",
|
||||
"2. Grow a cluster $C$ starting from the seed $i_{\\text{seed}}$ consisting only of sites $j$ whose spin $s_j$ is \"aligned\" with the seed, in the sense that $s_j\\cdot\\hat{n}$ has the same sign as $s_{i_{\\text{seed}}}\\cdot \\hat{n}$, or $(s_j\\cdot\\hat{n})(s_{i_{\\text{seed}}}\\cdot \\hat{n})>0$. Like in the Ising model this is done iteratively by examining the neighbors of sites that are already in the cluster, and adding those that are aligned with appropriate probability. The difference with the Ising model is that this probability depends on the spins $s_i$ and $s_j$ that are linked (meaning that $s_j$ is an aligned neighbor of $s_i$) via the formula\n",
|
||||
"$$ p_{\\text{add}}(s_i,s_j) = 1 - \\exp\\big( -2\\beta\\,(s_i\\cdot\\hat{n})(s_j\\cdot\\hat{n})\\big).$$\n",
|
||||
"3. Once the cluster $C$ is constructed, all of its spins are \"flipped\" in the sense that they are reflected in the plane perpendicular to $\\hat{n}$, i.e. $s_j \\to s_j - 2(s_j\\cdot\\hat{n})\\hat{n}$.\n",
|
||||
"\n",
|
||||
"__(a)__ Verify by a calculation, to be included using markdown and LaTeX below, that the probabilities $p_{\\text{add}}(s_i,s_j)$ are the appropriate ones to ensure detailed balance for the Boltzmann distribution $\\pi(s)$. _Hint_: Follow the same reasoning [as for the Ising model](https://hef.ru.nl/~tbudd/mct/lectures/cluster_algorithms.html#cluster-algorithm-for-the-ising-model-the-wolff-algorithm). Compare the probabilities involved in producing the cluster $C$ in state $s$ and state $s'$. Why do the probabilities only differ at the boundary edges in the cluster $C$? **(25 pts)**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "c1561680",
|
||||
"metadata": {
|
||||
"deletable": false,
|
||||
"nbgrader": {
|
||||
"cell_type": "markdown",
|
||||
"checksum": "1be75e4b28f44fd0e2a702c3f586003d",
|
||||
"grade": true,
|
||||
"grade_id": "cell-55323d84e19389a2",
|
||||
"locked": false,
|
||||
"points": 25,
|
||||
"schema_version": 3,
|
||||
"solution": true,
|
||||
"task": false
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"YOUR ANSWER HERE"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "66a9278e",
|
||||
"metadata": {
|
||||
"deletable": false,
|
||||
"editable": false,
|
||||
"nbgrader": {
|
||||
"cell_type": "markdown",
|
||||
"checksum": "696630fba38d4eedeebe2daa397e02e1",
|
||||
"grade": false,
|
||||
"grade_id": "cell-bf935a4be9ad6e06",
|
||||
"locked": true,
|
||||
"schema_version": 3,
|
||||
"solution": false,
|
||||
"task": false
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"__(b)__ In order to implement the cluster update described above, we take the state to be described by a Numpy array of dimension $(w,w,2)$, for which we have already provided a function `xy_aligned_init_config` to generate an all-aligned initial state. Write the function `xy_cluster_flip`, that grows and flips a cluster starting from the given seed site and $\\hat{n}$ and returns the cluster size, and `xy_cluster_move`, that performs the previous function to a random seed and direction $\\hat{n}$ and also returns the cluster size. **(20 pts)**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "6e4a6d63",
|
||||
"metadata": {
|
||||
"deletable": false,
|
||||
"nbgrader": {
|
||||
"cell_type": "code",
|
||||
"checksum": "0732e9388e50a79370df1d621764a8a8",
|
||||
"grade": false,
|
||||
"grade_id": "cell-4d68efbe5b0730b1",
|
||||
"locked": false,
|
||||
"schema_version": 3,
|
||||
"solution": true,
|
||||
"task": false
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def xy_aligned_init_config(width):\n",
|
||||
" '''Return an array of dimension (width,width,2) representing aligned spins in x-direction.'''\n",
|
||||
" return np.dstack((np.ones((width,width)),np.zeros((width,width))))\n",
|
||||
"\n",
|
||||
"def xy_cluster_flip(state,seed,nhat,beta):\n",
|
||||
" '''Perform a cluster move with specified seed and vector nhat on the state at temperature beta.'''\n",
|
||||
" w = len(state)\n",
|
||||
" # YOUR CODE HERE\n",
|
||||
" raise NotImplementedError()\n",
|
||||
" return cluster_size\n",
|
||||
"\n",
|
||||
"def xy_cluster_move(state,beta):\n",
|
||||
" '''Perform a single Wolff cluster move on the state with addition probability p_add.'''\n",
|
||||
" # YOUR CODE HERE\n",
|
||||
" raise NotImplementedError()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "3f810041",
|
||||
"metadata": {
|
||||
"deletable": false,
|
||||
"editable": false,
|
||||
"nbgrader": {
|
||||
"cell_type": "code",
|
||||
"checksum": "8abc44b0a19bcdb3c66ac52abd25b747",
|
||||
"grade": true,
|
||||
"grade_id": "cell-2e1ebc198d10ea5b",
|
||||
"locked": true,
|
||||
"points": 15,
|
||||
"schema_version": 3,
|
||||
"solution": false,
|
||||
"task": false
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from nose.tools import assert_almost_equal\n",
|
||||
"assert 1 <= xy_cluster_flip(xy_aligned_init_config(4),(0,0),np.array([np.cos(0.5),np.sin(0.5)]),0.5) <= 16\n",
|
||||
"assert_almost_equal(np.mean([xy_cluster_flip(xy_aligned_init_config(3),(0,0),\n",
|
||||
" np.array([np.cos(0.5),np.sin(0.5)]),0.3) \n",
|
||||
" for _ in range(200)]),5.3,delta=0.7)\n",
|
||||
"assert_almost_equal(np.mean([xy_cluster_flip(xy_aligned_init_config(3),(1,2),\n",
|
||||
" np.array([np.cos(0.2),np.sin(0.2)]),0.2) \n",
|
||||
" for _ in range(200)]),4.3,delta=0.6)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "bde560e4",
|
||||
"metadata": {
|
||||
"deletable": false,
|
||||
"editable": false,
|
||||
"nbgrader": {
|
||||
"cell_type": "code",
|
||||
"checksum": "6524b7571176499732d477ffb6b7e40d",
|
||||
"grade": true,
|
||||
"grade_id": "cell-9445d8ff6dc2a16f",
|
||||
"locked": true,
|
||||
"points": 5,
|
||||
"schema_version": 3,
|
||||
"solution": false,
|
||||
"task": false
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"assert 1 <= xy_cluster_move(xy_aligned_init_config(4),0.5) <= 16\n",
|
||||
"assert_almost_equal(np.mean([xy_cluster_move(xy_aligned_init_config(3),0.3) \n",
|
||||
" for _ in range(200)]),3.6,delta=0.75)\n",
|
||||
"assert_almost_equal(np.mean([xy_cluster_move(xy_aligned_init_config(3),0.9) \n",
|
||||
" for _ in range(200)]),6.3,delta=0.75)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "daa0b50e",
|
||||
"metadata": {
|
||||
"deletable": false,
|
||||
"editable": false,
|
||||
"nbgrader": {
|
||||
"cell_type": "markdown",
|
||||
"checksum": "4289e943aa451475a6d9402abbe6bcb1",
|
||||
"grade": false,
|
||||
"grade_id": "cell-aaed2919abf002dc",
|
||||
"locked": true,
|
||||
"schema_version": 3,
|
||||
"solution": false,
|
||||
"task": false
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"__(c)__ Estimate and plot the average cluster size in equilibrium for a 25x25 lattice ($w=25$) for the range of temperatures $T = 0.5,0.6,\\ldots,1.5$. It is not necessary first to estimate the equilibration time: you may start in a fully aligned state and use 400 moves for equilibration and 1000 for estimating the average cluster size. It is not necessary to estimate errors for this average. Store your averages in the data set `\"cluster-size\"` (an array of size 11) in the HDF5-file `xy_data.hdf5`, just like you did in Exercise sheet 5. Then read the data from file and produce a plot. **(20 pts)**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "1f91dd67",
|
||||
"metadata": {
|
||||
"deletable": false,
|
||||
"nbgrader": {
|
||||
"cell_type": "code",
|
||||
"checksum": "4d6b29807b163b30a89dbe9668117083",
|
||||
"grade": false,
|
||||
"grade_id": "cell-6e4765df49afeb66",
|
||||
"locked": false,
|
||||
"schema_version": 3,
|
||||
"solution": true,
|
||||
"task": false
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"temperatures = np.linspace(0.5,1.5,11)\n",
|
||||
"width = 25\n",
|
||||
"equilibration_moves = 400\n",
|
||||
"measurement_moves = 1000\n",
|
||||
"\n",
|
||||
"# YOUR CODE HERE\n",
|
||||
"raise NotImplementedError()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f5ea3143",
|
||||
"metadata": {
|
||||
"deletable": false,
|
||||
"editable": false,
|
||||
"nbgrader": {
|
||||
"cell_type": "code",
|
||||
"checksum": "77f0ca1931f96503b4141e3789bb1d68",
|
||||
"grade": true,
|
||||
"grade_id": "cell-69f351f86b801f63",
|
||||
"locked": true,
|
||||
"points": 10,
|
||||
"schema_version": 3,
|
||||
"solution": false,
|
||||
"task": false
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with h5py.File('xy_data.hdf5','r') as f:\n",
|
||||
" assert f[\"cluster-size\"][()].shape == (11,)\n",
|
||||
" assert_almost_equal(f[\"cluster-size\"][4],225,delta=40)\n",
|
||||
" assert_almost_equal(f[\"cluster-size\"][10],8,delta=8)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e84152c5",
|
||||
"metadata": {
|
||||
"deletable": false,
|
||||
"nbgrader": {
|
||||
"cell_type": "code",
|
||||
"checksum": "dda3e8db7a6a4b7a7d6c58a3a03e487d",
|
||||
"grade": true,
|
||||
"grade_id": "cell-88fb1bb0cd597195",
|
||||
"locked": false,
|
||||
"points": 10,
|
||||
"schema_version": 3,
|
||||
"solution": true,
|
||||
"task": false
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Plotting\n",
|
||||
"# YOUR CODE HERE\n",
|
||||
"raise NotImplementedError()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "bd5fa10f",
|
||||
"metadata": {
|
||||
"deletable": false,
|
||||
"editable": false,
|
||||
"nbgrader": {
|
||||
"cell_type": "markdown",
|
||||
"checksum": "bcf91e9b587d185ef00c6b25028e18e5",
|
||||
"grade": false,
|
||||
"grade_id": "cell-101fca705096fe8e",
|
||||
"locked": true,
|
||||
"schema_version": 3,
|
||||
"solution": false,
|
||||
"task": false
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"__(d)__ Make an MCMC estimate (and plot!) of the **mean square magnetization per spin** $\\mathbb{E}[ m^2(s) ]$ for the same set of temperatures, where \n",
|
||||
"\n",
|
||||
"$$\n",
|
||||
"m^2(s) = \\left(\\frac{1}{N}\\sum_{i=1}^N s_i\\right)\\cdot\\left(\\frac{1}{N}\\sum_{i=1}^N s_i\\right).\n",
|
||||
"$$\n",
|
||||
"\n",
|
||||
"To choose the equilibration time and time between measurement, use the average cluster size from (c) to estimate how many moves correspond to 1 _sweep_, i.e. roughly $N = w^2$ updates to spins. Then use 100 equilibration _sweeps_ and 200 measurements of $m^2(s)$, with 2 _sweeps_ between each measurement. Store the measured values of $m^2(s)$ in the data set `\"square-magn\"` of dimension $(11,200)$ in `xy_data.hdf5`.\n",
|
||||
"Then read the data and plot estimates for $\\mathbb{E}[ m^2(s) ]$ including errors (based on batching or jackknife). If the errors are too small to see, you may multiply them by some number and indicate this in the title of the plot. **(20 pts)**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "71323e81",
|
||||
"metadata": {
|
||||
"deletable": false,
|
||||
"nbgrader": {
|
||||
"cell_type": "code",
|
||||
"checksum": "f347e7079633e91d931730dfbcc231d4",
|
||||
"grade": false,
|
||||
"grade_id": "cell-7421b7a140318565",
|
||||
"locked": false,
|
||||
"schema_version": 3,
|
||||
"solution": true,
|
||||
"task": false
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"measurements = 200\n",
|
||||
"equil_sweeps = 100\n",
|
||||
"measure_sweeps = 2\n",
|
||||
"\n",
|
||||
"# YOUR CODE HERE\n",
|
||||
"raise NotImplementedError()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "35782ff3",
|
||||
"metadata": {
|
||||
"deletable": false,
|
||||
"editable": false,
|
||||
"nbgrader": {
|
||||
"cell_type": "code",
|
||||
"checksum": "e565b97bdc2fe455715db76f3e958a84",
|
||||
"grade": true,
|
||||
"grade_id": "cell-5d3a2f49615613b2",
|
||||
"locked": true,
|
||||
"points": 10,
|
||||
"schema_version": 3,
|
||||
"solution": false,
|
||||
"task": false
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with h5py.File('xy_data.hdf5','r') as f:\n",
|
||||
" assert f[\"square-magn\"][()].shape == (11, 200)\n",
|
||||
" assert_almost_equal(np.mean(f[\"square-magn\"][4]),0.456,delta=0.02)\n",
|
||||
" assert_almost_equal(np.mean(f[\"square-magn\"][9]),0.023,delta=0.01)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "1015f826",
|
||||
"metadata": {
|
||||
"deletable": false,
|
||||
"nbgrader": {
|
||||
"cell_type": "code",
|
||||
"checksum": "4e39eed6235d63377caca1b6ff0f28da",
|
||||
"grade": true,
|
||||
"grade_id": "cell-1f68f5bac39efe6c",
|
||||
"locked": false,
|
||||
"points": 10,
|
||||
"schema_version": 3,
|
||||
"solution": true,
|
||||
"task": false
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Plotting\n",
|
||||
"# YOUR CODE HERE\n",
|
||||
"raise NotImplementedError()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "326cc9c7",
|
||||
"metadata": {
|
||||
"deletable": false,
|
||||
"editable": false,
|
||||
"nbgrader": {
|
||||
"cell_type": "markdown",
|
||||
"checksum": "9f5b8ab1f6e5969e99680e87dd4ae2d4",
|
||||
"grade": false,
|
||||
"grade_id": "cell-9fc8c563421f36e4",
|
||||
"locked": true,
|
||||
"schema_version": 3,
|
||||
"solution": false,
|
||||
"task": false
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"__(e)__ Produce a single equilibrated state for each temperature and store them in the data set `\"states\"` of dimension $(11,25,25,2)$ in `xy_data.hdf5`. Then read them and produce a table of plots using the provided function `plot_xy`, which shows colors based on the angle of the spin, each with a title to indicate the temperature. Can you observe the [**Kosterlitz–Thouless transition** of the XY model](https://en.wikipedia.org/wiki/Classical_XY_model)? **(15 pts)**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "3d5a419b",
|
||||
"metadata": {
|
||||
"deletable": false,
|
||||
"nbgrader": {
|
||||
"cell_type": "code",
|
||||
"checksum": "fa2024da2b5516d1dc942aeabe1a6c1d",
|
||||
"grade": false,
|
||||
"grade_id": "cell-ef87967a80cd8dbe",
|
||||
"locked": false,
|
||||
"schema_version": 3,
|
||||
"solution": true,
|
||||
"task": false
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"width = 25\n",
|
||||
"state = xy_aligned_init_config(width)\n",
|
||||
"equil_sweeps = 200\n",
|
||||
"\n",
|
||||
"# YOUR CODE HERE\n",
|
||||
"raise NotImplementedError()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "2f322a5f",
|
||||
"metadata": {
|
||||
"deletable": false,
|
||||
"editable": false,
|
||||
"nbgrader": {
|
||||
"cell_type": "code",
|
||||
"checksum": "42eb725fdf7ff4b35fbc1be664deb579",
|
||||
"grade": true,
|
||||
"grade_id": "cell-8968d7e317e522f7",
|
||||
"locked": true,
|
||||
"points": 5,
|
||||
"schema_version": 3,
|
||||
"solution": false,
|
||||
"task": false
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with h5py.File('xy_data.hdf5','r') as f:\n",
|
||||
" assert f[\"states\"][()].shape == (11, 25, 25, 2)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "69d891e8",
|
||||
"metadata": {
|
||||
"deletable": false,
|
||||
"nbgrader": {
|
||||
"cell_type": "code",
|
||||
"checksum": "e9db0fa47469fbe95861db34bd66f432",
|
||||
"grade": true,
|
||||
"grade_id": "cell-cfcff21eb3fb562f",
|
||||
"locked": false,
|
||||
"points": 10,
|
||||
"schema_version": 3,
|
||||
"solution": true,
|
||||
"task": false
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def plot_xy(state,ax,title=\"\"):\n",
|
||||
" '''Plot the XY configuration given by state. Takes an Axes object ax from \n",
|
||||
" matplotlib to draw to, and adds the specified title.'''\n",
|
||||
" angles = np.arctan2(*np.transpose(state,axes=(2,0,1)))\n",
|
||||
" ax.matshow(angles, vmin=-np.pi, vmax=np.pi, cmap=plt.cm.hsv)\n",
|
||||
" ax.title.set_text(title)\n",
|
||||
" ax.set_yticklabels([])\n",
|
||||
" ax.set_xticklabels([])\n",
|
||||
" ax.set_yticks([])\n",
|
||||
" ax.set_xticks([])\n",
|
||||
"\n",
|
||||
"# Make a table of plots\n",
|
||||
"# YOUR CODE HERE\n",
|
||||
"raise NotImplementedError()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "4b31ffe5",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"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.9.12"
|
||||
},
|
||||
"vscode": {
|
||||
"interpreter": {
|
||||
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
|
||||
}
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
Reference in New Issue
Block a user