AlgebraicMultigrid 0.1
C++ algebraic multigrid.
Loading...
Searching...
No Matches
grid.hpp
Go to the documentation of this file.
1// Example problem: poisson equation w/ dirichlet boundary conditions
2#pragma once
3
4#include <cmath>
5
6#include <Eigen/Core>
7#include <Eigen/Sparse>
8
9#include <unsupported/Eigen/KroneckerProduct>
10
11namespace AMG {
12
19template <class EleType>
20class Grid {
21 private:
22 static const size_t n_boundary_points = 2;
23
24 public:
31 static EleType grid_spacing_h(size_t n) { return 2.0 / (n + 1); }
32
39 static size_t points_n_from_grid_spacing_h(EleType h = 1. / 50) {
40 return static_cast<size_t>((2 / h) - 1);
41 }
42
50 static Eigen::SparseMatrix<EleType> second_order_central_difference(
51 size_t n) {
52 // Grid spacing
53 EleType h = grid_spacing_h(n);
54
55 // Unfilled difference matrix
56 Eigen::SparseMatrix<EleType> D(n, n);
57
58 // Assemble the difference matrix by direct insertion of values
59 size_t n_diagonals = 3;
60 D.reserve(Eigen::VectorXi::Constant(n, n_diagonals));
61 for (int i = 0; i < n; ++i) {
62 D.insert(i, i) = -2.0;
63 if (i > 0) { // handle first row
64 D.insert(i, i - 1) = 1.0;
65 }
66 if (i < n - 1) { // handle last row
67 D.insert(i, i + 1) = 1.0;
68 }
69 }
70
71 // Finite differences requires this division
72 D = D / (h * h);
73
74 return D;
75 }
76
88 static Eigen::SparseMatrix<EleType> laplacian(size_t n) {
89 Eigen::SparseMatrix<EleType> D = second_order_central_difference(n);
90
91 Eigen::SparseMatrix<EleType> spidentity(n, n);
92 spidentity.setIdentity();
93
94 Eigen::SparseMatrix<EleType> A = Eigen::kroneckerProduct(spidentity, D) +
95 Eigen::kroneckerProduct(D, spidentity);
96
97 return A;
98 }
99
108 static Eigen::Matrix<EleType, -1, 1> rhs(
109 size_t n,
110 std::function<EleType(EleType, EleType)> f = [](EleType x, EleType y) {
111 return 5 * exp(-10 * (x * x + y * y));
112 }) {
113
114 // Initialize default 1D domain on [-1, 1]
115 size_t n_points_in_direction = n + n_boundary_points;
116 EleType left_bound = -1.0;
117 EleType right_bound = 1.0;
118 Eigen::Matrix<EleType, -1, 1> domain_1D =
119 Eigen::DenseBase<Eigen::Matrix<EleType, -1, 1>>::LinSpaced(
120 n_points_in_direction, left_bound, right_bound);
121
122 // Initialize the rhs vector using the size of the 1D domain
123 size_t ndofs = n * n;
124 Eigen::Matrix<EleType, -1, 1> b(ndofs);
125
126 // Evaluate the function at each interior grid point, traversing grid as col major
127 size_t dof = 0;
128 EleType xj, xi, feval;
129 for (size_t j = 1; j <= n; ++j) {
130 xj = domain_1D[j];
131 for (size_t i = 1; i <= n; ++i) {
132 xi = domain_1D[i];
133 feval = f(xj, xi);
134 b[dof] = feval;
135 ++dof;
136 }
137 }
138
139 return b;
140 }
141};
142
143} // end namespace AMG
Static functions for constructing A and b, the components of a linear system Au = b.
Definition grid.hpp:20
static Eigen::SparseMatrix< EleType > second_order_central_difference(size_t n)
Return second order central difference as linear operator on 1D function.
Definition grid.hpp:50
static Eigen::Matrix< EleType, -1, 1 > rhs(size_t n, std::function< EleType(EleType, EleType)> f=[](EleType x, EleType y) { return 5 *exp(-10 *(x *x+y *y));})
Return right hand side vector b by evaluating f on a mesh grid in [-1, 1]^2.
Definition grid.hpp:108
static size_t points_n_from_grid_spacing_h(EleType h=1./50)
Compute the number of points in a direction given gridspacing h.
Definition grid.hpp:39
static const size_t n_boundary_points
Definition grid.hpp:22
static EleType grid_spacing_h(size_t n)
Compute the gridspacing h of a domain given n points in a direction.
Definition grid.hpp:31
static Eigen::SparseMatrix< EleType > laplacian(size_t n)
Return coefficients matrix A for laplacian as linear operator on u(x,y) assuming homogenous dirichlet...
Definition grid.hpp:88
Definition common.hpp:6