[关闭]
@wudawufanfan 2016-11-26T05:35:32.000000Z 字数 2229 阅读 562

在此处输入标题

未分类


在此输入正文

  1. from numpy import *
  2. import matplotlib.pyplot as plt
  3. class INVERSE(object):
  4. '''
  5. class BINARY solves for system that not satisfy the inverse-square law
  6. where:
  7. -beta: index of the force
  8. e: ellipticity
  9. m: mass of central star
  10. dt, time : time step size and total time
  11. '''
  12. def __init__(self, _beta=2.05, _e=0., _m=4*(pi**2), _dt=0.001, _time=10):
  13. self.m=_m
  14. self.e=_e
  15. self.x, self.y=[1.],[0.]
  16. self.vx, self.vy=[0],[sqrt(_m)*sqrt((1.-_e)/(1.+_e))]
  17. self.beta=_beta
  18. self.dt=_dt
  19. self.time= _time
  20. self.n=int(_time/_dt)
  21. #print self.x[-1],self.y[-1],self.vx[-1],self.vy[-1]
  22. def cal(self): # use Euler-Cromer Method to calculate the trajectory of stars
  23. for i in range(self.n):
  24. self.r=sqrt(self.x[-1]**2+self.y[-1]**2)
  25. self.vx.append(self.vx[-1]+self.dt*(-self.m*self.x[-1]/self.r**(self.beta+1.)))
  26. self.vy.append(self.vy[-1]+self.dt*(-self.m*self.y[-1]/self.r**(self.beta+1.)))
  27. self.x.append(self.x[-1]+self.vx[-1]*self.dt)
  28. self.y.append(self.y[-1]+self.vy[-1]*self.dt)
  29. def plot_trajectory(self): # plot the trajectory
  30. plt.plot(self.x,self.y,markersize=0.5,label='e= %.2f'%self.e)
  31. plt.plot([self.x[-1]],[self.y[-1]],markersize=8)
  32. plt.plot([0],[0],'or',markersize=20)
  33. def precession_rate(self): # calculate the precession rate
  34. self.x_critical=0
  35. self.y_critical=0
  36. self.t_critical=0
  37. for i in range(len(self.x)-2):
  38. self.r_i=sqrt(self.x[i]**2+self.y[i]**2)
  39. self.r_i1=sqrt(self.x[i+1]**2+self.y[i+1]**2)
  40. self.r_i2=sqrt(self.x[i+2]**2+self.y[i+2]**2)
  41. if self.r_i<self.r_i1 and self.r_i1>self.r_i2:
  42. self.x_critical=self.x[i+1]
  43. self.y_critical=self.y[i+1]
  44. self.t_critical=self.dt*(i+1)
  45. break
  46. self.rate = arctan(self.y_critical/self.x_critical)/self.t_critical
  47. return self.rate
  48. # calculate the trajectory of planet with different ellipticity
  49. for i in range(8):
  50. fig=plt.figure(figsize=(10,10))
  51. plt.xlim(-1.2,1.2)
  52. plt.ylim(-1.2,1.2)
  53. plt.xlabel(r'$x$'+' (AU)',fontsize=18)
  54. plt.ylabel(r'$y$'+' (AU)',fontsize=18)
  55. plt.title(r'$\beta=2.05$'+' '+'e='+str(i*0.2),fontsize=18)
  56. cmp=INVERSE(2.05,i*0.2)
  57. cmp.cal()
  58. cmp.plot_trajectory()
  59. plt.show()
  60. # change the ellipticity and get the corresponding precession rate
  61. e=[]
  62. rate=[]
  63. for i in linspace(0.1,0.6,20):
  64. cmp= INVERSE(2.05,i)
  65. cmp.cal()
  66. e.append(i)
  67. rate.append(180/pi*cmp.precession_rate())
  68. plt.xlim(-0,0.7)
  69. plt.xlabel(r'e')
  70. plt.ylabel(r'Precession rate '+r'$ degree/yr $',fontsize=18)
  71. plt.title(r'Precession rate',fontsize=18)
  72. plt.plot(e,rate,'oy')
  73. plt.plot(e,rate,'-r')
  74. plt.show()
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注