% This code demonstrates the usage of the Matlab release of the code
% 
%     Roland Herzog, Kirk M. Soodhalter: 
%     SUBMINRES. A modified implementation of  MINRES to monitor 
%     residual subvector norms for block systems,
%     Version 1.0 (20160310)
%     DOI 10.5281/zenodo.TODO
% 
% accompanying the paper
%
%     Herzog, Soodhalter: A modified implementation of MINRES to monitor 
%     residual subvector norms for block systems (submitted)
%
%   If this code is used in a scientific publication, please cite.

% Load the matrix, preconditioner and right hand side pertaining to
% the 3D Stokes problem on grid level 1 from the above paper
load('subminres_example.mat');

% Run subminres with preconditioner and default settings
[x,flag,Rsubnorms,iter] = subminres(K,r,[],[],[],[],P);
Rsubnorms

% Run subminres with subvector residual output
ix = [1 n+1];
[x,flag,Rsubnorms,iter] = subminres(K,r,ix,[],[],[],P);
Rsubnorms

% Run subminres with subvector residual output and alternate specification 
% of subvectors (with first and second subvectors interchanged)
ix = {[n+1:n+m], [1:n]};
[x,flag,Rsubnorms,iter] = subminres(K,r,ix,[],[],[],P);
Rsubnorms

% Run subminres with subvector residual output and loose relative tolerance
% on the total residual norm
ix = [1 n+1]; rtol = 1e-4;
[x,flag,Rsubnorms,iter,~,~,~,~,toltable] = subminres(K,r,ix,rtol,[],[],P);
Rsubnorms
toltable

% Run subminres with subvector residual output and loose absolute tolerance
% on the total residual norm
ix = [1 n+1]; rtol = inf; atol = 1e-4;
[x,flag,Rsubnorms,iter,~,~,~,~,toltable] = subminres(K,r,ix,rtol,atol,[],P);
Rsubnorms
toltable

% Run subminres with full return arguments
ix = [1 n+1];
[x,flag,Rsubnorms,iter,RsubnormsHIST,XHIST,RHIST,T,toltable] = subminres(K,r,ix,[],[],[],P);
RsubnormsHIST(:,1:10)
figure(1); clf; hold on
plot(RsubnormsHIST(1,:),'-o');
plot(RsubnormsHIST(2,:),'-o');
plot(RsubnormsHIST(3,:),'k-');
set(gca,'YScale','log');

