% Created on 20210203
% By Guoxian Su

clear
close all
% clc

figure('Position',[2000 100 600 150])
%% Basis Generation
initialstate=[1 1];

title_ini=num2str(initialstate);
% initialstate=sparse(initialstate);

periotic=0;
NParticle=sum(initialstate);
NSite=length(initialstate);



% Basis Generation
Partition=intpartitions(NParticle,NSite);

basis=[];
for ii=1:length(Partition)
    tmp=cell2mat(Partition(ii));
    if max(tmp)<=6
        if max(tmp)>1
            C=nchoosek(1:NSite,length(tmp));
            tmp=perms(tmp);
            tmp=unique(tmp,'rows');
            basisTmp=[];
            for jj=1:size(C,1)
                basisTmp1=zeros(size(tmp,1),NSite);
                for kk=1:size(basisTmp1,1)
                    basisTmp1(kk,C(jj,:))=tmp(kk,:);
                end
                basisTmp=cat(1,basisTmp,basisTmp1);
            end
            basisTmp=unique(basisTmp,'rows');
        else
            C=nchoosek(1:NSite,NSite-length(tmp));
            basisTmp=ones(size(C,1),NSite);
            for jj=1:size(C,1)
                basisTmp(jj,C(jj,:))=0;
            end
        end
        

%         tmp=padarray(tmp,(NSite-length(tmp)),'post')';
%         basisTmp=perms(tmp);
%         basisTmp=unique(basisTmp,'rows');
        
        basis=cat(1,basis,basisTmp);
        
        
    end
end
basis=unique(basis,'rows');
HSpaceSize=size(basis,1);

disp(['Hspace=',num2str(HSpaceSize)])
disp(' ')

inindex=find(sum(abs(basis-initialstate),2)==0);
initialstate=zeros(HSpaceSize,1);
initialstate(inindex)=1;

%% H constraction
tic
% Hopping
T=zeros(HSpaceSize);
% T=sparse(T);
for ii=1:HSpaceSize
    for jj=1:HSpaceSize
        tmp=basis(ii,:)-basis(jj,:);
        if length(find(tmp))==2
%                 if periotic==1
%                     hopping=[tmp(2:end),tmp(1)].*tmp;
%                 else
                hopping=[tmp(2:end),0].*tmp;
%                 end
            if length(find(hopping))==1
                if sum(hopping)==-1
                    sit1=find(tmp==1);
                    sit2=find(tmp==-1);
%                         if (sit1+sit2==5)||(sit1+sit2==9)
                        T(ii,jj)=-sqrt(max(basis(ii,sit1),basis(jj,sit1)))*...
                            sqrt(max(basis(ii,sit2),basis(jj,sit2)));
%                             disp([ii,jj,T(ii,jj)])
%                         else
%                             T(ii,jj)=-sqrt(max(basis(ii,sit1),basis(jj,sit1)))*...
%                                 sqrt(max(basis(ii,sit2),basis(jj,sit2)))*t2;
%                         end
                end
            end
        end
    end
end

% Interection
U=zeros(HSpaceSize);
% U=sparse(U);
for ii=1:HSpaceSize
    tmp=basis(ii,:);
    U(ii,ii)=sum(tmp.*(tmp-1))*0.5;
end

disp('H Constracted')
toc
disp(' ')
%% Evolution
t1_l=[12];%between
t2_l=t1_l;%inside
u_l=20;
Delta=0;
delta_insideDW_l=0;


% gauge (2,0) or (1,3)
up=(mod(1:NSite,4)==2).*(basis==0)+(mod(1:NSite,4)==0).*(basis==2);
down=(mod(1:NSite,4)==2).*(basis==2)+(mod(1:NSite,4)==0).*(basis==0);

