Advertisement
CaelmBleidd

Untitled

Mar 20th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.49 KB | None | 0 0
  1. from math import *
  2. import math
  3.  
  4. Pi = math.pi
  5. g = 1.62  # m/(s^2), Moon
  6. d_t = 0.1
  7.  
  8.  
  9. def read_commands(name):
  10.     com = []
  11.     int_arr = []
  12.     for line in open(name, 'r'):
  13.         com.append(line.split(" "))
  14.     for line in com:
  15.         int_line = []
  16.         for word in line:
  17.             to_int_word = ""
  18.             for c in word:
  19.                 if c.isdigit() or c == '.':
  20.                     to_int_word += c
  21.             if to_int_word != "":
  22.                 int_line.append(float(to_int_word))
  23.         int_arr.append(int_line)
  24.     return int_arr
  25.  
  26.  
  27. class Shuttle:
  28.     M = 2150  # kg, mass of shuttle + mass of pilot
  29.     m = 1000  # kg, mass of fuel
  30.     a_limit = 29.43  # m/(s^2), 3g
  31.     Vp = 3660  # m/s, speed of fuel
  32.     lim_vx = 1
  33.     lim_vy = 3
  34.  
  35.  
  36.     def __init__(self, angle, delta_m, time):  # angle (degrees), fuel_consumption (kg / s), time (s)
  37.         self.delta_m = delta_m  # расход топлива
  38.         self.angle = angle  # начальный угол наклона (к оси OX) в радианах
  39.         self.time = time  # время, котоое нужно лететь в каком-то направлении
  40.         self.fuel = self.m  # чо по топливу
  41.         self.V = 0  # Общая скорость
  42.         self.y = 0  # актуальная y-координата
  43.         self.x = 0  # актуальная x - координата
  44.         self.all_time = 0  # время прошедшее с начала полёта (можно выпилить, но еды не просит)
  45.         self.Vx = 0  # Компоненты скорости
  46.         self.Vy = 0
  47.         self.Ax = 0  # компоненты ускорения
  48.         self.Ay = 0
  49.         self.A = 0  # само ускорение
  50.  
  51.     def update_time(self, time):  # обновим скорость
  52.         self.time = time
  53.  
  54.     def update_angle(self, angle):  # обновим угол
  55.         self.angle = angle
  56.  
  57.     def update_delta_m(self, delta_m):  # обновим скорость
  58.         self.delta_m = delta_m
  59.  
  60.     def update_all(self, angle, delta_m, time):  # обновим всё
  61.         self.update_time(time)
  62.         self.update_angle(angle)
  63.         self.update_delta_m(delta_m)
  64.         self.recount_parameters()  # надо пересчитать всё
  65.  
  66.     def update_speed(self):
  67.         self.V = self.get_speed()  # надо обновить отдельно и компоненты скорости
  68.  
  69.     def update_high(self):
  70.         self.y = self.get_coordinate_y()  # обновим высоту (нахуя?)
  71.  
  72.     def update_a(self):  # обновим общее ускорение
  73.         self.A = self.get_accelerate()
  74.  
  75.     def recount_parameters(self):  # обновим все данные спутника
  76.         # self.fuel -= (self.d_t * self.delta_m)
  77.         # self.y = self.get_coordinate_y()
  78.         # self.x = self.get_coordinate_x()
  79.         # self.V = self.get_speed()
  80.         # self.Vx = self.get_horizontal_speed()
  81.         # self.Vy = self.get_vertical_speed()
  82.         # self.Ax = self.get_horizontal_accelerate()
  83.         # self.Ay = self.get_vertical_accelerate()
  84.         # self.all_time += self.time
  85.  
  86.         self.Ax = self.get_horizontal_accelerate()
  87.         self.Ay = self.get_vertical_accelerate()
  88.         self.Vx = self.get_horizontal_speed()
  89.         self.Vy = self.get_vertical_speed()
  90.         self.x = self.get_coordinate_x()
  91.         self.y = self.get_coordinate_y()
  92.         self.fuel -= delta_m * self.time
  93.         self.all_time += self.time
  94.         self.V = self.get_speed()
  95.         self.A = self.get_accelerate()
  96.  
  97.     def get_coordinate_x(self):
  98.         x = self.x + self.Vx * self.time + self.Ax * self.time / 2
  99.         return x
  100.  
  101.     def get_coordinate_y(self):
  102.         y = self.y + self.Vy * self.time + self.Ay * self.time / 2
  103.         return y
  104.  
  105.     def check_g(self):
  106.         pass
  107.  
  108.     def check_fuel(self):
  109.         if self.time * self.delta_m <= self.fuel:
  110.             return True
  111.         return False
  112.  
  113.     def check_coordinate(self):
  114.         if self.y >= 0:
  115.             return True
  116.         return False
  117.  
  118.     def time_crash(self):
  119.         pass
  120.  
  121.     def low_fuel_time(self):
  122.         pass
  123.  
  124.     def is_soft_landing(self):
  125.         output_file.write(str(round(self.Vx)) + '\t\t' + str(round(self.Vy)) + '\t\t' + str(round(self.x)) +
  126.                           '\t\t' + str(round(self.y)) + '\n')
  127.         if abs(self.Vx) > self.lim_vx or abs(self.Vy) > self.lim_vy:
  128.             output_file.write("Landing failed!")
  129.         else:
  130.             output_file.write("Landing succeeded!")
  131.             output_file.close()
  132.  
  133.     def get_vertical_accelerate(self):
  134.         # a = Vp * sin(self.angle) * (1 / self.get_ksi()) * (-self.delta_m) - g
  135.         a = (math.sin(math.pi * self.angle / 180) * self.delta_m * self.Vp) / (self.M + self.fuel) - g
  136.         return a
  137.  
  138.     def get_horizontal_accelerate(self):
  139.         # a = Vp * cos(self.angle) * (1 / self.get_ksi()) * (-self.delta_m)
  140.         a = (math.cos(math.pi * self.angle / 180) * self.delta_m * self.Vp) / (self.M + self.fuel)
  141.         return a
  142.  
  143.     def get_accelerate(self):
  144.         Ax = self.get_horizontal_accelerate()
  145.         Ay = self.get_vertical_accelerate()
  146.         return sqrt(Ax * Ax + Ay * Ay)
  147.  
  148.     def get_vertical_speed(self):
  149.         # Vy = Vp * sin(self.angle) * log(self.get_ksi()) - g * self.time
  150.         Vy = self.Vy + self.Ay * self.time
  151.         return Vy
  152.  
  153.     def get_horizontal_speed(self):
  154.         # Vx = Vp * cos(self.angle) * log(self.get_ksi())
  155.         Vx = self.Vx + self.Ax * self.time
  156.         return Vx
  157.  
  158.     def get_speed(self):
  159.         Vx = self.get_horizontal_speed()
  160.         Vy = self.get_vertical_speed()
  161.         return sqrt(Vx * Vx + Vy * Vy)
  162.  
  163.     def step(self, angle, f_consumption, m_time):
  164.         for i in range(int(m_time / d_t)):
  165.             self.update_all(angle, f_consumption, d_t)
  166.             self.end()
  167.         tau = m_time - int(m_time / d_t) * d_t
  168.         self.update_all(angle, f_consumption, tau)
  169.         output_file.write(str(round(self.Vx)) + '\t\t' + str(round(self.Vy)) + '\t\t' + str(round(self.x)) + '\t\t'
  170.                      + str(round(self.y)) + '\n')
  171.  
  172.     def fall(self):
  173.         self.angle = 180 * math.atan2(self.Vy, self.Vx) / math.pi
  174.         output_file.write("FALL: ")
  175.         while self.y > 0:
  176.             self.update_all(self.angle, 0, d_t)
  177.             self.end()
  178.  
  179.  
  180.     def end(self):
  181.         if not space_shuttle.check_coordinate():
  182.             self.y = 0
  183.             space_shuttle.is_soft_landing()
  184.             exit(0)
  185.  
  186.  
  187. input_file_name = input("Enter file name: ")
  188. output_file_name = input("Enter output file name: ")
  189. output_file = open(output_file_name, 'w')
  190. space_shuttle = Shuttle(90, 0,
  191.                         0)  # инициализируем параметры аппарата на стартовой площадке (возможно есть смысл изменить угол к нормали)
  192.  
  193. task = input(
  194.     "Enter execution scenario, where \"1\" - flight simulation, \"2\" - calculation of the maximum flight distance: ")
  195.  
  196. if task == "1":
  197.     commands = read_commands(input_file_name)
  198.     for instruction in commands:
  199.         angle = instruction[0]
  200.         delta_m = instruction[1]
  201.         time = instruction[2]
  202.  
  203.        # f_consumption = delta_m / time
  204.         space_shuttle.step(angle, delta_m, time)
  205.  
  206.         space_shuttle.end()
  207.  
  208.     space_shuttle.fall()
  209.  
  210.  
  211. elif task == "2":
  212.     pass
  213. else:
  214.     output_file.write("Unexpected scenario")
  215. output_file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement