diff --git a/src/KSP_tools/stage_performance_plot.py b/src/KSP_tools/stage_performance_plot.py index 5368c5f..3ef6bde 100644 --- a/src/KSP_tools/stage_performance_plot.py +++ b/src/KSP_tools/stage_performance_plot.py @@ -8,14 +8,13 @@ def plot_stage_performance(isp, gross_mass, dry_mass, payload, stage_name): plt.plot(payload, stage_performance, label=stage_name) -payload_list = [i for i in range(0, 40200, 200)] -plot_stage_performance(450.5, 20830 + 2247, 2247, payload_list, 'Centaur III') +payload_list = [i for i in range(0, 15200, 200)] +# plot_stage_performance(450.5, 20830 + 2247, 2247, payload_list, 'Centaur III') # plot_stage_performance(438, 21000, 2800, payload_list, 'Long March 3m') # plot_stage_performance(450, 23500 + 2900, 2900, payload_list, 'Long March 3.35m') plot_stage_performance(453.8, 54000+4500, 5000, payload_list, 'Centaur V') plot_stage_performance(348, 112000, 4000, payload_list, 'Falcon 9') -plot_stage_performance(355, 80600, 600, payload_list, 'Neutron S2') -plot_stage_performance(365, 100600, 600, payload_list, 'Neutron S2 MAX') +plot_stage_performance(367, 82000, 2000, payload_list, 'Neutron S2') # plot_stage_performance(451, 68000, 8000, payload_list, 'Long March 10 S3') # plot_stage_performance(442.6, 36000, 5100, payload_list, 'Long March 5 S2') # plot_stage_performance(462, 30710, 3490, payload_list, 'DCSS 5m') diff --git a/src/KSP_tools/tube_drag_cube_generator.py b/src/KSP_tools/tube_drag_cube_generator.py new file mode 100644 index 0000000..fc8ae69 --- /dev/null +++ b/src/KSP_tools/tube_drag_cube_generator.py @@ -0,0 +1,81 @@ +from matplotlib import pyplot as plt, pyplot + + +def interpolate_y(x1, y1, x2, y2, x): + if x1 == x2: + raise ValueError("x1 and x2 cannot be the same value, as the line would be undefined.") + + # 计算插值的y值 + y = y1 + (y2 - y1) * (x - x1) / (x2 - x1) + return y + + +def cal_num_6(size): + if size < 2.5: + return interpolate_y(1.875, 2.363, 2.5, 4.87, size) + elif 2.5 <= size < 3.75: + return interpolate_y(3.75, 11, 2.5, 4.87, size) + elif 3.75 <= size: + return interpolate_y(3.75, 11, 5, 19.48, size) + + +def cal_cube_parameter(size, height): + + # size = 5 + # height = 1.25 + + num0 = size * height + num1 = 0.7778 + num2 = size / 2 + 0.03 + + num3 = num0 + num4 = num1 + num5 = num2 + + num6 = cal_num_6(size) + num7 = 0.9796 + num8 = interpolate_y(1.875, 0.1449, 5, 0.2207, size) + + num9 = cal_num_6(size) + num10 = 0.9796 + num11 = interpolate_y(1.875, 0.1449, 5, 0.2207, size) + + num12 = num0 + num13 = 0.7776 + num14 = num2 + + num15 = num0 + num16 = 0.7776 + num17 = num2 + + num18 = 0 + num19 = -0.5 * height + num20 = 0 + + num21 = size + num22 = height + num23 = size + + result = [num0, num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11, num12, num13, num14, num15, + num16, num17, num18, num19, num21, num22, num23] + return result + + +def print_cube_content(size, height_list): + print(' DRAG_CUBE') + print(' {') + for i in range(len(height_list)): + print(f' cube = {i}, ', end='') + print(','.join([str(round(item, 5)) for item in cal_cube_parameter(size, height_list[i])])) + print(' }') + + + +if __name__ == '__main__': + # sample_data_list = [6.25, 0.7781, 2.529, 6.25, 0.7781, 2.529, 19.48, 0.9796, 0.2207, 19.48, 0.9796, 0.2207, 6.25, + # 0.7776, 2.529, 6.25, 0.7776, 2.529, 0, -0.625, 0, 5, 1.25, 5] + height_list = [1.875, 2.5, 3.75, 5] + # new_table = cal_cube_parameter(3.35, 5) + # print(sample_data_list) + # print(new_table) + print_cube_content(5.4, height_list) diff --git a/src/KSP_tools/vessel_simulation_advanced.py b/src/KSP_tools/vessel_simulation_advanced.py index 9f581da..10c1024 100644 --- a/src/KSP_tools/vessel_simulation_advanced.py +++ b/src/KSP_tools/vessel_simulation_advanced.py @@ -1,15 +1,17 @@ import math import load_common_resources import copy +import time + from matplotlib import pyplot as plt +from tqdm import tqdm Vc = 299792458 Ga = 9.80665 -path = 'CommonResources.cfg' +path = 'data/CommonResources.cfg' common_resources_dict = load_common_resources.load_common_resources(path) - fuel_density_map = { 'methalox': common_resources_dict['LqdMethane']['density'] * 0.4137 + common_resources_dict['LqdOxygen'][ 'density'] * 0.5863, @@ -72,7 +74,8 @@ class Stage: for i in range(len(curve)): try: if curve_keys[i] <= stage_time < curve_keys[i + 1]: - throttle = curve[curve_keys[i]] + (curve[curve_keys[i+1]] - curve[curve_keys[i]]) / (curve_keys[i+1] - curve_keys[i]) * (stage_time - curve_keys[i]) + throttle = curve[curve_keys[i]] + (curve[curve_keys[i + 1]] - curve[curve_keys[i]]) / ( + curve_keys[i + 1] - curve_keys[i]) * (stage_time - curve_keys[i]) break except IndexError: throttle = curve[curve_keys[-1]] @@ -83,7 +86,7 @@ class Stage: try: if curve_keys[i] > flag >= curve_keys[i + 1]: throttle = curve[curve_keys[i]] + (curve[curve_keys[i + 1]] - curve[curve_keys[i]]) / ( - curve_keys[i + 1] - curve_keys[i]) * (flag - curve_keys[i]) + curve_keys[i + 1] - curve_keys[i]) * (flag - curve_keys[i]) break except IndexError: throttle = curve[curve_keys[-1]] @@ -95,7 +98,7 @@ class Stage: self.current_mass = self.fuel_mass + self.dry_mass self.current_fuel_mass = self.fuel_mass - def simulated_burn_step_by_step(self, stage_time, duration): + def simulated_burn_step_by_step(self, stage_time, duration, print_status): current_thrust = self.cal_current_thrust(stage_time) now_thrust = current_thrust if self.is_empty: @@ -103,7 +106,8 @@ class Stage: next_fuel_burned = cal_fuel_burned(self.isp, now_thrust, duration) if self.current_fuel_mass - next_fuel_burned < 0: self.is_empty = True - print(f'Part: {self.name} is depleted after {round(stage_time, 3)}s.') + if print_status: + print(f'Part: {self.name} is depleted after {round(stage_time, 3)}s.') else: self.current_fuel_mass -= next_fuel_burned self.current_mass = self.current_fuel_mass + self.dry_mass @@ -116,6 +120,7 @@ class Vessel: self.duration = duration self.payload = payload self.name = name + self.print_status = True def reset_simulation(self): for stages in self.stage_list: @@ -154,29 +159,29 @@ class Vessel: def stage_burn(self, stages, stage_time): result = [] for stage in stages: - stage_is_empty, stage_mass = stage.simulated_burn_step_by_step(stage_time, self.duration) + stage_is_empty, stage_mass = stage.simulated_burn_step_by_step(stage_time, self.duration, self.print_status) result.append((stage_is_empty, stage_mass)) return result - @staticmethod - def stage_info(stages): + def stage_info(self, stages): stage_thrust, stage_fuel, gross_mass, dry_mass = 0, 0, 0, 0 for stage in stages: stage_thrust += stage.max_thrust stage_fuel += stage.fuel_mass gross_mass += stage.total_mass dry_mass += stage.dry_mass - print(f' Total Vacuum Thrust: {stage_thrust}KN, Burned Mass: {stage_fuel/1000}ton, ' - f'Gross Mass: {gross_mass/1000}ton, ' - f'Dry Mass: {dry_mass/1000}ton') + self.vessel_print(f' Total Vacuum Thrust: {stage_thrust}KN, Burned Mass: {stage_fuel / 1000}ton, ' + f'Gross Mass: {gross_mass / 1000}ton, ' + f'Dry Mass: {dry_mass / 1000}ton') def run_sim_new(self): self.vessel_info() self.reset_simulation() - print('Start Simulation') + self.vessel_print('Start Simulation') stage_burn_time, timer, dv, stage_dv = 0, 0, 0, 0 now_stage_id = 0 stage_acc_result = {} + start = time.time() # time based simulation while True: @@ -197,25 +202,48 @@ class Vessel: stage_dv += step_dv if self.is_depleted(now_stage): - print(f'stage{now_stage_id}:\n burn time: {round(stage_burn_time, 3)}s, dv: {round(stage_dv, 3)}m/s ') + self.vessel_print( + f'stage{now_stage_id}:\n burn time: {round(stage_burn_time, 3)}s, dv: {round(stage_dv, 3)}m/s ') start_twr = round(stage_acc_result[f'stage{now_stage_id}'][0], 3) end_twr = round(stage_acc_result[f'stage{now_stage_id}'][-2], 3) max_twr = round(max(stage_acc_result[f'stage{now_stage_id}']), 3) - print(f' start twr: {start_twr} end twr: {end_twr} max_twr: {max_twr}') + self.vessel_print(f' start twr: {start_twr} end twr: {end_twr} max_twr: {max_twr}') self.stage_info(now_stage) stage_burn_time, stage_dv = 0, 0 now_stage_id += 1 timer = round(timer, 3) dv = round(dv, 3) - print(f'Total Engine Burn Time: {timer}s, Total dv: {dv}m/s') + self.vessel_print( + f'Simulation completed. Total Engine Burn Time: {timer}s, Total dv: {dv}m/s. Time: {round(time.time() - start, 3)}') + return dv + + def plot_sim(self, start_payload=500, step=200): + self.print_status = False + total_payload = self.payload + dv_list = [] + for payload in tqdm(range(start_payload, total_payload, step)): + self.payload = payload + dv_list.append(self.run_sim_new()) + plt.plot([i for i in range(start_payload, total_payload, step)], dv_list) + plt.axhline(y=9600, color='r') + plt.xlabel('payload mass in kg') + plt.ylabel('delta-v in m/s') + plt.show() + self.print_status = True + return def vessel_info(self): lift_off_mass = self.get_current_mass() / 1000 lift_off_thrust = self.get_current_thrust() - print(f'Info of {self.name}: ') - print(f' Vessel Lift-off Mass: {lift_off_mass} ton, Vessel Lift-off Vacuum Thrust: {lift_off_thrust} KN') - print(f' Payload Mass: {self.payload} KG') - print('-'*80) + self.vessel_print(f'Info of {self.name}: ') + self.vessel_print( + f' Vessel Lift-off Mass: {lift_off_mass} ton, Vessel Lift-off Vacuum Thrust: {lift_off_thrust} KN') + self.vessel_print(f' Payload Mass: {self.payload} KG') + self.vessel_print('-' * 80) + + def vessel_print(self, content): + if self.print_status: + print(content) if __name__ == '__main__': @@ -241,27 +269,34 @@ if __name__ == '__main__': # # cz_10 = Vessel([[cbc, S1], [S2]], name='CZ-10', payload=70000) # cz_10.run_sim_new() - booster = Stage(1040000, 100000, 1397*14, 338.2, 'kerolox', 'Side Booster') - S1 = Stage(680000, 60000, 1397*7, 338.2, 'kerolox', 'Stage 1') - S2 = Stage(185000, 20000, 2900, 352.3, 'kerolox', 'Stage 2') - S3 = Stage(58500, 11500, 276.324, 451, 'hydrolox', 'Stage 3') - cz_10 = Vessel([[booster, S1], [S2], [S3]], name='CZ-10', payload=27000) - cz_10.run_sim_new() + # booster = Stage(1040000, 100000, 1397*14, 338.2, 'kerolox', 'Side Booster') + # S1 = Stage(680000, 60000, 1397*7, 338.2, 'kerolox', 'Stage 1') + # S2 = Stage(185000, 20000, 2900, 352.3, 'kerolox', 'Stage 2') + # S3 = Stage(58500, 11500, 276.324, 451, 'hydrolox', 'Stage 3') + # cz_10 = Vessel([[booster, S1], [S2], [S3]], name='CZ-10', payload=27000) + # cz_10.run_sim_new() # S1 = Stage(339000, 30000, 1397*4, 338.2, 'kerolox', 'Stage 1') # S2 = Stage(48000, 6000, 360, 341.2, 'kerolox', 'Stage 2') # cz_12 = Vessel([[S1], [S2]], name='CZ-12', payload=10000) # cz_12.run_sim_new() - booster = Stage(72000*4, 7500*4, 1340*4, 335, 'kerolox', 'CZ-7A Booster') - Stage1 = Stage(144000, 15000, 1340*2, 335, 'kerolox', 'CZ-7A Stage1') - Stage2 = Stage(61500, 8000, 180*4, 342, 'kerolox', 'CZ-7A Stage2') - Stage3 = Stage(18250, 3050, 83*2, 438, 'hydrolox', 'CZ-7A Stage3') + # booster = Stage(72000*4, 7500*4, 1340*4, 335, 'kerolox', 'CZ-7A Booster') + # Stage1 = Stage(144000, 15000, 1340*2, 335, 'kerolox', 'CZ-7A Stage1') + # Stage2 = Stage(61500, 8000, 180*4, 342, 'kerolox', 'CZ-7A Stage2') + # Stage3 = Stage(18250, 3050, 83*2, 438, 'hydrolox', 'CZ-7A Stage3') + # + # cz_7a = Vessel([[booster, Stage1], [Stage2], [Stage3]], name='CZ-7A', payload=8000) + # cz_7a.run_sim_new() - cz_7a = Vessel([[booster, Stage1], [Stage2], [Stage3]], name='CZ-7A', payload=8000) - cz_7a.run_sim_new() + # S1 = Stage(335000, 48000, 7116.12, 329, 'methalox', 'Stage 1') + # S2 = Stage(80000, 2000, 890, 367, 'methalox', 'Stage 2') + # Neutron = Vessel([[S1], [S2]], name='Neutron', payload=15000, duration=0.05) + # Neutron.run_sim_new() + # Neutron.plot_sim(start_payload=13000, step=100) + S1 = Stage(12000, 2500, 80, 335, 'kerolox', 'Service Module') + S2 = Stage(7000, 3800, 50, 330, 'kerolox', 'Ascent Stage') + Lanyue = Vessel([[S1], [S2]], name='Lanyue', payload=200, duration=0.05) + Lanyue.run_sim_new() print('done') - - - diff --git a/src/tools/auto_safe.py b/src/tools/auto_safe.py index 0d3b6b4..9170157 100644 --- a/src/tools/auto_safe.py +++ b/src/tools/auto_safe.py @@ -1,31 +1,6 @@ import time from ctypes import * -from tcping import Ping - - -def ping_ip(ip_address, request_nums): - """ - ping ip - :param ip_address: - :param request_nums: 请求次数 - :return: 丢包率loss和统计结果res - """ - ping = Ping(ip_address, 2077, 3) - ping.ping(request_nums) - res = ping.result.table - ret = ping.result.raw - return_list = list(ret.split('\n')) - loss = return_list[2].split(',')[3].split(' ')[1] # 获取丢包率 - return loss, res - - -def ping_process(host): - # 调用ping_ip方法得到丢包率 - loss, res = ping_ip(host, 3) - if float(loss.strip('%')) / 100 <= 0.1: # 0.1为自定义丢包率阈值,可修改 - print("ping 不通") - else: - print("ping 通") +from src.tools.ping_ip import ping def shut_screen_and_lock(): @@ -55,8 +30,8 @@ class AutoSafe: self.start_monitor = True def check_connection(self) -> bool: - loss, res = ping_ip(self.target_server, 5) - if float(loss.strip('%')) / 100 <= 0.1: + success_rate = ping(self.target_server, 2077, 5, 10) + if success_rate <= 0.3: return False else: return True @@ -65,9 +40,12 @@ class AutoSafe: print("Starting AutoSafe") while self.start_monitor: time.sleep(20) + print('checking:', end=' ') if is_locked() or self.check_connection(): + print('ok @', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) continue else: + print('failed @', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) self.safe() def safe(self):