addpath C:\Users\sheila.dwyer\Documents\MATLAB\alm-master
close all
clear classes

lambda = 1064e-9;
%start by fitting measured beam from collimator
goo = beamPath;  %collimator from T1900649-v8 fig 23
gooh = beamPath;  %Maggie's measurement of lightpath collimator SN18 horizontal
goov = beamPath;  %Maggie's measurement of lightpath collimator SN18 vertical

%translate Z, Zr from Lee's plot to waist size and location for alm
designZr= 0.6137;
waist_size = sqrt(designZr*lambda/pi);
designZ = -1*-0.2066;
goo.seedWaist(waist_size, designZ, lambda);

%data from Maggies measurement for vertical and horizontal 
gooh.seedWaist(321e-6, 0.343, lambda);
goov.seedWaist(315e-6, 0.337, lambda);

%because alm propagates beam from the seed waist position out in either
%direction, we need to do this to tell it to propagate from the collimator
%position (so we can place a lens between collimator and seed waist).
goo = goo.branchPath(0);
gooh = gooh.branchPath(0);
goov = goov.branchPath(0);
newh = gooh.branchPath(0);%these are for use later to try the new solution with the O3 collimator measurement
newv = goov.branchPath(0);
zdomain = -0.1:.01:0.5;
figure(1)
goo.plotBeamWidth(zdomain,'k')
gooh.plotBeamWidth(zdomain,'b')
goov.plotBeamWidth(zdomain,'r')
goo.plotWaists(zdomain)
gooh.plotWaists(zdomain)
goov.plotWaists(zdomain)
legend('design', 'design', 'horizontal', 'horizontal', 'vertical')

%add components to based on what they are from O3:
gooh.addComponent(component.lens(0.334,44e-3,'F:L1+150mm ROC'));   
goov.addComponent(component.lens(0.334,44e-3,'F:L1+150mm ROC')); 
%to design beam add components based on where they are in figure 23 
goo.addComponent(component.lens(0.334,1*25.4e-3,'F:L1+150mm ROC')); 

%add target waist  
goo.targetWaist(379.7e-6/2, 10.697*25.4e-3); %from T1900649 fig 23  %Lee plots 2w

%according to T1900649, M2 should be 12.48 inches from collimator in
%design, I measured M2 is 269mm from collimator as built for O3.  
path_length_diff = 12.48*25.4e-3 - 0.269;  %how much longer is the path length in the design compared to O3 as built? 48mm
%this calculates how good we would expect the current O3 mode matching to
%be for this path, 95%, seems reasonable 
gooh.targetWaist(379.7e-6/2,  10.697*25.4e-3 - path_length_diff);  
goov.targetWaist(379.7e-6/2,  10.697*25.4e-3 - path_length_diff);

%now, use the O3 collimator, and try out the design from T1900649 (lens
%position and target waist)
newh.addComponent(component.lens(0.334,1*25.4e-3,'F:L1+150mm ROC')); %try moving the lens forward by 10m
newv.addComponent(component.lens(0.334,1*25.4e-3,'F:L1+150mm ROC')); 
newh.targetWaist(379.7e-6/2, 10.697*25.4e-3); %from T1900649 fig 23  %Lee plots 2w
newv.targetWaist(379.7e-6/2, 10.697*25.4e-3); %from T1900649 fig 23  %Lee plots 2w

figure(2)
goo.plotBeamWidth(zdomain,'k')
gooh.plotBeamWidth(zdomain,'b')
goov.plotBeamWidth(zdomain,'r')
newh.plotBeamWidth(zdomain, 'c')
%goo.plotWaists(zdomain)
%gooh.plotWaists(zdomain)
%goov.plotWaists(zdomain)
%goo.plotBeams(zdomain)
%gooh.plotBeams(zdomain)
%goov.plotBeams(zdomain)
newh.plotBeams(zdomain)
legend('D1500302-v6 beam + layout', '', 'horizontal SN18 + O3 layout', '',...
    'vertical SN18 + O3 layout', '', 'horiz SN18 + D1500302-v6 layout', 'Location', 'SouthWest')
xlabel('meters from collimator')
ylabel('beam radius (m)')
saveas(gcf, 'check_1064collimator_SN18_new_CLF_layout.png')

