Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.76 KB | None | 0 0
  1. from numpy import sqrt, array, cos, sin, pi, arange, array
  2. from pylab import plot, show, legend, xlabel, ylabel
  3. import time
  4.  
  5. %matplotlib inline
  6.  
  7. start_time = time.time()
  8.  
  9. M = 1.989e30
  10. G = 6.674e-11
  11.  
  12. # This program solves dr/dt = f(r,t)
  13. def f(r,t):
  14.    
  15.     # unpack variables
  16.     x = r[0]
  17.     y = r[1]
  18.     vx = r[2]
  19.     vy = r[3]
  20.    
  21.     radiusMagnitude = sqrt(x**2 + y**2)
  22.    
  23.     fx = vx
  24.     fy = vy
  25.     fvx = -G * M * x / (radiusMagnitude**3)
  26.     fvy = -G * M * y / (radiusMagnitude**3)
  27.    
  28.     return array([fx, fy, fvx, fvy], float)
  29.  
  30. # initial conditions
  31. t0 = 0.0                          # initial time
  32. tf = 40 * 3.154e7 # 3.154e7 is # of seconds in years
  33. N = 75000
  34. # h = (tf - t0) / N # timestep
  35. h = 5e4
  36.  
  37. x = 4e12                          # initial x in meters
  38. y = 0.0                          # intiial y
  39. vx = 0                # initial vx
  40. vy = 500       # initial vy m/s
  41. r = array([x, y, vx, vy], float) # initial r vector
  42. xpoints = [x]
  43. ypoints = [y]
  44. hlist = [h]
  45. tlist = [t0]
  46.  
  47. numcross = 0             # number of x = 0 crossings from left to right
  48. delta = 1000 / 3.154e7             # desired accuracy of 1 km / year
  49.  
  50. while numcross <= 2:
  51.     # adaptive step size
  52.    
  53.     # do one large step
  54.     k1 = 2*h*f(r, t)
  55.     k2 = 2*h*f(r + 0.5*k1, t + 0.5 * h)
  56.     k3 = 2*h*f(r + 0.5*k2, t + 0.5 * h)
  57.     k4 = 2*h*f(r + k3, t + h)
  58.     r1 = r + (k1 + 2*k2 + 2*k3 + k4)/6
  59.    
  60.     # do two small steps
  61.     k1 = h*f(r, t)
  62.     k2 = h*f(r + 0.5*k1, t + 0.5 * h)
  63.     k3 = h*f(r + 0.5*k2, t + 0.5 * h)
  64.     k4 = h*f(r + k3, t + h)
  65.     r2 = r + (k1 + 2*k2 + 2*k3 + k4)/6
  66.    
  67.     k1 = h*f(r2, t)
  68.     k2 = h*f(r2 + 0.5*k1, t + 0.5 * h)
  69.     k3 = h*f(r2 + 0.5*k2, t + 0.5 * h)
  70.     k4 = h*f(r2 + k3, t + h)
  71.     r2 += (k1 + 2*k2 + 2*k3 + k4)/6
  72.    
  73.     # calculate the value of rho
  74.     x1,y1 = r1[0],r1[2]
  75.     x2,y2 = r2[0],r2[2]
  76.     epsilon = sqrt((x1 - x2)**2 + (y1 - y2)**2)
  77.     rho = 30*h*delta/epsilon
  78.    
  79.     # if rho >= 1.0, increase step size, save data
  80.     if rho >= 1.0:
  81.         t += 2*h                  # increase t
  82.         h *= min(rho**0.25, 2.0)  # increase h
  83.        
  84.         yold = r[1]              
  85.         r = r2                    # update r
  86.         ynew = r[1]
  87.        
  88.         # test to see if object crossed the x-axis from negative to positive
  89.         if ynew > 0 and yold < 0:
  90.             numcross += 1
  91.        
  92.         # save data
  93.         xpoints.append(r[0])
  94.         ypoints.append(r[1])
  95.         hlist.append(h)
  96.         tlist.append(t)
  97.        
  98.     # if rho <= 1.0, decrease step size, repeat  
  99.     else:
  100.         h *= rho**0.25
  101.    
  102. plot (ypoints, xpoints, "b.", label="y vs x")
  103. xlabel("y pos")
  104. ylabel("x pos")
  105. legend()
  106. show()
  107.  
  108. print("--- %s seconds taken to run ---" % (time.time() - start_time))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement