Skip to content

Commit 984be8a

Browse files
createClusterST() updated to use new tree structure folder with constraints (#223)
1 parent 2d6c1da commit 984be8a

File tree

3 files changed

+57
-43
lines changed

3 files changed

+57
-43
lines changed

R/createClusterST.R

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -132,20 +132,15 @@
132132
#' # remember to prefix in the list
133133
#'
134134
#' name_no_prefix <- "add_constraints"
135-
#' clust_name <- paste(area_test_clust,
136-
#' name_no_prefix,
137-
#' sep = "_")
138135
#'
139136
#' constraints_properties <- list(
140137
#' "withdrawal-1"=list(
141-
#' cluster = clust_name,
142138
#' variable = "withdrawal",
143139
#' operator = "equal",
144140
#' hours = c("[1,3,5]",
145141
#' "[120,121,122,123,124,125,126,127,128]")
146142
#' ),
147143
#' "netting-1"=list(
148-
#' cluster = clust_name,
149144
#' variable = "netting",
150145
#' operator = "less",
151146
#' hours = c("[1, 168]")
@@ -157,6 +152,20 @@
157152
#' constraints_properties = constraints_properties)
158153
#'
159154
#' # Add optional constraints properties + TS
155+
#'
156+
#' constraints_properties <- list(
157+
#' "withdrawal-2"=list(
158+
#' variable = "withdrawal",
159+
#' operator = "equal",
160+
#' hours = c("[1,3,5]",
161+
#' "[120,121,122,123,124,125,126,127,128]")
162+
#' ),
163+
#' "netting-2"=list(
164+
#' variable = "netting",
165+
#' operator = "less",
166+
#' hours = c("[1, 168]")
167+
#' ))
168+
#'
160169
#' good_ts <- matrix(0.7, 8760)
161170
#' constraints_ts <- list(
162171
#' "withdrawal-2"=good_ts,
@@ -237,6 +246,7 @@ createClusterST <- function(area,
237246
params_cluster <- hyphenize_names(storage_parameters)
238247

239248
## Standardize cluster name + prefix ----
249+
cluster_name_ori <- cluster_name
240250
cluster_name <- generate_cluster_name(area = area,
241251
cluster_name = cluster_name,
242252
add_prefix = add_prefix)
@@ -411,7 +421,7 @@ createClusterST <- function(area,
411421
## add constraint(s) ----
412422
if(!is.null(constraints_properties))
413423
.add_storage_constraint(area = area,
414-
cluster_name = cluster_name,
424+
cluster_name = cluster_name_ori,
415425
constraints_properties = constraints_properties,
416426
constraints_ts = constraints_ts,
417427
overwrite = overwrite,
@@ -578,13 +588,14 @@ storage_values_default <- function(opts = simOptions()) {
578588
constraints_ts,
579589
overwrite,
580590
opts){
581-
# constraints/<area id>/additional-constraints.ini
591+
# constraints/<area id>/cluster/additional-constraints.ini
582592

583593
# create dir
584594
dir_path <- file.path(opts$inputPath,
585595
"st-storage",
586596
"constraints",
587-
area)
597+
area,
598+
cluster_name)
588599
dir.create(dir_path, recursive = TRUE, showWarnings = FALSE)
589600

590601
## write properties ----
@@ -633,25 +644,27 @@ storage_values_default <- function(opts = simOptions()) {
633644

634645
## write ts values ----
635646
# check if ts already exist
636-
ts_name <- paste0("rhs_", names(constraints_ts), ".txt")
637-
path_ts_files <- file.path(dir_path,
638-
ts_name)
639-
640-
if(any(file.exists(path_ts_files))){
641-
if(!overwrite)
642-
stop(paste(constraints_names, " already exist "),
643-
call. = FALSE)
644-
}
645-
lapply(names(constraints_ts),
646-
function(x){
647-
fwrite(
648-
x = constraints_ts[[x]],
649-
row.names = FALSE,
650-
col.names = FALSE,
651-
sep = "\t",
652-
file = file.path(dir_path,
653-
paste0("rhs_", x, ".txt")))
647+
if(!is.null(constraints_ts)){
648+
ts_name <- paste0("rhs_", names(constraints_ts), ".txt")
649+
path_ts_files <- file.path(dir_path,
650+
ts_name)
651+
652+
if(any(file.exists(path_ts_files))){
653+
if(!overwrite)
654+
stop(paste(constraints_names, " already exist "),
655+
call. = FALSE)
656+
}
657+
lapply(names(constraints_ts),
658+
function(x){
659+
fwrite(
660+
x = constraints_ts[[x]],
661+
row.names = FALSE,
662+
col.names = FALSE,
663+
sep = "\t",
664+
file = file.path(dir_path,
665+
paste0("rhs_", x, ".txt")))
654666
})
667+
}
655668
}
656669

657670

man/createClusterST.Rd

Lines changed: 14 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-createClusterST.R

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -690,21 +690,17 @@ test_that("Add right TS values",{
690690
## Optional constraints ----
691691
test_that("Add new binding constraint properties", {
692692
# given
693+
693694
name_no_prefix <- "add_constraints"
694-
clust_name <- paste(area_test_clust,
695-
name_no_prefix,
696-
sep = "_")
697695

698696
constraints_properties <- list(
699697
"withdrawal-1"=list(
700-
cluster = clust_name,
701698
variable = "withdrawal",
702699
operator = "equal",
703700
hours = c("[1,3,5]",
704701
"[120,121,122,123,124,125,126,127,128]")
705702
),
706703
"netting-1"=list(
707-
cluster = clust_name,
708704
variable = "netting",
709705
operator = "less",
710706
hours = c("[1, 168]")
@@ -721,6 +717,7 @@ test_that("Add new binding constraint properties", {
721717
"st-storage",
722718
"constraints",
723719
area_test_clust,
720+
name_no_prefix,
724721
"additional-constraints")
725722

726723
read_ini <- antaresRead::readIni(path_st_ini)
@@ -751,27 +748,20 @@ test_that("Add new binding constraint properties", {
751748
})
752749

753750

754-
755-
756751
test_that("Add new TS constraint", {
757752
# /!\ you can add ts only with properties
758753

759754
# given
760755
name_no_prefix <- "add_ts"
761-
clust_name <- paste(area_test_clust,
762-
name_no_prefix,
763-
sep = "_")
764756

765757
constraints_properties <- list(
766758
"withdrawal-2"=list(
767-
cluster = clust_name,
768759
variable = "withdrawal",
769760
operator = "equal",
770761
hours = c("[1,3,5]",
771762
"[120,121,122,123,124,125,126,127,128]")
772763
),
773764
"netting-2"=list(
774-
cluster = clust_name,
775765
variable = "netting",
776766
operator = "less",
777767
hours = c("[1, 168]")
@@ -794,6 +784,7 @@ test_that("Add new TS constraint", {
794784
"st-storage",
795785
"constraints",
796786
area_test_clust,
787+
name_no_prefix,
797788
"additional-constraints")
798789

799790
read_ini <- antaresRead::readIni(path_st_ini)
@@ -809,6 +800,7 @@ test_that("Add new TS constraint", {
809800
"st-storage",
810801
"constraints",
811802
area_test_clust,
803+
name_no_prefix,
812804
paste0("rhs_", names(constraints_ts), ".txt"))
813805

814806
# exist ?

0 commit comments

Comments
 (0)