% Coupled Dipole Model
close(gcf);
clear all;

% Simulation parameters (42 seconds computing time with parameters below)
Gamma=1; %inverse lifetime
Delta=0; % Detuning (in units of Gamma)
Omega=0.1; %Rabi frequency of laser drive (in units of Gamma)
Nat=10000; % atom number
kR=50; % cloud radius in units of 1/k
dt=1/200; % time step for simulation
Tpulse=100; % duration of laser pulse (in untis of Gamma-1)
Tmax=20; % duration of simulation(in untis of Gamma-1)
      
% Homogeneous cloud (for gaussian cloud replace 'rand' by 'randn')
X=(rand(Nat,1)-0.5)*kR;  
Y=(rand(Nat,1)-0.5)*kR;  
Z=(rand(Nat,1)-0.5)*kR;  
% computing relative distances
xjm=X*ones(1,Nat)-ones(Nat,1)*X';
yjm=Y*ones(1,Nat)-ones(Nat,1)*Y';
zjm=Z*ones(1,Nat)-ones(Nat,1)*Z';
rjm=sqrt(xjm.^2+yjm.^2+zjm.^2)+diag(ones(Nat,1)); % diagonal term added to avoid divergence of the Green function

% Green function of dipole dipole coupling
Vjm=exp(1i*rjm)./(1i*rjm); % 1/R long range dipole dipole coupling
V=Vjm-diag(diag(Vjm)); %removing artificially non-zero diagonal terms
% Adding free single atom evolution with dipole dipole coupling
M=(-Gamma/2+1i*Delta)*eye(Nat)-Gamma/2*Vjm; % 'eye' = ones on the diagonal
% Plave wave excitation
Om=1i*Omega*exp(1i*Z)/2; %plane wave excitation
% Om=1i*Omega.*cos(Z).^2.*exp(1i*Z)/2; %stat wave excitation
Om=1i*Omega*exp(1i*Z)/2+1i*Omega*exp(-1i*Z)/2; %2 wave excitation
Beta_SteadyState=M\Om; % steady-state solution for d beta / dt = M beta +Om


% time dynamics with all atoms intially in the ground state
beta=zeros(Nat,Tmax/dt); % initialization of time dependent dipole amplitudes
%Switch-on
for it=2:1:Tpulse/dt %even very small time steps with this simple time solver is not good
beta(:,it)=beta(:,it-1)+dt*(M*beta(:,it-1)+Om);
pop1(it)=sum(abs(beta(:,it)));
end
for it=2:1:Tpulse/dt %Kunge Kutta : a well known better solution
    k1=M*beta(:,it-1)+Om;
    k2=M*(beta(:,it-1)+k1*dt/2)+Om;
    k3=M*(beta(:,it-1)+k2*dt/2)+Om;
    k4=M*(beta(:,it-1)+k3*dt)+Om;
    beta(:,it)=beta(:,it-1)+(dt/6)*(k1+2*k2+2*k3+k4);
    popRK(it)=sum(abs(beta(:,it)).^2);
end

%Switch-off
for it=Tpulse/dt+1:1:Tmax/dt %Kunge Kutta : a well known better solution
    k1=M*beta(:,it-1); %same equation but with Om=0
    k2=M*(beta(:,it-1)+k1*dt/2); %same equation but with Om=0
    k3=M*(beta(:,it-1)+k2*dt/2);%same equation but with Om=0
    k4=M*(beta(:,it-1)+k3*dt);%same equation but with Om=0
    beta(:,it)=beta(:,it-1)+(dt/6)*(k1+2*k2+2*k3+k4);
    popRK(it)=sum(abs(beta(:,it)).^2);
end

% Single atom decay reference
 beta1=ones(1,Tmax/dt);
for it=Tpulse/dt+1:1:Tmax/dt
    beta1(it)=exp(-(it-Tpulse/dt)*dt);
end
%plot((Tmax/dt/2:Tmax/dt)*dt, beta1,'DisplayName','beta1','YDataSource','beta1', 'Color', 'Black');figure(gcf) % Single Atom decay for reference

