diff --git a/config/input.gdx b/config/input.gdx index 35cae3b04c464265a1524103d6d1e3ed58e563e4..93c41b8f9af51dd42bfe1ba1f4dfe29f69dad341 100644 Binary files a/config/input.gdx and b/config/input.gdx differ diff --git a/output.R b/output.R index 04a88c08173d4f1fd8b01b38e5e599943667d8e6..22448342e613d9b5bcde3290a964cc2afcd86f67 100644 --- a/output.R +++ b/output.R @@ -22,8 +22,12 @@ library(lucode) ###Define arguments that can be read from command line if(!exists("source_include")) { + # if this script is not being sourced by another script but called from the command line via Rscript read the command line arguments and let the user choose the slurm options readArgs("outputdir","output","comp","remind_dir") -} + #source("scripts/utils/choose_slurmConfig.R") + #slurmConfig <- choose_slurmConfig() +} + #Setting relevant paths if(file.exists('/iplex/01/landuse')) { #run is performed on the cluster pythonpath <- '/iplex/01/landuse/bin/python/bin/' @@ -92,7 +96,6 @@ choose_folder <- function(folder,title="Please choose a folder") { } else return(dirs[identifier]) } - choose_module <- function(Rfolder,title="Please choose an outputmodule") { module <- gsub("\\.R$","",grep("\\.R$",list.files(Rfolder), value=TRUE)) cat("\n\n",title,":\n\n") @@ -186,6 +189,16 @@ if (comp==TRUE) { } } } else outputdirs <- outputdir + + # define slurm class or direct execution + if(!exists("source_include")) { + # if this script is not being sourced by another script but called from the command line via Rscript let the user choose the slurm options + source("scripts/start/choose_slurmConfig.R") + slurmConfig <- choose_slurmConfig() + } else { + # if this script is being sourced by another script exectue the output scripts directly without sending them to the cluster + slurmConfig <- "direct" + } #Execute outputscripts for all choosen folders for (outputdir in outputdirs) { @@ -244,18 +257,27 @@ if (comp==TRUE) { for(rout in output){ name<-paste(rout,".R",sep="") if(file.exists(paste0("scripts/output/single/",name))){ - print(paste("Executing",name)) - tmp.env <- new.env() - tmp.error <- try(sys.source(paste0("scripts/output/single/",name),envir=tmp.env)) -# rm(list=ls(tmp.env),envir=tmp.env) - rm(tmp.env) - gc() - if(!is.null(tmp.error)) warning("Script ",name," was stopped by an error and not executed properly!") + if (slurmConfig == "direct") { + # execute output script directly (without sending it to slurm) + print(paste("Executing",name)) + tmp.env <- new.env() + tmp.error <- try(sys.source(paste0("scripts/output/single/",name),envir=tmp.env)) + # rm(list=ls(tmp.env),envir=tmp.env) + rm(tmp.env) + gc() + if(!is.null(tmp.error)) warning("Script ",name," was stopped by an error and not executed properly!") + } else { + # send the output script to slurm + slurmcmd <- paste0("sbatch ",slurmConfig," --job-name=",outputdir," --output=",outputdir,".txt --mail-type=END --comment=REMIND --wrap=\"Rscript scripts/output/single/",rout,".R outputdir=",outputdir,"\"") + cat("Sending to slurm: ",name,"\n") + system(slurmcmd) + Sys.sleep(1) + } } } # finished cat(paste("\nFinished output generation for",outputdir,"!\n\n")) rm(source_include) - print(warnings()) + if(!is.null(warnings())) print(warnings()) } } diff --git a/scripts/start/choose_slurmConfig.R b/scripts/start/choose_slurmConfig.R index f384c4eab22e2ec6f5db7d96b196250f216e8829..d4e099efaa8e8272b733a7d794c02c1fbe3ff330 100644 --- a/scripts/start/choose_slurmConfig.R +++ b/scripts/start/choose_slurmConfig.R @@ -22,14 +22,14 @@ choose_slurmConfig <- function() { modes <- c(" SLURM standby - task per node: 12 (nash H12) [recommended]", " SLURM standby - task per node: 13 (nash H12 coupled)", " SLURM standby - task per node: 16 (nash H12+)", - " SLURM standby - task per node: 1 (nash debug, test one regi)", + " SLURM standby - task per node: 1 (nash debug, testOneRegi, reporting)", " SLURM priority - task per node: 12 (nash H12) [recommended]", " SLURM priority - task per node: 13 (nash H12 coupled)", " SLURM priority - task per node: 16 (nash H12+)", - " SLURM priority - task per node: 1 (nash debug, test one regi)", + " SLURM priority - task per node: 1 (nash debug, testOneRegi, reporting)", " SLURM short - task per node: 12 (nash H12)", "SLURM short - task per node: 16 (nash H12+)", - "SLURM short - task per node: 1 (nash debug, test one regi)", + "SLURM short - task per node: 1 (nash debug, testOneRegi, reporting)", "SLURM medium - task per node: 1 (negishi)", "SLURM long - task per node: 1 (negishi)") diff --git a/scripts/start/prepare_and_run.R b/scripts/start/prepare_and_run.R index a1346dab09f3c020da84b80c2dc0dc83bbbf683b..320e62edbbf20852534062afda6aafdf8f283e12 100644 --- a/scripts/start/prepare_and_run.R +++ b/scripts/start/prepare_and_run.R @@ -765,7 +765,7 @@ prepare_and_run <- function() { # In case there are no subsequent runs (or it's coupled runs), the file contains only # a small message. - subseq_start_file <- paste0(cfg$results_folder,"/start_subsequentruns.R") + subseq_start_file <- paste0(cfg$results_folder,"/start_subsequentruns_manually.R") if(no_subsequent_runs){ write("cat('\nNo subsequent run was set for this scenario\n')",file=subseq_start_file) @@ -776,6 +776,7 @@ prepare_and_run <- function() { for(run in seq(1,length(cfg$subsequentruns))){ filetext <- paste0(filetext,"\n") filetext <- paste0(filetext,"load('",cfg$subsequentruns[run],".RData')\n") + #filetext <- paste0(filetext,"cfg$results_folder <- 'output/:title::date:'\n") filetext <- paste0(filetext,"cat('",cfg$subsequentruns[run],"')\n") filetext <- paste0(filetext,"submit(cfg)\n") } diff --git a/scripts/start/submit.R b/scripts/start/submit.R index 25d3db720391d8b7b0cd6fcfb76c63975127e605..ccc81c54aa367247828ef357ae059a3ec0cb8b2f 100644 --- a/scripts/start/submit.R +++ b/scripts/start/submit.R @@ -15,42 +15,44 @@ submit <- function(cfg) { - # Create name of output folder and output folder itself + # Generate name of output folder and create the folder date <- format(Sys.time(), "_%Y-%m-%d_%H.%M.%S") cfg$results_folder <- gsub(":date:", date, cfg$results_folder, fixed = TRUE) cfg$results_folder <- gsub(":title:", cfg$title, cfg$results_folder, fixed = TRUE) # Create output folder + cat(" Creating results folder",cfg$results_folder,"\n") if (!file.exists(cfg$results_folder)) { dir.create(cfg$results_folder, recursive = TRUE, showWarnings = FALSE) } else if (!cfg$force_replace) { stop(paste0("Results folder ",cfg$results_folder," could not be created because it already exists.")) } else { - cat("Deleting results folder because it alreay exists:",cfg$results_folder,"\n") + cat(" Deleting results folder because it alreay exists:",cfg$results_folder,"\n") unlink(cfg$results_folder, recursive = TRUE) dir.create(cfg$results_folder, recursive = TRUE, showWarnings = FALSE) } - # save main folder + # remember main folder cfg$remind_folder <- getwd() - - # save the cfg data before moving it into the results folder - cat("Writing cfg to file\n") - save(cfg,file=paste0(cfg$title,".RData")) + + # Save the cfg (with the updated name of the result folder) into the results folder. + # Do not save the new name of the results folder to the .RData file in REMINDs main folder, because it + # might be needed to restart subsequent runs manually and should not contain the time stamp in this case. + filename <- paste0(cfg$results_folder,"/config.Rdata") + cat(" Writing cfg to file",filename,"\n") + save(cfg,file=filename) # Copy files required to confiugre and start a run - filelist <- c("config.Rdata" = paste0(cfg$title,".RData"), - "prepare_and_run.R" = "scripts/start/prepare_and_run.R") + filelist <- c("prepare_and_run.R" = "scripts/start/prepare_and_run.R") .copy.fromlist(filelist,cfg$results_folder) - # remove config in main folder (after copying into results folder) - file.remove(paste0(cfg$title,".RData")) + # Do not remove .RData files from REMIND main folder because they are needed in case you need to manually restart subsequent runs. - # change to run folder + # Change to run folder setwd(cfg$results_folder) on.exit(setwd(cfg$remind_folder)) - + # send prepare_and_run.R to cluster - cat("Executing prepare_and_run.R for",cfg$title,"\n") + cat(" Executing prepare_and_run.R for",cfg$results_folder,"\n") if(cfg$slurmConfig=="direct") { log <- format(Sys.time(), paste0(cfg$title,"-%Y-%H-%M-%S-%OS3.log")) system("Rscript prepare_and_run.R") diff --git a/start.R b/start.R index cc4cfc2b99c577b0c87960ccd7b7c1c1b0b42b5a..854bb33a8d494fed43681241f62a2f9e16f5d88a 100644 --- a/start.R +++ b/start.R @@ -15,7 +15,7 @@ configure_cfg <- function(icfg, iscen, iscenarios, isettings) { # Edit run title icfg$title <- iscen - cat("\n", iscen, "\n") + cat(" Configuring cfg for", iscen,"\n") # Edit main file of model if( "model" %in% names(iscenarios)){ @@ -106,7 +106,7 @@ if ('--testOneRegi' %in% argv) { } if (!is.na(config.file)) { - cat(paste("reading config file", config.file, "\n")) + cat(paste("\nReading config file", config.file, "\n")) # Read-in the switches table, use first column as row names settings <- read.csv2(config.file, stringsAsFactors = FALSE, row.names = 1, comment.char = "#", na.strings = "") @@ -142,22 +142,28 @@ for (scen in rownames(scenarios)) { cfg$force_replace <- TRUE } + cat("\n",scen,"\n") + # configure cfg based on settings from csv if provided if (!is.na(config.file)) { - - cfg <- configure_cfg(cfg, scen, scenarios, settings) - - # Directly start runs that have a gdx file location given as path_gdx_ref or where this field is empty - start_now <- (substr(scenarios[scen,"path_gdx_ref"], nchar(scenarios[scen,"path_gdx_ref"])-3, nchar(scenarios[scen,"path_gdx_ref"])) == ".gdx" - | is.na(scenarios[scen,"path_gdx_ref"])) + cfg <- configure_cfg(cfg, scen, scenarios, settings) + # Directly start runs that have a gdx file location given as path_gdx_ref or where this field is empty + start_now <- (substr(scenarios[scen,"path_gdx_ref"], nchar(scenarios[scen,"path_gdx_ref"])-3, nchar(scenarios[scen,"path_gdx_ref"])) == ".gdx" + | is.na(scenarios[scen,"path_gdx_ref"])) } - + # save the cfg data for later start of subsequent runs (after preceding run finished) - cat("Writing cfg to file\n") - save(cfg,file=paste0(scen,".RData")) + filename <- paste0(scen,".RData") + cat(" Writing cfg to file",filename,"\n") + save(cfg,file=filename) if (start_now){ - cat("Creating and starting: ",cfg$title,"\n") - submit(cfg) - } + # Create results folder and start run + submit(cfg) + } else { + cat(" Waiting for", scenarios[scen,'path_gdx_ref'] ,"\n") + } + + if (!identical(cfg$subsequentruns,character(0))) cat(" Subsequent runs:",cfg$subsequentruns,"\n") + } diff --git a/start_bundle_coupled.R b/start_bundle_coupled.R index b6ffed3bd70592288b61eae4fd1063cb6b2dec68..9af2e4150e7ecdac4c9e8e82f5af3b2200ca1def 100644 --- a/start_bundle_coupled.R +++ b/start_bundle_coupled.R @@ -15,14 +15,14 @@ path_magpie <- "/p/projects/piam/runs/coupled-magpie/" # If there are existing runs you would like to take the gdxes (REMIND) or reportings (REMIND or MAgPIE) from provide the path and name prefix here. # Note: the sceanrio names have to be identical to the runs that are to be started. If they differ please provide the names of the old scenarios in the # file that you read in below to path_settings_coupled -path_remind_oldruns <- paste0(path_remind,"output-gams26/") -path_magpie_oldruns <- paste0(path_magpie,"output-gams26/") +path_remind_oldruns <- paste0(path_remind,"output-20200129/") +path_magpie_oldruns <- paste0(path_magpie,"output-20200129/") # The scripts automatically adds a prefix (name of your remind path) to the scenario names. This is useful because it enables # using the same MAgPIE and REMIND output folders to store results of coupled runs from multiple REMIND revisions (prevents double names) # If you want the script to find gdxs or reports of older runs as starting point for new runs please # provide the prefix of the old run names so the script can find them. -prefix_oldruns <- "coupled-remind_" # "REMIND_" # "coupled-remind_" # +prefix_oldruns <- "C_" # "REMIND_" # "coupled-remind_" # # Paths to the files where scenarios are defined # path_settings_remind contains the detailed configuration of the REMIND scenarios @@ -302,7 +302,7 @@ for(scen in common){ } if (start_now){ - if (!exists("test")) system(paste0("sbatch --qos=standby --job-name=",runname," --output=",runname,".log --mail-type=END --comment=REMIND-MAgPIE --tasks-per-node=",nr_of_regions," --wrap=\"Rscript start_coupled.R coupled_config=",runname,".RData\"")) + if (!exists("test")) system(paste0("sbatch --qos=priority --job-name=",runname," --output=",runname,".log --mail-type=END --comment=REMIND-MAgPIE --tasks-per-node=",nr_of_regions," --wrap=\"Rscript start_coupled.R coupled_config=",runname,".RData\"")) else cat("Test mode: run NOT submitted to the cluster\n") } else { cat(paste0("Run ",runname," will start after preceding run ",prefix_runname,settings_remind[scen,"path_gdx_ref"]," has finished\n")) diff --git a/start_coupled.R b/start_coupled.R index 2a918eff313bd307894c9daa489299d621be87c0..1d74e97e25a9deb038ab3650fbc1a1424e78c023 100644 --- a/start_coupled.R +++ b/start_coupled.R @@ -171,15 +171,9 @@ start_coupled <- function(path_remind,path_magpie,cfg_rem,cfg_mag,runname,max_it cfg_mag$files2export$start <- .setgdxcopy(".gdx",cfg_mag$files2export$start,gdxlist) } - # delete "+" and "++" from variable names - rep <- read.report(report) - names(rep[[1]]) <- "MAgPIE" - rep[[1]][[1]] <- deletePlus(report[[1]][[1]]) - #write.report(rep,"report.mif") - sceninreport <- names(rep) # report must only contain ONE scenario - cat("### COUPLING ### MAgPIE will be startet with\n Report = ",report,"\n Scenario used as input = ",sceninreport,"\n Folder=",cfg_mag$results_folder,"\n") + cat("### COUPLING ### MAgPIE will be startet with\n Report = ",report,"\n Folder=",cfg_mag$results_folder,"\n") ########### START MAGPIE ############# - outfolder_mag <- start_run(cfg_mag,report=rep,sceninreport=sceninreport,LU_pricing=LU_pricing,codeCheck=FALSE) + outfolder_mag <- start_run(cfg_mag,path_to_report=report,LU_pricing=LU_pricing,codeCheck=FALSE) ###################################### cat("### COUPLING ### MAgPIE output was stored in ",outfolder_mag,"\n") report <- paste0(path_magpie,outfolder_mag,"/report.mif") @@ -215,7 +209,7 @@ start_coupled <- function(path_remind,path_magpie,cfg_rem,cfg_mag,runname,max_it #start subsequent runs via sbatch for(run in cfg_rem$subsequentruns){ cat("Submitting subsequent run",run,"\n") - system(paste0("sbatch --qos=standby --job-name=",run," --output=",run,".log --mail-type=END --comment=REMIND-MAgPIE --tasks-per-node=",nr_of_regions," --wrap=\"Rscript start_coupled.R coupled_config=C_",run,".RData\"")) + system(paste0("sbatch --qos=priority --job-name=C_",run," --output=C_",run,".log --mail-type=END --comment=REMIND-MAgPIE --tasks-per-node=",nr_of_regions," --wrap=\"Rscript start_coupled.R coupled_config=C_",run,".RData\"")) } # Read runtime of ALL coupled runs (not just the current scenario) and produce comparison pdf