Files
multi_energy_complementarity/tests/test_zero_parameters.py
2025-12-27 19:15:56 +08:00

101 lines
4.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""
测试Excel中参数为0时的行为
验证当Excel中的火电可用发电量为0时系统使用0而不是默认值
"""
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'src'))
import pandas as pd
from excel_reader import create_excel_template, read_excel_data, read_system_parameters
from storage_optimization import optimize_storage_capacity, SystemParameters
def test_zero_parameters():
"""测试参数为0时的行为"""
print("=== 测试Excel中参数为0时的行为 ===")
# 创建一个测试Excel文件其中火电可用发电量为0
test_file = "test_zero_parameters.xlsx"
# 创建基本模板
create_excel_template(test_file, "24")
# 修改参数工作表将火电可用发电量设为0
df_params = pd.read_excel(test_file, sheet_name='参数')
# 找到火电可用发电量行并修改为0
for idx, row in df_params.iterrows():
if row['参数名称'] == '火电可用发电量':
df_params.at[idx, '参数值'] = 0.0
break
# 保存修改后的Excel文件
with pd.ExcelWriter(test_file, mode='a', engine='openpyxl', if_sheet_exists='replace') as writer:
df_params.to_excel(writer, sheet_name='参数', index=False)
print(f"创建测试Excel文件: {test_file}")
print("将火电可用发电量设置为0")
# 读取参数
print("\n读取系统参数:")
try:
params = read_system_parameters(test_file)
print(f" 火电可用发电量: {params.available_thermal_energy} MWh")
print(f" 光伏可用发电量: {params.available_solar_energy} MWh")
print(f" 风电可用发电量: {params.available_wind_energy} MWh")
# 验证火电可用发电量是否为0
if params.available_thermal_energy == 0.0:
print(" [OK] 火电可用发电量正确设置为0")
else:
print(f" [ERROR] 火电可用发电量应该为0但实际为{params.available_thermal_energy}")
except Exception as e:
print(f" [ERROR] 读取参数失败: {str(e)}")
return
# 测试系统运行
print("\n测试系统运行:")
try:
data = read_excel_data(test_file, include_parameters=True)
solar_output = data['solar_output']
wind_output = data['wind_output']
thermal_output = data['thermal_output']
load_demand = data['load_demand']
# 运行优化
result = optimize_storage_capacity(
solar_output, wind_output, thermal_output, load_demand, params
)
print(f" 所需储能容量: {result['required_storage_capacity']:.2f} MWh")
print(f" 上网电量: {sum(x for x in result['grid_feed_in'] if x > 0):.2f} MWh")
print(f" 弃风量: {sum(result['curtailed_wind']):.2f} MWh")
print(f" 弃光量: {sum(result['curtailed_solar']):.2f} MWh")
# 验证上网上限计算(不应包含火电)
total_available_energy = params.available_solar_energy + params.available_wind_energy
expected_max_grid_feed_in = total_available_energy * params.max_grid_ratio
actual_grid_feed_in = sum(x for x in result['grid_feed_in'] if x > 0)
print(f"\n上网电量验证:")
print(f" 可用发电量(不计火电): {total_available_energy:.2f} MWh")
print(f" 最大上网比例: {params.max_grid_ratio}")
print(f" 期望上网电量上限: {expected_max_grid_feed_in:.2f} MWh")
print(f" 实际上网电量: {actual_grid_feed_in:.2f} MWh")
if abs(actual_grid_feed_in - expected_max_grid_feed_in) < 1.0: # 允许1MW误差
print(" [OK] 上网电量计算正确(不计火电)")
else:
print(" [WARNING] 上网电量计算可能有误")
except Exception as e:
print(f" [ERROR] 系统运行失败: {str(e)}")
print("\n=== 测试完成 ===")
if __name__ == "__main__":
test_zero_parameters()