LehrFEM++ 1.0.0
A simple Finite Element Library for teaching
Loading...
Searching...
No Matches
coomatrix.h
1#ifndef INCG_LF_COOMATRIX_H
2#define INCG_LF_COOMATRIX_H
3/***************************************************************************
4 * LehrFEM++ - A simple C++ finite element libray for teaching
5 * Developed from 2018 at the Seminar of Applied Mathematics of ETH Zurich,
6 * lead developers Dr. R. Casagrande and Prof. R. Hiptmair
7 ***************************************************************************/
8
17#include "assembly_types.h"
18// clang-format off
19#include <Eigen/Sparse> // must be included after assembly_types.h
20// clang-format on
21
22namespace lf::assemble {
51template <typename SCALAR>
52class COOMatrix {
53 public:
54 using Scalar = SCALAR;
55 using Triplet = Eigen::Triplet<SCALAR>;
56 using TripletVec = std::vector<Triplet>;
57 using Index = Eigen::Index;
58
62
63 COOMatrix(const COOMatrix &) = default;
68
70 [[nodiscard]] Index rows() const { return rows_; }
72 [[nodiscard]] Index cols() const { return cols_; }
88 rows_ = (i + 1 > rows_) ? i + 1 : rows_;
89 cols_ = (j + 1 > cols_) ? j + 1 : cols_;
90 triplets_.push_back(Eigen::Triplet<SCALAR>(i, j, increment));
91 }
98 void setZero() { triplets_.clear(); }
108 template <typename PREDICATE>
110 auto new_last = std::remove_if(
111 triplets_.begin(), triplets_.end(),
112 [pred](Triplet &trp) { return (pred(trp.row(), trp.col())); });
113 // Adjust size of triplet vector
114 triplets_.erase(new_last, triplets_.end());
115 }
116
125 [[nodiscard]] const TripletVec &triplets() const { return triplets_; }
126
140 template <typename VECTOR>
141 [[nodiscard]] Eigen::Matrix<SCALAR, Eigen::Dynamic, 1> MatVecMult(
142 SCALAR alpha, const VECTOR &vec) const;
143
162 template <typename VECTOR, typename RESULTVECTOR>
163 void MatVecMult(SCALAR alpha, const VECTOR &vec, RESULTVECTOR &resvec) const;
164
172 [[nodiscard]] Eigen::SparseMatrix<Scalar> makeSparse() const {
173 Eigen::SparseMatrix<Scalar> result;
174 LF_VERIFY_MSG(
175 rows_ > 0 && cols_ > 0,
176 "matrix has zero rows or columns, this is probably an error.");
177 result.resize(rows_, cols_);
178 result.setFromTriplets(triplets_.cbegin(), triplets_.cend());
179 return result;
180 }
187 [[nodiscard]] Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>
188 makeDense() const {
189 Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> mat(rows_, cols_);
190 mat.setZero();
191 for (const Triplet &trp : triplets_) {
192 LF_ASSERT_MSG(
193 ((trp.col() < cols_) && (trp.row() < rows_)),
194 "Illegal triplet index (" << trp.col() << ',' << trp.row() << ")");
195 mat(trp.row(), trp.col()) += trp.value();
196 }
197 return mat;
198 }
199
205 void PrintInfo(std::ostream &o) const {
206 o << rows_ << " x " << cols_ << " COO matrix" << '\n';
207 for (const Triplet &trp : triplets_) {
208 o << "(" << trp.row() << ',' << trp.col() << ") -> " << trp.value()
209 << std::endl;
210 }
211 }
212
218 template <typename SCALARTYPE>
219 friend std::ostream &operator<<(std::ostream &o, // NOLINT
221
222 private:
225};
226
227// Implementation of output operator
228template <typename SCALARTYPE>
229std::ostream &operator<<(std::ostream &o, const COOMatrix<SCALARTYPE> &mat) {
230 mat.PrintInfo();
231 return o;
232}
233
234template <typename SCALAR>
235template <typename VECTOR>
236Eigen::Matrix<SCALAR, Eigen::Dynamic, 1> COOMatrix<SCALAR>::MatVecMult(
237 SCALAR alpha, const VECTOR &vec) const {
238 LF_ASSERT_MSG(vec.size() >= cols_,
239 "size mismatch: " << cols_ << " <-> " << vec.size());
240 Eigen::VectorXd result(rows_);
241 result.setZero();
242 for (const Triplet &trp : triplets_) {
243 result[trp.row()] += trp.value() * (alpha * vec[trp.col()]);
244 }
245 return result;
246}
247
248template <typename SCALAR>
249template <typename VECTOR, typename RESULTVECTOR>
251 RESULTVECTOR &resvec) const {
252 LF_ASSERT_MSG(vec.size() >= cols_,
253 "Vector vec size mismatch: " << cols_ << " <-> " << vec.size());
254 LF_ASSERT_MSG(
255 resvec.size() >= rows_,
256 "Vector result size mismatch: " << cols_ << " <-> " << resvec.size());
257 for (const Triplet &trp : triplets_) {
258 resvec[trp.row()] += trp.value() * (alpha * vec[trp.col()]);
259 }
260}
261
262} // namespace lf::assemble
263
264
266
269template<class SCALAR>
270struct fmt::formatter<lf::assemble::COOMatrix<SCALAR>> : ostream_formatter {};
272
273#endif
A temporary data structure for matrix in COO format.
Definition coomatrix.h:52
void PrintInfo(std::ostream &o) const
Ouput of triplet list describing COO matrix.
Definition coomatrix.h:205
void setZero()
Erase all entries of the matrix.
Definition coomatrix.h:98
Index cols() const
return number of column
Definition coomatrix.h:72
void setZero(PREDICATE &&pred)
Erase specific entries of the COO matrix, that is, set them to zero.
Definition coomatrix.h:109
Eigen::Matrix< SCALAR, Eigen::Dynamic, 1 > MatVecMult(SCALAR alpha, const VECTOR &vec) const
Computes the product of a (scaled) vector with the matrix in COO format.
Definition coomatrix.h:236
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > makeDense() const
Create an Eigen::MatrixX from the COO format.
Definition coomatrix.h:188
COOMatrix(COOMatrix &&) noexcept=default
const TripletVec & triplets() const
Definition coomatrix.h:125
std::vector< Triplet > TripletVec
Definition coomatrix.h:56
Eigen::SparseMatrix< Scalar > makeSparse() const
Create an Eigen::SparseMatrix out of the COO format.
Definition coomatrix.h:172
Eigen::Triplet< SCALAR > Triplet
Definition coomatrix.h:55
COOMatrix(const COOMatrix &)=default
COOMatrix(size_type num_rows, size_type num_cols)
Definition coomatrix.h:60
TripletVec & triplets()
Gives access to the vector of triplets.
Definition coomatrix.h:124
Index rows() const
return number of rows
Definition coomatrix.h:70
void AddToEntry(gdof_idx_t i, gdof_idx_t j, SCALAR increment)
Add a value to the specified entry.
Definition coomatrix.h:87
friend std::ostream & operator<<(std::ostream &o, const COOMatrix< SCALARTYPE > &mat)
Output operator for COO matrix.
Definition coomatrix.h:229
void AssembleMatrixLocally(dim_t codim, const DofHandler &dof_handler_trial, const DofHandler &dof_handler_test, ENTITY_MATRIX_PROVIDER &entity_matrix_provider, TMPMATRIX &matrix)
Assembly function for standard assembly of finite element matrices.
Definition assembler.h:115
D.o.f. index mapping and assembly facilities.
Definition assemble.h:31
lf::base::size_type size_type
std::ostream & operator<<(std::ostream &o, const COOMatrix< SCALARTYPE > &mat)
Definition coomatrix.h:229
Eigen::Index gdof_idx_t
Definition assemble.h:31