Files
2025-12-30 19:10:49 +08:00

190 lines
6.9 KiB
Python

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np
from core_model import AeroEngineDLL
import os
# ==========================================
# 范围测试示例
# ==========================================
def run_simulation():
print("=== 开始发动机控制量范围测试 (优化版) ===")
# 1. 初始化模型
dll_path = os.path.join(os.path.dirname(__file__), "libEngine.dll")
if not os.path.exists(dll_path):
dll_path = os.path.join(os.path.dirname(__file__), "..", "libEngine.dll")
sim = AeroEngineDLL(dll_path)
out = sim.reset()
# 初始状态
current_wf = 100.0
current_add_power = 90000.0
current_a8 = 0.1
current_fan_vane = 0.0
current_comp_vane = 0.0
# 数据记录
data = {
'time': [], 'nh': [], 'nl': [], 't5': [], 'p3': [], 'wf': [],
'a8': [], 'fan_vane': [], 'comp_vane': []
}
# 辅助记录函数
def record(t, o, wf, a8, fv, cv):
data['time'].append(t)
data['nh'].append(o.NH)
data['nl'].append(o.NL)
data['t5'].append(o.T5t)
data['p3'].append(o.P3s)
data['wf'].append(wf)
data['a8'].append(a8)
data['fan_vane'].append(fv)
data['comp_vane'].append(cv)
record(0.0, out, current_wf, current_a8, current_fan_vane, current_comp_vane)
total_time = 0.0
dt = 0.02
# ==========================================
# Phase 1: 起动与稳态 (0 - 20s)
# ==========================================
print("Phase 1: 起动与稳态 (0-20s)...")
while total_time < 20.0:
if out.NH > 0.25 and current_add_power > 0:
current_add_power = 0.0
current_wf = 2000.0 # 提高稳态燃油至 2000 (原 1000) 以测试高工况下的导叶效果
action = {
'Wf': current_wf, 'AddPower': current_add_power, 'A8': current_a8,
'FanVane': current_fan_vane, 'CompVane': current_comp_vane
}
out = sim.step(action)
total_time += dt
record(total_time, out, current_wf, current_a8, current_fan_vane, current_comp_vane)
# ==========================================
# Phase 2: A8 扫描 (Step Response)
# A8: 0.01 -> 1.0
# ==========================================
print("Phase 2: 测试 A8 阶跃变化 (0.01 -> 1.0)...")
steps_per_point = 50 # 1秒 per point
a8_targets = np.linspace(0.01, 1.0, 40)
for val in a8_targets:
current_a8 = val
for _ in range(steps_per_point):
action = {'Wf': current_wf, 'AddPower': 0.0, 'A8': current_a8, 'FanVane': current_fan_vane, 'CompVane': current_comp_vane}
out = sim.step(action)
total_time += dt
record(total_time, out, current_wf, current_a8, current_fan_vane, current_comp_vane)
# 恢复 A8 并稳定
current_a8 = 0.1
print("Restabilizing A8...")
for _ in range(100): # 2秒
action = {'Wf': current_wf, 'AddPower': 0.0, 'A8': current_a8, 'FanVane': current_fan_vane, 'CompVane': current_comp_vane}
out = sim.step(action)
total_time += dt
record(total_time, out, current_wf, current_a8, current_fan_vane, current_comp_vane)
# ==========================================
# Phase 3: Fan Vane 扫描 (Step Response)
# Range: -20 -> +20 度
# ==========================================
print("Phase 3: 测试 Fan Vane 阶跃变化 (-20 -> 20)...")
vane_targets = np.linspace(-20.0, 20.0, 40)
for val in vane_targets:
current_fan_vane = val
for _ in range(steps_per_point):
action = {'Wf': current_wf, 'AddPower': 0.0, 'A8': current_a8, 'FanVane': current_fan_vane, 'CompVane': current_comp_vane}
out = sim.step(action)
total_time += dt
record(total_time, out, current_wf, current_a8, current_fan_vane, current_comp_vane)
# 恢复 Fan Vane 并稳定
current_fan_vane = 0.0
print("Restabilizing Fan Vane...")
for _ in range(100):
action = {'Wf': current_wf, 'AddPower': 0.0, 'A8': current_a8, 'FanVane': current_fan_vane, 'CompVane': current_comp_vane}
out = sim.step(action)
total_time += dt
record(total_time, out, current_wf, current_a8, current_fan_vane, current_comp_vane)
# ==========================================
# Phase 4: Comp Vane 扫描 (Step Response)
# Range: -20 -> +20 度
# ==========================================
print("Phase 4: 测试 Comp Vane 阶跃变化 (-20 -> 20)...")
for val in vane_targets:
current_comp_vane = val
for _ in range(steps_per_point):
action = {'Wf': current_wf, 'AddPower': 0.0, 'A8': current_a8, 'FanVane': current_fan_vane, 'CompVane': current_comp_vane}
out = sim.step(action)
total_time += dt
record(total_time, out, current_wf, current_a8, current_fan_vane, current_comp_vane)
sim.close()
print("测试结束,正在绘图...")
plot_range_test(data)
def plot_range_test(data):
t = data['time']
plt.rcParams['font.family'] = 'sans-serif'
fig, axes = plt.subplots(5, 1, figsize=(12, 16), sharex=True)
# 1. Speed (NH & NL)
axes[0].plot(t, data['nh'], color='blue', label='NH')
axes[0].plot(t, data['nl'], color='cyan', linestyle='--', label='NL')
axes[0].set_ylabel('Speed (%)')
axes[0].grid(True, linestyle='--', alpha=0.7)
axes[0].legend(loc='upper right')
axes[0].set_title('Engine Response to Control Inputs')
# 2. T5
axes[1].plot(t, data['t5'], color='red', label='T5')
axes[1].set_ylabel('T5 (K)')
axes[1].grid(True, linestyle='--', alpha=0.7)
axes[1].legend(loc='upper right')
# 3. P3
axes[2].plot(t, data['p3'], color='orange', label='P3')
axes[2].set_ylabel('P3 (kPa)')
axes[2].grid(True, linestyle='--', alpha=0.7)
axes[2].legend(loc='upper right')
# 4. A8 Input
axes[3].plot(t, data['a8'], color='green', label='A8 Input')
axes[3].set_ylabel('A8 (m^2)')
axes[3].grid(True, linestyle='--', alpha=0.7)
axes[3].legend(loc='upper right')
axes[3].yaxis.set_major_locator(ticker.MaxNLocator(nbins=5))
axes[3].minorticks_on()
axes[3].grid(which='minor', linestyle=':', alpha=0.4)
# 5. Vane Inputs
axes[4].plot(t, data['fan_vane'], color='purple', label='Fan Vane')
axes[4].plot(t, data['comp_vane'], color='brown', label='Comp Vane')
axes[4].set_ylabel('Vane Angle (deg)')
axes[4].set_xlabel('Time (s)')
axes[4].grid(True, linestyle='--', alpha=0.7)
axes[4].legend(loc='upper right')
axes[4].yaxis.set_major_locator(ticker.MaxNLocator(nbins=5))
axes[4].minorticks_on()
axes[4].grid(which='minor', linestyle=':', alpha=0.4)
plt.tight_layout()
save_path = os.path.join(os.path.dirname(__file__), 'range_test_result.png')
plt.savefig(save_path)
print(f"结果图已保存至: {save_path}")
plt.show()
if __name__ == "__main__":
run_simulation()