Antares Simulator
Power System Simulator
timeseries_base.h
Go to the documentation of this file.
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 
67 #pragma once
68 
69 #include <algorithm>
70 #include <memory>
71 #include <string>
72 #include <string_view>
73 #include <vector>
74 
75 #include <antares/study/area/area.h>
76 #include "antares/solver/variable/variable.h"
77 
78 namespace Antares::Solver::Variable::Economy
79 {
80 
96 template<typename Derived>
98 {
100  static constexpr std::string_view kUnit = "MWh";
101 
105 
107  static constexpr uint8_t categoryDataLevel = Category::DataLevel::area;
109  static constexpr uint8_t categoryFileLevel = ResultsType::categoryFile
110  & (Category::FileLevel::id
111  | Category::FileLevel::va);
113  static constexpr uint8_t precision = Category::all;
115  static constexpr uint8_t nodeDepthForGUI = 0;
117  static constexpr uint8_t decimal = 0;
119  static constexpr int columnCount = 1;
121  static constexpr uint8_t spatialAggregate = Category::spatialAggregateSum;
123  static constexpr uint8_t spatialAggregateMode = Category::spatialAggregateEachYear;
125  static constexpr uint8_t spatialAggregatePostProcessing = 0;
127  static constexpr uint8_t hasIntermediateValues = 1;
129  static constexpr uint8_t isPossiblyNonApplicable = 0;
130 
134  using IntermediateValuesType = std::vector<IntermediateValues>;
136  using IntermediateValuesTypeForSpatialAg = std::unique_ptr<IntermediateValuesBaseType[]>;
137 };
138 
145 template<typename TraitsType>
146 struct VCardTimeSeriesBase: public TimeSeriesTraits<TraitsType>
147 {
152 
154  inline static constexpr std::string_view kCaption = TraitsType::kCaption;
155  inline static constexpr std::string_view kUnit = BaseType::kUnit;
156  inline static constexpr std::string_view kDescription = TraitsType::kDescription;
157 
160 
164  static std::string Caption()
165  {
166  return std::string(kCaption);
167  }
168 
173  static std::string Unit()
174  {
175  return std::string(kUnit);
176  }
177 
182  static std::string Description()
183  {
184  return std::string(kDescription);
185  }
186 
188 
191  using ResultsType = typename BaseType::ResultsType;
192  using IntermediateValuesBaseType = typename BaseType::IntermediateValuesBaseType;
193  using IntermediateValuesType = typename BaseType::IntermediateValuesType;
194  using IntermediateValuesTypeForSpatialAg = typename BaseType::
197 };
198 
202 namespace detail
203 {
213 template<class VCard, class Next, int CDataLevel, int CFile>
214 inline constexpr int StatisticsCount = ((VCard::categoryDataLevel & CDataLevel
215  && VCard::categoryFileLevel & CFile)
216  ? (Next::template Statistics<CDataLevel, CFile>::count
217  + VCard::columnCount * VCard::ResultsType::count)
218  : Next::template Statistics<CDataLevel, CFile>::count);
219 } // namespace detail
220 
261 template<typename Derived, typename NextT, typename VCardType>
262 class TimeSeriesValuesBase: public Variable::IVariable<Derived, NextT, VCardType>
263 {
264 public:
267  using NextType = NextT;
269  using ResultsType = typename VCardType::ResultsType;
272 
274  static constexpr int count = 1 + NextT::count;
275 
281  template<int CDataLevel, int CFile>
282  struct Statistics
283  {
284  static constexpr int count = detail::
285  StatisticsCount<VCardType, NextType, CDataLevel, CFile>;
286  };
287 
301  {
302  // Cache for performance - used frequently during simulation
303  nbYearsParallel = study.maxNbYearsInParallel;
304 
305  // Initialize the results storage with proper structure
307 
308  // Allocate intermediate values for each parallel space
310  std::for_each(yearlyValues.begin(),
311  yearlyValues.end(),
312  [&study](auto& value) { value.initializeFromStudy(study); });
313 
314  // Allow derived class to perform specific initialization
315  static_cast<Derived*>(this)->initializeDerivedFromStudy(study);
316  // Initialize next variable in chain
317  NextType::initializeFromStudy(study);
318  }
319 
326  template<class R>
328  {
329  VariableAccessorType::InitializeAndReset(results, study);
330  }
331 
337  void initializeFromArea(Data::Study* study, Data::Area* area) noexcept
338  {
339  areaPtr = area;
340  NextType::initializeFromArea(study, area);
341  }
342 
349  {
350  NextType::initializeFromAreaLink(study, link);
351  }
352 
358  {
359  std::for_each(yearlyValues.begin(), yearlyValues.end(), [](auto& value) { value.reset(); });
360  NextType::simulationBegin();
361  }
362 
367  {
368  NextType::simulationEnd();
369  }
370 
376  void yearBegin(unsigned int year, unsigned int space)
377  {
378  static_cast<Derived*>(this)->yearBeginImpl(year, space);
379  NextType::yearBegin(year, space);
380  }
381 
388  void yearEndBuild(State& state, unsigned int year, unsigned int space)
389  {
390  NextType::yearEndBuild(state, year, space);
391  }
392 
398  void yearEnd(unsigned int year, unsigned int space)
399  {
400  yearlyValues[space].computeStatisticsForTheCurrentYear();
401  NextType::yearEnd(year, space);
402  }
403 
409  void computeSummary(unsigned int year, unsigned int space)
410  {
411  AncestorType::pResults.merge(year, yearlyValues[space]);
412  NextType::computeSummary(year, space);
413  }
414 
419  void hourBegin(unsigned int hourInTheYear)
420  {
421  NextType::hourBegin(hourInTheYear);
422  }
423 
430  void hourForEachArea(State& state, unsigned int space)
431  {
432  static_cast<Derived*>(this)->hourForEachAreaImpl(state, space);
433  NextType::hourForEachArea(state, space);
434  }
435 
441  void hourEnd(State& state, unsigned int hourInTheYear)
442  {
443  NextType::hourEnd(state, hourInTheYear);
444  }
445 
452  Antares::Memory::Stored<double>::ConstReturnType retrieveRawHourlyValuesForCurrentYear(
453  unsigned int,
454  unsigned int space) const noexcept
455  {
456  return yearlyValues[space].hour;
457  }
458 
467  int fileLevel,
468  int precision,
469  unsigned int space) const
470  {
471  results.isCurrentVarNA = AncestorType::isNonApplicable;
472  if (AncestorType::isPrinted[0])
473  {
474  results.variableCaption = VCardType::kCaption.data();
475  results.variableUnit = std::string(VCardType::kUnit);
476  yearlyValues[space].template buildAnnualSurveyReport<VCardType>(results,
477  fileLevel,
478  precision);
479  }
480  }
481 
482 protected:
486  Data::Area* areaPtr{nullptr};
488  typename VCardType::IntermediateValuesType yearlyValues;
489 
491  unsigned int nbYearsParallel{0};
492 
494 };
495 
496 } // namespace Antares::Solver::Variable::Economy
Definition for a single area.
Definition: area.h:51
Definition: study.h:57
Base implementation for time series variables using CRTP pattern.
Definition: timeseries_base.h:263
void simulationEnd()
Cleanup at the end of simulation.
Definition: timeseries_base.h:366
void localBuildAnnualSurveyReport(SurveyResults &results, int fileLevel, int precision, unsigned int space) const
Build annual survey report for output.
Definition: timeseries_base.h:466
unsigned int nbYearsParallel
Number of parallel years (cached for performance)
Definition: timeseries_base.h:491
void hourBegin(unsigned int hourInTheYear)
Setup at the beginning of each hour.
Definition: timeseries_base.h:419
void hourEnd(State &state, unsigned int hourInTheYear)
Cleanup at the end of each hour.
Definition: timeseries_base.h:441
VCardType::IntermediateValuesType yearlyValues
Intermediate values for each parallel space.
Definition: timeseries_base.h:488
void hourForEachArea(State &state, unsigned int space)
Process data for each area during an hour.
Definition: timeseries_base.h:430
void initializeFromArea(Data::Study *study, Data::Area *area) noexcept
Associate this variable with a specific area.
Definition: timeseries_base.h:337
void yearEndBuild(State &state, unsigned int year, unsigned int space)
Post-processing after year end build.
Definition: timeseries_base.h:388
void yearEnd(unsigned int year, unsigned int space)
Finalize year and compute statistics.
Definition: timeseries_base.h:398
void initializeFromLink(Data::Study *study, Data::AreaLink *link)
Initialize from link (for link-based variables)
Definition: timeseries_base.h:348
void computeSummary(unsigned int year, unsigned int space)
Merge year results into global summary.
Definition: timeseries_base.h:409
void simulationBegin()
Reset state at the beginning of simulation.
Definition: timeseries_base.h:357
void initializeFromStudy(Data::Study &study)
Initialize the variable from study configuration.
Definition: timeseries_base.h:300
Antares::Memory::Stored< double >::ConstReturnType retrieveRawHourlyValuesForCurrentYear(unsigned int, unsigned int space) const noexcept
Retrieve raw hourly values for the current year.
Definition: timeseries_base.h:452
void yearBegin(unsigned int year, unsigned int space)
Setup at the beginning of each year.
Definition: timeseries_base.h:376
static void InitializeResultsFromStudy(R &results, Data::Study &study)
Static helper to initialize results from study.
Definition: timeseries_base.h:327
static constexpr int count
Total count of variables in the chain.
Definition: timeseries_base.h:274
Interface for any variable.
Definition: variable.h:47
const StoredResultType & results() const
The results.
Definition: variable.hxx:544
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
Definition: state.h:71
Class utility for building CSV results files.
Definition: surveyresults.h:41
constexpr int StatisticsCount
Compile-time calculation of statistics count.
Definition: timeseries_base.h:214
Base traits template providing common properties for all time series variables.
Definition: timeseries_base.h:98
static constexpr uint8_t categoryDataLevel
Data aggregation level (area-based)
Definition: timeseries_base.h:107
static constexpr uint8_t decimal
Decimal precision for display.
Definition: timeseries_base.h:117
static constexpr uint8_t isPossiblyNonApplicable
Whether variable can be non-applicable.
Definition: timeseries_base.h:129
static constexpr uint8_t nodeDepthForGUI
GUI indentation level.
Definition: timeseries_base.h:115
static constexpr uint8_t precision
Precision settings for output.
Definition: timeseries_base.h:113
static constexpr uint8_t categoryFileLevel
File output level configuration.
Definition: timeseries_base.h:109
Results< R::AllYears::Average< R::AllYears::StdDeviation< R::AllYears::Min< R::AllYears::Max<> >> >> ResultsType
Results configuration: Average, StdDev, Min, Max across all years.
Definition: timeseries_base.h:104
static constexpr uint8_t spatialAggregate
Spatial aggregation method (sum across areas)
Definition: timeseries_base.h:121
static constexpr uint8_t spatialAggregateMode
Spatial aggregation mode (each year separately)
Definition: timeseries_base.h:123
static constexpr uint8_t spatialAggregatePostProcessing
Post-processing for spatial aggregation.
Definition: timeseries_base.h:125
static constexpr uint8_t hasIntermediateValues
Whether intermediate values are computed.
Definition: timeseries_base.h:127
IntermediateValues IntermediateValuesBaseType
Base type for intermediate values storage.
Definition: timeseries_base.h:132
static constexpr int columnCount
Number of columns in output (typically 1 for time series)
Definition: timeseries_base.h:119
std::vector< IntermediateValues > IntermediateValuesType
Container for intermediate values (one per parallel space)
Definition: timeseries_base.h:134
std::unique_ptr< IntermediateValuesBaseType[]> IntermediateValuesTypeForSpatialAg
Pointer type for spatial aggregation.
Definition: timeseries_base.h:136
static constexpr std::string_view kUnit
Unit for all time series variables.
Definition: timeseries_base.h:100
Compile-time statistics calculation.
Definition: timeseries_base.h:283
VCard template providing variable metadata and configuration.
Definition: timeseries_base.h:147
static std::string Caption()
Get variable caption as std::string.
Definition: timeseries_base.h:164
static std::string Unit()
Get variable unit as std::string.
Definition: timeseries_base.h:173
static std::string Description()
Get variable description as std::string.
Definition: timeseries_base.h:182
static constexpr std::string_view kCaption
Modern C++20 API: compile-time string views.
Definition: timeseries_base.h:154