Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.22 KB | None | 0 0
  1.  
  2. from numpy import sqrt
  3. import time
  4.  
  5. import numpy as np
  6. import scipy.integrate as integrate
  7.  
  8. import matplotlib.pyplot as plot
  9. import matplotlib.animation as animation
  10.  
  11. import RungeKuttaFehlberg as RKF
  12.  
  13. class Orbit:
  14.     """
  15.  
  16.    Orbit Class
  17.  
  18.    init_state is [t0,x0,vx0,y0,vx0],
  19.    where (x0,y0) is the initial position
  20.    , (vx0,vy0) is the initial velocity
  21.    and t0 is the initial time
  22.    """
  23.     def __init__(self,
  24.                  init_state = [0, 0, 1, 2, 0],
  25.                  G=1,
  26.                  m1=1,
  27.                  m2=3):
  28.         self.GravConst = G
  29.         self.mPlanet = m1
  30.         self.mSol = m2
  31.         self.state = np.asarray(init_state, dtype='float')
  32.         self.rkf54 = RKF.RungeKuttaFehlberg54(self.ydot, len(self.state), 0.1, 5**(-14))
  33.  
  34.     def position(self):
  35.         """compute the current x,y positions of the pendulum arms"""
  36.         x = self.state[1]
  37.         y = self.state[3]
  38.         return (x, y)
  39.  
  40.     def energy(self):
  41.         x = self.state[1]
  42.         y = self.state[3]
  43.         vx = self.state[2]
  44.         vy = self.state[4]
  45.         m1 = self.mPlanet
  46.         m2 = self.mSol
  47.         G = self.GravConst
  48.         U=-G*m1*m2/sqrt(x**2+y**2)
  49.         K= m1*(vx**2+vy**2)/2
  50.         return K+U
  51.  
  52.     def time_elapsed(self):
  53.         return self.state[0]
  54.  
  55.     def step(self, h):
  56.         """Uses the trapes method to calculate the new state after h seconds."""
  57.         x=self.state
  58.         #print(self.rkf54.safeStep(x))
  59.         self.state,E = self.rkf54.safeStep(x)
  60.  
  61.     def ydot(self,x):
  62.         G=self.GravConst
  63.         m2=self.mSol
  64.         Gm2=G*m2;
  65.  
  66.         px2=0;py2=0;
  67.         px1=x[1];py1=x[3];vx1=x[2];vy1=x[4];
  68.         dist=sqrt((px2-px1)**2+(py2-py1)**2);
  69.         z=np.zeros(5);
  70.         z[0]=1
  71.         z[1]=vx1
  72.         z[2]=(Gm2*(px2-px1))/(dist**3)
  73.         z[3]=vy1
  74.         z[4]=(Gm2*(py2-py1))/(dist**3)
  75.         return z
  76.  
  77.  
  78.  
  79. # make an Orbit instance
  80. orbit = Orbit([0.0,0.0, (1.022*(10**3)), 385*10**6, 0.0],(6.674*(10**-11)),(7.3477*(10**22)),(5.9736*(10**24)))
  81. dt = 1./30 # 30 frames per second
  82.  
  83. # The figure is set
  84. fig = plot.figure()
  85. axes = fig.add_subplot(111, aspect='equal', autoscale_on=False,
  86.                      xlim=(-10**9, 10**9), ylim=(-10**9, 10**9))
  87.  
  88. line1, = axes.plot([], [], 'o-g', lw=2) # A green planet
  89. line2, = axes.plot([], [], 'o-y', lw=2) # A yellow sun
  90. time_text = axes.text(0.02, 0.95, '', transform=axes.transAxes)
  91. energy_text = axes.text(0.02, 0.90, '', transform=axes.transAxes)
  92.  
  93. def init():
  94.     """initialize animation"""
  95.     line1.set_data([], [])
  96.     line2.set_data([], [])
  97.     time_text.set_text('')
  98.     energy_text.set_text('')
  99.     return line1, line2, time_text, energy_text
  100.  
  101. def animate(i):
  102.     """perform animation step"""
  103.     global orbit, dt
  104.     secondsPerFrame=1
  105.     t0=orbit.state[0]
  106.     print(t0)
  107.     print(orbit.state[0])
  108.     print()
  109.     while orbit.state[0] < t0 + secondsPerFrame:
  110.         orbit.step(dt)
  111.  
  112.     line1.set_data(*orbit.position())
  113.     line2.set_data([0.0,0.0])
  114.     time=orbit.time_elapsed()/60/60/24
  115.     time_text.set_text('time = %.1f days' % time)
  116.     energy_text.set_text('energy = %.3f J' % orbit.energy())
  117.     return line1,line2, time_text, energy_text
  118.  
  119. # choose the interval based on dt and the time to animate one step
  120. # Take the time for one call of the animate.
  121. t0 = time.time()
  122. animate(0)
  123. t1 = time.time()
  124.  
  125. delay = 1000 * dt - (t1-t0)
  126. print(delay)
  127.  
  128. anim=animation.FuncAnimation(fig,        # figure to plot in
  129.                         animate,    # function that is called on each frame
  130.                         frames=900, # total number of frames
  131.                         interval=delay, # time to wait between each frame.
  132.                         repeat=False,
  133.                         blit=True,
  134.                         init_func=init # initialization
  135.                         )
  136.  
  137. # save the animation as an mp4.  This requires ffmpeg or mencoder to be
  138. # installed.  The extra_args ensure that the x264 codec is used, so that
  139. # the video can be embedded in html5.  You may need to adjust this for
  140. # your system: for more information, see
  141. # http://matplotlib.sourceforge.net/api/animation_api.html
  142. anim.save('orbit.mp4', fps=30, extra_args=['-vcodec', 'libx264'])
  143.  
  144. #plot.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement