def setUp(self): self.N = europe_plus_Nodes() self.testlapse = range(200) self.h0_sqr = au.get_quant_caps(\ filename='./results/Europe_aHE_copper_sqr_flows.npy') self.h0_lin = au.get_quant_caps(\ filename='./results/Europe_aHE_copper_lin_flows.npy')
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 solve_flow(flow_calc): admat = ''.join(['./settings/', flow_calc.layout, 'admat.txt']) filename = str(flow_calc) copperflow_filename = ''.join(['./results/', flow_calc.layout, '_', flow_calc.alphas, '_copper_', flow_calc.solvermode, '_flows.npy']) if flow_calc.alphas=='aHE': nodes = world_Nodes(admat=admat) elif flow_calc.alphas=='aHO0': nodes = world_Nodes(admat=admat, alphas=np.zeros(8)) elif flow_calc.alphas=='aHO1': nodes = world_Nodes(admat=admat, alphas=np.ones(8)) else: sys.stderr.write('The object has an distribution of mixes that\ is not accounted for.') if flow_calc.solvermode=='lin': mode = 'linear' elif flow_calc.solvermode=='sqr': mode = 'square' else: sys.stderr.write('The solver mode must be "lin" or "sqr"') flowfilename = ''.join(['./results/', str(flow_calc), '_flows.npy']) print flowfilename print flow_calc.capacities if flow_calc.capacities=='copper': solved_nodes, flows = au.solve(nodes, copper=1, mode=mode) print "Checkpt. copper" elif flow_calc.capacities=='q99': h0 = au.get_quant_caps(filename=copperflow_filename) print h0 print h0.mean() solved_nodes, flows = au.solve(nodes, copper=0, h0=h0, mode=mode) print "Checkpt. q99" elif flow_calc.capacities=='hq99': # corresponds to half the capacities # of the 99% quantile layout h0 = 0.5*au.get_quant_caps(filename=copperflow_filename) print h0 print h0.mean() solved_nodes, flows = au.solve(nodes, copper=0, h0=h0, mode=mode) print "Checkpt. hq99" elif flow_calc.capacities.endswith('q99'): scale = float(flow_calc.capacities[0:-3]) h0 = scale*au.get_quant_caps(filename=copperflow_filename) print h0 solved_nodes, flows = au.solve(nodes, copper=0, h0=h0, mode=mode) print "Checkpt. a*q99" else: sys.stderr.write('The capacities must be either "copper", "q99",\ "hq99", or on the form "<number>q99"') solved_nodes.save_nodes(filename) print filename try: flows except NameError: print "Flows not defined." np.save('./results/' + filename + '_flows', flows)
def analyze_incident(hour): mismatch = {} copper_injection = {} constr_injection = {} copper_flow_dict = {} constr_flow_dict = {} for i in xrange(4): mismatch[regions[i]] = Ncopper[i].mismatch[hour] copper_injection[regions[i]] = mismatch[regions[i]]\ - Ncopper[i].curtailment[hour]\ + Ncopper[i].balancing[hour] constr_injection[regions[i]] = mismatch[regions[i]]\ - Nconstrained[i].curtailment[hour]\ + Nconstrained[i].balancing[hour] constr_link_capacities = 0.6*au.get_quant_caps(\ filename="./results/EU_RU_NA_ME_aHE_copper_sqr_flows.npy") constr_link_cap_dict = {} for j in xrange(len(linklist)): copper_flow_dict[linklist[j]] = copper_flows[j][hour] constr_flow_dict[linklist[j]] = constr_flows[j][hour] constr_link_cap_dict[linklist[j]] = [constr_link_capacities[2*j], \ constr_link_capacities[2*j+1]] print(np.sum(copper_injection.values())) print(np.sum(constr_injection.values())) return mismatch, copper_injection, constr_injection, copper_flow_dict,\ constr_flow_dict, constr_link_cap_dict
def get_h0_A2(scalefactor): """ This function works returns a h0 vector, created by downscaling the capacities in the unconstrained flow by scalefactor. """ return scalefactor*au.get_quant_caps(1)
def get_h0_C(quantile): """ This function returns a vector of link capacities to be used with the solver. The capacities are genereated after rule C for interpolation in Rolando et. al. 2013 so f_l = f_l^cQ """ h0 = au.get_quant_caps(quantile) return h0
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 flow_hist(link, title, mean=None, quantiles = False, flow_filename = 'results/copper_flows.npy', number_of_bins = 250, xlim=None, ylim=None, savepath = None): """ Draws a histogram (normalized to mean if provided, takes a link number that can be found by running AtoKh() on the Nodes object that was solve to obtain the Flow vector. """ if not mean: # this is equivalent to if mean == None mean = 1.0 flows = np.load(flow_filename) flow = flows[link]/mean # this is the normalized flow timeseries of the # link in question bins = np.linspace(min(flow), max(flow), number_of_bins) hist_ydata = plt.hist(flow, bins=bins, normed=1, histtype='stepfilled', visible=0)[0] # the first argument returned from # plt.hist is the ydata we need plt.fill_between(bins[1:], 0, hist_ydata) if xlim: plt.xlim(xlim) if ylim: plt.ylim(ylim) plt.title(title) plt.xlabel('Directed power flow [normalized]') plt.ylabel('$P(F_l)$') if quantiles: Q1 = -au.get_quant_caps(0.99)[2*link+1]/mean Q99 = au.get_quant_caps(0.99)[2*link]/mean Q01 = -au.get_quant_caps(0.999)[2*link+1]/mean Q999 = au.get_quant_caps(0.999)[2*link]/mean Q001 = -au.get_quant_caps(0.9999)[2*link+1]/mean Q9999 = au.get_quant_caps(0.9999)[2*link]/mean Qs = [Q1, Q99, Q01, Q999, Q001, Q9999] Qlabels = ['1%', '99%', '0.1%', '99.9%', '0.01%', '99.99%'] for i in range(len(Qs)): plt.vlines(Qs[i],0,1) plt.text(Qs[i], 1.0+0.02*i, Qlabels[i]) fig_filename = ''.join([title.replace(' ','_'), '.pdf']) if not savepath: savepath = '' plt.savefig(savepath + fig_filename) plt.close()
def get_h0_A(scalefactor): """ This function returns a h0 vector of link capacities to be used with the solver. This function generates after the rule for interpolation A in Rolando et. al. 2013, that is f_l = min(a*f_l, f_l99Q), (see pp. 10). """ nodes = EU_Nodes() h99 = au.get_quant_caps(0.99) h_present = au.AtoKh(nodes)[-2]; h0 = scalefactor*h_present for i in xrange(h99.size): if (h99[i] < h0[i]): h0[i] = h99[i] return 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 solve_flow(flow_calc): admat = ''.join(['./settings/', flow_calc.layout, 'admat.txt']) filename = str(flow_calc) copperflow_filename = ''.join(['./results/', flow_calc.layout, '_', flow_calc.alphas, '_copper_', flow_calc.solvermode, '_flows.npy']) if flow_calc.alphas=='aHE': if flow_calc.basisnetwork == 'europeplus': nodes = europe_plus_Nodes(admat=admat) else: sys.stderr.write('The object has a basisnetwork that\ is not accounted for. Use "europeplus".') elif flow_calc.alphas.startswith('aHO'): alpha = float(flow_calc.alphas[3:]) # expects alphas on the form aHO0.4 if flow_calc.basisnetwork == 'europeplus': nodes = europe_plus_Nodes(admat=admat, alphas=alpha) if flow_calc.mismatch_path != None: nodes = set_mismatches(nodes, flow_calc.mismatch_path) else: sys.stderr.write('The object has a basisnetwork that\ is not accounted for. Use "europeplus".') else: sys.stderr.write('The object has an distribution of mixes that\ is not accounted for.') mode_str_list = [] if 'lin' in flow_calc.solvermode: mode_str_list.append('linear ') elif 'sqr' in flow_calc.solvermode: mode_str_list.append('square ') else: sys.stderr.write('The solver mode must be "lin", "sqr"') if 'imp' in flow_calc.solvermode: mode_str_list.append('impedance ') mode = ''.join(mode_str_list) flowfilename = ''.join(['./results/', filename, '_flows.npy']) if 'DC' in flow_calc.solvermode: solver = DCs.DC_solve else: solver = au.solve if flow_calc.capacities=='copper': solved_nodes, flows = solver(nodes, mode=''.join([mode, ' copper']),\ msg=str(flow_calc)) elif flow_calc.capacities=='q99': h0 = au.get_quant_caps(filename=copperflow_filename) solved_nodes, flows = solver(nodes, h0=h0, mode=mode, \ msg=str(flow_calc)) elif flow_calc.capacities=='hq99': # corresponds to half the capacities # of the 99% quantile layout h0 = 0.5*au.get_quant_caps(filename=copperflow_filename) solved_nodes, flows = solver(nodes, h0=h0, mode=mode, \ msg=str(flow_calc)) elif flow_calc.capacities.endswith('q99'): scale = float(flow_calc.capacities[0:-3]) h0 = au.get_quant_caps(filename=copperflow_filename) solved_nodes, flows = solver(nodes, h0=h0, b=scale, mode=mode,\ msg=str(flow_calc)) else: sys.stderr.write('The capacities must be either "copper", "q99",\ "hq99", or on the form "<number>q99"') if flow_calc.savemode == 'full': solved_nodes.save_nodes(filename) try: flows np.save('./results/' + filename + '_flows', flows) except NameError: print "Flows not defined." elif 'FCResult' in flow_calc.savemode: result = FCResult(filename+'.pkl') result.add_instance(solved_nodes, flows, flow_calc) result.save_results(filename+'.pkl') if 'flows' in flow_calc.savemode: try: flows np.save('./results/' + filename + '_flows', flows) except NameError: print "Flows not defined." else: print "Results not saved, invalid savemode provided"
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 get_h0_B(scalefactor): return scalefactor*au.get_quant_caps(0.99)
""" if 'init' in task: print('Mode selected: Calculate link capacities from unconstrained flows') Nodes = europe_plus_Nodes() N, F = au.solve(Nodes, mode='linear copper verbose') N.save_nodes(filename='Europe_aHE_copper_lin', path=dataPath) np.save(dataPath + 'Europe_aHE_copper_lin_flows', F) """ Solve power flows for various constraints """ if 'solve' in task: print('Mode selected: Solve power flows') # Calculate link capacities from 99% quantile of unconstrained flow h0 = au.get_quant_caps(filename=dataPath + 'Europe_aHE_copper_lin_flows.npy') p = Pool(8) p.map(solver, b) """ Calculate powermixes and nodes' usages of links and save results to file. """ if 'trace' in task: print('Mode selected: flow tracing') p = Pool(8) p.map(caller, b) """ Calculate nodes' contributions and save results to file.