-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbattery.py
253 lines (232 loc) · 8.12 KB
/
battery.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import csv
import datetime
import dateutil.parser
import numpy as np
def parse_csv(file):
"""
parse data from CSV files into an array
:param file: name of file to parse
:return: array of data
"""
with open(file, newline='\n') as csv_file:
#with open(file, newline='\n') as csv_file:
reader = csv.reader(csv_file)
data = []
i = 0
# Get the data from the files, ignore the first row
for row in reader:
try:
data.append(row)
i += 1
except ValueError:
pass
data_final = []
# Skip the label row.
for row in data[1:]:
# Parse the data.
date = dateutil.parser.parse(row[1]+" "+row[2])
power_kw = float(row[3])
power_solar = float(row[4])
data_final.append([date, power_kw - power_solar])
return data_final
def costsSaved(shaved, year, duration, accuracy, peakRate = 18, discount = .05, rateInflation = .02):
"""
Assumption: the operator of the battery has a really good prediction and can perfectly set peak threshold
:param shaved: data for amount possible to shave per month for one building for two years
:param year: 2014 or 2015, need to choose one year's data to replicate
:param duration: length of time in years that will project cost savings for
:param accuracy: percent accuracy that the operator will be able to set the threshold
:param peakRate: rate per peak kW in dollars
:param discount: annual discount rate for the building owner to invest in battery
:param rateInflation: expected peak rate increases annually
:return: present value dollar amount for costs saved from peak shaving, future value of costs saved
"""
if year == 2014:
shaved = shaved[:12]
if year == 2015:
shaved = shaved[12:]
mlyDiscount = (1 + discount) ** (1/12)
mlyInflation = (1 + rateInflation) ** (1/12)
for i in range(duration-1):
for i2 in range(12):
shaved.append(shaved[i2])
pvSave = []
fSave = []
for s in range(len(shaved)):
ir = (peakRate*(mlyInflation ** s))
dr = mlyDiscount ** s
s2 = shaved[s]
d1 = ir * s2 / dr
d1 = d1*accuracy
pvSave.append(d1)
fSave.append(s2*ir)
fval = sum(fSave)
presval = sum(pvSave)
return presval, fval
def netGain(saved, size, price = 500):
"""
Assumption : peak kW of battery = 1/2 kWh
:param saved: costs saved from peak shaving
:param size: size of battery in kWh
:param price: price per kWh of battery
:return: net profit in present value
"""
batteryCost = size * price
net = saved - batteryCost
return net
def monthSeparate(data):
"""
:param data: parsed data
:return: array of arrays separated by month
"""
data_final = []
for yr in [2014,2015]:
for mon in range(1,13,1):
data_final.append([])
ind = (yr - 2014)*12 + mon - 1
for i in range(len(data)):
if data[i][0].year == yr and data[i][0].month == mon:
data_final[ind].append(data[i])
return data_final
def testDeltaMonth(month, y):
"""
:param month: one month's energy usage data from monthSeparate
:param y: kwH of battery
:return: max amount able to peak shave for that month
"""
maxShaved = 0
for i in range(0, y+1, 1):
m = peakShaved(month, y, i)
if m[0]:
maxShaved = i
return maxShaved
def testSize(data,y):
"""
:param data: array of arrays from monthSeparate
:param y: battery size in kwh
:return: array of max amount shaved per month
"""
shavedByMonth = []
for i in range(len(data)):
x = testDeltaMonth(data[i], y)
shavedByMonth.append(x)
return shavedByMonth
def peakShaved(data, y, delta):
"""
:param data: one month's energy usage data from monthSeparate
:param y: battery size in kWh
:param delta: difference between month's peak and the threshold in kw
:return: boolean of whether it worked for that threshold and period that it failed in (or last period)
"""
worked = True
period = "all"
z = y/2 # kw peak, this is an assumption that that is a constant ratio
a = y
eU = [] # an array that just has the energy usage numbers, instead of an array of arrays
for i in range(len(data)):
eU.append(data[i][1])
thresh = max(eU) - delta # set the threshold for the month
for p in range(len(eU)):
b = z
x = eU[p]
if x > thresh: # if need to drain
b -= x - thresh # kw above thresh
a -= (x - thresh)/4 # drained kWh from battery
if thresh - x > 5: # leaves a buffer for charging, though operator should be able to respond in real time with perfect accuracy
da = thresh - x - 5
if da > b:
da = b
a += da/4
if a > y:
a = y
if a < 0:
worked = False
period = p
break
if b < 0:
worked = False
period = p
break
return worked, period
def loopSizes(data, largest, year, duration, accuracy, smallest = 200, increment = 50, price = 500, peakRate = 18, discount = .05, rateInflation = .02):
"""
:param data: monthlySeparated data for site
:param largest: largest battery size feasible
:param smallest: smallest battery size available, standard is 200 kWh for tesla powerpack
:param increment: size intervals that you can buy a pack in, let's say 50 is standard
:param price: price of battery per kWh
:param peakRate:
:param discount:
:param rateInflation:
:return: array of net profits, array of costs saved
"""
saved = []
nets = []
for size in range(smallest, largest + increment, increment):
a = testSize(data, size)
c = costsSaved(a, year, duration, accuracy, peakRate, discount, rateInflation)
n = netGain(c[0],size,price)
saved.append(c[1])
nets.append(n)
return nets, saved
if __name__ == '__main__':
site1 = parse_csv("site_1.csv")
ms1 = monthSeparate(site1)
site2 = parse_csv("site_2.csv")
ms2 = monthSeparate(site2)
inc = 25
small = 25
dur = 10
s1Accuracy = 1-.156
s2Accuracy = 1-.213
a = loopSizes(ms1, 500, 2014, dur, s1Accuracy, smallest=small, increment=inc)
print("site 1 using 2014 data:")
for i1 in range(len(a[0])):
print("size = " + str(small + i1*inc))
print("pv net profit = $" + str(a[0][i1]))
print("battery cost = $" +str((small + i1*inc)*500))
print("future savings: $" + str(a[1][i1]))
b = loopSizes(ms1, 500, 2015, dur, s1Accuracy, smallest=small, increment=inc)
print()
print()
print("site 1 using 2015 data:")
for i2 in range(len(b[0])):
print("size = " + str(small + i2*inc))
print("pv net profit = $" + str(b[0][i2]))
print("battery cost = $" +str((small + i2*inc)*500))
print("future savings: $" + str(b[1][i2]))
print()
print()
e = loopSizes(ms2, 500, 2014, dur, s2Accuracy, smallest=small, increment=inc)
print("site 2 using 2014 data:")
for i3 in range(len(e[0])):
print("size = " + str(small + i3 * inc))
print("pv net profit = $" + str(e[0][i3]))
print("battery cost = $" +str((small + i3*inc)*500))
print("future savings: $" + str(e[1][i3]))
f = loopSizes(ms2, 500, 2015, dur, s2Accuracy, smallest=small, increment=inc)
print()
print()
print("site 2 using 2015 data:")
for i4 in range(len(f[0])):
print("size = " + str(small + i4 * inc))
print("pv net profit = $" + str(f[0][i4]))
print("battery cost = $" +str((small + i4*inc)*500))
print("future savings: $" + str(f[1][i4]))
# a = testSize(ms, y)
# print(a[:12])
# print(a[12:])
# print()
# z = 400
# b = testSize(ms, z)
# print(b[:12])
# print(b[12:])
# c = costsSaved(a, 2014, 1)
# n = netGain(c, y)
# print(n)
# print()
#
# d = costsSaved(a, 2014, 10)
# n1 = netGain(d, y)
# print(n1)
# print(c)