% Hua-sheng XIE, huashengxie@gmail.com, IFTS-ZJU, 2014-05-24 17:45
% pdrk.m, Plasma Dispersion Relastion solver, Kinetic -> ES magnetized 
% -> Harris dispersion relation
% Transform to matrix eigenvalue problem lambda*X=M*X.
% Ronnmark1982, eight-pole approximation for Z(zeta)=-sum(b_j/(zeta-c_j))
% 
% Ref: [1] Gurnett, 2005, p387 [2] HuXiWei2006, p458
% Now, the problem is, too many roots! How to select the roots we want?
%
% This version, drift Bi-Maxwellian

% close all; 
clear; clc;

plt=2;

N=4; B0=1.0;

%% read input parameters
if(plt==2)
    par = importdata('pdrk2.in', ' ', 1); 
else
    par = importdata('pdrk.in', ' ', 1); 
end
[S, col]=size(par.data);
if(col~=6)
    disp('Wrong input data !!!');
end

for s=1:S
    qs(s)=par.data(s,1); % charge
    ms(s)=par.data(s,2); % mass
    ns0(s)=par.data(s,3); % desity
    Tzs(s)=par.data(s,4); % parallel temperature
    Tps(s)=par.data(s,5); % perp temperature
    vs0(s)=par.data(s,6); % para drift velocity
end
vtzs=sqrt(2*Tzs./ms); % para thermal velocity
lambdaDs=sqrt(Tzs./(ns0.*qs.^2)); % Debye length, Tzs or Tps?
kDs=1./lambdaDs;
wps=sqrt(ns0.*qs.^2./ms); % plasma frequency
wcs=B0*qs./ms; % cyclon frequency
rhos=sqrt(Tps./ms)./abs(wcs); % cyclotron radius


% Ronnmark1982, eight-pole approximation for Z(zeta)
bzetaj(1)=-1.734012457471826E-2-4.630639291680322E-2i;
bzetaj(2)=-7.399169923225014E-1+8.395179978099844E-1i;
bzetaj(3)=5.840628642184073+9.536009057643667E-1i;
bzetaj(4)=-5.583371525286853-1.120854319126599E1i;
czetaj(1)=2.237687789201900-1.625940856173727i;
czetaj(2)=1.465234126106004-1.789620129162444i;
czetaj(3)=.8392539817232638-1.891995045765206i;
czetaj(4)=.2739362226285564-1.941786875844713i;

bzetaj(1:4)=-bzetaj(1:4); %

bzetaj(5:8)=conj(bzetaj(1:4));
czetaj(5:8)=-conj(czetaj(1:4));

J=8;
SNJ=S*(2*N+1)*J;

tic;
if(plt==2)
    [kz,kp]=meshgrid((0.8:0.1:2.8)./(sqrt(2)*rhos(1)),(0.4:0.05:1.8)./(sqrt(2)*rhos(1)));
else
    [kz,kp]=meshgrid((1.0:0.125:5.5)./(sqrt(2)*rhos(1)),(0.4:0.05:1.8)./(sqrt(2)*rhos(1)));
end
[Jkz,Jkp]=size(kz);
% kk=[]; 
ww=zeros(Jkz,Jkp,2);
for jkz=1:Jkz
for jkp=1:Jkp
    kpara=kz(jkz,jkp); kperp=kp(jkz,jkp);
    k=sqrt(kpara^2+kperp^2);
    bs=kperp*rhos;
    M=zeros(SNJ,SNJ);
    snj=0;
    aSNJ=1;
    for s=1:S % species 
        aSNJ=aSNJ+(kDs(s)^2/k^2);
        for n=-N:N % Bessel function
            Gamn=exp(-bs(s)^2)*besseli(n,bs(s)^2);
            for j=1:J % poles of Z(zeta)
                snj=snj+1;
                csnjtmp=n*wcs(s)+czetaj(j)*kpara*vtzs(s);
                csnj(snj)=kpara*vs0(s)+csnjtmp;
                bsnj(snj)=(kDs(s)^2/k^2)*Gamn*bzetaj(j)*(csnjtmp-...
                    (1-Tzs(s)/Tps(s))*n*wcs(s));
                aSNJ=aSNJ-(kDs(s)^2/k^2)*Gamn*(bzetaj(j));
            end
        end
    end

    for snj=1:SNJ % set the eigen matrix
        M(snj,:)=bsnj(snj);
        M(snj,snj)=bsnj(snj)+aSNJ*csnj(snj);
    end

    d=eig(M); % solve the roots
    omega=d;
    [wi,ind]=sort(imag(omega),'descend');