phi_total=[];
for qq=1:length(t1_l)
    t1=t1_l(qq);
    t2=t1;
    u=u_l(qq);
    delta_insideDW=delta_insideDW_l(qq);
    
    T1=T*t1;
    U1=U*u;

    % Stagger
    D=zeros(HSpaceSize);
    D=sparse(D);
    for ii=1:HSpaceSize
        tmp=basis(ii,:);
        D(ii,ii)=...
            +sum((NSite-1:-1:0).*tmp)*Delta...
                +sum(-(~mod(1:NSite,2)).*tmp+mod(1:NSite,2).*tmp)*delta_insideDW*0.5;

    %             sum([1 1 0 0 0 0 0 0].*tmp.*2+[0 0 1 1].*tmp)*delta+...
    end
    
    H=T1+U1+D;
    [V,E]=eig(H);
    E=diag(E);
    
    tspan=0.5;
    Nt=200;
    time=linspace(0,tspan,Nt);

    tic
    Evolution=expm(-1i*2*pi*H*time(2));
    disp('Transfer Matrix Caculated')
    toc
    disp(' ')
    %%
    phi=zeros(HSpaceSize,length(time));
    odd=zeros(1,length(time));
    even=zeros(1,length(time));
    eps=zeros(1,length(time));
    g=zeros(1,length(time));
    oddSingle=zeros(1,length(time));
    singleOccu=(mod(basis,2));
    tic
    tmpState=initialstate;
    
   
    for tt=1:length(time)
        

        tmp=abs(tmpState).^2;
        g(tt)=sum(tmpState.*initialstate);
    %     tmp=abs(expm(-1i*2*pi*H*time(tt))*initialstate).^2;
        phi(:,tt)=tmp;
        odd(tt)=sum(sum(singleOccu.*mod(1:NSite,2),2).*tmp);
        even(tt)=sum(sum(singleOccu.*(~mod(1:NSite,2)),2).*tmp);
        
%         disp(sum((up-down).*tmp,1))
        eps(tt)=sum(sum((-up+down).*tmp,1))*0.5;
        
        oddSingle(tt)=sum(sum(singleOccu.*mod(1:NSite,2).*tmp,1));
%         Num(tt,:)=sum(tmp.*basis,1);
        

        plot(time,phi(1,:),'--','LineWidth',1.2)
        hold on
        plot(time,phi(2,:),'-','LineWidth',1.2)
        hold on
        plot(time,phi(3,:),'--','LineWidth',1.2)

        % Add labels
        xlabel('time') % Label for x-axis
        ylabel('probability') % Label for y-axis
        title(sprintf('J: %.1f , U: %.1f , Delta: %.1f', t1_l(1),u_l(1),Delta))
        
        % Add legend
        legend('20', '11', '02')

        grid on
        hold off
        ylim([0 1])
        drawnow

        tmpState=Evolution*tmpState;
    end
    disp('Evolution Finished')
    toc
    
    %%
    L=abs(g).^2;
    lambda=-log(L)/(2*NSite);
    
    phi_total=cat(3,phi_total,phi);

end

%%
yticks([0:0.5:1])
xticks([0:0.125:0.5])
xticklabels({})
% legend('left','right')

set(findall(gcf,'-property','FontSize'),'FontSize',18)
set(findall(gcf,'-property','FontName'),'FontName','Times New Roman')

savingdir='C:\Users\naeve\Ferdy-Repo\merging_tweezer_code\hubbard_matlab';
set(gcf,'Renderer','painters')
print(gcf,[savingdir,'\','HOM',title_ini,'.svg'],'-dsvg')

% 
% figure('Position',[0 0 500 500])
% imagesc(T)
% axis equal
% axis off
% colormap jet

% figure(2)
% hold on
% errorbar(expdata65(:,1),expdata65(:,2)/2,expdata65(:,3)/2,'o')
% 
% figure(1)
% hold on
% % plot(linspace(0,120,199),RBData(:,2)*1.5,'-')
% plot(zhoudata(:,1),zhoudata(:,2),'-')
% 
% xlim([0,20])
% errorbar(expdata109(:,1),expdata109(:,2),expdata109(:,3),'o')