def set_HVAC_T(dt_sim_time,df_bids_HVAC,mean_p,var_p, Pd): df_bids_HVAC.at[df_bids_HVAC['bid_p'] >= Pd,'active'] = 1 df_bids_HVAC['p_max'] = mean_p+df_bids_HVAC['k']*var_p df_bids_HVAC['temp'] = df_bids_HVAC['T_min'] + (df_bids_HVAC['p_max'] - Pd)*(df_bids_HVAC['T_h0'] - df_bids_HVAC['T_min'])/(2*df_bids_HVAC['k']*var_p) df_bids_HVAC['temp'] = df_bids_HVAC[['temp','T_min']].max(axis=1) df_bids_HVAC['temp'] = df_bids_HVAC[['temp','T_h0']].min(axis=1) df_bids_HVAC.loc[df_bids_HVAC['system_mode'] == 'HEAT','heating_setpoint'] = df_bids_HVAC['temp'] df_bids_HVAC.loc[df_bids_HVAC['system_mode'] == 'HEAT','cooling_setpoint'] = p_max #where should those be to not harm? df_bids_HVAC.loc[df_bids_HVAC['system_mode'] == 'OFF','cooling_setpoint'] = p_max #where should those be to not harm? df_bids_HVAC['temp'] = df_bids_HVAC['T_max'] + (df_bids_HVAC['p_max'] - Pd)*(df_bids_HVAC['T_c0'] - df_bids_HVAC['T_max'])/(2*df_bids_HVAC['k']*var_p) df_bids_HVAC['temp'] = df_bids_HVAC[['temp','T_c0']].max(axis=1) df_bids_HVAC['temp'] = df_bids_HVAC[['temp','T_max']].min(axis=1) df_bids_HVAC.loc[df_bids_HVAC['system_mode'] == 'COOL','cooling_setpoint'] = df_bids_HVAC['temp'] df_bids_HVAC.loc[df_bids_HVAC['system_mode'] == 'COOL','heating_setpoint'] = 0.0 #where should those be to not harm? df_bids_HVAC.loc[df_bids_HVAC['system_mode'] == 'OFF','heating_setpoint'] = 0.0 #where should those be to not harm? #Write to GridlabD objects #Read information via database later on! for house in houselist: cooling_setpoint = df_bids_HVAC['cooling_setpoint'].loc[df_bids_HVAC['house_name'] == house].values[0] heating_setpoint = df_bids_HVAC['heating_setpoint'].loc[df_bids_HVAC['house_name'] == house].values[0] active = df_bids_HVAC['active'].loc[df_bids_HVAC['house_name'] == house].values[0] #Can I do that for the whole table with a dataframe? #mysql_functions.set_values('market_HVAC_meter', '(system_mode,heating_setpoint,cooling_setpoint,active,timedate,appliance_id)',('OFF',float(heating_setpoint),float(cooling_setpoint),int(active),dt_sim_time,houselist.index(house)+1,)) #Write to gridlabd (to be done from mysql in future) gridlabd_functions.set(house,'cooling_setpoint',cooling_setpoint) gridlabd_functions.set(house,'heating_setpoint',heating_setpoint) return df_bids_HVAC
def transactive_price(house, T_curr, T_c_set, T_h_set, mean_p, var_p, epsilon): T_zero_h = T_h_set + (T_c_set - T_h_set)/2 - epsilon/2 T_zero_c = T_c_set - (T_c_set - T_h_set)/2 + epsilon/2 T_max = float(gridlabd_functions.get(house,'T_max')['value'][1:]) T_min = float(gridlabd_functions.get(house,'T_min')['value'][1:]) k = float(gridlabd_functions.get(house,'k')['value'][1:]) if T_curr > T_max or T_curr < T_min: return 1 #max price # cooling elif T_curr > T_zero_c: #Remains here if comfort settings are changed during operation m = (mean_p + k * np.sqrt(var_p))/(T_max - T_zero_c) gridlabd_functions.set(house,'m',m) n = - m * T_zero_c gridlabd_functions.set(house,'n',n) return (m * T_curr + n) # heating elif T_curr < T_zero_h: m = (mean_p + k * np.sqrt(var_p))/(T_min - T_zero_h) gridlabd_functions.set(house,'m',m) n = - m * T_zero_h gridlabd_functions.set(house,'n',n) return (m * T_curr + n) else: return 0
def shutdown_house(unresp_load,dt_sim_time,houses_off=[],market=None): if market: Pmax = market.Pmax else: Pmax = 100.0 while True: house_OFF = np.random.choice(houselist_inflex) while house_OFF in houses_off: house_OFF = np.random.choice(houselist_inflex) house_currHVAC = float(gridlabd_functions.get(house_OFF,'hvac_load')['value'][:-3]) if house_currHVAC > 0.0: unresp_load -= house_currHVAC print 'Switch off '+str(house_OFF)+' to '+str(unresp_load) gridlabd_functions.set(house_OFF,'thermostat_control','NONE') gridlabd_functions.set(house_OFF,'system_mode','OFF') houses_off += [house_OFF] mysql_functions.set_values('system_operations', '(timedate,q,costs,appliance_name)',(dt_sim_time,house_currHVAC,Pmax,'HVAC'+house_OFF[3:],)) break print 'Ready: '+str(unresp_load) return unresp_load, houses_off
def switch_on(s_SO): #houses if 'GLD' in s_SO['appliance_name']: gridlabd_functions.set(s_SO['appliance_name'],'thermostat_control','FULL') #batteries elif 'Bat' in s_SO['appliance_name']: gridlabd_functions.set('Bat_inverter_'+s_SO['appliance_name'][8:],'P_Out',-s_SO['q']*1000) #EV elif 'EV' in s_SO['appliance_name']: gridlabd_functions.set('EV_inverter_'+s_SO['appliance_name'][3:],'P_Out',-s_SO['q']*1000) else: print s_SO['appliance_name']+' is not recognized' return
def switch_off(s_awarded,dt_sim_time): #houses if 'GLD' in s_awarded['appliance_name']: gridlabd_functions.set(s_awarded['appliance_name'],'thermostat_control','NONE') #batteries elif 'Bat' in s_awarded['appliance_name']: gridlabd_functions.set('Bat_inverter_'+s_awarded['appliance_name'][8:],'P_Out',0.0) #EV elif 'EV' in s_awarded['appliance_name']: gridlabd_functions.set('EV_inverter_'+s_awarded['appliance_name'][3:],'P_Out',0.0) else: print s_awarded['appliance_name']+' is not recognized' mysql_functions.set_values('system_operations', '(timedate,q,costs,appliance_name)',(dt_sim_time,float(s_awarded['q_bid']),float(s_awarded['p_bid']),s_awarded['appliance_name'],)) return
#C = 3262 + random.uniform(-10,10) #for test purposes [in kW] C = 800.0 #capacity restriction mysql_functions.set_values('capacity_restrictions', '(cap_rest,timedate)',(C,dt_sim_time,)) #in kW retail.sell(C,supply_costs,gen_name='WS') #in [USD/kW] #How can I tweak clearing that we can name biider 'WS'? mysql_functions.set_values('supply_bids', '(bid_price,bid_quantity,timedate,gen_name)',(float(supply_costs),float(C),dt_sim_time,'wholesale')) ### #Market clearing (kW) ### retail.clear() Pd = retail.Pd # cleared demand price Qd = retail.Qd #in kW mysql_functions.set_values('clearing_pq', '(clearing_price,clearing_quantity,timedate)',(Pd,Qd,dt_sim_time,)) #for calculation of monthly bill in GridlabD / to be deleted when DB is active in GridlabD gridlabd_functions.set('retail','price',Pd) ### #Redistribute prices and quantities to market participants ### #Save market result for each HVAC system #df_bids_HVAC = HHfct.set_HVAC_T(dt_sim_time,df_bids_HVAC,mean_p,var_p, Pd) #Sets new T setpoint according to price #df_bids_HVAC = HHfct.set_HVAC_by_price(dt_sim_time,df_bids_HVAC,mean_p,var_p, Pd) #Switches the HVAC system on and off directly (depending on bid >= p) df_bids_HVAC = HHfct.set_HVAC_by_award(dt_sim_time,df_bids_HVAC,retail) #Switches the HVAC system on and off directly (depending on award) #Battery #df_bids_battery = HHfct.set_battery_by_price(dt_sim_time,df_bids_battery,mean_p,var_p, Pd) #Controls battery based on bid <-> p df_bids_battery = HHfct.set_battery_by_award(dt_sim_time,df_bids_battery,retail) #Controls battery based on award
def initialize(): mysql_functions.clear_databases() print('Initialize') #Start time of simulation is '2017-07-01 12:00:00' EST+5 - get this from GLOBALS or likewise interval = int(gridlabd_functions.get('retail', 'interval')['value']) #in seconds #prev_timedate = datetime.datetime(2015, 6, 30, 23, 60 - (interval/60)) #prev_timedate = datetime.datetime(2015, 7, 1, 4, 60 - (interval/60)) dt = parser.parse(os.getenv("clock")) prev_timedate = dt - timedelta(minutes=interval / 60) #Get list of house objects in GLM file and assign to global GLD variable "houselist" #houses = gridlabd_functions.find('class=house') #houselist = []; #Read in wholesale market prices df_prices = pandas.read_csv('ercot_2017.csv', parse_dates=[0]) mysql_functions.set_WSmarket(df_prices) #Check if downgrading pycurl changes gridlabd_functions # from sqlalchemy import create_engine # engine = create_engine("mysql://*****:*****@127.0.0.1/gridlabd") # con = engine.connect() # df_prices.to_sql(name='WS_market',con=con,if_exists='append') # con.close() for house in houselist: #houselist.append(name) #Fills TABLE market_houses (can this be done via GridlabD directly?) mysql_functions.set_values('market_houses', '(house_name)', (house, )) #Fills TABLE market_appliances (can this be done via GridlabD directly?) #HVAC k = float(gridlabd_functions.get(house, 'k')['value']) T_min = float(gridlabd_functions.get(house, 'T_min')['value']) T_max = float(gridlabd_functions.get(house, 'T_max')['value']) heat_q = float( gridlabd_functions.get( house, 'heating_demand')['value'][1:-3]) #heating_demand is in kW hvac_q = float( gridlabd_functions.get( house, 'cooling_demand')['value'][1:-3]) #cooling_demand is in kW #mysql_functions.set_values('market_HVAC', '(house_name,appliance_name,k,T_min,T_max,P_heat,P_cool)',(house['name'],'HVAC_'+str(houses.index(house)+1),k,T_min,T_max,heat_q,hvac_q,)) mysql_functions.set_values( 'market_HVAC', '(house_name,appliance_name,k,T_min,T_max,P_heat,P_cool)', ( house, 'HVAC_' + house[4:], k, T_min, T_max, heat_q, hvac_q, )) #Fills TABLE market_appliance_meter #HVAC heating_setpoint = float( gridlabd_functions.get(house, 'heating_setpoint')['value'][1:-5]) cooling_setpoint = float( gridlabd_functions.get(house, 'cooling_setpoint')['value'][1:-5]) #Set values for previous period, i.e. start - interval #mysql_functions.set_values('market_HVAC_meter', '(system_mode,heating_setpoint,cooling_setpoint,active,timedate,appliance_id)',('OFF',heating_setpoint,cooling_setpoint,0,prev_timedate,houses.index(house)+1,)) mysql_functions.set_values( 'market_HVAC_meter', '(system_mode,av_power,heating_setpoint,cooling_setpoint,active,timedate,appliance_id)', ( 'OFF', 0.0, heating_setpoint, cooling_setpoint, 0, prev_timedate, int(house.split('_')[-1]), )) #gridlabd_functions.set('houselist',';'.join(houselist)) #batteries = gridlabd_functions.find('class=battery') for battery in batterylist: house_name = 'GLD_' + battery[8:] #Fills TABLE market_appliances SOC_max = float( gridlabd_functions.get(battery, 'battery_capacity')['value'] [:-3]) / 1000 #Wh in Gridlabd -> kWh str_i_max = gridlabd_functions.get(battery, 'I_Max')['value'][:-2].replace( '-', '+') i_max = str_i_max.split('+')[1] u_max = float(gridlabd_functions.get(battery, 'V_Max')['value'] [:-2]) * float(i_max) / 1000 #W -> kW #better inverter? eff = float( gridlabd_functions.get(battery, 'base_efficiency')['value'][:-5]) mysql_functions.set_values( 'market_battery', '(house_name,appliance_name,appliance_id,SOC_max,u_max,eff)', ( house_name, battery, int(battery.split('_')[-1]), SOC_max, u_max, eff, )) #Fills TABLE market_appliance_meter SOC_0 = float( gridlabd_functions.get(battery, 'state_of_charge')['value'][:-3]) * SOC_max mysql_functions.set_values('market_battery_meter', '(SOC,active,timedate,appliance_id)', ( SOC_0, 0, prev_timedate, int(battery.split('_')[-1]), )) for EV in EVlist: house_name = 'GLD_' + EV[3:] #Fills TABLE market_appliances SOC_max = float( gridlabd_functions.get(EV, 'battery_capacity')['value'] [:-3]) / 1000 #Wh in Gridlabd -> kWh str_i_max = gridlabd_functions.get(EV, 'I_Max')['value'][:-2].replace( '-', '+') i_max = str_i_max.split('+')[1] u_max = float(gridlabd_functions.get(EV, 'V_Max')['value'] [:-2]) * float(i_max) / 1000 #W -> kW #better inverter? eff = float( gridlabd_functions.get(EV, 'base_efficiency')['value'][:-5]) charging_type = gridlabd_functions.get(EV, 'charging_type')['value'] k = gridlabd_functions.get(EV, 'k')['value'] mysql_functions.set_values( 'market_EV', '(house_name,appliance_name,appliance_id,SOC_max,u_max,eff,charging_type,k)', ( house_name, EV, int(EV.split('_')[-1]), SOC_max, u_max, eff, charging_type, k, )) #Fills TABLE market_appliance_meter mysql_functions.set_values( 'market_EV_meter', '(connected,SOC,active,timedate,appliance_id)', ( 0, 0, 0, prev_timedate, int(EV.split('_')[-1]), )) #Set all cars offline/disconnected in the beginning gridlabd_functions.set(EV, 'generator_status', 'OFFLINE') df_events = pandas.read_csv('EV_events.csv', index_col=[0]) df_events.to_csv('EV_events_pop.csv') # PVs = gridlabd_functions.find('class=solar') for pv in pvlist: house_name = 'GLD_' + pv[3:] #Fills TABLE market_appliances inverter_name = 'PV_inverter_' + pv[3:] rated_power = float( gridlabd_functions.get(inverter_name, 'rated_power')['value'][:-3]) / 1000 mysql_functions.set_values( 'market_pv', '(house_name,appliance_name,inverter_name,appliance_id,rated_power)', ( house_name, pv, inverter_name, int(pv.split('_')[-1]), rated_power, )) #Fills TABLE market_appliance_meter production = float( gridlabd_functions.get(inverter_name, 'P_Out')['value'][:-3]) / 1000 mysql_functions.set_values('market_pv_meter', '(P_Out,timedate,appliance_id)', ( production, prev_timedate, int(pv.split('_')[-1]), )) return dt
import os import gridlabd_functions import random import pandas import pycurl import json from StringIO import StringIO #Get list of house objects in GLM file and assign to global GLD variable "houselist" houses = gridlabd_functions.find('class=house') houselist = [] for house in houses: name = house['name'] houselist.append(name) gridlabd_functions.set('houselist', ';'.join(houselist)) print(houselist)
soc_setpoint = 0.7 # Baterry SOC target #Get state of charge of Battery object soc_read = gridlabd_functions.get('node1_batt', 'state_of_charge') soc = float(soc_read['value'][1:-3]) #Get power in the mains object p_hApp_read = gridlabd_functions.get('meter1', 'measured_real_power') p_hApp = float(p_hApp_read['value'][1:-2]) #Print out timestep and state of charge sim_time = os.getenv("clock") print soc # print sim_time print p_hApp k = 20000 # Controller gain p_control = -(soc_setpoint - soc) * k gridlabd_functions.set('node1_batt_inv', 'P_Out', p_control) # Write p_control to battery #batt_delta = -(p_cc - p_hApp) #gridlabd_functions.set('node1_batt_inv','P_Out',batt_delta) #Write P_Out to battery object ''' if sim_time[14] != "4": set_P_out=1000 gridlabd_functions.set('node1_batt_inv','P_Out',set_P_out) #Write P_Out to battery object else: gridlabd_functions.set('node1_batt_inv','P_Out',set_P_out) # print "P_OUT:", set_P_out'''
#Get power in the mains object p_hApp_read = gridlabd_functions.get('meter1','measured_real_power') print "p_hApp_read: ", p_hApp_read p_hApp = float(p_hApp_read['value'][1:-2]) ''' #Print out timestep and state of charge sim_time = os.getenv("clock") #print soc # print sim_time #print p_hApp #k = 20000 # Controller gain #p_control = -(soc_setpoint - soc)*k #gridlabd_functions.set('node1_batt_inv','P_Out',p_control) # Write p_control to battery #batt_delta = -(p_cc - p_hApp) #gridlabd_functions.set('node1_batt_inv','P_Out',batt_delta) #Write P_Out to battery object set_P_out = 0.099 if sim_time[14] != "4": set_P_out = 1000 gridlabd_functions.set('node1_batt_inv_H1', 'P_Out', set_P_out) #Write P_Out to battery object else: gridlabd_functions.set('node1_batt_inv_H1', 'P_Out', set_P_out) # print "P_OUT:", set_P_out
prices = DataPreLoaded_Prices("pricesCurrent.csv", 0) Q, U, boundsFlag = LC_Combined_No_Bounds_MultiHome( NLweight, prices, sellFactor, q0, LCscens, GCtime, umaxo, umino, qmaxo, qmino, number_of_houses) U = -U print 'U: ', U #u = 1000*U[0][0] #houses_dict[1]['Battery_LC_U'] = u u = BatteryProfiles(U, batt_solar_houses) print 'Battery Profile: ', u #houses_dict[1]['Battery_LC_U'] = u[0] #houses_dict[3]['Battery_LC_U'] = u[1] print 'GLD set u: ', u for idx, b in enumerate(battery_GLD): gridlabd_functions.set(b, 'P_Out', u[idx]) #print 'House number: ', b #print 'Power value: ', u[idx] #gridlabd_functions.set('node1_batt_inv_H1','P_Out',u[0]) #gridlabd_functions.set('node1_batt_inv_H3','P_Out',u[1]) # Creating file with new data #rwText.create_file_json('TestGLD.json', houses_dict) ############################################### # Old Functions ''' ######## LC Controller House 1 ######### try: with open('home_U.json') as file: pass
import gridlabd_functions import os #Get state of charge of Battery object SOC = gridlabd_functions.get('node1_batt', 'state_of_charge') print(SOC['value']) print(os.getenv("clock")) #Write P_Out to battery object gridlabd_functions.set('node1_batt_inv', 'P_Out', -1000)
def set_HVAC_setpoint(house,control_type,bid_price,Pd): #Set state: Load is active in that period if bid_price >= Pd: gridlabd_functions.set(house,'state',True) else: gridlabd_functions.set(house,'state',False) if 'deadband' in control_type: if bid_price >= Pd and bid_price > 0: #to exclude consumption when price is zero #switch on HVAC if 'COOL' in gridlabd_functions.get(house,'bid_mode')['value']: gridlabd_functions.set(house,'system_mode','COOL') elif 'HEAT' in gridlabd_functions.get(house,'bid_mode')['value']: gridlabd_functions.set(house,'system_mode','HEAT') else: print('Check bid mode - there might be an inconsistency in bidding and actual behavior') return else: #turn off HVAC gridlabd_functions.set(house,'system_mode','OFF') #gridlabd_functions.set('control_1','control','OFF') return elif ('trans' in control_type): m = float(gridlabd_functions.get(house,'m')['value']) n = float(gridlabd_functions.get(house,'n')['value']) T_max = float(gridlabd_functions.get(house,'T_max')['value'][1:]) T_min = float(gridlabd_functions.get(house,'T_min')['value'][1:]) #change setpoint on HVAC if 'COOL' in gridlabd_functions.get(house,'bid_mode')['value']: gridlabd_functions.set(house,'system_mode','COOL') # calculate new setpoint T_c_set_new = (Pd - n)/m if T_c_set_new > T_max: gridlabd_functions.set(house,'cooling_setpoint',T_max) else: gridlabd_functions.set(house,'cooling_setpoint',T_c_set_new) elif 'HEAT' in gridlabd_functions.get(house,'bid_mode')['value']: gridlabd_functions.set(house,'system_mode','HEAT') #calculate new setpoint T_h_set_new = (Pd - n)/m if T_h_set_new < T_min: gridlabd_functions.set(house,'heating_setpoint',T_min) else: gridlabd_functions.set(house,'heating_setpoint',T_h_set_new) else: gridlabd_functions.set(house,'system_mode','OFF') return else: print('HVAC could not be set') return
def bid_rule_HVAC(house, mean_p, var_p, interval): control_type = gridlabd_functions.get(house,'control_type')['value'] k = float(gridlabd_functions.get(house,'k')['value'][1:]) T_c_set = float(gridlabd_functions.get(house,'T_c_set_0')['value'][1:]) T_h_set = float(gridlabd_functions.get(house,'T_h_set_0')['value'][1:]) #T_curr = float(gridlabd_functions.get(house,'air_temperature')['value'][1:-5]) #calculate energy hvac_q = float(gridlabd_functions.get(house,'cooling_demand')['value'][1:-3]) * interval / (60*60) heat_q = float(gridlabd_functions.get(house,'heating_demand')['value'][1:-3]) * interval / (60*60) #State of appliance in previous period status = int(gridlabd_functions.get(house,'state')['value']) if 'deadband' in control_type: # cooling if T_curr > T_c_set + k: bid_price = 1 bid_quantity = hvac_q gridlabd_functions.set(house,'bid_mode','COOL') return bid_quantity, bid_price, status # heating elif T_curr < T_h_set - k: bid_price = 1 bid_quantity = heat_q gridlabd_functions.set(house,'bid_mode','HEAT') return bid_quantity, bid_price, status # no activity else: bid_price = 0 bid_quantity = 0 gridlabd_functions.set(house,'bid_mode','NONE') return bid_quantity, bid_price, status elif 'trans' in control_type: # Non-bid region size between cooling and heating [F] epsilon = 2 bid_price = transactive_price(house, T_curr, T_c_set, T_h_set, mean_p, var_p, epsilon) if T_curr > T_c_set - (T_c_set - T_h_set)/2 + epsilon/2: #above T_c_zero bid_quantity = hvac_q gridlabd_functions.set(house,'bid_mode','COOL') elif T_curr < T_h_set + (T_c_set - T_h_set)/2 - epsilon/2: #below T_h_zero bid_quantity = heat_q gridlabd_functions.set(house,'bid_mode','HEAT') else: bid_quantity = 0 gridlabd_functions.set(house,'bid_mode','NONE') return bid_quantity, bid_price, status else: print('Bid reserve price could not be calculated') return 0,0,0