-
Notifications
You must be signed in to change notification settings - Fork 1
/
investMat.m
154 lines (140 loc) · 4.1 KB
/
investMat.m
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
%for k=20
fprintf('Flag | relres | iter\n');
fprintf('--------------------------\n');
for k=1:3
%for k=1
% Load objects
sk = sprintf('M_%d.mat', k);
load(sk);
% Determine some quantities
d = size(B,1);
ndofs = length(idx_inner);
nW = size(M,1);
%fprintf('Ndofs: %d\n', ndofs)
%fprintf('Fraction of nonzeros: %f\n', nnz(Prec)/ prod(size(Prec)))
% Check for zero coefficients
il = [];
jl = [];
for i = 1:d
for j = 1:d
if C{i,j} ~= -1e15
il = [il,i];
jl = [jl,j];
end
end
end
nnzs = length(il);
% Set up block system for direct solve
blk11 = blkdiag(M, M, M);
blk13 = [];
blk21 = [];
for l = 1:nnzs
blk13 = [blk13; -C{il(l),jl(l)}];
blk21 = [blk21, -B{il(l),jl(l)}];
end
%blk13 = -[C{1,1}; C{2,2}; C{3,3}];
%blk21 = -[B{1,1}, B{2,2}, B{3,3}];
blk22 = M;
blk32 = 0;
for l=1:d
blk32 = blk32 + C{l,l}';
end
Clapl = blk32';
%blk32 = (C{1,1} + C{2,2} + C{3,3})';
blk33 = S;
% Determine correct sizes of zero matrizes
blk12 = sparse(nW*nnzs,nW);
blk23 = sparse(nW,nW);
blk31 = sparse(nW,nW*nnzs);
Blk = [blk11, blk12, blk13;
blk21, blk22, blk23;
blk31, blk32, blk33];
i2a = @(S) S(:,idx_inner +1);
i2i = @(S) S(idx_inner + 1,idx_inner +1);
b2a = @(S) S(:, idx_bnd + 1);
b2i = @(S) S(idx_bnd + 1, idx_bnd + 1);
[L, U] = lu(M);
% Deterime rhs
g_bc = g(idx_bnd + 1)';
Dv = 0;
for l = 1:nnzs
Dv = Dv + B{il(l), jl(l)} * ( U \ (L \ (b2a(C{il(l), jl(l)}) * g_bc)));
end
M_gc = U \ (L \ Dv);
rhs = i2a(Clapl)' * ( U \ (L \f') - M_gc);
Dv = @(x) 0;
for l = 1:nnzs
Dv = @(x) Dv(x) + B{il(l),jl(l)} * (U \ ( L \ ( i2a(C{il(l),jl(l)}) * x)));
end
x0 = zeros(ndofs,1);
Sx = @(x) i2a(Clapl)' * ( U \ ( L \ (Dv(x)) ) )+ i2i(S) * x;
% Test various preconditioners
% Use inverse of lumped mass matrix
%variant = 'Mlumpinv';
%variant = 'Mdiaginv';
%variant = 'plainMlump';
%variant = 'plainMdiag';
%variant = 'noPrec';
variant = 'ilu';
tic
switch variant
case 'Mlumpinv'
Mlumpinv = spdiags(1./sum(M,2),[0],nW,nW);
P = 0;
for l = 1:nnzs
P = P + B{il(l),jl(l)} * Mlumpinv * i2a(C{il(l),jl(l)});
end
P = i2a(Clapl)' * Mlumpinv * P + i2i(S);
[PL,PU] = lu(P);
case 'Mdiaginv'
Mdiaginv = spdiags(1./diag(M),[0],nW,nW);
P = 0;
for l = 1:nnzs
P = P + B{il(l),jl(l)} * Mdiaginv * i2a(C{il(l),jl(l)});
end
P = i2a(Clapl)' * Mdiaginv * P + i2i(S);
[PL,PU] = lu(P);
case 'plainMlump'
Mlumpinv = spdiags(1./sum(M,2),[0],nW,nW);
P = i2i(Mlumpinv);
[PL,PU] = lu(P);
case 'plainMdiag'
Mdiaginv = spdiags(diag(M),[0],nW,nW);
P = i2i(Mdiaginv);
[PL,PU] = lu(P);
case 'noPrec'
P = speye(ndofs);
[PL,PU] = lu(P);
case 'ilu'
P = 0;
Mdiaginv = spdiags(1./diag(M),[0],nW,nW);
for l = 1:nnzs
Bij = B{il(l), jl(l)};
Bijdiag = spdiags(diag(Bij),[0],nW,nW);
P = P + Bijdiag * Mdiaginv * i2a(C{il(l),jl(l)});
end
P = i2a(Clapl)' * Mdiaginv * P + i2i(S);
[PL,PU] = lu(P);
%setup.type = 'ilutp';
%P = sparse(P);
%[PL,PU] = ilu(P, setup);
case 'ilu2'
P = 0;
Mdiaginv = spdiags(1./diag(M),[0],nW,nW);
Bsum = 0;
for l = 1:nnzs
Bij = B{il(l), jl(l)};
Bijdiag = spdiags(diag(Bij),[0],nW,nW);
Bsum = Bsum + Bijdiag;
%P = P + Bijdiag * Mdiaginv * i2a(C{il(l),jl(l)});
end
P = i2a(Clapl)' * Mdiaginv * Bsum * Mdiaginv * i2a(Clapl) + i2i(S);
[PL,PU] = lu(P);
%setup.type = 'ilutp';
%P = sparse(P);
%[PL,PU] = ilu(P, setup);
end
[x, flag, relres, iter] = gmres(Sx,rhs,[],1e-10,min(1000,ndofs), PL, PU, x0);
fprintf(' %d | %6.4f | %4d (%s)\n', flag, relres, iter(2), variant);
toc
end