""" 测试周期性平衡功能 """ import sys import os # 添加src目录到路径 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'src')) from storage_optimization import optimize_storage_capacity, SystemParameters def test_24hour_data(): """测试24小时数据(不需要周期性平衡)""" print("=" * 60) print("测试24小时数据(不需要周期性平衡)") print("=" * 60) # 示例数据 solar_output = [0.0] * 6 + [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0, 0.0] + [0.0] * 6 wind_output = [2.0, 3.0, 4.0, 3.0, 2.0, 1.0] * 4 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 ) # 计算最优储能容量 result = optimize_storage_capacity( solar_output, wind_output, thermal_output, load_demand, params ) print(f"\n=== 24小时优化结果 ===") print(f"所需储能总容量: {result['required_storage_capacity']:.2f} MWh") print(f"初始SOC: {result['storage_profile'][0]:.4f} MWh") print(f"最终SOC: {result['storage_profile'][-1]:.4f} MWh") print(f"SOC差值: {abs(result['storage_profile'][-1] - result['storage_profile'][0]):.4f} MWh") print(f"实际弃风率: {result['total_curtailment_wind_ratio']:.3f}") print(f"实际弃光率: {result['total_curtailment_solar_ratio']:.3f}") print(f"实际上网电量比例: {result['total_grid_feed_in_ratio']:.3f}") print(f"能量平衡校验: {'通过' if result['energy_balance_check'] else '未通过'}") def test_8760hour_data(): """测试8760小时数据(需要周期性平衡)""" print("\n" + "=" * 60) print("测试8760小时数据(需要周期性平衡)") print("=" * 60) # 生成8760小时示例数据(简化版本) import numpy as np # 使用重复的24小时模式生成8760小时数据 daily_solar = [0.0] * 6 + [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0, 0.0] + [0.0] * 6 daily_wind = [2.0, 3.0, 4.0, 3.0, 2.0, 1.0] * 4 daily_thermal = [5.0] * 24 daily_load = [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] # 添加季节性变化 np.random.seed(42) solar_output = [] wind_output = [] thermal_output = [] load_demand = [] for day in range(365): # 季节性因子 season_factor = 1.0 + 0.3 * np.sin(2 * np.pi * day / 365) for hour in range(24): # 添加随机变化 solar_variation = 1.0 + 0.2 * (np.random.random() - 0.5) wind_variation = 1.0 + 0.3 * (np.random.random() - 0.5) load_variation = 1.0 + 0.1 * (np.random.random() - 0.5) solar_output.append(daily_solar[hour] * season_factor * solar_variation) wind_output.append(daily_wind[hour] * wind_variation) thermal_output.append(daily_thermal[hour]) load_demand.append(daily_load[hour] * (2.0 - season_factor) * load_variation) # 系统参数 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, max_storage_capacity=1000.0 # 设置储能容量上限以加快测试 ) # 计算最优储能容量 result = optimize_storage_capacity( solar_output, wind_output, thermal_output, load_demand, params ) print(f"\n=== 8760小时优化结果 ===") print(f"所需储能总容量: {result['required_storage_capacity']:.2f} MWh") print(f"初始SOC: {result['storage_profile'][0]:.4f} MWh") print(f"最终SOC: {result['storage_profile'][-1]:.4f} MWh") print(f"SOC差值: {abs(result['storage_profile'][-1] - result['storage_profile'][0]):.4f} MWh") print(f"实际弃风率: {result['total_curtailment_wind_ratio']:.3f}") print(f"实际弃光率: {result['total_curtailment_solar_ratio']:.3f}") print(f"实际上网电量比例: {result['total_grid_feed_in_ratio']:.3f}") print(f"能量平衡校验: {'通过' if result['energy_balance_check'] else '未通过'}") # 验证周期性平衡 soc_diff = abs(result['storage_profile'][-1] - result['storage_profile'][0]) capacity = result['required_storage_capacity'] soc_convergence_threshold = capacity * 0.001 # 0.1%阈值 if soc_diff < soc_convergence_threshold: print(f"\n✓ 周期性平衡验证通过") print(f" SOC差值: {soc_diff:.4f} MWh < 阈值: {soc_convergence_threshold:.4f} MWh") else: print(f"\n⚠ 周期性平衡验证未通过") print(f" SOC差值: {soc_diff:.4f} MWh >= 阈值: {soc_convergence_threshold:.4f} MWh") if __name__ == "__main__": test_24hour_data() test_8760hour_data() print("\n" + "=" * 60) print("所有测试完成") print("=" * 60)