Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't add empty realizations to main module files #78

Merged
merged 3 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '5511520'
ValidationKey: '5708360'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand Down
2 changes: 1 addition & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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.28.0
version: 0.29.0
date-released: '2023-11-23'
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
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: gms
Type: Package
Title: 'GAMS' Modularization Support Package
Version: 0.28.0
Version: 0.29.0
Date: 2023-11-23
Authors@R: c(person("Jan Philipp", "Dietrich", email = "[email protected]", role = c("aut","cre")),
person("David", "Klein", role = "aut"),
Expand Down
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
6 changes: 3 additions & 3 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.28.0**
R package **gms**, version **0.29.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,7 +43,7 @@ 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 (URL: https://doi.org/10.5281/zenodo.4390032), R package version 0.28.0, <URL: https://github.com/pik-piam/gms>.
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 (URL: https://doi.org/10.5281/zenodo.4390032), R package version 0.29.0, <URL: https://github.com/pik-piam/gms>.

A BibTeX entry for LaTeX users is

Expand All @@ -52,7 +52,7 @@ A BibTeX entry for LaTeX users is
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.28.0},
note = {R package version 0.29.0},
doi = {10.5281/zenodo.4390032},
url = {https://github.com/pik-piam/gms},
}
Expand Down
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))))
})