AlgebraicMultigrid 0.1
C++ algebraic multigrid.
Loading...
Searching...
No Matches
interpolator.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <Eigen/Sparse>
4#include <vector>
5
6namespace AMG {
7
15template <class EleType>
17 private:
18 std::vector<Eigen::SparseMatrix<EleType>> level_to_P;
19 std::vector<Eigen::SparseMatrix<EleType>> level_to_R;
20
21 public:
22 InterpolatorBase(size_t n_levels) {
23 // only need operators for levels 0 to n_levels-1
24 level_to_P.resize(n_levels - 1);
25 level_to_R.resize(n_levels - 1);
26 }
27
35
43 virtual void make_operators(size_t n_h_dofs, size_t n_H_dofs,
44 size_t level) = 0;
45
52 Eigen::Matrix<EleType, -1, 1> prolongation(
53 const Eigen::Matrix<EleType, -1, 1>& v, size_t level) {
54 Eigen::Matrix<EleType, -1, 1> result = get_P(level) * v;
55 return result;
56 }
57
64 Eigen::Matrix<EleType, -1, 1> restriction(
65 const Eigen::Matrix<EleType, -1, 1>& v, size_t level) {
66 Eigen::Matrix<EleType, -1, 1> result = get_R(level) * v;
67 return result;
68 }
69
70 const Eigen::SparseMatrix<EleType>& get_P(size_t level) const {
71 return level_to_P[level];
72 }
73
74 const Eigen::SparseMatrix<EleType>& get_R(size_t level) const {
75 return level_to_R[level];
76 }
77
78 void set_level_to_P(size_t level, Eigen::SparseMatrix<EleType>& P) {
79 level_to_P[level] = P;
80 return;
81 }
82
83 void set_level_to_R(size_t level, Eigen::SparseMatrix<EleType>& R) {
84 level_to_R[level] = R;
85 return;
86 }
87};
88
98template <class EleType>
99class LinearInterpolator : public InterpolatorBase<EleType> {
100 private:
101 const size_t n_elements_per_columns = 3;
102
103 public:
105
106 void make_operators(size_t n_h_dofs, size_t n_H_dofs, size_t level) {
107 // Create prolongation matrix
108 size_t nnz = n_H_dofs * n_elements_per_columns;
109 Eigen::SparseMatrix<EleType> P(n_h_dofs, n_H_dofs);
110 P.reserve(nnz);
111
112 // Populate nonzeros in matrix and bounds check the rows
113 // TODO: Do these bound checks disrupt algorithm correctness?
114 std::vector<Eigen::Triplet<EleType>> P_coefficients;
115 P_coefficients.reserve(nnz);
116 size_t i = 0;
117 for (size_t j = 0; j < n_H_dofs; ++j) {
118 if (i < n_h_dofs)
119 P_coefficients.push_back(Eigen::Triplet<EleType>(i, j, 0.5));
120
121 if (i + 1 < n_h_dofs)
122 P_coefficients.push_back(Eigen::Triplet<EleType>(i + 1, j, 1.0));
123
124 if (i + 2 < n_h_dofs)
125 P_coefficients.push_back(Eigen::Triplet<EleType>(i + 2, j, 0.5));
126
127 i += n_elements_per_columns - 1;
128 }
129 P.setFromTriplets(P_coefficients.begin(), P_coefficients.end());
130
131 // Restriction matrix follows from prolongation matrix
132 Eigen::SparseMatrix<EleType> R(n_H_dofs, n_h_dofs);
133 R.reserve(P.nonZeros());
134 R = P.transpose();
135
136 // Update the maps
137 this->set_level_to_P(level, P);
138 this->set_level_to_R(level, R);
139
140 return;
141 }
142};
143
144} // end namespace AMG
Base class for interpolators that implement prolongation and restriction as linear operators construc...
Definition interpolator.hpp:16
void set_level_to_P(size_t level, Eigen::SparseMatrix< EleType > &P)
Definition interpolator.hpp:78
InterpolatorBase(size_t n_levels)
Definition interpolator.hpp:22
virtual void make_operators(size_t n_h_dofs, size_t n_H_dofs, size_t level)=0
Construct P and R matrices based on dofs and level information.
std::vector< Eigen::SparseMatrix< EleType > > level_to_P
Definition interpolator.hpp:18
std::vector< Eigen::SparseMatrix< EleType > > level_to_R
Definition interpolator.hpp:19
const Eigen::SparseMatrix< EleType > & get_R(size_t level) const
Definition interpolator.hpp:74
void set_level_to_R(size_t level, Eigen::SparseMatrix< EleType > &R)
Definition interpolator.hpp:83
Eigen::Matrix< EleType, -1, 1 > restriction(const Eigen::Matrix< EleType, -1, 1 > &v, size_t level)
Restriction operator on v.
Definition interpolator.hpp:64
const Eigen::SparseMatrix< EleType > & get_P(size_t level) const
Definition interpolator.hpp:70
Eigen::Matrix< EleType, -1, 1 > prolongation(const Eigen::Matrix< EleType, -1, 1 > &v, size_t level)
Prolongation operator on v and updating result inplace.
Definition interpolator.hpp:52
InterpolatorBase()
Construct a new Interpolator Base object.
Definition interpolator.hpp:34
Interface for linear interpolation.
Definition interpolator.hpp:99
const size_t n_elements_per_columns
Definition interpolator.hpp:101
void make_operators(size_t n_h_dofs, size_t n_H_dofs, size_t level)
Construct P and R matrices based on dofs and level information.
Definition interpolator.hpp:106
Definition common.hpp:6