diff --git a/NEWS.md b/NEWS.md index ad1758f2..36f25334 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 : diff --git a/R/createClusterST.R b/R/createClusterST.R index e6bebf07..1077e692 100644 --- a/R/createClusterST.R +++ b/R/createClusterST.R @@ -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 : @@ -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 @@ -162,14 +144,33 @@ 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( @@ -177,14 +178,14 @@ createClusterST <- function(area, 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 @@ -209,11 +210,11 @@ createClusterST <- function(area, ) } } - + return(invisible(opts)) } ########################## - - + ## # parameters traitements @@ -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, @@ -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){ @@ -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) } - diff --git a/R/editClusterST.R b/R/editClusterST.R index 4bb683ce..17fedc6d 100644 --- a/R/editClusterST.R +++ b/R/editClusterST.R @@ -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", @@ -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) @@ -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" @@ -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] @@ -176,14 +194,10 @@ editClusterST <- function(area, ) } - - - ## # check DATA (series/) ## - # datas associated with cluster path_txt_file <- file.path(opts$inputPath, "st-storage", diff --git a/man/createClusterST.Rd b/man/createClusterST.Rd index d3e8b526..c52860c0 100644 --- a/man/createClusterST.Rd +++ b/man/createClusterST.Rd @@ -55,7 +55,7 @@ Create a new ST-storage cluster for >= v8.6.0 Antares studies. } \note{ To write parameters to the \code{list.ini} file. You have function \code{storage_values_default()} who is called by default. -This function return \code{list} containing six parameters for cluster \code{st-storage}. +This function return \code{list} containing properties according study version for cluster \code{st-storage}. See example section. To write data (.txt file), you have parameter for each output file : diff --git a/man/storage_values_default.Rd b/man/storage_values_default.Rd index b389b9e6..ba483658 100644 --- a/man/storage_values_default.Rd +++ b/man/storage_values_default.Rd @@ -4,14 +4,22 @@ \alias{storage_values_default} \title{Short Term Storage Property List} \usage{ -storage_values_default() +storage_values_default(opts = simOptions()) +} +\arguments{ +\item{opts}{List of simulation parameters returned by the function +\code{\link[antaresRead:setSimulationPath]{antaresRead::setSimulationPath()}}} } \value{ +An updated list containing various information about the simulation. + a named list } \description{ -Short Term Storage Property List +Default values are returned according to study version } \examples{ +\dontrun{ storage_values_default() } +} diff --git a/tests/testthat/test-createClusterST.R b/tests/testthat/test-createClusterST.R index 604cf497..04b65c34 100644 --- a/tests/testthat/test-createClusterST.R +++ b/tests/testthat/test-createClusterST.R @@ -1,5 +1,5 @@ - +# >=860 ---- test_that("Create short-term storage cluster (new feature v8.6)",{ ## basics errors cases ---- suppressWarnings( @@ -28,7 +28,8 @@ test_that("Create short-term storage cluster (new feature v8.6)",{ # cluster already exist in given area, with same name and group createClusterST(area_test_clust, - cluster_test_name, group_test_name, + cluster_test_name, + group_test_name, add_prefix = TRUE) testthat::expect_error(createClusterST(area_test_clust, @@ -55,7 +56,7 @@ test_that("Create short-term storage cluster (new feature v8.6)",{ info_clusters <- readClusterSTDesc() info_clusters <- info_clusters[cluster %in% namecluster_check, ] - # default values + # default values (only v860 properties) default_values <- storage_values_default() info_clusters <- info_clusters[, .SD, .SDcols= names(default_values)] @@ -172,112 +173,6 @@ test_that("Create short-term storage cluster (new feature v8.6)",{ }) -test_that("Test the behaviour of createClusterST() if the ST cluster already exists", { - - ant_version <- "8.6.0" - st_test <- paste0("my_study_860_", paste0(sample(letters,5),collapse = "")) - suppressWarnings(opts <- createStudy(path = pathstd, study_name = st_test, antares_version = ant_version)) - area <- "zone51" - createArea(area) - suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input")) - - val <- 0.7 - val_mat <- matrix(val, 8760) - cl_name <- "test_storage" - createClusterST(area = area, - cluster_name = cl_name, - storage_parameters = storage_values_default()[1], - PMAX_injection = val_mat, - PMAX_withdrawal = val_mat, - inflows = val_mat, - lower_rule_curve = val_mat, - upper_rule_curve = val_mat, - opts = opts) - - suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input")) - - ## createClusterST() - # With overwrite FALSE - expect_error(createClusterST(area = area, - cluster_name = cl_name, - storage_parameters = storage_values_default()[1], - PMAX_injection = val_mat, - PMAX_withdrawal = val_mat, - inflows = val_mat, - lower_rule_curve = val_mat, - upper_rule_curve = val_mat, - overwrite = FALSE, - opts = opts), regexp = "Cluster already exists.") - - # With overwrite TRUE - expect_no_error(createClusterST(area = area, - cluster_name = cl_name, - storage_parameters = storage_values_default()[1], - PMAX_injection = val_mat, - PMAX_withdrawal = val_mat, - inflows = val_mat, - lower_rule_curve = val_mat, - upper_rule_curve = val_mat, - overwrite = TRUE, - opts = opts)) - - # Test case insensitive - cl_name_2 <- "clUstEr_st_tEst_crEAtE2" - expect_no_error(createClusterST(area = area, - cluster_name = cl_name_2, - storage_parameters = storage_values_default()[1], - PMAX_injection = val_mat, - PMAX_withdrawal = val_mat, - inflows = val_mat, - lower_rule_curve = val_mat, - upper_rule_curve = val_mat, - overwrite = FALSE, - opts = simOptions())) - - expect_error(createClusterST(area = toupper(area), - cluster_name = toupper(cl_name_2), - storage_parameters = storage_values_default()[1], - PMAX_injection = val_mat, - PMAX_withdrawal = val_mat, - inflows = val_mat, - lower_rule_curve = val_mat, - upper_rule_curve = val_mat, - overwrite = FALSE, - opts = simOptions()), regexp = "Cluster already exists.") - - ## removeClusterST() - # On a non-existing area - expect_error(removeClusterST(area = "bla", - cluster_name = cl_name, - add_prefix = TRUE, - opts = simOptions()), regexp = "is not a valid area name") - - # On a non-existing cluster - expect_error(removeClusterST(area = area, - cluster_name = "not_a_cluster", - opts = simOptions()), regexp = "Cluster can not be removed.") - - # On an existing cluster - expect_no_error(removeClusterST(area = area, - cluster_name = cl_name, - add_prefix = TRUE, - opts = simOptions())) - - # On an existing cluster - idempotence - expect_error(removeClusterST(area = area, - cluster_name = cl_name, - opts = simOptions()), regexp = "Cluster can not be removed.") - - # On an existing cluster case insensitive - expect_no_error(removeClusterST(area = area, - cluster_name = "CLuSTeR_ST_TeST_CReaTe2", - add_prefix = TRUE, - opts = simOptions())) - - unlink(x = opts$studyPath, recursive = TRUE) -}) - - # API ---- test_that("API Command test for createClusterST", { @@ -365,3 +260,37 @@ test_that("API Command test for createClusterST", { testthat::expect_true(all(unlist(names_file_api) %in% names_file_list)) }) + +# >=880 ---- + +test_that("Create short-term storage cluster (new feature v8.8.0)",{ + ## basics errors cases ---- + suppressWarnings( + createStudy(path = tempdir(), + study_name = "st-storage880", + antares_version = "8.8.0")) + + # default area with st cluster + area_test_clust = "al" + createArea(name = area_test_clust) + + # default + createClusterST(area = area_test_clust, + cluster_name = "default") + + read_prop <- readClusterSTDesc() + + # "enabled" must be present with TRUE values default + testthat::expect_true("enabled"%in%names(read_prop)) + testthat::expect_true(read_prop$enabled[1]%in%TRUE) + + deleteStudy() + }) + + + + + + + + diff --git a/tests/testthat/test-editClusterST.R b/tests/testthat/test-editClusterST.R index 263e9ee8..6c57d5c6 100644 --- a/tests/testthat/test-editClusterST.R +++ b/tests/testthat/test-editClusterST.R @@ -1,4 +1,5 @@ +# v860 ---- test_that("edit st-storage clusters (only for study >= v8.6.0" , { # global params for structure v8.6 ---- opts_test <-createStudy(path = tempdir(), @@ -46,7 +47,7 @@ test_that("edit st-storage clusters (only for study >= v8.6.0" , { group = "Other1", add_prefix = FALSE, opts = opts_test), - regexp = "'casper' does not exist") + regexp = "'casper' doesn't exist") ## default edition cluster ---- # if all parameters are NULL => no edition of ini and data .txt @@ -236,3 +237,40 @@ test_that("API Command test for editClusterST", { names_file_list)) }) +# v880 ---- +test_that("Edit short-term storage cluster (new feature v8.8.0)",{ + ## basics errors cases ---- + suppressWarnings( + createStudy(path = tempdir(), + study_name = "st-storage880", + antares_version = "8.8.0")) + + # default area with st cluster + area_test_clust = "al" + createArea(name = area_test_clust) + + # default + createClusterST(area = area_test_clust, + cluster_name = "default") + + # edit + list_params <- storage_values_default() + list_params$efficiency <- 0.5 + list_params$reservoircapacity <- 50 + list_params$enabled <- FALSE + + editClusterST(area = area_test_clust, + cluster_name = "default", + storage_parameters = list_params) + + # read properties + st_params <- readClusterSTDesc() + + # "enabled" must be present + testthat::expect_true("enabled"%in%names(st_params)) + testthat::expect_true(st_params$enabled[1]%in%FALSE) + + deleteStudy() +}) + +