Antares Simulator
Power System Simulator
congestionProbability.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_ECONOMY_CongestionProbability_H__
22 #define __SOLVER_VARIABLE_ECONOMY_CongestionProbability_H__
23 
24 #include <antares/study/area/constants.h>
25 
26 #include "../../variable.h"
27 
28 namespace Antares::Solver::Variable::Economy
29 {
31 {
33  static std::string Caption()
34  {
35  return "CONG. PROB. (+/-)";
36  }
37 
39  static std::string Unit()
40  {
41  return "%";
42  }
43 
45  static std::string Description()
46  {
47  return "Probability for the line to be congested in the upstream-downstream way";
48  }
49 
51  typedef Results<R::AllYears::Raw< // The raw values throughout all years
52  >>
54 
56  static constexpr uint8_t categoryDataLevel = Category::DataLevel::link;
58  static constexpr uint8_t categoryFileLevel = ResultsType::categoryFile
59  & (Category::FileLevel::va);
61  static constexpr uint8_t precision = Category::all;
63  static constexpr uint8_t nodeDepthForGUI = +0;
65  static constexpr uint8_t decimal = 2;
67  static constexpr int columnCount = 2;
69  static constexpr uint8_t spatialAggregate = Category::spatialAggregateSum;
70  static constexpr uint8_t spatialAggregateMode = Category::spatialAggregateEachYear;
71  static constexpr uint8_t spatialAggregatePostProcessing = 0;
73  static constexpr uint8_t hasIntermediateValues = 1;
75  static constexpr uint8_t isPossiblyNonApplicable = 0;
76 
77  typedef IntermediateValues IntermediateValuesBaseType[columnCount];
78  typedef IntermediateValuesBaseType* IntermediateValuesType;
79 
80  struct Multiple
81  {
82  static std::string Caption(uint indx)
83  {
84  switch (indx)
85  {
86  case 0:
87  return "CONG. PROB +";
88  case 1:
89  return "CONG. PROB -";
90  default:
91  return "<unknown>";
92  }
93  }
94 
95  static std::string Unit([[maybe_unused]] const unsigned int indx)
96  {
98  }
99  };
100 
101 }; // class VCard
102 
106 template<class NextT = Container::EndOfList>
108  : public Variable::IVariable<CongestionProbability<NextT>, NextT, VCardCongestionProbability>
109 {
110 public:
112  typedef NextT NextType;
117 
120 
122 
123  enum
124  {
126  count = 1 + NextT::count,
127  };
128 
129  template<int CDataLevel, int CFile>
130  struct Statistics
131  {
132  enum
133  {
134  count = ((VCardType::categoryDataLevel & CDataLevel
135  && VCardType::categoryFileLevel & CFile)
136  ? (NextType::template Statistics<CDataLevel, CFile>::count
138  : NextType::template Statistics<CDataLevel, CFile>::count),
139  };
140  };
141 
142 public:
144  {
145  delete[] pValuesForTheCurrentYear;
146  delete[] pValuesForYearLocalReport;
147  }
148 
149  void initializeFromStudy(Data::Study& study)
150  {
151  pNbYearsParallel = study.maxNbYearsInParallel;
152 
153  yearsWeight = study.parameters.getYearsWeight();
154  yearsWeightSum = study.parameters.getYearsWeightSum();
155 
156  // Average on all years
157  for (uint i = 0; i != VCardType::columnCount; ++i)
158  {
159  AncestorType::pResults[i].initializeFromStudy(study);
160  AncestorType::pResults[i].reset();
161  }
162 
163  pValuesForTheCurrentYear = new VCardType::IntermediateValuesBaseType[pNbYearsParallel];
164  for (unsigned int numSpace = 0; numSpace < pNbYearsParallel; ++numSpace)
165  {
166  for (unsigned int i = 0; i != VCardType::columnCount; ++i)
167  {
168  pValuesForTheCurrentYear[numSpace][i].initializeFromStudy(study);
169  }
170  }
171 
172  pValuesForYearLocalReport = new VCardType::IntermediateValuesBaseType[pNbYearsParallel];
173  for (unsigned int numSpace = 0; numSpace < pNbYearsParallel; ++numSpace)
174  {
175  for (unsigned int i = 0; i != VCardType::columnCount; ++i)
176  {
177  pValuesForYearLocalReport[numSpace][i].initializeFromStudy(study);
178  }
179  }
180 
181  // Next
182  NextType::initializeFromStudy(study);
183  }
184 
185  void initializeFromArea(Data::Study* study, Data::Area* area)
186  {
187  // Next
188  NextType::initializeFromArea(study, area);
189  }
190 
191  void initializeFromAreaLink(Data::Study* study, Data::AreaLink* link)
192  {
193  // Next
194  NextType::initializeFromAreaLink(study, link);
195  }
196 
197  void simulationBegin()
198  {
199  for (unsigned int numSpace = 0; numSpace < pNbYearsParallel; ++numSpace)
200  {
201  pValuesForTheCurrentYear[numSpace][0].reset();
202  pValuesForTheCurrentYear[numSpace][1].reset();
203  }
204 
205  // Next
206  NextType::simulationBegin();
207  }
208 
209  void simulationEnd()
210  {
211  // Next variable
212  NextType::simulationEnd();
213  }
214 
215  void yearBegin(uint year, unsigned int numSpace)
216  {
217  // Reset
218  pValuesForTheCurrentYear[numSpace][0].reset();
219  pValuesForTheCurrentYear[numSpace][1].reset();
220 
221  pValuesForYearLocalReport[numSpace][0].reset();
222  pValuesForYearLocalReport[numSpace][1].reset();
223 
224  // Next variable
225  NextType::yearBegin(year, numSpace);
226  }
227 
228  void yearEndBuild(State& state, unsigned int year, unsigned int numSpace)
229  {
230  // Next variable
231  NextType::yearEndBuild(state, year, numSpace);
232  }
233 
234  void yearEnd(uint year, uint numSpace)
235  {
236  for (uint i = 0; i != VCardType::columnCount; ++i)
237  {
238  // Compute all statistics for the current year (daily,weekly,monthly)
239  pValuesForTheCurrentYear[numSpace][i].computeProbabilitiesForTheCurrentYear();
240  }
241 
242  // Next variable
243  NextType::yearEnd(year, numSpace);
244  }
245 
246  void computeSummary(unsigned int year, unsigned int numSpace)
247  {
248  // Merge all those values with the global results
249  for (uint i = 0; i != VCardType::columnCount; ++i)
250  {
251  // Merge all those values with the global results
252  AncestorType::pResults[i].merge(year, pValuesForTheCurrentYear[numSpace][i]);
253  }
254 
255  // Next variable
256  NextType::computeSummary(year, numSpace);
257  }
258 
259  void hourBegin(uint hourInTheYear)
260  {
261  // Next variable
262  NextType::hourBegin(hourInTheYear);
263  }
264 
265  void hourForEachArea(State& state, unsigned int numSpace)
266  {
267  // Next variable
268  NextType::hourForEachArea(state, numSpace);
269  }
270 
271  void hourForEachLink(State& state, unsigned int numSpace)
272  {
273  // Ratio take into account MC year weight
274  float ratio = yearsWeight[state.year] / yearsWeightSum;
275 
276  assert(state.link != NULL);
277  const auto& linkDirectCapa = state.link->directCapacities;
278  const auto& linkIndirectCapa = state.link->indirectCapacities;
279  // CONG. PROB +
280  if (state.ntc.ValeurDuFlux[state.link->index]
281  > +linkDirectCapa.getCoefficient(state.year, state.hourInTheYear) - 10e-6)
282  {
283  pValuesForTheCurrentYear[numSpace][0].hour[state.hourInTheYear] += 100.0 * ratio;
284  }
285  // CONG. PROB -
286  if (state.ntc.ValeurDuFlux[state.link->index]
287  < -linkIndirectCapa.getCoefficient(state.year, state.hourInTheYear) + 10e-6)
288  {
289  pValuesForTheCurrentYear[numSpace][1].hour[state.hourInTheYear] += 100.0 * ratio;
290  }
291 
292  // Next item in the list
293  NextType::hourForEachLink(state, numSpace);
294  }
295 
296  void beforeYearByYearExport(uint /*year*/, uint numSpace)
297  {
298  for (uint i = 0; i != VCardType::columnCount; ++i)
299  {
300  for (uint h = 0; h != HOURS_PER_YEAR; ++h)
301  {
302  pValuesForYearLocalReport[numSpace][i].hour[h] = (pValuesForTheCurrentYear[numSpace]
303  [i]
304  .hour[h]
305  > 0.)
306  ? 100.
307  : 0.;
308  }
309 
310  for (uint d = 0; d != DAYS_PER_YEAR; ++d)
311  {
312  pValuesForYearLocalReport[numSpace][i].day[d] = (pValuesForTheCurrentYear[numSpace]
313  [i]
314  .day[d]
315  > 0.)
316  ? 100.
317  : 0.;
318  }
319 
320  for (uint w = 0; w != WEEKS_PER_YEAR; ++w)
321  {
322  pValuesForYearLocalReport[numSpace][i].week[w] = (pValuesForTheCurrentYear[numSpace]
323  [i]
324  .week[w]
325  > 0.)
326  ? 100.
327  : 0.;
328  }
329 
330  for (uint m = 0; m != MONTHS_PER_YEAR; ++m)
331  {
332  pValuesForYearLocalReport[numSpace][i].month[m] = (pValuesForTheCurrentYear
333  [numSpace][i]
334  .month[m]
335  > 0.)
336  ? 100.
337  : 0.;
338  }
339 
340  pValuesForYearLocalReport[numSpace][i].year = (pValuesForTheCurrentYear[numSpace][i]
341  .year
342  > 0.)
343  ? 100.
344  : 0.;
345  }
346  }
347 
348  Antares::Memory::Stored<double>::ConstReturnType retrieveRawHourlyValuesForCurrentYear(
349  uint column,
350  uint numSpace) const
351  {
352  return pValuesForTheCurrentYear[numSpace][column].hour;
353  }
354 
355  void localBuildAnnualSurveyReport(SurveyResults& results,
356  int fileLevel,
357  int precision,
358  uint numSpace) const
359  {
360  // The current variable is actually a multiple-variable.
361  results.isCurrentVarNA = AncestorType::isNonApplicable;
362 
363  for (uint i = 0; i != VCardType::columnCount; ++i)
364  {
365  if (AncestorType::isPrinted[i])
366  {
367  // Write the data for the current year
368  results.variableCaption = VCardType::Multiple::Caption(i);
369  results.variableUnit = VCardType::Multiple::Unit(i);
370  pValuesForYearLocalReport[numSpace][i]
371  .template buildAnnualSurveyReport<VCardType>(results, fileLevel, precision);
372  }
373  results.isCurrentVarNA++;
374  }
375  }
376 
377 private:
378  std::vector<float> yearsWeight;
379  float yearsWeightSum;
381  typename VCardType::IntermediateValuesType pValuesForTheCurrentYear;
382  typename VCardType::IntermediateValuesType pValuesForYearLocalReport;
383  unsigned int pNbYearsParallel;
384 
385 }; // class CongestionProbability
386 
387 } // namespace Antares::Solver::Variable::Economy
388 
389 #endif // __SOLVER_VARIABLE_ECONOMY_CongestionProbability_H__
Definition for a single area.
Definition: area.h:51
Definition: study.h:57
Marginal CongestionProbability.
Definition: congestionProbability.h:109
VCardCongestionProbability VCardType
VCard.
Definition: congestionProbability.h:114
NextT NextType
Type of the next static variable.
Definition: congestionProbability.h:112
Variable::IVariable< CongestionProbability< NextT >, NextT, VCardType > AncestorType
Ancestor.
Definition: congestionProbability.h:116
@ count
How many items have we got.
Definition: congestionProbability.h:126
VCardType::ResultsType ResultsType
List of expected results.
Definition: congestionProbability.h:119
Interface for any variable.
Definition: variable.h:47
StoredResultType pResults
All the results about this variable.
Definition: variable.h:323
Temporary buffer for allocating results for a single year.
Definition: intermediate.h:42
Definition: results.h:44
@ count
The count if item in the list.
Definition: results.h:52
Definition: cbuilder.h:120
static constexpr uint8_t hasIntermediateValues
Intermediate values.
Definition: congestionProbability.h:73
static constexpr uint8_t isPossiblyNonApplicable
Can this variable be non applicable (0 : no, 1 : yes)
Definition: congestionProbability.h:75
static constexpr int columnCount
Number of columns used by the variable (One ResultsType per column)
Definition: congestionProbability.h:67
static constexpr uint8_t decimal
Decimal precision.
Definition: congestionProbability.h:65
static constexpr uint8_t categoryDataLevel
Data Level.
Definition: congestionProbability.h:56
static std::string Description()
The short description of the variable.
Definition: congestionProbability.h:45
static std::string Caption()
Caption.
Definition: congestionProbability.h:33
Results< R::AllYears::Raw< > > ResultsType
The expecte results.
Definition: congestionProbability.h:53
static constexpr uint8_t spatialAggregate
The Spatial aggregation.
Definition: congestionProbability.h:69
static constexpr uint8_t nodeDepthForGUI
Indentation (GUI)
Definition: congestionProbability.h:63
static std::string Unit()
Unit.
Definition: congestionProbability.h:39
static constexpr uint8_t categoryFileLevel
File level (provided by the type of the results)
Definition: congestionProbability.h:58
static constexpr uint8_t precision
Precision (views)
Definition: congestionProbability.h:61