Skip to content

Commit

Permalink
storage paramaters version 8.8.0 (#177)
Browse files Browse the repository at this point in the history
* create + edit storage updated to manage v880 parameter + cleaning + tests

* createClusterST() updated with temporary solution for api part causing error if cluster already exist + tests

* create/edit cluster ST suppress warning in api part

* update docs + newmd
  • Loading branch information
berthetclement authored Jul 9, 2024
1 parent 8edae19 commit 6c165de
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 202 deletions.
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

NEW FEATURES (Antares v8.8) :

* `updateOptimizationSettings()` allows the user to update solver.log property
* `updateOptimizationSettings()` allows the user to update solver.log property
* `createClusterST()` / `editClusterST()` use new parameters and default values


BUGFIXES :

Expand Down
157 changes: 85 additions & 72 deletions R/createClusterST.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#' @template opts
#' @note
#' To write parameters to the `list.ini` file. You have function `storage_values_default()` who is called by default.
#' This function return `list` containing six parameters for cluster `st-storage`.
#' This function return `list` containing properties according study version for cluster `st-storage`.
#' See example section.
#'
#' To write data (.txt file), you have parameter for each output file :
Expand Down Expand Up @@ -108,42 +108,24 @@ createClusterST <- function(area,
" you should be using one of: ", paste(st_storage_group, collapse = ", ")
)

# check area existing in current study
area <- tolower(area)
# check area exsiting in current study
check_area_name(area, opts)
area <- tolower(area)

# To avoid failure in an unit test (API is mocked) we add this block
api_study <- is_api_study(opts)
if (api_study && is_api_mocked(opts)) {
cluster_exists <- FALSE
} else {
cluster_exists <- check_cluster_name(area, cluster_name, add_prefix, opts)
}

if (!api_study) {
if (cluster_exists & !overwrite) {
stop("Cluster already exists. Overwrite it with overwrite option or edit it with editClusterST().")
}
}
if (api_study) {
if (cluster_exists) {
stop("Cluster already exists. Edit it with editClusterST().")
}
}
##
# check parameters (ini file)
##
assertthat::assert_that(inherits(storage_parameters, "list"))

# static name of list parameters
names_parameters <- names(storage_values_default())
# static name of list parameters
names_parameters <- names(storage_values_default(opts = opts))

if(!all(names(storage_parameters) %in% names_parameters))
stop(append("Parameter 'st-storage' must be named with the following elements: ",
paste0(names_parameters, collapse= ", ")))

# check values parameters
.st_mandatory_params(list_values = storage_parameters)
# check values parameters
.st_mandatory_params(list_values = storage_parameters, opts = opts)


# DATA parameters : default value + name txt file
Expand All @@ -162,29 +144,48 @@ createClusterST <- function(area,

# check syntax ini parameters
params_cluster <- hyphenize_names(storage_parameters)
cluster_name <- generate_cluster_name(area, cluster_name, add_prefix)
params_cluster <- c(list(name = cluster_name, group = group),params_cluster)
if (add_prefix)
cluster_name <- paste(area, cluster_name, sep = "_")
params_cluster <- c(list(name = cluster_name, group = group),
params_cluster)

################# -
# API block
if (api_study) {
if (is_api_study(opts)) {
# format name for API
cluster_name <- transform_name_to_id(cluster_name)

# /!\ temporary solution /!\
# as the endpoint does not return an error if the cluster already exist
if(!is_api_mocked(opts)){
exists <- FALSE
suppressWarnings(
clusters <- readClusterSTDesc(opts = opts)
)
if (nrow(clusters) > 0) {
clusters_filtered <- clusters[clusters$area == tolower(area) &
clusters$cluster == cluster_name,]
exists <- nrow(clusters_filtered) > 0
}
if(exists)
stop("Cluster already exists. Edit it with editClusterST().")
}

params_cluster$name <- cluster_name

cmd <- api_command_generate(
action = "create_st_storage",
area_id = area,
parameters = params_cluster
)

api_command_register(cmd, opts = opts)
`if`(
should_command_be_executed(opts),
api_command_execute(cmd, opts = opts, text_alert = "{.emph create_st_storage}: {msg_api}"),
cli_command_registered("create_st_storage")
)

for (i in names(storage_value)){
if (!is.null(get(i))) {
# format name for API
Expand All @@ -209,11 +210,11 @@ createClusterST <- function(area,
)
}
}

return(invisible(opts))
}
########################## -


##
# parameters traitements
Expand All @@ -222,25 +223,31 @@ createClusterST <- function(area,
inputPath <- opts$inputPath
assertthat::assert_that(!is.null(inputPath) && file.exists(inputPath))

# named list for writing ini file
# params_cluster <- stats::setNames(object = list(params_cluster), nm = cluster_name)

# path to ini file containing clusters' name and parameters
path_clusters_ini <- file.path(inputPath, "st-storage", "clusters", tolower(area), "list.ini")

# read previous content of ini
previous_params <- readIniFile(file = path_clusters_ini)

if (tolower(cluster_name) %in% tolower(names(previous_params)) & overwrite){
ind_cluster <- which(tolower(names(previous_params)) %in% tolower(cluster_name))[1]
previous_params[[ind_cluster]] <- params_cluster
names(previous_params)[[ind_cluster]] <- cluster_name
} else {
previous_params[[cluster_name]] <- params_cluster
# already exists ?
if (tolower(cluster_name) %in% tolower(names(previous_params))
& !overwrite)
stop(paste(cluster_name, "already exist"))

# overwrite
if(overwrite){
if(tolower(cluster_name) %in% tolower(names(previous_params))){
ind_cluster <- which(tolower(names(previous_params)) %in%
tolower(cluster_name))[1]
previous_params[[ind_cluster]] <- params_cluster
names(previous_params)[[ind_cluster]] <- cluster_name
}
}

# add properties
previous_params[[cluster_name]] <- params_cluster

# params_cluster <- c(previous_params, params_cluster)

# write properties (all properties are overwritten)
writeIni(
listData = previous_params,
pathIni = path_clusters_ini,
Expand Down Expand Up @@ -278,40 +285,35 @@ createClusterST <- function(area,
})

invisible(res)

}


# check parameters (`list`)
#' @return `list`
.st_mandatory_params <- function(list_values){
.is_ratio(list_values$efficiency,
.st_mandatory_params <- function(list_values, opts){
.is_ratio(list_values[["efficiency"]],
"efficiency")

.check_capacity(list_values$reservoircapacity,
.check_capacity(list_values[["reservoircapacity"]],
"reservoircapacity")
# if(!list_values$reservoircapacity >= 0)
# stop("reservoircapacity must be >= 0",
# call. = FALSE)

.is_ratio(list_values$initiallevel,
.is_ratio(list_values[["initiallevel"]],
"initiallevel")

.check_capacity(list_values$withdrawalnominalcapacity,
.check_capacity(list_values[["withdrawalnominalcapacity"]],
"withdrawalnominalcapacity")
# if(!list_values$withdrawalnominalcapacity >= 0)
# stop("withdrawalnominalcapacity must be >= 0",
# call. = FALSE)

.check_capacity(list_values$injectionnominalcapacity,
.check_capacity(list_values[["injectionnominalcapacity"]],
"injectionnominalcapacity")
# if(!list_values$injectionnominalcapacity >= 0)
# stop("injectionnominalcapacity must be >= 0",
# call. = FALSE)

if(!is.null(list_values$initialleveloptim))
assertthat::assert_that(inherits(list_values$initialleveloptim,
"logical"))
if(!is.null(list_values[["initialleveloptim"]]))
assertthat::assert_that(inherits(list_values[["initialleveloptim"]],
"logical"))

if (opts$antaresVersion >= 880)
if(!is.null(list_values[["enabled"]]))
assertthat::assert_that(inherits(list_values[["enabled"]],
"logical"))
}

.is_ratio <- function(x, mess){
Expand All @@ -334,18 +336,29 @@ createClusterST <- function(area,

#' Short Term Storage Property List
#'
#' @description
#' Default values are returned according to study version
#'
#' @template opts
#' @return a named list
#' @export
#'
#' @examples
#' \dontrun{
#' storage_values_default()
storage_values_default <- function() {
list(efficiency = 1,
reservoircapacity = 0,
initiallevel = 0,
withdrawalnominalcapacity = 0,
injectionnominalcapacity = 0,
initialleveloptim = FALSE)
#' }
storage_values_default <- function(opts = simOptions()) {
lst_parameters <- list(efficiency = 1,
reservoircapacity = 0,
initiallevel = 0,
withdrawalnominalcapacity = 0,
injectionnominalcapacity = 0,
initialleveloptim = FALSE)

if (opts$antaresVersion >= 880){
lst_parameters$initiallevel <- 0.5
lst_parameters$enabled <- TRUE
}

return(lst_parameters)
}

46 changes: 30 additions & 16 deletions R/editClusterST.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,6 @@ editClusterST <- function(area,
check_active_ST(opts, check_dir = TRUE)
check_area_name(area, opts)

api_study <- is_api_study(opts)
# To avoid failure in an unit test (API is mocked) we add this block
if (api_study && is_api_mocked(opts)) {
cluster_exists <- TRUE
} else {
cluster_exists <- check_cluster_name(area, cluster_name, add_prefix, opts)
}
cl_name_msg <- generate_cluster_name(area, cluster_name, add_prefix)
assertthat::assert_that(cluster_exists, msg = paste0("Cluster '", cl_name_msg, "' does not exist. It can not be edited."))
# statics groups
st_storage_group <- c("PSP_open",
"PSP_closed",
Expand All @@ -72,14 +63,14 @@ editClusterST <- function(area,
assertthat::assert_that(inherits(storage_parameters, "list"))

# static name of list parameters
names_parameters <- names(storage_values_default())
names_parameters <- names(storage_values_default(opts = opts))

if(!all(names(storage_parameters) %in% names_parameters))
stop(append("Parameter 'st-storage' must be named with the following elements: ",
paste0(names_parameters, collapse= ", ")))

# check values parameters
.st_mandatory_params(list_values = storage_parameters)
.st_mandatory_params(list_values = storage_parameters, opts = opts)

# check list of parameters
params_cluster <- hyphenize_names(storage_parameters)
Expand All @@ -96,10 +87,28 @@ editClusterST <- function(area,
params_cluster$group <- NULL

##### API block ----
if (api_study) {
if (is_api_study(opts)) {
# format name for API
cluster_name <- transform_name_to_id(cluster_name)

# /!\ temporary solution /!\
# as the endpoint does not return an error if the cluster does not exist
if(!is_api_mocked(opts)){
exists <- FALSE
suppressWarnings(
clusters <- readClusterSTDesc(opts = opts)
)
if (nrow(clusters) > 0) {
clusters_filtered <- clusters[clusters$area == tolower(area) &
clusters$cluster == cluster_name,]
exists <- nrow(clusters_filtered) > 0
}
assertthat::assert_that(exists,
msg = paste0("Cluster '",
cluster_name,
"' does not exist. It can not be edited."))
}

# update parameters if something else than name
if (length(params_cluster) > 1) {
currPath <- "input/st-storage/clusters/%s/list/%s"
Expand Down Expand Up @@ -161,6 +170,15 @@ editClusterST <- function(area,
# read previous content of ini
previous_params <- readIniFile(file = path_clusters_ini)

if (!tolower(cluster_name) %in% tolower(names(previous_params)))
stop(
"'",
cluster_name,
"' doesn't exist, it can't be edited. You can create cluster with createCluster().",
call. = FALSE
)


# select existing cluster
ind_cluster <- which(tolower(names(previous_params)) %in%
tolower(cluster_name))[1]
Expand All @@ -176,14 +194,10 @@ editClusterST <- function(area,
)
}




##
# check DATA (series/)
##


# datas associated with cluster
path_txt_file <- file.path(opts$inputPath,
"st-storage",
Expand Down
2 changes: 1 addition & 1 deletion man/createClusterST.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6c165de

Please sign in to comment.