Skip to content

Commit

Permalink
Merge pull request #78 from orichters/updatemodules
Browse files Browse the repository at this point in the history
don't add empty realizations to main module files
  • Loading branch information
orichters authored Feb 1, 2024
2 parents 9a604ba + bf38bab commit 4c89b71
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '5711550'
ValidationKey: '5926200'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand Down
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ cff-version: 1.2.0
message: If you use this software, please cite it using the metadata from this file.
type: software
title: 'gms: ''GAMS'' Modularization Support Package'
version: 0.29.0
date-released: '2023-12-04'
version: 0.30.0
date-released: '2024-02-01'
abstract: A collection of tools to create, use and maintain modularized model code
written in the modeling language 'GAMS' (<https://www.gams.com/>). Out-of-the-box
'GAMS' does not come with support for modularized model code. This package provides
Expand Down
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: gms
Type: Package
Title: 'GAMS' Modularization Support Package
Version: 0.29.0
Date: 2023-12-04
Version: 0.30.0
Date: 2024-02-01
Authors@R: c(person("Jan Philipp", "Dietrich", email = "[email protected]", role = c("aut","cre")),
person("David", "Klein", role = "aut"),
person("Anastasis", "Giannousakis", role = "aut"),
Expand Down Expand Up @@ -37,4 +37,4 @@ BugReports: https://github.com/pik-piam/gms/issues
License: BSD_2_clause + file LICENSE
LazyData: no
Encoding: UTF-8
RoxygenNote: 7.2.3
RoxygenNote: 7.3.0
32 changes: 18 additions & 14 deletions R/update_modules_embedding.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ update_modules_embedding <- function(modelpath = ".", modulepath = "modules/",
stop("Could not find model main file. Neither main.gms nor magpie.gms do exist!")
}

#connect whole code to one object by replacing $incude commands with
code <- c("", code, "") # includes in first or last line let repeat statement run infinitely
#connect whole code to one object by replacing $include commands with
#corresponding code (.csv, .cs2 includes and batincludes are excluded)
repeat {
i <- grep("^\\$include", code)[1]
Expand Down Expand Up @@ -76,8 +77,8 @@ update_modules_embedding <- function(modelpath = ".", modulepath = "modules/",
fullmodulepath <- path(modelpath, modulepath)
modules <- getModules(fullmodulepath)[, "folder"]
moduleGMSpathsAll <- switch(version,
"1" = path(".", modulepath, modules, modules, ftype = "gms"),
"2" = path(".", modulepath, modules, "module", ftype = "gms"))
"1" = path(modelpath, modulepath, modules, modules, ftype = "gms"),
"2" = path(modelpath, modulepath, modules, "module", ftype = "gms"))
# excludes modules that have no gms file
if (! all(file.exists(moduleGMSpathsAll))) {
warning("Missing module gms files: ",
Expand All @@ -97,8 +98,15 @@ update_modules_embedding <- function(modelpath = ".", modulepath = "modules/",
#remove reserved module type names
types <- setdiff(types, getOption("gms_reserved_types"))
realizationGMSpaths <- switch(version,
"1" = path(".", modulepath, module, types, ftype = "gms"),
"2" = path(".", modulepath, module, types, "realization", ftype = "gms"))
"1" = path(modelpath, modulepath, module, types, ftype = "gms"),
"2" = path(modelpath, modulepath, module, types, "realization", ftype = "gms"))
emptyrealization <- ! file.exists(realizationGMSpaths)
if (any(emptyrealization)) {
warning("For module ", module, ", the following realizations lack their main gms file and cannot be used: ",
paste(types[emptyrealization], collapse = ", "))
}
types <- types[! emptyrealization]
realizationGMSpaths <- realizationGMSpaths[! emptyrealization]
code <- paste0("$Ifi \"%", substring(module, 4), "%\" == \"", types, "\" $include \"", realizationGMSpaths, "\"")
replace_in_file(moduleGMSpaths[m], code, subject = "MODULETYPES")

Expand All @@ -107,23 +115,19 @@ update_modules_embedding <- function(modelpath = ".", modulepath = "modules/",
t <- types[ti]
code <- NULL
for (phase in phases) {
if (file.exists(path(".", modulepath, module, t, phase, ftype = "gms"))) {
if (file.exists(path(modelpath, modulepath, module, t, phase, ftype = "gms"))) {
if (verbose) message(module, " ", t, ": ", phase, " is used")
code <- c(code, paste("$Ifi \"%phase%\" == \"", phase, "\" $include \"",
path(".", modulepath, module, t, phase, ftype = "gms"), "\"", sep = ""))
path(modelpath, modulepath, module, t, phase, ftype = "gms"), "\"", sep = ""))
} else if (verbose) message(module, " ", t, ": ", phase, "is not used")
}
if (file.exists(realizationGMSpaths[ti])) {
replace_in_file(realizationGMSpaths[ti], code, subject = "PHASES")
} else {
warning(realizationGMSpaths[ti], " not found, this realization cannot be used!")
}
replace_in_file(realizationGMSpaths[ti], code, subject = "PHASES")
}
}

############# ADD MODULE INFO IN SETS ###################### START ##########################################

if (length(grep("module2realisation", readLines(path(modelpath, "core/sets.gms")))) > 0) {
if (length(grep("module2realisation", readLines(path(modelpath, "core", "sets.gms")))) > 0) {
content <- NULL
modification_warning <- c(
'*** THIS CODE IS CREATED AUTOMATICALLY, DO NOT MODIFY THESE LINES DIRECTLY',
Expand All @@ -136,7 +140,7 @@ update_modules_embedding <- function(modelpath = ".", modulepath = "modules/",
content <- c(content, '', ' module2realisation(modules,*) "mapping of modules and active realisations" /')
content <- c(content, paste0(" ", moduleNames, " . %", moduleNames, "%"))
content <- c(content, ' /', ';')
replace_in_file("core/sets.gms", content, "MODULES", comment = "***")
replace_in_file(path(modelpath, "core", "sets.gms"), content, "MODULES", comment = "***")


############# ADD MODULE INFO IN SETS ###################### END ############################################
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 'GAMS' Modularization Support Package

R package **gms**, version **0.29.0**
R package **gms**, version **0.30.0**

[![CRAN status](https://www.r-pkg.org/badges/version/gms)](https://cran.r-project.org/package=gms) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.4390032.svg)](https://doi.org/10.5281/zenodo.4390032) [![R build status](https://github.com/pik-piam/gms/workflows/check/badge.svg)](https://github.com/pik-piam/gms/actions) [![codecov](https://codecov.io/gh/pik-piam/gms/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/gms) [![r-universe](https://pik-piam.r-universe.dev/badges/gms)](https://pik-piam.r-universe.dev/builds)

Expand Down Expand Up @@ -43,16 +43,16 @@ In case of questions / problems please contact Jan Philipp Dietrich <dietrich@pi

To cite package **gms** in publications use:

Dietrich J, Klein D, Giannousakis A, Beier F, Koch J, Baumstark L, Pflüger M, Richters O (2023). _gms: 'GAMS' Modularization Support Package_. doi:10.5281/zenodo.4390032 <https://doi.org/10.5281/zenodo.4390032>, R package version 0.29.0, <https://github.com/pik-piam/gms>.
Dietrich J, Klein D, Giannousakis A, Beier F, Koch J, Baumstark L, Pflüger M, Richters O (2024). _gms: 'GAMS' Modularization Support Package_. doi: 10.5281/zenodo.4390032 (URL: https://doi.org/10.5281/zenodo.4390032), R package version 0.30.0, <URL: https://github.com/pik-piam/gms>.

A BibTeX entry for LaTeX users is

```latex
@Manual{,
title = {gms: 'GAMS' Modularization Support Package},
author = {Jan Philipp Dietrich and David Klein and Anastasis Giannousakis and Felicitas Beier and Johannes Koch and Lavinia Baumstark and Mika Pflüger and Oliver Richters},
year = {2023},
note = {R package version 0.29.0},
year = {2024},
note = {R package version 0.30.0},
doi = {10.5281/zenodo.4390032},
url = {https://github.com/pik-piam/gms},
}
Expand Down
3 changes: 2 additions & 1 deletion man/getfiledestinations.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions tests/testthat/test-update_modules_embedding.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
test_that("update_modules_embedding test", {
modelpath <- tempdir()
writeLines(c('$include "./core/sets.gms";',
'$batinclude "./modules/include.gms" sets'),
file.path(modelpath, "main.gms"))
dir.create(file.path(modelpath, "core"))
writeLines(c("***######################## R SECTION START (MODULES) ###############################",
"module2realisation",
"***######################### R SECTION END (MODULES) ################################"),
file.path(modelpath, "core", "sets.gms"))
modulepath <- file.path(modelpath, "modules")
dir.create(modulepath)
writeLines(c("*######################## R SECTION START (MODULES) ############################",
"*######################## R SECTION END (MODULES) ##############################"),
file.path(modulepath, "include.gms"))
module21path <- file.path(modulepath, "21_testmodule")
dir.create(module21path)
writeLines(c("*###################### R SECTION START (MODULETYPES) ##########################",
"*###################### R SECTION END (MODULETYPES) ############################"),
file.path(module21path, "module.gms"))
dir.create(file.path(module21path, "empty"))
dir.create(file.path(module21path, "works"))
writeLines(c("*####################### R SECTION START (PHASES) ##############################",
"*######################## R SECTION END (PHASES) ###############################"),
file.path(module21path, "works", "realization.gms"))
writeLines("some GAMS code",
file.path(module21path, "works", "sets.gms"))
expect_warning(update_modules_embedding(modelpath = modelpath, modulepath = "modules/",
includefile = "modules/include.gms", verbose = FALSE), "empty")
expect_true(any(grepl("testmodule", readLines(file.path(modelpath, "core", "sets.gms"), warn = FALSE))))
expect_true(any(grepl("21_testmodule", readLines(file.path(modulepath, "include.gms"), warn = FALSE))))
expect_true(any(grepl("works", readLines(file.path(module21path, "module.gms"), warn = FALSE))))
expect_false(any(grepl("empty", readLines(file.path(module21path, "module.gms"), warn = FALSE))))
expect_true(any(grepl("sets", readLines(file.path(module21path, "works", "realization.gms"), warn = FALSE))))
})

0 comments on commit 4c89b71

Please sign in to comment.