diff --git a/NEWS.md b/NEWS.md index d619c0b..56a1a42 100644 --- a/NEWS.md +++ b/NEWS.md @@ -25,6 +25,9 @@ BUGFIXES : * *[private function]* `api_command_execute()` manage snapshot generation of a variant study with a tempo to wait the end of current task (prevents the order from being ignored). +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 79df0c8..b297f70 100644 --- a/R/updateAdequacySettings.R +++ b/R/updateAdequacySettings.R @@ -46,79 +46,53 @@ 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")) - # API block - if (is_api_study(opts)) { - - 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 - ) - - return(update_api_opts(opts)) + if (opts[["antaresVersion"]] < 830) { + stop("This function is only available for studies v8.3 or higher.") } - pathIni <- file.path(opts$studyPath, "settings", "generaldata.ini") - general <- readIniFile(file = pathIni) + 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 + ) - 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 + new_params <- dropNulls(x = new_params) - 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 + 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] } - # 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 (opts[["antaresVersion"]] >= 860) { + 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 + } } - general$`adequacy patch` <- adequacy - writeIni(listData = general, pathIni = pathIni, overwrite = TRUE) - - # Maj simulation - suppressWarnings({ - res <- antaresRead::setSimulationPath(path = opts$studyPath, simulation = "input") - }) + new_params <- lapply(X = new_params, FUN = .format_ini_rhs) + names(new_params) <- sapply(names(new_params), dicoAdequacySettings, USE.NAMES = FALSE) + + res <- update_generaldata_by_section(opts = opts, section = "adequacy patch", new_params = new_params) invisible(res) } - - #' @title Read adequacy patch config.yml into Antares (v8.5+) #' #' @description @@ -152,3 +126,34 @@ 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. +#' +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/R/updateGeneralSettings.R b/R/updateGeneralSettings.R index 4d70536..2837e2e 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 96ae6a4..89ef3bc 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) } diff --git a/R/utils.R b/R/utils.R index bea7369..2004a12 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/dicoAdequacySettings.Rd b/man/dicoAdequacySettings.Rd new file mode 100644 index 0000000..f1b48fc --- /dev/null +++ b/man/dicoAdequacySettings.Rd @@ -0,0 +1,17 @@ +% 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. +} diff --git a/man/update_generaldata_by_section.Rd b/man/update_generaldata_by_section.Rd new file mode 100644 index 0000000..85ea2b8 --- /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 +}