GCC Code Coverage Report


Directory: ./
File: DegreeOfFreedom/csrmatrixpattern.hpp
Date: 2024-04-14 07:32:34
Exec Total Coverage
Lines: 27 43 62.8%
Branches: 2 12 16.7%

Line Branch Exec Source
1 // ______ ______ _ _ _____ ______
2 // | ____| ____| | (_)/ ____| | ____|
3 // | |__ | |__ | | _| (___ ___| |__
4 // | __| | __| | | | |\___ \ / __| __|
5 // | | | |____| |____| |____) | (__| |____
6 // |_| |______|______|_|_____/ \___|______|
7 // Finite Elements for Life Sciences and Engineering
8 //
9 // License: LGL2.1 License
10 // FELiScE default license: LICENSE in root folder
11 //
12 // Main authors: Cedric Doucet
13 //
14
15 #ifndef CSRMATRIXPATTERN_HPP
16 #define CSRMATRIXPATTERN_HPP
17
18 // System includes
19 #include <vector>
20
21 // External includes
22
23 // Project includes
24 #include "Core/felisce.hpp"
25
26 namespace felisce {
27
28 /// \class CSRMatrixPattern
29 /// \brief Sparse matrix pattern in Compressed Sparse Row (CSR) storage
30 class CSRMatrixPattern {
31
32 public:
33
34 /**
35 * @brief Default constructor
36 */
37 1638 CSRMatrixPattern() = default;
38
39 /**
40 * @brief Constructor
41 * @param[in] numRows number of rows
42 * @param[in] numNonzeros number of nonzeros
43 */
44 CSRMatrixPattern(
45 std::size_t numRows,
46 std::size_t numNonzeros
47 ): m_rowptr(numRows+1,0),
48 m_colind(numNonzeros,0)
49 {
50 }
51
52 /// \brief Index of first nonzero in a row
53 /// \param[in] rowIndex index of a row
54 /// \return index of first nonzero in row of index rowIndex
55 70679459 inline felInt & rowPointer(felInt rowIndex) {
56 #ifdef NDEBUG
57 return m_rowptr[rowIndex];
58 #else
59 70679459 return m_rowptr.at(rowIndex);
60 #endif
61 }
62
63 /// \brief Index of first nonzero in a row
64 /// \param[in] rowIndex index of a row
65 /// \return index of first nonzero in row of index rowIndex
66 inline felInt const & rowPointer(felInt rowIndex) const {
67 #ifdef NDEBUG
68 return m_rowptr[rowIndex];
69 #else
70 return m_rowptr.at(rowIndex);
71 #endif
72 }
73
74 /// \brief Number of rows
75 /// \return number of rows
76 614 inline std::size_t numRows() const {
77 614 return m_rowptr.size()-1;
78 }
79
80 /// \brief Number of nonzeros
81 /// \return number of nonzeros
82 584 inline std::size_t numNonzeros() const {
83 584 return m_colind.size();
84 }
85
86 /// \brief Number of nonzeros in a row
87 /// \param[in] rowIndex index of a row
88 /// \return number of nonzeros in row of index rowIndex
89 222982 inline felInt numNonzerosInRow(std::size_t rowIndex) const {
90 #ifdef NDEBUG
91 return m_rowptr[rowIndex+1] - m_rowptr[rowIndex];
92 #else
93 222982 return m_rowptr.at(rowIndex+1) - m_rowptr.at(rowIndex);
94 #endif
95 }
96
97 /// \brief Column index of a nonzero
98 /// \param[in] nonzeroIndex index of a nonzero
99 inline felInt const & columnIndex(std::size_t nonzeroIndex) const {
100 #ifdef NDEBUG
101 return m_colind[nonzeroIndex];
102 #else
103 return m_colind.at(nonzeroIndex);
104 #endif
105 }
106
107 /// \brief Column index of a nonzero
108 /// \param[in] nonzeroIndex index of a nonzero
109 67077319 inline felInt & columnIndex(std::size_t nonzeroIndex) {
110 #ifdef NDEBUG
111 return m_colind[nonzeroIndex];
112 #else
113 67077319 return m_colind.at(nonzeroIndex);
114 #endif
115 }
116
117 /// \brief Resizing
118 /// \param[in] numRows number of rows
119 /// \param[in] numNonzeros number of nonzeros
120 1071 inline void resize(std::size_t numRows,std::size_t numNonzeros) {
121
1/2
✓ Branch 1 taken 1071 times.
✗ Branch 2 not taken.
1071 m_rowptr.resize(numRows+1,0);
122
1/2
✓ Branch 1 taken 1071 times.
✗ Branch 2 not taken.
1071 m_colind.resize(numNonzeros,0);
123 1071 }
124
125 /// \brief clear
126 557 inline void clear() {
127 557 m_rowptr.clear();
128 557 m_colind.clear();
129 557 }
130
131 /// \param[in] rowPointers This std::vector contains row pointers.
132 /// Its size equals numRows+1: the number of rows (numRows) plus one.
133 /// The numRows first entries contain indices of first nonzero in each row.
134 /// The last entry contains the number of nonzeros.
135 //
136 /// \param[in] columnIndices This std::vector contains column indices of nonzeros.
137 /// Is size equals the number of nonzeros.
138 /// Each entry contain the column index of the corresponding nonzero.
139 /// \brief Modify pattern's content
140 84 void set(std::vector<felInt> const & rowPointers,std::vector<felInt> const & columnIndices) {
141 84 m_rowptr = rowPointers;
142 84 m_colind = columnIndices;
143 84 }
144
145 /// \brief Accessor to column indices of nonzeros.
146 /// \return Vector of column indices of nonzeros
147 500 std::vector<felInt> const & columnIndices() const {
148 500 return m_colind;
149 }
150
151
152 /// \brief Accessor to rowPointer.
153 /// \return Vector rowPointer
154 1006 std::vector<felInt> const & rowPointer() const {
155 1006 return m_rowptr;
156 }
157
158
159 /// \brief print the pattern
160 void print(int verbose, std::ostream& outstr) const {
161 IGNORE_UNUSED_ARGUMENT(verbose);
162 outstr << "\nidNode | iCSR[idNode] | jCSR[iCSR[idNode]], ..., jCSR[iCSR[idNode+1]-1]\n";
163 for (unsigned int iNode = 0; iNode < m_rowptr.size()-1; ++iNode) {
164 outstr << iNode << " | " << m_rowptr[iNode] << " | ";
165 for(felInt iDof=m_rowptr[iNode]; iDof<m_rowptr[iNode+1]; ++iDof)
166 outstr << m_colind[iDof] << " ";
167 outstr << "\n";
168 }
169 outstr << m_rowptr.size()-1 << " | " << m_rowptr[m_rowptr.size()-1];
170 outstr << std::endl << std::endl;
171 }
172
173 private:
174
175 /// \brief sparse row pointers (index of first nonzero in each row)
176 std::vector<felInt> m_rowptr;
177
178 /// \brief column indices of nonzeros
179 std::vector<felInt> m_colind;
180 };
181
182 }
183
184 #endif // CSRMATRIXPATTERN_HPP
185