% SOURCE_ITERATION
% Source iteration example for transport equation.
%
% This is one of the test cases from
%
% Radiative transfer in finite inhomogeneous plane-parallel atmospheres
% by Garcia and Siewert
% JQSRT (27), 1982 pp 141-148.
%
%

% Set up the grids.
na=20;
[angles, weights] = gauss(na);
nx=2001;
tau=5.0; dx=tau/(nx-1);
%
% Problem data
%
x=0:dx:tau; x=x';
c=exp(-x); 
psi_left=ones(na/2,1);
psi_right=zeros(na/2,1);
source=zeros(nx,1);
sweep_data=struct('c',c,'dx',dx);
%
% Source iteration.
%
phi=zeros(nx,1);
itt=0;
delflux=1;
while itt < 100 && delflux > 1.d-8
   psi = transport_sweep(phi, psi_left, psi_right, angles, source, sweep_data);
   flux=psi*weights;
   delflux=norm(flux-phi); itt=itt+1;
   phi=flux;
end
plot(x,phi)
%
% Tabulate the exit distributions to check results. 
%
angleout=(1:10)/10;
angleout=[.05, angleout];
angleout=[-angleout, angleout]';
na=length(angleout);
psi_left=ones(na/2,1);
psi_right=zeros(na/2,1);
psi = transport_sweep(phi, psi_left, psi_right, angleout, source, sweep_data);
nah=na/2;
[psi(1,1:nah)', psi(nx,nah+1:na)']
texprint=1;
vl=psi(1,1:nah)';
vr=psi(nx,nah+1:na);
if texprint == 1
disp('hello')
for i=1:nah
    fprintf('%8.2f & %12.4e & %12.4e \\\\ \n',angleout(nah+i),vr(i),vl(i));
end
end

