Loading [MathJax]/extensions/tex2jax.js
Antares Simulator
Power System Simulator
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages Concepts
matrix-to-buffer.hxx
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
22#ifndef __ANTARES_LIBS_ARRAY_MATRIX_TO_BUFFER_SENDER_HXX__
23#define __ANTARES_LIBS_ARRAY_MATRIX_TO_BUFFER_SENDER_HXX__
24
25#ifdef YUNI_OS_MSVC
26#define ANTARES_MATRIX_SNPRINTF sprintf_s
27#else
28#define ANTARES_MATRIX_SNPRINTF snprintf
29#endif
30
31#include <antares/utils/utils.h>
32
33namespace Antares
34{
35namespace // anonymous
36{
37template<class T>
38struct MatrixScalar
39{
40 static inline void Append(std::string& file, T v, const char* const)
41 {
42 if (Utils::isZero(v))
43 {
44 file.append(std::to_string(0));
45 }
46 else
47 {
48 file.append(std::to_string(v));
49 }
50 }
51};
52
53template<>
54struct MatrixScalar<double>
55{
56 static void Append(std::string& file, double v, const char* const format)
57 {
58 if (Utils::isZero(v))
59 {
60 file += '0';
61 }
62 else
63 {
64 char ConversionBuffer[128];
65 const int sizePrintf = Utils::isZero(v - floor(v))
66 ? ANTARES_MATRIX_SNPRINTF(ConversionBuffer,
67 sizeof(ConversionBuffer),
68 "%.0f",
69 v)
70 : ANTARES_MATRIX_SNPRINTF(ConversionBuffer,
71 sizeof(ConversionBuffer),
72 format,
73 v);
74
75 if (sizePrintf >= 0 and sizePrintf < (int)(sizeof(ConversionBuffer)))
76 {
77 file += (const char*)ConversionBuffer;
78 }
79 else
80 {
81 file += "ERR";
82 }
83 }
84 }
85};
86
87template<>
88struct MatrixScalar<float>
89{
90 static void Append(std::string& file, float v, const char* const format)
91 {
92 if (Utils::isZero(v))
93 {
94 file += '0';
95 }
96 else
97 {
98 char ConversionBuffer[128];
99 const int sizePrintf = Utils::isZero(v - floor(v))
100 ? ANTARES_MATRIX_SNPRINTF(ConversionBuffer,
101 sizeof(ConversionBuffer),
102 "%.0f",
103 (double)v)
104 : ANTARES_MATRIX_SNPRINTF(ConversionBuffer,
105 sizeof(ConversionBuffer),
106 format,
107 (double)v);
108
109 if (sizePrintf >= 0 and sizePrintf < (int)(sizeof(ConversionBuffer)))
110 {
111 file += (const char*)ConversionBuffer;
112 }
113 else
114 {
115 file += "ERR";
116 }
117 }
118 }
119};
120
121} // anonymous namespace
122
123template<class T, class ReadWriteT, class PredicateT>
124I_mtx_to_buffer_dumper<T, ReadWriteT, PredicateT>* matrix_to_buffer_dumper_factory::get_dumper(
125 const Matrix<T, ReadWriteT>* mtx,
126 std::string& data,
127 PredicateT& predicate)
128{
129 if (mtx->width == 1)
130 {
131 return new one_column__dumper<T, ReadWriteT, PredicateT>(mtx, data, predicate);
132 }
133 else
134 {
135 return new multiple_columns__dumper<T, ReadWriteT, PredicateT>(mtx, data, predicate);
136 }
137}
138
139template<class T, class ReadWriteT, class PredicateT>
140void I_mtx_to_buffer_dumper<T, ReadWriteT, PredicateT>::set_print_format(bool isDecimal,
141 uint precision)
142{
143 // Determining the string format to use according the given precision
144 format_ = "%.0f";
145
146 if (isDecimal and precision)
147 {
148 const char* const sfmt[] = {
149 "%.0f",
150 "%.1f",
151 "%.2f",
152 "%.3f",
153 "%.4f",
154 "%.5f",
155 "%.6f",
156 "%.7f",
157 "%.8f",
158 "%.9f",
159 "%.10f",
160 "%.11f",
161 "%.12f",
162 "%.13f",
163 "%.14f",
164 "%.15f",
165 "%.16f",
166 };
167 assert(precision <= 16);
168 format_ = sfmt[precision];
169 }
170}
171
172template<class T, class ReadWriteT, class PredicateT>
173void one_column__dumper<T, ReadWriteT, PredicateT>::run()
174{
175 for (uint y = 0; y != (this->mtx_)->height; ++y)
176 {
177 MatrixScalar<ReadWriteT>::Append(this->buffer_,
178 (ReadWriteT)this->predicate_((this->mtx_)->entry[0][y]),
179 this->format_);
180 this->buffer_ += '\n';
181 }
182}
183
184template<class T, class ReadWriteT, class PredicateT>
185void multiple_columns__dumper<T, ReadWriteT, PredicateT>::run()
186{
187 for (uint y = 0; y < (this->mtx_)->height; ++y)
188 {
189 MatrixScalar<ReadWriteT>::Append(this->buffer_,
190 (ReadWriteT)this->predicate_((this->mtx_)->entry[0][y]),
191 this->format_);
192 for (uint x = 1; x < (this->mtx_)->width; ++x)
193 {
194 this->buffer_ += '\t';
195 MatrixScalar<ReadWriteT>::Append(this->buffer_,
196 (ReadWriteT)this->predicate_(
197 (this->mtx_)->entry[x][y]),
198 this->format_);
199 }
200 this->buffer_ += '\n';
201 }
202}
203
204} // namespace Antares
205
206#undef ANTARES_MATRIX_SNPRINTF
207
208#endif // __ANTARES_LIBS_ARRAY_MATRIX_TO_BUFFER_SENDER_HXX__