Skip to content

Commit

Permalink
Fix conflict after pulling from master
Browse files Browse the repository at this point in the history
  • Loading branch information
kemihak committed Aug 25, 2023
2 parents 8d9a787 + 5dcfd4e commit e5ba62b
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 44 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ NEW FEATURES (Antares v8.6) :
* New functions `createClusterST()`, `editClusterST()`, `removeClusterST()` ("st-storage" family of functions for a study in "input" mode)
* Add control of data consistency between `mingen.txt` and `mod.txt` based on values in `hydro.ini` file
* Add control of data consistency between `mingen.txt` and `maxpower.txt` based on values in `hydro.ini` file
* Add and edit the property `enable-first-step` in `adequacy patch` section in `settings/generaldata.ini`

NEW FEATURES :

Expand All @@ -32,6 +33,8 @@ NEW FEATURES :
BUGFIXES :

* `api_command_execute()` add timer to request api
* `writeInputTS()` works with argument `type = "tsLink"`
* `createLink()` and `editLink()` write the appropriate time series in _direct.txt and _indirect.txt files even if the areas `from` and `to` given as arguments are not sorted


# antaresEditObject 0.5.1
Expand Down
16 changes: 12 additions & 4 deletions R/createLink.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ createLink <- function(from,
check_area_name(to, opts)
# areas' order
areas <- c(from, to)
if (!identical(areas, sort(areas))) {
are_areas_sorted <- identical(areas, sort(areas))
if (!are_areas_sorted) {
from <- areas[2]
to <- areas[1]
}
Expand Down Expand Up @@ -141,12 +142,19 @@ createLink <- function(from,
tsLink <- matrix(data = rep(0, 8760*2), ncol = 2)
}
tsLink <- data.table::as.data.table(tsLink)
direct <- seq_len(NCOL(tsLink) / 2)
indirect <- setdiff(seq_len(NCOL(tsLink)), seq_len(NCOL(tsLink) / 2))
first_cols <- seq_len(NCOL(tsLink) / 2)
last_cols <- setdiff(seq_len(NCOL(tsLink)), seq_len(NCOL(tsLink) / 2))
if (are_areas_sorted) {
direct <- first_cols
indirect <- last_cols
} else {
direct <- last_cols
indirect <- first_cols
}

# correct column order for antares < 820
if (!v820) {
if (!identical(areas, sort(areas))) {
if (!are_areas_sorted) {
dataLink[, 1:2] <- dataLink[, 2:1]
if (v7) {
dataLink[, 3:4] <- dataLink[, 4:3]
Expand Down
19 changes: 12 additions & 7 deletions R/editLink.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ editLink <- function(from,

# areas' order
areas <- c(from, to)
if (!identical(areas, sort(areas))) {
are_areas_sorted <- identical(areas, sort(areas))
if (!are_areas_sorted) {
from <- areas[2]
to <- areas[1]
}
Expand All @@ -106,8 +107,15 @@ editLink <- function(from,
"tsLink must have an even number of columns" = identical(ncol(tsLink) %% 2, 0)
)
if (v820) {
direct <- seq_len(NCOL(tsLink) / 2)
indirect <- setdiff(seq_len(NCOL(tsLink)), seq_len(NCOL(tsLink) / 2))
first_cols <- seq_len(NCOL(tsLink) / 2)
last_cols <- setdiff(seq_len(NCOL(tsLink)), seq_len(NCOL(tsLink) / 2))
if (are_areas_sorted) {
direct <- first_cols
indirect <- last_cols
} else {
direct <- last_cols
indirect <- first_cols
}
tsLink <- data.table::as.data.table(tsLink)
} else {
warning("tsLink will be ignored since Antares version < 820.", call. = FALSE)
Expand Down Expand Up @@ -223,7 +231,7 @@ editLink <- function(from,
file = file.path(inputPath, "links", from, paste0(to, "_parameters.txt"))
)
} else {
if (!identical(areas, sort(areas))) {
if (!are_areas_sorted) {
dataLink[, 1:2] <- dataLink[, 2:1]
dataLink[, 4:5] <- dataLink[, 5:4]
}
Expand Down Expand Up @@ -269,6 +277,3 @@ editLink <- function(from,

invisible(res)
}



16 changes: 12 additions & 4 deletions R/updateAdequacySettings.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
#' @param include_adq_patch Logical. If TRUE, will run Adequacy Patch
#' @param set_to_null_ntc_from_physical_out_to_physical_in_for_first_step Logical. default to TRUE
#' @param set_to_null_ntc_between_physical_out_for_first_step Logical. default to TRUE
#' @param price_taking_order Character. can take values DENS (default value) and Load.
#' @param include_hurdle_cost_csr Logical. default to FALSE
#' @param check_csr_cost_function Logical. default to FALSE
#' @param enable_first_step Logical. default to TRUE
#' @param price_taking_order Character. can take values DENS (default value) and Load.
#' @param threshold_initiate_curtailment_sharing_rule Double. default to 0.0
#' @param threshold_display_local_matching_rule_violations Double. default to 0.0
#' @param threshold_csr_variable_bounds_relaxation Integer. default to 3
Expand All @@ -36,9 +37,10 @@
updateAdequacySettings <- function(include_adq_patch = NULL,
set_to_null_ntc_from_physical_out_to_physical_in_for_first_step = NULL,
set_to_null_ntc_between_physical_out_for_first_step = NULL,
price_taking_order = NULL,
include_hurdle_cost_csr = NULL,
check_csr_cost_function = NULL,
enable_first_step = NULL,
price_taking_order = NULL,
threshold_initiate_curtailment_sharing_rule = NULL,
threshold_display_local_matching_rule_violations = NULL,
threshold_csr_variable_bounds_relaxation = NULL,
Expand Down Expand Up @@ -81,7 +83,7 @@ updateAdequacySettings <- function(include_adq_patch = NULL,
if (!is.null(set_to_null_ntc_between_physical_out_for_first_step))
adequacy$`set-to-null-ntc-between-physical-out-for-first-step` <- set_to_null_ntc_between_physical_out_for_first_step

if (opts$antaresVersion >= 850){
if (opts$antaresVersion >= 850) {
if (!is.null(price_taking_order))
adequacy$`price-taking-order` <- price_taking_order
if (!is.null(include_hurdle_cost_csr))
Expand All @@ -95,7 +97,13 @@ updateAdequacySettings <- function(include_adq_patch = NULL,
if (!is.null(threshold_csr_variable_bounds_relaxation))
adequacy$`threshold-csr-variable-bounds-relaxation` <- threshold_csr_variable_bounds_relaxation
}


# Necessary only for desktop application. Not used in API mode.
if (opts$antaresVersion >= 860) {
if (!is.null(enable_first_step)) {
adequacy$`enable-first-step` <- enable_first_step
}
}
general$`adequacy patch` <- adequacy

writeIni(listData = general, pathIni = pathIni, overwrite = TRUE)
Expand Down
6 changes: 5 additions & 1 deletion R/writeInputTS.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ writeInputTS <- function(data,
opts = antaresRead::simOptions()) {

type <- match.arg(type)
check_area_name(area, opts)
# No control on area possible for area with type = "tsLink"
if (type != "tsLink") {
check_area_name(area, opts)
}


assertthat::assert_that(inherits(opts, "simOptions"))

Expand Down
9 changes: 6 additions & 3 deletions man/updateAdequacySettings.Rd

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

78 changes: 76 additions & 2 deletions tests/testthat/test-createLink.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,82 @@ sapply(studies, function(study) {
})



test_that("Check if createLink() in version >= 8.2 writes time series link in the right file 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"))

dat_mat <- c(1,3,2,4)
dat_mat_inv <- c(2,4,1,3)
nb_cols <- length(dat_mat)
mat_multi_scen <- matrix(data = rep(dat_mat, each = 8760), ncol = nb_cols)
mat_multi_scen_inv <- matrix(data = rep(dat_mat_inv, each = 8760), ncol = nb_cols)

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"))

# alphabetical order ----
createLink(from = area, to = area2, opts = opts, tsLink = mat_multi_scen, overwrite = TRUE)
suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input"))

# first columns go to direct file
expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_direct_link_file),
as.data.table(mat_multi_scen[,seq(1, nb_cols/2)]))

# last columns go to indirect file
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)]))

editLink(from = area, to = area2, opts = opts, tsLink = mat_multi_scen_inv)

# first columns go to direct file
expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_direct_link_file),
as.data.table(mat_multi_scen_inv[,seq(1, nb_cols/2)]))

# last columns go to indirect file
expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_indirect_link_file),
as.data.table(mat_multi_scen_inv[,seq((nb_cols/2)+1, nb_cols)]))

# no alphabetical order ----
createLink(from = area2, to = area, opts = opts, tsLink = mat_multi_scen_inv, overwrite = TRUE)

# first columns go to indirect file
expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_indirect_link_file),
as.data.table(mat_multi_scen_inv[,seq(1, nb_cols/2)]))

# last columns go to direct file
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)]))

editLink(from = area2, to = area, opts = opts, tsLink = mat_multi_scen)

# first columns go to direct file
expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_indirect_link_file),
as.data.table(mat_multi_scen[,seq(1, nb_cols/2)]))

# last columns go to indirect file
expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_direct_link_file),
as.data.table(mat_multi_scen[,seq((nb_cols/2)+1, nb_cols)]))

})


test_that("removeLink() in 8.2.0 : check if the expected files are deleted/updated", {

ant_version <- "8.2.0"
Expand Down Expand Up @@ -150,5 +226,3 @@ test_that("removeLink() in 8.2.0 : check if the expected files are deleted/updat

unlink(x = opts$studyPath, recursive = TRUE)
})


49 changes: 27 additions & 22 deletions tests/testthat/test-updateAdequacyPatch.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,34 @@
context("Function updateAdequacySettings")


sapply(studies, function(study) {
test_that("Update an adequacy parameter for an Antares version 860", {

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 <- "aa"
area2 <- "zz"
createArea(area)
createArea(area2)
suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input"))

setup_study(study, sourcedir)
opts <- antaresRead::setSimulationPath(studyPath, "input")

if (opts$antaresVersion >= 850){
test_that("Update an adequacy parameter", {

updateAdequacySettings(include_adq_patch = TRUE)
updateAdequacySettings(set_to_null_ntc_from_physical_out_to_physical_in_for_first_step = FALSE)
updateAdequacySettings(set_to_null_ntc_between_physical_out_for_first_step = FALSE)
updateAdequacySettings(check_csr_cost_function = TRUE)

if (opts$antaresVersion >= 850) {
updateAdequacySettings(include_adq_patch = TRUE)
updateAdequacySettings(set_to_null_ntc_from_physical_out_to_physical_in_for_first_step = FALSE)
updateAdequacySettings(set_to_null_ntc_between_physical_out_for_first_step = FALSE)
updateAdequacySettings(check_csr_cost_function = TRUE)

expect_true(getOption("antares")$parameters$`adequacy patch`$`include-adq-patch`)
expect_false(getOption("antares")$parameters$`adequacy patch`$`set-to-null-ntc-from-physical-out-to-physical-in-for-first-step`)
expect_false(getOption("antares")$parameters$`adequacy patch`$`set-to-null-ntc-between-physical-out-for-first-step`)
expect_true(getOption("antares")$parameters$`adequacy patch`$`check-csr-cost-function`)

})

# remove temporary study
unlink(x = file.path(pathstd, "test_case"), recursive = TRUE)
expect_true(getOption("antares")$parameters$`adequacy patch`$`include-adq-patch`)
expect_false(getOption("antares")$parameters$`adequacy patch`$`set-to-null-ntc-from-physical-out-to-physical-in-for-first-step`)
expect_false(getOption("antares")$parameters$`adequacy patch`$`set-to-null-ntc-between-physical-out-for-first-step`)
expect_true(getOption("antares")$parameters$`adequacy patch`$`check-csr-cost-function`)
}

if (opts$antaresVersion >= 860) {
updateAdequacySettings(enable_first_step = FALSE)

expect_false(getOption("antares")$parameters$`adequacy patch`$`enable-first-step`)
}

})
unlink(x = opts$studyPath, recursive = TRUE)
})
50 changes: 49 additions & 1 deletion tests/testthat/test-writeInputTS.R
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ test_that("create mingen file data v860", {
})



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"
Expand Down Expand Up @@ -445,7 +446,54 @@ test_that("writeInputTS() in 8.6.0 : check if new data is written when control i
as.data.table(mat_mod_init))

unlink(x = opts$studyPath, recursive = TRUE)

})


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)]))

})

0 comments on commit e5ba62b

Please sign in to comment.