diff --git a/.buildlibrary b/.buildlibrary
index eda32ae..f4c599b 100644
--- a/.buildlibrary
+++ b/.buildlibrary
@@ -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'
diff --git a/CITATION.cff b/CITATION.cff
index 8a21b85..b0bd87d 100644
--- a/CITATION.cff
+++ b/CITATION.cff
@@ -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' (). Out-of-the-box
'GAMS' does not come with support for modularized model code. This package provides
diff --git a/DESCRIPTION b/DESCRIPTION
index a2db6a9..79a6639 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -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 = "dietrich@pik-potsdam.de", role = c("aut","cre")),
person("David", "Klein", role = "aut"),
person("Anastasis", "Giannousakis", role = "aut"),
@@ -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
diff --git a/R/update_modules_embedding.R b/R/update_modules_embedding.R
index ddb85d8..ff7ad16 100644
--- a/R/update_modules_embedding.R
+++ b/R/update_modules_embedding.R
@@ -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]
@@ -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: ",
@@ -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")
@@ -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',
@@ -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 ############################################
diff --git a/README.md b/README.md
index 5f22d5f..d5d42a6 100644
--- a/README.md
+++ b/README.md
@@ -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)
@@ -43,7 +43,7 @@ In case of questions / problems please contact Jan Philipp Dietrich , R package version 0.29.0, .
+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, .
A BibTeX entry for LaTeX users is
@@ -51,8 +51,8 @@ A BibTeX entry for LaTeX users is
@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},
}
diff --git a/man/getfiledestinations.Rd b/man/getfiledestinations.Rd
index cfb0993..54768b4 100644
--- a/man/getfiledestinations.Rd
+++ b/man/getfiledestinations.Rd
@@ -7,7 +7,8 @@
getfiledestinations()
}
\description{
-Create file2destination mapping based on information from the model
+Create file2destination mapping based on information from the model, ignoring
+top-level directories listed in \code{.gitignore}.
}
\author{
Jan Philipp Dietrich, David Klein
diff --git a/tests/testthat/test-update_modules_embedding.R b/tests/testthat/test-update_modules_embedding.R
new file mode 100644
index 0000000..89300ac
--- /dev/null
+++ b/tests/testthat/test-update_modules_embedding.R
@@ -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))))
+})
+