-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEV_functions.py
More file actions
562 lines (504 loc) · 31 KB
/
EV_functions.py
File metadata and controls
562 lines (504 loc) · 31 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
"""
Defines functions for the HH
Uses direct setting of system mode
"""
import gridlabd
import gridlabd_functions
#from gridlabd_functions import p_max # ???????????????
#import mysql_functions
#from HH_global import *
import mysql_functions as myfct
import datetime
import numpy as np
import pandas
from dateutil import parser
from datetime import timedelta
"""NEW FUNCTIONS / MYSQL DATABASE AVAILABLE"""
#HVAC
from HH_global import flexible_houses, C, p_max, interval, prec, start_time_str, end_time_str, EV_data, EV_speed
dep_hours = [6,7,8,9]
arr_hours = [16,17,18,19,20]
list_SOC = [30.,40.,50.,60.]
list_u = [7.,10.,14.,20.]
class EVCP:
def __init__(self,name,Qmax=0.0,Qset=0.0):
self.name = name #in GLD simulation
self.ID = name.split('_')[-1]
#Configuration
self.Qmax = Qmax
self.Qset = Qset
#Only executed if new EV is connected
def checkin_newEV(self,df_evcp_state_in,connected):
self.connected = connected
self.charging = False
self.Kev = df_evcp_state_in['Kev'].iloc[0]
self.tdep = df_evcp_state_in['tdep'].iloc[0]
self.Emax = df_evcp_state_in['Emax'].iloc[0]
self.DeltaE = df_evcp_state_in['DeltaE'].iloc[0]
self.E = 0.0 #Energy charged since connected
#Execute every time an EV is connected
def update_state(self,time):
if time > self.tdep:
######### This should be verified with physical model!!!
self.connected = False
return
if self.connected:
self.trem = self.tdep - time
if self.charging: #if it was allocated in the past market period
self.E = self.E + self.Qset*interval/3600./self.Emax #update SOC
self.charging =False
def bid(self,dt_sim_time,market,P_exp,P_dev):
treq = max((self.DeltaE - self.E),0.0)/100.*self.Emax/self.Qset #[h] Slightly changed from bidding fct Jupyter notebook
treq = pandas.Timedelta(seconds=treq*3600)
if self.trem > pandas.Timedelta(seconds=0):
self.P_bid = P_exp + 3*self.Kev*P_dev*(2*treq/self.trem - 1.)
self.Q_bid = self.Qset
else:
self.P_bid = 0.0
self.Q_bid = 0.0
if (self.Q_bid > 0.0):
try:
timestamp_arrival = market.send_supply_bid(dt_sim_time, float(self.P_bid), float(self.Q_bid), self.name) #Feedback: timestamp of arrival #C determined by market_operator
except:
import pdb; pdb.set_trace()
return
def dispatch(self,dt_sim_time,p_lem,alpha):
inverter = self.name
if (self.Q_bid > 0.0) and (self.P_bid > p_lem):
gridlabd.set_value(inverter,'P_Out',str(-self.Q_bid*1000.))
mode = 1.
elif (self.Q_bid > 0.0) and (self.P_bid == p_lem):
print('This HVAC is marginal; no partial implementation yet: '+str(alpha))
gridlabd.set_value(inverter,'P_Out',str(-self.Q_bid*1000.))
mode = 1.
else:
gridlabd.set_value(inverter,'P_Out',str(0.0))
mode = 0.
parameter_string = '(timedate, P_bid, Q_bid, mode, DeltaE)' #DeltaE at beginning of period
value_tuple = (dt_sim_time, float(self.P_bid), float(self.Q_bid), float(mode), float(self.DeltaE),)
myfct.set_values('EV_'+self.ID+'_state_out', parameter_string, value_tuple)
self.P_bid = -100000.0
self.Q_bid = 0.0
return
def get_CP(house,house_name):
CP_name = 'CP'+house_name[5:]
#import pdb; pdb.set_trace()
try:
df_CP_settings = myfct.get_values_td(CP_name + '_settings')
except:
df_CP_settings = pandas.DataFrame()
#If house has a charger
if len(df_CP_settings) > 0:
evcp = EVCP('EV_inverter'+house_name[5:]) #Name in GLD simulation
#import pdb; pdb.set_trace()
#Configuration
evcp.Qmax = df_CP_settings['Qmax'].iloc[-1]/1000. #[kW]
evcp.Qset = df_CP_settings['Qset'].iloc[-1]/1000. #[kW]
house.EVCP = evcp
return house
# class EV:
# def __init__(self,name,Kev=0.0,Emax=0.0,Qmax=0.0,Qon=0.0,Qoff=0.0,Status='OFF',E=0.0,treq=pandas.Timedelta(seconds=0),trem=pandas.Timedelta(seconds=0),Eest=0.0,Qset=0.0,Qobs=0.0):
# self.name = name
# #Configuration
# self.charge_rate
# #State
# self.E = E #Energy
# self.treq = treq #Time required to fully charge the battery
# self.trem = trem #Time remaining before the battery should be fully charged
# self.Eest = Eest #Estimated charge added to the battery
# self.Qset = Qset #Power setpoint (maximum allowed power a vehicle can draw)
# self.Qobs = Qobs #Current power been draw
# #Last bids
# self.P_sell_bid = 100000.0
# self.P_buy_bid = -100000.0
# self.Q_sell_bid = 0.0
# self.Q_buy_bid = 0.0
# def update_state(self,df_batt_state_in):
# self.soc_rel = df_batt_state_in['soc_rel'].iloc[0]
# self.SOC = self.soc_rel*self.SOC_max
# def bid(self,dt_sim_time,market,P_exp,P_dev):
# #Sell bid
# if self.soc_rel <= self.soc_des:
# soc_ref = self.soc_min
# else:
# soc_ref = 1.0
# p_buy_bid = P_exp + 3*self.k*P_dev*(self.soc_rel - self.soc_des)/abs(soc_ref - self.soc_des)
# #Buy bid
# p_oc = P_exp + 3*self.k+P_dev*(self.soc_rel*self.SOC_max - self.soc_des*self.SOC_max + self.u_max*(interval/360.))/abs(soc_ref*self.SOC_max - self.soc_des*self.SOC_max + self.u_max*(interval/360.))
# cap_cost = 500 #USD/KWh for Tesla powerwall
# p_sell_bid = p_oc/self.efficiency + cap_cost/(self.soc_des + 0.4)**2
# self.P_sell_bid = p_sell_bid
# u_res = (self.soc_rel - self.soc_min)*self.SOC_max/(interval/360.)
# self.Q_sell_bid = min(self.u_max,u_res)
# self.P_buy_bid = p_buy_bid
# u_res = (1. - self.soc_rel)*self.SOC_max/(interval/360.)
# self.Q_buy_bid = min(self.u_max,u_res)
# #write P_bid, Q_bid to market DB
# timestamp_arrival_buy = market.send_demand_bid(dt_sim_time, float(p_buy_bid), float(self.Q_buy_bid), self.name) #Feedback: timestamp of arrival #C determined by market_operator
# timestamp_arrival_sell = market.send_supply_bid(dt_sim_time, float(p_sell_bid), float(self.Q_sell_bid), self.name)
# return
# def dispatch(self,dt_sim_time,p_lem,alpha):
# #import pdb; pdb.set_trace()
# inverter = 'Bat_inverter_'+self.name.split('_')[-1]
# if (self.Q_buy_bid > 0.0) and (self.P_buy_bid > p_lem):
# gridlabd.set_value(inverter,'P_Out',str(-self.Q_buy_bid*1000.))
# elif (self.Q_buy_bid > 0.0) and (self.P_buy_bid == p_lem):
# print('This HVAC is marginal; no partial implementation yet: '+str(alpha))
# gridlabd.set_value(inverter,'P_Out',str(-self.Q_buy_bid*1000.))
# elif (self.Q_sell_bid > 0.0) and (self.P_sell_bid < p_lem):
# gridlabd.set_value(inverter,'P_Out',str(self.Q_sell_bid*1000.))
# elif (self.Q_sell_bid > 0.0) and (self.P_sell_bid == p_lem):
# print('This HVAC is marginal; no partial implementation yet: '+str(alpha))
# gridlabd.set_value(inverter,'P_Out',str(self.Q_sell_bid*1000.))
# else:
# gridlabd.set_value(inverter,'P_Out',str(0.0))
# myfct.set_values(self.name+'_state_out', '(timedate, p_demand, p_supply, q_demand, q_supply)', (dt_sim_time, str(self.P_buy_bid), str(self.P_sell_bid), str(self.Q_buy_bid), str(self.Q_sell_bid)))
# self.P_sell_bid = 100000.0
# self.P_buy_bid = -100000.0
# self.Q_sell_bid = 0.0
# self.Q_buy_bid = 0.0
############### OLD #############
def get_settings_EVs(EVlist,interval,mysql=False):
cols_EV = ['EV_name','house_name','SOC_max','i_max','v_max','u_max','efficiency','charging_type','k','soc_t','SOC_t','connected','next_event','active_t-1','active_t']
df_EV = pandas.DataFrame(columns=cols_EV)
#Read in charging events
#df_events = pandas.read_csv('EV_events_2016_july_Austin.csv',index_col=[0],parse_dates=True) #'EV_events_2015_july.csv'
df_events = pandas.read_csv(EV_data,index_col=[0],parse_dates=True)
first_time = pandas.Timestamp.today()
first_time = first_time.replace(year=first_time.year + 4) #to ensure working with RT data
for EV in EVlist:
df_events[EV] = pandas.to_datetime(df_events[EV])
if df_events[EV].min() < first_time:
first_time = df_events[EV].min()
first_time = first_time.replace(hour=0,minute=0,second=0)
start_time = pandas.to_datetime(start_time_str)
delta = start_time - first_time
for EV in EVlist:
df_events[EV] = df_events[EV] + delta
EV_obj = gridlabd.get_object(EV)
#gridlabd.set_value(EV,'use_internal_battery_model','FALSE')
house_name = 'GLD_'+EV[3:]
#Use GridLABD default values
v_max = float(EV_obj['V_Max']) #keep v_max constant for now
eff = float(EV_obj['base_efficiency'])
#Fills TABLE market_appliances
#SOC_min = float(EV_obj['reserve_state_of_charge']) #in % - 20%
#SOC_max = float(EV_obj['battery_capacity'])/1000 #Wh in Gridlabd -> kWh
#str_i_max = EV_obj['I_Max']
#i_max = str_i_max.split('+')[1]
#u_max = float(EV_obj['V_Max'])*float(i_max)/1000 #W -> kW #better inverter?
charging_type = EV_obj['charging_type']
k = float(EV_obj['k']) #no market: always highest wllingsness to pay
#Gets next event
cols = [EV,EV+'_u',EV+'_SOC',EV+'_SOCmax']
df_events_EV = df_events[cols]
discard_events = True
while discard_events:
if (df_events_EV[EV].iloc[0] < pandas.to_datetime(start_time_str)) and (df_events_EV[EV].iloc[1] < pandas.to_datetime(start_time_str)):
df_events_EV = df_events_EV.iloc[2:]
elif (df_events_EV[EV].iloc[0] < pandas.to_datetime(start_time_str)) and (df_events_EV[EV].iloc[1] > pandas.to_datetime(start_time_str)):
#Get information from EV events table
soc_t = df_events_EV[EV+'_SOC'].iloc[0] #relative soc
u_max = df_events_EV[EV+'_u'].iloc[0] #in kW
SOC_max = df_events_EV[EV+'_SOCmax'].iloc[0] #in kWh
#For start of the simulation
soc_t = soc_t + (1. - soc_t)/2. #No EV connected yet - at midnight, initialize with 50% charged already
SOC_t = soc_t*SOC_max
gridlabd.set_value(EV,'state_of_charge',str(soc_t))
gridlabd.set_value(EV,'battery_capacity',str(SOC_max*1000))
i_max = 1000*u_max/v_max
gridlabd.set_value(EV,'I_Max',str(i_max))
gridlabd.set_value(EV,'generator_status','ONLINE')
next_event = df_events_EV[EV].iloc[1] #Next event: disconnection
df_events_EV = df_events_EV.iloc[1:] #Only discard connection event
connected = 1
discard_events = False
elif (df_events_EV[EV].iloc[0] > pandas.to_datetime(start_time_str)) and (df_events_EV[EV].iloc[1] > pandas.to_datetime(start_time_str)):
soc_t = 0.0
u_max = 0.0
SOC_max= 0.0
soc_t = 0.0
SOC_t = 0.0
i_max = 0.0
next_event = df_events_EV[EV].iloc[0] #Next event: connection
connected =0
gridlabd.set_value(EV,'generator_status','OFFLINE')
discard_events = False
else:
print('Unclear or no EV events at that charging station during that time')
soc_t = 0.0
i_max = 0.0
next_event = pandas.to_datetime(end_time_str) #Next event: none / end_time
connected = 0
gridlabd.set_value(EV,'generator_status','OFFLINE')
discard_events = False
df_EV = df_EV.append(pandas.Series([EV,house_name,SOC_max,i_max,v_max,u_max,eff,charging_type,k,soc_t,SOC_t,connected,next_event,0,0],index=cols_EV),ignore_index=True)
df_events_EV.index = df_events_EV.index - df_events_EV.index[0] #reset index to range()
df_events[cols] = df_events_EV[cols]
df_events.to_csv('EV_events_pop.csv')
df_EV.set_index('EV_name',inplace=True)
#Maybe drop all columns which are not used in this glm
return df_EV
#If no EV data is available, random generation of arrival and departure events as well as SOC_t
def get_settings_EVs_rnd(EVlist,interval,mysql=False):
cols_EV = ['EV_name','house_name','SOC_max','i_max','v_max','u_max','efficiency','charging_type','k','soc_t','SOC_t','connected','next_event','active_t-1','active_t']
df_EV = pandas.DataFrame(columns=cols_EV)
start_time = dt_sim_time = parser.parse(gridlabd.get_global('clock')).replace(tzinfo=None)
for EV in EVlist:
house_name = 'GLD_'+EV[3:]
EV_obj = gridlabd.get_object(EV)
#Determine some values
v_max = float(EV_obj['V_Max']) #keep v_max constant for now
SOC_max = np.random.choice(list_SOC) #in kWh
gridlabd.set_value(EV,'battery_capacity',str(SOC_max*1000))
u_max = np.random.choice(list_u) #in kW
i_max = 1000*u_max/v_max
gridlabd.set_value(EV,'I_Max',str(i_max))
eff = float(EV_obj['base_efficiency'])
charging_type = EV_obj['charging_type']
k = float(EV_obj['k']) #no market: always highest wllingsness to pay
#Randomly generate SOC at midnight
soc_t = np.random.uniform(0.2,0.8) #relative soc
soc_t = soc_t + (1. - soc_t)/2. #No EV connected yet - at midnight, initialize with 50% charged already
gridlabd.set_value(EV,'state_of_charge',str(soc_t))
SOC_t = soc_t*SOC_max
connected = 1
gridlabd.set_value(EV,'generator_status','ONLINE')
next_event = start_time + pandas.Timedelta(hours=np.random.choice(dep_hours),minutes=np.random.choice(range(60))) #Next event: disconnection
#Save
df_EV = df_EV.append(pandas.Series([EV,house_name,SOC_max,i_max,v_max,u_max,eff,charging_type,k,soc_t,SOC_t,connected,next_event,0,0],index=cols_EV),ignore_index=True)
df_EV.set_index('EV_name',inplace=True)
#Maybe drop all columns which are not used in this glm
#import pdb; pdb.set_trace()
return df_EV
def update_EV(dt_sim_time,df_EV_state):
print('Update EV')
df_EV_state['active_t-1'] = df_EV_state['active_t']
df_EV_state['active_t'] = 0
df_events = pandas.read_csv('EV_events_pop.csv',index_col=[0], parse_dates=df_EV_state.index.tolist())
for EV in df_EV_state.index:
EV_number = int(EV.split('_')[-1])
next_event, next_u, next_soc, next_socmax = df_events[[EV,EV+'_u',EV+'_SOC',EV+'_SOCmax']].iloc[0]
next_event = next_event.to_pydatetime()
#Event change
if next_event <= dt_sim_time: #event change in the last period -> change in status of battery
#prev_event, prev_soc = df_events[[EV,EV+'_SOC']].iloc[0]
df_events[[EV,EV+'_u',EV+'_SOC',EV+'_SOCmax']] = df_events[[EV,EV+'_u',EV+'_SOC',EV+'_SOCmax']].shift(-1)
#Randomly choose for fast-charging
if EV_speed == 'fast':
energy_refuel = (1. - next_soc)*next_socmax
list_EVs = [(24.,22.),(50.,32.),(50.,60.),(120.,90.)]
EV_type = np.random.choice(len(list_EVs),1)[0]
next_u, next_socmax = list_EVs[EV_type]
next_soc = max(next_socmax - energy_refuel,0.0)/next_socmax
#next_event, next_soc = df_events[[EV,EV+'_SOC']].iloc[0]
if not np.isnan(next_soc):
#Set EV
gridlabd.set_value(EV,'generator_status','ONLINE')
gridlabd.set_value(EV,'state_of_charge',str(next_soc))
gridlabd.set_value(EV,'battery_capacity',str(next_socmax*1000))
i_max = 1000*next_u/df_EV_state['v_max'].loc[EV]
gridlabd.set_value(EV,'I_Max',str(i_max))
gridlabd.set_value(EV,'P_Max',str(next_u*1000))
gridlabd.set_value(EV,'E_Max',str(next_u*1000*1))
EV_inv = 'EV_inverter' + EV[2:]
gridlabd.set_value(EV_inv,'rated_power',str(1.2*next_u*1000))
gridlabd.set_value(EV_inv,'rated_battery_power',str(1.2*next_u*1000))
#import pdb; pdb.set_trace()
#Set df_EV_state
df_EV_state.at[EV,'SOC_max'] = next_socmax
df_EV_state.at[EV,'i_max'] = i_max
df_EV_state.at[EV,'u_max'] = next_u
df_EV_state.at[EV,'connected'] = 1 #Does this one not work such that delay in connection?
df_EV_state.at[EV,'soc_t'] = next_soc
df_EV_state.at[EV,'SOC_t'] = next_soc*next_socmax
df_EV_state.at[EV,'next_event'] = next_event
else: #if next event associated with non-NaN SOC - now connected and pot. charging
#Set EV (only switch off)
gridlabd.set_value(EV,'generator_status','OFFLINE')
gridlabd.set_value(EV,'state_of_charge',str(0.0))
#Set df_EV_state
df_EV_state.at[EV,'SOC_max'] = 0.0
df_EV_state.at[EV,'i_max'] = 0.0
df_EV_state.at[EV,'u_max'] = 0.0
df_EV_state.at[EV,'connected'] = 0 #Does this one not work such that delay in connection?
df_EV_state.at[EV,'soc_t'] = 0.0
df_EV_state.at[EV,'SOC_t'] = 0.0
df_EV_state.at[EV,'next_event'] = next_event
#After last event (no upcoming events)
elif pandas.isnull(next_event):
#Set EV (only switch off)
gridlabd.set_value(EV,'generator_status','OFFLINE')
#Set df_EV_state
df_EV_state.at[EV,'SOC_max'] = 0.0
df_EV_state.at[EV,'i_max'] = 0.0
df_EV_state.at[EV,'u_max'] = 0.0
df_EV_state.at[EV,'connected'] = 0 #Does this one not work such that delay in connection?
df_EV_state.at[EV,'soc_t'] = 0.0
df_EV_state.at[EV,'SOC_t'] = 0.0
df_EV_state.at[EV,'next_event'] = next_event
#During charging event: Update EV state (SOC)
elif pandas.isnull(next_socmax):
# Updating through GridlabD model
EV_obj = gridlabd.get_object(EV)
soc_t = float(EV_obj['state_of_charge'])
df_EV_state.at[EV,'soc_t'] = soc_t
SOC_t = soc_t*float(EV_obj['battery_capacity'])/1000 #In Wh #Losses updated by GridlabD ?
df_EV_state.at[EV,'SOC_t'] = SOC_t
# Updating using df
#print('GLD battery model is not used, manual updating!')
#gridlabd.set_value(EV,'state_of_charge',str(df_EV_state['soc_t'].loc[EV])) #in p.u.
else:
pass
df_events.to_csv('EV_events_pop.csv')
return df_EV_state
def update_EV_rnd(dt_sim_time,df_EV_state):
print('Update EV')
df_EV_state['active_t-1'] = df_EV_state['active_t']
df_EV_state['active_t'] = 0
today = pandas.Timestamp(dt_sim_time.year,dt_sim_time.month,dt_sim_time.day)
for EV in df_EV_state.index:
EV_number = int(EV.split('_')[-1])
next_event = df_EV_state['next_event'].loc[EV]
#Event change
if next_event <= dt_sim_time: #event change in the last period -> change in status of battery
#Randomly choose for fast-charging
if EV_speed == 'fast':
energy_refuel = (1. - next_soc)*next_socmax
list_EVs = [(24.,22.),(50.,32.),(50.,60.),(120.,90.)]
EV_type = np.random.choice(len(list_EVs),1)[0]
next_u, next_socmax = list_EVs[EV_type]
next_soc = max(next_socmax - energy_refuel,0.0)/next_socmax
#just arrived
if df_EV_state['connected'].loc[EV] == 0:
#Set EV
next_u = df_EV_state['u_max'].loc[EV]
gridlabd.set_value(EV,'generator_status','ONLINE')
soc_t = np.random.uniform(0.2,0.8)
gridlabd.set_value(EV,'state_of_charge',str(soc_t))
gridlabd.set_value(EV,'battery_capacity',str(df_EV_state['SOC_max'].loc[EV]*1000))
i_max = 1000*next_u/df_EV_state['v_max'].loc[EV]
gridlabd.set_value(EV,'I_Max',str(i_max))
gridlabd.set_value(EV,'P_Max',str(next_u*1000))
gridlabd.set_value(EV,'E_Max',str(next_u*1000*1))
EV_inv = 'EV_inverter' + EV[2:]
gridlabd.set_value(EV_inv,'rated_power',str(1.2*next_u*1000))
gridlabd.set_value(EV_inv,'rated_battery_power',str(1.2*next_u*1000))
#import pdb; pdb.set_trace()
#Set df_EV_state
df_EV_state.at[EV,'connected'] = 1 #Does this one not work such that delay in connection?
df_EV_state.at[EV,'soc_t'] = soc_t
df_EV_state.at[EV,'SOC_t'] = soc_t*df_EV_state['SOC_max'].loc[EV]
#Departure time tomorrow
df_EV_state.at[EV,'next_event'] = today + pandas.Timedelta(days=1,hours=np.random.choice(dep_hours),minutes=np.random.choice(range(60))) #Next event: disconnection
else: #if next event associated with non-NaN SOC - now connected and pot. charging
#Set EV (only switch off)
gridlabd.set_value(EV,'generator_status','OFFLINE')
gridlabd.set_value(EV,'state_of_charge',str(0.0))
#Set df_EV_state
df_EV_state.at[EV,'connected'] = 0 #Does this one not work such that delay in connection?
df_EV_state.at[EV,'soc_t'] = 0.0
df_EV_state.at[EV,'SOC_t'] = 0.0
#Arrival time today
df_EV_state.at[EV,'next_event'] = today + pandas.Timedelta(hours=np.random.choice(arr_hours),minutes=np.random.choice(range(60))) #Next event: disconnection
#During charging event: Update EV state (SOC)
elif df_EV_state['connected'].loc[EV] == 1:
# Updating through GridlabD model
# EV_obj = gridlabd.get_object(EV)
# soc_t = float(EV_obj['state_of_charge'])
# df_EV_state.at[EV,'soc_t'] = soc_t
# SOC_t = soc_t*float(EV_obj['battery_capacity'])/1000 #In Wh #Losses updated by GridlabD ?
# df_EV_state.at[EV,'SOC_t'] = SOC_t
# Updating using df
#print('GLD battery model is not used for EVs, manual updating!')
gridlabd.set_value(EV,'state_of_charge',str(df_EV_state['soc_t'].loc[EV])) #in p.u.
else:
pass
return df_EV_state
def calc_bids_EV(dt_sim_time,df_bids_EV,retail,mean_p,var_p):
#Quantity
safety_fac = 1.0
df_bids_EV['q_buy'] = 0.0 #general
#df_bids_EV['residual_u'] = round((3600./interval)*(safety_fac*df_bids_EV['SOC_max'] - df_bids_EV['SOC_t']),prec) #u at which EV would need to be charged during the interval to completely fill it
df_bids_EV['residual_u'] = (3600./interval)*(safety_fac*df_bids_EV['SOC_max'] - df_bids_EV['SOC_t']).values.astype(float).round(prec) #u at which EV would need to be charged during the interval to completely fill it
df_bids_EV['q_buy'].loc[df_bids_EV['connected'] == 1] = df_bids_EV.loc[df_bids_EV['connected'] == 1][['residual_u','u_max']].min(axis=1) #in kW
df_bids_EV['q_buy'].loc[df_bids_EV['q_buy'] < 1.] = 0.0
#Price
df_bids_EV['p_buy'] = 0.0 #general
#Commercial
df_bids_EV.loc[df_bids_EV['charging_type'].str.contains('comm') & (df_bids_EV['connected'] == 1) & (df_bids_EV['q_buy'] > 0.001),'p_buy'] = retail.Pmax #max for commercial cars
#Home-based charging
df_bids_EV['delta'] = df_bids_EV['next_event'] - dt_sim_time
df_bids_EV['residual_t'] = df_bids_EV['delta'].apply(lambda x: x.seconds)/3600. #residual time until departure
rel_index = df_bids_EV.loc[df_bids_EV['charging_type'].str.contains('resi') & (df_bids_EV['connected'] == 1) & (df_bids_EV['q_buy'] > 0.0) & (df_bids_EV['residual_t']*3600 >= interval),'p_buy'].index
#Bid calculation
#df_bids_EV.at[rel_index,'p_buy'] = retail.Pmax - df_bids_EV['k'].loc[rel_index] * df_bids_EV['residual_t'].loc[rel_index]
#Uncommented on 12/23
#df_bids_EV['intercept'] = mean_p - df_bids_EV['k']*df_bids_EV['u_max']
#df_bids_EV.at[rel_index,'p_buy'] = df_bids_EV['intercept'].loc[rel_index] + df_bids_EV['k'].loc[rel_index]*df_bids_EV['residual_u'].loc[rel_index]/df_bids_EV['residual_t'].loc[rel_index]
#Test on 12/23 - consistent with final report
df_bids_EV.at[rel_index,'p_buy'] = p_max - df_bids_EV['k'].loc[rel_index]*(df_bids_EV['u_max'].loc[rel_index] - df_bids_EV['residual_u'].loc[rel_index])
df_bids_EV['lower_bound'] = 0.0
df_bids_EV['upper_bound'] = p_max
df_bids_EV['p_buy'] = df_bids_EV[['p_buy','lower_bound']].max(axis=1) #non-negative bids
df_bids_EV['p_buy'] = df_bids_EV[['p_buy','upper_bound']].min(axis=1) #non-negative bids
#print(df_bids_EV[['residual_SOC','delta','p_buy']])
#For no market case
#df_bids_EV.loc[df_bids_EV['charging_type'].str.contains('resi') & (df_bids_EV['connected'] == 1) & (df_bids_EV['residual_SOC'] > 0.001),'p_buy'] = retail.Pmax
return df_bids_EV
def submit_bids_EV(dt_sim_time,retail,df_bids,df_buy_bids):
for ind in df_bids.index:
if df_bids['q_buy'].loc[ind] > 0.001: #Unconnected cars have q_buy = 0
retail.buy(df_bids['q_buy'].loc[ind],df_bids['p_buy'].loc[ind],active=df_bids['active_t'].loc[ind],appliance_name=ind)
#mysql_functions.set_values('buy_bids', '(bid_price,bid_quantity,timedate,appliance_name)',(float(df_bids['p_buy'].loc[ind]),float(df_bids['q_buy'].loc[ind]),dt_sim_time,ind,))
df_buy_bids = df_buy_bids.append(pandas.DataFrame(columns=df_buy_bids.columns,data=[[dt_sim_time,ind,float(df_bids['p_buy'].loc[ind]),float(df_bids['q_buy'].loc[ind])]]),ignore_index=True)
return retail,df_buy_bids
#Partial charging if car drives off?
def set_EV_GLD(dt_sim_time,df_bids_EV,df_awarded_bids):
#Set charging/discharging
#Change from no to battery_name
#Do more quickly by setting database through Gridlabd?
for EV in df_bids_EV.index:
EV_number = int(EV.split('_')[-1]) #int(battery.split('_')[1])
#print df_bids_EV[['connected','SOC','active','next_event']].loc[df_bids_EV['appliance_id'] == EV_number]
#print type(df_bids_EV['next_event'].loc[df_bids_EV['appliance_id'] == EV_number].values[0])
#print pd.isnull(df_bids_EV['next_event'].loc[df_bids_EV['appliance_id'] == EV_number].values[0])
SOC = df_bids_EV['SOC_t'].loc[EV] #this is SOC at the beginning of the period t
active = df_bids_EV['active_t'].loc[EV] #this is activity in t
connected = df_bids_EV['connected'].loc[EV]
if active == 1:
q_bid = -1000*df_bids_EV['q_buy'].loc[EV]
p_bid = df_bids_EV['p_buy'].loc[EV]
gridlabd.set_value('EV_inverter_'+EV[3:],'P_Out',str(q_bid)) #kW -> W
#mysql_functions.set_values('awarded_bids','(appliance_name,p_bid,q_bid,timedate)',(EV,float(p_bid),-float(q_bid)/1000,dt_sim_time))
df_awarded_bids = df_awarded_bids.append(pandas.DataFrame(columns=df_awarded_bids.columns,data=[[dt_sim_time,EV,float(p_bid),-float(q_bid)/1000,'D']]),ignore_index=True)
#elif active == -1:
# gridlabd_functions.set('EV_inverter_'+EV[3:],'P_Out',1000*df_bids_EV['q_sell'].loc[df_bids_EV['appliance_id'] == batt_number].values[0]) #kW -> W
else:
gridlabd.set_value('EV_inverter_'+EV[3:],'P_Out','0.0')
print('GLD battery model is not used for EV, manual updating!')
df_bids_EV['SOC_t'] = df_bids_EV['SOC_t'] + df_bids_EV['active_t']*df_bids_EV['q_buy']/12
df_bids_EV['soc_t'] = df_bids_EV['SOC_t']/df_bids_EV['SOC_max']
return df_bids_EV,df_awarded_bids
#All EVs switch on for which bid is more or equal to published clearing price
def set_EV_by_price(dt_sim_time,df_bids_EV,mean_p,var_p,Pd,df_awarded_bids):
#Determine activity
df_bids_EV.at[(df_bids_EV['p_buy'] >= Pd) & (df_bids_EV['SOC_t'] < df_bids_EV['SOC_max']),'active_t'] = 1
#df_bids_EV.at[(df_bids_EV['p_sell'] <= Pd) & (df_bids_EV['SOC'] > 0.0),'active'] = -1
#Set DB and GLD
df_bids_EV,df_awarded_bids = set_EV_GLD(dt_sim_time,df_bids_EV,df_awarded_bids)
return df_bids_EV,df_awarded_bids
#Only EVs switch on which got explicitely cleared even
#Not awarded EVs don't switch on even if their bid equals the bid of the marginal demand unit/the clearing price
def set_EV_by_award(dt_sim_time,df_bids_EV,market,df_awarded_bids):
try:
list_awards_D = market.D_awarded[:,3]
list_awards_D = [x for x in list_awards_D if x is not None]
except:
list_awards_D = []
for bidder in list_awards_D:
if 'EV_' in bidder:
df_bids_EV.at[bidder,'active_t'] = 1
#Set DB and GLD
df_bids_EV, df_awarded_bids = set_EV_GLD(dt_sim_time,df_bids_EV,df_awarded_bids)
return df_bids_EV,df_awarded_bids