%初始化Matlab工作空间
clear all;close all;clc;
%定义问题空间参数
define_problem_space_parameters;
define_geometry;
define_sources_and_lumped_elements;
define_output_parameters;
%初始化问题空间和参数
initialize_fdtd_material_grid;
if run_simulation
initialize_fdtd_parameters_and_arrays;
initialize_sources_and_lumped_elements;
initialize_updating_coefficients;
initialize_boundary_conditions;
initialize_output_parameters;
%FDTD主循环
run_fdtd_time_marching_loop;
end
disp('defining the problem space parameters');
%最大的仿真时间步
number_of_time_steps=1000;
%高斯脉冲准确因子
number_of_cells_per_wavelength=20;
%x,y,z方向单位网格尺寸(米)
dx=0.389e-3;
dy=0.4e-3;
dz=0.265e-3;
ra_x=0.681;
ra_y=0.665;
%《边界条件》
%定义的边界条件参数
%'pec':完美的电导体
%'cpml':完美吸收边界PML
%当cpml_number_of_cells小于零时
%CPML吸收边界向仿真空间内部扩展
%边界形式定义,边界的网格数
boundary.type_xn='cpml';
boundary.air_buffer_number_of_cells_xn=10;%吸收边界与物体之间的网格数
boundary.cpml_number_of_cells_xn=8;
boundary.type_xp='cpml';
boundary.air_buffer_number_of_cells_xp=10;%吸收边界与物体之间的网格数
boundary.cpml_number_of_cells_xp=8;
boundary.type_yn='cpml';
boundary.air_buffer_number_of_cells_yn=10;%吸收边界与物体之间的网格数
boundary.cpml_number_of_cells_yn=8;
boundary.type_yp='cpml';
boundary.air_buffer_number_of_cells_yp=10;%吸收边界与物体之间的网格数
boundary.cpml_number_of_cells_yp=8;
boundary.type_zn='cpml';
boundary.air_buffer_number_of_cells_zn=10;%吸收边界与物体之间的网格数
boundary.cpml_number_of_cells_zn=8;
boundary.type_zp='cpml';
boundary.air_buffer_number_of_cells_zp=10;%吸收边界与物体之间的网格数
boundary.cpml_number_of_cells_zp=8;
%<材料形式>
%材料形式定义
%eps_r:相对介电常数
%mu_r:相对磁导率
%sigma_e:电导率
%sigma_m:磁电导率
%空气
material_types(1).eps_r=1;
material_types(1).mu_r=1;
material_types(1).sigma_e=0;
material_types(1).sigma_m=0;
material_types(1).color=[1 1 1];
%PEC:理想电导体
material_types(2).eps_r=1;
material_types(2).mu_r=1;
material_types(2).sigma_e=1e10;
material_types(2).sigma_m=0;
material_types(2).color=[1 0 0];
%PMC:理想磁导体
material_types(3).eps_r=1;
material_types(3).mu_r=1;
material_types(3).sigma_e=0;
material_types(3).sigma_m=1e10;
material_types(3).color=[0 1 0];
%一种介质
material_types(4).eps_r=2.2;
material_types(4).mu_r=1;
material_types(4).sigma_e=0;
material_types(4).sigma_m=0;
material_types(4).color=[0 0 1];
%一种介质
material_types(5).eps_r=3.2;
material_types(5).mu_r=1.4;
material_types(5).sigma_e=0.5;
material_types(5).sigma_m=0.3;
material_types(5).color=[1 1 0];
%空气,PEC,PMC材料索引
material_type_index_air=1;
material_type_index_pec=2;
material_type_index_pmc=3;
disp('defining the problem geometry');
bricks=[ ];
spheres=[ ];
%用材料四来定义一个立方体
bricks(1).min_x=0;
bricks(1).min_y=0;
bricks(1).min_z=0;
bricks(1).max_x=24e-3;
bricks(1).max_y=20e-3;
bricks(1).max_z=11e-3;
bricks(1).material_type=4;
%用材料二来定义一个立方体
bricks(2).min_x=-20e-3;
bricks(2).min_y=-20e-3;
bricks(2).min_z=-11e-3;
bricks(2).max_x=0;
bricks(2).max_y=0;
bricks(2).max_z=0;
bricks(2).material_type=2;
%用材料五定义一个球
spheres(1).radius=20e-3;
spheres(1).center_x=0;
spheres(1).center_y=0;
spheres(1).center_z=40e-3;
spheres(1).material_type=5;
%用材料一定义一个球
spheres(2).radius=15e-3;
spheres(2).center_x=0;
spheres(2).center_y=0;
spheres(2).center_z=40e-3;
spheres(2).material_type=1;
for ind=1:number_of_bricks
%确定金属薄片的位置
blx=round((bricks(ind).min_x)-fdtd_domain.min_x)/dx+1;
bly=round((bricks(ind).min_y)-fdtd_domain.min_y)/dy+1;
blz=round((bricks(ind).min_z)-fdtd_domain.min_z)/dz+1;
bux=round((bricks(ind).min_x)-fdtd_domain.min_x)/dx+1;
buy=round((bricks(ind).min_y)-fdtd_domain.min_y)/dy+1;
buz=round((bricks(ind).min_z)-fdtd_domain.min_z)/dz+1;
if(blx==bux)
gay(blx,bly:buy-1,blz:buz)=0;
gaz(blx,bly:buy,blz:buz-1)=0;
end
if(bly==buy)
gaz(blx:bux,bly,blz:buz-1)=0;
gax(blx:bux-1,bly,blz:buz)=0;
end
if(blz==buz)
gax(blx:bux-1,bly:buy,blz)=0;
gay(blx:bux,bly:buy-1,blz)=0;
end
end
disp('initializing FDTD material grid');
%基于物体的位置和边界条件确定问题空间的尺寸
calculate_domain_size;
%空气填充所有问题空间
material_3d_space=ones(nx,ny,nz);
%生成球
create_spheres;
%生成立方体
create_bricks;
%初始化材料参数
eps_r_x=ones(nx,nyp1,nzp1);
eps_r_y=ones(nxp1,ny,nzp1);
eps_r_z=ones(nxp1,nyp1,nz);
mu_r_x=ones(nxp1,ny,nz);
mu_r_y=ones(nx,nyp1,nz);
mu_r_z=ones(nx,ny,nzp1);
sigma_e_x=zeros(nx,nyp1,nzp1);
sigma_e_y=zeros(nxp1,ny,nzp1);
sigma_e_z=zeros(nxp1,nyp1,nz);
sigma_m_x=zeros(nxp1,ny,nz);
sigma_m_y=zeros(nx,nyp1,nz);
sogma_m_z=zeros(nx,ny,nzp1);
%材料参数赋值(边界处材料取平均值)
calculate_material_component_values;
%生成零厚度PEC平板
create_PEC_plates;
disp('calculating the number of cells in the problem space');
number_of_spheres=size(spheres,2);
number_of_bricks=size(bricks,2);
%找到计算空间的所填充物体的最大值和最小值,并存储在fdtd_domain的结构体中
number_of_objects=1;
for i=1:number_of_spheres
min_x(number_of_objects)=spheres(i).center_x-spheres(i).radius;
min_y(number_of_objects)=spheres(i).center_y-spheres(i).radius;
min_z(number_of_objects)=spheres(i).center_z-spheres(i).radius;
max_x(number_of_objects)=spheres(i).center_x+spheres(i).radius;
max_y(number_of_objects)=spheres(i).center_y+spheres(i).radius;
max_z(number_of_objects)=spheres(i).center_z+spheres(i).radius;
number_of_objects=number_of_objects+1;
end
for i=1:number_of_bricks
min_x(number_of_objects)=bricks(i).min_x;
min_y(number_of_objects)=bricks(i).min_y;
min_z(number_of_objects)=bricks(i).min_z;
max_x(number_of_objects)=bricks(i).max_x;
max_y(number_of_objects)=bricks(i).max_y;
max_z(number_of_objects)=bricks(i).max_z;
number_of_objects=number_of_objects+1;
end
fdtd_domain.min_x=min(min_x);
fdtd_domain.min_y=min(min_y);
fdtd_domain.min_z=min(min_z);
fdtd_domain.max_x=max(max_x);
fdtd_domain.max_y=max(max_y);
fdtd_domain.max_z=max(max_z);
%在最大值和最小值上加入空气缓冲区的大小
fdtd_domain.min_x=fdtd_domain.min_x...
-dx*boundary.air_buffer_number_of_cells_xn;
fdtd_domain.min_y=fdtd_domain.min_y...
-dy*boundary.air_buffer_number_of_cells_yn;
fdtd_domain.min_z=fdtd_domain.min_z...
-dz*boundary.air_buffer_number_of_cells_zn;
fdtd_domain.max_x=fdtd_domain.max_x...
-dx*boundary.air_buffer_number_of_cells_xp;
fdtd_domain.max_y=fdtd_domain.max_y...
-dy*boundary.air_buffer_number_of_cells_yp;
fdtd_domain.max_z=fdtd_domain.max_z...
-dz*boundary.air_buffer_number_of_cells_zp;
%根据边界条件加入吸收边界的网格数
if strcmp(boundary.type_xn,'cpml')&&...
(boundary.cpml_number_of_cells_xn>0)
fdtd_domain.min_x=fdtd_domain.min_x...
-dx*boundary.cpml_number_of_cells_xn;
end
if strcmp(boundary.type_xp,'cpml')&&...
(boundary.cpml_number_of_cells_xp>0)
fdtd_domain.max_x=fdtd_domain.max_x...
+dx*boundary.cpml_number_of_cells_xp;
end
if strcmp(boundary.type_yn,'cpml')&&...
(boundary.cpml_number_of_cells_yn>0)
fdtd_domain.min_y=fdtd_domain.min_y...
-dy*boundary.cpml_number_of_cells_yn;
end
if strcmp(boundary.type_yp,'cpml')&&...
(boundary.cpml_number_of_cells_yp>0)
fdtd_domain.max_y=fdtd_domain.max_y...
+dy*boundary.cpml_number_of_cells_yp;
end
if strcmp(boundary.type_zn,'cpml')&&...
(boundary.cpml_number_of_cells_zn>0)
fdtd_domain.min_z=fdtd_domain.min_z...
-dz*boundary.cpml_number_of_cells_zn;
end
if strcmp(boundary.type_zp,'cpml')&&...
(boundary.cpml_number_of_cells_zp>0)
fdtd_domain.max_z=fdtd_domain.max_z...
+dz*boundary.cpml_number_of_cells_zp;
end
%确定问题空间的大小
fdtd_domain.size_x=fdtd_domain.max_x-fdtd_domain.min_x;
fdtd_domain.size_y=fdtd_domain.max_y-fdtd_domain.min_y;
fdtd_domain.size_z=fdtd_domain.max_z-fdtd_domain.min_z;
%确定在x,y,z方向的网格数
nx=round(fdtd_domain.size_x/dx);
ny=round(fdtd_domain.size_y/dy);
nz=round(fdtd_domain.size_z/dz);
%根据网格数,重新调整域的大小
fdtd_domain.size_x=nx*dx;
fdtd_domain.size_y=ny*dy;
fdtd_domain.size_z=nz*dz;
fdtd_domain.max_x=fdtd_domain.min_x+fdtd_domain.size_x;
fdtd_domain.max_y=fdtd_domain.min_y+fdtd_domain.size_y;
fdtd_domain.max_z=fdtd_domain.min_z+fdtd_domain.size_z;
%根据电磁场位置确定的辅助参数
nxp1=nx+1;
nyp1=ny+1;
nzp1=nz+1;
nxm1=nx-1;
nxm2=nx-2;
nym1=ny-1;
nym2=ny-2;
nzm1=nz-1;
nzm2=nz-2;
%创建阵列存储的网格的中心坐标
fdtd_domain.cell_center_coordinates_x=zeros(nx,ny,nz);
fdtd_domain.cell_center_coordinates_y=zeros(nx,ny,nz);
fdtd_domain.cell_center_coordinates_z=zeros(nx,ny,nz);
for ind=1:nx
fdtd_domain.cell_center_coordinates_x(ind,:,:)=...
(ind-0.5)*dx+fdtd_domain.min_x;
end
for ind=1:ny
fdtd_domain.cell_center_coordinates_y(ind,:,:)=...
(ind-0.5)*dy+fdtd_domain.min_y;
end
for ind=1:nz
fdtd_domain.cell_center_coordinates_z(ind,:,:)=...
(ind-0.5)*dz+fdtd_domain.min_z;
end
cx=fdtd_domain.cell_center_coordinates_x;
cy=fdtd_domain.cell_center_coordinates_y;
cz=fdtd_domain.cell_center_coordinates_z;
for ind=1:number_of_spheres
%distance为网格中心点到球中心点距离
distance=sqrt((spheres(ind).center_x-cx).^2 ...
+(spheres(ind).center_y-cy).^2 ...
+(spheres(ind).center_z-cz).^2 );
I=find(distance<=spheres(ind).radius);
material_3d_space(I)=spheres(ind).material_type;
end
clear cx cy cz;
disp('creating bricks');
for ind=1:number_of_bricks
%确定每个立方体的坐标
blx=round((bricks(ind).min_x-fdtd_domain.min_x)/dx)+1;
bly=round((bricks(ind).min_y-fdtd_domain.min_y)/dy)+1;
blz=round((bricks(ind).min_z-fdtd_domain.min_z)/dz)+1;
bux=round((bricks(ind).max_x-fdtd_domain.min_x)/dx)+1;
buy=round((bricks(ind).max_y-fdtd_domain.min_y)/dy)+1;
buz=round((bricks(ind).max_z-fdtd_domain.min_z)/dz)+1;
%给每个立方体赋材料属性
material_3d_space(blx:bux-1,bly:buy-1,blz:buz-1)...
=bricks(ind).material_type;
end