收敛段长度计算有误,已改正并改为PyQt5:
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QLineEdit, QPushButton, QVBoxLayout, QHBoxLayout, \ QWidget, QFileDialog, QMessageBox from PyQt5.QtCore import Qt from math import sqrt, tan, radians class RocketChamberDesign(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("推力室内弹道") self.setGeometry(100, 100, 600, 400) self.setStyleSheet(""" QMainWindow { background-color: #f0f0f0; } QLabel { font-size: 14px; color: #333333; } QLineEdit { font-size: 14px; border: 1px solid #cccccc; padding: 5px; background-color: white; border-radius: 5px; } QPushButton { font-size: 16px; padding: 10px 20px; border: none; background-color: #007acc; color: white; border-radius: 5px; } QPushButton:hover { background-color: #005fa3; } """) self.initUI() def initUI(self): central_widget = QWidget(self) main_layout = QVBoxLayout() central_widget.setLayout(main_layout) self.setCentralWidget(central_widget) # Labels and LineEdits self.create_input("质量流量(Kg/s):", main_layout) self.create_input("气体比热比(γ):", main_layout) self.create_input("燃室压力(MPa):", main_layout) self.create_input("燃室温度(K):", main_layout) self.create_input("气体常数(J/(Kg·K)):", main_layout) self.create_input("出口压力(MPa):", main_layout) self.create_input("特征长度(m):", main_layout) self.create_input("径收敛比(Dc/Dt):", main_layout) self.create_input("敛段半角(°):", main_layout) # Button layout button_layout = QHBoxLayout() calculate_button = QPushButton("计算", self) calculate_button.clicked.connect(self.calculate) button_layout.addWidget(calculate_button, alignment=Qt.AlignHCenter) save_button = QPushButton("保存", self) save_button.clicked.connect(self.save) button_layout.addWidget(save_button, alignment=Qt.AlignHCenter) # Add the button layout to the main layout main_layout.addLayout(button_layout) # Output labels and line edits output_layout = QVBoxLayout() self.create_output("燃烧室直径Dc(mm):", output_layout) self.create_output("圆筒段长度Lcyl(mm):", output_layout) self.create_output("喷喉直径Dt(mm):", output_layout) self.create_output("收敛段长度(mm):", output_layout) self.create_output("出口直径De(mm):", output_layout) self.create_output("膨胀比Ae/At:", output_layout) # Add the output layout to the main layout main_layout.addLayout(output_layout) def create_input(self, label_text, layout): input_layout = QHBoxLayout() label = QLabel(label_text, self) label.setAlignment(Qt.AlignRight | Qt.AlignVCenter) input_layout.addWidget(label) line_edit = QLineEdit(self) line_edit.setAlignment(Qt.AlignLeft) input_layout.addWidget(line_edit) layout.addLayout(input_layout) attr_name = self._format_attribute_name(label_text) setattr(self, attr_name, line_edit) print(f"Created attribute: {attr_name}") def create_output(self, label_text, layout): output_layout = QHBoxLayout() label = QLabel(label_text, self) label.setAlignment(Qt.AlignRight | Qt.AlignVCenter) output_layout.addWidget(label) line_edit = QLineEdit(self) line_edit.setReadOnly(True) output_layout.addWidget(line_edit) layout.addLayout(output_layout) attr_name = self._format_attribute_name(label_text) + '_output' setattr(self, attr_name, line_edit) print(f"Created attribute: {attr_name}") @staticmethod def _format_attribute_name(text): # Replace non-alphanumeric characters with underscores and remove leading numbers return ''.join([char if char.isalnum() else '_' for char in text]).lstrip('_') def calculate(self): try: gamma = float(getattr(self, self._format_attribute_name("比热比(γ):")).text()) mass_flow = float(getattr(self, self._format_attribute_name("质量流量(Kg/s):")).text()) pressure = float(getattr(self, self._format_attribute_name("压力(MPa):")).text()) temperature = float(getattr(self, self._format_attribute_name("温度(K):")).text()) gas_constant = float(getattr(self, self._format_attribute_name("气体常数(J/(Kg·K)):")).text()) pressure_export = float(getattr(self, self._format_attribute_name("出口压力(MPa):")).text()) characteristic_length = float(getattr(self, self._format_attribute_name("特征长度(m):")).text()) diameter_convergence_ratio_length = float( getattr(self, self._format_attribute_name("直径收敛比(Dc/Dt):")).text()) convergence_half_angle = float(getattr(self, self._format_attribute_name("敛段半角(°):")).text()) G = sqrt(gamma) * (2 / (gamma + 1)) ** ((gamma + 1) / (2 * (gamma - 1))) throat_area = mass_flow / (G * (pressure / sqrt(gas_constant * temperature))) compute_throat_diameter = sqrt(throat_area / 3.14159) * 2 expansion_area_ratio = ((2 / (gamma + 1)) ** (1 / (gamma - 1)) * ( (pressure / pressure_export) ** (1 / gamma))) / sqrt( ((gamma + 1) / (gamma - 1)) * (1 - ((pressure_export / pressure) ** ((gamma - 1) / gamma)))) compute_export_diameter = sqrt(throat_area * expansion_area_ratio / 3.14159) * 2 chamber_volume = throat_area * characteristic_length compute_chamber_radius = round(compute_throat_diameter * diameter_convergence_ratio_length + 0.4, 0) / 2 compute_convergence_length = (compute_chamber_radius * 2 - compute_throat_diameter) / ( 2 * tan(radians(convergence_half_angle))) chamber_volume -= float(compute_convergence_length) * 3.14159 * float(compute_chamber_radius ** 2 + (compute_throat_diameter / 2) ** 2 + compute_throat_diameter / 2 * compute_chamber_radius) / 3000 compute_cylinder_length = chamber_volume * 1000 / (compute_chamber_radius ** 2 * 3.14159) getattr(self, self._format_attribute_name("燃烧室直径Dc(mm):") + '_output').setText( f'{compute_chamber_radius * 2}') getattr(self, self._format_attribute_name("圆筒段长度Lcyl(mm):") + '_output').setText( f'{round(compute_cylinder_length + 0.4, 0)}') getattr(self, self._format_attribute_name("喷喉直径Dt(mm):") + '_output').setText( f'{round(compute_throat_diameter, 3)}') getattr(self, self._format_attribute_name("收敛段长度(mm):") + '_output').setText( f'{round(compute_convergence_length + 0.4, 0)}') getattr(self, self._format_attribute_name("出口直径De(mm):") + '_output').setText( f'{round(compute_export_diameter, 3)}') getattr(self, self._format_attribute_name("膨胀比Ae/At:") + '_output').setText( f'{round(expansion_area_ratio, 3)}') except Exception as e: QMessageBox.critical(self, "错误", f"计算过程中发生错误:\n{str(e)}") print(e) def save(self): filename, _ = QFileDialog.getSaveFileName(self, "保存文件", "", "Text Files (*.txt)") if filename: with open(filename, 'w', encoding="utf-8") as file: file.write("推力室设计参数:\n") file.write(f"燃烧室直径Dc(mm):{getattr(self, '燃烧室直径Dc_mm___output').text()}\n") file.write(f"圆筒段长度Lcyl(mm):{getattr(self, '圆筒段长度Lcyl_mm___output').text()}\n") file.write(f"喷喉直径Dt(mm):{getattr(self, '喷喉直径Dt_mm___output').text()}\n") file.write(f"收敛段长度(mm):{getattr(self, '收敛段长度_mm___output').text()}\n") file.write(f"出口直径De(mm):{getattr(self, '出口直径De_mm___output').text()}\n") file.write(f"膨胀比Ae/At:{getattr(self, '膨胀比Ae_At__output').text()}") if __name__ == '__main__': import sys app = QApplication(sys.argv) main_window = RocketChamberDesign() main_window.show() app.exec_() app.quit()
200字以内,仅用于支线交流,主线讨论请采用回复功能。