%FDTD Code Brian Egenriether
%This code is a finite-difference time-domain simulation of 3 gaussian
%pulses in a resonant cavity with perfectly conducting walls (PEC)
%It creates an animation of the results but if you want to let the system
%get to steady state you'll need around 50,000 steps (i.e.nMax= 50000)
%This cannot be animated, you will run out of memory. Comment out the
%getframe command at the end of the outer loop and uncomment the plots at
%the bottom to get slices of the steady state result and the FFT.
clc
clear
nMax = 700; % number of time steps
scalefactor=1; %Granularity (for animations don't exceed 2)
mu = 1.2566306E-6;
eps0 = 8.854E-12;
epsr = 4.2;
eps = eps0*epsr;
c = 1/sqrt(mu*eps);
cfln = 0.995; %stability criterion
tw = 15;% %half width of pulse 10-20 looks nice
n0 = 5*tw; %time delay for sources (stability criterion)
Lx = 0.08; %dimensions of resonant cavity
Ly = 0.06;
Lz = 0.048;
nx = 80*scalefactor; %3 equal discretizations
ny =60*scalefactor;
nz= 48*scalefactor;
dx = Lx/nx; %finite differences for differentials
dy = Ly/ny;
dz = Lz/nz;
dt = cfln/(c*sqrt((1/dx)^2+(1/dy)^2+(1/dz)^2));%time stability criterion
fres = c/sqrt(Lx^2+Ly^2+Lz^2);
%FDTD Coefficients
cHxy = dt/(mu*dy);
cHxz = dt/(mu*dz);
cHyz = dt/(mu*dz);
cHyx = dt/(mu*dx);
cHzx = dt/(mu*dx);
cHzy = dt/(mu*dy);
cExy = dt/(eps*dy);
cExz = dt/(eps*dz);
cEyz = dt/(eps*dz);
cEyx = dt/(eps*dx);
cEzx = dt/(eps*dx);
cEzy = dt/(eps*dy);
%PEC Boundary conditoins
%Set all tangential E fields to zero and never update them. This will keep
%them zero. Update loops only modify inner cells
Hx=zeros(nx,ny,nz);
Hy=zeros(nx,ny,nz);
Hz=zeros(nx,ny,nz);
Ex=zeros(nx,ny,nz);
Ey=zeros(nx,ny,nz);
Ez=zeros(nx,ny,nz);
%=====================Outer Loop
for n=1:nMax
%==================== Main update loops for the Electric fields:
%===================Ex
for k = 2:nz-1
for j = 2:ny-1
for i = 1:nx-1 %1->2
Ex(i,j,k) = Ex(i,j,k)+cExy*(Hz(i,j,k)-Hz(i,j-1,k))-cExz*(Hy(i,j,k)-Hy(i,j,k-1));
end
end
end
%===================Ey
for k = 2:nz-1
for j = 1:ny-1%1->2
for i = 2:nx-1
Ey(i,j,k) = Ey(i,j,k)+cEyz*(Hx(i,j,k)-Hx(i,j,k-1))-cEyx*(Hz(i,j,k)-Hz(i-1,j,k));
end
end
end
%===================Ez
for k = 1:nz-1 %1->2
for j = 2:ny-1
for i = 2:nx-1
Ez(i,j,k) = Ez(i,j,k)+cEzx*(Hy(i,j,k)-Hy(i-1,j,k))-cEzy*(Hx(i,j,k)-Hx(i,j-1,k));
end
end
end
%Define E field components at 3 seperate arbitrary points with Gaussian pulses
Ex(30*scalefactor,20*scalefactor,25*scalefactor)=2*tw*exp(-((n-n0)/tw)^2);
Ey(25*scalefactor,45*scalefactor,20*scalefactor)=2*tw*exp(-((n-n0)/tw)^2);
Ez(55*scalefactor,30*scalefactor,23*scalefactor)=2*tw*exp(-((n-n0)/tw)^2);
%==================== Main update loops for the Magnetic fields:
%===================Hx
for k = 1:nz-1 %1->2 all 3
for j = 1:ny-1
for i = 1:nx %-1
Hx(i,j,k) = Hx(i,j,k)-cHxy*(Ez(i,j+1,k)-Ez(i,j,k))+cHxz*(Ey(i,j,k+1)-Ey(i,j,k));
end
end
end
%===================Hy
for k = 1:nz-1 %1->2 all 3
for j = 1:ny %-1
for i = 1:nx-1
Hy(i,j,k) = Hy(i,j,k)-cHyz*(Ex(i,j,k+1)-Ex(i,j,k))+cHyx*(Ez(i+1,j,k)-Ez(i,j,k));
end
end
end
%===================Hz
for k = 1:nz %-1 %1->2 all 3
for j = 1:ny-1
for i = 1:nx-1
Hz(i,j,k) = Hz(i,j,k)-cHzx*(Ey(i+1,j,k)-Ey(i,j,k))+cHzy*(Ex(i,j+1,k)-Ex(i,j,k));
end
end
end
%Define H field components at 3 seperate arbitrary points with Gaussian pulse
%Must put this after the H field update for stability
% Hx(30,30,30)=exp(-((n-n0)/tw)^2);
% Hy(45,35,20)=exp(-((n-n0)/tw)^2);
% Hz(25,40,20)=exp(-((n-n0)/tw)^2);
clc
round(n/nMax*100) %gives progress update percentage on home screen
surf(Ez(:,:,28*scalefactor));
set(gcf,'renderer','zbuffer')
%hold on
whitebg('black');
grid off
set(gcf,'Position',[20 50 1500 800]);
axis([-10*scalefactor 60*scalefactor -20*scalefactor 70*scalefactor -.01/scalefactor .01/scalefactor])
view([3,4,5])
Mov(n)=getframe; % comment this out for nonanimated analysis
end
movie(Mov,20,40); % comment this out for nonanimated analysis
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。
资源推荐
资源详情
资源评论
收起资源包目录
毕业设计&课设-使用Matlab对波动光学进行建模。包括使用标量衍射理论的衍射以及菲涅耳和夫琅和费衍射.zip (5个子文件)
matlab_codings_tt
diffraction_grating_field.m 1KB
finite_difference_time_domain_simulation.m 4KB
Fresnel_Diffraction_Aasim.m 3KB
Fraunhofer_Diffraction_Aasim.m 2KB
Complete_Simulation.m 913B
共 5 条
- 1
资源评论
毕业小助手
- 粉丝: 2750
- 资源: 5583
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功