Skip to content

Commit 4df3f1e

Browse files
editClusterST() for 9.2 (#221)
* editClusterST() updated with constraints properties + TS + doc + tests
1 parent 984be8a commit 4df3f1e

File tree

5 files changed

+684
-15
lines changed

5 files changed

+684
-15
lines changed

R/createClusterST.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@
128128
#' cost_variation_injection = ratio_value,
129129
#' cost_variation_withdrawal = ratio_value)
130130
#'
131-
#' # Add optional constraints properties (name of cluster is prefixed by default)
132-
#' # remember to prefix in the list
131+
#' # Add optional constraints properties
133132
#'
134133
#' name_no_prefix <- "add_constraints"
135134
#'

R/editClusterST.R

Lines changed: 128 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,34 @@
7878
#' cost_level = ratio_data,
7979
#' cost_variation_injection = ratio_data,
8080
#' cost_variation_withdrawal = ratio_data)
81+
#'
82+
#' # Edit optional constraints properties
83+
#' # make a list with names for the section and mandatory parameters
84+
#'
85+
#' constraints_properties <- list(
86+
#' "withdrawal-1"= list(
87+
#' variable = "withdrawal",
88+
#' operator = "equal",
89+
#' hours = c("[1,3,5]",
90+
#' "[120,121,122,123,124,125,126,127,128]")
91+
#' ),
92+
#' "netting-1"= list(
93+
#' variable = "netting",
94+
#' operator = "less",
95+
#' hours = c("[1, 168]")
96+
#' ))
97+
#'
98+
#' # make a list for TS with same names like previous properties
99+
#' TS_values <- matrix(0.2, 8760)
100+
#'
101+
#' constraints_ts <- list(
102+
#' "withdrawal-1"=TS_values,
103+
#' "netting-1"=TS_values)0
104+
#'
105+
#' editClusterST(area = "areaname",
106+
#' cluster_name = "clustername",
107+
#' constraints_properties = constraints_properties,
108+
#' constraints_ts = constraints_ts)
81109
#' }
82110
#' @export
83111
editClusterST <- function(area,
@@ -94,6 +122,8 @@ editClusterST <- function(area,
94122
cost_level = NULL,
95123
cost_variation_injection = NULL,
96124
cost_variation_withdrawal = NULL,
125+
constraints_properties = NULL,
126+
constraints_ts = NULL,
97127
add_prefix = TRUE,
98128
opts = antaresRead::simOptions()) {
99129

@@ -135,6 +165,7 @@ editClusterST <- function(area,
135165
}
136166

137167
## Standardize cluster name + prefix ----
168+
cluster_name_ori <- cluster_name
138169
cluster_name <- generate_cluster_name(area,
139170
cluster_name,
140171
add_prefix)
@@ -256,9 +287,8 @@ editClusterST <- function(area,
256287

257288
# only edition if parameters are no NULL
258289
is_null_parameter <- all(names(params_cluster)%in%"name")
259-
if(is_null_parameter)
260-
warning("No edition for 'list.ini' file", call. = FALSE)
261-
else{
290+
291+
if(!is_null_parameter){
262292
# read previous content of ini
263293
previous_params <- readIniFile(file = path_clusters_ini)
264294

@@ -286,7 +316,6 @@ editClusterST <- function(area,
286316
)
287317
}
288318

289-
290319
## write TS ----
291320

292321
##
@@ -331,10 +360,105 @@ editClusterST <- function(area,
331360
}
332361
})
333362

363+
## Optional constraints ----
364+
.edit_storage_constraints(area = area,
365+
cluster_name = cluster_name_ori,
366+
constraints_properties = constraints_properties,
367+
constraints_ts = constraints_ts,
368+
opts = opts)
369+
334370
# Maj simulation
335371
suppressWarnings({
336372
res <- antaresRead::setSimulationPath(path = opts$studyPath, simulation = "input")
337373
})
338374

339375
invisible(res)
340376
}
377+
378+
379+
#' Edit constraints to a st-storage
380+
#'
381+
#' @inheritParams createClusterST
382+
#' @noRd
383+
.edit_storage_constraints <- function(area,
384+
cluster_name,
385+
constraints_properties,
386+
constraints_ts,
387+
opts){
388+
# constraints/<area id>/<cluster id>/additional-constraints.ini
389+
390+
# target dir
391+
dir_path <- file.path(opts$inputPath,
392+
"st-storage",
393+
"constraints",
394+
area,
395+
cluster_name)
396+
397+
# ini file path
398+
path_contraint_ini <- file.path(dir_path,
399+
"additional-constraints.ini")
400+
401+
# properties part
402+
if(!is.null(constraints_properties)){
403+
# read previous content of ini (if exists)
404+
previous_params <- .check_constaints_ini(path_file = path_contraint_ini,
405+
list_data_constraints = constraints_properties)
406+
407+
408+
# insert/update
409+
previous_params_updated <- utils::modifyList(x = previous_params,
410+
val = constraints_properties)
411+
412+
# write modified ini file
413+
writeIni(
414+
listData = previous_params_updated,
415+
pathIni = path_contraint_ini,
416+
overwrite = TRUE
417+
)
418+
}
419+
420+
# TS part
421+
if(!is.null(constraints_ts)){
422+
# check ini file => constraint name must be present
423+
424+
# check constraint name
425+
.check_constaints_ini(path_file = path_contraint_ini,
426+
list_data_constraints = constraints_ts)
427+
428+
# update/overwrite/create
429+
lapply(names(constraints_ts),
430+
function(x){
431+
fwrite(
432+
x = constraints_ts[[x]],
433+
row.names = FALSE,
434+
col.names = FALSE,
435+
sep = "\t",
436+
file = file.path(dir_path,
437+
paste0("rhs_", x, ".txt")))
438+
})
439+
}
440+
441+
}
442+
443+
444+
.check_constaints_ini <- function(path_file, list_data_constraints){
445+
# previous properties
446+
previous_params <- readIniFile(file = path_file)
447+
448+
## check constraints
449+
names_previous_params <- tolower(names(previous_params))
450+
constraints_names <- names(list_data_constraints)
451+
452+
if (!all(
453+
constraints_names %in% names_previous_params
454+
))
455+
stop("'",
456+
paste0(setdiff(constraints_names,
457+
names_previous_params),
458+
collapse = ", "),
459+
"' doesn't exist, it can't be edited. You can create constaints with createCluster().",
460+
call. = FALSE)
461+
462+
return(previous_params)
463+
}
464+

man/createClusterST.Rd

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/editClusterST.Rd

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

0 commit comments

Comments
 (0)