def bal_vs_cap_data(): number_of_nodes = 8 length_of_timeseries = 280512 hours_per_year = length_of_timeseries/32 modes = ['lin', 'sqr'] scalefactors = np.linspace(0, 2, 41) #scalefactors = [0.1, 1.45] for mode in modes: balancing_energy = [] # this is for saving the balancing energy total_capacity = [] max_balancing = [] # this is for saving the balancing capacity balancing_caps99 = [] # this is for saving the balancing capacity, # found as the 99% quantile of the balancing # energy if mode == 'lin': h0 = au.get_quant_caps(filename=\ './results/w_aHE_copper_lin_flows.npy') if mode == 'sqr': h0 = au.get_quant_caps(filename=\ './results/w_aHE_copper_sqr_flows.npy') total_caps_q99 = np.sum(au.biggestpair(h0)) print total_caps_q99 for a in scalefactors: flow_calc = fc.FlowCalculation('w', 'aHE', ''.join([str(a), 'q99']), mode) filename = ''.join([str(flow_calc), '.npz']) nodes = world_Nodes(load_filename = filename) total_balancing_power = sum([sum(n.balancing) for n in nodes]) total_mean_load = np.sum([n.mean for n in nodes]) normalized_BE = total_balancing_power/\ (length_of_timeseries*total_mean_load) balancing_energy.append(normalized_BE) max_balancing.append(np.sum(np.max(n.balancing) for n in nodes)\ /total_mean_load) balancing_caps99.append(np.sum(au.get_q(n.balancing,0.99) for n in nodes)/total_mean_load) total_capacity.append(a*total_caps_q99) if mode == 'lin': TC_lin = total_capacity BE_lin = balancing_energy BC_lin = max_balancing BC99_lin = balancing_caps99 if mode == 'sqr': TC_sqr = total_capacity BE_sqr = balancing_energy BC_sqr = max_balancing BC99_sqr = balancing_caps99 np.savez('./results/w_bal_vs_cap_data99.npz', TC_lin=TC_lin,\ BE_lin=BE_lin, BC_lin=BC_lin, BC99_lin=BC99_lin,\ TC_sqr=TC_sqr, BE_sqr=BE_sqr, BC_sqr=BC_sqr, BC99_sqr=BC99_sqr)
def get_total_capacity(h0): """ This function takes a list of capacity-vectors and returns the total capacity installed. """ total_capacity = sum(au.biggestpair(h0)) return total_capacity
def add_instance(self, N, F, FC): inst={} inst['alphas'] = [n.alpha for n in N] inst['gammas'] = [n.gamma for n in N] length_of_timeseries = len(N[0].balancing) # save data, that requires a Flowcalculation object for specification inst['FlowCalculation'] = FC if FC.capacities!='zerotrans': inst['BE'] = [np.sum(n.balancing)/\ length_of_timeseries for n in N] inst['BC'] = [au.get_q(n.balancing, 0.99) for n in N] # Save flow histograms of all the flow along the links flow_histograms = [] for link in xrange(F.shape[0]): flow, count = myhist(F[link], bins=200) flow_histograms.append(np.array([flow, count])) inst['flowhists'] = flow_histograms if FC.hourly_flowhist: # flow histograms, of flow along the links, conditioned # on the hour. hourly_flow_histograms = [] for link in xrange(F.shape[0]): hour_hist_list = [] for hour in range(24): hour_indices = [i for i in xrange(F.shape[1]) \ if np.mod(i, 24)==hour] hour_flows = F[link][hour_indices] flow, count = myhist(hour_flows, bins=200) hour_hist_list.append(np.array([flow, count])) hourly_flow_histograms.append(hour_hist_list) inst['hourly_flowhists'] = hourly_flow_histograms if FC.capacities[-3:len(FC.capacities)] == "q99": inst['Total_TC'] = TC_from_FC(FC)[0] inst['TC'] = TC_from_FC(FC)[1] elif FC.capacities == 'copper': h0 = au.get_quant_caps(F=F) inst['TC'] = h0 inst['Total_TC'] = np.sum(au.biggestpair(h0)) else: # that is if this is a zero transmission case inst['Total_TC'] = 0 balancing_timeseries = [] for n in N: balancing_timeseries.append(-au.get_negative(n.mismatch)) inst['BE'] = [np.sum(bal)/length_of_timeseries\ for bal in balancing_timeseries] inst['BC'] = [au.get_q(bal, 0.99) for bal in balancing_timeseries] self.cache.append(inst)
def TC_from_FC(FC): copperFC = FC.copy() copperFC.capacities = "copper" copperflowfile = ''.join(["results/", str(copperFC),\ '_flows.npy']) h0 = au.get_quant_caps(filename=copperflowfile) # expecting capacities of format: 0.35q99 scalefactor = float(FC.capacities[0:-3]) total_TC = scalefactor*np.sum(au.biggestpair(h0)) return total_TC, h0
def extract_BC_vs_TC_data(solvermode, TCscalefactors, savefilename=None): """ This function returns the a dataset with total balancing capacity (normalized to the total mean load) of the world along with the total transmission capacity of the world. The balancing capacity found as the maximum balancing over a timeseries, as well as the 99% quantile is returned. """ h0 = au.get_quant_caps(filename =\ ''.join(['./results/w_aHE_copper_', solvermode, '_flows.npy'])) total_TC_q99 = np.sum(au.biggestpair(h0)) BC_max = [] BC_q99 = [] TC = [] for a in TCscalefactors: flow_calc = fc.FlowCalculation('w', 'aHE',\ ''.join([str(a), 'q99']), solvermode) filename = ''.join([str(flow_calc), '.npz']) nodes = world_Nodes(load_filename = filename) total_mean_load = np.sum([n.mean for n in nodes]) BC_max.append(np.sum(np.max(n.balancing) for n in nodes)\ /total_mean_load) BC_q99.append(np.sum(au.get_q(n.balancing, 0.99) for n in nodes)\ /total_mean_load) TC.append(a*total_TC_q99) if not savefilename: savefilename = ''.join(['./results/BC_TC_data', solvermode, '.npz']) np.savez(savefilename, TC=TC, BC_max=BC_max, BC_q99=BC_q99)
def get_TCs(flowcalc, datapath='./results/AlphaSweepsCopper/'): filename = filename_from_flowcalc(flowcalc) h0 = get_data(filename, 'TC', path=datapath) return au.biggestpair(h0)
import aurespf.solvers as au import figutils as fig from FlowCalculation import FlowCalculation from europe_plusgrid import europe_plus_Nodes layouts = ['Europe_RU', 'Europe_NA', 'Europe_ME', 'Europe_RU_NA_ME'] modes = ['lin', 'sqr'] datapath = './results/Europeplus/' Europe = europe_plus_Nodes(admat='./settings/Europeadmat.txt') internal_links = [au.AtoKh(Europe)[-1][i][0]\ for i in range(len(au.AtoKh(Europe)[-1]))] for layout in layouts: admat = './settings/' + layout + 'admat.txt' print admat N = europe_plus_Nodes(admat=admat) for mode in modes: fc = FlowCalculation(layout, 'aHE', 'copper', mode) filename = str(fc) + '.pkl' internal_indices, external_indices = fig.get_internal_external_link_indices(N, internal_links) all_TC = au.biggestpair(fig.get_data(filename, 'TC', path=datapath)) print fc.pretty_layout(), fc.pretty_solvermode() for link_index in external_indices: print au.AtoKh(N)[-1][link_index] print all_TC[link_index]
filename = str(fc) + '.pkl' filename_sqr = str(fc_sqr) + '.pkl' flowfilename = './results/' + fc.layout + '_aHE_copper_lin_flows.npy' flowfilename_sqr = './results/' + fc.layout + '_aHE_copper_sqr_flows.npy' unnormalized_BE = [fig.get_data(filename, 'BE', \ path=datapath)[n.id] for n in N] unnormalized_BC = [fig.get_data(filename, 'BC', \ path=datapath)[n.id] for n in N] unnormalized_BC_sqr = [fig.get_data(filename_sqr, 'BC', \ path=datapath)[n.id] for n in N] BE.append(np.sum(unnormalized_BE)/total_mean_load) BC.append(np.sum(unnormalized_BC)/total_mean_load) BC_sqr.append(np.sum(unnormalized_BC_sqr)/total_mean_load) LI = au.linfo(admat) energywiseTC = au.biggestpair(au.get_quant_caps(filename=flowfilename)) energywiseTC_sqr = au.biggestpair(au.get_quant_caps(filename=flowfilename_sqr)) print np.sum(energywiseTC) TC.append(np.sum([energywiseTC[i]*float(LI[i][2]) \ for i in range(len(LI))])/(1e3*total_mean_load)) TC_sqr.append(np.sum([energywiseTC_sqr[i]*float(LI[i][2]) \ for i in range(len(LI))])/(1e3*total_mean_load)) print TC index1 = np.arange(len(BE)) left = index1 + 0.5*barwidth left2 = left + 0.5*barwidth plt.ion() ax1 = plt.subplot(3,1,1) if not interactive: plt.gcf().set_size_inches([7.5,12]) plt.gcf().set_dpi(400)
def balancing_energy(): """ This function replicates Figure 5 in Rolando et. al 2013. """ europe_raw = EU_Nodes() europe_copper = EU_Nodes(load_filename = "copper.npz") ########### Calculate the minimum balancing energy ############# # The balancing energy is the smallest in the case of unconstrained # flow. This is the total balancing energy for all of Europe, # averaged over the time and normalized to the total mean load total_balancing_copper = np.sum(n.balancing for n in europe_copper) mean_balancing_copper = np.mean(total_balancing_copper) total_mean_load = np.sum(n.mean for n in europe_copper) min_balancing = mean_balancing_copper/total_mean_load print "The minimum balancing energy is:", min_balancing ######### Calculate the maximum balancing energy ############# # The maximum balancing energy is the negative mismatch from # the raw date, that is the unsolved system, before any flow # has been taken into account. # Note, that contratry to total_balancing_copper (a timeseries) # total_balancing raw is just a number as it has been summed over # time. total_balancing_raw = -np.sum(np.sum(n.mismatch[n.mismatch<0]) for n in europe_raw) mean_balancing_raw = total_balancing_raw/\ total_balancing_copper.size max_balancing = mean_balancing_raw/total_mean_load #### Calculate the current total capacity ################ # the hardcoded 10000 stems from a link, we don't know the # actual capacity of, so it has been set to 10000 in the # eadmat.txt file. current_total_cap = sum(au.biggestpair(au.AtoKh(europe_raw)[-2])) - 10000 print min_balancing, max_balancing scalefactorsA = [0.5, 1, 2, 4, 6, 8, 10, 12, 14] #scalefactorsA = np.linspace(0,1,11) # for use with the alternative A rule smoothA_cap, smoothA = get_bal_vs_cap(scalefactorsA, 'lin_int_', get_h0_A) plt.plot(smoothA_cap, smoothA(smoothA_cap), 'r-', label='Interpolation A') scalefactorsB = np.linspace(0, 2.5, 10) smoothB_cap, smoothB = get_bal_vs_cap(scalefactorsB, 'linquant_int_', get_h0_B) plt.plot(smoothB_cap, smoothB(smoothB_cap), 'g-', label='Interpolation B') quantiles = [0.5, 0.8, 0.9, 0.95, 0.97, 0.99, 0.995, 0.999, 0.9995, 0.9999, 1] smoothC_cap,smoothC = get_bal_vs_cap(quantiles, 'quant_int_', get_h0_C) plt.plot(smoothC_cap,smoothC(smoothC_cap),'b-', label="Interpolation C") plt.hlines(min_balancing, 0, 900, linestyle='dashed') plt.vlines(current_total_cap/1000, 0, 0.27, linestyle='dashed') plt.xlabel("Total installed transmission capacity, [GW]") plt.ylabel("Balancing energy [normalized]") plt.axis([0, 900.1, .125, .27]) plt.yticks([.15,.17,.19,.21,.23,.25,.27]) plt.xticks([0,100,200,300,400,500,600,700,800,900]) plt.legend() plt.tight_layout() plt.savefig("./Plotting practice/balancing.pdf")
def make_europeplus_barplot(ydatalabel, interactive=False,\ barwidth=0.5): """ Creates a bar plot showing the property specified by ydatalabel in the five different layouts in the Europeplus configuration. """ plt.close() plt.rcParams['axes.color_cycle'] = color_cycle if interactive: plt.ion() datapath = "./results/Europeplus/" savepath = "./results/figures/Europeplusfigs/" sqr_and_lin = False if ydatalabel != 'BE': sqr_and_lin = True layoutlist = ['Europe',\ 'Europe-RU', 'Europe-NA', 'Europe-ME', 'Europe-RU-NA-ME'] ydata_lin = [] ydata_sqr = [] ydata_lin_ext = [] ydata_sqr_ext = [] for l in layoutlist: layout = l.replace('-', '_') fc_string_lin = str(FlowCalculation(layout, 'aHE', 'copper', 'lin')) fc_string_sqr = str(FlowCalculation(layout, 'aHE', 'copper', 'sqr')) admat = "./settings/" + layout + "admat.txt" N = europe_plus_Nodes(admat=admat) if layout=='Europe': internal_links = [au.AtoKh(N)[-1][i][0] \ for i in range(len(au.AtoKh(N)[-1]))] total_mean_load = np.sum([n.mean for n in N]) if ydatalabel == 'BE': unnormalized_data = get_data(fc_string_lin + '.pkl', \ ydatalabel, path=datapath) ydata_lin.append(np.sum(unnormalized_data)/total_mean_load) ylabel = "Backup energy [normalized]" elif ydatalabel == 'BC': unnormalized_data_lin = get_data(fc_string_lin + '.pkl',\ ydatalabel, path=datapath) unnormalized_data_sqr = get_data(fc_string_sqr + '.pkl',\ ydatalabel, path=datapath) ydata_lin.append(np.sum(unnormalized_data_lin)/total_mean_load) ydata_sqr.append(np.sum(unnormalized_data_sqr)/total_mean_load) ylabel = "Backup capacity [normalized]" elif ydatalabel == 'TC': internal_indices, external_indices =\ get_internal_external_link_indices(N, internal_links) print internal_indices, external_indices all_lin_TC = au.biggestpair(\ get_data(fc_string_lin + '.pkl', 'TC', path=datapath)) all_sqr_TC = au.biggestpair(\ get_data(fc_string_sqr + '.pkl', 'TC', path=datapath)) ydata_lin.append(\ sum([all_lin_TC[i] for i in internal_indices])/1e3) ydata_lin_ext.append(\ sum([all_lin_TC[i] for i in external_indices])/1e3) ydata_sqr.append(\ sum([all_sqr_TC[i] for i in internal_indices])/1e3) ydata_sqr_ext.append(\ sum([all_sqr_TC[i] for i in external_indices])/1e3) ylabel = "Transmission capacity [GW]" ## note the unit, this was ## obtained by /1e3 ################# implement transmission capacity that only include internal european links###### index = np.array([0, 1.5, 2.5, 3.5, 5]) left = index + 0.5*barwidth left2 = index + barwidth ax = plt.subplot(1,1,1) if ydatalabel=='BE': plt.bar(left, ydata_lin, width=barwidth, color=blue) elif ydatalabel=='BC': plt.bar(left, ydata_lin, width=0.5*barwidth,\ color=blue, label = 'Localized flow') plt.bar(left2, ydata_sqr, width=0.5*barwidth,\ color=red, label = 'Synchronized flow') plt.legend() elif ydatalabel=='TC': plt.bar(left, np.array(ydata_lin)+np.array(ydata_lin_ext), \ width=0.5*barwidth, color=darkblue,\ label = 'External capacity: Localized flow') plt.bar(left, np.array(ydata_lin), width=0.5*barwidth,\ color=blue, label = 'Internal capacity: Localized flow') plt.bar(left2, np.array(ydata_sqr)+np.array(ydata_sqr_ext), \ width=0.5*barwidth, color=darkred,\ label = 'External capacity: Synchronized flow') plt.bar(left2, np.array(ydata_sqr), width=0.5*barwidth,\ color=red, label = 'Internal capacity: Synchronized flow') plt.legend(prop={'size':13}, loc=2) plt.xticks(left + 0.5*barwidth, layoutlist) plt.ylabel(ylabel) plt.title('Copper flow') figfilename = ydatalabel + "vslayout.pdf" if not interactive: plt.savefig(savepath + figfilename)