% close all;clear;clc;
% h=figure('unit','normalized','Position',[0.01 0.37 0.4 0.55],...
%     'DefaultAxesFontSize',15);

global dwk

c2=1e0;

vthe=sqrt(c2)*sqrt(1)/5;  % c=1;
wpe=3;  % wce=1;

mi=4;  % me=1;

Ti=1; % Te=1;

vthi=vthe*sqrt(Ti/mi);
wpi=wpe*sqrt(1/mi);
wci=1/mi;

dwk.vthe=vthe;dwk.vthi=vthi;
dwk.wpe=wpe;dwk.wpi=wpi;
dwk.wce=1;dwk.wci=wci;

w=[];wlm=[];wrm=[];wes=[];wlm2=[];wrm2=[];
k=9.52:-0.5:0.01;
% k=0.02:0.5:10.02;
% k=k/10;
[row,col]=size(k);

x0=(2.2-0.001i);
xr0=(0.1-0.1i); % initial guess
xl0=(1.8-3.2i);
xl20=(10-0.0i);
xr20=(10+0.00i);
xes0=x0; xlm0=xl0; xrm0=xr0; xlm20=xl20; xrm20=xr20;
% if(plt==2)
%     xr0=xl0;
% end

% for jk=1:col
for jk=1:col
    dwk.kc=k(jk)*sqrt(c2);
    warning off;
    options=optimset('Display','off');
    
    dwk.left=0;dwk.em=1; % ES
    [x,fval]=fsolve(@em_kntc_fun,xes0,options);
    if(fval<10^(-4))
        wes=[wes;x];
        wesr=real(wes);wesi=imag(wes);  % wr, numerical solution of omega real
        xes0=x;
    end
    
    dwk.left=1;dwk.em=1; % left pol, EM
    [x,fval]=fsolve(@em_kntc_fun,xlm0,options);
    if(fval<10^(-4))
        wlm=[wlm;x];
        wlmr=real(wlm);wlmi=imag(wlm);  % wr, numerical solution of omega real
        xlm0=x;
    end
    
    dwk.left=-1;dwk.em=1; % rigth pol, EM
    [x,fval]=fsolve(@em_kntc_fun,xrm0,options);
    if(fval<10^(-4))
        wrm=[wrm;x];
        wrmr=real(wrm);wrmi=imag(wrm);  % wr, numerical solution of omega real
        xrm0=x;
    end
    
    dwk.left=1;dwk.em=1; % left pol, EM
    [x,fval]=fsolve(@em_kntc_fun,xlm20,options);
    if(fval<10^(-4))
        wlm2=[wlm2;x];
        wlm2r=real(wlm2);wlm2i=imag(wlm2);  % wr, numerical solution of omega real
        xlm20=x;
    end
    
    dwk.left=-1;dwk.em=1; % rigth pol, EM
    [x,fval]=fsolve(@em_kntc_fun,xrm20,options);
    if(fval<10^(-4))
        wrm2=[wrm2;x];
        wrm2r=real(wrm2);wrm2i=imag(wrm2);  % wr, numerical solution of omega real
        xrm20=x;
    end
        
end
%%

plt=2;
if (plt==1)
    h=figure('unit','normalized','Position',[0.01 0.45 0.7 0.45],...
        'DefaultAxesFontSize',15);
	subplot(121);
    title(['(a) \omega_{pe}=',num2str(wps(1)/wcs(1)),', m_i/m_e=',num2str(ms(2)/ms(1)),', N=',num2str(N),...
        ', \theta=',num2str(atan(kx(1)/kz(1))*180/pi)]);
    xlim([min(kk),max(kk)]); ylim([0,15]); box on;
    xlabel('kc/\omega_{ce}'); ylabel('\omega_r/\omega_{ce}');
    hold on;
    
    subplot(122);
    xlim([min(kk),max(kk)]); box on;
    xlabel('kc/\omega_{ce}'); ylabel('\omega_i/\omega_{ce}');
    title(['(b) J=',num2str(J),', NN=',num2str(NN)]);
%     title(['(b) J=',num2str(J),', NN=',num2str(NN),', runtime=',num2str(runtime),'s']);
    hold on;
else
    axes('position',[0.17 0.65 0.23 0.23]); box on;
    xlim([min(kk),max(kk)]);
    ylim([0.03,0.5]);
    hold on;
end

if(plt==1)
    subplot(121);
    plot(k,k,'k--');
%     plot(k,k,'g--','LineWidth',2);
    % legend('Re(EM)','Re(Darwin)');legend('boxoff');
    hold on;plot([0,max(k)],[wpe,wpe],'k--',[0,max(k)],[1,1],'k--',...
        [0,max(k)],[wpi,wpi],'k--',[0,max(k)],[wci,wci],'k--');
    ylabel('$\omega/\omega_{ce}$','Interpreter','LaTex');
    xlabel('$kc/\omega_{ce}$','Interpreter','LaTex');

    xmax=max(k); ymax=1.5;
    text(xmax,1,'\omega_{ce}');text(xmax,wpe,'\omega_{pe}');
    text(xmax,wci,'\omega_{ci}');text(xmax,wpi,'\omega_{pi}');
    xlim([0,xmax]);
    % ylim([0,ymax]);

    subplot(122);
    % legend('Im(EM)','Im(Darwin)');legend('boxoff');
    ylabel('$\gamma/\omega_{ce}$','Interpreter','LaTex');
    xlabel('$kc/\omega_{ce}$','Interpreter','LaTex');
    xlim([0,xmax]);

    subplot(121);
    hold on;
    plot(k,wesr,'g-',k,wlmr,'r-',k,wrmr,'b-',k,wlm2r,'b--',k,wrm2r,'b--','LineWidth',2);
    subplot(122);
    hold on;
    plot(k,wesi,'g-',k,wlmi,'r-',k,wrmi,'b-',k,wlm2i,'b--',k,wrm2i,'b--','LineWidth',2);
else
    hold on;
    plot(k,wesr,'g-',k,wlmr,'r-',k,wrmr,'b-',k,wlm2r,'b--',k,wrm2r,'b--','LineWidth',2);   
end
