%LightPipes Simulation with LightPipes for Matlab
%March 2014. F.A. van Goor.
%laser.m
%Laser example.

clear all;

m=1;
nm=1e-9*m;
micron=1e-6*m;
mm=1e-3*m;
cm=1e-2*m;
mrad=1e-3;

lambda=10.6*micron; %wavelength CO2 laser
size=32*mm;
N=300;
f1=5*m;
R=0.9;
f2=500000*m;
L=30*cm;
n=50;
tx=0.0*mrad; ty=0.0*mrad;
D=4*mm;
Isat=100;
alpha0=4.0;
Lgain=30*cm;

h1=figure('Position',[20 100 1492 462]);

F=LPBegin(size,lambda,N);
F=LPRandomIntensity(8,F);
F=LPRandomPhase(13,1,F);
Power=zeros(n);
rt=zeros(n);
for i=1:n
  rt(i)=i-1;
  F=LPCircAperture(D/2,0,0,F);
  F=LPGain(Isat,alpha0,Lgain,F);
  F=LPLensForvard(f1,L,F);
  F=LPTilt(tx,ty,F);
  F=LPLensForvard(f2,L,F);
  F=LPInterpol(size,N,0,0,0,1,F);
  F=LPConvert(F);
  F=LPIntAttenuator(R,F);
  Fout=LPIntAttenuator(1-R,F);
  [F0,Power(i)]=LPNormal(F);
  Iout=LPIntensity(2,Fout);
  if i<=10
    figure(h1);
    subplot(2,5,i);
    imagesc(Iout);
    Str=sprintf('i =%2d',rt(i))
    title(Str);
    axis off;axis equal;
  end
end
figure(2);
plot(rt,Power);
ylabel ('Power [a.u.]');
xlabel('i');
figure(3);
surf(Iout,'FaceColor','interp',...
   'EdgeColor','none',...
   'FaceLighting','phong');
axis tight; axis off;
view(-50,30);
camlight left;
