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
59 {
60 XASSERTM(num_rows <= Index(max_size), "User tried to generate a Matrix with more than 100000 num_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
84 {
85 return _num_col;
86 }
93 {
94 return _num_row;
95 }
96
103 {
104 return Index(memory.size());
105 }
106
113 {
114 //Creates CSR matrix with dimensions and number of NNZ of map.
116 // pointer on the empty arrays of the CSR matrix structure
117 Memory::TypedView<IT_> row_ptr = matrix.row_ptr_view_w();
118 Memory::TypedView<IT_> col_idx = matrix.col_idx_view_w();
119 Memory::TypedView<DT_> val = matrix.val_view_w();
120
121 row_ptr[0] = IT_(0);
122 // numberNotZero elements
123 IT_ _nnz = IT_(0);
124 // current row count
125 IT_ _row_cur = IT_(0);
126
127 //Iterates over the map to extract the data and paste in the corresponding arrays.
128 //typename std::map< IT_, DT_>::const_iterator it = memory.begin();
129 for(auto it = memory.begin(); it != memory.end();++it)
130 {
131 // extract column and row from the map key
132 IT_ col = it->first % max_size;
133 IT_ row = (it->first - col) / max_size;
134 // make sure the row pointer is valid even if there exist preceding empty rows in the matrix
135 while (_row_cur <row )
136 {
137 row_ptr[++_row_cur] = _nnz;
138 }
139 val[_nnz] = it->second;
140 col_idx[_nnz] = col;
141 ++_nnz;
142 }
143 // make sure that the row pointer is valid even if there are empty rows at the end of the matrix
144 while (_row_cur < _num_row)
145 {
146 row_ptr[++_row_cur] = _nnz;
147 }
148
149 /*// For testing Purposes: Printing the three arrays
150 std::cout<<"Values: ";
151 for (IT_ i = 0; i < _nzv; ++i)
152 {
153 std::cout << val[i] << " ";
154 }
155 std::cout << " \nColum Indizes:" ;
156
157 for (IT_ i = 0; i < _nzv; ++i)
158 {
159 std::cout << col_idx[i] << " ";
160 }
161 std::cout << " \nRow Pointers:";
162 for (IT_ i = 0; i < _num_row + 1; ++i)
163 {
164 std::cout << row_ptr[i] << " ";
165 }
166 std::cout << " \n";
167 */
168 return matrix;
169 }
170 }; // class SparseMatrixFactory
171 } // namespace LAFEM
172} // 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.
Factory for SparseMatrix construction.
Index num_cols() const
Returns number of columns.
Index num_nzes() const
Returns number of non-zero entries.
SparseMatrixCSR< DT_, IT_ > make_csr() const
Returns CSR matrix constructed from Map.
SparseMatrixFactory(Index num_rows, Index num_cols)
Constructor.
Index num_rows() const
Returns number of rows.
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 _num_row
Number of rows for the created matrix.
Typed memory view class.
FEAT namespace.
Definition: adjactor.hpp:12
std::uint64_t Index
Index data type.