% Create video showing how total emittance changes according to beta
% function and optimal matching of electron to photon emittance

clear; close all;

vid = VideoWriter('emittanceChanges.mp4','MPEG-4');
vid.FrameRate = 30;    % Default 30
vid.Quality = 100;    % Default 75
open(vid);
figure('units','pixels','position',[0 0 1920 1080],'ToolBar','none');
set(0,'defaultfigurecolor',[1 1 1]); % White background

myBlue = [0.4 0.44 0.73];
myGold = [0.7 0.6 0];

pos1 = [0.1 0.2 0.6 0.6];
pos2 = [0.74 0.65 0.25 0.25];
pos3 = [0.74 0.375 0.25 0.25];
pos4 = [0.74 0.1 0.25 0.25];

%epsxe = 150e-12; % Horizontal electron emittance 150 pm.rad for DLSR 
epsxe = 5500e-12; % Horizontal electron emittance 5.5 nm.rad for 3rd-gen 
undLength = 2; % Length of undulator in m

photonE = (0.1:0.02:100); % Range of photon energies in keV
lambda = (1e-10*12.3984)./photonE; % Convert photon energy in keV to wavelength in m

index300 = round(1 + (0.3-0.1)/0.02); % Index for 300 eV 
index3k = round(1 + (3-0.1)/0.02); % Index for 3 keV
index30k = round(1 + (30-0.1)/0.02); % Index for 30 keV 

part1 = [0.01:0.005:0.25]; % Lots happening for DLSRs in this regime of L/2pi
part2 = [0.26:0.02:1.0]; % Increasing dull... 
part3 = [1.05:0.05:5]; % Zzzzzzz 


for ii = [part1 part2 part3]
    subplot('Position',pos1); % Main plot 
    
    sigxp = (1/(4*pi))*(lambda.*undLength).^0.5; % Photon beam source size in m
    sigpxp = (lambda./(undLength)).^0.5; % Photon beam divergence in rad
    sigxe = (epsxe*ii)^0.5; % Range of sigma_x^e (horiz e-beam SD width) in m
    sigpxe = epsxe/sigxe; % Value of divergence of e-beam in rad for present value of sigxe
    totsigx = (sigxe^2 + sigxp.^2).^0.5; % Convoluted total source size
    totsigpx = (sigpxe^2 + sigpxp.^2).^0.5; % Convoluted total source divergence
    totEmitt = 1e12.*totsigx.*totsigpx; % Total emittance for present value of sigxe in pm.rad
    
    loglog(0.1,50,5,7000,photonE,totEmitt,'color',myBlue, 'LineWidth', 3.5);
    hold on 
    % Markers for 3 exemplary photon energies 
    plot(photonE(index300),totEmitt(index300),'ro','MarkerFaceColor','red','MarkerSize',10);
    plot(photonE(index3k),totEmitt(index3k),'ro','MarkerFaceColor','red','MarkerSize',10);
    plot(photonE(index30k),totEmitt(index30k),'ro','MarkerFaceColor','red','MarkerSize',10);
    
    str1 = '{\beta_e} = ';
    str2 = num2str(ii,'% 7.3f');
    str3 = ' m';
    strTot = [str1,str2,str3];
    annotation('textbox',[0.59 0.71 0.14 0.07],'String',strTot,'EdgeColor',...
        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...
        'left','verticalAlignment','bottom');
    
    str1 = '{\sigma_x^e} = ';
    str2 = num2str(sigxe*1e6,'% 7.2f');
    str3 = ' {\mu}m';
    strTot = [str1,str2,str3];
    annotation('textbox',[0.59 0.67 0.14 0.07],'String',strTot,'EdgeColor',...
        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...
        'left','verticalAlignment','bottom');
    
    str1 = '{\sigma_x^{\prime e}} = ';
    str2 = num2str(sigpxe*1e6,'% 7.3f');
    str3 = ' {\mu}rad';
    strTot = [str1,str2,str3];
    annotation('textbox',[0.59 0.63 0.14 0.07],'String',strTot,'EdgeColor'...
        ,'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...
        'left','verticalAlignment','bottom');
    
    str1 = '{\epsilon_x^e} = ';
    str2 = num2str(epsxe*1e12,'% 7.1f');
    str3 = ' pm.rad';
    strTot = [str1,str2,str3];
    annotation('textbox',[0.47 0.71 0.1 0.07],'String',strTot,'EdgeColor',...
        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...
        'left','verticalAlignment','bottom');
    
    str1 = '{L_u} = ';
    str2 = num2str(undLength,'% 7.1f');
    str3 = ' m';
    strTot = [str1,str2,str3];
    annotation('textbox',[0.47 0.67 0.1 0.07],'String',strTot,'EdgeColor',...
        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...
        'left','verticalAlignment','bottom');
    
    set(gca,'FontName','Helvetica','fontsize',18);
    set(gca,'TickLength',[0.016, 2]);
    set(gca,'xscale','log');
    set(gca,'yscale','log');
    set(gca,'linewidth',2);
    
    xlim([0.1 100]);
    %ylim([100 3400]); % For DLSR 
    ylim([5000 15000]); % For 3rd gen. 
    xlabel('Photon energy [keV]');
    ylabel('Total horizontal emittance [pm.rad]');
    hold off 
    
    % 300 eV phase-space plot 
    subplot('Position',pos2);
    newplot
    annotation('textbox',[0.77 0.88 0.1 0.1],'String','electron','EdgeColor',...
        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...
        'left','verticalAlignment','bottom','Color',myBlue);
    annotation('textbox',[0.77 0.855 0.1 0.1],'String','photon','EdgeColor',...
        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...
        'left','verticalAlignment','bottom','Color',myGold);
    annotation('textbox',[0.77 0.83 0.1 0.1],'String','total','EdgeColor',...
        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...
        'left','verticalAlignment','bottom','Color','g');

    annotation('textbox',[0.9 0.88 0.1 0.1],'String','300 eV','EdgeColor',...
        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...
        'left','verticalAlignment','bottom');
    
    set(gca,'xtick',[]); % No tick marks 
    set(gca,'ytick',[]); 

    sigp300 = (1e-10*(12.3984/0.3)*undLength)^0.5/(4*pi); % sigma_p @ 300 eV
    sigpp300 = (1e-10*(12.3984/0.3)/undLength)^0.5; % sigma_p' @ 300 eV
    maxSigx300 = 1.1*((max(part3)*epsxe) + sigp300^2)^0.5; % max sigma_p @ 300 eV
    maxSigpx300 = 1.1*((epsxe/min(part1)) + sigpp300^2)^0.5; % max sigma_p' @ 300 eV
        
    totsigx300 = (sigxe^2 + sigp300^2)^0.5; % Convoluted total source size @ 300 eV
    totsigpx300 = (sigpxe^2 + sigpp300^2)^0.5; % Convoluted total source divergence @ 300 eV
    
    ellipse(sigxe,sigpxe,0,0,0,myBlue); % electron-emittance phase-space ellipse 
    hold on
    ellipse(sigp300,sigpp300,0,0,0,myGold); % photon-emittance phase-space ellipse 
    ellipse(totsigx300,totsigpx300,0,0,0,'g'); % total-emittance phase-space ellipse 
    ax = gca;
    ax.XAxisLocation = 'origin'; % Plot with axes through origin 
    ax.YAxisLocation = 'origin';
    xlim([-maxSigx300 maxSigx300]); % Set axes limits 
    ylim([-maxSigpx300 maxSigpx300]);
    
    axis square;
    axis on;
    set(gca,'fontsize',18,'linewidth',2);
    xlabel('\sigma');
    ylabel('\sigma^{\prime}');

    hold off;
    
    % 3 keV phase-space plot 
    subplot('Position',pos3);
    newplot
    annotation('textbox',[0.9 0.605 0.1 0.1],'String','3 keV','EdgeColor',...
        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...
        'left','verticalAlignment','bottom');
    
    sigp3k = (1e-10*(12.3984/3)*undLength)^0.5/(4*pi); 
    sigpp3k = (1e-10*(12.3984/3)/undLength)^0.5; 
    maxSigx3k = 1.1*((max(part3)*epsxe) + sigp3k^2)^0.5;
    maxSigpx3k = 1.1*((epsxe/min(part1)) + sigpp3k^2)^0.5; 
        
    totsigx3k = (sigxe^2 + sigp3k^2)^0.5; 
    totsigpx3k = (sigpxe^2 + sigpp3k^2)^0.5; 
    ellipse(sigxe,sigpxe,0,0,0,myBlue);
    hold on
    ellipse(sigp3k,sigpp3k,0,0,0,myGold);
    ellipse(totsigx3k,totsigpx3k,0,0,0,'g');
    ax = gca;
    ax.XAxisLocation = 'origin';
    ax.YAxisLocation = 'origin';
    xlim([-maxSigx3k maxSigx3k]);
    ylim([-maxSigpx3k maxSigpx3k]);
    set(gca,'xtick',[]); 
    set(gca,'ytick',[]); 

    axis square;
    axis on;
    set(gca,'fontsize',18,'linewidth',2);
    xlabel('\sigma');
    ylabel('\sigma^{\prime}');
    hold off;
    
    % 30 keV phase-space plot 
    subplot('Position',pos4);
    newplot
    annotation('textbox',[0.9 0.33 0.1 0.1],'String','30 keV','EdgeColor',...
        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...
        'left','verticalAlignment','bottom');
    sigp30k = (1e-10*(12.3984/30)*undLength)^0.5/(4*pi); 
    sigpp30k = (1e-10*(12.3984/30)/undLength)^0.5;
    maxSigx30k = 1.1*((max(part3)*epsxe) + sigp30k^2)^0.5;
    maxSigpx30k = 1.1*((epsxe/min(part1)) + sigpp30k^2)^0.5; 
        
    totsigx30k = (sigxe^2 + sigp30k^2)^0.5; 
    totsigpx30k = (sigpxe^2 + sigpp30k^2)^0.5; 
    ellipse(sigxe,sigpxe,0,0,0,myBlue);
    hold on
    ellipse(sigp30k,sigpp30k,0,0,0,myGold);
    ellipse(totsigx30k,totsigpx30k,0,0,0,'g');
    ax = gca;
    ax.XAxisLocation = 'origin';
    ax.YAxisLocation = 'origin'; 
    xlim([-maxSigx30k maxSigx30k]);
    ylim([-maxSigpx30k maxSigpx30k]);
    set(gca,'xtick',[]); 
    set(gca,'ytick',[]); 

    axis square;
    axis on;
    set(gca,'fontsize',18,'linewidth',2);
    xlabel('\sigma');
    ylabel('\sigma^{\prime}');
    hold off;
    
    frame = getframe(gcf);
    writeVideo(vid,frame);
    delete(findall(gcf,'type','annotation'));
    
    
end
% Output the movie as an mp4 file
close(vid);