% Hua-sheng XIE, huashengxie@gmail.com, IFTS-ZJU, 2014-06-01 17:00
% pdrk.m, Plasma Dispersion Relastion solver, Maxwellian distribution
% Transform to matrix eigenvalue problem lambda*X=M*X.
% Ronnmark1982, eight-pole approximation for Z(zeta)=-sum(b_j/(zeta-c_j))
% 
% sparse, eigs

% close all; clear; clc;

N=2; B0=1.0;

% % read input parameters
par = importdata('pdrk.in', ' ', 1); 
[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
rhocs=sqrt(Tps./ms)./abs(wcs); % cyclotron radius

wps2=wps.^2;
lmdT=Tzs./Tps;

c2=1e4; % light speed

J=8;
if(J==4)
    % Martin1980, 4-pole
    bzj(1)=0.546796859834032 + 0.037196505239277i;
    bzj(2)=-1.046796859834027 + 2.101852568038518i;
    czj(1)=1.23588765343592 - 1.21498213255731i;
    czj(2)=-0.378611612386277 - 1.350943585432730i;
    
    bzj(3:4)=conj(bzj(1:2));
    czj(3:4)=-conj(czj(1:2));
else % J==8
    % Ronnmark1982, 8-pole for Z function, and Z'
    bzj(1)=-1.734012457471826E-2-4.630639291680322E-2i;
    bzj(2)=-7.399169923225014E-1+8.395179978099844E-1i;
    bzj(3)=5.840628642184073+9.536009057643667E-1i;
    bzj(4)=-5.583371525286853-1.120854319126599E1i;
    czj(1)=2.237687789201900-1.625940856173727i;
    czj(2)=1.465234126106004-1.789620129162444i;
    czj(3)=.8392539817232638-1.891995045765206i;
    czj(4)=.2739362226285564-1.941786875844713i;

    bzj(5:8)=conj(bzj(1:4));
    czj(5:8)=-conj(czj(1:4));
end
J=length(bzj);
% sum(bzj) % = -1
% sum(bzj.*czj) % = 0

SNJ=S*(2*N+1)*J;
SNJ1=SNJ+1;
SNJ3=3*SNJ1;
NN=SNJ3+6;


sp=0; % sparse eigs() or eig()
if(sp==0)
    nw=NN; % number of roots to obtain
else
    nw=2;
end
wg=1.5; % inital guess

kk=[]; ww=[];
tic;
for kj=9.52:-0.5:0.01
%     kz=kj/5; kx=kz/2;
%     kz=kj/100; kx=kj/1;
    kz=1*kj/100; kx=0.00*kj/100;
    k=sqrt(kz^2+kx^2);
    bs=kx*rhocs;
    bs(abs(bs)<1e-50)=1e-50;  % to avoid singular when k_perp=0       
    
    bs2=bs.^2;
    M=sparse(NN,NN);
    snj=0;
    
    b11=0; b12=0; b13=0; b21=0; b22=0; b23=0; b31=0; b32=0; b33=0;
    for s=1:S % species 
        for n=-N:N % Bessel function
            Gamn=exp(-bs2(s))*besseli(n,bs2(s));
            Gamnp=exp(-bs2(s))*(besseli(n+1,bs2(s))+...
                besseli(n-1,bs2(s))-2*besseli(n,bs2(s)))/2;
            for j=1:J % poles of Z(zeta)
                snj=snj+1;
                
                csnj(snj)=czj(j)*kz*vtzs(s)-n*wcs(s);
                
                tmp1=wps2(s)*bzj(j); tmp2=tmp1*czj(j);
                
                b11snj(snj)=tmp1*n^2*Gamn/bs2(s);
                b12snj(snj)=tmp1*1i*n*Gamnp;
                b21snj(snj)=-b12snj(snj);
                b22snj(snj)=tmp1*(n^2*Gamn/bs2(s)-2*bs2(s)*Gamnp);
                b13snj(snj)=tmp2*n*sqrt(2*lmdT(s))*Gamn/bs(s);
                b31snj(snj)=b13snj(snj);
                b23snj(snj)=-1i*tmp2*sqrt(2*lmdT(s))*Gamnp*bs(s);
                b32snj(snj)=-b23snj(snj);
                b33snj(snj)=tmp2*czj(j)*2*lmdT(s)*Gamn;
            end
        end
    end

    for snj=1:SNJ % set the eigen matrix
        jjx=snj+0*SNJ1;
        jjy=snj+1*SNJ1;
        jjz=snj+2*SNJ1;
        % v_snjx
        M=M+sparse(jjx,jjx,csnj(snj),NN,NN)+...
            sparse(jjx,SNJ3+1,b11snj(snj),NN,NN)+...
            sparse(jjx,SNJ3+2,b12snj(snj),NN,NN)+...
            sparse(jjx,SNJ3+3,b13snj(snj),NN,NN);
        
        % v_snjy
        M=M+sparse(jjy,jjy,csnj(snj),NN,NN)+...
            sparse(jjy,SNJ3+1,b21snj(snj),NN,NN)+...
            sparse(jjy,SNJ3+2,b22snj(snj),NN,NN)+...
            sparse(jjy,SNJ3+3,b23snj(snj),NN,NN);
        
        % v_snjz
        M=M+sparse(jjz,jjz,csnj(snj),NN,NN)+...
            sparse(jjz,SNJ3+1,b31snj(snj),NN,NN)+...
            sparse(jjz,SNJ3+2,b32snj(snj),NN,NN)+...
            sparse(jjz,SNJ3+3,b33snj(snj),NN,NN);
  
    end
%     jj=1:SNJ3;
    tp=-1;
%     jj=1:(3*SNJ); M(SNJ3+1,jj)=tp;
%     jj=(3*SNJ+1):(6*SNJ); M(SNJ3+2,jj)=tp;
%     jj=(6*SNJ+1):(9*SNJ); M(SNJ3+3,jj)=tp;
    jj=(0*SNJ1+1):(1*SNJ1); ii=0.*jj+SNJ3+1; M=M+sparse(ii,jj,tp,NN,NN);
    jj=(1*SNJ1+1):(2*SNJ1); ii=0.*jj+SNJ3+2; M=M+sparse(ii,jj,tp,NN,NN);
    jj=(2*SNJ1+1):(3*SNJ1); ii=0.*jj+SNJ3+3; M=M+sparse(ii,jj,tp,NN,NN);
    
    % jx(E), jy(E), jz(E)
    M=M+sparse(1*SNJ1,SNJ3+1,b11,NN,NN)+...
        sparse(1*SNJ1,SNJ3+2,b12,NN,NN)+...
        sparse(1*SNJ1,SNJ3+3,b13,NN,NN)+...
        sparse(2*SNJ1,SNJ3+1,b21,NN,NN)+...
        sparse(2*SNJ1,SNJ3+2,b22,NN,NN)+...
        sparse(2*SNJ1,SNJ3+3,b23,NN,NN)+...
        sparse(3*SNJ1,SNJ3+1,b31,NN,NN)+...
        sparse(3*SNJ1,SNJ3+2,b32,NN,NN)+...
        sparse(3*SNJ1,SNJ3+3,b33,NN,NN);
    
    % E(B)
%     M(SNJ3+1,SNJ3+5)=c2*kz;
%     M(SNJ3+2,SNJ3+4)=-c2*kz;
%     M(SNJ3+2,SNJ3+6)=c2*kx;
%     M(SNJ3+3,SNJ3+5)=-c2*kx;
    M=M+sparse(SNJ3+1,SNJ3+5,c2*kz,NN,NN)+...
        sparse(SNJ3+2,SNJ3+4,-c2*kz,NN,NN)+...
        sparse(SNJ3+2,SNJ3+6,c2*kx,NN,NN)+...
        sparse(SNJ3+3,SNJ3+5,-c2*kx,NN,NN);
    
    % B(E)
%     M(SNJ3+4,SNJ3+2)=-kz;
%     M(SNJ3+5,SNJ3+1)=kz;
%     M(SNJ3+5,SNJ3+3)=-kx;
%     M(SNJ3+6,SNJ3+2)=kx;
    M=M+sparse(SNJ3+4,SNJ3+2,-kz,NN,NN)+...
        sparse(SNJ3+5,SNJ3+1,kz,NN,NN)+...
        sparse(SNJ3+5,SNJ3+3,-kx,NN,NN)+...
        sparse(SNJ3+6,SNJ3+2,kx,NN,NN);
    if(sp==1)
        d=eigs(M,nw,wg); % solve the roots
    else
        d=eig(full(M));
    end
    omega=d;
    [wi,ind]=sort(imag(omega),'descend');
%     [wr,ind]=sort(real(omega),'descend');
    w=omega(ind);
    
    kk=[kk,k];
    ww=[ww,w];
end
runtime=toc;


%
% kk=kk*rhocs(1);
kk=kk*sqrt(c2);

%%

wwp=[]; jp=0;
for j=1:nw
    wwt=ww;
%     if(max(abs(real(wwt(j,:))))<10 && min(imag(wwt(j,:)))>-0.4e-0)
%     if(min(abs(real(wwt(j,:))))>1e-1 &&  min(imag(wwt(j,:)))/max(abs(real(wwt(j,:))))>-0.4e-1)
%     if(min(imag(wwt(j,:)))/max(abs(real(wwt(j,:))))>-0.96e-0 ...
%             && max(abs(real(wwt(j,:))))/max(kk)<2.902/10  || min(imag(wwt(j,:)))>-7.5e-1)
        wwp=[wwp,wwt(j,:)];
        jp=jp+1; % for plot
%     end
end
pltc=[0.0  1.0  0.0
    1.0  0.0  0.0
    0.2  0.2  1.0
    0.8 0.8 0.0
    1.0  0.6  0.0
    0.9  0.0  0.9
    0.0  0.8  0.0
    0.0  0.8  0.8
    0.0  0.0  0.0
    0.6  0.0  0.0
    0.4  0.7  0.4 
    0.0  0.0  0.5 
    0.6  0.0  0.6 
    0.0  0.5  1.0];
if (plt==1) subplot(121); end
for n=1:5
    plot([0,max(kk)],[n*abs(wcs(1)),n*abs(wcs(1))],'c--','LineWidth',2);
    hold on;
end
for j=1:jp
    
    if (plt==1) subplot(121); end
    if(j<1)
        plot(kk,real(wwt(j,:)),'*','Color',pltc(j,:),'LineWidth',2);
    else
        plot(kk,real(wwt(j,:)),'mx','LineWidth',2);
    end
%     ylim([0,10.0]);
    hold on;
    
    if(plt==1)
        subplot(122); 
        if(j<1)
            plot(kk,imag(wwt(j,:)),'x','Color',pltc(j,:),'LineWidth',2);
        else
            plot(kk,imag(wwt(j,:)),'mx','LineWidth',2);
        end
    %     ylim([-0.25,0.05]);
        hold on;
    end
end

print('-dpng',['pdrk_em_wp',num2str(wps(1)/wcs(1)),...
    '_N',num2str(N),'_J',num2str(J),'_sp',num2str(sp),'_nw',num2str(nw),'.png']);

