Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.37 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from scipy.integrate import odeint as ODEint
  4.  
  5. def deriv(X, t):
  6.     x1, x2, v1, v2 = X.reshape(4, -1)
  7.     a1 = -x1 * GM * ((x1**2).sum())**-1.5
  8.     a2 = -x2 * GM * ((x2**2).sum())**-1.5
  9.     return np.hstack((v1, v2, a1, a2))
  10.  
  11. GM  = 3.986E+14  # m^3/s^2
  12. a   = 384399000. # meters
  13. twopi = 2*np.pi
  14.  
  15. ecc  = 0.8
  16. apo, peri          = a * (1+ecc), a * (1-ecc)
  17. v_apo, v_a, v_peri = [np.sqrt(GM*(2./r - 1./a)) for r in (apo, a, peri)] # vis-viva equation
  18.  
  19. X0 = np.array([-apo, 0, -a, 0] + [0, -v_apo, 0, -v_a])
  20.  
  21. T  = twopi * np.sqrt(a**3/GM)
  22. times = np.linspace(0, T, 10001)
  23.  
  24. answer, info = ODEint(deriv, X0, times, full_output=True)
  25.  
  26. dirt_pos, moon_pos = answer.T[:4].reshape(2, 2, -1)
  27. r_dirt,   r_moon   = [np.sqrt((pos**2).sum(axis=0)) for pos in (dirt_pos, moon_pos)]
  28. dirt_ang, moon_ang = [np.arctan2(p[1], p[0]) for p in (dirt_pos, moon_pos)]
  29.  
  30. i_dirt_1 = np.argmax(r_dirt < a)
  31. ang_1 = dirt_ang[i_dirt_1]
  32. i_moon_1 = np.argmax(moon_ang[1:] > ang_1) + 1
  33.  
  34. i_dirt_2 = np.argmax(r_dirt[i_dirt_1+2:] > a) + i_dirt_1 + 2
  35. i_moon_2 = i_dirt_2 - i_dirt_1 + i_moon_1
  36. print('ang_1: '   , ang_1)
  37. print('i_dirt_1, i_dirt_2, diff: ', i_dirt_1, i_dirt_2, i_dirt_2-i_dirt_1)
  38. print('i_moon_1, i_moon_2, diff: ', i_moon_1, i_moon_2, i_moon_2-i_moon_1)
  39.  
  40.  
  41. if False:
  42.     fig = plt.figure()
  43.     ax  = fig.add_subplot(1, 1, 1)
  44.     for a in (dirt_ang, moon_ang):
  45.         plt.plot(a)
  46.         plt.plot([i_dirt_1], a[i_dirt_1:i_dirt_1+1], 'ok')
  47.     plt.show()
  48.  
  49.  
  50. if True:
  51.     fig = plt.figure()
  52.     ax  = fig.add_subplot(1, 1, 1)
  53.     # the moon
  54.     x, y = moon_pos
  55.     ax.plot(x, y, '-g')
  56.     ax.plot(x[i_moon_1:i_moon_1+1], y[i_moon_1:i_moon_1+1], 'og', markersize=14)
  57.     ax.plot(x[i_moon_2:i_moon_2+1], y[i_moon_2:i_moon_2+1], 'og', markersize=14)
  58.     ax.plot(x[i_moon_1:i_moon_2], y[i_moon_1:i_moon_2], '-g', linewidth=3)
  59.     # ax.plot(x[:1], y[:1], 'ok', markersize=8)
  60.     # the dirt
  61.     x, y = dirt_pos
  62.     ax.plot(x, y, '-r')
  63.     ax.plot(x[i_dirt_1:i_dirt_1+1], y[i_dirt_1:i_dirt_1+1], 'or', markersize=14)
  64.     ax.plot(x[i_dirt_2:i_dirt_2+1], y[i_dirt_2:i_dirt_2+1], 'or', markersize=14)
  65.     ax.plot(x[i_dirt_1:i_dirt_2], y[i_dirt_1:i_dirt_2], '-r', linewidth=3)
  66.     # ax.plot(x[:1], y[:1], 'ok', markersize=8)
  67.     # now mark the Earth's position
  68.     ax.set_aspect('equal')
  69.     ax.plot([0], [0], 'ob', markersize=16)
  70.     plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement