Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor calcFEdemand #458

Merged
merged 26 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
058ce26
remove no longer needed subtypes in calcFEdemand
fbenke-pik Dec 20, 2023
3e27b59
move FE demand building subtypes to a separate calc function
fbenke-pik Dec 21, 2023
4a31751
add tool function
fbenke-pik Jan 4, 2024
b1dc5a1
remove subtype ES from calcFEdemand
fbenke-pik Jan 5, 2024
7b36af7
remove subtype CapitalUnit from calcCapital
fbenke-pik Jan 5, 2024
018c045
remove EDGE data from calcCapital
fbenke-pik Jan 5, 2024
3cffb1a
increment to dev version
fbenke-pik Jan 5, 2024
f3fbdc6
Split readEDGE into readEdgeBuildings and readStationary
fbenke-pik Jan 5, 2024
f642046
add calcFeDemandIndustry
fbenke-pik Jan 8, 2024
9435aca
remove subtypes FE_for_Eff and UE_for_Eff from calcFEdemand
fbenke-pik Jan 8, 2024
25091fa
create calcFeDemandTransport
fbenke-pik Jan 9, 2024
0eea9c1
enhance calcFeDemandTransport
fbenke-pik Jan 9, 2024
2cdd49e
spread mapping accross calc functions
fbenke-pik Jan 11, 2024
4ac3c28
move industry calculations
fbenke-pik Jan 12, 2024
ff7fa12
Merge branch 'master' of https://github.com/pik-piam/mrremind into fe…
fbenke-pik Jan 13, 2024
6231dac
increment version
fbenke-pik Jan 15, 2024
3a0941a
remove no longer used subtype in calls of calcFEdemand
fbenke-pik Jan 16, 2024
54cf992
add comments
fbenke-pik Jan 22, 2024
151fdd5
Merge branch 'master' of https://github.com/pik-piam/mrremind into fe…
fbenke-pik Jan 23, 2024
5065369
refactor EDGE Transport function calls in fullREMIND
fbenke-pik Jan 23, 2024
8b4fbcd
increment version
fbenke-pik Jan 23, 2024
5cd422e
Merge branch 'master' into feDemand
fbenke-pik Jan 26, 2024
c0759bb
include NAV and CAMP scenarios in adjustments in convertStationary
fbenke-pik Jan 26, 2024
4a6a898
increment version
fbenke-pik Jan 29, 2024
51f1b41
Merge branch 'master' of https://github.com/pik-piam/mrremind into fe…
fbenke-pik Feb 6, 2024
91da4d5
increment version
fbenke-pik Feb 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '34630515'
ValidationKey: '34657286'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand Down
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ cff-version: 1.2.0
message: If you use this software, please cite it using the metadata from this file.
type: software
title: 'mrremind: MadRat REMIND Input Data Package'
version: 0.175.3
date-released: '2024-02-02'
version: 0.175.4
date-released: '2024-02-06'
abstract: The mrremind packages contains data preprocessing for the REMIND model.
authors:
- family-names: Baumstark
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Type: Package
Package: mrremind
Title: MadRat REMIND Input Data Package
Version: 0.175.3
Date: 2024-02-02
Version: 0.175.4
Date: 2024-02-06
Authors@R: c(
person("Lavinia", "Baumstark", , "[email protected]", role = c("aut", "cre")),
person("Renato", "Rodrigues", role = "aut"),
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export(readUSGS)
export(readindustry_subsectors_specific)
export(readvanRuijven2016)
export(readworldsteel)
export(toolAggregateTimeSteps)
export(toolBiomassSupplyAggregate)
export(toolCubicFunctionAggregate)
export(toolCubicFunctionDisaggregate)
Expand Down Expand Up @@ -229,7 +230,6 @@ importFrom(quitte,character.data.frame)
importFrom(quitte,df_populate_range)
importFrom(quitte,duplicate)
importFrom(quitte,duplicate_)
importFrom(quitte,getVars)
importFrom(quitte,inline.data.frame)
importFrom(quitte,interpolate_missing_periods)
importFrom(quitte,interpolate_missing_periods_)
Expand Down
220 changes: 75 additions & 145 deletions R/calcCapital.R
Original file line number Diff line number Diff line change
@@ -1,163 +1,93 @@
calcCapital <- function(subtype = "Capital") {

#--- 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,]
}
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) {
calcCapital <- function() {

# 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]
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]
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 <- 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_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))


# 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 = mbind(cap_macro[,y,], cap_future[,y,])

cap_macro <- cap_macro * millionDol2trillionDol

# 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')

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)
cap_future <- cap_int_new * gdp_weight * 1e-6

wfe <- calcOutput("FEdemand", subtype = "FE", aggregate = F)[,2015,"gdp_SSP2"]
wfe = collapseNames(wfe)
getSets(wfe) = gsub("item","variable", getSets(wfe))
# add industry subsectors energy efficiency capital stocks ----

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

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)
EEK <- calcOutput("Industry_EEK", kap = kap, supplementary = FALSE,
aggregate = FALSE, years = getYears(cap_future))

# 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]
}
}

output = list(x=data,weight=weights,
unit = "kWh for energy, $ for capital",
description = "Technology units with capital and energy properties")
}
# tie outputs together ----
output <- list(
x = mbind(cap_future, EEK),
weight = NULL,
unit = "trillion 2005US$",
description = "Capital stock at constant 2005 national prices"
)

return(output)
}
44 changes: 0 additions & 44 deletions R/calcEnergyEffPaths.R

This file was deleted.

Loading
Loading