Antares Simulator
Power System Simulator
spatial-aggregate.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 #pragma once
22 
23 #include "antares/solver/variable/variable.h"
24 
25 // #include <antares/logs/logs.h> // In case it is needed
26 
27 namespace Antares::Solver::Variable::Common
28 {
29 template<int ColumnCountT, class VCardT>
31 {
32  static std::string Caption(const uint indx)
33  {
34  return VCardT::Multiple::Caption(indx);
35  }
36 
37  static std::string Unit(const unsigned int indx)
38  {
39  return VCardT::Multiple::Unit(indx);
40  }
41 };
42 
43 template<class VCardT>
44 struct MultipleCaptionProxy<0, VCardT>
45 {
46  static std::string Caption(const uint)
47  {
48  return "";
49  }
50 
51  static std::string Unit(const uint)
52  {
53  return "";
54  }
55 };
56 
57 template<class VCardT>
58 struct MultipleCaptionProxy<1, VCardT>
59 {
60  static std::string Caption(const uint)
61  {
62  return "";
63  }
64 
65  static std::string Unit(const uint)
66  {
67  return "";
68  }
69 };
70 
71 template<class VCardT>
72 struct MultipleCaptionProxy<Category::dynamicColumns, VCardT>
73 {
74  static std::string Caption(const uint)
75  {
76  return "";
77  }
78 
79  static std::string Unit(const uint)
80  {
81  return "";
82  }
83 };
84 
85 template<template<class> class V>
86 struct VCardProxy
87 {
89  typedef typename V<Container::EndOfList>::VCardType VCardOrigin;
90 
92  static std::string Caption()
93  {
94  return VCardOrigin::Caption();
95  }
96 
98  static std::string Unit()
99  {
100  return VCardOrigin::Unit();
101  }
102 
104  static std::string Description()
105  {
106  return VCardOrigin::Description();
107  }
108 
110  typedef typename VCardOrigin::ResultsType ResultsType;
112  typedef typename VCardOrigin::VCardForSpatialAggregate VCardForSpatialAggregate;
113 
114  typedef typename VCardOrigin::IntermediateValuesType IntermediateValuesType;
115  typedef typename VCardOrigin::IntermediateValuesBaseType IntermediateValuesBaseType;
116  typedef
117  typename VCardOrigin::IntermediateValuesTypeForSpatialAg IntermediateValuesTypeForSpatialAg;
118 
120  static constexpr uint8_t categoryDataLevel = Category::DataLevel::setOfAreas;
122  static constexpr uint8_t categoryFileLevel = VCardOrigin::categoryFileLevel;
124  static constexpr uint8_t precision = VCardOrigin::precision;
126  static constexpr uint8_t nodeDepthForGUI = +0;
128  static constexpr uint8_t decimal = VCardOrigin::decimal;
130  static constexpr int columnCount = VCardOrigin::columnCount;
132  static constexpr uint8_t spatialAggregate = Category::noSpatialAggregate;
133  static constexpr uint8_t spatialAggregateMode = Category::spatialAggregateEachYear;
134  static constexpr uint8_t spatialAggregatePostProcessing = 0;
135 
137  static constexpr uint8_t hasIntermediateValues = 1;
139  static constexpr uint8_t isPossiblyNonApplicable = VCardOrigin::isPossiblyNonApplicable;
140 
141  struct Multiple
142  {
143  static std::string Caption(const uint indx)
144  {
146  }
147 
148  static std::string Unit(const unsigned int indx)
149  {
151  }
152  };
153 
154 }; // class VCard
155 
156 template<template<class> class VarT, class NextT = Container::EndOfList>
158  : public Variable::IVariable<SpatialAggregate<VarT, NextT>, NextT, VCardProxy<VarT>>
159 {
160 public:
162  typedef NextT NextType;
163 
168 
171 
173 
174  enum
175  {
177  count = 1 + NextT::count,
178  };
179 
180  template<int CDataLevel, int CFile>
181  struct Statistics
182  {
183  enum
184  {
185  count = ((VCardType::categoryDataLevel & CDataLevel
186  && VCardType::categoryFileLevel & CFile)
187  ? (NextType::template Statistics<CDataLevel, CFile>::count
188  + VCardType::columnCount * ResultsType::count)
189  : NextType::template Statistics<CDataLevel, CFile>::count),
190  };
191  };
192 
193 public:
194  void initializeFromStudy(Data::Study& study)
195  {
196  typedef
197  typename VCardType::VCardOrigin::IntermediateValuesBaseType IntermediateValuesBaseType;
198  pNbYearsParallel = study.maxNbYearsInParallel;
199 
200  // Intermediate values
201  VarT<Container::EndOfList>::InitializeResultsFromStudy(AncestorType::pResults, study);
202  pValuesForTheCurrentYear = std::make_unique<IntermediateValuesBaseType[]>(pNbYearsParallel);
203  for (unsigned int numSpace = 0; numSpace < pNbYearsParallel; numSpace++)
204  {
205  VariableAccessorType::InitializeAndReset(pValuesForTheCurrentYear[numSpace], study);
206  }
207 
208  // Next
209  NextType::initializeFromStudy(study);
210  }
211 
212  void initializeFromArea(Data::Study* study, Data::Area* area)
213  {
214  // Next
215  NextType::initializeFromArea(study, area);
216  }
217 
218  void initializeFromLink(Data::Study* study, Data::AreaLink* link)
219  {
220  // Next
221  NextType::initializeFromAreaLink(study, link);
222  }
223 
224  void simulationBegin()
225  {
226  // Next
227  NextType::simulationBegin();
228  }
229 
230  void simulationEnd()
231  {
232  NextType::simulationEnd();
233  }
234 
235  void yearBegin(uint year)
236  {
237  // Next variable
238  NextType::yearBegin(year);
239  }
240 
241  void yearEndBuildPrepareDataForEachThermalCluster(State& state, uint year)
242  {
243  // Next variable
244  NextType::yearEndBuildPrepareDataForEachThermalCluster(state, year);
245  }
246 
247  void yearEndBuildForEachThermalCluster(State& state, uint year)
248  {
249  // Next variable
250  NextType::yearEndBuildForEachThermalCluster(state, year);
251  }
252 
253  void yearEndBuild(State& state, unsigned int year, unsigned int numSpace)
254  {
255  // Next variable
256  NextType::yearEndBuild(state, year, numSpace);
257  }
258 
259  void yearEnd(uint year)
260  {
261  // Next variable
262  NextType::yearEnd(year);
263  }
264 
265  void weekBegin(State& state)
266  {
267  // Next variable
268  NextType::weekBegin(state);
269  }
270 
271  void weekEnd(State& state)
272  {
273  // Next variable
274  NextType::weekEnd(state);
275  }
276 
277  void hourBegin(uint hourInTheYear)
278  {
279  // Next variable
280  NextType::hourBegin(hourInTheYear);
281  }
282 
283  void hourForEachArea(State& state, unsigned int numSpace)
284  {
285  // Next variable
286  NextType::hourForEachArea(state, numSpace);
287  }
288 
289  template<class V, class SetT>
290  void yearEndSpatialAggregates(V& allVars, uint year, const SetT& set, uint numSpace)
291  {
292  if (VCardType::VCardOrigin::spatialAggregateMode & Category::spatialAggregateEachYear)
293  {
294  internalSpatialAggregateForCurrentYear(allVars, set, numSpace);
295  }
296 
297  // Next variable
298  NextType::yearEndSpatialAggregates(allVars, year, set, numSpace);
299  }
300 
301  template<class V>
302  void computeSpatialAggregatesSummary(V& allVars, unsigned int year, unsigned int numSpace)
303  {
304  if (VCardType::VCardOrigin::spatialAggregateMode & Category::spatialAggregateEachYear)
305  {
306  internalSpatialAggregateForParallelYears(year, numSpace);
307  }
308 
309  // Next variable
310  NextType::computeSpatialAggregatesSummary(allVars, year, numSpace);
311  }
312 
313  template<class V, class SetT>
314  void simulationEndSpatialAggregates(V& allVars, const SetT& set)
315  {
316  if (VCardType::VCardOrigin::spatialAggregateMode & Category::spatialAggregateOnce)
317  {
318  internalSpatialAggregate(allVars, 0, set);
319  }
320 
321  // Next variable
322  NextType::simulationEndSpatialAggregates(allVars, set);
323  }
324 
325  inline void buildDigest(SurveyResults& results, int digestLevel, int dataLevel) const
326  {
327  // Generate the Digest for the local results (districts part)
328  if (VCardType::columnCount != 0
329  && (VCardType::categoryDataLevel & Category::DataLevel::setOfAreas))
330  {
331  // Initializing pointer on variable non applicable and print stati arrays to beginning
332  results.isPrinted = AncestorType::isPrinted;
333  results.isCurrentVarNA = AncestorType::isNonApplicable;
334 
335  VariableAccessorType::template BuildDigest<typename VCardType::VCardOrigin>(
336  results,
338  digestLevel,
339  dataLevel);
340  }
341  // Ask to build the digest to the next variable
342  NextType::buildDigest(results, digestLevel, dataLevel);
343  }
344 
345  void localBuildAnnualSurveyReport(SurveyResults& results,
346  int fileLevel,
347  int precision,
348  uint numSpace) const
349  {
350  if (VCardType::columnCount != 0
351  && (VCardType::categoryDataLevel & Category::DataLevel::setOfAreas))
352  {
353  // Initializing pointer on variable non applicable and print stati arrays to beginning
354  results.isPrinted = AncestorType::isPrinted;
355  results.isCurrentVarNA = AncestorType::isNonApplicable;
356 
357  typedef VariableAccessor<typename VCardType::IntermediateValuesBaseType,
359  VAType;
360  VAType::template BuildAnnualSurveyReport<typename VCardType::VCardOrigin>(
361  results,
362  pValuesForTheCurrentYear[numSpace],
363  fileLevel,
364  precision);
365  }
366  }
367 
368 private:
369  template<class V, class SetT>
370  void internalSpatialAggregate(V& allVars, uint year, const SetT& set)
371  {
372  typedef typename VCardType::VCardOrigin VCardOrigin;
373  // Reset the results
374  VariableAccessorType::Reset(pValuesForTheCurrentYear[0]);
375 
376  // Make the spatial cluster
377  if (!set.empty())
378  {
379  auto end = set.end();
380  for (auto i = set.begin(); i != end; ++i)
381  {
382  allVars.template computeSpatialAggregateWith<
383  typename VCardType::VCardForSpatialAggregate> //<typename VCardType::VCardOrigin>
384  (pValuesForTheCurrentYear[0], *i /* the current area */, 0);
385  }
386 
387  // The spatial cluster may be an average
388  if (VCardType::VCardOrigin::spatialAggregate & Category::spatialAggregateAverage)
389  {
390  VariableAccessorType::MultiplyHourlyResultsBy(pValuesForTheCurrentYear[0],
391  1. / set.size());
392  }
393  // The spatial cluster may be an average
394  if (VCardType::VCardOrigin::spatialAggregate
395  & Category::spatialAggregateSumThen1IfPositive)
396  {
397  VariableAccessorType::SetTo1IfPositive(pValuesForTheCurrentYear[0]);
398  }
399  if (VCardType::VCardOrigin::spatialAggregate & Category::spatialAggregateOr)
400  {
401  VariableAccessorType::Or(pValuesForTheCurrentYear[0]);
402  }
403  }
404  else
405  {
406  assert(!set.empty() && "The set should not be empty at this point");
407  }
408 
409  // Compute all statistics for the current year (daily,weekly,monthly,...)
410  VariableAccessorType::template ComputeStatistics<VCardOrigin>(pValuesForTheCurrentYear[0]);
411  VariableAccessorType::ComputeSummary(pValuesForTheCurrentYear[0],
413  year);
414  }
415 
416  template<class V, class SetT>
417  void internalSpatialAggregateForCurrentYear(V& allVars, const SetT& set, uint numSpace)
418  {
419  typedef typename VCardType::VCardOrigin VCardOrigin;
420  // Reset the results
421  VariableAccessorType::Reset(pValuesForTheCurrentYear[numSpace]);
422 
423  // Make the spatial cluster
424  if (!set.empty())
425  {
426  // We compute the district's (or set) hourly values by looping over district's areas
427  // and calling computeSpatialAggregateWith on each area.
428  // The aggregated results is stored in pValuesForTheCurrentYear[numSpace].
429  auto end = set.end();
430  for (auto i = set.begin(); i != end; ++i)
431  {
432  // computeSpatialAggregateWith is called with VCardType::VCardForSpatialAggregate
433  // as a template parameter.
434  // Note that VCardType represents the vcard of the current spatial aggregate's
435  // variable (template parameter VarT).
436  // For most variables VCardType::VCardOrigin and
437  // VCardType::VCardForSpatialAggregate are identical vcard types.
438  // For example : for var OverallCost, VCardOverallCost::VCardForSpatialAggregate
439  // is simply VCardOverallCost itself.
440  // But var LOLD is different : VCardLOLD::VCardForSpatialAggregate is actually
441  // VCardUnsupliedEnergy.
442  // This means that, here :
443  // - for OverallCost, we sum the hourly values of var OverallCost for each
444  // district's areas.
445  allVars.template computeSpatialAggregateWith<
446  typename VCardType::VCardForSpatialAggregate>(pValuesForTheCurrentYear[numSpace],
447  *i /* the current area */,
448  numSpace);
449  }
450 
451  // The spatial cluster may be an average
452  if (VCardType::VCardOrigin::spatialAggregate & Category::spatialAggregateAverage)
453  {
454  VariableAccessorType::MultiplyHourlyResultsBy(pValuesForTheCurrentYear[numSpace],
455  1. / set.size());
456  }
457  // The spatial cluster may be an average
458  if (VCardType::VCardOrigin::spatialAggregate
459  & Category::spatialAggregateSumThen1IfPositive)
460  {
461  VariableAccessorType::SetTo1IfPositive(pValuesForTheCurrentYear[numSpace]);
462  }
463  if (VCardType::VCardOrigin::spatialAggregate & Category::spatialAggregateOr)
464  {
465  VariableAccessorType::Or(pValuesForTheCurrentYear[numSpace]);
466  }
467  }
468  else
469  {
470  assert(!set.empty() && "The set should not be empty at this point");
471  }
472 
473  // Compute all statistics for the current year (daily,weekly,monthly,...)
474  VariableAccessorType::template ComputeStatistics<VCardOrigin>(
475  pValuesForTheCurrentYear[numSpace]);
476  }
477 
478  void internalSpatialAggregateForParallelYears(unsigned int year, unsigned int numSpace)
479  {
480  // Merge all those values with the global results
481  VariableAccessorType::ComputeSummary(pValuesForTheCurrentYear[numSpace],
483  year);
484  }
485 
486 private:
488  VCardType::IntermediateValuesTypeForSpatialAg pValuesForTheCurrentYear;
489 
490  unsigned int pNbYearsParallel;
491 
492 }; // class SpatialAggregate
493 
494 // Variadic meta-template to build nested spatial aggregates
495 template<template<class> class Head, template<class> class... Tail>
497 {
498  using type = SpatialAggregate<Head, typename SpatialAggregateAll<Tail...>::type>;
499 };
500 
501 template<template<class> class Last>
503 {
505 };
506 
507 } // namespace Antares::Solver::Variable::Common
Definition for a single area.
Definition: area.h:51
Definition: study.h:57
Definition: spatial-aggregate.h:159
VCardType::ResultsType ResultsType
List of expected results.
Definition: spatial-aggregate.h:170
Variable::IVariable< SpatialAggregate< VarT, NextT >, NextT, VCardType > AncestorType
Ancestor.
Definition: spatial-aggregate.h:167
@ count
How many items have we got.
Definition: spatial-aggregate.h:177
VCardProxy< VarT > VCardType
VCard.
Definition: spatial-aggregate.h:165
NextT NextType
Type of the next static variable.
Definition: spatial-aggregate.h:162
Interface for any variable.
Definition: variable.h:47
StoredResultType pResults
All the results about this variable.
Definition: variable.h:323
void computeSpatialAggregateWith(O &out, uint numSpace)
Compute the spatial cluster with the results of a single variable.
Definition: variable.hxx:383
Definition: cbuilder.h:120
Definition: spatial-aggregate.h:87
static constexpr uint8_t spatialAggregate
The Spatial aggregation.
Definition: spatial-aggregate.h:132
static constexpr uint8_t categoryFileLevel
File level (provided by the type of the results)
Definition: spatial-aggregate.h:122
static constexpr uint8_t isPossiblyNonApplicable
Can this variable be non applicable (0 : no, 1 : yes)
Definition: spatial-aggregate.h:139
static constexpr uint8_t decimal
Decimal precision.
Definition: spatial-aggregate.h:128
static constexpr int columnCount
Number of columns used by the variable (One ResultsType per column)
Definition: spatial-aggregate.h:130
static constexpr uint8_t nodeDepthForGUI
Indentation (GUI)
Definition: spatial-aggregate.h:126
static constexpr uint8_t precision
Precision (views)
Definition: spatial-aggregate.h:124
static std::string Caption()
Caption.
Definition: spatial-aggregate.h:92
static constexpr uint8_t hasIntermediateValues
Intermediate values.
Definition: spatial-aggregate.h:137
V< Container::EndOfList >::VCardType VCardOrigin
The real VCard for the variable.
Definition: spatial-aggregate.h:89
static std::string Unit()
Unit.
Definition: spatial-aggregate.h:98
VCardOrigin::VCardForSpatialAggregate VCardForSpatialAggregate
The VCard to look for for calculating spatial aggregates.
Definition: spatial-aggregate.h:112
static constexpr uint8_t categoryDataLevel
Data Level.
Definition: spatial-aggregate.h:120
static std::string Description()
The short description of the variable.
Definition: spatial-aggregate.h:104
VCardOrigin::ResultsType ResultsType
The expecte results.
Definition: spatial-aggregate.h:110