⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 2d_te_pml.m

📁 应用MATLAB编写的二维时域有限差分程序。是初学FDTD的好帮手。建议初学者参考。
💻 M
📖 第 1 页 / 共 2 页
字号:
%***********************************************************************
%     2-D FDTD TE code with PML absorbing boundary conditions
%***********************************************************************
%
%     Program author: Susan C. Hagness
%                     Department of Electrical and Computer Engineering
%                     University of Wisconsin-Madison
%                     1415 Engineering Drive
%                     Madison, WI 53706-1691
%                     608-265-5739
%                     hagness@engr.wisc.edu
%
%     Date of this version:  February 2000
%
%     This MATLAB M-file implements the finite-difference time-domain
%     solution of Maxwell's curl equations over a two-dimensional
%     Cartesian space lattice comprised of uniform square grid cells.
%
%     To illustrate the algorithm, a 6-cm-diameter metal cylindrical 
%     scatterer in free space is modeled. The source excitation is 
%     a Gaussian pulse with a carrier frequency of 5 GHz.
%
%     The grid resolution (dx = 3 mm) was chosen to provide 20 samples
%     per wavelength at the center frequency of the pulse (which in turn
%     provides approximately 10 samples per wavelength at the high end
%     of the excitation spectrum, around 10 GHz).
%
%     The computational domain is truncated using the perfectly matched
%     layer (PML) absorbing boundary conditions.  The formulation used 
%     in this code is based on the original split-field Berenger PML. The
%     PML regions are labeled as shown in the following diagram: 
%
%            ----------------------------------------------
%           |  |                BACK PML                |  |
%            ----------------------------------------------
%           |L |                                       /| R|
%           |E |                                (ib,jb) | I|
%           |F |                                        | G|
%           |T |                                        | H|
%           |  |                MAIN GRID               | T|
%           |P |                                        |  |
%           |M |                                        | P|
%           |L | (1,1)                                  | M|
%           |  |/                                       | L|
%            ----------------------------------------------
%           |  |                FRONT PML               |  |
%            ----------------------------------------------
%
%     To execute this M-file, type "fdtd2D" at the MATLAB prompt.
%     This M-file displays the FDTD-computed Ex, Ey, and Hz fields at 
%     every 4th time step, and records those frames in a movie matrix, 
%     M, which is played at the end of the simulation using the "movie" 
%     command.
%
%***********************************************************************

 clear
clc
%***********************************************************************
%     Fundamental constants
%***********************************************************************

cc=2.99792458e8;            %speed of light in free space
muz=4.0*pi*1.0e-7;          %permeability of free space
epsz=1.0/(cc*cc*muz);       %permittivity of free space

freq=5.0e+9;                %center frequency of source excitation
lambda=cc/freq;             %center wavelength of source excitation
omega=2.0*pi*freq;          

%***********************************************************************
%     Grid parameters
%***********************************************************************

ie=5;           %number of grid cells in x-direction
je=4;            %number of grid cells in y-direction

ib=ie+1;
jb=je+1;

is=ceil(ie/2);            %location of z-directed hard source
js=ceil(je/2);          %location of z-directed hard source

dx=3.0e-3;        %space increment of square lattice
dt=dx/(2.0*cc);   %time step

nmax=150;         %total number of time steps

iebc=3;           %thickness of left and right PML region
jebc=3;           %thickness of front and back PML region
rmax=0.00001;     % 最大反射系数
orderbc=2;        % 指数
ibbc=iebc+1;
jbbc=jebc+1;
iefbc=ie+2*iebc;  %X方向总网格数
jefbc=je+2*jebc;  %Y方向总网格数
ibfbc=iefbc+1;
jbfbc=jefbc+1;

%***********************************************************************
%     Material parameters
%***********************************************************************

media=2;            %表示两种介质
eps=[1.0 1.0];      %相对介电常数
sig=[0.0 1.0e+7];   %电导率,真空中为0
%sig=[0.0 0.0];   %电导率,真空中为0
mur=[1.0 1.0];      %相对导磁率
sim=[0.0 0.0];      %磁导率,真空中为0

%***********************************************************************
%     Wave excitation
%***********************************************************************
% 激励源公式可参考时域有限差分方法P121。
rtau=160.0e-12;      %决定了高斯脉冲的宽度
tau=rtau/dt;         %将宽度化为时间步的整数位
delay=3*tau;         %高斯脉冲峰值出现在n=delay时刻。

source=zeros(1,nmax);
for n=1:7.0*tau
  source(n)=sin(omega*(n-delay)*dt)*exp(-((n-delay)^2/tau^2));
end

