-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcase_creation_functions.py
More file actions
1446 lines (1355 loc) · 54.8 KB
/
case_creation_functions.py
File metadata and controls
1446 lines (1355 loc) · 54.8 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
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
# imports
import os
import pandas as pd
import numpy as np
import math
import datetime
import warnings
import re
# directory structure for inputs/outputs
class DirStructure(object):
"""Creates directory structure for inputs (from NREL-RTS) and
outputs (which will be inputs for Pyomo model)"""
def __init__(
self,
code_directory,
RTS_folder="RTS-GMLC-master",
MPEC_folder="competitiveMPEC",
results_folder="",
results_case="",
):
"""initializes directory structure
Arguments:
code_directory {str} -- name of directory where NREL-RTS and your desired output folder are
Keyword Arguments:
RTS_folder {str} -- [name of NREL RTS folder you forked from their repo] (default: {'RTS-GMLC-master'})
MPEC_folder {str} -- [name of folder with MPEC code] (default: {'competitiveMPEC-master'})
results_folder {str} -- [folder to write cases to] (default: {''})
results_case {str} -- [name of individual case] (default: {''})"""
self.BASE_DIRECTORY = code_directory
self.RTS_GMLC_DIRECTORY = os.path.join(
self.BASE_DIRECTORY, RTS_folder, "RTS_Data"
) # code_directory
self.FORMATTED_INPUTS_DIRECTORY = os.path.join(
self.RTS_GMLC_DIRECTORY, "FormattedData"
)
self.SOURCE_INPUTS_DIRECTORY = os.path.join(
self.RTS_GMLC_DIRECTORY, "SourceData"
)
self.TIMESERIES_INPUTS_DIRECTORY = os.path.join(
self.RTS_GMLC_DIRECTORY, "timeseries_data_files"
)
self.HEATRATE_INPUTS_DIRECTORY = os.path.join(
self.SOURCE_INPUTS_DIRECTORY, "HR_data"
)
self.MPEC_DIRECTORY = os.path.join(self.BASE_DIRECTORY, MPEC_folder)
self.CASE_DIRECTORY = os.path.join(self.MPEC_DIRECTORY, results_folder)
self.RESULTS_DIRECTORY = os.path.join(
self.MPEC_DIRECTORY, results_folder + "\\" + results_case
)
self.RESULTS_INPUTS_DIRECTORY = os.path.join(self.RESULTS_DIRECTORY, "inputs")
def make_directories(self):
"""create any directories that don't already exist (applies only to output directories)
"""
if not os.path.exists(self.CASE_DIRECTORY):
os.mkdir(self.CASE_DIRECTORY)
if not os.path.exists(self.RESULTS_DIRECTORY):
os.mkdir(self.RESULTS_DIRECTORY)
if not os.path.exists(self.RESULTS_INPUTS_DIRECTORY):
os.mkdir(self.RESULTS_INPUTS_DIRECTORY)
# class for loading NREL RTS case data
class LoadNRELData(object):
def __init__(self, f):
self.f = f
self.nrel_dict = {}
def load_nrel_data(self):
"""
Arguments:
f {class(DirStructure)} -- a folder directory for the case. Needs pointer to NREL data
Returns:
[dict] -- dictionary containing dataframes with loaded NREL data
"""
## NREL SourceData folder imports ##
self.nrel_dict["branch_data"] = pd.read_csv(
os.path.join(self.f.SOURCE_INPUTS_DIRECTORY, "branch.csv")
)
self.nrel_dict["bus_data"] = pd.read_csv(
os.path.join(self.f.SOURCE_INPUTS_DIRECTORY, "bus.csv")
)
self.nrel_dict["dcbranch_data"] = pd.read_csv(
os.path.join(self.f.SOURCE_INPUTS_DIRECTORY, "dc_branch.csv")
)
self.nrel_dict["gen_data"] = pd.read_csv(
os.path.join(self.f.SOURCE_INPUTS_DIRECTORY, "gen.csv")
)
self.nrel_dict["reserves_data"] = pd.read_csv(
os.path.join(self.f.SOURCE_INPUTS_DIRECTORY, "reserves.csv")
)
self.nrel_dict["simulationobjects_data"] = pd.read_csv(
os.path.join(self.f.SOURCE_INPUTS_DIRECTORY, "simulation_objects.csv")
)
self.nrel_dict["storage_data"] = pd.read_csv(
os.path.join(self.f.SOURCE_INPUTS_DIRECTORY, "storage.csv")
)
self.nrel_dict["timeseriespointers_data"] = pd.read_csv(
os.path.join(self.f.SOURCE_INPUTS_DIRECTORY, "timeseries_pointers.csv")
)
## NREL TimeSeries folder imports ##
# you'll note the basic difference here is these are just params, like load, that will be indexed by time
# for now I pull the day-ahead ("DA") time-series files, since the model is only at hourly resolution.
# generation
self.nrel_dict["hydro_data"] = pd.read_csv(
os.path.join(
self.f.TIMESERIES_INPUTS_DIRECTORY, "Hydro\\DAY_AHEAD_hydro.csv"
)
)
self.nrel_dict["load_data"] = pd.read_csv(
os.path.join(
self.f.TIMESERIES_INPUTS_DIRECTORY, "Load\\DAY_AHEAD_regional_Load.csv"
)
)
self.nrel_dict["pv_data"] = pd.read_csv(
os.path.join(self.f.TIMESERIES_INPUTS_DIRECTORY, "PV\\DAY_AHEAD_pv.csv")
)
self.nrel_dict["rtpv_data"] = pd.read_csv(
os.path.join(self.f.TIMESERIES_INPUTS_DIRECTORY, "RTPV\\DAY_AHEAD_rtpv.csv")
)
self.nrel_dict["csp_data"] = pd.read_csv(
os.path.join(
self.f.TIMESERIES_INPUTS_DIRECTORY, "CSP\\DAY_AHEAD_Natural_Inflow.csv"
)
)
self.nrel_dict["wind_data"] = pd.read_csv(
os.path.join(self.f.TIMESERIES_INPUTS_DIRECTORY, "Wind\\DAY_AHEAD_Wind.csv")
)
# real-time generation
self.nrel_dict["hydro_data_rt"] = pd.read_csv(
os.path.join(
self.f.TIMESERIES_INPUTS_DIRECTORY, "Hydro\\REAL_TIME_hydro.csv"
)
)
self.nrel_dict["load_data_rt"] = pd.read_csv(
os.path.join(
self.f.TIMESERIES_INPUTS_DIRECTORY, "Load\\REAL_TIME_regional_Load.csv"
)
)
self.nrel_dict["pv_data_rt"] = pd.read_csv(
os.path.join(self.f.TIMESERIES_INPUTS_DIRECTORY, "PV\\REAL_TIME_pv.csv")
)
self.nrel_dict["rtpv_data_rt"] = pd.read_csv(
os.path.join(self.f.TIMESERIES_INPUTS_DIRECTORY, "RTPV\\REAL_TIME_rtpv.csv")
)
self.nrel_dict["csp_data_rt"] = pd.read_csv(
os.path.join(
self.f.TIMESERIES_INPUTS_DIRECTORY, "CSP\\REAL_TIME_Natural_Inflow.csv"
)
)
self.nrel_dict["wind_data_rt"] = pd.read_csv(
os.path.join(self.f.TIMESERIES_INPUTS_DIRECTORY, "Wind\\REAL_TIME_Wind.csv")
)
# reserves
self.nrel_dict["reg_up_data"] = pd.read_csv(
os.path.join(
self.f.TIMESERIES_INPUTS_DIRECTORY,
"Reserves\\DAY_AHEAD_regional_Reg_Up.csv",
)
)
self.nrel_dict["reg_down_data"] = pd.read_csv(
os.path.join(
self.f.TIMESERIES_INPUTS_DIRECTORY,
"Reserves\\DAY_AHEAD_regional_Reg_Down.csv",
)
)
self.nrel_dict["flex_up_data"] = pd.read_csv(
os.path.join(
self.f.TIMESERIES_INPUTS_DIRECTORY,
"Reserves\\DAY_AHEAD_regional_Flex_Up.csv",
)
)
self.nrel_dict["flex_down_data"] = pd.read_csv(
os.path.join(
self.f.TIMESERIES_INPUTS_DIRECTORY,
"Reserves\\DAY_AHEAD_regional_Flex_Down.csv",
)
)
return self.nrel_dict
def define_constants(self, input_dict):
"""defines constants to be used in case with input data
Arguments:
input_dict {dict} -- dictionary of input data, must already exist to run this method
Returns:
input_dict {dict} -- dictionary of input data updated with constants to be used in the case
"""
input_dict["hours"] = 24
input_dict["periods"] = 288 #Need to config periods if change RT resolution
input_dict["lb_to_tonne"] = 0.000453592
input_dict["baseMVA"] = 100
input_dict["km_per_mile"] = 1.60934
return input_dict
def add_unit(self, adds_list, adds_type):
for n, i in enumerate(adds_list):
gentype = adds_type[n]
if i not in self.nrel_dict["bus_data"]["Bus ID"].unique():
raise ValueError("assigned generator buses must exist in dataset")
copied_data = (
self.nrel_dict["gen_data"]
.loc[self.nrel_dict["gen_data"]["GEN UID"] == gentype]
.values
) # copy generator
copied_data[0][1] = i # replace bus ID
copied_data[0][0] = re.sub(
r"\d+", str(i), copied_data[0][0], 1
) # will make generator name unique
copied_data[0][0] = self.update_unique(
copied_data[0][0]
) # ensures generator name unique by giving new ID
new_data = pd.DataFrame(
copied_data,
index=[len(self.nrel_dict["gen_data"].index)],
columns=self.nrel_dict["gen_data"].columns,
)
self.nrel_dict["gen_data"] = self.nrel_dict["gen_data"].append(new_data)
for j in ["hydro_data", "pv_data", "rtpv_data", "csp_data", "wind_data"]:
if gentype in self.nrel_dict[j]:
copied_profile = self.nrel_dict[j][gentype].values
self.nrel_dict[j][copied_data[0][0]] = copied_profile
def update_unique(self, gen_str):
if gen_str in list(self.nrel_dict["gen_data"]["GEN UID"].values):
next_int = re.findall(r"_(\d+)", gen_str)
gen_str = re.sub(r"_(\d+)", "_" + str(int(next_int[0]) + 1), gen_str)
if gen_str in list(self.nrel_dict["gen_data"]["GEN UID"].values):
return self.update_unique(gen_str) # recursion if still not unique
else:
return gen_str
else:
return gen_str
# Class for creating case (this is the big, complicated part)
class CreateRTSCase(object):
def __init__(self, gentypes, directory, hour_begin, **kwargs):
self.gentypes = gentypes
self.directory = directory
self.hour_begin = hour_begin
self.period_begin = hour_begin
# data loads as kwargs
# constants are also kwargs
for k, v in kwargs.items():
setattr(self, k, v)
self.hour_end = hour_begin + self.hours
self.period_end = hour_begin + self.periods
def __getattr__(self, name):
# suppresses warning about **kwargs not existing
warnings.warn('No member "%s" contained in settings config.' % name)
return ""
def dict_to_csv(
self,
filename,
mydict,
index=["Gen_Index"],
owned_gens=False,
nuc_gens=False,
hybrid_gens=False,
):
df = pd.DataFrame.from_dict(mydict)
df.set_index(index, inplace=True)
if owned_gens:
for gen in self.owned_gen_list:
df.at[
gen, "GencoIndex"
] = 1 # overwrite to make owned by competitive agent when applicable
if nuc_gens:
for gen in self.nuc_gen_list:
df.at[
gen, "UCIndex"
] = 1 # overwrite to make owned by competitive agent when applicable
if hybrid_gens:
for gen in self.hybrid_gen_list:
df.at[
gen, "HybridIndex"
] = 1 # overwrite to make owned by competitive agent when applicable
df.to_csv(
os.path.join(self.directory.RESULTS_INPUTS_DIRECTORY, filename + ".csv")
)
def df_to_csv(
self, filename, mydict, owned_storage=False, hybrid_storage=False,
):
df = pd.DataFrame.from_dict(mydict)
df.set_index(["Storage_Index"], inplace=True)
if owned_storage:
for st in self.owned_storage_list:
df.at[
st, "StorageIndex"
] = 1 # overwrite to make owned by competitive agent when applicable
if hybrid_storage:
for st in self.hybrid_storage_list:
df.at[st, "HybridIndex"] = 1
df.to_csv(
os.path.join(self.directory.RESULTS_INPUTS_DIRECTORY, filename + ".csv")
)
def create_index(self):
case_index_df = pd.DataFrame({"ucgen": [1], "genco": [1], "": ["index"]})
case_index_df.set_index([""], inplace=True)
case_index_df.to_csv(
os.path.join(self.directory.RESULTS_INPUTS_DIRECTORY, "case_index.csv")
)
def generators(
self,
filename,
owned_gen_list=[],
nuc_gen_list=[],
hybrid_gen_list=[],
retained_bus_list=[],
start_cost_scalar=1,
no_load_cost_scalar=1,
pmin_scalar=1,
):
self.generators_dict = {}
self.owned_gen_list = owned_gen_list
self.nuc_gen_list = nuc_gen_list
self.hybrid_gen_list = hybrid_gen_list
index_list = [
"Gen_Index",
"Capacity",
"Fuel_Cost",
"Pmin",
"start_cost",
"No_Load_Cost",
"Ramp_Rate",
"tonneCO2perMWh",
"CO2price",
"CO2dollarsperMWh",
"ZoneLabel",
"GencoIndex",
"UCIndex",
"HybridIndex",
]
if retained_bus_list == []:
pass # print("default")
else:
self.gen_data = self.gen_data[
self.gen_data["Bus ID"].isin(retained_bus_list)
].copy()
self.retained_bus_list = retained_bus_list
self.generators_dict[index_list[0]] = self.gen_data[
self.gen_data["Unit Type"].isin(self.gentypes)
]["GEN UID"].values
self.generators_dict[index_list[1]] = self.gen_data[
self.gen_data["Unit Type"].isin(self.gentypes)
]["PMax MW"].values
self.gen_data.loc[:, "$/MWH"] = (
self.gen_data.loc[:, "Fuel Price $/MMBTU"]
* self.gen_data.loc[:, "HR_incr_3"]
/ 1000
)
self.generators_dict[index_list[2]] = self.gen_data[
self.gen_data["Unit Type"].isin(self.gentypes)
]["$/MWH"].values
# self.generators_dict[index_list[3]] = [0] * len(
# self.generators_dict[index_list[0]]
# )
self.generators_dict[index_list[3]] = [
pmin_scalar
* self.gen_data.at[i, "PMin MW"]
/ self.gen_data.at[i, "PMax MW"]
for i in self.gen_data.index[self.gen_data["Unit Type"].isin(self.gentypes)]
]
self.generators_dict[index_list[4]] = [
start_cost_scalar
* self.gen_data.at[i, "Fuel Price $/MMBTU"]
* self.gen_data.at[i, "Start Heat Hot MBTU"]
for i in self.gen_data.index[self.gen_data["Unit Type"].isin(self.gentypes)]
]
# self.generators_dict[index_list[5]] = [0] * len(
# self.generators_dict[index_list[0]]
# )
self.generators_dict[index_list[5]] = [
no_load_cost_scalar
* (self.gen_data.at[i, "HR_avg_0"] - self.gen_data.at[i, "HR_incr_1"])
* 0.001
* self.gen_data.at[i, "Fuel Price $/MMBTU"]
* (self.gen_data.at[i, "PMax MW"] * self.gen_data.at[i, "Output_pct_0"])
for i in self.gen_data.index[self.gen_data["Unit Type"].isin(self.gentypes)]
]
# ramp rates
self.generators_dict[index_list[6]] = (
60
* self.gen_data[self.gen_data["Unit Type"].isin(self.gentypes)][
"Ramp Rate MW/Min"
].values
)
self.gen_data.loc[:, "CO2/MWH"] = (
self.gen_data.loc[:, "Emissions CO2 Lbs/MMBTU"]
* self.lb_to_tonne
* self.gen_data.loc[:, "HR_incr_3"]
/ 1000
)
self.generators_dict[index_list[7]] = self.gen_data[
self.gen_data["Unit Type"].isin(self.gentypes)
]["CO2/MWH"].values
self.generators_dict[index_list[8]] = [0] * len(
self.generators_dict[index_list[0]]
)
self.generators_dict[index_list[9]] = [
a * b
for a, b in zip(
self.generators_dict[index_list[7]],
self.generators_dict[index_list[8]],
)
]
self.generators_dict[index_list[10]] = self.gen_data[
self.gen_data["Unit Type"].isin(self.gentypes)
]["Bus ID"].values
self.generators_dict[index_list[11]] = [2] * len(
self.generators_dict[index_list[0]]
)
self.generators_dict[index_list[12]] = [2] * len(
self.generators_dict[index_list[0]]
)
self.generators_dict[index_list[13]] = [2] * len(
self.generators_dict[index_list[0]]
)
# for gen in owned_gen_list:
self.dict_to_csv(
filename,
self.generators_dict,
owned_gens=True,
nuc_gens=True,
hybrid_gens=True,
)
def generators_descriptive(self, filename):
d = {}
index_list = [
"Gen_Index",
"Name",
"Zone",
"Category",
"Capacity",
"Fuel_Cost",
"UTILUNIT",
]
d[index_list[0]] = self.generators_dict["Gen_Index"]
d[index_list[1]] = self.generators_dict["Gen_Index"]
d[index_list[2]] = self.generators_dict["ZoneLabel"]
d[index_list[3]] = self.gen_data[
self.gen_data["Unit Type"].isin(self.gentypes)
]["Category"].values
d[index_list[4]] = self.generators_dict["Capacity"]
d[index_list[5]] = self.generators_dict["Fuel_Cost"]
d[index_list[6]] = ["NA"] * len(self.generators_dict[index_list[0]])
self.dict_to_csv(filename, d)
def storage(
self,
filename,
owned_storage_list=[],
hybrid_storage_list=[],
capacity_scalar=1,
duration_scalar=1,
busID=313,
RTEff=1.0,
):
self.storage_dict = {}
self.owned_storage_list = (owned_storage_list,)
self.hybrid_storage_list = hybrid_storage_list
index_list = [
"Storage_Index",
"Discharge",
"Charge",
"SOCMax",
"DischargeEff",
"ChargeEff",
"StorageZoneLabel",
"StorageIndex",
"HybridIndex",
]
# size_scalar*
self.storage_dict[index_list[0]] = self.gen_data[
self.gen_data["Unit Type"] == "STORAGE"
]["GEN UID"].values
self.storage_dict[index_list[1]] = self.gen_data[
self.gen_data["Unit Type"] == "STORAGE"
]["PMax MW"].values
self.storage_dict[index_list[2]] = self.gen_data[
self.gen_data["Unit Type"] == "STORAGE"
]["PMax MW"].values
self.storage_dict[index_list[3]] = [
duration_scalar * self.storage_data.at[1, "Max Volume GWh"] * 1000
] * len(self.storage_dict[index_list[0]])
self.storage_dict[index_list[4]] = [1.0] * len(self.storage_dict[index_list[0]])
self.storage_dict[index_list[5]] = [RTEff] * len(
self.storage_dict[index_list[0]]
)
self.storage_dict[index_list[6]] = list(
self.gen_data[self.gen_data["Unit Type"] == "STORAGE"]["Bus ID"]
)
if busID in self.retained_bus_list:
self.storage_dict[index_list[6]][
0
] = busID # replace first element to retain this behavior
else:
print(
"passed invalid BusID, so first storage resource will remain at bus "
+ str(self.storage_dict[index_list[6]][0])
)
self.storage_dict[index_list[7]] = [2] * len(self.storage_dict[index_list[0]])
self.storage_dict[index_list[8]] = [2] * len(self.storage_dict[index_list[0]])
self.df_to_csv(
filename, self.storage_dict, owned_storage=True, hybrid_storage=True
)
def scheduled_gens(self, filename):
scheduled_dict = {}
index_list = ["timepoint", "Gen_Index", "available", "Capacity", "Fuel_Cost"]
scheduled_dict[index_list[0]] = [
e
for e in list(range(1, self.hours + 1))
for i in range(len(self.generators_dict["Gen_Index"]))
]
scheduled_dict[index_list[1]] = (
list(self.generators_dict["Gen_Index"]) * self.hours
)
scheduled_dict[index_list[2]] = [1] * len(scheduled_dict[index_list[1]])
gen_cap_list = []
for h in range(self.hour_begin, self.hour_end):
for gen, capacity in zip(
self.generators_dict["Gen_Index"], self.generators_dict["Capacity"],
):
if gen in self.hydro_data.columns:
gen_cap_list.append(self.hydro_data.at[h, gen])
elif gen in self.pv_data.columns:
gen_cap_list.append(self.pv_data.at[h, gen])
elif gen in self.rtpv_data.columns:
gen_cap_list.append(self.rtpv_data.at[h, gen])
elif gen in self.csp_data.columns:
gen_cap_list.append(self.csp_data.at[h, gen])
elif gen in self.wind_data.columns:
gen_cap_list.append(self.wind_data.at[h, gen])
else:
gen_cap_list.append(capacity)
scheduled_dict[index_list[3]] = gen_cap_list
scheduled_dict[index_list[4]] = (
list(self.generators_dict["Fuel_Cost"]) * self.hours
)
self.dict_to_csv(filename, scheduled_dict, index="timepoint")
def scheduled_gens_rt(self, filename):
scheduled_dict = {}
index_list = ["timepoint", "Gen_Index", "available", "Capacity", "Fuel_Cost"]
scheduled_dict[index_list[0]] = [
e
for e in list(range(1, self.periods + 1))
for i in range(len(self.generators_dict["Gen_Index"]))
]
scheduled_dict[index_list[1]] = (
list(self.generators_dict["Gen_Index"]) * self.periods
)
scheduled_dict[index_list[2]] = [1] * len(scheduled_dict[index_list[1]])
gen_cap_list = []
for h in range(self.period_begin, self.period_end):
for gen, capacity in zip(
self.generators_dict["Gen_Index"],
self.generators_dict["Capacity"],
):
if gen in self.hydro_data_rt.columns:
gen_cap_list.append(self.hydro_data_rt.at[h, gen])
elif gen in self.pv_data_rt.columns:
gen_cap_list.append(self.pv_data_rt.at[h, gen])
elif gen in self.rtpv_data_rt.columns:
gen_cap_list.append(self.rtpv_data_rt.at[h, gen])
elif gen in self.csp_data_rt.columns:
gen_cap_list.append(self.csp_data_rt.at[h, gen])
elif gen in self.wind_data_rt.columns:
gen_cap_list.append(self.wind_data_rt.at[h, gen])
else:
gen_cap_list.append(capacity)
scheduled_dict[index_list[3]] = gen_cap_list
scheduled_dict[index_list[4]] = (
list(self.generators_dict["Fuel_Cost"]) * self.periods
)
self.dict_to_csv(filename, scheduled_dict, index="timepoint")
def scheduled_gens_rt_da_tmp(self, filename):
scheduled_dict = {}
scheduled_gens = pd.read_csv(
os.path.join(self.directory.RESULTS_INPUTS_DIRECTORY, "generators_scheduled_availability.csv")
)
scheduled_gens_rt = pd.read_csv(
os.path.join(self.directory.RESULTS_INPUTS_DIRECTORY, "generators_scheduled_availability_rt.csv")
)
for h in range(1, 25):
tmp = scheduled_gens_rt.loc[scheduled_gens_rt['timepoint'].isin(range((h-1)*12+1,h*12+1))]
mean = tmp.groupby('Gen_Index')['Capacity'].mean()
for gen in self.generators_dict["Gen_Index"]:
scheduled_gens.loc[(scheduled_gens['timepoint']==h) & (scheduled_gens['Gen_Index']==gen), 'Capacity'] = mean[gen]
scheduled_gens.to_csv(
os.path.join(self.directory.RESULTS_INPUTS_DIRECTORY, filename + ".csv"), index = False
)
def timepoints(self, filename):
self.timepoint_dict = {}
index_list = ["timepoint", "reference_bus", "hours"]
self.timepoint_dict[index_list[0]] = list(range(1, self.hours + 1))
if self.retained_bus_list == []:
self.timepoint_dict[index_list[1]] = [self.bus_data.at[12, "Bus ID"]] * len(
self.timepoint_dict[index_list[0]]
)
else:
self.timepoint_dict[index_list[1]] = [self.retained_bus_list[0]] * len(
self.timepoint_dict[index_list[0]]
)
self.timepoint_dict[index_list[2]] = [1] * len(
self.timepoint_dict[index_list[0]]
) # DA assumed hourly resolution
self.dict_to_csv(filename, self.timepoint_dict, index="timepoint")
def timepoints_rt(self, filename):
self.timepoint_rt_dict = {}
index_list = ["timepoint", "reference_bus", "hours"]
self.timepoint_rt_dict[index_list[0]] = list(range(1, self.periods + 1))
if self.retained_bus_list == []:
self.timepoint_rt_dict[index_list[1]] = [
self.bus_data.at[12, "Bus ID"]
] * len(self.timepoint_rt_dict[index_list[0]])
else:
self.timepoint_rt_dict[index_list[1]] = [self.retained_bus_list[0]] * len(
self.timepoint_rt_dict[index_list[0]]
)
self.timepoint_rt_dict[index_list[2]] = [
float(self.hours) / float(self.periods)
] * len(
self.timepoint_rt_dict[index_list[0]]
) # DA assumed hourly resolution
self.dict_to_csv(filename, self.timepoint_rt_dict, index="timepoint")
def zones(self, filename):
self.zone_dict = {}
index_list = [
"zone",
"voltage_angle_max",
"voltage_angle_min",
]
if self.retained_bus_list == []:
pass # print("default")
else:
self.bus_data = self.bus_data[
self.bus_data["Bus ID"].isin(self.retained_bus_list)
]
self.zone_dict[index_list[0]] = self.bus_data.loc[:, "Bus ID"].values
self.zone_dict[index_list[1]] = [180] * len(
self.zone_dict[index_list[0]]
) # math.pi/3
self.zone_dict[index_list[2]] = [-180] * len(
self.zone_dict[index_list[0]]
) # math.pi/3
self.dict_to_csv(filename, self.zone_dict, index="zone")
def zonal_loads(self, filename):
bus_df = self.bus_data[["Bus ID", "MW Load", "Area"]]
bus_df_load = pd.merge(
bus_df,
bus_df.groupby("Area").sum()[["MW Load"]].reset_index(),
how="left",
left_on="Area",
right_on="Area",
)
bus_df_load["Frac Load"] = bus_df_load["MW Load_x"] / bus_df_load["MW Load_y"]
hourly_df = pd.concat([bus_df_load] * self.hours, ignore_index=True)
hourly_df["timepoint"] = [
e for e in self.timepoint_dict["timepoint"] for i in self.zone_dict["zone"]
]
# print(load_data)
load_short = (
self.load_data.loc[self.hour_begin : self.hour_end - 1]
.set_index(["Month", "Day", "Period"])
.reset_index()
) # sets unique index
hourly_df["zonal_load"] = [
load_short.at[t - 1, str(a)]
for t, a in zip(hourly_df["timepoint"].values, hourly_df["Area"].values)
]
hourly_df["bus load"] = hourly_df["zonal_load"] * hourly_df["Frac Load"]
time_zone_dict = {}
index_list = ["timepoint", "zone", "gross_load"]
time_zone_dict[index_list[0]] = [
e for e in self.timepoint_dict["timepoint"] for i in self.zone_dict["zone"]
]
time_zone_dict[index_list[1]] = [
i for e in self.timepoint_dict["timepoint"] for i in self.zone_dict["zone"]
]
time_zone_dict[index_list[2]] = hourly_df[
"bus load"
].values # hourly_df['MW Load_x'].values
self.dict_to_csv(filename, time_zone_dict, index="timepoint")
def zonal_loads_rt(self, filename):
bus_df = self.bus_data[["Bus ID", "MW Load", "Area"]]
bus_df_load = pd.merge(
bus_df,
bus_df.groupby("Area").sum()[["MW Load"]].reset_index(),
how="left",
left_on="Area",
right_on="Area",
)
bus_df_load["Frac Load"] = bus_df_load["MW Load_x"] / bus_df_load["MW Load_y"]
hourly_df = pd.concat([bus_df_load] * self.periods, ignore_index=True)
hourly_df["timepoint"] = [
e
for e in self.timepoint_rt_dict["timepoint"]
for i in self.zone_dict["zone"]
]
# print(load_data_rt)
load_short = (
self.load_data_rt.loc[self.hour_begin : self.period_end - 1]
.set_index(["Month", "Day", "Period"])
.reset_index()
) # sets unique index
hourly_df["zonal_load"] = [
load_short.at[t - 1, str(a)]
for t, a in zip(hourly_df["timepoint"].values, hourly_df["Area"].values)
]
hourly_df["bus load"] = hourly_df["zonal_load"] * hourly_df["Frac Load"]
time_zone_dict = {}
index_list = ["timepoint", "zone", "gross_load"]
time_zone_dict[index_list[0]] = [
e
for e in self.timepoint_rt_dict["timepoint"]
for i in self.zone_dict["zone"]
]
time_zone_dict[index_list[1]] = [
i
for e in self.timepoint_rt_dict["timepoint"]
for i in self.zone_dict["zone"]
]
time_zone_dict[index_list[2]] = hourly_df[
"bus load"
].values # hourly_df['MW Load_x'].values
self.dict_to_csv(filename, time_zone_dict, index="timepoint")
def tx_lines(self, filename):
branch_data_update = pd.merge(
self.branch_data,
self.bus_data[["Bus ID", "BaseKV"]],
how="left",
left_on="From Bus",
right_on="Bus ID",
)
branch_data_update["puconversion"] = (
branch_data_update.BaseKV ** 2 / self.baseMVA
) # V^2/P=R, kv^2 and MV will cancel units
branch_data_update["Lengthkm"] = branch_data_update["Length"] * self.km_per_mile
for i in branch_data_update.index:
if branch_data_update.at[i, "B"] == 0:
branch_data_update.at[i, "puconversion"] = branch_data_update.at[
i, "Tr Ratio"
]
branch_data_update.at[i, "Lengthkm"] = 1
else:
branch_data_update.at[i, "puconversion"] = 1
branch_data_update["x_final"] = (
branch_data_update.X * branch_data_update.puconversion
) # *branch_data_update.Lengthkm
branch_data_update["r_final"] = (
branch_data_update.R * branch_data_update.puconversion
) # *branch_data_update.Lengthkm
self.branch_data_final = (
branch_data_update # [branch_data_update['Lengthkm']!=0].copy()
)
self.branch_data_final["Susceptance"] = [
1 / x * 100 * math.pi / 180
for x in self.branch_data_final["x_final"].values
]
self.tx_dict = {}
index_list = ["transmission_line", "susceptance"]
if self.retained_bus_list == []:
pass # print("default")
else:
self.branch_data_final = self.branch_data_final[
self.branch_data_final["From Bus"].isin(self.retained_bus_list)
]
self.branch_data_final = self.branch_data_final[
self.branch_data_final["To Bus"].isin(self.retained_bus_list)
]
self.tx_dict[index_list[0]] = self.branch_data_final["UID"].values
self.tx_dict[index_list[1]] = self.branch_data_final["Susceptance"].values
self.dict_to_csv(filename, self.tx_dict, index="transmission_line")
def tx_lines_hourly(self, filename, flow_multiplier=1):
tx_hourly_dict = {}
index_list = [
"timepoint",
"transmission_line",
"transmission_from",
"transmission_to",
"min_flow",
"max_flow",
]
tx_hourly_dict[index_list[0]] = [
t
for t in self.timepoint_dict["timepoint"]
for line in self.tx_dict["transmission_line"]
]
tx_hourly_dict[index_list[1]] = [
line
for t in self.timepoint_dict["timepoint"]
for line in self.tx_dict["transmission_line"]
]
tx_hourly_dict[index_list[2]] = [
t_from
for t in self.timepoint_dict["timepoint"]
for t_from in self.branch_data_final["From Bus"].values
]
tx_hourly_dict[index_list[3]] = [
t_to
for t in self.timepoint_dict["timepoint"]
for t_to in self.branch_data_final["To Bus"].values
]
tx_hourly_dict[index_list[4]] = [
-flow_multiplier * flow
for t in self.timepoint_dict["timepoint"]
for flow in self.branch_data_final["Cont Rating"].values
]
tx_hourly_dict[index_list[5]] = [
flow_multiplier * flow
for t in self.timepoint_dict["timepoint"]
for flow in self.branch_data_final["Cont Rating"].values
]
self.dict_to_csv(
filename, tx_hourly_dict, index=["timepoint", "transmission_line"]
)
def tx_lines_hourly_rt(self, filename, flow_multiplier=1):
tx_hourly_dict = {}
index_list = [
"timepoint",
"transmission_line",
"transmission_from",
"transmission_to",
"min_flow",
"max_flow",
]
tx_hourly_dict[index_list[0]] = [
t
for t in self.timepoint_rt_dict["timepoint"]
for line in self.tx_dict["transmission_line"]
]
tx_hourly_dict[index_list[1]] = [
line
for t in self.timepoint_rt_dict["timepoint"]
for line in self.tx_dict["transmission_line"]
]
tx_hourly_dict[index_list[2]] = [
t_from
for t in self.timepoint_rt_dict["timepoint"]
for t_from in self.branch_data_final["From Bus"].values
]
tx_hourly_dict[index_list[3]] = [
t_to
for t in self.timepoint_rt_dict["timepoint"]
for t_to in self.branch_data_final["To Bus"].values
]
tx_hourly_dict[index_list[4]] = [
-flow_multiplier * flow
for t in self.timepoint_rt_dict["timepoint"]
for flow in self.branch_data_final["Cont Rating"].values
]
tx_hourly_dict[index_list[5]] = [
flow_multiplier * flow
for t in self.timepoint_rt_dict["timepoint"]
for flow in self.branch_data_final["Cont Rating"].values
]
self.dict_to_csv(
filename, tx_hourly_dict, index=["timepoint", "transmission_line"]
)
def gs(self):
self.gs_list = [0, 1, 2, 3]
gs_df = pd.DataFrame(
{"generator_segment": self.gs_list, "length": [0.4, 0.2, 0.2, 0.2]}
)
gs_df.set_index(["generator_segment"], inplace=True)
gs_df.to_csv(
os.path.join(
self.directory.RESULTS_INPUTS_DIRECTORY, "generator_segments.csv"
)
)
def gs_seg(self, CO2price=0):
gs_seg_dict = {}
index_list = [
"time",
"Gen_Index",
"generator_segment",
"segment_length",
"marginal_cost",
"prev_offer",
"marginal_CO2",
"CO2damage",
]
n_tmps = len(self.timepoint_dict["timepoint"])
first_tmp = [self.timepoint_dict["timepoint"].pop(0)]
gs_seg_dict[index_list[0]] = [
t
for t in first_tmp
for gen in self.generators_dict["Gen_Index"]
for gs in self.gs_list
]
gs_seg_dict[index_list[1]] = [
gen
for t in first_tmp
for gen in self.generators_dict["Gen_Index"]
for gs in self.gs_list
]
gs_seg_dict[index_list[2]] = [
gs
for t in first_tmp
for gen in self.generators_dict["Gen_Index"]
for gs in self.gs_list
]
mcos_list = []
emissions_list = []
seg_length_list = []
for gen, gs in zip(gs_seg_dict[index_list[1]], gs_seg_dict[index_list[2]]):
EmissionsRate = (
self.gen_data.set_index("GEN UID").at[gen, "Emissions CO2 Lbs/MMBTU"]
* self.lb_to_tonne
)
if gs == 0:
seg_length_list.append(
self.gen_data.set_index("GEN UID").at[gen, "Output_pct_" + str(gs)]
)
if (
self.gen_data.set_index("GEN UID").at[gen, "HR_incr_" + str(1)]
/ 1000
!= 0
):
HeatRate = (
self.gen_data.set_index("GEN UID").at[gen, "HR_incr_" + str(1)]
/ 1000
) # convexify offer curve
else:
HeatRate = (
self.gen_data.set_index("GEN UID").at[gen, "HR_avg_" + str(gs)]
/ 1000
)
mcos_list.append(
HeatRate
* self.gen_data.set_index("GEN UID").at[gen, "Fuel Price $/MMBTU"]
)
emissions_list.append(HeatRate * EmissionsRate)
else:
seg_length_list.append(
max(
0,
self.gen_data.set_index("GEN UID").at[
gen, "Output_pct_" + str(gs)