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

freq = logspace(4,6,11);
nfreq = length(freq);

zNode = (-500:1:1000)';

centerZ  = zNode(1:end-1)+0.5*diff(zNode) ; % 层中心Z坐标
id = find(abs(zNode)== min(abs(zNode)));
zNode = zNode*1;

sig1D1 = 1e-15*ones(size(zNode,1)-1,1);   sig1D2=sig1D1;sig1D3=sig1D1;
epsr1D1  = ones(size(zNode,1)-1,1);   epsr1D2=epsr1D1; epsr1D3=epsr1D1;
aS  = zeros(size(zNode,1)-1,1);  aD=aS;aL=aS;


sig1D1(centerZ>0,:)  = 1/1e4;
sig1D2(centerZ>0,:)  = 1/1e4;
sig1D3(centerZ>0,:)  = 1/1e4;
epsr1D1(centerZ>0,:)  = 5;
epsr1D2(centerZ>0,:)  = 5;
epsr1D3(centerZ>0,:)  = 5;

aS(centerZ>0,:)  = 0 ;
aD(centerZ>0,:)  = 0;
aL(centerZ>0,:)  = 0;


h1 = 10000;
sig1D1(centerZ>h1,:)  = 1/10e2;
sig1D2(centerZ>h1,:)  = 1/1e2;
sig1D3(centerZ>h1,:)  = 1/1e1;
epsr1D1(centerZ>h1,:)  = 5;
epsr1D2(centerZ>h1,:)  = 5;
epsr1D3(centerZ>h1,:)  = 5;
aS(centerZ>h1,:)  = 0;
aD(centerZ>h1,:)  = 0;
aL(centerZ>h1,:)  = 0;

h2 = 20000;
sig1D1(centerZ>h2,:)  = 1/1e4;
sig1D2(centerZ>h2,:)  = 1/1e4;
sig1D3(centerZ>h2,:)  = 1/1e4;
epsr1D1(centerZ>h2,:)  = 5;
epsr1D2(centerZ>h2,:)  = 5;
epsr1D3(centerZ>h2,:)  = 5;
aS(centerZ>h2,:)  = 0;
aD(centerZ>h2,:)  = 0;
aL(centerZ>h2,:)  = 0;

h3 = 3000;
sig1D1(centerZ>h3,:)  = 1/1e4;
sig1D2(centerZ>h3,:)  = 1/1e4;
sig1D3(centerZ>h3,:)  = 1/1e4;
aS(centerZ>h3,:)  = 0;
aD(centerZ>h3,:)  = 0;
aL(centerZ>h3,:)  = 0;

sig1D = get_sig_ani(sig1D1,sig1D2,sig1D3,aS,aD,aL);
sig1D = sig1D(:,1:6);
epsr1D=get_sig_ani(epsr1D1,epsr1D2,epsr1D3,aS,aD,aL);
epsr1D=epsr1D(:,1:6);

% epsr1D(:,1:6)=0;

% epsr1D(:,4:6)  = 0; 
% epsr(centerZ>0,:) = 1;
% % epsr(centerZ>3,:) = 1;

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(m)') ;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('Relative 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('Relative 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);
    [EFx1,EFy1,EFz1,HFx1,HFy1]=mt1DAniAnalyticFieldEspMu(freq(ifreq),sig1D,epsr1D,mur1D,zNode',[1.0; 0]);
    [EFx2,EFy2,EFz2,HFx2,HFy2]=mt1DAniAnalyticFieldEspMu(freq(ifreq),sig1D,epsr1D,mur1D,zNode',[0; 1.0]);
    Ex1 = EFx1(id);
    Ey1 = EFy1(id);

    Hx1 =  HFx1(id);
    Hy1 =  HFy1(id);
    Hz1 =  Hx1*0 ;

    Ex2 = EFx2(id);
    Ey2 = EFy2(id);
    Hx2 = HFx2(id) ;
    Hy2 = HFy2(id) ;
    Hz2 = Hx1*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(EFx1),zNode,'-','LineWidth',1.5,'Color',col(ifreq,:));
    ylabel('Z(m)') ;xlabel('real(E)');
    % title('Real part of  electric field')
    set(gca,'YDir','reverse','XDir','reverse');
    axis tight;box on;

    subplot(1,4,2);hold on;
    plot(imag(EFx1),zNode,'-','LineWidth',1.5,'Color',col(ifreq,:));
    ylabel('Z(m)') ;xlabel('imag(E)');
    % title('Imaginary part of  electric field')
    set(gca,'YDir','reverse','XDir','reverse');
    axis tight;box on;

    subplot(1,4,3);hold on;
    plot(abs(EFx1),zNode,'-','LineWidth',1.5,'Color',col(ifreq,:));
    ylabel('Z(m)') ;xlabel('$|\mathbf{E}|$','Interpreter','latex');
    % title('Amplitude of electric field')
    set(gca,'YDir','reverse','XDir','reverse');
    axis tight;box on;
    % xlim([0,1])
    % l{ifreq} = [num2str(freq(ifreq)/1000),'kHz'] ;
    % legend(l,'FontSize',6,"Position", [0.722,0.392,0.15,0.53],Box="off");
    % 
    % MakeGif('my.Gif',ifreq);

    % drawnow ;
    pause(1);
end

%

figure
% yyaxis left;
p=plot(freq,data1.rhoXY,'-^','LineWidth',2);
hold on
plot(freq,data1.rhoYX,'-o','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');
% xlabel('Period(s)','FontWeight','bold');
% ylim([500,1500]);
set(gca,'LineWidth',1,'XScale','log','YScale','log');
box on;axis tight;

%

figure;
% yyaxis left;
p=plot(freq,data1.phaXY,'-^','LineWidth',2);
hold on; 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');
% xlabel('Period(s)','FontWeight','bold');
% ylim([500,2000]);
set(gca,'LineWidth',1,'XScale','log');
box on;axis tight;

%