Skip to content

Commit

Permalink
Merge pull request #1764 from orichters/codecheck
Browse files Browse the repository at this point in the history
fail tests if manipulating main.gms with default cfg drops/changes switches
  • Loading branch information
orichters authored Aug 5, 2024
2 parents 504940d + eb30329 commit 36fde29
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Imports:
gdxdt,
gdxrrw,
ggplot2,
gms (>= 0.32.0),
gms (>= 0.32.1),
goxygen (>= 1.4.4),
gridExtra,
gtools,
Expand All @@ -36,7 +36,7 @@ Imports:
knitr,
lazyeval,
lpjclass,
lucode2 (>= 0.36.0),
lucode2 (>= 0.47.8),
luplot,
luscale,
lusweave,
Expand Down
16 changes: 9 additions & 7 deletions main.gms
Original file line number Diff line number Diff line change
Expand Up @@ -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
*'
Expand Down Expand Up @@ -1623,12 +1623,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
Expand Down
58 changes: 58 additions & 0 deletions tests/testthat/test_01-manipulateConfig.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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
diffresult <- NULL
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 ",
basename(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 ", 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"))
}
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 ", 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"))
}
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 ", basename(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)
}
})

0 comments on commit 36fde29

Please sign in to comment.