Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.50 KB | None | 0 0
  1. import math as m
  2.  
  3. h = -2
  4. initialFuelMass = 1000
  5.  
  6. g = 1.62
  7. mass = 2150  
  8. fuelV = 3660  
  9. amax = 29.43  
  10. Vymax = 3  
  11. Vxmax = 1  
  12.  
  13.  
  14. def simulateStep(x, y, vx, vy, fuelMass, command):
  15.     alpha, fuelConsumption, dt = command
  16.     if fuelConsumption * dt > fuelMass:
  17.         print('Не хватит топлива')
  18.     ax = fuelV * m.sin(m.radians(alpha)) * fuelConsumption / (mass + fuelMass)
  19.     ay = -g + fuelV * m.cos(m.radians(alpha)) * fuelConsumption / (mass + fuelMass)
  20.     a = m.hypot(ax, abs(ay) - g)
  21.     if a > amax:
  22.         print('Ускорение больше разрешимого')
  23.     x += dt * (vx + ax * dt / 2)
  24.     y += dt * (vy + ay * dt / 2)
  25.     vx += ax * dt
  26.     vy += ay * dt
  27.     if (abs(x) < 5 and y <= 0) or (abs(x) >= 5 and y <= h):
  28.         if abs(vx) > Vxmax or abs(vy) > Vymax:
  29.             print('Рокета разобьётся')
  30.         y = 0 if abs(x) < 5 else h
  31.     fuelMass -= fuelConsumption * dt
  32.  
  33.     return x, y, vx, vy, fuelMass
  34.  
  35.  
  36. x, y = 0, 0
  37. vx, vy = 0, 0
  38. fuelMass = initialFuelMass
  39. dt = 0.1
  40.  
  41. f = open('Полёт корабля.txt', 'w')
  42.  
  43. # Подъём и разгон
  44. time = 0
  45. alpha = 45
  46. fuelConsumption1 = 18
  47. fuelConsumption = fuelConsumption1
  48. while fuelMass > initialFuelMass * 0.47:  
  49.     x, y, vx, vy, fuelMass = simulateStep(x, y, vx, vy, fuelMass, (alpha, fuelConsumption, dt))
  50.     time += dt
  51. print('liftoff::\t', 'x:', round(x), '\ty:', round(y), '\tvx:', round(vx), '\tvy:', round(vy), '\tfuel:',
  52.       round(fuelMass))
  53. f.write(f'liftoff::\t vx: 0\t vy: 0\t x: 0\t y: 0\t '
  54.         f'alpha: {round(alpha)}\t consumption: {fuelConsumption}\t time: {round(time, 1)} \n')
  55.  
  56. # Свободное падение
  57. f.write(f'freefall::\t vx: {round(vx, 1)}\t vy: {round(vy, 1)}\t x: {round(x)}\t y: {round(y)}\t '
  58.         f'alpha: 0\t consumption: 0\t')
  59. time = 0
  60. engineAltitude = y
  61. while y - h > engineAltitude or vy > 0:
  62.     x, y, vx, vy, fuelMass = simulateStep(x, y, vx, vy, fuelMass, (alpha, 0, dt))
  63.     time += dt
  64. print('freefall::\t', 'x:', round(x), '\ty:', round(y), '\tvx:', round(vx), '\tvy:', round(vy), '\tfuel:',
  65.       round(fuelMass))
  66. f.write(f'time: {round(time, 1)} \n')
  67.  
  68. # Торможение
  69. f.write(f'slowdown::\t vx: {round(vx, 1)}\t vy: {round(vy, 1)}\t x: {round(x)}\t y: {round(y)}\t')
  70. time = 0
  71. _fuelConsumptionThree = 16
  72. fuelConsumption = _fuelConsumptionThree
  73. while y > 100:
  74.     x, y, vx, vy, fuelMass = simulateStep(x, y, vx, vy, fuelMass, (-alpha, fuelConsumption, dt))
  75.     time += dt
  76. print('slowdown::\t', 'x:', round(x), '\ty:', round(y), '\tvx:', round(vx), '\tvy:', round(vy), '\tfuel:',
  77.       round(fuelMass))
  78. f.write(f'alpha: {-alpha}\t consumption: {fuelConsumption}\t time: {round(time, 1)} \n')
  79.  
  80. # Приземление
  81. f.write(f'landing::\t vx: {round(vx, 1)}\t vy: {round(vy, 1)}\t x: {round(x)}\t y: {round(y)}\t')
  82. time = 0
  83. YFuelConsumption = 9.8
  84. XFuelConsumption = 10.1
  85. fuelConsumption = m.hypot(YFuelConsumption, XFuelConsumption)
  86. alpha = round(-90 + m.degrees(m.atan(YFuelConsumption / XFuelConsumption)))
  87. while y > h:
  88.     x, y, vx, vy, fuelMass = simulateStep(x, y, vx, vy, fuelMass, (alpha, fuelConsumption, dt))
  89.     if (abs(vy) + m.sqrt(2 * g * (y - h)) < Vymax) and (abs(vx)<Vxmax):
  90.         fuelConsumption = 0
  91.         print('stop engine')
  92.     time += dt
  93.     print('landing::\t', 'x:', round(x), '\ty:', round(y), '\tvx:', round(vx), '\tvy:', round(vy), '\tfuel:',
  94.           round(fuelMass))
  95. f.write(f'alpha: {round(alpha)}\t consumption: {fuelConsumption}\t time: {round(time, 1)} \n')
  96.  
  97. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement