% Movie depicting cartoons of rules of thumb for changes in the appearance
% of EXAFS spectra according to the number of scatterers in a shell, the
% radius of the shell, and the scattering strength of the atoms in the
% shell 

clear; close all;

vid = VideoWriter('EXAFSrot.mp4','MPEG-4');
vid.Quality = 100;
vid.FrameRate = 30;
open(vid);
figure('units','pixels','position',[0 0 1920 1080],'ToolBar','none');
set(0,'defaultfigurecolor',[1 1 1]);
set(gca,'linewidth',7);

myBlue = [0.4 0.44 0.73];
myGold = [220 166 3]/255; 
myGrey = [0.5 0.5 0.5]; 

[xx,yy,zz] = sphere(50);  
r1 = 0.5;
r2 = 0.25; 
rt32 = (3^0.5)/2; 

% Positions of six subplots 
pos1 = [0.01 0.52 0.32 0.46]; % Top left 3D cartoon 
pos2 = [0.01 0.02 0.32 0.46]; % Bottom left EXAFS plot 
pos3 = [0.34 0.52 0.32 0.46]; % Top middle 3D cartoon 
pos4 = [0.34 0.02 0.32 0.46]; % Bottom middle EXAFS plot 
pos5 = [0.67 0.52 0.32 0.46]; % Top right 3D cartoon 
pos6 = [0.67 0.02 0.32 0.46]; % Bottom right EXAFS plot 

tt = 0:pi/180:pi*(1 - 1/180); % 180 frames for full cycle 
Rn = 1+(sin(tt)).^2; % Shell radius breathing sinusoidally out and in between 1 and 2 
ttt = 0; 
LL = 2.35; 
hold off;
lp = [0.4 -0.4 0.7];
light('Position',lp,'Style','infinite');

for ii = Rn % Breathing radius
    ttt = ttt+1; 
    
    % ------------------------------------------------------- 
    % Variation of EXAFS signal with number of atoms in shell 
    
    subplot('Position',pos1);
    newplot; 
    hold off
    surf(xx*r1,yy*r1,zz*r1,'FaceColor',myBlue,'EdgeColor','none','FaceLighting','gouraud');
    hold on
    surf(xx*1.5,yy*1.5,zz*1.5,'FaceColor',myGrey,'EdgeColor','none','FaceAlpha',0.1,'FaceLighting','gouraud'); 

    twoon = 30; 
    if (ttt <= twoon) 
        surf(xx*r2+1.5,yy*r2,zz*r2,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
        surf(xx*r2-1.5,yy*r2,zz*r2,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
    end 
    fouron = 60; 
    if (ttt > twoon) && (ttt <= fouron) 
        surf(xx*r2+1.5,yy*r2,zz*r2,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
        surf(xx*r2-1.5,yy*r2,zz*r2,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
        surf(xx*r2,yy*r2+1.5,zz*r2,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
        surf(xx*r2,yy*r2-1.5,zz*r2,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
    end 
    sixon = 90; 
    if (ttt > fouron) && (ttt <= sixon) 
        surf(xx*r2+1.5,yy*r2,zz*r2,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
        surf(xx*r2-1.5,yy*r2,zz*r2,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
        surf(xx*r2,yy*r2+1.5,zz*r2,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
        surf(xx*r2,yy*r2-1.5,zz*r2,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
        surf(xx*r2,yy*r2,zz*r2+1.5,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
        surf(xx*r2,yy*r2,zz*r2-1.5,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
    end 
    eighton = 120; 
    if (ttt > sixon) && (ttt <= eighton) 
        surf(xx*r2+rt32,yy*r2+rt32,zz*r2+rt32,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
        surf(xx*r2-rt32,yy*r2+rt32,zz*r2+rt32,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
        surf(xx*r2+rt32,yy*r2-rt32,zz*r2+rt32,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
        surf(xx*r2-rt32,yy*r2-rt32,zz*r2+rt32,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
        surf(xx*r2+rt32,yy*r2+rt32,zz*r2-rt32,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
        surf(xx*r2-rt32,yy*r2+rt32,zz*r2-rt32,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
        surf(xx*r2+rt32,yy*r2-rt32,zz*r2-rt32,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
        surf(xx*r2-rt32,yy*r2-rt32,zz*r2-rt32,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
    end 
    sixon2 = 150; 
    if (ttt > eighton) && (ttt <= sixon2) 
        surf(xx*r2+1.5,yy*r2,zz*r2,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
        surf(xx*r2-1.5,yy*r2,zz*r2,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
        surf(xx*r2,yy*r2+1.5,zz*r2,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
        surf(xx*r2,yy*r2-1.5,zz*r2,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
        surf(xx*r2,yy*r2,zz*r2+1.5,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
        surf(xx*r2,yy*r2,zz*r2-1.5,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
    end     
    fouron2 = 180; 
    if (ttt > sixon2) && (ttt <= fouron2) 
        surf(xx*r2+1.5,yy*r2,zz*r2,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
        surf(xx*r2-1.5,yy*r2,zz*r2,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
        surf(xx*r2,yy*r2+1.5,zz*r2,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
        surf(xx*r2,yy*r2-1.5,zz*r2,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
    end 

    axis off;
    axis equal; 
    light('Position',lp,'Style','infinite');
    set(gca, 'Projection','perspective');

    xlim([-LL LL]);
    ylim([-LL LL]);
    zlim([-LL LL]);
    view(16,10);
    hold off

    subplot('Position',pos2);
    newplot;
    hold off     
    if (ttt <= twoon)
        noAt = 2;
    end
    if (ttt > twoon) && (ttt <= fouron)
        noAt = 4;
    end
    if (ttt > fouron) && (ttt <= sixon)
        noAt = 6
    end
    if (ttt > sixon) && (ttt <= eighton)
        noAt = 8
    end
    if (ttt > eighton) && (ttt <= sixon2)
        noAt = 6;
    end
    if (ttt > sixon2) && (ttt <= fouron2)
        noAt = 4;
    end
    
    % Range of x centred around absorption edge
    x = -2:0.01:6;
    % Quasi step-function on a falling slope to represent bare absorption edge
    y1 = 1./(1+exp(-20*x))-x/20;
    % chi(E), the EXAFS modulation of the absorption with a period proportional
    % to the square-root of the electron energy (hv - E_B)
    y2 = 0.28*exp(-x).*cos(20*abs(x+0.2).^0.5)./(1+exp(-50*x));
    y3 = 0.4*y2*noAt/8; % Modulation varies as number of atoms in shell
    plot(x,y1+y3,'Color',myBlue,'LineWidth',3.0)
    axis off;

    xlim([-2 6]);
    ylim([-0.1 1.25]);
    hold off 
    
    % ----------------------------------------------
    % Variation of EXAFS signal with radius of shell 

    subplot('Position',pos3);
    newplot;
    hold off
    surf(xx*r1,yy*r1,zz*r1,'FaceColor',myBlue,'EdgeColor','none','FaceLighting','gouraud');
    hold on
    surf(xx*ii,yy*ii,zz*ii,'FaceColor',myGrey,'EdgeColor','none','FaceAlpha',0.1,'FaceLighting','gouraud'); 
    surf(xx*r2+ii,yy*r2,zz*r2,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
    surf(xx*r2-ii,yy*r2,zz*r2,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
    surf(xx*r2,yy*r2+ii,zz*r2,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
    surf(xx*r2,yy*r2-ii,zz*r2,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
    surf(xx*r2,yy*r2,zz*r2+ii,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
    surf(xx*r2,yy*r2,zz*r2-ii,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 

    axis off;
    axis equal; 
    light('Position',lp,'Style','infinite');
    set(gca, 'Projection','perspective');

    xlim([-LL LL]);
    ylim([-LL LL]);
    zlim([-LL LL]);
    view(16,10);
    hold off

    
    subplot('Position',pos4);
    newplot;
    hold off
    % Range of x centred around absorption edge
    x = -2:0.01:6;
    % Quasi step-function on a falling slope to represent bare absorption edge
    y1 = 1./(1+exp(-20*x))-x/20;
    % chi(E), the EXAFS modulation of the absorption with a period proportional
    % to the square-root of the electron energy (hv - E_B)
    y2 = 0.28*exp(-x).*cos(20*abs(x*ii+0.2).^0.5)./(1+exp(-50*x));
    y3 = y2/ii^2; % Modulation varies as 1/Rn^2
    plot(x,y1+y3,'Color',myBlue,'LineWidth',3.0)
    axis off;

    xlim([-2 6]);
    ylim([-0.1 1.25]);
    hold off 

    % ---------------------------------------------------------------------
    % Variation of EXAFS signal with scattering strenngth of atoms in shell

    subplot('Position',pos5);
    newplot;
    hold off
    surf(xx*r1,yy*r1,zz*r1,'FaceColor',myBlue,'EdgeColor','none','FaceLighting','gouraud');
    hold on
    surf(xx*1.5,yy*1.5,zz*1.5,'FaceColor',myGrey,'EdgeColor','none','FaceAlpha',0.1,'FaceLighting','gouraud'); 
    surf(xx*r2*ii+1.5,yy*r2*ii,zz*r2*ii,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
    surf(xx*r2*ii-1.5,yy*r2*ii,zz*r2*ii,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
    surf(xx*r2*ii,yy*r2*ii+1.5,zz*r2*ii,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
    surf(xx*r2*ii,yy*r2*ii-1.5,zz*r2*ii,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
    surf(xx*r2*ii,yy*r2*ii,zz*r2*ii+1.5,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 
    surf(xx*r2*ii,yy*r2*ii,zz*r2*ii-1.5,'FaceColor',myGold,'EdgeColor','none','FaceLighting','gouraud'); 

    axis off;
    axis equal; 
    light('Position',lp,'Style','infinite');
    set(gca, 'Projection','perspective');

    xlim([-LL LL]);
    ylim([-LL LL]);
    zlim([-LL LL]);
    view(16,10);
    hold off

    
    subplot('Position',pos6);
    newplot;
    hold off
    % Range of x centred around absorption edge
    x = -2:0.01:6;
    % Quasi step-function on a falling slope to represent bare absorption edge
    y1 = 1./(1+exp(-20*x))-x/20;
    % chi(E), the EXAFS modulation of the absorption with a period proportional
    % to the square-root of the electron energy (hv - E_B)
    y2 = 0.28*exp(-x).*cos(20*abs(x+0.2).^0.5)./(1+exp(-50*x));
    y3 = 0.25*y2*ii^2; % Modulation varies as tj, itself proportional to area 
    plot(x,y1+y3,'Color',myBlue,'LineWidth',3.0)
    axis off;

    xlim([-2 6]);
    ylim([-0.1 1.25]);
    hold off 
    

    % Store the frame
    frame = getframe(gcf);
    writeVideo(vid,frame);
    hold off;
end 
% Output the movie as an mpg file
close(vid)