semilogy((1:Tmax/dt)*dt, popRK(1:Tmax/dt)/popRK(Tpulse/dt),'DisplayName','popRK','YDataSource','popRK');figure(gcf)
hold on;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Simulation parameters (42 seconds computing time with parameters below)
Gamma=1; %inverse lifetime
Delta=-20; % Detuning (in units of Gamma)
Omega=0.1; %Rabi frequency of laser drive (in units of Gamma)
Nat=1000; % atom number
kR=5; % cloud radius in units of 1/k
dt=1/200; % time step for simulation
Tpulse=10; % duration of laser pulse (in untis of Gamma-1)
Tmax=20; % duration of simulation(in untis of Gamma-1)
      
% Homogeneous cloud (for gaussian cloud replace 'rand' by 'randn')
X=(rand(Nat,1)-0.5)*kR;  
Y=(rand(Nat,1)-0.5)*kR;  
Z=(rand(Nat,1)-0.5)*kR;  
% computing relative distances
xjm=X*ones(1,Nat)-ones(Nat,1)*X';
yjm=Y*ones(1,Nat)-ones(Nat,1)*Y';
zjm=Z*ones(1,Nat)-ones(Nat,1)*Z';
rjm=sqrt(xjm.^2+yjm.^2+zjm.^2)+diag(ones(Nat,1)); % diagonal term added to avoid divergence of the Green function

% Green function of dipole dipole coupling
Vjm=exp(1i*rjm)./(1i*rjm); % 1/R long range dipole dipole coupling
V=Vjm-diag(diag(Vjm)); %removing artificially non-zero diagonal terms
% Adding free single atom evolution with dipole dipole coupling
M=(-Gamma/2+1i*Delta)*eye(Nat)-Gamma/2*Vjm; % 'eye' = ones on the diagonal
% Plave wave excitation
Om=1i*Omega*exp(1i*Z)/2; %plane wave excitation
Beta_SteadyState=M\Om; % steady-state solution for d beta / dt = M beta +Om


% time dynamics with all atoms intially in the ground state
beta=zeros(Nat,Tmax/dt); % initialization of time dependent dipole amplitudes
%Switch-on
for it=2:1:Tpulse/dt %even very small time steps with this simple time solver is not good
beta(:,it)=beta(:,it-1)+dt*(M*beta(:,it-1)+Om);
pop1(it)=sum(abs(beta(:,it)));
end
for it=2:1:Tpulse/dt %Kunge Kutta : a well known better solution
    k1=M*beta(:,it-1)+Om;
    k2=M*(beta(:,it-1)+k1*dt/2)+Om;
    k3=M*(beta(:,it-1)+k2*dt/2)+Om;
    k4=M*(beta(:,it-1)+k3*dt)+Om;
    beta(:,it)=beta(:,it-1)+(dt/6)*(k1+2*k2+2*k3+k4);
    popRK(it)=sum(abs(beta(:,it)).^2);
end

%Switch-off
for it=Tpulse/dt+1:1:Tmax/dt %Kunge Kutta : a well known better solution
    k1=M*beta(:,it-1); %same equation but with Om=0
    k2=M*(beta(:,it-1)+k1*dt/2); %same equation but with Om=0
    k3=M*(beta(:,it-1)+k2*dt/2);%same equation but with Om=0
    k4=M*(beta(:,it-1)+k3*dt);%same equation but with Om=0
    beta(:,it)=beta(:,it-1)+(dt/6)*(k1+2*k2+2*k3+k4);
    popRK(it)=sum(abs(beta(:,it)).^2);
end

% Single atom decay reference
 beta1=ones(1,Tmax/dt);
for it=Tpulse/dt+1:1:Tmax/dt
    beta1(it)=exp(-(it-Tpulse/dt)*dt);
end
%plot((Tmax/dt/2:Tmax/dt)*dt, beta1,'DisplayName','beta1','YDataSource','beta1', 'Color', 'Black');figure(gcf) % Single Atom decay for reference

semilogy((1:Tmax/dt)*dt, popRK(1:Tmax/dt)/popRK(Tpulse/dt),'DisplayName','popRK','YDataSource','popRK');figure(gcf)
hold on;


