Advertisement
calcpage

CSH2012 FINAL orbits01.py

Jun 21st, 2013
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.06 KB | None | 0 0
  1. #!/usr/bin/python
  2. #orbits01.py    MrG     2013.0531
  3. #copied from http://vpython.erikthompson.com
  4. from __future__ import division
  5. from visual import *
  6.  
  7. print"""
  8. In this model we will attempt to figure out the speed of the moon orbiting the
  9. earth using Newton's Law of Universal Gravitation.
  10.  
  11. The earths mass is 5.98 * 10**24 kg
  12. The earths radius is 6,380 km
  13.  
  14. The moons mass is 7.36 * 10**22 kg
  15. The moons radius is 1,740 km
  16.  
  17. The center of the moon is 384000 km from the center of the earth.
  18. The hubble space telescope orbits 600 km from the earth surface.
  19. The international space station orbits about 360 km from the earth surface.
  20. Geostationary orbit is 35,786 km above the earth's surface (equator).
  21. GPS satellites orbit 20,000 km above the earth's surface.
  22.  
  23. In this episode we'll pretend the earth is stationary and ignore the satellites gravitational
  24. pool on the earth.
  25.  
  26. To use this program:
  27.  
  28. 1.  It is recommended that you uncomment the lines that
  29.    increase the radius of the earth and satellite.
  30.  
  31. 2.  Ajust the initial speed of the satellite to different speeds.  Try
  32.    to figure out what speed gives a circular orbit by adjusting the speed
  33.    and rerunning the program.
  34.  
  35. 3.  You may need to decrease the value of dt to smaller values if you are
  36.    trying to model satellites close to the earth.
  37. """
  38.  
  39. ##########################################################################################
  40. #
  41. # FUNCTIONS
  42. #
  43. ##########################################################################################
  44.  
  45. # this converts totalseconds to a nice string (days, hours, minutes and seconds)
  46. def make_time_string(t):
  47.     "Accept a number of seconds, return a relative string."
  48.     if t < 60: return "%02i seconds"%t
  49.     mins,secs = divmod(t, 60)
  50.     if mins < 60: return "%02i minutes %02i seconds"%(mins,secs)
  51.     hours, mins = divmod(mins,60)
  52.     if hours < 24: return "%02i hours %02i minutes"%(hours,mins)
  53.     days,hours = divmod(hours,24)
  54.     return "%02i days %02i hours"%(days,hours)
  55.  
  56.  
  57. ##########################################################################################
  58. #
  59. # INITIALIZE WINDOW & DECLARATIONS
  60. #
  61. ##########################################################################################
  62.  
  63. scene.center = (0,0,0)
  64. scene.width = 800
  65. scene.height = 600
  66.  
  67. scene.range = (1.0e9,1.0e9,1.0e9)
  68.  
  69. # some approximate data from the internet in scientific notation
  70. # note: the following are three different ways of writing the same number
  71. # 123000
  72. # 1.23 * 10**5
  73. # 1.23e5
  74. massOfEarth = 5.98e24   # kg
  75. massOfSatellite = 7.36e22    # kg
  76. radiusOfEarth = 6.38e6  # m
  77. radiusOfSatellite = 1.74e6   # m
  78. distanceEarthToSatellite = 3.84e8  # m - the moon
  79. #distanceEarthToSatellite = radiusOfEarth +
  80.  
  81. # if we want to model satellites close to earth we may
  82. # need to lower the difference in time (dt) which will
  83. # decrease the error in our calculations but will take
  84. # longer to run.
  85. dt = 100 # seconds (try 100 for the moon, 1 to 10 for lower satellites)
  86. totalseconds = 0
  87.  
  88. # Univsal Law of Gravitation
  89. # Gravitational Force = G * (mass1 * mass2) / r**2
  90. # G is the univasal gravitaion constant which is about 6.67 * 10**(-11)
  91. # r is the distance between the centers of the two masses
  92. G = 6.67e-11
  93.  
  94.  
  95. ##########################################################################################
  96. #
  97. # CREATE EARTH AND SATELLITE OBJECTS
  98. #
  99. ##########################################################################################
  100.  
  101. earth = sphere(pos=(0,0,0),radius=radiusOfEarth,color=color.blue)
  102. earth.mass = massOfEarth
  103.  
  104. satellite = sphere(pos=(0,distanceEarthToSatellite,0),radius=radiusOfSatellite,color=color.white)
  105. satellite.mass = massOfSatellite
  106.  
  107. mylabel = label(pos=(0,distanceEarthToSatellite + 100000000,0))
  108.  
  109. # when we draw things to scale as above the moon is a little hard to see
  110. # lets pretend the moon has a much larger radius so we can see it
  111. #satellite.radius = satellite.radius * 3
  112. #earth.radius = earth.radius * 3 # be careful with this one as low satellites will be hidden
  113.  
  114. ##########################################################################################
  115. #
  116. # SET INITIAL MOTION OF SATELLITE
  117. #
  118. ##########################################################################################
  119.  
  120. # the moon will have a starting velocity that we will provide
  121. # the direction will be left to right
  122. speedOfSatellite = 500 # change this to how fast we think the satellite is going in m/s
  123.  
  124. # Instead of guessing we could cheat and calculate what the speed instead
  125. # Satellites's Acceleration = Gravitational Force / Mass
  126. # This equation is often simplified by cancelling mass2 (the saellite.mass in this case)
  127. #initialAcceleration = (G * earth.mass * satellite.mass / distanceEarthToSatellite**2) / satellite.mass  # UNCOMMENT TO CHEAT
  128.  
  129. # Uniform Circular Motion: a = v**2/R (a is centripetal acceleration, R is the distance from the center of the circle)
  130. # v**2 = a * R
  131. # v = (a * R)**.5
  132.  
  133. #calculatedVelocity = (initialAcceleration * distanceEarthToSatellite)**.5 # UNCOMMENT TO CHEAT
  134. #speedOfSatellite = calculatedVelocity # UNCOMMENT TO CHEAT
  135.  
  136. satellite.velocity = speedOfSatellite * vector(1,0,0) # m/s from left to right  
  137.  
  138.  
  139. ##########################################################################################
  140. #
  141. # GO THRU OUR LOOP
  142. #
  143. ##########################################################################################
  144. finished = False # it's always false so an it's an infitite loop
  145. while not finished:
  146.     totalseconds += dt
  147.     rate(10000) # a high number pretty much means go as fast as our computer can
  148.    
  149.     # the magnitude of a vector subtracted from another is
  150.     # the distance between the two vectors
  151.     distEarthToSatellite = mag(earth.pos - satellite.pos)
  152.  
  153.     # the norm of a vector subtracted from another is a
  154.     # one unit vector in the direction of the other vector
  155.     # See diagram of vector subtraction on wikipedia:
  156.     # http://en.wikipedia.org/wiki/Vector_(spatial)#Vector_addition_and_subtraction
  157.     ForceGravityOnTheSatelliteDir = norm(earth.pos - satellite.pos)    
  158.  
  159.     # calculate force magnitude from Newton's Law of Universal Gravitation
  160.     ForceGravityOnTheSatelliteMag = G * (earth.mass * satellite.mass) / distEarthToSatellite**2
  161.  
  162.     # point that force magnitude in the direction of Earth
  163.     ForceGravityOnTheSatellite = ForceGravityOnTheSatelliteMag * ForceGravityOnTheSatelliteDir
  164.  
  165.     # move the satellite using the force, Newton's 2nd Law, and the position equations
  166.     satellite.acceleration = ForceGravityOnTheSatellite / satellite.mass
  167.     satellite.velocity += satellite.acceleration * dt
  168.     satellite.pos += satellite.velocity * dt + .5 * satellite.acceleration * dt**2
  169.  
  170.     # Display info to the user
  171.     message = "Satellite's distance from earth's surface: %3.f kilometers" % ((distEarthToSatellite-radiusOfEarth)/1000)
  172.     message += "\nSatellite's speed: %.0f m/s" % mag(satellite.velocity)
  173.     message += "\nSatellite's acceleration magnitude: %.4f m/s**2" % mag(satellite.acceleration)
  174.     message += "\nTime: " + make_time_string(totalseconds)
  175.     mylabel.text = message
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement