[关闭]
@computationalphysics-2014301020090 2016-12-04T14:46:42.000000Z 字数 9684 阅读 212

Code

  1. import math
  2. import pylab as pl
  3. class Hyperion:
  4. def __init__(self,theta1,theta2):
  5. self.theta1 = [theta1]
  6. self.theta2 = [theta2]
  7. self.dt = 0.0001
  8. self.t = [0]
  9. self.x = [1]
  10. self.y = [0]
  11. self.v_x = [0]
  12. self.v_y = [2.0*math.pi]
  13. #self.v_y = [5]
  14. self.r = [1]
  15. self.beta = 2
  16. self.avelo1 = [0]
  17. self.avelo2 = [0]
  18. self.dthe = [math.log(abs(0.01))]
  19. def calculate(self):
  20. while (self.t[-1]<=10):
  21. self.r[-1] = math.sqrt((self.x[-1]**2) + (self.y[-1]**2))
  22. self.v_x.append(self.v_x[-1] - \
  23. 4*(math.pi)**2*self.x[-1]*self.dt/((self.r[-1])**(self.beta + 1)))
  24. self.v_y.append(self.v_y[-1] - \
  25. 4*(math.pi)**2*self.y[-1]*self.dt/((self.r[-1])**(self.beta + 1)))
  26. self.avelo1.append(self.avelo1[-1]-12*(math.pi**2)*\
  27. (self.x[-1]*math.sin(self.theta1[-1]) - self.y[-1]*math.cos(self.theta1[-1]))*\
  28. (self.x[-1]*math.cos(self.theta1[-1]) + self.y[-1]*math.sin(self.theta1[-1]))*\
  29. self.dt/(self.r[-1])**5)
  30. self.theta1.append(self.theta1[-1]+self.avelo1[-1]*self.dt)
  31. self.avelo2.append(self.avelo2[-1]-12*(math.pi**2)*\
  32. (self.x[-1]*math.sin(self.theta2[-1]) - self.y[-1]*math.cos(self.theta2[-1]))*\
  33. (self.x[-1]*math.cos(self.theta2[-1]) + self.y[-1]*math.sin(self.theta2[-1]))*\
  34. self.dt/(self.r[-1])**5)
  35. self.theta2.append(self.theta2[-1]+self.avelo2[-1]*self.dt)
  36. if self.theta1[-1] < -math.pi:
  37. self.theta1[-1] = self.theta1[-1] + 2*math.pi
  38. if self.theta1[-1] > math.pi:
  39. self.theta1[-1] = self.theta1[-1] - 2*math.pi
  40. else:
  41. pass
  42. if self.theta2[-1] < -math.pi:
  43. self.theta2[-1] = self.theta2[-1] + 2*math.pi
  44. if self.theta2[-1] > math.pi:
  45. self.theta2[-1] = self.theta2[-1] - 2*math.pi
  46. else:
  47. pass
  48. self.x.append(self.x[-1] + self.v_x[-1]*self.dt)
  49. self.y.append(self.y[-1] + self.v_y[-1]*self.dt)
  50. self.t.append(self.t[-1] + self.dt)
  51. self.dthe.append(abs(self.theta2[-1] - self.theta1[-1]))
  52. def show_results(self):
  53. #font = {'family': 'serif',
  54. #'color': 'darkred',
  55. #'weight': 'normal',
  56. #'size': 16,
  57. #}
  58. #plot,= pl.plot(self.t, self.theta1)
  59. #plot, = pl.plot(self.t,self.avelo1)
  60. plot,= pl.plot(self.t, self.dthe)
  61. pl.semilogy(self.t,self.dthe)
  62. pl.xlim(0,10)
  63. #pl.ylabel('$\Theta$(radians)')
  64. #pl.ylabel('$\omega$(radians/yr)')
  65. pl.ylabel('$\Delta$ $\Theta$(radians)')
  66. pl.xlabel('time(yr)')
  67. #pl.title('Hyperion $\Theta$ versus time 096')
  68. #pl.title('Hyperion $\omega$ versus time 096')
  69. pl.title('Hyperion $\Delta$ $\Theta$ versus time 096')
  70. pl.legend([plot,],['Circular orbit',],loc = "best")
  71. #pl.legend([plot,],['Elliptical orbit',],loc = "best")
  72. #pl.text(self.t[600], self.theta1[600],'Circular orbit', fontdict = font)
  73. pl.show()
  74. a = Hyperion(0,0.01)
  75. a.calculate()
  76. a.show_results()
  1. import math as m
  2. import pylab as pl
  3. #Determine the initial value
  4. def initial(a,e):
  5. x0=a*(1+e)
  6. y0=0
  7. v_x0=0
  8. v_y0=2*m.pi*m.sqrt((1-e)/(a*(1+e)))
  9. return [x0,y0,v_x0,v_y0]
  10. def orbits(beta, e, theta_0):
  11. i_M=initial(1, e)
  12. x0=i_M[0]
  13. x=[]
  14. x.append(x0)
  15. y0=i_M[1]
  16. y=[]
  17. y.append(y0)
  18. v_x0=i_M[2]
  19. v_x=[]
  20. v_x.append(v_x0)
  21. v_y0=i_M[3]
  22. v_y=[]
  23. v_y.append(v_y0)
  24. r=[]
  25. r.append(m.sqrt(x0**2+y0**2))
  26. time=8
  27. t = [0]
  28. dt=0.0001
  29. #Hyperion motion
  30. theta = [theta_0]
  31. w =[0]
  32. for i in range(int(time/dt)):
  33. v_x.append(v_x[i]-4*m.pi**2*x[i]/(r[i]**(beta+1))*dt)
  34. x.append(x[i]+v_x[i+1]*dt)
  35. v_y.append(v_y[i]-4*m.pi**2*y[i]/(r[i]**(beta+1))*dt)
  36. y.append(y[i]+v_y[i+1]*dt)
  37. r.append(m.sqrt(x[i+1]**2+y[i+1]**2))
  38. w.append(w[i] - (3 * 4 * m.pi ** 2 * (x[i] * m.sin(theta[i]) - y[i] * \
  39. m.cos(theta[i])) * (x[i] * m.cos(theta[i]) + y[i] * \
  40. m.sin(theta[i]))) * dt)
  41. theta.append(theta[i] + w[i + 1] * dt)
  42. if theta[-1] >= m.pi:
  43. theta[-1] = theta[-1] - 2 * m.pi
  44. if theta[-1] <= -m.pi:
  45. theta[-1] = theta[-1] + 2 * m.pi
  46. t.append(t[i] + dt)
  47. return [x,y,v_x,v_y,r,theta,w,t]
  48. def delta_theta(theta_1, theta_2):
  49. delta_theta = []
  50. for i in range(len(theta_1)):
  51. delta_theta.append(abs(theta_1[i] - theta_2[i]))
  52. return delta_theta
  53. #The orbits of moon
  54. M5 = orbits(2.0, 0, 0.004)
  55. theta_M5 = M5[5]
  56. w_M5 = M5[6]
  57. t_M5 = M5[7]
  58. M6 = orbits(2.0, 0, 0.003)
  59. theta_M6 = M6[5]
  60. w_M6 = M6[6]
  61. t_M6 = M6[7]
  62. delta_theta_M = delta_theta(theta_M6, theta_M5)
  63. everage_M = sum(delta_theta_M) / len(delta_theta_M)#Lyapunov exponent
  64. print everage_M
  65. Delta_theta = []
  66. for i in range(len(delta_theta_M)):
  67. Delta_theta.append(m.exp(everage_M * t_M5[i]))
  68. pl.figure(figsize=[10,5])
  69. pl.subplot(121)
  70. pl.title('Hyperion $\Delta\\theta$ versus time',fontsize=15)
  71. pl.xlabel('time $(yr)$',fontsize=15)
  72. pl.ylabel('$\Delta\\theta$ (radius)',fontsize=15)
  73. pl.text(3, 0.06, 'Circular orbit', fontsize=15)
  74. pl.plot(t_M5, delta_theta_M)
  75. pl.plot(t_M5, Delta_theta)
  76. pl.semilogy(0.0001, 0.1)
  77. M7 = orbits(2.0, 0.28, 0.004)
  78. theta_M7 = M7[5]
  79. w_M7 = M7[6]
  80. t_M7 = M7[7]
  81. M8 = orbits(2.0, 0.28, 0.003)
  82. theta_M8 = M8[5]
  83. w_M8 = M8[6]
  84. t_M8 = M8[7]
  85. delta_theta_M = delta_theta(theta_M8, theta_M7)
  86. everage_M = sum(delta_theta_M) / len(delta_theta_M)
  87. Delta_theta = []
  88. for i in range(len(delta_theta_M)):
  89. Delta_theta.append(m.exp(everage_M * t_M7[i]))
  90. print everage_M
  91. pl.subplot(122)
  92. pl.title('Hyperion $\Delta\\theta$ versus time',fontsize=15)
  93. pl.xlabel('time $(yr)$',fontsize=15)
  94. pl.ylabel('$\Delta\\theta$ (radius)',fontsize=15)
  95. pl.plot(t_M7, delta_theta_M)
  96. pl.plot(t_M7, Delta_theta)
  97. pl.semilogy(0.0001, 0.1)
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. import math
  4. class hyperion:
  5. def __init__(self,GM=4*math.pi**2,dt=0.0001,time=10):
  6. self.GM=GM
  7. self.x=[[1],[1],[1],[1]]
  8. self.y=[[0],[0],[0],[0]]
  9. self.vx=[[0],[0],[0],[0]]
  10. self.vy=[[5.5],[5],[4],[3]]
  11. self.x1=[[1],[1],[1],[1]]
  12. self.y1=[[0],[0],[0],[0]]
  13. self.vx1=[[0],[0],[0],[0]]
  14. self.vy1=[[5.5],[5],[4],[3]]
  15. self.dt=dt
  16. self.time=time
  17. self.r=[[math.sqrt(self.x[0][0]**2+self.y[0][0]**2)],[math.sqrt(self.x[1][0]**2+self.y[1][0]**2)],[math.sqrt(self.x[2][0]**2+self.y[2][0]**2)],[math.sqrt(self.x[3][0]**2+self.y[3][0]**2)]]
  18. self.t=[[0],[0],[0],[0]]
  19. self.w=[[0],[0],[0],[0]]
  20. self.theta=[[0.05],[0.05],[0.05],[0.05]]
  21. self.r1=[[math.sqrt(self.x1[0][0]**2+self.y1[0][0]**2)],[math.sqrt(self.x1[1][0]**2+self.y1[1][0]**2)],[math.sqrt(self.x1[2][0]**2+self.y1[2][0]**2)],[math.sqrt(self.x1[3][0]**2+self.y1[3][0]**2)]]
  22. self.t1=[[0],[0],[0],[0]]
  23. self.w1=[[0],[0],[0],[0]]
  24. self.theta1=[[0],[0],[0],[0]]
  25. self.d=[[math.log(abs(self.theta[0][0]-self.theta1[0][0]))],[math.log(abs(self.theta[1][0]-self.theta1[1][0]))],[math.log(abs(self.theta[2][0]-self.theta1[2][0]))],[math.log(abs(self.theta[3][0]-self.theta1[3][0]))]]
  26. self.dw=[[abs(self.w[0][0]-self.w1[0][0])],[abs(self.w[1][0]-self.w1[1][0])],[abs(self.w[2][0]-self.w1[2][0])],[abs(self.w[3][0]-self.w1[3][0])]]
  27. def calculate(self):
  28. for n in range(4):
  29. for i in range(int(self.time//self.dt)):
  30. self.vx[n].append(self.vx[n][i]-self.GM*self.x[n][i]*self.dt/self.r[n][i]**3)
  31. self.vy[n].append(self.vy[n][i]-self.GM*self.y[n][i]*self.dt/self.r[n][i]**3)
  32. self.x[n].append(self.x[n][i]+self.vx[n][i+1]*self.dt)
  33. self.y[n].append(self.y[n][i]+self.vy[n][i+1]*self.dt)
  34. self.r[n].append(math.sqrt(self.x[n][i+1]**2+self.y[n][i+1]**2))
  35. self.w[n].append(self.w[n][i]-3*self.GM/self.r[n][i]**5*(self.x[n][i]*math.sin(self.theta[n][i])-self.y[n][i]*math.cos(self.theta[n][i]))*(self.x[n][i]*math.cos(self.theta[n][i])+self.y[n][i]*math.sin(self.theta[n][i]))*self.dt)
  36. self.theta[n].append(self.theta[n][i]+self.w[n][i+1]*self.dt)
  37. self.t[n].append(self.t[n][i]+self.dt)
  38. if self.theta[n][i+1]<-math.pi:
  39. self.theta[n][i+1]=self.theta[n][i+1]+2*math.pi
  40. if self.theta[n][i+1]>math.pi:
  41. self.theta[n][i+1]=self.theta[n][i+1]-2*math.pi
  42. self.vx1[n].append(self.vx1[n][i]-self.GM*self.x1[n][i]*self.dt/self.r1[n][i]**3)
  43. self.vy1[n].append(self.vy1[n][i]-self.GM*self.y1[n][i]*self.dt/self.r1[n][i]**3)
  44. self.x1[n].append(self.x1[n][i]+self.vx1[n][i+1]*self.dt)
  45. self.y1[n].append(self.y1[n][i]+self.vy1[n][i+1]*self.dt)
  46. self.r1[n].append(math.sqrt(self.x1[n][i+1]**2+self.y1[n][i+1]**2))
  47. self.w1[n].append(self.w1[n][i]-3*self.GM/self.r1[n][i]**5*(self.x1[n][i]*math.sin(self.theta1[n][i])-self.y1[n][i]*math.cos(self.theta1[n][i]))*(self.x1[n][i]*math.cos(self.theta1[n][i])+self.y1[n][i]*math.sin(self.theta1[n][i]))*self.dt)
  48. self.theta1[n].append(self.theta1[n][i]+self.w1[n][i+1]*self.dt)
  49. self.t1[n].append(self.t1[n][i]+self.dt)
  50. if self.theta1[n][i+1]<-math.pi:
  51. self.theta1[n][i+1]=self.theta1[n][i+1]+2*math.pi
  52. if self.theta1[n][i+1]>math.pi:
  53. self.theta1[n][i+1]=self.theta1[n][i+1]-2*math.pi
  54. self.d[n].append(math.log(abs(self.theta[n][i]-self.theta1[n][i])))
  55. self.dw[n].append(abs(self.w[n][i]-self.w1[n][i]))
  56. def show_results(self,color):
  57. ax1=plt.subplot(221)
  58. ax2=plt.subplot(222)
  59. ax3=plt.subplot(223)
  60. ax4=plt.subplot(224)
  61. plt.sca(ax1)
  62. plt.plot(self.t[0],self.d[0],'g',label=r'd$\Theta$=0.01,v=5.5 Elliptical orbit')
  63. #plt.plot(self.t[0],self.dw[0],'b',label=r'd$\Theta$=0.05,v=5.5 Elliptical orbit')
  64. plt.title(r'Hyperion $\Delta{\Theta}$ versus time',fontsize=12)
  65. #plt.title(r'Hyperion $\Delta{\omega}$ versus time',fontsize=12)
  66. plt.xlabel(u'time(yr)',fontsize=12)
  67. plt.ylabel(u'$\Delta{\Theta}$(radians)',fontsize=12)
  68. #plt.ylabel(u'$\Delta{\omega}$(radians/yr)',fontsize=12)
  69. plt.legend(fontsize=10,loc='best')
  70. plt.sca(ax2)
  71. plt.plot(self.t[1],self.d[1],'g',label=r'd$\Theta$=0.01,v=5 Elliptical orbit')
  72. #plt.plot(self.t[1],self.dw[1],'b',label=r'd$\Theta$=0.05,v=5 Elliptical orbit')
  73. plt.title(r'Hyperion $\Delta{\Theta}$ versus time',fontsize=12)
  74. #plt.title(r'Hyperion $\Delta{\omega}$ versus time',fontsize=12)
  75. plt.xlabel(u'time(yr)',fontsize=12)
  76. plt.ylabel(u'$\Delta{\Theta}$(radians)',fontsize=12)
  77. #plt.ylabel(u'$\Delta{\omega}$(radians/yr)',fontsize=12)
  78. plt.legend(fontsize=10,loc='best')
  79. plt.sca(ax3)
  80. plt.plot(self.t[2],self.d[2],'g',label=r'd$\Theta$=0.01,v=4 Elliptical orbit')
  81. #plt.plot(self.t[2],self.dw[2],'b',label=r'd$\Theta$=0.05,v=4 Elliptical orbit')
  82. plt.title(r'Hyperion $\Delta{\Theta}$ versus time',fontsize=12)
  83. #plt.title(r'Hyperion $\Delta{\omega}$ versus time',fontsize=12)
  84. plt.xlabel(u'time(yr)',fontsize=12)
  85. plt.ylabel(u'$\Delta{\Theta}$(radians)',fontsize=12)
  86. #plt.ylabel(u'$\Delta{\omega}$(radians/yr)',fontsize=12)
  87. plt.legend(fontsize=10,loc='best')
  88. plt.sca(ax4)
  89. plt.plot(self.t[3],self.d[3],'g',label=r'd$\Theta$=0.01,v=3 Elliptical orbit')
  90. #plt.plot(self.t[3],self.dw[3],'b',label=r'd$\Theta$=0.05,v=3 Elliptical orbit')
  91. plt.title(r'Hyperion $\Delta{\Theta}$ versus time',fontsize=12)
  92. #plt.title(r'Hyperion $\Delta{\omega}$ versus time',fontsize=12)
  93. plt.xlabel(u'time(yr)',fontsize=12)
  94. plt.ylabel(u'$\Delta{\Theta}$(radians)',fontsize=12)
  95. #plt.ylabel(u'$\Delta{\omega}$(radians/yr)',fontsize=12)
  96. plt.legend(fontsize=10,loc='best')
  97. a=hyperion()
  98. a.calculate()
  99. a.show_results('b')
  100. plt.show()
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注