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

Ergodic Theorem Replication

\n", "

\n", "A demonstration of the Ergodic theorem. The program continuously generates 16 random particles\n", "in a box and counts the number in the right and left halfs. The distributions of numbers are plotted. \n", "See too the program \"ParticlesinBox\" for time evolution of the histogram." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [ "\"\"\" From \"COMPUTATIONAL PHYSICS\" & \"COMPUTER PROBLEMS in PHYSICS\"\n", " by RH Landau, MJ Paez, and CC Bordeianu (deceased).\n", " Copyright R Landau, Oregon State Unv, MJ Paez, Univ Antioquia, \n", " C Bordeianu, Univ Bucharest, 2021. \n", " Please respect copyright & acknowledge our work.\"\"\"\n", "\n", "# ErgodicTheorem.ipynb: Demostration of the ergodic theorem\n", "\n", "from vpython import *\n", "import random\n", "import numpy as np\n", "\n", "L = 1 # side square\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)\n", "Natom = 16 # number of atoms\n", "Nr = 0 # number particles right side\n", "dt = 1e-6 # time step\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)\n", "\n", "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) ]# add atoms\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 initial right atoms \n", " inside2.text = '%4s'%Nr \n", " dN[Nr]+ = 1 # for the histogram\n", " for j in arange(0,16): bars.plot(pos = (j,dN[j]))\n", " for obj in scene.objects: # to 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 }