forked from NREL/ReEDS-2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Augur.py
195 lines (168 loc) · 6.73 KB
/
Augur.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
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 16 16:30:25 2020
@author: afrazier
"""
#%% Imports
import argparse
import os
import sys
import subprocess
import datetime
import pandas as pd
import gdxpds
import ReEDS_Augur.prep_data as prep_data
import ReEDS_Augur.capacity_credit as capacity_credit
import ReEDS_Augur.stress_periods as stress_periods
import ReEDS_Augur.functions as functions
#%% Functions
def run_pras(
casedir, t, sw, iteration=0, recordtime=True,
repo=False, overwrite=True, include_samples=False,
write_flow=False, write_surplus=False, write_energy=False,
):
"""
"""
### Get the PRAS settings for this solve year
print('Running ReEDS2PRAS and PRAS')
scriptpath = (sw['reeds_path'] if repo else casedir)
command = [
"julia",
f"--project={sw['reeds_path']}",
### As of 20231113 there seems to be a problem with multithreading in julia on
### mac M1 machines and Kestrel that causes multithreaded processes to hang
### without resolution. So disable multithreading on those systems.
('--threads=1' if (
(sys.platform == 'darwin') or (os.environ.get('NREL_CLUSTER') == 'kestrel')
) else f"--threads={sw['threads'] if sw['threads'] > 0 else 'auto'}"),
f"{os.path.join(scriptpath, 'ReEDS_Augur','run_pras.jl')}",
f"--reeds_path={sw['reeds_path']}",
f"--reedscase={casedir}",
f"--solve_year={t}",
"--weather_year=2007",
"--timesteps=61320",
f"--write_flow={int(write_flow)}",
f"--write_surplus={int(write_surplus)}",
f"--write_energy={int(write_energy)}",
f"--iteration={iteration}",
f"--samples={sw['pras_samples']}",
f"--overwrite={int(overwrite)}",
f"--include_samples={int(include_samples)}",
]
print(' '.join(command))
print(f'vvvvvvvvvvvvvvv run_pras.jl {t}i{iteration} vvvvvvvvvvvvvvv')
log = open(os.path.join(casedir, 'gamslog.txt'), 'a')
result = subprocess.run(command, stdout=log, stderr=log, text=True)
log.close()
print(f'^^^^^^^^^^^^^^^ run_pras.jl {t}i{iteration} ^^^^^^^^^^^^^^^')
if recordtime:
try:
functions.write_last_pras_runtime(year=t)
except Exception as err:
print(err)
return result
#%% Main function
def main(t, tnext, casedir, iteration=0):
# #%% To debug, uncomment these lines and update the run path
# t = 2020
# tnext = 2023
# reeds_path = os.path.expanduser('~/github2/ReEDS-2.0')
# casedir = os.path.join(
# reeds_path,'runs','v20240708_tforM1_Pacific')
# iteration = 0
# assert tnext >= t
# os.chdir(casedir)
# # ## Copy reeds2pras from repo to run folder
# # import shutil
# # shutil.rmtree(os.path.join(casedir, 'reeds2pras'))
# # shutil.copytree(
# # os.path.join(reeds_path, 'reeds2pras'),
# # os.path.join(casedir, 'reeds2pras'),
# # ignore=shutil.ignore_patterns('test'),
# # )
#%% Get switches from inputs_case/switches.csv and ReEDS_Augur/augur_switches.csv
sw = functions.get_switches(casedir)
sw['t'] = t
#%% Prep data for resource adequacy
print('Preparing data for resource adequacy calculations')
tic = datetime.datetime.now()
augur_csv, augur_h5 = prep_data.main(t, casedir)
functions.toc(tic=tic, year=t, process='ReEDS_Augur/prep_data.py')
#%% Calculate capacity credit if necessary; otherwise bypass
print('calculating capacity credit...')
tic = datetime.datetime.now()
if int(sw['GSw_PRM_CapCredit']):
cc_results = capacity_credit.reeds_cc(t, tnext, casedir)
else:
cc_results = {
'cc_mar': pd.DataFrame(columns=['i','r','ccreg','szn','t','Value']),
'cc_old': pd.DataFrame(columns=['i','r','ccreg','szn','t','Value']),
'cc_dr': pd.DataFrame(columns=['i','r','szn','t','Value']),
'sdbin_size': pd.DataFrame(columns=['ccreg','szn','bin','t','Value']),
}
functions.toc(tic=tic, year=t, process='ReEDS_Augur/capacity_credit.py')
#%% Run PRAS if necessary
solveyears = pd.read_csv(
os.path.join(casedir,'inputs_case','modeledyears.csv')
).columns.astype(int)
pras_this_solve_year = {
0: False,
1: True if t == max(solveyears) else False,
2: True,
}[int(sw['pras'])]
if pras_this_solve_year or (not int(sw.GSw_PRM_CapCredit)):
result = run_pras(
casedir, t, sw, iteration=iteration,
write_flow=(True if t == max(solveyears) else False),
write_energy=True,
)
if result.returncode:
raise Exception(
f"run_pras.jl returned code {result.returncode}. Check gamslog.txt for error trace."
)
#%% Identify stress periods
print('identifying new stress periods...')
tic = datetime.datetime.now()
if 'user' not in sw['GSw_PRM_StressModel'].lower():
_eue_sorted_periods = stress_periods.main(sw=sw, t=t, iteration=iteration)
functions.toc(tic=tic, year=t, process='ReEDS_Augur/stress_periods.py')
#%% Write gdx file explicitly to ensure that all entries
### (even empty dataframes) are written as parameters, not sets
with gdxpds.gdx.GdxFile() as gdx:
for key in cc_results:
gdx.append(
gdxpds.gdx.GdxSymbol(
key, gdxpds.gdx.GamsDataType.Parameter,
dims=cc_results[key].columns[:-1].tolist(),
)
)
gdx[-1].dataframe = cc_results[key]
gdx.write(
os.path.join('ReEDS_Augur', 'augur_data', f'ReEDS_Augur_{t}.gdx')
)
# #%% Uncomment to run diagnostic_plots
# ### (typically run from call_{}.sh script for parallelization)
# try:
# import ReEDS_Augur.diagnostic_plots as diagnostic_plots
# diagnostic_plots.main(sw)
# except Exception as err:
# print('diagnostic_plots.py failed with the following exception:')
# print(err)
#%% Procedure
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="""Running ReEDS Augur""")
parser.add_argument("tnext", help="Next ReEDS solve year", type=int)
parser.add_argument("t", help="Previous ReEDS solve year", type=int)
parser.add_argument("casedir", help="Path to ReEDS run")
parser.add_argument('--iteration', '-i', default=0, type=int,
help='iteration number on this solve year')
args = parser.parse_args()
tnext = args.tnext
t = args.t
casedir = args.casedir
iteration = args.iteration
#%% Set up logger
log = functions.makelog(
scriptname=f'{__file__} {t}-{tnext}',
logpath=os.path.join(casedir,'gamslog.txt'))
main(t=t, tnext=tnext, casedir=casedir, iteration=iteration)