Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.28 KB | None | 0 0
  1. # You will need to paste the relevant code from above
  2. # into this template and run in Spyder.
  3. # Don't forget to type '%matplotlib qt'
  4. # into the Spyder console before running to enable
  5. # the interactive plots
  6. #
  7. # It should work in Jupyter notebook too but you
  8. # won't be able to detect keyboard input or mouse clicks
  9. # to control the rocket. For it to work in Jupyter notebook,
  10. # you will need a '%matplotlib notebook' line at the top.
  11.  
  12.  
  13. # Template begins here
  14. # --------------------
  15.  
  16. # Include import statements and unit registry from above
  17. import numpy as np
  18. from matplotlib import pyplot as pl
  19. import pint
  20. ur = pint.UnitRegistry()
  21. F = (5*ur.kg) * (10*ur.m/ur.s**2)
  22. v = (9*ur.knot/ur.psi**.5)*(49*ur.psi)**.5
  23.  
  24. # Include Rocket and planet characteristics; physics constants
  25. mtmass = 105*ur.kg
  26. payload = 5000*ur.kg
  27. fuelmass = 1300000*ur.kg
  28. TSFC = 3.5*10**-4*ur.s/ur.m
  29. thrust = 23000*1000*ur.N
  30. G = 6.674*10**-11*ur.m**3/(ur.kg/ur.s**2)
  31. Earth = 5.8*10**24*ur.kg
  32. rocketmass=mtmass+payload+fuelmass
  33. print(mtmass)
  34. print(payload)
  35. print(fuelmass)
  36. print(TSFC)
  37. print(G)
  38. print(Earth)
  39. d_earth =10.7e6*ur.m
  40. v_surface = 460.0*ur.m/ur.s
  41. pos = np.array((0.0,float(d_earth/2.0/ur.m)))*ur.m
  42. vel= np.array((float(v_surface*ur.s/ur.m),0.0))*ur.m/ur.s
  43.  
  44. T_angle = 60.0*ur.degree
  45. T_direc = np.array((np.cos(float(T_angle/ur.radian)),np.sin(float(T_angle/ur.radian))))
  46.  
  47. pl.ion()
  48. fig = pl.figure()
  49. pl.axis((-1e6,1e6,6e6,7e6))
  50. pl.grid(True)
  51.  
  52.  
  53.  
  54.  
  55. earth = pl.Circle((0,0),float(d_earth/2.0/ur.m),color='g')
  56.  
  57. fig.gca().add_artist(earth)
  58. # Include earth diameter, surface speed, initial rocket position,
  59. # initial rocket velocity
  60.  
  61. # Include code for the baseline plot and initial arrow
  62.  
  63.  
  64. # Include the event handler
  65. def event_handler(event):
  66. global T_angle
  67. if event.key==',': # press comma to rotate left
  68. # Rotate left (increase angle)
  69. T_angle += 10.0*ur.degree
  70. pass
  71. elif event.key=='.': # press period to rotate right
  72. # Rotate right (decrease angle)
  73. T_angle -= 10.0*ur.degree
  74. pass
  75. elif hasattr(event,"button") and event.button==1:
  76. if event.xdata < 0.0:
  77. # Click on left-half of plot to
  78. # Rotate left (increase angle)
  79. T_angle += 10.0*ur.degree
  80. pass
  81. else:
  82. # Click on right-half of plot to
  83. # Rotate right (decrease angle)
  84. T_angle -= 10.0*ur.degree
  85. pass
  86. pass
  87.  
  88. pass
  89.  
  90. # Connect this event handler to the figure so it is called
  91. # when the mouse is clicked or keys are pressed.
  92. fig.canvas.mpl_connect('key_press_event',event_handler)
  93. fig.canvas.mpl_connect('button_press_event',event_handler)
  94.  
  95. # Include the vector norm function from the
  96. # force of gravity calculation, above.
  97.  
  98. t=0.0 * ur.s # Start at t=0
  99.  
  100. # Loop until ctrl-C or press stop button on
  101. # Spyder console or t exceeds 36000 seconds (10 hours
  102. # of simulated time)
  103.  
  104. while t < 1900 * ur.s:
  105. # Select the time step (code above)
  106. arrowplt = pl.arrow(float(pos[0]/ur.m),float(pos[1]/ur.m),T_direc[0]*500000,T_direc[1]*500000,width=300000)
  107.  
  108. # Update the plot:
  109. if(fuelmass>0):
  110. dt=10*ur.s
  111.  
  112. else:
  113. dt=5*ur.s
  114.  
  115. # * Call arrowplt.remove() method on the old arrow
  116. # * Calculate the thrust (rocket) direction from
  117. # the rocket angle (code way above)
  118. arrowplt = pl.arrow(float(pos[0]/ur.m),float(pos[1]/ur.m),T_direc[0]*500000,T_direc[1]*5000000,width=300000)
  119.  
  120.  
  121. T_direc = np.array((np.cos(float(T_angle/ur.radian)),np.sin(float(T_angle/ur.radian))))
  122. # * Plot the new arrow (code way above)
  123. # * Label the fuel state in the plot title, e.g.
  124. # pl.title("Fuel remaining %f kg" % ())
  125. pl.title("Fuel remaining %f kg" % ((fuelmass)/ur.kg))
  126. pl.xlabel("Time = %f s" % (float(t/ur.s)))
  127. if(fuelmass>0):
  128. pl.axis((-1e6,1e6,6e6,7e6))
  129. else:
  130. pl.axis((-8e6,8e6,-8e6,8e6))
  131.  
  132. # * Show the time in the x axis label, e.g.
  133. # pl.xlabel("Time = %f s" % ())
  134.  
  135. # * Select the plot region according to fuel state (code above)
  136.  
  137. # These next two lines cause the plot display to refresh
  138. fig.canvas.draw()
  139. fig.canvas.flush_events()
  140.  
  141. # Determine the forces on the rocket (code above):
  142. # * Calculate the magnitude of the force of gravity
  143. def norm(vec):
  144. return np.sqrt(np.sum(vec**2.0))
  145. r=norm(pos)
  146. f_gravity = (G)*(mtmass+payload+fuelmass)*(Earth)/r**2
  147. Direc_Gravity = (-pos)/(norm(pos))
  148. Vec_Gravity=(Direc_Gravity)*(f_gravity)
  149. # * Calculate the direction of the force of gravity
  150. # and gravity vector on the rocket
  151. # * Calculate the thrust vector
  152.  
  153. # Update the fuel mass (code above)
  154. if(fuelmass>0):
  155. Vec_Thrust = (thrust)*(T_direc)
  156. else:
  157. Vec_Thrust = 0
  158. # Determine the change in velocity (code above)
  159. # Determine the change in position (code above)
  160.  
  161. dm=((-TSFC)*(thrust)*(dt)).to(ur.kg)
  162. if (fuelmass>0):
  163. fuelmass=fuelmass+dm
  164. else:
  165. fuelmass=0*ur.kg
  166. Sumforce_Vec = 1000
  167. a=1000/rocketmass
  168. dv=a*10
  169. vel=vel*900
  170.  
  171.  
  172.  
  173.  
  174. dpos=pos+(vel*dt)
  175. pos=dpos+pos
  176.  
  177.  
  178. t=t+dt
  179. arrowplt.remove()
  180. pass
  181. # End of loop block
  182. pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement