Skip to content

Commit 7b6d832

Browse files
authored
Remove depency to readClusterDesc() from antaresRead package (#251)
* Remove depency to `readClusterDesc()` from antaresRead * Make the control insensitive to case
1 parent 5b9f5fd commit 7b6d832

File tree

4 files changed

+55
-9
lines changed

4 files changed

+55
-9
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ NEW FEATURES :
88
* `createDistrict()` uses a specific endpoint to create the district
99
* `editDistrict()` allows the user to edit a district. Use of a specific endpoint in API mode.
1010

11+
ENHANCEMENT :
12+
13+
* `createBindingConstraint()`/`editBindingCOnstraint()` : remove depency to `readClusterDesc()` from antaresRead package
14+
1115
BUGFIXES :
1216

1317
* `.get_version_solver_from_path_solver()` get solver version by running command line instead of searching in the path.

R/createBindingConstraint.R

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ utils::globalVariables(c('V2', 'dim_study', 'dim_input', 'name_group'))
4242
#'
4343
#' @name createBindingConstraint
4444
#'
45-
#' @importFrom antaresRead getLinks readClusterDesc setSimulationPath readIniFile simOptions
45+
#' @importFrom antaresRead getLinks setSimulationPath readIniFile simOptions
4646
#' @importFrom utils write.table
4747
#' @importFrom assertthat assert_that
4848
#'
@@ -212,7 +212,7 @@ createBindingConstraint <- function(name,
212212
group,
213213
overwrite,
214214
links = getLinks(opts = opts, namesOnly = TRUE),
215-
clusters = readClusterDesc(opts = opts),
215+
clusters = .list_clusters_for_binding_constraints(opts = opts),
216216
output_operator = values_operator,
217217
opts = opts
218218
)
@@ -668,7 +668,7 @@ group_values_meta_check <- function(group_value,
668668
#' @template opts
669669
#' @family binding constraints functions
670670
#'
671-
#' @importFrom antaresRead getLinks setSimulationPath readIniFile readClusterDesc
671+
#' @importFrom antaresRead getLinks setSimulationPath readIniFile
672672
#'
673673
#' @details
674674
#' According to Antares version, usage may vary :
@@ -754,7 +754,7 @@ createBindingConstraintBulk <- function(constraints,
754754
bindingConstraints <- readIniFile(pathIni, stringsAsFactors = FALSE)
755755

756756
all_links <- getLinks(opts = opts, namesOnly = TRUE)
757-
all_clusters <- readClusterDesc(opts = opts)
757+
all_clusters <- .list_clusters_for_binding_constraints(opts = opts)
758758

759759
for (i in seq_along(constraints)) {
760760
values_operator <- switch_to_list_name_operator_870(operator = constraints[[i]][["operator"]])
@@ -893,12 +893,43 @@ switch_to_list_name_operator_870 <- function(operator) {
893893
}
894894

895895
coefficientsToControl <- coefficients[grep(type_elements[[type]][["pattern"]], names(coefficients))]
896+
coefs_names <- tolower(names(coefficientsToControl))
896897

897-
if(length(coefficientsToControl) > 0) {
898-
if (!all(names(coefficientsToControl) %in% reference)) {
899-
badcoef <- names(coefficientsToControl)[!names(coefficientsToControl) %in% reference]
898+
if(length(coefs_names) > 0) {
899+
if (!all(coefs_names %in% reference)) {
900+
badcoef <- coefs_names[!coefs_names %in% reference]
900901
badcoef <- paste(shQuote(badcoef), collapse = ", ")
901902
stop(paste0(badcoef, " : is or are not valid ", type_elements[[type]][["stop_msg"]]))
902903
}
903904
}
904905
}
906+
907+
908+
#' @importFrom antaresRead readIniFile
909+
.list_clusters_for_binding_constraints <- function(opts) {
910+
911+
path <- file.path(opts[["inputPath"]], "thermal", "clusters")
912+
areas <- data.frame("area" = list.files(path))
913+
areas$path <- file.path(path, areas$area, "list.ini")
914+
areas$file_size <- sapply(areas$path, file.size)
915+
areas_fi <- areas[areas$file_size > 0,]
916+
917+
all_clusters <- data.frame("area" = c(), "cluster" = c())
918+
if (nrow(areas_fi) > 0) {
919+
clusters_by_area <- apply(areas_fi[,c("area","path")],
920+
MARGIN = 1,
921+
FUN = function(row) {
922+
iniFile <- readIniFile(file = as.character(row[2]))
923+
cl_names <- sapply(X = iniFile, "[[", "name")
924+
cl_names <- unname(cl_names)
925+
df_cl_names <- data.frame("area" = rep(as.character(row[1]), length(cl_names)), "cluster" = cl_names)
926+
return(df_cl_names)
927+
}
928+
)
929+
all_clusters <- do.call("rbind", clusters_by_area)
930+
all_clusters$area <- tolower(all_clusters$area)
931+
all_clusters$cluster <- tolower(all_clusters$cluster)
932+
}
933+
934+
return(all_clusters)
935+
}

R/editBindingConstraint.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#'
2929
#' @export
3030
#'
31-
#' @importFrom antaresRead getLinks setSimulationPath readClusterDesc simOptions readIniFile
31+
#' @importFrom antaresRead getLinks setSimulationPath simOptions readIniFile
3232
#' @importFrom utils write.table
3333
#' @importFrom assertthat assert_that
3434
#' @importFrom data.table as.data.table fwrite
@@ -205,7 +205,7 @@ editBindingConstraint <- function(name,
205205
}
206206

207207
if (has_clusters_coefs) {
208-
clusters <- readClusterDesc(opts = opts)
208+
clusters <- .list_clusters_for_binding_constraints(opts = opts)
209209
.check_bc_validity_coefficients(coefficients = coefficients, reference = clusters, type = "thermal")
210210
}
211211

tests/testthat/test-createBindingConstraint.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ sapply(studies, function(study) {
2020
)
2121

2222
expect_true("myconstraint" %in% names(antaresRead::readBindingConstraints()))
23+
24+
createBindingConstraint(
25+
name = "myconstraint_coef_no_lower",
26+
values = matrix(data = rep(0, 8760 * 3), ncol = 3),
27+
enabled = FALSE,
28+
coefficients = c("a.BaSe" = 0.1, "c.PeaK" = 0.3),
29+
timeStep = "hourly",
30+
operator = "both"
31+
)
32+
33+
expect_true("myconstraint_coef_no_lower" %in% names(antaresRead::readBindingConstraints()))
2334
})
2435

2536

0 commit comments

Comments
 (0)