FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
sparse_matrix_factory.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
8// includes, FEAT
10#include <kernel/lafem/sparse_matrix_csr.hpp>
12
13// includes, system
14#include <map>
15#include <iterator>
16
17
18namespace FEAT
19{
20 namespace LAFEM
21 {
36 template<typename DT_, typename IT_>
38 {
39 private:
45 static constexpr IT_ max_size = IT_(100000u);
47 std::map<IT_, DT_> memory;
48
49 public:
50
57 explicit SparseMatrixFactory(Index num_rows, Index num_cols) :
58 _num_row(num_rows), _num_col(num_cols)
59 {
60 XASSERTM(num_rows <= Index(max_size), "User tried to generate a Matrix with more than 100000 rows!");
61 XASSERTM(num_cols <= Index(max_size), "User tried to generate a Matrix with more than 100000 colums!");
62 }
63
71 void add(Index i, Index j, DT_ a_ij)
72 {
73 ASSERTM(i < _num_row, "User tried to add input out of bounds of the matrix" );
74 ASSERTM(j < _num_col, "User tried to add input out of bounds of the matrix");
75 memory.emplace(IT_(i) * max_size + IT_(j), a_ij);
76 }
77
83 Index columns() const
84 {
85 return _num_col;
86 }
92 Index rows() const
93 {
94 return _num_row;
95 }
101 Index size() const
102 {
103 return _num_col*_num_row;
104 }
105
112 {
113 return Index(memory.size());
114 }
115
122 {
123 //Creates CSR matrix with dimensions and number of NNZ of map.
125 // pointer on the empty arrays of the CSR matrix structure
126 IT_* row_ptr = matrix.row_ptr();
127 IT_* col_ind = matrix.col_ind();
128 DT_* val = matrix.val();
129
130 row_ptr[0] = IT_(0);
131 // numberNotZero elements
132 IT_ _nnz = IT_(0);
133 // current row count
134 IT_ _row_cur = IT_(0);
135
136 //Iterates over the map to extract the data and paste in the corresponding arrays.
137 //typename std::map< IT_, DT_>::const_iterator it = memory.begin();
138 for(auto it = memory.begin(); it != memory.end();++it)
139 {
140 // extract column and row from the map key
141 IT_ col = it->first % max_size;
142 IT_ row = (it->first - col) / max_size;
143 // make sure the row pointer is valid even if there exist preceeding empty rows in the matrix
144 while (_row_cur <row )
145 {
146 row_ptr[++_row_cur] = _nnz;
147 }
148 val[_nnz] = it->second;
149 col_ind[_nnz] = col;
150 ++_nnz;
151 }
152 // make sure that the row pointer is valid even if there are empty rows at the end of the matrix
153 while (_row_cur < _num_row)
154 {
155 row_ptr[++_row_cur] = _nnz;
156 }
157
158 /*// For testing Purposes: Printing the three arrays
159 std::cout<<"Values: ";
160 for (IT_ i = 0; i < _nzv; ++i)
161 {
162 std::cout << val[i] << " ";
163 }
164 std::cout << " \nColum Indizes:" ;
165
166 for (IT_ i = 0; i < _nzv; ++i)
167 {
168 std::cout << col_ind[i] << " ";
169 }
170 std::cout << " \nRow Pointers:";
171 for (IT_ i = 0; i < _num_row + 1; ++i)
172 {
173 std::cout << row_ptr[i] << " ";
174 }
175 std::cout << " \n";
176 */
177 return matrix;
178 }
179 }; // class SparseMatrixFactory
180 } // namespace LAFEM
181} // namespace FEAT
#define ASSERTM(expr, msg)
Debug-Assertion macro definition with custom message.
Definition: assertion.hpp:230
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
FEAT Kernel base header.
CSR based sparse matrix.
IT_ * col_ind()
Retrieve column indices array.
DT_ * val()
Retrieve non zero element array.
IT_ * row_ptr()
Retrieve row start index array.
Factory for SparseMatrix construction.
Index columns() const
Returns number of columns.
Index rows() const
Returns number of rows.
Index size() const
Returns product of number columns and number rows.
SparseMatrixCSR< DT_, IT_ > make_csr() const
Returns CSR matrix constructed from Map.
SparseMatrixFactory(Index num_rows, Index num_cols)
Constructor.
Index _num_col
Number of columns for the created matrix.
std::map< IT_, DT_ > memory
Map collecting the input for the matrix.
static constexpr IT_ max_size
Maximal size of matrix.
void add(Index i, Index j, DT_ a_ij)
Adds a new matrix entry to the factory.
Index used_elements() const
Returns number of used elements.
Index _num_row
Number of rows for the created matrix.
FEAT namespace.
Definition: adjactor.hpp:12
std::uint64_t Index
Index data type.