R/reportCosts.R

Defines functions reportCosts

Documented in reportCosts

#' Read in GDX and calculate costs, used in convGDX2MIF.R for the reporting
#' 
#' Read in cost information from GDX file, information used in convGDX2MIF.R
#' for the reporting
#' 
#' 
#' @param gdx a GDX object as created by readGDX, or the path to a gdx
#' @param output a magpie object containing all needed variables generated by other report*.R functions
#' @param regionSubsetList a list containing regions to create report variables region
#' aggregations. If NULL (default value) only the global region aggregation "GLO" will
#' be created.
#' @return MAgPIE object - contains the cost variables
#' @author David Klein
#' @seealso \code{\link{convGDX2MIF}}
#' @examples
#' 
#' \dontrun{reportCosts(gdx)}
#' 
#' @export
#' @importFrom rlang .data
#' @importFrom magclass mbind getYears collapseNames dimSums setNames
#' @importFrom gdx readGDX
#' @importFrom dplyr filter

reportCosts <- function(gdx,output=NULL,regionSubsetList=NULL) {
  
  # Cost calculation requires information from other reportings
  if(is.null(output)){
      cat("Executing reportExtraction\n")
      output <- mbind(output,reportExtraction(gdx,regionSubsetList))
      cat("Executing reportPrices\n")
      output <- mbind(output,reportPrices(gdx,regionSubsetList=regionSubsetList))
      cat("Executing reportEnergyInvestments\n")
      output <- mbind(output,reportEnergyInvestment(gdx,regionSubsetList)[,getYears(output),])
  }
  
  ########################################################################################
  ############# R E A D I N G    I N P U T ###############################################
  ########################################################################################
  
  # module realisations
  module2realisation <- readGDX(gdx, "module2realisation", react = "silent")
  industry_module <- module2realisation['industry' == module2realisation$modules, 2]

  # Mappings
  ccs2te       <- readGDX(gdx,"ccs2te")
  enty2rlf     <- readGDX(gdx,c("pe2rlf","enty2rlf"),format="first_found")
  teall2rlf    <- readGDX(gdx,c("te2rlf","teall2rlf"),format="first_found")
  pe2se        <- readGDX(gdx,"pe2se")
  se2fe        <- readGDX(gdx,"se2fe")
  se2se        <- readGDX(gdx,c("se2se"),format="first_found")
  
  # Sets
  tenoccs      <- readGDX(gdx,c("teNoCCS","tenoccs"),format="first_found")
  temapall     <- readGDX(gdx,c("en2en","temapall"),format="first_found")
  tenotransform<- readGDX(gdx,c("teNoTransform","tenotransform"),format="first_found")
  stor         <- readGDX(gdx,c("teStor","stor"),format="first_found")
  grid         <- readGDX(gdx,c("teGrid","grid"),format="first_found")
  trade_pe     <- readGDX(gdx,c("tradePe","trade_pe"),format="first_found")
  perenew      <- readGDX(gdx,c("peRe","perenew"),format="first_found")
  petyf        <- readGDX(gdx,c("peFos","petyf"),format="first_found")
  sety         <- readGDX(gdx,c("entySe","sety"),format="first_found")
  fety         <- readGDX(gdx,c("entyFe","fety"),format="first_found")
  potentialseLiq <- c("seliq","sepet","sedie")  # the sety liquids changed from sepet+sedie to seLiq in REMIND 1.7
  se_Liq    <- intersect(potentialseLiq,sety)
  teccs        <- readGDX(gdx,c("teCCS","teccs"),format="first_found")
  pebio        <- readGDX(gdx,c("peBio","pebio"),format="first_found")
  ppfen_stat   <- readGDX(gdx,c("ppfen_stationary_dyn38","ppfen_stationary_dyn28","ppfen_stationary"),format="first_found", react = "silent")
  if (length(ppfen_stat) == 0) ppfen_stat = NULL
  fe2ppfen37 <- readGDX(gdx, 'fe2ppfen37', react = 'silent')
  emiMacMagpie    <- readGDX(gdx,c("emiMacMagpie"),format="first_found")
  emiMacMagpieN2O <- readGDX(gdx,c("emiMacMagpieN2O"),format="first_found")
  emiMacMagpieCH4 <- readGDX(gdx,c("emiMacMagpieCH4"),format="first_found")
  emiMacMagpieCO2 <- readGDX(gdx,c("emiMacMagpieCO2"),format="first_found")
  
  # Parameters
  pm_conv_TWa_EJ         <- 31.536  
  sm_tdptwyr2dpgj        <- 31.71   #TerraDollar per TWyear to Dollar per 
  pm_data                <- readGDX(gdx,name="pm_data",format="first_found")
  pm_petradecost2_Mp_fin <- readGDX(gdx,name="pm_petradecost2_Mp_fin",format="first_found", react = "silent")
  if(is.null(pm_petradecost2_Mp_fin)){
    pm_petradecost2_Mp_fin  <- readGDX(gdx,name="pm_costsTradePeFinancial",format="first_found")
    pm_petradecost2_Mp_fin  <- collapseNames(pm_petradecost2_Mp_fin[,,"Mport"])
  }
  p_dataeta              <- readGDX(gdx,name=c("pm_dataeta","p_dataeta"),format="first_found")
  pm_pvp                 <- readGDX(gdx,name=c("pm_pvp"),format = "first_found")
  cost_emu_pre           <- readGDX(gdx,name="p30_pebiolc_costs_emu_preloop",format="first_found")
  cost_mag               <- readGDX(gdx,name="p30_pebiolc_costsmag",format="first_found")[,getYears(cost_emu_pre),]
  totLUcosts             <- readGDX(gdx,name=c("pm_totLUcosts"),format="first_found")[,getYears(cost_emu_pre),]
  totLUcostsWithMAC      <- readGDX(gdx,name=c("p26_totLUcosts_withMAC"),format="first_found")[,getYears(cost_emu_pre),]
  costsLuMACLookup       <- readGDX(gdx,name=c("p26_macCostLu"),format="first_found")[,getYears(cost_emu_pre),]
  costsMAC               <- readGDX(gdx,name=c("pm_macCost","p_macCost"),format="first_found")[,getYears(cost_emu_pre),]
  p_eta_conv             <- readGDX(gdx,name=c("pm_eta_conv","p_eta_conv"),format = "first_found")
  
  # Variables  
  Xport            <- readGDX(gdx,name=c("vm_Xport"),         field = "l",format = "first_found")
  Mport            <- readGDX(gdx,name=c("vm_Mport"),         field = "l",format = "first_found")
  vm_fuelex        <- readGDX(gdx,name=c("vm_fuExtr","vm_fuelex"),        field="l",restore_zeros=FALSE,format="first_found")[enty2rlf]
  vm_costfu_ex     <- readGDX(gdx,name=c("vm_costFuEx","vm_costfu_ex"),     field="l",restore_zeros=FALSE,format="first_found")
  v_costfu         <- readGDX(gdx,name=c("v_costFu","v_costfu"),         field="l",restore_zeros=FALSE,format="first_found")
  v_costom         <- readGDX(gdx,name=c("v_costOM","v_costom"),         field="l",                    format="first_found")
  v_costin         <- readGDX(gdx,name=c("v_costInv","v_costin"),         field = "l",format = "first_found")
  vm_omcosts_cdr   <- readGDX(gdx,name=c("vm_omcosts_cdr"),   field="l",                  format="first_found")
  vm_otherFEdemand <- readGDX(gdx,name=c("vm_otherFEdemand"), field="l",restore_zeros=FALSE,format="first_found")
  v_investcost     <- readGDX(gdx,name=c("vm_costTeCapital","v_costTeCapital","v_investcost"),     field="l",format="first_found")
  vm_cap           <- readGDX(gdx,name=c("vm_cap"),           field="l",format="first_found")
  vm_cap[is.na(vm_cap)]    <- 0
  vm_prodSe        <- readGDX(gdx,name=c("vm_prodSe"),        field="l",restore_zeros=FALSE,format="first_found")
  vm_prodFe        <- readGDX(gdx,name=c("vm_prodFe"),        field="l",restore_zeros=FALSE,format="first_found")
  cost_emu         <- readGDX(gdx,name=c("v30_pebiolc_costs"),field="l",format="first_found")
  bio_cost_adjfac  <- readGDX(gdx,name=c("v30_multcost"),field="l",format="first_found")
  vm_cesIO         <- readGDX(gdx, name = c('vm_cesIO'), field = 'l', 
                              restore_zeros = FALSE)
  
  # Equations
  finenbal     <- readGDX(gdx,name=c("fe2ppfEn","finenbal"),  types="sets",format="first_found")
  pebal.m      <- readGDX(gdx,name=c("q_balPe","qm_pebal"),  types="equations",field="m",format="first_found")
  budget.m     <- readGDX(gdx,name='qm_budget', types="equations",field="m",format="first_found") # alternative: calcPrice
  sebal.m      <- readGDX(gdx,name=c("q_balSe","q_sebal"),   types="equations",field="m",format="first_found")
  febal.m      <- readGDX(gdx,name=c("q_balFe","q_febal"),   types="equations",field="m",format="first_found")[,,fety]
  balfinen.m   <- readGDX(gdx,name=c("qm_balFeForCesAndEs","qm_balFeForCes","q_balFeForCes","q_balfinen"),types="equations",field="m",format="first_found")[finenbal]
  
  # Find common years
  y <- Reduce(intersect,list(getYears(vm_fuelex),
                             getYears(vm_costfu_ex),
                             getYears(p_dataeta),
                             getYears(v_investcost),
                             getYears(vm_cap),
                             getYears(vm_prodSe)))
  
  # Reduce to common years
  p_dataeta        <- p_dataeta[,y,]   
  pm_pvp           <- pm_pvp[,y,]
  vm_fuelex        <- vm_fuelex[,y,]   
  Xport            <- Xport[,y,]   
  Mport            <- Mport[,y,]
  vm_costfu_ex     <- vm_costfu_ex[,y,]
  v_costfu         <- v_costfu[,y,]
  v_costom         <- v_costom[,y,]
  v_costin         <- v_costin[,y,]
  vm_otherFEdemand <- vm_otherFEdemand[,y,]
  v_investcost     <- v_investcost[,y,]
  vm_cap           <- vm_cap[,y,]       
  vm_prodSe        <- vm_prodSe[,y,]
  pebal.m          <- pebal.m[,y,]
  budget.m         <- budget.m[,y,]
  sebal.m          <- sebal.m[,y,]
  febal.m          <- febal.m[,y,]
  balfinen.m       <- balfinen.m[,y,]
  vm_omcosts_cdr   <- vm_omcosts_cdr[,y,]
  output           <- output[,y,]
  p_eta_conv       <- p_eta_conv[,y,]
  
  # Gather efficiency data in only one magpie object to enable generic structure of below defined functions:
  # Take values from pm_data (constant efficiency over time) for technologies where p_dataeta (time variant efficiencies) has no values.
  # Thus, in the below defined formulas time variant efficiencies will be used if available otherwise time invariant
  for (te in magclass::getNames(p_dataeta)) {
    if(all(p_dataeta[,,te]==0)) {
      for (year in getYears(p_dataeta)) {
        p_dataeta[,year,te] <- pm_data[,,"eta"][,,te]
      }
    } 
  }
  
  ########################################################################################
  ############# F U N C T I O N S    D E F I N I T I O N #################################
  ########################################################################################
  
  op_costs_part2_foss <- function(pe,se,te,vm_prodSe,i_pe2se,p_dataeta,vm_costfu_ex,vm_fuelex,sm_tdptwyr2dpgj,pm_conv_TWa_EJ) {
    sub_pe2se         <- i_pe2se[(pe2se$all_enty %in% pe) & (i_pe2se$all_enty1 %in% se) & (i_pe2se$all_te %in% te),]
    out <- dimSums( vm_costfu_ex[,,pe]   / dimSums(vm_fuelex[,,pe],dim = 3, na.rm = T), dim = 3, na.rm = T) * sm_tdptwyr2dpgj * # Average supply costs
           dimSums( vm_prodSe[sub_pe2se] / p_dataeta[,,sub_pe2se$all_te],  dim = 3, na.rm = T) * pm_conv_TWa_EJ  
    return(out)
  }

  op_costs_part2_bio_nuc <- function(pe,se,te,vm_prodSe,i_pe2se,p_dataeta,pm_conv_TWa_EJ) {
    sub_pe2se         <- i_pe2se[(pe2se$all_enty %in% pe) & (i_pe2se$all_enty1 %in% se) & (i_pe2se$all_te %in% te),]
    out <- dimSums( vm_prodSe[sub_pe2se] / p_dataeta[,,sub_pe2se$all_te],  dim = 3, na.rm = T) * pm_conv_TWa_EJ  
    return(out)
  }
 
  # operation and maintenance costs
   op_costs <- function(ei,eo,te,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=NULL,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost) {
     
   
    # Check whether a mapping with energy transfering technologies is given or just technologies
    if (!is.null(e2e)) {
      sub_e2e         <- e2e[(e2e$all_enty %in% ei) & (e2e$all_enty1 %in% eo) & (e2e$all_te %in% te),]
      # Save technologies to extra variables because in the "else" case there is no sub_e2e list that could be used
      e2e_allte       <- sub_e2e$all_te
    } else {
      e2e_allte       <- te
    }
    
    vm_cap <- dimSums(vm_cap,dim=3.2,na.rm=T) # get rid of grades which are not used in calculations below because all other variables dont have them neither
    
    out <- dimSums(                                       # sum OMF over technologies
      collapseNames(pm_data[,,"omf"])[,,e2e_allte] *                                           
        (dimSums((v_investcost[,,e2e_allte] * vm_cap[,,e2e_allte])[teall2rlf],dim=3.2,na.rm=T)   # sum over rlf   
        )[,, e2e_allte],dim = 3, na.rm = T)                                                                
    
    if(!is.null(vm_prodE) & !length(e2e_allte)==0) {     # if either SE or FE is produced, then variable O&M is added
       out <- out + dimSums(collapseNames(pm_data[,,"omv"])[,,sub_e2e$all_te] * dimSums(vm_prodE[sub_e2e],dim=c(3.1,3.2),na.rm=T)
                            , dim = 3, na.rm = T) 
    }
    
    out <- out * 1000
    
    return(out)
  }

  cost_Mport <- function(pety,pm_petradecost2_Mp_fin,pm_pvp,budget.m,Mport) {
    out <- dimSums((pm_petradecost2_Mp_fin[,,pety] + (pm_pvp[,,pety] / (budget.m + 1.e-10))) * Mport[,,pety], dim = 3, na.rm = T) * 1000
    return(out)
  }
  
  ########################################################################################
  ############# O U T P U T   G E N E R A T I O N ########################################
  ########################################################################################
  
  # Initialize empty object that will be filled with reporting results
  tmp <- NULL
  
  # Calculate and append new variables to magpie object
  
  #######################################
  ########## Biomass costs ##############
  #######################################
  
  if(!is.null(cost_mag)) {tmp <- mbind(tmp,setNames(cost_mag * 1000, "Costs|Biomass|MAgPIE (billion US$2005/yr)"))}
  tmp <- mbind(tmp,setNames(cost_emu_pre                     * 1000, "Costs|Biomass|Price integral presolve (billion US$2005/yr)"))
  tmp <- mbind(tmp,setNames(cost_emu                         * 1000, "Costs|Biomass|Price integral (billion US$2005/yr)"))
  tmp <- mbind(tmp,setNames(bio_cost_adjfac,                         "Costs|Biomass|Adjfactor (-)"))
  
  if(!is.null(totLUcosts))        {tmp <- mbind(tmp,setNames(totLUcosts                                 * 1000, "Costs|Land Use (billion US$2005/yr)"))}
  if(!is.null(totLUcostsWithMAC)) {tmp <- mbind(tmp,setNames(totLUcostsWithMAC                          * 1000, "Costs|Land Use with MAC-costs from MAgPIE (billion US$2005/yr)"))}
  if(!is.null(costsLuMACLookup))  {tmp <- mbind(tmp,setNames(costsLuMACLookup                           * 1000, "Costs|Land Use|MAC-costs Lookup (billion US$2005/yr)"))}
  if(!is.null(getNames(costsMAC))) tmp <- mbind(tmp,setNames(dimSums(costsMAC[,,emiMacMagpie],   dim = 3, na.rm = T) * 1000, "Costs|Land Use|MAC-costs (billion US$2005/yr)"))
  if(!is.null(getNames(costsMAC))) tmp <- mbind(tmp,setNames(dimSums(costsMAC[,,emiMacMagpieN2O],dim = 3, na.rm = T) * 1000, "Costs|Land Use|MAC-costs|N2O (billion US$2005/yr)"))
  if(!is.null(getNames(costsMAC))) tmp <- mbind(tmp,setNames(dimSums(costsMAC[,,emiMacMagpieCH4],dim = 3, na.rm = T) * 1000, "Costs|Land Use|MAC-costs|CH4 (billion US$2005/yr)"))
  if(!is.null(getNames(costsMAC))) tmp <- mbind(tmp,setNames(dimSums(costsMAC[,,emiMacMagpieCO2],dim = 3, na.rm = T) * 1000, "Costs|Land Use|MAC-costs|CO2 (billion US$2005/yr)"))
  
  tmp    <- tmp[,y,]
  
  #######################################
  ########## Fuel Costs #################
  #######################################
  
  ##### Total
  tmp  <- mbind(tmp,setNames(v_costfu * 1000, "Fuel supply costs (billion US$2005/yr)"))   
       
  ##### Fuel costs for own ESM
  regi_on_gdx <- unique(readGDX(gdx, name = "regi2iso")[,1])
  
  # v_costfu: these costs are the total fuel extraction costs, even the ones for export
  # Supply costs: exports valued at supply cost (else the ESM costs would be greatly underestimated for a resource-exporting country that makes large profits from the export), except for biomass where supply costs are not yet reported
  cost <- v_costfu * 1000 - setNames(output[regi_on_gdx,,"Res|Average Supply Costs|Coal ($/GJ)"] * Xport[,,"pecoal"]  * pm_conv_TWa_EJ,NULL) -
                            setNames(output[regi_on_gdx,,"Res|Average Supply Costs|Gas ($/GJ)"]       * Xport[,,"pegas"]   * pm_conv_TWa_EJ,NULL) - 
                            setNames(output[regi_on_gdx,,"Res|Average Supply Costs|Oil ($/GJ)"]       * Xport[,,"peoil"]   * pm_conv_TWa_EJ,NULL) -  
                            setNames(output[regi_on_gdx,,"Price|Biomass|Primary Level (US$2005/GJ)"]  * Xport[,,"pebiolc"] * pm_conv_TWa_EJ,NULL) -
                            setNames(output[regi_on_gdx,,"Res|Average Supply Costs|Uranium ($/GJ)"]   * Xport[,,"peur"]    * pm_conv_TWa_EJ * 4.43,NULL) +
                            dimSums(Mport[,,trade_pe] * pebal.m[,,trade_pe] / (budget.m + 1.e-10), dim = 3, na.rm = T) * 1000 # imports valued with domestic market price
  
  tmp  <- mbind(tmp,setNames(cost, "Fuel costs for own ESM (billion US$2005/yr)"))
  
  ##### Import fuel costs
  cost<- cost_Mport(trade_pe,pm_petradecost2_Mp_fin,pm_pvp,budget.m,Mport)
  tmp  <- mbind(tmp,setNames(cost, "Import Fuel costs (billion US$2005/yr)"))
  
  ###### Import fuel costs|Coal 
  cost<- cost_Mport("pecoal",pm_petradecost2_Mp_fin,pm_pvp,budget.m,Mport)
  tmp  <- mbind(tmp,setNames(cost, "Import Fuel costs|Coal (billion US$2005/yr)"))
  
  ###### Import fuel costs|Gas
  cost<- cost_Mport("pegas",pm_petradecost2_Mp_fin,pm_pvp,budget.m,Mport)
  tmp  <- mbind(tmp,setNames(cost, "Import Fuel costs|Gas (billion US$2005/yr)"))
  
  ###### Import fuel costs|Oil
  cost<- cost_Mport("peoil",pm_petradecost2_Mp_fin,pm_pvp,budget.m,Mport)
  tmp  <- mbind(tmp,setNames(cost, "Import Fuel costs|Oil (billion US$2005/yr)"))
  
  ###### Import fuel costs|Uranium
  cost<- cost_Mport("peur",pm_petradecost2_Mp_fin,pm_pvp,budget.m,Mport)
  tmp  <- mbind(tmp,setNames(cost, "Import Fuel costs|Uranium (billion US$2005/yr)"))

  ###### Import fuel costs|Pebiolc
  cost<- cost_Mport("pebiolc",pm_petradecost2_Mp_fin,pm_pvp,budget.m,Mport)
  tmp  <- mbind(tmp,setNames(cost, "Import Fuel costs|Biomass (billion US$2005/yr)"))

  ###### Export Fuel Costs|Coal
  cost <- output[regi_on_gdx,,"Res|Average Supply Costs|Coal ($/GJ)"] * Xport[,,"pecoal"] * pm_conv_TWa_EJ
  tmp  <- mbind(tmp,setNames(cost, "Export Fuel costs|Coal (billion US$2005/yr)"))
  
  ###### Export Fuel Costs|Gas 
  cost <- output[regi_on_gdx,,"Res|Average Supply Costs|Gas ($/GJ)"] * Xport[,,"pegas"] * pm_conv_TWa_EJ
  tmp  <- mbind(tmp,setNames(cost, "Export Fuel costs|Gas (billion US$2005/yr)"))
  
  ###### Export Fuel Costs|Oil
  cost <- output[regi_on_gdx,,"Res|Average Supply Costs|Oil ($/GJ)"] * Xport[,,"peoil"] * pm_conv_TWa_EJ
  tmp  <- mbind(tmp,setNames(cost, "Export Fuel costs|Oil (billion US$2005/yr)"))
  
  ###### Export Fuel Costs|Biomass
  #cost <- output[regi_on_gdx,,"Res|Average Supply Costs|Biomass ($/GJ)"] * Xport[,,"pebiolc"]
  #tmp  <- mbind(tmp,setNames(cost, "Export Fuel costs|Biomass (billion US$2005/yr)"))
  
  ###### Export Fuel Costs|Uranium
  cost <- output[regi_on_gdx,,"Res|Average Supply Costs|Uranium ($/GJ)"] * Xport[,,"peur"] * p_eta_conv[,,"tnrs"] * pm_conv_TWa_EJ
  tmp  <- mbind(tmp,setNames(cost, "Export Fuel costs|Uranium (billion US$2005/yr)"))
  
  #######################################
  ########## Energy System costs ########
  #######################################
  
  cost <- (v_costin + v_costom) * 1000 + tmp[,,"Fuel costs for own ESM (billion US$2005/yr)"]
  tmp  <- mbind(tmp,setNames(cost,"Energy system costs (billion US$2005/yr)"))
    
  #######################################
  ########## Energy costs ###############
  #######################################
  if (!is.null(ppfen_stat)){
    ##### CDR
    price_feels <- abs(balfinen.m[,,"feels.feels"]/(budget.m+1e-10)) * 1000 / pm_conv_TWa_EJ # "Price|Final Energy|Electricity|Stationary (US$2005/GJ)")
    price_fedie <- abs(febal.m[,,"fedie"]         /(budget.m+1e-10)) * 1000 / pm_conv_TWa_EJ # "Price|Final Energy|Diesel (US$2005/GJ)")
    price_fehes <- abs(balfinen.m[,,"fehes.fehes"]/(budget.m+1e-10)) * 1000 / pm_conv_TWa_EJ #  "Price|Final Energy|Heat|
    price_fegas <- abs(balfinen.m[,,"fegas.fegas"]/(budget.m+1e-10)) * 1000 / pm_conv_TWa_EJ # "Price|Final Energy|Gas (US$2005/GJ)")
    price_feh2s <- abs(balfinen.m[,,"feh2s.feh2s"]/(budget.m+1e-10)) * 1000 / pm_conv_TWa_EJ # "Price|Final Energy|Hydrogen|Stationary (US$2005/GJ)")
    
    # reduce names in 3rd dimension
    price_feels <- setNames(price_feels,"feels") # reduce feels.feels to feels
    price_fehes <- setNames(price_fehes,"fehes") # reduce fehes.fehes to fehes
    price_fegas <- setNames(price_fegas,"fegas") # reduce fegas.fegas to fegas
    price_feh2s <- setNames(price_feh2s,"feh2s") # reduce feh2s.feh2s to feh2s
    
    tmp  <- mbind(tmp,setNames(price_feels * vm_otherFEdemand[,,"feels"] * pm_conv_TWa_EJ, "Energy costs CDR|Electricity (billion US$2005/yr)"))   
    tmp  <- mbind(tmp,setNames(price_fedie * vm_otherFEdemand[,,"fedie"] * pm_conv_TWa_EJ, "Energy costs CDR|Diesel (billion US$2005/yr)"))
    tmp  <- mbind(tmp,setNames((price_fegas * vm_otherFEdemand[,,"fegas"] + price_feh2s * vm_otherFEdemand[,,"feh2s"]) * pm_conv_TWa_EJ, "Energy costs CDR|Heat (billion US$2005/yr)"))
    
    tmp  <- mbind(tmp,setNames(price_feels * vm_otherFEdemand[,,"feels"] * pm_conv_TWa_EJ + 
                                 price_fedie * vm_otherFEdemand[,,"fedie"] * pm_conv_TWa_EJ +
                                 price_fehes * vm_otherFEdemand[,,"fehes"] * pm_conv_TWa_EJ,
                               "Energy costs CDR (billion US$2005/yr)"))
  } else {
    
    if ('subsectors' == industry_module) {
      fe2ppfen37_feels <- filter(fe2ppfen37, 'feels' == .data$all_enty)
      fe2ppfen37_fegas <- filter(fe2ppfen37, 'fegas' == .data$all_enty)
      fe2ppfen37_feh2s <- filter(fe2ppfen37, 'feh2s' == .data$all_enty)
      
      
      # "Price|Final Energy|Electricity|Industry (US$2005/GJ)")
      price_feeli <- abs(
        ( dimSums(
            mselect(balfinen.m, fe2ppfen37_feels) 
          * vm_cesIO[,y,fe2ppfen37_feels$all_in],
          dim = 3, na.rm = T) 
        / dimSums(vm_cesIO[,y,fe2ppfen37_feels$all_in], dim = 3, na.rm = T)
        )
        / (budget.m + 1e-10) * 1000 / pm_conv_TWa_EJ
      )
      
      # "Price|Final Energy|Gases|Industry (US$2005/GJ)")
      price_fegai <- abs(
        ( dimSums(
          mselect(balfinen.m, fe2ppfen37_fegas) 
          * vm_cesIO[,y,fe2ppfen37_fegas$all_in],
          dim = 3, na.rm = T) 
          / dimSums(vm_cesIO[,y,fe2ppfen37_fegas$all_in], dim = 3, na.rm = T)
        )
        / (budget.m + 1e-10) * 1000 / pm_conv_TWa_EJ
      )
      
      # "Price|Final Energy|Hydrogen|Industry (US$2005/GJ)")
      price_feh2i <- abs(
        ( dimSums(
          mselect(balfinen.m, fe2ppfen37_feh2s) 
          * vm_cesIO[,y,fe2ppfen37_feh2s$all_in],
          dim = 3, na.rm = T) 
          / dimSums(vm_cesIO[,y,fe2ppfen37_feh2s$all_in], dim = 3, na.rm = T)
        )
        / (budget.m + 1e-10) * 1000 / pm_conv_TWa_EJ
      )
    } else {
      price_feeli <- abs(balfinen.m[,,"feels.feeli"]/(budget.m+1e-10)) * 1000 / pm_conv_TWa_EJ # "Price|Final Energy|Electricity|Industry (US$2005/GJ)")
      price_fegai <- abs(balfinen.m[,,"fegas.fegai"]/(budget.m+1e-10)) * 1000 / pm_conv_TWa_EJ # "Price|Final Energy|Gases|Industry (US$2005/GJ)")
      price_feh2i <- abs(balfinen.m[,,"feh2s.feh2i"]/(budget.m+1e-10)) * 1000 / pm_conv_TWa_EJ # "Price|Final Energy|Hydrogen|Industry (US$2005/GJ)")
    }
    
    
    price_fedie <- abs(febal.m[,,"fedie"]         /(budget.m+1e-10)) * 1000 / pm_conv_TWa_EJ # "Price|Final Energy|Diesel (US$2005/GJ)")
    
    # reduce names in 3rd dimension
    
    price_feeli <- setNames(price_feeli,"feeli") # reduce feels.feels to feels
    price_fegai <- setNames(price_fegai,"fegai") # reduce fehes.fehes to fehes
    price_feh2i <- setNames(price_feh2i,"feh2i") # reduce fehes.fehes to fehes
    
    tmp  <- mbind(tmp,setNames(price_feeli * vm_otherFEdemand[,,"feels"] * pm_conv_TWa_EJ , "Energy costs CDR|Electricity (billion US$2005/yr)"))   
    tmp  <- mbind(tmp,setNames(price_fedie * vm_otherFEdemand[,,"fedie"] * pm_conv_TWa_EJ, "Energy costs CDR|Diesel (billion US$2005/yr)"))
    tmp  <- mbind(tmp,setNames(price_fegai * vm_otherFEdemand[,,"fegas"] * pm_conv_TWa_EJ + 
                                 price_feh2i * vm_otherFEdemand[,,"feh2s"] * pm_conv_TWa_EJ, "Energy costs CDR|Heat (billion US$2005/yr)"))
    
    tmp  <- mbind(tmp,setNames(price_feeli * vm_otherFEdemand[,,"feels"] * pm_conv_TWa_EJ +
                                 price_fedie * vm_otherFEdemand[,,"fedie"] * pm_conv_TWa_EJ +
                                 price_fegai * vm_otherFEdemand[,,"fegas"] * pm_conv_TWa_EJ +
                                 price_feh2i * vm_otherFEdemand[,,"feh2s"] * pm_conv_TWa_EJ,
                               "Energy costs CDR (billion US$2005/yr)"))
  }
  
  #######################################
  ########## Total Energy costs #########
  #######################################
  
  ##### Total 
  cost1 <- op_costs(ei=temapall$all_enty,eo=sety,te=teall2rlf$all_te,e2e=temapall,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  cost2 <- op_costs(ei=temapall$all_enty,eo=fety,te=teall2rlf$all_te,e2e=temapall,teall2rlf=teall2rlf,vm_prodE=vm_prodFe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  cost3 <- op_costs(ei=NULL,eo=NULL,te=tenotransform,e2e=NULL,teall2rlf=teall2rlf,vm_prodE=NULL,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost1 + cost2 + cost3 + output[regi_on_gdx,,"Energy Investments|Supply (billion US$2005/yr)"], "Total Energy costs (billion US$2005/yr)"))
  
  ##### Electricity 
  cost1 <- op_costs(ei=pe2se$all_enty,eo="seel",te=pe2se$all_te,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  cost2 <- op_costs(ei="seel",eo=se2fe$all_enty1,te=se2fe$all_te,e2e=se2fe,teall2rlf=teall2rlf,vm_prodE=vm_prodFe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  cost3 <- op_costs(ei=NULL,eo=NULL,te=tenotransform,e2e=NULL,teall2rlf=teall2rlf,vm_prodE=NULL,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost1 + cost2 + cost3 + output[regi_on_gdx,,"Energy Investments|Electricity (billion US$2005/yr)"], "Total Energy costs|Electricity (billion US$2005/yr)"))
  
  ##### Elec|Fossil
  cost <- op_costs(ei=petyf,eo="seel",te=pe2se$all_te,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost + output[regi_on_gdx,,"Energy Investments|Elec|Fossil (billion US$2005/yr)"], "Total Energy costs|Elec|Fossil (billion US$2005/yr)"))
  
  ##### Elec|Non-fossil 
  cost <- op_costs(ei=setdiff(pe2se$all_enty,petyf),eo="seel",te=pe2se$all_te,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost + output[regi_on_gdx,,"Energy Investments|Elec|Non-Fossil (billion US$2005/yr)"], "Total Energy costs|Elec|Non-Fossil (billion US$2005/yr)"))
  
  ##### Elec|Biomass
  cost <- op_costs(ei="pebiolc",eo="seel",te=pe2se$all_te,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost + output[regi_on_gdx,,"Energy Investments|Elec|Biomass (billion US$2005/yr)"], "Total Energy costs|Elec|Biomass (billion US$2005/yr)"))
  
  cost <- op_costs(ei="pebiolc",eo="seel",te=teccs,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost + output[regi_on_gdx,,"Energy Investments|Elec|Biomass|w/ CCS (billion US$2005/yr)"], "Total Energy costs|Elec|Biomass|w/ CCS (billion US$2005/yr)"))
  
  cost <- op_costs(ei="pebiolc",eo="seel",te=tenoccs,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost + output[regi_on_gdx,,"Energy Investments|Elec|Biomass|w/o CCS (billion US$2005/yr)"], "Total Energy costs|Elec|Biomass|w/o CCS (billion US$2005/yr)"))
  
  ##### Elec|Coal
  cost <- op_costs(ei="pecoal",eo="seel",te=pe2se$all_te,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  cost <- cost + output[regi_on_gdx,,"Energy Investments|Elec|Coal (billion US$2005/yr)"]
  tmp  <- mbind(tmp,setNames(cost, "Total Energy costs|Elec|Coal (billion US$2005/yr)"))
  
  cost <- op_costs(ei="pecoal",eo="seel",te=teccs,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  cost <- cost + output[regi_on_gdx,,"Energy Investments|Elec|Coal|w/ CCS (billion US$2005/yr)"]
  tmp  <- mbind(tmp,setNames(cost, "Total Energy costs|Elec|Coal|w/ CCS (billion US$2005/yr)"))
  
  cost <- op_costs(ei="pecoal",eo="seel",te=tenoccs,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  cost <- cost + output[regi_on_gdx,,"Energy Investments|Elec|Coal|w/o CCS (billion US$2005/yr)"]
  tmp  <- mbind(tmp,setNames(cost, "Total Energy costs|Elec|Coal|w/o CCS (billion US$2005/yr)"))
  
  ##### Elec|Gas
  cost <- op_costs(ei="pegas",eo="seel",te=pe2se$all_te,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost + output[regi_on_gdx,,"Energy Investments|Elec|Gas (billion US$2005/yr)"], "Total Energy costs|Elec|Gas (billion US$2005/yr)"))
  
  cost <- op_costs(ei="pegas",eo="seel",te=teccs,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost + output[regi_on_gdx,,"Energy Investments|Elec|Gas|w/ CCS (billion US$2005/yr)"], "Total Energy costs|Elec|Gas|w/ CCS (billion US$2005/yr)"))
  
  cost <- op_costs(ei="pegas",eo="seel",te=tenoccs,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost + output[regi_on_gdx,,"Energy Investments|Elec|Gas|w/o CCS (billion US$2005/yr)"], "Total Energy costs|Elec|Gas|w/o CCS (billion US$2005/yr)"))
  
  ##### Elec|Oil
  cost <- op_costs(ei="peoil",eo="seel",te=pe2se$all_te,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost + output[regi_on_gdx,,"Energy Investments|Elec|Oil (billion US$2005/yr)"], "Total Energy costs|Elec|Oil (billion US$2005/yr)"))
  
  cost <- op_costs(ei="peoil",eo="seel",te=tenoccs,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost + output[regi_on_gdx,,"Energy Investments|Elec|Oil|w/o CCS (billion US$2005/yr)"], "Total Energy costs|Elec|Oil|w/o CCS (billion US$2005/yr)"))
  
  ##### Elec|Nuclear 
  cost <- op_costs(ei="peur",eo="seel",te=pe2se$all_te,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost + output[regi_on_gdx,,"Energy Investments|Elec|Nuclear (billion US$2005/yr)"], "Total Energy costs|Elec|Nuclear (billion US$2005/yr)"))
  
  ##### Elec|Non-biomass renewables
  cost <- op_costs(ei=setdiff(perenew,pebio),eo="seel",te=pe2se$all_te,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost + output[regi_on_gdx,,"Energy Investments|Elec|Non-Bio Re (billion US$2005/yr)"], "Total Energy costs|Elec|Non-Bio Re (billion US$2005/yr)"))
  
  ##### Elec|Solar
  cost <- op_costs(ei="pesol",eo="seel",te=pe2se$all_te,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost + output[regi_on_gdx,,"Energy Investments|Elec|Solar (billion US$2005/yr)"], "Total Energy costs|Elec|Solar (billion US$2005/yr)"))
  
  ##### Elec|Wind
  cost <- op_costs(ei="pewin",eo="seel",te=pe2se$all_te,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost + output[regi_on_gdx,,"Energy Investments|Elec|Wind (billion US$2005/yr)"], "Total Energy costs|Elec|Wind (billion US$2005/yr)"))
  
  ##### Elec|Hydro
  cost <- op_costs(ei="pehyd",eo="seel",te=pe2se$all_te,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost + output[regi_on_gdx,,"Energy Investments|Elec|Hydro (billion US$2005/yr)"], "Total Energy costs|Elec|Hydro (billion US$2005/yr)"))
  
  ##### Elec|Geothermal
  cost <- op_costs(ei="pegeo",eo="seel",te=pe2se$all_te,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost + output[regi_on_gdx,,"Energy Investments|Elec|Geothermal (billion US$2005/yr)"], "Total Energy costs|Elec|Geothermal (billion US$2005/yr)"))
  
  ##### Elec|Hydrogen
  if ("h2turb" %in% se2se$all_te) {
    cost <- op_costs(ei="seh2",eo="seel",te=se2se$all_te,e2e=se2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
    tmp  <- mbind(tmp,setNames(cost + output[regi_on_gdx,,"Energy Investments|Elec|Hydrogen (billion US$2005/yr)"], "Total Energy costs|Elec|Hydrogen (billion US$2005/yr)"))
  }
  
  ##### Elec|Storage
  cost <- op_costs(ei=NULL,eo=NULL,te=stor,e2e=NULL,teall2rlf=teall2rlf,vm_prodE=NULL,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost + output[regi_on_gdx,,"Energy Investments|Elec|Storage (billion US$2005/yr)"], "Total Energy costs|Elec|Storage (billion US$2005/yr)"))
  
  ##### Elec|Grid
  cost1 <- op_costs(ei=NULL,eo=NULL,te=grid,e2e=NULL,teall2rlf=teall2rlf,vm_prodE=NULL,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  cost2 <- op_costs(ei="seel",eo=se2fe$all_enty1,te=se2fe$all_te,e2e=se2fe,teall2rlf=teall2rlf,vm_prodE=NULL,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost1 + cost2 + output[regi_on_gdx,,"Energy Investments|Elec|Grid (billion US$2005/yr)"], "Total Energy costs|Elec|Grid (billion US$2005/yr)"))
  
  ##### Heat
  #TiA: including investments, and operation and maintenance, BUT excluding input fuel costs
  cost_pe2se <- op_costs(ei=pe2se$all_enty,eo="sehe",te=pe2se$all_te,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  cost_se2fe <- op_costs(ei="sehe",eo=se2fe$all_enty1,te=se2fe$all_te,e2e=se2fe,teall2rlf=teall2rlf,vm_prodE=vm_prodFe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost_pe2se + cost_se2fe + output[regi_on_gdx,,"Energy Investments|Heat (billion US$2005/yr)"], "Total Energy costs|Heat (billion US$2005/yr)"))
  
  ##### Hydrogen
  #TiA: including investments, and operation and maintenance, BUT excluding input fuel costs
  cost_pe2se <- op_costs(ei=pe2se$all_enty,eo="seh2",te=pe2se$all_te,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  cost_se2fe <- op_costs(ei="seh2",eo=se2fe$all_enty1,te=se2fe$all_te,e2e=se2fe,teall2rlf=teall2rlf,vm_prodE=vm_prodFe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost_pe2se + cost_se2fe + output[regi_on_gdx,,"Energy Investments|Hydrogen (billion US$2005/yr)"], "Total Energy costs|Hydrogen (billion US$2005/yr)"))
  
  ##### Hydrogen|Fossil
  cost <- op_costs(ei=petyf,eo="seh2",te=pe2se$all_te,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost + output[regi_on_gdx,,"Energy Investments|Hydrogen|Fossil (billion US$2005/yr)"], "Total Energy costs|Hydrogen|Fossil (billion US$2005/yr)"))
  
  ##### Hydrogen|RE
  cost <- op_costs(ei=perenew,eo="seh2",te=pe2se$all_te,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost + output[regi_on_gdx,,"Energy Investments|Hydrogen|RE (billion US$2005/yr)"], "Total Energy costs|Hydrogen|RE (billion US$2005/yr)"))
  
  ##### Liquids
  #TiA: including investments, and operation and maintenance, BUT excluding input fuel costs
  cost_pe2se <- op_costs(ei=pe2se$all_enty,eo=se_Liq,te=pe2se$all_te,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  cost_se2fe <- op_costs(ei=se_Liq,eo=se2fe$all_enty1,te=se2fe$all_te,e2e=se2fe,teall2rlf=teall2rlf,vm_prodE=vm_prodFe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost_pe2se + cost_se2fe + output[regi_on_gdx,,"Energy Investments|Liquids (billion US$2005/yr)"], "Total Energy costs|Liquids (billion US$2005/yr)"))
  
  ##### Liquids|Oil Ref
  cost <- op_costs(ei="peoil",eo=se_Liq,te=pe2se$all_te,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost + output[regi_on_gdx,,"Energy Investments|Liquids|Oil Ref (billion US$2005/yr)"], "Total Energy costs|Liquids|Oil Ref (billion US$2005/yr)"))
  
  ##### Liquids|Fossil|w/ oil
  cost <- op_costs(ei=petyf,eo=se_Liq,te=pe2se$all_te,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost + output[regi_on_gdx,,"Energy Investments|Liquids|Fossil|w/ oil (billion US$2005/yr)"], "Total Energy costs|Liquids|Fossil|w/ oil (billion US$2005/yr)"))
  
  ##### Liquids|Fossil 
  cost <- op_costs(ei="pecoal",eo=se_Liq,te=pe2se$all_te,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost + output[regi_on_gdx,,"Energy Investments|Liquids|Fossil (billion US$2005/yr)"], "Total Energy costs|Liquids|Fossil (billion US$2005/yr)"))
  
  ##### Liquids|Bio
  cost <- op_costs(ei=perenew,eo=se_Liq,te=pe2se$all_te,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost + output[regi_on_gdx,,"Energy Investments|Liquids|Bio (billion US$2005/yr)"], "Total Energy costs|Liquids|Bio (billion US$2005/yr)"))
  
  ##### CO2 Trans&Stor
  cost <- op_costs(ei=ccs2te$all_enty,eo=ccs2te$all_enty1,te=ccs2te$all_te,e2e=ccs2te,teall2rlf=teall2rlf,vm_prodE=NULL,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost + output[regi_on_gdx,,"Energy Investments|CO2 Trans&Stor (billion US$2005/yr)"], "Total Energy costs|CO2 Trans&Stor (billion US$2005/yr)"))
  
  ##### Other 
  tmp  <- mbind(tmp,setNames(tmp[,,"Total Energy costs (billion US$2005/yr)"]-
                               tmp[,,"Total Energy costs|Electricity (billion US$2005/yr)"]-
                               tmp[,,"Total Energy costs|Hydrogen (billion US$2005/yr)"]-
                               tmp[,,"Total Energy costs|Liquids (billion US$2005/yr)"]-
                               tmp[,,"Total Energy costs|Heat (billion US$2005/yr)"]-
                               tmp[,,"Total Energy costs|CO2 Trans&Stor (billion US$2005/yr)"],
                             "Total Energy costs|Other (billion US$2005/yr)"))
  
  #####################################
  ########## O & M costs ##############
  #####################################
  
  ei=temapall$all_enty
  eo=sety
  te=teall2rlf$all_te
  e2e=temapall
  
  ##### Total
  tmp <- mbind(tmp,setNames(v_costom * 1000, "OandM costs (billion US$2005/yr)"))
  
  ##### CDR
  tmp  <- mbind(tmp,setNames(vm_omcosts_cdr * 1000, "OandM costs CDR (billion US$2005/yr)"))  
  
  #####################################
  ########## Operational costs ########
  #####################################
  
  ##### Fossils
  cost1 <- op_costs(ei="pecoal",eo="seel",te=tenoccs,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  cost2 <- op_costs_part2_foss(pe="pecoal",se="seel",te=tenoccs,vm_prodSe,pe2se,p_dataeta,vm_costfu_ex,vm_fuelex,sm_tdptwyr2dpgj,pm_conv_TWa_EJ)
  tmp   <- mbind(tmp,setNames(cost1+cost2, "Operational costs|Elec|Coal|w/o CCS (billion US$2005/yr)"))
  
  cost1 <- op_costs(ei="pecoal",eo="seel",te=teccs,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  cost2 <- op_costs_part2_foss(pe="pecoal",se="seel",te=teccs,vm_prodSe,pe2se,p_dataeta,vm_costfu_ex,vm_fuelex,sm_tdptwyr2dpgj,pm_conv_TWa_EJ)
  tmp   <- mbind(tmp,setNames(cost1+cost2, "Operational costs|Elec|Coal|w/ CCS (billion US$2005/yr)"))
  
  cost1 <- op_costs(ei="pegas",eo="seel",te=tenoccs,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  cost2 <- op_costs_part2_foss(pe="pegas",se="seel",te=tenoccs,vm_prodSe,pe2se,p_dataeta,vm_costfu_ex,vm_fuelex,sm_tdptwyr2dpgj,pm_conv_TWa_EJ)
  tmp   <- mbind(tmp,setNames(cost1+cost2, "Operational costs|Elec|Gas|w/o CCS (billion US$2005/yr)"))
  
  cost1 <- op_costs(ei="pegas",eo="seel",te=teccs,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  cost2 <- op_costs_part2_foss(pe="pegas",se="seel",te=teccs,vm_prodSe,pe2se,p_dataeta,vm_costfu_ex,vm_fuelex,sm_tdptwyr2dpgj,pm_conv_TWa_EJ)
  tmp   <- mbind(tmp,setNames(cost1+cost2, "Operational costs|Elec|Gas|w/ CCS (billion US$2005/yr)"))
  
  ##### Biomass
  price_pebiolc <- pebal.m[,,"pebiolc"] / (budget.m+1e-10) * 1000 / pm_conv_TWa_EJ
  
  cost1 <- op_costs(ei="pebiolc",eo="seel",te=tenoccs,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  cost2 <- price_pebiolc * op_costs_part2_bio_nuc(pe="pebiolc",se="seel",te=tenoccs,vm_prodSe,pe2se,p_dataeta,pm_conv_TWa_EJ)
  tmp  <- mbind(tmp,setNames(cost1+cost2, "Operational costs|Elec|Biomass|w/o CCS (billion US$2005/yr)"))
  
  cost1 <- op_costs(ei="pebiolc",eo="seel",te=teccs,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  cost2 <- price_pebiolc * op_costs_part2_bio_nuc(pe="pebiolc",se="seel",te=teccs,vm_prodSe,pe2se,p_dataeta,pm_conv_TWa_EJ)
  tmp  <- mbind(tmp,setNames(cost1+cost2, "Operational costs|Elec|Biomass|w/ CCS (billion US$2005/yr)"))
  
  ##### Nuclear 
  cost1 <- op_costs(ei="peur",eo="seel",te="tnrs",e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  cost2 <- op_costs_part2_bio_nuc(pe="peur",se="seel",te="tnrs",vm_prodSe,pe2se,p_dataeta,pm_conv_TWa_EJ)
  tmp  <- mbind(tmp,setNames(cost1*cost2, "Operational costs|Elec|Nuclear (billion US$2005/yr)"))
  
  ##### Renewables
  cost1 <- op_costs(ei=setdiff(perenew,pebio),eo="seel",te=tenoccs,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost1, "Operational costs|Elec|Non-Bio Re (billion US$2005/yr)"))
  
  cost1 <- op_costs(ei="pesol",eo="seel",te=tenoccs,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost1, "Operational costs|Elec|Solar (billion US$2005/yr)"))
  
  cost1 <- op_costs(ei="pewin",eo="seel",te=tenoccs,e2e=pe2se,teall2rlf=teall2rlf,vm_prodE=vm_prodSe,pm_data=pm_data,vm_cap=vm_cap,v_investcost=v_investcost)
  tmp  <- mbind(tmp,setNames(cost1, "Operational costs|Elec|wind (billion US$2005/yr)"))
  
  # add global values
  tmp <- mbind(tmp, dimSums(tmp,dim=1))
  # add other region aggregations
  if (!is.null(regionSubsetList))
    tmp <- mbind(tmp, calc_regionSubset_sums(tmp, regionSubsetList))
  
  return(tmp)
}
pik-piam/remind documentation built on Sept. 9, 2021, 1:09 p.m.