% clear
addpath 'D:\EM_work\MT3D\bin'
addpath 'D:\EM_work\MT3D\bin\MT1D'
close all

freq = logspace(4,6,11);
% freq = linspace(1e6,1e8,21);

nfreq = length(freq);

% zNode = (-20:1:max(mesh.node2coord(:,3)))';
zNode = (-500:1:1500)';
% zNode = linspace(-1000,5000,1000)';
% zNode =zNode /30;
centerZ  = zNode(1:end-1)+0.5*diff(zNode) ; % 层中心Z坐标
% zNode = zNode/10;
id = find(abs(zNode)== min(abs(zNode)));
zNode(id);

sig1D = 1e-8*ones(size(zNode,1)-1,1);
sig1D(centerZ>0,:)  = 1/1e4;
sig1D(centerZ>10,:) = 1/1e4 ;
sig1D(centerZ>20,:) = 1/1e4 ;

epsr1D  = 1*ones(size(zNode,1)-1,1);
epsr1D(centerZ>0,:) = 5;
% epsr1D(centerZ>20,:) =50;
% epsr1D(centerZ>40,:) =5;

mur1D = ones(size(zNode,1)-1,1);
% mur(centerZ>0,:) = 1;

%

figure(1)
subplot(1,3,1);
stairs( [sig1D(1,1);sig1D(:,1)],zNode,'k',"LineWidth",2)
title('Conductivity');
ylabel('Z(km)') ;xlabel('sigma(S/m)');axis tight;box on;
set(gca,'YDir','reverse','Xscale','log');
subplot(1,3,2);
stairs( [epsr1D(1,1);epsr1D(:,1)],zNode,'k',"LineWidth",2)
title('Permittivity');
ylabel('Z(m)') ;xlabel('epsilon');axis tight;box on;
set(gca,'YDir','reverse');
subplot(1,3,3)
stairs( [mur1D(1,1);mur1D(:,1)],zNode,'k',"LineWidth",2)
title('Permeability')
xlabel('Mur'); set(gca,'YDir','reverse'); axis tight; box on;

%

 
data1 = table;
col = jet(nfreq);
f2 = figure(2);
l={};
for ifreq =1:nfreq
    [eField,hField]         =  mt1DAnalyticFieldsEpsMu(freq(ifreq),sig1D,epsr1D,mur1D,zNode,1,'E', 1);
    % [eField,hField]         =  mt1DAnalyticFields(freq(ifreq),sig1D,zNode,1,'E', 1);

    Ex1 = eField(id).';
    Ey1 = Ex1*0 ;
    Hx1 =  Ex1*0 ;
    Hy1 =  hField(id).' ;
    Hz1 = Ex1*0 ;
    Ex2 = Ey1 ;
    Ey2 = Ex1 ;
    Hx2 = Hy1 ;
    Hy2 = Ex1*0 ;
    Hz2 = Ex1*0 ;

    mtr = get_MTR(Ex1,Ey1,Hx1,Hy1,Hz1,Ex2,Ey2,Hx2,Hy2,Hz2,freq(ifreq));
    Frequency  =  repmat(freq(ifreq),1,1);
    RX   =(1:1)';

    datatemp = table(Frequency, RX,Ex1,Ey1,Hx1,Hy1,Hz1,Ex2,Ey2,Hx2,Hy2,Hz2,...
        mtr.Zxx, mtr.Zxy, mtr.Zyx, mtr.Zyy,mtr.rhoXX, mtr.rhoXY, mtr.rhoYX, mtr.rhoYY,...
        mtr.phaXX, mtr.phaXY, mtr.phaYX, mtr.phaYY,...
        'VariableNames',{'Frequency';'RX';'Ex1';'Ey1';'Hx1';'Hy1';'Hz1';'Ex2';...
        'Ey2';'Hx2';'Hy2';'Hz2';'Zxx';'Zxy';'Zyx';'Zyy';'rhoXX';'rhoXY';'rhoYX';'rhoYY'; ...
        'phaXX';'phaXY';'phaYX';'phaYY'});
    data1 = cat(1,data1,datatemp);

    figure(f2);
    subplot(1,4,1);hold on;

    plot(real(eField),zNode,'-','LineWidth',1.5,'Color',col(ifreq,:));
    ylabel('Z(m)') ;xlabel('real(E)');
    set(gca,'FontSize',8,'FontWeight','bold','YDir','reverse','XDir','reverse','LineWidth',1);
    axis tight;box on;

    subplot(1,4,2);hold on;
    plot(imag(eField),zNode,'-','LineWidth',1.5,'Color',col(ifreq,:));
    xlabel('imag(E)');
   
    set(gca,'FontSize',8,'FontWeight','bold','YDir','reverse','XDir','reverse','LineWidth',1);
    axis tight;box on;

    subplot(1,4,3);hold on;
    plot(abs(eField),zNode,'-','LineWidth',1.5,'Color',col(ifreq,:));
    xlabel('$|\mathbf{E}|$','Interpreter','latex');
    set(gca,'FontSize',8,'FontWeight','bold','YDir','reverse','XDir','reverse','LineWidth',1)
    axis tight;box on;
 
    l{ifreq} = [num2str(round(freq(ifreq)/1000)),'kHz'] ;
    legend(l,'FontSize',8,'FontWeight','bold', "Position", [0.72087,0.30412,0.22381,0.48477],Box="off");


    % MakeGif('myelps0.Gif',ifreq);
    % drawnow ;
 
    % pause(1);
end

%

figure;
% yyaxis left;
p=plot(freq,data1.rhoXY,'-s','LineWidth',2);
c =get(p,'Color');
xlabel('frequency/Hz','LineWidth',1,'FontWeight','bold');
ylabel('Apparent resistivity(ohm.m)','LineWidth',1,'FontWeight','bold');
% hold on;
% loglog(freq,rhoaXY_3D,'.-','LineWidth',2);
xlabel('frequency(Hz)','FontWeight','bold');
% ylim([500,1500]);
set(gca,'LineWidth',1,'XScale','log');
box on;axis tight;


%

figure;
% yyaxis left;
p=plot(freq,data1.phaYX,'-s','LineWidth',2);
c =get(p,'Color');
xlabel('frequency/Hz','LineWidth',1,'FontWeight','bold');
ylabel('Phase(degree)','LineWidth',1,'FontWeight','bold');
% hold on;
% loglog(freq,rhoaXY_3D,'.-','LineWidth',2);
xlabel('frequency(Hz)','FontWeight','bold');
% ylim([500,2000]);
set(gca,'LineWidth',1,'XScale','log');
box on;axis padded;