Antares Simulator
Power System Simulator
applyToMatrix.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 //
22 // Created by marechaljas on 03/07/23.
23 //
24 
25 #pragma once
26 
27 #include <cstdlib>
28 
29 #include "antares/study/binding_constraint/BindingConstraintGroup.h"
30 #include "antares/study/parts/hydro/series.h"
31 #include "antares/study/parts/short-term-storage/cluster.h"
32 #include "antares/study/scenario-builder/TSnumberData.h"
33 
34 namespace Antares::Data::ScenarioBuilder
35 {
36 
37 static constexpr unsigned maxErrors = 20;
38 
39 template<class D>
40 static inline bool CheckValidity(uint value, const D& data, uint tsGenMax)
41 {
42  // When the TS-Generators are not used
43  return (!tsGenMax) ? (value < data.timeSeries.width) : (value < tsGenMax);
44 }
45 
46 template<>
47 inline bool CheckValidity<Data::DataSeriesHydro>(uint value,
48  const Data::DataSeriesHydro& data,
49  uint tsGenMax)
50 {
51  // When the TS-Generators are not used
52  return (!tsGenMax) ? (value < data.TScount()) : (value < tsGenMax);
53 }
54 
55 template<>
56 inline bool CheckValidity<Data::AreaLink>(uint value,
57  const Data::AreaLink& data,
58  uint /* tsGenMax */)
59 {
60  // Value = index of time series
61  // Direct Capacities = all time series
62  // directCapacities.timeSeries.width = Number of time series
63  return value < data.directCapacities.timeSeries.width;
64 }
65 
66 template<>
67 inline bool CheckValidity<BindingConstraintGroup>(uint value,
68  const BindingConstraintGroup& group,
69  uint)
70 {
71  return value < group.numberOfTimeseries();
72 }
73 
74 template<class StringT, class D>
75 bool ApplyToMatrix(uint& errors,
76  StringT& logprefix,
77  D& data,
79  uint tsGenMax)
80 {
81  bool ret = true;
82 
83  // In this case, m.height represents the total number of years
84  const uint nbYears = data.timeseriesNumbers.height();
85  // The matrix m has only one column
86  auto& target = data.timeseriesNumbers;
87 
88  for (uint y = 0; y != nbYears; ++y)
89  {
90  if (years[y] != 0)
91  {
92  // The new TS number
93  uint32_t tsNum = years[y] - 1;
94 
95  // When the TS-Generators are not used
96  if (!CheckValidity(tsNum, data, tsGenMax))
97  {
98  if (errors <= maxErrors)
99  {
100  if (++errors == maxErrors)
101  {
102  logs.warning() << "scenario-builder: ... (skipped)";
103  }
104  else
105  {
106  logs.warning() << "scenario-builder: " << logprefix
107  << "value out of bounds for the year " << (y + 1);
108  }
109  }
110  ret = false;
111  continue;
112  }
113  // Ok, assign. The value provided by the interface is user-friendly
114  // and starts from 1.
115  target[y] = tsNum;
116  }
117  }
118 
119  return ret;
120 }
121 } // namespace Antares::Data::ScenarioBuilder
Data series (Hydro)
Definition: series.h:38
typename Antares::Memory::Stored< uint32_t >::Type ColumnType
Column type.
Definition: matrix.h:62
uint width
Width of the matrix.
Definition: matrix.h:441