%     [wi,ind]=sort(abs(real(omega)),'ascend');
    w=omega(ind);
    
%     kk=[kk,k];
    ww(jkz,jkp,1:2)=w(1:2);
end
end
runtime=toc;


%%
kz=kz*(sqrt(2)*rhos(1)); kp=kp*rhos(1);
ww=ww/wps(1);

%%
if(plt==1)
h=figure('unit','normalized','Position',[0.01 0.37 0.6 0.55],...
    'DefaultAxesFontSize',15);
subplot(121);
else
subplot(122);
end
% % for j=1:SNJ %j=(SNJ-4):(SNJ)
% for j=1:4
%     wwt=ww;
% %     ind=find(real(ww)<=0); wwt(ind)=NaN;
% %     ind=find(imag(ww)./abs(ww)<-1e-2); wwt(ind)=NaN+1i*NaN;
% %     ind=find(abs(real(ww))<2e-1); wwt(ind)=NaN+1i*NaN;
% %     if(sum(abs(imag(ww(j,:))))<2e-1)
%         plot(kk,real(wwt(j,:)),'r*',kk,imag(wwt(j,:)),'b*','LineWidth',2);
%         hold on;
% %     end
% end
% for n=0:6
%     plot([0,max(kk)],[n*abs(wcs(1)),n*abs(wcs(1))],'g--','LineWidth',2);
% % end
% title(['ES magnetized plasma modes, \omega_p/\omega_c=',...
%     num2str(wps(1)/wcs(1)),...
%     ', N=',num2str(N),', runtime=',num2str(runtime),'s']);
% xlim([0,max(kk)]);
% ylim([0,2*abs(wcs(1))]);
% xlabel('k'); ylabel('\omega_r');
% print('-dpng',['pdrk_es_harris_wp',num2str(wps(1)/wcs(1)),...
%     '_N',num2str(N),'.png']);

if(plt==2)
    v=0:0.02:0.2;
else
    v=0:0.03:0.21;
end
[C,h] =contour(kz,kp,imag(ww(:,:,1)),v,'LineWidth',2);
clabel(C,h);

% title(['Anisotropic Harris instability, \omega_p/\omega_c=',...
%     num2str(wps(1)/wcs(1)),', T_{\perp}/T_{||}=',num2str(Tps(1)/Tzs(1)),...
%     ', N=',num2str(N),', runtime=',num2str(runtime),'s']);
if(plt==1)
title(['(a) \omega_p/\omega_c=',...
    num2str(wps(1)/wcs(1)),', T_{\perp}/T_{||}=',num2str(Tps(1)/Tzs(1)),...
    ', N=',num2str(N)]);
else
title(['(b) \omega_p/\omega_c=',...
    num2str(wps(1)/wcs(1)),', T_{\perp}/T_{||}=',num2str(Tps(1)/Tzs(1)),...
    ', N=',num2str(N)]);
end
xlabel('k_{||}\rho_c');ylabel('k_{\perp}\rho_c');

print('-dpng',['pdrk_es_harris_wp',num2str(wps(1)/wcs(1)),...
    '_TpTz',num2str(Tps(1)/Tzs(1)),'_N',num2str(N),'.png']);

