Antares Simulator
Power System Simulator
progression.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 __ANTARES_LIBS_SOLVER_SIMULATION_PROGRESSION_H__
22 #define __ANTARES_LIBS_SOLVER_SIMULATION_PROGRESSION_H__
23 
24 #include <atomic>
25 #include <list>
26 #include <map>
27 #include <mutex>
28 #include <vector>
29 
30 #include <yuni/core/singleton.h>
31 #include <yuni/io/file.h>
32 #include <yuni/thread/timer.h>
33 
34 #include <antares/writer/i_writer.h>
35 
36 #include "../fwd.h"
37 
38 namespace Antares::Solver
39 {
43 class Progression final
44 {
45 public:
46  enum Section
47  {
48  sectYear = 0,
49  sectOutput,
50  sectTSGLoad,
51  sectTSGSolar,
52  sectTSGWind,
53  sectTSGHydro,
54  sectTSGThermal,
55  sectImportTS,
56  sectMax
57  };
58 
59  enum
60  {
61  npos = (uint)-1,
62  };
63 
64  const char* SectionToCStr(Section section);
65 
66 private:
67  class Part final
68  {
69  public:
70  using MapPerSection = std::map<Section, Part>;
71  using Map = std::map<uint, MapPerSection>;
72  // using ListRef = Yuni::LinkedList<Part*>;
73  using ListRef = std::list<Part*>;
74 
75  public:
77  unsigned maxTickCount;
79  std::atomic<unsigned> tickCount;
81  unsigned lastTickCount;
82  // Caption to use when displaying logs
83  // Example: 'year: 10000, task: thermal'
84  Yuni::CString<40, false> caption;
85  };
86 
87 public:
88  class Task final
89  {
90  public:
91  Task(const Antares::Data::Study& study, Section section);
92  Task(const Antares::Data::Study& study, uint year, Section section);
93 
94  ~Task()
95  {
96  pProgression.end(pPart);
97  }
98 
99  Task& operator++()
100  {
101  ++pPart.tickCount;
102  return *this;
103  }
104 
105  Task& operator+=(unsigned value)
106  {
107  pPart.tickCount += value;
108  return *this;
109  }
110 
111  private:
112  Progression& pProgression;
113  Part& pPart;
114  };
115 
116 public:
118 
119 
122  Progression();
126  ~Progression();
128 
139  void add(uint year, Section section, unsigned nbTicks);
140 
141  void add(Section section, int nbTicks);
142 
143  bool saveToFile(const Yuni::String& filename, IResultWriter& writer);
144 
145  void setNumberOfParallelYears(uint nb);
146 
148 
149 
152  void start();
153 
157  void stop();
159 
160 protected:
161  Part& begin(uint year, Section section);
162  void end(Part& part);
163 
164 private:
165  class Meter final: public Yuni::Thread::Timer
166  {
167  public:
168  Meter();
169 
170  virtual ~Meter() = default;
171 
175  void taskCount(uint n);
176 
177  protected:
178  virtual bool onInterval(uint) override;
179 
180  public:
181  Progression::Part::Map parts;
182  Part::ListRef inUse;
183  std::mutex mutex;
184  uint nbParallelYears;
185 
186  std::vector<Yuni::CString<256, false>> logsContainer;
187 
188  }; // class Meter
189 
190 private:
192  Meter pProgressMeter;
194  // This variable has no need to be thread-safe because initialized at the very beginning
195  bool pStarted;
196  // Friend
197  friend class Task;
198 
199 }; // class Progression
200 
201 } // namespace Antares::Solver
202 
203 #include "progression.hxx"
204 
205 #endif // __ANTARES_LIBS_SOLVER_SIMULATION_PROGRESSION_H__
Definition: study.h:57
Definition: i_writer.h:32
Definition: progression.h:89
Progress meter about any operation performed on the attached study.
Definition: progression.h:44
void start()
Start the thread dedicated to the progress notification.
Definition: progression.cpp:193
void add(uint year, Section section, unsigned nbTicks)
Declare a new part of the progression.
Definition: progression.cpp:45
~Progression()
Destructor.
Definition: progression.cpp:157
void stop()
Stop the thread dedicated to the progress notification.
Definition: progression.cpp:200