ergo
transform.h
Go to the documentation of this file.
1/* Ergo, version 3.8.2, a program for linear scaling electronic structure
2 * calculations.
3 * Copyright (C) 2023 Elias Rudberg, Emanuel H. Rubensson, Pawel Salek,
4 * and Anastasia Kruchinina.
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Primary academic reference:
20 * Ergo: An open-source program for linear-scaling electronic structure
21 * calculations,
22 * Elias Rudberg, Emanuel H. Rubensson, Pawel Salek, and Anastasia
23 * Kruchinina,
24 * SoftwareX 7, 107 (2018),
25 * <http://dx.doi.org/10.1016/j.softx.2018.03.005>
26 *
27 * For further information about Ergo, see <http://www.ergoscf.org>.
28 */
40#ifndef HEADER_TRANSFORM
41#define HEADER_TRANSFORM
42
43#include <iostream>
44#include "matrix_typedefs.h"
46
48
49
50
51template<typename TYPE1, typename TYPE2>
52inline void transform_matrix_from_to(const TYPE1& A, TYPE2& B, const ParamsType& P)
53{
54 throw std::runtime_error("Error in transform_matrix_from_to : it is not implemented for given template parameters.");
55}
56
57
58/* "FAKE" TRANSFORMATIONS */
59
60template<>
62 (const symmMatrix& A, symmMatrix& B, const ParamsType& P)
63{
64 B = A;
65}
66
67
68template<>
70 (const normalMatrix& A, normalMatrix& B, const ParamsType& P)
71{
72 B = A;
73}
74
75
76template<>
78 (const triangMatrix& A, triangMatrix& B, const ParamsType& P)
79{
80 B = A;
81}
82
83
84#ifdef USE_CHUNKS_AND_TASKS
85
86
87
88/* FROM ERGO TO CHT */
89
90
91template<typename MatrixType, typename MatrixTypeWrapper>
92inline void get_sparse_matrix_data(const MatrixType& X,
93 std::vector<int>& rows,
94 std::vector<int>& cols,
95 std::vector<real>& vals)
96{
97 throw std::runtime_error("Error in transform.h : get_sparse_matrix_data is not implemented for a given template parameters.");
98}
99
100
101template<>
102inline void get_sparse_matrix_data<symmMatrix, chtml::CHTSymmMatrix<real, ParamsType> >(const symmMatrix& X,
103 std::vector<int>& rows,
104 std::vector<int>& cols,
105 std::vector<real>& vals)
106{
107 rows.clear();
108 cols.clear();
109 vals.clear();
110 X.get_all_values(rows, cols, vals);
111
112 size_t count = 0;
113 for (size_t i = 0; i < rows.size(); ++i)
114 {
115 if (vals[i] == 0)
116 {
117 continue;
118 }
119 rows[count] = rows[i];
120 cols[count] = cols[i];
121 vals[count] = vals[i];
122 count++;
123 }
124
125 rows.resize(count);
126 cols.resize(count);
127 vals.resize(count);
128}
129
130
131template<>
132inline void get_sparse_matrix_data<symmMatrix, chtml::CHTGeneralMatrix<real, ParamsType> >(const symmMatrix& X,
133 std::vector<int>& rows,
134 std::vector<int>& cols,
135 std::vector<real>& vals)
136{
137 rows.clear();
138 cols.clear();
139 vals.clear();
140 X.get_all_values(rows, cols, vals);
141
142 size_t count = 0;
143 for (size_t i = 0; i < rows.size(); ++i)
144 {
145 if (vals[i] == 0)
146 {
147 continue;
148 }
149 rows[count] = rows[i];
150 cols[count] = cols[i];
151 vals[count] = vals[i];
152 count++;
153 }
154
155
156 // here we have just upper triangle
157 // now set the lower triangle
158
159 rows.resize(count);
160 cols.resize(count);
161 vals.resize(count);
162 rows.reserve(count * 2);
163 cols.reserve(count * 2);
164 vals.reserve(count * 2);
165
166 size_t N = rows.size();
167 for (size_t i = 0; i < N; ++i)
168 {
169 if (rows[i] != cols[i])
170 {
171 rows.push_back(cols[i]);
172 cols.push_back(rows[i]);
173 vals.push_back(vals[i]);
174 count++;
175 }
176 }
177}
178
179
180template<>
181inline void get_sparse_matrix_data<triangMatrix, chtml::CHTTriangMatrix<real, ParamsType> >(const triangMatrix& X,
182 std::vector<int>& rows,
183 std::vector<int>& cols,
184 std::vector<real>& vals)
185{
186 rows.clear();
187 cols.clear();
188 vals.clear();
189 X.get_all_values(rows, cols, vals);
190
191 size_t count = 0;
192 for (size_t i = 0; i < rows.size(); ++i)
193 {
194 if (vals[i] == 0)
195 {
196 continue;
197 }
198 rows[count] = rows[i];
199 cols[count] = cols[i];
200 vals[count] = vals[i];
201 count++;
202 }
203
204 rows.resize(count);
205 cols.resize(count);
206 vals.resize(count);
207}
208
209
210template<>
211inline void get_sparse_matrix_data<normalMatrix, chtml::CHTGeneralMatrix<real, ParamsType> >(const normalMatrix& X,
212 std::vector<int>& rows,
213 std::vector<int>& cols,
214 std::vector<real>& vals)
215{
216 rows.clear();
217 cols.clear();
218 vals.clear();
219 X.get_all_values(rows, cols, vals);
220
221 size_t count = 0;
222 for (size_t i = 0; i < rows.size(); ++i)
223 {
224 if (vals[i] == 0)
225 {
226 continue;
227 }
228 rows[count] = rows[i];
229 cols[count] = cols[i];
230 vals[count] = vals[i];
231 count++;
232 }
233
234 rows.resize(count);
235 cols.resize(count);
236 vals.resize(count);
237}
238
239
240template<>
241inline void transform_matrix_from_to<symmMatrix, chtml::CHTSymmMatrix<real, ParamsType> >
242 (const symmMatrix& A, chtml::CHTSymmMatrix<real, ParamsType>& B, const ParamsType& P)
243{
244 B.set_matrix_params(P);
245
246 int n = A.get_nrows();
247 int m = A.get_ncols();
248 // check dim of B
249
250 std::vector<int> rows;
251 std::vector<int> cols;
252 std::vector<real> vals;
253 get_sparse_matrix_data<symmMatrix, chtml::CHTSymmMatrix<real, ParamsType> >(A, rows, cols, vals);
254 B.create_CHT_matrix_from_sparse(rows, cols, vals);
255}
256
257
258template<>
259inline void transform_matrix_from_to<symmMatrix, chtml::CHTGeneralMatrix<real, ParamsType> >
260 (const symmMatrix& A, chtml::CHTGeneralMatrix<real, ParamsType>& B, const ParamsType& P)
261{
262 B.set_matrix_params(P);
263
264 int n = A.get_nrows();
265 int m = A.get_ncols();
266 // check dim of B
267
268 std::vector<int> rows;
269 std::vector<int> cols;
270 std::vector<real> vals;
271 get_sparse_matrix_data<symmMatrix, chtml::CHTGeneralMatrix<real, ParamsType> >(A, rows, cols, vals);
272 B.create_CHT_matrix_from_sparse(rows, cols, vals);
273}
274
275
276template<>
277inline void transform_matrix_from_to<normalMatrix, chtml::CHTGeneralMatrix<real, ParamsType> >
278 (const normalMatrix& A, chtml::CHTGeneralMatrix<real, ParamsType>& B, const ParamsType& P)
279{
280 B.set_matrix_params(P);
281
282 int n = A.get_nrows();
283 int m = A.get_ncols();
284 // check dim of B
285
286 std::vector<int> rows;
287 std::vector<int> cols;
288 std::vector<real> vals;
289 get_sparse_matrix_data<normalMatrix, chtml::CHTGeneralMatrix<real, ParamsType> >(A, rows, cols, vals);
290 B.create_CHT_matrix_from_sparse(rows, cols, vals);
291}
292
293
294template<>
295inline void transform_matrix_from_to<triangMatrix, chtml::CHTTriangMatrix<real, ParamsType> >
296 (const triangMatrix& A, chtml::CHTTriangMatrix<real, ParamsType>& B, const ParamsType& P)
297{
298 B.set_matrix_params(P);
299
300 int n = A.get_nrows();
301 int m = A.get_ncols();
302 // check dim of B
303
304 std::vector<int> rows;
305 std::vector<int> cols;
306 std::vector<real> vals;
307 get_sparse_matrix_data<triangMatrix, chtml::CHTTriangMatrix<real, ParamsType> >(A, rows, cols, vals);
308 B.create_CHT_matrix_from_sparse(rows, cols, vals);
309}
310
311
312/* FROM CHT TO ERGO */
313
314
315template<typename MatrixTypeCHT, typename MatrixType>
316inline void set_sparse_matrix_from_data(MatrixType& X,
317 const std::vector<int>& rows,
318 const std::vector<int>& cols,
319 const std::vector<real>& vals)
320{
321 throw std::runtime_error("Error in transform_matrix_from_to : set_sparse_matrix_from_data is not implemented for a given template parameters.");
322}
323
324
325template<>
326inline void set_sparse_matrix_from_data<chtml::CHTGeneralMatrix<real, ParamsType>, symmMatrix>(symmMatrix& A,
327 const std::vector<int>& rows,
328 const std::vector<int>& cols,
329 const std::vector<real>& vals)
330{
331 // we need just upper triangle
332 std::vector<int> rows1;
333 rows1.resize(rows.size());
334 std::vector<int> cols1;
335 cols1.resize(cols.size());
336 std::vector<real> vals1;
337 vals1.resize(vals.size());
338 size_t count = 0;
339 for (size_t i = 0; i < rows.size(); ++i)
340 {
341 if (rows[i] > cols[i])
342 {
343 continue;
344 }
345 rows1[count] = rows[i];
346 cols1[count] = cols[i];
347 vals1[count] = vals[i];
348 count++;
349 }
350 rows1.resize(count);
351 cols1.resize(count);
352 vals1.resize(count);
353 A.assign_from_sparse(rows1, cols1, vals1);
354}
355
356
357template<>
358inline void set_sparse_matrix_from_data<chtml::CHTSymmMatrix<real, ParamsType>, symmMatrix>(symmMatrix& A,
359 const std::vector<int>& rows,
360 const std::vector<int>& cols,
361 const std::vector<real>& vals)
362{
363 A.assign_from_sparse(rows, cols, vals);
364}
365
366
367template<>
368inline void transform_matrix_from_to<chtml::CHTSymmMatrix<real, ParamsType>, symmMatrix>
369 (const chtml::CHTSymmMatrix<real, ParamsType>& A, symmMatrix& B, const ParamsType& P)
370{
371 int n, m, nB, mB;
372
373 std::vector<int> rows;
374 std::vector<int> cols;
375 std::vector<real> vals;
376
377 n = A.get_nrows();
378 nB = B.get_nrows();
379 assert(nB == n);
380 m = A.get_ncols();
381 mB = B.get_ncols();
382 assert(mB == m);
383 A.get_matrix(rows, cols, vals);
384 set_sparse_matrix_from_data<chtml::CHTSymmMatrix<real, ParamsType>, symmMatrix>(B, rows, cols, vals);
385}
386
387
388template<>
389inline void transform_matrix_from_to<chtml::CHTGeneralMatrix<real, ParamsType>, symmMatrix>
390 (const chtml::CHTGeneralMatrix<real, ParamsType>& A, symmMatrix& B, const ParamsType& P)
391{
392 int n, m, nB, mB;
393
394 std::vector<int> rows;
395 std::vector<int> cols;
396 std::vector<real> vals;
397
398 n = A.get_nrows();
399 nB = B.get_nrows();
400 assert(nB == n);
401 m = A.get_ncols();
402 mB = B.get_ncols();
403 assert(mB == m);
404 A.get_matrix(rows, cols, vals);
405 set_sparse_matrix_from_data<chtml::CHTGeneralMatrix<real, ParamsType>, symmMatrix>(B, rows, cols, vals);
406}
407
408
409#endif
410
411
412
413#endif // HEADER_TRANSFORM
Definition: matrix_typedefs_chtml.h:79
Symmetric matrix.
Definition: MatrixSymmetric.h:68
mat::SizesAndBlocks rows
Definition: test.cc:51
mat::SizesAndBlocks cols
Definition: test.cc:52
#define B
#define A
Header file with typedefs for matrix and vector types.
Header file with typedefs for matrix types, using either the hierarchical matrix library (HML) or the...
double ergo_real
Definition: realtype.h:69
MatrixSymmetric< real, matri > symmMatrix
Definition: test_LanczosSeveralLargestEig.cc:69
MatrixTriangular< real, matri > triangMatrix
Definition: test_LanczosSeveralLargestEig.cc:70
MatrixGeneral< real, matri > normalMatrix
Definition: test_LanczosSeveralLargestEig.cc:71
void transform_matrix_from_to< symmMatrix, symmMatrix >(const symmMatrix &A, symmMatrix &B, const ParamsType &P)
Definition: transform.h:62
void transform_matrix_from_to< normalMatrix, normalMatrix >(const normalMatrix &A, normalMatrix &B, const ParamsType &P)
Definition: transform.h:70
void transform_matrix_from_to(const TYPE1 &A, TYPE2 &B, const ParamsType &P)
Definition: transform.h:52
ergo_real real
Definition: transform.h:47
void transform_matrix_from_to< triangMatrix, triangMatrix >(const triangMatrix &A, triangMatrix &B, const ParamsType &P)
Definition: transform.h:78