Skip to content

Commit bd65f4e

Browse files
AssilMaMANSOURI Assil Ext
andauthored
New functions for thermal (#193)
* new function readInputThermal() * added support + warning for same clusters in multiple areas * added new fct : readAntaresClusters() * addedv case insensitivity for readAntaresClusters() Co-authored-by: MANSOURI Assil Ext <[email protected]>
1 parent eeb227a commit bd65f4e

File tree

9 files changed

+302
-1
lines changed

9 files changed

+302
-1
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: antaresRead
22
Type: Package
33
Title: Import, Manipulate and Explore the Results of an 'Antares' Simulation
4-
Version: 2.4.0
4+
Version: 2.4.1
55
Authors@R: c(
66
person("Veronique", "Bachelier", email = "[email protected]", role = c("aut", "cre")),
77
person("Jalal-Edine", "ZAWAM", role = "aut"),

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ export(parAggregateMCall)
4545
export(ponderateMcAggregation)
4646
export(readAntares)
4747
export(readAntaresAreas)
48+
export(readAntaresClusters)
4849
export(readBindingConstraints)
4950
export(readClusterDesc)
5051
export(readClusterResDesc)
5152
export(readIni)
5253
export(readIniAPI)
5354
export(readIniFile)
5455
export(readInputTS)
56+
export(readInputThermal)
5557
export(readLayout)
5658
export(readOptimCriteria)
5759
export(removeVirtualAreas)

NEWS.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
> Copyright © 2016 RTE Réseau de transport d’électricité
22
3+
# antaresRead 2.4.1
4+
5+
NEW FEATURES:
6+
7+
Added new functions `readInputThermal()` and `readAntaresClusters()` :
8+
Both functions take a vector of **clusters** instead of **areas**
9+
* `readInputThermal()` : read thermal TS (availabilities) and modulation in Input mode
10+
* `readAntaresClusters()` : read output data for clusters only with thematic trimming
11+
12+
13+
314
# antaresRead 2.4.0
415

516
NEW FEATURES:
17+
618
Major upgrade to `aggregateResult()` and `parAggregateMCall()` :
719
* Faster & memory efficient
820
* Support for Antares studies up to v8.3 (v8.4 experimental)
@@ -15,9 +27,11 @@ Major upgrade to `aggregateResult()` and `parAggregateMCall()` :
1527
# antaresRead 2.3.2
1628

1729
NEW FEATURES:
30+
1831
added "profit by cluster" when reading cluster data
1932

2033
BUGFIXES:
34+
2135
Fix for 404 error when some output is missing in API mode(#188)
2236

2337

R/readAntaresClusters.R

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#Copyright © 2016 RTE Réseau de transport d’électricité
2+
#' Read output for a list of clusters
3+
#'
4+
#' @param clusters vector of thermal clusters to be imported
5+
#' @param selected vector of thematic trimming
6+
#' @inheritParams readAntares
7+
#'
8+
#' @return data.table of results for thermal clusters
9+
#'
10+
#' @export
11+
readAntaresClusters <- function(clusters, selected = c("production", "NP Cost", "NODU", "profit"),
12+
timeStep = c("hourly", "daily", "weekly", "monthly", "annual"),
13+
opts = simOptions(), parallel = FALSE, showProgress = TRUE) {
14+
15+
if (missing(clusters))
16+
stop("The function 'readAntaresClusters' expects a vector of cluster names as argument.")
17+
if ("Input" %in% opts$mode)
18+
stop("Cannot use 'readAntaresClusters' in 'Input' mode.")
19+
20+
##Add check control for all
21+
allClusters <- readClusterDesc()[, c("area","cluster")]
22+
ind_cluster <- which(tolower(allClusters$cluster) %in% .checkArg(tolower(clusters),
23+
tolower(unique(allClusters$cluster)),
24+
"clusters %s do not exist in the simulation."))
25+
clusters <- allClusters$cluster[ind_cluster]
26+
27+
ind_cluster <- which(tolower(allClusters$cluster) %in% .checkArg(tolower(clusters),
28+
tolower(unique(allClusters[area %in% opts$areasWithClusters]$cluster)),
29+
"clusters %s have no output."))
30+
clusters <- unique(allClusters$cluster[ind_cluster])
31+
32+
areas <- unique(allClusters[cluster %in% clusters]$area)
33+
34+
res <- readAntares(clusters = areas, timeStep = timeStep, opts = opts,
35+
parallel = parallel, showProgress = showProgress)
36+
37+
subset(res, cluster %in% clusters, select = c(setdiff(colnames(res),c("production", "NP Cost", "NODU", "profit")),
38+
intersect(colnames(res),selected))) #support for up to v8.4
39+
}

R/readInputThermal.R

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#Copyright © 2016 RTE Réseau de transport d’électricité
2+
3+
#' Read Input thermal time series
4+
#'
5+
#' @description
6+
#' \code{readInputThermal} is a function that reads thermal time series from an antares
7+
#' project. But contrary to \code{\link{readAntares}}, it only reads time series
8+
#' stored in the input folder, so it can work in "input" mode.
9+
#'
10+
#' @param clusters vector of clusters names for which thermal time series must be read.
11+
#' @param thermalModulation if TRUE, return thermalModulation data
12+
#' @inheritParams readAntares
13+
#'
14+
#' @return
15+
#' If thermalModulation is TRUE, an object of class "antaresDataList" is returned. It is a list of
16+
#' data.tables for thermalAvailabilities and thermalModulation
17+
#'
18+
#' Else the result is a data.table with class "antaresDataTable".
19+
#'
20+
#' @note
21+
#' the clusters parameter can also accept the special value "all".
22+
#' It indicates the function to read the desired time series for all clusters.
23+
#'
24+
#' @seealso
25+
#' \code{\link{setSimulationPath}}, \code{\link{readAntares}},
26+
#' \code{\link{getAreas}}, \code{\link{getLinks}}
27+
#'
28+
#' @export
29+
readInputThermal <- function(clusters = NULL, thermalModulation = FALSE,
30+
opts = simOptions(),
31+
timeStep = c("hourly", "daily", "weekly", "monthly", "annual"),
32+
simplify = TRUE, parallel = FALSE,
33+
showProgress = TRUE) {
34+
35+
timeStep <- match.arg(timeStep)
36+
37+
# Can the importation be parallelized ?
38+
if (parallel) {
39+
if(!requireNamespace("foreach")) stop("Parallelized importation impossible. Please install the 'foreach' package and a parallel backend provider like 'doParallel'.")
40+
if (!foreach::getDoParRegistered()) stop("Parallelized importation impossible. Please register a parallel backend, for instance with function 'registerDoParallel'")
41+
}
42+
43+
allAreasClusters <- readClusterDesc()[area %in% opts$areasWithClusters, c("area", "cluster")]
44+
allClusters <- unique(allAreasClusters$cluster)
45+
# Manage special value "all"
46+
if(identical(clusters, "all")) clusters <- allClusters
47+
48+
if (length(setdiff(tolower(clusters), tolower(allClusters))) > 0){
49+
cat(c("the following clusters are not available : ",setdiff(tolower(clusters), tolower(allClusters))))
50+
stop("Some clusters are not available in the areas specified")
51+
}
52+
53+
ind_cluster <- which(tolower(allClusters) %in% tolower(clusters))
54+
clusters <- unique(allClusters[ind_cluster])
55+
res <- list() # Object the function will return
56+
57+
thermalTS <- as.data.table(ldply(clusters, function(cl) {
58+
59+
area <- unique(allAreasClusters[cluster == cl]$area)
60+
if (length(area) > 1) warning(cl," is in more than one area")
61+
resCl <- ldply(area, function(x){
62+
filePattern <- sprintf("%s/%s/%%s/series.txt", "thermal/series", x)
63+
mid <- .importInputTS(cl, timeStep, opts, filePattern, "ThermalAvailabilities",
64+
inputTimeStep = "hourly", type = "matrix")
65+
if (is.null(mid)) return (data.table())
66+
mid$area <- x
67+
mid$cluster <- cl
68+
mid
69+
})
70+
71+
resCl <- dcast(as.data.table(resCl), area + cluster + timeId ~ tsId, value.var = "ThermalAvailabilities")
72+
}))
73+
74+
tsCols <- setdiff(colnames(thermalTS), c("area", "cluster", "timeId"))
75+
setnames(thermalTS, tsCols, paste0("ts",tsCols))
76+
setcolorder(thermalTS, c("area", "cluster", "timeId", setdiff(names(thermalTS), c("area", "cluster", "timeId"))))
77+
78+
if (nrow(thermalTS) > 0) res$thermalAvailabilities <- thermalTS
79+
80+
# thermalModulation processing
81+
if (thermalModulation){
82+
areas <- unique(allAreasClusters[cluster %in% clusters]$area)
83+
thermalMod <- as.data.table(ldply(areas, .importThermalModulation, opts = opts, timeStep = timeStep))
84+
thermalMod <- thermalMod[cluster %in% clusters]
85+
setcolorder(thermalMod, c("area", "cluster", "timeId", setdiff(names(thermalMod), c("area", "cluster", "timeId"))))
86+
87+
if (nrow(thermalMod) > 0) res$thermalModulation <- thermalMod
88+
}
89+
90+
if (length(res) == 0) stop("At least one argument of readInputTS has to be defined.")
91+
92+
# Class and attributes
93+
res <- .addClassAndAttributes(res, NULL, timeStep, opts, simplify)
94+
addDateTimeColumns(res)
95+
}

man/readAntaresClusters.Rd

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/readInputThermal.Rd

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#Copyright © 2016 RTE Réseau de transport d’électricité
2+
3+
context("Function readAntaresClusters")
4+
5+
sapply(studyPathS, function(studyPath){
6+
7+
opts <- setSimulationPath(studyPath)
8+
9+
test_that("Clusters importation is ok", {
10+
clusters <- readAntaresClusters(clusters = "peak_must_run_partial", showProgress = FALSE)
11+
expect_is(clusters, "data.table")
12+
expect_true(!is.null(clusters$cluster))
13+
expect_equal(nrow(clusters), 24 * 7 * nweeks)
14+
})
15+
16+
17+
# Test that importation works for all time resolutions.
18+
for (timeStep in c("hourly", "daily", "weekly", "monthly", "annual")) {
19+
expected_rows = switch(timeStep,
20+
hourly = 24 * 7 * nweeks,
21+
daily = 7 * nweeks,
22+
weekly = nweeks,
23+
monthly = nmonths,
24+
annual = 1)
25+
26+
test_that(sprintf("one can import cluster %s output", timeStep), {
27+
clusters <- readAntaresClusters(clusters = "peak_must_run_partial", showProgress = FALSE, timeStep = timeStep)
28+
expect_equal(nrow(clusters), expected_rows)
29+
})
30+
}
31+
32+
})
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#Copyright © 2016 RTE Réseau de transport d’électricité
2+
3+
context("Function readInputThermal")
4+
sapply(studyPathS, function(studyPath){
5+
6+
opts <- setSimulationPath(studyPath)
7+
8+
9+
if(!isH5Opts(opts)){
10+
11+
test_that("Thermal availabilities importation works", {
12+
input <- readInputThermal(clusters = "peak_must_run_partial", showProgress = FALSE)
13+
expect_is(input, "antaresDataTable")
14+
expect_gt(nrow(input), 0)
15+
expect_equal(nrow(input) %% (24 * 7 * nweeks), 0)
16+
})
17+
18+
test_that("Thermal modulation importation works", {
19+
input <- readInputThermal(clusters = "peak_must_run_partial", thermalModulation = TRUE, showProgress = FALSE)
20+
expect_is(input, "antaresDataList")
21+
expect_is(input$thermalModulation, "antaresDataTable")
22+
expect_gt(nrow(input$thermalModulation), 0)
23+
expect_equal(nrow(input$thermalModulation) %% (24 * 7 * nweeks), 0)
24+
})
25+
26+
}
27+
})

0 commit comments

Comments
 (0)