diff --git a/.buildlibrary b/.buildlibrary index 96cf19df..b3baa4d2 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '217223782' +ValidationKey: '217533133' AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' - 'Warning: namespace ''.*'' is not available and has been replaced' diff --git a/CITATION.cff b/CITATION.cff index fb74452f..4a8f01e9 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -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: 'remind2: The REMIND R package (2nd generation)' -version: 1.112.2 -date-released: '2023-06-23' +version: 1.113.1 +date-released: '2023-07-05' abstract: Contains the REMIND-specific routines for data and model output manipulation. authors: - family-names: Rodrigues diff --git a/DESCRIPTION b/DESCRIPTION index e5529d65..24374810 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Type: Package Package: remind2 Title: The REMIND R package (2nd generation) -Version: 1.112.2 -Date: 2023-06-23 +Version: 1.113.1 +Date: 2023-07-05 Authors@R: c( person("Renato", "Rodrigues", , "renato.rodrigues@pik-potsdam.de", role = c("aut", "cre")), person("Lavinia", "Baumstark", role = "aut"), diff --git a/R/reportEmi.R b/R/reportEmi.R index eac2bf33..1858b1c7 100644 --- a/R/reportEmi.R +++ b/R/reportEmi.R @@ -19,9 +19,10 @@ #' @export #' #' @importFrom gdx readGDX -#' @importFrom dplyr %>% +#' @importFrom dplyr %>% filter full_join group_by inner_join left_join mutate rename select summarise #' @importFrom magclass mselect dimSums mselect<- collapseDim getItems getRegions getYears #' @importFrom madrat toolAggregate +#' @importFrom tibble as_tibble reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(2005, 2060, 5), seq(2070, 2110, 10), 2130, 2150)) { @@ -92,6 +93,17 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 } se_sol <- c("sesofos", "sesobio") + # SE carriers by origin + if (is.null(entySEfos <- readGDX(gdx, 'entySEfos', react = 'silent'))) + entySEfos <- c('sesofos', 'seliqfos', 'segafos') + + if (is.null(entySEbio <- readGDX(gdx, 'entySEbio', react = 'silent'))) + entySEbio <- c('sesobio', 'seliqbio', 'segabio') + + if ( is.null(entySEsyn <- readGDX(gdx, 'entySEsyn', react = 'silent')) + || (length(entySEbio) == length(entySEsyn) && all(entySEbio == entySEsyn))) + entySEsyn <- c('seliqsyn', 'segasyn') + ### emissions variables from REMIND (see definitions in core/equations.gms) # total GHG emissions @@ -131,21 +143,6 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 if (!is.null(o37_demFeIndSub)) { o37_demFeIndSub[is.na(o37_demFeIndSub)] <- 0 } -# CO2 captured per industry subsector - # note: this has to be read in with restore_zeros=T because sometimes it contains only non-zero values for "ETS" emiMkt - # and magclass will wrongly interpret this as a region - pm_IndstCO2Captured <- readGDX(gdx, "pm_IndstCO2Captured", restore_zeros = T, - react = 'silent')[,t,] - # if all zero, set to NULL - if (length(pm_IndstCO2Captured) == 0) { - pm_IndstCO2Captured <- NULL - } - # subset parameter to decrease size as restore_zeros=F was not possible - if (!is.null(pm_IndstCO2Captured)) { - pm_IndstCO2Captured <- pm_IndstCO2Captured[se2fe[,c(1,2)]] - } - - # secondary energy production vm_prodSe <- readGDX(gdx, "vm_prodSe", field = "l", restore_zeros = F) @@ -178,8 +175,108 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 # helper variable to release captured CO2 in no CCU capacities are standing anymore to take the captured CO2 v_co2capturevalve <- readGDX(gdx, "v_co2capturevalve", field = "l", restore_zeros = F)[, t, ] + # CO2 captured per industry subsector + # NOTE: The parameter pm_IndstCO2Captured was calculated without taking into + # account the different emission factors of energy carriers, so we recalculate + # it here. Might load it again when we are reasonably sure we won't process + # any gdxes with the faulty parameter any more. + vm_emiIndCCS_tibble <- vm_emiIndCCS[,,emiInd37_fuel] %>% + as_tibble() %>% + select(t = 'ttot', regi = 'all_regi', emiInd37 = 'secInd37', + vm_emiIndCCS = 'value') %>% + left_join(secInd37_2_emiInd37, 'emiInd37') %>% + select(-'emiInd37') + + o37_demFeIndSub_tibble <- o37_demFeIndSub %>% + as_tibble() %>% + select(t = 'ttot', regi = 'all_regi', sety = 'all_enty', + fety = 'all_enty1', 'secInd37', 'emiMkt' = 'all_emiMkt', + o37_demFEindsub = 'value') + + pm_emifac_tibble <- pm_emifac %>% + mselect(mutate(se2fe, all_enty2 = 'co2')) %>% + as_tibble() %>% + select(t = 'tall', regi = 'all_regi', fety = 'all_enty1', + pm_emifac = 'value') %>% + # extend fossil emission factors to biomass and synfuels + left_join( + se2fe %>% + distinct(sety = .data$all_enty, fety = .data$all_enty1), + + by = 'fety', + + relationship = 'many-to-many' + ) + subsector_total_emissions <- inner_join( + o37_demFeIndSub_tibble, + pm_emifac_tibble, + + c('t', 'regi', 'sety', 'fety') + ) %>% + group_by(.data$t, .data$regi, .data$secInd37) %>% + summarise( + subsector_total_emissions = sum(.data$o37_demFEindsub * .data$pm_emifac), + .groups = 'drop') + + subsector_emissions <- inner_join( + o37_demFeIndSub_tibble, + pm_emifac_tibble, + + c('t', 'regi', 'sety', 'fety') + ) %>% + group_by(.data$t, .data$regi, .data$secInd37, .data$sety, .data$fety, + .data$emiMkt) %>% + summarise( + subsector_emissions = sum(.data$o37_demFEindsub * .data$pm_emifac), + .groups = 'drop') + + pm_IndstCO2Captured <- subsector_emissions %>% + full_join( + inner_join( + vm_emiIndCCS_tibble, + + subsector_total_emissions, + + c('t', 'regi', 'secInd37') + ) %>% + mutate(subsector_capture_share = .data$vm_emiIndCCS + / .data$subsector_total_emissions) %>% + select(-'vm_emiIndCCS', -'subsector_total_emissions'), + + c('t', 'regi', 'secInd37') + ) %>% + mutate( + value = .data$subsector_emissions * .data$subsector_capture_share) %>% + select(-'subsector_emissions', -'subsector_capture_share') %>% + select(ttot = 't', all_regi = 'regi', all_enty = 'sety', all_enty1 = 'fety', + 'secInd37', all_emiMkt = 'emiMkt', 'value') %>% + as.magpie(spatial = 2, temporal = 1, data = ncol(.)) %>% + ifelse(is.finite(.), ., 0) # replace NaN by 0 + + rm(vm_emiIndCCS_tibble, subsector_emissions, subsector_total_emissions, + pm_emifac_tibble) + + # utility functions ---- + # Convert a mixer table into a list that can be passed to mselect() to + # select specified dimensions from a magpie object + .mixer_to_selector <- function(mixer) { + selector <- list() + for (i in seq_len(nrow(mixer))) { + selector <- c( + selector, + + list(mixer[i,] %>% + as.list() %>% + # exclude list entries that are NULL + Filter(function(x) { !is.null(x[[1]]) }, x = .) %>% + # coerce character vector elements one level up + lapply(unlist)) + ) + } + return(selector) + } # Calculate Variables ---- @@ -454,18 +551,14 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 if (!is.null(o37_demFeIndSub)) { # relabel industry energy CC from CCS sectors to industry sectors - emiInd37 <- NULL - vm_emiIndCCS_Mapped <- toolAggregate(vm_emiIndCCS[, , emiInd37_fuel], rel = secInd37_2_emiInd37 %>% filter(emiInd37 %in% emiInd37_fuel), - from = "emiInd37", to = "secInd37", dim = 3) - - - # calculate captured CO2 per subsector and FE carrier by multiplying subsectoral share of fesos, fehos, fegas in total FE from fesos, fehos and fegas with the captured CO2 by subsector - vm_emiIndCCS_Sub <- vm_emiIndCCS_Mapped * dimSums(mselect(o37_demFeIndSub, all_enty1 = c("fesos", "fehos", "fegas")), dim = c(3.1, 3.4)) / dimSums(mselect(o37_demFeIndSub, all_enty1 = c("fesos", "fehos", "fegas")), dim = c(3.1, 3.2, 3.4)) - vm_emiIndCCS_Sub[is.na(vm_emiIndCCS_Sub)] <- 0 - getSets(vm_emiIndCCS_Sub)[c(3, 4)] <- c("secInd37", "all_enty1") - - + vm_emiIndCCS_Mapped <- toolAggregate( + vm_emiIndCCS[, , emiInd37_fuel], + rel = secInd37_2_emiInd37 %>% filter(.data$emiInd37 %in% emiInd37_fuel), + from = "emiInd37", to = "secInd37", dim = 3) + # calculate captured CO2 per subsector and FE carrier + vm_emiIndCCS_Sub <- dimSums(pm_IndstCO2Captured, + dim = c('all_enty', 'all_emiMkt')) # calculate industry emissions by subsector (before industry CO2 Capture) sel_pm_emifac_co2 <- if(getSets(pm_emifac)[[6]] == "emiAll"){ @@ -504,21 +597,6 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 out <- mbind(out, - # industry solids emissions: direct solids emissions - industry CCS of solids - setNames((dimSums(mselect(EmiFeCarrier, all_enty1 = "fesos", emi_sectors = "indst"), dim = 3) - - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fesos")*p_share_CCS, dim = 3)) * GtC_2_MtCO2, - "Emi|CO2|Energy|Demand|Industry|+|Solids (Mt CO2/yr)"), - - # industry liquids emissions: direct liquids emissions - industry CCS of liquids - setNames((dimSums(mselect(EmiFeCarrier, all_enty1 = c("fehos"), emi_sectors = "indst"), dim = 3) - - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fehos")*p_share_CCS, dim = 3)) * GtC_2_MtCO2, - "Emi|CO2|Energy|Demand|Industry|+|Liquids (Mt CO2/yr)"), - - # industry gases emissions: direct gases emissions - industry CCS of gases - setNames((dimSums(mselect(EmiFeCarrier, all_enty1 = c("fegas"), emi_sectors = "indst"), dim = 3) - - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fegas")*p_share_CCS, dim = 3)) * GtC_2_MtCO2, - "Emi|CO2|Energy|Demand|Industry|+|Gases (Mt CO2/yr)"), - # buildings setNames((dimSums(mselect(EmiFeCarrier, all_enty1 = "fesos", emi_sectors = "build"), dim = 3)) * GtC_2_MtCO2, "Emi|CO2|Energy|Demand|Buildings|+|Solids (Mt CO2/yr)"), @@ -536,19 +614,56 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 ) + ##### industry ---- + variable_prefix <- 'Emi|CO2|Energy|Demand|Industry|' + variable_postfix <- ' (Mt CO2/yr)' + + mixer <- tribble( + ~variable, ~secInd37, ~all_enty1, + '+|Solids', NULL, 'fesos', + '+|Liquids', NULL, 'fehos', + '+|Gases', NULL, 'fegas', + + '++|Cement', 'cement', NULL, + 'Cement|+|Solids', 'cement', 'fesos', + 'Cement|+|Liquids', 'cement', 'fehos', + 'Cement|+|Gases', 'cement', 'fegas', + + '++|Chemicals', 'chemicals', NULL, + 'Chemicals|+|Solids', 'chemicals', 'fesos', + 'Chemicals|+|Liquids', 'chemicals', 'fehos', + 'Chemicals|+|Gases', 'chemicals', 'fegas', + + '++|Steel', 'steel', NULL, + 'Steel|+|Solids', 'steel', 'fesos', + 'Steel|+|Liquids', 'steel', 'fehos', + 'Steel|+|Gases', 'steel', 'fegas', + + '++|Other Industry', 'otherInd', NULL, + 'Other Industry|+|Solids', 'otherInd', 'fesos', + 'Other Industry|+|Liquids', 'otherInd', 'fehos', + 'Other Industry|+|Gases', 'otherInd', 'fegas') %>% + mutate( + variable = paste0(variable_prefix, .data$variable, variable_postfix)) - # reporting of emissions for industry sectors (steel, cement, etc.) - - out <- mbind(out, - setNames((dimSums(mselect(EmiIndSubSec, secInd37 = "steel"), dim = 3) - dimSums(mselect(vm_emiIndCCS_Sub, secInd37 = "steel")*p_share_CCS, dim = 3)) * GtC_2_MtCO2, - "Emi|CO2|Energy|Demand|Industry|++|Steel (Mt CO2/yr)"), - setNames((dimSums(mselect(EmiIndSubSec, secInd37 = "cement"), dim = 3) - dimSums(mselect(vm_emiIndCCS_Sub, secInd37 = "cement")*p_share_CCS, dim = 3)) * GtC_2_MtCO2, - "Emi|CO2|Energy|Demand|Industry|++|Cement (Mt CO2/yr)"), - setNames((dimSums(mselect(EmiIndSubSec, secInd37 = "chemicals"), dim = 3) - dimSums(mselect(vm_emiIndCCS_Sub, secInd37 = "chemicals")*p_share_CCS, dim = 3)) * GtC_2_MtCO2, - "Emi|CO2|Energy|Demand|Industry|++|Chemicals (Mt CO2/yr)"), - setNames((dimSums(mselect(EmiIndSubSec, secInd37 = "otherInd"), dim = 3) - dimSums(mselect(vm_emiIndCCS_Sub, secInd37 = "otherInd")*p_share_CCS, dim = 3)) * GtC_2_MtCO2, - "Emi|CO2|Energy|Demand|Industry|++|Other Industry (Mt CO2/yr)")) + out <- mbind( + out, + lapply(.mixer_to_selector(mixer), function(x) { + setNames( + # extract relevant portions from EmiIndSubSec and vm_emiIndCCS_Sub, + # call mselect(), but without the 'variable' column + ( dimSums(mselect(EmiIndSubSec, x[setdiff(names(x), 'variable')]), + dim = 3) + - dimSums( + ( mselect(vm_emiIndCCS_Sub, x[setdiff(names(x), 'variable')]) + * p_share_CCS + ), + dim = 3) + ) * GtC_2_MtCO2, + x[['variable']]) + }) %>% + mbind()) } else { # if o37_demFeIndSub not existing in GDX, calculate reporting parameter here, note: works for industry fixed_shares only @@ -671,7 +786,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 # intra-region bunker emissions intraRegionFactor <- new.magpie(getRegions(bunkersEmi), getYears(bunkersEmi), fill = 0) # is equal to 0 for non EU countries # is equal to 35% of total bunkers in average from 2000-2020 for EU27 + UKI countries - if("EUR" %in% getRegions(bunkersEmi)){ + if("EUR" %in% getRegions(bunkersEmi)){ intraRegionFactor["EUR",,] <- 0.35 } else if (!is.null(regionSubsetList$EUR)) { intraRegionFactor[regionSubsetList$EUR,,] <- 0.35 @@ -939,6 +1054,8 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 # total co2 captured in industry from proess emissions (only cemnent process CO2 for now) setNames(dimSums(vm_emiIndCCS[, , "co2cement_process"], dim = 3) * GtC_2_MtCO2, "Carbon Management|Carbon Capture|+|Industry Process (Mt CO2/yr)"), + setNames(dimSums(vm_emiIndCCS[, , "co2cement_process"], dim = 3) * GtC_2_MtCO2, + "Carbon Management|Carbon Capture|Industry Process|+|Cement (Mt CO2/yr)"), # total co2 captured by DAC setNames(-v33_emiDAC * GtC_2_MtCO2, "Carbon Management|Carbon Capture|+|DAC (Mt CO2/yr)"), @@ -949,118 +1066,80 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 ### report industry captured CO2 ---- if (!is.null(pm_IndstCO2Captured)) { + variable_prefix <- 'Carbon Management|Carbon Capture|Industry Energy|' + variable_postfix <- ' (Mt CO2/yr)' + + mixer <- tribble( + ~variable, ~secInd37, ~all_enty1, ~all_enty, + '+|Fossil', NULL, NULL, entySEfos, + '+|Biomass', NULL, NULL, entySEbio, + '+|Synfuel', NULL, NULL, entySEsyn, + + 'Cement', 'cement', NULL, NULL, + 'Cement|++|Solids', 'cement', 'fesos', NULL, + 'Cement|Solids|+|Fossil', 'cement', 'fesos', entySEfos, + 'Cement|Solids|+|Biomass', 'cement', 'fesos', entySEbio, + 'Cement|++|Liquids', 'cement', 'fehos', NULL, + 'Cement|Liquids|+|Fossil', 'cement', 'fehos', entySEfos, + 'Cement|Liquids|+|Biomass', 'cement', 'fehos', entySEbio, + 'Cement|Liquids|+|Synfuel', 'cement', 'fehos', entySEsyn, + 'Cement|++|Gases', 'cement', 'fegas', NULL, + 'Cement|Gases|+|Fossil', 'cement', 'fegas', entySEfos, + 'Cement|Gases|+|Biomass', 'cement', 'fegas', entySEbio, + 'Cement|Gases|+|Synfuel', 'cement', 'fegas', entySEsyn, + 'Cement|+|Fossil', 'cement', NULL, entySEfos, + 'Cement|+|Biomass', 'cement', NULL, entySEbio, + 'Cement|+|Synfuel', 'cement', NULL, entySEsyn, + + 'Chemicals', 'chemicals', NULL, NULL, + 'Chemicals|++|Solids', 'chemicals', 'fesos', NULL, + 'Chemicals|Solids|+|Fossil', 'chemicals', 'fesos', entySEfos, + 'Chemicals|Solids|+|Biomass', 'chemicals', 'fesos', entySEbio, + 'Chemicals|++|Liquids', 'chemicals', 'fehos', NULL, + 'Chemicals|Liquids|+|Fossil', 'chemicals', 'fehos', entySEfos, + 'Chemicals|Liquids|+|Biomass', 'chemicals', 'fehos', entySEbio, + 'Chemicals|Liquids|+|Synfuel', 'chemicals', 'fehos', entySEsyn, + 'Chemicals|++|Gases', 'chemicals', 'fegas', NULL, + 'Chemicals|Gases|+|Fossil', 'chemicals', 'fegas', entySEfos, + 'Chemicals|Gases|+|Biomass', 'chemicals', 'fegas', entySEbio, + 'Chemicals|Gases|+|Synfuel', 'chemicals', 'fegas', entySEsyn, + 'Chemicals|+|Fossil', 'chemicals', NULL, entySEfos, + 'Chemicals|+|Biomass', 'chemicals', NULL, entySEbio, + 'Chemicals|+|Synfuel', 'chemicals', NULL, entySEsyn, + + 'Steel', 'steel', NULL, NULL, + 'Steel|++|Solids', 'steel', 'fesos', NULL, + 'Steel|Solids|+|Fossil', 'steel', 'fesos', entySEfos, + 'Steel|Solids|+|Biomass', 'steel', 'fesos', entySEbio, + 'Steel|++|Liquids', 'steel', 'fehos', NULL, + 'Steel|Liquids|+|Fossil', 'steel', 'fehos', 'seliqfos', + 'Steel|Liquids|+|Biomass', 'steel', 'fehos', entySEbio, + 'Steel|Liquids|+|Synfuel', 'steel', 'fehos', entySEsyn, + 'Steel|++|Gases', 'steel', 'fegas', NULL, + 'Steel|Gases|+|Fossil', 'steel', 'fegas', entySEfos, + 'Steel|Gases|+|Biomass', 'steel', 'fegas', entySEbio, + 'Steel|Gases|+|Synfuel', 'steel', 'fegas', entySEsyn, + 'Steel|+|Fossil', 'steel', NULL, entySEfos, + 'Steel|+|Biomass', 'steel', NULL, entySEbio, + 'Steel|+|Synfuel', 'steel', NULL, entySEsyn + ) %>% + mutate( + variable = paste0(variable_prefix, .data$variable, variable_postfix)) + out <- mbind( out, - #### industry captured carbon from biomass fuels ---- - setNames( - dimSums(pm_IndstCO2Captured[,,c("sesobio", "seliqbio", "segabio")], - dim = 3) - * GtC_2_MtCO2, - "Carbon Management|Carbon Capture|Industry Energy|+|Biomass (Mt CO2/yr)" - ), - - setNames( - dimSums( - pm_IndstCO2Captured[,,c("sesobio", "seliqbio", "segabio")][,,'cement'], - dim = 3) - * GtC_2_MtCO2, - 'Carbon Management|Carbon Capture|Industry Energy|Cement|+|Biomass (Mt CO2/yr)' - ), - - setNames( - dimSums( - pm_IndstCO2Captured[,,c("sesobio", "seliqbio", "segabio")][,,'chemicals'], - dim = 3) - * GtC_2_MtCO2, - 'Carbon Management|Carbon Capture|Industry Energy|Chemicals|+|Biomass (Mt CO2/yr)' - ), - - setNames( - dimSums( - pm_IndstCO2Captured[,,c("sesobio", "seliqbio", "segabio")][,,'steel'], - dim = 3) - * GtC_2_MtCO2, - 'Carbon Management|Carbon Capture|Industry Energy|Steel|+|Biomass (Mt CO2/yr)' - ), - - #### industry captured carbon from synthetic fuels ---- - setNames( - dimSums(pm_IndstCO2Captured[,,c("seliqsyn", "segasyn")], - dim = 3) - * GtC_2_MtCO2, - "Carbon Management|Carbon Capture|Industry Energy|+|Synfuel (Mt CO2/yr)" - ), - - setNames( - dimSums(pm_IndstCO2Captured[,,c("seliqsyn", "segasyn")][,,'cement'], - dim = 3) - * GtC_2_MtCO2, - "Carbon Management|Carbon Capture|Industry Energy|Cement|+|Synfuel (Mt CO2/yr)" - ), - - setNames( - dimSums(pm_IndstCO2Captured[,,c("seliqsyn", "segasyn")][,,'chemicals'], - dim = 3) - * GtC_2_MtCO2, - "Carbon Management|Carbon Capture|Industry Energy|Chemicals|+|Synfuel (Mt CO2/yr)" - ), - - setNames( - dimSums(pm_IndstCO2Captured[,,c("seliqsyn", "segasyn")][,,'steel'], - dim = 3) - * GtC_2_MtCO2, - "Carbon Management|Carbon Capture|Industry Energy|Steel|+|Synfuel (Mt CO2/yr)" - ), - - #### industry captured carbon from fossil fuels ---- - setNames( - dimSums(pm_IndstCO2Captured[,,c("sesofos", "seliqfos", "segafos")], - dim = 3) - * GtC_2_MtCO2, - "Carbon Management|Carbon Capture|Industry Energy|+|Fossil (Mt CO2/yr)" - ), - - setNames( - dimSums(pm_IndstCO2Captured[,,c("sesofos", "seliqfos", "segafos")][,,'cement'], - dim = 3) - * GtC_2_MtCO2, - "Carbon Management|Carbon Capture|Industry Energy|Cement|+|Fossil (Mt CO2/yr)" - ), - - setNames( - dimSums(pm_IndstCO2Captured[,,c("sesofos", "seliqfos", "segafos")][,,'chemicals'], - dim = 3) - * GtC_2_MtCO2, - "Carbon Management|Carbon Capture|Industry Energy|Chemicals|+|Fossil (Mt CO2/yr)" - ), - - setNames( - dimSums(pm_IndstCO2Captured[,,c("sesofos", "seliqfos", "segafos")][,,'steel'], - dim = 3) - * GtC_2_MtCO2, - "Carbon Management|Carbon Capture|Industry Energy|Steel|+|Fossil (Mt CO2/yr)" - ), - - #### subsectors captured carbon ---- - setNames( - dimSums(pm_IndstCO2Captured[,,'cement'], dim = 3) - * GtC_2_MtCO2, - "Carbon Management|Carbon Capture|Industry Energy|Cement (Mt CO2/yr)" - ), - - setNames( - dimSums(pm_IndstCO2Captured[,,'chemicals'], dim = 3) - * GtC_2_MtCO2, - "Carbon Management|Carbon Capture|Industry Energy|Chemicals (Mt CO2/yr)" - ), - - setNames( - dimSums(pm_IndstCO2Captured[,,'steel'], dim = 3) - * GtC_2_MtCO2, - "Carbon Management|Carbon Capture|Industry Energy|Steel (Mt CO2/yr)" - ) - ) + lapply(.mixer_to_selector(mixer), function(x) { # for each row + setNames( + # extract relevant portions from pm_IndstCO2Captured + # call mselect(), but without the 'variable' column + dimSums( + mselect(pm_IndstCO2Captured, x[setdiff(names(x), 'variable')]), + dim = 3) + * GtC_2_MtCO2, + x[['variable']]) + }) %>% + mbind()) } else { if (!is.null(o37_demFeIndSub)) { @@ -1234,27 +1313,27 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 setNames(out[, , "Carbon Management|Carbon Capture|Fossil|Pe2Se|+|Gases w/ couple prod (Mt CO2/yr)"] * p_share_CCS, "Carbon Management|Storage|Fossil|Pe2Se|+|Gases w/ couple prod (Mt CO2/yr)")) - + #### calculate corresponding negative emissions variables by CDR for bar plots with gross emissions # same as "Carbon Management|Storage|+|DAC (Mt CO2/yr)" etc. but negative - + # only negative land-use change emissions EmiCDR.LUC <- dimSums(vm_emiMacSector[, , "co2luc"], dim = 3) * GtC_2_MtCO2 EmiCDR.LUC[EmiCDR.LUC > 0] <- 0 - + # compute share of atmospheric carbon in total captured carbon - p_share_atmosco2 <- dimSums((out[, , "Carbon Management|Carbon Capture|+|Biomass|Pe2Se (Mt CO2/yr)"] + p_share_atmosco2 <- dimSums((out[, , "Carbon Management|Carbon Capture|+|Biomass|Pe2Se (Mt CO2/yr)"] + out[, , "Carbon Management|Carbon Capture|+|DAC (Mt CO2/yr)"] + out[, , "Carbon Management|Carbon Capture|Industry Energy|+|Biomass (Mt CO2/yr)"]) - / (out[, , "Carbon Management|Carbon Capture|+|Biomass|Pe2Se (Mt CO2/yr)"] - + out[, , "Carbon Management|Carbon Capture|+|DAC (Mt CO2/yr)"] + / (out[, , "Carbon Management|Carbon Capture|+|Biomass|Pe2Se (Mt CO2/yr)"] + + out[, , "Carbon Management|Carbon Capture|+|DAC (Mt CO2/yr)"] + out[, , "Carbon Management|Carbon Capture|Industry Energy|+|Biomass (Mt CO2/yr)"] + out[, , "Carbon Management|Carbon Capture|+|Fossil|Pe2Se (Mt CO2/yr)"] + out[, , "Carbon Management|Carbon Capture|Industry Energy|+|Fossil (Mt CO2/yr)"] + out[, , "Carbon Management|Carbon Capture|+|Industry Process (Mt CO2/yr)"]) ,dim=3) p_share_atmosco2[is.infinite(p_share_atmosco2)] <- 0 p_share_atmosco2[is.na(p_share_atmosco2)] <- 0 - + # Emi|CO2|CDR is defined negative out <- mbind(out, # total negative land-use change emissions @@ -1273,7 +1352,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 # stored CO2 in industry from carbon-neutral fuels (synthetic fuels) setNames(-out[, , "Carbon Management|Carbon Capture|Industry Energy|+|Synfuel (Mt CO2/yr)"] * p_share_atmosco2 * p_share_CCS, "Emi|CO2|CDR|Industry CCS|Synthetic Fuels (Mt CO2/yr)"), - + # total DACCS setNames(-out[, , "Carbon Management|Storage|+|DAC (Mt CO2/yr)"], "Emi|CO2|CDR|DACCS (Mt CO2/yr)"), @@ -1281,7 +1360,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 # total co2 captured by EW setNames(v33_emiEW * GtC_2_MtCO2, "Emi|CO2|CDR|EW (Mt CO2/yr)")) - + out <- mbind(out, # total CDR setNames( out[, , "Emi|CO2|CDR|Land-Use Change (Mt CO2/yr)"] @@ -1290,8 +1369,8 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 + out[, , "Emi|CO2|CDR|EW (Mt CO2/yr)"] + out[, , "Emi|CO2|CDR|Industry CCS|Synthetic Fuels (Mt CO2/yr)"], "Emi|CO2|CDR (Mt CO2/yr)")) - - + + @@ -1302,8 +1381,8 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 # all standard emissions variables "Emi|CO2|..." are defined as net emissions. # This means that negative emissions are counted in and have to be subtracted to obtain gross emissions. - # calculate gross emissions in energy supply sector (i.e. subtracting contribution from supply side BECCS) - # using the respective "Carbon Management|Storage" variables as we don't have the necessary level of detail in the "Emi|CO2|CDR" variables + # calculate gross emissions in energy supply sector (i.e. subtracting contribution from supply side BECCS) + # using the respective "Carbon Management|Storage" variables as we don't have the necessary level of detail in the "Emi|CO2|CDR" variables out <- mbind(out, # gross supply emissions across SE carriers setNames(out[, , "Emi|CO2|Energy|Supply|+|Electricity w/ couple prod (Mt CO2/yr)"] @@ -2161,6 +2240,25 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 out <- mbind(out, calc_regionSubset_sums(out, regionSubsetList)) } + ## aggregate intensive variables ---- + .regionSubsetList <- c(list('GLO' = getItems(vm_co2CCS, dim = 'all_regi')), + regionSubsetList) + + for (i in seq_along(.regionSubsetList)) { + var <- 'Carbon Management|Share of Stored CO2 from Captured CO2 (%)' + + target_region <- .regionSubsetList[i] + source_regions <- .regionSubsetList[[i]] + + out[names(target_region),,var] <- ( + dimSums(vm_co2CCS[source_regions,,], dim = c(1, 3), na.rm = TRUE) + / dimSums(vm_co2capture[source_regions,,], dim = c(1, 3)) + * 100 + ) %>% + ifelse(is.finite(.), ., 0) # set NaN (division by 0) to 0 + } + + # Bunker Correction ---- ### variables for which bunker emissions should be subtracted diff --git a/R/reportTechnology.R b/R/reportTechnology.R index 90d82528..1ebd2f2a 100644 --- a/R/reportTechnology.R +++ b/R/reportTechnology.R @@ -36,7 +36,7 @@ reportTechnology <- function(gdx, output = NULL, regionSubsetList = NULL, t = c( ) } - ## Check realisations + # Check realisations ---- module2realisation <- readGDX(gdx, "module2realisation", react = "silent") tran_mod <- module2realisation[module2realisation$modules == "transport", 2] @@ -45,7 +45,7 @@ reportTechnology <- function(gdx, output = NULL, regionSubsetList = NULL, t = c( sety <- readGDX(gdx, c("entySe", "sety"), format = "first_found") te <- readGDX(gdx, "te") - # calculate maximal temporal resolution + # calculate maximal temporal resolution ---- p_dataeta <- readGDX(gdx, name = c("pm_dataeta", "p_dataeta"), format = "first_found") p_eta_conv <- readGDX(gdx, name = c("pm_eta_conv", "p_eta_conv"), format = "first_found") pm_inco0_t <- readGDX(gdx, name = c("pm_inco0_t", "p_inco0_t"), format = "first_found") @@ -97,7 +97,7 @@ reportTechnology <- function(gdx, output = NULL, regionSubsetList = NULL, t = c( v_adjustteinv_avg <- v_investcost[,,]*0 } - ############ build reporting ##################### + # build reporting ---- techmap <- c( "bioigccc" = "Electricity|Biomass|Gasification Combined Cycle w/ CC", @@ -156,7 +156,6 @@ reportTechnology <- function(gdx, output = NULL, regionSubsetList = NULL, t = c( techmap <- append(techmap, c("MeOH" = "Liquids|Hydrogen", "h22ch4" = "Gases|Hydrogen")) - if (CDR_mod != "off") { cdrmap <- c("dac" = "DAC", "ccsinje" = "CO2 Storage") @@ -213,7 +212,7 @@ reportTechnology <- function(gdx, output = NULL, regionSubsetList = NULL, t = c( ## For cars (technically a Energy Service), we use FEs. int2ext <- c() if (all(map %in% techmap)) { - for (label in techmap) { + for (label in map) { int2ext[[report_str(label, category, unit)]] <- report_str(label, unit = "EJ/yr", predicate = "SE") } ## storage needs a special mapping @@ -244,7 +243,7 @@ reportTechnology <- function(gdx, output = NULL, regionSubsetList = NULL, t = c( tmp <- NULL - ### capital costs ### + ## capital costs ---- category <- "Capital Costs" unit <- "US$2005/kW" @@ -253,7 +252,7 @@ reportTechnology <- function(gdx, output = NULL, regionSubsetList = NULL, t = c( tmp <- bind_category(tmp, v_investcost, category, unit, factor, techmap) int2ext <- get_global_mapping(category, unit, techmap) - ### Capital cost including adjustment cost ### + ### Capital cost including adjustment cost ---- if (!is.null(v_adjustteinv_avg)) { category <- "Capital Costs|w/ Adj Costs" unit <- "US$2005/kW" @@ -276,7 +275,7 @@ reportTechnology <- function(gdx, output = NULL, regionSubsetList = NULL, t = c( int2ext <- c(int2ext, get_global_mapping(category, unit, cdrmap)) } - ### efficiency ### + ## efficiency ---- ## efficiency variables can be found in both p_dataeta and p_eta_conv, so do it one-by-one ## for cars, efficiencies are not given @@ -286,7 +285,9 @@ reportTechnology <- function(gdx, output = NULL, regionSubsetList = NULL, t = c( in_dataeta <- c("bioigccc", "bioigcc", "igccc", "igcc", "pc", "ngccc", "ngcc", "ngt") - for (key in names(techmap)) { + tech_exclude <- techmap[setdiff(names(techmap), 'tnrs')] # exclude nuclear + + for (key in names(tech_exclude)) { if (key %in% in_dataeta) { tmp <- bind_element(tmp, p_dataeta, key, category, unit, factor) } else { @@ -294,10 +295,9 @@ reportTechnology <- function(gdx, output = NULL, regionSubsetList = NULL, t = c( } } - int2ext <- c(int2ext, get_global_mapping(category, unit, techmap)) + int2ext <- c(int2ext, get_global_mapping(category, unit, tech_exclude)) - - ### lifetime ### + ## lifetime ---- category <- "Lifetime" unit <- "years" @@ -316,7 +316,7 @@ reportTechnology <- function(gdx, output = NULL, regionSubsetList = NULL, t = c( - ### o&m fix costs ### + ## o&m fix costs ---- category <- "OM Cost|fixed" unit <- "US$2005/kW/yr" tmp <- bind_category(tmp, omf * v_investcost, category, unit, 1000.) @@ -330,7 +330,6 @@ reportTechnology <- function(gdx, output = NULL, regionSubsetList = NULL, t = c( int2ext <- c(int2ext, get_global_mapping(category, unit, carmap)) } - if (CDR_mod != "off") { ## op costs for CDR technologies ### category <- "OM Cost|fixed" @@ -339,14 +338,14 @@ reportTechnology <- function(gdx, output = NULL, regionSubsetList = NULL, t = c( int2ext <- c(int2ext, get_global_mapping(category, unit, cdrmap)) } - ### o&m variable costs ### + ## o&m variable costs ---- category <- "OM Cost|variable" unit <- "US$2005/GJ" tmp <- bind_category(tmp, omv, category, unit, 1000. / 31.7098) int2ext <- c(int2ext, get_global_mapping(category, unit, techmap)) - ### write to output ### + # write to output ---- ## substitute NA by 1E-30 to avoid that if in 2005, 2010, 2015, 2130, 2150, ## output is 0 in each region, the sum is returned by speed_aggregate output[is.na(output) | output == 0] <- 1E-30 @@ -354,7 +353,7 @@ reportTechnology <- function(gdx, output = NULL, regionSubsetList = NULL, t = c( output <- deletePlus(output) - # add global values + # add global values ---- map <- data.frame(region = getRegions(tmp), world = "GLO", stringsAsFactors = FALSE) y <- Reduce(intersect, list(getYears(tmp), getYears(output))) tmp <- tmp[, y, ] @@ -365,7 +364,7 @@ reportTechnology <- function(gdx, output = NULL, regionSubsetList = NULL, t = c( } tmp <- mbind(tmp, tmp_GLO) - # add other region aggregations + # add other region aggregations ---- if (!is.null(regionSubsetList)) { tmp_RegAgg <- new.magpie(names(regionSubsetList), getYears(tmp), magclass::getNames(tmp), fill = 0) for (region in names(regionSubsetList)) { diff --git a/README.md b/README.md index 92780be4..5a330831 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # The REMIND R package (2nd generation) -R package **remind2**, version **1.112.2** +R package **remind2**, version **1.113.1** [![CRAN status](https://www.r-pkg.org/badges/version/remind2)](https://cran.r-project.org/package=remind2) [![R build status](https://github.com/pik-piam/remind2/workflows/check/badge.svg)](https://github.com/pik-piam/remind2/actions) [![codecov](https://codecov.io/gh/pik-piam/remind2/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/remind2) [![r-universe](https://pik-piam.r-universe.dev/badges/remind2)](https://pik-piam.r-universe.dev/builds) @@ -49,7 +49,7 @@ In case of questions / problems please contact Renato Rodrigues . +Rodrigues R, Baumstark L, Benke F, Dietrich J, Dirnaichner A, Führlich P, Giannousakis A, Hasse R, Hilaire J, Klein D, Koch J, Kowalczyk K, Levesque A, Malik A, Merfort A, Merfort L, Morena-Leiva S, Pehl M, Pietzcker R, Rauner S, Richters O, Rottoli M, Schötz C, Schreyer F, Siala K, Sörgel B, Spahr M, Strefler J, Verpoort P (2023). _remind2: The REMIND R package (2nd generation)_. R package version 1.113.1, . A BibTeX entry for LaTeX users is @@ -58,7 +58,7 @@ A BibTeX entry for LaTeX users is title = {remind2: The REMIND R package (2nd generation)}, author = {Renato Rodrigues and Lavinia Baumstark and Falk Benke and Jan Philipp Dietrich and Alois Dirnaichner and Pascal Führlich and Anastasis Giannousakis and Robin Hasse and Jérome Hilaire and David Klein and Johannes Koch and Katarzyna Kowalczyk and Antoine Levesque and Aman Malik and Anne Merfort and Leon Merfort and Simón Morena-Leiva and Michaja Pehl and Robert Pietzcker and Sebastian Rauner and Oliver Richters and Marianna Rottoli and Christof Schötz and Felix Schreyer and Kais Siala and Björn Sörgel and Mike Spahr and Jessica Strefler and Philipp Verpoort}, year = {2023}, - note = {R package version 1.112.2}, + note = {R package version 1.113.1}, url = {https://github.com/pik-piam/remind2}, } ``` diff --git a/inst/markdown/compareScenarios2/cs2_09_carbon_management.Rmd b/inst/markdown/compareScenarios2/cs2_09_carbon_management.Rmd index c6ca899d..5112a696 100644 --- a/inst/markdown/compareScenarios2/cs2_09_carbon_management.Rmd +++ b/inst/markdown/compareScenarios2/cs2_09_carbon_management.Rmd @@ -30,3 +30,10 @@ showAreaAndBarPlotsPlus(data, "Carbon Management|Usage", scales = "fixed") ```{r carbon management share} showLinePlots(data, "Carbon Management|Share of Stored CO2 from Captured CO2") ``` + +```{r} +showAreaAndBarPlots(data, + vars = c('Carbon Management|Storage', + 'Carbon Management|Usage'), + tot = 'Carbon Management|Carbon Capture') +``` diff --git a/tests/testthat/test-piamInterfaces-Ariadne.R b/tests/testthat/test-piamInterfaces-Ariadne.R index 6dfd7832..acd3f3e9 100644 --- a/tests/testthat/test-piamInterfaces-Ariadne.R +++ b/tests/testthat/test-piamInterfaces-Ariadne.R @@ -1,4 +1,3 @@ -skip() library(gdx) test_that("Test if REMIND reporting produces mandatory variables for Ariadne reporting", { diff --git a/tests/testthat/test-piamInterfaces-NAVIGATE.R b/tests/testthat/test-piamInterfaces-NAVIGATE.R index b4c57ff2..1e4adebb 100644 --- a/tests/testthat/test-piamInterfaces-NAVIGATE.R +++ b/tests/testthat/test-piamInterfaces-NAVIGATE.R @@ -1,4 +1,3 @@ -skip() library(gdx) test_that("Test if REMIND reporting produces mandatory variables for NAVIGATE reporting", { diff --git a/tests/testthat/test-piamInterfaces-NGFS.R b/tests/testthat/test-piamInterfaces-NGFS.R index 00d21373..972f3a02 100644 --- a/tests/testthat/test-piamInterfaces-NGFS.R +++ b/tests/testthat/test-piamInterfaces-NGFS.R @@ -1,4 +1,3 @@ -skip() library(gdx) test_that("Test if REMIND reporting produces mandatory variables for NGFS reporting", { diff --git a/tests/testthat/test-piamInterfaces-SHAPE.R b/tests/testthat/test-piamInterfaces-SHAPE.R index 21bac3e5..4c78e2f4 100644 --- a/tests/testthat/test-piamInterfaces-SHAPE.R +++ b/tests/testthat/test-piamInterfaces-SHAPE.R @@ -1,4 +1,3 @@ -skip() library(gdx) test_that("Test if REMIND reporting produces mandatory variables for SHAPE reporting", {