Skip to content

Commit dc81ff2

Browse files
authored
Jupyter notebook version added
1 parent 22694d2 commit dc81ff2

File tree

3 files changed

+225
-0
lines changed

3 files changed

+225
-0
lines changed

ch10/Program_10.1_walk2d.ipynb

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"#\n",
10+
"# Program 10.1: Random walk in 2D (walk2d.ipynb)\n",
11+
"# J Wang, Computational modeling and visualization with Python\n",
12+
"#\n",
13+
"\n",
14+
"import numpy as np, random as rnd\n",
15+
"import matplotlib.pyplot as plt\n",
16+
"%matplotlib notebook\n",
17+
"\n",
18+
"def walknsteps(n): # walk n steps\n",
19+
" x, y = np.zeros(n+1), np.zeros(n+1)\n",
20+
" for i in range(n):\n",
21+
" phi = rnd.random()*2*np.pi # random in $[0,2\\pi]$\n",
22+
" x[i+1] = x[i] + np.cos(phi)\n",
23+
" y[i+1] = y[i] + np.sin(phi)\n",
24+
" return x, y\n",
25+
" \n",
26+
"nstep, N = 20, 4 # steps, num of walkers\n",
27+
"col = ['k', 'r', 'g', 'b', 'c', 'm'] # color codes\n",
28+
"plt.subplot(111, aspect='equal')\n",
29+
"for i in range(N):\n",
30+
" x, y = walknsteps(nstep)\n",
31+
" plt.quiver(x[:-1], y[:-1], x[1:]-x[:-1], y[1:]-y[:-1], scale=1,\n",
32+
" scale_units='xy', angles='xy', color=col[i%len(col)])\n",
33+
"plt.plot([0],[0],'y*', markersize=16)\n",
34+
"plt.xlabel('$x$'), plt.ylabel('$y$')\n",
35+
"plt.show()"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": null,
41+
"metadata": {
42+
"collapsed": true
43+
},
44+
"outputs": [],
45+
"source": []
46+
}
47+
],
48+
"metadata": {
49+
"kernelspec": {
50+
"display_name": "Python 3",
51+
"language": "python",
52+
"name": "python3"
53+
},
54+
"language_info": {
55+
"codemirror_mode": {
56+
"name": "ipython",
57+
"version": 3
58+
},
59+
"file_extension": ".py",
60+
"mimetype": "text/x-python",
61+
"name": "python",
62+
"nbconvert_exporter": "python",
63+
"pygments_lexer": "ipython3",
64+
"version": "3.6.1"
65+
}
66+
},
67+
"nbformat": 4,
68+
"nbformat_minor": 2
69+
}

