-
Notifications
You must be signed in to change notification settings - Fork 10
/
ABfgPreEliminateIterativeMethodComparision.m
executable file
·327 lines (210 loc) · 8.8 KB
/
ABfgPreEliminateIterativeMethodComparision.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
function [x,y,tolA,tolB,Peq,Req,Ceq,L1,U1,L1eq,U1eq]=ABfgPreEliminateIterativeMethodComparision(CtrlVar,A,B,f,g,x0,y0,Peq,Req,Ceq,L1,U1,L1eq,U1eq)
%% This is a test run
%
% The purpose is to run various iterative options for comparision
%
%
%
if nargin < 8
Peq=[];
Req=[];
Ceq=[];
L1=[];
U1=[];
L1eq=[];
U1eq=[];
end
if nargin<6
x0=[];
y0=[];
end
[nA,mA]=size(A);
[nB,mB]=size(B);
[nf,mf]=size(f);
if ~isempty(x0) && ~isempty(y0)
tolA=norm(A*x0+B'*y0-f)/norm(f);
tolB=norm(B*x0-g);
fprintf(" Initial normed residuals are: \n")
fprintf(" norm(A*x+B'*y-f)/norm(f) = %g \n",tolA)
fprintf(" norm(B*x-g)= %g \n",tolB)
end
if isempty(B) && isempty(g) && ~isempty(A) && ~isempty(f) && mA==nf
% Possibly not needed, but check if this is not just a very simple case of B=g=[]
x=A\f;
y=NaN;
if nargout>2
tolB=NaN;
tolA=norm(A*x-f)/norm(f);
end
else
BBT=B*B';
if isdiag(BBT) % the method assumes that B B' is diagonal
% It also assumes that B B' is a unity matrix, but if not then simple scaling can be used
% to ensure that this is the case.
% To make this a bit more general, I here check if B B' is indeed unity, and
% if not I do the required scaling.
tolerance=eps*1000;
isBBTunity=all(abs(diag(BBT) - 1) < tolerance) ;
if ~isBBTunity
[B,g,~,Scale]=ScaleL(CtrlVar,B,g) ;
else
Scale=1;
end
% For numerical reasons a further simple scaling of A is done to bring the
% sizes of the elements of A in line with those of B.
factor=1./(full(mean(abs(diag(A)))));
if ~isfinite(factor) % just in case all elements along the diagonal happen to be equal to zero
factor=1;
end
BtB=B'*B;
A=factor*A ; f=factor*f ; % this leaves x unaffected but y is scaled
Q=speye(nA,nA)-BtB ;
Atilde=Q*A+ BtB ;
btilde=(Q*f+B'*g) ;
teq=tic;
% c1 = condest(Atilde)
%% Equilibriate (this seems to take surprisingly long time, better to save locally when testing using same matrix)
if isempty(Peq)
[Peq,Req,Ceq] = equilibrate(Atilde);
end
AtildeEquilibrated = Req*Peq*Atilde*Ceq;
btildeEquilibrated = Req*Peq*btilde;
teq=toc(teq) ;
%% ilu for both equilibrated and not
setup.type = 'nofill'; setup.milu = 'off'; isReorder=false;
setup.type = "ilutp"; setup.milu = "off"; setup.droptol = 1e-8; setup.udiag=0 ; isReorder=true; % must be used with re-ordering
if isReorder
tdissectAtilde=tic;
pAtilde=dissect(Atilde);
% pAtilde=symrcm(Atilde); % ilu appears to take longer as compared to using dissect
tdissectAtilde=toc(tdissectAtilde);
tdissectBeq=tic;
pAtildeEquilibrated=dissect(AtildeEquilibrated);
% pBeq=symrcm(Beq);
tdissectBeq=toc(tdissectBeq);
invpAtilde(pAtilde)=1:length(pAtilde); % inverse of the permutation vector
invpBeq(pAtildeEquilibrated)=1:length(pAtildeEquilibrated); % inverse of the permutation vector
Atilde=Atilde(pAtilde,pAtilde) ;
btilde=btilde(pAtilde);
AtildeEquilibrated=AtildeEquilibrated(pAtildeEquilibrated,pAtildeEquilibrated) ;
btildeEquilibrated=btildeEquilibrated(pAtildeEquilibrated);
else
tdissectAtilde=0;
tdissectBeq=0;
end
tluincEq=tic;
if isempty(L1eq)
[L1eq,U1eq] = ilu(AtildeEquilibrated,setup);
end
tluincEq=toc(tluincEq);
tluinc=tic;
if isempty(L1)
[L1,U1] = ilu(Atilde,setup);
end
tluinc=toc(tluinc);
tol=1e-15 ; maxit=1; restart=300;
tol=1e-15 ; restart=10; maxit=floor(15/restart) ; % quick for testing purposes
%[sol,flag,relres,iter,resvec]=bicgstabl(AA,ff,tol,maxit,L1,U1,xx0);
if isempty(x0)
x0=f*0;
end
% gmres without preconditioners
tgmres=tic;
[x,flag,relres,iter,resvec]=gmres(Atilde,btilde,restart,tol,maxit,[],[],x0);
if isReorder
x=x(invpAtilde) ;
end
tgmres=toc(tgmres);
% gmres with il0 preconditioners
tgmresPre=tic;
[xPre,flagPre,relresPre,iterPre,resvecPre]=gmres(Atilde,btilde,restart,tol,maxit,L1,U1,x0);
if isReorder
xPre=xPre(invpAtilde) ;
end
tgmresPre=toc(tgmresPre);
% gmres, equilibrated matrix but no preconditioner
xEq0=f*0;
tgmresEq=tic;
[xeq,flagEq,relresEq,iterEq,resvecEq]=gmres(AtildeEquilibrated,btildeEquilibrated,restart,tol,maxit,[],[],xEq0);
if isReorder
xeq=xeq(invpBeq) ;
end
xeq=Ceq*xeq;
tgmresEq=toc(tgmresEq);
% gmres, equilibrated matrix with preconditioners
tgmresEqPre=tic;
[xeqPre,flagEqPre,relresEqPre,iterEqPre,resvecEqPre]=gmres(AtildeEquilibrated,btildeEquilibrated,restart,tol,maxit,L1eq,U1eq,xEq0);
if isReorder
xeqPre=xeqPre(invpBeq) ;
end
xeqPre=Ceq*xeqPre;
tgmresEqPre=toc(tgmresEqPre);
sol{1}=x; text(1)="x " ;
sol{2}=xPre; text(2)="xPre ";
sol{3}=xeq; text(3)="eq ";
sol{4}=xeqPre; text(4)="xeqPre" ;
if CtrlVar.InfoLevelLinSolve>=1
fprintf(" equlibrate %f sec\n",teq)
fprintf(" dissect Atilde %f sec\n",tdissectAtilde)
fprintf(" dissect ABEq %f sec\n",tdissectBeq)
fprintf(" ilu %f sec\n",tluinc)
fprintf(" iluEq %f sec\n",tluincEq)
fprintf(" gmres %f sec\n",tgmres)
fprintf(" gmresPre %f sec\n",tgmresPre)
fprintf(" gmresEq %f sec\n",tgmresEq)
fprintf(" gmresEqPre %f sec\n",tgmresEqPre)
fprintf("\n total time for nonEq with pre is about %f",tdissectAtilde+tluinc+tgmresPre)
end
if CtrlVar.InfoLevelLinSolve>=10
figure
fprintf("\n\n")
fprintf(' flag=%-i, iter=%-i %-i, relres=%-g \n ',flag,iter(1),iter(2),relres)
fprintf(' flag=%-i, iter=%-i %-i, relresPre=%-g \n ',flagPre,iterPre(1),iterPre(2),relresPre)
fprintf(' flag=%-i, iter=%-i %-i, relresEq=%-g \n ',flagEq,iterEq(1),iterEq(2),relresEq)
fprintf(' flag=%-i, iter=%-i %-i, relresEqPre=%-g \n ',flagEqPre,iterEqPre(1),iterEqPre(2),relresEqPre)
nnn=numel(resvec);
semilogy((0:nnn-1)/2,resvec,'-',LineWidth=2)
hold on
nnn=numel(resvecPre);
semilogy((0:nnn-1)/2,resvecPre,'-',LineWidth=2)
nnn=numel(resvecEq);
semilogy((0:nnn-1)/2,resvecEq,'-',LineWidth=2)
nnn=numel(resvecEqPre);
semilogy((0:nnn-1)/2,resvecEqPre,'-',LineWidth=2)
xlabel('Iteration Number',Interpreter='latex')
ylabel('Relative Residual',Interpreter='latex')
legend("Original system","With ilu(0) preconditioning","Equlibrated","Equlibrated and ilu(0) preconditioning",interpreter="latex")
% fig = gcf; exportgraphics(fig,'IterativeSolveExample.pdf')
end
fprintf("\n\n")
TolAmin=inf;
isScaled=true;
for I=1:4 % now plug the four solutions into the system and estimate norm of residuals
xx=sol{I} ;
% x=Atilde\btilde;
if isScaled
A=A/factor ;
f=f/factor ;
B=B/Scale;
g=g/Scale;
isScaled=false;
end
% yy=B*(f-A*xx); % B' yy= (f-A*xx); but since B B'= 1 this is correct, but then must use the scaled B here
yy= ( B*B') \ (B*(f-A*xx)) ;
tolA=norm(A*xx+B'*yy-f)/norm(f);
tolB=norm(B*xx-g);
% if tolA>1e-6 || tolB>1e-6
fprintf('\t %s \t %g \t %g \n ',text(I),tolA,tolB)
%
% Select the solution with the lowers tolerance as the one returned
% yy=Scale*yy; % and now scale y in case B and g were scaled above.
if tolA < TolAmin
x=xx;
y=yy;
TolAmin=tolA;
end
end
else
error('ABfgPreEliminate:B','B*B^T not diagonal')
end
end