Antares Simulator
Power System Simulator
Loading...
Searching...
No Matches
stdDeviation.h
1/*
2** Copyright 2007-2024, 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 __SOLVER_VARIABLE_STORAGE_STD_DEVIATION_H__
22#define __SOLVER_VARIABLE_STORAGE_STD_DEVIATION_H__
23
24#include <cmath>
25#include <float.h>
26#include <limits>
27
28namespace Antares
29{
30namespace Solver
31{
32namespace Variable
33{
34namespace R
35{
36namespace AllYears
37{
38template<class NextT = Empty, int FileFilter = Variable::Category::FileLevel::allFile>
39struct StdDeviation: public NextT
40{
41public:
43 typedef NextT NextType;
44
45 enum
46 {
48 count = 1 + NextT::count,
49
50 categoryFile = NextT::categoryFile | Variable::Category::FileLevel::allFile,
51 };
52
53 struct Data
54 {
55 double value;
56 uint32_t indice;
57 };
58
60 static const char* Name()
61 {
62 return "std deviation";
63 }
64
65public:
67 {
68 using namespace Yuni;
69 stdDeviationHourly = nullptr;
70 }
71
72 ~StdDeviation()
73 {
74 Antares::Memory::Release(stdDeviationHourly);
75 }
76
77protected:
78 void initializeFromStudy(Antares::Data::Study& study)
79 {
80 Antares::Memory::Allocate<double>(stdDeviationHourly, HOURS_PER_YEAR);
81 // Next
82 NextType::initializeFromStudy(study);
83
84 yearsWeight = study.parameters.getYearsWeight();
85 yearsWeightSum = study.parameters.getYearsWeightSum();
86 }
87
88 void reset()
89 {
90 // Reset
91 (void)::memset(stdDeviationMonthly, 0, sizeof(double) * MONTHS_PER_YEAR);
92 (void)::memset(stdDeviationWeekly, 0, sizeof(double) * WEEKS_PER_YEAR);
93 (void)::memset(stdDeviationDaily, 0, sizeof(double) * DAYS_PER_YEAR);
94 Antares::Memory::Zero(HOURS_PER_YEAR, stdDeviationHourly);
95 stdDeviationYear = 0.;
96 // Next
97 NextType::reset();
98 }
99
100 void merge(unsigned int year, const IntermediateValues& rhs)
101 {
102 // Ratio take into account MC year weight
103 double pRatio = (double)yearsWeight[year] / (double)yearsWeightSum;
104
105 unsigned int i;
106 // StdDeviation value for each hour throughout all years
107 for (i = 0; i != HOURS_PER_YEAR; ++i)
108 {
109 stdDeviationHourly[i] += rhs.hour[i] * rhs.hour[i] * pRatio;
110 }
111 // StdDeviation value for each day throughout all years
112 for (i = 0; i != DAYS_PER_YEAR; ++i)
113 {
114 stdDeviationDaily[i] += rhs.day[i] * rhs.day[i] * pRatio;
115 }
116 // StdDeviation value for each week throughout all years
117 for (i = 0; i != WEEKS_PER_YEAR; ++i)
118 {
119 stdDeviationWeekly[i] += rhs.week[i] * rhs.week[i] * pRatio;
120 }
121 // StdDeviation value for each month throughout all years
122 for (i = 0; i != MONTHS_PER_YEAR; ++i)
123 {
124 stdDeviationMonthly[i] += rhs.month[i] * rhs.month[i] * pRatio;
125 }
126 // StdDeviation value throughout all years
127 stdDeviationYear += rhs.year * rhs.year * pRatio;
128
129 // Next
130 NextType::merge(year, rhs);
131 }
132
133 template<class S, class VCardT>
134 void buildSurveyReport(SurveyResults& report,
135 const S& results,
136 int dataLevel,
137 int fileLevel,
138 int precision) const
139 {
140 if (!(fileLevel & Category::FileLevel::id))
141 {
142 switch (precision)
143 {
144 case Category::hourly:
145 InternalExportValues<S, HOURS_PER_YEAR, VCardT, Category::hourly>(
146 report,
147 results,
148 Memory::RawPointer(stdDeviationHourly));
149 break;
150 case Category::daily:
151 InternalExportValues<S, DAYS_PER_YEAR, VCardT, Category::daily>(report,
152 results,
153 stdDeviationDaily);
154 break;
155 case Category::weekly:
156 InternalExportValues<S, WEEKS_PER_YEAR, VCardT, Category::weekly>(
157 report,
158 results,
159 stdDeviationWeekly);
160 break;
161 case Category::monthly:
162 InternalExportValues<S, MONTHS_PER_YEAR, VCardT, Category::monthly>(
163 report,
164 results,
165 stdDeviationMonthly);
166 break;
167 case Category::annual:
168 InternalExportValues<S, 1, VCardT, Category::annual>(report,
169 results,
170 &stdDeviationYear);
171 break;
172 }
173 }
174 // Next
175 NextType::template buildSurveyReport<S, VCardT>(report,
176 results,
177 dataLevel,
178 fileLevel,
179 precision);
180 }
181
182 template<template<class, int> class DecoratorT>
183 Antares::Memory::Stored<double>::ConstReturnType hourlyValuesForSpatialAggregate() const
184 {
185 if (Yuni::Static::Type::StrictlyEqual<DecoratorT<Empty, 0>, StdDeviation<Empty, 0>>::Yes)
186 {
187 return stdDeviationHourly;
188 }
189 return NextType::template hourlyValuesForSpatialAggregate<DecoratorT>();
190 }
191
192public:
193 double stdDeviationMonthly[MONTHS_PER_YEAR];
194 double stdDeviationWeekly[WEEKS_PER_YEAR];
195 double stdDeviationDaily[DAYS_PER_YEAR];
196 Antares::Memory::Stored<double>::Type stdDeviationHourly;
197 double stdDeviationYear;
198
199private:
200 template<class S, unsigned int Size, class VCardT, int PrecisionT, class A>
201 void InternalExportValues(SurveyResults& report, const S& results, const A& array) const
202 {
203 assert(report.data.columnIndex < report.maxVariables && "Column index out of bounds");
204
205 // Caption
206 report.captions[0][report.data.columnIndex] = report.variableCaption;
207 report.captions[1][report.data.columnIndex] = report.variableUnit;
208 report.captions[2][report.data.columnIndex] = "std";
209
210 // Precision
211 report.precision[report.data.columnIndex] = PrecisionToPrintfFormat<
212 VCardT::decimal>::Value();
213
214 // Non applicability
215 report.nonApplicableStatus[report.data.columnIndex] = *report.isCurrentVarNA;
216
217 // Values
218 double* target = report.values[report.data.columnIndex];
219 // A mere copy
220
221 auto squareRootChecked = [](double d) { return d >= 0 ? std::sqrt(d) : 0.; };
222
223 switch (PrecisionT)
224 {
225 case Category::hourly:
226 {
227 for (unsigned int i = 0; i != Size; ++i)
228 {
229 target[i] = squareRootChecked(
230 array[i] - results.avgdata.hourly[i] * results.avgdata.hourly[i]);
231 }
232 }
233 break;
234 case Category::daily:
235 {
236 for (unsigned int i = 0; i != Size; ++i)
237 {
238 target[i] = squareRootChecked(
239 array[i] - results.avgdata.daily[i] * results.avgdata.daily[i]);
240 }
241 }
242 break;
243 case Category::weekly:
244 {
245 for (unsigned int i = 0; i != Size; ++i)
246 {
247 target[i] = squareRootChecked(
248 array[i] - results.avgdata.weekly[i] * results.avgdata.weekly[i]);
249 }
250 }
251 break;
252 case Category::monthly:
253 {
254 for (unsigned int i = 0; i != Size; ++i)
255 {
256 target[i] = squareRootChecked(
257 array[i] - results.avgdata.monthly[i] * results.avgdata.monthly[i]);
258 }
259 }
260 break;
261 case Category::annual:
262 {
263 const double d = *array - results.avgdata.allYears * results.avgdata.allYears;
264 *target = squareRootChecked(d);
265 }
266 break;
267 }
268
269 // Next column index
270 ++report.data.columnIndex;
271 }
272
273 template<class S, unsigned int Size, class VCardT, int PrecisionT, class A>
274 void InternalExportValuesMC(SurveyResults& report, const S& /*results*/, const A& array) const
275 {
276 if (not(PrecisionT & Category::annual))
277 {
278 return;
279 }
280 assert(report.data.columnIndex < report.maxVariables && "Column index out of bounds");
281
282 // Caption
283 report.captions[0][report.data.columnIndex] = report.variableCaption;
284 report.captions[1][report.data.columnIndex] = report.variableUnit;
285 report.captions[2][report.data.columnIndex] = "std";
286
287 // Precision
288 report.precision[report.data.columnIndex] = PrecisionToPrintfFormat<
289 VCardT::decimal>::Value();
290
291 // Non applicability
292 report.nonApplicableStatus[report.data.columnIndex] = *report.isCurrentVarNA;
293
294 (void)::memcpy(report.data.matrix[report.data.columnIndex], array, Size * sizeof(double));
295
296 // Next column index
297 ++report.data.columnIndex;
298 }
299
300private:
301 std::vector<float> yearsWeight;
302 float yearsWeightSum;
303
304}; // class StdDeviation
305
306} // namespace AllYears
307} // namespace R
308} // namespace Variable
309} // namespace Solver
310} // namespace Antares
311
312#endif // __SOLVER_VARIABLE_STORAGE_STD_DEVIATION_H__
Definition study.h:61
static void Release(T *&pointer)
Release a raw pointer.
Definition memory.hxx:32
Definition variable.h:25
NextT NextType
Type of the net item in the list.
Definition stdDeviation.h:43
static const char * Name()
Name of the filter.
Definition stdDeviation.h:60
@ count
The count if item in the list.
Definition stdDeviation.h:48