1 function [x,it]=rb_schwarz_solve(K,F,X)
2 % x=rb_line_gs_solve(K,F,X)
3 disp('solver red-black horizonal schwarz')
7 error('rb_line_gs_solve: inconsistent sizes')
12 tol = 1e-5*big(F)/big(K)
15 overlap=sub_size-sub_step
18 % divide into intervals in dimensions 1 and 2
21 msub=ceil(n(i)/sub_step(i));
22 % sub_nodes{i}=cell(msub,1);
24 from = (j-1)*sub_step(i)+1;
25 to = min(from+sub_size(i)-1,n(i));
26 sub_nodes{i}{j}=[from:to];
27 disp(['direction ',num2str(i),' group ',num2str(j),' indices ',num2str(sub_nodes{i}{j})])
35 check_cover(n(1),sub_nodes{1}(:))
36 check_cover(n(2),sub_nodes{2}(:))
39 % global node indices for the subdomains
43 idx1 = sub_nodes{1}{i1}; % horizontal rectangle
44 idx2 = sub_nodes{2}{i2};
45 idx3 = 1:n(3); % vertical all
46 disp(['subdomain ',num2str([i1,i2]),' nodes ',num2str(idx1),' by ',num2str(idx2)])
47 [ix1,ix2,ix3]=ndgrid(idx1,idx2,idx3);
48 ix=sub2ind(n,ix1(:),ix2(:),ix3(:));
56 disp('decomposing subdomain matrices')
57 R=cell(nsub); % cholesky factor
58 P=cell(nsub); % permutation
61 idx1 = sub_nodes{1}{i1}; % horizontal rectangle
62 idx2 = sub_nodes{2}{i2};
63 idx3 = 1:n(3); % vertical all
65 [R{i1,i2},ierr,P{i1,i2}]=chol(K(ix,ix),'vector');
67 error(['Subdomain matrix is not positive definite, error ',num2str(ierr)])
69 p = P{i1,i2}; r = R{i1,i2}; k = K(ix,ix); err(1) = big(k(p,p)-r'*r);
70 b = rand(length(p),1); x = r\(r'\b(p)); x(p)=x; err(2) = big(k*x-b);
71 fprintf('subdomain %g %g size %g %g %g nodes %g nonzeros %g Cholesky %g error %g %g\n',...
72 i1,i2,length(idx1),length(idx2),length(idx3),length(ix),...
73 nnz(K(ix,ix)),nnz(R{i1,i2}),err)
84 % solving horizontal location i1 i2 and vertical line
86 % x(ix) = x(ix) + K(ix,ix)\(F(ix) - K(:,ix)'*x);
87 p = P{i1,i2}; % Ksub(p,p)= R'*R
88 res = F(ix) - K(:,ix)'*x;
89 sol = R{i1,i2}\(R{i1,i2}'\res(p));
91 % err = big(res - K(ix,ix)*sol)
93 % err = big(xx(ix)-x(ix))
99 fprintf('iteration %g residual %g tolerance %g\n',it,res,tol)
107 function check_cover(m,x)
108 % check if cell array x contains indices that cover 1 to m
114 error('subdomains do not cover all nodes')