Skip to content

Commit 337ca0b

Browse files
removeClusterST() manage constraints (#224)
* removeClusterST() manage constraints * create/edit minor fix for prefix and directory name * removeClusterST() updated to remove constraints properties and TS files
1 parent 4df3f1e commit 337ca0b

File tree

7 files changed

+152
-22
lines changed

7 files changed

+152
-22
lines changed

NEWS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ NEW FEATURES :
1313
* `createClusterST()`/`editClusterST()` :
1414
- **New properties** (*efficiencywithdrawal*, *penalize-variation-injection*, *penalize-variation-withdrawal*, see list of properties according to study version of Antares with `storage_values_default()`)
1515
- **New optional time series** (cost-injection, cost-withdrawal, cost-level, cost-variation-injection, cost-variation-withdrawal)
16-
- **New additional constraints** (properties and time series)
16+
- **New additional constraints** (properties and time series)
17+
* `removeClusterST()` : remove **New optional time series** + **New additional constraints**
1718
* `updateScenarioBuilder()` New type of series "hfl" ("hydro final level", similar to "hydrolevels") is available
1819

1920

R/createClusterST.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,6 @@ createClusterST <- function(area,
245245
params_cluster <- hyphenize_names(storage_parameters)
246246

247247
## Standardize cluster name + prefix ----
248-
cluster_name_ori <- cluster_name
249248
cluster_name <- generate_cluster_name(area = area,
250249
cluster_name = cluster_name,
251250
add_prefix = add_prefix)
@@ -420,7 +419,7 @@ createClusterST <- function(area,
420419
## add constraint(s) ----
421420
if(!is.null(constraints_properties))
422421
.add_storage_constraint(area = area,
423-
cluster_name = cluster_name_ori,
422+
cluster_name = cluster_name,
424423
constraints_properties = constraints_properties,
425424
constraints_ts = constraints_ts,
426425
overwrite = overwrite,

R/editClusterST.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ editClusterST <- function(area,
165165
}
166166

167167
## Standardize cluster name + prefix ----
168-
cluster_name_ori <- cluster_name
169168
cluster_name <- generate_cluster_name(area,
170169
cluster_name,
171170
add_prefix)
@@ -362,7 +361,7 @@ editClusterST <- function(area,
362361

363362
## Optional constraints ----
364363
.edit_storage_constraints(area = area,
365-
cluster_name = cluster_name_ori,
364+
cluster_name = cluster_name,
366365
constraints_properties = constraints_properties,
367366
constraints_ts = constraints_ts,
368367
opts = opts)

R/removeCluster.R

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ removeClusterST <- function(area,
8181
add_prefix = TRUE,
8282
cluster_type = c("thermal", "renewables", "st-storage"),
8383
opts = antaresRead::simOptions()) {
84-
8584
cluster_type <- match.arg(cluster_type)
8685

8786
# tolower area ----
@@ -169,9 +168,18 @@ removeClusterST <- function(area,
169168
}
170169

171170
dirs_to_remove <- file.path(clustertypePath, subdirs_to_remove, area)
171+
172+
# remove dir "constraints" (st-storage)
173+
if(opts$antaresVersion>=920 & cluster_type %in% "st-storage")
174+
dirs_to_remove <- file.path(clustertypePath,
175+
append(subdirs_to_remove, "constraints"),
176+
area)
177+
178+
# if list.ini is empty, area/series area/constraints can be removed
172179
if (length(previous_params) > 0) {
173180
dirs_to_remove <- file.path(dirs_to_remove, cluster_name)
174181
}
182+
175183
lapply(dirs_to_remove, unlink, recursive = TRUE)
176184

177185
# Maj simulation

tests/testthat/test-RemoveClusterST.R

Lines changed: 127 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,77 @@ suppressWarnings(
179179
area_test_clust = "al"
180180
createArea(name = area_test_clust)
181181

182-
test_that("Remove cluster", {
182+
test_that("Remove cluster without constraints", {
183183
# at least need 1 st cluster
184+
name_no_prefix <- "remove_no_constraints"
184185
createClusterST(area = area_test_clust,
185-
cluster_name = "cluster_init")
186+
cluster_name = name_no_prefix)
186187

187188
# remove cluster
188189
removeClusterST(area = area_test_clust,
189-
cluster_name = "cluster_init")
190+
cluster_name = name_no_prefix)
190191

191192
# removed from ini file ?
192193

194+
# read prop
195+
path_st_ini <- file.path("input",
196+
"st-storage",
197+
"clusters",
198+
area_test_clust,
199+
"list")
200+
201+
read_ini <- antaresRead::readIni(path_st_ini)
202+
target_prop <- read_ini[[paste(area_test_clust,
203+
name_no_prefix,
204+
sep = "_")]]
205+
206+
expect_null(target_prop)
207+
208+
# remove directory ?
209+
path_st_dir <- file.path("input",
210+
"st-storage",
211+
"clusters",
212+
"series",
213+
area_test_clust,
214+
paste0(area_test_clust, "_",name_no_prefix))
215+
216+
expect_false(dir.exists(path_st_dir))
217+
})
218+
219+
220+
test_that("Remove cluster with constraints", {
221+
# given
222+
name_no_prefix <- "remove_constraints"
223+
224+
constraints_properties <- list(
225+
"withdrawal-1"=list(
226+
variable = "withdrawal",
227+
operator = "equal",
228+
hours = c("[1,3,5]",
229+
"[120,121,122,123,124,125,126,127,128]")
230+
),
231+
"netting-1"=list(
232+
variable = "netting",
233+
operator = "less",
234+
hours = c("[1, 168]")
235+
))
236+
237+
good_ts <- data.table::as.data.table(matrix(10, 8760))
238+
constraints_ts <- list(
239+
"withdrawal-1"=good_ts,
240+
"netting-1"=good_ts)
241+
242+
createClusterST(area = area_test_clust,
243+
cluster_name = name_no_prefix,
244+
constraints_properties = constraints_properties,
245+
constraints_ts = constraints_ts)
246+
247+
# when
248+
# remove cluster
249+
removeClusterST(area = area_test_clust,
250+
cluster_name = name_no_prefix)
251+
252+
# then
193253
# read prop
194254
path_st_ini <- file.path("input",
195255
"st-storage",
@@ -210,10 +270,73 @@ test_that("Remove cluster", {
210270
"clusters",
211271
"series",
212272
area_test_clust,
213-
"al_cluster_init")
273+
paste0(area_test_clust, "_",name_no_prefix))
214274

215275
expect_false(dir.exists(path_st_dir))
216276
})
217277

278+
test_that("Remove cluster with multiple cluster/constraints", {
279+
# given
280+
name_no_prefix <- "remove_constraints1"
281+
282+
constraints_properties <- list(
283+
"withdrawal-1"=list(
284+
variable = "withdrawal",
285+
operator = "equal",
286+
hours = c("[1,3,5]",
287+
"[120,121,122,123,124,125,126,127,128]")
288+
),
289+
"netting-1"=list(
290+
variable = "netting",
291+
operator = "less",
292+
hours = c("[1, 168]")
293+
))
294+
295+
good_ts <- data.table::as.data.table(matrix(10, 8760))
296+
constraints_ts <- list(
297+
"withdrawal-1"=good_ts,
298+
"netting-1"=good_ts)
299+
300+
createClusterST(area = area_test_clust,
301+
cluster_name = name_no_prefix,
302+
constraints_properties = constraints_properties,
303+
constraints_ts = constraints_ts)
304+
305+
name_no_prefix2 <- "remove_constraints2"
306+
307+
createClusterST(area = area_test_clust,
308+
cluster_name = name_no_prefix2,
309+
constraints_properties = constraints_properties,
310+
constraints_ts = constraints_ts)
311+
312+
# when
313+
# remove cluster
314+
removeClusterST(area = area_test_clust,
315+
cluster_name = name_no_prefix)
316+
317+
# then
318+
# read prop
319+
path_st_ini <- file.path("input",
320+
"st-storage",
321+
"clusters",
322+
area_test_clust,
323+
"list")
324+
325+
read_ini <- antaresRead::readIni(path_st_ini)
326+
327+
expect_equal(names(read_ini), "al_remove_constraints2")
328+
329+
# remove directory ?
330+
path_st_dir <- file.path("input",
331+
"st-storage",
332+
"clusters",
333+
c("series", "constraints"),
334+
area_test_clust,
335+
paste0(area_test_clust, "_",name_no_prefix2))
336+
337+
expect_false(all(
338+
dir.exists(path_st_dir)))
339+
})
340+
218341
deleteStudy()
219342

tests/testthat/test-createClusterST.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ test_that("Add new binding constraint properties", {
717717
"st-storage",
718718
"constraints",
719719
area_test_clust,
720-
name_no_prefix,
720+
paste0(area_test_clust, "_",name_no_prefix),
721721
"additional-constraints")
722722

723723
read_ini <- antaresRead::readIni(path_st_ini)
@@ -784,7 +784,7 @@ test_that("Add new TS constraint", {
784784
"st-storage",
785785
"constraints",
786786
area_test_clust,
787-
name_no_prefix,
787+
paste0(area_test_clust, "_",name_no_prefix),
788788
"additional-constraints")
789789

790790
read_ini <- antaresRead::readIni(path_st_ini)
@@ -800,7 +800,7 @@ test_that("Add new TS constraint", {
800800
"st-storage",
801801
"constraints",
802802
area_test_clust,
803-
name_no_prefix,
803+
paste0(area_test_clust, "_",name_no_prefix),
804804
paste0("rhs_", names(constraints_ts), ".txt"))
805805

806806
# exist ?

tests/testthat/test-editClusterST.R

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ test_that("Edit NULL dont update file", {
547547
"st-storage",
548548
"constraints",
549549
area_test_clust,
550-
name_no_prefix,
550+
paste0(area_test_clust, "_",name_no_prefix),
551551
"additional-constraints")
552552

553553
read_ini <- antaresRead::readIni(path_st_ini)
@@ -649,7 +649,7 @@ test_that("Edit constraint properties", {
649649
"st-storage",
650650
"constraints",
651651
area_test_clust,
652-
name_no_prefix,
652+
paste0(area_test_clust, "_",name_no_prefix),
653653
"additional-constraints")
654654

655655
read_ini <- antaresRead::readIni(path_st_ini)
@@ -717,7 +717,7 @@ test_that("Edit one or more of list of constraints", {
717717
"st-storage",
718718
"constraints",
719719
area_test_clust,
720-
name_no_prefix,
720+
paste0(area_test_clust, "_",name_no_prefix),
721721
"additional-constraints")
722722

723723
read_ini <- antaresRead::readIni(path_st_ini)
@@ -770,7 +770,7 @@ test_that("Edit NULL dont create TS file", {
770770
"st-storage",
771771
"constraints",
772772
area_test_clust,
773-
name_no_prefix,
773+
paste0(area_test_clust, "_",name_no_prefix),
774774
ts_names)
775775

776776
expect_false(all(
@@ -819,7 +819,7 @@ test_that("Edit NULL dont update existing TS file", {
819819
"st-storage",
820820
"constraints",
821821
area_test_clust,
822-
name_no_prefix,
822+
paste0(area_test_clust, "_",name_no_prefix),
823823
ts_names)
824824

825825
ts_read <- lapply(path_ts_files, function(x){
@@ -870,7 +870,7 @@ test_that("Edit TS which do not exist", {
870870
"st-storage",
871871
"constraints",
872872
area_test_clust,
873-
name_no_prefix,
873+
paste0(area_test_clust, "_",name_no_prefix),
874874
ts_names)
875875

876876
ts_read <- lapply(path_ts_files, data.table::fread)
@@ -925,7 +925,7 @@ test_that("Edit existing TS", {
925925
"st-storage",
926926
"constraints",
927927
area_test_clust,
928-
name_no_prefix,
928+
paste0(area_test_clust, "_",name_no_prefix),
929929
ts_names)
930930

931931
ts_read <- lapply(path_ts_files, data.table::fread)
@@ -991,7 +991,7 @@ test_that("Edit properties + TS", {
991991
"st-storage",
992992
"constraints",
993993
area_test_clust,
994-
name_no_prefix,
994+
paste0(area_test_clust, "_",name_no_prefix),
995995
"additional-constraints")
996996

997997
read_ini <- antaresRead::readIni(path_st_ini)
@@ -1022,7 +1022,7 @@ test_that("Edit properties + TS", {
10221022
"st-storage",
10231023
"constraints",
10241024
area_test_clust,
1025-
name_no_prefix,
1025+
paste0(area_test_clust, "_",name_no_prefix),
10261026
ts_names)
10271027

10281028
ts_read <- lapply(path_ts_files, data.table::fread)

0 commit comments

Comments
 (0)