From 2e3a204ee5a5091571abc736146ea60690acff2d Mon Sep 17 00:00:00 2001 From: orichters Date: Mon, 5 Aug 2024 11:17:48 +0200 Subject: [PATCH 1/2] fail tests if manipulating main.gms with default cfg drops/changes switches --- CHANGELOG.md | 2 + DESCRIPTION | 4 +- main.gms | 16 ++++--- tests/testthat/test_01-manipulateConfig.R | 56 +++++++++++++++++++++++ 4 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 tests/testthat/test_01-manipulateConfig.R diff --git a/CHANGELOG.md b/CHANGELOG.md index 9eacc60f7..09e8406cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). [[#1739](https://github.com/remindmodel/remind/pull/1739)] - **scripts** fail transparently on duplicated column names in `scenario_config*.csv` files [[#1742](https://github.com/remindmodel/remind/pull/1742)] +- **testthat** fail if manipulating main.gms with default cfg drops/changes switches + [[#1764](https://github.com/remindmodel/remind/pull/1764)] ### fixed - included CCS from plastic waste incineration in CCS mass flows so it is diff --git a/DESCRIPTION b/DESCRIPTION index 75c41f426..4532bfced 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -25,7 +25,7 @@ Imports: gdxdt, gdxrrw, ggplot2, - gms (>= 0.32.0), + gms (>= 0.32.1), goxygen (>= 1.4.4), gridExtra, gtools, @@ -36,7 +36,7 @@ Imports: knitr, lazyeval, lpjclass, - lucode2 (>= 0.36.0), + lucode2 (>= 0.47.8), luplot, luscale, lusweave, diff --git a/main.gms b/main.gms index 4294f8f27..58cd7c0f6 100755 --- a/main.gms +++ b/main.gms @@ -454,7 +454,7 @@ parameter *' * (2): parallel - all regions are run in parallel *' parameter - cm_iteration_max "number of iterations, if optimization is set to negishi or testOneRegi; is overwritten in Nash mode, except for cm_nash_autoconverge = 0" + cm_iteration_max "number of iterations, if optimization is set to negishi or testOneRegi; is overwritten in Nash mode, except if cm_nash_autoconverge is set to 0" ; cm_iteration_max = 1; !! def = 1 *' @@ -1621,12 +1621,14 @@ $setGlobal cm_CESMkup_ind standard !! def = standard $setGlobal cm_CESMkup_ind_data "" !! def = "" *** cm_fxIndUe "switch for fixing UE demand in industry to baseline level - no endogenous demand adjustment" -*** default cm_fxIndUe = off -> endogenous demand, cm_fxIndUe = on -> exogenous demand fixed to baseline/NPi level (read in from input_ref.gdx) -*** cm_fxIndUeReg indicates the regions under which the industry demand will be fixed -*** for example, cm_fxIndUe = on and cm_fxIndUeReg = SSA,NEU,CHA,IND,OAS,MEA,LAM gives a scenario where all non global north (non-OECD) industry demand is fixed to baseline -*** cm_fxIndUeReg = GLO fixes industry demand to baseline level everywhere -$setGlobal cm_fxIndUe off !! def = off -$setGlobal cm_fxIndUeReg "" !! def = "" +*** off: endogenous demand. +*** on: exogenous demand fixed to baseline/NPi level (read in from input_ref.gdx) +*** cm_fxIndUeReg "indicates the regions under which the industry demand will be fixed, requires cm_fxIndUe set to on" +*** examples: +*** SSA,NEU,CHA,IND,OAS,MEA,LAM: gives a scenario where all non global north (non-OECD) industry demand is fixed to baseline +*** GLO: fixes industry demand to baseline level everywhere +$setGlobal cm_fxIndUe off !! def = off !! regexp = off|on +$setGlobal cm_fxIndUeReg "" !! def = "" *** cm_ind_energy_limit Switch for setting upper limits on industry energy *** efficiency improvements. See ./modules/37_subsectors/datainput.gms for diff --git a/tests/testthat/test_01-manipulateConfig.R b/tests/testthat/test_01-manipulateConfig.R new file mode 100644 index 000000000..bffc87d84 --- /dev/null +++ b/tests/testthat/test_01-manipulateConfig.R @@ -0,0 +1,56 @@ +test_that("manipulate config with default configuration does not change main.gms", { + # copy main file and manipulate it based on default settings + cfg_init <- gms::readDefaultConfig("../..") + tmpfile <- tempfile(pattern = "main", tmpdir = "../..", fileext = ".gms") + file.copy("../../main.gms", tmpfile) + lucode2::manipulateConfig(tmpfile, cfg_init$gms) + cfg_after <- gms::readDefaultConfig("../..", basename(tmpfile)) + + # check diff + diffavailable <- ! Sys.which("diff") == "" + if (diffavailable) { + diffresult <- suppressWarnings(system(paste("diff -b ../../main.gms", tmpfile), intern = TRUE)) + # drop all sorts of comments until https://github.com/pik-piam/lucode2/issues/121 is fixed + drop <- c("^< \\*\\*\\*", "^> \\*\\*\\*", "^> \\*' \\*", "^< \\*' \\*", "^---$", "^[0-9,]+c[0-9,]+$") + for (d in drop) { + diffresult <- grep(d, diffresult, value = TRUE, invert = TRUE) + } + if (length(diffresult) > 0) { + warning("Applying manipulateConfig with the default configuration leads to this diff between main.gms and ", tmpfile, ":\n", + paste(diffresult, collapse = "\n")) + } + expect_equal(length(diffresult), 0) + } + + # check for switches missing in the new cfg + removedgms <- setdiff(names(cfg_init$gms), names(cfg_after$gms)) + if (length(removedgms) > 0) { + warning("These cfg$gms switches can't be found after manipulation of main.gms, see ", tmpfile, ".\n", + "Please file an issue in the gms package and try to adjust the code until the error goes away:\n", + paste("-", removedgms, collapse = "\n")) + } + expect_equal(length(removedgms), 0) + + # check for switches added to the new cfg + addedgms <- setdiff(names(cfg_after$gms), names(cfg_init$gms)) + if (length(addedgms) > 0) { + warning("These cfg$gms switches were somehow added by manipulateConfig to main.gms, see ", tmpfile, ".\n", + "Please file an issue in the gms package and try to adjust the code until the error goes away:\n", + paste("-", addedgms, collapse = "\n")) + } + expect_equal(length(addedgms), 0) + + # check for switches with different content between old and new cfg + joinednames <- intersect(names(cfg_after$gms), names(cfg_init$gms)) + contentdiff <- joinednames[! unlist(cfg_init$gms[joinednames]) == unlist(cfg_after$gms[joinednames])] + if (length(contentdiff) > 0) { + warning("After file manipulation, the following cfg$gms switches differ, see ", tmpfile, ":\n", + paste0("- ", contentdiff, ": ", unlist(cfg_init$gms[contentdiff]), " -> ", unlist(cfg_after$gms[contentdiff]), collapse = "\n")) + } + expect_equal(length(contentdiff), 0) + + # cleanup if no error found + if (length(addedgms) + length(removedgms) + length(contentdiff) + length(diffresult) == 0) { + file.remove(tmpfile) + } +}) From eb30329db60f7b41133c856216981406cda3a1bd Mon Sep 17 00:00:00 2001 From: orichters Date: Mon, 5 Aug 2024 11:29:56 +0200 Subject: [PATCH 2/2] short filename, make sure it works also if diff is missing --- tests/testthat/test_01-manipulateConfig.R | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/testthat/test_01-manipulateConfig.R b/tests/testthat/test_01-manipulateConfig.R index bffc87d84..5cb88dae0 100644 --- a/tests/testthat/test_01-manipulateConfig.R +++ b/tests/testthat/test_01-manipulateConfig.R @@ -7,6 +7,7 @@ test_that("manipulate config with default configuration does not change main.gms cfg_after <- gms::readDefaultConfig("../..", basename(tmpfile)) # check diff + diffresult <- NULL diffavailable <- ! Sys.which("diff") == "" if (diffavailable) { diffresult <- suppressWarnings(system(paste("diff -b ../../main.gms", tmpfile), intern = TRUE)) @@ -16,7 +17,8 @@ test_that("manipulate config with default configuration does not change main.gms diffresult <- grep(d, diffresult, value = TRUE, invert = TRUE) } if (length(diffresult) > 0) { - warning("Applying manipulateConfig with the default configuration leads to this diff between main.gms and ", tmpfile, ":\n", + warning("Applying manipulateConfig with the default configuration leads to this diff between main.gms and ", + basename(tmpfile), ":\n", paste(diffresult, collapse = "\n")) } expect_equal(length(diffresult), 0) @@ -25,7 +27,7 @@ test_that("manipulate config with default configuration does not change main.gms # check for switches missing in the new cfg removedgms <- setdiff(names(cfg_init$gms), names(cfg_after$gms)) if (length(removedgms) > 0) { - warning("These cfg$gms switches can't be found after manipulation of main.gms, see ", tmpfile, ".\n", + warning("These cfg$gms switches can't be found after manipulation of main.gms, see ", basename(tmpfile), ".\n", "Please file an issue in the gms package and try to adjust the code until the error goes away:\n", paste("-", removedgms, collapse = "\n")) } @@ -34,7 +36,7 @@ test_that("manipulate config with default configuration does not change main.gms # check for switches added to the new cfg addedgms <- setdiff(names(cfg_after$gms), names(cfg_init$gms)) if (length(addedgms) > 0) { - warning("These cfg$gms switches were somehow added by manipulateConfig to main.gms, see ", tmpfile, ".\n", + warning("These cfg$gms switches were somehow added by manipulateConfig to main.gms, see ", basename(tmpfile), ".\n", "Please file an issue in the gms package and try to adjust the code until the error goes away:\n", paste("-", addedgms, collapse = "\n")) } @@ -44,7 +46,7 @@ test_that("manipulate config with default configuration does not change main.gms joinednames <- intersect(names(cfg_after$gms), names(cfg_init$gms)) contentdiff <- joinednames[! unlist(cfg_init$gms[joinednames]) == unlist(cfg_after$gms[joinednames])] if (length(contentdiff) > 0) { - warning("After file manipulation, the following cfg$gms switches differ, see ", tmpfile, ":\n", + warning("After file manipulation, the following cfg$gms switches differ, see ", basename(tmpfile), ":\n", paste0("- ", contentdiff, ": ", unlist(cfg_init$gms[contentdiff]), " -> ", unlist(cfg_after$gms[contentdiff]), collapse = "\n")) } expect_equal(length(contentdiff), 0)