Antares Simulator
Power System Simulator
in-memory-study.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 #define WIN32_LEAN_AND_MEAN
23 #include <limits>
24 
25 #include "antares/io/outputs/SimulationTableCsv.h"
26 #include "antares/solver/simulation/ISimulationObserver.h"
27 #include "antares/solver/simulation/economy.h"
28 #include "antares/solver/simulation/simulation.h"
29 #include "antares/study/scenario-builder/rules.h"
30 #include "antares/study/scenario-builder/sets.h"
31 #include "antares/study/study.h"
32 
33 using namespace Antares::Solver;
34 using namespace Antares::Solver::Simulation;
35 using namespace Antares::Data::ScenarioBuilder;
36 
37 void initializeStudy(Data::Study* study);
38 void configureLinkCapacities(AreaLink* link);
39 
41 {
42 public:
43  TimeSeriesConfigurer() = default;
44 
46  ts_(&matrix)
47  {
48  }
49 
51  ts_(&ts.timeSeries)
52  {
53  }
54 
55  TimeSeriesConfigurer& setDimensions(unsigned columnCount, unsigned rowCount = HOURS_PER_YEAR);
56  TimeSeriesConfigurer& fillColumnWith(unsigned column, double value);
57  TimeSeriesConfigurer& fillColumnWith(unsigned column, const std::vector<double>& values);
58 
59 private:
60  Matrix<>* ts_ = nullptr;
61 };
62 
64 {
65 public:
66  ThermalClusterConfig() = delete;
67  explicit ThermalClusterConfig(std::shared_ptr<ThermalCluster> cluster);
68  ThermalClusterConfig& setNominalCapacity(double nominalCapacity);
69  ThermalClusterConfig& setUnitCount(unsigned unitCount);
70  ThermalClusterConfig& setCosts(double cost);
71  ThermalClusterConfig& setAvailablePowerNumberOfTS(unsigned columnCount);
72  ThermalClusterConfig& setAvailablePower(unsigned column, double value);
73 
74 private:
75  std::shared_ptr<ThermalCluster> cluster_ = nullptr;
76  TimeSeriesConfigurer tsAvailablePowerConfig_;
77 };
78 
80 {
81 public:
83 
85  storage(storage),
86  constraint(std::make_shared<Antares::Data::ShortTermStorage::AdditionalConstraints>())
87  {
88  }
89 
90  ShortTermStorageAddConstraintConfig& setName(const std::string& name)
91  {
92  constraint->name = name;
93  return *this;
94  }
95 
96  ShortTermStorageAddConstraintConfig& setVariable(const std::string& variable)
97  {
98  constraint->variable = variable;
99  return *this;
100  }
101 
102  ShortTermStorageAddConstraintConfig& setOperatorType(const std::string& operatorType)
103  {
104  constraint->operatorType = operatorType;
105  return *this;
106  }
107 
108  ShortTermStorageAddConstraintConfig& setHours(const std::vector<std::set<int>>& hourSets)
109  {
110  for (const auto& hourSet: hourSets)
111  {
112  constraint->constraints.push_back(
113  {.hours = hourSet, .globalIndex = 0, .localIndex = 0});
114  }
115  return *this;
116  }
117 
118  std::shared_ptr<Antares::Data::ShortTermStorage::AdditionalConstraints> build()
119  {
120  storage.additionalConstraints.push_back(std::move(constraint));
121  // The ShortTermStorageAddConstraintConfig instance may be re-used
122  constraint = std::make_shared<Antares::Data::ShortTermStorage::AdditionalConstraints>();
123  return storage.additionalConstraints.back();
124  }
125 
126 private:
128  std::shared_ptr<Antares::Data::ShortTermStorage::AdditionalConstraints> constraint;
129 };
130 
132 
133 {
134 public:
135  ShortTermStorageConfig() = delete;
137  ShortTermStorageConfig& setInjectionNominalCapacity(double injectionNominalCapacity);
138  ShortTermStorageConfig& setWithdrawalNominalCapacity(double withdrawalNominalCapacity);
139  ShortTermStorageConfig& setReservoirCapacity(double reservoirCapacity);
140  ShortTermStorageConfig& setInitialLevel(double initialLevel);
141  ShortTermStorageConfig& setInitialLevelOptim(bool initialLevelOptim);
142  ShortTermStorageConfig& setInjectionEfficiency(double injectionEfficiency);
143  ShortTermStorageConfig& setWithdrawalEfficiency(double withdrawalEfficiency);
144  ShortTermStorageConfig& setGroupName(const std::string& groupName);
145  ShortTermStorageConfig& setName(const std::string& name);
146  ShortTermStorageConfig& setPenalizeVariationWithdrawal(bool penalizeVariationWithdrawal);
147  ShortTermStorageConfig& setAllowOverflow(bool allowOverflow);
148 
149  ShortTermStorageConfig& setPenalizeVariationInjection(bool penalizeVariationInjection);
150  ShortTermStorageConfig& setEnabled(bool enabled);
151 
152  ShortTermStorageAddConstraintConfig& addConstraint()
153  {
154  return constraintConfig;
155  }
156 
157 private:
159  ShortTermStorageAddConstraintConfig constraintConfig;
160 };
161 
162 std::shared_ptr<ThermalCluster> addClusterToArea(Area* area, const std::string& clusterName);
163 
165  const std::string& stsName);
166 
167 void addScratchpadToEachArea(Data::Study& study);
168 
169 // -------------------------------
170 // Simulation results retrieval
171 // -------------------------------
172 class averageResults final
173 {
174 public:
176  averageResults_(averageResults)
177  {
178  }
179 
180  long double* hours()
181  {
182  return averageResults_.hourly.data();
183  }
184 
185  double hour(unsigned hour)
186  {
187  return averageResults_.hourly[hour];
188  }
189 
190  long double* days()
191  {
192  return averageResults_.daily.data();
193  }
194 
195  double day(unsigned day)
196  {
197  return averageResults_.daily[day];
198  }
199 
200  long double* weeks()
201  {
202  return averageResults_.weekly.data();
203  }
204 
205  double week(unsigned week)
206  {
207  return averageResults_.weekly[week];
208  }
209 
210 private:
211  Variable::R::AllYears::AverageData& averageResults_;
212 };
213 
214 class OutputRetriever final
215 {
216 public:
218  simulation_(simulation)
219  {
220  }
221 
222  averageResults overallCost(Area* area);
223  averageResults levelForSTSgroup(Area* area, unsigned groupNb);
224  averageResults withdrawalForSTSgroup(Area* area, unsigned groupNb);
225  averageResults load(Area* area);
226  averageResults hydroStorage(Area* area);
227  averageResults flow(AreaLink* link);
228  averageResults thermalGeneration(ThermalCluster* cluster);
229  averageResults thermalNbUnitsON(ThermalCluster* cluster);
230 
231 private:
232  template<class VCard>
233  typename Variable::Storage<VCard>::ResultsType* retrieveAreaResults(Area* area);
234 
235  template<class VCard>
236  typename Variable::Storage<VCard>::ResultsType* retrieveLinkResults(AreaLink* link);
237 
238  template<class VCard>
239  typename Variable::Storage<VCard>::ResultsType* retrieveResultsForThermalCluster(
240  ThermalCluster* cluster);
241 
242  ISimulation<Economy>& simulation_;
243 };
244 
245 template<class VCard>
246 typename Variable::Storage<VCard>::ResultsType* OutputRetriever::retrieveAreaResults(Area* area)
247 {
248  typename Variable::Storage<VCard>::ResultsType* result = nullptr;
249  simulation_.variables.retrieveResultsForArea<VCard>(&result, area);
250  return result;
251 }
252 
253 template<class VCard>
254 typename Variable::Storage<VCard>::ResultsType* OutputRetriever::retrieveLinkResults(AreaLink* link)
255 {
256  typename Variable::Storage<VCard>::ResultsType* result = nullptr;
257  simulation_.variables.retrieveResultsForLink<VCard>(&result, link);
258  return result;
259 }
260 
261 template<class VCard>
262 typename Variable::Storage<VCard>::ResultsType* OutputRetriever::retrieveResultsForThermalCluster(
263  ThermalCluster* cluster)
264 {
265  typename Variable::Storage<VCard>::ResultsType* result = nullptr;
266  simulation_.variables.retrieveResultsForThermalCluster<VCard>(&result, cluster);
267  return result;
268 }
269 
271 {
272 public:
274 
275  loadTSNumberData& load()
276  {
277  return rules_->load;
278  }
279 
281  {
282  return rules_->binding_constraints;
283  }
284 
285  hydroTSNumberData& hydro()
286  {
287  return rules_->hydro;
288  }
289 
290  // index = area index
291  std::vector<ShortTermAdditionalConstraintsTSNumberData>& stsAdditionalConstraints()
292  {
293  return rules_->shortTermStorageAdditionalConstraints;
294  }
295 
296 private:
297  Rules::Ptr rules_;
298 };
299 
300 // =====================
301 // Simulation handler
302 // =====================
303 
305 {
306 public:
307  struct Variable
308  {
309  // All comparisons with NaN return false, except for !=
310  // For example (NaN == 4.) => false
311  // (NaN == NaN) => false
312  // Using any other arbitrary value (infinity, 0, etc.) may result in false positives
313  // or false negatives
314  double Xmin = std::numeric_limits<double>::quiet_NaN();
315  double Xmax = std::numeric_limits<double>::quiet_NaN();
316  double objectiveCoefficient = std::numeric_limits<double>::quiet_NaN();
317  };
318 
319  struct Constraint
320  {
321  double rhs = std::numeric_limits<double>::quiet_NaN();
322  std::map<std::string, double> coefficients;
323  };
324 
326 
327  {
328  std::map<std::string, Variable> variables;
329  std::map<std::string, Constraint> constraints;
330  };
331 
332  std::map<std::pair<int, std::string>, SingleProblem> problems;
333 
334  void notifyHebdoProblem(const PROBLEME_HEBDO& problemeHebdo,
335  int optimizationNumber,
336  std::string_view name) override;
337 };
338 
339 class SimulationHandler final
340 {
341 public:
343  study_(study)
344  {
345  }
346 
347  ~SimulationHandler() = default;
348 
349  SimulationHandler(const SimulationHandler&) = delete;
350  SimulationHandler& operator=(const SimulationHandler&) = delete;
351 
352  void create();
353 
354  void run()
355  {
356  simulation_->run();
357  }
358 
359  ISimulation<Economy>& rawSimu()
360  {
361  return *simulation_;
362  }
363 
364 public:
365  const TestingSimulationObserver& getObserver() const
366  {
367  return observer_;
368  }
369 
370 private:
371  std::shared_ptr<ISimulation<Economy>> simulation_;
372  Benchmarking::DurationCollector durationCollector_;
373  Settings settings_;
374  Data::Study& study_;
375  NullResultWriter resultWriter_;
376  TestingSimulationObserver observer_;
377 };
378 
379 // =========================
380 // Basic study builder
381 // =========================
382 
384 {
385  StudyBuilder();
386 
387  void simulationBetweenDays(const unsigned firstDay, const unsigned lastDay);
388  Area* addAreaToStudy(const std::string& areaName);
389  void setNumberMCyears(unsigned nbYears);
390  void playOnlyYear(unsigned year);
391  void giveWeightToYear(float weight, unsigned year);
392 
393  // Data members
394  std::unique_ptr<Data::Study> study;
395  SimulationHandler simulation;
396 };
397 
398 std::shared_ptr<Antares::Data::BindingConstraint> addBindingConstraints(Antares::Data::Study& study,
399  std::string name,
400  std::string group);
Definition for a single area.
Definition: area.h:51
Definition: BindingConstraintsTSNumbersData.h:32
std::shared_ptr< Rules > Ptr
Smart pointer.
Definition: rules.h:53
Definition: HydroTSNumberData.h:29
Definition: LoadTSNumberData.h:29
Definition: study.h:57
A single thermal cluster.
Definition: cluster.h:76
This class is used to represent the generic time series.
Definition: series.h:65
A n-by-n matrix.
Definition: matrix.h:44
Definition: i_writer.h:53
The ISimulationObserver class is an interface for observing the simulation.
Definition: ISimulationObserver.h:36
Definition: DurationCollector.h:36
Definition: in-memory-study.h:215
Definition: in-memory-study.h:271
Command line settings for launching the simulation.
Definition: options.h:36
Definition: in-memory-study.h:80
Definition: in-memory-study.h:133
Definition: in-memory-study.h:340
Definition: in-memory-study.h:305
Definition: in-memory-study.h:64
Definition: in-memory-study.h:41
Definition: in-memory-study.h:173
VariableAccessor< typename VCardT::ResultsType, VCardT::columnCount >::Type ResultsType
The true type used for the results.
Definition: info.h:760
Definition: sim_structure_probleme_economique.h:403
Definition: in-memory-study.h:384
Definition: in-memory-study.h:320
Definition: in-memory-study.h:327
Definition: in-memory-study.h:308