Antares Simulator
Power System Simulator
generator.hxx
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 __ANTARES_SOLVER_timeSeries_GENERATOR_HXX__
22 #define __ANTARES_SOLVER_timeSeries_GENERATOR_HXX__
23 
24 #include <antares/logs/logs.h>
25 
26 namespace Antares::TSGenerator
27 {
28 
29 // forward declaration
30 // Hydro - see hydro.cpp
31 bool GenerateHydroTimeSeries(Data::Study& study, IResultWriter& writer);
32 
33 template<>
34 inline bool GenerateTimeSeries<Data::timeSeriesHydro>(Data::Study& study, IResultWriter& writer)
35 {
36  return GenerateHydroTimeSeries(study, writer);
37 }
38 
39 // --- TS Generators using XCast ---
40 template<enum Data::TimeSeriesType T>
41 bool GenerateTimeSeries(Data::Study& study, IResultWriter& writer)
42 {
43  auto* xcast = reinterpret_cast<XCast::XCast*>(
44  study.cacheTSGenerator[Data::TimeSeriesBitPatternIntoIndex<T>::value]);
45 
46  if (not xcast)
47  {
48  logs.debug() << "Preparing the " << Data::TimeSeriesToCStr<T>::Value() << " TS Generator";
49  xcast = new XCast::XCast(study, T, writer);
50  study.cacheTSGenerator[Data::TimeSeriesBitPatternIntoIndex<T>::value] = xcast;
51  }
52 
53  // The current year
54  xcast->year = 0;
55 
56  switch (T)
57  {
58  case Data::timeSeriesLoad:
59  xcast->random = &(study.runtime.random[Data::seedTsGenLoad]);
60  break;
61  case Data::timeSeriesSolar:
62  xcast->random = &(study.runtime.random[Data::seedTsGenSolar]);
63  break;
64  case Data::timeSeriesWind:
65  xcast->random = &(study.runtime.random[Data::seedTsGenWind]);
66  break;
67  case Data::timeSeriesHydro:
68  xcast->random = &(study.runtime.random[Data::seedTsGenHydro]);
69  break;
70  default:
71  xcast->random = nullptr;
72  assert(false and "invalid ts type");
73  }
74 
75  // Run the generation of the time-series
76  bool r = xcast->run();
77  // Destroy if required the TS Generator
78  Destroy<T>(study);
79  return r;
80 }
81 
82 // TODO REMOVE
83 template<Data::TimeSeriesType T>
84 void Destroy(Data::Study& study)
85 {
86  auto* xcast = reinterpret_cast<XCast::XCast*>(
87  study.cacheTSGenerator[Data::TimeSeriesBitPatternIntoIndex<T>::value]);
88  if (not xcast)
89  {
90  return;
91  }
92 
93  logs.info() << " Releasing the " << Data::TimeSeriesToCStr<T>::Value() << " TS Generator";
94  study.cacheTSGenerator[Data::TimeSeriesBitPatternIntoIndex<T>::value] = nullptr;
95  study.destroyTSGeneratorData<T>();
96  delete xcast;
97  xcast = nullptr;
98 }
99 
100 } // namespace Antares::TSGenerator
101 
102 #endif // __ANTARES_SOLVER_timeSeries_GENERATOR_HXX__