% This is a self-consistent mode model for the IMC IM and PRC mirrors
% small fuge-factors have been applied to the IM3 RoC and IM3-IM4 distance
% to match the eigen-modes of IMC and PRC
%
% based on T1200470
addpath(genpath('../lib'));
set(0,'DefaultAxesFontSize',16);
set(0,'DefaultAxesFontWeight','normal');

% fudge factors
%n=1.45;
%epsilon3=-0.0088877330300472; % change to IM3 RoC to match PRC eigenmode
%epsilon4= 0.037267406184070;  % change to IM3 - IM4 length to match PRC eigenmode
n=1.44963;
epsilon3=-0.012483303719256;% change to IM3 RoC to match PRC eigenmode
epsilon4= 0.031219289189112;% change to IM3 - IM4 length to match PRC eigenmode

% estimate: guess - need more precise data!
PeriscopeToViewport = 1.6+.82+.29;

%parameters
f1=9099451;
c=299792458;
lambda=1.064e-6;

RPRM=-11;
RPR2=-4.555;
RPR3=36.0;
RITM=1939;
RETM=2241.5; % guess
LPRM=16.6128;         %PRM to PR2
LPR2=16.1626-0.0075;  %PR2 to PR3
LPR3=24.5;            %PR3 to ITM lens

LPRC=3.5*c/2/f1;
LPR3=LPRC-LPR2-LPRM;

LARM=3994.5;
LIM4=0.413;  % IM4 to PRM HR
LIM3=1.17;

fITM=-RITM/(n-1);
fPRM=-RPRM/(n-1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Input Mode Cleaner
IMC.lambda=lambda;
IMC.q=0; %0.2325+13.383212207463499i; % at MC1 HR (is eigenmode of OMC)
IMC.N=4;
IMC.dist=[16.2406,16.2406,0.4650/2,0.4650/2,0];
IMC.ifoc=[2/27.346,0,0,0];
IMC.label={'MC2','MC3','Waist','MC1'};
IMC.Pin=50;
IMC=setTelescopeEigenBeam(IMC); % sets the input beam

%Input Mode Cleaner, from input to output only
IMCp=IMC;
IMCp.N=2;
IMCp.dist=IMC.dist([1,2,5]);
IMCp.ifoc=IMC.ifoc([1,2]);
IMCp.label=IMC.label([1,2]);

% from IMC to PRM
IM.lambda=lambda;
IM.q=getTelescopeBeam(IMCp,'MC3',true,1);
IM.N=6;
IM.dist=[0.4650/2,0.4282+0.0845*n,1.8445-0.4650/2-(0.4282+0.0845*n), 1.1703, 1.5889-LIM4+epsilon4,LIM4,0];
IM.ifoc=[0,0,2/12.8,2/(-6.24+epsilon3),0,1/fPRM];
IM.label={'Waist','IM1','IM2','IM3','IM4','PRMlens'};
IM.Pin=IMCp.Pin;

% PRC forward path
PRC.lambda=lambda;
PRC.q=getTelescopeBeam(IM,'PRMlens',1,1);
PRC.N=3;
PRC.dist=[LPRM,LPR2,LPR3,0];
PRC.ifoc=[2/RPR2,2/RPR3,1/fITM];
PRC.label={'PR2','PR3','ITMlens'};
PRC.Pin=IM.Pin;
% PRC return path
PRCr=PRC;
PRCr.N=5;
PRCr.dist=[0,0,LPR3,LPR2,LPRM,0];
PRCr.ifoc=[-2/RITM,1/fITM,2/RPR3,2/RPR2,2/RPRM];
PRCr.label={'ITMback','ITMlensr','PR3r','PR2r','PRM'};

% PRC round trip
PRCrt=concatTelescope(PRC,PRCr);
PRCrt=setTelescopeEigenBeam(PRCrt);

% input beam from PSL
INP.lambda=lambda;
INP.q=0;
INP.N=6;
INP.dist=[0,PeriscopeToViewport*1000,4018,211.8,140,256.3,167+84.5*n]/1000;
INP.ifoc=[0,0,0,0,0,0];
INP.label={'PSLperiscope','INPUTViewPort','PeriscopeTop','PeriscopeBottom','Steer1','Steer2'};
INP.Pin=IMC.Pin;
INP.q=IMC.q-sum(INP.dist);

%disp('MC3')
%printMode(getTelescopeBeam(IM,0));
%disp('IM2')
%printMode(getTelescopeBeam(IM,'IM2'));
%disp('IM3')
%printMode(getTelescopeBeam(IM,'IM3'));
%disp('IM4')
%printMode(getTelescopeBeam(IM,'IM4'));
%plotTelescope(concatTelescope(IMC,IMCp,IM))
%plotTelescope(concatTelescope(INP,IMCp,IM,PRC))
%plotTelescope(PRCrt)
%overlap=abs(exactGaussianBeamOverlap(getTelescopeInputBeam(PRC),getTelescopeInputBeam(PRCrt)))^2
figure(1)
plotTelescope(INP);
subplot(211)
title('Input beam: From PSL periscope to MC3')
figure(2)
plotTelescope(IMC);
subplot(211)
title('Input Mode Cleaner')
figure(3)
plotTelescope(IM);
subplot(211)
title('Input Mirros: from IMC to PRM')
figure(4)
plotTelescope(PRC);
subplot(211)
title('Power Recycling Cavity')

