% 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 (Ref: Gurnett, 2005, p387)
% transform to matrix eigenvalue problem lambda*X=M*X
% Ronnmark1982, eight-pole approximation for Z(zeta)=-sum(b_j/(zeta-c_j))
close all; clear; clc;

N=10;

%% read input parameters
par = importdata('pdrk.in', ' ', 1); 
[S, col]=size(par.data);
if(col~=5)
    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
    Ts(s)=par.data(s,4); % temperature
    vs0(s)=par.data(s,5); % drift velocity
end
vts=sqrt(2*Ts./ms); % thermal velocity
lambdaDs=sqrt(Ts./(ns0.*qs.^2)); % Debye length
kDs=1./lambdaDs;
wps=sqrt(ns0.*qs.^2./ms); % plasma frequency
wcs=qs./ms; % B0=1.0; cyclon frequency
rhocs=sqrt(Ts./ms)./wcs; % cyclotron radius


% Ronnmark1982
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;

kk=[]; ww=[];
kpara=0.0;
tic;
for k=0.05:0.1:5
    kperp=k;
    bs=kperp*rhocs;
    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);
%             aSNJ=aSNJ-(kDs(s)^2/k^2)*Gamn;
            for j=1:J % poles of Z(zeta)
                snj=snj+1;
                csnj(snj)=n*wcs(s)+czetaj(j)*kpara*vts(s);
                bsnj(snj)=(kDs(s)^2/k^2)*Gamn*bzetaj(j)*csnj(snj);
                aSNJ=aSNJ-(kDs(s)^2/k^2)*Gamn*(bzetaj(j));
            end
        end
    end

    for snj=1:SNJ
        M(snj,:)=bsnj(snj);
        M(snj,snj)=bsnj(snj)+aSNJ*csnj(snj);
    end

    d=eig(M);
    omega=d;
    [wi,ind]=sort(abs(real(omega)),'ascend');
    w=omega(ind);
    
    kk=[kk,k];
    ww=[ww,w];
end
runtime=toc;
%%
h=figure('unit','normalized','Position',[0.01 0.4 0.65 0.4],...
    'DefaultAxesFontSize',15);
subplot(121);
for j=1:SNJ
%     ind=find(imag(ww)<-1e-3); ww(ind)=NaN;    
%     if(min((imag(ww(j,:))))<-2e-3)
        plot(kk,real(ww(j,:)),'r*','LineWidth',2);
%         plot(kk,imag(ww(j,:)),'r*','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
wuh=sqrt(wcs(1)^2+wps(1)^2); % UH wave at cold limit (k*rho -> 0)
plot([0,max(kk)],[wuh,wuh],'b--','LineWidth',2); 
text(0,wuh,'\omega_{UH}','HorizontalAlignment','right');
% title(['\omega_p/\omega_c=',num2str(wps(1)/wcs(1)),...
%     ', N=',num2str(N),', runtime=',num2str(runtime),'s']);
title(['(a) PDRK solutions, \omega_p/\omega_c=',num2str(abs(wps(1)/wcs(1))),...
    ', N=',num2str(N)]);
xlim([0,max(kk)]);
ylim([0,5.5]);
xlabel('k_{\perp}\rho_c'); ylabel('\omega_r/\omega_c');
print('-dpng',['pdrk_es_harris_wp',num2str(wps(1)/wcs(1)),...
    '_N',num2str(N),'.png']);

% kk=[]; ww=[];
% tic;
% for k=0.1:0.1:5
%     betac=k*rhoc; % k=kperp
%     M=zeros(N,N);
%     for n=1:N
%         cn=n^2*wc^2;
%         Gamn=exp(-betac^2)*besseli(n,betac^2);
%         bn=2*wp^2*n^2*Gamn/betac^2;
%         M(n,:)=bn;
%         M(n,n)=bn+cn;    
%     end
%     d=eig(M);
%     omega=sqrt(d);
%     [wr,ind]=sort(real(omega),'ascend');
%     w=omega(ind);
%     kk=[kk,k];
%     ww=[ww,w(1:5)];
% end
% runtime=toc;
% %%
% h=figure('unit','normalized','Position',[0.01 0.57 0.5 0.35],...
%     'DefaultAxesFontSize',15);
% plot(kk,real(ww(1,:)),'r*-',kk,real(ww(2,:)),'r*-',kk,real(ww(3,:)),...
%     'r*-',kk,real(ww(4,:)),'r*-','LineWidth',2);
% hold on;
% plot([0,max(kk)],[wc,wc],'g--',[0,max(kk)],[2*wc,2*wc],'g--',...
%     [0,max(kk)],[3*wc,3*wc],'g--',[0,max(kk)],[4*wc,4*wc],'g--','LineWidth',2);
% title(['Eletron Bernstein modes, \omega_p/\omega_c=',num2str(wp/wc),...
%     ', N=',num2str(N),', runtime=',num2str(runtime),'s']);
% xlabel('k'); ylabel('\omega_r');
% xlim([0,max(kk)]); ylim([0,5*wc]);
% 
% 
% print('-dpng',['pdrk_electron_benstein_wp',num2str(wp/wc),...
%     '_N',num2str(N),'.png']);


