[关闭]
@2014301020054 2017-01-08T16:48:43.000000Z 字数 10037 阅读 454

Final Exercise:Random System

Python physics 2014301020054


Abstract

Chapter 7 Random System

In this chapter we consider a class of systems in which randomness plays a central role. These are called random or stochastic systems. Typically, these systems consist of a very large number of "degrees of freedom," which might be associated with particles(e.g.,in a macroscopic sample of a liquid) or perhaps spins(in a piece of a ferromagnet). Randomness can then arise in several different waays. For example,it might be impossible to obtain complete knowledge of the positions and velocities of all the particles. Or, our system may interact with a thermal reservoir in the complicated manner that is best described using probabilies and averages as familiar from statitcal mechanics. Hence, even though the "underlying" physics of the system may be deterministic, our incomplete knowlegde may force us to a statistcal,i.e.,stochastic, description. Foetunately, as we will see, a statistical description is often extremely useful, and indeed, most appropriate in these problems.


Background

In Statistical and Thermal Physics, we have done some simulation like "stp_MDApproachToEquilibriumTwoPartitions":

We alse have discussed the behavior of macroscopic systems by appealing to everyday experience and simple observations. So we know that the macroscopic systems are very similar to the following question we discussed.


Main Body

Problem7.1

Calculate the diffusion constant analytically for the random-walk simulations in Figures7.3 and 7.4. These simulations were caarried out in one dimension and on a three-dimensional simple-cubic lattice(respectively). Can you generalize your analytic results to a wider class of lattice types such as a two-dimensional triangular lattice, or a three-dimensional face-centered or body-centered cubic lattice?

(7.2)

7.3 Self-Avoiding Walks

Solution

At first We consider a class of systems in which randomness plays a central role.These are called random or stochastic systems.

1.To simplify the problem, we first only consider the one dimension problem.

Firstly,sketch of a random walk in one dimension. The walker began at x=x0=0,and each step is indicated schematically by a dotted arrow.Here the first step happend to be to the right,while the next three steps were to the left.

To plot two figure about versus step number and <> versus step number. The codes are typed as follows. And the figures can be ploted standardly.

Total codes:

  1. # -*- coding:utf-8 -*-
  2. import pylab as pl
  3. import numpy as np
  4. import math
  5. import random
  6. from mpl_toolkits.mplot3d import Axes3D
  7. import matplotlib.pyplot as plt
  8. from matplotlib import animation
  9. class oneparticle():
  10. def __init__(self,x0=0,dt=0.1,T=10,p1=0.5,p2=0.6,x2ave=0,tot=400):
  11. self.x1=[x0]
  12. self.t1=[0]
  13. self.x2 = [x0]
  14. self.t2 = [0]
  15. self.T=T
  16. self.dt=dt
  17. self.y=[0]
  18. self.num=[0]
  19. self.num1=[0]
  20. self.num2 = [0]
  21. self.p1=p1
  22. self.p2=p2
  23. self.x2ave=[x2ave]
  24. self.x=[0]
  25. self.y=[0]
  26. self.tot=tot
  27. def run(self):
  28. xnew1=0
  29. xnew2=0
  30. for j in range (self.tot):
  31. x1=0
  32. for i in range (100): #每人走100步
  33. if (random.random ()<0.5):
  34. dx=-1
  35. else:
  36. dx=1
  37. x1=x1+dx
  38. self.x.append(x1**2)
  39. for k in range (100):
  40. xx=0
  41. for l in range(self.tot): #取出第k步每人的位移
  42. need = k+1+l*100
  43. xx=self.x[need]+xx
  44. xneed=xx/self.tot #求出k步位移平方的均值
  45. self.x2ave.append(xneed)
  46. self.num.append(self.num[-1]+1)
  47. ##以下是两个人的位移统计
  48. for i in range (100):
  49. if (random.random() < 0.5):
  50. dx = -1
  51. else:
  52. dx = 1
  53. xnew1=xnew1+dx
  54. self.x1.append(xnew1)
  55. self.num1.append(self.num1[-1]+1)
  56. self.y.append(0)
  57. for i in range(100):
  58. if (random.random() < 0.5):
  59. dx = -1
  60. else:
  61. dx = 1
  62. xnew2 = xnew2 + dx
  63. self.x2.append(xnew2)
  64. self.num2.append(self.num2[-1] + 1)
  65. #print(self.num,self.x)
  66. def show(self):
  67. pl.subplot(122)
  68. pl.plot(self.num, self.x2, 'bo', label='$<x^2>$versus time,n=100')
  69. pl.xlabel('step number')
  70. pl.ylabel('$<x^2>$')
  71. pl.title('Random walk in one dimension')
  72. pl.xlim(0, 100)
  73. pl.ylim(0, 100)
  74. pl.axis('equal')
  75. pl.legend(loc='upper left', frameon=False)
  76. pl.subplot(121)
  77. pl.plot(self.num1, self.x1, 'bo', label='walker1')
  78. pl.plot(self.num2, self.x2, 'ro', label='walker2')
  79. pl.xlabel('step number')
  80. pl.ylabel('x')
  81. pl.xlim(0,100)
  82. pl.ylim(-20.20)
  83. pl.title('Random walk in one dimension')
  84. #pl.axis('equal')
  85. pl.legend(loc='best',frameon=False)
  86. pl.show()
  87. def show_trajectory(self):
  88. fig = plt.figure()
  89. ax = plt.axes(title=('Random Walk'), aspect='equal', autoscale_on=False, xlim=(-1.1, 1.1), ylim=(-1.1, 1.1), xlabel=('x'), ylabel=('y'))
  90. line = ax.plot([], [], 'b')
  91. point = ax.plot([], [], 'ro', markersize=10)
  92. images = []
  93. def init():
  94. line = ax.plot([], [], 'b', markersize=8)
  95. point = ax.plot([], [], 'ro', markersize=10)
  96. return line, point
  97. def anmi(i):
  98. ax.clear()
  99. line = ax.plot(self.x1[0:(1 * i)], self.y[0:(1 * i)], 'b', markersize=8)
  100. point = ax.plot(self.x1[(1 * i - 1):(1* i)], self.y[(1 * i - 1):(1 * i)], 'ro', markersize=10)
  101. return line, point
  102. anmi = animation.FuncAnimation(fig, anmi, init_func=init, frames=1000, interval=1, blit=False)
  103. plt.show()
  104. a=oneparticle()
  105. a.run()
  106. a.show()
  107. a.show_trajectory()

