forked from remindmodel/remind
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request remindmodel#1356 from orichters/smallfix4
reactive check_config, add regexp check for cfg$… values
- Loading branch information
Showing
19 changed files
with
402 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
title;start;qos;sbatch;magpie_scen;magpie_empty;no_ghgprices_land_until;max_iterations;oldrun;path_gdx;path_gdx_ref;path_gdx_bau;path_report;cm_nash_autoconverge_lastrun;path_mif_ghgprice_land | ||
TESTTHAT-SSP2EU-Base;1;auto;--wait;SSP2|NPI;TRUE;y2150;2;;;;;;2; | ||
TESTTHAT-SSP2EU-NDC;1;auto;--wait;SSP2|NDC;TRUE;y2150;2;;;;;;2;TESTTHAT-SSP2EU-Base | ||
TESTTHAT-SSP2EU-Policy;2;auto;--wait;SSP2|NDC;TRUE;y2150;2;TESTTHAT-SSP2EU-Base;;;;;;output/C_TESTTHAT-SSP2EU-Base-rem-1/REMIND_generic_C_TESTTHAT-SSP2EU-Base-rem-1.mif | ||
TESTTHAT-SSP2EU-Base;1;auto;--wait --mail-type=FAIL;SSP2|NPI;TRUE;y2150;2;;;;;;2; | ||
TESTTHAT-SSP2EU-NDC;1;auto;--wait --mail-type=FAIL;SSP2|NDC;TRUE;y2150;2;;;;;;2;TESTTHAT-SSP2EU-Base | ||
TESTTHAT-SSP2EU-Policy;2;auto;--wait --mail-type=FAIL;SSP2|NDC;TRUE;y2150;2;TESTTHAT-SSP2EU-Base;;;;;;output/C_TESTTHAT-SSP2EU-Base-rem-1/REMIND_generic_C_TESTTHAT-SSP2EU-Base-rem-1.mif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#' take a REMIND cfg, runs some consistency checks and automatically fix some wrong settings | ||
#' The regexp check loads the code from main.gms and looks for 'regexp = ' patterns. | ||
#' It then checks whether the current cfg matches those patterns. | ||
#' | ||
#' @param cfg list with REMIND setting | ||
#' @param remindPath path to REMIND directory containing the main.gms | ||
#' @param testmode boolean. Default is FALSE which fails on errors, in testmode only raise warnings | ||
#' @author Oliver Richters | ||
#' @return updated cfg | ||
checkFixCfg <- function(cfg, remindPath = ".", testmode = FALSE) { | ||
refcfg <- gms::readDefaultConfig(remindPath) | ||
gms::check_config(cfg, reference_file = refcfg, modulepath = file.path(remindPath, "modules"), | ||
settings_config = file.path(remindPath, "config", "settings_config.csv"), | ||
extras = c("backup", "remind_folder", "pathToMagpieReport", "cm_nash_autoconverge_lastrun", | ||
"gms$c_expname", "restart_subsequent_runs", "gms$c_GDPpcScen", | ||
"gms$cm_CES_configuration", "gms$c_description", "model")) | ||
|
||
errorsfound <- 0 | ||
|
||
## regexp check | ||
# extract all instances of 'regexp' from main.gms | ||
code <- system(paste0("grep regexp ", file.path(remindPath, "main.gms")), intern = TRUE) | ||
# this is used to replace all 'regexp = is.numeric' | ||
grepisnum <- "((\\+|-)?[0-9]*([0-9]\\.?|\\.?[0-9])[0-9]*)" | ||
grepisshare <- "(\\+?0?\\.[0-9]+|0|0\\.0*|1|1\\.0*)" | ||
# some simple tests | ||
if (testmode) { | ||
stopifnot(all( grepl(paste0("^", grepisnum, "$"), c("2", "2.2", "32.", "+32.", "+.05", "-0.5", "-.5", "-5", "-7.")))) | ||
stopifnot(all(! grepl(paste0("^", grepisnum, "$"), c("2.2.", "0a", "1e1", ".2.", "ab", "2.3a", "--a", "++2")))) | ||
stopifnot(all( grepl(paste0("^", grepisshare, "$"), c("0", "0.0", ".000", "1.0", "1.", "1", "0.12341234")))) | ||
stopifnot(all(! grepl(paste0("^", grepisshare, "$"), c("1.1", "-0.3", "-0", ".")))) | ||
} | ||
|
||
for (n in names(cfg$gms)) { | ||
errormsg <- NULL | ||
# how parameter n is defined in main.gms | ||
paramdef <- paste0("^([ ]*", n, "[ ]*=|\\$setglobal[ ]+", n, " )") | ||
# filter fitting parameter definition from code snippets containing regexp | ||
filtered <- grep(paste0(paramdef, ".*regexp[ ]*=[ ]*"), code, value = TRUE) | ||
if (length(filtered) == 1) { | ||
# search for string '!! regexp = whatever', potentially followed by '!! otherstuff' and extract 'whatever' | ||
regexp <- paste0("^(", trimws(gsub("!!.*", "", gsub("^.*regexp[ ]*=", "", filtered))), ")$") | ||
# replace is.numeric by pattern defined above | ||
useregexp <- gsub("is.numeric", grepisnum, regexp, fixed = TRUE) | ||
useregexp <- gsub("is.share", grepisshare, useregexp, fixed = TRUE) | ||
# check whether parameter value fits regular expression | ||
if (! grepl(useregexp, cfg$gms[[n]])) { | ||
errormsg <- paste0("Parameter cfg$gms$", n, "=", cfg$gms[[n]], " does not fit this regular expression: ", regexp) | ||
} | ||
} else if (length(filtered) > 1) { | ||
# fail if more than one regexp found for parameter | ||
errormsg <- paste0("More than one regexp found for ", n, ". These are the code lines:\n", paste(filtered, collapse = "\n")) | ||
} | ||
# count errors | ||
if (! is.null(errormsg)) { | ||
errorsfound <- errorsfound + 1 | ||
if (testmode) warning(errormsg) else message(errormsg) | ||
} | ||
} | ||
|
||
# Check for compatibility with subsidizeLearning | ||
if ((cfg$gms$optimization != "nash") && (cfg$gms$subsidizeLearning == "globallyOptimal") ) { | ||
message("Only optimization='nash' is compatible with subsidizeLearning='globallyOptimal'. Switching subsidizeLearning to 'off' now.\n") | ||
cfg$gms$subsidizeLearning <- "off" | ||
} | ||
|
||
# reportCEScalib only works with the calibrate module | ||
if (! isTRUE(cfg$gms$CES_parameters == "calibrate")) { | ||
cfg$output <- setdiff(cfg$output, "reportCEScalib") | ||
} | ||
if (errorsfound > 0) { | ||
if (testmode) warning(errorsfound, " errors found.") | ||
else stop(errorsfound, " errors found, see above. Either adapt the parameter choice or the regexp in main.gms") | ||
} | ||
|
||
return(cfg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# | (C) 2006-2023 Potsdam Institute for Climate Impact Research (PIK) | ||
# | authors, and contributors see CITATION.cff file. This file is part | ||
# | of REMIND and licensed under AGPL-3.0-or-later. Under Section 7 of | ||
# | AGPL-3.0, you are granted additional permissions described in the | ||
# | REMIND License Exception, version 1.0 (see LICENSE file). | ||
# | Contact: [email protected] | ||
test_that("checkFixCfg works", { | ||
remind_folder <- "../.." | ||
savecfg <- cfg <- gms::readDefaultConfig(remind_folder) | ||
# regexp = NA means: no warning | ||
expect_warning(checkFixCfg(cfg, remind_folder, testmode = TRUE), regexp = NA) | ||
|
||
wrongsetting <- c( | ||
"cm_NDC_version" = "2004_cond", | ||
"cm_emiscen" = "123", | ||
"cm_nash_autoconverge" = "NA", | ||
"cm_co2_tax_2020" = "2.2.2", | ||
"cm_co2_tax_growth" = "333++", | ||
"c_macscen" = "-1", | ||
"cm_keep_presolve_gdxes" = "1.1", | ||
"cm_startyear" = "1985", | ||
"cm_netZeroScen" = "NöööGFS_v4", | ||
"cm_rcp_scen" = "apocalypse", | ||
"c_testOneRegi_region" = "LOONG", | ||
"c_shGreenH2" = "1.5", | ||
NULL) | ||
|
||
cfg <- savecfg | ||
cfg$gms[names(wrongsetting)] <- wrongsetting | ||
w <- capture_warnings(checkFixCfg(cfg, remind_folder, testmode = TRUE)) | ||
for (n in names(wrongsetting)) { | ||
expect_match(w, paste0(n, "=", wrongsetting[[n]]), all = FALSE, fixed = TRUE) | ||
} | ||
expect_match(w, paste0(length(wrongsetting), " errors found"), all = FALSE, fixed = TRUE) | ||
expect_equal(length(w), length(wrongsetting) + 1) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,13 @@ | |
# | Contact: [email protected] | ||
test_that("combine_slurmConfig works", { | ||
teststring <- "--qos=priority --time=03:30:00" | ||
expect_identical(combine_slurmConfig(teststring, teststring), teststring) | ||
expect_identical(combine_slurmConfig(teststring, NULL), teststring) | ||
expect_identical(combine_slurmConfig(NULL, NULL), "") | ||
expect_identical(combine_slurmConfig(teststring, ""), teststring) | ||
expect_identical(combine_slurmConfig(teststring, "--qos=standby"), "--qos=standby --time=03:30:00") | ||
expect_identical(combine_slurmConfig(teststring, "--bla=blub"), paste("--bla=blub", teststring)) | ||
expect_identical(combine_slurmConfig(teststring, "--wait"), paste("--wait", teststring)) | ||
teststring <- "--qos=priority --wait" | ||
expect_identical(combine_slurmConfig(teststring, "--qos=standby"), "--qos=standby --wait") | ||
}) |
Oops, something went wrong.