diff --git a/main.py b/main.py index e850eda..573325d 100644 --- a/main.py +++ b/main.py @@ -7,6 +7,8 @@ 创建日期: 2025-12-25 """ +import matplotlib +matplotlib.use('TkAgg') # 设置为TkAgg后端以支持图形窗口显示 import matplotlib.pyplot as plt import numpy as np from storage_optimization import optimize_storage_capacity, SystemParameters @@ -17,7 +19,7 @@ plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'DejaVu Sans'] plt.rcParams['axes.unicode_minus'] = False -def plot_system_curves(solar_output, wind_output, thermal_output, load_demand, result): +def plot_system_curves(solar_output, wind_output, thermal_output, load_demand, result, show_window=False, display_only=False): """ 绘制系统运行曲线 @@ -27,6 +29,8 @@ def plot_system_curves(solar_output, wind_output, thermal_output, load_demand, r thermal_output: 火电出力曲线 (MW) - 支持24小时或8760小时 load_demand: 负荷曲线 (MW) - 支持24小时或8760小时 result: 优化结果字典 + show_window: 是否显示图形窗口 + display_only: 是否只显示不保存文件 """ import matplotlib.pyplot as plt import numpy as np @@ -110,9 +114,26 @@ def plot_system_curves(solar_output, wind_output, thermal_output, load_demand, r # 调整布局 plt.tight_layout() - # 保存图片 - plt.savefig('system_curves.png', dpi=300, bbox_inches='tight') - plt.close() # 关闭图形,不显示窗口 + # 根据参数决定是否保存和显示图形 + if display_only: + # 只显示,不保存 + try: + plt.show() + except Exception as e: + print(f"无法显示图形窗口:{str(e)}") + else: + # 保存图片 + plt.savefig('system_curves.png', dpi=300, bbox_inches='tight') + + # 根据参数决定是否显示图形窗口 + if show_window: + try: + plt.show() + except Exception as e: + print(f"无法显示图形窗口:{str(e)}") + print("图形已保存为 'system_curves.png'") + else: + plt.close() # 关闭图形,不显示窗口 # 打印统计信息 print("\n=== 系统运行统计 ===") @@ -172,6 +193,8 @@ def main(): return command = sys.argv[1] + show_window = '--show' in sys.argv # 检查是否包含--show参数 + display_only = '--display-only' in sys.argv # 检查是否只显示不保存 if command == '--yearly': print("生成8760小时全年数据...") @@ -187,7 +210,7 @@ def main(): print(f"从Excel文件读取数据:{excel_file}") try: - data = read_excel_data(excel_file) + data = read_excel_data(excel_file, include_parameters=True) solar_output = data['solar_output'] wind_output = data['wind_output'] thermal_output = data['thermal_output'] @@ -197,6 +220,28 @@ def main(): print(f"原始数据长度:{data['original_length']}小时") print(f"处理后数据长度:{len(solar_output)}小时") + # 使用Excel中的系统参数 + if 'system_parameters' in data: + params = data['system_parameters'] + print("\n使用Excel中的系统参数:") + print(f" 最大弃风率: {params.max_curtailment_wind}") + print(f" 最大弃光率: {params.max_curtailment_solar}") + print(f" 最大上网电量比例: {params.max_grid_ratio}") + print(f" 储能效率: {params.storage_efficiency}") + print(f" 放电倍率: {params.discharge_rate}") + print(f" 充电倍率: {params.charge_rate}") + print(f" 最大储能容量: {params.max_storage_capacity}") + else: + print("\n警告:未找到系统参数,使用默认参数") + params = SystemParameters( + max_curtailment_wind=0.1, + max_curtailment_solar=0.1, + max_grid_ratio=0.2, + storage_efficiency=0.9, + discharge_rate=1.0, + charge_rate=1.0 + ) + # 显示数据统计 stats = analyze_excel_data(excel_file) if stats: @@ -225,17 +270,38 @@ def main(): thermal_output = [5.0] * 24 load_demand = [3.0, 4.0, 5.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0, 18.0, 20.0, 18.0, 16.0, 14.0, 12.0, 10.0, 8.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0, 2.0] - - -# 系统参数 - params = SystemParameters( - max_curtailment_wind=0.1, - max_curtailment_solar=0.1, - max_grid_ratio=0.2, - storage_efficiency=0.9, - discharge_rate=1.0, - charge_rate=1.0 - ) + + # 使用默认系统参数 + params = SystemParameters( + max_curtailment_wind=0.1, + max_curtailment_solar=0.1, + max_grid_ratio=0.2, + storage_efficiency=0.9, + discharge_rate=1.0, + charge_rate=1.0 + ) + + # 对于 --yearly 参数,也需要设置默认参数 + if command == '--yearly': + params = SystemParameters( + max_curtailment_wind=0.1, + max_curtailment_solar=0.1, + max_grid_ratio=0.2, + storage_efficiency=0.9, + discharge_rate=1.0, + charge_rate=1.0 + ) + + # 显示当前使用的系统参数 + print("\n=== 当前使用的系统参数 ===") + print(f"最大弃风率: {params.max_curtailment_wind}") + print(f"最大弃光率: {params.max_curtailment_solar}") + print(f"最大上网电量比例: {params.max_grid_ratio}") + print(f"储能效率: {params.storage_efficiency}") + print(f"放电倍率: {params.discharge_rate}") + print(f"充电倍率: {params.charge_rate}") + print(f"最大储能容量: {params.max_storage_capacity if params.max_storage_capacity is not None else '无限制'}") + print("=" * 40) # 计算最优储能容量 print("正在计算最优储能容量...") @@ -245,9 +311,14 @@ def main(): # 绘制曲线 print("正在绘制系统运行曲线...") - plot_system_curves(solar_output, wind_output, thermal_output, load_demand, result) + plot_system_curves(solar_output, wind_output, thermal_output, load_demand, result, show_window, display_only) - print("\n曲线图已保存为 'system_curves.png'") + if display_only: + print("\n正在显示图形窗口...") + elif show_window: + print("\n曲线图已保存为 'system_curves.png' 并显示图形窗口") + else: + print("\n曲线图已保存为 'system_curves.png'") def print_usage(): @@ -258,10 +329,15 @@ def print_usage(): print(" python main.py --yearly # 使用8760小时全年数据") print(" python main.py --create-template [类型] # 创建Excel模板(24或8760)") print(" python main.py # 使用24小时示例数据") + print(" python main.py --show # 显示图形窗口(可与其他参数组合使用)") + print(" python main.py --display-only # 只显示图形窗口,不保存文件") print("\n示例:") print(" python main.py --excel data.xlsx") + print(" python main.py --excel data.xlsx --show") + print(" python main.py --excel data.xlsx --display-only") print(" python main.py --create-template 8760") print(" python main.py --create-template 24") + print(" python main.py --display-only # 使用示例数据并只显示图形窗口") if __name__ == "__main__":