Advertisement
Guest User

Three-body Problem simulation code

a guest
Jul 16th, 2018
505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.72 KB | None | 0 0
  1. # Three body problem graphical simulation
  2.  
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. from  scipy.integrate import odeint
  6. from matplotlib.animation import FuncAnimation
  7. from matplotlib.widgets import Button
  8. from matplotlib.animation import writers
  9.  
  10. acc = lambda r, m, g : -r*m*g/np.linalg.norm(r)**3
  11.  
  12. m1 = 1
  13. m2 = 1
  14. G = 1
  15. init = np.array([0.0, -0.00, 0.2, -0.2,
  16.                   -1.0, 0.0, 0.0, 0.5,
  17.                   1.0, 0.0, 0.0,-0.5,
  18.                  0.0, 0.00, 0.2005, -0.2,])
  19. init2 = np.array([0.0, -0.00, 0.5, -0.2,
  20.                   -1.0, 0.0, 0.0, 0.2,
  21.                   1.0, 0.0, 0.0,-0.5,
  22.                  0.0001, 0.00, 0.5, -0.2,])
  23.  
  24.  
  25. def f(x, t):
  26.     # This describes gravitational pull of two planets at (-2,0) and (2,0)
  27.     # Position of planets
  28.     x1 = x[4:6]
  29.     x2 = x[8:10]
  30.    
  31.     r1 = x[:2]-x1 # Get radial vectors
  32.     r2 = x[:2]-x2
  33.     r12 = x2-x1
  34.     a = acc(r1, m1, G)+acc(r2, m2, G) # Calculate net acceleration
  35.     a3 = acc(x[12:14]-x1, m1, G)+acc(x[12:14]-x2, m2, G)
  36.    
  37.     a1 = acc(-r12, m2, G)
  38.     a2 = acc( r12, m1, G)
  39.     return np.array([x[2], x[3], a[0], a[1],
  40.                      x[6], x[7],a1[0],a1[1],
  41.                     x[10],x[11],a2[0],a2[1],
  42.                     x[14],x[15],a3[0],a3[1]])
  43.  
  44.  
  45.  
  46. # First two numbers position, second two velocity, for test mass, m1, m2
  47. # Calculate different solutions
  48. res1 = odeint(f, init, np.arange(0,100,0.05))
  49.  
  50. m2 = 0.4
  51. res2 = odeint(f, init2, np.arange(0,100,0.05))
  52. res = res1
  53.  
  54. log1 = np.log(np.linalg.norm(res1[:,0:2]-res1[:,12:14], axis=1))
  55. log2 = np.log(np.linalg.norm(res2[:,0:2]-res2[:,12:14], axis=1))
  56. log = log1
  57.  
  58. c1 = plt.Circle(res[0, 4:6], 0.08, fc='orange', ec='black')
  59. c2 = plt.Circle(res[0, 8:10], 0.08, fc='g', ec='black')
  60. h1 = plt.Circle(res[0,:2], 0.04, fc='r', ec='black')
  61. h2 = plt.Circle(res[0,12:14], 0.04, fc='b', ec='black')
  62.  
  63. def setup():
  64.     ax.set_xlim(-3,3)
  65.     ax.set_ylim(-3,3)
  66.     ax.plot(res[0,0], res[0,1], 'r--', linewidth=2)
  67.     ax.plot(res[0,12], res[0,13], 'b--', linewidth=2)
  68.     ax.add_artist(c1)
  69.     ax.add_artist(c2)
  70.     ax.add_artist(h1)
  71.     ax.add_artist(h2)
  72.    
  73.     ax2.plot(0, log[0], '-', color='black', linewidth=0.5)
  74.    
  75. def update(i):
  76.     ax.lines[0].set_data(res[max(i-100,0):i+1,0], res[max(i-100,0):i+1,1])
  77.     ax.lines[1].set_data(res[max(i-100,0):i+1,12], res[max(i-100,0):i+1,13])
  78.    
  79.     h1.center = res[i,:2]
  80.     h2.center = res[i,12:14]
  81.     c1.center = res[i, 4:6]
  82.     c2.center = res[i, 8:10]
  83.     ax2.lines[0].set_data(np.arange(0,i,1)*0.05,log[:i:1])
  84.  
  85. state = 0
  86. def callback(event):
  87.     global res, state, log
  88.     state += 1
  89.     if(state%2):
  90.         res = res2
  91.         log = log2
  92.         t.set_text('Simulation:\nUneven Masses')
  93.     else:
  94.         res = res1
  95.         log = log1
  96.         t.set_text('Simulation:\nEven Masses')
  97.        
  98.  
  99. fig = plt.figure(figsize=(10,5))
  100. ax = plt.axes([0.1, 0.2, 0.35, 0.7], aspect='equal')
  101. ax.set_title('Restricted Three-Body Problem Simulation')
  102. ax2 = plt.axes([0.55, 0.1, 0.35, 0.8])
  103. ax2.set_title('Logarithm of distance between balls')
  104. ax2.set_xlim(0,100)
  105. ax2.set_ylabel(r'$\log(distance) \rightarrow$')
  106. ax2.set_ylim(-10,2)
  107. ax2.set_xlabel(r'time $\rightarrow$')
  108. # Add Button
  109. axbutton = plt.axes([0.1, 0.05, 0.15, 0.08])
  110. button = Button(axbutton, 'Change simulation', color='red', hovercolor='green')
  111. axsimname = plt.axes([0.30, 0.05, 0.15, 0.1])
  112. t = axsimname.text(0.5,0.5,'Simulation:\nEven Masses', horizontalalignment='center',
  113.                verticalalignment='center', transform=axsimname.transAxes)
  114. axsimname.axis('off')
  115. button.on_clicked(callback)
  116.  
  117. ani = FuncAnimation(fig, update, frames=range(len(res)),
  118.                     init_func=setup, blit=False, interval=30, repeat=False)
  119. #ani.save('Even.mp4')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement