From 6ab89c21ce98d0e2ea0db17e650abccfe3a7f3a8 Mon Sep 17 00:00:00 2001 From: kemihak Date: Thu, 28 Nov 2024 16:58:16 +0100 Subject: [PATCH 1/6] Add dicoAdequacySettings() function, clean list of values to send by removing NULL --- NAMESPACE | 1 + NEWS.md | 3 + R/updateAdequacySettings.R | 135 +++++++++++++++++++++++------------- man/dicoAdequacySettings.Rd | 20 ++++++ 4 files changed, 109 insertions(+), 50 deletions(-) create mode 100644 man/dicoAdequacySettings.Rd diff --git a/NAMESPACE b/NAMESPACE index 3c974f59..adef8482 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -34,6 +34,7 @@ export(createVariant) export(create_scb_referential_series_type) export(deduplicateScenarioBuilder) export(deleteStudy) +export(dicoAdequacySettings) export(dicoGeneralSettings) export(dicoOptimizationSettings) export(dicoOutputSettings) diff --git a/NEWS.md b/NEWS.md index c0733781..5e5e2127 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,6 +6,9 @@ NEW FEATURES (Antares v9.0) : * `createStudy()` takes into account the new format of Antares studies (e.g. 9.0, 9.15 instead of 900, 915) +BUGFIXES : + +* `updateAdequacySettings()` : in API mode do not send NULL value # antaresEditObject 0.7.1 diff --git a/R/updateAdequacySettings.R b/R/updateAdequacySettings.R index 79df0c82..594e248d 100644 --- a/R/updateAdequacySettings.R +++ b/R/updateAdequacySettings.R @@ -46,67 +46,67 @@ updateAdequacySettings <- function(include_adq_patch = NULL, threshold_csr_variable_bounds_relaxation = NULL, opts = antaresRead::simOptions()) { - if (opts$antaresVersion < 830) stop("This function is only available for studies v8.3 or higher.") - assertthat::assert_that(inherits(opts, "simOptions")) + if (opts[["antaresVersion"]] < 830) { + stop("This function is only available for studies v8.3 or higher.") + } + api_study <- is_api_study(opts) + + new_params <- list( + "include_adq_patch" = include_adq_patch, + "set_to_null_ntc_from_physical_out_to_physical_in_for_first_step" = set_to_null_ntc_from_physical_out_to_physical_in_for_first_step, + "set_to_null_ntc_between_physical_out_for_first_step" = set_to_null_ntc_between_physical_out_for_first_step, + "include_hurdle_cost_csr" = include_hurdle_cost_csr, + "check_csr_cost_function" = check_csr_cost_function, + "enable_first_step" = enable_first_step, + "price_taking_order" = price_taking_order, + "threshold_initiate_curtailment_sharing_rule" = threshold_initiate_curtailment_sharing_rule, + "threshold_display_local_matching_rule_violations" = threshold_display_local_matching_rule_violations, + "threshold_csr_variable_bounds_relaxation" = threshold_csr_variable_bounds_relaxation + ) + + new_params <- dropNulls(x = new_params) + + if (opts[["antaresVersion"]] < 850) { + properties_850 <- c("price_taking_order", + "include_hurdle_cost_csr", + "check_csr_cost_function", + "threshold_initiate_curtailment_sharing_rule", + "threshold_display_local_matching_rule_violations", + "threshold_csr_variable_bounds_relaxation") + new_params <- new_params[!names(new_params) %in% properties_850] + } + + if (opts[["antaresVersion"]] < 860 | (opts[["antaresVersion"]] >= 860 & api_study)) { + # enable_first_step is necessary only for desktop application. Not used in API mode. + properties_860 <- c("enable_first_step") + new_params <- new_params[!names(new_params) %in% properties_860] + } + + new_params <- lapply(X = new_params, FUN = .format_ini_rhs) + names(new_params) <- sapply(names(new_params), dicoAdequacySettings, USE.NAMES = FALSE) + # API block - if (is_api_study(opts)) { + if (api_study) { - writeIni( - listData = list( - `include-adq-patch` = include_adq_patch, - `set-to-null-ntc-from-physical-out-to-physical-in-for-first-step` = set_to_null_ntc_from_physical_out_to_physical_in_for_first_step, - `set-to-null-ntc-between-physical-out-for-first-step` = set_to_null_ntc_between_physical_out_for_first_step, - `price-taking-order` = price_taking_order, - `include-hurdle-cost-csr` = include_hurdle_cost_csr, - `check-csr-cost-function` = check_csr_cost_function, - `threshold-initiate-curtailment-sharing-rule` = threshold_initiate_curtailment_sharing_rule, - `threshold-display-local-matching-rule-violations` = threshold_display_local_matching_rule_violations, - `threshold-csr-variable-bounds-relaxation` = threshold_csr_variable_bounds_relaxation - ), - pathIni = "settings/generaldata/adequacy patch", - opts = opts - ) + writeIni(listData = new_params, pathIni = "settings/generaldata/adequacy patch", opts = opts) return(update_api_opts(opts)) } - pathIni <- file.path(opts$studyPath, "settings", "generaldata.ini") - general <- readIniFile(file = pathIni) - - adequacy <- general$`adequacy patch` - if (!is.null(include_adq_patch)) - adequacy$`include-adq-patch` <- include_adq_patch - if (!is.null(set_to_null_ntc_from_physical_out_to_physical_in_for_first_step)) - adequacy$`set-to-null-ntc-from-physical-out-to-physical-in-for-first-step` <- set_to_null_ntc_from_physical_out_to_physical_in_for_first_step - 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 (!is.null(price_taking_order)) - adequacy$`price-taking-order` <- price_taking_order - if (!is.null(include_hurdle_cost_csr)) - adequacy$`include-hurdle-cost-csr` <- include_hurdle_cost_csr - if (!is.null(check_csr_cost_function)) - adequacy$`check-csr-cost-function` <- check_csr_cost_function - if (!is.null(threshold_initiate_curtailment_sharing_rule)) - adequacy$`threshold-initiate-curtailment-sharing-rule` <- threshold_initiate_curtailment_sharing_rule - if (!is.null(threshold_display_local_matching_rule_violations)) - adequacy$`threshold-display-local-matching-rule-violations` <- threshold_display_local_matching_rule_violations - if (!is.null(threshold_csr_variable_bounds_relaxation)) - adequacy$`threshold-csr-variable-bounds-relaxation` <- threshold_csr_variable_bounds_relaxation - } + generaldatapath <- file.path(opts[["studyPath"]], "settings", "generaldata.ini") + generaldata <- readIniFile(file = generaldatapath) - # 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 - } + if ("adequacy patch" %in% names(generaldata)) { + l_output <- generaldata[["adequacy patch"]] + l_output <- modifyList(x = l_output, val = new_params) + } else { + l_output <- new_params } - general$`adequacy patch` <- adequacy + generaldata[["adequacy patch"]] <- l_output - writeIni(listData = general, pathIni = pathIni, overwrite = TRUE) + writeIni(listData = generaldata, pathIni = generaldatapath, overwrite = TRUE, opts = opts) # Maj simulation suppressWarnings({ @@ -152,3 +152,38 @@ convertConfigToAdq <- function(opts = simOptions(), path = "default"){ editArea, adequacy = adequacyOptions(adequacy_patch_mode = "inside")) if (length(pathOut) > 0) setSimulationPath(pathOut) } + + +#' Correspondence between arguments of \code{updateAdequacySettings} and actual Antares parameters. +#' +#' @param arg An argument from function \code{updateAdequacySettings}. +#' +#' @return The corresponding Antares general parameter. +#' +#' @export +#' +#' @examples +#' dicoAdequacySettings("threshold_initiate_curtailment_sharing_rule") +dicoAdequacySettings <- function(arg) { + + if (length(arg) > 1) { + stop("'arg' must be length one") + } + + antares_params <- as.list(c("include-adq-patch", "set-to-null-ntc-from-physical-out-to-physical-in-for-first-step", + "set-to-null-ntc-between-physical-out-for-first-step", "include-hurdle-cost-csr", + "check-csr-cost-function", "enable-first-step", + "price-taking-order", "threshold-initiate-curtailment-sharing-rule", + "threshold-display-local-matching-rule-violations", "threshold-csr-variable-bounds-relaxation" + ) + ) + + names(antares_params) <- c("include_adq_patch", "set_to_null_ntc_from_physical_out_to_physical_in_for_first_step", + "set_to_null_ntc_between_physical_out_for_first_step", "include_hurdle_cost_csr", + "check_csr_cost_function", "enable_first_step", + "price_taking_order", "threshold_initiate_curtailment_sharing_rule", + "threshold_display_local_matching_rule_violations", "threshold_csr_variable_bounds_relaxation" + ) + + return(antares_params[[arg]]) +} diff --git a/man/dicoAdequacySettings.Rd b/man/dicoAdequacySettings.Rd new file mode 100644 index 00000000..2e8bf458 --- /dev/null +++ b/man/dicoAdequacySettings.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/updateAdequacySettings.R +\name{dicoAdequacySettings} +\alias{dicoAdequacySettings} +\title{Correspondence between arguments of \code{updateAdequacySettings} and actual Antares parameters.} +\usage{ +dicoAdequacySettings(arg) +} +\arguments{ +\item{arg}{An argument from function \code{updateAdequacySettings}.} +} +\value{ +The corresponding Antares general parameter. +} +\description{ +Correspondence between arguments of \code{updateAdequacySettings} and actual Antares parameters. +} +\examples{ +dicoAdequacySettings("threshold_initiate_curtailment_sharing_rule") +} From c473419492793b8fce73749b9480a24f5c4e6508 Mon Sep 17 00:00:00 2001 From: kemihak Date: Fri, 29 Nov 2024 08:45:27 +0100 Subject: [PATCH 2/6] Add update_generaldata_by_section() and use it in updateAdequacySettings() --- R/updateAdequacySettings.R | 26 +--------------------- R/utils.R | 32 ++++++++++++++++++++++++++++ man/update_generaldata_by_section.Rd | 22 +++++++++++++++++++ 3 files changed, 55 insertions(+), 25 deletions(-) create mode 100644 man/update_generaldata_by_section.Rd diff --git a/R/updateAdequacySettings.R b/R/updateAdequacySettings.R index 594e248d..a4bf2d5a 100644 --- a/R/updateAdequacySettings.R +++ b/R/updateAdequacySettings.R @@ -87,31 +87,7 @@ updateAdequacySettings <- function(include_adq_patch = NULL, new_params <- lapply(X = new_params, FUN = .format_ini_rhs) names(new_params) <- sapply(names(new_params), dicoAdequacySettings, USE.NAMES = FALSE) - # API block - if (api_study) { - - writeIni(listData = new_params, pathIni = "settings/generaldata/adequacy patch", opts = opts) - - return(update_api_opts(opts)) - } - - generaldatapath <- file.path(opts[["studyPath"]], "settings", "generaldata.ini") - generaldata <- readIniFile(file = generaldatapath) - - if ("adequacy patch" %in% names(generaldata)) { - l_output <- generaldata[["adequacy patch"]] - l_output <- modifyList(x = l_output, val = new_params) - } else { - l_output <- new_params - } - generaldata[["adequacy patch"]] <- l_output - - writeIni(listData = generaldata, pathIni = generaldatapath, overwrite = TRUE, opts = opts) - - # Maj simulation - suppressWarnings({ - res <- antaresRead::setSimulationPath(path = opts$studyPath, simulation = "input") - }) + res <- update_generaldata_by_section(opts = opts, section = "adequacy patch", new_params = new_params) invisible(res) } diff --git a/R/utils.R b/R/utils.R index bea73698..2004a121 100644 --- a/R/utils.R +++ b/R/utils.R @@ -172,3 +172,35 @@ generate_cluster_name <- function(area, cluster_name, add_prefix) { return(paste(as.character(value), collapse = ", ")) } + +#' @title Update a specific section in generaldata.ini file +#' +#' @template opts +#' @param section The section to update. +#' @param new_params The values to write in the section. +#' +#' @importFrom antaresRead readIniFile +update_generaldata_by_section <- function(opts, section, new_params) { + + if (is_api_study(opts = opts)) { + + writeIni(listData = new_params, pathIni = sprintf("settings/generaldata/%s", section), opts = opts) + + } else { + + generaldatapath <- file.path(opts[["studyPath"]], "settings", "generaldata.ini") + generaldata <- readIniFile(file = generaldatapath) + + if (section %in% names(generaldata)) { + l_section <- generaldata[[section]] + l_section <- modifyList(x = l_section, val = new_params) + } else { + l_section <- new_params + } + generaldata[[section]] <- l_section + + writeIni(listData = generaldata, pathIni = generaldatapath, overwrite = TRUE, opts = opts) + } + + return(update_opts(opts = opts)) +} diff --git a/man/update_generaldata_by_section.Rd b/man/update_generaldata_by_section.Rd new file mode 100644 index 00000000..85ea2b86 --- /dev/null +++ b/man/update_generaldata_by_section.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{update_generaldata_by_section} +\alias{update_generaldata_by_section} +\title{Update a specific section in generaldata.ini file} +\usage{ +update_generaldata_by_section(opts, section, new_params) +} +\arguments{ +\item{opts}{List of simulation parameters returned by the function +\code{\link[antaresRead:setSimulationPath]{antaresRead::setSimulationPath()}}} + +\item{section}{The section to update.} + +\item{new_params}{The values to write in the section.} +} +\value{ +An updated list containing various information about the simulation. +} +\description{ +Update a specific section in generaldata.ini file +} From 266e29c4a0c2df777ab54b18a3dd3d4a5afbb4f7 Mon Sep 17 00:00:00 2001 From: kemihak Date: Fri, 29 Nov 2024 08:46:54 +0100 Subject: [PATCH 3/6] Use update_generaldata_by_section() --- R/updateGeneralSettings.R | 21 +-------------------- R/updateOutputSettings.R | 22 +--------------------- 2 files changed, 2 insertions(+), 41 deletions(-) diff --git a/R/updateGeneralSettings.R b/R/updateGeneralSettings.R index 4d70536f..2837e2e1 100644 --- a/R/updateGeneralSettings.R +++ b/R/updateGeneralSettings.R @@ -158,26 +158,7 @@ updateGeneralSettings <- function(mode = NULL, new_params <- lapply(X = new_params, FUN = .format_ini_rhs) names(new_params) <- sapply(names(new_params), dicoGeneralSettings, USE.NAMES = FALSE) - # API block - if (is_api_study(opts)) { - - writeIni(listData = new_params, pathIni = "settings/generaldata/general", opts = opts) - - return(update_api_opts(opts)) - } - - generaldatapath <- file.path(opts[["studyPath"]], "settings", "generaldata.ini") - generaldata <- readIniFile(file = generaldatapath) - - l_general <- generaldata[["general"]] - l_general <- modifyList(x = l_general, val = new_params) - generaldata[["general"]] <- l_general - - writeIni(listData = generaldata, pathIni = generaldatapath, overwrite = TRUE, opts = opts) - - suppressWarnings({ - res <- setSimulationPath(path = opts[["studyPath"]], simulation = "input") - }) + res <- update_generaldata_by_section(opts = opts, section = "general", new_params = new_params) invisible(res) } diff --git a/R/updateOutputSettings.R b/R/updateOutputSettings.R index 96ae6a4e..89ef3bc4 100644 --- a/R/updateOutputSettings.R +++ b/R/updateOutputSettings.R @@ -51,30 +51,10 @@ updateOutputSettings <- function(synthesis = NULL, ) new_params <- dropNulls(x = new_params) - new_params <- lapply(X = new_params, FUN = .format_ini_rhs) names(new_params) <- sapply(names(new_params), dicoOutputSettings, USE.NAMES = FALSE) - # API block - if (is_api_study(opts)) { - - writeIni(listData = new_params, pathIni = "settings/generaldata/output", opts = opts) - - return(update_api_opts(opts)) - } - - generaldatapath <- file.path(opts[["studyPath"]], "settings", "generaldata.ini") - generaldata <- readIniFile(file = generaldatapath) - - l_output <- generaldata[["output"]] - l_output <- modifyList(x = l_output, val = new_params) - generaldata[["output"]] <- l_output - - writeIni(listData = generaldata, pathIni = generaldatapath, overwrite = TRUE, opts = opts) - - suppressWarnings({ - res <- setSimulationPath(path = opts[["studyPath"]], simulation = "input") - }) + res <- update_generaldata_by_section(opts = opts, section = "output", new_params = new_params) invisible(res) } From bb7ba724859fca0c6a580aac9b22d14cebd9d28f Mon Sep 17 00:00:00 2001 From: kemihak Date: Fri, 29 Nov 2024 15:15:50 +0100 Subject: [PATCH 4/6] Treat enable_first_step property as disabled in API and disk mode. --- R/updateAdequacySettings.R | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/R/updateAdequacySettings.R b/R/updateAdequacySettings.R index a4bf2d5a..f1f7e505 100644 --- a/R/updateAdequacySettings.R +++ b/R/updateAdequacySettings.R @@ -51,7 +51,6 @@ updateAdequacySettings <- function(include_adq_patch = NULL, if (opts[["antaresVersion"]] < 830) { stop("This function is only available for studies v8.3 or higher.") } - api_study <- is_api_study(opts) new_params <- list( "include_adq_patch" = include_adq_patch, @@ -78,10 +77,11 @@ updateAdequacySettings <- function(include_adq_patch = NULL, new_params <- new_params[!names(new_params) %in% properties_850] } - if (opts[["antaresVersion"]] < 860 | (opts[["antaresVersion"]] >= 860 & api_study)) { - # enable_first_step is necessary only for desktop application. Not used in API mode. - properties_860 <- c("enable_first_step") - new_params <- new_params[!names(new_params) %in% properties_860] + if (opts[["antaresVersion"]] >= 860) { + if ("enable_first_step" %in% names(new_params) & new_params[["enable_first_step"]]) { + message("Property enable_first_step is disabled for the moment. Set to FALSE.\n") + new_params[["enable_first_step"]] <- FALSE + } } new_params <- lapply(X = new_params, FUN = .format_ini_rhs) @@ -93,8 +93,6 @@ updateAdequacySettings <- function(include_adq_patch = NULL, } - - #' @title Read adequacy patch config.yml into Antares (v8.5+) #' #' @description From 03a6688b46efa8af810d79d1540db0121c4a40cc Mon Sep 17 00:00:00 2001 From: kemihak Date: Fri, 29 Nov 2024 16:28:07 +0100 Subject: [PATCH 5/6] Set enable_first_step to FALSE if it is in new_params --- R/updateAdequacySettings.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/updateAdequacySettings.R b/R/updateAdequacySettings.R index f1f7e505..01512075 100644 --- a/R/updateAdequacySettings.R +++ b/R/updateAdequacySettings.R @@ -78,7 +78,7 @@ updateAdequacySettings <- function(include_adq_patch = NULL, } if (opts[["antaresVersion"]] >= 860) { - if ("enable_first_step" %in% names(new_params) & new_params[["enable_first_step"]]) { + if ("enable_first_step" %in% names(new_params)) { message("Property enable_first_step is disabled for the moment. Set to FALSE.\n") new_params[["enable_first_step"]] <- FALSE } From e242f815c313f2c6395fb868d56bef246603961f Mon Sep 17 00:00:00 2001 From: kemihak Date: Tue, 21 Jan 2025 10:08:11 +0100 Subject: [PATCH 6/6] Do not export dicoAdequacySettings() --- NAMESPACE | 1 - R/updateAdequacySettings.R | 6 +----- man/dicoAdequacySettings.Rd | 3 --- 3 files changed, 1 insertion(+), 9 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index adef8482..3c974f59 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -34,7 +34,6 @@ export(createVariant) export(create_scb_referential_series_type) export(deduplicateScenarioBuilder) export(deleteStudy) -export(dicoAdequacySettings) export(dicoGeneralSettings) export(dicoOptimizationSettings) export(dicoOutputSettings) diff --git a/R/updateAdequacySettings.R b/R/updateAdequacySettings.R index 01512075..b297f700 100644 --- a/R/updateAdequacySettings.R +++ b/R/updateAdequacySettings.R @@ -133,11 +133,7 @@ convertConfigToAdq <- function(opts = simOptions(), path = "default"){ #' @param arg An argument from function \code{updateAdequacySettings}. #' #' @return The corresponding Antares general parameter. -#' -#' @export -#' -#' @examples -#' dicoAdequacySettings("threshold_initiate_curtailment_sharing_rule") +#' dicoAdequacySettings <- function(arg) { if (length(arg) > 1) { diff --git a/man/dicoAdequacySettings.Rd b/man/dicoAdequacySettings.Rd index 2e8bf458..f1b48fc0 100644 --- a/man/dicoAdequacySettings.Rd +++ b/man/dicoAdequacySettings.Rd @@ -15,6 +15,3 @@ The corresponding Antares general parameter. \description{ Correspondence between arguments of \code{updateAdequacySettings} and actual Antares parameters. } -\examples{ -dicoAdequacySettings("threshold_initiate_curtailment_sharing_rule") -}