%//************************************************************************
%//    fig_1.m: MATLAB visualization program
%//***********************************************************************
%//    Program author: Dr.-Ing. René Marklein
%//                    University of Kassel, Department of Electrical and 
%//                    Computer Engineering, Electromagnetic Field Theory
%//                    Wilhelmshöher Allee 71
%//                    D-34121 Kassel
%//                    Tel. 0561 804 6426, marklein@uni-kassel.de
%//
%//    Date of this version:  November 15, 2001
%//
%//    MATLAB program to visualize the output file Ex.bin and Hy.bin of the
%//    1D FDTD code with PEC boundary conditions.
%//************************************************************************

close all;
clear all;

Nt = 500;
Nz = 200;

Dz = 1.5e-3;

Z = Dz*Nz;

Ex = zeros(Nt,Nz);
Hy = zeros(Nt,Nz);

X = zeros(Nt*Nz,1);

figure(1);

fp=fopen('ex.bin','r');
[X,nread] = fread(fp,Nt*Nz,'float32');
fclose(fp);

for nt=1:Nt
  for nz=1:Nz    
    Ex(nt,nz) = X((nt-1)*Nz+nz);
  end
end

Ex_max = max(max(Ex));
Ex_min = min(min(Ex));

fprintf('Ex_min = %g Ex_max = %g\n', Ex_min, Ex_max);

fp=fopen('hy.bin','r');
[X,nread] = fread(fp,Nt*Nz,'float32');
fclose(fp);

for nt=1:Nt
  for nz=1:Nz    
    Hy(nt,nz) = X((nt-1)*Nz+nz);
  end
end

Hy_max = max(X);
Hy_min = min(X);

fprintf('Hy_min = %g Hy_max = %g\n', Hy_min, Hy_max);

j=1;
k=2;

for i=1:10

 subplot(10,2,j)
 plot(Ex(25*j,:),'b')
 set(gca, 'Ytick',[-2 -1 0 1 2],'FontSize',6);
 set(gca, 'YTickLabel',[-2 -1 0 1 2],'FontSize',6);
 axis([1 Nz -2 2]);
 set(gca, 'XTick',[1 Nz/4 Nz/2 3*Nz/4 Nz],'FontSize',6);
 set(gca, 'XTickLabel',[0 Z/4 Z/2 3*Z/4 Z],'FontSize',6);
 grid on 

 subplot(10,2,k)
 plot(Hy(25*j,:),'r')
 set(gca, 'Ytick',[-2 -1 0 1 2],'FontSize',6);
 set(gca, 'YTickLabel',[-2 -1 0 1 2],'FontSize',6);
 axis([1 Nz -2 2]);
 set(gca, 'XTick',[1 Nz/4 Nz/2 3*Nz/4 Nz],'FontSize',6);
 set(gca, 'XTickLabel',[0 Z/4 Z/2 3*Z/4 Z],'FontSize',6);
 grid on 

 j = j+2;    
 k = k+2;

end

subplot(10,2,1)
title('E_x(z,t_0) component at different time points t_0','FontSize',10)
subplot(10,2,19)
xlabel('z in m','FontSize',10);    
subplot(10,2,2)
title('H_y(z,t_0) component at different time points t_0','FontSize',10)
subplot(10,2,20)
xlabel('z in m','FontSize',10);    

fprintf('...finished.\n');

set(gcf,'PaperType','a4');
set(gcf,'PaperOrientation','portrait');

fprintf('Generate EPS file: fdtd1d_fig_1.eps\n');
print -depsc2 -r150 fdtd1d_fig_1.eps
fprintf('...finished.\n');
fprintf('Generate EPS file: fdtd1d_fig_1.jpg\n');
print -djpeg -r150 fdtd1d_fig_1.jpg
fprintf('...finished.\n');

figure(1)
colormap(gray)

fprintf('Generate EPS file: fdtd1d_fig_1_sw.eps\n');
print -deps2 -r150 fdtd1d_fig_1_sw.eps
fprintf('...finished.\n');
fprintf('Generate EPS file: fdtd1d_fig_1_sw.jpg\n');
print -djpeg -r150 fdtd1d_fig_1_sw.jpg
fprintf('...finished.\n');