%***********************************************************************
%     Field arrays
%***********************************************************************

ex=zeros(ie,jb);           %fields in main grid 
ey=zeros(ib,je);
hz=zeros(ie,je);

exbcf=zeros(iefbc,jebc);   %fields in front PML region
eybcf=zeros(ibfbc,jebc);
hzxbcf=zeros(iefbc,jebc);   %在PML中把Hz分成hzx和hzy两个分量
hzybcf=zeros(iefbc,jebc);

exbcb=zeros(iefbc,jbbc);   %fields in back PML region
eybcb=zeros(ibfbc,jebc);
hzxbcb=zeros(iefbc,jebc);
hzybcb=zeros(iefbc,jebc);

exbcl=zeros(iebc,jb);      %fields in left PML region
eybcl=zeros(iebc,je);
hzxbcl=zeros(iebc,je);
hzybcl=zeros(iebc,je);

exbcr=zeros(iebc,jb);      %fields in right PML region
eybcr=zeros(ibbc,je);
hzxbcr=zeros(iebc,je);
hzybcr=zeros(iebc,je);

%***********************************************************************
%     Updating coefficients
%***********************************************************************
% 系数计算公式可参考时域有限差分方法P速度22。
for i=1:media      %i=1代表自由空间 , i=2代表金属体
  eaf =dt*sig(i)/(2.0*epsz*eps(i));  %电场分量系数
  ca(i)=(1.0-eaf)/(1.0+eaf);          
  cb(i)=dt/epsz/eps(i)/dx/(1.0+eaf); 
  haf =dt*sim(i)/(2.0*muz*mur(i));  %磁场分量系数
  da(i)=(1.0-haf)/(1.0+haf);
  db(i)=dt/muz/mur(i)/dx/(1.0+haf);
end

%***********************************************************************
%     Geometry specification (main grid)
%***********************************************************************

%     Initialize entire main grid to free space

caex(1:ie,1:jb)=ca(1);     
cbex(1:ie,1:jb)=cb(1);

caey(1:ib,1:je)=ca(1);
cbey(1:ib,1:je)=cb(1);

dahz(1:ie,1:je)=da(1);
dbhz(1:ie,1:je)=db(1);

%     Add metal cylinder

diam=20;          % diam代表圆柱直径占用的网格数,diameter of cylinder: 6 cm,(dx = 3 mm,diam=60/3)
rad=diam/2.0;     % radius of cylinder: 3 cm
icenter=4*ie/5;   % i-coordinate of cylinder's center
jcenter=je/2;     % j-coordinate of cylinder's center

for i=1:ie
for j=1:je
  dist2=(i+0.5-icenter)^2 + (j-jcenter)^2;%注意加0.5是由于Ex在X方向有半个空间步位移
  if dist2 <= rad^2 
     caex(i,j)=ca(2);
     cbex(i,j)=cb(2);
  end
  dist2=(i-icenter)^2 + (j+0.5-jcenter)^2;%注意加0.5是由于Ey在Y方向有半个空间步位移
  if dist2 <= rad^2 
     caey(i,j)=ca(2);
     cbey(i,j)=cb(2);
  end
end
end
% 为什么没有计入Hz,因为在金属体内和外部自由空间有相同的导磁率sim和相对磁导率mur
%***********************************************************************
%     Fill the PML regions
%***********************************************************************

delbc=iebc*dx;    %PML厚度
sigmam=-log(rmax/100.0)*epsz*cc*(orderbc+1)/(2*delbc);%最外层的电导率(此处达到最大值) 
bcfactor=eps(1)*sigmam/(dx*(delbc^orderbc)*(orderbc+1));%计算公式见<计算电磁学的数值方法>P179

%     FRONT region 

caexbcf(1:iefbc,1)=1.0;    %最下边
cbexbcf(1:iefbc,1)=0.0;
for j=2:jebc
  y1=(jebc-j+1.5)*dx;
  y2=(jebc-j+0.5)*dx;
  sigmay=bcfactor*(y1^(orderbc+1)-y2^(orderbc+1));  %计算Y方向电导率,计算公式见<计算电磁学的数值方法>P179
  ca1=exp(-sigmay*dt/(epsz*eps(1)));
  cb1=(1.0-ca1)/(sigmay*dx);
  caexbcf(1:iefbc,j)=ca1;                          %自第2层到最外层
  cbexbcf(1:iefbc,j)=cb1;
