-
Notifications
You must be signed in to change notification settings - Fork 0
/
figutils.py
1193 lines (971 loc) · 42.7 KB
/
figutils.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
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import interp1d
import aurespf.solvers as au
from nhgrid import nh_Nodes
from europe_plusgrid import europe_plus_Nodes
from FCResult import FCResult
from FCResult import myhist
from FlowCalculation import FlowCalculation
import costtools as ct
#### Colorschemes ################################
dth = (3.425)
dcolwidth = (2*3.425+0.236)
blue = '#134b7c'
yellow = '#f8ca00'
orange = '#e97f02'
brown = '#876310'
green = '#4a8e05'
red = '#ae1215'
purple = '#4f0a3d'
darkred= '#4f1215'
pink = '#bd157d'
lightpink = '#d89bc2'
aqua = '#37a688'
darkblue = '#09233b'
lightblue = '#8dc1e0'
grayblue = '#4a7fa2'
blue_cycle = [darkblue, blue, grayblue, lightblue]
color_cycle = [blue, red, orange, purple, green, \
pink, lightblue, darkred, yellow,
darkblue, grayblue, brown]
def make_layout_flow_hists(flowcalc, interactive=False):
""" Takes a FlowCalculation object,
assuming there is a corresponding saved nodes
file (.npz) and flows (.npy).
"""
filename = str(flowcalc) + '.npz'
N = nh_Nodes(load_filename=filename)
EU_meanload = N[0].mean
print(EU_meanload)
print(N.pathadmat)
flowfilename = str(flowcalc) + '_flows.npy'
flows = np.load('./results/'+flowfilename)
listflows = [au.AtoKh(N)[-1][i][0] for i in range(len(au.AtoKh(N)[-1]))]
print([min(flows[i]) for i in range(len(flows))])
print([max(flows[i]) for i in range(len(flows))])
print(listflows)
xmin = -1.9e5/EU_meanload
xmax = 1.5e5/EU_meanload
bins = np.linspace(xmin, xmax, 200)
if interactive:
plt.ion()
for f in listflows:
index = listflows.index(f)
plt.figure()
plt.hist(flows[index]/EU_meanload, bins=bins, normed=True)
plt.title(f + ': ' + flowcalc.capacities)
plt.xlabel("Directed power flow [normalized to EU mean load]")
plt.ylabel("$P(F_l)$")
figfilename = f.replace(' ','_') + '_' + flowcalc.capacities + '_flowhist.pdf'
savepath = './results/figures/CheckStrangeBE/'
if not interactive:
plt.savefig(savepath + figfilename)
plt.close()
def make_layout_mismatchhists(flowcalc, interactive=False):
""" Takes a FlowCalculation object,
assuming there is a corresponding saved nodes
file (.npz).
"""
filename = str(flowcalc) + '.npz'
N = nh_Nodes(load_filename=filename)
xmin = -1
xmax = 2
bins = np.linspace(xmin,xmax,200)
if interactive:
plt.ion()
for n in N:
mismatch = n.curtailment - n.balancing
nonzero_mismatch = []
for w in mismatch:
if w>=1 or w<-1:
nonzero_mismatch.append(float(w)/n.mean)
plt.figure()
plt.hist(nonzero_mismatch, bins=bins, normed=True)
plt.title(''.join([str(n.label), '(', flowcalc.capacities, ')']))
plt.xlabel('Mismatch [normalized]')
plt.ylabel('Mismatch distribution')
figfilename = ''.join([str(n.label), '_mismatchhist_',\
flowcalc.capacities, '.pdf'])
savepath = "./results/figures/CheckStrangeBE/"
if not interactive:
plt.savefig(savepath+figfilename)
plt.close()
def ion_BE_vs_TC_testplot(layout):
""" This function plots the normalized balancing energy
as a function of transmission capacity for all the
regions in a provided layout. (sqr flow)
"""
plt.ion()
plt.figure()
plt.rcParams['axes.color_cycle'] = color_cycle
filenames = []
for a in np.linspace(0,1.5,31):
capacity = ''.join([str(a), 'q99'])
filenames.append(''.join([layout,'_aHE_',capacity,'_sqr.pkl']))
admat = "./settings/" + layout + "admat.txt"
N = nh_Nodes(admat=admat)
for n in N:
TC = []
BE = []
for filename in filenames:
TC.append(get_data(filename, 'Total_TC',\
'./results/BalvsTrans/')/1e6) # now in TW
BE.append(get_data(filename, 'BE',\
'./results/BalvsTrans/')[n.id])
plt.plot(TC, BE, '-o', label=str(n.label) )
plt.xlabel('Total transmission capacity [TW]')
plt.ylabel('Balancing energy [normalized]')
plt.legend()
plt.title(layout)
def get_data(filename, field, path='./results/'):
""" Returns the data in a certain field,
from an FCResult file.
See the FCResult class for available fields.
Example
-------
>>> get_data("eurasia_aHE_0.95q99_lin.pkl", 'Total_TC')
"""
result = FCResult(filename, path=path)
returnvalue = result.cache[0][field]
return returnvalue
def make_all_BEBC_TC():
""" Running this generates the graphs
for BE vs TC and BC vs TC for all
layouts both for EU and for the whole layout together.
"""
modes = ['lin', 'lin_imp', 'sqr', 'sqr_imp']
layouts = ['EU_RU_NA_ME', 'eurasia', 'US_eurasia_open', \
'US_eurasia_closed', 'US_EU_RU_NA_ME']
ydatalabels = ['BE', 'BC']
regions = ['EU', 'all']
for r in regions:
for l in layouts:
fc_list = []
for m in modes:
fc = FlowCalculation(l, 'aHE', 'copper', m)
fc_list.append(fc)
for ydatalabel in ydatalabels:
savepath = ''.join(['./results/figures/', ydatalabel, 'vsTC/'])
make_bal_vs_trans_graph(fc_list, ydatalabel, region=r,\
savepath=savepath)
def make_bal_vs_trans_graph(flowcalcs, ydatalabel, region='EU', \
trans_scalerange=np.linspace(0,1.5, 31),\
figfilename=None, savepath = './results/figures/', \
datapath='./results/BalvsTrans/', interactive=False,
title=True, legend=True, ylim=None):
""" Example
-------
make_bal_vs_trans_graph([flowcalclin, flowcalcsqr], 'BE')
"""
plt.close()
plt.rc('lines', lw=2)
plt.rcParams['axes.color_cycle'] = color_cycle
if type(flowcalcs)!=list:
flowcalcs = [flowcalcs]
xmaxlist = []
if interactive:
plt.ion()
for fc in flowcalcs:
admat = "./settings/" + fc.layout + "admat.txt"
N = nh_Nodes(admat=admat)
filenames = []
for a in trans_scalerange:
capacity = ''.join([str(a), 'q99'])
filenames.append(''.join([str(FlowCalculation(fc.layout, fc.alphas,\
capacity, fc.solvermode)), '.pkl']))
ydata = []
TC = []
if region=='EU':
for filename in filenames:
ydata.append(get_data(filename, ydatalabel,\
path=datapath)[0]/N[0].mean)
TC.append(get_data(filename, 'Total_TC', path=datapath)\
/1e6) # now in TW
if region=='all':
### the total mean load for the regions in the current layout
total_mean_load = np.sum([n.mean for n in N])
for filename in filenames:
unnormalized_data = [get_data(filename, ydatalabel, \
path=datapath)[n.id] for n in N]
ydata.append(np.sum(unnormalized_data)/total_mean_load)
TC.append(get_data(filename, 'Total_TC', path=datapath)\
/1e6) # now in TW
xmaxlist.append(np.max(TC))
plt.plot(TC,ydata, label=fc.pretty_solvermode())
plt.xlabel('Total transmission capacity [TW]')
if ydatalabel=='BE':
plt.ylabel('Backup energy [normalized]')
if ydatalabel=='BC':
plt.ylabel('Backup capacity [normalized]')
if legend:
plt.legend()
if title:
plt.title(region + ': ' + flowcalcs[0].layout)
plt.xlim((0,np.min(xmaxlist)))
if ylim:
plt.ylim(ylim)
if not figfilename:
figfilename = ''.join([flowcalcs[0].layout, '_', ydatalabel, '_', \
region, '.pdf'])
if not interactive:
plt.savefig(savepath+figfilename)
plt.close()
def get_total_linkflows(filename, path='./results/BalvsTrans/'):
""" Function that return a list of the total flows over the
full time series (not directed) for each link in a given
layout. Takes a filename and path to an FCResults file.
The total flows are (well) estimated based on the flow
histogramst that are save in the FCResults class.
"""
flow_hists = get_data(filename, 'flowhists', path)
total_flows = []
for i in range(len(flow_hists)):
total_flows.append(np.sum(\
[np.abs(flow_hists[i][0][j])*flow_hists[i][1][j] for j in \
range(len(flow_hists[i][0]))]))
return total_flows
def normalize_list(mylist):
total = np.sum(mylist)
normalized_list = [float(x)/total for x in mylist]
return normalized_list
def make_relflow_vs_TC_graph(flow_calcs, \
trans_scalerange=np.linspace(0.05,1.5, 30),\
figfilename=None, savepath = './results/figures/', \
datapath='./results/BalvsTrans/', interactive=False):
plt.close()
plt.rc('lines', lw=2)
plt.rcParams['axes.color_cycle'] = color_cycle
if interactive:
plt.ion()
for fc in flow_calcs:
plt.subplot(1,len(flow_calcs), flow_calcs.index(fc))
admat = './settings/' + fc.layout + 'admat.txt'
N= nh_Nodes(admat=admat)
filenames = []
for a in trans_scalerange:
capacity = ''.join([str(a), 'q99'])
filenames.append(''.join([str(FlowCalculation(fc.layout, fc.alphas,\
capacity, fc.solvermode)), '.pkl']))
rel_flows = []
TC = []
for filename in filenames:
rel_flows.append(normalize_list(get_total_linkflows(filename,\
path=datapath)))
TC.append(get_data(filename, 'Total_TC', path=datapath)\
/1e6) # Now in TW
linklist = au.AtoKh(N)[-1]
for i in range(len(linklist)):
linkflow = [rel_flows[j][i] for j in range(len(rel_flows))]
plt.plot(TC, linkflow, label=''.join([linklist[i][0], " ", \
fc.solvermode]))
plt.legend(prop={'size':8})
plt.xlabel('Total transmission capacity [TW]')
plt.ylabel('Relative link usage')
plt.ylim(0, 0.4)
plt.xlim(min(TC), max(TC))
plt.title(fc.layout)
if not figfilename:
figfilename = ''.join([flow_calcs[0].layout, '_relflows', '.pdf'])
if not interactive:
plt.savefig(savepath+figfilename)
plt.close()
def make_all_relflowgraphs():
modes = [['lin_imp', 'lin'], ['sqr_imp', 'sqr']]
layouts = ['EU_RU_NA_ME', 'eurasia', 'US_eurasia_open', \
'US_eurasia_closed', 'US_EU_RU_NA_ME']
savepath = './results/figures/RelflowsImpNoImp/'
for l in layouts:
for modesublist in modes:
fclist = [FlowCalculation(l, 'aHE', 'copper', modesublist[0]),
FlowCalculation(l, 'aHE', 'copper', modesublist[1])]
figfilename = l + '_' + modesublist[0] + '.pdf'
make_relflow_vs_TC_graph(fclist, figfilename=figfilename,\
savepath=savepath)
def make_all_y_vs_alpha_graph():
modes = ['lin', 'lin_imp', 'sqr', 'sqr_imp']
layouts = ['EU_RU_NA_ME', 'eurasia', 'US_eurasia_open', \
'US_eurasia_closed', 'US_EU_RU_NA_ME']
ydatalabels = ['BE', 'BC', 'Total_TC']
for l in layouts:
fc_list = []
for m in modes:
fc = FlowCalculation(l, 'aHO1', 'copper', m)
fc_list.append(fc)
for ydatalabel in ydatalabels:
savepath = ''.join(['./results/figures/', ydatalabel, 'vsAlpha/'])
make_y_vs_alpha_graph(fc_list, ydatalabel, savepath=savepath, \
zerotrans=True, showminima=True)
def make_y_vs_alpha_graph(flowcalcs, ydatalabel, alphas=np.linspace(0,1,21), \
figfilename=None, savepath='./results/figures/', \
datapath='./results/AlphaSweepsCopper/', \
interactive=False, zerotrans=False,\
showminima=False, hetpoints=True, labels=None,\
title=True, small_legend=True):
""" ydatalabel should be 'BE', 'BC' or 'Total_TC'
The alphas's field of the FlowCalculation objects are disregared,
an a graph is generated based on the other fields in the object,
in a range of alphas.
Example
-------
make_y_vs_alpha_graph([flowcalclin, flowcalcsqr], 'BE')
"""
plt.close()
plt.rc('lines', lw=2)
local_color_cycle = [red, purple, green, \
lightblue, pink, darkred, yellow,
darkblue, grayblue, brown, blue, orange]
plt.rcParams['axes.color_cycle'] = local_color_cycle
if type(flowcalcs)!=list:
flowcalcs = [flowcalcs]
if interactive:
plt.ion()
layoutlist = [fc.layout for fc in flowcalcs]
samelayout = (layoutlist[1:]==layoutlist[:-1]) ## True if all the layouts
# are the same, False
# otherwise
minimum_text = 'Minima:\n'
label_counter = 0
for fc in flowcalcs:
admat = "./settings/" + fc.layout + "admat.txt"
N = nh_Nodes(admat=admat)
total_mean_load = np.sum([n.mean for n in N])
####### Plot the zero transmission case ######################################
current_fc_is_zerotrans=False
if zerotrans and ydatalabel!='Total_TC':
current_fc_is_zerotrans=True
if (not samelayout) or flowcalcs.index(fc)==0:
zerotrans_ydata = []
for a in alphas:
zerotrans_ydata.append(get_zerotrans_data(alpha=a, \
ydatalabel=ydatalabel, fc=fc))
if not labels:
zerotrans_label = ''.join([fc.layout, ': zerotrans'])
else:
zerotrans_label = labels[label_counter]
label_counter += 1
plt.plot(alphas, zerotrans_ydata, \
label=zerotrans_label)
if hetpoints:
het_zerotrans_filename = \
''.join([str(FlowCalculation(fc.layout, 'aHE',\
'zerotrans', 'raw')), '.pkl'])
avg_het_alpha = np.sum([n.mean*n.alpha for n in N])\
/total_mean_load
het_zerotrans_y_value = np.sum(\
get_data(het_zerotrans_filename, ydatalabel, \
path=datapath))/total_mean_load
if not labels:
zerotrans_het_label = ''.join(\
[fc.layout, ': No transmission: ', \
r'$\alpha_{\mathrm{opt}}$'])
else:
zerotrans_het_label = labels[label_counter]
label_counter += 1
plt.plot(avg_het_alpha, het_zerotrans_y_value, 'x',
markersize=8, label=zerotrans_het_label)
####### extract an plot data from homogenous alpha layout ####################
filenames = []
for a in alphas:
alphacode = ''.join(['aHO', str(a)])
filenames.append(''.join([str(FlowCalculation(fc.layout,\
alphacode, fc.capacities, fc.solvermode)), '.pkl']))
ydata = []
for filename in filenames:
if ydatalabel in ['BE', 'BC']:
unnormalized_data = get_data(filename, ydatalabel, \
path=datapath)
assert(len(unnormalized_data)==len(N))
ydata.append(np.sum(unnormalized_data)/total_mean_load)
elif ydatalabel == 'Total_TC':
ydata.append(get_data(filename, ydatalabel, path=datapath)\
/1e6) # now in TW
if not labels:
label = ''.join([fc.layout, ': ', fc.solvermode])
else:
label = labels[label_counter]
label_counter += 1
plt.plot(alphas, ydata, \
label=label)
####### find and show minima if this option is set True ######################
if showminima:
if zerotrans and ydatalabel!='Total_TC':
if (not samelayout) or flowcalcs.index(fc)==0:
alphamin, ymin = find_interp_minimum(alphas,\
zerotrans_ydata)
plt.plot(alphamin, ymin, 'ok')
minimum_text = minimum_text + '%s %s: (%f, %f)\n' \
%(fc.layout, 'zerotrans', alphamin, ymin)
alphamin, ymin = find_interp_minimum(alphas, ydata)
plt.plot(alphamin, ymin, 'ok')
minimum_text = minimum_text + '%s %s: (%f, %f)\n' \
%(fc.layout, fc.solvermode, alphamin, ymin)
####### generate point from heterogeneous alpha layout (optimal mixes) #####
####### if the hetpoints option is set as true #############################
if hetpoints:
het_filename = ''.join([str(FlowCalculation(fc.layout, 'aHE',\
'copper', fc.solvermode)), '.pkl'])
# the folowing i an average of the mixes in the heterogeneous
# (optimal wrt. balancing energy) layout of alphas. This works
# because N is loaded with these mixes as default
avg_het_alpha = np.sum([n.mean*n.alpha for n in N])\
/total_mean_load
if ydatalabel in ['BE', 'BC']:
het_y_value = np.sum(get_data(het_filename, ydatalabel, \
path=datapath))/total_mean_load
elif ydatalabel == 'Total_TC':
het_y_value = get_data(het_filename, ydatalabel,\
path=datapath)/1e6 # now in TW
if not labels:
het_label = fc.layout + ' ' + r'$\alpha_{\mathrm{opt}}$' + ': ' \
+ fc.solvermode
else:
het_label = labels[label_counter]
label_counter += 1
plt.plot(avg_het_alpha, het_y_value, 'x', markersize=8, \
label=het_label)
#### finish up the plot ####################################################
if showminima:
plt.text(0.1, 0, minimum_text)
plt.xlabel(r'$\alpha$')
if ydatalabel=='BE':
plt.ylabel('Backup energy [normalized]')
elif ydatalabel=='BC':
plt.ylabel('Backup capacity [normalized]')
elif ydatalabel=='Total_TC':
plt.ylabel('Total transmission capacity [TW]')
if small_legend:
plt.legend(prop={'size':7})
else:
plt.legend()
if samelayout:
if title:
plt.title(flowcalcs[0].layout)
plt.xlim((0, 1))
plt.ylim(ymin=0)
if not figfilename:
figfilename = ''.join([flowcalcs[0].layout, '_', ydatalabel, \
'_vs_alpha.pdf'])
if not interactive:
plt.savefig(savepath+figfilename)
plt.close()
def get_zerotrans_data(alpha, ydatalabel, fc):
""" If ydatalabel is 'BE' it returns the normalized
total backup energy in the layout specified in the
Flowcalculation object fc, in a homogenous mixing
layout with mixing alpha. If 'BC' the backup capacity
is retured.
"""
admat = "./settings/" + fc.layout + "admat.txt"
N = nh_Nodes(admat=admat, alphas=alpha)
total_mean_load = np.sum([n.mean for n in N])
length_of_timeseries = len(N[0].mismatch)
for n in N:
n.balancing = -au.get_negative(n.mismatch)
if ydatalabel=='BE':
result = np.sum([np.sum(n.balancing) for n in N])\
/(length_of_timeseries*total_mean_load)
elif ydatalabel=='BC':
result = np.sum([au.get_q(n.balancing, 0.99) for n in N])\
/total_mean_load
else:
print "ydatalabel must be 'BE' or 'BC'"
return
return result
def find_interp_minimum(x, y, rel_tol = 1e-3):
""" Given an independent variable x and a variable
dependent on x, y, this function creates a cubic
interpolation, to estimate the minimum value of
y as a function of x, with the relative precicion
rel_tol.
"""
N = int(1.0/rel_tol)
xfine = np.linspace(np.min(x), np.max(x), N)
f = interp1d(x, y, kind='cubic')
ymin = f(xfine).min()
xmin = xfine[f(xfine).argmin()]
return xmin, ymin
def make_all_hourly_flowhists():
modes = ['lin', 'lin_imp', 'sqr', 'sqr_imp']
layouts = ['EU_RU_NA_ME', 'eurasia', 'US_eurasia_open', \
'US_eurasia_closed', 'US_EU_RU_NA_ME']
# make plots with one mode in each plot,
# at 4 different times a day
for layout in layouts:
for m in modes:
fc = FlowCalculation(layout, 'aHO1', 'copper', m)
savepath = './results/figures/HourlyFlowhists/Every6hours/' \
+ layout + '/'
make_hourly_flowhists(fc, hours = [0, 6, 12, 18],
savepath = savepath)
modes = [['lin', 'lin_imp'], ['sqr', 'sqr_imp']]
for layout in layouts:
for m in modes:
flowcalcs = [FlowCalculation(layout, 'aHO1', 'copper', m[0]),\
FlowCalculation(layout, 'aHO1', 'copper', m[1])]
savepath = './results/figures/HourlyFlowhists/ImpVsNoImp/'\
+ layout + '/'
make_hourly_flowhists(flowcalcs, hours = [0, 12],
figfileending=m[0] + '_hflowhist',
savepath = savepath)
return
def make_hourly_flowhists(flowcalcs, links='all', alphas=[0.0, 0.5, 0.9, 1.0],\
hours = [0, 12], figfileending=None, \
savepath='./results/figures/', \
datapath='./results/AlphaSweepsCopper/', \
interactive=False, subplotshape=(2,2)):
""" The produced plots will be named <link>_<figfileending>.pdf
The arguments links must be either 'all' or on the form:
['EU to RU', ...] (a list!). Make sure that the alphas match
existing .pkl-files, from the FCResults-class, number of decimals
matter.
"""
plt.close()
plt.rc('lines', lw=2)
plt.rcParams['axes.color_cycle'] = color_cycle
if type(flowcalcs)!=list:
flowcalcs = [flowcalcs]
if interactive:
plt.ion()
layoutlist = [fc.layout for fc in flowcalcs]
samelayout = (layoutlist[1:]==layoutlist[:-1]) ## True if all the layouts
# are the same, False
# otherwise
only_openclosed = all([('US_eurasia' in l) for l in layoutlist])
if links=='all' and (samelayout or only_openclosed) :
admat = "./settings/" + layoutlist[0] + "admat.txt"
N = nh_Nodes(admat=admat)
linklist = [link[0] for link in au.AtoKh(N)[-1]]
elif links=='all':
print "'all' option for links, is only possible when all flowcalcs\
have the same layout, or all the layouts are either\
US_eurasia_open or -closed. No figure produced. "
return
else:
linklist = links
print linklist
for link in linklist:
fig = plt.figure(figsize=(12,12))
minflow = 0
maxflow = 0
if samelayout:
linkindex = get_link_number_in_layout(link, flowcalcs[0].layout)
lines = []
labels = []
for fc in flowcalcs:
if not samelayout:
linkindex = get_link_number_in_layout(link, fc.layout)
for a in alphas:
filename = ''.join([fc.layout, '_aHO', str(a),\
'_copper_', fc.solvermode, '.pkl'])
flowhistdata = get_data(filename, 'hourly_flowhists', \
path=datapath)
plt.subplot(subplotshape[0], subplotshape[1], alphas.index(a)+1)
for h in hours:
flow = flowhistdata[linkindex][h][0]
count = flowhistdata[linkindex][h][1]
totalcount = np.sum(count)
Pflow = [float(c)/totalcount for c in count]
# plotting flow in GW
if samelayout:
label = ''.join([fc.pretty_solvermode(),': ', r'$h=$', str(h)])
else:
label = ''.join([fc.pretty_layout(), ' ', fc.pretty_solvermode(),': ',\
r'$h=$', str(h)])
line = plt.plot(flow/1e3, Pflow, label=label)
if alphas.index(a)==0:
lines.append(line)
labels.append(label)
if maxflow < np.max(flow)/1e3:
maxflow = np.max(flow)/1e3
if minflow > np.min(flow)/1e3:
minflow = np.min(flow)/1e3
plt.legend(prop={'size':10})
plt.title(r'$\alpha$ = ' + str(a))
plt.xlabel(r'$F_l$' + ' [GW]')
plt.ylabel(r'$p_l(F_l | h)$')
# loop for adjusting all the axis, to be equal
for a in alphas:
plt.subplot(subplotshape[0], subplotshape[1], alphas.index(a)+1)
plt.xlim(minflow, maxflow)
if 'lin' in [fc.solvermode for fc in flowcalcs]:
plt.ylim(0, 0.04)
else:
plt.ylim(0, 0.024)
lines = [l[0] for l in lines]
#fig.legend(lines, labels, 'best')
if samelayout:
fig.suptitle(''.join([link, ' (in ', fc.pretty_layout(), ' layout)']),\
fontsize='20')
else:
fig.suptitle(link, fontsize='20')
if not figfileending:
figfilename = ''.join([link.replace(' ', '_'), '_', \
flowcalcs[0].solvermode, '_',\
'hflowhist.pdf'])
else:
figfilename = ''.join([link.replace(' ', '_'), '_', \
figfileending, '.pdf'])
if not interactive:
plt.savefig(savepath+figfilename, bbox_inches='tight')
plt.close()
return
def get_link_number_in_layout(link, layout):
""" Takes a link and a layout and returns the index if the
link in the layout.
Example:
-------
get_link_number_in_layout('IN to SE', 'eurasia')
"""
admat = './settings/' + layout + 'admat.txt'
N = nh_Nodes(admat=admat)
linkinfo = au.AtoKh(N)[-1]
reversedlink = ''.join([link[-2:], link[2:-2], link[0:2]])
for i in xrange(len(linkinfo)):
if linkinfo[i][0] in [link, reversedlink]:
return linkinfo[i][1]
print "Link not found"
return
def make_bal_vs_layout_barplot(flowcalcs, ydatalabel,\
figfilename=None, savepath = './results/figures/', \
datapath='./results/BalvsTrans/', interactive=False,
barwidth=0.5, title=None):
plt.close()
plt.rcParams['axes.color_cycle'] = color_cycle
if type(flowcalcs)!=list:
flowcalcs = [flowcalcs]
if interactive:
plt.ion()
ydata = []
layoutlist = []
for fc in flowcalcs:
admat = "./settings/" + fc.layout + "admat.txt"
N = nh_Nodes(admat=admat)
total_mean_load = np.sum([n.mean for n in N])
filename = str(fc) + '.pkl'
unnormalized_data = [get_data(filename, ydatalabel, \
path=datapath)[n.id] for n in N]
ydata.append(np.sum(unnormalized_data)/total_mean_load)
layoutlist.append(fc.pretty_layout())
index = np.arange(len(ydata))
left = index+0.5*barwidth
plt.ion()
ax = plt.subplot(1,1,1)
plt.bar(left, ydata, width=barwidth, color=blue)
plt.xticks(left + 0.5*barwidth, layoutlist)
if ydatalabel=='BE':
plt.ylabel('Backup energy [normalized]')
elif ydatalabel=='BC':
plt.ylabel('Backup capacity [normalized]')
if title:
plt.title(title)
if not figfilename:
figfilename = ydatalabel + '_vs_layout.pdf'
if not interactive:
plt.savefig(savepath+figfilename)
return
def make_all_LCOE_vs_alpha_graphs():
modes = ['lin', 'lin_imp', 'sqr', 'sqr_imp']
layouts = ['EU_RU_NA_ME', 'eurasia', 'US_eurasia_open', \
'US_eurasia_closed', 'US_EU_RU_NA_ME']
for m in modes:
for l in layouts:
fc = FlowCalculation(l, 'aHO1.0', 'copper', m)
make_LCOE_vs_alpha_graph(fc, savepath='./results/figures/LCOEvsAlpha/')
return
def make_additional_LCOE_vs_alpha_graphs():
modes = ['lin', 'sqr']
layouts = ['EU_RU', 'EU_NA', 'EU_ME', \
'EU_RU_NA_MEstar']
for m in modes:
for l in layouts:
fc = FlowCalculation(l, 'aHO1.0', 'copper', m)
make_LCOE_vs_alpha_graph(fc, savepath='./results/figures/LCOEvsAlpha/')
return
def make_LCOE_vs_alpha_graph(masterflowcalc, alphas=np.linspace(0,1,21), \
figfilename=None, savepath='./results/figures/', \
datapath='./results/AlphaSweepsCopper/', \
interactive=False, CFw=0.35, CFs=0.15, title=True):
plt.close()
if interactive:
plt.ion()
total_energy = ct.total_annual_energy_consumption(masterflowcalc)
admat = './settings/' + masterflowcalc.layout + 'admat.txt'
BE_LCOE = []
BC_LCOE = []
wind_LCOE = []
solar_LCOE = []
TC_LCOE = []
zerotrans_total_LCOE = []
zerotrans_datapath = './results/AlphaSweepsZerotrans/'
for a in alphas:
alphacode = ''.join(['aHO', str(a)])
fc = FlowCalculation(masterflowcalc.layout, alphacode, \
masterflowcalc.capacities, masterflowcalc.solvermode)
BE_LCOE.append(au.cbe(ct.total_annual_BE(fc, datapath))/total_energy)
BC_LCOE.append(au.cbc(ct.get_total_BC(fc, datapath))/total_energy)
wind_LCOE.append(au.cwc(ct.get_total_wind_capacity(fc, CFw, datapath))\
/total_energy)
solar_LCOE.append(au.csc(\
ct.get_total_solar_capacity(fc, CFs, datapath))\
/total_energy)
TC_LCOE.append(au.ctc(ct.get_TCs(fc, datapath), pathadmat=admat)\
/total_energy)
zerotrans_fc = FlowCalculation(masterflowcalc.layout, alphacode,\
'zerotrans', 'raw')
zerotrans_total_LCOE.append(
(au.cbe(ct.total_annual_BE(zerotrans_fc, zerotrans_datapath))
+ au.cbc(ct.get_total_BC(zerotrans_fc, zerotrans_datapath))
+ au.cwc(\
ct.get_total_wind_capacity(zerotrans_fc, CFw, zerotrans_datapath))
+ au.csc(\
ct.get_total_solar_capacity(zerotrans_fc, CFs, zerotrans_datapath)))\
/total_energy)
print TC_LCOE
plt.ion()
plt.fill_between(alphas,
np.array(BE_LCOE) + np.array(BC_LCOE) + \
np.array(solar_LCOE) + np.array(wind_LCOE) +\
np.array(TC_LCOE), label='Backup energy', color=orange,
edgecolor='k')
plt.fill_between(alphas,
np.array(BC_LCOE) +
np.array(solar_LCOE) + np.array(wind_LCOE) +
np.array(TC_LCOE), label='Backup capacity', color=red,
edgecolor='k')
plt.fill_between(alphas,
np.array(solar_LCOE) + np.array(wind_LCOE) +
np.array(TC_LCOE), label='Solar capacity', color=yellow,
edgecolor='k')
plt.fill_between(alphas,
np.array(wind_LCOE) +
np.array(TC_LCOE), label='Wind capacity', color=blue,
edgecolor='k')
plt.fill_between(alphas, np.array(TC_LCOE), label='Transmission capacity',\
color=green, edgecolor='k')
plt.plot(alphas, zerotrans_total_LCOE, color=lightblue, lw=2, ls='--',\
label="Total LCOE, zero transmission")
colors = [orange, red, yellow, blue, green]
rectangles = [plt.Rectangle((0,0), 1, 1, fc=c) for c in colors]
plt.legend(rectangles, ['Backup energy', 'Backup capacity', \
'Solar capacity', 'Wind capacity',\
'Transmission capacity'])
plt.ylim(0,250)
plt.xlabel(r'$\alpha$')
plt.ylabel('LCOE [' + u'\u20AC' + '/MWh]')
if title:
plt.title(masterflowcalc.pretty_layout() + ': ' + masterflowcalc.pretty_solvermode())
if not figfilename:
figfilename = ''.join([masterflowcalc.layout, '_', \
masterflowcalc.solvermode, '_LCOEvsalpha', '.pdf'])
if not interactive:
plt.savefig(savepath+figfilename)
plt.close()
def make_LCOE_vs_alpha_graph_TContop(masterflowcalc, alphas=np.linspace(0,1,21), \
figfilename=None, savepath='./results/figures/', \
datapath='./results/AlphaSweepsCopper/', \
interactive=False, CFw=0.35, CFs=0.15, title=True):
plt.close()
if interactive:
plt.ion()
total_energy = ct.total_annual_energy_consumption(masterflowcalc)
admat = './settings/' + masterflowcalc.layout + 'admat.txt'
BE_LCOE = []
BC_LCOE = []
wind_LCOE = []
solar_LCOE = []
TC_LCOE = []
zerotrans_total_LCOE = []
zerotrans_datapath = './results/AlphaSweepsZerotrans/'
for a in alphas:
alphacode = ''.join(['aHO', str(a)])
fc = FlowCalculation(masterflowcalc.layout, alphacode, \
masterflowcalc.capacities, masterflowcalc.solvermode)
BE_LCOE.append(au.cbe(ct.total_annual_BE(fc, datapath))/total_energy)
BC_LCOE.append(au.cbc(ct.get_total_BC(fc, datapath))/total_energy)
wind_LCOE.append(au.cwc(ct.get_total_wind_capacity(fc, CFw, datapath))\
/total_energy)
solar_LCOE.append(au.csc(\
ct.get_total_solar_capacity(fc, CFs, datapath))\
/total_energy)
TC_LCOE.append(au.ctc(ct.get_TCs(fc, datapath), pathadmat=admat)\
/total_energy)
zerotrans_fc = FlowCalculation(masterflowcalc.layout, alphacode,\
'zerotrans', 'raw')
zerotrans_total_LCOE.append(
(au.cbe(ct.total_annual_BE(zerotrans_fc, zerotrans_datapath))
+ au.cbc(ct.get_total_BC(zerotrans_fc, zerotrans_datapath))
+ au.cwc(\
ct.get_total_wind_capacity(zerotrans_fc, CFw, zerotrans_datapath))
+ au.csc(\
ct.get_total_solar_capacity(zerotrans_fc, CFs, zerotrans_datapath)))\
/total_energy)
print TC_LCOE
plt.ion()
plt.fill_between(alphas,