Compare commits

...

1 Commits
0.0.7 ... main

Author SHA1 Message Date
Hanzhang ma
f1b2959143 add English comments 2024-05-17 10:48:07 +02:00
3 changed files with 93 additions and 65 deletions

View File

@ -37,10 +37,25 @@ class EnergySystem:
def get_cost(self):
return self.ess.get_cost()+self.pv.get_cost()
# 优先使用PV供电给工厂 - 如果PV输出能满足工厂的需求则直接供电多余的电能用来给ESS充电。
# PV不足时使用ESS补充 - 如果PV输出不足以满足工厂需求首先从ESS获取所需电量。
# 如果ESS也不足以满足需求再从电网获取 - 当ESS中的存储电量也不足以补充时再从电网购买剩余所需电量。
def simulate(self, data, time_interval):
"""
The program will use the PV to supply the factory first. If the PV output can meet the factory's demand, it will be directly powered, and the excess electrical energy will be used to charge the ESS. Program will use the PV to supply the Ess.
When the PV is insufficient, the ESS is used to supplement. If the PV output is not enough to meet the factory's demand, the required power is first obtained from the ESS.
If the ESS is also insufficient to meet the demand, it will be obtained from the grid. When the stored power in the ESS is also insufficient to supplement, the remaining required power will be purchased from the grid.
Args:
data: pandas.DataFrame
The data that contains the factory's demand, PV output, and electricity price.
time_interval: float
The time interval of the data in hours.
Returns:
tuple
The total benefit, total netto benefit, and total generated energy.
"""
total_benefit = 0
total_netto_benefit = 0
total_gen = 0
@ -55,87 +70,100 @@ class EnergySystem:
sell_price = row['sell']
# electricity_price = self.grid.get_price_for_time(time)
if time == '00:00':
self.day_generated.append(self.generated)
self.generated = 0
if time.endswith('14:00'):
soc = self.ess.storage / self.ess.capacity
self.hour_stored.append(soc)
if time.endswith('08:00'):
soc = self.ess.storage / self.ess.capacity
self.hour_stored_2.append(soc)
# if time == '00:00':
# self.day_generated.append(self.generated)
# self.generated = 0
# if time.endswith('14:00'):
# soc = self.ess.storage / self.ess.capacity
# self.hour_stored.append(soc)
# if time.endswith('08:00'):
# soc = self.ess.storage / self.ess.capacity
# self.hour_stored_2.append(soc)
# `generated_pv_power`: the power generated by the PV in kW
# `generated_pv_energy`: the energy generated by the PV in kWh
generated_pv_power = self.pv.capacity * pv_yield
generated_pv_energy = generated_pv_power * time_interval * self.pv.loss
generated_pv_power = self.pv.capacity * pv_yield# 生成的功率,单位 kW
generated_pv_energy = generated_pv_power * time_interval * self.pv.loss # 生成的能量,单位 kWh
self.pv_generated_kWh.append(generated_pv_energy)
self.factory_demand.append(factory_demand)
self.buy_price_kWh.append(electricity_price)
self.sell_price_kWh.append(sell_price)
self.generated += generated_pv_energy
# pv生成的能量如果比工厂的需求要大
# generated_pv_energy is larger than factory_demand energy
if generated_pv_energy >= factory_demand * time_interval:
# 剩余的能量(kwh) = pv生成的能量 - 工厂需求的功率 * 时间间隔
"""
That means the generated energy is enough to power the factory.
The surplus energy will be used to charge the ESS.
surplus_energy: The energy that is left after powering the factory.
formula: generated_pv_energy - factory_demand * time_interval
charge_to_ess: The energy that will be charged to the ESS.
formula: min(surplus_energy, ess.charge_power * time_interval, ess.capacity - ess.storage)
surplus_after_ess: The energy that is left after charging the ESS.
"""
surplus_energy = generated_pv_energy - factory_demand * time_interval
# 要充到ess中的能量 = min(剩余的能量,ess的充电功率*时间间隔(ess在时间间隔内能充进的电量),ess的容量-ess储存的能量(ess中能冲进去的电量))
charge_to_ess = min(surplus_energy, self.ess.charge_power * time_interval, self.ess.capacity - self.ess.storage)
self.ess.storage += charge_to_ess
surplus_after_ess = surplus_energy - charge_to_ess
# 如果还有电量盈余,且pv功率大于ess的充电功率+工厂的需求功率则准备卖电
"""
If there is still surplus energy after charging the ESS, and the generated PV power is greater than the sum of the ESS's charge power and the factory's demand power, the surplus energy will be sold to the grid.
"""
if surplus_after_ess > 0 and generated_pv_power > self.ess.charge_power + factory_demand:
sold_to_grid = surplus_after_ess
sell_income = sold_to_grid * sell_price
total_benefit += sell_income
# 节省的能量 = 工厂需求的能量 * 时间段
# total_energy = factory_demand * time_interval
"""
Saved energy is the energy that is saved by using the PV to power the factory.
"""
saved_energy = factory_demand * time_interval
self.grid_need_power_kW.append(0)
# pv比工厂的需求小
else:
# 从ess中需要的电量 = 工厂需要的电量 - pv中的电量
"""
If the generated energy is not enough to power the factory, the ESS will be used to supplement the energy.
needed_from_ess: The energy that is needed from the ESS to power the factory.
formula: factory_demand * time_interval - generated_pv_energy
"""
needed_from_ess = factory_demand * time_interval - generated_pv_energy
# 如果ess中存的电量比需要的多
"""
If the ESS has enough stored energy to power the factory, the energy will be taken from the ESS.
"""
if self.ess.storage * self.ess.loss >= needed_from_ess:
# 取出电量
if self.ess.discharge_power * time_interval * self.ess.loss < needed_from_ess:
discharging_power = self.ess.discharge_power * time_interval
else:
discharging_power = needed_from_ess / self.ess.loss
self.ess.storage -= discharging_power
# 节省下来的能量 = pv的能量 + 放出来的能量
"""
In this case, the energy that is needed from the grid is 0.
"""
saved_energy = generated_pv_energy + discharging_power * self.ess.loss
self.grid_need_power_kW.append(0)
else:
# 如果存的电量不够
# 需要把ess中的所有电量释放出来
"""
If the ESS does not have enough stored energy to power the factory, the energy will be taken from the grid.
"""
if self.grid.capacity * time_interval + generated_pv_energy + self.ess.storage * self.ess.loss < factory_demand * time_interval:
self.afford = False
self.overload_cnt+=1
log = f"index: {index}, time: {time}, SoC:{self.ess.storage / self.ess.capacity}%, storage: {self.ess.storage}, pv_gen:{generated_pv_power}, power_demand: {factory_demand}, overload_cnt:{self.overload_cnt}, day:{int(index/96) + 1}"
self.unmet.append((index,time,factory_demand,generated_pv_power))
# with open(f'plots/summary/ess-{self.ess.capacity}-pv-{self.pv.capacity}', 'a') as f:
# f.write(log)
# print(log)
# self.unmet.append(log)
saved_energy = generated_pv_energy + self.ess.storage * self.ess.loss
self.ess.storage = 0
needed_from_grid = factory_demand * time_interval - saved_energy
net_grid = min(self.grid.capacity * time_interval, needed_from_grid) * self.grid.loss
self.grid_need_power_kW.append(needed_from_grid * 4)
# grid_energy += net_grid
# total_energy += net_grid
# print(total_energy)
# 工厂需求量-总能量
# unmet_demand = max(0, factory_demand * time_interval - total_energy)
# benefit = (total_energy - unmet_demand) * electricity_price
total_gen += saved_energy
benefit = (saved_energy) * electricity_price
cost = net_grid * electricity_price
# print(f"time:{time} benefit: {benefit}, cost: {cost}")
total_netto_benefit += benefit
total_benefit += benefit - cost
# # spring
print_season_flag = False
if print_season_flag == True:
week_start = self.season_start
week_end = self.week_length + week_start
if index in range(week_start, week_end):
@ -143,21 +171,21 @@ class EnergySystem:
self.spring_week_soc.append(self.ess.storage / self.ess.capacity)
self.ess_rest = self.ess.storage
# summer
# week_start += self.season_step
# week_end += self.season_step
# if index in range(week_start, week_end):
# self.summer_week_gen.append(generated_pv_power)
# self.summer_week_soc.append(self.ess.storage / self.ess.capacity)
week_start += self.season_step
week_end += self.season_step
if index in range(week_start, week_end):
self.summer_week_gen.append(generated_pv_power)
self.summer_week_soc.append(self.ess.storage / self.ess.capacity)
# # autumn
# week_start += self.season_step
# week_end += self.season_step
# if index in range(week_start, week_end):
# self.autumn_week_gen.append(generated_pv_power)
# self.autumn_week_soc.append(self.ess.storage / self.ess.capacity)
# week_start += self.season_step
# week_end += self.season_step
# if index in range(week_start, week_end):
# self.winter_week_gen.append(generated_pv_power)
# self.winter_week_soc.append(self.ess.storage / self.ess.capacity)
week_start += self.season_step
week_end += self.season_step
if index in range(week_start, week_end):
self.autumn_week_gen.append(generated_pv_power)
self.autumn_week_soc.append(self.ess.storage / self.ess.capacity)
week_start += self.season_step
week_end += self.season_step
if index in range(week_start, week_end):
self.winter_week_gen.append(generated_pv_power)
self.winter_week_soc.append(self.ess.storage / self.ess.capacity)
return (total_benefit, total_netto_benefit, total_gen)

View File

@ -805,7 +805,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.11.0"
}
},
"nbformat": 4,

View File

@ -44,7 +44,7 @@ from config import pv_config, grid_config, ess_config
import json
print("Version 0.0.5")
print("Version 0.0.7\n")
with open('config.json', 'r') as f:
js_data = json.load(f)