end
sigmay = bcfactor*(0.5*dx)^(orderbc+1);
ca1=exp(-sigmay*dt/(epsz*eps(1)));
cb1=(1-ca1)/(sigmay*dx);
caex(1:ie,1)=ca1;
cbex(1:ie,1)=cb1;
caexbcl(1:iebc,1)=ca1;
cbexbcl(1:iebc,1)=cb1;
caexbcr(1:iebc,1)=ca1;
cbexbcr(1:iebc,1)=cb1;

for j=1:jebc
  y1=(jebc-j+1)*dx;
  y2=(jebc-j)*dx;
  sigmay=bcfactor*(y1^(orderbc+1)-y2^(orderbc+1));
  sigmays=sigmay*(muz/(epsz*eps(1)));
  da1=exp(-sigmays*dt/muz);
  db1=(1-da1)/(sigmays*dx);
  dahzybcf(1:iefbc,j)=da1;
  dbhzybcf(1:iefbc,j)=db1;
  caeybcf(1:ibfbc,j)=ca(1);
  cbeybcf(1:ibfbc,j)=cb(1);
  dahzxbcf(1:iefbc,j)=da(1);
  dbhzxbcf(1:iefbc,j)=db(1);
end

%     BACK region 

caexbcb(1:iefbc,jbbc)=1.0;   %最顶边
cbexbcb(1:iefbc,jbbc)=0.0;
for j=2:jebc
  y1=(j-0.5)*dx;
  y2=(j-1.5)*dx;
  sigmay=bcfactor*(y1^(orderbc+1)-y2^(orderbc+1));
  ca1=exp(-sigmay*dt/(epsz*eps(1)));
  cb1=(1-ca1)/(sigmay*dx);
  caexbcb(1:iefbc,j)=ca1;
  cbexbcb(1:iefbc,j)=cb1;
end
sigmay = bcfactor*(0.5*dx)^(orderbc+1);
ca1=exp(-sigmay*dt/(epsz*eps(1)));
cb1=(1-ca1)/(sigmay*dx);
caex(1:ie,jb)=ca1;
cbex(1:ie,jb)=cb1;
caexbcl(1:iebc,jb)=ca1;
cbexbcl(1:iebc,jb)=cb1;
caexbcr(1:iebc,jb)=ca1;
cbexbcr(1:iebc,jb)=cb1;

for j=1:jebc
  y1=j*dx;
  y2=(j-1)*dx;
  sigmay=bcfactor*(y1^(orderbc+1)-y2^(orderbc+1));
  sigmays=sigmay*(muz/(epsz*eps(1)));
  da1=exp(-sigmays*dt/muz);
  db1=(1-da1)/(sigmays*dx);
  dahzybcb(1:iefbc,j)=da1;
  dbhzybcb(1:iefbc,j)=db1;
  caeybcb(1:ibfbc,j)=ca(1);
  cbeybcb(1:ibfbc,j)=cb(1);
  dahzxbcb(1:iefbc,j)=da(1);
  dbhzxbcb(1:iefbc,j)=db(1);
end

%     LEFT region 

caeybcl(1,1:je)=1.0;     %最左边
cbeybcl(1,1:je)=0.0;
for i=2:iebc
  x1=(iebc-i+1.5)*dx;
  x2=(iebc-i+0.5)*dx;
  sigmax=bcfactor*(x1^(orderbc+1)-x2^(orderbc+1));
  ca1=exp(-sigmax*dt/(epsz*eps(1)));
  cb1=(1-ca1)/(sigmax*dx);
  caeybcl(i,1:je)=ca1;
  cbeybcl(i,1:je)=cb1;
  caeybcf(i,1:jebc)=ca1;
  cbeybcf(i,1:jebc)=cb1;
  caeybcb(i,1:jebc)=ca1;
  cbeybcb(i,1:jebc)=cb1;
end
sigmax=bcfactor*(0.5*dx)^(orderbc+1);
ca1=exp(-sigmax*dt/(epsz*eps(1)));
cb1=(1-ca1)/(sigmax*dx);
caey(1,1:je)=ca1;
cbey(1,1:je)=cb1;
caeybcf(iebc+1,1:jebc)=ca1;
cbeybcf(iebc+1,1:jebc)=cb1;
caeybcb(iebc+1,1:jebc)=ca1;
cbeybcb(iebc+1,1:jebc)=cb1;

for i=1:iebc
  x1=(iebc-i+1)*dx;
  x2=(iebc-i)*dx;
  sigmax=bcfactor*(x1^(orderbc+1)-x2^(orderbc+1));
  sigmaxs=sigmax*(muz/(epsz*eps(1)));
  da1=exp(-sigmaxs*dt/muz);
  db1=(1-da1)/(sigmaxs*dx);
  dahzxbcl(i,1:je)=da1;
  dbhzxbcl(i,1:je)=db1;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -