-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathGeometricLevelGen.cpp
143 lines (106 loc) · 2.71 KB
/
GeometricLevelGen.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
#include "GeometricLevelGen.h"
template<class T>
void levelGen<T>::generateRP(const FixedSparseMatrix<T> &A,
FixedSparseMatrix<T> &R,
FixedSparseMatrix<T> &P,
int ni, int nj, int nk)
{
//matlab code(2D):
//m = ceil(M/2);
//n = ceil(N/2);
//R = sparse(zeros(m*n, M*N));
//coef = [1 3 3 1; 3 9 9 3; 3 9 9 3; 1 3 3 1]/16.0;
//for i=0:m-1
// for j=0:n-1
// index = i*n + j +1;
// for ii=0:1
// for jj=0:1
// iii = i*2+ii;
// jjj = j*2+jj;
// if iii<M && jjj<N && iii>=0 && jjj>=0
// index2 = iii*N + jjj +1;
// if A(index2,index2)~=0
// R(index, index2) = 0.25;
// %R(index, index2) = coef(ii+2,jj+2)/4;
// else
// R(index, index2) = 0.0;
// end
// end
// end
// end
// end
//end
//P = 4*R';
int nni = ceil((float)ni/2.0);
int nnj = ceil((float)nj/2.0);
int nnk = ceil((float)nk/2.0);
SparseMatrix<T> r;
SparseMatrix<T> p;
p.resize(ni*nj*nk);
p.zero();
r.resize(nni*nnj*nnk);
r.zero();
for(int k=0;k<nnk;k++)for(int j=0;j<nnj;j++)for (int i=0;i<nni;i++)
{
unsigned int index = (k*nnj +j)*nni + i;
for(int kk=0;kk<=1;kk++)for(int jj=0;jj<=1;jj++)for(int ii=0;ii<=1;ii++)
{
int iii = i*2 + ii;
int jjj = j*2 + jj;
int kkk = k*2 + kk;
if(iii<ni && jjj<nj && kkk<nk)
{
unsigned int index2 = (kkk*nj + jjj)*ni + iii;
r.set_element(index, index2, (T)0.125);
p.set_element(index2,index, 1.0);
}
}
}
R.construct_from_matrix(r);
P.construct_from_matrix(p);
r.clear();
p.clear();
//transposeMat(R,P,(T)8.0);
}
template<class T>
void levelGen<T>::generateLevelsGalerkinCoarsening(vector<FixedSparseMatrix<T> *> &A_L,
vector<FixedSparseMatrix<T> *> &R_L,
vector<FixedSparseMatrix<T> *> &P_L,
vector<Vec3i> &S_L, int & total_level,
FixedSparseMatrix<T> &A,
int ni,int nj,int nk)
{
cout<<"building levels ...... "<<endl;
A_L.resize(0);
R_L.resize(0);
P_L.resize(0);
S_L.resize(0);
total_level = 1;
A_L.push_back(&A);
S_L.push_back(Vec3i(ni,nj,nk));
int nni = ni, nnj = nj, nnk = nk;
unsigned int unknowns = ni*nj*nk;
while (unknowns > 16*16*16)
{
A_L.push_back(new FixedSparseMatrix<T>);
R_L.push_back(new FixedSparseMatrix<T>);
P_L.push_back(new FixedSparseMatrix<T>);
nni = ceil((float)nni/2.0);
nnj = ceil((float)nnj/2.0);
nnk = ceil((float)nnk/2.0);
S_L.push_back(Vec3i(nni,nnj,nnk));
unknowns = nni*nnj*nnk;
total_level++;
}
for (int i=0;i<total_level-1;i++)
{
generateRP(*(A_L[i]), *(R_L[i]),*(P_L[i]),S_L[i].v[0], S_L[i].v[1],S_L[i].v[2]);
FixedSparseMatrix<T> temp;
multiplyMat(*(A_L[i]),*(P_L[i]),temp,1.0);
multiplyMat(*(R_L[i]),temp, *(A_L[i+1]),0.5);
temp.resize(0);
temp.clear();
}
cout<<"build levels done"<<endl;
}
template struct levelGen<double>;