forked from anandrdbz/Couette_Meshless_Multigrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FracStepMultigrid.cpp
211 lines (187 loc) · 6.65 KB
/
FracStepMultigrid.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "FracStepMultigrid.hpp"
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
FractionalStepMultigrid::FractionalStepMultigrid() {
grids_ = vector<std::pair<int, FractionalStepGrid*>>(); //int = grid size
residuals_ = vector<double>();
residuals_.push_back(1.0);
}
FractionalStepMultigrid::~FractionalStepMultigrid() {
for (size_t i = 0; i < grids_.size(); i++) {
delete grids_.at(i).second;
delete prolongMatrices_.at(i);
delete restrictionMatrices_.at(i);
}
}
Eigen::SparseMatrix<double>* FractionalStepMultigrid::buildInterpMatrix(FractionalStepGrid* baseGrid, FractionalStepGrid* targetGrid) {
Eigen::SparseMatrix<double>* interpMatrix = new Eigen::SparseMatrix<double>(targetGrid->getSize(), baseGrid->getSize());
vector<Eigen::Triplet<double>> tripletList;
//tripletList: <row, col, value>
std::pair<Eigen::VectorXd, vector<int>> pointWeights;
for (int i = 0; i < targetGrid->getSize(); i++) {
if(baseGrid->laplaceMatSize_ >= targetGrid->laplaceMatSize_){
pointWeights = baseGrid->pointInterpWeights(targetGrid->points_[i], baseGrid->properties_.polyDeg);
}
else{
pointWeights = baseGrid->pointInterpWeights(targetGrid->points_[i], targetGrid->properties_.polyDeg);
}
for (size_t j = 0; j < pointWeights.second.size(); j++) {
tripletList.push_back(Eigen::Triplet<double>(i, pointWeights.second[j], pointWeights.first(j)));
}
}
interpMatrix->setFromTriplets(tripletList.begin(), tripletList.end());
return interpMatrix;
}
void FractionalStepMultigrid::buildProlongMatrices() {
prolongMatrices_.resize(grids_.size());
for (size_t i = 0; i < grids_.size() - 1; i++) {
prolongMatrices_[i] = (buildInterpMatrix(grids_[i].second, grids_[i + 1].second));
}
prolongMatrices_[prolongMatrices_.size() - 1] = NULL;
}
void FractionalStepMultigrid::buildRestrictionMatrices() {
restrictionMatrices_.resize(grids_.size());
restrictionMatrices_[0] = NULL;
for (size_t i = 1; i < grids_.size(); i++) {
restrictionMatrices_[i] = buildInterpMatrix(grids_[i].second, grids_[i - 1].second);
}
}
void FractionalStepMultigrid::buildMatrices() {
buildProlongMatrices();
buildRestrictionMatrices();
Grid* currGrid;
//set coarse source coeffs back to 0
for (size_t i = 0; i < grids_.size() - 1; i++) {
currGrid = grids_[i].second;
if (i != grids_.size() - 1) {
currGrid->modify_coeff_neumann("coarse");
}
}
}
void FractionalStepMultigrid::vCycle() {
//Restriction
Grid* currGrid;
currGrid = grids_[grids_.size() - 1].second;
if (grids_.size() == 1) {
currGrid->sor(currGrid->laplaceMat_, currGrid->values_, (currGrid->source_));
return;
}
currGrid->bound_eval_neumann();
//std::cout << std::setprecision(12) << "PPE Residual: " << resid_norm << std::endl;
//Restriction
for (size_t i = grids_.size() - 1; i > 0; i--) {
currGrid = grids_[i].second;
std::string gridType = i == grids_.size() - 1 ? "fine" : "coarse";
//cout << gridType << endl;
if (i != grids_.size() - 1) {
currGrid->values_->setZero();
}
currGrid->boundaryOp(gridType);
currGrid->sor(currGrid->laplaceMat_, currGrid->values_, (currGrid->source_));
(grids_[i - 1].second->source_)(Eigen::seq(0, grids_[i - 1].second->laplaceMatSize_ - 1)) = (*(restrictionMatrices_[i])) * (currGrid->residual())(Eigen::seq(0, currGrid->laplaceMatSize_ - 1));
grids_[i - 1].second->fix_vector_bound_coarse(&grids_[i - 1].second->source_);
if (currGrid->neumannFlag_) {
grids_[i - 1].second->source_.coeffRef(grids_[i - 1].second->source_.rows() - 1) = 0;
}
}
//cout << grids_[0].second->source_.norm() << endl;
//Iterate on coarsest grid
currGrid->boundaryOp("coarse");
currGrid = grids_[0].second;
currGrid->values_->setZero();
currGrid->sor(currGrid->laplaceMat_, currGrid->values_, (currGrid->source_));
currGrid->sor(currGrid->laplaceMat_, currGrid->values_, (currGrid->source_));
//correction and prolongation
Eigen::VectorXd correction;
for (size_t i = 1; i < grids_.size(); i++) {
currGrid = grids_[i].second;
//correction
correction = (*prolongMatrices_[i - 1]) * ((*grids_[i - 1].second->values_)(Eigen::seq(0, grids_[i - 1].second->laplaceMatSize_ - 1)));
currGrid->fix_vector_bound_coarse(&correction);
(*currGrid->values_)(Eigen::seq(0, currGrid->laplaceMatSize_ - 1)) += correction;
//smoother
currGrid->sor(currGrid->laplaceMat_, currGrid->values_, (currGrid->source_));
}
currGrid->boundaryOp("fine");
}
double FractionalStepMultigrid::gmres(double tol, std::string Solver){
Grid* finestGrid;
finestGrid = grids_[grids_.size()-1].second;
Eigen::VectorXd r = finestGrid->rhs_ - *(finestGrid->interior_mat) * (*finestGrid->sol_);
Eigen::VectorXd z,p,w,v;
double alpha,l2res,l2res_prev;
l2res = 0;
int kmax;
vector<Eigen::VectorXd> P;
vector<Eigen::VectorXd> W;
kmax = finestGrid->laplaceMatSize_;
for(int k = 0; k < kmax; k++){
//GMRES with Multigrid
if(Solver == "Multigrid"){
finestGrid->source_ = *(finestGrid->prolong_mat) * r;
finestGrid->values_->setZero();
vCycle();
z = *(finestGrid->restrict_mat) * *(finestGrid->values_);
}
else if(Solver == "ILU"){
z = finestGrid->solver.solve(r);
}
else if(Solver == "SOR"){
finestGrid->source_ = *(finestGrid->prolong_mat) * r;
finestGrid->values_->setZero();
finestGrid->sor(finestGrid->laplaceMat_,finestGrid->values_,(finestGrid->source_));
//finestGrid->sor(finestGrid->laplaceMat_,finestGrid->values_,&(finestGrid->source_));
z = *(finestGrid->restrict_mat) * *(finestGrid->values_);
}
else if(Solver == "None"){
z = r;
}
if(k == 0){
p = z;
P.push_back(p);
w = *(finestGrid->interior_mat) * p;
W.push_back(w);
}
else{
p = z;
v = *(finestGrid->interior_mat) * z;
for(int i = 0; i<k; i++){
double beta = W[i].dot(v)/(W[i].dot(W[i]));
p = p - beta*P[i];
}
P.push_back(p);
w = *(finestGrid->interior_mat) * p;
W.push_back(w);
}
alpha = w.dot(r)/(w.dot(w));
*(finestGrid->sol_) = *(finestGrid->sol_) + alpha*p;
r = r - alpha*w;
Eigen::VectorXd b = finestGrid->rhs_;
l2res_prev = l2res;
l2res = r.lpNorm<2>() / (b.lpNorm<2>()) ;
//cout<<l2res<<endl;
residuals_.push_back(l2res);
if(l2res < tol){
break;
}
/*if(std::abs(l2res_prev - l2res) < 1e-12){
break;
}*/
}
return l2res;
}
void FractionalStepMultigrid::solveLoop() {
}
double FractionalStepMultigrid::residual() {
Grid* finegrid = grids_[grids_.size() - 1].second;
return finegrid->residual().lpNorm<1>() / finegrid->source_.lpNorm<1>();
}
void FractionalStepMultigrid::addGrid(FractionalStepGrid* grid) {
grids_.push_back(std::pair<int, FractionalStepGrid*>(grid->getSize(), grid));
sortGridsBySize();
}
void FractionalStepMultigrid::sortGridsBySize() {
std::sort(grids_.begin(), grids_.end());
}