forked from blackstonep/Numerical-Recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interp_laplace.h
executable file
·94 lines (89 loc) · 2.07 KB
/
interp_laplace.h
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
struct Laplace_interp : Linbcg {
MatDoub &mat;
Int ii,jj;
Int nn,iter;
VecDoub b,y,mask;
Laplace_interp(MatDoub_IO &matrix) : mat(matrix), ii(mat.nrows()),
jj(mat.ncols()), nn(ii*jj), iter(0), b(nn), y(nn), mask(nn) {
Int i,j,k;
Doub vl = 0.;
for (k=0;k<nn;k++) {
i = k/jj;
j = k - i*jj;
if (mat[i][j] < 1.e99) {
b[k] = y[k] = vl = mat[i][j];
mask[k] = 1;
} else {
b[k] = 0.;
y[k] = vl;
mask[k] = 0;
}
}
}
void asolve(VecDoub_I &b, VecDoub_O &x, const Int itrnsp);
void atimes(VecDoub_I &x, VecDoub_O &r, const Int itrnsp);
Doub solve(Doub tol=1.e-6, Int itmax=-1) {
Int i,j,k;
Doub err;
if (itmax <= 0) itmax = 2*MAX(ii,jj);
Linbcg::solve(b,y,1,tol,itmax,iter,err);
for (k=0,i=0;i<ii;i++) for (j=0;j<jj;j++) mat[i][j] = y[k++];
return err;
}
};
void Laplace_interp::asolve(VecDoub_I &b, VecDoub_O &x, const Int itrnsp) {
Int i,n=b.size();
for (i=0;i<n;i++) x[i] = b[i];
}
void Laplace_interp::atimes(VecDoub_I &x, VecDoub_O &r, const Int itrnsp) {
Int i,j,k,n=r.size(),jjt,it;
Doub del;
for (k=0;k<n;k++) r[k] = 0.;
for (k=0;k<n;k++) {
i = k/jj;
j = k - i*jj;
if (mask[k]) {
r[k] += x[k];
} else if (i>0 && i<ii-1 && j>0 && j<jj-1) {
if (itrnsp) {
r[k] += x[k];
del = -0.25*x[k];
r[k-1] += del;
r[k+1] += del;
r[k-jj] += del;
r[k+jj] += del;
} else {
r[k] = x[k] - 0.25*(x[k-1]+x[k+1]+x[k+jj]+x[k-jj]);
}
} else if (i>0 && i<ii-1) {
if (itrnsp) {
r[k] += x[k];
del = -0.5*x[k];
r[k-jj] += del;
r[k+jj] += del;
} else {
r[k] = x[k] - 0.5*(x[k+jj]+x[k-jj]);
}
} else if (j>0 && j<jj-1) {
if (itrnsp) {
r[k] += x[k];
del = -0.5*x[k];
r[k-1] += del;
r[k+1] += del;
} else {
r[k] = x[k] - 0.5*(x[k+1]+x[k-1]);
}
} else {
jjt = i==0 ? jj : -jj;
it = j==0 ? 1 : -1;
if (itrnsp) {
r[k] += x[k];
del = -0.5*x[k];
r[k+jjt] += del;
r[k+it] += del;
} else {
r[k] = x[k] - 0.5*(x[k+jjt]+x[k+it]);
}
}
}
}