LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
ctmc_gmres_multi.m
1function [X,flag] = ctmc_gmres_multi(A,B,tol,restart,maxit)
2% [X,FLAG]=CTMC_GMRES_MULTI(A,B,TOL,RESTART,MAXIT)
3%
4% Solve A*X=B for every column of B by restarted GMRES, reusing one ILUT
5% factorization across all of them and starting each column from the previous
6% solution. This is the shape of the stochastic complement, whose right-hand
7% side is a whole block of the generator: refactorizing per column would cost
8% more than the direct solve it replaces.
9%
10% Preparation follows CTMC_GMRES: rows are equilibrated to unit max norm and the
11% states reordered by reverse Cuthill-McKee, without which the unpivoted
12% elimination overflows on a chain of a few thousand states.
13%
14% FLAG is 0 only if every column converged. On any other value X must be
15% discarded and the caller must fall back to the direct solve; returning a
16% partial block would leave the fallback ambiguous.
17
18% Copyright (c) 2012-2026, Imperial College London
19% All rights reserved.
20
21% Relative threshold below which ILUT discards a fill-in entry.
22ILUT_DROP_TOL = 1e-4;
23
24n = size(A,1);
25nrhs = size(B,2);
26if nargin<3 || isempty(tol) || tol<=0
27 tol = 1e-12;
28end
29if nargin<4 || isempty(restart) || restart<=0
30 restart = min(n,50);
31end
32restart = min(restart,n);
33if nargin<5 || isempty(maxit) || maxit<=0
34 maxit = ceil(n/restart);
35end
36maxit = max(1,min(maxit,n));
37
38if ~issparse(A)
39 A = sparse(A);
40end
41
42rownorm = full(max(abs(A),[],2));
43rownorm(rownorm==0) = 1;
44A = spdiags(1./rownorm,0,n,n)*A;
45B = B./rownorm;
46
47p = symrcm(spones(A)+spones(A)');
48A = A(p,p);
49B = B(p,:);
50
51L = [];
52U = [];
53try
54 [L,U] = ilu(A,struct('type','ilutp','droptol',ILUT_DROP_TOL,'udiag',1));
55 if any(~isfinite(nonzeros(L))) || any(~isfinite(nonzeros(U)))
56 L = [];
57 U = [];
58 end
59catch
60 L = [];
61 U = [];
62end
63
64if isempty(L)
65 d = full(diag(A));
66 d(d==0) = 1;
67 L = spdiags(1./d,0,n,n);
68 U = speye(n);
69end
70
71Xp = zeros(n,nrhs);
72guess = ones(n,1)/n;
73warnstate = warning('off','MATLAB:gmres:tooSmallTolerance');
74for c = 1:nrhs
75 try
76 [xc,fc] = gmres(A,full(B(:,c)),restart,tol,maxit,L,U,guess);
77 catch
78 warning(warnstate);
79 X = [];
80 flag = 3;
81 return
82 end
83 if fc ~= 0
84 warning(warnstate);
85 X = [];
86 flag = fc;
87 return
88 end
89 Xp(:,c) = xc;
90 guess = xc;
91end
92warning(warnstate);
93
94X = zeros(n,nrhs);
95X(p,:) = Xp;
96flag = 0;
97end
Definition Station.m:245