Antares Simulator
Power System Simulator
average.h
1 /*
2  * Copyright 2007-2025, 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 #ifndef __SOLVER_VARIABLE_STORAGE_AVERAGE_H__
22 #define __SOLVER_VARIABLE_STORAGE_AVERAGE_H__
23 
24 #include <vector>
25 
26 #include "averagedata.h"
27 
28 namespace Antares::Solver::Variable::R::AllYears
29 {
30 template<class NextT = Empty, int FileFilter = Variable::Category::FileLevel::allFile>
31 struct Average: public NextT
32 {
33 public:
35  typedef NextT NextType;
36 
37  enum
38  {
40  count = 1 + NextT::count,
41 
42  categoryFile = NextT::categoryFile | Variable::Category::FileLevel::allFile,
43  };
44 
45  struct Data
46  {
47  double value;
48  uint32_t indice;
49  };
50 
52  static const char* Name()
53  {
54  return "average";
55  }
56 
57 public:
58  Average()
59  {
60  }
61 
62 protected:
63  void initializeFromStudy(Antares::Data::Study& study)
64  {
65  avgdata.initializeFromStudy(study);
66  // Next
67  NextType::initializeFromStudy(study);
68  }
69 
70  void reset()
71  {
72  // Reset
73  avgdata.reset();
74  // Next
75  NextType::reset();
76  }
77 
78  void merge(uint year, const IntermediateValues& rhs)
79  {
80  avgdata.merge(year, rhs);
81  // Next
82  NextType::merge(year, rhs);
83  }
84 
85  template<class S, class VCardT>
86  void buildSurveyReport(SurveyResults& report,
87  const S& results,
88  int dataLevel,
89  int fileLevel,
90  int precision) const
91  {
92  if (!(fileLevel & Category::FileLevel::id))
93  {
94  switch (precision)
95  {
96  case Category::hourly:
97  InternalExportValues<HOURS_PER_YEAR, VCardT, Category::hourly>(report,
98  avgdata.hourly);
99  break;
100  case Category::daily:
101  InternalExportValues<DAYS_PER_YEAR, VCardT, Category::daily>(report, avgdata.daily);
102  break;
103  case Category::weekly:
104  InternalExportValues<WEEKS_PER_YEAR, VCardT, Category::weekly>(report,
105  avgdata.weekly);
106  break;
107  case Category::monthly:
108  InternalExportValues<MONTHS_PER_YEAR, VCardT, Category::monthly>(report,
109  avgdata.monthly);
110  break;
111  case Category::annual:
112  InternalExportValues<1, VCardT, Category::annual>(report, avgdata.year);
113  break;
114  }
115  }
116  // Next
117  NextType::template buildSurveyReport<S, VCardT>(report,
118  results,
119  dataLevel,
120  fileLevel,
121  precision);
122  }
123 
124  template<class VCardT>
125  void buildDigest(SurveyResults& report, int digestLevel, int dataLevel) const
126  {
127  const bool isCluster = (VCardT::categoryFileLevel & Category::FileLevel::de)
128  || (VCardT::categoryFileLevel & Category::FileLevel::de_res);
129  const bool isBindingConstraint = VCardT::categoryFileLevel & Category::FileLevel::bc;
130  const bool isDigest = digestLevel & Category::digestAllYears;
131  if ((dataLevel & Category::DataLevel::area || dataLevel & Category::DataLevel::setOfAreas)
132  && isDigest && !isCluster && !isBindingConstraint)
133  {
134  assert(report.data.columnIndex < report.maxVariables && "Column index out of bounds");
135 
136  report.captions[0][report.data.columnIndex] = report.variableCaption;
137  report.captions[1][report.data.columnIndex] = report.variableUnit;
138  report.captions[2][report.data.columnIndex] = (report.variableCaption == "LOLP")
139  ? "values"
140  : "EXP";
141 
142  // Precision
143  report.precision[report.data.columnIndex] = PrecisionToPrintfFormat<
144  VCardT::decimal>::Value();
145  // Value
146  report.values[report.data.columnIndex][report.data.rowIndex] = avgdata.allYears;
147  // Non applicability
148  report.digestNonApplicableStatus[report.data.rowIndex][report.data.columnIndex]
149  = *report.isCurrentVarNA;
150 
151  ++(report.data.columnIndex);
152  }
153  // Next
154  NextType::template buildDigest<VCardT>(report, digestLevel, dataLevel);
155  }
156 
157 public:
158  AverageData avgdata;
159 
160 private:
161  template<uint Size, class VCardT, int PrecisionT>
162  void InternalExportValues(SurveyResults& report, const std::vector<HighPrecision>& array) const
163  {
164  assert(report.data.columnIndex < report.maxVariables && "Column index out of bounds");
165 
166  // Caption
167  report.captions[0][report.data.columnIndex] = report.variableCaption;
168  report.captions[1][report.data.columnIndex] = report.variableUnit;
169  report.captions[2][report.data.columnIndex] = (report.variableCaption == "LOLP") ? "values"
170  : "EXP";
171  // Precision
172  report.precision[report.data.columnIndex] = PrecisionToPrintfFormat<
173  VCardT::decimal>::Value();
174  // Non applicability
175  report.nonApplicableStatus[report.data.columnIndex] = *report.isCurrentVarNA;
176 
177  // Values
178  switch (PrecisionT)
179  {
180  case Category::hourly:
181  {
182  for (uint h = 0; h < HOURS_PER_YEAR; ++h)
183  {
184  report.values[report.data.columnIndex][h] = array[h];
185  }
186  break;
187  }
188  case Category::daily:
189  {
190  for (uint d = 0; d < DAYS_PER_YEAR; ++d)
191  {
192  report.values[report.data.columnIndex][d] = array[d];
193  }
194  break;
195  }
196 
197  case Category::weekly:
198  {
199  for (uint w = 0; w < WEEKS_PER_YEAR; ++w)
200  {
201  report.values[report.data.columnIndex][w] = array[w];
202  }
203  break;
204  }
205 
206  case Category::monthly:
207  {
208  for (uint m = 0; m < MONTHS_PER_YEAR; ++m)
209  {
210  report.values[report.data.columnIndex][m] = array[m];
211  }
212  break;
213  }
214  case Category::annual:
215  {
216  double& target = *(report.values[report.data.columnIndex]);
217  target = 0;
218  for (uint i = 0; i != avgdata.nbYearsCapacity; ++i)
219  {
220  target += array[i];
221  }
222  avgdata.allYears = target;
223  break;
224  }
225 
226  default:
227  logs.warning() << "Category not found for variable: " << report.variableCaption;
228  break;
229  }
230 
231  // Next column index
232  ++report.data.columnIndex;
233  }
234 
235 }; // class Average
236 
237 } // namespace Antares::Solver::Variable::R::AllYears
238 
239 #endif // __SOLVER_VARIABLE_STORAGE_AVERAGE_H__
Definition: study.h:57
@ count
The count if item in the list.
Definition: average.h:40
NextT NextType
Type of the net item in the list.
Definition: average.h:35
static const char * Name()
Name of the filter.
Definition: average.h:52