Skip to content

Commit

Permalink
remove subtype CapitalUnit from calcCapital
Browse files Browse the repository at this point in the history
  • Loading branch information
fbenke-pik committed Jan 5, 2024
1 parent b1dc5a1 commit 7b36af7
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 181 deletions.
248 changes: 103 additions & 145 deletions R/calcCapital.R
Original file line number Diff line number Diff line change
@@ -1,163 +1,121 @@
calcCapital <- function(subtype = "Capital") {

calcCapital <- function() {
#--- Parameters ---
#***Reproduce this in the aggregation of Capital in convertEDGE
corres_ener_cap = c(kapal = "fealelb",
kapsc = "fescelb",
kaphc = "ueswb")
#---

if (subtype == "Capital") {
# get capital stocks for EDGE sectors
cap_macro <- readSource("EDGE",subtype = subtype)
millionDol2trillionDol <- 1e-6

additional_years <- seq(2105, 2150, 5)
cap_macro <- time_interpolate(cap_macro,
additional_years,
integrate_interpolated_years = TRUE,
extrapolation_type = "constant")

# compute macroeconomic capital stock based on capital intensities from PWT and ssp scenarios
# t.b.d.: correct for capital stock part that enters energy sectors
capital <- readSource("PWT")[, , "rkna"]
getNames(capital) <- "kap"
capital[is.na(capital)] <- 0
gdpppp_hist <- calcOutput("GDPPast", GDPPast = "PWT", aggregate = FALSE)
#pop = calcOutput("Population", aggregate = F)
cap_intensity <- capital / setNames(gdpppp_hist, NULL)


#use initial gdp as in REMIND which differs from PWT
gdpppp_hist = calcOutput("GDPPast", aggregate = FALSE)
gdpppp <- calcOutput("GDP", aggregate = FALSE, years = seq(2005, 2150, 5))
#getNames(gdpppp) <- sub("gdp_","",getNames(gdpppp))
my_scen <- c("gdp_SSP1", "gdp_SSP2", "gdp_SSP3", "gdp_SSP4", "gdp_SSP5", "gdp_SSP2EU",
"gdp_SDP", "gdp_SDP_EI", "gdp_SDP_RC", "gdp_SDP_MC")
gdpppp <- mselect(gdpppp, variable = my_scen)

p41 <- setYears(cap_intensity[,rep(1,32),],seq(1995,2150,5))
p41 <- add_dimension(p41, dim=3.1, add="ssp",nm=my_scen)
cap_intensity_future <- p41
convtime <- p41
gdp_weight <- p41

#ssp variation
convtime[,,"gdp_SSP1"] = 150
convtime[,,"gdp_SSP2"] = 250
convtime[,,"gdp_SSP3"] = 500
convtime[,,"gdp_SSP4"] = 300
convtime[,,"gdp_SSP5"] = 150
convtime[,,"gdp_SDP"] = 150
convtime[,,"gdp_SDP_EI"] = 150
convtime[,,"gdp_SDP_RC"] = 150
convtime[,,"gdp_SDP_MC"] = 150
convtime[,,"gdp_SSP2EU"] = 250

for (t in c("y1995","y2000","y2005")){
cap_intensity_future[,t,] <- cap_intensity[,t,]
gdp_weight[,t,] <- gdpppp_hist[,t,]
}
cap_intensity_ref = cap_intensity["JPN","y2010"]
getRegions(cap_intensity_ref) <- "GLO"
lambda=0
for(t in getYears(gdpppp)) {
cap_intensity_future[,t,] = ((convtime[,t,]-lambda) * collapseNames(setYears(cap_intensity[,"y2010",])) + +
lambda* setNames(setYears(cap_intensity_ref,NULL),NULL))/convtime[,t,]
lambda =lambda+5
gdp_weight[,t,] <- gdpppp[,t,]

# get capital stocks for EDGE sectors
cap_macro <- readSource("EDGE", subtype = "Capital")
millionDol2trillionDol <- 1e-6

additional_years <- seq(2105, 2150, 5)
cap_macro <- time_interpolate(cap_macro,
additional_years,
integrate_interpolated_years = TRUE,
extrapolation_type = "constant")

# compute macroeconomic capital stock based on capital intensities from PWT and ssp scenarios
# t.b.d.: correct for capital stock part that enters energy sectors
capital <- readSource("PWT")[, , "rkna"]
getNames(capital) <- "kap"
capital[is.na(capital)] <- 0
gdpppp_hist <- calcOutput("GDPPast", GDPPast = "PWT", aggregate = FALSE)
cap_intensity <- capital / setNames(gdpppp_hist, NULL)


# use initial gdp as in REMIND which differs from PWT
gdpppp_hist <- calcOutput("GDPPast", aggregate = FALSE)
gdpppp <- calcOutput("GDP", aggregate = FALSE, years = seq(2005, 2150, 5))
my_scen <- c("gdp_SSP1", "gdp_SSP2", "gdp_SSP3", "gdp_SSP4", "gdp_SSP5", "gdp_SSP2EU",
"gdp_SDP", "gdp_SDP_EI", "gdp_SDP_RC", "gdp_SDP_MC")
gdpppp <- mselect(gdpppp, variable = my_scen)

p41 <- setYears(cap_intensity[, rep(1, 32), ], seq(1995, 2150, 5))
p41 <- add_dimension(p41, dim = 3.1, add = "ssp", nm = my_scen)
cap_intensity_future <- p41
convtime <- p41
gdp_weight <- p41

# ssp variation
convtime[, , "gdp_SSP1"] <- 150
convtime[, , "gdp_SSP2"] <- 250
convtime[, , "gdp_SSP3"] <- 500
convtime[, , "gdp_SSP4"] <- 300
convtime[, , "gdp_SSP5"] <- 150
convtime[, , "gdp_SDP"] <- 150
convtime[, , "gdp_SDP_EI"] <- 150
convtime[, , "gdp_SDP_RC"] <- 150
convtime[, , "gdp_SDP_MC"] <- 150
convtime[, , "gdp_SSP2EU"] <- 250

for (t in c("y1995", "y2000", "y2005")) {
cap_intensity_future[, t, ] <- cap_intensity[, t, ]
gdp_weight[, t, ] <- gdpppp_hist[, t, ]
}
cap_intensity_ref <- cap_intensity["JPN", "y2010"]
getRegions(cap_intensity_ref) <- "GLO"
lambda <- 0
for (t in getYears(gdpppp)) {
cap_intensity_future[, t, ] <- ((convtime[, t, ] - lambda) * collapseNames(setYears(cap_intensity[, "y2010", ])) + +
lambda * setNames(setYears(cap_intensity_ref, NULL), NULL)) / convtime[, t, ]
lambda <- lambda + 5
gdp_weight[, t, ] <- gdpppp[, t, ]
}
cap_intensity_future[is.na(cap_intensity_future)] <- 0
cap_int_new <- cap_intensity_future
for (t in getYears(cap_intensity_future)) {
for (r in getRegions(cap_intensity_future)) {
if (cap_intensity_future[r, t, "gdp_SSP2"] == 0) {
# get current mapping
map <- toolGetMapping(type = "regional", name = getConfig("regionmapping"), where = "mappingfolder")
# get list of countries that belong to the same region as r
regi <- map$RegionCode[map$CountryCode == r]
c_regi <- map$CountryCode[map$RegionCode == regi]
# filter out the regions that are 0
c_regi <- c_regi[!cap_intensity_future[c_regi, t, "gdp_SSP2"] == 0]
# generate mapping for the aggregation
mapping <- map[which(map$CountryCode %in% c_regi), ]
mapping$RegionCode <- r
# store calculated data in separate file
cap_int_new[r, t, ] <- toolAggregate(cap_intensity_future[c_regi, t, ], mapping, weight = gdp_weight[c_regi, t, ])
}
cap_intensity_future[is.na(cap_intensity_future)] <- 0
cap_int_new <- cap_intensity_future
for(t in getYears(cap_intensity_future)) {
for(r in getRegions(cap_intensity_future)) {
if(cap_intensity_future[r,t,"gdp_SSP2"]==0) {
# get current mapping
map <- toolGetMapping(type = "regional", name = getConfig("regionmapping"), where = "mappingfolder")
# get list of countries that belong to the same region as r
regi <- map$RegionCode[map$CountryCode==r]
c_regi <- map$CountryCode[map$RegionCode==regi]
# filter out the regions that are 0
c_regi <- c_regi[!cap_intensity_future[c_regi,t,"gdp_SSP2"]==0]
# generate mapping for the aggregation
mapping <- map[which(map$CountryCode %in% c_regi),]
mapping$RegionCode <- r
# store calculated data in separate file
cap_int_new[r,t,] <- toolAggregate(cap_intensity_future[c_regi,t,],mapping,weight=gdp_weight[c_regi,t,])
}
}
}
}

cap_future <- cap_int_new * gdp_weight
y = intersect(getYears(cap_future), getYears(cap_macro))
cap_future <- cap_int_new * gdp_weight
y <- intersect(getYears(cap_future), getYears(cap_macro))


# Add SSP2EU and SDP scenarios
cap_macro_SSP2A <- cap_macro[,,"gdp_SSP2"]
getNames(cap_macro_SSP2A) <- gsub("SSP2", "SSP2EU", getNames(cap_macro_SSP2A))
cap_macro <- mbind(cap_macro, cap_macro_SSP2A)
# Add SSP2EU and SDP scenarios
cap_macro_SSP2A <- cap_macro[, , "gdp_SSP2"]
getNames(cap_macro_SSP2A) <- gsub("SSP2", "SSP2EU", getNames(cap_macro_SSP2A))
cap_macro <- mbind(cap_macro, cap_macro_SSP2A)

cap_macro_SDP <- cap_macro[,,"gdp_SSP1"]
for (i in c("SDP", "SDP_EI", "SDP_RC", "SDP_MC")) {
getNames(cap_macro_SDP) <- gsub("SSP1", i, getNames(cap_macro[,,"gdp_SSP1"]))
cap_macro <- mbind(cap_macro, cap_macro_SDP)
}
cap_macro_SDP <- cap_macro[, , "gdp_SSP1"]
for (i in c("SDP", "SDP_EI", "SDP_RC", "SDP_MC")) {
getNames(cap_macro_SDP) <- gsub("SSP1", i, getNames(cap_macro[, , "gdp_SSP1"]))
cap_macro <- mbind(cap_macro, cap_macro_SDP)
}


cap_macro = mbind(cap_macro[,y,], cap_future[,y,])
cap_macro <- mbind(cap_macro[, y, ], cap_future[, y, ])

cap_macro <- cap_macro * millionDol2trillionDol
cap_macro <- cap_macro * millionDol2trillionDol

# add industry subsectors energy efficiency capital stocks ----
# add industry subsectors energy efficiency capital stocks ----

kap <- cap_macro %>%
`[`(,2015,'gdp_SSP2EU.kap') %>%
quitte::magclass_to_tibble(c('iso3c', NA, NA, NA, 'kap')) %>%
dplyr::select('iso3c', 'kap')
kap <- cap_macro %>%
`[`(, 2015, "gdp_SSP2EU.kap") %>%
quitte::magclass_to_tibble(c("iso3c", NA, NA, NA, "kap")) %>%
dplyr::select("iso3c", "kap")

EEK <- calcOutput('Industry_EEK', kap = kap, supplementary = FALSE,
aggregate = FALSE, years = getYears(cap_macro))
EEK <- calcOutput("Industry_EEK", kap = kap, supplementary = FALSE,
aggregate = FALSE, years = getYears(cap_macro))

# tie outputs together ----
output <- list(
x = mbind(cap_macro, EEK),
weight = NULL,
unit = "trillion 2005US$",
description = "Capital stock at constant 2005 national prices")
}
else if( subtype == "CapitalUnit") {

data = readSource("EDGE", subtype = subtype)

wfe <- calcOutput("FEdemand", subtype = "FE", aggregate = F)[,2015,"gdp_SSP2"]
wfe = collapseNames(wfe)
getSets(wfe) = gsub("item","variable", getSets(wfe))


wfe_kap = do.call(mbind,
lapply(names(corres_ener_cap), function(kap_nm){
ener_nm = corres_ener_cap[kap_nm]
tmp = wfe[,,ener_nm]
getNames(tmp) = gsub(ener_nm,kap_nm,getNames(tmp))
return(tmp)
})
)
wg_prep = mbind(wfe[,,corres_ener_cap],
wfe_kap)

# Apply the structure of data to the weights to avoid issues with multiplication
weights = data
weights[] <- NA
for (item in getNames(wg_prep)){
for (type in getNames(weights[,,item], dim = "type")){
weights[,,item][,,type] <- wg_prep[,,item]
}
}
# tie outputs together ----
output <- list(
x = mbind(cap_macro, EEK),
weight = NULL,
unit = "trillion 2005US$",
description = "Capital stock at constant 2005 national prices")

output = list(x=data,weight=weights,
unit = "kWh for energy, $ for capital",
description = "Technology units with capital and energy properties")
}

return(output)
}
19 changes: 2 additions & 17 deletions R/convertEDGE.R
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ convertEDGE <- function(x, subtype = "FE_stationary") {



} else if (subtype %in% c("Capital")) {
} else if (subtype == "Capital") {

mappingfile <- toolGetMapping(type = "regional", name = "regionmappingEDGE.csv",
returnPathOnly = TRUE, where = "mappingfolder")
Expand Down Expand Up @@ -295,8 +295,7 @@ convertEDGE <- function(x, subtype = "FE_stationary") {
})
)

years_select <- intersect(intersect(getYears(x), getYears(wg)),
getYears(wfe))
years_select <- intersect(intersect(getYears(x), getYears(wg)), getYears(wfe))

wfe <- wfe[, years_select, ]
wg <- wg[, years_select, ]
Expand All @@ -306,20 +305,6 @@ getYears(wfe))
x <- toolAggregate(x[, years_select, ], mappingfile, weight = weights[, , getNames(x)], from = region_col, to = iso_col)
result <- x

} else if (subtype %in% c("CapitalUnit")) {

mappingfile <- toolGetMapping(type = "regional", name = "regionmappingEDGE.csv",
returnPathOnly = TRUE, where = "mappingfolder")
mapping <- read.csv2(mappingfile)
region_col <- which(names(mapping) == "RegionCodeEUR_ETP")
iso_col <- which(names(mapping) == "CountryCode")

wg <- NULL


x <- toolAggregate(x[, , ], mappingfile, weight = wg, from = region_col, to = iso_col)
result <- x

} else if (subtype == "Floorspace") {
mappingfile <- toolGetMapping(type = "regional", name = "regionmappingEDGE.csv",
returnPathOnly = TRUE, where = "mappingfolder")
Expand Down
8 changes: 2 additions & 6 deletions R/fullDECENT.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,10 @@
#'
fullDECENT <- function(rev = 0) {

calcOutput("Labour", years = 2010, round = 8, file = "f_lab.cs3r")
calcOutput("GDP", years = 2010, round = 8, file = "f_gdp.cs3r")
calcOutput("Labour", years = 2010, round = 8, file = "f_lab.cs3r")
calcOutput("GDP", years = 2010, round = 8, file = "f_gdp.cs3r")
calcOutput("RatioPPP2MER", round = 8, file = "pm_shPPPMER.cs4r")
calcOutput("Capital", round = 6, file = "p29_capitalQuantity.cs4r")

# calcOutput("Capital", subtype = "CapitalUnit", round=6, file="f29_capitalUnitProjections.cs4r")
# capital cost per unit of consumed energy and final energy per unit of useful energy (useful energy entspricht z.B. passenger kms)

calcOutput("EmissionsTe", round = 5, file = "f_emissions.cs4r")

}
3 changes: 1 addition & 2 deletions R/fullREMIND.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ fullREMIND <- function() {
calcOutput("TaxLimits", subtype = "propFeSubsidy", round = 2, file = "f21_prop_fe_sub.cs4r")
calcOutput("PETaxes", subtype = "subsidies", round = 2, file = "f21_tau_pe_sub.cs4r")
calcOutput("TaxXport", round = 2, file = "p21_tau_xpres_tax.cs4r") # not default, overwritten with 0
calcOutput("Capital", subtype = "Capital", signif = 4, file = "f29_capitalQuantity.cs4r") # TODO
calcOutput("Capital", subtype = "CapitalUnit", round = 6, file = "f29_capitalUnitProjections.cs4r") # TODO
calcOutput("Capital", signif = 4, file = "f29_capitalQuantity.cs4r") # TODO
calcOutput("FEdemand", subtype = "FE", signif = 4, file = "f_fedemand.cs4r")
calcOutput("ExogDemScen", round = 8, file = "p47_exogDemScen.cs4r") # exogenous demand scenarios activated by cm_exogDem_scen
calcOutput(
Expand Down
12 changes: 1 addition & 11 deletions R/readEDGE.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' @author Antoine Levesque, Robin Hasse
#' @seealso \code{\link{readSource}}
#' @importFrom magclass read.magpie mselect as.magpie mbind add_dimension
readEDGE <- function(subtype = c("FE_stationary", "FE_buildings", "Capital", "CapitalUnit", "Floorspace")) {
readEDGE <- function(subtype = c("FE_stationary", "FE_buildings", "Capital", "Floorspace")) {
subtype <- match.arg(subtype)

# input data version
Expand Down Expand Up @@ -56,16 +56,6 @@ readEDGE <- function(subtype = c("FE_stationary", "FE_buildings", "Capital", "Ca
getItems(data, 3.1) <- sub("gdp_", "", getItems(data, 3.1))
getSets(data) <- c("region", "year", "scenario", "variable")
},
CapitalUnit = {
mcapitalunitCap <- read.csv(file.path(ver, "capitalUnitCost_cap.csv"))
mcapitalunitCap$type <- "cap"
mcapitalunitInv <- read.csv(file.path(ver, "capitalUnitCost_inv.csv"))
mcapitalunitInv$type <- "inv"
data <- rbind(mcapitalunitCap, mcapitalunitInv)
data <- data[c(setdiff(colnames(data), "value"), "value")]
data <- as.magpie(data, tidy = TRUE)
data <- collapseNames(data)
},
Floorspace = {
data <- read.csv(file.path(ver, "EDGE_buildings_floorspace.csv"))
data <- as.magpie(data)
Expand Down

0 comments on commit 7b36af7

Please sign in to comment.