According to the second picture ,we can find that:

In fact ,this result D can be obtained analytically.Writing the position after n steps, xn ,as a sum of n separate steps gives

where is the displacement of the ith step.For this problem, with equal probabilities.We can write that

since n is equal to time,this is identical to D=1/2

2.Then we consider the step is random too,x versus step is in the range -1 to 1

we just need to change the expression of dx to

  1. dx=random.uniform(-1,1)

And then we plot the follow pictures after several attempts.

we can obtain different results to show the meaning of "random":

3.Then we consider the three-dimensional random walks.

Modefied code as follows.

  1. for j in range(self.tot):
  2. x = 0
  3. y = 0
  4. z = 0
  5. r = 1
  6. for i in range(100): # 每人走100步
  7. phi = random.uniform(-math.pi, math.pi)
  8. theta = random.uniform(0, math.pi)
  9. dx=r*math.sin(theta)*math.cos(phi)
  10. dy=r*math.sin(theta)*math.sin(phi)
  11. dz=r*math.cos(theta)
  12. x = x + dx
  13. y = y + dy
  14. z = z + dz
  15. r_2=x**2+y**2+z**2
  16. self.r.append(r_2)
  17. for k in range(100):
  18. xx = 0
  19. for l in range(self.tot): # 取出第k步每人的位移
  20. need = k + 1 + l * 100
  21. xx = self.r[need] + xx
  22. xneed = xx / self.tot # 求出k步位移平方的均值
  23. self.r2ave.append(xneed)
  24. self.num.append(self.num[-1] + 1)

we can also get the animation of one ball's trajectory in three-dimension.

The animation can be obtained by followed code:

  1. from mpl_toolkits.mplot3d import Axes3D #生成3D图像的包
  2. import matplotlib.pyplot as plt #用于绘图
  3. from matplotlib import animation #用于制作动画
  4. ##此处省略class中前面部分的代码
  5. def show_trajectory(self):
  6. fig = plt.figure()
  7. ax =Axes3D(fig)
  8. line1=ax.plot([],[],'b:')
  9. point1=ax.plot([],[],'bo',markersize=10)
  10. images=[]
  11. def init():
  12. line1 = ax.plot([], [], 'b', markersize=8)
  13. point1 = ax.plot([], [], 'ro', markersize=10)
  14. return line1, point1
  15. def anmi(i):
  16. ax.clear()
  17. line1 = ax.plot(self.x[0:(1 * i)], self.y[0:(1 * i)], self.z[0:1*i],'b', markersize=8)
  18. point1 = ax.plot(self.x[(1 * i - 1):(1 * i)], self.y[(1 * i - 1):(1 * i)], self.z[1 * i -1:1 * i],'ro', markersize=10)
  19. return line1, point1
  20. anmi = animation.FuncAnimation(fig, anmi, init_func=init, frames=100, interval=1,
  21. blit=False,repeat=False)
  22. plt.show()

If the path followed by our polymer molecule must not be allowed to intersect itself.Only one segment of the polymer molecule is allowed to occupy any particular region of space. A random walk that is subject to this constraint is called a self-avoiding walk,or SAW.

