diff --git a/scripts/input/prepare_NDC2018.R b/scripts/input/prepare_NDC2018.R index a1b7e6e1a7764effadde62fd623e898cf2875a54..666602d3486a27e88582c5a6ebdd328026b85781 100644 --- a/scripts/input/prepare_NDC2018.R +++ b/scripts/input/prepare_NDC2018.R @@ -12,41 +12,35 @@ prepare_NDC2018<-function(gdx){ library(lucode,quietly=TRUE,warn.conflicts =FALSE) require(remind,quietly = TRUE,warn.conflicts =FALSE) - ############################# BASIC CONFIGURATION ############################# - - #Define arguments that can be read from command line - # gdx <- "fulldata.gdx" - # readArgs("fulldata.gdx") - - ############################################################################### - if (file.exists(gdx)) { emi <- reportEmi(gdx) } else { stop("No gdx file found - please provide gdx from reference BAU run") } + regs <- setdiff(getRegions(emi),"GLO") p45_BAU_reg_emi_wo_LU_bunkers <- emi[regs,seq(2005,2050,5),"Emi|Kyoto Gases excl Land-Use Change|w/o Bunkers (Mt CO2-equiv/yr)"] - getNames(p45_BAU_reg_emi_wo_LU_bunkers) <- NULL - write.magpie(p45_BAU_reg_emi_wo_LU_bunkers,"modules/45_carbonprice/NDC2018/input/p45_BAU_reg_emi_wo_LU_bunkers.cs4r", comment="** description: Regional GHG emi (excl. LU and bunkers) in BAU scenario \n*** unit: Mt CO2eq/yr \n*** file created with scripts/input/create_BAU_reg_emi_wo_LU_bunkers.R") - # - - shares <- as.magpie(read.csv("modules/45_carbonprice/NDC2018/input/p45_2005share_target.cs4r",skip=4,header = F)) -r2025 <- NULL -r2030 <- NULL - for(r in getRegions(shares)){ - ## depending on which target has the higher share of emissions, assign region to group with either 2025 or 2030 target year - if(as.numeric(shares[r,2025,])>as.numeric(shares[r,2030,])){ - r2025 <- c(r2025,r) - } else { - r2030 <- c(r2030,r) - } - } - -# to be done: add up the shares from both time-steps to the dominant time step and copy the target value to the other time step -# (assuming that the target is similar for the other time step - likely the minor error in comparison to not considering country at all) + getNames(p45_BAU_reg_emi_wo_LU_bunkers) <- NULL + + write.magpie(p45_BAU_reg_emi_wo_LU_bunkers,"modules/45_carbonprice/NDC2018/input/p45_BAU_reg_emi_wo_LU_bunkers.cs4r", comment="** description: Regional GHG emi (excl. LU and bunkers) in BAU scenario \n*** unit: Mt CO2eq/yr \n*** file created with scripts/input/create_BAU_reg_emi_wo_LU_bunkers.R") -write.table(r2025,"modules/45_carbonprice/NDC2018/input/set_regi2025.cs4r", row.names = F,col.names=F,quote = F) -write.table(r2030,"modules/45_carbonprice/NDC2018/input/set_regi2030.cs4r", row.names = F,col.names=F,quote = F) + shares <- as.magpie(read.csv("modules/45_carbonprice/NDC2018/input/p45_2005share_target.cs4r",skip=4,header = F)) + r2025 <- NULL + r2030 <- NULL + + for(r in getRegions(shares)){ + ## depending on which target has the higher share of emissions, assign region to group with either 2025 or 2030 target year + if(as.numeric(shares[r,2025,])>as.numeric(shares[r,2030,])){ + r2025 <- c(r2025,r) + } else { + r2030 <- c(r2030,r) + } + } + + # to be done: add up the shares from both time-steps to the dominant time step and copy the target value to the other time step + # (assuming that the target is similar for the other time step - likely the minor error in comparison to not considering country at all) + + write.table(r2025,"modules/45_carbonprice/NDC2018/input/set_regi2025.cs4r", row.names = F,col.names=F,quote = F) + write.table(r2030,"modules/45_carbonprice/NDC2018/input/set_regi2030.cs4r", row.names = F,col.names=F,quote = F) } diff --git a/scripts/start/prepare_and_run.R b/scripts/start/prepare_and_run.R index 93e4f5c8f8d3980d44201454090d562780f2d3d7..62e2ce5f70df88dd36021758341389e72e49ecf0 100644 --- a/scripts/start/prepare_and_run.R +++ b/scripts/start/prepare_and_run.R @@ -176,24 +176,12 @@ prepare_and_run <- function() { # Check configuration for consistency cfg <- check_config(cfg, reference_file="config/default.cfg", settings_config = "config/settings_config.csv") - - ###-------- do update of input files based on previous runs if applicable ------### - if(!is.null(cfg$gms$carbonprice) && (cfg$gms$carbonprice == "NDC2018")){ - source("scripts/input/prepare_NDC2018.R") - prepare_NDC2018(as.character(cfg$files2export$start["input_ref.gdx"])) - } - ## the following is outcommented because by now it has to be done by hand ( currently only one gdx is handed to the next run, so it is impossible to fix to one run and use the tax from another run) - ## Update CO2 tax information for exogenous carbon price runs with the same CO2 price as a previous run - #if(!is.null(cfg$gms$carbonprice) && (cfg$gms$carbonprice == "ExogSameAsPrevious")){ - # source("scripts/input/create_ExogSameAsPrevious_CO2price_file.R") - # create_ExogSameAsPrevious_CO2price_file(as.character(cfg$files2export$start["input_ref.gdx"])) - #} - - #AJS + # Check for compatibility with subsidizeLearning if ( (cfg$gms$optimization != 'nash') & (cfg$gms$subsidizeLearning == 'globallyOptimal') ) { cat("Only optimization='nash' is compatible with subsudizeLearning='globallyOptimal'. Switching subsidizeLearning to 'off' now. \n") cfg$gms$subsidizeLearning = 'off' } + # reportCEScalib only works with the calibrate module if ( cfg$gms$CES_parameters != "calibrate" ) cfg$output <- setdiff(cfg$output,"reportCEScalib") @@ -202,21 +190,6 @@ prepare_and_run <- function() { stop("This title is too long or the name contains dots - GAMS would not tolerate this, and quit working at a point where you least expect it. Stopping now. ") } - # Calculate CES configuration string - cfg$gms$cm_CES_configuration <- paste0("stat_",cfg$gms$stationary,"-", - "indu_",cfg$gms$industry,"-", - "buil_",cfg$gms$buildings,"-", - "tran_",cfg$gms$transport,"-", - "POP_", cfg$gms$cm_POPscen, "-", - "GDP_", cfg$gms$cm_GDPscen, "-", - "Kap_", cfg$gms$capitalMarket, "-", - "Reg_", substr(regionscode(cfg$regionmapping),1,10)) - - # write name of corresponding CES file to datainput.gms - replace_in_file(file = "./modules/29_CES_parameters/load/datainput.gms", - content = paste0('$include "./modules/29_CES_parameters/load/input/',cfg$gms$cm_CES_configuration,'.inc"'), - subject = "CES INPUT") - # adjust GDPpcScen based on GDPscen cfg$gms$c_GDPpcScen <- gsub("gdp_","",cfg$gms$cm_GDPscen) @@ -228,7 +201,7 @@ prepare_and_run <- function() { system("find ./core/magicc/ -type f | xargs dos2unix -q") # Set source_include so that loaded scripts know they are included as - # source (instead a load from command line) + # source (instead of being executed from the command line) source_include <- TRUE ################## M O D E L L O C K ################################### @@ -237,8 +210,40 @@ prepare_and_run <- function() { on.exit(model_unlock(lock_id, oncluster=on_cluster)) ################## M O D E L L O C K ################################### - # If report and scenname are supplied the data of this scenario in the report will be converted to REMIND input - # Used for REMIND-MAgPIE coupling + ########################################################### + ### PROCESSING INPUT DATA ###################### START #### + ########################################################### + + # update input files based on previous runs if applicable + # ATTENTION: modifying gms files + if(!is.null(cfg$gms$carbonprice) && (cfg$gms$carbonprice == "NDC2018")){ + source("scripts/input/prepare_NDC2018.R") + prepare_NDC2018(as.character(cfg$files2export$start["input_ref.gdx"])) + } + ## the following is outcommented because by now it has to be done by hand ( currently only one gdx is handed to the next run, so it is impossible to fix to one run and use the tax from another run) + ## Update CO2 tax information for exogenous carbon price runs with the same CO2 price as a previous run + #if(!is.null(cfg$gms$carbonprice) && (cfg$gms$carbonprice == "ExogSameAsPrevious")){ + # source("scripts/input/create_ExogSameAsPrevious_CO2price_file.R") + # create_ExogSameAsPrevious_CO2price_file(as.character(cfg$files2export$start["input_ref.gdx"])) + #} + + # Calculate CES configuration string + cfg$gms$cm_CES_configuration <- paste0("stat_",cfg$gms$stationary,"-", + "indu_",cfg$gms$industry,"-", + "buil_",cfg$gms$buildings,"-", + "tran_",cfg$gms$transport,"-", + "POP_", cfg$gms$cm_POPscen, "-", + "GDP_", cfg$gms$cm_GDPscen, "-", + "Kap_", cfg$gms$capitalMarket, "-", + "Reg_", substr(regionscode(cfg$regionmapping),1,10)) + + # write name of corresponding CES file to datainput.gms + replace_in_file(file = "./modules/29_CES_parameters/load/datainput.gms", + content = paste0('$include "./modules/29_CES_parameters/load/input/',cfg$gms$cm_CES_configuration,'.inc"'), + subject = "CES INPUT") + + # If a path to a MAgPIE report is supplied use it as REMIND intput (used for REMIND-MAgPIE coupling) + # ATTENTION: modifying gms files if (!is.null(cfg$pathToMagpieReport)) { getReportData(path_to_report = cfg$pathToMagpieReport,inputpath_mag=cfg$gms$biomass,inputpath_acc=cfg$gms$agCosts) } @@ -255,12 +260,7 @@ prepare_and_run <- function() { if(is.null(cfg$model)) cfg$model <- "main.gms" manipulateConfig(cfg$model, cfg$gms) - ########################################################################################################### - ############# PROCESSING INPUT DATA ###################### START ########################################## - ########################################################################################################### - - - ########## declare functions for updating information ################ + ######## declare functions for updating information #### update_info <- function(regionscode,revision) { subject <- 'VERSION INFO' @@ -324,7 +324,7 @@ prepare_and_run <- function() { replace_in_file('core/sets.gms',content,"SETS",comment="***") } - ############### download and distribute input data ################### + ############ download and distribute input data ######## # check wheather the regional resolution and input data revision are outdated and update data if needed if(file.exists("input/source_files.log")) { input_old <- readLines("input/source_files.log")[1] @@ -341,7 +341,7 @@ prepare_and_run <- function() { debug = FALSE) } - ##################### update information ############################# + ############ update information ######################## # update_info, which regional resolution and input data revision in cfg$model update_info(regionscode(cfg$regionmapping),cfg$revision) # update_sets, which is updating the region-depending sets in core/sets.gms @@ -349,11 +349,11 @@ prepare_and_run <- function() { map <- read.csv(cfg$regionmapping,sep=";") update_sets(map) - ########################################################################################################### - ############# PROCESSING INPUT DATA ###################### END ############################################ - ########################################################################################################### + ######################################################## + ### PROCESSING INPUT DATA ###################### END ### + ######################################################## - ############# ADD MODULE INFO IN SETS ###################### START ####################################### + ### ADD MODULE INFO IN SETS ############# START ####### content <- NULL modification_warning <- c( '*** THIS CODE IS CREATED AUTOMATICALLY, DO NOT MODIFY THESE LINES DIRECTLY', @@ -366,7 +366,7 @@ prepare_and_run <- function() { content <- c(content,paste0(" ",getModules("modules/")[,"name"]," . %",getModules("modules/")[,"name"],"%")) content <- c(content,' /',';') replace_in_file('core/sets.gms',content,"MODULES",comment="***") - ############# ADD MODULE INFO IN SETS ###################### END ######################################### + ### ADD MODULE INFO IN SETS ############# END ######### # choose which conopt files to copy cfg$files2export$start <- sub("conopt3",cfg$gms$cm_conoptv,cfg$files2export$start) @@ -400,9 +400,6 @@ prepare_and_run <- function() { ################## M O D E L U N L O C K ################################### setwd(cfg$results_folder) - ########################################################################################################################################################### - #################################################### Copied from submit.R ################################################################################# - ########################################################################################################################################################### # Function to create the levs.gms, fixings.gms, and margs.gms files, used in # delay scenarios. @@ -797,10 +794,6 @@ prepare_and_run <- function() { outputdir <- cfg$results_folder sys.source("output.R",envir=new.env()) - ########################################################################################################################################################### - ########################################################################################################################################################### - ########################################################################################################################################################### - end_time <- Sys.time() # Save run statistics to local file cat("Saving start_time and end_time to runstatistics.rda\n")