% Random walk (RW) in a finite slab + CBS terms
% All lengths (Lz, R, ell, x0, y0) have to be given in the same unit. 
% The outputs are: Nsc the number of scattering event (0 for transmitted photons), the output angles theta, phi, the output positions x, y
clear all;
% Parameters of the slab
Lz=1000; % length of the slab
R=2000; %transverse size
ell=20; %scattering mean free path
   
theta_out=(0:0.001:0.5);
phi_out=(0:0.001:0.5);
Rdiff=0;
Nph=1000; % number of photons
dICBS=zeros(Nph,501);
for iph=1:1:Nph
    
% Initial Conditions 
Nsc=0;  % number of scattering events already done
cos_phi = 1; theta=0; % direction of propagation
x=0; %initial position along x
y=0; %initial position along y
z=0; % Initial impact with the vapor (NOT the 1st scattering event!)
r=sqrt(x^2+y^2);

% length of the 1st step using an exponential distribution (Beer-Lambert)
step = -ell*log(rand);  % use the inverse function f^(-1) to draw the random number following a distribution f: exp -> log

% The first step is ballistic in forward direction   
z = z+step;
z1=z;

while all(z<=Lz  && z>0  && r<R) % Next scattering event inside the slab
  
    Nsc = Nsc+1;  % add 1 to number of scattering events
    
    % record the trajectory (optional)
    X(Nsc) = x;
    Y(Nsc) = y;
    Z(Nsc) = z;
    
    % draw a new random direction
    theta = 2*pi*rand;
    cos_phi = -1+2*rand;  % uniform between -1 and 1 (always  check that the distribution is uniform: mistakes often happen with cos, sin etc)
    sin_phi = sqrt(1-cos_phi^2);  % always >0 since phi is between 0 and pi
    
    % length of the next step in an exponential ditribution (Beer-Lambert)
    step = -ell*log(rand);  

    % new position
    x = x + step*sin_phi*cos(theta) ;  
    y = y + step*sin_phi*sin(theta);
    z = z + step*cos_phi;
    r = sqrt(x^2+y^2);

    % record the trajectory (optional)
    X(Nsc) = x;
    Y(Nsc) = y;
    Z(Nsc) = z;
   
end
phi = acos(cos_phi);

% plotting the trajectory (just for illustration)
%figure(1), plot3([0 X], [0 Y], [0 Z]), xlabel('x/l'), ylabel('y/l'), zlabel('z/l'), axis([-R R -R R -1 Lz+1]);
Nfinx(iph)=x;
Nfiny(iph)=y;
Nfinz(iph)=z;

% Adding the reverse path with the corresponding dephasing 
% but only for double scattering!
%(in the y=0 plane)
if Nsc>2
    dICBS(iph,:)=1+cos(sin(theta_out)*x+(1-cos(theta_out))*(z-z1));
else
    dICBS(iph,:)=1;
end;
if (z<0) 
    Rdiff=Rdiff+1; %just to count diffuse transmission 
    Rdifftime(iph)=Nsc; % register number of events, which we put to time
end
end;
ICBS=sum(dICBS)/Nph;
plot(theta_out, ICBS,'DisplayName','ICBS','YDataSource','ICBS');figure(gcf)
xlabel ('theta');
ylabel('I(theta)');

% Analytical Formula
xh=(1:1:100);
hh=hist(Rdifftime, xh);
BackScattering=sum(hh(2:1:100));
SingleScattering=hh(2)/BackScattering; % hh(1) is unscattered light
MultipleScattering=sum(hh(3:1:100))/BackScattering;
%Diffusion limit formula with reduction for single scattering
kl=ell;
Enhancement_MS=(1+(3/7).*(1+(1-exp(-4*kl*theta_out/3))./(kl.*theta_out))./(1+kl*theta_out).^2);
IcbsNorm=SingleScattering+MultipleScattering*Enhancement_MS; 
%IcbsNorm=MultipleScattering*Enhancement/(SingleScattering+MultipleScattering);
hold on; 
plot(theta_out, IcbsNorm,'Color','red','DisplayName','ICBS','YDataSource','ICBS');figure(gcf)