ch10/Program_10.2_brownian.ipynb

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"#\n",
10+
"# Program 10.2: Brownian motion (brownian.ipynb)\n",
11+
"# J Wang, Computational modeling and visualization with Python\n",
12+
"#\n",
13+
"\n",
14+
"import numpy as np, random as rnd\n",
15+
"import matplotlib.pyplot as plt\n",
16+
"import matplotlib.animation as am\n",
17+
"%matplotlib notebook\n",
18+
"\n",
19+
"class BrownianMotion: # Brownian motion class\n",
20+
" def __init__(self, N=400, F=0.005, b=0.1, tau=2., h = 0.5):\n",
21+
" self.N, self.F, self.b, self.tau, self.h = N, F, b, tau, h\n",
22+
" self.r, self.v = np.zeros((N, 2)), np.zeros((N, 2))\n",
23+
" self.t = -np.log(np.random.rand(N))*tau # initial kick times\n",
24+
"\n",
25+
" def move(self, r, v, dt): # move between kicks\n",
26+
" edt = np.exp(-self.b*dt)\n",
27+
" return r + v*(1-edt)/self.b, v*edt\n",
28+
"\n",
29+
" def iterate(self): # advance one step\n",
30+
" r, v, t, h, F = self.r, self.v, self.t, self.h, self.F # alias\n",
31+
" for i in range(self.N):\n",
32+
" if t[i] > h: # no kick within current step\n",
33+
" dt = h # dt= time to end of step\n",
34+
" else: # suffers kicks before end of step\n",
35+
" tot, dt = 0., t[i] # tot=time to last kick\n",
36+
" while t[i] <= h:\n",
37+
" r[i], v[i] = self.move(r[i], v[i], dt)\n",
38+
" phi = rnd.random()*2*np.pi # apply kick\n",
39+
" v[i] += [F*np.cos(phi), F*np.sin(phi)] \n",
40+
" tot += dt\n",
41+
" dt = -np.log(rnd.random())*self.tau # sample dt\n",
42+
" t[i] += dt # next kick\n",
43+
" dt = h - tot # dt= to end of current step\n",
44+
" r[i], v[i] = self.move(r[i], v[i], dt) # no kick, just move\n",
45+
" t[i] -= h\n",
46+
" \n",
47+
"def updatefig(*args): # update figure data\n",
48+
" bm.iterate()\n",
49+
" plot.set_data(bm.r[:,0], bm.r[:,1]) # update data\n",
50+
" return [plot] # return plot object\n",
51+
" \n",
52+
"bm = BrownianMotion() # create Brownian model\n",
53+
"fig = plt.figure()\n",
54+
"plt.subplot(111, aspect='equal')\n",
55+
"plot = plt.plot(bm.r[:,0], bm.r[:,1], 'o')[0] # create plot object\n",
56+
"ani = am.FuncAnimation(fig, updatefig, interval=10, blit=True) # animate \n",
57+
"plt.xlim(-1., 1.), plt.ylim(-1., 1.), plt.show()"
58+
]
59+
},
60+
{
61+
"cell_type": "code",
62+
"execution_count": null,
63+
"metadata": {
64+
"collapsed": true
65+
},
66+
"outputs": [],
67+
"source": []
68+
}
69+
],
70+
"metadata": {
71+
"kernelspec": {
72+
"display_name": "Python 3",
73+
"language": "python",
74+
"name": "python3"
75+
},
76+
"language_info": {
77+
"codemirror_mode": {
78+
"name": "ipython",
79+
"version": 3
80+
},
81+
"file_extension": ".py",
82+
"mimetype": "text/x-python",
83+
"name": "python",
84+
"nbconvert_exporter": "python",
85+
"pygments_lexer": "ipython3",
86+
"version": "3.6.1"
87+
}
88+
},
89+
"nbformat": 4,
90+
"nbformat_minor": 2
91+
}

ch10/Program_10.3_mcint.ipynb

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"#\n",
10+
"# Program 10.3: Electrostatic energy (mcint.ipynb)\n",
11+
"# J Wang, Computational modeling and visualization with Python\n",
12+
"#\n",
13+
"\n",
14+
"import numpy as np, random as rnd\n",
15+
"\n",
16+
"def sample(r): # return a random point inside a sphere\n",
17+
" while True:\n",
18+
" x, y, z = rnd.random(), rnd.random(), rnd.random()\n",
19+
" x, y, z = r*(x+x-1), r*(y+y-1), r*(z+z-1) # map to [-r, r]\n",
20+
" if (x*x + y*y + z*z <= r*r): break\n",
21+
" return x, y, z\n",
22+
"\n",
23+
"r1, r2, d = 1.0, 2.0, 4.0 # radii and separation, d>r1+r2\n",
24+
"\n",
25+
"f, V, N = 0., (4*np.pi/3.)**2*(r1*r2)**3, 100\n",
26+
"for i in range(N):\n",
27+
" x1, y1, z1 = sample(r1)\n",
28+
" x2, y2, z2 = sample(r2)\n",
29+
" f += 1./np.sqrt((x1-x2-d)**2+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2))\n",
30+
" \n",
31+
"print ('MC=', V*f/N, 'exact=', V/d)"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": null,
37+
"metadata": {
38+
"collapsed": true
39+
},
40+
"outputs": [],
41+
"source": []
42+
}
43+
],
44+
"metadata": {
45+
"kernelspec": {
46+
"display_name": "Python 3",
47+
"language": "python",
48+
"name": "python3"
49+
},
50+
"language_info": {
51+
"codemirror_mode": {
52+
"name": "ipython",
53+
"version": 3
54+
},
55+
"file_extension": ".py",
56+
"mimetype": "text/x-python",
57+
"name": "python",
58+
"nbconvert_exporter": "python",
59+
"pygments_lexer": "ipython3",
60+
"version": "3.6.1"
61+
}
62+
},
63+
"nbformat": 4,
64+
"nbformat_minor": 2
65+
}

0 commit comments

Comments
 (0)