Antares Simulator
Power System Simulator
Loading...
Searching...
No Matches
STSbyGroup.h
1/*
2** Copyright 2007-2024, RTE (https://www.rte-france.com)
3** See AUTHORS.txt
4** SPDX-License-Identifier: MPL-2.0
5** This file is part of Antares-Simulator,
6** Adequacy and Performance assessment for interconnected energy networks.
7**
8** Antares_Simulator is free software: you can redistribute it and/or modify
9** it under the terms of the Mozilla Public Licence 2.0 as published by
10** the Mozilla Foundation, either version 2 of the License, or
11** (at your option) any later version.
12**
13** Antares_Simulator is distributed in the hope that it will be useful,
14** but WITHOUT ANY WARRANTY; without even the implied warranty of
15** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16** Mozilla Public Licence 2.0 for more details.
17**
18** You should have received a copy of the Mozilla Public Licence 2.0
19** along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
20*/
21#pragma once
22
23#include "antares/solver/variable/variable.h"
24
25namespace
26{
27inline std::vector<std::string> sortedUniqueGroups(
28 const std::vector<Antares::Data::ShortTermStorage::STStorageCluster>& storages)
29{
30 std::set<std::string> names;
31 for (const auto& cluster: storages)
32 {
33 names.insert(cluster.properties.groupName);
34 }
35 return {names.begin(), names.end()};
36}
37
38inline std::map<std::string, unsigned int> giveNumbersToGroups(
39 const std::vector<std::string>& groupNames)
40{
41 unsigned int groupNumber{0};
42 std::map<std::string, unsigned int> groupToNumbers;
43 for (const auto& name: groupNames)
44 {
45 groupToNumbers[name] = groupNumber++;
46 }
47 return groupToNumbers;
48}
49} // namespace
50
51namespace Antares::Solver::Variable::Economy
52{
54{
56 static std::string Caption()
57 {
58 return "STS by group";
59 }
60
62 static std::string Unit()
63 {
64 return "MWh";
65 }
66
68 static std::string Description()
69 {
70 return "STS injections, withdrawals and levels";
71 }
72
74 typedef Results<R::AllYears::Average< // The average values throughout all years
75 R::AllYears::StdDeviation< // The standard deviation values throughout all years
76 R::AllYears::Min< // The minimum values throughout all years
77 R::AllYears::Max< // The maximum values throughout all years
78 >>>>>
80
83
85 static constexpr uint8_t categoryDataLevel = Category::DataLevel::area;
87 static constexpr uint8_t categoryFileLevel = ResultsType::categoryFile
88 & (Category::FileLevel::id
89 | Category::FileLevel::va);
91 static constexpr uint8_t precision = Category::all;
93 static constexpr uint8_t nodeDepthForGUI = +0;
95 static constexpr uint8_t decimal = 0;
96 // Nb of columns occupied by this variable in year-by-year results
97 static constexpr int columnCount = Category::dynamicColumns;
99 static constexpr uint8_t spatialAggregate = Category::spatialAggregateSum;
100 static constexpr uint8_t spatialAggregateMode = Category::spatialAggregateEachYear;
101 static constexpr uint8_t spatialAggregatePostProcessing = 0;
103 static constexpr uint8_t hasIntermediateValues = 1;
105 static constexpr uint8_t isPossiblyNonApplicable = 0;
106
108 typedef std::vector<IntermediateValues> IntermediateValuesBaseType;
109 typedef std::vector<IntermediateValuesBaseType> IntermediateValuesType;
110
111}; // class VCard
112
116template<class NextT = Container::EndOfList>
117class STSbyGroup: public Variable::IVariable<STSbyGroup<NextT>, NextT, VCardSTSbyGroup>
118{
119private:
120 enum VariableType
121 {
122 injection = 0,
123 withdrawal = 1,
124 level = 2
125 };
126
127public:
129 typedef NextT NextType;
134
137
139
140 enum
141 {
143 count = 1 + NextT::count,
144 };
145
146 template<int CDataLevel, int CFile>
148 {
149 enum
150 {
151 count = ((VCardType::categoryDataLevel & CDataLevel
153 ? (NextType::template Statistics<CDataLevel, CFile>::count
154 + VCardType::columnCount * ResultsType::count)
155 : NextType::template Statistics<CDataLevel, CFile>::count),
156 };
157 };
158
159public:
160 void initializeFromArea(Data::Study* study, Data::Area* area)
161 {
162 // Get the number of years in parallel
163 pNbYearsParallel = study->maxNbYearsInParallel;
164 pValuesForTheCurrentYear.resize(pNbYearsParallel);
165
166 // Building the vector of group names the clusters belong to.
167 groupNames_ = sortedUniqueGroups(area->shortTermStorage.storagesByIndex);
168 groupToNumbers_ = giveNumbersToGroups(groupNames_);
169
170 nbColumns_ = groupNames_.size() * NB_COLS_PER_GROUP;
171
172 if (nbColumns_)
173 {
174 AncestorType::pResults.resize(nbColumns_);
175
176 for (unsigned int numSpace = 0; numSpace < pNbYearsParallel; numSpace++)
177 {
178 pValuesForTheCurrentYear[numSpace].resize(nbColumns_);
179 }
180
181 for (unsigned int numSpace = 0; numSpace < pNbYearsParallel; numSpace++)
182 {
183 for (unsigned int i = 0; i != nbColumns_; ++i)
184 {
185 pValuesForTheCurrentYear[numSpace][i].initializeFromStudy(*study);
186 }
187 }
188
189 for (unsigned int i = 0; i != nbColumns_; ++i)
190 {
191 AncestorType::pResults[i].initializeFromStudy(*study);
192 AncestorType::pResults[i].reset();
193 }
194 }
195 else
196 {
198 }
199 // Next
200 NextType::initializeFromArea(study, area);
201 }
202
203 size_t getMaxNumberColumns() const
204 {
205 return nbColumns_ * ResultsType::count;
206 }
207
208 void yearBegin(unsigned int year, unsigned int numSpace)
209 {
210 // Reset the values for the current year
211 for (unsigned int i = 0; i != nbColumns_; ++i)
212 {
213 pValuesForTheCurrentYear[numSpace][i].reset();
214 }
215 // Next variable
216 NextType::yearBegin(year, numSpace);
217 }
218
219 void yearEnd(unsigned int year, unsigned int numSpace)
220 {
221 // Here we perform time-aggregations :
222 // ---------------------------------
223 // For a given MC year, from hourly results we compute daily, weekly, monthly and annual
224 // results by aggregation operations (averages or sums).
225 // Caution :
226 // - level results are stored in columns of which indices satisfy : col_index % 3 == 2.
227 // They are time-aggregated by means of averages
228 // - injection and withdrawal results are stored in columns of which indices
229 // satisfy : col_index % 3 != 2.
230 // They are time-aggregated by means of sums.
231
232 for (unsigned int column = 0; column < nbColumns_; column++)
233 {
234 switch (column % NB_COLS_PER_GROUP)
235 {
236 case VariableType::level:
237 pValuesForTheCurrentYear[numSpace][column]
238 .computeAveragesForCurrentYearFromHourlyResults();
239 break;
240 case VariableType::injection:
241 case VariableType::withdrawal:
242 pValuesForTheCurrentYear[numSpace][column].computeStatisticsForTheCurrentYear();
243 break;
244 }
245 }
246 // Next variable
247 NextType::yearEnd(year, numSpace);
248 }
249
250 void computeSummary(std::map<unsigned int, unsigned int>& numSpaceToYear,
251 unsigned int nbYearsForCurrentSummary)
252 {
253 // Here we compute synthesis :
254 // for each interval of any time period results (hourly, daily, weekly, ...),
255 // we compute the average over all MC years :
256 // For instance :
257 // - we compute the average of the results of the first hour over all MC years
258 // - or we compute the average of the results of the n-th day over all MC years
259 for (unsigned int numSpace = 0; numSpace < nbYearsForCurrentSummary; ++numSpace)
260 {
261 VariableAccessorType::ComputeSummary(pValuesForTheCurrentYear[numSpace],
263 numSpaceToYear[numSpace]);
264 }
265
266 // Next variable
267 NextType::computeSummary(numSpaceToYear, nbYearsForCurrentSummary);
268 }
269
270 void hourBegin(unsigned int hourInTheYear)
271 {
272 NextType::hourBegin(hourInTheYear);
273 }
274
275 void hourForEachArea(State& state, unsigned int numSpace)
276 {
277 using namespace Antares::Data::ShortTermStorage;
278 const auto& shortTermStorage = state.area->shortTermStorage;
279
280 uint clusterIndex = 0;
281 for (const auto& cluster: shortTermStorage.storagesByIndex)
282 {
283 unsigned int groupNumber = groupToNumbers_[cluster.properties.groupName];
284 const auto& result = state.hourlyResults->ShortTermStorage[state.hourInTheWeek];
285 // Injection
286 pValuesForTheCurrentYear[numSpace][NB_COLS_PER_GROUP * groupNumber
287 + VariableType::injection][state.hourInTheYear]
288 += result.injection[clusterIndex];
289
290 // Withdrawal
291 pValuesForTheCurrentYear[numSpace][NB_COLS_PER_GROUP * groupNumber
292 + VariableType::withdrawal][state.hourInTheYear]
293 += result.withdrawal[clusterIndex];
294
295 // Levels
296 pValuesForTheCurrentYear[numSpace][NB_COLS_PER_GROUP * groupNumber
297 + VariableType::level][state.hourInTheYear]
298 += result.level[clusterIndex];
299
300 clusterIndex++;
301 }
302
303 // Next variable
304 NextType::hourForEachArea(state, numSpace);
305 }
306
307 inline void buildDigest(SurveyResults& results, int digestLevel, int dataLevel) const
308 {
309 // Ask to build the digest to the next variable
310 NextType::buildDigest(results, digestLevel, dataLevel);
311 }
312
313 Antares::Memory::Stored<double>::ConstReturnType retrieveRawHourlyValuesForCurrentYear(
314 unsigned int column,
315 unsigned int numSpace) const
316 {
317 return pValuesForTheCurrentYear[numSpace][column].hour;
318 }
319
320 std::string caption(unsigned int column) const
321 {
322 static const std::vector<std::string> VAR_POSSIBLE_KINDS = {"INJECTION",
323 "WITHDRAWAL",
324 "LEVEL"};
325 const std::string& groupName = groupNames_[column / NB_COLS_PER_GROUP];
326 const std::string& variableKind = VAR_POSSIBLE_KINDS[column % NB_COLS_PER_GROUP];
327 return groupName + "_" + variableKind;
328 }
329
330 std::string unit(unsigned int column) const
331 {
332 switch (column % NB_COLS_PER_GROUP)
333 {
334 case VariableType::level:
335 return "MWh";
336 case VariableType::injection:
337 case VariableType::withdrawal:
338 return "MW";
339 default:
340 return "error";
341 }
342 }
343
344 void localBuildAnnualSurveyReport(SurveyResults& results,
345 int fileLevel,
346 int precision,
347 unsigned int numSpace) const
348 {
349 // Initializing external pointer on current variable non applicable status
350 results.isCurrentVarNA = AncestorType::isNonApplicable;
351
352 if (!AncestorType::isPrinted[0])
353 {
354 return;
355 }
356
357 for (unsigned int column = 0; column < nbColumns_; column++)
358 {
359 results.variableCaption = caption(column);
360 results.variableUnit = unit(column);
361 pValuesForTheCurrentYear[numSpace][column]
362 .template buildAnnualSurveyReport<VCardType>(results, fileLevel, precision);
363 }
364 }
365
366 void buildSurveyReport(SurveyResults& results,
367 int dataLevel,
368 int fileLevel,
369 int precision) const
370 {
371 // Building synthesis results
372 // ------------------------------
373
374 if (AncestorType::isPrinted[0])
375 {
376 // And only if we match the current data level _and_ precision level
377 if ((dataLevel & VCardType::categoryDataLevel)
378 && (fileLevel & VCardType::categoryFileLevel) && (precision & VCardType::precision))
379 {
380 results.isCurrentVarNA[0] = AncestorType::isNonApplicable[0];
381
382 for (unsigned int column = 0; column < nbColumns_; column++)
383 {
384 results.variableCaption = caption(column);
385 results.variableUnit = unit(column);
387 .template buildSurveyReport<ResultsType, VCardType>(
388 results,
390 dataLevel,
391 fileLevel,
392 precision);
393 }
394 }
395 }
396 // Ask to the next item in the static list to export its results as well
397 NextType::buildSurveyReport(results, dataLevel, fileLevel, precision);
398 }
399
400private:
402 typename VCardType::IntermediateValuesType pValuesForTheCurrentYear;
403 size_t nbColumns_ = 0;
404 std::vector<std::string> groupNames_; // Names of group containing the clusters of the area
405 std::map<std::string, unsigned int> groupToNumbers_; // Gives to each group (of area) a number
406 const int NB_COLS_PER_GROUP = 3; // Injection + withdrawal + levels = 3 variables
407 unsigned int pNbYearsParallel;
408
409}; // class STSbyGroup
410
411} // End namespace Antares::Solver::Variable::Economy
Definition for a single area.
Definition area.h:52
Definition study.h:61
Variables related to short term storage groups.
Definition STSbyGroup.h:118
@ count
How many items have we got.
Definition STSbyGroup.h:143
VCardSTSbyGroup VCardType
VCard.
Definition STSbyGroup.h:131
VCardType::ResultsType ResultsType
List of expected results.
Definition STSbyGroup.h:136
Variable::IVariable< STSbyGroup< NextT >, NextT, VCardType > AncestorType
Ancestor.
Definition STSbyGroup.h:133
NextT NextType
Type of the next static variable.
Definition STSbyGroup.h:129
Interface for any variable.
Definition variable.h:51
StoredResultType pResults
All the results about this variable.
Definition variable.h:327
Temporary buffer for allocating results for a single year.
Definition intermediate.h:46
Definition results.h:48
@ count
The count if item in the list.
Definition results.h:56
Definition cbuilder.h:120
static constexpr uint8_t spatialAggregate
The Spatial aggregation.
Definition STSbyGroup.h:99
static constexpr uint8_t decimal
Decimal precision.
Definition STSbyGroup.h:95
static constexpr uint8_t nodeDepthForGUI
Indentation (GUI)
Definition STSbyGroup.h:93
static constexpr uint8_t categoryFileLevel
File level (provided by the type of the results)
Definition STSbyGroup.h:87
VCardSTSbyGroup VCardForSpatialAggregate
The VCard to look for for calculating spatial aggregates.
Definition STSbyGroup.h:82
static constexpr uint8_t precision
Precision (views)
Definition STSbyGroup.h:91
Results< R::AllYears::Average< R::AllYears::StdDeviation< R::AllYears::Min< R::AllYears::Max< > > > > > ResultsType
The synhesis results.
Definition STSbyGroup.h:79
static constexpr uint8_t categoryDataLevel
Data Level.
Definition STSbyGroup.h:85
static constexpr uint8_t hasIntermediateValues
Intermediate values.
Definition STSbyGroup.h:103
static std::string Unit()
Unit.
Definition STSbyGroup.h:62
static std::string Description()
The short description of the variable.
Definition STSbyGroup.h:68
static std::string Caption()
Caption.
Definition STSbyGroup.h:56
static constexpr uint8_t isPossiblyNonApplicable
Can this variable be non applicable (0 : no, 1 : yes)
Definition STSbyGroup.h:105