-
Notifications
You must be signed in to change notification settings - Fork 8
/
ek_uqme.m
330 lines (304 loc) · 10.3 KB
/
ek_uqme.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
328
329
330
function [Xu, Xv, VA, VB] = ek_uqme(A, B, X0, u, v, k, tol, debug, nrmtype, sA, sB)
%EK_UQME Approximate the solution the quadratic equation A X^2 + (A X0 + B) X + A X X0 + u v' = 0
% by means of a rational krylov projection method.
%
% [XU,XV] = EK_UQME(A, B, X0, U, V) approximates the solution of the
% quadratic equation in the factored form XU * XV'.
%
% [Xu, Xv, VA, VB] = EK_UQME(A, B, X0, U, V, K, TOL, DEBUG, NRMTYPE) also returns the bases VA
% and VB, and the optional parameters K, TOL, DEBUG and NRMTYPE control the maximum dimension
% of the Krylov subspace, the threshold for the stopping criterion, the debugging during the iteration
% and the norm to which the solution has to be accurate
%
%
warning('off','MATLAB:nearlySingularMatrix');
if ~exist('debug', 'var')
debug = false;
end
if ~exist('k', 'var')
k = 1000;
end
if ~exist('tol', 'var')
tol = 1e-8;
end
% tol can be function tol(r, n) that is given the residual and the norm, or
% a scalar. In the latter case, we turn it into a function
if isfloat(tol)
tol_eps = tol;
tol = @(r, n) r < tol_eps * n;
end
if ~exist('nrmtype', 'var')
nrmtype = 2;
end
% Poles
tA = .9999; tB = 1.0001;
zA = [tA, -tA];
zB = [tB, -tB];
%-----------Create the structure for (X0 + A^-1 B) and X0'---------------------------
if ~isstruct(A)
if isa(X0, 'hodlr') || isa(X0, 'hss')
%AA = ek_uqme_struct(A, zA, A * X0 + B);
AA = ek_uqme_struct(X0 + A\B, zB); % Both structures work; this is one is faster, the former might be more accurate if A is nearly singular
else
AA = X0 + A \ B; % Unstructured case
end
else
AA = A;
nrmA = norm(AA);
end
if ~exist('nrmA', 'var')
nrmA = AA.nrm;
end
if ~isstruct(X0)
if(isa(X0,'hodlr') || isa(X0,'hss'))
BB = ek_uqme_struct(X0', zB);
%BB = X0';
%nrmB = norm(BB);
else
BB = X0'; % Unstructured case
nrmB = norm(BB);
end
else
BB = X0; % Note that in this case the struct which refers to X0' has to be passed
end
if ~exist('nrmB', 'var')
nrmB = BB.nrm;
end
%---------------------------------------------------------------------------------------
% multiply the rhs by A^-1
u = A\u;
% Dimension of the space
sa = size(u, 2);
sb = size(v, 2);
n = size(A, 1);
bsa = sa;
bsb = sb;
it = 1;
while max(sa-2*bsa, sb-2*bsb) < k
%----------------When the Krylov space is too big -----> dense solver-----------
if exist('VA', 'var') && exist('HA2', 'var') && ( ( size(HA2, 1) + 2 * bsa >= size(VA, 1) ) || ...
( size(HB2, 1) + 2 * bsb >= size(VB, 1) ) )
warning('EK_UQME:: Rational Krylov space is equal to the whole space: using dense solver\n');
n = size(A,1);
S = [eye(n), eye(n); zeros(n), full(-X0 - A\B)]; S = S\[full(X0), zeros(n); -u*v', eye(n)];
E = S(1:n, 1:n); G = -S(1:n, n+1:2*n);
P = -S(n+1:2*n, 1:n); F = S(n+1:2*n, n+1:2*n);
Y = sda(E, F, G, P);
if debug
fprintf('Dense solver: It = %d, size = %d, residual = %e\n', it, n, norm(A * Y^2 + (A * X0 + B) * Y + A * Y * X0 + A * u * v'));
end
[UU, DD, VV] = svd(Y);
switch nrmtype
case 2
s = diag(DD);
rk = sum( arrayfun(@(ss) tol(ss, max(s) / (nrmA + nrmB)), s) == 0);
case 'fro'
d = sort(diag(DD));
s = sqrt(cumsum(d.^2));
rk = sum( arrayfun(@(ss) tol(ss, s(end) / (nrmA + nrmB)), s) == 0 );
end
Xu = UU(:,1:rk) * sqrt(DD(1:rk, 1:rk));
Xv = VV(:,1:rk) * sqrt(DD(1:rk, 1:rk));
return;
end
%----------------------------------------------------------------------------------
%---------------Generation/augmentation of Krylov subspaces------------------------
if ~exist('VA', 'var')
[VA, KA, HA, param_A] = rk_krylov(AA, u, zA);
[VB, KB, HB, param_B] = rk_krylov(BB, v, zB);
[~, KA2, HA2, ~] = rk_krylov(AA, VA, KA, HA, inf, param_A);
[~, KB2, HB2, ~] = rk_krylov(BB, VB, KB, HB, inf, param_B);
else
[VA, KA, HA, param_A] = rk_krylov(AA, VA, KA, HA, zA, param_A);
[VB, KB, HB, param_B] = rk_krylov(BB, VB, KB, HB, zB, param_B);
[~, KA2, HA2, ~] = rk_krylov(AA, VA, KA, HA, inf, param_A);
[~, KB2, HB2, ~] = rk_krylov(BB, VB, KB, HB, inf, param_B);
end
sa = size(VA, 2);
sb = size(VB, 2);
%----------------------------------------------------------------------------------
%------Compute the solution and residual of the projected Lyapunov equation--------
As = HA2(1:end-bsa, :)/KA2(1:end-bsa, :);
Bs = HB2(1:end-bsb, :)/KB2(1:end-bsb, :);
Cs = zeros(size(As, 1), size(Bs, 1));
Ar = HA2/KA2(1:end-bsa, :);
Br = HB2/KB2(1:end-bsb, :);
if ~exist('Cprojected', 'var')
Cprojected = ( VA(:,1:size(u,2))' * u ) * ( v' * VB(:,1:size(v,2)) );
end
if size(As,1) > size(u,2)
Cs(1:size(u,2), 1:size(v,2)) = Cprojected;
else
Cs = Cprojected(1:size(As, 1), 1:size(Bs, 1));
end
if ~exist('Ds', 'var')
Ds = VB(:,1:size(As,1))'*VA(:,1:size(As,1));
else
Ds = [Ds, VB(:, 1:size(Ds,1))' * VA(:, size(Ds,2)+1:size(As,2)); VB(:, size(Ds,1)+1:size(As,1))' * VA(:, 1:size(As,2))];
end
% projected solution
[Y, converged] = newton_nare(Bs', -Ds, Cs, As, zeros(size(As)), debug);
%----------------------------------------------------------------------------------
%-----------DEBUG----------------------
% YY = VA(:, 1:size(Y,1)) * Y * VB(:,1:size(Y,2))'; % projected back solution
% fprintf('dim. space = %d res. corr = %e res. corr2 = %e\n',size(Y,1), norm((YY)^2+ (X0 + A\B) * YY + YY * X0 + u*v')/norm(YY), norm(A*(YY)^2+ (A*X0 + B) * YY + A * YY * X0 + A*u*v')/norm(YY))
%--------------------------------------
if converged % if the newton's method did not converge then we enlarge the space moving to next iteration
%--------------------Compute and check the residual--------------------------------
res = max(norm(Ar(end-bsa+1:end, 1:size(Y,1)) * Y), ...
norm(Y * Br(end-bsb+1 : end, 1:size(Y,2))'));
if debug
fprintf('%d Relative Residual: %e, Absolute residual: %e\n', it, res/norm(Y), res);
end
if tol(res, norm(Y, nrmtype))
break
end
end
%----------------------------------------------------------------------------------
it = it + 1;
end
% Recompression
[UU, DD, VV] = svd(Y);
switch nrmtype
case 2
s = diag(DD);
rk = sum( arrayfun(@(ss) tol(ss, max(s) / (nrmA + nrmB)), s) == 0);
case 'fro'
d = sort(diag(DD));
s = sqrt(cumsum(d.^2));
rk = sum( arrayfun(@(ss) tol(ss, s(end) / (nrmA + nrmB)), s) == 0 );
end
Xu = VA(:,1:size(Y,1)) * UU(:,1:rk) * sqrt(DD(1:rk, 1:rk));
Xv = VB(:,1:size(Y,2)) * VV(:,1:rk) * sqrt(DD(1:rk, 1:rk));
end
%----------------------------------------------------------------------
% Auxiliary functions
%----------------------------------------------------------------------
function S = ek_uqme_struct(A, poles, AXB)
% Build a struct for building rational Krylov subspaces for the correction equation of UQMEs.
%
if exist('AXB', 'var')
if isa(A, 'hodlr')
[LA, UA] = lu(A);
elseif isa(A, 'hss')
ulvA = ulv(A);
end
if poles(1) ~= inf
if isa(A, 'hodlr')
[LA1, UA1] = lu(AXB - poles(1) * A);
elseif isa(A, 'hss')
ulvA1 = ulv(AXB - poles(1) * A);
end
else
LA1 = 0; UA1 = 0;
end
if poles(2) ~= inf
if isa(AXB, 'hodlr')
[LAm1,UAm1] = lu(AXB - poles(2) * A);
elseif isa(AXB, 'hss')
ulvAm1 = ulv(AXB - poles(2) * A);
end
else
LA1 = 0; UA1 = 0;
end
if isa(AXB, 'hodlr')
S = struct(...
'solve', @(nu, mu, x) aux(A, x, mu, nu, LA1, UA1, LAm1, UAm1, LA, UA, poles), ...
'multiply', @(rho, eta, x) rho * AXB * x - eta * A * x, ...
'isreal', isreal(AXB), ...
'nrm', normest(AXB, 1e-2));
elseif isa(AXB, 'hss')
S = struct(...
'solve', @(nu, mu, x) aux(A, x, mu, nu, ulvA1, [], ulvAm1, [], ulvA, [], poles), ...
'multiply', @(rho, eta, x) rho * AXB * x - eta * A * x, ...
'isreal', isreal(AXB), ...
'nrm', normest(AXB, 1e-2));
end
else
if poles(1) ~= inf
if isa(A, 'hodlr')
[LA1, UA1] = lu(A - poles(1) * eye(size(A), 'like', A));
elseif isa(A, 'hss')
ULV1 = ulv(A - poles(1) * eye(size(A), 'like', A));
else
error('Unsupported structure')
end
else
LA1 = 0; UA1 = 0; poles(1) = poles(2) + 3;
end
if poles(2) ~= inf
if isa(A, 'hodlr')
[LAm1, UAm1] = lu(A - poles(2) * eye(size(A), 'like', A));
elseif isa(A, 'hss')
ULVm1 = ulv(A - poles(2) * eye(size(A), 'like', A));
else
error('Unsupported structure')
end
else
LAm1 = 0; UAm1 = 0; poles(2) = poles(1) + 3;
end
if isa(A, 'hodlr')
S = struct(...
'solve', @(nu, mu, x) aux2(A, x, mu, nu, LA1, UA1, LAm1, UAm1, poles), ...
'multiply', @(rho, eta, x) rho * A * x - eta * x, ...
'isreal', isreal(A), ...
'nrm', normest(A, 1e-2));
elseif isa(A, 'hss')
S = struct(...
... % 'solve', @(nu, mu, x) (nu * A - mu * eye(size(A), 'like', A)) \ x, ...
'solve', @(nu, mu, x) aux2(A, x, mu, nu, ULV1, [], ULVm1, [], poles),...
'multiply', @(rho, eta, x) rho * (A * x) - eta * x, ...
'isreal', isreal(A), ...
'nrm', normest(A, 1e-2));
end
end
end
%----------------------------------------------------------------------
%----------------------------------------------------------------------
function y = aux(A, x, mu, nu, LA1, UA1, LAm1, UAm1, LA, UA, poles)
if nu == 0
if isa(A, 'hodlr')
y = -lu_solve(1.0, 0.0, LA, UA, x) / mu;
return
elseif isa(A, 'hss')
y = -ulv_solve(LA, x) / mu;
return
end
end
if mu == poles(1)
if isa(A, 'hodlr')
y = lu_solve(1.0, 0, LA1, UA1, x);
elseif isa(A, 'hss')
y = ulv_solve(LA1, x);
end
elseif mu == poles(2)
if isa(A, 'hodlr')
y = lu_solve(1.0, 0, LAm1, UAm1, x);
elseif isa(A, 'hss')
y = ulv_solve(LAm1, x);
end
end
end
%----------------------------------------------------------------------
%----------------------------------------------------------------------
function y = aux2(A, x, mu, nu, LA1, UA1, LAm1, UAm1, poles)
if nu == 0
y = -x/mu;
return
end
if nu == 1/poles(1)
if isa(A, 'hodlr')
y = lu_solve(1.0, 0, LA1, UA1, x) * poles(1);
elseif isa(A, 'hss')
y = ulv_solve(LA1, x) * poles(1);
end
elseif nu == 1/poles(2)
if isa(A, 'hodlr')
y = lu_solve(1.0, 0, LAm1, UAm1, x) * poles(2);
elseif isa(A, 'hss')
y = ulv_solve(LAm1, x) * poles(2);
end
else
y = (nu * A - mu * eye(size(A), 'like', A))\x;
end
end