From d36f8ce7cc70c21489a53ac921d46b208fa9627e Mon Sep 17 00:00:00 2001 From: BERTHET Clement Ext Date: Thu, 20 Apr 2023 15:12:07 +0200 Subject: [PATCH 01/74] new release 0.5.2 --- NEWS.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/NEWS.md b/NEWS.md index eb21673e..703abd3b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,12 @@ + + +# antaresEditObject 0.5.2 + +### Breaking changes (Antares v8.6) : + +* Existing function `createBindingConstraint()` / `createBindingConstraintBulk()` has new arguments `group`(cf. Antares v8.6 changelog) + * Integration of the scenario of the coupling constraints of second member + # antaresEditObject 0.5.1 NEW FEATURES : From 21acb0445fa25f1d070fa6ac4b9d7bd9d729c149 Mon Sep 17 00:00:00 2001 From: BERTHET Clement Ext Date: Thu, 20 Apr 2023 15:13:23 +0200 Subject: [PATCH 02/74] CreateBindingConstraint dev v860 + tests --- R/createBindingConstraint.R | 101 ++++++++++++++++-- ...nstraint.Rd => createBindingConstraint.Rd} | 13 ++- tests/testthat/helper_init.R | 11 ++ tests/testthat/test-createBindingConstraint.R | 93 +++++++++++++++- 4 files changed, 209 insertions(+), 9 deletions(-) rename man/{create-binding-constraint.Rd => createBindingConstraint.Rd} (85%) diff --git a/R/createBindingConstraint.R b/R/createBindingConstraint.R index bd47c78c..83fdd642 100644 --- a/R/createBindingConstraint.R +++ b/R/createBindingConstraint.R @@ -17,15 +17,23 @@ #' @param filter_year_by_year Marginal price granularity for year by year #' @param filter_synthesis Marginal price granularity for synthesis #' @param coefficients A named vector containing the coefficients used by the constraint. +#' @param group "character" group of the constraint, default value : "default group" #' @param overwrite If the constraint already exist, overwrite the previous value. #' #' @template opts #' #' @seealso [editBindingConstraint()] to edit existing binding constraints, [removeBindingConstraint()] to remove binding constraints. #' +#' According to Antares version, usage may vary : +#' +#' **< v8.6.0** : For each constraint name, a .txt file containing 3 time series `"less", "greater", "equal"` +#' +#' **>= v8.6.0** : For each constraint name, one file .txt containing `_lt.txt, _gt.txt, _eq.txt` +#' Parameter `values` must be named `list` ("lt", "gt", "eq") containing `data.frame` scenarized +#' #' @export #' -#' @name create-binding-constraint +#' @name createBindingConstraint #' #' @importFrom antaresRead getLinks setSimulationPath #' @importFrom utils write.table @@ -73,6 +81,7 @@ createBindingConstraint <- function(name, filter_year_by_year = "hourly, daily, weekly, monthly, annual", filter_synthesis = "hourly, daily, weekly, monthly, annual", coefficients = NULL, + group = NULL, overwrite = FALSE, opts = antaresRead::simOptions()) { @@ -108,6 +117,18 @@ createBindingConstraint <- function(name, pathIni <- file.path(opts$inputPath, "bindingconstraints/bindingconstraints.ini") bindingConstraints <- readIniFile(pathIni, stringsAsFactors = FALSE) + # v860 + if(opts$antaresVersion>=860){ + if(is.null(group)) + group <- "default group" + + if(!is.null(values)){ + assertthat::assert_that(inherits(values, "list")) + if(!all(names(values)%in%c("lt", "gt", "eq"))) + stop("Put for 'values' argument, named 'list' => see Doc `?createBindingConstraint`") + } + } + bindingConstraints <- createBindingConstraint_( bindingConstraints, name, @@ -119,6 +140,7 @@ createBindingConstraint <- function(name, filter_year_by_year, filter_synthesis, coefficients, + group, overwrite, links = antaresRead::getLinks(opts = opts, namesOnly = TRUE), opts = opts @@ -136,6 +158,7 @@ createBindingConstraint <- function(name, } +#' @importFrom data.table fwrite createBindingConstraint_ <- function(bindingConstraints, name, id, @@ -146,6 +169,7 @@ createBindingConstraint_ <- function(bindingConstraints, filter_year_by_year = "hourly, daily, weekly, monthly, annual", filter_synthesis = "hourly, daily, weekly, monthly, annual", coefficients, + group, overwrite, links, opts) { @@ -176,6 +200,10 @@ createBindingConstraint_ <- function(bindingConstraints, iniParams$`filter-synthesis` <- filter_synthesis } + # v860 + if(opts$antaresVersion>=860) + iniParams$group <- group + # Check coefficients if (!is.null(coefficients)) { links <- as.character(links) @@ -211,15 +239,76 @@ createBindingConstraint_ <- function(bindingConstraints, bindingConstraints[[indexBC]] <- c(iniParams, coefficients) ## Values - values <- .valueCheck(values, timeStep) + if(opts$antaresVersion>=860 & !is.null(values)){ + values <- .valueCheck860(values, timeStep) + }else + values <- .valueCheck(values, timeStep) # Write values - pathValues <- file.path(opts$inputPath, "bindingconstraints", paste0(id, ".txt")) - data.table::fwrite(x = data.table::as.data.table(values), file = pathValues, col.names = FALSE, row.names = FALSE, sep = "\t") - + # v860 + if(opts$antaresVersion>=860){ + names_order_ts <- c("lt", "gt", "eq") + name_file <- paste0(id, "_", names_order_ts, ".txt") + + up_path <- file.path(opts$inputPath, "bindingconstraints", name_file) + + lapply(up_path, function(x, df_ts= values, vect_path= up_path){ + index <- grep(x = up_path, pattern = x) + fwrite(x = data.table::as.data.table(df_ts[[index]]), + file = x, + col.names = FALSE, + row.names = FALSE, + sep = "\t") + }) + }else{ + pathValues <- file.path(opts$inputPath, "bindingconstraints", paste0(id, ".txt")) + data.table::fwrite(x = data.table::as.data.table(values), + file = pathValues, + col.names = FALSE, + row.names = FALSE, + sep = "\t") + } return(bindingConstraints) } +# v860 +.valueCheck860 <- function(values, timeStep){ + # check nrow Vs timeStep + nrows <- switch(timeStep, + hourly = 24*366, + daily = 366, + weekly = 366, + monthly = 12, + annual = 1) + + list_checked <- sapply(names(values), function(x, list_in= values, check_standard_rows= nrows){ + list_work <- list_in[[x]] + + # one column scenario + if(ncol(list_work)==1){ + if (NROW(list_work) == 24*365) + list_work <- rbind(list_work, matrix(rep(0, 24*1), ncol = 1)) + if (NROW(list_work) == 365) + list_work <- rbind(list_work, matrix(rep(0, 1), ncol = 1)) + if (! NROW(list_work) %in% c(0, check_standard_rows)) + stop("Incorrect number of rows according to the timeStep") + }else{# scenarized columns + if(dim(list_work)[1]==24*365) + list_work <- rbind(list_work, + matrix(rep(0, 24*dim(list_work)[2]), + ncol = dim(list_work)[2])) + if(dim(list_work)[1]==365) + list_work <- rbind(list_work, + matrix(rep(0, dim(list_work)[2]), + ncol = dim(list_work)[2])) + if (! dim(list_work)[1] %in% c(0, check_standard_rows)) + stop("Incorrect number of rows according to the timeStep") + } + list_work + }, simplify = FALSE) + list_checked +} + .valueCheck <- function(values, timeStep) { @@ -273,7 +362,7 @@ createBindingConstraint_ <- function(bindingConstraints, #' **Warning** all arguments for creating a binding constraints must be provided, see examples. #' @export #' -#' @rdname create-binding-constraint +#' @rdname createBindingConstraint createBindingConstraintBulk <- function(constraints, opts = antaresRead::simOptions()) { assertthat::assert_that(inherits(opts, "simOptions")) diff --git a/man/create-binding-constraint.Rd b/man/createBindingConstraint.Rd similarity index 85% rename from man/create-binding-constraint.Rd rename to man/createBindingConstraint.Rd index ace476d5..40a433bd 100644 --- a/man/create-binding-constraint.Rd +++ b/man/createBindingConstraint.Rd @@ -1,7 +1,6 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/createBindingConstraint.R -\name{create-binding-constraint} -\alias{create-binding-constraint} +\name{createBindingConstraint} \alias{createBindingConstraint} \alias{createBindingConstraintBulk} \title{Create a binding constraint} @@ -16,6 +15,7 @@ createBindingConstraint( filter_year_by_year = "hourly, daily, weekly, monthly, annual", filter_synthesis = "hourly, daily, weekly, monthly, annual", coefficients = NULL, + group = NULL, overwrite = FALSE, opts = antaresRead::simOptions() ) @@ -42,6 +42,8 @@ It contains one line per time step and three columns "less", "greater" and "equa \item{coefficients}{A named vector containing the coefficients used by the constraint.} +\item{group}{"character" group of the constraint, default value : "default group"} + \item{overwrite}{If the constraint already exist, overwrite the previous value.} \item{opts}{List of simulation parameters returned by the function @@ -96,4 +98,11 @@ createBindingConstraintBulk(bindings_constraints) } \seealso{ \code{\link[=editBindingConstraint]{editBindingConstraint()}} to edit existing binding constraints, \code{\link[=removeBindingConstraint]{removeBindingConstraint()}} to remove binding constraints. + +#' According to Antares version, usage may vary : + +\strong{< v8.6.0} : For each constraint name, a .txt file containing 3 time series \verb{"less", "greater", "equal"} + +\strong{>= v8.6.0} : For each constraint name, one file .txt containing \verb{_lt.txt, _gt.txt, _eq.txt} +Parameter \code{values} must be named \code{list} ("lt", "gt", "eq") containing \code{data.frame} scenarized } diff --git a/tests/testthat/helper_init.R b/tests/testthat/helper_init.R index b582d28a..3ff4c59b 100644 --- a/tests/testthat/helper_init.R +++ b/tests/testthat/helper_init.R @@ -26,3 +26,14 @@ setup_study <- function(study, sourcedir) { } } + +# study v850 ---- +sourcedir850 <- system.file("test_v8", package = "antaresRead") +studies850 <- list.files(sourcedir850, pattern = "\\.tar\\.gz$", full.names = TRUE) +studies850 <- studies850[grep(x = studies850, pattern = "v85")] + +# untar etude +path_850 <- file.path(tempdir(), "studyv850") +untar(studies850[1], exdir = path_850) # v85 +study_temp_path <- file.path(path_850, "test_case") + diff --git a/tests/testthat/test-createBindingConstraint.R b/tests/testthat/test-createBindingConstraint.R index 711b1c10..4a20dd51 100644 --- a/tests/testthat/test-createBindingConstraint.R +++ b/tests/testthat/test-createBindingConstraint.R @@ -1,5 +1,5 @@ - +# v7 ---- context("Function createBindingConstraint") @@ -175,3 +175,94 @@ sapply(studies, function(study) { }) +# v8.6 ---- + +## Global data +# read / open template study +opts_v850 <- antaresRead::setSimulationPath(study_temp_path, "input") + +# areas list +antaresRead::getAreas(opts = opts_v850) + +# remove BC none v860 +names_bc_to_remove <- names(readBindingConstraints(opts = opts_v850)) + +lapply(names_bc_to_remove, + removeBindingConstraint, + opts = simOptions()) + +# temporary to test with "860" +# force version +opts_v850$antaresVersion <- 860 + +# scenarized data +n <- 10 +lt_data <- matrix(data = rep(1, 8760 * n), ncol = n) +gt_data <- matrix(data = rep(2, 8760 * n), ncol = n) +eq_data <- matrix(data = rep(3, 8760 * n), ncol = n) + +scenar_values <- list(lt= lt_data, + gt= gt_data, + eq= eq_data) + +test_that("createBindingConstraint v8.6", { + + # create binding constraint (default group value) + createBindingConstraint( + name = "myconstraint", + values = scenar_values, + enabled = FALSE, + timeStep = "hourly", + operator = "both", + coefficients = c("al%gr" = 1), + opts = opts_v850 + ) + + # tests + testthat::expect_true("myconstraint" %in% + names(readBindingConstraints(opts = opts_v850))) + + # remove BC + removeBindingConstraint(name = "myconstraint", + opts = opts_v850) + +}) + + +test_that("createBindingConstraintBulk v8.6", { + # Prepare data for constraints + bindings_constraints <- lapply( + X = seq_len(10), + FUN = function(i) { + # use arguments of createBindingConstraint() + # all arguments must be provided ! + list( + name = paste0("constraints", i), + id = paste0("constraints", i), + values = scenar_values, + enabled = FALSE, + timeStep = "hourly", + operator = "both", + coefficients = c("al%gr" = 1), + group= "groupv860", + overwrite = TRUE + ) + } + ) + # create all constraints + createBindingConstraintBulk(bindings_constraints, opts = opts_v850) + + # tests + testthat::expect_true("constraints1" %in% + names(readBindingConstraints(opts = opts_v850))) + testthat::expect_true("constraints10" %in% + names(readBindingConstraints(opts = opts_v850))) +}) + +test_that("editBindingConstraint v8.6", { + +}) + +test_that("removeBindingConstraint v8.6", { + +}) \ No newline at end of file From 67038f1dbe5b5ca9e9903c6730afd88bb03afbd5 Mon Sep 17 00:00:00 2001 From: BERTHET Clement Ext Date: Thu, 20 Apr 2023 15:13:50 +0200 Subject: [PATCH 03/74] EditBindingConstraint v860 maj doc --- R/editBindingConstraint.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/editBindingConstraint.R b/R/editBindingConstraint.R index d8eca1a8..4ac93774 100644 --- a/R/editBindingConstraint.R +++ b/R/editBindingConstraint.R @@ -6,7 +6,7 @@ #' Update an existing binding constraint in an Antares study. #' #' -#' @inheritParams create-binding-constraint +#' @inheritParams createBindingConstraint #' @template opts #' #' @seealso [createBindingConstraint()] to create new binding constraints, [removeBindingConstraint()] to remove binding constraints. From ac56c4bd3fbe76cf5ed52f06f3aae1d8fe3c4178 Mon Sep 17 00:00:00 2001 From: BERTHET Clement Ext Date: Thu, 27 Apr 2023 15:08:29 +0200 Subject: [PATCH 04/74] editBindingConstraint now compatible version >= 860 + tests --- DESCRIPTION | 21 +-- NEWS.md | 5 +- R/createBindingConstraint.R | 4 +- R/editBindingConstraint.R | 107 +++++++++++--- man/createBindingConstraint.Rd | 2 +- man/editBindingConstraint.Rd | 4 + tests/testthat/helper_init.R | 21 ++- tests/testthat/test-createBindingConstraint.R | 14 +- tests/testthat/test-editBindingConstraint.R | 135 ++++++++++++++++++ tests/testthat/test-updateBindingConstraint.R | 104 +++++++------- 10 files changed, 319 insertions(+), 98 deletions(-) create mode 100644 tests/testthat/test-editBindingConstraint.R diff --git a/DESCRIPTION b/DESCRIPTION index bedf3ab6..bd5ada1d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,19 +1,20 @@ Package: antaresEditObject Type: Package Title: Edit an 'Antares' Simulation -Version: 0.5.1 +Version: 0.5.2 Authors@R: c( - person("Veronique", "Bachelier", email = "veronique.bachelier@rte-france.com", role = c("aut", "cre")), - person("Frederic", "Breant", role = "aut"), - person("Victor", "Perrier", role = c("aut")), - person("Baptiste", "Seguinot", role = "ctb"), - person("Benoit", "Thieurmel", role = "ctb"), - person("Titouan", "Robert", role = "ctb"), - person("Jalal-Edine", "Zawam", role = "ctb"), + person("Tatiana", "Vargas", email = "tatiana.vargas@rte-france.com", role = c("aut", "cre")), + person("Jalal-Edine", "ZAWAM", role = "aut"), + person("Frederic", "Breant", role = "ctb"), + person("Francois", "Guillem", role = "aut"), + person("Benoit", "Thieurmel", role = "aut"), + person("Titouan", "Robert", role = "aut"), + person("Victor", "Perrier", role = "ctb"), person("Etienne", "Sanchez", role = "ctb"), - person("Janus", "De Bondt", role = "ctb"), person("Assil", "Mansouri", role = "ctb"), - person("RTE", role = "cph")) + person("Clement", "Berthet", role = "ctb"), + person("RTE", role = "cph") + ) Description: Edit an 'Antares' simulation before running it : create new areas, links, thermal clusters or binding constraints or edit existing ones. Update 'Antares' general & optimization settings. 'Antares' is an open source power system generator, more information available here : . diff --git a/NEWS.md b/NEWS.md index 703abd3b..8f9e55b5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,8 +4,9 @@ ### Breaking changes (Antares v8.6) : -* Existing function `createBindingConstraint()` / `createBindingConstraintBulk()` has new arguments `group`(cf. Antares v8.6 changelog) - * Integration of the scenario of the coupling constraints of second member +* Existing function `createBindingConstraint()` / `createBindingConstraintBulk()` has new arguments `group` +* Existing function `editBindingConstraint()` has new arguments `group` + * Integration of the scenario of the coupling constraints of second member (cf. Antares v8.6 changelog) # antaresEditObject 0.5.1 diff --git a/R/createBindingConstraint.R b/R/createBindingConstraint.R index 83fdd642..113479ec 100644 --- a/R/createBindingConstraint.R +++ b/R/createBindingConstraint.R @@ -239,9 +239,9 @@ createBindingConstraint_ <- function(bindingConstraints, bindingConstraints[[indexBC]] <- c(iniParams, coefficients) ## Values - if(opts$antaresVersion>=860 & !is.null(values)){ + if(opts$antaresVersion>=860 & !is.null(values)) values <- .valueCheck860(values, timeStep) - }else + else values <- .valueCheck(values, timeStep) # Write values diff --git a/R/editBindingConstraint.R b/R/editBindingConstraint.R index 4ac93774..32b1c687 100644 --- a/R/editBindingConstraint.R +++ b/R/editBindingConstraint.R @@ -3,10 +3,11 @@ #' @description #' `r antaresEditObject:::badge_api_ok()` #' -#' Update an existing binding constraint in an Antares study. +#' Update an existing binding constraint in an Antares study. +#' The key search value of the constraint is the `id` field #' -#' #' @inheritParams createBindingConstraint +#' @param group "character" group of the constraint, default value : "default group" #' @template opts #' #' @seealso [createBindingConstraint()] to create new binding constraints, [removeBindingConstraint()] to remove binding constraints. @@ -36,6 +37,7 @@ editBindingConstraint <- function(name, filter_year_by_year = NULL, filter_synthesis = NULL, coefficients = NULL, + group = NULL, opts = antaresRead::simOptions()) { assertthat::assert_that(inherits(opts, "simOptions")) @@ -67,10 +69,13 @@ editBindingConstraint <- function(name, return(invisible(opts)) } - valuesIn <- values - # Ini file + # valuesIn <- values + # check Ini file names constraints pathIni <- file.path(opts$inputPath, "bindingconstraints/bindingconstraints.ini") + + # initial parameter list bindingConstraints <- readIniFile(pathIni, stringsAsFactors = FALSE) + previds <- lapply(bindingConstraints, `[[`, "id") previds <- unlist(previds, use.names = FALSE) if(!id %in% previds){ @@ -81,6 +86,7 @@ editBindingConstraint <- function(name, bc_update_pos <- which(previds %in% id) bc_update <- bindingConstraints[[bc_update_pos]] + # Initial parameters of constraint to edit iniParams <- list( name = bc_update$name, id = bc_update$id, @@ -89,19 +95,35 @@ editBindingConstraint <- function(name, operator = bc_update$operator ) + # if(!is.null(name)) iniParams$name <- name + # if(!is.null(id)) iniParams$id <- id - if(!is.null(name)) iniParams$name <- name - if(!is.null(id)) iniParams$id <- id - if(!is.null(enabled)) iniParams$enabled <- enabled - if(!is.null(timeStep)) iniParams$type <- timeStep - if(!is.null(operator)) iniParams$operator <- operator - if(!is.null(filter_year_by_year)){ - if(opts$antaresVersion >= 832) iniParams$`filter-year-by-year` <- filter_year_by_year - } - if(!is.null(filter_synthesis)){ - if(opts$antaresVersion >= 832) iniParams$`filter-synthesis` <- filter_synthesis + # update parameters + # name can be different of id + if(!is.null(name)) + iniParams$name <- name + if(!is.null(enabled)) + iniParams$enabled <- enabled + if(!is.null(timeStep)) + iniParams$type <- timeStep + if(!is.null(operator)) + iniParams$operator <- operator + + # Marginal price granularity (v8.3.2) + if (opts$antaresVersion >= 832){ + if(!is.null(filter_year_by_year)) + iniParams$`filter-year-by-year` <- filter_year_by_year + if(!is.null(filter_synthesis)) + iniParams$`filter-synthesis` <- filter_synthesis } + # v860 + if(opts$antaresVersion>=860){ + if(!is.null(group)) + iniParams$group <- group + } + + # update constraint parameters with new parameters bindingConstraints[[bc_update_pos]]$name <- iniParams$name bindingConstraints[[bc_update_pos]]$id <- iniParams$id bindingConstraints[[bc_update_pos]]$enabled <- iniParams$enabled @@ -134,15 +156,64 @@ editBindingConstraint <- function(name, } } - values <- .valueCheck(values, bindingConstraints[[bc_update_pos]]$type) + # write txt files + # v860 + if(opts$antaresVersion>=860 & !is.null(values)) + values <- .valueCheck860(values, bindingConstraints[[bc_update_pos]]$type) + else + values <- .valueCheck(values, bindingConstraints[[bc_update_pos]]$type) # Write Ini writeIni(listData = bindingConstraints, pathIni = pathIni, overwrite = TRUE) # Write values - pathValues <- file.path(opts$inputPath, "bindingconstraints", paste0(id, ".txt")) - - if(!is.null(valuesIn))write.table(x = values, file = pathValues, col.names = FALSE, row.names = FALSE, sep = "\t") + # v860 + if(opts$antaresVersion>=860){ + if(!identical(values, character(0))){ + names_order_ts <- c("lt", "gt", "eq") + name_file <- paste0(id, "_", names_order_ts, ".txt") + + up_path <- file.path(opts$inputPath, "bindingconstraints", name_file) + + lapply(up_path, function(x, df_ts= values, vect_path= up_path){ + index <- grep(x = up_path, pattern = x) + fwrite(x = data.table::as.data.table(df_ts[[index]]), + file = x, + col.names = FALSE, + row.names = FALSE, + sep = "\t") + }) + } + + }else{ + pathValues <- file.path(opts$inputPath, + "bindingconstraints", + paste0(id, ".txt")) + + # read to check timestep + suppressWarnings( + file_r <- fread(pathValues) + ) + + # # check nrow Vs timeStep + # nrows <- switch(timeStep, + # hourly = 24*366, + # daily = 366, + # weekly = 366, + # monthly = 12, + # annual = 1) + # + # # check existing values + # if(!is.null(timeStep) & nrow(file_r)>0) + # if(!nrow(file_r) %in% nrows) + # stop("Incorrect number of rows according to the timeStep") + + if(!identical(values, character(0))) + write.table(x = values, + file = pathValues, + col.names = FALSE, + row.names = FALSE, sep = "\t") + } # Maj simulation suppressWarnings({ diff --git a/man/createBindingConstraint.Rd b/man/createBindingConstraint.Rd index 40a433bd..4bd20ec8 100644 --- a/man/createBindingConstraint.Rd +++ b/man/createBindingConstraint.Rd @@ -99,7 +99,7 @@ createBindingConstraintBulk(bindings_constraints) \seealso{ \code{\link[=editBindingConstraint]{editBindingConstraint()}} to edit existing binding constraints, \code{\link[=removeBindingConstraint]{removeBindingConstraint()}} to remove binding constraints. -#' According to Antares version, usage may vary : +According to Antares version, usage may vary : \strong{< v8.6.0} : For each constraint name, a .txt file containing 3 time series \verb{"less", "greater", "equal"} diff --git a/man/editBindingConstraint.Rd b/man/editBindingConstraint.Rd index 8c6e58bd..440b86a5 100644 --- a/man/editBindingConstraint.Rd +++ b/man/editBindingConstraint.Rd @@ -14,6 +14,7 @@ editBindingConstraint( filter_year_by_year = NULL, filter_synthesis = NULL, coefficients = NULL, + group = NULL, opts = antaresRead::simOptions() ) } @@ -37,6 +38,8 @@ It contains one line per time step and three columns "less", "greater" and "equa \item{coefficients}{A named vector containing the coefficients used by the constraint.} +\item{group}{"character" group of the constraint, default value : "default group"} + \item{opts}{List of simulation parameters returned by the function \code{\link[antaresRead:setSimulationPath]{antaresRead::setSimulationPath()}}} } @@ -47,6 +50,7 @@ An updated list containing various information about the simulation. \ifelse{html}{\figure{badge_api_ok.svg}{options: alt='Antares API OK'}}{Antares API: \strong{OK}} Update an existing binding constraint in an Antares study. +The key search value of the constraint is the \code{id} field } \examples{ \dontrun{ diff --git a/tests/testthat/helper_init.R b/tests/testthat/helper_init.R index 3ff4c59b..a6a94b12 100644 --- a/tests/testthat/helper_init.R +++ b/tests/testthat/helper_init.R @@ -29,11 +29,20 @@ setup_study <- function(study, sourcedir) { # study v850 ---- sourcedir850 <- system.file("test_v8", package = "antaresRead") -studies850 <- list.files(sourcedir850, pattern = "\\.tar\\.gz$", full.names = TRUE) -studies850 <- studies850[grep(x = studies850, pattern = "v85")] -# untar etude -path_850 <- file.path(tempdir(), "studyv850") -untar(studies850[1], exdir = path_850) # v85 -study_temp_path <- file.path(path_850, "test_case") +setup_study_850 <- function(dir_path){ + studies850 <- list.files(dir_path, pattern = "\\.tar\\.gz$", full.names = TRUE) + studies850 <- studies850[grep(x = studies850, pattern = "v85")] + # untar etude + path_850 <- file.path(tempdir(), "studyv850") + untar(studies850[1], exdir = path_850) # v85 + study_temp_path <- file.path(path_850, "test_case") + + assign("study_temp_path", + file.path(path_850, + "test_case"), + envir = globalenv()) +} + + diff --git a/tests/testthat/test-createBindingConstraint.R b/tests/testthat/test-createBindingConstraint.R index 4a20dd51..d6b6f293 100644 --- a/tests/testthat/test-createBindingConstraint.R +++ b/tests/testthat/test-createBindingConstraint.R @@ -179,6 +179,8 @@ sapply(studies, function(study) { ## Global data # read / open template study +setup_study_850(dir_path = sourcedir850) + opts_v850 <- antaresRead::setSimulationPath(study_temp_path, "input") # areas list @@ -221,11 +223,6 @@ test_that("createBindingConstraint v8.6", { # tests testthat::expect_true("myconstraint" %in% names(readBindingConstraints(opts = opts_v850))) - - # remove BC - removeBindingConstraint(name = "myconstraint", - opts = opts_v850) - }) @@ -257,12 +254,13 @@ test_that("createBindingConstraintBulk v8.6", { names(readBindingConstraints(opts = opts_v850))) testthat::expect_true("constraints10" %in% names(readBindingConstraints(opts = opts_v850))) -}) - -test_that("editBindingConstraint v8.6", { + # remove temporary study + unlink(x = study_temp_path, recursive = TRUE) }) + + test_that("removeBindingConstraint v8.6", { }) \ No newline at end of file diff --git a/tests/testthat/test-editBindingConstraint.R b/tests/testthat/test-editBindingConstraint.R new file mode 100644 index 00000000..d7f6e100 --- /dev/null +++ b/tests/testthat/test-editBindingConstraint.R @@ -0,0 +1,135 @@ +# v710 ---- + +test_that("editBindingConstraint v710", { + + setup_study(studies, sourcedir) + opts <- antaresRead::setSimulationPath(studyPath, "input") + + # reading bc + existing_bc <- antaresRead::readBindingConstraints() + bc_names <- names(antaresRead::readBindingConstraints()) + + details_bc <- existing_bc[[bc_names[1]]] + details_bc$coefs + + # edit BC + data_hourly <- matrix(data = rep(15, 8760 * 3), ncol = 3) + data_daily <- matrix(data = rep(15, 365 * 3), ncol = 3) + + editBindingConstraint(name = bc_names[1], + values = data_hourly, + timeStep = "hourly", + operator = "less", + coefficients = c("b%psp in"= 1.75, + "b%psp out"= 2), + opts = simOptions()) + + bc_modified <- antaresRead::readBindingConstraints() + new_coef <- bc_modified[[bc_names[1]]]$coefs + + # test + testthat::expect_true(all(new_coef %in% c(1.75, 2))) + + # remove temporary study + unlink(x = file.path(pathstd, "test_case"), recursive = TRUE) + +}) + +# v860 ---- + +## Global data +# read / open template study +setup_study_850(dir_path = sourcedir850) +opts_v850 <- antaresRead::setSimulationPath(study_temp_path, "input") + +# areas list +antaresRead::getAreas(opts = opts_v850) + +# remove BC none v860 +names_bc_to_remove <- names(readBindingConstraints(opts = opts_v850)) + +lapply(names_bc_to_remove, + removeBindingConstraint, + opts = simOptions()) + +# temporary to test with "860" +# force version +opts_v850$antaresVersion <- 860 + +# scenarized data + # hourly +n <- 10 +lt_data <- matrix(data = rep(1, 8760 * n), ncol = n) +gt_data <- matrix(data = rep(2, 8760 * n), ncol = n) +eq_data <- matrix(data = rep(3, 8760 * n), ncol = n) + +scenar_values_hourly <- list(lt= lt_data, + gt= gt_data, + eq= eq_data) + # daily +lt_data <- matrix(data = rep(1, 365 * n), ncol = n) +gt_data <- matrix(data = rep(2, 365 * n), ncol = n) +eq_data <- matrix(data = rep(3, 365 * n), ncol = n) + +scenar_values_daily <- list(lt= lt_data, + gt= gt_data, + eq= eq_data) + + +test_that("editBindingConstraint v8.6", { + + # create binding constraint (default group value) + createBindingConstraint( + name = "myconstraint", + values = scenar_values_hourly, + enabled = FALSE, + timeStep = "hourly", + operator = "both", + coefficients = c("al%gr" = 1), + opts = opts_v850 + ) + + bc_names_v860 <- names(readBindingConstraints(opts = opts_v850)) + + # edit BC with NULL VALUES + editBindingConstraint(name = bc_names_v860, + values = NULL, + timeStep = "daily", + operator = "both", + coefficients = c("al%gr"= 7.45), + opts = opts_v850) + + bc_modified <- readBindingConstraints(opts = opts_v850) + new_coef <- bc_modified[[bc_names_v860[1]]]$coefs + + # test + testthat::expect_true(all(new_coef %in% c(7.45))) + + # edit BC with "daily" VALUES + # add new coeff + editBindingConstraint(name = bc_names_v860, + values = scenar_values_daily, + timeStep = "daily", + operator = "both", + coefficients = c("00_pump_d%gr" = 12, + "gr%00_turb_d" = 0, + "al%gr"= 0.5), + opts = opts_v850) + + bc_modified <- readBindingConstraints(opts = opts_v850) + new_coef <- bc_modified[[bc_names_v860[1]]]$coefs + + # test coefs + testthat::expect_true(all(new_coef %in% c(0.5, 12.0, 0.0))) + + # test dim values "daily" + # the length of a standard year is 366 + res_dim_values <- lapply(bc_modified$myconstraint$values, dim) + res_dim_values <- lapply(res_dim_values, `[[`, 1) + res_dim_values <- unlist(res_dim_values) + testthat::expect_equal(mean(res_dim_values), 366) + + # remove temporary study + unlink(x = study_temp_path, recursive = TRUE) + +}) diff --git a/tests/testthat/test-updateBindingConstraint.R b/tests/testthat/test-updateBindingConstraint.R index 795fea7a..fcf508b0 100644 --- a/tests/testthat/test-updateBindingConstraint.R +++ b/tests/testthat/test-updateBindingConstraint.R @@ -1,56 +1,58 @@ context("Function editBindingConstraint") - -sapply(studies, function(study) { - - setup_study(study, sourcedir) - opts <- antaresRead::setSimulationPath(studyPath, "input") - - #Create a new binding constraint - createBindingConstraint( - name = "myconstraint", - values = matrix(data = rep(0, 8760 * 3), ncol = 3), - enabled = FALSE, - timeStep = "hourly", - operator = "both" - ) - - ###Write params - bc <- antaresRead::readBindingConstraints() - bc <- bc[["myconstraint"]] - editBindingConstraint("myconstraint", enabled = TRUE) - bc2 <- antaresRead::readBindingConstraints() - bc2 <- bc2[["myconstraint"]] - expect_true(bc2$enabled) - bc2$enabled <- FALSE - bc$values <- data.frame(bc$values) - bc2$values <- data.frame(bc2$values) - expect_true(identical(bc, bc2)) - editBindingConstraint("myconstraint", coefficients = c("a%b" = 10)) - - ##Write coef - bc <- antaresRead::readBindingConstraints() - expect_true(bc$myconstraint$coefs == c("a%b" = 10)) - editBindingConstraint("myconstraint", coefficients = c("a%b" = 100)) - bc <- antaresRead::readBindingConstraints() - expect_true(bc$myconstraint$coefs == c("a%b" = 100)) - editBindingConstraint("myconstraint", coefficients = c("b%c" = 10)) - bc <- antaresRead::readBindingConstraints() - expect_true(identical(bc$myconstraint$coefs,c("a%b" = 100, "b%c" = 10))) - - - ##Write values - - expect_true(sum(bc$myconstraint$values) == 0) - bc$myconstraint$timeStep - editBindingConstraint("myconstraint", values = matrix(data = rep(1, 8760 * 3), ncol = 3)) - bc <- antaresRead::readBindingConstraints() - expect_true(sum(bc$myconstraint$values) > 0 ) - - - # remove temporary study - unlink(x = file.path(pathstd, "test_case"), recursive = TRUE) - +test_that("editBindingConstraint tests", { + sapply(studies, function(study) { + + setup_study(study, sourcedir) + opts <- antaresRead::setSimulationPath(studyPath, "input") + + #Create a new binding constraint + createBindingConstraint( + name = "myconstraint", + values = matrix(data = rep(0, 8760 * 3), ncol = 3), + enabled = FALSE, + timeStep = "hourly", + operator = "both" + ) + + ###Write params + bc <- antaresRead::readBindingConstraints() + bc <- bc[["myconstraint"]] + editBindingConstraint("myconstraint", enabled = TRUE) + bc2 <- antaresRead::readBindingConstraints() + bc2 <- bc2[["myconstraint"]] + expect_true(bc2$enabled) + bc2$enabled <- FALSE + bc$values <- data.frame(bc$values) + bc2$values <- data.frame(bc2$values) + expect_true(identical(bc, bc2)) + editBindingConstraint("myconstraint", coefficients = c("a%b" = 10)) + + ##Write coef + bc <- antaresRead::readBindingConstraints() + expect_true(bc$myconstraint$coefs == c("a%b" = 10)) + editBindingConstraint("myconstraint", coefficients = c("a%b" = 100)) + bc <- antaresRead::readBindingConstraints() + expect_true(bc$myconstraint$coefs == c("a%b" = 100)) + editBindingConstraint("myconstraint", coefficients = c("b%c" = 10)) + bc <- antaresRead::readBindingConstraints() + expect_true(identical(bc$myconstraint$coefs,c("a%b" = 100, "b%c" = 10))) + + + ##Write values + + expect_true(sum(bc$myconstraint$values) == 0) + bc$myconstraint$timeStep + editBindingConstraint("myconstraint", values = matrix(data = rep(1, 8760 * 3), ncol = 3)) + bc <- antaresRead::readBindingConstraints() + expect_true(sum(bc$myconstraint$values) > 0 ) + + + # remove temporary study + unlink(x = file.path(pathstd, "test_case"), recursive = TRUE) + + }) }) + From faf531df49cf648443ca1a8b1af9fd0b5bc63384 Mon Sep 17 00:00:00 2001 From: BERTHET Clement Ext Date: Tue, 2 May 2023 17:02:35 +0200 Subject: [PATCH 05/74] remonveBindingConstraint compatible v860 + tests --- R/removeBindingConstraint.R | 21 ++++- tests/testthat/test-createBindingConstraint.R | 3 - tests/testthat/test-removeBindingConstraint.R | 94 +++++++++++++++++++ 3 files changed, 113 insertions(+), 5 deletions(-) create mode 100644 tests/testthat/test-removeBindingConstraint.R diff --git a/R/removeBindingConstraint.R b/R/removeBindingConstraint.R index 7c1f76a1..5529eb97 100644 --- a/R/removeBindingConstraint.R +++ b/R/removeBindingConstraint.R @@ -48,6 +48,7 @@ removeBindingConstraint <- function(name, opts = antaresRead::simOptions()) { namesbc <- unlist(lapply(bindingConstraints, `[[`, "name"), use.names = FALSE) + # suppression txt files + remove constraint from ini file for (i in name) { if (! i %in% namesbc) { warning(paste0("No binding constraint with name '", i, "'")) @@ -55,8 +56,24 @@ removeBindingConstraint <- function(name, opts = antaresRead::simOptions()) { index <- which(namesbc == i) id <- bindingConstraints[[index]]$id bindingConstraints[[index]] <- NULL - pathValues <- file.path(opts$inputPath, "bindingconstraints", paste0(id, ".txt")) - unlink(x = pathValues) + # v860 + if(opts$antaresVersion>=860){ + path_lt <- file.path(opts$inputPath, + sprintf("bindingconstraints/%s.txt", + paste0(id, "_lt"))) + path_gt <- file.path(opts$inputPath, + sprintf("bindingconstraints/%s.txt", + paste0(id, "_gt"))) + path_eq <- file.path(opts$inputPath, + sprintf("bindingconstraints/%s.txt", + paste0(id, "_eq"))) + lapply(c(path_lt, path_gt, path_eq), + unlink) + }else{ + pathValues <- file.path(opts$inputPath, "bindingconstraints", paste0(id, ".txt")) + unlink(x = pathValues) + } + namesbc <- unlist(lapply(bindingConstraints, `[[`, "name"), use.names = FALSE) names(bindingConstraints) <- as.character(seq_along(bindingConstraints) - 1) } diff --git a/tests/testthat/test-createBindingConstraint.R b/tests/testthat/test-createBindingConstraint.R index d6b6f293..eb283301 100644 --- a/tests/testthat/test-createBindingConstraint.R +++ b/tests/testthat/test-createBindingConstraint.R @@ -261,6 +261,3 @@ test_that("createBindingConstraintBulk v8.6", { -test_that("removeBindingConstraint v8.6", { - -}) \ No newline at end of file diff --git a/tests/testthat/test-removeBindingConstraint.R b/tests/testthat/test-removeBindingConstraint.R new file mode 100644 index 00000000..a729e002 --- /dev/null +++ b/tests/testthat/test-removeBindingConstraint.R @@ -0,0 +1,94 @@ + +# v860 ---- + +## Global data +# read / open template study +setup_study_850(dir_path = sourcedir850) +opts_v850 <- antaresRead::setSimulationPath(study_temp_path, "input") + +# areas list +antaresRead::getAreas(opts = opts_v850) + +# remove BC none v860 +names_bc_to_remove <- names(readBindingConstraints(opts = opts_v850)) + +lapply(names_bc_to_remove, + removeBindingConstraint, + opts = simOptions()) + +# temporary to test with "860" +# force version +opts_v850$antaresVersion <- 860 + +# scenarized data +# hourly +n <- 10 +lt_data <- matrix(data = rep(1, 8760 * n), ncol = n) +gt_data <- matrix(data = rep(2, 8760 * n), ncol = n) +eq_data <- matrix(data = rep(3, 8760 * n), ncol = n) + +scenar_values_hourly <- list(lt= lt_data, + gt= gt_data, + eq= eq_data) +# daily +lt_data <- matrix(data = rep(1, 365 * n), ncol = n) +gt_data <- matrix(data = rep(2, 365 * n), ncol = n) +eq_data <- matrix(data = rep(3, 365 * n), ncol = n) + +scenar_values_daily <- list(lt= lt_data, + gt= gt_data, + eq= eq_data) + +test_that("removeBindingConstraint v8.6", { + + # create binding constraint (default group value) + createBindingConstraint( + name = "myconstraint", + values = scenar_values_hourly, + enabled = FALSE, + timeStep = "hourly", + operator = "both", + coefficients = c("al%gr" = 1), + opts = opts_v850 + ) + + createBindingConstraint( + name = "myconstraint1", + values = scenar_values_hourly, + enabled = FALSE, + timeStep = "hourly", + operator = "both", + group = "group_test", + coefficients = c("al%gr" = 1), + opts = opts_v850 + ) + + createBindingConstraint( + name = "myconstraint2", + values = scenar_values_hourly, + enabled = FALSE, + timeStep = "hourly", + operator = "both", + group = "group_test", + coefficients = c("al%gr" = 1), + opts = opts_v850 + ) + + bc_names_v860 <- names(readBindingConstraints(opts = opts_v850)) + + removeBindingConstraint(bc_names_v860[1], + opts = opts_v850) + + bc_in_study <- readBindingConstraints(opts = opts_v850) + + # test + if(is.null(bc_in_study)) + testthat::expect_null(names(bc_in_study)) + else + testthat::expect_false("myconstraint" %in% + names(readBindingConstraints(opts = opts_v850))) + + # remove temporary study + unlink(x = study_temp_path, recursive = TRUE) + +}) From 3e1cd5b0031221edfd426763179d90baa1a9c63f Mon Sep 17 00:00:00 2001 From: BERTHET Clement Ext Date: Fri, 5 May 2023 16:53:59 +0200 Subject: [PATCH 06/74] createBindingConstraint add function to check dimension of value for every group --- NAMESPACE | 1 + R/createBindingConstraint.R | 66 ++++++++- man/createBindingConstraint.Rd | 18 +-- man/group_values_check.Rd | 23 +++ tests/testthat/test-createBindingConstraint.R | 134 +++++++++++++++++- 5 files changed, 224 insertions(+), 18 deletions(-) create mode 100644 man/group_values_check.Rd diff --git a/NAMESPACE b/NAMESPACE index dde27322..2f884404 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -45,6 +45,7 @@ export(getJobLogs) export(getJobs) export(getPlaylist) export(getVariantCommands) +export(group_values_check) export(is_antares_v7) export(is_antares_v820) export(mockSimulationAPI) diff --git a/R/createBindingConstraint.R b/R/createBindingConstraint.R index 113479ec..9ff78b3f 100644 --- a/R/createBindingConstraint.R +++ b/R/createBindingConstraint.R @@ -22,8 +22,10 @@ #' #' @template opts #' -#' @seealso [editBindingConstraint()] to edit existing binding constraints, [removeBindingConstraint()] to remove binding constraints. +#' @seealso [editBindingConstraint()] to edit existing binding constraints, +#' [removeBindingConstraint()] to remove binding constraints. #' +#' @details #' According to Antares version, usage may vary : #' #' **< v8.6.0** : For each constraint name, a .txt file containing 3 time series `"less", "greater", "equal"` @@ -127,6 +129,11 @@ createBindingConstraint <- function(name, if(!all(names(values)%in%c("lt", "gt", "eq"))) stop("Put for 'values' argument, named 'list' => see Doc `?createBindingConstraint`") } + + # v860 : check group and values + group_values_check(group_value = group, + values_data = values, + opts = opts) } bindingConstraints <- createBindingConstraint_( @@ -253,12 +260,20 @@ createBindingConstraint_ <- function(bindingConstraints, up_path <- file.path(opts$inputPath, "bindingconstraints", name_file) lapply(up_path, function(x, df_ts= values, vect_path= up_path){ - index <- grep(x = up_path, pattern = x) - fwrite(x = data.table::as.data.table(df_ts[[index]]), - file = x, - col.names = FALSE, - row.names = FALSE, - sep = "\t") + if(identical(df_ts, character(0))) + fwrite(x = data.table::as.data.table(df_ts), + file = x, + col.names = FALSE, + row.names = FALSE, + sep = "\t") + else{ + index <- grep(x = up_path, pattern = x) + fwrite(x = data.table::as.data.table(df_ts[[index]]), + file = x, + col.names = FALSE, + row.names = FALSE, + sep = "\t") + } }) }else{ pathValues <- file.path(opts$inputPath, "bindingconstraints", paste0(id, ".txt")) @@ -271,6 +286,43 @@ createBindingConstraint_ <- function(bindingConstraints, return(bindingConstraints) } + + +#' @title Check dimension of time series for binding constraints +#' @description Only needed for study version >= 860 +#' @param group_value `character` name of group +#' @param values_data `list` values used by the constraint +#' @template opts +#' @export +#' @keywords internal +group_values_check <- function(group_value, + values_data, + opts = antaresRead::simOptions()){ + + # read existing binding constraint + existing_bc <- readBindingConstraints(opts = opts) + + # study with no BC or virgin study + if(is.null(existing_bc)) + return() + + # check existing group Versus new group to create + existing_groups <- unlist(lapply(existing_bc, `[[`, "group")) + search_group <- grep(pattern = group_value, x = existing_groups) + index_group <- search_group[length(search_group)] + + # check dimension values existing group Versus new group + if( !identical(index_group, integer(0)) ){ + p_col <- dim(existing_bc[[index_group]]$values[[1]])[2] + p_col_new <- dim(values_data[[1]])[2] + + if(p_col!=p_col_new & p_col!=0) + stop(paste0("Put right columns dimension : ", + p_col, " for existing 'group' : ", + group_value)) + } +} + # v860 .valueCheck860 <- function(values, timeStep){ # check nrow Vs timeStep diff --git a/man/createBindingConstraint.Rd b/man/createBindingConstraint.Rd index 4bd20ec8..3208141b 100644 --- a/man/createBindingConstraint.Rd +++ b/man/createBindingConstraint.Rd @@ -61,6 +61,14 @@ An updated list containing various information about the simulation. Create a new binding constraint in an Antares study. \code{createBindingConstraintBulk()} allow to create multiple constraints at once. } +\details{ +According to Antares version, usage may vary : + +\strong{< v8.6.0} : For each constraint name, a .txt file containing 3 time series \verb{"less", "greater", "equal"} + +\strong{>= v8.6.0} : For each constraint name, one file .txt containing \verb{_lt.txt, _gt.txt, _eq.txt} +Parameter \code{values} must be named \code{list} ("lt", "gt", "eq") containing \code{data.frame} scenarized +} \examples{ \dontrun{ createBindingConstraint( @@ -97,12 +105,6 @@ createBindingConstraintBulk(bindings_constraints) } } \seealso{ -\code{\link[=editBindingConstraint]{editBindingConstraint()}} to edit existing binding constraints, \code{\link[=removeBindingConstraint]{removeBindingConstraint()}} to remove binding constraints. - -According to Antares version, usage may vary : - -\strong{< v8.6.0} : For each constraint name, a .txt file containing 3 time series \verb{"less", "greater", "equal"} - -\strong{>= v8.6.0} : For each constraint name, one file .txt containing \verb{_lt.txt, _gt.txt, _eq.txt} -Parameter \code{values} must be named \code{list} ("lt", "gt", "eq") containing \code{data.frame} scenarized +\code{\link[=editBindingConstraint]{editBindingConstraint()}} to edit existing binding constraints, +\code{\link[=removeBindingConstraint]{removeBindingConstraint()}} to remove binding constraints. } diff --git a/man/group_values_check.Rd b/man/group_values_check.Rd new file mode 100644 index 00000000..8604f373 --- /dev/null +++ b/man/group_values_check.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/createBindingConstraint.R +\name{group_values_check} +\alias{group_values_check} +\title{Check dimension of time series for binding constraints} +\usage{ +group_values_check(group_value, values_data, opts = antaresRead::simOptions()) +} +\arguments{ +\item{group_value}{\code{character} name of group} + +\item{values_data}{\code{list} values used by the constraint} + +\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. +} +\description{ +Only needed for study version >= 860 +} +\keyword{internal} diff --git a/tests/testthat/test-createBindingConstraint.R b/tests/testthat/test-createBindingConstraint.R index eb283301..0b7a89a5 100644 --- a/tests/testthat/test-createBindingConstraint.R +++ b/tests/testthat/test-createBindingConstraint.R @@ -197,7 +197,7 @@ lapply(names_bc_to_remove, # force version opts_v850$antaresVersion <- 860 -# scenarized data +# scenarized data hourly n <- 10 lt_data <- matrix(data = rep(1, 8760 * n), ncol = n) gt_data <- matrix(data = rep(2, 8760 * n), ncol = n) @@ -207,6 +207,16 @@ scenar_values <- list(lt= lt_data, gt= gt_data, eq= eq_data) +# daily +n <- 9 +lt_data <- matrix(data = rep(1, 365 * n), ncol = n) +gt_data <- matrix(data = rep(2, 365 * n), ncol = n) +eq_data <- matrix(data = rep(3, 365 * n), ncol = n) + +scenar_values_daily <- list(lt= lt_data, + gt= gt_data, + eq= eq_data) + test_that("createBindingConstraint v8.6", { # create binding constraint (default group value) @@ -255,9 +265,127 @@ test_that("createBindingConstraintBulk v8.6", { testthat::expect_true("constraints10" %in% names(readBindingConstraints(opts = opts_v850))) - # remove temporary study - unlink(x = study_temp_path, recursive = TRUE) }) +test_that("createBindingConstraint check values group v8.6", { + + # create binding constraint (default group value) + createBindingConstraint( + name = "myconstraint_group", + values = scenar_values, + enabled = FALSE, + timeStep = "hourly", + operator = "both", + coefficients = c("al%gr" = 1), + opts = opts_v850 + ) + + # create binding constraint + createBindingConstraint( + name = "myconstraint_group1", + values = scenar_values_daily, + enabled = FALSE, + timeStep = "daily", + operator = "both", + group = "group_test", + coefficients = c("al%gr" = 1), + opts = opts_v850 + ) + + # tests + testthat::expect_true( + all( + c("myconstraint_group", "myconstraint_group1") %in% + names(readBindingConstraints(opts = opts_v850)) + ) + ) + + # create binding constraint with bad values of existing group [ERROR] + testthat::expect_error( + createBindingConstraint( + name = "myconstraint_group2", + values = scenar_values, + enabled = FALSE, + timeStep = "hourly", + operator = "both", + group = "group_test", + coefficients = c("al%gr" = 1), + opts = opts_v850) + ) + + # create binding constraint with existing group + createBindingConstraint( + name = "myconstraint_group2", + values = scenar_values_daily, + enabled = FALSE, + timeStep = "daily", + operator = "both", + group = "group_test", + coefficients = c("al%gr" = 1), + opts = opts_v850) + + testthat::expect_true("myconstraint_group2" %in% + names(readBindingConstraints(opts = opts_v850))) + + +}) + +test_that("createBindingConstraint check values (NULL case) group v8.6", { + # create binding constraint (NULL value) + createBindingConstraint( + name = "myconstraint_group_NULL", + values = NULL, + enabled = FALSE, + operator = "both", + group = "null_values", + coefficients = c("al%gr" = 1), + opts = opts_v850 + ) + + bc <- readBindingConstraints(opts = opts_v850) + + # check name + testthat::expect_true("myconstraint_group_NULL" %in% + names(bc)) + + # check dim + dim_values <- unlist(lapply(bc$myconstraint_group_NULL$values, + function(x) dim(x)[2])) + + testthat::expect_equal(sum(dim_values), 0) + + # create binding constraint (existing group with NULL value) + createBindingConstraint( + name = "myconstraint_group_3", + values = scenar_values, + enabled = FALSE, + operator = "both", + timeStep = "hourly", + group = "null_values", + coefficients = c("al%gr" = 1), + opts = opts_v850 + ) + + bc <- readBindingConstraints(opts = opts_v850) + + # check name + testthat::expect_true("myconstraint_group_3" %in% + names(bc)) + + # create binding constraint (existing group) + createBindingConstraint( + name = "myconstraint_group_4", + values = scenar_values, + enabled = FALSE, + operator = "both", + timeStep = "hourly", + group = "null_values", + coefficients = c("al%gr" = 1), + opts = opts_v850 + ) + + # remove temporary study + unlink(x = study_temp_path, recursive = TRUE) +}) From 7705dc01c28a029bbe7c757f6ac554873b1673f9 Mon Sep 17 00:00:00 2001 From: BERTHET Clement Ext Date: Thu, 11 May 2023 11:58:36 +0200 Subject: [PATCH 07/74] createBindingConstraint fix to check group values with only same dimension --- R/createBindingConstraint.R | 5 +- tests/testthat/test-editBindingConstraint.R | 57 ++++++++++++++++++++- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/R/createBindingConstraint.R b/R/createBindingConstraint.R index 9ff78b3f..e7d9025e 100644 --- a/R/createBindingConstraint.R +++ b/R/createBindingConstraint.R @@ -316,7 +316,10 @@ group_values_check <- function(group_value, p_col <- dim(existing_bc[[index_group]]$values[[1]])[2] p_col_new <- dim(values_data[[1]])[2] - if(p_col!=p_col_new & p_col!=0) + if(is.null(p_col_new)) + p_col_new <- 0 + + if(p_col!=p_col_new) # & p_col!=0 stop(paste0("Put right columns dimension : ", p_col, " for existing 'group' : ", group_value)) diff --git a/tests/testthat/test-editBindingConstraint.R b/tests/testthat/test-editBindingConstraint.R index d7f6e100..a32c0efd 100644 --- a/tests/testthat/test-editBindingConstraint.R +++ b/tests/testthat/test-editBindingConstraint.R @@ -91,7 +91,7 @@ test_that("editBindingConstraint v8.6", { bc_names_v860 <- names(readBindingConstraints(opts = opts_v850)) - # edit BC with NULL VALUES + # edit BC with NULL values (edit only .ini file) editBindingConstraint(name = bc_names_v860, values = NULL, timeStep = "daily", @@ -128,8 +128,61 @@ test_that("editBindingConstraint v8.6", { res_dim_values <- lapply(res_dim_values, `[[`, 1) res_dim_values <- unlist(res_dim_values) testthat::expect_equal(mean(res_dim_values), 366) + + # test error with bad values dimension + testthat::expect_error( + editBindingConstraint(name = bc_names_v860, + values = list(lt=matrix(data = rep(1, 365 * 9), + ncol = 9)), + timeStep = "daily", + operator = "both", + opts = opts_v850), + regexp = "Put right columns dimension : 10 for existing 'group' : default group" + ) + + # create new binding constraint with new dimension + n <- 20 + lt_data <- matrix(data = rep(1, 8760 * n), ncol = n) + gt_data <- matrix(data = rep(2, 8760 * n), ncol = n) + eq_data <- matrix(data = rep(3, 8760 * n), ncol = n) + + scenar_values_hourly_new <- list(lt= lt_data, + gt= gt_data, + eq= eq_data) + + createBindingConstraint( + name = "myconstraint_new", + values = scenar_values_hourly_new, + enabled = FALSE, + timeStep = "hourly", + operator = "both", + coefficients = c("al%gr" = 1), + group = "new", + opts = opts_v850 + ) + + # edit group of last bindingConstraint with bad dimension + testthat::expect_error( + editBindingConstraint(name = "myconstraint_new", + group = "default group", + opts = opts_v850), + regexp = "Put right columns dimension : 10 for existing 'group' : default group" + ) + + # edit param + editBindingConstraint(name = "myconstraint_new", + operator = "less", + opts = opts_v850) + + bc_modified <- readBindingConstraints(opts = opts_v850) + new_param <- bc_modified[["myconstraint_new"]]$operator + + # test coefs + testthat::expect_equal(new_param, "less") + # remove temporary study unlink(x = study_temp_path, recursive = TRUE) - }) + + From aee13968bdfc6f323d2a12ec50c62a3092621126 Mon Sep 17 00:00:00 2001 From: BERTHET Clement Ext Date: Thu, 11 May 2023 12:00:52 +0200 Subject: [PATCH 08/74] editBindingConstraint fix check group values with null values --- R/editBindingConstraint.R | 17 +++- tests/testthat/test-createBindingConstraint.R | 78 +++++++++++++------ 2 files changed, 72 insertions(+), 23 deletions(-) diff --git a/R/editBindingConstraint.R b/R/editBindingConstraint.R index 32b1c687..d0558fb5 100644 --- a/R/editBindingConstraint.R +++ b/R/editBindingConstraint.R @@ -119,8 +119,23 @@ editBindingConstraint <- function(name, # v860 if(opts$antaresVersion>=860){ - if(!is.null(group)) + if(!is.null(group)){ iniParams$group <- group + + # edit group with check group values + if(is.null(values)) + group_values_check(group_value = group, + values_data = values, + opts = opts) + }else + group <- "default group" + + # check group values + if(!is.null(values)) + group_values_check(group_value = group, + values_data = values, + opts = opts) + } # update constraint parameters with new parameters diff --git a/tests/testthat/test-createBindingConstraint.R b/tests/testthat/test-createBindingConstraint.R index 0b7a89a5..768fdb81 100644 --- a/tests/testthat/test-createBindingConstraint.R +++ b/tests/testthat/test-createBindingConstraint.R @@ -217,9 +217,9 @@ scenar_values_daily <- list(lt= lt_data, gt= gt_data, eq= eq_data) -test_that("createBindingConstraint v8.6", { +test_that("createBindingConstraint (default group value) v8.6", { - # create binding constraint (default group value) + # create binding constraint createBindingConstraint( name = "myconstraint", values = scenar_values, @@ -230,9 +230,15 @@ test_that("createBindingConstraint v8.6", { opts = opts_v850 ) + bc <- readBindingConstraints(opts = opts_v850) + # tests testthat::expect_true("myconstraint" %in% - names(readBindingConstraints(opts = opts_v850))) + names(bc)) + + testthat::expect_equal(bc$myconstraint$group, "default group") + + }) @@ -268,7 +274,7 @@ test_that("createBindingConstraintBulk v8.6", { }) -test_that("createBindingConstraint check values group v8.6", { +test_that("createBindingConstraint check group values v8.6", { # create binding constraint (default group value) createBindingConstraint( @@ -281,7 +287,18 @@ test_that("createBindingConstraint check values group v8.6", { opts = opts_v850 ) - # create binding constraint + # ADD binding constraint still (default group value) + createBindingConstraint( + name = "myconstraint_group_bis", + values = scenar_values, + enabled = FALSE, + timeStep = "hourly", + operator = "both", + coefficients = c("al%gr" = 1), + opts = opts_v850 + ) + + # ADD binding constraint with named group createBindingConstraint( name = "myconstraint_group1", values = scenar_values_daily, @@ -296,11 +313,37 @@ test_that("createBindingConstraint check values group v8.6", { # tests testthat::expect_true( all( - c("myconstraint_group", "myconstraint_group1") %in% + c("myconstraint_group", "myconstraint_group_bis", "myconstraint_group1") %in% names(readBindingConstraints(opts = opts_v850)) ) ) + # create binding constraint with bad values of existing group (default group) [ERROR] + testthat::expect_error( + createBindingConstraint( + name = "myconstraint_group_err", + values = scenar_values_daily, + enabled = FALSE, + timeStep = "daily", + operator = "both", + coefficients = c("al%gr" = 1), + opts = opts_v850 + ) + ) + + # create binding constraint with bad values (NULL) of existing group (default group) [ERROR] + testthat::expect_error( + createBindingConstraint( + name = "myconstraint_group_err", + values = NULL, + enabled = FALSE, + timeStep = "daily", + operator = "both", + coefficients = c("al%gr" = 1), + opts = opts_v850 + ) + ) + # create binding constraint with bad values of existing group [ERROR] testthat::expect_error( createBindingConstraint( @@ -314,7 +357,7 @@ test_that("createBindingConstraint check values group v8.6", { opts = opts_v850) ) - # create binding constraint with existing group + # ADD binding constraint with existing group createBindingConstraint( name = "myconstraint_group2", values = scenar_values_daily, @@ -325,14 +368,17 @@ test_that("createBindingConstraint check values group v8.6", { coefficients = c("al%gr" = 1), opts = opts_v850) + bc <- readBindingConstraints(opts = opts_v850) + testthat::expect_true("myconstraint_group2" %in% names(readBindingConstraints(opts = opts_v850))) + testthat::expect_equal(bc[["myconstraint_group2"]]$group, "group_test") }) -test_that("createBindingConstraint check values (NULL case) group v8.6", { +test_that("createBindingConstraint check values (NULL) group v8.6", { # create binding constraint (NULL value) createBindingConstraint( name = "myconstraint_group_NULL", @@ -356,10 +402,10 @@ test_that("createBindingConstraint check values (NULL case) group v8.6", { testthat::expect_equal(sum(dim_values), 0) - # create binding constraint (existing group with NULL value) + # ADD binding constraint (existing group with NULL value) createBindingConstraint( name = "myconstraint_group_3", - values = scenar_values, + values = NULL, enabled = FALSE, operator = "both", timeStep = "hourly", @@ -374,18 +420,6 @@ test_that("createBindingConstraint check values (NULL case) group v8.6", { testthat::expect_true("myconstraint_group_3" %in% names(bc)) - # create binding constraint (existing group) - createBindingConstraint( - name = "myconstraint_group_4", - values = scenar_values, - enabled = FALSE, - operator = "both", - timeStep = "hourly", - group = "null_values", - coefficients = c("al%gr" = 1), - opts = opts_v850 - ) - # remove temporary study unlink(x = study_temp_path, recursive = TRUE) }) From 356f7d0eb44fba4b1b124b332efad81319433a02 Mon Sep 17 00:00:00 2001 From: BERTHET Clement Ext Date: Fri, 12 May 2023 16:11:32 +0200 Subject: [PATCH 09/74] update code to v870 for all bindingConstraints functions family + tests --- R/createBindingConstraint.R | 28 +++++------ R/editBindingConstraint.R | 14 +++--- R/removeBindingConstraint.R | 4 +- man/createBindingConstraint.Rd | 4 +- man/group_values_check.Rd | 2 +- tests/testthat/test-createBindingConstraint.R | 18 ++++---- tests/testthat/test-editBindingConstraint.R | 20 ++++---- tests/testthat/test-removeBindingConstraint.R | 12 ++--- tests/testthat/test-scenarioBuilder.R | 46 +++++++++++++++++++ 9 files changed, 97 insertions(+), 51 deletions(-) diff --git a/R/createBindingConstraint.R b/R/createBindingConstraint.R index e7d9025e..dddc9ddd 100644 --- a/R/createBindingConstraint.R +++ b/R/createBindingConstraint.R @@ -28,9 +28,9 @@ #' @details #' According to Antares version, usage may vary : #' -#' **< v8.6.0** : For each constraint name, a .txt file containing 3 time series `"less", "greater", "equal"` +#' **< v8.7.0** : For each constraint name, a .txt file containing 3 time series `"less", "greater", "equal"` #' -#' **>= v8.6.0** : For each constraint name, one file .txt containing `_lt.txt, _gt.txt, _eq.txt` +#' **>= v8.7.0** : For each constraint name, one file .txt containing `_lt.txt, _gt.txt, _eq.txt` #' Parameter `values` must be named `list` ("lt", "gt", "eq") containing `data.frame` scenarized #' #' @export @@ -119,8 +119,8 @@ createBindingConstraint <- function(name, pathIni <- file.path(opts$inputPath, "bindingconstraints/bindingconstraints.ini") bindingConstraints <- readIniFile(pathIni, stringsAsFactors = FALSE) - # v860 - if(opts$antaresVersion>=860){ + # v870 + if(opts$antaresVersion>=870){ if(is.null(group)) group <- "default group" @@ -130,7 +130,7 @@ createBindingConstraint <- function(name, stop("Put for 'values' argument, named 'list' => see Doc `?createBindingConstraint`") } - # v860 : check group and values + # v870 : check group and values group_values_check(group_value = group, values_data = values, opts = opts) @@ -207,8 +207,8 @@ createBindingConstraint_ <- function(bindingConstraints, iniParams$`filter-synthesis` <- filter_synthesis } - # v860 - if(opts$antaresVersion>=860) + # v870 + if(opts$antaresVersion>=870) iniParams$group <- group # Check coefficients @@ -246,14 +246,14 @@ createBindingConstraint_ <- function(bindingConstraints, bindingConstraints[[indexBC]] <- c(iniParams, coefficients) ## Values - if(opts$antaresVersion>=860 & !is.null(values)) - values <- .valueCheck860(values, timeStep) + if(opts$antaresVersion>=870 & !is.null(values)) + values <- .valueCheck870(values, timeStep) else values <- .valueCheck(values, timeStep) # Write values - # v860 - if(opts$antaresVersion>=860){ + # v870 + if(opts$antaresVersion>=870){ names_order_ts <- c("lt", "gt", "eq") name_file <- paste0(id, "_", names_order_ts, ".txt") @@ -289,7 +289,7 @@ createBindingConstraint_ <- function(bindingConstraints, #' @title Check dimension of time series for binding constraints -#' @description Only needed for study version >= 860 +#' @description Only needed for study version >= 870 #' @param group_value `character` name of group #' @param values_data `list` values used by the constraint #' @template opts @@ -326,8 +326,8 @@ group_values_check <- function(group_value, } } -# v860 -.valueCheck860 <- function(values, timeStep){ +# v870 +.valueCheck870 <- function(values, timeStep){ # check nrow Vs timeStep nrows <- switch(timeStep, hourly = 24*366, diff --git a/R/editBindingConstraint.R b/R/editBindingConstraint.R index d0558fb5..a60d3556 100644 --- a/R/editBindingConstraint.R +++ b/R/editBindingConstraint.R @@ -117,8 +117,8 @@ editBindingConstraint <- function(name, iniParams$`filter-synthesis` <- filter_synthesis } - # v860 - if(opts$antaresVersion>=860){ + # v870 + if(opts$antaresVersion>=870){ if(!is.null(group)){ iniParams$group <- group @@ -172,9 +172,9 @@ editBindingConstraint <- function(name, } # write txt files - # v860 - if(opts$antaresVersion>=860 & !is.null(values)) - values <- .valueCheck860(values, bindingConstraints[[bc_update_pos]]$type) + # v870 + if(opts$antaresVersion>=870 & !is.null(values)) + values <- .valueCheck870(values, bindingConstraints[[bc_update_pos]]$type) else values <- .valueCheck(values, bindingConstraints[[bc_update_pos]]$type) @@ -182,8 +182,8 @@ editBindingConstraint <- function(name, writeIni(listData = bindingConstraints, pathIni = pathIni, overwrite = TRUE) # Write values - # v860 - if(opts$antaresVersion>=860){ + # v870 + if(opts$antaresVersion>=870){ if(!identical(values, character(0))){ names_order_ts <- c("lt", "gt", "eq") name_file <- paste0(id, "_", names_order_ts, ".txt") diff --git a/R/removeBindingConstraint.R b/R/removeBindingConstraint.R index 5529eb97..18d3bd36 100644 --- a/R/removeBindingConstraint.R +++ b/R/removeBindingConstraint.R @@ -56,8 +56,8 @@ removeBindingConstraint <- function(name, opts = antaresRead::simOptions()) { index <- which(namesbc == i) id <- bindingConstraints[[index]]$id bindingConstraints[[index]] <- NULL - # v860 - if(opts$antaresVersion>=860){ + # v870 + if(opts$antaresVersion>=870){ path_lt <- file.path(opts$inputPath, sprintf("bindingconstraints/%s.txt", paste0(id, "_lt"))) diff --git a/man/createBindingConstraint.Rd b/man/createBindingConstraint.Rd index 3208141b..1be609cb 100644 --- a/man/createBindingConstraint.Rd +++ b/man/createBindingConstraint.Rd @@ -64,9 +64,9 @@ Create a new binding constraint in an Antares study. \details{ According to Antares version, usage may vary : -\strong{< v8.6.0} : For each constraint name, a .txt file containing 3 time series \verb{"less", "greater", "equal"} +\strong{< v8.7.0} : For each constraint name, a .txt file containing 3 time series \verb{"less", "greater", "equal"} -\strong{>= v8.6.0} : For each constraint name, one file .txt containing \verb{_lt.txt, _gt.txt, _eq.txt} +\strong{>= v8.7.0} : For each constraint name, one file .txt containing \verb{_lt.txt, _gt.txt, _eq.txt} Parameter \code{values} must be named \code{list} ("lt", "gt", "eq") containing \code{data.frame} scenarized } \examples{ diff --git a/man/group_values_check.Rd b/man/group_values_check.Rd index 8604f373..958a6042 100644 --- a/man/group_values_check.Rd +++ b/man/group_values_check.Rd @@ -18,6 +18,6 @@ group_values_check(group_value, values_data, opts = antaresRead::simOptions()) An updated list containing various information about the simulation. } \description{ -Only needed for study version >= 860 +Only needed for study version >= 870 } \keyword{internal} diff --git a/tests/testthat/test-createBindingConstraint.R b/tests/testthat/test-createBindingConstraint.R index 768fdb81..00fc9b93 100644 --- a/tests/testthat/test-createBindingConstraint.R +++ b/tests/testthat/test-createBindingConstraint.R @@ -175,7 +175,7 @@ sapply(studies, function(study) { }) -# v8.6 ---- +# v8.7 ---- ## Global data # read / open template study @@ -186,16 +186,16 @@ opts_v850 <- antaresRead::setSimulationPath(study_temp_path, "input") # areas list antaresRead::getAreas(opts = opts_v850) -# remove BC none v860 +# remove BC none v870 names_bc_to_remove <- names(readBindingConstraints(opts = opts_v850)) lapply(names_bc_to_remove, removeBindingConstraint, opts = simOptions()) -# temporary to test with "860" +# temporary to test with "870" # force version -opts_v850$antaresVersion <- 860 +opts_v850$antaresVersion <- 870 # scenarized data hourly n <- 10 @@ -217,7 +217,7 @@ scenar_values_daily <- list(lt= lt_data, gt= gt_data, eq= eq_data) -test_that("createBindingConstraint (default group value) v8.6", { +test_that("createBindingConstraint (default group value) v8.7", { # create binding constraint createBindingConstraint( @@ -242,7 +242,7 @@ test_that("createBindingConstraint (default group value) v8.6", { }) -test_that("createBindingConstraintBulk v8.6", { +test_that("createBindingConstraintBulk v8.7", { # Prepare data for constraints bindings_constraints <- lapply( X = seq_len(10), @@ -257,7 +257,7 @@ test_that("createBindingConstraintBulk v8.6", { timeStep = "hourly", operator = "both", coefficients = c("al%gr" = 1), - group= "groupv860", + group= "groupv870", overwrite = TRUE ) } @@ -274,7 +274,7 @@ test_that("createBindingConstraintBulk v8.6", { }) -test_that("createBindingConstraint check group values v8.6", { +test_that("createBindingConstraint check group values v8.7", { # create binding constraint (default group value) createBindingConstraint( @@ -378,7 +378,7 @@ test_that("createBindingConstraint check group values v8.6", { }) -test_that("createBindingConstraint check values (NULL) group v8.6", { +test_that("createBindingConstraint check values (NULL) group v8.7", { # create binding constraint (NULL value) createBindingConstraint( name = "myconstraint_group_NULL", diff --git a/tests/testthat/test-editBindingConstraint.R b/tests/testthat/test-editBindingConstraint.R index a32c0efd..e0c57a18 100644 --- a/tests/testthat/test-editBindingConstraint.R +++ b/tests/testthat/test-editBindingConstraint.R @@ -35,7 +35,7 @@ test_that("editBindingConstraint v710", { }) -# v860 ---- +# v870 ---- ## Global data # read / open template study @@ -45,16 +45,16 @@ opts_v850 <- antaresRead::setSimulationPath(study_temp_path, "input") # areas list antaresRead::getAreas(opts = opts_v850) -# remove BC none v860 +# remove BC none v870 names_bc_to_remove <- names(readBindingConstraints(opts = opts_v850)) lapply(names_bc_to_remove, removeBindingConstraint, opts = simOptions()) -# temporary to test with "860" +# temporary to test with "870" # force version -opts_v850$antaresVersion <- 860 +opts_v850$antaresVersion <- 870 # scenarized data # hourly @@ -89,10 +89,10 @@ test_that("editBindingConstraint v8.6", { opts = opts_v850 ) - bc_names_v860 <- names(readBindingConstraints(opts = opts_v850)) + bc_names_v870 <- names(readBindingConstraints(opts = opts_v850)) # edit BC with NULL values (edit only .ini file) - editBindingConstraint(name = bc_names_v860, + editBindingConstraint(name = bc_names_v870, values = NULL, timeStep = "daily", operator = "both", @@ -100,14 +100,14 @@ test_that("editBindingConstraint v8.6", { opts = opts_v850) bc_modified <- readBindingConstraints(opts = opts_v850) - new_coef <- bc_modified[[bc_names_v860[1]]]$coefs + new_coef <- bc_modified[[bc_names_v870[1]]]$coefs # test testthat::expect_true(all(new_coef %in% c(7.45))) # edit BC with "daily" VALUES # add new coeff - editBindingConstraint(name = bc_names_v860, + editBindingConstraint(name = bc_names_v870, values = scenar_values_daily, timeStep = "daily", operator = "both", @@ -117,7 +117,7 @@ test_that("editBindingConstraint v8.6", { opts = opts_v850) bc_modified <- readBindingConstraints(opts = opts_v850) - new_coef <- bc_modified[[bc_names_v860[1]]]$coefs + new_coef <- bc_modified[[bc_names_v870[1]]]$coefs # test coefs testthat::expect_true(all(new_coef %in% c(0.5, 12.0, 0.0))) @@ -131,7 +131,7 @@ test_that("editBindingConstraint v8.6", { # test error with bad values dimension testthat::expect_error( - editBindingConstraint(name = bc_names_v860, + editBindingConstraint(name = bc_names_v870, values = list(lt=matrix(data = rep(1, 365 * 9), ncol = 9)), timeStep = "daily", diff --git a/tests/testthat/test-removeBindingConstraint.R b/tests/testthat/test-removeBindingConstraint.R index a729e002..5b3f6ed1 100644 --- a/tests/testthat/test-removeBindingConstraint.R +++ b/tests/testthat/test-removeBindingConstraint.R @@ -1,5 +1,5 @@ -# v860 ---- +# v870 ---- ## Global data # read / open template study @@ -9,16 +9,16 @@ opts_v850 <- antaresRead::setSimulationPath(study_temp_path, "input") # areas list antaresRead::getAreas(opts = opts_v850) -# remove BC none v860 +# remove BC none v870 names_bc_to_remove <- names(readBindingConstraints(opts = opts_v850)) lapply(names_bc_to_remove, removeBindingConstraint, opts = simOptions()) -# temporary to test with "860" +# temporary to test with "870" # force version -opts_v850$antaresVersion <- 860 +opts_v850$antaresVersion <- 870 # scenarized data # hourly @@ -74,9 +74,9 @@ test_that("removeBindingConstraint v8.6", { opts = opts_v850 ) - bc_names_v860 <- names(readBindingConstraints(opts = opts_v850)) + bc_names_v870 <- names(readBindingConstraints(opts = opts_v850)) - removeBindingConstraint(bc_names_v860[1], + removeBindingConstraint(bc_names_v870[1], opts = opts_v850) bc_in_study <- readBindingConstraints(opts = opts_v850) diff --git a/tests/testthat/test-scenarioBuilder.R b/tests/testthat/test-scenarioBuilder.R index 9671bc8d..48a8c99b 100644 --- a/tests/testthat/test-scenarioBuilder.R +++ b/tests/testthat/test-scenarioBuilder.R @@ -139,4 +139,50 @@ sapply(studies, function(study) { }) +# v870 ---- + +## Global data +# read / open template study +setup_study_850(dir_path = sourcedir850) +opts_v850 <- antaresRead::setSimulationPath(study_temp_path, "input") + +# areas list +antaresRead::getAreas(opts = opts_v850) + +# remove BC none v870 +names_bc_to_remove <- names(readBindingConstraints(opts = opts_v850)) + +lapply(names_bc_to_remove, + removeBindingConstraint, + opts = simOptions()) + +# temporary to test with "870" +# force version +opts_v850$antaresVersion <- 870 + +test_that("scenarioBuilder works with binding constraint (v870)", { + + # Read, create & update scenario builder + + sbuilder <- scenarioBuilder( + n_scenario = opts_v850$parameters$general$nbyears, + n_mc = opts_v850$parameters$general$nbyears, + areas = getAreas()[1:3], + areas_rand = getAreas()[1:2], + opts = opts_v850 + ) + + # Read previous scenario builder + # in a matrix format + prev_sb <- readScenarioBuilder(opts = opts_v850, as_matrix = TRUE) + + # Update scenario builder + # for binding constraints series + # updateScenarioBuilder(ldata = sbuilder, series = "binding") + + # remove temporary study + unlink(x = study_temp_path, recursive = TRUE) +}) + + From 1a60a9475320389622a5477a5b7145e73af74e71 Mon Sep 17 00:00:00 2001 From: BERTHET Clement Ext Date: Mon, 15 May 2023 13:23:35 +0200 Subject: [PATCH 10/74] scenarioBuilder v870 work in progress + tests --- R/scenarioBuilder.R | 40 ++++++++++++++++++--------- man/scenario-builder.Rd | 1 + tests/testthat/test-scenarioBuilder.R | 10 ++----- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/R/scenarioBuilder.R b/R/scenarioBuilder.R index 80efa8b5..ced3e30a 100644 --- a/R/scenarioBuilder.R +++ b/R/scenarioBuilder.R @@ -124,28 +124,32 @@ readScenarioBuilder <- function(ruleset = "Default Ruleset", as_matrix = TRUE, opts = antaresRead::simOptions()) { assertthat::assert_that(inherits(opts, "simOptions")) + + # read existing scenariobuilder.dat if (is_api_study(opts)) { if (is_api_mocked(opts)) { sb <- list("Default Ruleset" = NULL) } else { - sb <- readIni("settings/scenariobuilder", opts = opts, default_ext = ".dat") + sb <- readIni("settings/scenariobuilder", + opts = opts, default_ext = ".dat") } } else { - sb <- readIni("settings/scenariobuilder", opts = opts, default_ext = ".dat") + sb <- readIni("settings/scenariobuilder", + opts = opts, default_ext = ".dat") } + + # check structure in top of file scenariobuilder.dat if (!ruleset %in% names(sb)) { - warning(sprintf("Ruleset '%s' not found, possible values are: %s", ruleset, paste(names(sb), collapse = ", ")), call. = FALSE) + warning(sprintf("Ruleset '%s' not found, possible values are: %s", + ruleset, paste(names(sb), collapse = ", ")), + call. = FALSE) sb <- NULL } else { sb <- sb[[ruleset]] } if (is.null(sb)) return(list()) - extract_el <- function(l, indice) { - res <- strsplit(x = names(l), split = ",") - res <- lapply(res, `[`, i = indice) - unlist(res) - } + types <- extract_el(sb, 1) sbt <- split(x = sb, f = types) if (is_active_RES(opts)) { @@ -205,6 +209,12 @@ readScenarioBuilder <- function(ruleset = "Default Ruleset", ) } +extract_el <- function(l, indice) { + res <- strsplit(x = names(l), split = ",") + res <- lapply(res, `[`, i = indice) + unlist(res) +} + #' @param ldata A `matrix` obtained with `scenarioBuilder`, #' or a named list of matrices obtained with `scenarioBuilder`, names must be @@ -219,7 +229,8 @@ readScenarioBuilder <- function(ruleset = "Default Ruleset", #' #' #' @note -#' `series = "ntc"` is only available with Antares >= 8.2.0. +#' `series = "ntc"` is only available with Antares >= 8.2.0. +#' `series = "binding"` is only available with Antares >= 8.7.0. #' #' @export #' @@ -232,18 +243,21 @@ updateScenarioBuilder <- function(ldata, opts = antaresRead::simOptions()) { assertthat::assert_that(inherits(opts, "simOptions")) suppressWarnings(prevSB <- readScenarioBuilder(ruleset = ruleset, as_matrix = FALSE, opts = opts)) + if (!is.list(ldata)) { if (!is.null(series)) { series <- match.arg( arg = series, - choices = c("load", "hydro", "wind", "solar", "thermal", "renewables", "ntc"), + choices = c("load", "hydro", "wind", "solar", "thermal", "renewables", "ntc", "binding"), several.ok = TRUE ) if (isTRUE("ntc" %in% series) & isTRUE(opts$antaresVersion < 820)) stop("updateScenarioBuilder: cannot use series='ntc' with Antares < 8.2.0", call. = FALSE) ind_ntc <- which(series == "ntc") + ind_bc <- which(series == "binding") series <- substr(series, 1, 1) series[ind_ntc] <- "ntc" + series[ind_bc] <- "bc" } else { stop("If 'ldata' isn't a named list, you must specify which serie(s) to use!", call. = FALSE) } @@ -258,8 +272,8 @@ updateScenarioBuilder <- function(ldata, prevSB[series] <- NULL } else { series <- names(ldata) - if (!all(series %in% c("l", "h", "w", "s", "t", "r", "ntc"))) { - stop("'ldata' must be 'l', 'h', 'w', 's', 't', 'r' or 'ntc'", call. = FALSE) + if (!all(series %in% c("l", "h", "w", "s", "t", "r", "ntc", "bc"))) { + stop("'ldata' must be 'l', 'h', 'w', 's', 't', 'r' , 'bc' or 'ntc'", call. = FALSE) } if (isTRUE("ntc" %in% series) & isTRUE(opts$antaresVersion < 820)) stop("updateScenarioBuilder: cannot use series='ntc' with Antares < 8.2.0", call. = FALSE) @@ -344,7 +358,7 @@ clearScenarioBuilder <- function(ruleset = "Default Ruleset", #' Converts a scenarioBuilder matrix to a list #' #' @param mat A matrix obtained from scenarioBuilder(). -#' @param series Name of the series, among 'l', 'h', 'w', 's', 't' and 'r'. +#' @param series Name of the series, among 'l', 'h', 'w', 's', 't' 'bc' and 'r'. #' @param clusters_areas A `data.table` with two columns `area` and `cluster` #' to identify area/cluster couple to use for thermal or renewable series. #' @param links Either a simple vector with links described as `"area01%area02` or a `data.table` with two columns `from` and `to`. diff --git a/man/scenario-builder.Rd b/man/scenario-builder.Rd index 822b2afc..4c9511d1 100644 --- a/man/scenario-builder.Rd +++ b/man/scenario-builder.Rd @@ -78,6 +78,7 @@ Read, create & update scenario builder. } \note{ \code{series = "ntc"} is only available with Antares >= 8.2.0. +\code{series = "binding"} is only available with Antares >= 8.7.0. } \examples{ \dontrun{ diff --git a/tests/testthat/test-scenarioBuilder.R b/tests/testthat/test-scenarioBuilder.R index 48a8c99b..98d8b535 100644 --- a/tests/testthat/test-scenarioBuilder.R +++ b/tests/testthat/test-scenarioBuilder.R @@ -2,6 +2,7 @@ context("Function scenarioBuilder") +# v710 ---- sapply(studies, function(study) { @@ -149,13 +150,6 @@ opts_v850 <- antaresRead::setSimulationPath(study_temp_path, "input") # areas list antaresRead::getAreas(opts = opts_v850) -# remove BC none v870 -names_bc_to_remove <- names(readBindingConstraints(opts = opts_v850)) - -lapply(names_bc_to_remove, - removeBindingConstraint, - opts = simOptions()) - # temporary to test with "870" # force version opts_v850$antaresVersion <- 870 @@ -178,7 +172,7 @@ test_that("scenarioBuilder works with binding constraint (v870)", { # Update scenario builder # for binding constraints series - # updateScenarioBuilder(ldata = sbuilder, series = "binding") + updateScenarioBuilder(ldata = sbuilder, series = "binding") # remove temporary study unlink(x = study_temp_path, recursive = TRUE) From 477a9244fd03e32ac9313fa3ab80c7bec410d3f0 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Mon, 6 Nov 2023 14:31:20 +0100 Subject: [PATCH 11/74] testthat helper_init rename function to load study test + update all test impacted --- tests/testthat/helper_init.R | 43 +++++------------ tests/testthat/test-createBindingConstraint.R | 48 +++++++++---------- tests/testthat/test-createCluster.R | 2 +- tests/testthat/test-createClusterST.R | 2 +- tests/testthat/test-editBindingConstraint.R | 32 ++++++------- tests/testthat/test-editCluster.R | 2 +- tests/testthat/test-editClusterST.R | 2 +- tests/testthat/test-removeBindingConstraint.R | 24 +++++----- tests/testthat/test-scenarioBuilder.R | 18 +++---- tests/testthat/test-writeHydroValues.R | 2 +- tests/testthat/test-writeInputTS.R | 2 +- 11 files changed, 78 insertions(+), 99 deletions(-) diff --git a/tests/testthat/helper_init.R b/tests/testthat/helper_init.R index 762c1803..8d833a88 100644 --- a/tests/testthat/helper_init.R +++ b/tests/testthat/helper_init.R @@ -26,42 +26,21 @@ setup_study <- function(study, sourcedir) { } } -# <<<<<<< HEAD -# -# # study v850 ---- -# sourcedir850 <- system.file("test_v8", package = "antaresRead") -# -# setup_study_850 <- function(dir_path){ -# studies850 <- list.files(dir_path, pattern = "\\.tar\\.gz$", full.names = TRUE) -# studies850 <- studies850[grep(x = studies850, pattern = "v85")] -# # untar etude -# path_850 <- file.path(tempdir(), "studyv850") -# untar(studies850[1], exdir = path_850) # v85 -# study_temp_path <- file.path(path_850, "test_case") -# -# assign("study_temp_path", -# file.path(path_850, -# "test_case"), -# envir = globalenv()) -# } -# -# -# -# ======= -# study v860 ---- -sourcedir860 <- system.file("test_v8", package = "antaresRead") +# study version > v800 ---- + # new template study for each new release +sourcedir_last_study <- system.file("test_v8", package = "antaresRead") -setup_study_860 <- function(dir_path){ - studies860 <- list.files(dir_path, pattern = "\\.tar\\.gz$", full.names = TRUE) - studies860 <- studies860[grep(x = studies860, pattern = "v86")] +setup_study_last <- function(dir_path){ + studies <- list.files(dir_path, pattern = "\\.tar\\.gz$", full.names = TRUE) + studies_last_version <- studies[grep(x = studies, pattern = "v86")] # untar etude - path_860 <- file.path(tempdir(), "studyv860") - untar(studies860[1], exdir = path_860) # v86 - study_temp_path <- file.path(path_860, "test_case") + path_last_version <- file.path(tempdir(), "studyv860") + untar(studies_last_version[1], exdir = path_last_version) # v86 + study_temp_path <- file.path(path_last_version, "test_case") assign("study_temp_path", - file.path(path_860, + file.path(path_last_version, "test_case"), envir = globalenv()) } -# >>>>>>> 28f01006492e9467bbd5b4697c810c26e889324e + diff --git a/tests/testthat/test-createBindingConstraint.R b/tests/testthat/test-createBindingConstraint.R index 782aac7b..e04b27ea 100644 --- a/tests/testthat/test-createBindingConstraint.R +++ b/tests/testthat/test-createBindingConstraint.R @@ -179,15 +179,15 @@ sapply(studies, function(study) { ## Global data # read / open template study -setup_study_860(dir_path = sourcedir860) +setup_study_last(dir_path = sourcedir_last_study) -opts_v860 <- antaresRead::setSimulationPath(study_temp_path, "input") +opts_test <- antaresRead::setSimulationPath(study_temp_path, "input") # areas list -antaresRead::getAreas(opts = opts_v860) +antaresRead::getAreas(opts = opts_test) # remove BC none v870 -names_bc_to_remove <- names(readBindingConstraints(opts = opts_v860)) +names_bc_to_remove <- names(readBindingConstraints(opts = opts_test)) lapply(names_bc_to_remove, removeBindingConstraint, @@ -195,7 +195,7 @@ lapply(names_bc_to_remove, # temporary to test with "870" # force version -opts_v860$antaresVersion <- 870 +opts_test$antaresVersion <- 870 # scenarized data hourly n <- 10 @@ -227,10 +227,10 @@ test_that("createBindingConstraint (default group value) v8.7", { timeStep = "hourly", operator = "both", coefficients = c("al%gr" = 1), - opts = opts_v860 + opts = opts_test ) - bc <- readBindingConstraints(opts = opts_v860) + bc <- readBindingConstraints(opts = opts_test) # tests testthat::expect_true("myconstraint" %in% @@ -263,13 +263,13 @@ test_that("createBindingConstraintBulk v8.7", { } ) # create all constraints - createBindingConstraintBulk(bindings_constraints, opts = opts_v860) + createBindingConstraintBulk(bindings_constraints, opts = opts_test) # tests testthat::expect_true("constraints1" %in% - names(readBindingConstraints(opts = opts_v860))) + names(readBindingConstraints(opts = opts_test))) testthat::expect_true("constraints10" %in% - names(readBindingConstraints(opts = opts_v860))) + names(readBindingConstraints(opts = opts_test))) }) @@ -284,7 +284,7 @@ test_that("createBindingConstraint check group values v8.7", { timeStep = "hourly", operator = "both", coefficients = c("al%gr" = 1), - opts = opts_v860 + opts = opts_test ) # ADD binding constraint still (default group value) @@ -295,7 +295,7 @@ test_that("createBindingConstraint check group values v8.7", { timeStep = "hourly", operator = "both", coefficients = c("al%gr" = 1), - opts = opts_v860 + opts = opts_test ) # ADD binding constraint with named group @@ -307,14 +307,14 @@ test_that("createBindingConstraint check group values v8.7", { operator = "both", group = "group_test", coefficients = c("al%gr" = 1), - opts = opts_v860 + opts = opts_test ) # tests testthat::expect_true( all( c("myconstraint_group", "myconstraint_group_bis", "myconstraint_group1") %in% - names(readBindingConstraints(opts = opts_v860)) + names(readBindingConstraints(opts = opts_test)) ) ) @@ -327,7 +327,7 @@ test_that("createBindingConstraint check group values v8.7", { timeStep = "daily", operator = "both", coefficients = c("al%gr" = 1), - opts = opts_v860 + opts = opts_test ) ) @@ -340,7 +340,7 @@ test_that("createBindingConstraint check group values v8.7", { timeStep = "daily", operator = "both", coefficients = c("al%gr" = 1), - opts = opts_v860 + opts = opts_test ) ) @@ -354,7 +354,7 @@ test_that("createBindingConstraint check group values v8.7", { operator = "both", group = "group_test", coefficients = c("al%gr" = 1), - opts = opts_v860) + opts = opts_test) ) # ADD binding constraint with existing group @@ -366,12 +366,12 @@ test_that("createBindingConstraint check group values v8.7", { operator = "both", group = "group_test", coefficients = c("al%gr" = 1), - opts = opts_v860) + opts = opts_test) - bc <- readBindingConstraints(opts = opts_v860) + bc <- readBindingConstraints(opts = opts_test) testthat::expect_true("myconstraint_group2" %in% - names(readBindingConstraints(opts = opts_v860))) + names(readBindingConstraints(opts = opts_test))) testthat::expect_equal(bc[["myconstraint_group2"]]$group, "group_test") @@ -387,10 +387,10 @@ test_that("createBindingConstraint check values (NULL) group v8.7", { operator = "both", group = "null_values", coefficients = c("al%gr" = 1), - opts = opts_v860 + opts = opts_test ) - bc <- readBindingConstraints(opts = opts_v860) + bc <- readBindingConstraints(opts = opts_test) # check name testthat::expect_true("myconstraint_group_NULL" %in% @@ -411,10 +411,10 @@ test_that("createBindingConstraint check values (NULL) group v8.7", { timeStep = "hourly", group = "null_values", coefficients = c("al%gr" = 1), - opts = opts_v860 + opts = opts_test ) - bc <- readBindingConstraints(opts = opts_v860) + bc <- readBindingConstraints(opts = opts_test) # check name testthat::expect_true("myconstraint_group_3" %in% diff --git a/tests/testthat/test-createCluster.R b/tests/testthat/test-createCluster.R index 68f5ea41..c54830ba 100644 --- a/tests/testthat/test-createCluster.R +++ b/tests/testthat/test-createCluster.R @@ -85,7 +85,7 @@ sapply(studies, function(study) { # v860 ---- # global params for structure v8.6 -setup_study_860(sourcedir860) +setup_study_last(sourcedir_last_study) opts_test <- antaresRead::setSimulationPath(study_temp_path, "input") test_that("Create cluster with pollutants params (new feature v8.6)",{ diff --git a/tests/testthat/test-createClusterST.R b/tests/testthat/test-createClusterST.R index 2310e6f9..fbc73d1b 100644 --- a/tests/testthat/test-createClusterST.R +++ b/tests/testthat/test-createClusterST.R @@ -1,6 +1,6 @@ # global params for structure v8.6 ---- -setup_study_860(sourcedir860) +setup_study_last(sourcedir_last_study) opts_test <- antaresRead::setSimulationPath(study_temp_path, "input") path_master <- file.path(opts_test$inputPath, "st-storage") diff --git a/tests/testthat/test-editBindingConstraint.R b/tests/testthat/test-editBindingConstraint.R index ffec21a8..b64690fc 100644 --- a/tests/testthat/test-editBindingConstraint.R +++ b/tests/testthat/test-editBindingConstraint.R @@ -39,14 +39,14 @@ test_that("editBindingConstraint v710", { ## Global data # read / open template study -setup_study_860(dir_path = sourcedir860) -opts_v860 <- antaresRead::setSimulationPath(study_temp_path, "input") +setup_study_last(dir_path = sourcedir_last_study) +opts_test <- antaresRead::setSimulationPath(study_temp_path, "input") # areas list -antaresRead::getAreas(opts = opts_v860) +antaresRead::getAreas(opts = opts_test) # remove BC none v870 -names_bc_to_remove <- names(readBindingConstraints(opts = opts_v860)) +names_bc_to_remove <- names(readBindingConstraints(opts = opts_test)) lapply(names_bc_to_remove, removeBindingConstraint, @@ -54,7 +54,7 @@ lapply(names_bc_to_remove, # temporary to test with "870" # force version -opts_v860$antaresVersion <- 870 +opts_test$antaresVersion <- 870 # scenarized data # hourly @@ -86,10 +86,10 @@ test_that("editBindingConstraint v8.6", { timeStep = "hourly", operator = "both", coefficients = c("al%gr" = 1), - opts = opts_v860 + opts = opts_test ) - bc_names_v870 <- names(readBindingConstraints(opts = opts_v860)) + bc_names_v870 <- names(readBindingConstraints(opts = opts_test)) # edit BC with NULL values (edit only .ini file) editBindingConstraint(name = bc_names_v870, @@ -97,9 +97,9 @@ test_that("editBindingConstraint v8.6", { timeStep = "daily", operator = "both", coefficients = c("al%gr"= 7.45), - opts = opts_v860) + opts = opts_test) - bc_modified <- readBindingConstraints(opts = opts_v860) + bc_modified <- readBindingConstraints(opts = opts_test) new_coef <- bc_modified[[bc_names_v870[1]]]$coefs # test @@ -114,9 +114,9 @@ test_that("editBindingConstraint v8.6", { coefficients = c("00_pump_d%gr" = 12, "gr%00_turb_d" = 0, "al%gr"= 0.5), - opts = opts_v860) + opts = opts_test) - bc_modified <- readBindingConstraints(opts = opts_v860) + bc_modified <- readBindingConstraints(opts = opts_test) new_coef <- bc_modified[[bc_names_v870[1]]]$coefs # test coefs @@ -136,7 +136,7 @@ test_that("editBindingConstraint v8.6", { ncol = 9)), timeStep = "daily", operator = "both", - opts = opts_v860), + opts = opts_test), regexp = "Put right columns dimension : 10 for existing 'group' : default group" ) @@ -158,23 +158,23 @@ test_that("editBindingConstraint v8.6", { operator = "both", coefficients = c("al%gr" = 1), group = "new", - opts = opts_v860 + opts = opts_test ) # edit group of last bindingConstraint with bad dimension testthat::expect_error( editBindingConstraint(name = "myconstraint_new", group = "default group", - opts = opts_v860), + opts = opts_test), regexp = "Put right columns dimension : 10 for existing 'group' : default group" ) # edit param editBindingConstraint(name = "myconstraint_new", operator = "less", - opts = opts_v860) + opts = opts_test) - bc_modified <- readBindingConstraints(opts = opts_v860) + bc_modified <- readBindingConstraints(opts = opts_test) new_param <- bc_modified[["myconstraint_new"]]$operator # test coefs diff --git a/tests/testthat/test-editCluster.R b/tests/testthat/test-editCluster.R index a5f6a8b7..89bf480a 100644 --- a/tests/testthat/test-editCluster.R +++ b/tests/testthat/test-editCluster.R @@ -53,7 +53,7 @@ sapply(studies, function(study) { # v860 ---- # global params for structure v8.6 -setup_study_860(sourcedir860) +setup_study_last(sourcedir_last_study) opts_test <- antaresRead::setSimulationPath(study_temp_path, "input") test_that("Edit cluster with pollutants params (new feature v8.6)",{ diff --git a/tests/testthat/test-editClusterST.R b/tests/testthat/test-editClusterST.R index 48eb14e8..d6f4d719 100644 --- a/tests/testthat/test-editClusterST.R +++ b/tests/testthat/test-editClusterST.R @@ -1,7 +1,7 @@ test_that("edit st-storage clusters (only for study >= v8.6.0" , { # global params for structure v8.6 ---- - setup_study_860(sourcedir860) + setup_study_last(sourcedir_last_study) opts_test <- antaresRead::setSimulationPath(study_temp_path, "input") # areas tests diff --git a/tests/testthat/test-removeBindingConstraint.R b/tests/testthat/test-removeBindingConstraint.R index 060d4e49..c38d3632 100644 --- a/tests/testthat/test-removeBindingConstraint.R +++ b/tests/testthat/test-removeBindingConstraint.R @@ -3,14 +3,14 @@ ## Global data # read / open template study -setup_study_860(dir_path = sourcedir860) -opts_v860 <- antaresRead::setSimulationPath(study_temp_path, "input") +setup_study_last(dir_path = sourcedir_last_study) +opts_test <- antaresRead::setSimulationPath(study_temp_path, "input") # areas list -antaresRead::getAreas(opts = opts_v860) +antaresRead::getAreas(opts = opts_test) # remove BC none v870 -names_bc_to_remove <- names(readBindingConstraints(opts = opts_v860)) +names_bc_to_remove <- names(readBindingConstraints(opts = opts_test)) lapply(names_bc_to_remove, removeBindingConstraint, @@ -18,7 +18,7 @@ lapply(names_bc_to_remove, # temporary to test with "870" # force version -opts_v860$antaresVersion <- 870 +opts_test$antaresVersion <- 870 # scenarized data # hourly @@ -49,7 +49,7 @@ test_that("removeBindingConstraint v8.6", { timeStep = "hourly", operator = "both", coefficients = c("al%gr" = 1), - opts = opts_v860 + opts = opts_test ) createBindingConstraint( @@ -60,7 +60,7 @@ test_that("removeBindingConstraint v8.6", { operator = "both", group = "group_test", coefficients = c("al%gr" = 1), - opts = opts_v860 + opts = opts_test ) createBindingConstraint( @@ -71,22 +71,22 @@ test_that("removeBindingConstraint v8.6", { operator = "both", group = "group_test", coefficients = c("al%gr" = 1), - opts = opts_v860 + opts = opts_test ) - bc_names_v870 <- names(readBindingConstraints(opts = opts_v860)) + bc_names_v870 <- names(readBindingConstraints(opts = opts_test)) removeBindingConstraint(bc_names_v870[1], - opts = opts_v860) + opts = opts_test) - bc_in_study <- readBindingConstraints(opts = opts_v860) + bc_in_study <- readBindingConstraints(opts = opts_test) # test if(is.null(bc_in_study)) testthat::expect_null(names(bc_in_study)) else testthat::expect_false("myconstraint" %in% - names(readBindingConstraints(opts = opts_v860))) + names(readBindingConstraints(opts = opts_test))) # remove temporary study unlink(x = study_temp_path, recursive = TRUE) diff --git a/tests/testthat/test-scenarioBuilder.R b/tests/testthat/test-scenarioBuilder.R index ec04bb14..adac8fab 100644 --- a/tests/testthat/test-scenarioBuilder.R +++ b/tests/testthat/test-scenarioBuilder.R @@ -352,35 +352,35 @@ test_that("updateScenarioBuilder() for hl with all values between 0 and 1", { ## Global data # read / open template study -setup_study_860(dir_path = sourcedir860) -opts_v860 <- antaresRead::setSimulationPath(study_temp_path, "input") +setup_study_last(dir_path = sourcedir_last_study) +opts_test <- antaresRead::setSimulationPath(study_temp_path, "input") # areas list -antaresRead::getAreas(opts = opts_v860) +antaresRead::getAreas(opts = opts_test) # temporary to test with "870" # force version -opts_v860$antaresVersion <- 870 +opts_test$antaresVersion <- 870 test_that("scenarioBuilder works with binding constraint (v870)", { # Read, create & update scenario builder sbuilder <- scenarioBuilder( - n_scenario = opts_v860$parameters$general$nbyears, - n_mc = opts_v860$parameters$general$nbyears, + n_scenario = opts_test$parameters$general$nbyears, + n_mc = opts_test$parameters$general$nbyears, areas = getAreas()[1:3], areas_rand = getAreas()[1:2], - opts = opts_v860 + opts = opts_test ) # Read previous scenario builder # in a matrix format - prev_sb <- readScenarioBuilder(opts = opts_v860, as_matrix = TRUE) + prev_sb <- readScenarioBuilder(opts = opts_test, as_matrix = TRUE) # Update scenario builder # for binding constraints series - updateScenarioBuilder(ldata = sbuilder, series = "binding") + updateScenarioBuilder(ldata = sbuilder, series = "bc", opts = opts_test) # remove temporary study unlink(x = study_temp_path, recursive = TRUE) diff --git a/tests/testthat/test-writeHydroValues.R b/tests/testthat/test-writeHydroValues.R index 7e152d15..21df7698 100644 --- a/tests/testthat/test-writeHydroValues.R +++ b/tests/testthat/test-writeHydroValues.R @@ -3,7 +3,7 @@ context("Function writeHydroValues") #WriteHydroValues does not depend on antaresVersion. # waterValues ---- # global params for structure v8.6 -setup_study_860(sourcedir860) +setup_study_last(sourcedir_last_study) #Avoid warning related to code writed outside test_that. suppressWarnings(opts <- antaresRead::setSimulationPath(study_temp_path, "input")) diff --git a/tests/testthat/test-writeInputTS.R b/tests/testthat/test-writeInputTS.R index 533ebe02..5883a1ee 100644 --- a/tests/testthat/test-writeInputTS.R +++ b/tests/testthat/test-writeInputTS.R @@ -98,7 +98,7 @@ sapply(studies, function(study) { # v860 ---- -setup_study_860(sourcedir860) +setup_study_last(sourcedir_last_study) #Avoid warning related to code writed outside test_that. suppressWarnings(opts <- antaresRead::setSimulationPath(study_temp_path, "input")) From 241040b048a696555b5d2029f4931344107abfb0 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Tue, 16 Jan 2024 08:37:50 +0100 Subject: [PATCH 12/74] skip to fix binding constraints --- tests/testthat/test-createBindingConstraint.R | 26 +++++++++---------- tests/testthat/test-createDSR.R | 2 +- tests/testthat/test-createPSP.R | 2 +- tests/testthat/test-editBindingConstraint.R | 6 +++-- tests/testthat/test-removeBindingConstraint.R | 5 ++-- tests/testthat/test-updateBindingConstraint.R | 1 + 6 files changed, 23 insertions(+), 19 deletions(-) diff --git a/tests/testthat/test-createBindingConstraint.R b/tests/testthat/test-createBindingConstraint.R index e04b27ea..09a0d72d 100644 --- a/tests/testthat/test-createBindingConstraint.R +++ b/tests/testthat/test-createBindingConstraint.R @@ -181,21 +181,21 @@ sapply(studies, function(study) { # read / open template study setup_study_last(dir_path = sourcedir_last_study) -opts_test <- antaresRead::setSimulationPath(study_temp_path, "input") +opts_test <- antaresRead::setSimulationPath(study_latest_version, "input") # areas list antaresRead::getAreas(opts = opts_test) -# remove BC none v870 -names_bc_to_remove <- names(readBindingConstraints(opts = opts_test)) - -lapply(names_bc_to_remove, - removeBindingConstraint, - opts = simOptions()) - -# temporary to test with "870" -# force version -opts_test$antaresVersion <- 870 + # # remove BC none v870 + # names_bc_to_remove <- names(readBindingConstraints(opts = opts_test)) + # + # lapply(names_bc_to_remove, + # removeBindingConstraint, + # opts = simOptions()) + # + # # temporary to test with "870" + # # force version + # opts_test$antaresVersion <- 870 # scenarized data hourly n <- 10 @@ -216,7 +216,7 @@ eq_data <- matrix(data = rep(3, 365 * n), ncol = n) scenar_values_daily <- list(lt= lt_data, gt= gt_data, eq= eq_data) - +testthat::skip() test_that("createBindingConstraint (default group value) v8.7", { # create binding constraint @@ -421,5 +421,5 @@ test_that("createBindingConstraint check values (NULL) group v8.7", { names(bc)) # remove temporary study - unlink(x = study_temp_path, recursive = TRUE) + unlink(x = study_latest_version, recursive = TRUE) }) diff --git a/tests/testthat/test-createDSR.R b/tests/testthat/test-createDSR.R index 5b1d7aec..c458fa7c 100644 --- a/tests/testthat/test-createDSR.R +++ b/tests/testthat/test-createDSR.R @@ -1,5 +1,5 @@ - +testthat::skip() context("Function createDSR") diff --git a/tests/testthat/test-createPSP.R b/tests/testthat/test-createPSP.R index 936536f8..2e0c566e 100644 --- a/tests/testthat/test-createPSP.R +++ b/tests/testthat/test-createPSP.R @@ -1,5 +1,5 @@ - +testthat::skip() context("Function createPSP") diff --git a/tests/testthat/test-editBindingConstraint.R b/tests/testthat/test-editBindingConstraint.R index b64690fc..977c9535 100644 --- a/tests/testthat/test-editBindingConstraint.R +++ b/tests/testthat/test-editBindingConstraint.R @@ -37,10 +37,12 @@ test_that("editBindingConstraint v710", { # v870 ---- +testthat::skip() + ## Global data # read / open template study setup_study_last(dir_path = sourcedir_last_study) -opts_test <- antaresRead::setSimulationPath(study_temp_path, "input") +opts_test <- antaresRead::setSimulationPath(study_latest_version, "input") # areas list antaresRead::getAreas(opts = opts_test) @@ -182,7 +184,7 @@ test_that("editBindingConstraint v8.6", { # remove temporary study - unlink(x = study_temp_path, recursive = TRUE) + unlink(x = study_latest_version, recursive = TRUE) }) diff --git a/tests/testthat/test-removeBindingConstraint.R b/tests/testthat/test-removeBindingConstraint.R index c38d3632..d2f6f583 100644 --- a/tests/testthat/test-removeBindingConstraint.R +++ b/tests/testthat/test-removeBindingConstraint.R @@ -1,10 +1,11 @@ +testthat::skip() # v870 ---- ## Global data # read / open template study setup_study_last(dir_path = sourcedir_last_study) -opts_test <- antaresRead::setSimulationPath(study_temp_path, "input") +opts_test <- antaresRead::setSimulationPath(study_latest_version, "input") # areas list antaresRead::getAreas(opts = opts_test) @@ -89,6 +90,6 @@ test_that("removeBindingConstraint v8.6", { names(readBindingConstraints(opts = opts_test))) # remove temporary study - unlink(x = study_temp_path, recursive = TRUE) + unlink(x = study_latest_version, recursive = TRUE) }) diff --git a/tests/testthat/test-updateBindingConstraint.R b/tests/testthat/test-updateBindingConstraint.R index fcf508b0..9c647d24 100644 --- a/tests/testthat/test-updateBindingConstraint.R +++ b/tests/testthat/test-updateBindingConstraint.R @@ -1,3 +1,4 @@ +testthat::skip() context("Function editBindingConstraint") test_that("editBindingConstraint tests", { From 9a101e7fe8ca592907a97d2b867b231c75cfe7cf Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Tue, 16 Jan 2024 08:39:59 +0100 Subject: [PATCH 13/74] skip cause study test has not mod.txt file expected --- tests/testthat/test-writeInputTS.R | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-writeInputTS.R b/tests/testthat/test-writeInputTS.R index 5883a1ee..664e321a 100644 --- a/tests/testthat/test-writeInputTS.R +++ b/tests/testthat/test-writeInputTS.R @@ -101,10 +101,11 @@ sapply(studies, function(study) { setup_study_last(sourcedir_last_study) #Avoid warning related to code writed outside test_that. -suppressWarnings(opts <- antaresRead::setSimulationPath(study_temp_path, "input")) +suppressWarnings( + opts <- antaresRead::setSimulationPath(study_latest_version, "input")) test_that("create mingen file data v860", { - + testthat::skip() #Initialize mingen data M_mingen = matrix(0,8760,5) From 4a0e4108768cc83e90c6470421ba01161bbe5649 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Tue, 16 Jan 2024 08:40:50 +0100 Subject: [PATCH 14/74] study test v8.7.0 integration ok --- tests/testthat/helper_init.R | 11 +++++------ tests/testthat/test-createCluster.R | 2 +- tests/testthat/test-createClusterBulk.R | 4 ++-- tests/testthat/test-createClusterST.R | 9 +++++---- tests/testthat/test-editCluster.R | 2 +- tests/testthat/test-editClusterST.R | 2 +- tests/testthat/test-scenarioBuilder.R | 4 ++-- tests/testthat/test-writeHydroValues.R | 6 +++--- 8 files changed, 20 insertions(+), 20 deletions(-) diff --git a/tests/testthat/helper_init.R b/tests/testthat/helper_init.R index 8d833a88..da962094 100644 --- a/tests/testthat/helper_init.R +++ b/tests/testthat/helper_init.R @@ -32,15 +32,14 @@ sourcedir_last_study <- system.file("test_v8", package = "antaresRead") setup_study_last <- function(dir_path){ studies <- list.files(dir_path, pattern = "\\.tar\\.gz$", full.names = TRUE) - studies_last_version <- studies[grep(x = studies, pattern = "v86")] + studies_last_version <- studies[grep(x = studies, pattern = "v87")] # untar etude - path_last_version <- file.path(tempdir(), "studyv860") - untar(studies_last_version[1], exdir = path_last_version) # v86 + path_last_version <- file.path(tempdir(), "studyv870") + untar(studies_last_version[1], exdir = path_last_version) # v87 study_temp_path <- file.path(path_last_version, "test_case") - assign("study_temp_path", - file.path(path_last_version, - "test_case"), + assign("study_latest_version", + study_temp_path, envir = globalenv()) } diff --git a/tests/testthat/test-createCluster.R b/tests/testthat/test-createCluster.R index c54830ba..7b3b7739 100644 --- a/tests/testthat/test-createCluster.R +++ b/tests/testthat/test-createCluster.R @@ -86,7 +86,7 @@ sapply(studies, function(study) { # v860 ---- # global params for structure v8.6 setup_study_last(sourcedir_last_study) -opts_test <- antaresRead::setSimulationPath(study_temp_path, "input") +opts_test <- antaresRead::setSimulationPath(study_latest_version, "input") test_that("Create cluster with pollutants params (new feature v8.6)",{ diff --git a/tests/testthat/test-createClusterBulk.R b/tests/testthat/test-createClusterBulk.R index ab82693f..385e5440 100644 --- a/tests/testthat/test-createClusterBulk.R +++ b/tests/testthat/test-createClusterBulk.R @@ -11,8 +11,8 @@ studies <- list.files(sourcedir, pattern = "\\.tar\\.gz$", full.names = TRUE) # untar etude untar(studies[1], exdir = tempdir()) # v8 -study_temp_path <- file.path(tempdir(), "test_case") -opts_temp <- antaresRead::setSimulationPath(study_temp_path, "input") +study_latest_version <- file.path(tempdir(), "test_case") +opts_temp <- antaresRead::setSimulationPath(study_latest_version, "input") # areas list antaresRead::getAreas() diff --git a/tests/testthat/test-createClusterST.R b/tests/testthat/test-createClusterST.R index fbc73d1b..ef9b35bc 100644 --- a/tests/testthat/test-createClusterST.R +++ b/tests/testthat/test-createClusterST.R @@ -1,7 +1,7 @@ # global params for structure v8.6 ---- setup_study_last(sourcedir_last_study) -opts_test <- antaresRead::setSimulationPath(study_temp_path, "input") +opts_test <- antaresRead::setSimulationPath(study_latest_version, "input") path_master <- file.path(opts_test$inputPath, "st-storage") @@ -10,7 +10,7 @@ if (opts_test$antaresVersion >= 860){ ## basics errors cases ---- # default area with st cluster - area_test_clust = "al" + area_test_clust = antaresRead::getAreas()[1] # study parameters # version ? == is ST study compatibility @@ -21,13 +21,14 @@ if (opts_test$antaresVersion >= 860){ regexp = "is not a valid area name") # bad dimension of data parameters - testthat::expect_error(createClusterST(area_test_clust, "cluster1", + testthat::expect_error(createClusterST(area_test_clust, + "cluster1", PMAX_injection = matrix(1, 2, 2), opts = opts_test), regexp = "Input data for") # cluster already exist - name_st_clust <-levels(readClusterSTDesc(opts = opts_test)$cluster) + name_st_clust <-levels(readClusterSTDesc(opts = opts_test)$cluster)[1] testthat::expect_error(createClusterST(area_test_clust, name_st_clust, add_prefix = FALSE, diff --git a/tests/testthat/test-editCluster.R b/tests/testthat/test-editCluster.R index 89bf480a..e175cbd5 100644 --- a/tests/testthat/test-editCluster.R +++ b/tests/testthat/test-editCluster.R @@ -54,7 +54,7 @@ sapply(studies, function(study) { # v860 ---- # global params for structure v8.6 setup_study_last(sourcedir_last_study) -opts_test <- antaresRead::setSimulationPath(study_temp_path, "input") +opts_test <- antaresRead::setSimulationPath(study_latest_version, "input") test_that("Edit cluster with pollutants params (new feature v8.6)",{ diff --git a/tests/testthat/test-editClusterST.R b/tests/testthat/test-editClusterST.R index d6f4d719..728b8782 100644 --- a/tests/testthat/test-editClusterST.R +++ b/tests/testthat/test-editClusterST.R @@ -2,7 +2,7 @@ test_that("edit st-storage clusters (only for study >= v8.6.0" , { # global params for structure v8.6 ---- setup_study_last(sourcedir_last_study) - opts_test <- antaresRead::setSimulationPath(study_temp_path, "input") + opts_test <- antaresRead::setSimulationPath(study_latest_version, "input") # areas tests area_test = getAreas()[1] diff --git a/tests/testthat/test-scenarioBuilder.R b/tests/testthat/test-scenarioBuilder.R index adac8fab..86a1b9b7 100644 --- a/tests/testthat/test-scenarioBuilder.R +++ b/tests/testthat/test-scenarioBuilder.R @@ -353,7 +353,7 @@ test_that("updateScenarioBuilder() for hl with all values between 0 and 1", { ## Global data # read / open template study setup_study_last(dir_path = sourcedir_last_study) -opts_test <- antaresRead::setSimulationPath(study_temp_path, "input") +opts_test <- antaresRead::setSimulationPath(study_latest_version, "input") # areas list antaresRead::getAreas(opts = opts_test) @@ -383,5 +383,5 @@ test_that("scenarioBuilder works with binding constraint (v870)", { updateScenarioBuilder(ldata = sbuilder, series = "bc", opts = opts_test) # remove temporary study - unlink(x = study_temp_path, recursive = TRUE) + unlink(x = study_latest_version, recursive = TRUE) }) diff --git a/tests/testthat/test-writeHydroValues.R b/tests/testthat/test-writeHydroValues.R index 21df7698..f4910fe8 100644 --- a/tests/testthat/test-writeHydroValues.R +++ b/tests/testthat/test-writeHydroValues.R @@ -6,7 +6,7 @@ context("Function writeHydroValues") setup_study_last(sourcedir_last_study) #Avoid warning related to code writed outside test_that. -suppressWarnings(opts <- antaresRead::setSimulationPath(study_temp_path, "input")) +suppressWarnings(opts <- antaresRead::setSimulationPath(study_latest_version, "input")) test_that("Write hydro values, 'waterValues' case", { @@ -22,7 +22,7 @@ test_that("Write hydro values, 'waterValues' case", { data = m_water , overwrite = FALSE) - values_file <- file.path(study_temp_path, "input", "hydro", "common", "capacity", + values_file <- file.path(study_latest_version, "input", "hydro", "common", "capacity", paste0("waterValues_", tolower(area), ".txt")) expect_equal(antaresRead:::fread_antares(opts = opts, @@ -105,7 +105,7 @@ test_that("writeHydroValues, reservoir/maxpower/inflowPattern/creditmodulations data = m_data , overwrite = TRUE) - values_file <- file.path(study_temp_path, "input", "hydro", "common", "capacity", + values_file <- file.path(study_latest_version, "input", "hydro", "common", "capacity", paste0(file_type, "_", tolower(area), ".txt")) #Test that the created file respect the matrix. From 48dd25fbf27dbc455000e89e4b48d9ef0db47ec4 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Thu, 18 Jan 2024 09:12:01 +0100 Subject: [PATCH 15/74] fix mingen test with writeInputTS --- tests/testthat/test-writeInputTS.R | 111 ++++++++++++++++++++++------- 1 file changed, 87 insertions(+), 24 deletions(-) diff --git a/tests/testthat/test-writeInputTS.R b/tests/testthat/test-writeInputTS.R index 664e321a..1fb6e314 100644 --- a/tests/testthat/test-writeInputTS.R +++ b/tests/testthat/test-writeInputTS.R @@ -98,22 +98,23 @@ sapply(studies, function(study) { # v860 ---- -setup_study_last(sourcedir_last_study) +## write mingen file ---- + # write mingen file depend of dimension of mod.txt file -#Avoid warning related to code writed outside test_that. -suppressWarnings( - opts <- antaresRead::setSimulationPath(study_latest_version, "input")) - -test_that("create mingen file data v860", { - testthat::skip() +test_that("create mingen file with one or empty column dimension of mod.txt file", { + # create study to have mod empty and mod with one column + createStudy(path = tempdir(), antares_version = "8.6.0") + area <- "zone51" + createArea(area) + #Initialize mingen data M_mingen = matrix(0,8760,5) - # [management rules] for mingen data : # file mod.txt (in /series) have to be same column dimension # or column dimension of 1 or NULL (empty file) + opts <- simOptions() # check dimensions of mod.txt for every areas path_file_mod <- file.path(opts$inputPath, "hydro", "series", getAreas(), @@ -128,7 +129,34 @@ test_that("create mingen file data v860", { names(list_dim) <- getAreas() ## trivial case - # mod.txt column dimension == 1 + + # mod.txt column dimension == 0 (empty file) + area_0 <- getAreas()[list_dim==0][1] + + # write for an area with file mod.txt empty columns == 0 + writeInputTS(area = area_0, + type = "mingen", + data = M_mingen , + overwrite = TRUE, + opts = opts) + + # use antaresRead to test + read_ts_file <- readInputTS(mingen = "all", opts = opts) + + # check your area + testthat::expect_true(area_0 %in% unique(read_ts_file$area)) + + + # mod.txt column dimension == 1 + + list_dim <- lapply(path_file_mod, function(x){ + # read + file <- fread(file = x) + dim_file <- dim(file)[2] + }) + + names(list_dim) <- getAreas() + area_1 <- getAreas()[list_dim==1][1] # write for an area with file mod.txt NULL or nb columns == 1 @@ -139,37 +167,68 @@ test_that("create mingen file data v860", { read_ts_file <- readInputTS(mingen = "all", opts = opts) # tests correct reading data - # check col name "mingen" + # check col name "mingen" testthat::expect_true("mingen" %in% names(read_ts_file)) - # check your area + # check your area testthat::expect_true(area_1 %in% unique(read_ts_file$area)) - # check dimension data for your area + # check dimension data for your area testthat::expect_equal(dim(M_mingen)[2], max(read_ts_file[area %in% area_1, tsId])) - # mod.txt column dimension == 0 (empty file) - area_0 <- getAreas()[list_dim==0][1] + unlink(x = opts, recursive = TRUE) +}) + + +test_that("create mingen file with multi dimension mod.txt file", { - # write for an area with file mod.txt empty columns == 0 - writeInputTS(area = area_0, type = "mingen", - data = M_mingen , overwrite = TRUE, opts = opts) + setup_study_last(sourcedir_last_study) - # use antaresRead to test - read_ts_file <- readInputTS(mingen = "all", opts = opts) + #Avoid warning related to code writed outside test_that. + suppressWarnings( + opts <- antaresRead::setSimulationPath(study_latest_version, "input")) + + + #Initialize mingen data + M_mingen = matrix(0,8760,5) - # check your area - testthat::expect_true(area_0 %in% unique(read_ts_file$area)) + + # [management rules] for mingen data : + # file mod.txt (in /series) have to be same column dimension + # or column dimension of 1 or NULL (empty file) + + # check dimensions of mod.txt for every areas + path_file_mod <- file.path(opts$inputPath, "hydro", "series", + getAreas(), + "mod.txt") + + list_dim <- lapply(path_file_mod, function(x){ + # read + file <- fread(file = x) + dim_file <- dim(file)[2] + }) + + names(list_dim) <- getAreas() + + # PS : study v8.7.0 have only mod files with 5 columns dimension ## multi columns cas for mod.txt file # mod.txt column dimension >= 1 area_mult <- getAreas()[list_dim>1][1] + # rewrite with less columns + mod_data = matrix(60,365,4) + + writeInputTS(area = area_mult, + type = "hydroSTOR", + data = mod_data, + overwrite = TRUE) + # write for an area with file mod.txt >1 columns # error case cause mod.txt dimension testthat::expect_error(writeInputTS(area = area_mult, type = "mingen", data = M_mingen , overwrite = TRUE, opts = opts), - regexp = 'mingen \'data\' must be either a 8760\\*1 or 8760\\*3 matrix.') + regexp = 'mingen \'data\' must be either a 8760\\*1 or 8760\\*4 matrix.') # you can write only mingen file with dimension 1 writeInputTS(area = area_mult, type = "mingen", @@ -191,12 +250,16 @@ test_that("create mingen file data v860", { ## display warning message with type= "hydroSTOR" (minor update function v860) # Wrong format of data, here it must be either 1 or 5 columns. - M_hydrostor <- matrix(c(rep(8, 365), rep(5.1, 365)), nrow = 365) + M_hydrostor <- matrix(c(rep(60, 365), rep(60, 365)), nrow = 365) # warning about the file format - expect_warning(writeInputTS(area = area_1, type = "hydroSTOR", data = M_hydrostor, opts = opts), + expect_warning(writeInputTS(area = getAreas()[2], + type = "hydroSTOR", + data = M_hydrostor, + opts = opts), regexp = "mod 'data' must be") + unlink(x = opts$studyPath, recursive = TRUE) }) From ce43998bf062b79f6c4feec36fba0e6de59d0998 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Thu, 18 Jan 2024 09:26:44 +0100 Subject: [PATCH 16/74] test-createPSP fixed with new structure of binding constraints read --- tests/testthat/test-createPSP.R | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/tests/testthat/test-createPSP.R b/tests/testthat/test-createPSP.R index 2e0c566e..ddc64a3d 100644 --- a/tests/testthat/test-createPSP.R +++ b/tests/testthat/test-createPSP.R @@ -1,8 +1,7 @@ -testthat::skip() context("Function createPSP") - +# >=v710 ---- sapply(studies, function(study) { setup_study(study, sourcedir) @@ -35,9 +34,9 @@ sapply(studies, function(study) { } expect_equal(efficiencyTest, 0.8) - expect_equal(binding$a_psp_weekly$operator, "equal") - expect_equal(binding$a_psp_weekly$timeStep, "weekly") - expect_equal(binding$a_psp_weekly$enabled, TRUE) + expect_equal(binding$a_psp_weekly$properties$operator, "equal") + expect_equal(binding$a_psp_weekly$properties$timeStep, "weekly") + expect_equal(binding$a_psp_weekly$properties$enabled, TRUE) }) @@ -84,9 +83,9 @@ sapply(studies, function(study) { efficiencyTest<-as.double(binding$b_psp_daily$coefs["b%psp_in_d"]) } expect_equal(efficiencyTest, 0.66) - expect_equal(binding$b_psp_daily$operator, "equal") - expect_equal(binding$b_psp_daily$timeStep, "daily") - expect_equal(binding$b_psp_daily$enabled, TRUE) + expect_equal(binding$b_psp_daily$properties$operator, "equal") + expect_equal(binding$b_psp_daily$properties$timeStep, "daily") + expect_equal(binding$b_psp_daily$properties$enabled, TRUE) }) @@ -146,9 +145,9 @@ sapply(studies, function(study) { binding<-readBindingConstraints() expect_equal(as.double(binding$suisse_psp_daily$coefs["psp_in_d%suisse"]), 0.5) - expect_equal(binding$suisse_psp_daily$operator, "equal") - expect_equal(binding$suisse_psp_daily$timeStep, "daily") - expect_equal(binding$suisse_psp_daily$enabled, TRUE) + expect_equal(binding$suisse_psp_daily$properties$operator, "equal") + expect_equal(binding$suisse_psp_daily$properties$timeStep, "daily") + expect_equal(binding$suisse_psp_daily$properties$enabled, TRUE) }) test_that("Get and set the PSP ", { From a0a09c1448c8e6ea5be554c18586c335efc90e0b Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Thu, 18 Jan 2024 09:30:56 +0100 Subject: [PATCH 17/74] test-createDSR fiwed with new structure binding constraint object --- tests/testthat/test-createDSR.R | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/testthat/test-createDSR.R b/tests/testthat/test-createDSR.R index c458fa7c..4fd4c7db 100644 --- a/tests/testthat/test-createDSR.R +++ b/tests/testthat/test-createDSR.R @@ -1,5 +1,4 @@ -testthat::skip() context("Function createDSR") @@ -30,9 +29,9 @@ sapply(studies, function(study) { bindingList<-antaresRead::readBindingConstraints(opts = optsRes) expect_true("a_dsr_3h" %in% names(bindingList)) expect_true("b_dsr_7h" %in% names(bindingList)) - expect_equal(bindingList$a_dsr_3h$enabled, TRUE) - expect_equal(bindingList$a_dsr_3h$timeStep, "daily") - expect_equal(bindingList$a_dsr_3h$operator, "less") + expect_equal(bindingList$a_dsr_3h$properties$enabled, TRUE) + expect_equal(bindingList$a_dsr_3h$properties$timeStep, "daily") + expect_equal(bindingList$a_dsr_3h$properties$operator, "less") expect_equal(as.double(bindingList$a_dsr_3h$coefs["a%a_dsr_3h"]), -1) expect_equal(as.double(bindingList$b_dsr_7h$coefs["b%b_dsr_7h"]), -1) From 97522c9272970f76cef9915b41b97682167680ba Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Thu, 18 Jan 2024 14:18:29 +0100 Subject: [PATCH 18/74] test-updateBindingConstraint fixed with new strucuture of bc object --- tests/testthat/test-updateBindingConstraint.R | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/testthat/test-updateBindingConstraint.R b/tests/testthat/test-updateBindingConstraint.R index 9c647d24..8a3a9b58 100644 --- a/tests/testthat/test-updateBindingConstraint.R +++ b/tests/testthat/test-updateBindingConstraint.R @@ -1,4 +1,3 @@ -testthat::skip() context("Function editBindingConstraint") test_that("editBindingConstraint tests", { @@ -22,8 +21,8 @@ test_that("editBindingConstraint tests", { editBindingConstraint("myconstraint", enabled = TRUE) bc2 <- antaresRead::readBindingConstraints() bc2 <- bc2[["myconstraint"]] - expect_true(bc2$enabled) - bc2$enabled <- FALSE + expect_true(bc2$properties$enabled) + bc2$properties$enabled <- FALSE bc$values <- data.frame(bc$values) bc2$values <- data.frame(bc2$values) expect_true(identical(bc, bc2)) @@ -43,7 +42,7 @@ test_that("editBindingConstraint tests", { ##Write values expect_true(sum(bc$myconstraint$values) == 0) - bc$myconstraint$timeStep + bc$myconstraint$properties$timeStep editBindingConstraint("myconstraint", values = matrix(data = rep(1, 8760 * 3), ncol = 3)) bc <- antaresRead::readBindingConstraints() expect_true(sum(bc$myconstraint$values) > 0 ) From 02b3a83ac3156920f82fec58a6c92724abba07ec Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Thu, 25 Jan 2024 14:13:18 +0100 Subject: [PATCH 19/74] script to generate study test v8.6.0 --- inst/study_test_generator/generate_template.R | 337 ++++++++++++++++++ 1 file changed, 337 insertions(+) create mode 100644 inst/study_test_generator/generate_template.R diff --git a/inst/study_test_generator/generate_template.R b/inst/study_test_generator/generate_template.R new file mode 100644 index 00000000..85de96be --- /dev/null +++ b/inst/study_test_generator/generate_template.R @@ -0,0 +1,337 @@ +library(waldo) +library(antaresEditObject) + + +# create template study for next release + +# params ---- +version <- "8.6.0" +name <- "test_case" + +# create study ---- +createStudy(path = tempdir(), + study_name = name, + antares_version = version) + +# areas ---- +lapply(c("fr", "it", "at"), + createArea) + +# thermal clusters ---- +pollutants_tests_values <- list_pollutants_values(multi_values = 0.3) + + # folder prepro/area/cluster/{data, mudulation} + # are created with default values + +capacity <- 500 +count <- 3L + + # data TS (data + modulation with default) +cluster_ts_data <- matrix(count*capacity, 8760, 1) +cluster_ts_data <- cbind(cluster_ts_data, + matrix(count*capacity*1.25, 8760, 1)) + +createCluster(area = getAreas()[1], + cluster_name = "gas", + group = "Gas", + unitcount = count, + nominalcapacity = capacity, + min_stable_power = 180, + min_up_time = 3L, + marginal_cost = 135.9, + market_bid_cost = 135.9, + list_pollutants = pollutants_tests_values, + time_series = cluster_ts_data) + +createCluster(area = getAreas()[2], + cluster_name = "oil", + group = "Oil", + unitcount = count, + nominalcapacity = capacity, + min_stable_power = 280, + min_up_time = 3L, + marginal_cost = 535.9, + market_bid_cost = 535.9, + list_pollutants = pollutants_tests_values, + time_series = cluster_ts_data) + +createCluster(area = getAreas()[3], + cluster_name = "nuc", + group = "Nuclear", + unitcount = count, + nominalcapacity = capacity, + min_stable_power = 280, + min_up_time = 3L, + marginal_cost = 835.9, + market_bid_cost = 835.9, + list_pollutants = pollutants_tests_values, + time_series = cluster_ts_data) + +# renewables ---- +cluster_ts_res_data <- cluster_ts_data/2 + + # production factor +createClusterRES(area = getAreas()[1], + cluster_name = "res_1", + group = "Other RES 1", + unitcount = count, + nominalcapacity = capacity/2, + enabled = TRUE, + ts_interpretation = "production-factor", + time_series = cluster_ts_res_data) + + # power generation +createClusterRES(area = getAreas()[2], + cluster_name = "res_2", + group = "Other RES 2", + unitcount = count, + nominalcapacity = capacity/2, + enabled = TRUE, + ts_interpretation = "power-generation", + time_series = cluster_ts_res_data) + +# load ---- + # calculated with cluster params (for every area) +load_value <- count*capacity +load_value_2 <- load_value*0.75 + +data_load <- matrix(c( + rep(load_value, 8760), + rep(load_value_2, 8760)), ncol = 2) + +lapply(getAreas(), + writeInputTS, + data=data_load, + type="load") + +#writeInputTS(data = data_load, area = getAreas()[1]) + +# links ---- + # set properties +link_properties <- propertiesLinkOptions() + + # data link +ts_link <- matrix(rep(count*capacity/3, 8760*2), ncol = 2) + +createLink(from = getAreas()[1], + to = getAreas()[2], + propertiesLink = link_properties, + tsLink = ts_link) + +createLink(from = getAreas()[2], + to = getAreas()[3], + propertiesLink = link_properties, + tsLink = ts_link) + +# binding constraints ---- +less <- rep(200, 8760) +greater <- rep(300, 8760) +equal <- rep(400, 8760) +data_bc <- matrix(cbind(less, greater, equal), + ncol = 3) + +createBindingConstraint(name = "bc_1", + values = data_bc, + timeStep = "hourly", + operator = "less", + filter_year_by_year = "hourly", + filter_synthesis = "hourly", + coefficients = c("at%fr" = 1)) + +createBindingConstraint(name = "bc_2", + values = data_bc[1:365,], + timeStep = "weekly", + operator = "greater", + filter_year_by_year = "hourly", + filter_synthesis = "hourly", + coefficients = c("fr%it" = 1)) + +createBindingConstraint(name = "bc_3", + values = data_bc[1:365,], + timeStep = "weekly", + operator = "equal", + filter_year_by_year = "hourly", + filter_synthesis = "hourly", + coefficients = c("fr%it" = 1)) + +createBindingConstraint(name = "bc_4", + values = NULL, + timeStep = "daily", + operator = "both", + filter_year_by_year = "hourly", + filter_synthesis = "hourly") + +# st-storage ---- +inflows_data <- matrix(3, 8760) +ratio_values <- matrix(0.7, 8760) + +list_params_st <- storage_values_default() +list_params_st$efficiency <- 0.9 +list_params_st$reservoircapacity <- 500 +list_params_st$injectionnominalcapacity <- 100 +list_params_st$withdrawalnominalcapacity <- 100 +list_params_st$initiallevel <- 0.1 + +# creation with data default values +createClusterST(area = getAreas()[1], + cluster_name = "st_batt", + group = "Battery", + storage_parameters = list_params_st) + +createClusterST(area = getAreas()[2], + cluster_name = "st_other1", + group = "Other1", + storage_parameters = list_params_st) + +createClusterST(area = getAreas()[3], + cluster_name = "st_pondage", + group = "Pondage", + storage_parameters = list_params_st) + + +# hydro ---- + # properties hydro.ini (use writeIniHydro()) + # /series (fichiers mod, ror, mingen => writeInputTS()) + # /common/capacity data maxpower (use writeHydroValues()) + # /prepro (no need for mingen) + # /allocation (no need for mingen) + +hydro_ini <- readIni(pathIni = "input/hydro/hydro") + + # for every areas +hydro_params <- c('use heuristic', 'follow load', "reservoir") +hydro_ini[hydro_params] + +# weekly rules for mingen checks +hydro_ini$`follow load`[[getAreas()[1]]] <- FALSE + +# annual rules for mingen checks +hydro_ini$reservoir[[getAreas()[2]]] <- TRUE + + # create section [reservoir capacity] +section_name <- "reservoir capacity" +hydro_ini[[section_name]] <- list(area_name = 11840000) + +names(hydro_ini[[section_name]]) <- getAreas()[2] + +# add new section to write +hydro_params <- append(hydro_params, section_name) + +# last area is on monthly mode + + ## write properties ---- +lapply(getAreas(), function(x){ + writeIniHydro(area = x, + params= lapply(hydro_ini[hydro_params], + `[[`, + x)) +}) + + ## write data ---- + ### TS + max power + mingen ---- + # write TS (mod file only) + max power + mingen +mod_data = matrix(60,365,5) + +# max power is only first column +study_path <- simOptions() +study_path <- study_path$inputPath +max_power_file_path <- file.path(study_path, + "hydro", + "common", + "capacity", + "maxpower_at.txt") +maxpower_data_origin <- antaresRead:::fread_antares(opts = simOptions(), + file = max_power_file_path) +maxpower_data <- rep(80,365) +maxpower_data_upgrade <- maxpower_data_origin +maxpower_data_upgrade$V1 <- maxpower_data + +# mingen data +mingen_data = matrix(2,8760,5) + +lapply(getAreas(), function(x){ + writeInputTS(area = x, type = "hydroSTOR", + data = mod_data, + overwrite = TRUE) + + writeHydroValues(area = x, + type = "maxpower", + data = maxpower_data_upgrade) + + writeInputTS(area = x, type = "mingen", + data = mingen_data, + overwrite = TRUE) +}) + + +# wind ---- + +# solar ---- + +# general data ---- + +general_data_file <- readIni("settings/generaldata") + + # /!\/!\/!\ + # click "run time series" to run simulation + # this part is for time series generated by solver for a simulation + # Input time series are generated + +# values_generate <- c("thermal, hydro", "load", "st-storage") +value_nb_year <- 2 +active_year_by_year <- "true" + +# section [general] +# updateGeneralSettings(generate = values_generate) +updateGeneralSettings(nbyears = value_nb_year, + year.by.year = active_year_by_year) + +# section [input] +# updateInputSettings(import = values_generate) + +# read generaldata +general_data_file_updated <- readIni("settings/generaldata") + +# compare files +waldo::compare(general_data_file, + general_data_file_updated) + +# scenario builder ---- + + +# delete study ---- +# deleteStudy() + + +## +# POST UPDATE ---- +## + +# edit binding values to v870 format + # provide antares study in your $HOME env +study_path <- file.path("~", name) +setSimulationPath(path = study_path) + +file.path(study_path, "input", "bindingconstraints", "bc_1_lt.txt") +path_file_bc <- file.path(study_path, "input", "bindingconstraints", "bc_1_lt.txt") +# update biding file to add time series + +bc_1 <- antaresRead:::fread_antares(file = path_file_bc, + opts = simOptions()) + +bc_up <- cbind(bc_1, as.integer(c(equal, rep(0, 24)))) + +data.table::fwrite(x = bc_up, + file = path_file_bc, + sep = "\t", + col.names = FALSE) + + # /!\/!\/!\ edit group to "group_test" in study + +# edit scenariobuilder file + +# bc,group_test,0 = 1 +# bc,group_test,1 = 2 + +# make tar.gz archive with following name + # "test_case_study_v870" From c381a69edf0d5fcf9f6632206029bceed5b5d025 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Thu, 25 Jan 2024 16:14:35 +0100 Subject: [PATCH 20/74] resolve conflicts --- DESCRIPTION | 4 +- NEWS.md | 32 +++- tests/testthat/test-writeInputTS.R | 225 ++++++++++++++++++----------- 3 files changed, 171 insertions(+), 90 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 845cb7b0..b31706c6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -15,12 +15,14 @@ Authors@R: c( person("Assil", "Mansouri", role = "ctb"), person("Abdallah", "Mahoudi", role = "ctb"), person("Clement", "Berthet", role = "ctb"), + person("Kamel", "Kemiha", role = "ctb"), + person("Nicolas", "Boitard", role = "ctb"), person("RTE", role = "cph")) Description: Edit an 'Antares' simulation before running it : create new areas, links, thermal clusters or binding constraints or edit existing ones. Update 'Antares' general & optimization settings. 'Antares' is an open source power system generator, more information available here : . License: GPL (>= 2) | file LICENSE -URL: https://github.com/rte-antares-rpackage/antaresEditObject +URL: https://github.com/rte-antares-rpackage/antaresEditObject, https://rte-antares-rpackage.github.io/antaresEditObject/ BugReports: https://github.com/rte-antares-rpackage/antaresEditObject/issues Encoding: UTF-8 RoxygenNote: 7.2.2 diff --git a/NEWS.md b/NEWS.md index 44f0c841..acf64285 100644 --- a/NEWS.md +++ b/NEWS.md @@ -7,6 +7,29 @@ * Integration of the scenario of the coupling constraints of second member +# antaresEditObject 0.6.2 + +NEW FEATURES : + +* Complete function `deleteStudy()` with new parameter `simulation` to delete a simulation in an Antares study. +* New parameter `geographic.trimming` in `updateGeneralSettings()`to activate or deactivate this general parameter. + +### Breaking changes + +* `setPlaylist()` optimized for the API mode + - returned an updated list of simulation parameters returned by the function `setSimulationPath()` and `setSimulationPathAPI()` + +# antaresEditObject 0.6.1 + + +* `writeInputTS()` allows the user to set a link with the separator ' - ' (ex: 'area1 - area2') + + +BUGFIXES : + +* Error CRAN CHECKS (fix issue #115) + + # antaresEditObject 0.6.0 ### Breaking changes (Antares v8.6, cf. Antares v8.6 changelog) : @@ -33,10 +56,10 @@ NEW FEATURES : * Add `writeIniHydro()` function to make easier the edition of the `input/hydro/hydro.ini` file * Call `writeIniHydro()` in `createArea()` and `removeArea()` * Enable edition of hydro levels in `settings/scenariobuilder.dat` by using `scenarioBuilder()` and `updateScenarioBuilder()` -* Add deduplicateScenarioBuilder() function to keep the last value if a key is duplicated in settings/scenariobuilder.dat -* Add writeIniHydro() function to make easier the edition of the input/hydro/hydro.ini file -* Call writeIniHydro() in createArea() and removeArea() -* removeArea() removes only expected files in links directory +* Add `deduplicateScenarioBuilder()` function to keep the last value if a key is duplicated in settings/scenariobuilder.dat +* Add `writeIniHydro()` function to make easier the edition of the input/hydro/hydro.ini file +* Call `writeIniHydro()` in `createArea()` and `removeArea()` +* `removeArea()` removes only expected files in links directory ### Breaking changes @@ -47,6 +70,7 @@ NEW FEATURES : * `removeClusterRES()` in API mode * `removeLink()` delete properly data for an Antares version >= 820 * `rollback_to_previous_data()` enable to rollback if original value is NULL +* `writeInputTS()` allows the user to set a link with the separator ' - ' (ex: 'area1 - area2') BUGFIXES : diff --git a/tests/testthat/test-writeInputTS.R b/tests/testthat/test-writeInputTS.R index 1fb6e314..eaae0ebb 100644 --- a/tests/testthat/test-writeInputTS.R +++ b/tests/testthat/test-writeInputTS.R @@ -96,7 +96,109 @@ sapply(studies, function(study) { }) -# v860 ---- +# >= 820 ---- +## Alphabetical order links ---- +test_that("Check if writeInputTS() writes time series link regardless alphabetical order", { + + ant_version <- "8.2.0" + st_test <- paste0("my_study_820_", paste0(sample(letters,5),collapse = "")) + suppressWarnings(opts <- createStudy(path = pathstd, study_name = st_test, antares_version = ant_version)) + area <- "aa" + area2 <- "zz" + createArea(area) + createArea(area2) + suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input")) + + createLink(from = area, to = area2, opts = opts) + suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input")) + + path_direct_link_file <- file.path(opts$inputPath, "links", area, "capacities", paste0(area2,"_direct.txt")) + path_indirect_link_file <- file.path(opts$inputPath, "links", area, "capacities", paste0(area2,"_indirect.txt")) + + dat_mat <- c(1,3,2,4) + dat_mat_inv <- c(4,2,3,1) + nb_cols <- length(dat_mat) + + # alphabetical order + mat_multi_scen <- matrix(data = rep(dat_mat, each = 8760), ncol = nb_cols) + writeInputTS(data = mat_multi_scen, link = paste0(area,"%",area2), type = "tsLink", opts = opts) + + expect_equal(antaresRead:::fread_antares(opts = opts, + file = path_direct_link_file), + as.data.table(mat_multi_scen[,seq(1, nb_cols/2)])) + + expect_equal(antaresRead:::fread_antares(opts = opts, + file = path_indirect_link_file), + as.data.table(mat_multi_scen[,seq((nb_cols/2)+1, nb_cols)])) + + # no alphabetical order + mat_multi_scen_inv <- matrix(data = rep(dat_mat_inv, each = 8760), ncol = nb_cols) + writeInputTS(data = mat_multi_scen_inv, link = paste0(area2,"%",area), type = "tsLink", opts = opts) + + expect_equal(antaresRead:::fread_antares(opts = opts, + file = path_indirect_link_file), + as.data.table(mat_multi_scen_inv[,seq(1, nb_cols/2)])) + + expect_equal(antaresRead:::fread_antares(opts = opts, + file = path_direct_link_file), + as.data.table(mat_multi_scen_inv[,seq((nb_cols/2)+1, nb_cols)])) + +}) + + +## Separator link type ---- +test_that("Check if writeInputTS() writes links time series with argument link 'area1 - area2' or 'area1%area2'", { + + ant_version <- "8.2.0" + st_test <- paste0("my_study_820_", paste0(sample(letters,5),collapse = "")) + suppressWarnings(opts <- createStudy(path = pathstd, study_name = st_test, antares_version = ant_version)) + area <- "aa" + area2 <- "zz" + createArea(area) + createArea(area2) + suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input")) + + createLink(from = area, to = area2, opts = opts) + suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input")) + + path_direct_link_file <- file.path(opts$inputPath, "links", area, "capacities", paste0(area2,"_direct.txt")) + path_indirect_link_file <- file.path(opts$inputPath, "links", area, "capacities", paste0(area2,"_indirect.txt")) + + dat_mat_sep_1 <- c(1,3,2,4) + nb_cols <- length(dat_mat_sep_1) + mat_ts_sep_1 <- matrix(data = rep(dat_mat_sep_1, each = 8760), ncol = nb_cols) + + dat_mat_sep_2 <- c(5,7,6,8) + nb_cols <- length(dat_mat_sep_2) + mat_ts_sep_2 <- matrix(data = rep(dat_mat_sep_2, each = 8760), ncol = nb_cols) + + # link separator '%' + writeInputTS(data = mat_ts_sep_1, link = paste0(area,"%",area2), type = "tsLink", opts = opts) + + expect_equal(antaresRead:::fread_antares(opts = opts, + file = path_direct_link_file), + as.data.table(mat_ts_sep_1[,seq(1, nb_cols/2)])) + + expect_equal(antaresRead:::fread_antares(opts = opts, + file = path_indirect_link_file), + as.data.table(mat_ts_sep_1[,seq((nb_cols/2)+1, nb_cols)])) + + # link separator ' - ' + writeInputTS(data = mat_ts_sep_2, link = paste0(area," - ",area2), type = "tsLink", opts = opts) + + expect_equal(antaresRead:::fread_antares(opts = opts, + file = path_direct_link_file), + as.data.table(mat_ts_sep_2[,seq(1, nb_cols/2)])) + + expect_equal(antaresRead:::fread_antares(opts = opts, + file = path_indirect_link_file), + as.data.table(mat_ts_sep_2[,seq((nb_cols/2)+1, nb_cols)])) + +}) + + + +# >= v860 ---- ## write mingen file ---- # write mingen file depend of dimension of mod.txt file @@ -111,8 +213,8 @@ test_that("create mingen file with one or empty column dimension of mod.txt file M_mingen = matrix(0,8760,5) # [management rules] for mingen data : - # file mod.txt (in /series) have to be same column dimension - # or column dimension of 1 or NULL (empty file) + # file mod.txt (in /series) have to be same column dimension + # or column dimension of 1 or NULL (empty file) opts <- simOptions() # check dimensions of mod.txt for every areas @@ -193,8 +295,8 @@ test_that("create mingen file with multi dimension mod.txt file", { # [management rules] for mingen data : - # file mod.txt (in /series) have to be same column dimension - # or column dimension of 1 or NULL (empty file) + # file mod.txt (in /series) have to be same column dimension + # or column dimension of 1 or NULL (empty file) # check dimensions of mod.txt for every areas path_file_mod <- file.path(opts$inputPath, "hydro", "series", @@ -213,7 +315,7 @@ test_that("create mingen file with multi dimension mod.txt file", { ## multi columns cas for mod.txt file - # mod.txt column dimension >= 1 + # mod.txt column dimension >= 1 area_mult <- getAreas()[list_dim>1][1] # rewrite with less columns @@ -225,10 +327,10 @@ test_that("create mingen file with multi dimension mod.txt file", { overwrite = TRUE) # write for an area with file mod.txt >1 columns - # error case cause mod.txt dimension + # error case cause mod.txt dimension testthat::expect_error(writeInputTS(area = area_mult, type = "mingen", - data = M_mingen , overwrite = TRUE, opts = opts), - regexp = 'mingen \'data\' must be either a 8760\\*1 or 8760\\*4 matrix.') + data = M_mingen , overwrite = TRUE, opts = opts), + regexp = 'mingen \'data\' must be either a 8760\\*1 or 8760\\*4 matrix.') # you can write only mingen file with dimension 1 writeInputTS(area = area_mult, type = "mingen", @@ -263,9 +365,34 @@ test_that("create mingen file with multi dimension mod.txt file", { }) +## Rollback to empty file ---- +test_that("writeInputTS() in 8.6.0 : rollback to an empty file", { -test_that("writeInputTS() in 8.6.0 : check if there is an error when control is enabled and data is inconsistent between mingen.txt and mod.txt", { + 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) + opts <- setSimulationPath(opts$studyPath, simulation = "input") + + path_mingen <- file.path(opts$inputPath, "hydro", "series", area, "mingen.txt") + mat_mingen <- matrix(6,8760,5) + expect_error(writeInputTS(area = area, + data = mat_mingen, + type = "mingen", + opts = opts + ) + ,regexp = "can not be updated" + ) + expect_true(file.size(path_mingen) == 0) + + unlink(x = opts$studyPath, recursive = TRUE) +}) + +## Error mingen.txt vs mod.txt ---- +test_that("writeInputTS() in 8.6.0 : check if there is an error when control is enabled and data is inconsistent between mingen.txt and mod.txt", { + 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)) @@ -303,7 +430,7 @@ test_that("writeInputTS() in 8.6.0 : check if there is an error when control is data = mat_mingen_false, type = "mingen", opts = opts - ) + ) ,regexp = "can not be updated" ) # ref mingen @@ -363,6 +490,7 @@ test_that("writeInputTS() in 8.6.0 : check if there is an error when control is }) +## Success mingen.txt vs mod.txt ---- test_that("writeInputTS() in 8.6.0 : check if new data is written when control is enabled and data is consistent between mingen.txt and mod.txt", { ant_version <- "8.6.0" @@ -447,6 +575,7 @@ test_that("writeInputTS() in 8.6.0 : check if new data is written when control i }) +## Success when disabled control ---- test_that("writeInputTS() in 8.6.0 : check if new data is written when control is disabled", { ant_version <- "8.6.0" @@ -513,77 +642,3 @@ test_that("writeInputTS() in 8.6.0 : check if new data is written when control i }) -test_that("Check if writeInputTS() writes time series link regardless alphabetical order", { - - ant_version <- "8.2.0" - st_test <- paste0("my_study_820_", paste0(sample(letters,5),collapse = "")) - suppressWarnings(opts <- createStudy(path = pathstd, study_name = st_test, antares_version = ant_version)) - area <- "aa" - area2 <- "zz" - createArea(area) - createArea(area2) - suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input")) - - createLink(from = area, to = area2, opts = opts) - suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input")) - - path_direct_link_file <- file.path(opts$inputPath, "links", area, "capacities", paste0(area2,"_direct.txt")) - path_indirect_link_file <- file.path(opts$inputPath, "links", area, "capacities", paste0(area2,"_indirect.txt")) - - dat_mat <- c(1,3,2,4) - dat_mat_inv <- c(4,2,3,1) - nb_cols <- length(dat_mat) - - ## Future developments will come because this is not the expected behaviour - ## Time series direct and indirect have to be reordered - # alphabetical order - mat_multi_scen <- matrix(data = rep(dat_mat, each = 8760), ncol = nb_cols) - writeInputTS(data = mat_multi_scen, link = paste0(area,"%",area2), type = "tsLink", opts = opts) - - expect_equal(antaresRead:::fread_antares(opts = opts, - file = path_direct_link_file), - as.data.table(mat_multi_scen[,seq(1, nb_cols/2)])) - - expect_equal(antaresRead:::fread_antares(opts = opts, - file = path_indirect_link_file), - as.data.table(mat_multi_scen[,seq((nb_cols/2)+1, nb_cols)])) - - # no alphabetical order - mat_multi_scen_inv <- matrix(data = rep(dat_mat_inv, each = 8760), ncol = nb_cols) - writeInputTS(data = mat_multi_scen_inv, link = paste0(area2,"%",area), type = "tsLink", opts = opts) - - expect_equal(antaresRead:::fread_antares(opts = opts, - file = path_indirect_link_file), - as.data.table(mat_multi_scen_inv[,seq(1, nb_cols/2)])) - - expect_equal(antaresRead:::fread_antares(opts = opts, - file = path_direct_link_file), - as.data.table(mat_multi_scen_inv[,seq((nb_cols/2)+1, nb_cols)])) - -}) - - -test_that("writeInputTS() in 8.6.0 : rollback to an empty file", { - - 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) - opts <- setSimulationPath(opts$studyPath, simulation = "input") - - path_mingen <- file.path(opts$inputPath, "hydro", "series", area, "mingen.txt") - mat_mingen <- matrix(6,8760,5) - expect_error(writeInputTS(area = area, - data = mat_mingen, - type = "mingen", - opts = opts - ) - ,regexp = "can not be updated" - ) - expect_true(file.size(path_mingen) == 0) - - unlink(x = opts$studyPath, recursive = TRUE) -}) - - From 87991342e08c572978a38bbff68d4eceb158627e Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Thu, 25 Jan 2024 16:15:22 +0100 Subject: [PATCH 21/74] pull from master + doc --- R/backupStudy.R | 2 +- R/createCluster.R | 8 +- R/createClusterBulk.R | 2 +- R/createStudy.R | 71 ++++++--- R/editCluster.R | 2 +- R/playlist.R | 41 ++++- R/updateGeneralSettings.R | 10 +- R/updateOutputSettings.R | 2 +- R/writeHydroValues.R | 50 +++--- R/writeInputTS.R | 16 +- cran-comments.md | 5 +- docs/404.html | 2 +- docs/LICENSE-text.html | 2 +- docs/articles/Antares_new_features_v860.html | 22 +-- docs/articles/antaresEditObject.html | 146 +++++++++++++++++- docs/articles/api-variant-management.html | 10 +- docs/articles/index.html | 2 +- docs/articles/renewables-energy-sources.html | 8 +- docs/articles/scenario-builder.html | 26 ++-- docs/authors.html | 19 ++- docs/index.html | 2 +- docs/news/index.html | 38 ++++- docs/pkgdown.yml | 2 +- docs/reference/activateRES.html | 8 +- docs/reference/activateST.html | 8 +- .../add_week_number_column_to_ts.html | 2 +- docs/reference/adequacyOptions.html | 2 +- docs/reference/antaresRead-reexports.html | 4 +- docs/reference/backupStudy.html | 8 +- docs/reference/check-version.html | 8 +- docs/reference/checkRemovedArea.html | 6 +- .../check_consistency_reservoir_values.html | 2 +- .../check_mingen_vs_hydro_storage.html | 6 +- docs/reference/check_mingen_vs_maxpower.html | 6 +- docs/reference/cleanUpOutput.html | 4 +- .../computeOtherFromHourlyMulti.html | 6 +- .../reference/computeOtherFromHourlyYear.html | 4 +- .../reference/computeTimeStampFromHourly.html | 4 +- docs/reference/convertConfigToAdq.html | 4 +- docs/reference/copyOutput.html | 6 +- docs/reference/copyStudyWeb.html | 6 +- docs/reference/create-binding-constraint.html | 8 +- docs/reference/create-study.html | 4 +- docs/reference/createArea.html | 8 +- docs/reference/createCluster.html | 115 +++++++++++++- docs/reference/createClusterBulk.html | 8 +- docs/reference/createClusterST.html | 10 +- docs/reference/createDSR.html | 14 +- docs/reference/createDistrict.html | 6 +- docs/reference/createLink.html | 8 +- docs/reference/createPSP.html | 14 +- .../create_referential_series_type.html | 111 +++++++++++++ docs/reference/deleteStudy.html | 14 +- docs/reference/dicoGeneralSettings.html | 2 +- docs/reference/dicoOptimizationSettings.html | 2 +- docs/reference/dot-dailyToWeekly.html | 4 +- docs/reference/dot-hourlyToOther.html | 2 +- docs/reference/editArea.html | 14 +- docs/reference/editBindingConstraint.html | 6 +- docs/reference/editCluster.html | 8 +- docs/reference/editClusterST.html | 6 +- docs/reference/editLink.html | 6 +- docs/reference/fill_empty_hydro_ini_file.html | 6 +- docs/reference/fill_empty_hydro_ts_file.html | 6 +- docs/reference/filteringOptions.html | 2 +- docs/reference/getJobLogs.html | 8 +- docs/reference/getJobs.html | 6 +- .../get_default_hydro_ini_values.html | 2 +- ...get_type_check_mingen_vs_hydrostorage.html | 2 +- ...eck_mingen_vs_hydrostorage_to_trigger.html | 6 +- ...e_check_mingen_vs_maxpower_to_trigger.html | 6 +- docs/reference/index.html | 8 +- docs/reference/list_pollutants_values.html | 2 +- docs/reference/mockSimulationAPI.html | 4 +- docs/reference/nodalOptimizationOptions.html | 2 +- docs/reference/playlist.html | 12 +- docs/reference/propertiesLinkOptions.html | 2 +- docs/reference/removeArea.html | 6 +- docs/reference/removeBindingConstraint.html | 6 +- docs/reference/removeCluster.html | 10 +- docs/reference/removeLink.html | 6 +- docs/reference/replicate_missing_ts.html | 2 +- docs/reference/rollback_to_previous_data.html | 6 +- docs/reference/runSimulation.html | 6 +- docs/reference/runTsGenerator.html | 8 +- docs/reference/scenario-builder.html | 41 +++-- docs/reference/searchStudy.html | 2 +- docs/reference/setAPImode.html | 6 +- docs/reference/setSolverPath.html | 2 +- docs/reference/storage_values_default.html | 2 +- docs/reference/updateAdequacySettings.html | 6 +- docs/reference/updateGeneralSettings.html | 11 +- docs/reference/updateInputSettings.html | 6 +- .../reference/updateOptimizationSettings.html | 6 +- docs/reference/updateOutputSettings.html | 19 ++- docs/reference/variant-commands.html | 8 +- docs/reference/variant.html | 8 +- docs/reference/write-ini.html | 6 +- docs/reference/writeEconomicOptions.html | 8 +- docs/reference/writeHydroValues.html | 16 +- docs/reference/writeIniHydro.html | 38 ++--- docs/reference/writeInputTS.html | 8 +- docs/reference/writeMiscGen.html | 6 +- docs/reference/writeOutputValues.html | 8 +- docs/reference/writeSeriesPrepro.html | 6 +- docs/reference/writeWaterValues.html | 6 +- docs/sitemap.xml | 3 + man/backupStudy.Rd | 2 +- man/createCluster.Rd | 6 +- man/deleteStudy.Rd | 8 +- ...check_mingen_vs_hydrostorage_to_trigger.Rd | 4 +- ...ype_check_mingen_vs_maxpower_to_trigger.Rd | 4 +- man/updateGeneralSettings.Rd | 3 + man/updateOutputSettings.Rd | 2 +- man/writeHydroValues.Rd | 12 +- man/writeIniHydro.Rd | 30 ++-- man/writeInputTS.Rd | 2 +- tests/testthat/test-createArea.R | 1 + tests/testthat/test-playlist.R | 34 ++++ tests/testthat/test-updateGeneralSettings.R | 7 +- vignettes/antaresEditObject.Rmd | 137 ++++++++++++++++ 121 files changed, 1115 insertions(+), 435 deletions(-) create mode 100644 docs/reference/create_referential_series_type.html diff --git a/R/backupStudy.R b/R/backupStudy.R index d4c23b55..dbb17cc0 100644 --- a/R/backupStudy.R +++ b/R/backupStudy.R @@ -12,7 +12,7 @@ #' @param opts #' List of simulation parameters returned by the function #' \code{antaresRead::setSimulationPath} -#' @param extension Defaut is {.zip}. +#' @param extension Defaut is \code{.zip}. #' #' @return The path of the backup #' @export diff --git a/R/createCluster.R b/R/createCluster.R index 6deb3998..3f043bee 100644 --- a/R/createCluster.R +++ b/R/createCluster.R @@ -117,7 +117,7 @@ #' area = "fr", #' cluster_name = "my_cluster", #' prepro_data = data.frame( -#' v1 = rep(7, 365), # column name doesn't matter +#' v1 = rep(7, 365), # column name does not matter #' v2 = rep(27, 365), #' v3 = rep(0.05, 365), #' v4 = rep(0.12, 365), @@ -132,7 +132,7 @@ #' createCluster( #' area = "fr", #' cluster_name = "my_cluster", -#' prepro_modulation = = matrix( +#' prepro_modulation = matrix( #' data = c(rep(1, times = 365 * 24 * 3), #' rep(0, times = 365 * 24 * 1)), #' ncol = 4 @@ -144,7 +144,7 @@ #' area = "fr", #' cluster_name = "my_cluster", #' prepro_modulation = data.frame( -#' var1 = rep(0, 8760), # column name doesn't matter +#' var1 = rep(0, 8760), # column name does not matter #' var2 = rep(1, 8760), #' var3 = rep(0, 8760), #' var4 = rep(1, 8760) @@ -290,7 +290,7 @@ createClusterRES <- function(area, if (!NROW(prepro_modulation) %in% c(0, 8736, 8760)) { stop("Number of rows for modulation data must be 0 or 8760") } - if (!NCOL(prepro_modulation) %in% c(1, 4)) { + if (!NCOL(prepro_modulation) %in% c(0, 1, 4)) { # issue 115 NCOL NULL return stop("Number of cols for modulation data must be 0 or 4") } diff --git a/R/createClusterBulk.R b/R/createClusterBulk.R index 8d8e4f71..fc6113e5 100644 --- a/R/createClusterBulk.R +++ b/R/createClusterBulk.R @@ -177,7 +177,7 @@ createClusterBulk <- function(cluster_object, if (!NROW(list_params$prepro_modulation) %in% c(0, 8736, 8760)) { stop("Number of rows for modulation data must be 0 or 8760") } - if (!NCOL(list_params$prepro_modulation) %in% c(1, 4)) { + if (!NCOL(list_params$prepro_modulation) %in% c(0, 1, 4)) { # issue 115 NCOL NULL return stop("Number of cols for modulation data must be 0 or 4") } diff --git a/R/createStudy.R b/R/createStudy.R index d9dc4a6b..74a86a0f 100644 --- a/R/createStudy.R +++ b/R/createStudy.R @@ -112,32 +112,59 @@ createStudyAPI <- function(host, token = NULL, study_name = "my_study", antares_ invisible(opts) } - -#' @title Delete a study +#' @title Delete a study or a simulation #' #' @param opts List. study options #' @param prompt_validation `logical` to put validation message to delete study (default `FALSE`) -#' +#' @param simulation simulation to be deleted (default `NULL`) +#' @importFrom antaresRead api_delete #' @export -deleteStudy <- function(opts = simOptions(), prompt_validation= FALSE){ +deleteStudy <- function(opts = simOptions(), prompt_validation = FALSE, simulation = NULL){ + + delete_simulation <- !is.null(simulation) + is_api_study <- is_api_study(opts) + + if(!is_api_study && !file.exists(opts$studyPath)) + stop("Study not found.") + if(prompt_validation){ - prompt_question <- sprintf("Are you sure you want to delete the study : %s (%s)?", - ifelse(opts$typeLoad == "api", - opts$study_id, - opts$studyPath), - opts$studyName) - prompt_answer <- menu(c("Yes", "No"), - title=prompt_question) - if (prompt_answer == 2) - return() + if(delete_simulation){ + prompt_question <- sprintf("Are you sure you want to delete the simulation : %s ?", + simulation) + } else { + prompt_question <- sprintf("Are you sure you want to delete the study : %s (%s)?", + ifelse(opts$typeLoad == "api", + opts$study_id, + opts$studyPath), + opts$studyName) + } + + prompt_answer <- menu(c("Yes", "No"),title=prompt_question) + if(prompt_answer == 2) + return(NULL) } - if(opts$typeLoad == "api") - api_delete(opts = opts, endpoint = opts$study_id) - else{ - if(file.exists(opts$studyPath)) - unlink(opts$studyPath, recursive = TRUE) - else - stop("Study not found.") - } - cat("\nStudy successfully deleted") + + if(is_api_study){ + if(delete_simulation){ + study_path <- gsub(pattern = "\\/raw\\?path=", + replacement = "", + x = opts$studyPath) + url <- I(file.path(study_path,"outputs",simulation)) + } else { + url <- opts$study_id + } + api_delete(opts = opts, endpoint = url) + + } else { + path <- opts$studyPath + if(delete_simulation) + path <- file.path(path,"output",simulation) + unlink(path, recursive = TRUE) + } + + cat(sprintf("\n%s successfully deleted", + ifelse(delete_simulation, + "Simulation", + "Study"))) } + diff --git a/R/editCluster.R b/R/editCluster.R index a287bcef..94c96fbe 100644 --- a/R/editCluster.R +++ b/R/editCluster.R @@ -140,7 +140,7 @@ editClusterRES <- function(area, if (!NROW(prepro_modulation) %in% c(0, 8736, 8760)) { stop("Number of rows for modulation data must be 0 or 8760") } - if (!NCOL(prepro_modulation) %in% c(1, 4)) { + if (!NCOL(prepro_modulation) %in% c(0, 1, 4)) {# issue 115 NCOL NULL return stop("Number of cols for modulation data must be 0 or 4") } diff --git a/R/playlist.R b/R/playlist.R index c7283b12..f58d6489 100644 --- a/R/playlist.R +++ b/R/playlist.R @@ -151,6 +151,8 @@ setPlaylist <- function(playlist, weights = NULL, opts = antaresRead::simOptions()) { + api_study <- is_api_study(opts) + version_study <- substr(opts$antaresVersion, 1, 1) version_study <- as.numeric(version_study) @@ -181,9 +183,11 @@ setPlaylist <- function(playlist, if (length(playlist) == length(mc_years)) { # update line to disable the playlist generaldata$general$`user-playlist` <- FALSE - # write updated file - writeIni(listData = generaldata, pathIni = "settings/generaldata", overwrite = TRUE, opts = opts) - + if (api_study) { + # To minimize the number of queries, we reduce the list to the updated items + generaldata <- generaldata[which(names(generaldata) == "general")] + generaldata$general[which(names(generaldata$general) != "user-playlist")] <- NULL + } } else { # otherwise, set the playlist # update line to enable the playlist generaldata$general$`user-playlist` <- TRUE @@ -193,7 +197,7 @@ setPlaylist <- function(playlist, # create new playlist (+ patch double to integer) new_playlist <- setNames(as.list(sort(as.integer(playlist - 1))), rep("playlist_year +", length(playlist))) - if (opts$typeLoad == "api"){ + if (api_study){ new_playlist$sep <- ", " new_playlist <- list("playlist_year +" = paste0("[", do.call(paste, new_playlist), "]")) } @@ -215,11 +219,32 @@ setPlaylist <- function(playlist, ), rep("playlist_year_weight", length(weights))) ) } - + if (api_study) { + # To minimize the number of queries, we reduce the list to the updated items + generaldata <- generaldata[which(names(generaldata) == "general")] + generaldata$general[which(names(generaldata$general) != "user-playlist")] <- NULL + } # add new playlist to the parameters description generaldata$playlist <- new_playlist - - # write updated file - writeIni(listData = generaldata, pathIni = "settings/generaldata", overwrite = TRUE, opts = opts) } + + # write updated file + writeIni(listData = generaldata, pathIni = "settings/generaldata", overwrite = TRUE, opts = opts) + + # Update simulation options object + if(api_study){ + suppressWarnings( + res <- antaresRead::setSimulationPathAPI(host = opts$host, + study_id = opts$study_id, + token = opts$token, + simulation = "input") + ) + }else{ + suppressWarnings( + res <- antaresRead::setSimulationPath(path = opts$studyPath, + simulation = "input") + ) + } + + return(invisible(res)) } diff --git a/R/updateGeneralSettings.R b/R/updateGeneralSettings.R index 5503a024..9d59e2e0 100644 --- a/R/updateGeneralSettings.R +++ b/R/updateGeneralSettings.R @@ -38,7 +38,7 @@ #' @param refreshintervalthermal See Antares General Reference Guide. #' @param refreshintervalsolar See Antares General Reference Guide. #' @param readonly See Antares General Reference Guide. -#' +#' @param geographic.trimming \code{logical} indicates whether to store the results for all time spans (FALSE) or for custom time spans (TRUE) #' @template opts #' #' @export @@ -88,6 +88,7 @@ updateGeneralSettings <- function(mode = NULL, refreshintervalthermal = NULL, refreshintervalsolar = NULL, readonly = NULL, + geographic.trimming = NULL, opts = antaresRead::simOptions()) { assertthat::assert_that(inherits(opts, "simOptions")) @@ -132,7 +133,8 @@ updateGeneralSettings <- function(mode = NULL, refreshintervalwind = refreshintervalwind, refreshintervalthermal = refreshintervalthermal, refreshintervalsolar = refreshintervalsolar, - readonly = readonly + readonly = readonly, + geographic.trimming = geographic.trimming ) new_params <- dropNulls(new_params) for (i in seq_along(new_params)) { @@ -252,7 +254,7 @@ dicoGeneralSettings <- function(arg) { "nbtimeserieshydro", "nbtimeserieswind", "nbtimeseriesthermal", "nbtimeseriessolar", "refreshtimeseries", "intra-modal", "inter-modal", "refreshintervalload", "refreshintervalhydro", "refreshintervalwind", - "refreshintervalthermal", "refreshintervalsolar", "readonly") + "refreshintervalthermal", "refreshintervalsolar", "readonly", "geographic-trimming") ) names(antares_params) <- c("mode", "horizon", "nbyears", "simulation.start", "simulation.end", "january.1st", "first.month.in.year", "first.weekday", "leapyear", @@ -261,7 +263,7 @@ dicoGeneralSettings <- function(arg) { "nbtimeserieshydro", "nbtimeserieswind", "nbtimeseriesthermal", "nbtimeseriessolar", "refreshtimeseries", "intra.modal", "inter.modal", "refreshintervalload", "refreshintervalhydro", "refreshintervalwind", - "refreshintervalthermal", "refreshintervalsolar", "readonly") + "refreshintervalthermal", "refreshintervalsolar", "readonly", "geographic.trimming") antares_params[[arg]] } diff --git a/R/updateOutputSettings.R b/R/updateOutputSettings.R index ec4acd39..98538edf 100644 --- a/R/updateOutputSettings.R +++ b/R/updateOutputSettings.R @@ -25,7 +25,7 @@ #' updateOutputSettings( #' synthesis = TRUE, #' storenewset = FALSE, -#' archives = c("load", "wind") +#' archives = c("load", "wind"), #' result.format = "zip" #' ) #' diff --git a/R/writeHydroValues.R b/R/writeHydroValues.R index 6d24678f..297e8e64 100644 --- a/R/writeHydroValues.R +++ b/R/writeHydroValues.R @@ -9,16 +9,16 @@ #' @param type Type of hydro file, it can be "waterValues", "reservoir", "maxpower", "inflowPattern" or "creditmodulations". #' @param data The data must have specific dimension depending on the type of file : #' \itemize{ -#' \item{waterValues}{: a 365x101 numeric matrix: +#' \item \code{waterValues}: a 365x101 numeric matrix: #' marginal values for the stored energy based on date (365 days) #' and on the reservoir level (101 round percentage values ranging from #' 0% to 100%). OR a 3-column matrix with 365x101 rows. In this latter case the 3 columns must #' be 'date', 'level' and 'value' (in this order), and the rows must be sorted by: -#' ascending day, ascending level.} -#' \item{reservoir}{: a 365x3 numeric matrix. The columns contains respectively the levels min, avg and max.} -#' \item{maxpower}{: a 365x4 numeric matrix.} -#' \item{inflowPattern}{: a 365x1 numeric matrix.} -#' \item{creditmodulations}{: a 2x101 numeric matrix.} +#' ascending day, ascending level. +#' \item \code{reservoir}: a 365x3 numeric matrix. The columns contains respectively the levels min, avg and max. +#' \item \code{maxpower}: a 365x4 numeric matrix. +#' \item \code{inflowPattern}: a 365x1 numeric matrix. +#' \item \code{creditmodulations}: a 2x101 numeric matrix. #' } #' #' @param overwrite Logical. Overwrite the values if a file already exists. @@ -234,21 +234,21 @@ check_consistency_reservoir_values <- function(area, new_data, prev_data){ #' @param area The area where to edit the values. #' @param params The list data must have specific names and specific types : #' \itemize{ -#' \item{follow load}{ : logical or NULL} -#' \item{use heuristic}{ : logical or NULL} -#' \item{use water}{ : logical or NULL} -#' \item{hard bounds}{ : logical or NULL} -#' \item{use leeway}{ : logical or NULL} -#' \item{power to level}{ : logical or NULL} -#' \item{reservoir}{ : logical or NULL} -#' \item{inter-daily-breakdown}{ : numeric, integer or NULL} -#' \item{intra-daily-modulation}{ : numeric, integer or NULL} -#' \item{inter-monthly-breakdown}{ : numeric, integer or NULL} -#' \item{leeway low}{ : numeric, integer or NULL} -#' \item{leeway up}{ : numeric, integer or NULL} -#' \item{pumping efficiency}{ : numeric, integer or NULL} -#' \item{initialize reservoir date}{ : numeric, integer or NULL} -#' \item{reservoir capacity}{ : numeric, integer or NULL} +#' \item \code{follow load} : logical or NULL +#' \item \code{use heuristic} : logical or NULL +#' \item \code{use water} : logical or NULL +#' \item \code{hard bounds} : logical or NULL +#' \item \code{use leeway} : logical or NULL +#' \item \code{power to level} : logical or NULL +#' \item \code{reservoir} : logical or NULL +#' \item \code{inter-daily-breakdown} : numeric, integer or NULL +#' \item \code{intra-daily-modulation} : numeric, integer or NULL +#' \item \code{inter-monthly-breakdown} : numeric, integer or NULL +#' \item \code{leeway low} : numeric, integer or NULL +#' \item \code{leeway up} : numeric, integer or NULL +#' \item \code{pumping efficiency} : numeric, integer or NULL +#' \item \code{initialize reservoir date} : numeric, integer or NULL +#' \item \code{reservoir capacity} : numeric, integer or NULL #' } #' @param mode Execution mode. Useful when you create a new area or remove an existing area to avoid control on hydro data. #' @param opts List of simulation parameters returned by the function @@ -557,8 +557,8 @@ get_type_check_mingen_vs_hydrostorage <- function(hydro_params){ #' #' Compute the type of control to make between : #' \itemize{ -#' \item{`input/hydro/series//mingen.txt`} -#' \item{`input/hydro/series//mod.txt`} +#' \item \code{input/hydro/series//mingen.txt} +#' \item \code{input/hydro/series//mod.txt} #' } #' This control is implemented in Antares too. #' @@ -728,8 +728,8 @@ check_mingen_vs_hydro_storage <- function(area, opts = antaresRead::simOptions() #' #' Compute the type of control to make between : #' \itemize{ -#' \item{`input/hydro/series//mingen.txt`} -#' \item{`input/hydro/common/capacity/maxpower_.txt`} +#' \item \code{input/hydro/series//mingen.txt} +#' \item \code{input/hydro/common/capacity/maxpower_.txt} #' } #' This control is implemented in Antares too. #' No control to execute if `reservoir` section in hydro.ini for the area is set to TRUE. diff --git a/R/writeInputTS.R b/R/writeInputTS.R index 4e1d436d..6276f20c 100644 --- a/R/writeInputTS.R +++ b/R/writeInputTS.R @@ -19,7 +19,7 @@ #' #' @param area The area where to write the input time series. #' @param link Link for which writing transmission capacities time series, -#' must like `"area01%area02"` or `c("area01", "area02")`. +#' must like `"area01%area02"` or `"area01 - area02"` or `c("area01", "area02")`. #' @param overwrite Logical. Overwrite the values if a file already exists. #' #' @@ -163,13 +163,19 @@ writeInputTS <- function(data, # tsLink block (file & API) if (!is.null(link)) { stopifnot( - "link must be a character, like 'area01%area02' or c('area01', 'area02')" = is.character(link) + "link must be a character, like 'area01%area02' or 'area01 - area02' or c('area01', 'area02')" = is.character(link) ) - if (length(link) == 1) - link <- strsplit(x = link, split = "%")[[1]] + if (length(link) == 1) { + sep_hyphen <- " - " + sep_expected <- "%" + if (grepl(pattern = sep_hyphen, x = link)) { + link <- gsub(pattern = sep_hyphen, replacement = sep_expected, x = link) + } + link <- strsplit(x = link, split = sep_expected)[[1]] + } stopifnot( - "Invalid link specification, must be 'area01%area02' or c('area01', 'area02')" = length(link) == 2 + "Invalid link specification, must be 'area01%area02' or 'area01 - area02' or c('area01', 'area02')" = length(link) == 2 ) from <- tolower(as.character(link[1])) diff --git a/cran-comments.md b/cran-comments.md index be08d72b..bea84ab0 100644 --- a/cran-comments.md +++ b/cran-comments.md @@ -12,4 +12,7 @@ Thanks! ## Fix CRAN NOTE for release 0.6.0 -"Running R code in ‘testthat.R’ had CPU time 8.1 times elapsed time" \ No newline at end of file +"Running R code in ‘testthat.R’ had CPU time 8.1 times elapsed time" + +## Fix CRAN CHECKS on version 0.6.0 +* Fix ERROR on `r-devel-linux-x86_64-debian-gcc` cause function `base::NCOL()` is updated diff --git a/docs/404.html b/docs/404.html index f9125c81..4ef7f603 100644 --- a/docs/404.html +++ b/docs/404.html @@ -32,7 +32,7 @@ antaresEditObject - 0.6.0 + 0.6.2 diff --git a/docs/LICENSE-text.html b/docs/LICENSE-text.html index 23e48566..36a9f797 100644 --- a/docs/LICENSE-text.html +++ b/docs/LICENSE-text.html @@ -17,7 +17,7 @@ antaresEditObject - 0.6.0 + 0.6.2 diff --git a/docs/articles/Antares_new_features_v860.html b/docs/articles/Antares_new_features_v860.html index 13be0475..2ba9031d 100644 --- a/docs/articles/Antares_new_features_v860.html +++ b/docs/articles/Antares_new_features_v860.html @@ -33,7 +33,7 @@ antaresEditObject - 0.6.0 + 0.6.2 @@ -102,7 +102,9 @@

Antares new features v8.6.0

-library(antaresEditObject)
+ # CRAN limite CPU usage
+data.table::setDTthreads(2)
+library(antaresEditObject)
 #> Loading required package: antaresRead
 #> 
 #> Attaching package: 'antaresEditObject'
@@ -184,16 +186,16 @@ 

Create “st-storage”#> x being coerced from class: matrix to data.table

Now you can see informations in simulation options.

-opts <- simOptions()
+opts <- simOptions()
 opts$areasWithSTClusters
 #> [1] "fr" "it"

Read st-storages parameters

-

After creating “st-storage” clusters, you can read all information with specific function readClusterSTDesc().

+

After creating “st-storage” clusters, you can read all information with specific function readClusterSTDesc().

-tab <- readClusterSTDesc()
+tab <- readClusterSTDesc()
 rmarkdown::paged_table(tab)
+ + +
+
+ + + +
+
+ + +
+

Create the correspondence data frame between the symbol and the type in scenario builder

+
+ +
+
create_referential_series_type()
+
+ +
+

Value

+ + +

a data.frame.

+
+ +
+ +
+ + +
+ +
+

Site built with pkgdown 2.0.6.

+
+ +
+ + + + + + + + diff --git a/docs/reference/deleteStudy.html b/docs/reference/deleteStudy.html index 5de40c86..f9f6af87 100644 --- a/docs/reference/deleteStudy.html +++ b/docs/reference/deleteStudy.html @@ -1,5 +1,5 @@ -Delete a study — deleteStudy • antaresEditObjectDelete a study or a simulation — deleteStudy • antaresEditObject @@ -17,7 +17,7 @@ antaresEditObject - 0.6.0 + 0.6.2
@@ -65,17 +65,17 @@
-

Delete a study

+

Delete a study or a simulation

-
deleteStudy(opts = simOptions(), prompt_validation = FALSE)
+
deleteStudy(opts = simOptions(), prompt_validation = FALSE, simulation = NULL)
@@ -87,6 +87,10 @@

Arguments

prompt_validation

logical to put validation message to delete study (default FALSE)

+ +
simulation
+

simulation to be deleted (default NULL)

+
diff --git a/docs/reference/dicoGeneralSettings.html b/docs/reference/dicoGeneralSettings.html index ac32c8da..14507e2b 100644 --- a/docs/reference/dicoGeneralSettings.html +++ b/docs/reference/dicoGeneralSettings.html @@ -17,7 +17,7 @@ antaresEditObject - 0.6.0 + 0.6.2
diff --git a/docs/reference/dicoOptimizationSettings.html b/docs/reference/dicoOptimizationSettings.html index 962d22ac..503a97f4 100644 --- a/docs/reference/dicoOptimizationSettings.html +++ b/docs/reference/dicoOptimizationSettings.html @@ -17,7 +17,7 @@ antaresEditObject - 0.6.0 + 0.6.2
diff --git a/docs/reference/dot-dailyToWeekly.html b/docs/reference/dot-dailyToWeekly.html index 572331dc..396eddad 100644 --- a/docs/reference/dot-dailyToWeekly.html +++ b/docs/reference/dot-dailyToWeekly.html @@ -17,7 +17,7 @@ antaresEditObject - 0.6.0 + 0.6.2 @@ -75,7 +75,7 @@

Computation function for rebuild mc-ind weekly from daily data.

-
.dailyToWeekly(dailydata, opts = simOptions(), type)
+
.dailyToWeekly(dailydata, opts = simOptions(), type)
diff --git a/docs/reference/dot-hourlyToOther.html b/docs/reference/dot-hourlyToOther.html index 102dc707..bd046369 100644 --- a/docs/reference/dot-hourlyToOther.html +++ b/docs/reference/dot-hourlyToOther.html @@ -17,7 +17,7 @@ antaresEditObject - 0.6.0 + 0.6.2
diff --git a/docs/reference/editArea.html b/docs/reference/editArea.html index 6b19e132..4ae647d9 100644 --- a/docs/reference/editArea.html +++ b/docs/reference/editArea.html @@ -18,7 +18,7 @@ antaresEditObject - 0.6.0 + 0.6.2 @@ -84,7 +84,7 @@

Edit an area in an Antares study

nodalOptimization = NULL, filtering = NULL, adequacy = NULL, - opts = antaresRead::simOptions() + opts = antaresRead::simOptions() )
@@ -116,7 +116,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath()

+antaresRead::setSimulationPath()

@@ -137,18 +137,18 @@

Examples

library(antaresRead) # Set simulation path -setSimulationPath(path = "PATH/TO/SIMULATION", simulation = "input") +setSimulationPath(path = "PATH/TO/SIMULATION", simulation = "input") # Edit an existing area editArea("area", color = grDevices::rgb(230, 108, 44, max = 255), localization = c(1, 1), - opts = antaresRead::simOptions()) + opts = antaresRead::simOptions()) editArea("de", nodalOptimization = list("spilledenergycost" = list(fr = 30)), -opts = antaresRead::simOptions()) +opts = antaresRead::simOptions()) editArea("de", nodalOptimization = nodalOptimizationOptions(), -opts = antaresRead::simOptions()) +opts = antaresRead::simOptions()) }
diff --git a/docs/reference/editBindingConstraint.html b/docs/reference/editBindingConstraint.html index d6ea3446..d96d181f 100644 --- a/docs/reference/editBindingConstraint.html +++ b/docs/reference/editBindingConstraint.html @@ -18,7 +18,7 @@ antaresEditObject - 0.6.0 + 0.6.2 @@ -87,7 +87,7 @@

Update an existing binding constraint

filter_year_by_year = NULL, filter_synthesis = NULL, coefficients = NULL, - opts = antaresRead::simOptions() + opts = antaresRead::simOptions() )
@@ -132,7 +132,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath()

+antaresRead::setSimulationPath()

diff --git a/docs/reference/editCluster.html b/docs/reference/editCluster.html index 74260602..f03888ee 100644 --- a/docs/reference/editCluster.html +++ b/docs/reference/editCluster.html @@ -18,7 +18,7 @@ antaresEditObject - 0.6.0 + 0.6.2
@@ -86,7 +86,7 @@

Edit an existing cluster

prepro_data = NULL, prepro_modulation = NULL, add_prefix = TRUE, - opts = antaresRead::simOptions() + opts = antaresRead::simOptions() ) editClusterRES( @@ -95,7 +95,7 @@

Edit an existing cluster

..., time_series = NULL, add_prefix = TRUE, - opts = antaresRead::simOptions() + opts = antaresRead::simOptions() ) @@ -139,7 +139,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath()

+antaresRead::setSimulationPath()

diff --git a/docs/reference/editClusterST.html b/docs/reference/editClusterST.html index 7b0b802a..c4f6a4fa 100644 --- a/docs/reference/editClusterST.html +++ b/docs/reference/editClusterST.html @@ -18,7 +18,7 @@ antaresEditObject - 0.6.0 + 0.6.2
@@ -88,7 +88,7 @@

Edit a short-term storage cluster

lower_rule_curve = NULL, upper_rule_curve = NULL, add_prefix = TRUE, - opts = antaresRead::simOptions() + opts = antaresRead::simOptions() ) @@ -136,7 +136,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath()

+antaresRead::setSimulationPath()

diff --git a/docs/reference/editLink.html b/docs/reference/editLink.html index 88f52239..bff2da8c 100644 --- a/docs/reference/editLink.html +++ b/docs/reference/editLink.html @@ -18,7 +18,7 @@ antaresEditObject - 0.6.0 + 0.6.2
@@ -88,7 +88,7 @@

Edit a link between two areas

filter_year_by_year = NULL, dataLink = NULL, tsLink = NULL, - opts = antaresRead::simOptions() + opts = antaresRead::simOptions() ) @@ -137,7 +137,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath()

+antaresRead::setSimulationPath()

diff --git a/docs/reference/fill_empty_hydro_ini_file.html b/docs/reference/fill_empty_hydro_ini_file.html index b8f7c8ef..5e063cbd 100644 --- a/docs/reference/fill_empty_hydro_ini_file.html +++ b/docs/reference/fill_empty_hydro_ini_file.html @@ -17,7 +17,7 @@ antaresEditObject - 0.6.0 + 0.6.2
@@ -75,7 +75,7 @@

Write default values in hydro.ini file if the section is empty

-
fill_empty_hydro_ini_file(area, opts = antaresRead::simOptions())
+
fill_empty_hydro_ini_file(area, opts = antaresRead::simOptions())
@@ -86,7 +86,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath().

+antaresRead::setSimulationPath().

diff --git a/docs/reference/fill_empty_hydro_ts_file.html b/docs/reference/fill_empty_hydro_ts_file.html index e480c47a..e356348e 100644 --- a/docs/reference/fill_empty_hydro_ts_file.html +++ b/docs/reference/fill_empty_hydro_ts_file.html @@ -17,7 +17,7 @@ antaresEditObject - 0.6.0 + 0.6.2 @@ -75,7 +75,7 @@

Write default input time series if mingen.txt or/and mod.
-
fill_empty_hydro_ts_file(area, opts = antaresRead::simOptions())
+
fill_empty_hydro_ts_file(area, opts = antaresRead::simOptions())
@@ -86,7 +86,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath().

+antaresRead::setSimulationPath().

diff --git a/docs/reference/filteringOptions.html b/docs/reference/filteringOptions.html index 64f33cc7..da9beb55 100644 --- a/docs/reference/filteringOptions.html +++ b/docs/reference/filteringOptions.html @@ -17,7 +17,7 @@ antaresEditObject - 0.6.0 + 0.6.2 diff --git a/docs/reference/getJobLogs.html b/docs/reference/getJobLogs.html index 7fd69e5b..c5da8b1f 100644 --- a/docs/reference/getJobLogs.html +++ b/docs/reference/getJobLogs.html @@ -17,7 +17,7 @@ antaresEditObject - 0.6.0 + 0.6.2 @@ -75,7 +75,7 @@

Retrieve job log from API

-
getJobLogs(job_id, opts = antaresRead::simOptions())
+
getJobLogs(job_id, opts = antaresRead::simOptions())
@@ -86,7 +86,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath()

+antaresRead::setSimulationPath()

@@ -100,7 +100,7 @@

Value

Examples

if (FALSE) {
 
-antaresRead::setSimulationPathAPI(
+antaresRead::setSimulationPathAPI(
   host = "http://localhost:8080",
   study_id = "39c604fc-687f-46c4-9fa6-59b57ff9c8d1",
   token = NULL,
diff --git a/docs/reference/getJobs.html b/docs/reference/getJobs.html
index 61d83e80..672753c6 100644
--- a/docs/reference/getJobs.html
+++ b/docs/reference/getJobs.html
@@ -17,7 +17,7 @@
       
       
         antaresEditObject
-        0.6.0
+        0.6.2
       
     
@@ -75,7 +75,7 @@

Retrieve API jobs

-
getJobs(job_id = NULL, opts = antaresRead::simOptions())
+
getJobs(job_id = NULL, opts = antaresRead::simOptions())
@@ -86,7 +86,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath()

+antaresRead::setSimulationPath()

diff --git a/docs/reference/get_default_hydro_ini_values.html b/docs/reference/get_default_hydro_ini_values.html index a771b1b3..0943d25a 100644 --- a/docs/reference/get_default_hydro_ini_values.html +++ b/docs/reference/get_default_hydro_ini_values.html @@ -17,7 +17,7 @@ antaresEditObject - 0.6.0 + 0.6.2
diff --git a/docs/reference/get_type_check_mingen_vs_hydrostorage.html b/docs/reference/get_type_check_mingen_vs_hydrostorage.html index 88a31b2b..31d1469f 100644 --- a/docs/reference/get_type_check_mingen_vs_hydrostorage.html +++ b/docs/reference/get_type_check_mingen_vs_hydrostorage.html @@ -17,7 +17,7 @@ antaresEditObject - 0.6.0 + 0.6.2 diff --git a/docs/reference/get_type_check_mingen_vs_hydrostorage_to_trigger.html b/docs/reference/get_type_check_mingen_vs_hydrostorage_to_trigger.html index 8c1ebbdf..b2ca46c7 100644 --- a/docs/reference/get_type_check_mingen_vs_hydrostorage_to_trigger.html +++ b/docs/reference/get_type_check_mingen_vs_hydrostorage_to_trigger.html @@ -20,7 +20,7 @@ antaresEditObject - 0.6.0 + 0.6.2 @@ -82,7 +82,7 @@

Get the type of control to execute between mingen data and hydro storage dat
get_type_check_mingen_vs_hydrostorage_to_trigger(
   area,
-  opts = antaresRead::simOptions()
+  opts = antaresRead::simOptions()
 )
@@ -94,7 +94,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath().

+antaresRead::setSimulationPath().

diff --git a/docs/reference/get_type_check_mingen_vs_maxpower_to_trigger.html b/docs/reference/get_type_check_mingen_vs_maxpower_to_trigger.html index 959a2be5..ed0d8774 100644 --- a/docs/reference/get_type_check_mingen_vs_maxpower_to_trigger.html +++ b/docs/reference/get_type_check_mingen_vs_maxpower_to_trigger.html @@ -21,7 +21,7 @@ antaresEditObject - 0.6.0 + 0.6.2
@@ -84,7 +84,7 @@

Get the type of control to execute between mingen data and maxpower data

get_type_check_mingen_vs_maxpower_to_trigger(
   area,
-  opts = antaresRead::simOptions()
+  opts = antaresRead::simOptions()
 )
@@ -96,7 +96,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath().

+antaresRead::setSimulationPath().

diff --git a/docs/reference/index.html b/docs/reference/index.html index 96a8aa89..ad8ae394 100644 --- a/docs/reference/index.html +++ b/docs/reference/index.html @@ -17,7 +17,7 @@ antaresEditObject - 0.6.0 + 0.6.2
@@ -180,10 +180,14 @@

All functions createPSP() getCapacityPSP() editPSP()

Create a Pumped Storage Power plant (PSP)

+ +

create_referential_series_type()

+ +

Create the correspondence data frame between the symbol and the type in scenario builder

deleteStudy()

-

Delete a study

+

Delete a study or a simulation

dicoGeneralSettings()

diff --git a/docs/reference/list_pollutants_values.html b/docs/reference/list_pollutants_values.html index 9f2eaa37..a01bf273 100644 --- a/docs/reference/list_pollutants_values.html +++ b/docs/reference/list_pollutants_values.html @@ -17,7 +17,7 @@ antaresEditObject - 0.6.0 + 0.6.2 diff --git a/docs/reference/mockSimulationAPI.html b/docs/reference/mockSimulationAPI.html index d7c29cea..dad0c3cb 100644 --- a/docs/reference/mockSimulationAPI.html +++ b/docs/reference/mockSimulationAPI.html @@ -18,7 +18,7 @@ antaresEditObject - 0.6.0 + 0.6.2 @@ -84,7 +84,7 @@

Mock API usage

Arguments

force

Logical, force mocking simulation even if -antaresRead::setSimulationPathAPI has already been called.

+antaresRead::setSimulationPathAPI has already been called.

antares_version
diff --git a/docs/reference/nodalOptimizationOptions.html b/docs/reference/nodalOptimizationOptions.html index acb5f117..a5df6453 100644 --- a/docs/reference/nodalOptimizationOptions.html +++ b/docs/reference/nodalOptimizationOptions.html @@ -17,7 +17,7 @@ antaresEditObject - 0.6.0 + 0.6.2 diff --git a/docs/reference/playlist.html b/docs/reference/playlist.html index 34b9eb36..0d1e6725 100644 --- a/docs/reference/playlist.html +++ b/docs/reference/playlist.html @@ -23,7 +23,7 @@ antaresEditObject - 0.6.0 + 0.6.2 @@ -86,16 +86,16 @@

Get the playlist of an Antares study
-
getPlaylist(opts = antaresRead::simOptions())
+    
getPlaylist(opts = antaresRead::simOptions())
 
-setPlaylist(playlist, weights = NULL, opts = antaresRead::simOptions())
+setPlaylist(playlist, weights = NULL, opts = antaresRead::simOptions())

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath()

+antaresRead::setSimulationPath()

playlist
@@ -117,9 +117,9 @@

Value

Examples

if (FALSE) {
-setSimulationPath("PATH/TO/STUDY/")
+setSimulationPath("PATH/TO/STUDY/")
 # or 
-setSimulationPathAPI(
+setSimulationPathAPI(
   host = "http://localhost:8080",
   study_id = "6f98a393-155d-450f-a581-8668dc355235",
   token = NULL,
diff --git a/docs/reference/propertiesLinkOptions.html b/docs/reference/propertiesLinkOptions.html
index 029b2a66..d7e64e0c 100644
--- a/docs/reference/propertiesLinkOptions.html
+++ b/docs/reference/propertiesLinkOptions.html
@@ -17,7 +17,7 @@
       
       
         antaresEditObject
-        0.6.0
+        0.6.2
       
     
diff --git a/docs/reference/removeArea.html b/docs/reference/removeArea.html index 114af32f..f81b07a8 100644 --- a/docs/reference/removeArea.html +++ b/docs/reference/removeArea.html @@ -18,7 +18,7 @@ antaresEditObject - 0.6.0 + 0.6.2
@@ -77,7 +77,7 @@

Remove an area from an Antares study

-
removeArea(name, opts = antaresRead::simOptions())
+
removeArea(name, opts = antaresRead::simOptions())
@@ -88,7 +88,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath()

+antaresRead::setSimulationPath()

diff --git a/docs/reference/removeBindingConstraint.html b/docs/reference/removeBindingConstraint.html index b115a2d6..25a76797 100644 --- a/docs/reference/removeBindingConstraint.html +++ b/docs/reference/removeBindingConstraint.html @@ -18,7 +18,7 @@ antaresEditObject - 0.6.0 + 0.6.2
@@ -77,7 +77,7 @@

Remove a Binding Constraint

-
removeBindingConstraint(name, opts = antaresRead::simOptions())
+
removeBindingConstraint(name, opts = antaresRead::simOptions())
@@ -88,7 +88,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath()

+antaresRead::setSimulationPath()

diff --git a/docs/reference/removeCluster.html b/docs/reference/removeCluster.html index 2b0e9ae3..9bf6689e 100644 --- a/docs/reference/removeCluster.html +++ b/docs/reference/removeCluster.html @@ -18,7 +18,7 @@ antaresEditObject - 0.6.0 + 0.6.2
@@ -81,21 +81,21 @@

Remove a cluster

area, cluster_name, add_prefix = TRUE, - opts = antaresRead::simOptions() + opts = antaresRead::simOptions() ) removeClusterRES( area, cluster_name, add_prefix = TRUE, - opts = antaresRead::simOptions() + opts = antaresRead::simOptions() ) removeClusterST( area, cluster_name, add_prefix = TRUE, - opts = antaresRead::simOptions() + opts = antaresRead::simOptions() )
@@ -115,7 +115,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath()

+antaresRead::setSimulationPath()

diff --git a/docs/reference/removeLink.html b/docs/reference/removeLink.html index 302a8f87..ea3e0086 100644 --- a/docs/reference/removeLink.html +++ b/docs/reference/removeLink.html @@ -18,7 +18,7 @@ antaresEditObject - 0.6.0 + 0.6.2
@@ -77,7 +77,7 @@

Remove a link between two areas

-
removeLink(from, to, opts = antaresRead::simOptions())
+
removeLink(from, to, opts = antaresRead::simOptions())
@@ -88,7 +88,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath()

+antaresRead::setSimulationPath()

diff --git a/docs/reference/replicate_missing_ts.html b/docs/reference/replicate_missing_ts.html index 8db2e5b4..5f43e42f 100644 --- a/docs/reference/replicate_missing_ts.html +++ b/docs/reference/replicate_missing_ts.html @@ -17,7 +17,7 @@ antaresEditObject - 0.6.0 + 0.6.2
diff --git a/docs/reference/rollback_to_previous_data.html b/docs/reference/rollback_to_previous_data.html index 9316c47b..6d86cc84 100644 --- a/docs/reference/rollback_to_previous_data.html +++ b/docs/reference/rollback_to_previous_data.html @@ -18,7 +18,7 @@ antaresEditObject - 0.6.0 + 0.6.2 @@ -81,7 +81,7 @@

Rollback to previous hydro data if the data is not consistent

area, prev_data, rollback_type, - opts = antaresRead::simOptions() + opts = antaresRead::simOptions() )
@@ -101,7 +101,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath().

+antaresRead::setSimulationPath().

diff --git a/docs/reference/runSimulation.html b/docs/reference/runSimulation.html index ea78351c..d2be55ae 100644 --- a/docs/reference/runSimulation.html +++ b/docs/reference/runSimulation.html @@ -18,7 +18,7 @@ antaresEditObject - 0.6.0 + 0.6.2
@@ -85,7 +85,7 @@

Run an Antares Simulation

show_output_on_console = FALSE, parallel = TRUE, ..., - opts = antaresRead::simOptions() + opts = antaresRead::simOptions() )
@@ -126,7 +126,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath()

+antaresRead::setSimulationPath()

diff --git a/docs/reference/runTsGenerator.html b/docs/reference/runTsGenerator.html index b92fb76e..0e6422a6 100644 --- a/docs/reference/runTsGenerator.html +++ b/docs/reference/runTsGenerator.html @@ -17,7 +17,7 @@ antaresEditObject - 0.6.0 + 0.6.2
@@ -79,7 +79,7 @@

Run Time-Series Generator

path_solver = getOption("antares.solver"), wait = TRUE, show_output_on_console = FALSE, - opts = antaresRead::simOptions() + opts = antaresRead::simOptions() ) @@ -101,7 +101,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath.

+antaresRead::setSimulationPath.

@@ -109,7 +109,7 @@

Arguments

Examples

if (FALSE) {
 library(antaresRead)
-setSimulationPath(path = "path/to/study")
+setSimulationPath(path = "path/to/study")
 
 library(antaresEditObject)
 runTsGenerator(
diff --git a/docs/reference/scenario-builder.html b/docs/reference/scenario-builder.html
index e38ea6ca..39efdf96 100644
--- a/docs/reference/scenario-builder.html
+++ b/docs/reference/scenario-builder.html
@@ -18,7 +18,7 @@
       
       
         antaresEditObject
-        0.6.0
+        0.6.2
       
     
@@ -82,13 +82,14 @@

Read, create, update & deduplicate scenario builder

n_mc = NULL, areas = NULL, areas_rand = NULL, - opts = antaresRead::simOptions() + coef_hydro_levels = NULL, + opts = antaresRead::simOptions() ) readScenarioBuilder( ruleset = "Default Ruleset", as_matrix = TRUE, - opts = antaresRead::simOptions() + opts = antaresRead::simOptions() ) updateScenarioBuilder( @@ -97,17 +98,17 @@

Read, create, update & deduplicate scenario builder

series = NULL, clusters_areas = NULL, links = NULL, - opts = antaresRead::simOptions() + opts = antaresRead::simOptions() ) clearScenarioBuilder( ruleset = "Default Ruleset", - opts = antaresRead::simOptions() + opts = antaresRead::simOptions() ) deduplicateScenarioBuilder( ruleset = "Default Ruleset", - opts = antaresRead::simOptions() + opts = antaresRead::simOptions() )
@@ -129,9 +130,13 @@

Arguments

Areas for which to use "rand".

+
coef_hydro_levels
+

Hydro levels coefficients.

+ +
opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath()

+antaresRead::setSimulationPath()

ruleset
@@ -145,7 +150,7 @@

Arguments

ldata

A matrix obtained with scenarioBuilder, or a named list of matrices obtained with scenarioBuilder, names must be -'l', 'h', 'w', 's', 't', 'r' or 'ntc', depending on the series to update.

+'l', 'h', 'w', 's', 't', 'r', 'ntc' or 'hl', depending on the series to update.

series
@@ -176,7 +181,8 @@

Value

Note

-

series = "ntc" is only available with Antares >= 8.2.0.

+

series = "ntc" is only available with Antares >= 8.2.0. +series = "hl" each value must be between 0 and 1.

@@ -187,7 +193,7 @@

Examples

library(antaresEditObject) # simulation path -setSimulationPath( +setSimulationPath( path = "pat/to/simulation", simulation = "input" ) @@ -201,6 +207,21 @@

Examples

sbuilder[, 1:6] dim(sbuilder) +# Create a scenario builder matrix for hydro levels (use case 1) +sbuilder <- scenarioBuilder( + n_mc = opts$parameters$general$nbyears, + areas = c("fr", "be"), + coef_hydro_levels = c(0.1, 0.9) +) + +# Create a scenario builder matrix for hydro levels (use case 2) +sbuilder <- scenarioBuilder( + n_mc = opts$parameters$general$nbyears, + areas = c("fr", "be"), + coef_hydro_levels = c(runif(opts$parameters$general$nbyears) + , runif(opts$parameters$general$nbyears) + ) +) # Read previous scenario builder # in a matrix format diff --git a/docs/reference/searchStudy.html b/docs/reference/searchStudy.html index 0db51946..9751c88c 100644 --- a/docs/reference/searchStudy.html +++ b/docs/reference/searchStudy.html @@ -17,7 +17,7 @@ antaresEditObject - 0.6.0 + 0.6.2
diff --git a/docs/reference/setAPImode.html b/docs/reference/setAPImode.html index 9f530f25..d36dd212 100644 --- a/docs/reference/setAPImode.html +++ b/docs/reference/setAPImode.html @@ -21,7 +21,7 @@ antaresEditObject - 0.6.0 + 0.6.2 @@ -80,7 +80,7 @@

Set API mode

-
setAPImode(mode = c("sync", "async"), opts = antaresRead::simOptions())
+
setAPImode(mode = c("sync", "async"), opts = antaresRead::simOptions())
@@ -91,7 +91,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath()

+antaresRead::setSimulationPath()

diff --git a/docs/reference/setSolverPath.html b/docs/reference/setSolverPath.html index 0acb8f2b..09fd52e4 100644 --- a/docs/reference/setSolverPath.html +++ b/docs/reference/setSolverPath.html @@ -17,7 +17,7 @@ antaresEditObject - 0.6.0 + 0.6.2
diff --git a/docs/reference/storage_values_default.html b/docs/reference/storage_values_default.html index 4358be74..8ba869c8 100644 --- a/docs/reference/storage_values_default.html +++ b/docs/reference/storage_values_default.html @@ -17,7 +17,7 @@ antaresEditObject - 0.6.0 + 0.6.2 diff --git a/docs/reference/updateAdequacySettings.html b/docs/reference/updateAdequacySettings.html index 1ccbe1fd..89f659fc 100644 --- a/docs/reference/updateAdequacySettings.html +++ b/docs/reference/updateAdequacySettings.html @@ -18,7 +18,7 @@ antaresEditObject - 0.6.0 + 0.6.2 @@ -88,7 +88,7 @@

Update adequacy parameters of an Antares study

threshold_initiate_curtailment_sharing_rule = NULL, threshold_display_local_matching_rule_violations = NULL, threshold_csr_variable_bounds_relaxation = NULL, - opts = antaresRead::simOptions() + opts = antaresRead::simOptions() ) @@ -136,7 +136,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath()

+antaresRead::setSimulationPath()

diff --git a/docs/reference/updateGeneralSettings.html b/docs/reference/updateGeneralSettings.html index a1326a73..2ffeac8f 100644 --- a/docs/reference/updateGeneralSettings.html +++ b/docs/reference/updateGeneralSettings.html @@ -18,7 +18,7 @@ antaresEditObject - 0.6.0 + 0.6.2
@@ -108,7 +108,8 @@

Update general parameters of an Antares study

refreshintervalthermal = NULL, refreshintervalsolar = NULL, readonly = NULL, - opts = antaresRead::simOptions() + geographic.trimming = NULL, + opts = antaresRead::simOptions() ) @@ -237,9 +238,13 @@

Arguments

See Antares General Reference Guide.

+
geographic.trimming
+

logical indicates whether to store the results for all time spans (FALSE) or for custom time spans (TRUE)

+ +
opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath()

+antaresRead::setSimulationPath()

diff --git a/docs/reference/updateInputSettings.html b/docs/reference/updateInputSettings.html index e6669b43..bf8e0adc 100644 --- a/docs/reference/updateInputSettings.html +++ b/docs/reference/updateInputSettings.html @@ -18,7 +18,7 @@ antaresEditObject - 0.6.0 + 0.6.2
@@ -77,7 +77,7 @@

Update input parameters of an Antares study

-
updateInputSettings(import, opts = antaresRead::simOptions())
+
updateInputSettings(import, opts = antaresRead::simOptions())
@@ -88,7 +88,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath()

+antaresRead::setSimulationPath()

diff --git a/docs/reference/updateOptimizationSettings.html b/docs/reference/updateOptimizationSettings.html index ba98df89..20aeb380 100644 --- a/docs/reference/updateOptimizationSettings.html +++ b/docs/reference/updateOptimizationSettings.html @@ -18,7 +18,7 @@ antaresEditObject - 0.6.0 + 0.6.2
@@ -96,7 +96,7 @@

Update optimization parameters of an Antares study

number.of.cores.mode = NULL, renewable.generation.modelling = NULL, day.ahead.reserve.management = NULL, - opts = antaresRead::simOptions() + opts = antaresRead::simOptions() ) @@ -177,7 +177,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath()

+antaresRead::setSimulationPath()

diff --git a/docs/reference/updateOutputSettings.html b/docs/reference/updateOutputSettings.html index 8b167422..ba51c938 100644 --- a/docs/reference/updateOutputSettings.html +++ b/docs/reference/updateOutputSettings.html @@ -18,7 +18,7 @@ antaresEditObject - 0.6.0 + 0.6.2
@@ -82,7 +82,7 @@

Update output parameters of an Antares study

storenewset = NULL, archives = NULL, result.format = NULL, - opts = antaresRead::simOptions() + opts = antaresRead::simOptions() ) @@ -108,7 +108,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath()

+antaresRead::setSimulationPath()

@@ -120,7 +120,18 @@

Value

Examples

- +
if (FALSE) {
+
+updateOutputSettings(
+  synthesis = TRUE,
+  storenewset = FALSE,
+  archives = c("load", "wind"),
+  result.format = "zip"
+)
+
+}
+
+
@@ -78,7 +78,7 @@

Get API commands generated

getVariantCommands(
   last = NULL,
   actions = NULL,
-  opts = antaresRead::simOptions()
+  opts = antaresRead::simOptions()
 )
 
 writeVariantCommands(
@@ -86,7 +86,7 @@ 

Get API commands generated

last = NULL, actions = NULL, ..., - opts = antaresRead::simOptions() + opts = antaresRead::simOptions() )
@@ -103,7 +103,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath()

+antaresRead::setSimulationPath()

path
diff --git a/docs/reference/variant.html b/docs/reference/variant.html index 5968cb37..c71c03be 100644 --- a/docs/reference/variant.html +++ b/docs/reference/variant.html @@ -17,7 +17,7 @@ antaresEditObject - 0.6.0 + 0.6.2 @@ -75,9 +75,9 @@

Create a study's variant

-
createVariant(name, opts = antaresRead::simOptions())
+    
createVariant(name, opts = antaresRead::simOptions())
 
-useVariant(name, variant_id = NULL, opts = antaresRead::simOptions())
+useVariant(name, variant_id = NULL, opts = antaresRead::simOptions())
@@ -88,7 +88,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath()

+antaresRead::setSimulationPath()

variant_id
diff --git a/docs/reference/write-ini.html b/docs/reference/write-ini.html index a714dd9f..7902f680 100644 --- a/docs/reference/write-ini.html +++ b/docs/reference/write-ini.html @@ -17,7 +17,7 @@ antaresEditObject - 0.6.0 + 0.6.2
@@ -78,7 +78,7 @@

Write configuration options in file or API

writeIni(
   listData,
   pathIni,
-  opts = antaresRead::simOptions(),
+  opts = antaresRead::simOptions(),
   ...,
   default_ext = ".ini"
 )
@@ -100,7 +100,7 @@ 

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath()

+antaresRead::setSimulationPath()

...
diff --git a/docs/reference/writeEconomicOptions.html b/docs/reference/writeEconomicOptions.html index b2a7c0fd..782c5fe3 100644 --- a/docs/reference/writeEconomicOptions.html +++ b/docs/reference/writeEconomicOptions.html @@ -19,7 +19,7 @@ antaresEditObject - 0.6.0 + 0.6.2
@@ -79,7 +79,7 @@

Write Economic Options

-
writeEconomicOptions(x, opts = antaresRead::simOptions())
+
writeEconomicOptions(x, opts = antaresRead::simOptions())
@@ -96,7 +96,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath

+antaresRead::setSimulationPath

@@ -107,7 +107,7 @@

Examples

library(antaresRead) # Set simulation path -setSimulationPath(path = "PATH/TO/SIMULATION", simulation = "input") +setSimulationPath(path = "PATH/TO/SIMULATION", simulation = "input") # Write some economic options for areas a, b and c writeEconomicOptions(data.frame( diff --git a/docs/reference/writeHydroValues.html b/docs/reference/writeHydroValues.html index dbea1b30..07fcbf15 100644 --- a/docs/reference/writeHydroValues.html +++ b/docs/reference/writeHydroValues.html @@ -18,7 +18,7 @@ antaresEditObject - 0.6.0 + 0.6.2 @@ -82,7 +82,7 @@

Write Hydro Values

type, data, overwrite = TRUE, - opts = antaresRead::simOptions() + opts = antaresRead::simOptions() )
@@ -97,16 +97,16 @@

Arguments

data
-

The data must have specific dimension depending on the type of file :

  • waterValues: a 365x101 numeric matrix: +

    The data must have specific dimension depending on the type of file :

    • waterValues: a 365x101 numeric matrix: marginal values for the stored energy based on date (365 days) and on the reservoir level (101 round percentage values ranging from 0% to 100%). OR a 3-column matrix with 365x101 rows. In this latter case the 3 columns must be 'date', 'level' and 'value' (in this order), and the rows must be sorted by: ascending day, ascending level.

    • -
    • reservoir: a 365x3 numeric matrix. The columns contains respectively the levels min, avg and max.

    • -
    • maxpower: a 365x4 numeric matrix.

    • -
    • inflowPattern: a 365x1 numeric matrix.

    • -
    • creditmodulations: a 2x101 numeric matrix.

    • +
    • reservoir: a 365x3 numeric matrix. The columns contains respectively the levels min, avg and max.

    • +
    • maxpower: a 365x4 numeric matrix.

    • +
    • inflowPattern: a 365x1 numeric matrix.

    • +
    • creditmodulations: a 2x101 numeric matrix.

    @@ -116,7 +116,7 @@

    Arguments

    opts

    List of simulation parameters returned by the function -antaresRead::setSimulationPath().

    +antaresRead::setSimulationPath().

diff --git a/docs/reference/writeIniHydro.html b/docs/reference/writeIniHydro.html index ae9fcb31..ddd7567f 100644 --- a/docs/reference/writeIniHydro.html +++ b/docs/reference/writeIniHydro.html @@ -18,7 +18,7 @@ antaresEditObject - 0.6.0 + 0.6.2
@@ -77,7 +77,7 @@

Edit hydro.ini values

-
writeIniHydro(area, params, mode = "other", opts = antaresRead::simOptions())
+
writeIniHydro(area, params, mode = "other", opts = antaresRead::simOptions())
@@ -87,21 +87,21 @@

Arguments

params
-

The list data must have specific names and specific types :

  • follow load : logical or NULL

  • -
  • use heuristic : logical or NULL

  • -
  • use water : logical or NULL

  • -
  • hard bounds : logical or NULL

  • -
  • use leeway : logical or NULL

  • -
  • power to level : logical or NULL

  • -
  • reservoir : logical or NULL

  • -
  • inter-daily-breakdown : numeric, integer or NULL

  • -
  • intra-daily-modulation : numeric, integer or NULL

  • -
  • inter-monthly-breakdown : numeric, integer or NULL

  • -
  • leeway low : numeric, integer or NULL

  • -
  • leeway up : numeric, integer or NULL

  • -
  • pumping efficiency : numeric, integer or NULL

  • -
  • initialize reservoir date : numeric, integer or NULL

  • -
  • reservoir capacity : numeric, integer or NULL

  • +

    The list data must have specific names and specific types :

    • follow load : logical or NULL

    • +
    • use heuristic : logical or NULL

    • +
    • use water : logical or NULL

    • +
    • hard bounds : logical or NULL

    • +
    • use leeway : logical or NULL

    • +
    • power to level : logical or NULL

    • +
    • reservoir : logical or NULL

    • +
    • inter-daily-breakdown : numeric, integer or NULL

    • +
    • intra-daily-modulation : numeric, integer or NULL

    • +
    • inter-monthly-breakdown : numeric, integer or NULL

    • +
    • leeway low : numeric, integer or NULL

    • +
    • leeway up : numeric, integer or NULL

    • +
    • pumping efficiency : numeric, integer or NULL

    • +
    • initialize reservoir date : numeric, integer or NULL

    • +
    • reservoir capacity : numeric, integer or NULL

    @@ -111,7 +111,7 @@

    Arguments

    opts

    List of simulation parameters returned by the function -antaresRead::setSimulationPath().

    +antaresRead::setSimulationPath().

@@ -126,7 +126,7 @@

Warning

Examples

if (FALSE) {
-opts <- setSimulationPath(studypath, simulation = "input")
+opts <- setSimulationPath(studypath, simulation = "input")
 createArea("fictive_area") 
 writeIniHydro(area = "fictive_area"
 , params = list("leeway low" = 2.5, "leeway up" = 25))
diff --git a/docs/reference/writeInputTS.html b/docs/reference/writeInputTS.html
index 82a3f537..25bb959c 100644
--- a/docs/reference/writeInputTS.html
+++ b/docs/reference/writeInputTS.html
@@ -18,7 +18,7 @@
       
       
         antaresEditObject
-        0.6.0
+        0.6.2
       
     
@@ -83,7 +83,7 @@

Write input time series

area = NULL, link = NULL, overwrite = TRUE, - opts = antaresRead::simOptions() + opts = antaresRead::simOptions() )
@@ -109,7 +109,7 @@

Arguments

link

Link for which writing transmission capacities time series, -must like "area01%area02" or c("area01", "area02").

+must like "area01%area02" or "area01 - area02" or c("area01", "area02").

overwrite
@@ -118,7 +118,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath()

+antaresRead::setSimulationPath()

diff --git a/docs/reference/writeMiscGen.html b/docs/reference/writeMiscGen.html index 1673ce51..c4b480a4 100644 --- a/docs/reference/writeMiscGen.html +++ b/docs/reference/writeMiscGen.html @@ -17,7 +17,7 @@ antaresEditObject - 0.6.0 + 0.6.2
@@ -75,7 +75,7 @@

Write Misc Gen data

-
writeMiscGen(data, area, opts = antaresRead::simOptions())
+
writeMiscGen(data, area, opts = antaresRead::simOptions())
@@ -90,7 +90,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath()

+antaresRead::setSimulationPath()

diff --git a/docs/reference/writeOutputValues.html b/docs/reference/writeOutputValues.html index 31080429..4cc45f0b 100644 --- a/docs/reference/writeOutputValues.html +++ b/docs/reference/writeOutputValues.html @@ -18,7 +18,7 @@ antaresEditObject - 0.6.0 + 0.6.2
@@ -88,7 +88,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath()

+antaresRead::setSimulationPath()

@@ -98,8 +98,8 @@

Examples

library(antaresRead) library(data.table) -opts <- setSimulationPath("my_study") -data <- readAntares(links = "all", areas = "all", clusters = "all") +opts <- setSimulationPath("my_study") +data <- readAntares(links = "all", areas = "all", clusters = "all") writeOutputValues(data) } diff --git a/docs/reference/writeSeriesPrepro.html b/docs/reference/writeSeriesPrepro.html index d9da4c64..e0837148 100644 --- a/docs/reference/writeSeriesPrepro.html +++ b/docs/reference/writeSeriesPrepro.html @@ -19,7 +19,7 @@ antaresEditObject - 0.6.0 + 0.6.2 @@ -87,7 +87,7 @@

Write prepro data

translation = NULL, conversion = NULL, overwrite = TRUE, - opts = antaresRead::simOptions() + opts = antaresRead::simOptions() ) @@ -129,7 +129,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath().

+antaresRead::setSimulationPath().

diff --git a/docs/reference/writeWaterValues.html b/docs/reference/writeWaterValues.html index 9f0f3a01..f7cb4cf2 100644 --- a/docs/reference/writeWaterValues.html +++ b/docs/reference/writeWaterValues.html @@ -18,7 +18,7 @@ antaresEditObject - 0.6.0 + 0.6.2 @@ -81,7 +81,7 @@

Write water values

area, data = NULL, overwrite = TRUE, - opts = antaresRead::simOptions() + opts = antaresRead::simOptions() ) @@ -105,7 +105,7 @@

Arguments

opts

List of simulation parameters returned by the function -antaresRead::setSimulationPath().

+antaresRead::setSimulationPath().

diff --git a/docs/sitemap.xml b/docs/sitemap.xml index 7c3b470e..c5800d13 100644 --- a/docs/sitemap.xml +++ b/docs/sitemap.xml @@ -117,6 +117,9 @@ /reference/createPSP.html + + /reference/create_referential_series_type.html + /reference/deleteStudy.html diff --git a/man/backupStudy.Rd b/man/backupStudy.Rd index 2b917638..af6a16bf 100644 --- a/man/backupStudy.Rd +++ b/man/backupStudy.Rd @@ -21,7 +21,7 @@ or \code{study} for the whole study.} \item{opts}{List of simulation parameters returned by the function \code{antaresRead::setSimulationPath}} -\item{extension}{Defaut is {.zip}.} +\item{extension}{Defaut is \code{.zip}.} } \value{ The path of the backup diff --git a/man/createCluster.Rd b/man/createCluster.Rd index 96f157b5..4e1e5d0e 100644 --- a/man/createCluster.Rd +++ b/man/createCluster.Rd @@ -148,7 +148,7 @@ createCluster( area = "fr", cluster_name = "my_cluster", prepro_data = data.frame( - v1 = rep(7, 365), # column name doesn't matter + v1 = rep(7, 365), # column name does not matter v2 = rep(27, 365), v3 = rep(0.05, 365), v4 = rep(0.12, 365), @@ -163,7 +163,7 @@ createCluster( createCluster( area = "fr", cluster_name = "my_cluster", - prepro_modulation = = matrix( + prepro_modulation = matrix( data = c(rep(1, times = 365 * 24 * 3), rep(0, times = 365 * 24 * 1)), ncol = 4 @@ -175,7 +175,7 @@ createCluster( area = "fr", cluster_name = "my_cluster", prepro_modulation = data.frame( - var1 = rep(0, 8760), # column name doesn't matter + var1 = rep(0, 8760), # column name does not matter var2 = rep(1, 8760), var3 = rep(0, 8760), var4 = rep(1, 8760) diff --git a/man/deleteStudy.Rd b/man/deleteStudy.Rd index b74d6a9a..ca9af5e1 100644 --- a/man/deleteStudy.Rd +++ b/man/deleteStudy.Rd @@ -2,15 +2,17 @@ % Please edit documentation in R/createStudy.R \name{deleteStudy} \alias{deleteStudy} -\title{Delete a study} +\title{Delete a study or a simulation} \usage{ -deleteStudy(opts = simOptions(), prompt_validation = FALSE) +deleteStudy(opts = simOptions(), prompt_validation = FALSE, simulation = NULL) } \arguments{ \item{opts}{List. study options} \item{prompt_validation}{\code{logical} to put validation message to delete study (default \code{FALSE})} + +\item{simulation}{simulation to be deleted (default \code{NULL})} } \description{ -Delete a study +Delete a study or a simulation } diff --git a/man/get_type_check_mingen_vs_hydrostorage_to_trigger.Rd b/man/get_type_check_mingen_vs_hydrostorage_to_trigger.Rd index 5e47763a..04b0cd54 100644 --- a/man/get_type_check_mingen_vs_hydrostorage_to_trigger.Rd +++ b/man/get_type_check_mingen_vs_hydrostorage_to_trigger.Rd @@ -21,8 +21,8 @@ a character containing the type of control to execute. \description{ Compute the type of control to make between : \itemize{ -\item{\verb{input/hydro/series//mingen.txt}} -\item{\verb{input/hydro/series//mod.txt}} +\item \code{input/hydro/series//mingen.txt} +\item \code{input/hydro/series//mod.txt} } This control is implemented in Antares too. } diff --git a/man/get_type_check_mingen_vs_maxpower_to_trigger.Rd b/man/get_type_check_mingen_vs_maxpower_to_trigger.Rd index 98c7884f..f9ee2965 100644 --- a/man/get_type_check_mingen_vs_maxpower_to_trigger.Rd +++ b/man/get_type_check_mingen_vs_maxpower_to_trigger.Rd @@ -21,8 +21,8 @@ a character containing the type of control to execute. \description{ Compute the type of control to make between : \itemize{ -\item{\verb{input/hydro/series//mingen.txt}} -\item{\verb{input/hydro/common/capacity/maxpower_.txt}} +\item \code{input/hydro/series//mingen.txt} +\item \code{input/hydro/common/capacity/maxpower_.txt} } This control is implemented in Antares too. No control to execute if \code{reservoir} section in hydro.ini for the area is set to TRUE. diff --git a/man/updateGeneralSettings.Rd b/man/updateGeneralSettings.Rd index c3053903..d62ed913 100644 --- a/man/updateGeneralSettings.Rd +++ b/man/updateGeneralSettings.Rd @@ -35,6 +35,7 @@ updateGeneralSettings( refreshintervalthermal = NULL, refreshintervalsolar = NULL, readonly = NULL, + geographic.trimming = NULL, opts = antaresRead::simOptions() ) } @@ -102,6 +103,8 @@ Study_name/OUTPUT/simu_tag/Economy /mc-i-number} \item{readonly}{See Antares General Reference Guide.} +\item{geographic.trimming}{\code{logical} indicates whether to store the results for all time spans (FALSE) or for custom time spans (TRUE)} + \item{opts}{List of simulation parameters returned by the function \code{\link[antaresRead:setSimulationPath]{antaresRead::setSimulationPath()}}} } diff --git a/man/updateOutputSettings.Rd b/man/updateOutputSettings.Rd index c46ea915..06d561a7 100644 --- a/man/updateOutputSettings.Rd +++ b/man/updateOutputSettings.Rd @@ -40,7 +40,7 @@ Update output parameters of an Antares study updateOutputSettings( synthesis = TRUE, storenewset = FALSE, - archives = c("load", "wind") + archives = c("load", "wind"), result.format = "zip" ) diff --git a/man/writeHydroValues.Rd b/man/writeHydroValues.Rd index 2ffb0615..e2cc7b0d 100644 --- a/man/writeHydroValues.Rd +++ b/man/writeHydroValues.Rd @@ -19,16 +19,16 @@ writeHydroValues( \item{data}{The data must have specific dimension depending on the type of file : \itemize{ -\item{waterValues}{: a 365x101 numeric matrix: +\item \code{waterValues}: a 365x101 numeric matrix: marginal values for the stored energy based on date (365 days) and on the reservoir level (101 round percentage values ranging from 0\% to 100\%). OR a 3-column matrix with 365x101 rows. In this latter case the 3 columns must be 'date', 'level' and 'value' (in this order), and the rows must be sorted by: -ascending day, ascending level.} -\item{reservoir}{: a 365x3 numeric matrix. The columns contains respectively the levels min, avg and max.} -\item{maxpower}{: a 365x4 numeric matrix.} -\item{inflowPattern}{: a 365x1 numeric matrix.} -\item{creditmodulations}{: a 2x101 numeric matrix.} +ascending day, ascending level. +\item \code{reservoir}: a 365x3 numeric matrix. The columns contains respectively the levels min, avg and max. +\item \code{maxpower}: a 365x4 numeric matrix. +\item \code{inflowPattern}: a 365x1 numeric matrix. +\item \code{creditmodulations}: a 2x101 numeric matrix. }} \item{overwrite}{Logical. Overwrite the values if a file already exists.} diff --git a/man/writeIniHydro.Rd b/man/writeIniHydro.Rd index 01122148..aa43d222 100644 --- a/man/writeIniHydro.Rd +++ b/man/writeIniHydro.Rd @@ -11,21 +11,21 @@ writeIniHydro(area, params, mode = "other", opts = antaresRead::simOptions()) \item{params}{The list data must have specific names and specific types : \itemize{ -\item{follow load}{ : logical or NULL} -\item{use heuristic}{ : logical or NULL} -\item{use water}{ : logical or NULL} -\item{hard bounds}{ : logical or NULL} -\item{use leeway}{ : logical or NULL} -\item{power to level}{ : logical or NULL} -\item{reservoir}{ : logical or NULL} -\item{inter-daily-breakdown}{ : numeric, integer or NULL} -\item{intra-daily-modulation}{ : numeric, integer or NULL} -\item{inter-monthly-breakdown}{ : numeric, integer or NULL} -\item{leeway low}{ : numeric, integer or NULL} -\item{leeway up}{ : numeric, integer or NULL} -\item{pumping efficiency}{ : numeric, integer or NULL} -\item{initialize reservoir date}{ : numeric, integer or NULL} -\item{reservoir capacity}{ : numeric, integer or NULL} +\item \code{follow load} : logical or NULL +\item \code{use heuristic} : logical or NULL +\item \code{use water} : logical or NULL +\item \code{hard bounds} : logical or NULL +\item \code{use leeway} : logical or NULL +\item \code{power to level} : logical or NULL +\item \code{reservoir} : logical or NULL +\item \code{inter-daily-breakdown} : numeric, integer or NULL +\item \code{intra-daily-modulation} : numeric, integer or NULL +\item \code{inter-monthly-breakdown} : numeric, integer or NULL +\item \code{leeway low} : numeric, integer or NULL +\item \code{leeway up} : numeric, integer or NULL +\item \code{pumping efficiency} : numeric, integer or NULL +\item \code{initialize reservoir date} : numeric, integer or NULL +\item \code{reservoir capacity} : numeric, integer or NULL }} \item{mode}{Execution mode. Useful when you create a new area or remove an existing area to avoid control on hydro data.} diff --git a/man/writeInputTS.Rd b/man/writeInputTS.Rd index 76bebe61..c3ed1a31 100644 --- a/man/writeInputTS.Rd +++ b/man/writeInputTS.Rd @@ -28,7 +28,7 @@ Refers to note section below.} \item{area}{The area where to write the input time series.} \item{link}{Link for which writing transmission capacities time series, -must like \code{"area01\%area02"} or \code{c("area01", "area02")}.} +must like \code{"area01\%area02"} or \code{"area01 - area02"} or \code{c("area01", "area02")}.} \item{overwrite}{Logical. Overwrite the values if a file already exists.} diff --git a/tests/testthat/test-createArea.R b/tests/testthat/test-createArea.R index 68d2418b..76cbf615 100644 --- a/tests/testthat/test-createArea.R +++ b/tests/testthat/test-createArea.R @@ -22,6 +22,7 @@ sapply(studies, function(study) { test_that("Backup study/input", { + skip_on_cran() # issue 115 backupStudy(what = "study", extension = ".zip") backupStudy(what = "input", extension = ".tar.gz") expect_true(file.exists(paste0(opts$studyPath, ".zip"))) diff --git a/tests/testthat/test-playlist.R b/tests/testthat/test-playlist.R index 066c9fbd..6e250b39 100644 --- a/tests/testthat/test-playlist.R +++ b/tests/testthat/test-playlist.R @@ -28,3 +28,37 @@ sapply(studies, function(study) { unlink(x = file.path(pathstd, "test_case"), recursive = TRUE) }) + + +test_that("Check if setPlaylist() is disabled when playlist = mcYear and enabled when playlist is customized", { + + ant_version <- "8.2.0" + st_test <- paste0("my_study_820_", paste0(sample(letters,5),collapse = "")) + suppressWarnings(opts <- createStudy(path = pathstd, study_name = st_test, antares_version = ant_version)) + + # Default behaviour + expect_false(opts$parameters$general$`user-playlist`) + + # Set parameters + updateGeneralSettings(nbyears = 10) + suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input")) + nbyears <- opts$parameters$general$nbyears + mc_years <- seq(nbyears) + + # With custom playlist - user-playlist is enabled + playlist_to_write <- sort(sample(mc_years, size = nbyears/2, replace = FALSE)) + opts <- setPlaylist(playlist = playlist_to_write, opts = opts) + playlist_in_file <- getPlaylist(opts = opts) + + expect_true(opts$parameters$general$`user-playlist`) + playlist_opts <- opts$parameters$playlist + playlist_opts <- playlist_opts[which(names(playlist_opts) == "playlist_year +")] + expect_true(all(unlist(playlist_opts) %in% (playlist_to_write - 1))) + expect_equal(playlist_to_write, playlist_in_file) + + # No playlist if length(playlist) == length(mcYear) - user-playlist is disabled + setPlaylist(playlist = mc_years, opts = opts) + suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input")) + + expect_false(opts$parameters$general$`user-playlist`) +}) diff --git a/tests/testthat/test-updateGeneralSettings.R b/tests/testthat/test-updateGeneralSettings.R index 10032948..f103ad6d 100644 --- a/tests/testthat/test-updateGeneralSettings.R +++ b/tests/testthat/test-updateGeneralSettings.R @@ -12,10 +12,15 @@ sapply(studies, function(study) { test_that("Update a general parameter", { + # year-by-year + expect_true(getOption("antares")$parameters$general$`year-by-year`) updateGeneralSettings(year.by.year = FALSE) - expect_false(getOption("antares")$parameters$general$`year-by-year`) + # geographic-trimming + expect_true(getOption("antares")$parameters$general$`geographic-trimming`) + updateGeneralSettings(geographic.trimming = FALSE) + expect_false(getOption("antares")$parameters$general$`geographic-trimming`) }) # remove temporary study diff --git a/vignettes/antaresEditObject.Rmd b/vignettes/antaresEditObject.Rmd index 363dc444..874bec43 100644 --- a/vignettes/antaresEditObject.Rmd +++ b/vignettes/antaresEditObject.Rmd @@ -220,3 +220,140 @@ runSimulation( ) ``` +## Read a time series, update it and write it + +To update an existing time series and write it, you can use the following commands : + +```r +# Filepath of the study, version >= 820 +my_study <- file.path("", "", "") +opts <- setSimulationPath(my_study, simulation ="input") +opts$timeIdMax <- 8760 + +# Links, use only one link +my_link <- as.character(getLinks()[1]) +ts_input <- readInputTS(linkCapacity = my_link, opts = opts) + +# Sort the data to ensure its reliability +data.table::setorder(ts_input, cols = "tsId", "timeId") + +# Reshape to wide format : writeInputTS expects a 8760 * N matrix +metrics <- c("transCapacityDirect", "transCapacityIndirect") +ts_input_reformatted <- data.table::dcast(ts_input, + timeId ~ tsId, + value.var = metrics + ) +# Add a value my_param to your matrix +my_param <- 123 +writeInputTS(data = ts_input_reformatted[,2:ncol(ts_input_reformatted)] + my_param, + type = "tsLink", + link = my_link, + overwrite = TRUE, + opts = opts + ) + + +# Thermal, use only one area and one cluster +my_area <- "zone" +my_cluster <- "mon_cluster" +ts_input <- readInputTS(thermalAvailabilities = my_area, opts = opts) +ts_input <- ts_input[cluster == paste0(my_area,"_",my_cluster)] + +# Sort the data to ensure its reliability +data.table::setorder(ts_input, cols = "tsId", "timeId") + +# Reshape to wide format : writeInputTS expects a 8760 * N matrix +metrics <- c("ThermalAvailabilities") +ts_input_reformatted <- data.table::dcast(ts_input, + timeId ~ tsId, + value.var = metrics + ) + +# Add a value my_param to your matrix +my_param <- 1000 +editCluster(area = my_area, + cluster_name = my_cluster, + time_series = ts_input_reformatted[,2:ncol(ts_input_reformatted)] + my_param, + opts = opts + ) + + +# Run of River, use only one area +my_area <- "zone" +ts_input <- readInputTS(ror = my_area, opts = opts) + +# Sort the data to ensure its reliability +data.table::setorder(ts_input, cols = "tsId", "timeId") + +# Reshape to wide format : writeInputTS expects a 8760 * N matrix +metrics <- c("ror") +ts_input_reformatted <- data.table::dcast(ts_input, + timeId ~ tsId, + value.var = metrics +) + +# Add a value my_param to your matrix +my_param <- 1000 +writeInputTS(area = my_area, + type = "hydroROR", + data = ts_input_reformatted[,2:ncol(ts_input_reformatted)] + my_param, + overwrite = TRUE, + opts = opts +) +``` + +## Edit geographic trimming + +```r +# set the path to an Antares study +my_study <- file.path("", "", "") +opts <- setSimulationPath(my_study, simulation ="input") + +# choose geographic trimming when creating new Antares areas +# default filtering : c("hourly","daily","weekly","monthly","annual") +initial_filtering_synthesis <- c("weekly","monthly") +initial_filtering_year_by_year <- c("monthly","annual") + + +opts <- createArea(name = "area1", + filtering = filteringOptions( + filter_synthesis = initial_filtering_synthesis, + filter_year_by_year = initial_filtering_year_by_year), + opts = opts) +opts <- createArea(name = "area2",opts = opts) +opts <- createLink(from = "area1", + to = "area2", + propertiesLink = propertiesLinkOptions( + filter_synthesis = initial_filtering_synthesis, + filter_year_by_year = initial_filtering_year_by_year), + opts = opts) + +# check the initial filters +initial_GT <- getGeographicTrimming(areas="area1",links=TRUE,opts=opts) +print(initial_GT$areas[["area1"]]) +print(initial_GT$links[["area1 - area2"]]) + +# edit geographic trimming of an existing area or link +new_filtering_synthesis <- c("monthly") +new_filtering_year_by_year <- c("annual") + +opts <- editArea(name = "area1", + filtering = filteringOptions( + filter_synthesis = new_filtering_synthesis, + filter_year_by_year = new_filtering_year_by_year), + opts = opts) +opts <- editLink(from = "area1", + to = "area2", + filter_year_by_year = new_filtering_year_by_year, + filter_synthesis = new_filtering_synthesis, + opts = opts) + +# check the new filters +new_GT <- antaresRead::getGeographicTrimming(areas="area1",links=TRUE,opts=opts) +print(new_GT$areas[["area1"]]) +print(new_GT$links[["area1 - area2"]]) + +# important : make sure that `geographic-trimming` parameter is activated in general settings +opts <- updateGeneralSettings(geographic.trimming = TRUE,opts = opts) +``` + From ffb554870ce593abcaddca73f09d4f06e4cd799e Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Thu, 25 Jan 2024 17:12:49 +0100 Subject: [PATCH 22/74] use dev package antaresRead --- DESCRIPTION | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index b31706c6..26b1826f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -27,7 +27,8 @@ BugReports: https://github.com/rte-antares-rpackage/antaresEditObject/issues Encoding: UTF-8 RoxygenNote: 7.2.2 Roxygen: list(markdown = TRUE) -Depends: antaresRead (>= 2.4.2) +Depends: + antaresRead (>= 2.4.2) Imports: assertthat, cli, @@ -51,3 +52,5 @@ Suggests: knitr, rmarkdown VignetteBuilder: knitr +Remotes: + rte-antares-rpackage/antaresRead@release/v8.7.0 From fc029e04f0f66b7052b26d90f19c242df7187a63 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Mon, 29 Jan 2024 15:51:36 +0100 Subject: [PATCH 23/74] add lifecycle badge "experimental" on package and function createBindingConstraint() --- DESCRIPTION | 5 ++- NAMESPACE | 1 + NEWS.md | 2 +- R/antaresEditObject-package.R | 7 ++++ README.md | 2 +- man/antaresEditObject-package.Rd | 46 +++++++++++++++++++++++ man/figures/lifecycle-archived.svg | 21 +++++++++++ man/figures/lifecycle-defunct.svg | 21 +++++++++++ man/figures/lifecycle-deprecated.svg | 21 +++++++++++ man/figures/lifecycle-experimental.svg | 21 +++++++++++ man/figures/lifecycle-maturing.svg | 21 +++++++++++ man/figures/lifecycle-questioning.svg | 21 +++++++++++ man/figures/lifecycle-soft-deprecated.svg | 21 +++++++++++ man/figures/lifecycle-stable.svg | 29 ++++++++++++++ man/figures/lifecycle-superseded.svg | 21 +++++++++++ 15 files changed, 256 insertions(+), 4 deletions(-) create mode 100644 R/antaresEditObject-package.R create mode 100644 man/antaresEditObject-package.Rd create mode 100644 man/figures/lifecycle-archived.svg create mode 100644 man/figures/lifecycle-defunct.svg create mode 100644 man/figures/lifecycle-deprecated.svg create mode 100644 man/figures/lifecycle-experimental.svg create mode 100644 man/figures/lifecycle-maturing.svg create mode 100644 man/figures/lifecycle-questioning.svg create mode 100644 man/figures/lifecycle-soft-deprecated.svg create mode 100644 man/figures/lifecycle-stable.svg create mode 100644 man/figures/lifecycle-superseded.svg diff --git a/DESCRIPTION b/DESCRIPTION index 26b1826f..22a5f4d7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -45,12 +45,13 @@ Imports: parallel, future, plyr, - yaml + yaml, + lifecycle Suggests: testthat, covr, knitr, rmarkdown VignetteBuilder: knitr -Remotes: +Remotes: rte-antares-rpackage/antaresRead@release/v8.7.0 diff --git a/NAMESPACE b/NAMESPACE index 933693d6..8c05f02c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -143,6 +143,7 @@ importFrom(httr,status_code) importFrom(httr,stop_for_status) importFrom(jsonlite,toJSON) importFrom(jsonlite,write_json) +importFrom(lifecycle,deprecated) importFrom(memuse,Sys.meminfo) importFrom(plyr,ldply) importFrom(plyr,llply) diff --git a/NEWS.md b/NEWS.md index acf64285..2bf82d89 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -# antaresRead 0.7.0 +# antaresRead 0.7.0 (devlopment) ### Breaking changes (Antares v8.7, cf. Antares v8.7 changelog) : diff --git a/R/antaresEditObject-package.R b/R/antaresEditObject-package.R new file mode 100644 index 00000000..425b3c1c --- /dev/null +++ b/R/antaresEditObject-package.R @@ -0,0 +1,7 @@ +#' @keywords internal +"_PACKAGE" + +## usethis namespace: start +#' @importFrom lifecycle deprecated +## usethis namespace: end +NULL diff --git a/README.md b/README.md index a3af37d6..f24b7df5 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ [![CRAN status](https://www.r-pkg.org/badges/version/antaresEditObject)](https://CRAN.R-project.org/package=antaresEditObject) [![cranlogs](https://cranlogs.r-pkg.org/badges/antaresEditObject)](https://cran.r-project.org/package=antaresEditObject) -[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable) +[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental) [![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active) [![R-CMD-check](https://github.com/rte-antares-rpackage/antaresEditObject/workflows/R-CMD-check/badge.svg)](https://github.com/rte-antares-rpackage/antaresEditObject/actions) [![Codecov test coverage](https://codecov.io/gh/rte-antares-rpackage/antaresEditObject/branch/master/graph/badge.svg)](https://app.codecov.io/gh/rte-antares-rpackage/antaresEditObject?branch=master) diff --git a/man/antaresEditObject-package.Rd b/man/antaresEditObject-package.Rd new file mode 100644 index 00000000..d31722e3 --- /dev/null +++ b/man/antaresEditObject-package.Rd @@ -0,0 +1,46 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/antaresEditObject-package.R +\docType{package} +\name{antaresEditObject-package} +\alias{antaresEditObject} +\alias{antaresEditObject-package} +\title{antaresEditObject: Edit an 'Antares' Simulation} +\description{ +Edit an 'Antares' simulation before running it : create new areas, links, thermal clusters or binding constraints or edit existing ones. Update 'Antares' general & optimization settings. 'Antares' is an open source power system generator, more information available here : \url{https://antares-simulator.org/}. +} +\seealso{ +Useful links: +\itemize{ + \item \url{https://github.com/rte-antares-rpackage/antaresEditObject} + \item \url{https://rte-antares-rpackage.github.io/antaresEditObject/} + \item Report bugs at \url{https://github.com/rte-antares-rpackage/antaresEditObject/issues} +} + +} +\author{ +\strong{Maintainer}: Tatiana Vargas \email{tatiana.vargas@rte-france.com} + +Authors: +\itemize{ + \item Frederic Breant + \item Victor Perrier +} + +Other contributors: +\itemize{ + \item Baptiste Seguinot [contributor] + \item Benoit Thieurmel [contributor] + \item Titouan Robert [contributor] + \item Jalal-Edine Zawam [contributor] + \item Etienne Sanchez [contributor] + \item Janus De Bondt [contributor] + \item Assil Mansouri [contributor] + \item Abdallah Mahoudi [contributor] + \item Clement Berthet [contributor] + \item Kamel Kemiha [contributor] + \item Nicolas Boitard [contributor] + \item RTE [copyright holder] +} + +} +\keyword{internal} diff --git a/man/figures/lifecycle-archived.svg b/man/figures/lifecycle-archived.svg new file mode 100644 index 00000000..745ab0c7 --- /dev/null +++ b/man/figures/lifecycle-archived.svg @@ -0,0 +1,21 @@ + + lifecycle: archived + + + + + + + + + + + + + + + lifecycle + + archived + + diff --git a/man/figures/lifecycle-defunct.svg b/man/figures/lifecycle-defunct.svg new file mode 100644 index 00000000..d5c9559e --- /dev/null +++ b/man/figures/lifecycle-defunct.svg @@ -0,0 +1,21 @@ + + lifecycle: defunct + + + + + + + + + + + + + + + lifecycle + + defunct + + diff --git a/man/figures/lifecycle-deprecated.svg b/man/figures/lifecycle-deprecated.svg new file mode 100644 index 00000000..b61c57c3 --- /dev/null +++ b/man/figures/lifecycle-deprecated.svg @@ -0,0 +1,21 @@ + + lifecycle: deprecated + + + + + + + + + + + + + + + lifecycle + + deprecated + + diff --git a/man/figures/lifecycle-experimental.svg b/man/figures/lifecycle-experimental.svg new file mode 100644 index 00000000..5d88fc2c --- /dev/null +++ b/man/figures/lifecycle-experimental.svg @@ -0,0 +1,21 @@ + + lifecycle: experimental + + + + + + + + + + + + + + + lifecycle + + experimental + + diff --git a/man/figures/lifecycle-maturing.svg b/man/figures/lifecycle-maturing.svg new file mode 100644 index 00000000..897370ec --- /dev/null +++ b/man/figures/lifecycle-maturing.svg @@ -0,0 +1,21 @@ + + lifecycle: maturing + + + + + + + + + + + + + + + lifecycle + + maturing + + diff --git a/man/figures/lifecycle-questioning.svg b/man/figures/lifecycle-questioning.svg new file mode 100644 index 00000000..7c1721d0 --- /dev/null +++ b/man/figures/lifecycle-questioning.svg @@ -0,0 +1,21 @@ + + lifecycle: questioning + + + + + + + + + + + + + + + lifecycle + + questioning + + diff --git a/man/figures/lifecycle-soft-deprecated.svg b/man/figures/lifecycle-soft-deprecated.svg new file mode 100644 index 00000000..9c166ff3 --- /dev/null +++ b/man/figures/lifecycle-soft-deprecated.svg @@ -0,0 +1,21 @@ + + lifecycle: soft-deprecated + + + + + + + + + + + + + + + lifecycle + + soft-deprecated + + diff --git a/man/figures/lifecycle-stable.svg b/man/figures/lifecycle-stable.svg new file mode 100644 index 00000000..9bf21e76 --- /dev/null +++ b/man/figures/lifecycle-stable.svg @@ -0,0 +1,29 @@ + + lifecycle: stable + + + + + + + + + + + + + + + + lifecycle + + + + stable + + + diff --git a/man/figures/lifecycle-superseded.svg b/man/figures/lifecycle-superseded.svg new file mode 100644 index 00000000..db8d757f --- /dev/null +++ b/man/figures/lifecycle-superseded.svg @@ -0,0 +1,21 @@ + + lifecycle: superseded + + + + + + + + + + + + + + + lifecycle + + superseded + + From 2f864b15bbceacfcb6dede704cf37628f053bfe2 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Mon, 29 Jan 2024 15:52:39 +0100 Subject: [PATCH 24/74] rework createBindingConstraint() and private function .group_values_check() to manage values check dimension --- R/createBindingConstraint.R | 137 ++++++++-- man/createBindingConstraint.Rd | 1 + man/group_values_check.Rd | 11 +- tests/testthat/test-createBindingConstraint.R | 251 +++++++----------- 4 files changed, 227 insertions(+), 173 deletions(-) diff --git a/R/createBindingConstraint.R b/R/createBindingConstraint.R index dddc9ddd..d64750bc 100644 --- a/R/createBindingConstraint.R +++ b/R/createBindingConstraint.R @@ -1,7 +1,8 @@ #' @title Create a binding constraint #' #' @description -#' `r antaresEditObject:::badge_api_ok()` +#' `r antaresEditObject:::badge_api_ok()` +#' `r lifecycle::badge("experimental")` #' #' Create a new binding constraint in an Antares study. #' `createBindingConstraintBulk()` allow to create multiple constraints at once. @@ -122,20 +123,26 @@ createBindingConstraint <- function(name, # v870 if(opts$antaresVersion>=870){ if(is.null(group)) - group <- "default group" + group <- "default" if(!is.null(values)){ assertthat::assert_that(inherits(values, "list")) - if(!all(names(values)%in%c("lt", "gt", "eq"))) + if(!all(c("lt", "gt", "eq")%in%names(values))) stop("Put for 'values' argument, named 'list' => see Doc `?createBindingConstraint`") + + # v870 : check group and values + # no check for add BC with NULL values + group_values_check(group_value = group, + values_data = values, + operator_check = operator, + opts = opts) } - - # v870 : check group and values - group_values_check(group_value = group, - values_data = values, - opts = opts) } + ## + # build properties + write values + ## + bindingConstraints <- createBindingConstraint_( bindingConstraints, name, @@ -292,37 +299,108 @@ createBindingConstraint_ <- function(bindingConstraints, #' @description Only needed for study version >= 870 #' @param group_value `character` name of group #' @param values_data `list` values used by the constraint +#' @param operator_check `character` parameter "operator" +#' @return NULL if it's new group to add or error exceptions with dimension control #' @template opts #' @export #' @keywords internal group_values_check <- function(group_value, values_data, + operator_check, opts = antaresRead::simOptions()){ - + ## + # check "values" according to "operator" + ## + values_operator <- switch(operator_check, + less = "lt", + equal = "eq", + greater = "gt", + both = c("lt", "gt")) + + if(!all(values_operator %in% names(values_data))) + stop(paste( + "you must provide a list named according your parameter 'operator' : ", + "'", + operator_check, + "'", + " with "), + paste(values_operator, collapse = " "), + call. = FALSE) + # read existing binding constraint + # /!\/!\ function return "default values" (vector of 0) existing_bc <- readBindingConstraints(opts = opts) # study with no BC or virgin study if(is.null(existing_bc)) return() - # check existing group Versus new group to create - existing_groups <- unlist(lapply(existing_bc, `[[`, "group")) - search_group <- grep(pattern = group_value, x = existing_groups) - index_group <- search_group[length(search_group)] + ## + # group creation + ## + + # check existing group Versus new group + existing_groups <- unlist( + lapply(existing_bc, + function(x){ + x[["properties"]][["group"]]}) + ) + search_group_index <- grep(pattern = group_value, + x = existing_groups) + + # new group ? + new_group <- identical(search_group_index, + integer(0)) + if(new_group) + message("New group ", "'", group_value, "'", " will be created") # check dimension values existing group Versus new group - if( !identical(index_group, integer(0)) ){ - p_col <- dim(existing_bc[[index_group]]$values[[1]])[2] - p_col_new <- dim(values_data[[1]])[2] - - if(is.null(p_col_new)) - p_col_new <- 0 + if(!new_group){ + # check dimension of existing group + p_col <- sapply(existing_bc[search_group_index], + function(x){ + op <- x[["properties"]][["operator"]] + if(!op %in%"both") + dim(x[["values"]])[2] + else{ + lt_dim <- dim(x[["values"]][["less"]])[2] + gt_dim <- dim(x[["values"]][["greater"]])[2] + if(lt_dim!=gt_dim) + stop("dimension of values are not similar for constraint : ", + x$properties$id, call. = FALSE) + lt_dim + } + }) + + # keep dimension >1 + names(p_col) <- NULL + if(identical(p_col[p_col>1], + integer(0))){ + message("actual dimension of group : ", group_value, " is NULL or 1") + return(NULL) # continue process to write data + }else + p_col <- unique(p_col[p_col>1]) + message("actual dimension of group : ", group_value, " is ", p_col) + + # check dimension of new group + if(operator_check%in%"both"){ + lt_dim <- dim(values_data$lt)[2] + gt_dim <- dim(values_data$gt)[2] + if(lt_dim!=gt_dim) + stop("dimension of values are not similar ", + call. = FALSE) + p_col_new <- lt_dim + }else + p_col_new <- dim(values_data[[values_operator]])[2] + + # # no values provided + # if(is.null(p_col_new)) + # p_col_new <- 0 if(p_col!=p_col_new) # & p_col!=0 stop(paste0("Put right columns dimension : ", p_col, " for existing 'group' : ", - group_value)) + group_value), call. = FALSE) } } @@ -446,6 +524,25 @@ createBindingConstraintBulk <- function(constraints, +constructor_binding_values <- function(lt = NULL, gt = NULL, eq = NULL){ + # # check args + # args <- sapply(match.call()[-1], + # FUN = is.null) + # + # if(!any(args)){ + # args <- sapply(match.call()[-1], + # FUN = is.numeric) + # + # values <- sapply(match.call()[-1], + # FUN = as.data.frame) + # } + + list(lt = lt, + gt = gt, + eq = eq) + + +} diff --git a/man/createBindingConstraint.Rd b/man/createBindingConstraint.Rd index 1be609cb..fd939714 100644 --- a/man/createBindingConstraint.Rd +++ b/man/createBindingConstraint.Rd @@ -57,6 +57,7 @@ An updated list containing various information about the simulation. } \description{ \ifelse{html}{\figure{badge_api_ok.svg}{options: alt='Antares API OK'}}{Antares API: \strong{OK}} +\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}} Create a new binding constraint in an Antares study. \code{createBindingConstraintBulk()} allow to create multiple constraints at once. diff --git a/man/group_values_check.Rd b/man/group_values_check.Rd index 958a6042..09b2c938 100644 --- a/man/group_values_check.Rd +++ b/man/group_values_check.Rd @@ -4,17 +4,26 @@ \alias{group_values_check} \title{Check dimension of time series for binding constraints} \usage{ -group_values_check(group_value, values_data, opts = antaresRead::simOptions()) +group_values_check( + group_value, + values_data, + operator_check, + opts = antaresRead::simOptions() +) } \arguments{ \item{group_value}{\code{character} name of group} \item{values_data}{\code{list} values used by the constraint} +\item{operator_check}{\code{character} parameter "operator"} + \item{opts}{List of simulation parameters returned by the function \code{\link[antaresRead:setSimulationPath]{antaresRead::setSimulationPath()}}} } \value{ +NULL if it's new group to add or error exceptions with dimension control + An updated list containing various information about the simulation. } \description{ diff --git a/tests/testthat/test-createBindingConstraint.R b/tests/testthat/test-createBindingConstraint.R index 09a0d72d..81951c87 100644 --- a/tests/testthat/test-createBindingConstraint.R +++ b/tests/testthat/test-createBindingConstraint.R @@ -216,17 +216,21 @@ eq_data <- matrix(data = rep(3, 365 * n), ncol = n) scenar_values_daily <- list(lt= lt_data, gt= gt_data, eq= eq_data) -testthat::skip() + +## parameter group ---- + +## parameter values ---- + +## add default bc ---- test_that("createBindingConstraint (default group value) v8.7", { - - # create binding constraint + ### with no values ---- createBindingConstraint( name = "myconstraint", - values = scenar_values, + values = NULL, enabled = FALSE, timeStep = "hourly", operator = "both", - coefficients = c("al%gr" = 1), + coefficients = c("at%fr" = 1), opts = opts_test ) @@ -234,192 +238,135 @@ test_that("createBindingConstraint (default group value) v8.7", { # tests testthat::expect_true("myconstraint" %in% - names(bc)) - - testthat::expect_equal(bc$myconstraint$group, "default group") - - -}) - - -test_that("createBindingConstraintBulk v8.7", { - # Prepare data for constraints - bindings_constraints <- lapply( - X = seq_len(10), - FUN = function(i) { - # use arguments of createBindingConstraint() - # all arguments must be provided ! - list( - name = paste0("constraints", i), - id = paste0("constraints", i), - values = scenar_values, - enabled = FALSE, - timeStep = "hourly", - operator = "both", - coefficients = c("al%gr" = 1), - group= "groupv870", - overwrite = TRUE - ) - } - ) - # create all constraints - createBindingConstraintBulk(bindings_constraints, opts = opts_test) - - # tests - testthat::expect_true("constraints1" %in% - names(readBindingConstraints(opts = opts_test))) - testthat::expect_true("constraints10" %in% - names(readBindingConstraints(opts = opts_test))) - -}) - - -test_that("createBindingConstraint check group values v8.7", { - - # create binding constraint (default group value) - createBindingConstraint( - name = "myconstraint_group", - values = scenar_values, - enabled = FALSE, - timeStep = "hourly", - operator = "both", - coefficients = c("al%gr" = 1), - opts = opts_test - ) + names(bc)) + testthat::expect_equal(bc$myconstraint$properties$group, "default") - # ADD binding constraint still (default group value) + ### with values ---- createBindingConstraint( - name = "myconstraint_group_bis", + name = "myconstraint2", values = scenar_values, enabled = FALSE, timeStep = "hourly", operator = "both", - coefficients = c("al%gr" = 1), + coefficients = c("at%fr" = 1), opts = opts_test ) - # ADD binding constraint with named group - createBindingConstraint( - name = "myconstraint_group1", - values = scenar_values_daily, - enabled = FALSE, - timeStep = "daily", - operator = "both", - group = "group_test", - coefficients = c("al%gr" = 1), - opts = opts_test - ) + bc <- readBindingConstraints(opts = opts_test) # tests - testthat::expect_true( - all( - c("myconstraint_group", "myconstraint_group_bis", "myconstraint_group1") %in% - names(readBindingConstraints(opts = opts_test)) - ) - ) + testthat::expect_true("myconstraint2" %in% + names(bc)) + testthat::expect_equal(bc$myconstraint2$properties$group, "default") - # create binding constraint with bad values of existing group (default group) [ERROR] + ### error dim ---- + # add BC with daily values (different columns dimension ERROR) testthat::expect_error( createBindingConstraint( - name = "myconstraint_group_err", + name = "myconstraint_daily", values = scenar_values_daily, enabled = FALSE, timeStep = "daily", operator = "both", - coefficients = c("al%gr" = 1), + coefficients = c("at%fr" = 1), opts = opts_test - ) + ), regexp = "Put right columns dimension" ) - # create binding constraint with bad values (NULL) of existing group (default group) [ERROR] +}) + +## existing named group ---- + # study provide BC with group "group_test" +test_that("createBindingConstraint with existing group v8.7", { + testthat::skip() + + # read to have dimension + bc <- readBindingConstraints() + + # dimension according BC/group + dim_bc <- lapply(bc, function(x){ + if(x$properties$operator %in% "both") + list(group = x$properties$group, + dim_values = dim(x$values$less)[2]) + else + list(group = x$properties$group, + dim_values = dim(x$values)[2]) + }) + + existing_name_group <- dim_bc$bc_1$group + dim_existing_group <- dim_bc$bc_1$dim_values + + # ADD binding constraint with bad dimension testthat::expect_error( createBindingConstraint( - name = "myconstraint_group_err", - values = NULL, + name = "myconstraint_group1", + values = scenar_values_daily, enabled = FALSE, timeStep = "daily", - operator = "both", - coefficients = c("al%gr" = 1), + operator = "both", + group = existing_name_group, + coefficients = c("at%fr" = 1), opts = opts_test - ) + ), regexp = "Put right columns dimension" ) - # create binding constraint with bad values of existing group [ERROR] - testthat::expect_error( - createBindingConstraint( - name = "myconstraint_group2", - values = scenar_values, - enabled = FALSE, - timeStep = "hourly", - operator = "both", - group = "group_test", - coefficients = c("al%gr" = 1), - opts = opts_test) - ) + # put right dimension + data_ok <- list(lt=NULL, + gt=NULL, + eq=NULL) + data_ok$lt <- scenar_values_daily$lt[,1:dim_existing_group] + data_ok$gt <- scenar_values_daily$gt[,1:dim_existing_group] - # ADD binding constraint with existing group + # ADD binding constraint with bad dimension createBindingConstraint( - name = "myconstraint_group2", - values = scenar_values_daily, + name = "bc_existing_group", + values = data_ok, enabled = FALSE, timeStep = "daily", operator = "both", - group = "group_test", - coefficients = c("al%gr" = 1), - opts = opts_test) - - bc <- readBindingConstraints(opts = opts_test) - - testthat::expect_true("myconstraint_group2" %in% - names(readBindingConstraints(opts = opts_test))) + group = existing_name_group, + coefficients = c("at%fr" = 1), + opts = opts_test + ) - testthat::expect_equal(bc[["myconstraint_group2"]]$group, "group_test") + }) -test_that("createBindingConstraint check values (NULL) group v8.7", { - # create binding constraint (NULL value) - createBindingConstraint( - name = "myconstraint_group_NULL", - values = NULL, - enabled = FALSE, - operator = "both", - group = "null_values", - coefficients = c("al%gr" = 1), - opts = opts_test - ) - - bc <- readBindingConstraints(opts = opts_test) - - # check name - testthat::expect_true("myconstraint_group_NULL" %in% - names(bc)) - - # check dim - dim_values <- unlist(lapply(bc$myconstraint_group_NULL$values, - function(x) dim(x)[2])) - - testthat::expect_equal(sum(dim_values), 0) - - # ADD binding constraint (existing group with NULL value) - createBindingConstraint( - name = "myconstraint_group_3", - values = NULL, - enabled = FALSE, - operator = "both", - timeStep = "hourly", - group = "null_values", - coefficients = c("al%gr" = 1), - opts = opts_test +## bulk ---- +test_that("createBindingConstraintBulk v8.7", { + testthat::skip() + # Prepare data for constraints + bindings_constraints <- lapply( + X = seq_len(10), + FUN = function(i) { + # use arguments of createBindingConstraint() + # all arguments must be provided ! + list( + name = paste0("constraints", i), + id = paste0("constraints", i), + values = scenar_values, + enabled = FALSE, + timeStep = "hourly", + operator = "both", + coefficients = c("al%gr" = 1), + group= "groupv870", + overwrite = TRUE + ) + } ) - - bc <- readBindingConstraints(opts = opts_test) + # create all constraints + createBindingConstraintBulk(bindings_constraints, opts = opts_test) - # check name - testthat::expect_true("myconstraint_group_3" %in% - names(bc)) + # tests + testthat::expect_true("constraints1" %in% + names(readBindingConstraints(opts = opts_test))) + testthat::expect_true("constraints10" %in% + names(readBindingConstraints(opts = opts_test))) - # remove temporary study - unlink(x = study_latest_version, recursive = TRUE) }) + + +# remove temporary study ---- +unlink(x = study_latest_version, recursive = TRUE) From 5b5ccd2b9be820212b7428d70afc069e498efabb Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Fri, 2 Feb 2024 16:06:21 +0100 Subject: [PATCH 25/74] createBindingConstraint() major update to be compatible with scenarized binding constraints+ doc + tests --- R/createBindingConstraint.R | 135 +++++++++--- man/createBindingConstraint.Rd | 51 ++++- man/group_values_check.Rd | 3 + tests/testthat/test-createBindingConstraint.R | 194 +++++++++++++++--- 4 files changed, 323 insertions(+), 60 deletions(-) diff --git a/R/createBindingConstraint.R b/R/createBindingConstraint.R index d64750bc..22ec1ec8 100644 --- a/R/createBindingConstraint.R +++ b/R/createBindingConstraint.R @@ -32,7 +32,8 @@ #' **< v8.7.0** : For each constraint name, a .txt file containing 3 time series `"less", "greater", "equal"` #' #' **>= v8.7.0** : For each constraint name, one file .txt containing `_lt.txt, _gt.txt, _eq.txt` -#' Parameter `values` must be named `list` ("lt", "gt", "eq") containing `data.frame` scenarized +#' Parameter `values` must be named `list` ("lt", "gt", "eq") containing `data.frame` scenarized. +#' see example section below. #' #' @export #' @@ -43,6 +44,7 @@ #' #' @examples #' \dontrun{ +#' # < v8.7.0 : #' createBindingConstraint( #' name = "myconstraint", #' values = matrix(data = rep(0, 8760 * 3), ncol = 3), @@ -74,6 +76,53 @@ #' ) #' # create all constraints #' createBindingConstraintBulk(bindings_constraints) +#' +#' # >= v8.7.0 : +#' +#' # values are now named list containing `data.frame` according to +#' # `operator` parameter (for "less", build a list with at least "lt" floor in list) +#' +#' # data values (daily) +#' df <- matrix(data = rep(0, 8760 * 3), ncol = 3) +#' values_data <- list(lt=df) +#' +#' # create bc with minimum value +#' createBindingConstraint(name = "bc_example", +#' operator = "less", +#' values = values_data, +#' overwrite = TRUE) +#' +#' # or you can provide list data with all value +#' values_data <- list(lt=df, +#' gt= df, +#' eq= df) +#' +#' createBindingConstraint(name = "bc_example", +#' operator = "less", +#' values = values_data, +#' overwrite = TRUE) +#' +#' # create multiple constraints +#' bindings_constraints <- lapply( +#' X = seq_len(10), +#' FUN = function(i) { +#' # use arguments of createBindingConstraint() +#' # all arguments must be provided ! +#' list( +#' name = paste0("constraints_bulk", i), +#' id = paste0("constraints_bulk", i), +#' values = values_data, +#' enabled = FALSE, +#' timeStep = "hourly", +#' operator = "both", +#' coefficients = c("at%fr" = 1), +#' group= "group_bulk", +#' overwrite = TRUE +#' ) +#' } +#' ) +#' +#' createBindingConstraintBulk(bindings_constraints) #' } createBindingConstraint <- function(name, id = tolower(name), @@ -125,16 +174,35 @@ createBindingConstraint <- function(name, if(is.null(group)) group <- "default" + values_operator <- switch(operator, + less = "lt", + equal = "eq", + greater = "gt", + both = c("lt", "gt")) + if(!is.null(values)){ assertthat::assert_that(inherits(values, "list")) - if(!all(c("lt", "gt", "eq")%in%names(values))) - stop("Put for 'values' argument, named 'list' => see Doc `?createBindingConstraint`") + # if(!all(c("lt", "gt", "eq")%in%names(values))) + # stop("Put for 'values' argument, + # named 'list' => see Doc `?createBindingConstraint`") + + ## + # check "values" according to "operator" + ## + if(!all(values_operator %in% names(values))) + stop(paste0( + "you must provide a list named according your parameter 'operator' : ", + "'", operator, "'", + " with "), + paste0("'", values_operator, "'", collapse = " "), + call. = FALSE) # v870 : check group and values - # no check for add BC with NULL values + # no check for add BC with NULL values group_values_check(group_value = group, values_data = values, operator_check = operator, + output_operator = values_operator, opts = opts) } } @@ -157,6 +225,7 @@ createBindingConstraint <- function(name, group, overwrite, links = antaresRead::getLinks(opts = opts, namesOnly = TRUE), + output_operator = values_operator, opts = opts ) @@ -186,6 +255,7 @@ createBindingConstraint_ <- function(bindingConstraints, group, overwrite, links, + output_operator = NULL, opts) { # Get ids and check if not already exist @@ -245,11 +315,15 @@ createBindingConstraint_ <- function(bindingConstraints, } } } - + + # check when overwrite element in list + # names of bindingConstraints are provided by R automatically indexBC <- as.character(length(bindingConstraints)) if (indexBC %in% names(bindingConstraints)) { indexBC <- as.character(max(as.numeric(names(bindingConstraints))) + 1) } + + # add new bc to write then in .ini file bindingConstraints[[indexBC]] <- c(iniParams, coefficients) ## Values @@ -261,8 +335,8 @@ createBindingConstraint_ <- function(bindingConstraints, # Write values # v870 if(opts$antaresVersion>=870){ - names_order_ts <- c("lt", "gt", "eq") - name_file <- paste0(id, "_", names_order_ts, ".txt") + # names_order_ts <- c("lt", "gt", "eq") + name_file <- paste0(id, "_", output_operator, ".txt") up_path <- file.path(opts$inputPath, "bindingconstraints", name_file) @@ -300,32 +374,26 @@ createBindingConstraint_ <- function(bindingConstraints, #' @param group_value `character` name of group #' @param values_data `list` values used by the constraint #' @param operator_check `character` parameter "operator" +#' @param output_operator `character` for #' @return NULL if it's new group to add or error exceptions with dimension control #' @template opts -#' @export +#' @export #' @keywords internal group_values_check <- function(group_value, values_data, operator_check, + output_operator, opts = antaresRead::simOptions()){ - ## - # check "values" according to "operator" - ## - values_operator <- switch(operator_check, - less = "lt", - equal = "eq", - greater = "gt", - both = c("lt", "gt")) - - if(!all(values_operator %in% names(values_data))) - stop(paste( - "you must provide a list named according your parameter 'operator' : ", - "'", - operator_check, - "'", - " with "), - paste(values_operator, collapse = " "), - call. = FALSE) + + # no check if col dim ==1 + if(operator_check%in%"both"){ + if(dim(values_data$lt)[2] <= 1) + return() + }else{ + if(dim(values_data[[output_operator]])[2] <= 1) + return() + } + # read existing binding constraint # /!\/!\ function return "default values" (vector of 0) @@ -391,7 +459,7 @@ group_values_check <- function(group_value, call. = FALSE) p_col_new <- lt_dim }else - p_col_new <- dim(values_data[[values_operator]])[2] + p_col_new <- dim(values_data[[output_operator]])[2] # # no values provided # if(is.null(p_col_new)) @@ -490,7 +558,7 @@ group_values_check <- function(group_value, } - +#' `r lifecycle::badge("experimental")` #' @param constraints A `list` of several named `list` containing data to create binding constraints. #' **Warning** all arguments for creating a binding constraints must be provided, see examples. #' @export @@ -503,13 +571,22 @@ createBindingConstraintBulk <- function(constraints, pathIni <- file.path(opts$inputPath, "bindingconstraints/bindingconstraints.ini") bindingConstraints <- readIniFile(pathIni, stringsAsFactors = FALSE) + + for (i in seq_along(constraints)) { + values_operator <- switch(constraints[[i]]$operator, + less = "lt", + equal = "eq", + greater = "gt", + both = c("lt", "gt")) + bindingConstraints <- do.call("createBindingConstraint_", c( constraints[[i]], list( opts = opts, bindingConstraints = bindingConstraints, - links = antaresRead::getLinks(opts = opts, namesOnly = TRUE) + links = antaresRead::getLinks(opts = opts, namesOnly = TRUE), + output_operator = values_operator ) )) } diff --git a/man/createBindingConstraint.Rd b/man/createBindingConstraint.Rd index fd939714..d24054b2 100644 --- a/man/createBindingConstraint.Rd +++ b/man/createBindingConstraint.Rd @@ -68,10 +68,12 @@ According to Antares version, usage may vary : \strong{< v8.7.0} : For each constraint name, a .txt file containing 3 time series \verb{"less", "greater", "equal"} \strong{>= v8.7.0} : For each constraint name, one file .txt containing \verb{_lt.txt, _gt.txt, _eq.txt} -Parameter \code{values} must be named \code{list} ("lt", "gt", "eq") containing \code{data.frame} scenarized +Parameter \code{values} must be named \code{list} ("lt", "gt", "eq") containing \code{data.frame} scenarized. +see example section below. } \examples{ \dontrun{ +# < v8.7.0 : createBindingConstraint( name = "myconstraint", values = matrix(data = rep(0, 8760 * 3), ncol = 3), @@ -103,6 +105,53 @@ bindings_constraints <- lapply( ) # create all constraints createBindingConstraintBulk(bindings_constraints) + +# >= v8.7.0 : + +# values are now named list containing `data.frame` according to + # `operator` parameter (for "less", build a list with at least "lt" floor in list) + +# data values (daily) +df <- matrix(data = rep(0, 8760 * 3), ncol = 3) +values_data <- list(lt=df) + +# create bc with minimum value +createBindingConstraint(name = "bc_example", + operator = "less", + values = values_data, + overwrite = TRUE) + +# or you can provide list data with all value +values_data <- list(lt=df, + gt= df, + eq= df) + +createBindingConstraint(name = "bc_example", + operator = "less", + values = values_data, + overwrite = TRUE) + +# create multiple constraints +bindings_constraints <- lapply( + X = seq_len(10), + FUN = function(i) { + # use arguments of createBindingConstraint() + # all arguments must be provided ! + list( + name = paste0("constraints_bulk", i), + id = paste0("constraints_bulk", i), + values = values_data, + enabled = FALSE, + timeStep = "hourly", + operator = "both", + coefficients = c("at\%fr" = 1), + group= "group_bulk", + overwrite = TRUE + ) + } +) + +createBindingConstraintBulk(bindings_constraints) } } \seealso{ diff --git a/man/group_values_check.Rd b/man/group_values_check.Rd index 09b2c938..1e4e1899 100644 --- a/man/group_values_check.Rd +++ b/man/group_values_check.Rd @@ -8,6 +8,7 @@ group_values_check( group_value, values_data, operator_check, + output_operator, opts = antaresRead::simOptions() ) } @@ -18,6 +19,8 @@ group_values_check( \item{operator_check}{\code{character} parameter "operator"} +\item{output_operator}{\code{character} for} + \item{opts}{List of simulation parameters returned by the function \code{\link[antaresRead:setSimulationPath]{antaresRead::setSimulationPath()}}} } diff --git a/tests/testthat/test-createBindingConstraint.R b/tests/testthat/test-createBindingConstraint.R index 81951c87..09cab3c3 100644 --- a/tests/testthat/test-createBindingConstraint.R +++ b/tests/testthat/test-createBindingConstraint.R @@ -186,17 +186,6 @@ opts_test <- antaresRead::setSimulationPath(study_latest_version, "input") # areas list antaresRead::getAreas(opts = opts_test) - # # remove BC none v870 - # names_bc_to_remove <- names(readBindingConstraints(opts = opts_test)) - # - # lapply(names_bc_to_remove, - # removeBindingConstraint, - # opts = simOptions()) - # - # # temporary to test with "870" - # # force version - # opts_test$antaresVersion <- 870 - # scenarized data hourly n <- 10 lt_data <- matrix(data = rep(1, 8760 * n), ncol = n) @@ -217,20 +206,53 @@ scenar_values_daily <- list(lt= lt_data, gt= gt_data, eq= eq_data) -## parameter group ---- - ## parameter values ---- +test_that("createBindingConstraint with bad structure values object v8.7", { + + # less + bad_values <- scenar_values_daily[c("eq", "gt")] + + testthat::expect_error( + createBindingConstraint( + name = "bad_values", + values = bad_values, + enabled = FALSE, + timeStep = "daily", + operator = "less", + coefficients = c("at%fr" = 1), + opts = opts_test + ), regexp = "you must provide a list named according your parameter" + ) + + # both + bad_values <- scenar_values_daily["eq"] + + testthat::expect_error( + createBindingConstraint( + name = "bad_values", + values = bad_values, + enabled = FALSE, + timeStep = "daily", + operator = "both", + coefficients = c("at%fr" = 1), + opts = opts_test + ), regexp = "you must provide a list named according your parameter" + ) + +}) ## add default bc ---- test_that("createBindingConstraint (default group value) v8.7", { ### with no values ---- + # /!\/!\/!\ output .txt file has to be empty createBindingConstraint( name = "myconstraint", values = NULL, enabled = FALSE, timeStep = "hourly", operator = "both", - coefficients = c("at%fr" = 1), + coefficients = c("fr%at" = 1), + overwrite = TRUE, opts = opts_test ) @@ -239,7 +261,23 @@ test_that("createBindingConstraint (default group value) v8.7", { # tests testthat::expect_true("myconstraint" %in% names(bc)) - testthat::expect_equal(bc$myconstraint$properties$group, "default") + testthat::expect_equal(bc$myconstraint$properties$group, + "default") + + # for both + operator_bc <- c("_lt", "_gt") + path_bc <- file.path(opts_test$inputPath, "bindingconstraints") + path_file_bc <- paste0(file.path(path_bc, "myconstraint"), + operator_bc, ".txt") + + # read .txt + res <- lapply(path_file_bc, + antaresRead:::fread_antares, + opts = opts_test) + + res <- unlist(res) + + testthat::expect_equal(res, NULL) ### with values ---- createBindingConstraint( @@ -258,9 +296,11 @@ test_that("createBindingConstraint (default group value) v8.7", { testthat::expect_true("myconstraint2" %in% names(bc)) testthat::expect_equal(bc$myconstraint2$properties$group, "default") + testthat::expect_equal(dim(scenar_values$lt)[2], + dim(bc$myconstraint2$values$less)[2]) ### error dim ---- - # add BC with daily values (different columns dimension ERROR) + # add BC with daily values (different columns dimension ERROR) testthat::expect_error( createBindingConstraint( name = "myconstraint_daily", @@ -278,7 +318,6 @@ test_that("createBindingConstraint (default group value) v8.7", { ## existing named group ---- # study provide BC with group "group_test" test_that("createBindingConstraint with existing group v8.7", { - testthat::skip() # read to have dimension bc <- readBindingConstraints() @@ -291,7 +330,7 @@ test_that("createBindingConstraint with existing group v8.7", { else list(group = x$properties$group, dim_values = dim(x$values)[2]) - }) + }) existing_name_group <- dim_bc$bc_1$group dim_existing_group <- dim_bc$bc_1$dim_values @@ -311,13 +350,11 @@ test_that("createBindingConstraint with existing group v8.7", { ) # put right dimension - data_ok <- list(lt=NULL, - gt=NULL, - eq=NULL) + data_ok <- list() data_ok$lt <- scenar_values_daily$lt[,1:dim_existing_group] data_ok$gt <- scenar_values_daily$gt[,1:dim_existing_group] - # ADD binding constraint with bad dimension + # ADD binding constraint with good dimension createBindingConstraint( name = "bc_existing_group", values = data_ok, @@ -329,14 +366,111 @@ test_that("createBindingConstraint with existing group v8.7", { opts = opts_test ) + bc <- readBindingConstraints(opts = opts_test) + + # tests + testthat::expect_true("bc_existing_group" %in% + names(bc)) + testthat::expect_equal(bc$bc_existing_group$properties$group, + existing_name_group) + testthat::expect_equal(dim(data_ok$lt)[2], + dim(bc$bc_existing_group$values$less)[2]) + +}) - +## add new group ---- +testthat::test_that("createBindingConstraint with new group v8.7",{ + + # add values with the following steps + # NULL => 1 column => >1 column => 1 column => NULL + # error case with dimension different + + # ADD binding with NULL values + createBindingConstraint( + name = "bc_new_group_NULL", + values = NULL, + enabled = FALSE, + timeStep = "hourly", + operator = "greater", + group = "new_group", + coefficients = c("at%fr" = 1), + opts = opts_test + ) + + # ADD binding with 1 col + df_one_col <- scenar_values["lt"] + df_one_col$lt <- df_one_col$lt[,1, drop = FALSE] + + createBindingConstraint( + name = "bc_new_group_1", + values = df_one_col, + enabled = FALSE, + timeStep = "hourly", + operator = "less", + group = "new_group", + coefficients = c("at%fr" = 1), + opts = opts_test + ) + + # ADD binding with multi cols + df_one_col <- scenar_values["lt"] + df_one_col$lt <- df_one_col$lt[,1:3, drop = FALSE] + + createBindingConstraint( + name = "bc_new_group_multi", + values = df_one_col, + enabled = FALSE, + timeStep = "hourly", + operator = "less", + group = "new_group", + coefficients = c("at%fr" = 1), + opts = opts_test + ) + + # ADD binding with 1 col + df_one_col <- scenar_values["lt"] + df_one_col$lt <- df_one_col$lt[,1, drop = FALSE] + + createBindingConstraint( + name = "bc_new_group_1_bis", + values = df_one_col, + enabled = FALSE, + timeStep = "hourly", + operator = "less", + group = "new_group", + coefficients = c("at%fr" = 1), + opts = opts_test + ) + + # ADD binding with NULL values + createBindingConstraint( + name = "bc_new_group_NULL_bis", + values = NULL, + enabled = FALSE, + timeStep = "hourly", + operator = "greater", + group = "new_group", + coefficients = c("at%fr" = 1), + opts = opts_test + ) + + # ADD binding with NULL values + createBindingConstraint( + name = "bc_new_group_NULL_bis_both", + values = NULL, + enabled = FALSE, + timeStep = "hourly", + operator = "both", + group = "new_group", + coefficients = c("at%fr" = 1), + opts = opts_test + ) + }) ## bulk ---- test_that("createBindingConstraintBulk v8.7", { - testthat::skip() # Prepare data for constraints bindings_constraints <- lapply( X = seq_len(10), @@ -344,14 +478,14 @@ test_that("createBindingConstraintBulk v8.7", { # use arguments of createBindingConstraint() # all arguments must be provided ! list( - name = paste0("constraints", i), - id = paste0("constraints", i), + name = paste0("constraints_bulk", i), + id = paste0("constraints_bulk", i), values = scenar_values, enabled = FALSE, timeStep = "hourly", operator = "both", - coefficients = c("al%gr" = 1), - group= "groupv870", + coefficients = c("at%fr" = 1), + group= "group_bulk", overwrite = TRUE ) } @@ -360,9 +494,9 @@ test_that("createBindingConstraintBulk v8.7", { createBindingConstraintBulk(bindings_constraints, opts = opts_test) # tests - testthat::expect_true("constraints1" %in% + testthat::expect_true("constraints_bulk1" %in% names(readBindingConstraints(opts = opts_test))) - testthat::expect_true("constraints10" %in% + testthat::expect_true("constraints_bulk10" %in% names(readBindingConstraints(opts = opts_test))) }) From fe7746e1117ed31defbe3fcfdd3be9eaeacccfc9 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Fri, 2 Feb 2024 16:07:22 +0100 Subject: [PATCH 26/74] createStudy() add test for v8.7.0 deleteStudy() add test for v8.7.0 --- tests/testthat/test-createStudy.R | 34 ++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-createStudy.R b/tests/testthat/test-createStudy.R index 1fa89f10..2112a313 100644 --- a/tests/testthat/test-createStudy.R +++ b/tests/testthat/test-createStudy.R @@ -1,5 +1,19 @@ #Copyright © 2019 RTE Réseau de transport d’électricité +# create ---- + + ## v8.7.0---- +test_that("Create a new v8.7.0 study", { + path <- file.path(tempdir(), "tests_createStudy") + suppressWarnings( + opts <- createStudy(path, antares_version = "8.7.0") + ) + properties <- antaresRead:::readIniFile(file.path(path, "study.antares")) + expect_identical(properties$antares$version, 870L) + unlink(path, recursive = TRUE) +}) + + ## v8.6.0---- test_that("Create a new v8.6.0 study", { path <- file.path(tempdir(), "tests_createStudy") suppressWarnings( @@ -13,6 +27,7 @@ test_that("Create a new v8.6.0 study", { unlink(path, recursive = TRUE) }) + ## v8.1.0---- test_that("Create a new v8.1.0 study", { path <- file.path(tempdir(), "tests_createStudy") suppressWarnings( @@ -24,6 +39,7 @@ test_that("Create a new v8.1.0 study", { unlink(path, recursive = TRUE) }) + ## v7.0.0---- test_that("Create a new v7 study", { path <- file.path(tempdir(), "tests_createStudy") suppressWarnings( @@ -34,6 +50,7 @@ test_that("Create a new v7 study", { unlink(path, recursive = TRUE) }) + ## v6.0.0---- test_that("Create a new v6 study", { path <- file.path(tempdir(), "tests_createStudy") suppressWarnings( @@ -44,7 +61,22 @@ test_that("Create a new v6 study", { unlink(path, recursive = TRUE) }) +# delete ---- + ## v8.7.0---- +test_that("delete v8.7.0", { + path <- file.path(tempdir(), "tests_createStudy") + suppressWarnings( + opts <- createStudy(path, antares_version = "8.7.0") + ) + properties <- antaresRead:::readIniFile(file.path(path, + "study.antares")) + expect_identical(properties$antares$version, 870L) + deleteStudy(opts = simOptions()) + testthat::expect_true(!file.exists(opts$studyPath)) +}) + + ## v8.1.0---- test_that("delete v8.1.0 study", { path <- file.path(tempdir(), "tests_createStudy") suppressWarnings( @@ -58,7 +90,7 @@ test_that("delete v8.1.0 study", { testthat::expect_true(!file.exists(opts$studyPath)) }) - + ## v8.6.0---- test_that("delete v8.6.0 simulation", { setup_study_last(sourcedir_last_study) suppressWarnings( From a785d1163c9ce75d1fff24bf3debeda7df4a6617 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Tue, 6 Feb 2024 10:46:05 +0100 Subject: [PATCH 27/74] create_binding_constraint() corrected API call with the addition of filter_year_by_year and filter_synthesis parameters --- R/createBindingConstraint.R | 9 ++++----- man/createBindingConstraint.Rd | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/R/createBindingConstraint.R b/R/createBindingConstraint.R index 22ec1ec8..a18165a7 100644 --- a/R/createBindingConstraint.R +++ b/R/createBindingConstraint.R @@ -82,7 +82,7 @@ #' # values are now named list containing `data.frame` according to #' # `operator` parameter (for "less", build a list with at least "lt" floor in list) #' -#' # data values (daily) +#' # data values (hourly) #' df <- matrix(data = rep(0, 8760 * 3), ncol = 3) #' values_data <- list(lt=df) #' @@ -137,6 +137,7 @@ createBindingConstraint <- function(name, overwrite = FALSE, opts = antaresRead::simOptions()) { + # check input parameters assertthat::assert_that(inherits(opts, "simOptions")) timeStep <- match.arg(arg = timeStep) @@ -151,6 +152,8 @@ createBindingConstraint <- function(name, enabled = enabled, time_step = timeStep, operator = operator, + filter_year_by_year = filter_year_by_year, + filter_synthesis = filter_synthesis, values = values, coeffs = lapply(as.list(coefficients), as.list) ) @@ -182,10 +185,6 @@ createBindingConstraint <- function(name, if(!is.null(values)){ assertthat::assert_that(inherits(values, "list")) - # if(!all(c("lt", "gt", "eq")%in%names(values))) - # stop("Put for 'values' argument, - # named 'list' => see Doc `?createBindingConstraint`") - ## # check "values" according to "operator" ## diff --git a/man/createBindingConstraint.Rd b/man/createBindingConstraint.Rd index d24054b2..910fb840 100644 --- a/man/createBindingConstraint.Rd +++ b/man/createBindingConstraint.Rd @@ -111,7 +111,7 @@ createBindingConstraintBulk(bindings_constraints) # values are now named list containing `data.frame` according to # `operator` parameter (for "less", build a list with at least "lt" floor in list) -# data values (daily) +# data values (hourly) df <- matrix(data = rep(0, 8760 * 3), ncol = 3) values_data <- list(lt=df) From 0b6ed9cede44a2d172bf5f340884eca07e336e2d Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Tue, 6 Feb 2024 11:29:58 +0100 Subject: [PATCH 28/74] add parameter in block api call --- R/createBindingConstraint.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/createBindingConstraint.R b/R/createBindingConstraint.R index a18165a7..22dc4eef 100644 --- a/R/createBindingConstraint.R +++ b/R/createBindingConstraint.R @@ -154,6 +154,7 @@ createBindingConstraint <- function(name, operator = operator, filter_year_by_year = filter_year_by_year, filter_synthesis = filter_synthesis, + group = group, values = values, coeffs = lapply(as.list(coefficients), as.list) ) From 7dde1a40a82c64a4c63ecca4d438a94bde4d0664 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Wed, 7 Feb 2024 15:29:53 +0100 Subject: [PATCH 29/74] editBindingConstraint() add badge lifecycle "experimental" --- R/editBindingConstraint.R | 3 ++- tests/testthat/test-editBindingConstraint.R | 28 +++------------------ 2 files changed, 5 insertions(+), 26 deletions(-) diff --git a/R/editBindingConstraint.R b/R/editBindingConstraint.R index a60d3556..f84d501f 100644 --- a/R/editBindingConstraint.R +++ b/R/editBindingConstraint.R @@ -2,12 +2,13 @@ #' #' @description #' `r antaresEditObject:::badge_api_ok()` +#' `r lifecycle::badge("experimental")` #' #' Update an existing binding constraint in an Antares study. #' The key search value of the constraint is the `id` field #' #' @inheritParams createBindingConstraint -#' @param group "character" group of the constraint, default value : "default group" +#' @param group "character" group of the constraint, default value : "default" #' @template opts #' #' @seealso [createBindingConstraint()] to create new binding constraints, [removeBindingConstraint()] to remove binding constraints. diff --git a/tests/testthat/test-editBindingConstraint.R b/tests/testthat/test-editBindingConstraint.R index 977c9535..db2419dd 100644 --- a/tests/testthat/test-editBindingConstraint.R +++ b/tests/testthat/test-editBindingConstraint.R @@ -47,17 +47,6 @@ opts_test <- antaresRead::setSimulationPath(study_latest_version, "input") # areas list antaresRead::getAreas(opts = opts_test) -# remove BC none v870 -names_bc_to_remove <- names(readBindingConstraints(opts = opts_test)) - -lapply(names_bc_to_remove, - removeBindingConstraint, - opts = simOptions()) - -# temporary to test with "870" -# force version -opts_test$antaresVersion <- 870 - # scenarized data # hourly n <- 10 @@ -77,21 +66,10 @@ scenar_values_daily <- list(lt= lt_data, gt= gt_data, eq= eq_data) +## default group ---- +test_that("editBindingConstraint with 'default group' v8.7.0", { -test_that("editBindingConstraint v8.6", { - - # create binding constraint (default group value) - createBindingConstraint( - name = "myconstraint", - values = scenar_values_hourly, - enabled = FALSE, - timeStep = "hourly", - operator = "both", - coefficients = c("al%gr" = 1), - opts = opts_test - ) - - bc_names_v870 <- names(readBindingConstraints(opts = opts_test)) + bc <- readBindingConstraints(opts = opts_test) # edit BC with NULL values (edit only .ini file) editBindingConstraint(name = bc_names_v870, From 6c602d7ad12ed01c453017fab8f6b7b0d5f852e0 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Mon, 12 Feb 2024 16:06:52 +0100 Subject: [PATCH 30/74] createBindingConstraint() minor fix + doc --- R/createBindingConstraint.R | 8 ++++---- man/createBindingConstraint.Rd | 9 ++++++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/R/createBindingConstraint.R b/R/createBindingConstraint.R index 22dc4eef..07b5dd86 100644 --- a/R/createBindingConstraint.R +++ b/R/createBindingConstraint.R @@ -11,7 +11,8 @@ #' @param name The name for the binding constraint. #' @param id An id, default is to use the name. #' @param values Values used by the constraint. -#' It contains one line per time step and three columns "less", "greater" and "equal". +#' It contains one line per time step and three columns "less", "greater" and "equal" +#' (see documentation below if you're using version study >= v8.7.0) #' @param enabled Logical, is the constraint enabled ? #' @param timeStep Time step the constraint applies to : `hourly`, `daily` or `weekly`. #' @param operator Type of constraint: equality, inequality on one side or both sides. @@ -23,8 +24,7 @@ #' #' @template opts #' -#' @seealso [editBindingConstraint()] to edit existing binding constraints, -#' [removeBindingConstraint()] to remove binding constraints. +#' @family binding constraints functions #' #' @details #' According to Antares version, usage may vary : @@ -348,7 +348,7 @@ createBindingConstraint_ <- function(bindingConstraints, row.names = FALSE, sep = "\t") else{ - index <- grep(x = up_path, pattern = x) + index <- grep(x = vect_path, pattern = x) fwrite(x = data.table::as.data.table(df_ts[[index]]), file = x, col.names = FALSE, diff --git a/man/createBindingConstraint.Rd b/man/createBindingConstraint.Rd index 910fb840..861b58f2 100644 --- a/man/createBindingConstraint.Rd +++ b/man/createBindingConstraint.Rd @@ -28,7 +28,8 @@ createBindingConstraintBulk(constraints, opts = antaresRead::simOptions()) \item{id}{An id, default is to use the name.} \item{values}{Values used by the constraint. -It contains one line per time step and three columns "less", "greater" and "equal".} +It contains one line per time step and three columns "less", "greater" and "equal" +(see documentation below if you're using version study >= v8.7.0)} \item{enabled}{Logical, is the constraint enabled ?} @@ -155,6 +156,8 @@ createBindingConstraintBulk(bindings_constraints) } } \seealso{ -\code{\link[=editBindingConstraint]{editBindingConstraint()}} to edit existing binding constraints, -\code{\link[=removeBindingConstraint]{removeBindingConstraint()}} to remove binding constraints. +Other binding constraints functions: +\code{\link{editBindingConstraint}()}, +\code{\link{removeBindingConstraint}()} } +\concept{binding constraints functions} From 7cc5a51338eaeffb1cf9d23b3ff7a14aec60820d Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Mon, 12 Feb 2024 16:07:57 +0100 Subject: [PATCH 31/74] editBindingConstraint() update function according to v870 features + doc + tests --- R/editBindingConstraint.R | 61 +++++-- man/editBindingConstraint.Rd | 40 ++++- tests/testthat/test-editBindingConstraint.R | 182 ++++++++++++-------- 3 files changed, 201 insertions(+), 82 deletions(-) diff --git a/R/editBindingConstraint.R b/R/editBindingConstraint.R index f84d501f..bb5406c0 100644 --- a/R/editBindingConstraint.R +++ b/R/editBindingConstraint.R @@ -9,9 +9,17 @@ #' #' @inheritParams createBindingConstraint #' @param group "character" group of the constraint, default value : "default" +#' @param values Values used by the constraint. +#' It contains one line per time step and three columns "less", "greater" and "equal" +#' (see documentation below if you're using version study >= v8.7.0) #' @template opts #' -#' @seealso [createBindingConstraint()] to create new binding constraints, [removeBindingConstraint()] to remove binding constraints. +#' @family binding constraints functions +#' +#' @section Warning: +#' **>= v8.7.0** : For each constraint name, one file .txt containing `_lt.txt, _gt.txt, _eq.txt` +#' Parameter `values` must be named `list` ("lt", "gt", "eq") containing `data.frame` scenarized. +#' see example section below. #' #' @export #' @@ -20,6 +28,7 @@ #' #' @examples #' \dontrun{ +#' # < v8.7.0 : #' editBindingConstraint( #' name = "myconstraint", #' values = matrix(data = rep(0, 8760 * 3), ncol = 3), @@ -28,6 +37,27 @@ #' operator = "both", #' coefficients = c("fr%de" = 1) #' ) +#' +#' # >= v8.7.0 : +#' +#' # data values scenarized (hourly) +#' df <- matrix(data = rep(0, 8760 * 3), ncol = 3) +#' +#' # you can provide list data with all value +#' # or just according with 'operator' (ex : 'lt' for 'less) +#' values_data <- list(lt=df, +#' gt= df, +#' eq= df) +#' +#' editBindingConstraint(name = "myconstraint", +#' values = values_data, +#' enabled = TRUE, +#' timeStep = "hourly", +#' operator = "both", +#' filter_year_by_year = "hourly", +#' filter_synthesis = "hourly", +#' coefficients = c("fr%de" = 1), +#' group = "myconstraint_group") #' } editBindingConstraint <- function(name, id = tolower(name), @@ -56,6 +86,8 @@ editBindingConstraint <- function(name, enabled = enabled, time_step = timeStep, operator = operator, + filter_year_by_year = filter_year_by_year, + filter_synthesis = filter_synthesis, values = values, coeffs = lapply(as.list(coefficients), as.list) ) @@ -123,18 +155,26 @@ editBindingConstraint <- function(name, if(!is.null(group)){ iniParams$group <- group - # edit group with check group values - if(is.null(values)) - group_values_check(group_value = group, - values_data = values, - opts = opts) + # # edit group with check group values + # if(is.null(values)) + # group_values_check(group_value = group, + # values_data = values, + # opts = opts) }else - group <- "default group" + group <- "default" + + values_operator <- switch(operator, + less = "lt", + equal = "eq", + greater = "gt", + both = c("lt", "gt")) # check group values if(!is.null(values)) group_values_check(group_value = group, values_data = values, + operator_check = operator, + output_operator = values_operator, opts = opts) } @@ -145,6 +185,8 @@ editBindingConstraint <- function(name, bindingConstraints[[bc_update_pos]]$enabled <- iniParams$enabled bindingConstraints[[bc_update_pos]]$type <- iniParams$type bindingConstraints[[bc_update_pos]]$operator <- iniParams$operator + bindingConstraints[[bc_update_pos]]$`filter-year-by-year` <- iniParams$`filter-year-by-year` + bindingConstraints[[bc_update_pos]]$`filter-synthesis` <- iniParams$`filter-synthesis` if(!is.null(coefficients)){ @@ -186,13 +228,12 @@ editBindingConstraint <- function(name, # v870 if(opts$antaresVersion>=870){ if(!identical(values, character(0))){ - names_order_ts <- c("lt", "gt", "eq") - name_file <- paste0(id, "_", names_order_ts, ".txt") + name_file <- paste0(id, "_", values_operator, ".txt") up_path <- file.path(opts$inputPath, "bindingconstraints", name_file) lapply(up_path, function(x, df_ts= values, vect_path= up_path){ - index <- grep(x = up_path, pattern = x) + index <- grep(x = vect_path, pattern = x) fwrite(x = data.table::as.data.table(df_ts[[index]]), file = x, col.names = FALSE, diff --git a/man/editBindingConstraint.Rd b/man/editBindingConstraint.Rd index 440b86a5..fcdcfda4 100644 --- a/man/editBindingConstraint.Rd +++ b/man/editBindingConstraint.Rd @@ -24,7 +24,8 @@ editBindingConstraint( \item{id}{An id, default is to use the name.} \item{values}{Values used by the constraint. -It contains one line per time step and three columns "less", "greater" and "equal".} +It contains one line per time step and three columns "less", "greater" and "equal" +(see documentation below if you're using version study >= v8.7.0)} \item{enabled}{Logical, is the constraint enabled ?} @@ -38,7 +39,7 @@ It contains one line per time step and three columns "less", "greater" and "equa \item{coefficients}{A named vector containing the coefficients used by the constraint.} -\item{group}{"character" group of the constraint, default value : "default group"} +\item{group}{"character" group of the constraint, default value : "default"} \item{opts}{List of simulation parameters returned by the function \code{\link[antaresRead:setSimulationPath]{antaresRead::setSimulationPath()}}} @@ -48,12 +49,21 @@ An updated list containing various information about the simulation. } \description{ \ifelse{html}{\figure{badge_api_ok.svg}{options: alt='Antares API OK'}}{Antares API: \strong{OK}} +\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}} Update an existing binding constraint in an Antares study. The key search value of the constraint is the \code{id} field } +\section{Warning}{ + +\strong{>= v8.7.0} : For each constraint name, one file .txt containing \verb{_lt.txt, _gt.txt, _eq.txt} +Parameter \code{values} must be named \code{list} ("lt", "gt", "eq") containing \code{data.frame} scenarized. +see example section below. +} + \examples{ \dontrun{ + # < v8.7.0 : editBindingConstraint( name = "myconstraint", values = matrix(data = rep(0, 8760 * 3), ncol = 3), @@ -62,8 +72,32 @@ editBindingConstraint( operator = "both", coefficients = c("fr\%de" = 1) ) + + # >= v8.7.0 : + +# data values scenarized (hourly) +df <- matrix(data = rep(0, 8760 * 3), ncol = 3) + +# you can provide list data with all value +# or just according with 'operator' (ex : 'lt' for 'less) +values_data <- list(lt=df, + gt= df, + eq= df) + +editBindingConstraint(name = "myconstraint", + values = values_data, + enabled = TRUE, + timeStep = "hourly", + operator = "both", + filter_year_by_year = "hourly", + filter_synthesis = "hourly", + coefficients = c("fr\%de" = 1), + group = "myconstraint_group") } } \seealso{ -\code{\link[=createBindingConstraint]{createBindingConstraint()}} to create new binding constraints, \code{\link[=removeBindingConstraint]{removeBindingConstraint()}} to remove binding constraints. +Other binding constraints functions: +\code{\link{createBindingConstraint}()}, +\code{\link{removeBindingConstraint}()} } +\concept{binding constraints functions} diff --git a/tests/testthat/test-editBindingConstraint.R b/tests/testthat/test-editBindingConstraint.R index db2419dd..0d0602d2 100644 --- a/tests/testthat/test-editBindingConstraint.R +++ b/tests/testthat/test-editBindingConstraint.R @@ -37,8 +37,6 @@ test_that("editBindingConstraint v710", { # v870 ---- -testthat::skip() - ## Global data # read / open template study setup_study_last(dir_path = sourcedir_last_study) @@ -67,102 +65,148 @@ scenar_values_daily <- list(lt= lt_data, eq= eq_data) ## default group ---- -test_that("editBindingConstraint with 'default group' v8.7.0", { +test_that("editBindingConstraint with 'default' group v8.7.0", { + # PS : in this study, "default" have 1 column dimension bc <- readBindingConstraints(opts = opts_test) - # edit BC with NULL values (edit only .ini file) + # edit properties + values (good dimension) + # edit "greater" to "both" + bc_names_v870 <- bc$bc_2$properties$id editBindingConstraint(name = bc_names_v870, - values = NULL, + values = scenar_values_daily, timeStep = "daily", operator = "both", - coefficients = c("al%gr"= 7.45), + filter_year_by_year = "daily", + filter_synthesis = "daily", + coefficients = c("fr%it"= 7.45), opts = opts_test) + # read bc_modified <- readBindingConstraints(opts = opts_test) - new_coef <- bc_modified[[bc_names_v870[1]]]$coefs + new_coef <- bc_modified[[bc_names_v870]]$coefs + timeStep <- bc_modified[[bc_names_v870]]$properties$timeStep + operator <- bc_modified[[bc_names_v870]]$properties$operator + filter_year <- bc_modified[[bc_names_v870]]$properties$`filter-year-by-year` + filter_synthesis <- bc_modified[[bc_names_v870]]$properties$`filter-synthesis` - # test + # test properties testthat::expect_true(all(new_coef %in% c(7.45))) + testthat::expect_true(timeStep %in% "daily") + testthat::expect_true(operator %in% "both") + testthat::expect_true(filter_year %in% "daily") + testthat::expect_true(filter_synthesis %in% "daily") - # edit BC with "daily" VALUES - # add new coeff - editBindingConstraint(name = bc_names_v870, - values = scenar_values_daily, - timeStep = "daily", - operator = "both", - coefficients = c("00_pump_d%gr" = 12, - "gr%00_turb_d" = 0, - "al%gr"= 0.5), - opts = opts_test) + # test values + dim_col_values_input <- dim(scenar_values_daily$lt)[2] + dim_col_values_edited <- dim(bc_modified[[bc_names_v870]]$values$less)[2] + testthat::expect_equal(dim_col_values_input, dim_col_values_edited) - bc_modified <- readBindingConstraints(opts = opts_test) - new_coef <- bc_modified[[bc_names_v870[1]]]$coefs - # test coefs - testthat::expect_true(all(new_coef %in% c(0.5, 12.0, 0.0))) + # edit properties + values (bad dimension) + ### error dimension ---- + + n <- 9 + # daily + lt_data <- matrix(data = rep(1, 365 * n), ncol = n) + gt_data <- matrix(data = rep(2, 365 * n), ncol = n) + eq_data <- matrix(data = rep(3, 365 * n), ncol = n) - # test dim values "daily" - # the length of a standard year is 366 - res_dim_values <- lapply(bc_modified$myconstraint$values, dim) - res_dim_values <- lapply(res_dim_values, `[[`, 1) - res_dim_values <- unlist(res_dim_values) - testthat::expect_equal(mean(res_dim_values), 366) + scenar_values_daily_n <- list(lt= lt_data, + gt= gt_data, + eq= eq_data) - # test error with bad values dimension testthat::expect_error( editBindingConstraint(name = bc_names_v870, - values = list(lt=matrix(data = rep(1, 365 * 9), - ncol = 9)), + values = scenar_values_daily_n, timeStep = "daily", - operator = "both", + operator = "both", + coefficients = c("fr%it"= 7.45), opts = opts_test), - regexp = "Put right columns dimension : 10 for existing 'group' : default group" - ) - - # create new binding constraint with new dimension - n <- 20 - lt_data <- matrix(data = rep(1, 8760 * n), ncol = n) - gt_data <- matrix(data = rep(2, 8760 * n), ncol = n) - eq_data <- matrix(data = rep(3, 8760 * n), ncol = n) - - scenar_values_hourly_new <- list(lt= lt_data, - gt= gt_data, - eq= eq_data) - - createBindingConstraint( - name = "myconstraint_new", - values = scenar_values_hourly_new, - enabled = FALSE, - timeStep = "hourly", - operator = "both", - coefficients = c("al%gr" = 1), - group = "new", - opts = opts_test + regexp = "Put right columns dimension" ) - # edit group of last bindingConstraint with bad dimension - testthat::expect_error( - editBindingConstraint(name = "myconstraint_new", - group = "default group", - opts = opts_test), - regexp = "Put right columns dimension : 10 for existing 'group' : default group" - ) - # edit param - editBindingConstraint(name = "myconstraint_new", - operator = "less", + + ### multi coeff ---- + editBindingConstraint(name = bc_names_v870, + values = NULL, + timeStep = "daily", + operator = "both", + coefficients = c("fr%it" = 12, + "fr%at" = 0), opts = opts_test) + # read bc_modified <- readBindingConstraints(opts = opts_test) - new_param <- bc_modified[["myconstraint_new"]]$operator + new_coef <- bc_modified[[bc_names_v870]]$coefs # test coefs - testthat::expect_equal(new_param, "less") + testthat::expect_true(all(new_coef %in% c(12, 0))) - - # remove temporary study - unlink(x = study_latest_version, recursive = TRUE) }) +## exisintg group ---- +test_that("editBindingConstraint with existing group v8.7.0", { + # read existing binding + bc <- readBindingConstraints(opts = opts_test) + + # list names group (none default) + # dimension according BC/group + info_bc <- lapply(bc, function(x){ + if(x$properties$operator %in% "both") + list(group = x$properties$group, + dim_values = dim(x$values$less)[2]) + else + list(group = x$properties$group, + dim_values = dim(x$values)[2]) + }) + + index <- !lapply(info_bc, `[[`, 1) %in% + "default" + + # select on bc none "default" + bc_no_default <- names(info_bc[index])[1] + + group_bc <- info_bc[index][[bc_no_default]]$group + dim_bc <- info_bc[index][[bc_no_default]]$dim_values + + # edit bc with good dim + n <- 2 + # daily + lt_data <- matrix(data = rep(1, 365 * n), ncol = n) + gt_data <- matrix(data = rep(2, 365 * n), ncol = n) + eq_data <- matrix(data = rep(3, 365 * n), ncol = n) + + scenar_values_daily_n <- list(lt= lt_data, + gt= gt_data, + eq= eq_data) + + editBindingConstraint(name = bc_no_default, + values = scenar_values_daily_n, + group = group_bc, + timeStep = "daily", + operator = "both", + coefficients = c("fr%it" = 12), + opts = opts_test) + + # read + bc_modified <- readBindingConstraints(opts = opts_test) + new_coef <- bc_modified[[bc_no_default]]$coefs + timeStep <- bc_modified[[bc_no_default]]$properties$timeStep + operator <- bc_modified[[bc_no_default]]$properties$operator + + # test properties + testthat::expect_true(all(new_coef %in% c(1, 12))) + testthat::expect_true(timeStep %in% "daily") + testthat::expect_true(operator %in% "both") + + # test values + dim_col_values_input <- dim(scenar_values_daily_n$lt)[2] + dim_col_values_edited <- dim(bc_modified[[bc_no_default]]$values$less)[2] + testthat::expect_equal(dim_col_values_input, dim_col_values_edited) + +}) +# remove study ---- +unlink(x = study_latest_version, recursive = TRUE) From 1ae33a3ad840b0493d9d3b0c811c8ab1b5ce9f4b Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Mon, 12 Feb 2024 16:08:25 +0100 Subject: [PATCH 32/74] removeBindingConstraint() minor update doc --- R/removeBindingConstraint.R | 2 +- man/removeBindingConstraint.Rd | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/R/removeBindingConstraint.R b/R/removeBindingConstraint.R index 18d3bd36..dc9b3927 100644 --- a/R/removeBindingConstraint.R +++ b/R/removeBindingConstraint.R @@ -10,7 +10,7 @@ #' #' @template opts #' -#' @seealso [createBindingConstraint()] to create new binding constraints, [editBindingConstraint()] to edit existing binding constraints. +#' @family binding constraints functions #' #' @export #' diff --git a/man/removeBindingConstraint.Rd b/man/removeBindingConstraint.Rd index d4f15fe1..47033908 100644 --- a/man/removeBindingConstraint.Rd +++ b/man/removeBindingConstraint.Rd @@ -26,5 +26,8 @@ removeBindingConstraint("mybindingconstraint") } } \seealso{ -\code{\link[=createBindingConstraint]{createBindingConstraint()}} to create new binding constraints, \code{\link[=editBindingConstraint]{editBindingConstraint()}} to edit existing binding constraints. +Other binding constraints functions: +\code{\link{createBindingConstraint}()}, +\code{\link{editBindingConstraint}()} } +\concept{binding constraints functions} From 14086d7bd002be437bb6ce8f27d50621de54028a Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Mon, 12 Feb 2024 16:35:04 +0100 Subject: [PATCH 33/74] resolving the pull master branch error --- tests/testthat/test-editLink.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-editLink.R b/tests/testthat/test-editLink.R index 47d756aa..35fb53b3 100644 --- a/tests/testthat/test-editLink.R +++ b/tests/testthat/test-editLink.R @@ -3,9 +3,9 @@ test_that("Edit a link filters", { pasteVectorItemsWithComma <- function(x) paste(x,collapse=", ") - setup_study_860(sourcedir860) + setup_study_last(dir_path = sourcedir_last_study) suppressWarnings( - opts_test <- setSimulationPath(study_temp_path,simulation="input") + opts_test <- setSimulationPath(study_latest_version,simulation="input") ) opts_test <- createArea(name="area1",opts=opts_test) From f4b661a0dc7511c6ff1aada038ff137b6ae9f3b6 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Fri, 16 Feb 2024 15:13:04 +0100 Subject: [PATCH 34/74] removeBindingConstraint() reforged and updated according to Antares simulator v870 + doc + tests --- R/removeBindingConstraint.R | 129 ++++++++--- man/removeBindingConstraint.Rd | 24 ++- tests/testthat/test-removeBindingConstraint.R | 201 +++++++++++------- 3 files changed, 252 insertions(+), 102 deletions(-) diff --git a/R/removeBindingConstraint.R b/R/removeBindingConstraint.R index dc9b3927..7fbd77b2 100644 --- a/R/removeBindingConstraint.R +++ b/R/removeBindingConstraint.R @@ -2,11 +2,13 @@ #' #' @description #' `r antaresEditObject:::badge_api_ok()` +#' `r lifecycle::badge("experimental")` #' #' Remove a binding constraint in an Antares study. #' #' #' @param name Name(s) of the binding constraint(s) to remove. +#' @param group `character` Name(s) of group to delete #' #' @template opts #' @@ -16,21 +18,44 @@ #' #' @examples #' \dontrun{ -#' removeBindingConstraint("mybindingconstraint") +#'# < v8.7.0 : +#' removeBindingConstraint(name = "mybindingconstraint") +#' +#' # >= v8.7.0 (delete by names group) : +#' # read +#' bc <- readBindingConstraints() +#' +#' # select all groups +#' group_to_delete <- sapply(bc, function(x){ +#' x$properties$group +#' }) +#' +#' # delete all groups +#' removeBindingConstraint(group = group_to_delete) #' } -removeBindingConstraint <- function(name, opts = antaresRead::simOptions()) { +removeBindingConstraint <- function(name = NULL, + group = NULL, + opts = antaresRead::simOptions()) { assertthat::assert_that(inherits(opts, "simOptions")) + # some checks for "group" parameter according to study version + if(!opts$antaresVersion >= 870 & !is.null(group)) + stop("Parameter 'group' is only for Antares study version >= v8.7.0", + call. = FALSE) + if(opts$antaresVersion >= 870){ + if(!is.null(name) & !is.null(group)) + stop("You can only delete binding constraint by id/name or by group", + call. = FALSE) + } + # API block if (is_api_study(opts)) { - for (i in name) { cmd <- api_command_generate( "remove_binding_constraint", id = i ) - api_command_register(cmd, opts = opts) `if`( should_command_be_executed(opts), @@ -38,24 +63,54 @@ removeBindingConstraint <- function(name, opts = antaresRead::simOptions()) { cli_command_registered("remove_binding_constraint") ) } - return(invisible(opts)) } - ## Ini file + ## read Ini file pathIni <- file.path(opts$inputPath, "bindingconstraints/bindingconstraints.ini") bindingConstraints <- readIniFile(pathIni, stringsAsFactors = FALSE) - namesbc <- unlist(lapply(bindingConstraints, `[[`, "name"), use.names = FALSE) + namesbc <- unlist(lapply(bindingConstraints, `[[`, "id"), use.names = FALSE) # suppression txt files + remove constraint from ini file - for (i in name) { - if (! i %in% namesbc) { + if(!is.null(name)) + updated_bc <- .delete_by_name(bc_properties = bindingConstraints, + names_to_delete = name, + all_bc_names = namesbc, + opts = opts) + + # suppression txt files + remove constraint from ini file [by group] + if(!is.null(group)) + updated_bc <- .delete_by_group(group = group, + bc_properties = bindingConstraints, + all_bc_names = namesbc, + opts = opts) + + # Write Ini + writeIni(listData = updated_bc, pathIni = pathIni, overwrite = TRUE) + + # Maj simulation + suppressWarnings({ + res <- antaresRead::setSimulationPath(path = opts$studyPath, simulation = "input") + }) + + invisible(res) +} + +.delete_by_name <- function(bc_properties, + names_to_delete, + all_bc_names, + opts){ + # delete all bc with names/id matching in study + # delete values + # delete .ini section + for (i in names_to_delete) { + if (! i %in% all_bc_names) { warning(paste0("No binding constraint with name '", i, "'")) } else { - index <- which(namesbc == i) - id <- bindingConstraints[[index]]$id - bindingConstraints[[index]] <- NULL + index <- which(all_bc_names == i) + id <- bc_properties[[index]]$id + bc_properties[[index]] <- NULL # v870 if(opts$antaresVersion>=870){ path_lt <- file.path(opts$inputPath, @@ -74,19 +129,41 @@ removeBindingConstraint <- function(name, opts = antaresRead::simOptions()) { unlink(x = pathValues) } - namesbc <- unlist(lapply(bindingConstraints, `[[`, "name"), use.names = FALSE) - names(bindingConstraints) <- as.character(seq_along(bindingConstraints) - 1) - } + all_bc_names <- unlist(lapply(bc_properties, `[[`, "id"), use.names = FALSE) + names(bc_properties) <- as.character(seq_along(bc_properties) - 1) + } } - - # Write Ini - writeIni(listData = bindingConstraints, pathIni = pathIni, overwrite = TRUE) - - - # Maj simulation - suppressWarnings({ - res <- antaresRead::setSimulationPath(path = opts$studyPath, simulation = "input") - }) - - invisible(res) + return(bc_properties) +} + +# feature v870 delete bc by group +.delete_by_group <- function(group, + bc_properties, + all_bc_names, + opts){ + # extract groups + bc_groups <- unlist( + lapply(bc_properties, + `[[`, + "group"), + use.names = FALSE) + + if(!all(group%in%bc_groups)) + warning(paste0("No binding constraint with group '", + group[!group%in%bc_groups], "'"), + call. = FALSE) + else{ + index <- which(bc_groups%in%group) + names_to_delete <- sapply(index, + function(x, + bc = bc_properties){ + bc[[x]]$id + }) + + updated_bc <- .delete_by_name(bc_properties = bc_properties, + names_to_delete = names_to_delete, + all_bc_names = all_bc_names, + opts = opts) + updated_bc + } } diff --git a/man/removeBindingConstraint.Rd b/man/removeBindingConstraint.Rd index 47033908..14c9d342 100644 --- a/man/removeBindingConstraint.Rd +++ b/man/removeBindingConstraint.Rd @@ -4,11 +4,17 @@ \alias{removeBindingConstraint} \title{Remove a Binding Constraint} \usage{ -removeBindingConstraint(name, opts = antaresRead::simOptions()) +removeBindingConstraint( + name = NULL, + group = NULL, + opts = antaresRead::simOptions() +) } \arguments{ \item{name}{Name(s) of the binding constraint(s) to remove.} +\item{group}{\code{character} Name(s) of group to delete} + \item{opts}{List of simulation parameters returned by the function \code{\link[antaresRead:setSimulationPath]{antaresRead::setSimulationPath()}}} } @@ -17,12 +23,26 @@ An updated list containing various information about the simulation. } \description{ \ifelse{html}{\figure{badge_api_ok.svg}{options: alt='Antares API OK'}}{Antares API: \strong{OK}} +\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}} Remove a binding constraint in an Antares study. } \examples{ \dontrun{ -removeBindingConstraint("mybindingconstraint") +# < v8.7.0 : +removeBindingConstraint(name = "mybindingconstraint") + +# >= v8.7.0 (delete by names group) : +# read +bc <- readBindingConstraints() + +# select all groups +group_to_delete <- sapply(bc, function(x){ + x$properties$group +}) + +# delete all groups +removeBindingConstraint(group = group_to_delete) } } \seealso{ diff --git a/tests/testthat/test-removeBindingConstraint.R b/tests/testthat/test-removeBindingConstraint.R index d2f6f583..f42dfc03 100644 --- a/tests/testthat/test-removeBindingConstraint.R +++ b/tests/testthat/test-removeBindingConstraint.R @@ -1,95 +1,148 @@ -testthat::skip() -# v870 ---- - -## Global data -# read / open template study -setup_study_last(dir_path = sourcedir_last_study) -opts_test <- antaresRead::setSimulationPath(study_latest_version, "input") - -# areas list -antaresRead::getAreas(opts = opts_test) - -# remove BC none v870 -names_bc_to_remove <- names(readBindingConstraints(opts = opts_test)) - -lapply(names_bc_to_remove, - removeBindingConstraint, - opts = simOptions()) -# temporary to test with "870" -# force version -opts_test$antaresVersion <- 870 - -# scenarized data -# hourly -n <- 10 -lt_data <- matrix(data = rep(1, 8760 * n), ncol = n) -gt_data <- matrix(data = rep(2, 8760 * n), ncol = n) -eq_data <- matrix(data = rep(3, 8760 * n), ncol = n) - -scenar_values_hourly <- list(lt= lt_data, - gt= gt_data, - eq= eq_data) -# daily -lt_data <- matrix(data = rep(1, 365 * n), ncol = n) -gt_data <- matrix(data = rep(2, 365 * n), ncol = n) -eq_data <- matrix(data = rep(3, 365 * n), ncol = n) - -scenar_values_daily <- list(lt= lt_data, - gt= gt_data, - eq= eq_data) +# v870 ---- -test_that("removeBindingConstraint v8.6", { - - # create binding constraint (default group value) - createBindingConstraint( - name = "myconstraint", - values = scenar_values_hourly, - enabled = FALSE, - timeStep = "hourly", - operator = "both", - coefficients = c("al%gr" = 1), - opts = opts_test - ) - - createBindingConstraint( - name = "myconstraint1", - values = scenar_values_hourly, - enabled = FALSE, - timeStep = "hourly", - operator = "both", - group = "group_test", - coefficients = c("al%gr" = 1), - opts = opts_test - ) - - createBindingConstraint( - name = "myconstraint2", - values = scenar_values_hourly, - enabled = FALSE, - timeStep = "hourly", - operator = "both", - group = "group_test", - coefficients = c("al%gr" = 1), - opts = opts_test - ) +## one name ---- +test_that("removeBindingConstraint v8.7.0 by name", { + # read / open template study + setup_study_last(dir_path = sourcedir_last_study) + opts_test <- antaresRead::setSimulationPath(study_latest_version, "input") + + # areas list + antaresRead::getAreas(opts = opts_test) + # read bc_names_v870 <- names(readBindingConstraints(opts = opts_test)) + # delete removeBindingConstraint(bc_names_v870[1], opts = opts_test) + # read bc_in_study <- readBindingConstraints(opts = opts_test) # test if(is.null(bc_in_study)) testthat::expect_null(names(bc_in_study)) else - testthat::expect_false("myconstraint" %in% + testthat::expect_false(bc_names_v870[1] %in% names(readBindingConstraints(opts = opts_test))) + # again with "both" binding constraint + bc_names_v870 <- names(readBindingConstraints(opts = opts_test)) + + removeBindingConstraint(bc_names_v870[3], + opts = opts_test) + + bc_in_study <- readBindingConstraints(opts = opts_test) + + # test + if(is.null(bc_in_study)) + testthat::expect_null(names(bc_in_study)) + else + testthat::expect_false(bc_names_v870[3] %in% + names(readBindingConstraints(opts = opts_test))) + + + # remove temporary study unlink(x = study_latest_version, recursive = TRUE) }) + +## multi names ---- +test_that("removeBindingConstraint v8.7.0 by names", { + # read / open template study + setup_study_last(dir_path = sourcedir_last_study) + opts_test <- antaresRead::setSimulationPath(study_latest_version, "input") + + # areas list + antaresRead::getAreas(opts = opts_test) + + # read + bc_names_v870 <- names(readBindingConstraints(opts = opts_test)) + + # delete + name_to_delete <- bc_names_v870[-1] + removeBindingConstraint(name_to_delete, + opts = opts_test) + + # read + bc_in_study <- readBindingConstraints(opts = opts_test) + + # test + if(is.null(bc_in_study)) + testthat::expect_null(names(bc_in_study)) + else + testthat::expect_false(all(name_to_delete %in% + names(readBindingConstraints(opts = opts_test)))) + + # remove temporary study + unlink(x = study_latest_version, recursive = TRUE) +}) + + +## one group ---- +test_that("removeBindingConstraint v8.7.0 by group", { + # read / open template study + setup_study_last(dir_path = sourcedir_last_study) + opts_test <- antaresRead::setSimulationPath(study_latest_version, "input") + + # areas list + antaresRead::getAreas(opts = opts_test) + + # read + bc <- readBindingConstraints(opts = opts_test) + + # delete + group_to_delete <- bc$bc_2$properties$group + + removeBindingConstraint(group = group_to_delete, + opts = opts_test) + + # read + bc_in_study <- readBindingConstraints(opts = opts_test) + + # test + if(is.null(bc_in_study)) + testthat::expect_null(names(bc_in_study)) + else + testthat::expect_false(all(group_to_delete %in% + names(readBindingConstraints(opts = opts_test)))) + # remove temporary study + unlink(x = study_latest_version, recursive = TRUE) +}) + +## multi group ---- +test_that("removeBindingConstraint v8.7.0 by group", { + # read / open template study + setup_study_last(dir_path = sourcedir_last_study) + opts_test <- antaresRead::setSimulationPath(study_latest_version, "input") + + # areas list + antaresRead::getAreas(opts = opts_test) + + # read + bc <- readBindingConstraints(opts = opts_test) + + # select all groups + group_to_delete <- sapply(bc, function(x){ + x$properties$group + }) + + # delete all groups + removeBindingConstraint(group = group_to_delete, + opts = opts_test) + + # read + bc_in_study <- readBindingConstraints(opts = opts_test) + + # test + if(is.null(bc_in_study)) + testthat::expect_null(names(bc_in_study)) + else + testthat::expect_false(all(group_to_delete %in% + names(readBindingConstraints(opts = opts_test)))) + # remove temporary study + unlink(x = study_latest_version, recursive = TRUE) +}) From 10363a78ea0a35e8de8d363cf6d8f875f219ef9f Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Wed, 21 Feb 2024 10:50:35 +0100 Subject: [PATCH 35/74] editBindingConstraint() minor cleaning code --- R/editBindingConstraint.R | 88 +++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 45 deletions(-) diff --git a/R/editBindingConstraint.R b/R/editBindingConstraint.R index bb5406c0..a465ff3d 100644 --- a/R/editBindingConstraint.R +++ b/R/editBindingConstraint.R @@ -76,9 +76,11 @@ editBindingConstraint <- function(name, if (is_api_study(opts)) { if (is.null(timeStep)) - stop("You must provide `timeStep` argument with API.", call. = FALSE) + stop("You must provide `timeStep` argument with API.", + call. = FALSE) if (is.null(operator)) - stop("You must provide `operator` argument with API.", call. = FALSE) + stop("You must provide `operator` argument with API.", + call. = FALSE) cmd <- api_command_generate( "update_binding_constraint", @@ -95,7 +97,8 @@ editBindingConstraint <- function(name, api_command_register(cmd, opts = opts) `if`( should_command_be_executed(opts), - api_command_execute(cmd, opts = opts, text_alert = "update_binding_constraint: {msg_api}"), + api_command_execute(cmd, opts = opts, + text_alert = "update_binding_constraint: {msg_api}"), cli_command_registered("update_binding_constraint") ) @@ -104,16 +107,21 @@ editBindingConstraint <- function(name, # valuesIn <- values # check Ini file names constraints - pathIni <- file.path(opts$inputPath, "bindingconstraints/bindingconstraints.ini") + pathIni <- file.path(opts$inputPath, + "bindingconstraints/bindingconstraints.ini") # initial parameter list bindingConstraints <- readIniFile(pathIni, stringsAsFactors = FALSE) - previds <- lapply(bindingConstraints, `[[`, "id") + previds <- lapply(bindingConstraints, + `[[`, + "id") previds <- unlist(previds, use.names = FALSE) - if(!id %in% previds){ - stop("Binding constraint with id '", id, "' doesn't exist in current study.") - } + if(!id %in% previds) + stop("Binding constraint with id '", + id, + "' doesn't exist in current study.") + # Update general params bc_update_pos <- which(previds %in% id) @@ -128,9 +136,6 @@ editBindingConstraint <- function(name, operator = bc_update$operator ) - # if(!is.null(name)) iniParams$name <- name - # if(!is.null(id)) iniParams$id <- id - # update parameters # name can be different of id if(!is.null(name)) @@ -152,15 +157,9 @@ editBindingConstraint <- function(name, # v870 if(opts$antaresVersion>=870){ - if(!is.null(group)){ + if(!is.null(group)) iniParams$group <- group - - # # edit group with check group values - # if(is.null(values)) - # group_values_check(group_value = group, - # values_data = values, - # opts = opts) - }else + else group <- "default" values_operator <- switch(operator, @@ -217,28 +216,39 @@ editBindingConstraint <- function(name, # write txt files # v870 if(opts$antaresVersion>=870 & !is.null(values)) - values <- .valueCheck870(values, bindingConstraints[[bc_update_pos]]$type) + values <- .valueCheck870(values, + bindingConstraints[[bc_update_pos]]$type) else - values <- .valueCheck(values, bindingConstraints[[bc_update_pos]]$type) + values <- .valueCheck(values, + bindingConstraints[[bc_update_pos]]$type) # Write Ini - writeIni(listData = bindingConstraints, pathIni = pathIni, overwrite = TRUE) + writeIni(listData = bindingConstraints, + pathIni = pathIni, + overwrite = TRUE) # Write values # v870 if(opts$antaresVersion>=870){ if(!identical(values, character(0))){ - name_file <- paste0(id, "_", values_operator, ".txt") + name_file <- paste0(id, "_", + values_operator, + ".txt") - up_path <- file.path(opts$inputPath, "bindingconstraints", name_file) + up_path <- file.path(opts$inputPath, + "bindingconstraints", + name_file) - lapply(up_path, function(x, df_ts= values, vect_path= up_path){ - index <- grep(x = vect_path, pattern = x) - fwrite(x = data.table::as.data.table(df_ts[[index]]), - file = x, - col.names = FALSE, - row.names = FALSE, - sep = "\t") + lapply(up_path, + function(x, + df_ts= values, + vect_path= up_path){ + index <- grep(x = vect_path, pattern = x) + fwrite(x = data.table::as.data.table(df_ts[[index]]), + file = x, + col.names = FALSE, + row.names = FALSE, + sep = "\t") }) } @@ -251,19 +261,6 @@ editBindingConstraint <- function(name, suppressWarnings( file_r <- fread(pathValues) ) - - # # check nrow Vs timeStep - # nrows <- switch(timeStep, - # hourly = 24*366, - # daily = 366, - # weekly = 366, - # monthly = 12, - # annual = 1) - # - # # check existing values - # if(!is.null(timeStep) & nrow(file_r)>0) - # if(!nrow(file_r) %in% nrows) - # stop("Incorrect number of rows according to the timeStep") if(!identical(values, character(0))) write.table(x = values, @@ -274,7 +271,8 @@ editBindingConstraint <- function(name, # Maj simulation suppressWarnings({ - res <- antaresRead::setSimulationPath(path = opts$studyPath, simulation = "input") + res <- antaresRead::setSimulationPath(path = opts$studyPath, + simulation = "input") }) } From c580972fbe6e74fa55864b36f893ac4068e8820b Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Fri, 23 Feb 2024 15:25:09 +0100 Subject: [PATCH 36/74] scenarioBuilder() work in progress .... --- R/scenarioBuilder.R | 8 ++++++-- man/scenario-builder.Rd | 2 +- tests/testthat/test-scenarioBuilder.R | 4 ---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/R/scenarioBuilder.R b/R/scenarioBuilder.R index 92b4d521..fb61ad65 100644 --- a/R/scenarioBuilder.R +++ b/R/scenarioBuilder.R @@ -277,7 +277,7 @@ extract_el <- function(l, indice) { #' #' @note #' `series = "ntc"` is only available with Antares >= 8.2.0. -#' `series = "binding"` is only available with Antares >= 8.7.0. +#' `series = "bc"` is only available with Antares >= 8.7.0. #' `series = "hl"` each value must be between 0 and 1. #' #' @export @@ -289,10 +289,14 @@ updateScenarioBuilder <- function(ldata, clusters_areas = NULL, links = NULL, opts = antaresRead::simOptions()) { + browser() assertthat::assert_that(inherits(opts, "simOptions")) - suppressWarnings(prevSB <- readScenarioBuilder(ruleset = ruleset, as_matrix = FALSE, opts = opts)) + suppressWarnings( + prevSB <- readScenarioBuilder(ruleset = ruleset, + as_matrix = FALSE, + opts = opts)) ref_series <- create_referential_series_type() possible_series <- ref_series$series diff --git a/man/scenario-builder.Rd b/man/scenario-builder.Rd index 985a1a73..805dfdd6 100644 --- a/man/scenario-builder.Rd +++ b/man/scenario-builder.Rd @@ -87,7 +87,7 @@ Read, create, update & deduplicate scenario builder. } \note{ \code{series = "ntc"} is only available with Antares >= 8.2.0. -\code{series = "binding"} is only available with Antares >= 8.7.0. +\code{series = "bc"} is only available with Antares >= 8.7.0. \code{series = "hl"} each value must be between 0 and 1. } \examples{ diff --git a/tests/testthat/test-scenarioBuilder.R b/tests/testthat/test-scenarioBuilder.R index 86a1b9b7..84f7ef12 100644 --- a/tests/testthat/test-scenarioBuilder.R +++ b/tests/testthat/test-scenarioBuilder.R @@ -358,10 +358,6 @@ opts_test <- antaresRead::setSimulationPath(study_latest_version, "input") # areas list antaresRead::getAreas(opts = opts_test) -# temporary to test with "870" -# force version -opts_test$antaresVersion <- 870 - test_that("scenarioBuilder works with binding constraint (v870)", { # Read, create & update scenario builder From 5d59e2c4a4d56fdbc7bc60c950debd18450159fb Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Wed, 28 Feb 2024 12:12:43 +0100 Subject: [PATCH 37/74] update rcmdcheck workflow with NOTE alert --- .github/workflows/R-CMD-check.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index c984aa41..a07a9dcd 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -44,6 +44,9 @@ jobs: extra-packages: rcmdcheck - uses: r-lib/actions/check-r-package@v2 + with: + args: 'c("--no-manual", "--as-cran")' + error-on: 'c("error", "warning", "note")' - name: Show testthat output if: always() From cf2b3b80368556d7c9bf311efc169b2cb39fd4c5 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Wed, 28 Feb 2024 12:17:26 +0100 Subject: [PATCH 38/74] update github check workflow --- .github/workflows/R-CMD-check.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index a07a9dcd..85190aa8 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -46,7 +46,7 @@ jobs: - uses: r-lib/actions/check-r-package@v2 with: args: 'c("--no-manual", "--as-cran")' - error-on: 'c("error", "warning", "note")' + error-on: '"note"' - name: Show testthat output if: always() From c428ecc73717d6dc99f267ad1ddb783405698a27 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Wed, 28 Feb 2024 15:34:23 +0100 Subject: [PATCH 39/74] updateScenarioBuilder() updated with bc + doc + tests --- R/scenarioBuilder.R | 3 +-- man/scenario-builder.Rd | 2 +- tests/testthat/test-scenarioBuilder.R | 18 ++++++++++-------- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/R/scenarioBuilder.R b/R/scenarioBuilder.R index fb61ad65..eafd1360 100644 --- a/R/scenarioBuilder.R +++ b/R/scenarioBuilder.R @@ -265,7 +265,7 @@ extract_el <- function(l, indice) { #' @param ldata A `matrix` obtained with `scenarioBuilder`, #' or a named list of matrices obtained with `scenarioBuilder`, names must be -#' 'l', 'h', 'w', 's', 't', 'r', 'ntc' or 'hl', depending on the series to update. +#' 'l', 'h', 'w', 's', 't', 'r', 'ntc', 'hl' or 'bc', depending on the series to update. #' @param series Name(s) of the serie(s) to update if `ldata` is a single `matrix`. #' @param clusters_areas A `data.table` with two columns `area` and `cluster` #' to identify area/cluster couple to update for thermal or renewable series. @@ -289,7 +289,6 @@ updateScenarioBuilder <- function(ldata, clusters_areas = NULL, links = NULL, opts = antaresRead::simOptions()) { - browser() assertthat::assert_that(inherits(opts, "simOptions")) diff --git a/man/scenario-builder.Rd b/man/scenario-builder.Rd index 805dfdd6..27748c03 100644 --- a/man/scenario-builder.Rd +++ b/man/scenario-builder.Rd @@ -63,7 +63,7 @@ deduplicateScenarioBuilder( \item{ldata}{A \code{matrix} obtained with \code{scenarioBuilder}, or a named list of matrices obtained with \code{scenarioBuilder}, names must be -'l', 'h', 'w', 's', 't', 'r', 'ntc' or 'hl', depending on the series to update.} +'l', 'h', 'w', 's', 't', 'r', 'ntc', 'hl' or 'bc', depending on the series to update.} \item{series}{Name(s) of the serie(s) to update if \code{ldata} is a single \code{matrix}.} diff --git a/tests/testthat/test-scenarioBuilder.R b/tests/testthat/test-scenarioBuilder.R index 84f7ef12..12b36b80 100644 --- a/tests/testthat/test-scenarioBuilder.R +++ b/tests/testthat/test-scenarioBuilder.R @@ -364,19 +364,21 @@ test_that("scenarioBuilder works with binding constraint (v870)", { sbuilder <- scenarioBuilder( n_scenario = opts_test$parameters$general$nbyears, - n_mc = opts_test$parameters$general$nbyears, + n_mc = 10, areas = getAreas()[1:3], - areas_rand = getAreas()[1:2], opts = opts_test ) - # Read previous scenario builder - # in a matrix format - prev_sb <- readScenarioBuilder(opts = opts_test, as_matrix = TRUE) - # Update scenario builder - # for binding constraints series - updateScenarioBuilder(ldata = sbuilder, series = "bc", opts = opts_test) + # for binding constraints series + updateScenarioBuilder(ldata = sbuilder, series = "bc") + + # Read scenario builder + # in a matrix format + prev_sb <- readScenarioBuilder(as_matrix = TRUE) + + # test + testthat::expect_equal(names(prev_sb), "bc") # remove temporary study unlink(x = study_latest_version, recursive = TRUE) From b10916ba2e1691f7b8fc4d36f0b9c2b3ae7c5bd0 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Thu, 29 Feb 2024 11:43:01 +0100 Subject: [PATCH 40/74] update news.md + doc --- NEWS.md | 16 +++++++++++++--- R/scenarioBuilder.R | 4 +++- man/scenario-builder.Rd | 4 +++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/NEWS.md b/NEWS.md index c5852d4e..eb0bd906 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,10 +1,20 @@ # antaresRead 0.7.0 (devlopment) +> Second-member coupling constraint scenarios + ### Breaking changes (Antares v8.7, cf. Antares v8.7 changelog) : -* Existing function `createBindingConstraint()` / `createBindingConstraintBulk()` has new arguments `group` -* Existing function `editBindingConstraint()` has new arguments `group` - * Integration of the scenario of the coupling constraints of second member +* `createBindingConstraint()` / `createBindingConstraintBulk()` + - New parameters `group` + - Parameter `values` is now list of `data.frame` + +* `editBindingConstraint()` + - New parameters `group` + - Parameter `values` is now list of `data.frame` + +* `removeBindingConstraint()` can now delete coupling constraints from the `group` parameter. +* `updateGeneralSettings()` adds coupling constraints to the `scenariobuilder.dat` file + # antaresEditObject 0.6.2 diff --git a/R/scenarioBuilder.R b/R/scenarioBuilder.R index eafd1360..d7665d77 100644 --- a/R/scenarioBuilder.R +++ b/R/scenarioBuilder.R @@ -276,9 +276,11 @@ extract_el <- function(l, indice) { #' #' #' @note +#' `series = "hl"` each value must be between 0 and 1. +#' #' `series = "ntc"` is only available with Antares >= 8.2.0. +#' #' `series = "bc"` is only available with Antares >= 8.7.0. -#' `series = "hl"` each value must be between 0 and 1. #' #' @export #' diff --git a/man/scenario-builder.Rd b/man/scenario-builder.Rd index 27748c03..99c5a5bf 100644 --- a/man/scenario-builder.Rd +++ b/man/scenario-builder.Rd @@ -86,9 +86,11 @@ Default is to read existing links and update them all.} Read, create, update & deduplicate scenario builder. } \note{ +\code{series = "hl"} each value must be between 0 and 1. + \code{series = "ntc"} is only available with Antares >= 8.2.0. + \code{series = "bc"} is only available with Antares >= 8.7.0. -\code{series = "hl"} each value must be between 0 and 1. } \examples{ \dontrun{ From 5e9ebc3aa478d129a413cfe8d40d5761c16bb030 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Thu, 29 Feb 2024 18:09:28 +0100 Subject: [PATCH 41/74] createBindingConstraint() update api part with endpoint --- R/createBindingConstraint.R | 68 +++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 15 deletions(-) diff --git a/R/createBindingConstraint.R b/R/createBindingConstraint.R index 2c796d37..7eb2a45f 100644 --- a/R/createBindingConstraint.R +++ b/R/createBindingConstraint.R @@ -162,27 +162,65 @@ createBindingConstraint <- function(name, # API block if (is_api_study(opts)) { + + if(opts$antaresVersion<870){ + cmd <- api_command_generate( + "create_binding_constraint", + name = name, + enabled = enabled, + time_step = timeStep, + operator = operator, + filter_year_by_year = filter_year_by_year, + filter_synthesis = filter_synthesis, + group = group, + values = values, + coeffs = lapply(as.list(coefficients), as.list) + ) + api_command_register(cmd, opts = opts) + `if`( + should_command_be_executed(opts), + api_command_execute(cmd, opts = opts, text_alert = "create_binding_constraint: {msg_api}"), + cli_command_registered("create_binding_constraint") + ) + } - cmd <- api_command_generate( - "create_binding_constraint", - name = name, - enabled = enabled, - time_step = timeStep, - operator = operator, - filter_year_by_year = filter_year_by_year, - filter_synthesis = filter_synthesis, - group = group, - values = values, - coeffs = lapply(as.list(coefficients), as.list) - ) + # v870 + body <- list(name = name, + enabled = enabled, + time_step = timeStep, + operator = operator, + filter_year_by_year = filter_year_by_year, + filter_synthesis = filter_synthesis, + group = group, + coeffs = lapply(as.list(coefficients), as.list)) + + if(!is.null(values)){ + list_values <- list(less_term_matrix = values$lt, + equal_term_matrix = values$eq, + greater_term_matrix = values$gt) + + list_values <- dropNulls(list_values) + + body <- append(body, list_values) + } + + if(is.null(body$group)) + body$group <- NULL + + body <- jsonlite::toJSON(body, + auto_unbox = TRUE) - api_command_register(cmd, opts = opts) `if`( should_command_be_executed(opts), - api_command_execute(cmd, opts = opts, text_alert = "create_binding_constraint: {msg_api}"), - cli_command_registered("create_binding_constraint") + api_post(opts, paste0(opts$study_id, "/bindingconstraints"), + body = body, + encode = "raw"), + cli::cli_alert_info("Endpoint {.emph {'/bindingconstraints'}} success") ) + + + return(invisible(opts)) } From f0e060cc230c670ff4c43cfabb482eb8002a8408 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Fri, 1 Mar 2024 16:08:04 +0100 Subject: [PATCH 42/74] createBindingConstraint() update function with standardized values before API block --- R/createBindingConstraint.R | 59 ++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/R/createBindingConstraint.R b/R/createBindingConstraint.R index 7eb2a45f..a3b875ed 100644 --- a/R/createBindingConstraint.R +++ b/R/createBindingConstraint.R @@ -147,6 +147,9 @@ createBindingConstraint <- function(name, # Check parameter values + standardization of values if(opts$antaresVersion<870) values <- .valueCheck(values, timeStep) + else + if(!is.null(values)) + values <- .valueCheck870(values, timeStep) if(!is.null(coefficients)){ names_coef <- names(coefficients) @@ -182,6 +185,7 @@ createBindingConstraint <- function(name, api_command_execute(cmd, opts = opts, text_alert = "create_binding_constraint: {msg_api}"), cli_command_registered("create_binding_constraint") ) + return(invisible(opts)) } # v870 @@ -381,10 +385,6 @@ createBindingConstraint_ <- function(bindingConstraints, # add new bc to write then in .ini file bindingConstraints[[indexBC]] <- c(iniParams, coefficients) - # Check parameter values + standardization of values - if(opts$antaresVersion>=870 & !is.null(values)) - values <- .valueCheck870(values, timeStep) - # Write values # v870 if(opts$antaresVersion>=870){ @@ -535,31 +535,36 @@ group_values_check <- function(group_value, monthly = 12, annual = 1) - list_checked <- sapply(names(values), function(x, list_in= values, check_standard_rows= nrows){ - list_work <- list_in[[x]] - - # one column scenario - if(ncol(list_work)==1){ - if (NROW(list_work) == 24*365) - list_work <- rbind(list_work, matrix(rep(0, 24*1), ncol = 1)) - if (NROW(list_work) == 365) - list_work <- rbind(list_work, matrix(rep(0, 1), ncol = 1)) - if (! NROW(list_work) %in% c(0, check_standard_rows)) - stop("Incorrect number of rows according to the timeStep") + list_checked <- sapply( + names(values), + function(x, + list_in= values, + check_standard_rows= nrows){ + + list_work <- list_in[[x]] + + # one column scenario + if(ncol(list_work)==1){ + if (NROW(list_work) == 24*365) + list_work <- rbind(list_work, matrix(rep(0, 24*1), ncol = 1)) + if (NROW(list_work) == 365) + list_work <- rbind(list_work, matrix(rep(0, 1), ncol = 1)) + if (! NROW(list_work) %in% c(0, check_standard_rows)) + stop("Incorrect number of rows according to the timeStep") }else{# scenarized columns - if(dim(list_work)[1]==24*365) - list_work <- rbind(list_work, - matrix(rep(0, 24*dim(list_work)[2]), - ncol = dim(list_work)[2])) - if(dim(list_work)[1]==365) - list_work <- rbind(list_work, - matrix(rep(0, dim(list_work)[2]), - ncol = dim(list_work)[2])) - if (! dim(list_work)[1] %in% c(0, check_standard_rows)) - stop("Incorrect number of rows according to the timeStep") + if(dim(list_work)[1]==24*365) + list_work <- rbind(list_work, + matrix(rep(0, 24*dim(list_work)[2]), + ncol = dim(list_work)[2])) + if(dim(list_work)[1]==365) + list_work <- rbind(list_work, + matrix(rep(0, dim(list_work)[2]), + ncol = dim(list_work)[2])) + if (! dim(list_work)[1] %in% c(0, check_standard_rows)) + stop("Incorrect number of rows according to the timeStep") } - list_work - }, simplify = FALSE) + list_work + }, simplify = FALSE) list_checked } From 6b1acf0c491a2d8f62c1f70d12b753abe3594279 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Wed, 13 Mar 2024 17:21:37 +0100 Subject: [PATCH 43/74] add private function to create binding constraints in api mode --- R/createBindingConstraint.R | 203 ++++++++++++++++++++---------------- 1 file changed, 116 insertions(+), 87 deletions(-) diff --git a/R/createBindingConstraint.R b/R/createBindingConstraint.R index a3b875ed..e253eb17 100644 --- a/R/createBindingConstraint.R +++ b/R/createBindingConstraint.R @@ -166,66 +166,73 @@ createBindingConstraint <- function(name, # API block if (is_api_study(opts)) { - if(opts$antaresVersion<870){ - cmd <- api_command_generate( - "create_binding_constraint", - name = name, - enabled = enabled, - time_step = timeStep, - operator = operator, - filter_year_by_year = filter_year_by_year, - filter_synthesis = filter_synthesis, - group = group, - values = values, - coeffs = lapply(as.list(coefficients), as.list) - ) - api_command_register(cmd, opts = opts) - `if`( - should_command_be_executed(opts), - api_command_execute(cmd, opts = opts, text_alert = "create_binding_constraint: {msg_api}"), - cli_command_registered("create_binding_constraint") - ) - return(invisible(opts)) - } + api_opts <- .createBindingConstraint_api(name = name, + enabled = enabled, + time_step = timeStep, + operator = operator, + filter_year_by_year = filter_year_by_year, + filter_synthesis = filter_synthesis, + values = values, + group = group, + coeffs = lapply(as.list(coefficients), as.list), + opts = opts) - # v870 - body <- list(name = name, - enabled = enabled, - time_step = timeStep, - operator = operator, - filter_year_by_year = filter_year_by_year, - filter_synthesis = filter_synthesis, - group = group, - coeffs = lapply(as.list(coefficients), as.list)) + return(invisible(api_opts)) - if(!is.null(values)){ - list_values <- list(less_term_matrix = values$lt, - equal_term_matrix = values$eq, - greater_term_matrix = values$gt) - - list_values <- dropNulls(list_values) - - body <- append(body, list_values) - } - - if(is.null(body$group)) - body$group <- NULL - - body <- jsonlite::toJSON(body, - auto_unbox = TRUE) - - `if`( - should_command_be_executed(opts), - api_post(opts, paste0(opts$study_id, "/bindingconstraints"), - body = body, - encode = "raw"), - cli::cli_alert_info("Endpoint {.emph {'/bindingconstraints'}} success") - ) - - - - - return(invisible(opts)) + # if(opts$antaresVersion<870){ + # cmd <- api_command_generate( + # "create_binding_constraint", + # name = name, + # enabled = enabled, + # time_step = timeStep, + # operator = operator, + # filter_year_by_year = filter_year_by_year, + # filter_synthesis = filter_synthesis, + # values = values, + # coeffs = lapply(as.list(coefficients), as.list) + # ) + # api_command_register(cmd, opts = opts) + # `if`( + # should_command_be_executed(opts), + # api_command_execute(cmd, opts = opts, text_alert = "create_binding_constraint: {msg_api}"), + # cli_command_registered("create_binding_constraint") + # ) + # return(invisible(opts)) + # } + # + # # v870 + # body <- list(name = name, + # enabled = enabled, + # time_step = timeStep, + # operator = operator, + # filter_year_by_year = filter_year_by_year, + # filter_synthesis = filter_synthesis, + # group = group, + # coeffs = lapply(as.list(coefficients), as.list)) + # + # if(!is.null(values)){ + # list_values <- list(less_term_matrix = values$lt, + # equal_term_matrix = values$eq, + # greater_term_matrix = values$gt) + # + # list_values <- dropNulls(list_values) + # + # body <- append(body, list_values) + # } + # + # if(is.null(body$group)) + # body$group <- NULL + # + # body <- jsonlite::toJSON(body, + # auto_unbox = TRUE) + # + # should_command_be_executed(opts) + # api_post(opts, paste0(opts$study_id, "/bindingconstraints"), + # body = body, + # encode = "raw") + # cli::cli_alert_info("Endpoint {.emph {'/bindingconstraints'}} success") + # + # return(invisible(opts)) } ## Ini file @@ -300,6 +307,57 @@ createBindingConstraint <- function(name, } +.createBindingConstraint_api <- function(..., opts){ + # Date: Thu, 14 Mar 2024 09:38:25 +0100 Subject: [PATCH 44/74] cleaning create_binding_constraint() --- R/createBindingConstraint.R | 79 ++++++------------------------------- 1 file changed, 12 insertions(+), 67 deletions(-) diff --git a/R/createBindingConstraint.R b/R/createBindingConstraint.R index e253eb17..eda7fa09 100644 --- a/R/createBindingConstraint.R +++ b/R/createBindingConstraint.R @@ -165,74 +165,19 @@ createBindingConstraint <- function(name, # API block if (is_api_study(opts)) { - - api_opts <- .createBindingConstraint_api(name = name, - enabled = enabled, - time_step = timeStep, - operator = operator, - filter_year_by_year = filter_year_by_year, - filter_synthesis = filter_synthesis, - values = values, - group = group, - coeffs = lapply(as.list(coefficients), as.list), - opts = opts) + api_opts <- .createBC_api(name = name, + enabled = enabled, + time_step = timeStep, + operator = operator, + filter_year_by_year = filter_year_by_year, + filter_synthesis = filter_synthesis, + values = values, + group = group, + coeffs = lapply(as.list(coefficients), + as.list), + opts = opts) return(invisible(api_opts)) - - # if(opts$antaresVersion<870){ - # cmd <- api_command_generate( - # "create_binding_constraint", - # name = name, - # enabled = enabled, - # time_step = timeStep, - # operator = operator, - # filter_year_by_year = filter_year_by_year, - # filter_synthesis = filter_synthesis, - # values = values, - # coeffs = lapply(as.list(coefficients), as.list) - # ) - # api_command_register(cmd, opts = opts) - # `if`( - # should_command_be_executed(opts), - # api_command_execute(cmd, opts = opts, text_alert = "create_binding_constraint: {msg_api}"), - # cli_command_registered("create_binding_constraint") - # ) - # return(invisible(opts)) - # } - # - # # v870 - # body <- list(name = name, - # enabled = enabled, - # time_step = timeStep, - # operator = operator, - # filter_year_by_year = filter_year_by_year, - # filter_synthesis = filter_synthesis, - # group = group, - # coeffs = lapply(as.list(coefficients), as.list)) - # - # if(!is.null(values)){ - # list_values <- list(less_term_matrix = values$lt, - # equal_term_matrix = values$eq, - # greater_term_matrix = values$gt) - # - # list_values <- dropNulls(list_values) - # - # body <- append(body, list_values) - # } - # - # if(is.null(body$group)) - # body$group <- NULL - # - # body <- jsonlite::toJSON(body, - # auto_unbox = TRUE) - # - # should_command_be_executed(opts) - # api_post(opts, paste0(opts$study_id, "/bindingconstraints"), - # body = body, - # encode = "raw") - # cli::cli_alert_info("Endpoint {.emph {'/bindingconstraints'}} success") - # - # return(invisible(opts)) } ## Ini file @@ -307,7 +252,7 @@ createBindingConstraint <- function(name, } -.createBindingConstraint_api <- function(..., opts){ +.createBC_api <- function(..., opts){ # Date: Mon, 18 Mar 2024 16:17:22 +0100 Subject: [PATCH 45/74] createBindingConstraint() update cli message api mode --- R/createBindingConstraint.R | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/R/createBindingConstraint.R b/R/createBindingConstraint.R index eda7fa09..25b2fa99 100644 --- a/R/createBindingConstraint.R +++ b/R/createBindingConstraint.R @@ -293,11 +293,12 @@ createBindingConstraint <- function(name, auto_unbox = TRUE) # send request - should_command_be_executed(opts) api_post(opts, paste0(opts$study_id, "/bindingconstraints"), body = body, encode = "raw") - cli::cli_alert_info("Endpoint {.emph {'/bindingconstraints'}} success") + bc_name <- list(...)$name + cli::cli_alert_info("Endpoint {.emph {'Create bindingconstraints'}} {.emph + {.strong {bc_name}}} success") return(invisible(opts)) } From b2d5dfcca790da61e77c07374c73f849f803991a Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Mon, 18 Mar 2024 16:37:25 +0100 Subject: [PATCH 46/74] editBindingConstraint() update api part + doc --- R/editBindingConstraint.R | 119 ++++++++++++++++++++++++++--------- man/editBindingConstraint.Rd | 10 ++- 2 files changed, 96 insertions(+), 33 deletions(-) diff --git a/R/editBindingConstraint.R b/R/editBindingConstraint.R index a465ff3d..8dc70ff7 100644 --- a/R/editBindingConstraint.R +++ b/R/editBindingConstraint.R @@ -17,6 +17,11 @@ #' @family binding constraints functions #' #' @section Warning: +#' Put values with rights dimensions : +#' - hourly : 8784 +#' - daily = 366 +#' +#' #' **>= v8.7.0** : For each constraint name, one file .txt containing `_lt.txt, _gt.txt, _eq.txt` #' Parameter `values` must be named `list` ("lt", "gt", "eq") containing `data.frame` scenarized. #' see example section below. @@ -31,7 +36,7 @@ #' # < v8.7.0 : #' editBindingConstraint( #' name = "myconstraint", -#' values = matrix(data = rep(0, 8760 * 3), ncol = 3), +#' values = matrix(data = rep(0, 8784 * 3), ncol = 3), #' enabled = FALSE, #' timeStep = "hourly", #' operator = "both", @@ -41,7 +46,7 @@ #' # >= v8.7.0 : #' #' # data values scenarized (hourly) -#' df <- matrix(data = rep(0, 8760 * 3), ncol = 3) +#' df <- matrix(data = rep(0, 8784 * 3), ncol = 3) #' #' # you can provide list data with all value #' # or just according with 'operator' (ex : 'lt' for 'less) @@ -72,37 +77,20 @@ editBindingConstraint <- function(name, opts = antaresRead::simOptions()) { assertthat::assert_that(inherits(opts, "simOptions")) - # API block + ## API block ---- if (is_api_study(opts)) { + opts_api <- .editBC_api(id = name, + enabled = enabled, + time_step = timeStep, + operator = operator, + filter_year_by_year = filter_year_by_year, + filter_synthesis = filter_synthesis, + values = values, + coeffs = lapply(as.list(coefficients), as.list), + group = group, + opts = opts) - if (is.null(timeStep)) - stop("You must provide `timeStep` argument with API.", - call. = FALSE) - if (is.null(operator)) - stop("You must provide `operator` argument with API.", - call. = FALSE) - - cmd <- api_command_generate( - "update_binding_constraint", - id = name, - enabled = enabled, - time_step = timeStep, - operator = operator, - filter_year_by_year = filter_year_by_year, - filter_synthesis = filter_synthesis, - values = values, - coeffs = lapply(as.list(coefficients), as.list) - ) - - api_command_register(cmd, opts = opts) - `if`( - should_command_be_executed(opts), - api_command_execute(cmd, opts = opts, - text_alert = "update_binding_constraint: {msg_api}"), - cli_command_registered("update_binding_constraint") - ) - - return(invisible(opts)) + return(invisible(opts_api)) } # valuesIn <- values @@ -276,3 +264,72 @@ editBindingConstraint <- function(name, }) } + +# api part code +.editBC_api <- function(..., opts){ + args <- list(...) + # multi vers checks (legacy) + if (is.null(args$time_step)) + stop("You must provide `timeStep` argument with API.", + call. = FALSE) + if (is.null(args$time_step)) + stop("You must provide `operator` argument with API.", + call. = FALSE) + + # =v870 + + # reforge list structure + if(!is.null(args$values)){ + list_values <- list(less_term_matrix = args$values$lt, + equal_term_matrix = args$values$eq, + greater_term_matrix = args$values$gt) + + list_values <- dropNulls(list_values) + args$values <- NULL + + args <- append(args, list_values) + } + + # delete NULL from parameters + args <- dropNulls(args) + + # loop for every parameter (legacy endpoint) + names_to_keep <- setdiff(names(args), "id") + id_bc <- args$id + + lapply(names_to_keep, function(x){ + select_element <- args[x] + body <- list(key= names(select_element), + value= args[[x]]) + # make json file + body <- jsonlite::toJSON(body, + auto_unbox = TRUE) + # send request + api_put(opts = opts, + endpoint = file.path(opts$study_id, "bindingconstraints", id_bc), + body = body, + encode = "raw") + }) + + cli::cli_alert_info("Endpoint {.emph {'Update bindingconstraints'}} {.emph + {.strong {id_bc}}} success") + + return(invisible(opts)) +} \ No newline at end of file diff --git a/man/editBindingConstraint.Rd b/man/editBindingConstraint.Rd index b922521e..d3d0d4bd 100644 --- a/man/editBindingConstraint.Rd +++ b/man/editBindingConstraint.Rd @@ -57,6 +57,12 @@ The key search value of the constraint is the \code{id} field } \section{Warning}{ +Put values with rights dimensions : +\itemize{ +\item hourly : 8784 +\item daily = 366 +} + \strong{>= v8.7.0} : For each constraint name, one file .txt containing \verb{_lt.txt, _gt.txt, _eq.txt} Parameter \code{values} must be named \code{list} ("lt", "gt", "eq") containing \code{data.frame} scenarized. see example section below. @@ -67,7 +73,7 @@ see example section below. # < v8.7.0 : editBindingConstraint( name = "myconstraint", - values = matrix(data = rep(0, 8760 * 3), ncol = 3), + values = matrix(data = rep(0, 8784 * 3), ncol = 3), enabled = FALSE, timeStep = "hourly", operator = "both", @@ -77,7 +83,7 @@ editBindingConstraint( # >= v8.7.0 : # data values scenarized (hourly) -df <- matrix(data = rep(0, 8760 * 3), ncol = 3) +df <- matrix(data = rep(0, 8784 * 3), ncol = 3) # you can provide list data with all value # or just according with 'operator' (ex : 'lt' for 'less) From ab165f65989387fba38ca4c5a5434d48be100cca Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Tue, 19 Mar 2024 17:33:20 +0100 Subject: [PATCH 47/74] removeBindingConstraint() updated with api part + doc --- R/removeBindingConstraint.R | 53 ++++++++++++++++++++++++---------- man/removeBindingConstraint.Rd | 4 +-- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/R/removeBindingConstraint.R b/R/removeBindingConstraint.R index 7fbd77b2..8a263b00 100644 --- a/R/removeBindingConstraint.R +++ b/R/removeBindingConstraint.R @@ -8,7 +8,7 @@ #' #' #' @param name Name(s) of the binding constraint(s) to remove. -#' @param group `character` Name(s) of group to delete +#' @param group `character` Name(s) of group to delete (**disk mode only**) #' #' @template opts #' @@ -21,7 +21,7 @@ #'# < v8.7.0 : #' removeBindingConstraint(name = "mybindingconstraint") #' -#' # >= v8.7.0 (delete by names group) : +#' # >= v8.7.0 (delete by names group **ONLY WITH DISK MODE**) : #' # read #' bc <- readBindingConstraints() #' @@ -51,19 +51,9 @@ removeBindingConstraint <- function(name = NULL, # API block if (is_api_study(opts)) { - for (i in name) { - cmd <- api_command_generate( - "remove_binding_constraint", - id = i - ) - api_command_register(cmd, opts = opts) - `if`( - should_command_be_executed(opts), - api_command_execute(cmd, opts = opts, text_alert = "remove_binding_constraint: {msg_api}"), - cli_command_registered("remove_binding_constraint") - ) - } - return(invisible(opts)) + opts_api <- .remove_bc_api(name = name, opts = opts) + + return(invisible(opts_api)) } ## read Ini file @@ -167,3 +157,36 @@ removeBindingConstraint <- function(name = NULL, updated_bc } } + +.remove_bc_api <- function(..., opts){ + args <- list(...) + # =v870 + lapply(args$name, function(x){ + # send request + api_delete(opts = opts, + endpoint = file.path(opts$study_id, "bindingconstraints", x), + encode = "raw") + + cli::cli_alert_info("Endpoint {.emph {'Delete bindingconstraints'}} {.emph + {.strong {x}}} success") + }) + return(invisible(opts)) +} diff --git a/man/removeBindingConstraint.Rd b/man/removeBindingConstraint.Rd index 14c9d342..0be62a9a 100644 --- a/man/removeBindingConstraint.Rd +++ b/man/removeBindingConstraint.Rd @@ -13,7 +13,7 @@ removeBindingConstraint( \arguments{ \item{name}{Name(s) of the binding constraint(s) to remove.} -\item{group}{\code{character} Name(s) of group to delete} +\item{group}{\code{character} Name(s) of group to delete (\strong{disk mode only})} \item{opts}{List of simulation parameters returned by the function \code{\link[antaresRead:setSimulationPath]{antaresRead::setSimulationPath()}}} @@ -32,7 +32,7 @@ Remove a binding constraint in an Antares study. # < v8.7.0 : removeBindingConstraint(name = "mybindingconstraint") -# >= v8.7.0 (delete by names group) : +# >= v8.7.0 (delete by names group **ONLY WITH DISK MODE**) : # read bc <- readBindingConstraints() From 6ab76fb4e7750d60392cd546db6e96a51f59f24f Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Thu, 21 Mar 2024 14:38:09 +0100 Subject: [PATCH 48/74] removeBindingConstraint() updated to delete bc with group name in api mode --- R/removeBindingConstraint.R | 60 ++++++++++++++++++++++++++++------ man/removeBindingConstraint.Rd | 8 +++-- 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/R/removeBindingConstraint.R b/R/removeBindingConstraint.R index 8a263b00..45388079 100644 --- a/R/removeBindingConstraint.R +++ b/R/removeBindingConstraint.R @@ -8,10 +8,14 @@ #' #' #' @param name Name(s) of the binding constraint(s) to remove. -#' @param group `character` Name(s) of group to delete (**disk mode only**) +#' @param group `character` Name(s) of group to delete #' #' @template opts #' +#' @note +#' Starting with version **v8.7.0**, you can delete binding constraints by +#' name or by group. +#' #' @family binding constraints functions #' #' @export @@ -21,7 +25,7 @@ #'# < v8.7.0 : #' removeBindingConstraint(name = "mybindingconstraint") #' -#' # >= v8.7.0 (delete by names group **ONLY WITH DISK MODE**) : +#' # >= v8.7.0 (delete by names group) : #' # read #' bc <- readBindingConstraints() #' @@ -51,7 +55,9 @@ removeBindingConstraint <- function(name = NULL, # API block if (is_api_study(opts)) { - opts_api <- .remove_bc_api(name = name, opts = opts) + opts_api <- .remove_bc_api(name = name, + group = group, + opts = opts) return(invisible(opts_api)) } @@ -179,14 +185,48 @@ removeBindingConstraint <- function(name = NULL, } # >=v870 - lapply(args$name, function(x){ - # send request - api_delete(opts = opts, - endpoint = file.path(opts$study_id, "bindingconstraints", x), - encode = "raw") + # delete by group(s) name(s) + if(!is.null(args$group)){ + group <- args$group + all_bc <- readBindingConstraints(opts = opts) + + # extract groups + bc_groups <- sapply(all_bc, function(x){ + x$properties$group + }) + + # check + if(!all(group%in%bc_groups)) + stop(paste0("No binding constraint with group '", + group[!group%in%bc_groups], "'"), + call. = FALSE) + + # select name to delete + index <- which(bc_groups%in%group) + names_to_delete <- sapply(index, + function(x, + bc = all_bc){ + bc[[x]]$properties$id + }) + + # delete names + lapply(names_to_delete, function(x){ + # send request + api_delete(opts = opts, + endpoint = file.path(opts$study_id, "bindingconstraints", x), + encode = "raw") + cli::cli_alert_info("Endpoint {.emph {'Delete bindingconstraints'}} {.emph + {.strong {x}}} success") + }) - cli::cli_alert_info("Endpoint {.emph {'Delete bindingconstraints'}} {.emph - {.strong {x}}} success") + }else + lapply(args$name, function(x){ + # send request + api_delete(opts = opts, + endpoint = file.path(opts$study_id, "bindingconstraints", x), + encode = "raw") + cli::cli_alert_info("Endpoint {.emph {'Delete bindingconstraints'}} {.emph + {.strong {x}}} success") }) return(invisible(opts)) } diff --git a/man/removeBindingConstraint.Rd b/man/removeBindingConstraint.Rd index 0be62a9a..5caf004d 100644 --- a/man/removeBindingConstraint.Rd +++ b/man/removeBindingConstraint.Rd @@ -13,7 +13,7 @@ removeBindingConstraint( \arguments{ \item{name}{Name(s) of the binding constraint(s) to remove.} -\item{group}{\code{character} Name(s) of group to delete (\strong{disk mode only})} +\item{group}{\code{character} Name(s) of group to delete} \item{opts}{List of simulation parameters returned by the function \code{\link[antaresRead:setSimulationPath]{antaresRead::setSimulationPath()}}} @@ -27,12 +27,16 @@ An updated list containing various information about the simulation. Remove a binding constraint in an Antares study. } +\note{ +Starting with version \strong{v8.7.0}, you can delete binding constraints by +name or by group. +} \examples{ \dontrun{ # < v8.7.0 : removeBindingConstraint(name = "mybindingconstraint") -# >= v8.7.0 (delete by names group **ONLY WITH DISK MODE**) : +# >= v8.7.0 (delete by names group) : # read bc <- readBindingConstraints() From 97b0f20db80495a13ea73f3a7f485586c23089e4 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Fri, 22 Mar 2024 17:46:08 +0100 Subject: [PATCH 49/74] scenarioBuilder() updated with 3 new parameters and manage code with private function + doc + tests --- R/scenarioBuilder.R | 147 ++++++++++++++++++++++---- man/scenario-builder.Rd | 11 +- tests/testthat/test-scenarioBuilder.R | 4 +- 3 files changed, 141 insertions(+), 21 deletions(-) diff --git a/R/scenarioBuilder.R b/R/scenarioBuilder.R index d7665d77..64f7355a 100644 --- a/R/scenarioBuilder.R +++ b/R/scenarioBuilder.R @@ -9,6 +9,9 @@ #' @param n_mc Number of Monte-Carlo years. #' @param areas Areas to use in scenario builder, if `NULL` (default) all areas in Antares study are used. #' @param areas_rand Areas for which to use `"rand"`. +#' @param group_bc `character` Bindgind constraints's groups names to use. +#' @param group_bc_rand `character` Bindgind constraints which to use `"rand"`. +#' @param mode `character` "bc" to edit binding constraints. #' @param coef_hydro_levels Hydro levels coefficients. #' @param opts #' List of simulation parameters returned by the function @@ -92,54 +95,160 @@ #' #' deduplicateScenarioBuilder() #' } -scenarioBuilder <- function(n_scenario, +scenarioBuilder <- function(n_scenario = NULL, n_mc = NULL, areas = NULL, areas_rand = NULL, + group_bc = NULL, + group_bc_rand = NULL, coef_hydro_levels = NULL, + mode = NULL, opts = antaresRead::simOptions()) { + if (is_api_study(opts) && is_api_mocked(opts)) { stopifnot("In mocked API mode, n_mc cannot be NULL" = !is.null(n_mc)) - stopifnot("In mocked API mode, areas cannot be NULL" = !is.null(n_mc)) + stopifnot("In mocked API mode, areas cannot be NULL" = !is.null(areas)) } - if (is.null(areas)) { - areas <- antaresRead::getAreas(opts = opts) + + # check version >=870 from group parameter + if(!opts$antaresVersion >= 870 & !is.null(group_bc)) + stop("Parameter 'group_bc' is only for Antares study version >= v8.7.0", + call. = FALSE) + if(!opts$antaresVersion >= 870 & !is.null(group_bc_rand)) + stop("Parameter 'group_bc_rand' is only for Antares study version >= v8.7.0", + call. = FALSE) + + # >=v870 + if(opts$antaresVersion >= 870){ + # update with bc + if(mode %in% "bc"){ + # read groups + if(is.null(group_bc)){ + group_bc <- readBindingConstraints(opts = opts) + group_bc <- sapply(group_bc, function(x){ + x$properties$group + }) + } + else + group_bc <- unique(c(group_bc, group_bc_rand)) + + # check parameters + if (!all(group_bc_rand %in% group_bc)) + warning("Some 'group_bc_rand' are not Antares 'group_bc'", call. = FALSE) + + # n_mc parameter + if (is.null(n_mc)) + n_mc <- opts$parameters$general$nbyears + else + if (isTRUE(n_mc != opts$parameters$general$nbyears)) + warning("Specified number of Monte-Carlo years differ from the one in Antares general parameter", call. = FALSE) + } + + # write data + data_mat <- rep(rep_len(seq_len(n_scenario), + n_mc), + length(group_bc)) + + sb <- matrix( + data = data_mat, + byrow = TRUE, + nrow = length(group_bc), + dimnames = list(group_bc, NULL) + ) + sb[group_bc %in% group_bc_rand, ] <- "rand" + + return(sb) + } + else # without bc + .manage_parameter(n_scenario = n_scenario, + n_mc = n_mc, + areas = areas, + areas_rand = areas_rand, + coef_hydro_levels = coef_hydro_levels, + opts = opts) + + # if (is.null(areas)) { + # areas <- antaresRead::getAreas(opts = opts) + # } else { + # areas <- unique(c(areas, areas_rand)) + # } + # if (!all(areas_rand %in% areas)) { + # warning("Some 'areas_rand' are not Antares' areas", call. = FALSE) + # } + # if (is.null(n_mc)) { + # n_mc <- opts$parameters$general$nbyears + # } else { + # if (isTRUE(n_mc != opts$parameters$general$nbyears)) { + # warning("Specified number of Monte-Carlo years differ from the one in Antares general parameter", call. = FALSE) + # } + # } + # + # if (!is.null(coef_hydro_levels)) { + # nb_areas <- length(areas) + # nb_coef_hydro_levels <- length(coef_hydro_levels) + # if (nb_coef_hydro_levels == nb_areas) { + # data_mat <- rep(coef_hydro_levels, each = n_mc) + # } else if(nb_coef_hydro_levels == nb_areas * n_mc) { + # data_mat <- coef_hydro_levels + # } else { + # stop("Please check the number of areas and the number of coefficients for hydro levels that you provided.") + # } + # } else { + # data_mat <- rep_len(seq_len(n_scenario), length(areas) * n_mc) + # } + # + # sb <- matrix( + # data = data_mat, + # byrow = TRUE, + # nrow = length(areas), + # dimnames = list(areas, NULL) + # ) + # sb[areas %in% areas_rand, ] <- "rand" + # + # return(sb) +} + +.manage_parameter <- function(..., opts){ + args <- list(...) + if (is.null(args$areas)) { + args$areas <- antaresRead::getAreas(opts = opts) } else { - areas <- unique(c(areas, areas_rand)) + args$areas <- unique(c(args$areas, args$areas_rand)) } - if (!all(areas_rand %in% areas)) { + if (!all(args$areas_rand %in% args$areas)) { warning("Some 'areas_rand' are not Antares' areas", call. = FALSE) } - if (is.null(n_mc)) { - n_mc <- opts$parameters$general$nbyears + if (is.null(args$n_mc)) { + args$n_mc <- opts$parameters$general$nbyears } else { - if (isTRUE(n_mc != opts$parameters$general$nbyears)) { + if (isTRUE(args$n_mc != opts$parameters$general$nbyears)) { warning("Specified number of Monte-Carlo years differ from the one in Antares general parameter", call. = FALSE) } } - if (!is.null(coef_hydro_levels)) { - nb_areas <- length(areas) - nb_coef_hydro_levels <- length(coef_hydro_levels) + if (!is.null(args$coef_hydro_levels)) { + nb_areas <- length(args$areas) + nb_coef_hydro_levels <- length(args$coef_hydro_levels) if (nb_coef_hydro_levels == nb_areas) { - data_mat <- rep(coef_hydro_levels, each = n_mc) - } else if(nb_coef_hydro_levels == nb_areas * n_mc) { - data_mat <- coef_hydro_levels + data_mat <- rep(args$coef_hydro_levels, each = args$n_mc) + } else if(nb_coef_hydro_levels == nb_areas * args$n_mc) { + data_mat <- args$coef_hydro_levels } else { stop("Please check the number of areas and the number of coefficients for hydro levels that you provided.") } } else { - data_mat <- rep_len(seq_len(n_scenario), length(areas) * n_mc) + data_mat <- rep_len(seq_len(args$n_scenario), + length(args$areas) * args$n_mc) } sb <- matrix( data = data_mat, byrow = TRUE, - nrow = length(areas), - dimnames = list(areas, NULL) + nrow = length(args$areas), + dimnames = list(args$areas, NULL) ) - sb[areas %in% areas_rand, ] <- "rand" + sb[args$areas %in% args$areas_rand, ] <- "rand" return(sb) } diff --git a/man/scenario-builder.Rd b/man/scenario-builder.Rd index 99c5a5bf..ea360778 100644 --- a/man/scenario-builder.Rd +++ b/man/scenario-builder.Rd @@ -10,11 +10,14 @@ \title{Read, create, update & deduplicate scenario builder} \usage{ scenarioBuilder( - n_scenario, + n_scenario = NULL, n_mc = NULL, areas = NULL, areas_rand = NULL, + group_bc = NULL, + group_bc_rand = NULL, coef_hydro_levels = NULL, + mode = NULL, opts = antaresRead::simOptions() ) @@ -52,8 +55,14 @@ deduplicateScenarioBuilder( \item{areas_rand}{Areas for which to use \code{"rand"}.} +\item{group_bc}{\code{character} Bindgind constraints's groups names to use.} + +\item{group_bc_rand}{\code{character} Bindgind constraints which to use \code{"rand"}.} + \item{coef_hydro_levels}{Hydro levels coefficients.} +\item{mode}{\code{character} "bc" to edit binding constraints.} + \item{opts}{List of simulation parameters returned by the function \code{\link[antaresRead:setSimulationPath]{antaresRead::setSimulationPath()}}} diff --git a/tests/testthat/test-scenarioBuilder.R b/tests/testthat/test-scenarioBuilder.R index 12b36b80..b3437257 100644 --- a/tests/testthat/test-scenarioBuilder.R +++ b/tests/testthat/test-scenarioBuilder.R @@ -365,7 +365,9 @@ test_that("scenarioBuilder works with binding constraint (v870)", { sbuilder <- scenarioBuilder( n_scenario = opts_test$parameters$general$nbyears, n_mc = 10, - areas = getAreas()[1:3], + group_bc = c("group_test", "default"), + group_bc_rand = "default", + mode = "bc", opts = opts_test ) From d16663575d13c2effe1276db201fa8d5d11ca4ca Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Tue, 26 Mar 2024 14:35:42 +0100 Subject: [PATCH 50/74] scenarioBuilder(), readScenarioBuilder() updated to be able with bc scenarized + doc + tests --- NAMESPACE | 1 + R/scenarioBuilder.R | 272 +++++++++++++++----------- man/scenario-builder.Rd | 15 +- tests/testthat/test-scenarioBuilder.R | 94 ++++++++- 4 files changed, 252 insertions(+), 130 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 9cf1dba3..048c6725 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -30,6 +30,7 @@ export(createPSP) export(createStudy) export(createStudyAPI) export(createVariant) +export(create_referential_series_type) export(deduplicateScenarioBuilder) export(deleteStudy) export(dicoGeneralSettings) diff --git a/R/scenarioBuilder.R b/R/scenarioBuilder.R index 64f7355a..dad8a8da 100644 --- a/R/scenarioBuilder.R +++ b/R/scenarioBuilder.R @@ -61,6 +61,17 @@ #' ) #' ) #' +#' # Create a scenario builder matrix with +#' # bindings constraints groups (study version >= 8.7.0) +#' # Use parameter "mode" with "bc" +#' sbuilder <- scenarioBuilder( +#' n_scenario = 51, +#' n_mc = 2040, +#' group_bc = c("my_bc_1", "my_bc_2"), +#' group_bc_rand = "my_bc_2", +#' mode = "bc" +#' ) +#' #' # Read previous scenario builder #' # in a matrix format #' prev_sb <- readScenarioBuilder() @@ -74,6 +85,8 @@ #' # equivalent as #' updateScenarioBuilder(ldata = list(l = sbuilder)) #' +#' # for binding constraints (study version >= 8.7.0) +#' updateScenarioBuilder(ldata = sbuilder, series = "bc") #' #' # update several series #' @@ -95,7 +108,7 @@ #' #' deduplicateScenarioBuilder() #' } -scenarioBuilder <- function(n_scenario = NULL, +scenarioBuilder <- function(n_scenario = 1, n_mc = NULL, areas = NULL, areas_rand = NULL, @@ -111,6 +124,9 @@ scenarioBuilder <- function(n_scenario = NULL, stopifnot("In mocked API mode, areas cannot be NULL" = !is.null(areas)) } + if(n_scenario %in% 1) + warning("'n_scenario' parameter set to default value {1}", call. = FALSE) + # check version >=870 from group parameter if(!opts$antaresVersion >= 870 & !is.null(group_bc)) stop("Parameter 'group_bc' is only for Antares study version >= v8.7.0", @@ -122,95 +138,35 @@ scenarioBuilder <- function(n_scenario = NULL, # >=v870 if(opts$antaresVersion >= 870){ # update with bc - if(mode %in% "bc"){ - # read groups - if(is.null(group_bc)){ - group_bc <- readBindingConstraints(opts = opts) - group_bc <- sapply(group_bc, function(x){ - x$properties$group - }) - } - else - group_bc <- unique(c(group_bc, group_bc_rand)) - - # check parameters - if (!all(group_bc_rand %in% group_bc)) - warning("Some 'group_bc_rand' are not Antares 'group_bc'", call. = FALSE) - - # n_mc parameter - if (is.null(n_mc)) - n_mc <- opts$parameters$general$nbyears - else - if (isTRUE(n_mc != opts$parameters$general$nbyears)) - warning("Specified number of Monte-Carlo years differ from the one in Antares general parameter", call. = FALSE) - } - - # write data - data_mat <- rep(rep_len(seq_len(n_scenario), - n_mc), - length(group_bc)) - - sb <- matrix( - data = data_mat, - byrow = TRUE, - nrow = length(group_bc), - dimnames = list(group_bc, NULL) - ) - sb[group_bc %in% group_bc_rand, ] <- "rand" - - return(sb) + if(!is.null(mode) && mode %in% "bc") + .manage_parameter_bc(n_scenario = n_scenario, + n_mc = n_mc, + group_bc = group_bc, + group_bc_rand = group_bc_rand, + opts = opts) + else # without bc + .manage_parameter(n_scenario = n_scenario, + n_mc = n_mc, + areas = areas, + areas_rand = areas_rand, + coef_hydro_levels = coef_hydro_levels, + opts = opts) } - else # without bc + else # =v870 paradigm +.manage_parameter_bc <- function(..., opts){ + args <- list(...) + + # read groups + if(is.null(args$group_bc)){ + args$group_bc <- readBindingConstraints(opts = opts) + args$group_bc <- sapply(group_bc, function(x){ + x$properties$group + }) + } + else + group_bc <- unique(c(args$group_bc, args$group_bc_rand)) + + # check parameters + if (!all(args$group_bc_rand %in% args$group_bc)) + warning("Some 'group_bc_rand' are not Antares 'group_bc'", + call. = FALSE) + + # n_mc parameter + if (is.null(args$n_mc)) + args$n_mc <- opts$parameters$general$nbyears + else + if (isTRUE(args$n_mc != opts$parameters$general$nbyears)) + warning("Specified number of Monte-Carlo years differ from the one in Antares general parameter", + call. = FALSE) + + # write data + data_mat <- rep(rep_len(seq_len(args$n_scenario), + args$n_mc), + length(args$group_bc)) + + sb <- matrix( + data = data_mat, + byrow = TRUE, + nrow = length(args$group_bc), + dimnames = list(args$group_bc, NULL) + ) + sb[args$group_bc %in% args$group_bc_rand, ] <- "rand" + + return(sb) +} + #' @title Create the correspondence data frame between the symbol and the type in scenario builder #' @return a `data.frame`. +#' @export create_referential_series_type <- function(){ ref_series <- data.frame("series" = c("l", "h", "w", "s", "t", "r", "ntc", "hl", "bc"), @@ -279,7 +281,7 @@ create_referential_series_type <- function(){ readScenarioBuilder <- function(ruleset = "Default Ruleset", as_matrix = TRUE, opts = antaresRead::simOptions()) { - assertthat::assert_that(inherits(opts, "simOptions")) +assertthat::assert_that(inherits(opts, "simOptions")) # read existing scenariobuilder.dat if (is_api_study(opts)) { @@ -318,49 +320,75 @@ readScenarioBuilder <- function(ruleset = "Default Ruleset", X = sbt, FUN = function(x) { type <- extract_el(x, 1)[1] - areas <- extract_el(x, 2) - if (type %in% c("t", "r")) { - clusters <- extract_el(x, 4) - areas <- paste(areas, clusters, sep = "_") - # all_areas <- areas # for the moment - if (type == "t") { - clusdesc <- readClusterDesc(opts = opts) + + # >= v870 : scenarized binding constraints + if(type %in% "bc"){ + # extract informations for matrix output + bc_groups <- extract_el(x, 2) + years <- extract_el(x, 3) + + # output format + if (as_matrix) { + SB <- data.table( + group = bc_groups, + years = as.numeric(years) + 1, + values = unlist(x, use.names = FALSE) + ) + + SB <- dcast(data = SB, + formula = group ~ years, + value.var = "values") + mat <- as.matrix(SB, rownames = 1) + colnames(mat) <- NULL + mat + } else + x + }else{ + areas <- extract_el(x, 2) + if (type %in% c("t", "r")) { + clusters <- extract_el(x, 4) + areas <- paste(areas, clusters, sep = "_") + # all_areas <- areas # for the moment + if (type == "t") { + clusdesc <- readClusterDesc(opts = opts) + } else { + if (packageVersion("antaresRead") < "2.2.8") + stop("You need to install a more recent version of antaresRead (>2.2.8)", call. = FALSE) + if (!exists("readClusterResDesc", where = "package:antaresRead", mode = "function")) + stop("You need to install a more recent version of antaresRead (>2.2.8)", call. = FALSE) + read_cluster_res_desc <- getFromNamespace("readClusterResDesc", ns = "antaresRead") + clusdesc <- read_cluster_res_desc(opts = opts) + } + all_areas <- paste(clusdesc$area, clusdesc$cluster, sep = "_") } else { - if (packageVersion("antaresRead") < "2.2.8") - stop("You need to install a more recent version of antaresRead (>2.2.8)", call. = FALSE) - if (!exists("readClusterResDesc", where = "package:antaresRead", mode = "function")) - stop("You need to install a more recent version of antaresRead (>2.2.8)", call. = FALSE) - read_cluster_res_desc <- getFromNamespace("readClusterResDesc", ns = "antaresRead") - clusdesc <- read_cluster_res_desc(opts = opts) + all_areas <- getAreas(opts = opts) } - all_areas <- paste(clusdesc$area, clusdesc$cluster, sep = "_") - } else { - all_areas <- getAreas(opts = opts) - } - if (type %in% c("ntc")) { - areas2 <- extract_el(x, 3) - areas <- paste(areas, areas2, sep = "%") - years <- extract_el(x, 4) - } else { - years <- extract_el(x, 3) - } - - if (as_matrix) { - SB <- data.table( - areas = areas, - years = as.numeric(years) + 1, - values = unlist(x, use.names = FALSE) - ) - if (!type %in% c("ntc")) { - SB <- SB[CJ(areas = all_areas, years = seq_len(opts$parameters$general$nbyears)), on = c("areas", "years")] + if (type %in% c("ntc")) { + areas2 <- extract_el(x, 3) + areas <- paste(areas, areas2, sep = "%") + years <- extract_el(x, 4) + } else { + years <- extract_el(x, 3) + } + + if (as_matrix) { + SB <- data.table( + areas = areas, + years = as.numeric(years) + 1, + values = unlist(x, use.names = FALSE) + ) + if (!type %in% c("ntc")) { + SB <- SB[CJ(areas = all_areas, years = seq_len(opts$parameters$general$nbyears)), on = c("areas", "years")] + } + SB <- dcast(data = SB, formula = areas ~ years, value.var = "values") + mat <- as.matrix(SB, rownames = 1) + colnames(mat) <- NULL + mat + } else { + x } - SB <- dcast(data = SB, formula = areas ~ years, value.var = "values") - mat <- as.matrix(SB, rownames = 1) - colnames(mat) <- NULL - mat - } else { - x } + } ) } @@ -416,6 +444,9 @@ updateScenarioBuilder <- function(ldata, series <- ref_series[possible_series %in% series, "choices"] if (isTRUE("ntc" %in% series) & isTRUE(opts$antaresVersion < 820)) stop("updateScenarioBuilder: cannot use series='ntc' with Antares < 8.2.0", call. = FALSE) + if (isTRUE("bc" %in% series) & isTRUE(opts$antaresVersion < 870)) + stop("updateScenarioBuilder: cannot use series='bc' with Antares < 8.7.0", + call. = FALSE) series <- ref_series[ref_series$choices %in% series, "series"] } else { stop("If 'ldata' isn't a named list, you must specify which serie(s) to use!", call. = FALSE) @@ -436,6 +467,9 @@ updateScenarioBuilder <- function(ldata, } if (isTRUE("ntc" %in% series) & isTRUE(opts$antaresVersion < 820)) stop("updateScenarioBuilder: cannot use series='ntc' with Antares < 8.2.0", call. = FALSE) + if (isTRUE("bc" %in% series) & isTRUE(opts$antaresVersion < 870)) + stop("updateScenarioBuilder: cannot use series='bc' with Antares < 8.7.0", + call. = FALSE) sbuild <- lapply( X = series, FUN = function(x) { diff --git a/man/scenario-builder.Rd b/man/scenario-builder.Rd index ea360778..8a30c81a 100644 --- a/man/scenario-builder.Rd +++ b/man/scenario-builder.Rd @@ -10,7 +10,7 @@ \title{Read, create, update & deduplicate scenario builder} \usage{ scenarioBuilder( - n_scenario = NULL, + n_scenario = 1, n_mc = NULL, areas = NULL, areas_rand = NULL, @@ -138,6 +138,17 @@ sbuilder <- scenarioBuilder( ) ) +# Create a scenario builder matrix with + # bindings constraints groups (study version >= 8.7.0) + # Use parameter "mode" with "bc" +sbuilder <- scenarioBuilder( + n_scenario = 51, + n_mc = 2040, + group_bc = c("my_bc_1", "my_bc_2"), + group_bc_rand = "my_bc_2", + mode = "bc" +) + # Read previous scenario builder # in a matrix format prev_sb <- readScenarioBuilder() @@ -151,6 +162,8 @@ updateScenarioBuilder(ldata = sbuilder, series = "load") # equivalent as updateScenarioBuilder(ldata = list(l = sbuilder)) +# for binding constraints (study version >= 8.7.0) +updateScenarioBuilder(ldata = sbuilder, series = "bc") # update several series diff --git a/tests/testthat/test-scenarioBuilder.R b/tests/testthat/test-scenarioBuilder.R index b3437257..778f2f30 100644 --- a/tests/testthat/test-scenarioBuilder.R +++ b/tests/testthat/test-scenarioBuilder.R @@ -12,6 +12,24 @@ sapply(studies, function(study) { test_that("scenarioBuilder works", { + # default call + testthat::expect_warning( + sbuilder <- scenarioBuilder(), + regexp = "'n_scenario' parameter set to default value {1}" + ) + + # error call with bc (>=v870) + testthat::expect_error( + sbuilder <- scenarioBuilder(group_bc = "test"), + regexp = "Parameter 'group_bc' is only" + ) + + testthat::expect_error( + sbuilder <- scenarioBuilder(group_bc_rand = "test"), + regexp = "Parameter 'group_bc_rand' is only" + ) + + # standard sbuilder <- scenarioBuilder( n_scenario = 2, n_mc = 2, @@ -349,19 +367,35 @@ test_that("updateScenarioBuilder() for hl with all values between 0 and 1", { }) # v870 ---- - -## Global data -# read / open template study -setup_study_last(dir_path = sourcedir_last_study) -opts_test <- antaresRead::setSimulationPath(study_latest_version, "input") - -# areas list -antaresRead::getAreas(opts = opts_test) - test_that("scenarioBuilder works with binding constraint (v870)", { + # read / open template study + setup_study_last(dir_path = sourcedir_last_study) + opts_test <- antaresRead::setSimulationPath(study_latest_version, "input") + + ## no group rand ---- + sbuilder <- scenarioBuilder( + n_scenario = opts_test$parameters$general$nbyears, + n_mc = 10, + group_bc = c("group_test", "default"), + group_bc_rand = NULL, + mode = "bc", + opts = opts_test + ) + + # Update scenario builder + # for binding constraints series + updateScenarioBuilder(ldata = sbuilder, series = "bc") + + # Read scenario builder + # in a matrix format + prev_sb <- readScenarioBuilder(as_matrix = TRUE) - # Read, create & update scenario builder + # test + testthat::expect_equal(names(prev_sb), "bc") + testthat::expect_equal(rownames(prev_sb$bc), c("default", + "group_test")) + ## with group rand ---- sbuilder <- scenarioBuilder( n_scenario = opts_test$parameters$general$nbyears, n_mc = 10, @@ -381,6 +415,46 @@ test_that("scenarioBuilder works with binding constraint (v870)", { # test testthat::expect_equal(names(prev_sb), "bc") + testthat::expect_equal(rownames(prev_sb$bc), + "group_test") + + ## no bc mode ---- + # (classic mode of operation) + sbuilder <- scenarioBuilder() + + # Update scenario builder + # for binding constraints series + updateScenarioBuilder(ldata = sbuilder, series = "t") + + # Read scenario builder + # in a matrix format + prev_sb <- readScenarioBuilder(as_matrix = TRUE) + + # test + testthat::expect_equal(names(prev_sb), c("bc", "t")) + + ## parameter n_mc NULL ---- + # (classic mode of operation) + sbuilder <- scenarioBuilder( + n_scenario = opts_test$parameters$general$nbyears, + n_mc = NULL, + group_bc = c("group_test", "default"), + group_bc_rand = NULL, + mode = "bc", + opts = opts_test + ) + + # Update scenario builder + # for binding constraints series + updateScenarioBuilder(ldata = sbuilder, series = "bc") + + # Read scenario builder + # in a matrix format + prev_sb <- readScenarioBuilder(as_matrix = TRUE) + + # test + value_default_n_mc <- opts_test$parameters$general$nbyears + testthat::expect_equal(prev_sb$bc[1,], seq(value_default_n_mc)) # remove temporary study unlink(x = study_latest_version, recursive = TRUE) From 986743b07f40b366681a9c697b329effd16ed324 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Thu, 28 Mar 2024 15:00:07 +0100 Subject: [PATCH 51/74] removeBindingConstraint() updated from code review comment + tests --- R/removeBindingConstraint.R | 12 +++--- tests/testthat/test-removeBindingConstraint.R | 43 +++++++++++++++++-- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/R/removeBindingConstraint.R b/R/removeBindingConstraint.R index 45388079..f6aa903c 100644 --- a/R/removeBindingConstraint.R +++ b/R/removeBindingConstraint.R @@ -44,13 +44,15 @@ removeBindingConstraint <- function(name = NULL, assertthat::assert_that(inherits(opts, "simOptions")) # some checks for "group" parameter according to study version - if(!opts$antaresVersion >= 870 & !is.null(group)) - stop("Parameter 'group' is only for Antares study version >= v8.7.0", - call. = FALSE) - if(opts$antaresVersion >= 870){ - if(!is.null(name) & !is.null(group)) + if(!is.null(group)){ + if(!opts$antaresVersion >= 870){ + stop("Parameter 'group' is only for Antares study version >= v8.7.0", + call. = FALSE) + } + else if(!is.null(name)){ stop("You can only delete binding constraint by id/name or by group", call. = FALSE) + } } # API block diff --git a/tests/testthat/test-removeBindingConstraint.R b/tests/testthat/test-removeBindingConstraint.R index f42dfc03..afa8528f 100644 --- a/tests/testthat/test-removeBindingConstraint.R +++ b/tests/testthat/test-removeBindingConstraint.R @@ -1,7 +1,44 @@ - +# v710 ---- +## error function calls with bc group ---- +test_that("removeBindingConstraint with name group", { + # read / open template study + setup_study(studies, sourcedir) + opts <- antaresRead::setSimulationPath(studyPath, 1) + + # delete + testthat::expect_error( + removeBindingConstraint(name = "fake", + group = "group_fake", + opts = opts), + regexp = "Parameter 'group' is only for Antares study version >= v8.7.0" + ) + + # remove temporary study + unlink(x = studyPath, recursive = TRUE) +}) # v870 ---- - +## error function calls ---- +test_that("removeBindingConstraint v8.7.0 error call", { + # read / open template study + setup_study_last(dir_path = sourcedir_last_study) + opts_test <- antaresRead::setSimulationPath(study_latest_version, "input") + + # read + bc_v870 <- readBindingConstraints(opts = opts_test) + + bc_names_v870 <- names(bc_v870) + + group_to_delete <- bc_v870$bc_1$properties$group + + # delete + testthat::expect_error( + removeBindingConstraint(name = bc_names_v870[1], + group = "group_to_delete", + opts = opts_test), + regexp = "You can only delete binding constraint by" + ) +}) ## one name ---- test_that("removeBindingConstraint v8.7.0 by name", { # read / open template study @@ -43,8 +80,6 @@ test_that("removeBindingConstraint v8.7.0 by name", { testthat::expect_false(bc_names_v870[3] %in% names(readBindingConstraints(opts = opts_test))) - - # remove temporary study unlink(x = study_latest_version, recursive = TRUE) From b49bd17ee0a098865af49abf28b87d08af7a85b1 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Tue, 9 Apr 2024 11:36:08 +0200 Subject: [PATCH 52/74] createBindingConstraint() updated to use parameter "terms" instead of "coeffs" for version 870 only + add endpoint GET "/validate" --- R/createBindingConstraint.R | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/R/createBindingConstraint.R b/R/createBindingConstraint.R index 25b2fa99..3d9f86c0 100644 --- a/R/createBindingConstraint.R +++ b/R/createBindingConstraint.R @@ -283,21 +283,30 @@ createBindingConstraint <- function(name, } # delete NULL from parameters - if(is.null(body$group)) - body$group <- NULL - if(is.null(body$values)) - body$values <- NULL + body <- dropNulls(body) + + # rename parameter coeffs + index_to_change <- which(names(body)%in%"coeffs") + names(body)[index_to_change] <- "terms" # make json file body <- jsonlite::toJSON(body, auto_unbox = TRUE) # send request - api_post(opts, paste0(opts$study_id, "/bindingconstraints"), + result <- api_post(opts = opts, + endpoint = file.path(opts$study_id, "bindingconstraints"), body = body, encode = "raw") - bc_name <- list(...)$name - cli::cli_alert_info("Endpoint {.emph {'Create bindingconstraints'}} {.emph + # /validate + api_get(opts = opts, + endpoint = file.path(opts$study_id, + "constraint-groups", + result$group, + "validate")) + + bc_name <- result$id + cli::cli_alert_success("Endpoint {.emph {'Create bindingconstraints'}} {.emph {.strong {bc_name}}} success") return(invisible(opts)) From 042da50ac7ff247a61f2e6e8e879034b75a945e9 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Tue, 9 Apr 2024 17:01:36 +0200 Subject: [PATCH 53/74] editBindingConstraint() updated with new structure endpoint + add endpoint "/validate" to check group --- R/editBindingConstraint.R | 42 +++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/R/editBindingConstraint.R b/R/editBindingConstraint.R index 8dc70ff7..2a5b0383 100644 --- a/R/editBindingConstraint.R +++ b/R/editBindingConstraint.R @@ -310,25 +310,37 @@ editBindingConstraint <- function(name, # delete NULL from parameters args <- dropNulls(args) - # loop for every parameter (legacy endpoint) + # rename parameter coeffs + index_to_change <- which(names(args)%in%"coeffs") + names(args)[index_to_change] <- "terms" + + # keep id/name of constraint names_to_keep <- setdiff(names(args), "id") id_bc <- args$id - lapply(names_to_keep, function(x){ - select_element <- args[x] - body <- list(key= names(select_element), - value= args[[x]]) - # make json file - body <- jsonlite::toJSON(body, - auto_unbox = TRUE) - # send request - api_put(opts = opts, - endpoint = file.path(opts$study_id, "bindingconstraints", id_bc), - body = body, - encode = "raw") - }) + # drop id + args$id <- NULL + + # make json file + args <- jsonlite::toJSON(args, + auto_unbox = TRUE) + + # send request + result <- api_put(opts = opts, + endpoint = file.path(opts$study_id, + "bindingconstraints", + id_bc), + body = args, + encode = "raw") + + # /validate + api_get(opts = opts, + endpoint = file.path(opts$study_id, + "constraint-groups", + result$group, + "validate")) - cli::cli_alert_info("Endpoint {.emph {'Update bindingconstraints'}} {.emph + cli::cli_alert_success("Endpoint {.emph {'Update bindingconstraints'}} {.emph {.strong {id_bc}}} success") return(invisible(opts)) From 68556437e34ac4b6d0372d70cf393896e151ff05 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Fri, 12 Apr 2024 15:27:51 +0200 Subject: [PATCH 54/74] removeBindingConstraint() minor update output message validation in api mode --- R/removeBindingConstraint.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/removeBindingConstraint.R b/R/removeBindingConstraint.R index f6aa903c..e0a8772b 100644 --- a/R/removeBindingConstraint.R +++ b/R/removeBindingConstraint.R @@ -217,7 +217,7 @@ removeBindingConstraint <- function(name = NULL, api_delete(opts = opts, endpoint = file.path(opts$study_id, "bindingconstraints", x), encode = "raw") - cli::cli_alert_info("Endpoint {.emph {'Delete bindingconstraints'}} {.emph + cli::cli_alert_success("Endpoint {.emph {'Delete bindingconstraints'}} {.emph {.strong {x}}} success") }) @@ -227,7 +227,7 @@ removeBindingConstraint <- function(name = NULL, api_delete(opts = opts, endpoint = file.path(opts$study_id, "bindingconstraints", x), encode = "raw") - cli::cli_alert_info("Endpoint {.emph {'Delete bindingconstraints'}} {.emph + cli::cli_alert_success("Endpoint {.emph {'Delete bindingconstraints'}} {.emph {.strong {x}}} success") }) return(invisible(opts)) From cb2773de139d22bf7224cf62e1c9cb7904275fe4 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Fri, 12 Apr 2024 15:28:55 +0200 Subject: [PATCH 55/74] editBindingConstraint() fix coefficients parameter to update weight and offset --- R/editBindingConstraint.R | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/R/editBindingConstraint.R b/R/editBindingConstraint.R index 2a5b0383..d8beeff1 100644 --- a/R/editBindingConstraint.R +++ b/R/editBindingConstraint.R @@ -86,7 +86,7 @@ editBindingConstraint <- function(name, filter_year_by_year = filter_year_by_year, filter_synthesis = filter_synthesis, values = values, - coeffs = lapply(as.list(coefficients), as.list), + coeffs = coefficients, group = group, opts = opts) @@ -278,9 +278,23 @@ editBindingConstraint <- function(name, # Date: Fri, 12 Apr 2024 15:30:24 +0200 Subject: [PATCH 56/74] createBindingConstraint() fix coefficient parameter to create bc with weight + offset fix regression on value when NULL --- R/createBindingConstraint.R | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/R/createBindingConstraint.R b/R/createBindingConstraint.R index 3d9f86c0..1e1f405e 100644 --- a/R/createBindingConstraint.R +++ b/R/createBindingConstraint.R @@ -173,8 +173,7 @@ createBindingConstraint <- function(name, filter_synthesis = filter_synthesis, values = values, group = group, - coeffs = lapply(as.list(coefficients), - as.list), + coeffs = coefficients, opts = opts) return(invisible(api_opts)) @@ -253,12 +252,32 @@ createBindingConstraint <- function(name, .createBC_api <- function(..., opts){ + body <- list(...) # =v870 # reforge list structure if(!is.null(body$values)){ @@ -305,6 +323,7 @@ createBindingConstraint <- function(name, result$group, "validate")) + # output message bc_name <- result$id cli::cli_alert_success("Endpoint {.emph {'Create bindingconstraints'}} {.emph {.strong {bc_name}}} success") From bcd96ee2d3d0ebeb684896370eea15f19c2e5f63 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Wed, 17 Apr 2024 15:10:21 +0200 Subject: [PATCH 57/74] createBindingConstraint() updated to manage endpoint terms with links areas and thermal cluster --- R/createBindingConstraint.R | 53 ++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/R/createBindingConstraint.R b/R/createBindingConstraint.R index 1e1f405e..62a34dbb 100644 --- a/R/createBindingConstraint.R +++ b/R/createBindingConstraint.R @@ -302,20 +302,49 @@ createBindingConstraint <- function(name, # delete NULL from parameters body <- dropNulls(body) + body_terms <- NULL - # rename parameter coeffs - index_to_change <- which(names(body)%in%"coeffs") - names(body)[index_to_change] <- "terms" + # filter coeffs if none null + if(!is.null(body$coeffs)){ + body_terms <- body$coeffs + body$coeffs <- NULL + + # extract areas/cluster (links or thermal) + terms_values <- strsplit(x = names(body_terms), split = "%|\\.") + + is_dot <- grep(x = names(body_terms), + pattern = ".") + + # build list + if(is_dot) + data_list <- list(area=terms_values[[1]][1], + cluster=terms_values[[1]][2]) + else + data_list <- list(area1=terms_values[[1]][1], + area2=terms_values[[1]][2]) + + if(length(body_terms[[1]])>1) + body_terms <- list(weight=body_terms[[1]][1], + offset=body_terms[[1]][2], + data=data_list) + else + body_terms <- list(weight=body_terms[[1]][1], + data=data_list) + + # make json file + body_terms <- jsonlite::toJSON(body_terms, + auto_unbox = TRUE) + } # make json file body <- jsonlite::toJSON(body, auto_unbox = TRUE) - # send request + # send request (without coeffs/term) result <- api_post(opts = opts, - endpoint = file.path(opts$study_id, "bindingconstraints"), - body = body, - encode = "raw") + endpoint = file.path(opts$study_id, "bindingconstraints"), + body = body, + encode = "raw") # /validate api_get(opts = opts, endpoint = file.path(opts$study_id, @@ -323,6 +352,16 @@ createBindingConstraint <- function(name, result$group, "validate")) + # specific endpoint for coeffs/term + if(!is.null(body_terms)) + api_post(opts = opts, + endpoint = file.path(opts$study_id, + "bindingconstraints", + result$id, + "term"), + body = body_terms, + encode = "raw") + # output message bc_name <- result$id cli::cli_alert_success("Endpoint {.emph {'Create bindingconstraints'}} {.emph From da54e0e11563637e6b5d0aa6074782fc2937404a Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Fri, 19 Apr 2024 10:49:34 +0200 Subject: [PATCH 58/74] editBindingConstraint() updated with "/term" endpoint --- NEWS.md | 5 +++- R/editBindingConstraint.R | 58 +++++++++++++++++++-------------------- 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/NEWS.md b/NEWS.md index 5070b6ed..6075c1ec 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,7 +2,7 @@ > Second-member coupling constraint scenarios -### Breaking changes (Antares v8.7, cf. Antares v8.7 changelog) : +NEW FEATURES (Antares v8.7, cf. Antares v8.7 changelog) : * `createBindingConstraint()` / `createBindingConstraintBulk()` - New parameters `group` @@ -16,6 +16,9 @@ * `scenarioBuilder()` has 3 new parameters dedicated to use with binding constraints. * `updateGeneralSettings()` adds coupling constraints to the `scenariobuilder.dat` file. +### Breaking changes : + +* `createBindingConstraint()` is available with **offset** parameter in API mode # antaresEditObject 0.6.2 diff --git a/R/editBindingConstraint.R b/R/editBindingConstraint.R index d8beeff1..4afac1ca 100644 --- a/R/editBindingConstraint.R +++ b/R/editBindingConstraint.R @@ -267,34 +267,34 @@ editBindingConstraint <- function(name, # api part code .editBC_api <- function(..., opts){ - args <- list(...) + body <- list(...) # multi vers checks (legacy) - if (is.null(args$time_step)) + if (is.null(body$time_step)) stop("You must provide `timeStep` argument with API.", call. = FALSE) - if (is.null(args$time_step)) + if (is.null(body$time_step)) stop("You must provide `operator` argument with API.", call. = FALSE) # =v870 # reforge list structure - if(!is.null(args$values)){ - list_values <- list(less_term_matrix = args$values$lt, - equal_term_matrix = args$values$eq, - greater_term_matrix = args$values$gt) + if(!is.null(body$values)){ + list_values <- list(less_term_matrix = body$values$lt, + equal_term_matrix = body$values$eq, + greater_term_matrix = body$values$gt) list_values <- dropNulls(list_values) - args$values <- NULL + body$values <- NULL - args <- append(args, list_values) + body <- append(body, list_values) } # delete NULL from parameters - args <- dropNulls(args) + body <- dropNulls(body) # rename parameter coeffs - index_to_change <- which(names(args)%in%"coeffs") - names(args)[index_to_change] <- "terms" + index_to_change <- which(names(body)%in%"coeffs") + names(body)[index_to_change] <- "terms" # keep id/name of constraint - names_to_keep <- setdiff(names(args), "id") - id_bc <- args$id + names_to_keep <- setdiff(names(body), "id") + id_bc <- body$id # drop id - args$id <- NULL + body$id <- NULL # make json file - args <- jsonlite::toJSON(args, + body <- jsonlite::toJSON(body, auto_unbox = TRUE) # send request @@ -344,7 +344,7 @@ editBindingConstraint <- function(name, endpoint = file.path(opts$study_id, "bindingconstraints", id_bc), - body = args, + body = body, encode = "raw") # /validate From 4d1ac0d3206f2759fd7080aa2aae0531c9ae0d6f Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Fri, 19 Apr 2024 17:06:09 +0200 Subject: [PATCH 59/74] createBindingConstraint() fix in api part terms --- R/createBindingConstraint.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/createBindingConstraint.R b/R/createBindingConstraint.R index 62a34dbb..d3e36c14 100644 --- a/R/createBindingConstraint.R +++ b/R/createBindingConstraint.R @@ -312,8 +312,8 @@ createBindingConstraint <- function(name, # extract areas/cluster (links or thermal) terms_values <- strsplit(x = names(body_terms), split = "%|\\.") - is_dot <- grep(x = names(body_terms), - pattern = ".") + is_dot <- grepl(x = names(body_terms), + pattern = "\\.") # build list if(is_dot) From 1403f2c2bb58bda9a5bff8628efbb2f7919d4946 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Fri, 19 Apr 2024 17:15:00 +0200 Subject: [PATCH 60/74] editBindingConstraint()updated with specific endpoint /term --- R/editBindingConstraint.R | 48 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/R/editBindingConstraint.R b/R/editBindingConstraint.R index 4afac1ca..0ced23d9 100644 --- a/R/editBindingConstraint.R +++ b/R/editBindingConstraint.R @@ -323,10 +323,42 @@ editBindingConstraint <- function(name, # delete NULL from parameters body <- dropNulls(body) + body_terms <- NULL - # rename parameter coeffs - index_to_change <- which(names(body)%in%"coeffs") - names(body)[index_to_change] <- "terms" + # filter coeffs if none null + if(!is.null(body$coeffs)){ + body_terms <- body$coeffs + body$coeffs <- NULL + + # extract areas/cluster (links or thermal) + id_terms <- names(body_terms) + terms_values <- strsplit(x = id_terms, split = "%|\\.") + + is_dot <- grepl(x = id_terms, + pattern = "\\.") + + # build list + if(is_dot) + data_list <- list(area=terms_values[[1]][1], + cluster=terms_values[[1]][2]) + else + data_list <- list(area1=terms_values[[1]][1], + area2=terms_values[[1]][2]) + + if(length(body_terms[[1]])>1) + body_terms <- list(id=id_terms, + weight=body_terms[[1]][1], + offset=body_terms[[1]][2], + data=data_list) + else + body_terms <- list(id=id_terms, + weight=body_terms[[1]][1], + data=data_list) + + # make json file + body_terms <- jsonlite::toJSON(body_terms, + auto_unbox = TRUE) + } # keep id/name of constraint names_to_keep <- setdiff(names(body), "id") @@ -354,6 +386,16 @@ editBindingConstraint <- function(name, result$group, "validate")) + # specific endpoint for coeffs/term + if(!is.null(body_terms)) + api_put(opts = opts, + endpoint = file.path(opts$study_id, + "bindingconstraints", + result$id, + "term"), + body = body_terms, + encode = "raw") + cli::cli_alert_success("Endpoint {.emph {'Update bindingconstraints'}} {.emph {.strong {id_bc}}} success") From 619c8c8e65c516f924a7024bfd200bb40fb6bce8 Mon Sep 17 00:00:00 2001 From: berthetclement Date: Mon, 22 Apr 2024 18:20:23 +0200 Subject: [PATCH 61/74] createBindingConstraint() doc updated with offset case + test with multi coeffs + offset --- R/createBindingConstraint.R | 134 ++++++++++-------- man/createBindingConstraint.Rd | 24 +++- man/editBindingConstraint.Rd | 2 +- tests/testthat/test-createBindingConstraint.R | 49 ++++++- 4 files changed, 143 insertions(+), 66 deletions(-) diff --git a/R/createBindingConstraint.R b/R/createBindingConstraint.R index d3e36c14..71746236 100644 --- a/R/createBindingConstraint.R +++ b/R/createBindingConstraint.R @@ -18,7 +18,7 @@ #' @param operator Type of constraint: equality, inequality on one side or both sides. #' @param filter_year_by_year Marginal price granularity for year by year #' @param filter_synthesis Marginal price granularity for synthesis -#' @param coefficients A named vector containing the coefficients used by the constraint, +#' @param coefficients A named list containing the coefficients used by the constraint, #' the coefficients have to be alphabetically ordered. #' @param group "character" group of the constraint, default value : "default group" #' @param overwrite If the constraint already exist, overwrite the previous value. @@ -46,13 +46,29 @@ #' @examples #' \dontrun{ #' # < v8.7.0 : +#' +#' # Create constraints with multi coeffs (only weight) +#' #' createBindingConstraint( #' name = "myconstraint", #' values = matrix(data = rep(0, 8760 * 3), ncol = 3), #' enabled = FALSE, #' timeStep = "hourly", #' operator = "both", -#' coefficients = c("fr%myarea" = 1) +#' coefficients = list("area1%area2" = 1, +#' "area1%area3" = 2) +#' ) +#' +#' # Create constraints with multi coeffs + offset +#' +#' createBindingConstraint( +#' name = "myconstraint", +#' values = matrix(data = rep(0, 8760 * 3), ncol = 3), +#' enabled = FALSE, +#' timeStep = "hourly", +#' operator = "both", +#' coefficients = list("area1%area2" = "1%1", +#' "area1%area3" = "2%3") #' ) #' #' # Create multiple constraints @@ -70,7 +86,7 @@ #' enabled = FALSE, #' timeStep = "hourly", #' operator = "both", -#' coefficients = c("area1%area2" = 1), +#' coefficients = list("area1%area2" = 1), #' overwrite = TRUE #' ) #' } @@ -116,7 +132,7 @@ #' enabled = FALSE, #' timeStep = "hourly", #' operator = "both", -#' coefficients = c("at%fr" = 1), +#' coefficients = list("at%fr" = 1), #' group= "group_bulk", #' overwrite = TRUE #' ) @@ -162,7 +178,7 @@ createBindingConstraint <- function(name, stop("The areas are not sorted alphabetically.", call. = FALSE) } } - + # API block if (is_api_study(opts)) { api_opts <- .createBC_api(name = name, @@ -208,7 +224,7 @@ createBindingConstraint <- function(name, call. = FALSE) # v870 : check group and values - # no check for add BC with NULL values + # no check for add BC with NULL values group_values_check(group_value = group, values_data = values, operator_check = operator, @@ -313,15 +329,15 @@ createBindingConstraint <- function(name, terms_values <- strsplit(x = names(body_terms), split = "%|\\.") is_dot <- grepl(x = names(body_terms), - pattern = "\\.") + pattern = "\\.") # build list if(is_dot) data_list <- list(area=terms_values[[1]][1], - cluster=terms_values[[1]][2]) + cluster=terms_values[[1]][2]) else data_list <- list(area1=terms_values[[1]][1], - area2=terms_values[[1]][2]) + area2=terms_values[[1]][2]) if(length(body_terms[[1]])>1) body_terms <- list(weight=body_terms[[1]][1], @@ -445,9 +461,9 @@ createBindingConstraint_ <- function(bindingConstraints, } } } - + # check when overwrite element in list - # names of bindingConstraints are provided by R automatically + # names of bindingConstraints are provided by R automatically indexBC <- as.character(length(bindingConstraints)) if (indexBC %in% names(bindingConstraints)) { indexBC <- as.character(max(as.numeric(names(bindingConstraints))) + 1) @@ -517,10 +533,10 @@ group_values_check <- function(group_value, if(dim(values_data[[output_operator]])[2] <= 1) return() } - + # read existing binding constraint - # /!\/!\ function return "default values" (vector of 0) + # /!\/!\ function return "default values" (vector of 0) existing_bc <- readBindingConstraints(opts = opts) # study with no BC or virgin study @@ -536,7 +552,7 @@ group_values_check <- function(group_value, lapply(existing_bc, function(x){ x[["properties"]][["group"]]}) - ) + ) search_group_index <- grep(pattern = group_value, x = existing_groups) @@ -562,7 +578,7 @@ group_values_check <- function(group_value, x$properties$id, call. = FALSE) lt_dim } - }) + }) # keep dimension >1 names(p_col) <- NULL @@ -573,7 +589,7 @@ group_values_check <- function(group_value, }else p_col <- unique(p_col[p_col>1]) message("actual dimension of group : ", group_value, " is ", p_col) - + # check dimension of new group if(operator_check%in%"both"){ lt_dim <- dim(values_data$lt)[2] @@ -588,55 +604,55 @@ group_values_check <- function(group_value, # # no values provided # if(is.null(p_col_new)) # p_col_new <- 0 - - if(p_col!=p_col_new) # & p_col!=0 - stop(paste0("Put right columns dimension : ", - p_col, " for existing 'group' : ", - group_value), call. = FALSE) + + if(p_col!=p_col_new) # & p_col!=0 + stop(paste0("Put right columns dimension : ", + p_col, " for existing 'group' : ", + group_value), call. = FALSE) } } # v870 .valueCheck870 <- function(values, timeStep){ # check nrow Vs timeStep - nrows <- switch(timeStep, - hourly = 24*366, - daily = 366, - weekly = 366, - monthly = 12, - annual = 1) - - list_checked <- sapply( - names(values), - function(x, - list_in= values, - check_standard_rows= nrows){ - - list_work <- list_in[[x]] - - # one column scenario - if(ncol(list_work)==1){ - if (NROW(list_work) == 24*365) - list_work <- rbind(list_work, matrix(rep(0, 24*1), ncol = 1)) - if (NROW(list_work) == 365) - list_work <- rbind(list_work, matrix(rep(0, 1), ncol = 1)) - if (! NROW(list_work) %in% c(0, check_standard_rows)) - stop("Incorrect number of rows according to the timeStep") - }else{# scenarized columns - if(dim(list_work)[1]==24*365) - list_work <- rbind(list_work, - matrix(rep(0, 24*dim(list_work)[2]), - ncol = dim(list_work)[2])) - if(dim(list_work)[1]==365) - list_work <- rbind(list_work, - matrix(rep(0, dim(list_work)[2]), - ncol = dim(list_work)[2])) - if (! dim(list_work)[1] %in% c(0, check_standard_rows)) - stop("Incorrect number of rows according to the timeStep") - } - list_work - }, simplify = FALSE) - list_checked + nrows <- switch(timeStep, + hourly = 24*366, + daily = 366, + weekly = 366, + monthly = 12, + annual = 1) + + list_checked <- sapply( + names(values), + function(x, + list_in= values, + check_standard_rows= nrows){ + + list_work <- list_in[[x]] + + # one column scenario + if(ncol(list_work)==1){ + if (NROW(list_work) == 24*365) + list_work <- rbind(list_work, matrix(rep(0, 24*1), ncol = 1)) + if (NROW(list_work) == 365) + list_work <- rbind(list_work, matrix(rep(0, 1), ncol = 1)) + if (! NROW(list_work) %in% c(0, check_standard_rows)) + stop("Incorrect number of rows according to the timeStep") + }else{# scenarized columns + if(dim(list_work)[1]==24*365) + list_work <- rbind(list_work, + matrix(rep(0, 24*dim(list_work)[2]), + ncol = dim(list_work)[2])) + if(dim(list_work)[1]==365) + list_work <- rbind(list_work, + matrix(rep(0, dim(list_work)[2]), + ncol = dim(list_work)[2])) + if (! dim(list_work)[1] %in% c(0, check_standard_rows)) + stop("Incorrect number of rows according to the timeStep") + } + list_work + }, simplify = FALSE) + list_checked } diff --git a/man/createBindingConstraint.Rd b/man/createBindingConstraint.Rd index d5d208cc..c7eb4a8a 100644 --- a/man/createBindingConstraint.Rd +++ b/man/createBindingConstraint.Rd @@ -41,7 +41,7 @@ It contains one line per time step and three columns "less", "greater" and "equa \item{filter_synthesis}{Marginal price granularity for synthesis} -\item{coefficients}{A named vector containing the coefficients used by the constraint, +\item{coefficients}{A named list containing the coefficients used by the constraint, the coefficients have to be alphabetically ordered.} \item{group}{"character" group of the constraint, default value : "default group"} @@ -76,13 +76,29 @@ see example section below. \examples{ \dontrun{ # < v8.7.0 : + +# Create constraints with multi coeffs (only weight) + +createBindingConstraint( + name = "myconstraint", + values = matrix(data = rep(0, 8760 * 3), ncol = 3), + enabled = FALSE, + timeStep = "hourly", + operator = "both", + coefficients = list("area1\%area2" = 1, + "area1\%area3" = 2) +) + +# Create constraints with multi coeffs + offset + createBindingConstraint( name = "myconstraint", values = matrix(data = rep(0, 8760 * 3), ncol = 3), enabled = FALSE, timeStep = "hourly", operator = "both", - coefficients = c("fr\%myarea" = 1) + coefficients = list("area1\%area2" = "1\%1", + "area1\%area3" = "2\%3") ) # Create multiple constraints @@ -100,7 +116,7 @@ bindings_constraints <- lapply( enabled = FALSE, timeStep = "hourly", operator = "both", - coefficients = c("area1\%area2" = 1), + coefficients = list("area1\%area2" = 1), overwrite = TRUE ) } @@ -146,7 +162,7 @@ bindings_constraints <- lapply( enabled = FALSE, timeStep = "hourly", operator = "both", - coefficients = c("at\%fr" = 1), + coefficients = list("at\%fr" = 1), group= "group_bulk", overwrite = TRUE ) diff --git a/man/editBindingConstraint.Rd b/man/editBindingConstraint.Rd index d3d0d4bd..6447caba 100644 --- a/man/editBindingConstraint.Rd +++ b/man/editBindingConstraint.Rd @@ -37,7 +37,7 @@ It contains one line per time step and three columns "less", "greater" and "equa \item{filter_synthesis}{Marginal price granularity for synthesis} -\item{coefficients}{A named vector containing the coefficients used by the constraint, +\item{coefficients}{A named list containing the coefficients used by the constraint, the coefficients have to be alphabetically ordered.} \item{group}{"character" group of the constraint, default value : "default"} diff --git a/tests/testthat/test-createBindingConstraint.R b/tests/testthat/test-createBindingConstraint.R index 2d842bb2..267927dd 100644 --- a/tests/testthat/test-createBindingConstraint.R +++ b/tests/testthat/test-createBindingConstraint.R @@ -109,7 +109,7 @@ sapply(studies, function(study) { }) - + ## coeffs ---- test_that("Create a new binding constraint with coefficients", { coefs <- antaresRead::readBindingConstraints()[[1]]$coefs @@ -124,8 +124,53 @@ sapply(studies, function(study) { expect_identical(antaresRead::readBindingConstraints()[["coeffs"]]$coefs, coefs) }) + ## multi coeffs ---- + test_that("Create new bc with multi coefficients values", { + links_available <- getLinks()[1:3] + names_links <- gsub(pattern = " - ", replacement = "%", x = links_available) + + list_coeffs_values <- list(a=1, b=2, c=3) + names(list_coeffs_values) <- names_links + + createBindingConstraint( + name = "multi_coeffs", + timeStep = "weekly", + values = matrix(data = rep(0, 365 * 3), ncol = 3), + coefficients = list_coeffs_values + ) + + path_bc_ini <- file.path("input", "bindingconstraints", "bindingconstraints") + + read_bc <- antaresRead::readIni(path_bc_ini) + bc_to_test <- read_bc[length(read_bc)] + + testthat::expect_true(all(names_links %in% names(bc_to_test[[1]]))) + }) - + ## multi coeffs + offset ---- + test_that("Create new bc with multi coefficients values + offset", { + links_available <- getLinks()[1:3] + names_links <- gsub(pattern = " - ", replacement = "%", x = links_available) + + list_coeffs_values <- list(a="1%8", b="2%7", c="3%9") + names(list_coeffs_values) <- names_links + + createBindingConstraint( + name = "multi_coeffs_offset", + timeStep = "weekly", + values = matrix(data = rep(0, 365 * 3), ncol = 3), + coefficients = list_coeffs_values + ) + + path_bc_ini <- file.path("input", "bindingconstraints", "bindingconstraints") + + read_bc <- antaresRead::readIni(path_bc_ini) + bc_to_test <- read_bc[length(read_bc)] + + offset_values <- unlist(bc_to_test[[1]][names_links]) + + testthat::expect_equal(offset_values, unlist(list_coeffs_values)) + }) test_that("Create a new binding constraint with BAD coefficients", { From 162ba57a659df7616f8657535310747bdcebf785 Mon Sep 17 00:00:00 2001 From: clement Date: Thu, 25 Apr 2024 17:18:58 +0200 Subject: [PATCH 62/74] createBindingConstraint() is updated with new private function who check format of param coefficients and reforge it to API mode --- R/createBindingConstraint.R | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/R/createBindingConstraint.R b/R/createBindingConstraint.R index 71746236..142aa541 100644 --- a/R/createBindingConstraint.R +++ b/R/createBindingConstraint.R @@ -168,6 +168,7 @@ createBindingConstraint <- function(name, values <- .valueCheck870(values, timeStep) if(!is.null(coefficients)){ + # check if areas are sorted names_coef <- names(coefficients) splitted_names <- strsplit(names_coef, "%") are_areas_sorted <- sapply(splitted_names, function(areas) { @@ -181,6 +182,11 @@ createBindingConstraint <- function(name, # API block if (is_api_study(opts)) { + + # reformat coefficients offset values + coefficients <- .check_format_offset(coefficients = coefficients) + + # api treatments api_opts <- .createBC_api(name = name, enabled = enabled, time_step = timeStep, @@ -702,6 +708,31 @@ group_values_check <- function(group_value, values } +# update structure of coefficients/offset for api mode (char to vector) +.check_format_offset <- function(coefficients){ + if(!is.null(coefficients)){ + # check if offset + is_character_values <- sapply(coefficients, + function(x) is.character(x)) + + if(any(is_character_values)){ + # format offset for solver + index <- which(is_character_values) + + list_format <- lapply(index, function(x){ + var <- unlist(strsplit(x = coefficients[[x]], + split = "%")) + as.numeric(var) + }) + + # update list with format + coefficients <- append(list_format, + coefficients[-index]) + }else + coefficients + } +} + #' `r lifecycle::badge("experimental")` #' @param constraints A `list` of several named `list` containing data to create binding constraints. From 8597ce6af2bc8d7fb25ab814c32cebf0715ac6de Mon Sep 17 00:00:00 2001 From: clement Date: Fri, 26 Apr 2024 16:34:01 +0200 Subject: [PATCH 63/74] add script to replace dependance to study from antaresRead package --- .../generate_test_study_870.R | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 inst/study_test_generator/generate_test_study_870.R diff --git a/inst/study_test_generator/generate_test_study_870.R b/inst/study_test_generator/generate_test_study_870.R new file mode 100644 index 00000000..23bf08ba --- /dev/null +++ b/inst/study_test_generator/generate_test_study_870.R @@ -0,0 +1,120 @@ +# create study version 8.7.0 for test + +# params ---- +version <- "8.7.0" +name <- "test_case" + +# create study ---- +createStudy(path = tempdir(), + study_name = name, + antares_version = version) + +# areas ---- +lapply(c("fr", "it", "at"), + createArea) + +# thermal clusters ---- +pollutants_tests_values <- list_pollutants_values(multi_values = 0.3) + +# folder prepro/area/cluster/{data, mudulation} +# are created with default values + +capacity <- 500 +count <- 3L + +# data TS (data + modulation with default) +cluster_ts_data <- matrix(count*capacity, 8760, 1) +cluster_ts_data <- cbind(cluster_ts_data, + matrix(count*capacity*1.25, 8760, 1)) + +createCluster(area = getAreas()[1], + cluster_name = "gas", + group = "Gas", + unitcount = count, + nominalcapacity = capacity, + min_stable_power = 180, + min_up_time = 3L, + marginal_cost = 135.9, + market_bid_cost = 135.9, + list_pollutants = pollutants_tests_values, + time_series = cluster_ts_data) + +createCluster(area = getAreas()[2], + cluster_name = "oil", + group = "Oil", + unitcount = count, + nominalcapacity = capacity, + min_stable_power = 280, + min_up_time = 3L, + marginal_cost = 535.9, + market_bid_cost = 535.9, + list_pollutants = pollutants_tests_values, + time_series = cluster_ts_data) + +createCluster(area = getAreas()[3], + cluster_name = "nuc", + group = "Nuclear", + unitcount = count, + nominalcapacity = capacity, + min_stable_power = 280, + min_up_time = 3L, + marginal_cost = 835.9, + market_bid_cost = 835.9, + list_pollutants = pollutants_tests_values, + time_series = cluster_ts_data) + +# renewables ---- +cluster_ts_res_data <- cluster_ts_data/2 + +# production factor +createClusterRES(area = getAreas()[1], + cluster_name = "res_1", + group = "Other RES 1", + unitcount = count, + nominalcapacity = capacity/2, + enabled = TRUE, + ts_interpretation = "production-factor", + time_series = cluster_ts_res_data) + +# power generation +createClusterRES(area = getAreas()[2], + cluster_name = "res_2", + group = "Other RES 2", + unitcount = count, + nominalcapacity = capacity/2, + enabled = TRUE, + ts_interpretation = "power-generation", + time_series = cluster_ts_res_data) + +# load ---- +# calculated with cluster params (for every area) +load_value <- count*capacity +load_value_2 <- load_value*0.75 + +data_load <- matrix(c( + rep(load_value, 8760), + rep(load_value_2, 8760)), ncol = 2) + +lapply(getAreas(), + writeInputTS, + data=data_load, + type="load") + +#writeInputTS(data = data_load, area = getAreas()[1]) + +# links ---- +# set properties +link_properties <- propertiesLinkOptions() + +# data link +ts_link <- matrix(rep(count*capacity/3, 8760*2), ncol = 2) + +createLink(from = getAreas()[1], + to = getAreas()[2], + propertiesLink = link_properties, + tsLink = ts_link) + +createLink(from = getAreas()[2], + to = getAreas()[3], + propertiesLink = link_properties, + tsLink = ts_link) \ No newline at end of file From 5d63268d82bdb24249622515d01bfd199730d03d Mon Sep 17 00:00:00 2001 From: clement Date: Fri, 26 Apr 2024 16:34:39 +0200 Subject: [PATCH 64/74] editBindingConstraint() updated to manage offset values --- R/editBindingConstraint.R | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/R/editBindingConstraint.R b/R/editBindingConstraint.R index 0ced23d9..9f7e1f44 100644 --- a/R/editBindingConstraint.R +++ b/R/editBindingConstraint.R @@ -79,6 +79,10 @@ editBindingConstraint <- function(name, ## API block ---- if (is_api_study(opts)) { + # reformat coefficients offset values + coefficients <- .check_format_offset(coefficients = coefficients) + + # api treatments opts_api <- .editBC_api(id = name, enabled = enabled, time_step = timeStep, From c47caa384c3bee194d4a15f65ce022f5a02b6e20 Mon Sep 17 00:00:00 2001 From: clement Date: Fri, 26 Apr 2024 16:35:26 +0200 Subject: [PATCH 65/74] test createBindingConstraint updated with new script generator study and multi offset case --- tests/testthat/test-createBindingConstraint.R | 252 ++++++++++-------- 1 file changed, 135 insertions(+), 117 deletions(-) diff --git a/tests/testthat/test-createBindingConstraint.R b/tests/testthat/test-createBindingConstraint.R index 267927dd..d0d706fc 100644 --- a/tests/testthat/test-createBindingConstraint.R +++ b/tests/testthat/test-createBindingConstraint.R @@ -235,15 +235,15 @@ sapply(studies, function(study) { # v8.7 ---- -## Global data -# read / open template study -setup_study_last(dir_path = sourcedir_last_study) +# read script to generate study v8.7.0 +sourcedir_last_study <- system.file("study_test_generator/generate_test_study_870.R", + package = "antaresEditObject") -opts_test <- antaresRead::setSimulationPath(study_latest_version, "input") - -# areas list -antaresRead::getAreas(opts = opts_test) +# create study +source(file = sourcedir_last_study) +opts_test <- simOptions() +## Global data---- # scenarized data hourly n <- 10 lt_data <- matrix(data = rep(1, 8760 * n), ncol = n) @@ -264,7 +264,7 @@ scenar_values_daily <- list(lt= lt_data, gt= gt_data, eq= eq_data) -## parameter values ---- +## ERROR CASE ---- test_that("createBindingConstraint with bad structure values object v8.7", { # less @@ -277,9 +277,8 @@ test_that("createBindingConstraint with bad structure values object v8.7", { enabled = FALSE, timeStep = "daily", operator = "less", - coefficients = c("at%fr" = 1), - opts = opts_test - ), regexp = "you must provide a list named according your parameter" + coefficients = list("at%fr" = 1)), + regexp = "you must provide a list named according your parameter" ) # both @@ -292,9 +291,8 @@ test_that("createBindingConstraint with bad structure values object v8.7", { enabled = FALSE, timeStep = "daily", operator = "both", - coefficients = c("at%fr" = 1), - opts = opts_test - ), regexp = "you must provide a list named according your parameter" + coefficients = list("at%fr" = 1)), + regexp = "you must provide a list named according your parameter" ) }) @@ -309,12 +307,10 @@ test_that("createBindingConstraint (default group value) v8.7", { enabled = FALSE, timeStep = "hourly", operator = "both", - coefficients = c("at%fr" = 1), - overwrite = TRUE, - opts = opts_test - ) + coefficients = list("at%fr" = 1), + overwrite = TRUE) - bc <- readBindingConstraints(opts = opts_test) + bc <- readBindingConstraints() # tests testthat::expect_true("myconstraint" %in% @@ -335,6 +331,7 @@ test_that("createBindingConstraint (default group value) v8.7", { res <- unlist(res) + # txt files are empty testthat::expect_equal(res, NULL) ### with values ---- @@ -344,11 +341,9 @@ test_that("createBindingConstraint (default group value) v8.7", { enabled = FALSE, timeStep = "hourly", operator = "both", - coefficients = c("at%fr" = 1), - opts = opts_test - ) + coefficients = c("at%fr" = 1)) - bc <- readBindingConstraints(opts = opts_test) + bc <- readBindingConstraints() # tests testthat::expect_true("myconstraint2" %in% @@ -373,75 +368,15 @@ test_that("createBindingConstraint (default group value) v8.7", { }) -## existing named group ---- - # study provide BC with group "group_test" -test_that("createBindingConstraint with existing group v8.7", { - - # read to have dimension - bc <- readBindingConstraints() - - # dimension according BC/group - dim_bc <- lapply(bc, function(x){ - if(x$properties$operator %in% "both") - list(group = x$properties$group, - dim_values = dim(x$values$less)[2]) - else - list(group = x$properties$group, - dim_values = dim(x$values)[2]) - }) - - existing_name_group <- dim_bc$bc_1$group - dim_existing_group <- dim_bc$bc_1$dim_values - - # ADD binding constraint with bad dimension - testthat::expect_error( - createBindingConstraint( - name = "myconstraint_group1", - values = scenar_values_daily, - enabled = FALSE, - timeStep = "daily", - operator = "both", - group = existing_name_group, - coefficients = c("at%fr" = 1), - opts = opts_test - ), regexp = "Put right columns dimension" - ) - - # put right dimension - data_ok <- list() - data_ok$lt <- scenar_values_daily$lt[,1:dim_existing_group] - data_ok$gt <- scenar_values_daily$gt[,1:dim_existing_group] - - # ADD binding constraint with good dimension - createBindingConstraint( - name = "bc_existing_group", - values = data_ok, - enabled = FALSE, - timeStep = "daily", - operator = "both", - group = existing_name_group, - coefficients = c("at%fr" = 1), - opts = opts_test - ) - - bc <- readBindingConstraints(opts = opts_test) - - # tests - testthat::expect_true("bc_existing_group" %in% - names(bc)) - testthat::expect_equal(bc$bc_existing_group$properties$group, - existing_name_group) - testthat::expect_equal(dim(data_ok$lt)[2], - dim(bc$bc_existing_group$values$less)[2]) - -}) ## add new group ---- testthat::test_that("createBindingConstraint with new group v8.7",{ # add values with the following steps - # NULL => 1 column => >1 column => 1 column => NULL - # error case with dimension different + # NULL => 1 column => >1 column => 1 column => NULL + # error case with dimension different + + name_group <- "new_group" # ADD binding with NULL values createBindingConstraint( @@ -450,10 +385,8 @@ testthat::test_that("createBindingConstraint with new group v8.7",{ enabled = FALSE, timeStep = "hourly", operator = "greater", - group = "new_group", - coefficients = c("at%fr" = 1), - opts = opts_test - ) + group = name_group, + coefficients = list("at%fr" = 1)) # ADD binding with 1 col df_one_col <- scenar_values["lt"] @@ -465,40 +398,34 @@ testthat::test_that("createBindingConstraint with new group v8.7",{ enabled = FALSE, timeStep = "hourly", operator = "less", - group = "new_group", + group = name_group, coefficients = c("at%fr" = 1), opts = opts_test ) # ADD binding with multi cols - df_one_col <- scenar_values["lt"] - df_one_col$lt <- df_one_col$lt[,1:3, drop = FALSE] + df_multi_col <- scenar_values["lt"] + df_multi_col$lt <- df_multi_col$lt[,1:3, drop = FALSE] + # now, group will keep this dimension createBindingConstraint( name = "bc_new_group_multi", - values = df_one_col, + values = df_multi_col, enabled = FALSE, timeStep = "hourly", operator = "less", - group = "new_group", - coefficients = c("at%fr" = 1), - opts = opts_test - ) + group = name_group, + coefficients = list("at%fr" = 1)) # ADD binding with 1 col - df_one_col <- scenar_values["lt"] - df_one_col$lt <- df_one_col$lt[,1, drop = FALSE] - createBindingConstraint( name = "bc_new_group_1_bis", values = df_one_col, enabled = FALSE, timeStep = "hourly", operator = "less", - group = "new_group", - coefficients = c("at%fr" = 1), - opts = opts_test - ) + group = name_group, + coefficients = list("at%fr" = 1)) # ADD binding with NULL values createBindingConstraint( @@ -507,25 +434,116 @@ testthat::test_that("createBindingConstraint with new group v8.7",{ enabled = FALSE, timeStep = "hourly", operator = "greater", - group = "new_group", - coefficients = c("at%fr" = 1), - opts = opts_test - ) + group = name_group, + coefficients = list("at%fr" = 1)) - # ADD binding with NULL values + # ADD binding with NULL values (both case) createBindingConstraint( name = "bc_new_group_NULL_bis_both", values = NULL, enabled = FALSE, timeStep = "hourly", operator = "both", - group = "new_group", - coefficients = c("at%fr" = 1), - opts = opts_test + group = name_group, + coefficients = list("at%fr" = 1)) + + # test dimension of group "new_group" + path_bc_value_file <- file.path(opts_test$inputPath, + "bindingconstraints", + "bc_new_group_multi_lt.txt") + + # read value + dim_new_group <- dim(data.table::fread(file = path_bc_value_file)) + testthat::expect_equal(3, dim_new_group[2]) + +}) + + + +## existing named group ---- + # study provide BC with group "group_test" +test_that("createBindingConstraint with existing group v8.7", { + + # create "group_test" + name_group <- "group_test" + createBindingConstraint( + name = "bc_with_group", + values = scenar_values, + enabled = FALSE, + timeStep = "hourly", + operator = "both", + group = name_group, + coefficients = list("at%fr" = 1)) + + # ADD binding constraint with bad dimension + testthat::expect_error( + createBindingConstraint( + name = "bc_with_group_error", + values = scenar_values_daily, + enabled = FALSE, + timeStep = "daily", + operator = "both", + group = name_group, + coefficients = list("at%fr" = 1)), + regexp = "Put right columns dimension" ) + n <- 10 + ts_data <- matrix(data = rep(1, 365 * n), ncol = n) + data_ok <- list() + data_ok$lt <- ts_data + data_ok$gt <- ts_data + + # ADD binding constraint with good dimension + createBindingConstraint( + name = "bc_existing_group", + values = data_ok, + enabled = FALSE, + timeStep = "daily", + operator = "both", + group = name_group, + coefficients = list("at%fr" = 1)) + + bc <- readBindingConstraints(opts = opts_test) + + # tests + testthat::expect_true("bc_existing_group" %in% + names(bc)) + testthat::expect_equal(bc$bc_existing_group$properties$group, + name_group) + testthat::expect_equal(dim(data_ok$lt)[2], + dim(bc$bc_existing_group$values$less)[2]) + }) +## multi terms properties ---- +test_that("bc with multi weight + offset properties", { + + # multi properties + data_terms <- list("at%fr" = "1%10", + "at%fr" = "1%11", + "fr%it" = "1%-5", + "at.at_gas" = "1%10") + + createBindingConstraint( + name = "bc_multi_offset", + values = scenar_values, + enabled = TRUE, + timeStep = "hourly", + operator = "both", + coefficients = data_terms) + + path_bc_ini <- file.path("input", "bindingconstraints", "bindingconstraints") + + read_bc <- antaresRead::readIni(path_bc_ini) + bc_id <- sapply(read_bc, `[[`, "id") + index_my_bc <- which(bc_id%in%"bc_multi_offset") + + # test if all terms are created + testthat::expect_true(all( + names(data_terms)%in%names(read_bc[[index_my_bc]]))) + +}) ## bulk ---- test_that("createBindingConstraintBulk v8.7", { @@ -542,14 +560,14 @@ test_that("createBindingConstraintBulk v8.7", { enabled = FALSE, timeStep = "hourly", operator = "both", - coefficients = c("at%fr" = 1), + coefficients = list("at%fr" = 1), group= "group_bulk", overwrite = TRUE ) } ) # create all constraints - createBindingConstraintBulk(bindings_constraints, opts = opts_test) + createBindingConstraintBulk(bindings_constraints) # tests testthat::expect_true("constraints_bulk1" %in% @@ -561,4 +579,4 @@ test_that("createBindingConstraintBulk v8.7", { # remove temporary study ---- -unlink(x = study_latest_version, recursive = TRUE) +deleteStudy() From e4dbcfd53703125ed8e20f4648550d788aaa4871 Mon Sep 17 00:00:00 2001 From: clement Date: Fri, 26 Apr 2024 16:36:10 +0200 Subject: [PATCH 66/74] test editBindingConstraint() updated with multi offset cases --- tests/testthat/test-editBindingConstraint.R | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/testthat/test-editBindingConstraint.R b/tests/testthat/test-editBindingConstraint.R index 0d0602d2..5574a3fb 100644 --- a/tests/testthat/test-editBindingConstraint.R +++ b/tests/testthat/test-editBindingConstraint.R @@ -20,15 +20,16 @@ test_that("editBindingConstraint v710", { values = data_hourly, timeStep = "hourly", operator = "less", - coefficients = c("b%psp in"= 1.75, - "b%psp out"= 2), + coefficients = list("b%psp in"= 1.75, + "b%psp out"= 2, + "a%a_offshore"= "2%-5"), opts = simOptions()) bc_modified <- antaresRead::readBindingConstraints() new_coef <- bc_modified[[bc_names[1]]]$coefs # test - testthat::expect_true(all(new_coef %in% c(1.75, 2))) + testthat::expect_true(all(new_coef %in% c(1.75, 2, "2%-5"))) # remove temporary study unlink(x = file.path(pathstd, "test_case"), recursive = TRUE) @@ -79,7 +80,7 @@ test_that("editBindingConstraint with 'default' group v8.7.0", { operator = "both", filter_year_by_year = "daily", filter_synthesis = "daily", - coefficients = c("fr%it"= 7.45), + coefficients = list("fr%it"= 7.45), opts = opts_test) # read @@ -121,7 +122,7 @@ test_that("editBindingConstraint with 'default' group v8.7.0", { values = scenar_values_daily_n, timeStep = "daily", operator = "both", - coefficients = c("fr%it"= 7.45), + coefficients = list("fr%it"= 7.45), opts = opts_test), regexp = "Put right columns dimension" ) @@ -133,7 +134,7 @@ test_that("editBindingConstraint with 'default' group v8.7.0", { values = NULL, timeStep = "daily", operator = "both", - coefficients = c("fr%it" = 12, + coefficients = list("fr%it" = 12, "fr%at" = 0), opts = opts_test) @@ -143,7 +144,6 @@ test_that("editBindingConstraint with 'default' group v8.7.0", { # test coefs testthat::expect_true(all(new_coef %in% c(12, 0))) - }) ## exisintg group ---- @@ -187,7 +187,7 @@ test_that("editBindingConstraint with existing group v8.7.0", { group = group_bc, timeStep = "daily", operator = "both", - coefficients = c("fr%it" = 12), + coefficients = list("fr%it" = 12), opts = opts_test) # read From a1c0953bfa54c73a270cebc064ea796570d7d140 Mon Sep 17 00:00:00 2001 From: berthetclement Date: Tue, 30 Apr 2024 15:34:22 +0200 Subject: [PATCH 67/74] createBindingConstraint() update doc --- R/createBindingConstraint.R | 3 ++- man/createBindingConstraint.Rd | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/R/createBindingConstraint.R b/R/createBindingConstraint.R index 142aa541..2e316188 100644 --- a/R/createBindingConstraint.R +++ b/R/createBindingConstraint.R @@ -19,7 +19,8 @@ #' @param filter_year_by_year Marginal price granularity for year by year #' @param filter_synthesis Marginal price granularity for synthesis #' @param coefficients A named list containing the coefficients used by the constraint, -#' the coefficients have to be alphabetically ordered. +#' the coefficients have to be alphabetically ordered see examples below for entering +#' weight or weight with offset. #' @param group "character" group of the constraint, default value : "default group" #' @param overwrite If the constraint already exist, overwrite the previous value. #' diff --git a/man/createBindingConstraint.Rd b/man/createBindingConstraint.Rd index c7eb4a8a..3566141f 100644 --- a/man/createBindingConstraint.Rd +++ b/man/createBindingConstraint.Rd @@ -42,7 +42,8 @@ It contains one line per time step and three columns "less", "greater" and "equa \item{filter_synthesis}{Marginal price granularity for synthesis} \item{coefficients}{A named list containing the coefficients used by the constraint, -the coefficients have to be alphabetically ordered.} +the coefficients have to be alphabetically ordered see examples below for entering +weight or weight with offset.} \item{group}{"character" group of the constraint, default value : "default group"} From 80a5afb6d24c62954d8e635bd2ea2fec42ea1f6c Mon Sep 17 00:00:00 2001 From: berthetclement Date: Tue, 30 Apr 2024 15:36:56 +0200 Subject: [PATCH 68/74] editBindingConstraint() update doc + tests with no longer using the test study in antaresRead --- R/editBindingConstraint.R | 14 ++- man/editBindingConstraint.Rd | 17 ++- tests/testthat/test-createBindingConstraint.R | 2 +- tests/testthat/test-editBindingConstraint.R | 109 ++++++++++++++---- 4 files changed, 114 insertions(+), 28 deletions(-) diff --git a/R/editBindingConstraint.R b/R/editBindingConstraint.R index 9f7e1f44..dc9cac07 100644 --- a/R/editBindingConstraint.R +++ b/R/editBindingConstraint.R @@ -40,7 +40,17 @@ #' enabled = FALSE, #' timeStep = "hourly", #' operator = "both", -#' coefficients = c("fr%de" = 1) +#' coefficients = list("fr%de" = 1) +#' ) +#' +#' # update binding constraint with weight + offset +#' editBindingConstraint( +#' name = "myconstraint", +#' values = matrix(data = rep(0, 8784 * 3), ncol = 3), +#' enabled = FALSE, +#' timeStep = "hourly", +#' operator = "both", +#' coefficients = list("fr%de" = "1%-5") #' ) #' #' # >= v8.7.0 : @@ -61,7 +71,7 @@ #' operator = "both", #' filter_year_by_year = "hourly", #' filter_synthesis = "hourly", -#' coefficients = c("fr%de" = 1), +#' coefficients = list("fr%de" = 1), #' group = "myconstraint_group") #' } editBindingConstraint <- function(name, diff --git a/man/editBindingConstraint.Rd b/man/editBindingConstraint.Rd index 6447caba..d2536510 100644 --- a/man/editBindingConstraint.Rd +++ b/man/editBindingConstraint.Rd @@ -38,7 +38,8 @@ It contains one line per time step and three columns "less", "greater" and "equa \item{filter_synthesis}{Marginal price granularity for synthesis} \item{coefficients}{A named list containing the coefficients used by the constraint, -the coefficients have to be alphabetically ordered.} +the coefficients have to be alphabetically ordered see examples below for entering +weight or weight with offset.} \item{group}{"character" group of the constraint, default value : "default"} @@ -77,7 +78,17 @@ editBindingConstraint( enabled = FALSE, timeStep = "hourly", operator = "both", - coefficients = c("fr\%de" = 1) + coefficients = list("fr\%de" = 1) +) + +# update binding constraint with weight + offset +editBindingConstraint( + name = "myconstraint", + values = matrix(data = rep(0, 8784 * 3), ncol = 3), + enabled = FALSE, + timeStep = "hourly", + operator = "both", + coefficients = list("fr\%de" = "1\%-5") ) # >= v8.7.0 : @@ -98,7 +109,7 @@ editBindingConstraint(name = "myconstraint", operator = "both", filter_year_by_year = "hourly", filter_synthesis = "hourly", - coefficients = c("fr\%de" = 1), + coefficients = list("fr\%de" = 1), group = "myconstraint_group") } } diff --git a/tests/testthat/test-createBindingConstraint.R b/tests/testthat/test-createBindingConstraint.R index d0d706fc..faf12173 100644 --- a/tests/testthat/test-createBindingConstraint.R +++ b/tests/testthat/test-createBindingConstraint.R @@ -233,7 +233,7 @@ sapply(studies, function(study) { }) -# v8.7 ---- +# v870 ---- # read script to generate study v8.7.0 sourcedir_last_study <- system.file("study_test_generator/generate_test_study_870.R", diff --git a/tests/testthat/test-editBindingConstraint.R b/tests/testthat/test-editBindingConstraint.R index 5574a3fb..d93cfa7a 100644 --- a/tests/testthat/test-editBindingConstraint.R +++ b/tests/testthat/test-editBindingConstraint.R @@ -38,14 +38,15 @@ test_that("editBindingConstraint v710", { # v870 ---- -## Global data -# read / open template study -setup_study_last(dir_path = sourcedir_last_study) -opts_test <- antaresRead::setSimulationPath(study_latest_version, "input") +# read script to generate study v8.7.0 +sourcedir_last_study <- system.file("study_test_generator/generate_test_study_870.R", + package = "antaresEditObject") -# areas list -antaresRead::getAreas(opts = opts_test) +# create study +source(file = sourcedir_last_study) +opts_test <- simOptions() +## global data ---- # scenarized data # hourly n <- 10 @@ -67,21 +68,37 @@ scenar_values_daily <- list(lt= lt_data, ## default group ---- test_that("editBindingConstraint with 'default' group v8.7.0", { + # INIT with creation BC + # multi properties + data_terms <- list("at%fr" = "1%10", + "at%fr" = "1%11", + "fr%it" = "1%-5", + "at.at_gas" = "1%10") + + name_bc <- "bc_multi_offset" + + createBindingConstraint( + name = name_bc, + values = scenar_values_hourly, + enabled = TRUE, + timeStep = "hourly", + operator = "both", + overwrite = TRUE, + coefficients = data_terms) # PS : in this study, "default" have 1 column dimension bc <- readBindingConstraints(opts = opts_test) # edit properties + values (good dimension) # edit "greater" to "both" - bc_names_v870 <- bc$bc_2$properties$id + bc_names_v870 <- bc[[name_bc]]$properties$id editBindingConstraint(name = bc_names_v870, values = scenar_values_daily, timeStep = "daily", operator = "both", filter_year_by_year = "daily", filter_synthesis = "daily", - coefficients = list("fr%it"= 7.45), - opts = opts_test) + coefficients = list("fr%it"= 7.45)) # read bc_modified <- readBindingConstraints(opts = opts_test) @@ -92,7 +109,7 @@ test_that("editBindingConstraint with 'default' group v8.7.0", { filter_synthesis <- bc_modified[[bc_names_v870]]$properties$`filter-synthesis` # test properties - testthat::expect_true(all(new_coef %in% c(7.45))) + testthat::expect_true(7.45 %in% new_coef) testthat::expect_true(timeStep %in% "daily") testthat::expect_true(operator %in% "both") testthat::expect_true(filter_year %in% "daily") @@ -122,32 +139,58 @@ test_that("editBindingConstraint with 'default' group v8.7.0", { values = scenar_values_daily_n, timeStep = "daily", operator = "both", - coefficients = list("fr%it"= 7.45), - opts = opts_test), + coefficients = list("fr%it"= 7.45)), regexp = "Put right columns dimension" ) - - ### multi coeff ---- editBindingConstraint(name = bc_names_v870, values = NULL, timeStep = "daily", operator = "both", coefficients = list("fr%it" = 12, - "fr%at" = 0), - opts = opts_test) + "fr%at" = 0)) # read bc_modified <- readBindingConstraints(opts = opts_test) new_coef <- bc_modified[[bc_names_v870]]$coefs # test coefs - testthat::expect_true(all(new_coef %in% c(12, 0))) + testthat::expect_true(all( + c(12, 0) %in% new_coef)) }) ## exisintg group ---- test_that("editBindingConstraint with existing group v8.7.0", { + # INIT with creation BC + # multi properties + data_terms <- list("at%fr" = "1%10", + "at%fr" = "1%11", + "fr%it" = "1%-5", + "at.at_gas" = "1%10") + + name_bc <- "bc_group_multi_offset" + name_group <- "group_test" + + createBindingConstraint( + name = name_bc, + values = scenar_values_hourly, + enabled = TRUE, + timeStep = "hourly", + operator = "both", + group = name_group, + overwrite = TRUE, + coefficients = data_terms) + + createBindingConstraint( + name = "bc_test_default_group", + values = scenar_values_hourly, + enabled = TRUE, + timeStep = "hourly", + operator = "both", + overwrite = TRUE, + coefficients = data_terms) + # read existing binding bc <- readBindingConstraints(opts = opts_test) @@ -172,7 +215,7 @@ test_that("editBindingConstraint with existing group v8.7.0", { dim_bc <- info_bc[index][[bc_no_default]]$dim_values # edit bc with good dim - n <- 2 + n <- dim_bc # daily lt_data <- matrix(data = rep(1, 365 * n), ncol = n) gt_data <- matrix(data = rep(2, 365 * n), ncol = n) @@ -197,16 +240,38 @@ test_that("editBindingConstraint with existing group v8.7.0", { operator <- bc_modified[[bc_no_default]]$properties$operator # test properties - testthat::expect_true(all(new_coef %in% c(1, 12))) - testthat::expect_true(timeStep %in% "daily") - testthat::expect_true(operator %in% "both") + testthat::expect_true(12 %in% new_coef) + testthat::expect_true("daily" %in% timeStep) + testthat::expect_true("both" %in% operator) # test values dim_col_values_input <- dim(scenar_values_daily_n$lt)[2] dim_col_values_edited <- dim(bc_modified[[bc_no_default]]$values$less)[2] testthat::expect_equal(dim_col_values_input, dim_col_values_edited) + # edit properties + values (bad dimension) + ### error dimension ---- + + n <- 9 + # daily + lt_data <- matrix(data = rep(1, 365 * n), ncol = n) + gt_data <- matrix(data = rep(2, 365 * n), ncol = n) + eq_data <- matrix(data = rep(3, 365 * n), ncol = n) + + scenar_values_daily_n <- list(lt= lt_data, + gt= gt_data, + eq= eq_data) + + testthat::expect_error( + editBindingConstraint(name = bc_no_default, + values = scenar_values_daily_n, + timeStep = "daily", + operator = "both", + coefficients = list("fr%it"= 7.45)), + regexp = "Put right columns dimension" + ) + }) # remove study ---- -unlink(x = study_latest_version, recursive = TRUE) +deleteStudy() From 2d7c48c0b368aaaf80228b2bf96cfb5394aa586e Mon Sep 17 00:00:00 2001 From: berthetclement Date: Thu, 2 May 2024 15:57:39 +0200 Subject: [PATCH 69/74] removeBindingConstraint() + ScenarioBuilder tests updated to delete study reference from antaresRead --- R/removeBindingConstraint.R | 2 +- tests/testthat/test-removeBindingConstraint.R | 249 +++++++++++------- tests/testthat/test-scenarioBuilder.R | 24 +- 3 files changed, 168 insertions(+), 107 deletions(-) diff --git a/R/removeBindingConstraint.R b/R/removeBindingConstraint.R index e0a8772b..69ead271 100644 --- a/R/removeBindingConstraint.R +++ b/R/removeBindingConstraint.R @@ -147,7 +147,7 @@ removeBindingConstraint <- function(name = NULL, use.names = FALSE) if(!all(group%in%bc_groups)) - warning(paste0("No binding constraint with group '", + stop(paste0("No binding constraint with group '", group[!group%in%bc_groups], "'"), call. = FALSE) else{ diff --git a/tests/testthat/test-removeBindingConstraint.R b/tests/testthat/test-removeBindingConstraint.R index afa8528f..daffaf0d 100644 --- a/tests/testthat/test-removeBindingConstraint.R +++ b/tests/testthat/test-removeBindingConstraint.R @@ -18,147 +18,205 @@ test_that("removeBindingConstraint with name group", { }) # v870 ---- -## error function calls ---- +# study test creation ---- +# read script to generate study v8.7.0 +sourcedir_last_study <- system.file("study_test_generator/generate_test_study_870.R", + package = "antaresEditObject") + +# create study +source(file = sourcedir_last_study) +opts_test <- simOptions() + +## Error parameters ---- test_that("removeBindingConstraint v8.7.0 error call", { - # read / open template study - setup_study_last(dir_path = sourcedir_last_study) - opts_test <- antaresRead::setSimulationPath(study_latest_version, "input") - - # read - bc_v870 <- readBindingConstraints(opts = opts_test) - - bc_names_v870 <- names(bc_v870) - - group_to_delete <- bc_v870$bc_1$properties$group - - # delete + # try to delete BC with name + group testthat::expect_error( - removeBindingConstraint(name = bc_names_v870[1], - group = "group_to_delete", - opts = opts_test), + removeBindingConstraint(name = "whereAreYou", + group = "where_is_group"), regexp = "You can only delete binding constraint by" ) }) + +## error unknown group ---- +test_that("removeBindingConstraint v8.7.0 warning message", { + # try to delete BC with name + group + testthat::expect_error( + removeBindingConstraint(group = "where_is_group"), + regexp = "No binding constraint with group 'where_is_group'" + ) +}) + ## one name ---- test_that("removeBindingConstraint v8.7.0 by name", { - # read / open template study - setup_study_last(dir_path = sourcedir_last_study) - opts_test <- antaresRead::setSimulationPath(study_latest_version, "input") - - # areas list - antaresRead::getAreas(opts = opts_test) + # INIT with creation BC + # multi properties + data_terms <- list("at%fr" = "1%10", + "at%fr" = "1%11", + "fr%it" = "1%-5", + "at.at_gas" = "1%10") + + name_bc <- "bc_group_multi_offset" + name_group <- "group_test" + + createBindingConstraint( + name = name_bc, + enabled = TRUE, + timeStep = "hourly", + operator = "both", + group = name_group, + overwrite = TRUE, + coefficients = data_terms) + + createBindingConstraint( + name = "bc_test_default_group", + enabled = TRUE, + timeStep = "hourly", + operator = "both", + overwrite = TRUE, + coefficients = data_terms) # read - bc_names_v870 <- names(readBindingConstraints(opts = opts_test)) + bc_names_v870 <- names(readBindingConstraints()) # delete - removeBindingConstraint(bc_names_v870[1], - opts = opts_test) + removeBindingConstraint(name = bc_names_v870[1]) # read - bc_in_study <- readBindingConstraints(opts = opts_test) + bc_in_study <- readBindingConstraints() # test - if(is.null(bc_in_study)) - testthat::expect_null(names(bc_in_study)) - else - testthat::expect_false(bc_names_v870[1] %in% - names(readBindingConstraints(opts = opts_test))) + testthat::expect_false(bc_names_v870[1] %in% + names(readBindingConstraints())) # again with "both" binding constraint - bc_names_v870 <- names(readBindingConstraints(opts = opts_test)) + bc_names_v870 <- names(readBindingConstraints()) - removeBindingConstraint(bc_names_v870[3], - opts = opts_test) + removeBindingConstraint(name = bc_names_v870[1]) - bc_in_study <- readBindingConstraints(opts = opts_test) + bc_in_study <- readBindingConstraints() # test - if(is.null(bc_in_study)) - testthat::expect_null(names(bc_in_study)) - else - testthat::expect_false(bc_names_v870[3] %in% - names(readBindingConstraints(opts = opts_test))) - - # remove temporary study - unlink(x = study_latest_version, recursive = TRUE) - + testthat::expect_false(bc_names_v870[1] %in% + names(readBindingConstraints())) }) ## multi names ---- test_that("removeBindingConstraint v8.7.0 by names", { - # read / open template study - setup_study_last(dir_path = sourcedir_last_study) - opts_test <- antaresRead::setSimulationPath(study_latest_version, "input") - - # areas list - antaresRead::getAreas(opts = opts_test) + # INIT with creation BC + # multi properties + data_terms <- list("at%fr" = "1%10", + "at%fr" = "1%11", + "fr%it" = "1%-5", + "at.at_gas" = "1%10") + + name_bc <- "bc_group_multi_offset" + name_group <- "group_test" + + createBindingConstraint( + name = name_bc, + enabled = TRUE, + timeStep = "hourly", + operator = "both", + group = name_group, + overwrite = TRUE, + coefficients = data_terms) + + createBindingConstraint( + name = "bc_test_default_group", + enabled = TRUE, + timeStep = "hourly", + operator = "both", + overwrite = TRUE, + coefficients = data_terms) # read - bc_names_v870 <- names(readBindingConstraints(opts = opts_test)) + bc_names_v870 <- names(readBindingConstraints()) # delete - name_to_delete <- bc_names_v870[-1] - removeBindingConstraint(name_to_delete, - opts = opts_test) + name_to_delete <- bc_names_v870 + removeBindingConstraint(name_to_delete) # read - bc_in_study <- readBindingConstraints(opts = opts_test) + bc_in_study <- readBindingConstraints() # test - if(is.null(bc_in_study)) - testthat::expect_null(names(bc_in_study)) - else - testthat::expect_false(all(name_to_delete %in% - names(readBindingConstraints(opts = opts_test)))) - - # remove temporary study - unlink(x = study_latest_version, recursive = TRUE) + testthat::expect_false(all(name_to_delete %in% + bc_in_study)) }) ## one group ---- test_that("removeBindingConstraint v8.7.0 by group", { - # read / open template study - setup_study_last(dir_path = sourcedir_last_study) - opts_test <- antaresRead::setSimulationPath(study_latest_version, "input") - - # areas list - antaresRead::getAreas(opts = opts_test) + # INIT with creation BC + # multi properties + data_terms <- list("at%fr" = "1%10", + "at%fr" = "1%11", + "fr%it" = "1%-5", + "at.at_gas" = "1%10") + + name_bc <- "bc_group_multi_offset" + name_group <- "group_test" + + createBindingConstraint( + name = name_bc, + enabled = TRUE, + timeStep = "hourly", + operator = "both", + group = name_group, + overwrite = TRUE, + coefficients = data_terms) # read bc <- readBindingConstraints(opts = opts_test) # delete - group_to_delete <- bc$bc_2$properties$group + group_to_delete <- bc$bc_group_multi_offset$properties$group - removeBindingConstraint(group = group_to_delete, - opts = opts_test) + removeBindingConstraint(group = group_to_delete) # read - bc_in_study <- readBindingConstraints(opts = opts_test) + bc_in_study <- readBindingConstraints() # test - if(is.null(bc_in_study)) - testthat::expect_null(names(bc_in_study)) - else - testthat::expect_false(all(group_to_delete %in% - names(readBindingConstraints(opts = opts_test)))) - # remove temporary study - unlink(x = study_latest_version, recursive = TRUE) + testthat::expect_false(all(group_to_delete %in% + bc_in_study)) }) ## multi group ---- test_that("removeBindingConstraint v8.7.0 by group", { - # read / open template study - setup_study_last(dir_path = sourcedir_last_study) - opts_test <- antaresRead::setSimulationPath(study_latest_version, "input") - - # areas list - antaresRead::getAreas(opts = opts_test) + # INIT with creation BC + # multi properties + data_terms <- list("at%fr" = "1%10", + "at%fr" = "1%11", + "fr%it" = "1%-5", + "at.at_gas" = "1%10") + + name_bc1 <- "bc_group_multi_offset" + name_group1 <- "group_test" + + name_bc2 <- "bc_group_multi_offset2" + name_group2 <- "group_test2" + + createBindingConstraint( + name = name_bc1, + enabled = TRUE, + timeStep = "hourly", + operator = "both", + group = name_group1, + overwrite = TRUE, + coefficients = data_terms) + + createBindingConstraint( + name = name_bc2, + enabled = TRUE, + timeStep = "hourly", + operator = "both", + group = name_group2, + overwrite = TRUE, + coefficients = data_terms) # read - bc <- readBindingConstraints(opts = opts_test) + bc <- readBindingConstraints() # select all groups group_to_delete <- sapply(bc, function(x){ @@ -166,18 +224,15 @@ test_that("removeBindingConstraint v8.7.0 by group", { }) # delete all groups - removeBindingConstraint(group = group_to_delete, - opts = opts_test) + removeBindingConstraint(group = group_to_delete) # read - bc_in_study <- readBindingConstraints(opts = opts_test) + bc_in_study <- readBindingConstraints() # test - if(is.null(bc_in_study)) - testthat::expect_null(names(bc_in_study)) - else - testthat::expect_false(all(group_to_delete %in% - names(readBindingConstraints(opts = opts_test)))) - # remove temporary study - unlink(x = study_latest_version, recursive = TRUE) + testthat::expect_false(all(group_to_delete %in% + bc_in_study)) }) + +## remove temporary study ---- +deleteStudy() diff --git a/tests/testthat/test-scenarioBuilder.R b/tests/testthat/test-scenarioBuilder.R index 9e731532..b4db0a2d 100644 --- a/tests/testthat/test-scenarioBuilder.R +++ b/tests/testthat/test-scenarioBuilder.R @@ -534,9 +534,14 @@ test_that("updateScenarioBuilder() has error if names of list or argument series # v870 ---- test_that("scenarioBuilder works with binding constraint (v870)", { - # read / open template study - setup_study_last(dir_path = sourcedir_last_study) - opts_test <- antaresRead::setSimulationPath(study_latest_version, "input") + # study test creation ---- + # read script to generate study v8.7.0 + sourcedir_last_study <- system.file("study_test_generator/generate_test_study_870.R", + package = "antaresEditObject") + + # create study + source(file = sourcedir_last_study) + opts_test <- simOptions() ## no group rand ---- sbuilder <- scenarioBuilder( @@ -584,6 +589,9 @@ test_that("scenarioBuilder works with binding constraint (v870)", { testthat::expect_equal(rownames(prev_sb$bc), "group_test") + # clear + clearScenarioBuilder() + ## no bc mode ---- # (classic mode of operation) sbuilder <- scenarioBuilder() @@ -597,7 +605,7 @@ test_that("scenarioBuilder works with binding constraint (v870)", { prev_sb <- readScenarioBuilder(as_matrix = TRUE) # test - testthat::expect_equal(names(prev_sb), c("bc", "t")) + testthat::expect_equal(names(prev_sb), "t") ## parameter n_mc NULL ---- # (classic mode of operation) @@ -606,9 +614,7 @@ test_that("scenarioBuilder works with binding constraint (v870)", { n_mc = NULL, group_bc = c("group_test", "default"), group_bc_rand = NULL, - mode = "bc", - opts = opts_test - ) + mode = "bc") # Update scenario builder # for binding constraints series @@ -620,8 +626,8 @@ test_that("scenarioBuilder works with binding constraint (v870)", { # test value_default_n_mc <- opts_test$parameters$general$nbyears - testthat::expect_equal(prev_sb$bc[1,], seq(value_default_n_mc)) + testthat::expect_equal(prev_sb$bc[1, drop = FALSE], rep(value_default_n_mc)) # remove temporary study - unlink(x = study_latest_version, recursive = TRUE) + deleteStudy() }) From 78992a005d6c61faa89572749ffe4d297bbdc3b4 Mon Sep 17 00:00:00 2001 From: berthetclement Date: Thu, 23 May 2024 15:37:32 +0200 Subject: [PATCH 70/74] updated herlpet_init.R to no longer use study >= 800 from antaresRead --- tests/testthat/helper_init.R | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/tests/testthat/helper_init.R b/tests/testthat/helper_init.R index e387e002..8ade53ef 100644 --- a/tests/testthat/helper_init.R +++ b/tests/testthat/helper_init.R @@ -25,20 +25,3 @@ setup_study <- function(study, sourcedir) { assign("nweeks", 2, envir = globalenv()) } } - -# study version > v800 ---- - # new template study for each new release -sourcedir_last_study <- system.file("test_v8", package = "antaresRead") - -setup_study_last <- function(dir_path){ - studies <- list.files(dir_path, pattern = "\\.tar\\.gz$", full.names = TRUE) - studies_last_version <- studies[grep(x = studies, pattern = "v87")] - # untar etude - path_last_version <- file.path(tempdir(), "studyv870") - untar(studies_last_version[1], exdir = path_last_version) # v87 - study_temp_path <- file.path(path_last_version, "test_case") - - assign("study_latest_version", - study_temp_path, - envir = globalenv()) -} From fe438b7ca2a7e742ad177912fee4446c34f3e433 Mon Sep 17 00:00:00 2001 From: berthetclement Date: Thu, 23 May 2024 17:49:44 +0200 Subject: [PATCH 71/74] createBindingConstraint updated with "/terms" endpoint to add list of terms --- R/createBindingConstraint.R | 50 ++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/R/createBindingConstraint.R b/R/createBindingConstraint.R index 2e316188..e50a652c 100644 --- a/R/createBindingConstraint.R +++ b/R/createBindingConstraint.R @@ -332,33 +332,37 @@ createBindingConstraint <- function(name, body_terms <- body$coeffs body$coeffs <- NULL - # extract areas/cluster (links or thermal) - terms_values <- strsplit(x = names(body_terms), split = "%|\\.") - - is_dot <- grepl(x = names(body_terms), - pattern = "\\.") - - # build list - if(is_dot) - data_list <- list(area=terms_values[[1]][1], - cluster=terms_values[[1]][2]) - else - data_list <- list(area1=terms_values[[1]][1], - area2=terms_values[[1]][2]) - - if(length(body_terms[[1]])>1) - body_terms <- list(weight=body_terms[[1]][1], - offset=body_terms[[1]][2], - data=data_list) - else - body_terms <- list(weight=body_terms[[1]][1], - data=data_list) + body_terms <- lapply(seq(length(body_terms)), function(x){ + # extract areas/cluster (links or thermal) + name_coeff <- names(body_terms[x]) + term_coeff <- body_terms[x] + terms_values <- strsplit(x = name_coeff, split = "%|\\.") + + is_dot <- grepl(x = name_coeff, + pattern = "\\.") + + # build list + if(is_dot) + data_list <- list(area=terms_values[[1]][1], + cluster=terms_values[[1]][2]) + else + data_list <- list(area1=terms_values[[1]][1], + area2=terms_values[[1]][2]) + + if(length(term_coeff[[1]])>1) + body_terms <- list(weight=term_coeff[[1]][1], + offset=term_coeff[[1]][2], + data=data_list) + else + body_terms <- list(weight=term_coeff[[1]][1], + data=data_list) + }) # make json file body_terms <- jsonlite::toJSON(body_terms, auto_unbox = TRUE) } - + # make json file body <- jsonlite::toJSON(body, auto_unbox = TRUE) @@ -381,7 +385,7 @@ createBindingConstraint <- function(name, endpoint = file.path(opts$study_id, "bindingconstraints", result$id, - "term"), + "terms"), body = body_terms, encode = "raw") From c62cc932e90df8f4a335d66d0ed0f4414dd282e7 Mon Sep 17 00:00:00 2001 From: berthetclement Date: Fri, 24 May 2024 15:12:05 +0200 Subject: [PATCH 72/74] editBindingConstraint updated with "/terms" endpoint to update list of coefficient --- R/editBindingConstraint.R | 55 ++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/R/editBindingConstraint.R b/R/editBindingConstraint.R index dc9cac07..bba4d724 100644 --- a/R/editBindingConstraint.R +++ b/R/editBindingConstraint.R @@ -282,7 +282,7 @@ editBindingConstraint <- function(name, # api part code .editBC_api <- function(..., opts){ body <- list(...) - # multi vers checks (legacy) + # checks for any study version (legacy) if (is.null(body$time_step)) stop("You must provide `timeStep` argument with API.", call. = FALSE) @@ -344,30 +344,31 @@ editBindingConstraint <- function(name, body_terms <- body$coeffs body$coeffs <- NULL - # extract areas/cluster (links or thermal) - id_terms <- names(body_terms) - terms_values <- strsplit(x = id_terms, split = "%|\\.") - - is_dot <- grepl(x = id_terms, - pattern = "\\.") - - # build list - if(is_dot) - data_list <- list(area=terms_values[[1]][1], - cluster=terms_values[[1]][2]) - else - data_list <- list(area1=terms_values[[1]][1], - area2=terms_values[[1]][2]) - - if(length(body_terms[[1]])>1) - body_terms <- list(id=id_terms, - weight=body_terms[[1]][1], - offset=body_terms[[1]][2], - data=data_list) - else - body_terms <- list(id=id_terms, - weight=body_terms[[1]][1], - data=data_list) + body_terms <- lapply(seq(length(body_terms)), function(x){ + # extract areas/cluster (links or thermal) + name_coeff <- names(body_terms[x]) + term_coeff <- body_terms[x] + terms_values <- strsplit(x = name_coeff, split = "%|\\.") + + is_dot <- grepl(x = name_coeff, + pattern = "\\.") + + # build list + if(is_dot) + data_list <- list(area=terms_values[[1]][1], + cluster=terms_values[[1]][2]) + else + data_list <- list(area1=terms_values[[1]][1], + area2=terms_values[[1]][2]) + + if(length(term_coeff[[1]])>1) + body_terms <- list(weight=term_coeff[[1]][1], + offset=term_coeff[[1]][2], + data=data_list) + else + body_terms <- list(weight=term_coeff[[1]][1], + data=data_list) + }) # make json file body_terms <- jsonlite::toJSON(body_terms, @@ -400,13 +401,13 @@ editBindingConstraint <- function(name, result$group, "validate")) - # specific endpoint for coeffs/term + # specific endpoint for coeffs/terms if(!is.null(body_terms)) api_put(opts = opts, endpoint = file.path(opts$study_id, "bindingconstraints", result$id, - "term"), + "terms"), body = body_terms, encode = "raw") From 58354a32fa9636779a4cd5d0579dc66b455e4721 Mon Sep 17 00:00:00 2001 From: berthetclement Date: Tue, 28 May 2024 14:44:06 +0200 Subject: [PATCH 73/74] editBindingConstraint minor fix with check parameter in sub function api part --- R/editBindingConstraint.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/editBindingConstraint.R b/R/editBindingConstraint.R index bba4d724..84ce11d0 100644 --- a/R/editBindingConstraint.R +++ b/R/editBindingConstraint.R @@ -286,7 +286,7 @@ editBindingConstraint <- function(name, if (is.null(body$time_step)) stop("You must provide `timeStep` argument with API.", call. = FALSE) - if (is.null(body$time_step)) + if (is.null(body$operator)) stop("You must provide `operator` argument with API.", call. = FALSE) From 12126d0a154f3a5b62d4f17506f5314fa16c61af Mon Sep 17 00:00:00 2001 From: berthetclement Date: Tue, 28 May 2024 15:06:01 +0200 Subject: [PATCH 74/74] github actions remote branch on develop --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 177745d1..2e5cd967 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -54,5 +54,5 @@ Suggests: rmarkdown VignetteBuilder: knitr Remotes: - rte-antares-rpackage/antaresRead@release/v8.7.0 + rte-antares-rpackage/antaresRead@develop