Newer
Older
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
# | (C) 2006-2019 Potsdam Institute for Climate Impact Research (PIK)
# | authors, and contributors see CITATION.cff file. This file is part
# | of REMIND and licensed under AGPL-3.0-or-later. Under Section 7 of
# | AGPL-3.0, you are granted additional permissions described in the
# | REMIND License Exception, version 1.0 (see LICENSE file).
# | Contact: remind@pik-potsdam.de
#---------------------------------------------------------------------------
#---------------------------- PREPARATION --------------------------
#---------------------------------------------------------------------------
library(tidyverse)
library(remind)
library(gridExtra)
library(quitte)
require(lucode)
require(colorspace)
if(!exists("source_include")) {
#Define arguments that can be read from command line
output_folder <- "dummy"
readArgs("output_folder")
scenario<-"remind17_6013_SSP2-tax20-Noaff-CT-rem-5"
output_folder <- "remind17_6013_SSP2-tax20-Noaff-CT-rem-5"
} else {
output_folder <- outputdir
}
scenario <- getScenNames(outputdir)
#---------------------------------------------------------------------------
#---------------------------- FUNCTIONS ----------------------------
#---------------------------------------------------------------------------
quant_outliers = function(df, threshold){
target_period_items = df %>% filter(iteration == "target") %>%
select(t,pf) %>%
unique()
tmp = left_join(target_period_items,df, by = c("pf","t")) %>%
filter(variable == "quantity", iteration %in% c("target", iter.max),
t <= 2100) %>%
group_by( t, regi, variable, pf ) %>%
filter(abs((value[iteration == "target"] - value[iteration == iter.max])/value[iteration == "target"]) > threshold) %>%
ungroup() %>%
filter(value > eps) %>%
select(regi, pf, t) %>%
unique() %>%
group_by(regi, pf ) %>%
# filter(length(t) > 1) %>%
mutate(period = paste(t, collapse = ", ")) %>%
select(-t) %>%
unique() %>%
ungroup() %>%
arrange(regi, pf, period)
return(tmp)
}
price_outliers <- function(df, threshold){
tmp = df %>%
filter(variable == "price",
iteration %in% c(iter.max),
pf != "inco",
t <= 2100,
value < threshold) %>%
select(regi, pf, t) %>%
group_by(regi, pf ) %>%
filter(length(t) > 1) %>%
mutate(period = paste(t, collapse = ", ")) %>%
select(-t) %>%
unique() %>%
ungroup() %>%
arrange(regi, pf, period)
return(tmp)
}
#---------------------------------------------------------------------------
#---------------------------- READ INPUT DATA ---------------------------
#---------------------------------------------------------------------------
filename<-"CES_calibration.csv"
cat("Reading CES calibration output from ",filename,"\n")
if (file.exists(filename)) {
CES.cal.report <- read.table(filename, header = TRUE, sep = ",", quote = "\"") %>%
as.data.frame()
} else if (file.exists(path(output_folder,filename))) {
CES.cal.report <- read.table(path(output_folder,filename), header = TRUE, sep = ",", quote = "\"") %>%
as.data.frame()
} else {
stop("No CES_calibration.csv file found. CES_calibration.csv is normally produced during calibration runs")
}
#---------------------------------------------------------------------------
#---------------------------- Parameters ----------------------------
#---------------------------------------------------------------------------
in_set = readGDX(path(outputdir,"fulldata.gdx"), "in", "sets")
itr <- getColValues(CES.cal.report,"iteration")
itr_num <- sort(as.double(setdiff(itr, c("origin","target"))))
itr <- c("origin", "target", itr_num)
col <- c("#fc0000", "#000000",
rainbow_hcl(length(itr_num) - 1),
"#bc80bd"#,
#"#808080"
)
names(col) <- c("origin", "target", seq(1,length(itr_num)))
lns <- c(rep("solid", 2), rep("longdash", length(itr_num)))
names(lns) <- c("origin", "target", seq(1,length(itr_num)))
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
.pf <- list("TE" = c("gastr", "refliq", "biotr", "coaltr","hydro", "ngcc","ngt","pc", "apCarDiT","apCarPeT","apCarElT","dot","gaschp","wind","tnrs"))
eps = 1e-2
threshold_quant = 0.15
threshold_price = 0.01
#---------------------------------------------------------------------------
#---------------------------- Process Data ----------------------------
#---------------------------------------------------------------------------
CES.cal.report = CES.cal.report %>%
order.levels(iteration = itr)
CES.cal.report <- unique(CES.cal.report)
CES.cal.report <- CES.cal.report %>% tbl_df()
CES.cal.report$scenario = as.character(CES.cal.report$scenario)
CES.cal.report$scenario = as.factor(CES.cal.report$scenario)
CES.cal.report$t <- as.numeric(as.character(CES.cal.report$t))
CES.cal.report$value <- as.numeric(as.character(CES.cal.report$value))
CES.cal.report = CES.cal.report %>% filter(iteration %in% c("target", "origin", itr_num))
iter.max = max(itr_num)
.pf$structure = sort(intersect(in_set,getColValues(CES.cal.report,"pf")))
#---------------------------------------------------------------------------
#------------------------ PLOTS ----------------------------------
#---------------------------------------------------------------------------
pdf(path(outputdir,paste0("CES calibration report_",scenario,".pdf")),
width = 42 / 2.54, height = 29.7 / 2.54, title = "CES calibration report")
# Include tables with quantities outliers
try(grid.table(quant_outliers(CES.cal.report, threshold_quant), rows = NULL))
grid.text(paste0("Quantities diverge by more than ",threshold_quant *100," %"),rot = 90,x = 0.05, y = 0.5,
gp=gpar(fontsize=20, col="grey38"))
# nclude tables with price outliers
grid.newpage()
try(grid.table(price_outliers(CES.cal.report, threshold_price), rows = NULL))
grid.text(paste0("Prices below ",threshold_price),rot = 90,x = 0.05, y = 0.5,
gp=gpar(fontsize=20, col="grey38"))
for (s in levels(CES.cal.report$scenario)) {
for (r in unique(CES.cal.report[CES.cal.report$scenario == s,][["regi"]])) {
# plot quantities
CES.cal.report %>%
filter(scenario == s,
t <= 2100,
regi == r,
variable == "quantity") %>%
order.levels(pf = getElement(.pf,"structure" )) %>%
ggplot(aes(x = t, y = value, colour = iteration,
linetype = iteration)) +
geom_line() +
facet_wrap(~ pf, scales = "free", as.table = FALSE) +
expand_limits(y = 0) +
scale_colour_manual(values = col) +
scale_linetype_manual(values = lns) +
ggtitle(paste("quantities", r, s)) -> p
plot(p)
# plot prices
CES.cal.report %>%
filter(scenario == s,
t <= 2100,
regi == r,
variable == "price") %>%
order.levels(pf = getElement(.pf,"structure" )) %>%
ggplot(aes(x = t, y = value, colour = iteration,
linetype = iteration)) +
geom_line() +
facet_wrap(~ pf, scales = "free", as.table = FALSE) +
expand_limits(y = 0) +
scale_colour_manual(values = col) +
scale_linetype_manual(values = lns) +
ggtitle(paste("prices", r, s)) -> p
plot(p)
# plot efficiencies
CES.cal.report %>%
filter(scenario == s,
t <= 2100,
regi == r,
variable == "total efficiency",
iteration != "origin") %>%
group_by(scenario,t,regi,pf,variable) %>%
mutate(value = value / value[as.character(iteration) == "1"]) %>%
ungroup() %>%
order.levels(pf = getElement(.pf,"structure" )) %>%
ggplot(aes(x = t, y = value, colour = iteration,
linetype = iteration)) +
geom_line() +
facet_wrap(~ pf, scales = "free", as.table = FALSE) +
scale_colour_manual(values = col) +
scale_linetype_manual(values = lns) +
ggtitle(paste("total efficiency (1 = iteration 1)", r, s)) -> p
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
# plot Putty quantities
if ( dim(CES.cal.report %>% filter(variable == "quantity_putty"))[1] > 0){
CES.cal.report %>%
filter(scenario == s,
t <= 2100,
regi == r,
variable == "quantity_putty") %>%
order.levels(pf = getElement(.pf,"structure" )) %>%
ggplot(aes(x = t, y = value, colour = iteration,
linetype = iteration)) +
geom_line() +
facet_wrap(~ pf, scales = "free", as.table = FALSE) +
expand_limits(y = 0) +
scale_colour_manual(values = col) +
scale_linetype_manual(values = lns) +
ggtitle(paste("Putty quantities", r, s)) -> p
plot(p)
# plot prices putty
CES.cal.report %>%
filter(scenario == s,
t <= 2100,
regi == r,
variable == "price_putty") %>%
order.levels(pf = getElement(.pf,"structure" )) %>%
ggplot(aes(x = t, y = value, colour = iteration,
linetype = iteration)) +
geom_line() +
facet_wrap(~ pf, scales = "free", as.table = FALSE) +
expand_limits(y = 0) +
scale_colour_manual(values = col) +
scale_linetype_manual(values = lns) +
ggtitle(paste("prices", r, s)) -> p
plot(p)
# plot efficiencies
CES.cal.report %>%
filter(scenario == s,
t <= 2100,
regi == r,
variable == "total efficiency putty",
iteration != "origin") %>%
group_by(scenario,t,regi,pf,variable) %>%
mutate(value = value / value[as.character(iteration) == "1"]) %>%
ungroup() %>%
order.levels(pf = getElement(.pf,"structure" )) %>%
ggplot(aes(x = t, y = value, colour = iteration,
linetype = iteration)) +
geom_line() +
facet_wrap(~ pf, scales = "free", as.table = FALSE) +
expand_limits(y = 0) +
scale_colour_manual(values = col) +
scale_linetype_manual(values = lns) +
ggtitle(paste("total efficiency (1 = iteration 1)", r, s)) -> p
plot(p)
}
# plot delta_cap
CES.cal.report %>%
filter(scenario == s,
t <= 2100,
t >= 1980,
regi == r,
variable == "vm_deltaCap",
pf%in% .pf$TE) %>%
order.levels(pf = getElement(.pf, "TE")) %>%
ggplot(aes(x = t, y = value, colour = iteration,
linetype = iteration)) +
geom_line() +
facet_wrap(~ pf, scales = "free", as.table = FALSE) +
expand_limits(y = 0) +
scale_colour_manual(values = col) +
scale_linetype_manual(values = lns) +