% LIGO Hanford ISCT1 Table - ALS beam profile from PSL down to the table
% 
% Alberto Stochino, LHO 21 September 2012

clear all
close all 
% plotsettings


%% PSL Pick-off Matching to Doubling Crystal

% constants
lambda  = 1064e-9;
nr      = 1.4493; % fused silica refractive index

% lenses
% PSL Tbale
f(1) = .075/.4493;  % .2291; -.150/(nr-1);  % PLCX-25.4-103-UV-1064
f(2) = .2575/.4493; % PLCX-25.4-257.5-UV-1064 .35/(nr-1);    
% ISCT1 Table, before doubling crystal
f(3) = .0644/.4493; % PLCX-25.4-64.4-C-1064 %.1/(nr-1);  

% Distance between bottom mirror (M1) of ALS PSL Periscope (PP)  and bottom
% mittot of ALS ISCT1 Periscope (IP)
PPM1_IPM1 = 9.003;

% lenses location
z(1) = 1.1546;
z(2) = 1.6987;
z(3) = PPM1_IPM1+0.12; 

% beam waist at the PSL PMC: beam path origin is set at PMC waist
w00 = 0.57e-3;
z00 = -0.0759;

PSLpath = beamPath;
PSLpath.seedWaist(w00,z00,lambda);

for ii = 1 : length(f)
    PSLpath.addComponent(component.lens(f(ii),z(ii),['L',num2str(ii)]));
end

% Waist between two tables 
% PSLpath.targetWaist(1.5e-3,9,lambda);
% PSLpath = PSLpath.optimizePath('L1',[0.8 1.5],'L2',[.767 1.914]); 

% % Waist inside doubling crystal must be 70um (seen from the inside)
PSLpath.targetWaist(56e-6,PPM1_IPM1+.65,lambda);
PSLpath = PSLpath.optimizePath('L1',[0.8 1.5],'L2',[.767 1.914],'L3',[z(3) PPM1_IPM1+.65-0.12]);

zdomain = linspace(9.6,9.8,301);
hold on 
yplot = PSLpath.plotBeamWidth(zdomain,'b');
PSLpath.plotComponents(zdomain,0,'r*');
set(gca,'xscale','lin');

xlabel('Distance From Origin [m]')
ylabel('Waist Radius [m]')
title('Beam Profile')
grid on
hold off
% printplot('./','PSL_thru_TMS_beam_profile')
PSLpath
PSLpath.components


%% Mode Matching of Doubled PSL to PD

% constants
lambda  = 532e-9;
ng      = 1.89; % KTP Crystal z-axis refractive index at 532nm

% PSL Pick-off Matchign to Doubling Crystal
clear f z
close all

% lenses
% PSL Tbale
f(1) = .217;
f(2) = .1628;

% lenses location
z(1) = .52; 
z(2) = .7;

% Interference beam splitter
bs = 1.3767;

% effective beam waist inside the doubling crystal
w00 = 56e-6;
z00 = 0;    

% doubled PSL structure
dPSLpath = beamPath;
dPSLpath.seedWaist(w00,z00,lambda);

for ii = 1 : length(f)
    dPSLpath.addComponent(component.lens(f(ii),z(ii),['L',num2str(ii)]));
end

% Waist inside doubling crystal must be 30um
dPSLpath.targetWaist(1e-3,bs,lambda);
dPSLpath = dPSLpath.optimizePath('L1',[0.6325 bs],'L2',[0.6325 bs]);

zdomain = linspace(0,3,301);
hold on 
yplot = dPSLpath.plotBeamWidth(zdomain,'b');
dPSLpath.plotComponents(zdomain,0,'r*');
set(gca,'xscale','lin');

xlabel('Distance From Origin [m]')
ylabel('Waist Radius [m]')
title('Beam Profile')
grid on
hold off
% printplot('./','PSL_thru_TMS_beam_profile')
dPSLpath
dPSLpath.components

%% ALSX-ALSY Matching
clear all
close
lambda  = 532e-9;
ng      = 1.46071; % fused silica refractive index at 532nm

% beam waist at the output of the ALS periscope on ISCT1
w00 = 1.4984e-3;
% distance from the beam splitter that picks off ALSX
z00 = -(14.676 + 21.17*0.0254 + 6.1*0.0254); % -14.6726 is the distance of the waist from a point 25" far from the periscope'b bottom mirror   

% lenses
% PSL Tbale
f(1) = .325;   % 
f(2) = -.162;
f(3) = .217;

% lenses location
z(1) = .1; 
z(2) = .2674;
z(3) = .35;

ALSpath = beamPath;
ALSpath.seedWaist(w00,z00,lambda);

for ii = 1 : length(f)
    ALSpath.addComponent(component.lens(f(ii),z(ii),['L',num2str(ii)]));
end

% Waist radius at the PD should be about 0.4mm (since the diode's radius is 1.2mm) 
% ALSpath.targetWaist(1e-3,.660,lambda);
% ALSpath = ALSpath.optimizePath('L1',[0.1 0.5],'L2',[0 0.5]);

zdomain = linspace(0,.5,301);
hold on 
yplot = ALSpath.plotBeamWidth(zdomain,'b');
ALSpath.plotComponents(zdomain,0,'r*');
set(gca,'xscale','lin');

xlabel('Distance From Origin [m]')
ylabel('Waist Radius [m]')
title('Beam Profile')
grid on
hold off
% printplot('./','ALS_thru_TMS_beam_profile')
ALSpath
ALSpath.components

%% PSL Reference cavity to fiber mode matching
clear all
close
lambda  = 1064e-9;
nr      = 1.4493;

% beam waist inside reference cavity
w00 = 216e-6;
z00 = 0; 

% lenses
% PSL Tbale
f(1) = .077/(nr-1);
f(2) = .1/(nr-1);

% lenses location
z(1) = 1;
z(2) = 30*0.0254;

fiberDistance = (35+10*2+4)*0.0254;

fALSpath = beamPath;
fALSpath.seedWaist(w00,z00,lambda);

for ii = 1 : length(f)
    fALSpath.addComponent(component.lens(f(ii),z(ii),['L',num2str(ii)]));
end

% Waist radius at the PD should be about 0.4mm (since the diode's radius is 1.2mm) 
% fALSpath.targetWaist(700e-6,1,lambda);
% fALSpath = fALSpath.optimizePath('L2',[0.76 1]);
fALSpath.targetWaist(1.5e-3,fiberDistance,lambda);
fALSpath = fALSpath.optimizePath('L2',[1 2]);

zdomain = linspace(0,2,301);
hold on 
yplot = fALSpath.plotBeamWidth(zdomain,'b');
fALSpath.plotComponents(zdomain,0,'r*');
set(gca,'xscale','lin');

xlabel('Distance From Origin [m]')
ylabel('Waist Radius [m]')
title('Beam Profile')
grid on
hold off
fALSpath
fALSpath.components
