forked from NREL/ReEDS-2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_pras.jl
333 lines (306 loc) · 10.2 KB
/
run_pras.jl
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
#%% Imports
import ArgParse
import DataFrames as DF
import Logging
import LoggingExtras
import Dates
import PRAS
import HDF5
try
using Revise
catch e
@warn "Error initializing Revise" exception=(e, catch_backtrace())
end
#%% Functions
"""
Parse command line arguments for use with ReEDS2PRAS and PRAS
"""
function parse_commandline()
s = ArgParse.ArgParseSettings()
@ArgParse.add_arg_table s begin
"--reeds_path"
help = "Path to ReEDS-2.0 folder"
arg_type = String
required = true
"--reedscase"
help = "Path to ReEDS run (usually .../ReEDS-2.0/runs/{casename})"
arg_type = String
required = true
"--solve_year"
help = "ReEDS solve year (usually in [2020..2050])"
arg_type = Int
required = true
"--weather_year"
help = "The weather year to start from, in [2007..2013]"
arg_type = Int
default = 2007
required = true
"--reeds2praspath"
help = "Path to ReEDS2PRAS folder"
arg_type = String
required = true
"--samples"
help = "Number of Monte Carlo samples to run in PRAS"
arg_type = Int
default = 10
required = false
"--timesteps"
help = "Number of hourly timesteps to use"
arg_type = Int
default = 61320
required = false
"--write_flow"
help = "Write the hourly interface flows"
arg_type = Int
default = 0
required = false
"--write_surplus"
help = "Write the hourly surplus"
arg_type = Int
default = 0
required = false
"--write_energy"
help = "Write the hourly storage energy"
arg_type = Int
default = 0
required = false
"--iteration"
help = "Solve-year iteration number (only used in file label)"
arg_type = Int
default = 0
required = false
"--overwrite"
help = "Overwrite an existing .pras file"
arg_type = Int
default = 1
required = false
"--include_samples"
help = "Include the number of samples in the output .csv filename"
arg_type = Int
default = 0
required = false
"--debug"
help = "Log debug-level messages"
arg_type = Int
default = 0
required = false
end
return ArgParse.parse_args(s)
end
"""
Set up logging to file and console
"""
function setup_logger(pras_system_path::String, args::Dict)
if ~isnothing(pras_system_path)
logfile = replace(pras_system_path, ".pras"=>".log")
if args["debug"] == 1
logfilehandle = LoggingExtras.MinLevelLogger(
LoggingExtras.FileLogger(logfile; append=true),
Logging.Debug)
else
logfilehandle = LoggingExtras.MinLevelLogger(
LoggingExtras.FileLogger(logfile; append=true),
Logging.Info)
end
logger = LoggingExtras.TeeLogger(
Logging.global_logger(),
logfilehandle
)
### https://github.com/JuliaLogging/LoggingExtras.jl#add-timestamp-to-all-logging
timestamp_logger(logger) = LoggingExtras.TransformerLogger(logger) do log
merge(
log,
(; message = "$(Dates.format(Dates.now(), "yyyy-mm-dd HH:MM:SS")) | $(log.message)")
)
end
Logging.global_logger(timestamp_logger(logger))
end
end
"""
Simple PRAS analysis.
Parameters
----------
Returns
-------
"""
function run_pras(pras_system_path::String, args::Dict)
#%% Load the system model
sys = PRAS.SystemModel(pras_system_path)
#%% Run PRAS
short, flow, surplus, energy = PRAS.assess(
sys,
PRAS.SequentialMonteCarlo(
samples=args["samples"], threaded=true, verbose=true, seed=1),
PRAS.Shortfall(),
PRAS.Flow(),
PRAS.Surplus(),
PRAS.StorageEnergy()
)
#%% Print some results for the entire modeled region to show it worked
@info "$(PRAS.LOLE(short)) event-h"
@info "$(PRAS.EUE(short)) MWh"
@info "NEUE = $(1e6 * PRAS.EUE(short).eue.estimate / sum(sys.regions.load)) ppm"
#%% Print some more detailed results if debugging
for (i, reg) in enumerate(sys.regions.names)
@debug "$reg: $(round(PRAS.LOLE(short,reg).lole.estimate)) event-h"
@debug "$reg: $(round(PRAS.EUE(short,reg).eue.estimate)) MWh"
@debug "$reg: NEUE = $(round(
1e6 * PRAS.EUE(short,reg).eue.estimate
/ sum(sys.regions.load[i,:])
)) ppm\n\n"
end
#%% Record the EUE and LOLE outputs by region and timestep
## Units are:
## * LOLE: event-h
## * EUE: MWh
## First for the whole modeled area (labeled as "USA" but if modeling a smaller
## region (speicified by GSw_Region) it will be for that modeled region)
dfout = DF.DataFrame(
USA_LOLE=[PRAS.LOLE(short,h).lole.estimate for h in sys.timestamps],
USA_EUE=[PRAS.EUE(short,h).eue.estimate for h in sys.timestamps],
)
## Now for each constituent region
for (i,r) in enumerate(sys.regions.names)
dfout[!, "$(r)_LOLE"] = [PRAS.LOLE(short,r,h).lole.estimate for h in sys.timestamps]
dfout[!, "$(r)_EUE"] = [PRAS.EUE(short,r,h).eue.estimate for h in sys.timestamps]
end
#%% Write it
if args["include_samples"] == 1
outfile = replace(pras_system_path, ".pras"=>"-$(args["samples"]).h5")
else
outfile = replace(pras_system_path, ".pras"=>".h5")
end
HDF5.h5open(outfile, "w") do f
for column in DF._names(dfout)
f["$column", compress=4] = convert(Array, dfout[!, column])
end
end
@info("Wrote PRAS EUE and LOLE to $(outfile)")
#%%### Record more operational details if desired
### Flow
if args["write_flow"] == 1
dfflow = DF.DataFrame()
for i in flow.interfaces
## Flow results are tuples of (mean, standard deviation). Keep the mean.
dfflow[!, "$(i)"] = [flow[i,h][1] for h in sys.timestamps]
end
## Write it
flowfile = replace(outfile, ".h5"=>"-flow.h5")
HDF5.h5open(flowfile, "w") do f
for column in DF._names(dfflow)
f["$column", compress=4] = convert(Array, dfflow[!, column])
end
end
@info("Wrote PRAS flow to $(flowfile)")
end
### Surplus
if args["write_surplus"] == 1
dfsurplus = DF.DataFrame()
for r in sys.regions.names
## Surplus results are tuples of (mean, standard deviation). Keep the mean.
dfsurplus[!, "$(r)"] = [surplus[r,h][1] for h in sys.timestamps]
end
## Write it
surplusfile = replace(outfile, ".h5"=>"-surplus.h5")
HDF5.h5open(surplusfile, "w") do f
for column in DF._names(dfsurplus)
f["$column", compress=4] = convert(Array, dfsurplus[!, column])
end
end
@info("Wrote PRAS surplus to $(surplusfile)")
end
### Storage energy
if args["write_energy"] == 1
dfenergy = DF.DataFrame()
for i in sys.storages.names
## Energy results are tuples of (mean, standard deviation). Keep the mean.
dfenergy[!, strip("$(i)", '_')] = [energy[i,h][1] for h in sys.timestamps]
end
## Write it
energyfile = replace(outfile, ".h5"=>"-energy.h5")
HDF5.h5open(energyfile, "w") do f
for column in DF._names(dfenergy)
f["$column", compress=4] = convert(Array, dfenergy[!, column])
end
end
@info("Wrote PRAS storage energy to $(energyfile)")
end
#%%
return dfout
end
#%% Main function
"""
Run ReEDS2PRAS and PRAS
"""
function main(args::Dict)
#%% Define some intermediate filenames
if args["timesteps"] == 8760
pras_system_path = joinpath(
args["reedscase"],"ReEDS_Augur","PRAS",
"PRAS_m$(args["solve_year"])i$(args["iteration"])_w$(args["weather_year"]).pras"
)
else
pras_system_path = joinpath(
args["reedscase"],"ReEDS_Augur","PRAS",
"PRAS_$(args["solve_year"])i$(args["iteration"]).pras"
)
end
#%% Set up the logger
setup_logger(pras_system_path, args)
@info "Running ReEDS2PRAS with the following inputs:"
for (arg, val) in args
@info "$arg => $val"
end
#%% Run ReEDS2PRAS
if (args["overwrite"] == 1) | ~isfile(pras_system_path)
pras_system = ReEDS2PRAS.reeds_to_pras(
args["reedscase"],
args["solve_year"],
args["timesteps"],
args["weather_year"],
)
### Save the PRAS system
## Could use compression_level={integer} here but it doesn't really help
@info "Saving PRAS model to to $(pras_system_path)"
PRAS.savemodel(pras_system, pras_system_path, verbose=true)
@info "Finished ReEDS2PRAS"
end
#%% Run PRAS
if args["samples"] > 0
@info "Running PRAS"
dfout = run_pras(pras_system_path, args)
@info "Finished PRAS"
#%%
return dfout
end
end
#%% Procedure
if abspath(PROGRAM_FILE) == @__FILE__
# #%% Inputs for debugging
# args = Dict(
# "reeds_path" => "/Users/pbrown/github/ReEDS-2.0",
# "reedscase" => (
# "/Users/pbrown/github/ReEDS-2.0/runs/"
# *"v20240118_stressM0_Z45_SP_5yr_H2_WECC"),
# "solve_year" => 2050,
# "weather_year" => 2007,
# "reeds2praspath" => "/Users/pbrown/github/ReEDS2PRAS",
# "samples" => 10,
# "iteration" => 0,
# # "timesteps" => 8760,
# "timesteps" => 61320,
# "write_flow" => 1,
# "write_surplus" => 1,
# "write_energy" => 1,
# "overwrite" => 1,
# "debug" => 0,
# "include_samples" => 0,
# )
#%% Parse the command line arguments
args = parse_commandline()
#%% Include ReEDS2PRAS
include(joinpath(args["reeds2praspath"], "src", "ReEDS2PRAS.jl"))
#%% Run it
main(args)
#%%
end