[关闭]
@Guoguo0605 2016-04-11T21:58:40.000000Z 字数 4064 阅读 1238

Isothermal v.s. Adiabatic

code trajectory_of_cannon_shell


  1. # -*- coding: utf-8 -*-
  2. from pylab import *
  3. from math import *
  4. g = 9.8
  5. b2m = 4e-5
  6. class flight_state:
  7. def __init__(self, _x = 0, _y = 0, _vx = 0, _vy = 0, _t = 0):
  8. self.x = _x
  9. self.y = _y
  10. self.vx = _vx
  11. self.vy = _vy
  12. self.t = _t
  13. class cannon:
  14. def __init__(self, _fs = flight_state(0, 0, 0, 0, 0), _dt = 0.1):
  15. self.cannon_flight_state = []
  16. self.cannon_flight_state.append(_fs)
  17. self.dt = _dt
  18. def next_state(self, current_state):
  19. global g
  20. next_x = current_state.x + current_state.vx * self.dt
  21. next_vx = current_state.vx
  22. next_y = current_state.y + current_state.vy * self.dt
  23. next_vy = current_state.vy - g * self.dt
  24. #print next_x, next_y
  25. return flight_state(next_x, next_y, next_vx, next_vy, current_state.t + self.dt)
  26. def shoot(self):
  27. while not(self.cannon_flight_state[-1].y < 0):
  28. self.cannon_flight_state.append(self.next_state(self.cannon_flight_state[-1]))
  29. r = - self.cannon_flight_state[-2].y / self.cannon_flight_state[-1].y
  30. self.cannon_flight_state[-1].x = (self.cannon_flight_state[-2].x + r * self.cannon_flight_state[-1].x) / (r + 1)
  31. self.cannon_flight_state[-1].y = 0
  32. def show_trajectory(self, labe, n, colo):
  33. x = []
  34. y = []
  35. for fs in self.cannon_flight_state:
  36. x.append(fs.x)
  37. y.append(fs.y)
  38. if n == 1:
  39. subplot(121)
  40. plot(x,y, label = labe, color=colo)
  41. xlabel(r'$x(m)$', fontsize=16)
  42. ylabel(r'$y(m)$', fontsize=16)
  43. text(40767, 14500, 'initial speed: 700m/s\n' + 'firing angle: 45' + r'$^{\circ}$', color='black')
  44. title('Trajectory of cannon shell')
  45. ax = gca()
  46. ax.spines['right'].set_color('none')
  47. ax.spines['top'].set_color('none')
  48. ax.xaxis.set_ticks_position('bottom')
  49. ax.yaxis.set_ticks_position('left')
  50. ax.set_xlim(0, 60000)
  51. ax.set_ylim(0, 18000)
  52. elif n == 2:
  53. subplot(122)
  54. plot(x,y, label = labe)
  55. xlabel(r'$x(m)$', fontsize=16)
  56. text(35367, 15000, 'initial speed: *700m/s*\n' + 'firing angle: 45' + r'$^{\circ}$', fontsize=16)
  57. title('Trajectory of cannon shell')
  58. ax = gca()
  59. ax.spines['right'].set_color('none')
  60. ax.spines['top'].set_color('none')
  61. ax.xaxis.set_ticks_position('bottom')
  62. ax.yaxis.set_ticks_position('left')
  63. ax.set_xlim(0, 60000)
  64. ax.set_ylim(0, 18000)
  65. #show()
  66. class drag_cannon(cannon):
  67. def next_state(self, current_state):
  68. global g, b2m
  69. v = sqrt(current_state.vx * current_state.vx + current_state.vy * current_state.vy)
  70. next_x = current_state.x + current_state.vx * self.dt
  71. next_vx = current_state.vx - b2m * v * current_state.vx * self.dt
  72. next_y = current_state.y + current_state.vy * self.dt
  73. next_vy = current_state.vy - g * self.dt - b2m * v * current_state.vy * self.dt
  74. #print next_x, next_y
  75. return flight_state(next_x, next_y, next_vx, next_vy, current_state.t + self.dt)
  76. class adiabatic_drag_cannon(cannon):
  77. def next_state(self, current_state):
  78. global g, b2m
  79. factor = (1 - 6.5e-3 * current_state.y / 288.15) ** 2.5
  80. v = sqrt(current_state.vx * current_state.vx + current_state.vy * current_state.vy)
  81. next_x = current_state.x + current_state.vx * self.dt
  82. next_vx = current_state.vx - factor * b2m * v * current_state.vx * self.dt
  83. next_y = current_state.y + current_state.vy * self.dt
  84. next_vy = current_state.vy - g * self.dt - factor * b2m * v * current_state.vy * self.dt
  85. #print next_x, next_y
  86. return flight_state(next_x, next_y, next_vx, next_vy, current_state.t + self.dt)
  87. class isothermal_drag_cannon(cannon):
  88. def next_state(self, current_state):
  89. global g, b2m
  90. factor = exp(-current_state.y / 1e4)
  91. v = sqrt(current_state.vx * current_state.vx + current_state.vy * current_state.vy)
  92. next_x = current_state.x + current_state.vx * self.dt
  93. next_vx = current_state.vx - factor * b2m * v * current_state.vx * self.dt
  94. next_y = current_state.y + current_state.vy * self.dt
  95. next_vy = current_state.vy - g * self.dt - factor * b2m * v * current_state.vy * self.dt
  96. #print next_x, next_y
  97. return flight_state(next_x, next_y, next_vx, next_vy, current_state.t + self.dt)
  98. speed = 700
  99. theta = [45]
  100. v_x = [speed * cos(i * pi / 180) for i in theta]
  101. v_y = [speed * sin(i * pi / 180) for i in theta]
  102. def wahaha():
  103. a = []
  104. b = []
  105. c = []
  106. for i in range(len(theta)):
  107. a.append(isothermal_drag_cannon(flight_state(0, 0, v_x[i], v_y[i], 0), _dt = 0.1))
  108. labe1 = 'isothermal model'
  109. a[i].shoot()
  110. a[i].show_trajectory(labe1,1,'blue')
  111. legend(loc='upper left', frameon=False)
  112. b.append(adiabatic_drag_cannon(flight_state(0, 0, v_x[i], v_y[i], 0), _dt = 0.1))
  113. labe2 = 'adiabatic model'
  114. b[i].shoot()
  115. b[i].show_trajectory(labe2,1,'red')
  116. legend(loc='upper left', frameon=False)
  117. c.append(drag_cannon(flight_state(0, 0, v_x[i], v_y[i], 0), _dt = 0.1))
  118. labe1 = 'No air density change'
  119. c[i].shoot()
  120. c[i].show_trajectory(labe1,1,'black')
  121. legend(loc='upper left', frameon=False)
  122. wahaha()
  123. show()
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注