Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

don't write rdata in test mode, improve start scripts #1500

Merged
merged 3 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
[[#1414](https://github.com/remindmodel/remind/pull/1414)]
- correctly report `Tech|*|Capital Costs|w/ Adj Costs` for t < cm_startyear
[[#1429](https://github.com/remindmodel/remind/pull/1429), [#1476](https://github.com/remindmodel/remind/pull/1476)]
- **scripts** '--test' mode for start.R and start_bundle_coupled.R does not write RData files anymore
[[#1500](https://github.com/remindmodel/remind/pull/1500)]
- prevent tradtional biomass spillover to other sectors than buildings
[[#1519](https://github.com/remindmodel/remind/pull/1519)]

Expand Down
3 changes: 2 additions & 1 deletion scripts/start/readCheckScenarioConfig.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ readCheckScenarioConfig <- function(filename, remindPath = ".", testmode = FALSE
BAUbutNotNeeded <- ! is.na(scenConf$path_gdx_bau) & ! (scenNeedsBau)
if (sum(BAUbutNotNeeded) > 0) {
msg <- paste0("In ", sum(BAUbutNotNeeded), " scenarios, 'path_gdx_bau' is not empty although no realization is selected that needs it.\n",
"To avoid unnecessary dependencies to other runs, setting 'path_gdx_bau' to NA for:\n",
"To avoid unnecessary dependencies to other runs, automatically setting 'path_gdx_bau' to NA for:\n",
paste(rownames(scenConf)[BAUbutNotNeeded], collapse = ", "))
message(msg)
scenConf$path_gdx_bau[BAUbutNotNeeded] <- NA
Expand Down Expand Up @@ -132,6 +132,7 @@ readCheckScenarioConfig <- function(filename, remindPath = ".", testmode = FALSE
}
unknownColumnNames <- names(scenConf)[! names(scenConf) %in% knownColumnNames]
if (length(unknownColumnNames) > 0) {
message("")
forbiddenColumnNames <- list( # specify forbidden column name and what should be done with it
"c_budgetCO2" = "Rename to c_budgetCO2from2020, adapt emission budgets, see https://github.com/remindmodel/remind/pull/640",
"c_budgetCO2FFI" = "Rename to c_budgetCO2from2020FFI, adapt emission budgets, see https://github.com/remindmodel/remind/pull/640",
Expand Down
23 changes: 11 additions & 12 deletions start.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ helpText <- "
#' the full model.
#' --reprepare, -R: rewrite full.gms and restart run
#' --restart, -r: interactively restart run(s)
#' --test, -t: test scenario configuration and writing the RData files
#' in the REMIND main folder without starting the runs
#' --test, -t: test scenario configuration without starting the runs
#' --testOneRegi, -1: starting the REMIND run(s) in testOneRegi mode
#' startgroup=MYGROUP when reading a scenario config .csv file, don't start
#' everything specified by \"start = 1\", instead start everything
Expand Down Expand Up @@ -74,15 +73,13 @@ if(!exists("argv")) argv <- commandArgs(trailingOnly = TRUE)
argv <- argv[! grepl("^-", argv) & ! grepl("=", argv)]
# check if user provided any unknown arguments or config files that do not exist
if (length(argv) == 1) {
if (file.exists(argv)) {
config.file <- argv
} else if (file.exists(file.path("config", argv))) {
config.file <- file.path("config", argv)
} else {
stop("Unknown parameter provided: ", paste(argv, collapse = ", "))
}
config.file <- argv
if (! file.exists(config.file)) config.file <- file.path("config", argv)
if (! file.exists(config.file)) config.file <- file.path("config", paste0("scenario_config_", argv, ".csv"))
if (! file.exists(config.file)) stop("Unknown parameter provided: ", paste(argv, collapse = ", "))
} else if (length(argv) > 1) {
stop("You provided more than one file or other command line argument, start.R can only handle one.")
stop("You provided more than one file or other command line argument, start.R can only handle one: ",
paste(argv, collapse = ", "))
}

if ("--help" %in% flags) {
Expand Down Expand Up @@ -333,7 +330,7 @@ if (any(c("--reprepare", "--restart") %in% flags)) {
}

# save the cfg object for the later automatic start of subsequent runs (after preceding run finished)
if (! "--gamscompile" %in% flags) {
if (! any(c("--test", "--gamscompile") %in% flags)) {
filename <- paste0(cfg$title,".RData")
message(" Writing cfg to file ", filename)
save(cfg, file=filename)
Expand Down Expand Up @@ -366,12 +363,14 @@ if (any(c("--reprepare", "--restart") %in% flags)) {
if (exists("lockID")) gms::model_unlock(lockID)
}

warnings()

message("\nFinished: ", startedRuns, " runs started. ", waitingRuns, " runs are waiting. ",
if (modeltestRunsUsed > 0) paste0(modeltestRunsUsed, " GDX files from modeltests selected."))
if ("--gamscompile" %in% flags) {
message("To investigate potential FAILs, run: less -j 4 --pattern='^\\*\\*\\*\\*' filename.lst")
} else if ("--test" %in% flags) {
message("You are in --test mode: Rdata files were written, but no runs were started. ", errorsfound, " errors were identified.")
message("You are in --test mode: no runs were started. ", errorsfound, " errors were identified.")
} else if (model_was_locked & (! "--restart" %in% flags | "--reprepare" %in% flags)) {
message("The model was locked before runs were started, so they will have to queue.")
}
Expand Down
79 changes: 41 additions & 38 deletions start_bundle_coupled.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,17 @@ helpText <- "
#'
#' Control the script's behavior by providing additional arguments:
#'
#' --help, -h: show this help text and exit
#' --gamscompile, -g: compile gms of all selected runs. Combined with
#' --interactive, it stops in case of compilation
#' errors, allowing to fix them and rerun gamscompile
#' --interactive, -i: interactively select run(s) to be started. Asks for
#' config file also if the one specified as
#' path_settings_coupled cannot be found.
#' --test, -t: Test scenario configuration and write the RData files
#' in the REMIND main folder without starting the runs.
#' --help, -h: show this help text and exit
#' --gamscompile, -g: compile gms of all selected runs. Combined with
#' --interactive, it stops in case of compilation
#' errors, allowing to fix them and rerun gamscompile
#' --interactive, -i: interactively select run(s) to be started. Asks for
#' config file also if the one specified as
#' path_settings_coupled cannot be found.
#' --test, -t: Test scenario configuration without starting the runs.
#' startgroup=MYGROUP when reading a scenario config .csv file, don't start
#' everything specified by \"start = 1\", instead start everything
#' specified by \"start = MYGROUP\"
#' everything specified by \"start = 1\", instead start
#' everything specified by \"start = MYGROUP\"
"

########################################################################################################
Expand Down Expand Up @@ -523,7 +522,7 @@ for(scen in common){
# runs by hand after the NDC has finished.
}
if (i == start_iter_first && ! start_now && all(file.exists(cfg_rem$files2export$start[path_gdx_list]) | unlist(gdx_na))) {
start_now <- TRUE
start_now <- TRUE
}
foldername <- file.path("output", fullrunname)
if ((i > start_iter_first || !scenarios_coupled[scen, "start_magpie"]) && file.exists(foldername)) {
Expand Down Expand Up @@ -551,11 +550,15 @@ for(scen in common){
errorsfound <- errorsfound + cfg_rem$errorsfoundInCheckFixCfg
}

Rdatafile <- paste0(fullrunname, ".RData")
message("Save settings to ", Rdatafile)
save(path_remind, path_magpie, cfg_rem, cfg_mag, runname, fullrunname, max_iterations, start_iter,
n600_iterations, path_report, qos, sbatch, prefix_runname, run_compareScenarios, magpie_empty,
numberOfTasks, start_now, file = Rdatafile)
if (! "--test" %in% flags) {
Rdatafile <- paste0(fullrunname, ".RData")
message("Save settings to ", Rdatafile)
save(path_remind, path_magpie, cfg_rem, cfg_mag, runname, fullrunname, max_iterations, start_iter,
n600_iterations, path_report, qos, sbatch, prefix_runname, run_compareScenarios, magpie_empty,
numberOfTasks, start_now, file = Rdatafile)
} else if (start_now) {
startedRuns <- c(startedRuns, fullrunname)
}

} # end for (i %in% iterations)

Expand Down Expand Up @@ -604,12 +607,16 @@ for(scen in common){
# start runs
message("\nStarting Runs")
for (scen in common) {
if (!scenarios_coupled[scen, "start_scenario"]) {
if (! scenarios_coupled[scen, "start_scenario"]) {
next
}
start_iter_first <- scenarios_coupled[scen, "start_iter_first"]
runname <- paste0(prefix_runname, scen)
fullrunname <- paste0(runname, "-rem-", start_iter_first)
if ("--test" %in% flags || "--gamscompile" %in% flags) {
message("Test mode: run ", fullrunname, " NOT submitted to the cluster.")
next
}
Rdatafile <- paste0(fullrunname, ".RData")
runEnv <- new.env()
load(Rdatafile, envir = runEnv)
Expand All @@ -623,26 +630,22 @@ for (scen in common) {
runEnv$qos <- gsub("^(medium|standby)$", "auto", runEnv$qos)
runEnv$numberOfTasks <- 3
}
if ("--test" %in% flags || "--gamscompile" %in% flags) {
message("Test mode: run ", fullrunname, " NOT submitted to the cluster.")
} else {
logfile <- file.path("output", fullrunname, paste0("log", if (scenarios_coupled[scen, "start_magpie"]) "-mag", ".txt"))
if (! file.exists(dirname(logfile))) dir.create(dirname(logfile))
message("Find logging in ", logfile)
if (isTRUE(runEnv$qos == "auto")) {
sq <- system(paste0("squeue -u ", Sys.info()[["user"]], " -o '%q %j'"), intern = TRUE)
runEnv$qos <- if (is.null(attr(sq, "status")) && sum(grepl("^priority ", sq)) < 4) "priority" else "short"
}
slurmOptions <- combine_slurmConfig(paste0("--qos=", runEnv$qos, " --job-name=", fullrunname, " --output=", logfile,
" --open-mode=append --mail-type=END --comment=REMIND-MAgPIE --tasks-per-node=", runEnv$numberOfTasks,
if (runEnv$numberOfTasks == 1) " --mem=8000"), runEnv$sbatch)
slurmCommand <- paste0("sbatch ", slurmOptions, " --wrap=\"Rscript start_coupled.R coupled_config=", Rdatafile, "\"")
message(slurmCommand)
exitCode <- system(slurmCommand)
if (0 < exitCode) {
errorsfound <- errorsfound + 1
message("sbatch command failed, check logs.")
}
logfile <- file.path("output", fullrunname, paste0("log", if (scenarios_coupled[scen, "start_magpie"]) "-mag", ".txt"))
if (! file.exists(dirname(logfile))) dir.create(dirname(logfile))
message("Find logging in ", logfile)
if (isTRUE(runEnv$qos == "auto")) {
sq <- system(paste0("squeue -u ", Sys.info()[["user"]], " -o '%q %j'"), intern = TRUE)
runEnv$qos <- if (is.null(attr(sq, "status")) && sum(grepl("^priority ", sq)) < 4) "priority" else "short"
}
slurmOptions <- combine_slurmConfig(paste0("--qos=", runEnv$qos, " --job-name=", fullrunname, " --output=", logfile,
" --open-mode=append --mail-type=END --comment=REMIND-MAgPIE --tasks-per-node=", runEnv$numberOfTasks,
if (runEnv$numberOfTasks == 1) " --mem=8000"), runEnv$sbatch)
slurmCommand <- paste0("sbatch ", slurmOptions, " --wrap=\"Rscript start_coupled.R coupled_config=", Rdatafile, "\"")
message(slurmCommand)
exitCode <- system(slurmCommand)
if (0 < exitCode) {
errorsfound <- errorsfound + 1
message("sbatch command failed, check logs.")
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test_01-readDefaultConfig.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ test_that("readDefaultConfig works", {
expect_no_warning(cfg <- gms::readDefaultConfig("../.."))
# make sure that no identical names are used to guarantee unique matching of scenario_config data
expect_identical(intersect(names(cfg), names(cfg$gms)), character(0))
# make sure there is no cm_test and c_test simultaneously
shortnames <- tolower(gsub("^[a-zA-Z][a-zA-Z]?_", "", names(cfg$gms)))
expect_equal(shortnames[duplicated(shortnames)], character(0))
})
2 changes: 1 addition & 1 deletion tutorials/03_RunningBundleOfRuns.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ The column `slurmConfig` can be used to specify the slurm configuration, either

Everything in the row after a `#` is interpreted as comment. Best use it as first character in the first column to structure the file. Using `#` elsewhere else can lead to unexpected data losses of the cells that follow in the row. If you want to switch off the use of a column, either temporarily or to add some comments, add a dot before the parameter name, which then may read `.cm_startyear` and is then ignored.

Before you start the runs, you can test whether the right runs would be started and find all necessary gdx files. This also writes the `.Rdata` files in the REMIND main folder:
Before you start the runs, you can test whether the right runs would be started and find all necessary gdx files:
```bash
Rscript start.R --test config/scenario_config_XYZ.csv
```
Expand Down
Loading