PolyFEM
Loading...
Searching...
No Matches
MatrixCache.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <Eigen/Dense>
6#include <Eigen/Sparse>
7
8#include <memory>
9
10namespace polyfem::utils
11{
14 {
15 public:
17 virtual ~MatrixCache() = default;
18
19 virtual std::unique_ptr<MatrixCache> copy() const = 0;
20
21 virtual void init(const size_t size) = 0;
22 virtual void init(const size_t rows, const size_t cols) = 0;
23 virtual void init(const MatrixCache &other) = 0;
24
25 virtual void set_zero() = 0;
26
27 virtual void reserve(const size_t size) = 0;
28 virtual size_t entries_size() const = 0;
29 virtual size_t capacity() const = 0;
30 virtual size_t non_zeros() const = 0;
31 virtual size_t triplet_count() const = 0;
32 virtual bool is_sparse() const = 0;
33 bool is_dense() const { return !is_sparse(); }
34
35 virtual void add_value(const int e, const int i, const int j, const double value) = 0;
36 virtual StiffnessMatrix get_matrix(const bool compute_mapping = true) = 0;
37 virtual void prune() = 0;
38
39 virtual std::shared_ptr<MatrixCache> operator+(const MatrixCache &a) const = 0;
40 virtual void operator+=(const MatrixCache &o) = 0;
41 };
42
44 {
45 public:
46 // constructors (call init functions below)
48 SparseMatrixCache(const size_t size);
49 SparseMatrixCache(const size_t rows, const size_t cols);
50 SparseMatrixCache(const MatrixCache &other);
51 SparseMatrixCache(const SparseMatrixCache &other, const bool copy_main_cache_ptr = false);
52
53 inline std::unique_ptr<MatrixCache> copy() const override
54 {
55 // just copy main cache pointer
56 return std::make_unique<SparseMatrixCache>(*this, true);
57 }
58
60 void init(const size_t size) override;
62 void init(const size_t rows, const size_t cols) override;
64 void init(const MatrixCache &other) override;
66 void init(const SparseMatrixCache &other, const bool copy_main_cache_ptr = false);
67
70 void set_zero() override;
71
72 inline void reserve(const size_t size) override { entries_.reserve(size); }
73 inline size_t entries_size() const override { return entries_.size(); }
74 inline size_t capacity() const override { return entries_.capacity(); }
75 inline size_t non_zeros() const override { return mapping_.empty() ? mat_.nonZeros() : values_.size(); }
76 inline size_t triplet_count() const override { return entries_.size() + mat_.nonZeros(); }
77 inline bool is_sparse() const override { return true; }
78 inline size_t mapping_size() const { return mapping_.size(); }
79
85 void add_value(const int e, const int i, const int j, const double value) override;
93 StiffnessMatrix get_matrix(const bool compute_mapping = true) override;
96 void prune() override;
97
98 std::shared_ptr<MatrixCache> operator+(const MatrixCache &a) const override;
99 std::shared_ptr<MatrixCache> operator+(const SparseMatrixCache &a) const;
100 void operator+=(const MatrixCache &o) override;
101 void operator+=(const SparseMatrixCache &o);
102
103 const StiffnessMatrix &mat() const { return mat_; }
104 const std::vector<Eigen::Triplet<double>> &entries() const { return entries_; }
105
106 private:
107 size_t size_;
109 std::vector<Eigen::Triplet<double>> entries_;
110 std::vector<std::vector<std::pair<int, size_t>>> mapping_;
111 std::vector<int> inner_index_, outer_index_;
112 std::vector<double> values_;
114
115 std::vector<std::vector<int>> second_cache_;
116 std::vector<std::vector<std::pair<int, int>>> second_cache_entries_;
117 int current_e_ = -1;
119
120 inline const SparseMatrixCache *main_cache() const
121 {
122 return main_cache_ == nullptr ? this : main_cache_;
123 }
124
125 inline const std::vector<std::vector<std::pair<int, size_t>>> &mapping() const
126 {
127 return main_cache()->mapping_;
128 }
129
130 inline const std::vector<std::vector<int>> &second_cache() const
131 {
132 return main_cache()->second_cache_;
133 }
134 };
135
137 {
138 public:
140 DenseMatrixCache(const size_t size);
141 DenseMatrixCache(const size_t rows, const size_t cols);
142 DenseMatrixCache(const MatrixCache &other);
144
145 inline std::unique_ptr<MatrixCache> copy() const override
146 {
147 return std::make_unique<DenseMatrixCache>(*this);
148 }
149
150 void init(const size_t size) override;
151 void init(const size_t rows, const size_t cols) override;
152 void init(const MatrixCache &other) override;
153 void init(const DenseMatrixCache &other);
154
155 void set_zero() override;
156
157 inline void reserve(const size_t size) override {}
158 inline size_t entries_size() const override { return 0; }
159 inline size_t capacity() const override { return mat_.size(); }
160 inline size_t non_zeros() const override { return mat_.size(); }
161 inline size_t triplet_count() const override { return non_zeros(); }
162 inline bool is_sparse() const override { return false; }
163
164 void add_value(const int e, const int i, const int j, const double value) override;
165 StiffnessMatrix get_matrix(const bool compute_mapping = true) override;
166 void prune() override;
167
168 std::shared_ptr<MatrixCache> operator+(const MatrixCache &a) const override;
169 std::shared_ptr<MatrixCache> operator+(const DenseMatrixCache &a) const;
170 void operator+=(const MatrixCache &o) override;
171 void operator+=(const DenseMatrixCache &o);
172
173 const Eigen::MatrixXd &mat() const { return mat_; }
174
175 private:
176 Eigen::MatrixXd mat_;
177 };
178} // namespace polyfem::utils
size_t entries_size() const override
std::shared_ptr< MatrixCache > operator+(const MatrixCache &a) const override
size_t non_zeros() const override
const Eigen::MatrixXd & mat() const
void init(const size_t size) override
StiffnessMatrix get_matrix(const bool compute_mapping=true) override
size_t capacity() const override
void add_value(const int e, const int i, const int j, const double value) override
bool is_sparse() const override
size_t triplet_count() const override
std::unique_ptr< MatrixCache > copy() const override
void reserve(const size_t size) override
void operator+=(const MatrixCache &o) override
abstract class used for caching
virtual void add_value(const int e, const int i, const int j, const double value)=0
virtual size_t triplet_count() const =0
virtual ~MatrixCache()=default
virtual void init(const MatrixCache &other)=0
virtual size_t entries_size() const =0
virtual void operator+=(const MatrixCache &o)=0
virtual void reserve(const size_t size)=0
virtual void set_zero()=0
virtual StiffnessMatrix get_matrix(const bool compute_mapping=true)=0
virtual void init(const size_t rows, const size_t cols)=0
virtual bool is_sparse() const =0
virtual std::shared_ptr< MatrixCache > operator+(const MatrixCache &a) const =0
virtual size_t capacity() const =0
virtual size_t non_zeros() const =0
virtual std::unique_ptr< MatrixCache > copy() const =0
virtual void init(const size_t size)=0
void operator+=(const MatrixCache &o) override
void add_value(const int e, const int i, const int j, const double value) override
e = element_index, i = global row_index, j = global column_index, value = value to add to matrix if t...
void set_zero() override
set matrix values to zero modifies tmp_, mat_, and values (setting all to zero)
size_t non_zeros() const override
size_t triplet_count() const override
std::vector< int > outer_index_
saves inner/outer indices for sparse matrix
void prune() override
if caches have yet to be constructed, add the saved triplets to mat_ modifies tmp_ and mat_,...
const SparseMatrixCache * main_cache_
size_t capacity() const override
std::shared_ptr< MatrixCache > operator+(const MatrixCache &a) const override
const std::vector< std::vector< std::pair< int, size_t > > > & mapping() const
std::vector< std::vector< std::pair< int, int > > > second_cache_entries_
maps element indices to global matrix indices
size_t entries_size() const override
const SparseMatrixCache * main_cache() const
bool is_sparse() const override
std::vector< double > values_
buffer for values (corresponds to inner/outer_index_ structure for sparse matrix)
const StiffnessMatrix & mat() const
const std::vector< std::vector< int > > & second_cache() const
std::vector< Eigen::Triplet< double > > entries_
contains global matrix indices and corresponding value
StiffnessMatrix get_matrix(const bool compute_mapping=true) override
if the cache is yet to be constructed, save the cached (ordered) indices in inner_index_ and outer_in...
void reserve(const size_t size) override
const std::vector< Eigen::Triplet< double > > & entries() const
void init(const size_t size) override
set matrix to be size x size
std::unique_ptr< MatrixCache > copy() const override
std::vector< std::vector< int > > second_cache_
maps element index to local index
std::vector< std::vector< std::pair< int, size_t > > > mapping_
maps row indices to column index/local index pairs
Eigen::SparseMatrix< double, Eigen::ColMajor > StiffnessMatrix
Definition Types.hpp:20