-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCG.cpp
More file actions
80 lines (67 loc) · 1.98 KB
/
Copy pathCG.cpp
File metadata and controls
80 lines (67 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// Created by felix on 27.06.20.
//
#include <iostream>
#include "CG.h"
#include "sor.hpp"
#include "helper.hpp"
void CG::solve(int& it, double& residual) {
it = 0;
residual = 0;
double alpha = 0;
double beta = 0;
double r_2 = 0;
double r_2_old = 0;
double dAd = 0;
MatrixXd& d = *_d;
MatrixXd& res = *_res;
MatrixXd& Ad = *_Ad;
// boundary has to be applied only once in the beginning as you work only on the residual
boundary_p(*_x, _types);
comp_residual(*_x, *_rhs, res, _config.dx, _config.dy);
d = res;
//double r_2_test = 0;
do{
it++;
calc_Ad();
r_2 = res.squaredNorm();
dAd = 0;
for (auto j = 1; j < d.cols()-1; j++) {
for (auto i = 1; i < d.rows()-1; i++) {
dAd = dAd + d.coeffRef(i,j) * Ad.coeffRef(i,j);
}
}
alpha = r_2 / (dAd);
// update x
*_x = *_x + alpha * d;
r_2_old = r_2;
// update residual
res = res - alpha * Ad;
r_2 = res.squaredNorm();
beta = r_2/(r_2_old);
// update conjugate d
d = res + beta * d;
compute_l2Norm(&residual, res);
} while (it < _config.itermax && residual > _config.eps);
}
CG::CG(Config& config, MatrixXd& p, MatrixXd& rhs, matrix<cell_type>& types): Solver(config, types) {
int rows = p.rows();
int cols = p.cols();
_d = new MatrixXd(rows, cols);
_Ad = new MatrixXd(rows, cols);
_res = new MatrixXd(rows, cols);
_x = &p;
_rhs = &rhs;
_dyy_inv = 1 / (_config.dy*_config.dy);
_dxx_inv = 1 / (_config.dx*_config.dx);
}
void CG::calc_Ad() {
MatrixXd& d = *_d;
MatrixXd& Ad = *_Ad;
for (int j = 1; j < d.cols()-1; j++) {
for (int i = 1; i < d.rows()-1; i++) {
Ad.coeffRef(i,j) = (d.coeffRef(i+1,j) - 2 * d.coeffRef(i,j) + d.coeffRef(i-1,j))* _dxx_inv +
(d.coeffRef(i,j+1) - 2 * d.coeffRef(i,j) + d.coeffRef(i,j-1))* _dyy_inv;
}
}
}