Skip to content

Commit

Permalink
fail tests if manipulating main.gms with default cfg drops/changes sw…
Browse files Browse the repository at this point in the history
…itches
  • Loading branch information
orichters committed Aug 5, 2024
1 parent 1c045e6 commit 93f10c7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
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 @@ -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
Expand Down
56 changes: 56 additions & 0 deletions tests/testthat/test_01-manipulateConfig.R
Original file line number Diff line number Diff line change
@@ -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)
}
})

0 comments on commit 93f10c7

Please sign in to comment.