{ "cells": [ { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "

Replica

\n", "

\n", "A simulation of the Ergodic theorem. The positions of 16 particles are generated randomly\n", "within two halfs of a box, with a histogram created of the number of particles in the \n", "right half. Replicas are made of the original distribution and the frequency of the \n", "same number of particles on the right is determined. See the program \"ParticlesInBox\" in which the histrogram \n", "is found as the system evolves with time." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [ "# Replica.ipynb: A simulation of the Ergodic theorem calling Vpython\n", "\n", "\"\"\"From \"COMPUTATIONAL PHYSICS\" & \"COMPUTER PROBLEMS in PHYSICS\" \n", "by RH Landau, MJ Paez, and CC Bordeianu (deceased). Copyright R Landau, \n", "Oregon State Unv, MJ Paez, Univ Antioquia, C Bordeianu (deceased), \n", "Univ Bucharest, 2020. Please respect copyright & acknowledge our work.\"\"\"\n", "\n", "from vpython import *\n", "import random\n", "import numpy as np\n", "\n", "L = 1 # box size\n", "scene = canvas(width = 400,height = 400,range = (1.3) )\n", "ndist = graph(x = 500, ymax = 50,\n", " width = 400, height = 300, xtitle = 'Nr', ytitle = 'N')\n", "bars = gvbars(delta = 0.8,color = color.red)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [ "Natom = 16; Nr = 0; dt = 1e-6 \n", "dN = [0]*(Natom)\n", "ar = 0.03 # radius of atom \n", "deltaN = 1 # for histogram\n", "Atom = []\n", "t = 1\n", "dnew = [0]*(Natom)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [ "for jj in range (0,200):\n", " Nr = 0\n", " positions = [] # position of atoms\n", " curve(pos = [(-L,-L,0),(L,-L,0),(L,L,0),(-L,L,0),(-L,-L,0)]) #limits figure\n", " curve(pos = [(0,-L,0),(0,L,0)],color = color.yellow)\n", " inside = label(pos = vec(0.4,1.1,0),text = 'Particles here = ',box = 0)\n", " inside2 = label(pos = vec(0.8,1.1,0),box = 0) \n", " for i in range (Natom): # initial positions and velocities\n", " rate(100)\n", " x = 2.*(L-ar)*random.random()-L+ar # positons \n", " y = 2.*(L-ar)*random.random()-L+ar # border forbidden\n", " Atom = Atom+[sphere(pos = vec(x,y,0),radius = ar,color = color.green) ] \n", " positions.append((x,y,0)) # add positions to list\n", " pos = np.array(positions) # array with positions\n", " ddp = pos[i]\n", " if ddp[0]> = 0 and ddp[0]< = L: Nr += 1 # count number right\n", " inside2.text = '%4s'%Nr\n", " dN[Nr] += 1 # for histogram\n", " for j in arange(0,16): bars.plot(pos = (j,dN[j]))\n", " for obj in scene.objects: # start new walk\n", " if (obj is curve or obj is sphere or obj is label): continue\n", " else: obj.visible = 0 # clear curve to make new one\n", " inside2.text = '%4s'%Nr" ] } ], "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.13" } }, "nbformat": 4, "nbformat_minor": 2 }