Codes about calculation:

  1. import matplotlib.pyplot as pl
  2. import numpy as np
  3. import math
  4. import random
  5. class particle(object):#def a class"particle"
  6. diret = 0
  7. def __init__(self,x,y,state):
  8. self.x=x
  9. self.y=y
  10. self.state=state
  11. def moveparticle(self):
  12. if self.diret==0:
  13. self.x=self.x+0.5
  14. elif self.diret==1:
  15. self.x=self.x-0.5
  16. elif self.diret==2:
  17. self.y=self.y-0.5
  18. elif self.diret==3:
  19. self.y=self.y+0.5
  20. else :
  21. print ('error2')
  22. self.diret=random.randint(0,3)
  23. return
  24. class screen(object):#def a class"screen"
  25. lis=[particle(50.0,50.0,0)]
  26. state=1
  27. def chargepoint(self,x,y):
  28. for i in self.lis:
  29. if i.x==x and i.y==y:
  30. return 1
  31. return 0
  32. def addparticle(self,x=65.0,y=65.0):#add a new particle
  33. i=self.chargepoint(x,y)
  34. if self.state==1:
  35. if i==0:
  36. self.lis.append(particle(x,y,1))
  37. else:
  38. print ('same particle')
  39. self.state=0
  40. else:
  41. print ('full screen')
  42. for a in self.lis:
  43. if a.x>66.0 or a.y>66.0 or a.x<34.0 or a.y<34.0:
  44. self.state=0
  45. return
  46. def delparticle(self,x,y):
  47. for i in range(len(self.lis)):
  48. if self.lis[i].x==x and self.lis[i]==y:
  49. self.lis.pop(i)
  50. self.state=1
  51. return
  52. return
  53. def showscreen(self):
  54. x=[]
  55. y=[]
  56. for i in self.lis:
  57. x.append(i.x)
  58. y.append(i.y)
  59. pl.plot(x,y,'.')
  60. pl.show()
  61. def move(self):#random walk of particle
  62. t1=self.lis.pop()
  63. while t1.state==1 and t1.x<75.0 and t1.y<75.0 and t1.x>=25 and t1.y>=25:
  64. i1=self.chargepoint(t1.x+0.5,t1.y)
  65. i2=self.chargepoint(t1.x-0.5,t1.y)
  66. i3=self.chargepoint(t1.x,t1.y-0.5)
  67. i4=self.chargepoint(t1.x,t1.y+0.5)
  68. s=[i1,i2,i3,i4]
  69. if i1==0 and i2==0 and i3==0 and i4==0:
  70. t1.moveparticle()
  71. elif random.randint(1,100)>85:
  72. t1.state=0
  73. elif i1==1 and i2==1 and i3==1 and i4==1:
  74. t1.state=0
  75. else:
  76. while s[t1.diret]==1:
  77. t1.diret=random.randint(0,3)
  78. t1.moveparticle()
  79. #print 'move a particle'
  80. #print t1.x,t1.y
  81. if t1.state==0 :
  82. self.lis.append(t1)
  83. if __name__=='__main__':
  84. sc=screen()
  85. n=0
  86. while sc.state==1:
  87. i=random.randint(70,130)
  88. x=i/2.0
  89. r=(15**2-(x-50)**2)**0.5#The new particle can appear where r = 15
  90. k=int(r*2)
  91. y1=k/2.0
  92. if random.randint(1,2)==1:
  93. y=50+y1
  94. else:
  95. y=50-y1
  96. print (x,y)
  97. sc.addparticle(x,y)
  98. print ('generate a new particle')
  99. print (len(sc.lis),sc.state)
  100. #s=raw_input(':')
  101. sc.move()
  102. print (len(sc.lis))
  103. sc.showscreen()

After this let's consider the cluter growth models.We make some assumptions as follows:

1.we only consider the growth is singler-layer
2.we make a new particle randomly located in the place outside of condensation nuclei(we consider the distance between the particle and the center of the condensation nuclei r>15).Then the particle walk randomly in two dimensions, when tha particle comes into contact with the condensation nuclei, it will stop with a certain probability (adhesion coefficient) and become one part of the condensation nuclei.
3.When the particle walk farther then a certain disstance,we consider the particle wouldn't join the growth of the condensation nuclei.
4.When the condensation meet the border,the activity finished.

粘着系数=1

粘着系数=0.75

粘着系数=0.50

粘着系数=0.15

We can find that the number of particles is become larger with the decrease of the adhesion coefficient.And the shape of condensation nuclei is becoming more and more smooth.

Then we can use the expression to describe the degree of branching

is the number of the particles; is the boundary dimension.


Conclusion

The situations of particle's activity in one,two or three dimensions have been simulated roughly.And the property of the random systems can be analyzed detailedly.


Acknowledgement


写在结尾的话

计算物理这门课其实一直都令我感兴趣,只是因为这学期比较忙,我没能花太多的时间去钻研,所以之前的作业,包括期末作业,我都交的比较匆忙,有几次可能直到周一晚才交上来,请老师谅解。
然后这门课也让我明白了计算机解决物理问题的有趣,我等寒假有时间了,想慢慢自己去学习,去做长远的实践。
最后谢谢老师的授课啦~

END

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注