声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2651|回复: 4

[其他相关] 有哪位大声有声学DtN边界或者PML的程序吗

[复制链接]
发表于 2016-3-21 15:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
如题自己写的程序老感觉运行结果有点问题,想对比一下,谢谢
回复
分享到:

使用道具 举报

发表于 2016-3-21 15:33 | 显示全部楼层
一阶声波方程波场模拟,采用的是交错网格,二阶精度,pml边界

  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %%交错网格---非均匀介质二维声波方程(一阶压力--速度)、2阶时间差分、2阶空间差分精度
  3. %%加上边界
  4. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5. close all;clear,clc
  6. tic
  7. %%***********************震源为Ricker子波*********
  8. dtt=0.0001;
  9. tt=-0.06:dtt:0.06;
  10. fm=30;
  11. A=0.01;
  12. wave=A*(1-2*(pi*fm*tt).^2).*exp(-(pi*fm*tt).^2);
  13. % plot(wave),title('震源子波--Ricker子波');
  14. %%***********************************************
  15. %% 模型参数设置
  16. dz=5;         % 纵向网格大小,单位m
  17. dx=5;         % 横向网格大小,单位m
  18. dt=0.0001;    % 时间步长,单位s
  19. T=0.5;        % 波动传播时间,单位s
  20. wave(round(T/dt))=0;    % 将子波后面部分补零
  21. % %% 研究区域
  22. % z=-75:dz:750;   x=-1000:dz:1000;
  23. pml=50;          % 吸收层的网格数
  24. plx=pml*dx;      % 上下吸收层的厚度
  25. plz=pml*dz;      % 左右吸收层的厚度
  26. z=-750-plz:dz:750+plz;   
  27. x=-1000-plx:dx:1000+plx;  % 采样区间
  28. n=length(z);     m=length(x);      % 采样点数
  29. z0=round(n/2);   x0=round(m/2);    % 震源位置
  30. Vmax=0;         % 纵波最大速度

  31. %%Setting Velocity & Density
  32. zt=-750-plz:dz/2:750+plz;  
  33. xt=-1000-plx:dx/2:1000+plx;   % 速度与密度采样区间
  34. nt=length(zt);     mt=length(xt);       % 速度与密度采样点数目
  35. V=zeros(n,m);       % 介质速度,m/s
  36. d=zeros(nt,mt);     % 介质密度,kg/m^3

  37. %%均匀介质模型
  38. for i=1:n
  39.     for k=1:m
  40.         V(i,k)=2.0e3;
  41.     end
  42. end
  43. for i=1:n
  44.     for k=1:m
  45.         d(2*i,2*k)=2.3e3;
  46.     end
  47. end

  48. % % %%层状介质模型
  49. % % for i=1:n
  50. % %     for k=1:m
  51. % %         if i < round(n/3)
  52. % %             V(i,k)=2.3e3;
  53. % %         else
  54. % %             V(i,k)=3.0e3;
  55. % %         end
  56. % %     end
  57. % % end
  58. for i=1:n-1
  59.     for k=1:m-1
  60.         d(2*i+1,2*k)=(d(2*i,2*k)+d(2*(i+1),2*k))/2;
  61.         d(2*i,2*k+1)=(d(2*i,2*k)+d(2*i,2*(k+1)))/2;
  62.     end
  63. end
  64. for i=1:n
  65.     for k=1:m
  66.         if V(i,k) > Vmax
  67.             Vmax=V(i,k);
  68.         end
  69.     end
  70. end
  71. %%**********************衰减系数************************
  72. %% ddx、ddz 即,x方向和z方向的衰减系数
  73. R=1e-6;          % 理论反射系数
  74. ddx=zeros(n,m); ddz=zeros(n,m);

  75. for i=1:n
  76.     for k=1:m
  77.         %% 区域1
  78.         if i>=1 & i<=pml & k>=1 & k<=pml
  79.             x=pml-k;z=pml-i;
  80.             ddx(i,k)=-log(R)*3*Vmax*x^2/(2*plx^2);
  81.             ddz(i,k)=-log(R)*3*Vmax*z^2/(2*plz^2);
  82.         elseif i>=1 & i<=pml & k>m-pml & k<=m
  83.             x=k-(m-pml);z=pml-i;
  84.             ddx(i,k)=-log(R)*3*Vmax*x^2/(2*plx^2);
  85.             ddz(i,k)=-log(R)*3*Vmax*z^2/(2*plz^2);
  86.         elseif i>n-pml & i<=n & k>=1 & k<=pml
  87.             x=pml-k;z=i-(n-pml);
  88.             ddx(i,k)=-log(R)*3*Vmax*x^2/(2*plx^2);
  89.             ddz(i,k)=-log(R)*3*Vmax*z^2/(2*plz^2);
  90.         elseif i>n-pml & i<=n & k>m-pml & k<=m
  91.             x=k-(m-pml);z=i-(n-pml);
  92.             ddx(i,k)=-log(R)*3*Vmax*x^2/(2*plx^2);
  93.             ddz(i,k)=-log(R)*3*Vmax*z^2/(2*plz^2);
  94.         %% 区域2
  95.         elseif i<=pml & k>pml & k<m-pml+1
  96.             x=0;z=pml-i;
  97.             ddx(i,k)=0;ddz(i,k)=-log(R)*3*Vmax*z^2/(2*plz^2);
  98.         elseif  i>n-pml & i<=n & k>pml & k<=m-pml
  99.             x=0;z=i-(n-pml);
  100.             ddx(i,k)=0;ddz(i,k)=-log(R)*3*Vmax*z^2/(2*plz^2);
  101.         %% 区域3
  102.         elseif i>pml & i<=n-pml & k<=pml
  103.             x=pml-k;z=0;
  104.             ddx(i,k)=-log(R)*3*Vmax*x^2/(2*plx^2);ddz(i,k)=0;
  105.         elseif i>pml & i<=n-pml & k>m-pml & k<=m
  106.             x=k-(m-pml);z=0;
  107.             ddx(i,k)=-log(R)*3*Vmax*x^2/(2*plx^2);ddz(i,k)=0;
  108.         end
  109.     end
  110. end
  111. % figure(1),imagesc(ddz),title('z方向衰减系数');
  112. % figure(2),imagesc(ddx),title('x方向衰减系数');
  113. %%**************************************************
  114. %%**********************波场模拟********************
  115. p0=zeros(n,m);    p1=zeros(n,m);
  116. px0=zeros(n,m);   px1=zeros(n,m);
  117. pz0=zeros(n,m);   pz1=zeros(n,m);
  118. K=zeros(n,m);      
  119. Vx1=zeros(nt,mt); Vx0=zeros(nt,mt);
  120. Vz1=zeros(nt,mt); Vz0=zeros(nt,mt);

  121. for t=dt:dt:T
  122.     p0(z0,x0)=dt*V(z0,x0)^2*wave(round(t/dt));
  123.     for i=2:n-1
  124.         for k=2:m-1
  125.             K(i,k)=d(2*i,2*k)*V(i,k)^2;
  126.             Vz1(2*i+1,2*k)=((1-0.5*dt*ddz(i,k))*Vz0(2*i+1,2*k)-dt*(p0(i+1,k)-p0(i,k))/(d(2*i+1,2*k)*dz))/(1+0.5*dt*ddz(i,k));
  127.             Vx1(2*i,2*k+1)=((1-0.5*dt*ddx(i,k))*Vx0(2*i,2*k+1)-dt*(p0(i,k+1)-p0(i,k))/(d(2*i,2*k+1)*dx))/(1+0.5*dt*ddx(i,k));
  128.             
  129.             pz1(i,k)=((1-0.5*dt*ddz(i,k))*pz0(i,k)-K(i,k)*dt*(Vz1(2*i+1,2*k)-Vz1(2*i-1,2*k))/dz)/(1+0.5*dt*ddz(i,k));
  130.             px1(i,k)=((1-0.5*dt*ddx(i,k))*px0(i,k)-K(i,k)*dt*(Vx1(2*i,2*k+1)-Vx1(2*i,2*k-1))/dx)/(1+0.5*dt*ddx(i,k));  
  131.             
  132.             p1(i,k)=px1(i,k)+pz1(i,k);
  133.         end
  134.     end
  135.     p0=p1;
  136.     pz0=pz1;px0=px1;
  137.     Vz0=Vz1;Vx0=Vx1;
  138.     for i=1:n-2*pml
  139.         for k=1:m-2*pml
  140.             p(i,k)=p1(i+pml,k+pml);
  141.         end
  142.     end
  143. %     imagesc(p1),title('声波波场'),pause(0.0000001);
  144. end
  145. figure(1),imagesc(p1);title('声波波场--交错网格,2阶');
  146. figure(2),imagesc(p);title('声波方程--交错网格、加边界');
  147. % figure(2),imagesc(pz1);title('z分量');
  148. % figure(3),imagesc(px1);title('x分量');
  149. %%*************************************************
  150. toc
复制代码

发表于 2016-3-21 15:35 | 显示全部楼层

  1. %***********************************************************************
  2. %     2-D FDTD TE code with PML absorbing boundary conditions
  3. %***********************************************************************
  4. %
  5. %     Program author: Susan C. Hagness
  6. %                     Department of Electrical and Computer Engineering
  7. %                     University of Wisconsin-Madison
  8. %                     1415 Engineering Drive
  9. %                     Madison, WI 53706-1691
  10. %                     608-265-5739
  11. %                     hagness@engr.wisc.edu
  12. %
  13. %     Date of this version:  February 2000
  14. %
  15. %     This MATLAB M-file implements the finite-difference time-domain
  16. %     solution of Maxwell's curl equations over a two-dimensional
  17. %     Cartesian space lattice comprised of uniform square grid cells.
  18. %
  19. %     To illustrate the algorithm, a 6-cm-diameter metal cylindrical  
  20. %     scatterer in free space is modeled. The source excitation is  
  21. %     a Gaussian pulse with a carrier frequency of 5 GHz.
  22. %
  23. %     The grid resolution (dx = 3 mm) was chosen to provide 20 samples
  24. %     per wavelength at the center frequency of the pulse (which in turn
  25. %     provides approximately 10 samples per wavelength at the high end
  26. %     of the excitation spectrum, around 10 GHz).
  27. %
  28. %     The computational domain is truncated using the perfectly matched
  29. %     layer (PML) absorbing boundary conditions.  The formulation used  
  30. %     in this code is based on the original split-field Berenger PML. The
  31. %     PML regions are labeled as shown in the following diagram:  
  32. %
  33. %            ----------------------------------------------
  34. %           |  |                BACK PML                |  |
  35. %            ----------------------------------------------
  36. %           |L |                                       /| R|
  37. %           |E |                                (ib,jb) | I|
  38. %           |F |                                        | G|
  39. %           |T |                                        | H|
  40. %           |  |                MAIN GRID               | T|
  41. %           |P |                                        |  |
  42. %           |M |                                        | P|
  43. %           |L | (1,1)                                  | M|
  44. %           |  |/                                       | L|
  45. %            ----------------------------------------------
  46. %           |  |                FRONT PML               |  |
  47. %            ----------------------------------------------
  48. %
  49. %     To execute this M-file, type "fdtd2D" at the MATLAB prompt.
  50. %     This M-file displays the FDTD-computed Ex, Ey, and Hz fields at  
  51. %     every 4th time step, and records those frames in a movie matrix,  
  52. %     M, which is played at the end of the simulation using the "movie"  
  53. %     command.
  54. %
  55. %***********************************************************************

  56. clear

  57. %***********************************************************************
  58. %     Fundamental constants
  59. %***********************************************************************

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

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

  66. %***********************************************************************
  67. %     Grid parameters
  68. %***********************************************************************

  69. ie=100;           %number of grid cells in x-direction
  70. je=50;            %number of grid cells in y-direction

  71. ib=ie+1;
  72. jb=je+1;

  73. is=15;            %location of z-directed hard source
  74. js=je/2;          %location of z-directed hard source

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

  77. nmax=300;         %total number of time steps

  78. iebc=8;           %thickness of left and right PML region
  79. jebc=8;           %thickness of front and back PML region
  80. rmax=0.00001;
  81. orderbc=2;
  82. ibbc=iebc+1;
  83. jbbc=jebc+1;
  84. iefbc=ie+2*iebc;
  85. jefbc=je+2*jebc;
  86. ibfbc=iefbc+1;
  87. jbfbc=jefbc+1;

  88. %***********************************************************************
  89. %     Material parameters
  90. %***********************************************************************

  91. media=2;

  92. eps=[1.0 1.0];
  93. sig=[0.0 1.0e+7];
  94. mur=[1.0 1.0];
  95. sim=[0.0 0.0];

  96. %***********************************************************************
  97. %     Wave excitation
  98. %***********************************************************************

  99. rtau=160.0e-12;
  100. tau=rtau/dt;
  101. delay=3*tau;

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

  106. %***********************************************************************
  107. %     Field arrays
  108. %***********************************************************************

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

  112. exbcf=zeros(iefbc,jebc);   %fields in front PML region
  113. eybcf=zeros(ibfbc,jebc);
  114. hzxbcf=zeros(iefbc,jebc);
  115. hzybcf=zeros(iefbc,jebc);

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

  120. exbcl=zeros(iebc,jb);      %fields in left PML region
  121. eybcl=zeros(iebc,je);
  122. hzxbcl=zeros(iebc,je);
  123. hzybcl=zeros(iebc,je);

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

  128. %***********************************************************************
  129. %     Updating coefficients
  130. %***********************************************************************

  131. for i=1:media
  132.   eaf  =dt*sig(i)/(2.0*epsz*eps(i));
  133.   ca(i)=(1.0-eaf)/(1.0+eaf);
  134.   cb(i)=dt/epsz/eps(i)/dx/(1.0+eaf);
  135.   haf  =dt*sim(i)/(2.0*muz*mur(i));
  136.   da(i)=(1.0-haf)/(1.0+haf);
  137.   db(i)=dt/muz/mur(i)/dx/(1.0+haf);
  138. end

  139. %***********************************************************************
  140. %     Geometry specification (main grid)
  141. %***********************************************************************

  142. %     Initialize entire main grid to free space

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

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

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

  149. %     Add metal cylinder

  150. diam=20;          % diameter of cylinder: 6 cm
  151. rad=diam/2.0;     % radius of cylinder: 3 cm
  152. icenter=4*ie/5;   % i-coordinate of cylinder's center
  153. jcenter=je/2;     % j-coordinate of cylinder's center

  154. for i=1:ie
  155. for j=1:je
  156.   dist2=(i+0.5-icenter)^2 + (j-jcenter)^2;
  157.   if dist2 <= rad^2  
  158.      caex(i,j)=ca(2);
  159.      cbex(i,j)=cb(2);
  160.   end
  161.   dist2=(i-icenter)^2 + (j+0.5-jcenter)^2;
  162.   if dist2 <= rad^2  
  163.      caey(i,j)=ca(2);
  164.      cbey(i,j)=cb(2);
  165.   end
  166. end
  167. end

  168. %***********************************************************************
  169. %     Fill the PML regions
  170. %***********************************************************************

  171. delbc=iebc*dx;
  172. sigmam=-log(rmax/100.0)*epsz*cc*(orderbc+1)/(2*delbc);
  173. bcfactor=eps(1)*sigmam/(dx*(delbc^orderbc)*(orderbc+1));

  174. %     FRONT region  

  175. caexbcf(1:iefbc,1)=1.0;
  176. cbexbcf(1:iefbc,1)=0.0;
  177. for j=2:jebc
  178.   y1=(jebc-j+1.5)*dx;
  179.   y2=(jebc-j+0.5)*dx;
  180.   sigmay=bcfactor*(y1^(orderbc+1)-y2^(orderbc+1));
  181.   ca1=exp(-sigmay*dt/(epsz*eps(1)));
  182.   cb1=(1.0-ca1)/(sigmay*dx);
  183.   caexbcf(1:iefbc,j)=ca1;
  184.   cbexbcf(1:iefbc,j)=cb1;
  185. end
  186. sigmay = bcfactor*(0.5*dx)^(orderbc+1);
  187. ca1=exp(-sigmay*dt/(epsz*eps(1)));
  188. cb1=(1-ca1)/(sigmay*dx);
  189. caex(1:ie,1)=ca1;
  190. cbex(1:ie,1)=cb1;
  191. caexbcl(1:iebc,1)=ca1;
  192. cbexbcl(1:iebc,1)=cb1;
  193. caexbcr(1:iebc,1)=ca1;
  194. cbexbcr(1:iebc,1)=cb1;

  195. for j=1:jebc
  196.   y1=(jebc-j+1)*dx;
  197.   y2=(jebc-j)*dx;
  198.   sigmay=bcfactor*(y1^(orderbc+1)-y2^(orderbc+1));
  199.   sigmays=sigmay*(muz/(epsz*eps(1)));
  200.   da1=exp(-sigmays*dt/muz);
  201.   db1=(1-da1)/(sigmays*dx);
  202.   dahzybcf(1:iefbc,j)=da1;
  203.   dbhzybcf(1:iefbc,j)=db1;
  204.   caeybcf(1:ibfbc,j)=ca(1);
  205.   cbeybcf(1:ibfbc,j)=cb(1);
  206.   dahzxbcf(1:iefbc,j)=da(1);
  207.   dbhzxbcf(1:iefbc,j)=db(1);
  208. end

  209. %     BACK region  

  210. caexbcb(1:iefbc,jbbc)=1.0;
  211. cbexbcb(1:iefbc,jbbc)=0.0;
  212. for j=2:jebc
  213.   y1=(j-0.5)*dx;
  214.   y2=(j-1.5)*dx;
  215.   sigmay=bcfactor*(y1^(orderbc+1)-y2^(orderbc+1));
  216.   ca1=exp(-sigmay*dt/(epsz*eps(1)));
  217.   cb1=(1-ca1)/(sigmay*dx);
  218.   caexbcb(1:iefbc,j)=ca1;
  219.   cbexbcb(1:iefbc,j)=cb1;
  220. end
  221. sigmay = bcfactor*(0.5*dx)^(orderbc+1);
  222. ca1=exp(-sigmay*dt/(epsz*eps(1)));
  223. cb1=(1-ca1)/(sigmay*dx);
  224. caex(1:ie,jb)=ca1;
  225. cbex(1:ie,jb)=cb1;
  226. caexbcl(1:iebc,jb)=ca1;
  227. cbexbcl(1:iebc,jb)=cb1;
  228. caexbcr(1:iebc,jb)=ca1;
  229. cbexbcr(1:iebc,jb)=cb1;

  230. for j=1:jebc
  231.   y1=j*dx;
  232.   y2=(j-1)*dx;
  233.   sigmay=bcfactor*(y1^(orderbc+1)-y2^(orderbc+1));
  234.   sigmays=sigmay*(muz/(epsz*eps(1)));
  235.   da1=exp(-sigmays*dt/muz);
  236.   db1=(1-da1)/(sigmays*dx);
  237.   dahzybcb(1:iefbc,j)=da1;
  238.   dbhzybcb(1:iefbc,j)=db1;
  239.   caeybcb(1:ibfbc,j)=ca(1);
  240.   cbeybcb(1:ibfbc,j)=cb(1);
  241.   dahzxbcb(1:iefbc,j)=da(1);
  242.   dbhzxbcb(1:iefbc,j)=db(1);
  243. end

  244. %     LEFT region  

  245. caeybcl(1,1:je)=1.0;
  246. cbeybcl(1,1:je)=0.0;
  247. for i=2:iebc
  248.   x1=(iebc-i+1.5)*dx;
  249.   x2=(iebc-i+0.5)*dx;
  250.   sigmax=bcfactor*(x1^(orderbc+1)-x2^(orderbc+1));
  251.   ca1=exp(-sigmax*dt/(epsz*eps(1)));
  252.   cb1=(1-ca1)/(sigmax*dx);
  253.   caeybcl(i,1:je)=ca1;
  254.   cbeybcl(i,1:je)=cb1;
  255.   caeybcf(i,1:jebc)=ca1;
  256.   cbeybcf(i,1:jebc)=cb1;
  257.   caeybcb(i,1:jebc)=ca1;
  258.   cbeybcb(i,1:jebc)=cb1;
  259. end
  260. sigmax=bcfactor*(0.5*dx)^(orderbc+1);
  261. ca1=exp(-sigmax*dt/(epsz*eps(1)));
  262. cb1=(1-ca1)/(sigmax*dx);
  263. caey(1,1:je)=ca1;
  264. cbey(1,1:je)=cb1;
  265. caeybcf(iebc+1,1:jebc)=ca1;
  266. cbeybcf(iebc+1,1:jebc)=cb1;
  267. caeybcb(iebc+1,1:jebc)=ca1;
  268. cbeybcb(iebc+1,1:jebc)=cb1;

  269. for i=1:iebc
  270.   x1=(iebc-i+1)*dx;
  271.   x2=(iebc-i)*dx;
  272.   sigmax=bcfactor*(x1^(orderbc+1)-x2^(orderbc+1));
  273.   sigmaxs=sigmax*(muz/(epsz*eps(1)));
  274.   da1=exp(-sigmaxs*dt/muz);
  275.   db1=(1-da1)/(sigmaxs*dx);
  276.   dahzxbcl(i,1:je)=da1;
  277.   dbhzxbcl(i,1:je)=db1;
  278.   dahzxbcf(i,1:jebc)=da1;
  279.   dbhzxbcf(i,1:jebc)=db1;
  280.   dahzxbcb(i,1:jebc)=da1;
  281.   dbhzxbcb(i,1:jebc)=db1;
  282.   caexbcl(i,2:je)=ca(1);
  283.   cbexbcl(i,2:je)=cb(1);
  284.   dahzybcl(i,1:je)=da(1);
  285.   dbhzybcl(i,1:je)=db(1);
  286. end

  287. %     RIGHT region  

  288. caeybcr(ibbc,1:je)=1.0;
  289. cbeybcr(ibbc,1:je)=0.0;
  290. for i=2:iebc
  291.   x1=(i-0.5)*dx;
  292.   x2=(i-1.5)*dx;
  293.   sigmax=bcfactor*(x1^(orderbc+1)-x2^(orderbc+1));
  294.   ca1=exp(-sigmax*dt/(epsz*eps(1)));
  295.   cb1=(1-ca1)/(sigmax*dx);
  296.   caeybcr(i,1:je)=ca1;
  297.   cbeybcr(i,1:je)=cb1;
  298.   caeybcf(i+iebc+ie,1:jebc)=ca1;
  299.   cbeybcf(i+iebc+ie,1:jebc)=cb1;
  300.   caeybcb(i+iebc+ie,1:jebc)=ca1;
  301.   cbeybcb(i+iebc+ie,1:jebc)=cb1;
  302. end
  303. sigmax=bcfactor*(0.5*dx)^(orderbc+1);
  304. ca1=exp(-sigmax*dt/(epsz*eps(1)));
  305. cb1=(1-ca1)/(sigmax*dx);
  306. caey(ib,1:je)=ca1;
  307. cbey(ib,1:je)=cb1;
  308. caeybcf(iebc+ib,1:jebc)=ca1;
  309. cbeybcf(iebc+ib,1:jebc)=cb1;
  310. caeybcb(iebc+ib,1:jebc)=ca1;
  311. cbeybcb(iebc+ib,1:jebc)=cb1;

  312. for i=1:iebc
  313.   x1=i*dx;
  314.   x2=(i-1)*dx;
  315.   sigmax=bcfactor*(x1^(orderbc+1)-x2^(orderbc+1));
  316.   sigmaxs=sigmax*(muz/(epsz*eps(1)));
  317.   da1=exp(-sigmaxs*dt/muz);
  318.   db1=(1-da1)/(sigmaxs*dx);
  319.   dahzxbcr(i,1:je) = da1;
  320.   dbhzxbcr(i,1:je) = db1;
  321.   dahzxbcf(i+ie+iebc,1:jebc)=da1;
  322.   dbhzxbcf(i+ie+iebc,1:jebc)=db1;
  323.   dahzxbcb(i+ie+iebc,1:jebc)=da1;
  324.   dbhzxbcb(i+ie+iebc,1:jebc)=db1;
  325.   caexbcr(i,2:je)=ca(1);
  326.   cbexbcr(i,2:je)=cb(1);
  327.   dahzybcr(i,1:je)=da(1);
  328.   dbhzybcr(i,1:je)=db(1);
  329. end

  330. %***********************************************************************
  331. %     Movie initialization
  332. %***********************************************************************

  333. subplot(3,1,1),pcolor(ex');
  334. shading flat;
  335. caxis([-80.0 80.0]);
  336. axis([1 ie 1 jb]);
  337. colorbar;
  338. axis image;
  339. axis off;
  340. title(['Ex at time step = 0']);

  341. subplot(3,1,2),pcolor(ey');
  342. shading flat;
  343. caxis([-80.0 80.0]);
  344. axis([1 ib 1 je]);
  345. colorbar;
  346. axis image;
  347. axis off;
  348. title(['Ey at time step = 0']);

  349. subplot(3,1,3),pcolor(hz');
  350. shading flat;
  351. caxis([-0.2 0.2]);
  352. axis([1 ie 1 je]);
  353. colorbar;
  354. axis image;
  355. axis off;
  356. title(['Hz at time step = 0']);

  357. rect=get(gcf,'Position');
  358. rect(1:2)=[0 0];

  359. M=moviein(nmax/4,gcf,rect);

  360. %***********************************************************************
  361. %     BEGIN TIME-STEPPING LOOP
  362. %***********************************************************************

  363. for n=1:nmax

  364. %***********************************************************************
  365. %     Update electric fields (EX and EY) in main grid
  366. %***********************************************************************

  367. ex(:,2:je)=caex(:,2:je).*ex(:,2:je)+...
  368.            cbex(:,2:je).*(hz(:,2:je)-hz(:,1:je-1));

  369. ey(2:ie,:)=caey(2:ie,:).*ey(2:ie,:)+...
  370.            cbey(2:ie,:).*(hz(1:ie-1,:)-hz(2:ie,:));

  371. %***********************************************************************
  372. %     Update EX in PML regions
  373. %***********************************************************************

  374. %     FRONT

  375. exbcf(:,2:jebc)=caexbcf(:,2:jebc).*exbcf(:,2:jebc)-...   
  376.   cbexbcf(:,2:jebc).*(hzxbcf(:,1:jebc-1)+hzybcf(:,1:jebc-1)-...
  377.                       hzxbcf(:,2:jebc)-hzybcf(:,2:jebc));
  378. ex(1:ie,1)=caex(1:ie,1).*ex(1:ie,1)-...
  379.   cbex(1:ie,1).*(hzxbcf(ibbc:iebc+ie,jebc)+...
  380.                 hzybcf(ibbc:iebc+ie,jebc)-hz(1:ie,1));
  381.   
  382. %     BACK

  383. exbcb(:,2:jebc-1)=caexbcb(:,2:jebc-1).*exbcb(:,2:jebc-1)-...
  384.   cbexbcb(:,2:jebc-1).*(hzxbcb(:,1:jebc-2)+hzybcb(:,1:jebc-2)-...
  385.                         hzxbcb(:,2:jebc-1)-hzybcb(:,2:jebc-1));
  386. ex(1:ie,jb)=caex(1:ie,jb).*ex(1:ie,jb)-...
  387.   cbex(1:ie,jb).*(hz(1:ie,jb-1)-hzxbcb(ibbc:iebc+ie,1)-...
  388.                  hzybcb(ibbc:iebc+ie,1));
  389.   
  390. %     LEFT

  391. exbcl(:,2:je)=caexbcl(:,2:je).*exbcl(:,2:je)-...
  392.   cbexbcl(:,2:je).*(hzxbcl(:,1:je-1)+hzybcl(:,1:je-1)-...
  393.                     hzxbcl(:,2:je)-hzybcl(:,2:je));
  394. exbcl(:,1)=caexbcl(:,1).*exbcl(:,1)-...
  395.   cbexbcl(:,1).*(hzxbcf(1:iebc,jebc)+hzybcf(1:iebc,jebc)-...
  396.                  hzxbcl(:,1)-hzybcl(:,1));
  397. exbcl(:,jb)=caexbcl(:,jb).*exbcl(:,jb)-...
  398.   cbexbcl(:,jb).*(hzxbcl(:,je)+hzybcl(:,je)-...
  399.                   hzxbcb(1:iebc,1)-hzybcb(1:iebc,1));
  400.   
  401. %     RIGHT

  402. exbcr(:,2:je)=caexbcr(:,2:je).*exbcr(:,2:je)-...
  403.   cbexbcr(:,2:je).*(hzxbcr(:,1:je-1)+hzybcr(:,1:je-1)-...
  404.                     hzxbcr(:,2:je)-hzybcr(:,2:je));
  405. exbcr(:,1)=caexbcr(:,1).*exbcr(:,1)-...
  406.   cbexbcr(:,1).*(hzxbcf(1+iebc+ie:iefbc,jebc)+...
  407.                  hzybcf(1+iebc+ie:iefbc,jebc)-...
  408.                  hzxbcr(:,1)-hzybcr(:,1));
  409. exbcr(:,jb)=caexbcr(:,jb).*exbcr(:,jb)-...
  410.   cbexbcr(:,jb).*(hzxbcr(:,je)+hzybcr(:,je)-...
  411.                   hzxbcb(1+iebc+ie:iefbc,1)-...
  412.                   hzybcb(1+iebc+ie:iefbc,1));
  413.   
  414. %***********************************************************************
  415. %     Update EY in PML regions
  416. %***********************************************************************

  417. %     FRONT

  418. eybcf(2:iefbc,:)=caeybcf(2:iefbc,:).*eybcf(2:iefbc,:)-...
  419.   cbeybcf(2:iefbc,:).*(hzxbcf(2:iefbc,:)+hzybcf(2:iefbc,:)-...
  420.                        hzxbcf(1:iefbc-1,:)-hzybcf(1:iefbc-1,:));
  421.   
  422. %     BACK

  423. eybcb(2:iefbc,:)=caeybcb(2:iefbc,:).*eybcb(2:iefbc,:)-...
  424.   cbeybcb(2:iefbc,:).*(hzxbcb(2:iefbc,:)+hzybcb(2:iefbc,:)-...
  425.                        hzxbcb(1:iefbc-1,:)-hzybcb(1:iefbc-1,:));
  426.   
  427. %     LEFT

  428. eybcl(2:iebc,:)=caeybcl(2:iebc,:).*eybcl(2:iebc,:)-...
  429.   cbeybcl(2:iebc,:).*(hzxbcl(2:iebc,:)+hzybcl(2:iebc,:)-...
  430.                       hzxbcl(1:iebc-1,:)-hzybcl(1:iebc-1,:));
  431. ey(1,:)=caey(1,:).*ey(1,:)-...
  432.   cbey(1,:).*(hz(1,:)-hzxbcl(iebc,:)-hzybcl(iebc,:));
  433.   
  434. %     RIGHT

  435. eybcr(2:iebc,:)=caeybcr(2:iebc,:).*eybcr(2:iebc,:)-...
  436.   cbeybcr(2:iebc,:).*(hzxbcr(2:iebc,:)+hzybcr(2:iebc,:)-...
  437.                       hzxbcr(1:iebc-1,:)-hzybcr(1:iebc-1,:));
  438. ey(ib,:)=caey(ib,:).*ey(ib,:)-...
  439.   cbey(ib,:).*(hzxbcr(1,:)+hzybcr(1,:)- hz(ie,:));


  440. %***********************************************************************
  441. %     Update magnetic fields (HZ) in main grid
  442. %***********************************************************************

  443. hz(1:ie,1:je)=dahz(1:ie,1:je).*hz(1:ie,1:je)+...  
  444.               dbhz(1:ie,1:je).*(ex(1:ie,2:jb)-ex(1:ie,1:je)+...
  445.                                 ey(1:ie,1:je)-ey(2:ib,1:je));

  446. hz(is,js)=source(n);


  447. %***********************************************************************
  448. %     Update HZX in PML regions
  449. %***********************************************************************

  450. %     FRONT

  451. hzxbcf(1:iefbc,:)=dahzxbcf(1:iefbc,:).*hzxbcf(1:iefbc,:)-...
  452.   dbhzxbcf(1:iefbc,:).*(eybcf(2:ibfbc,:)-eybcf(1:iefbc,:));
  453.   
  454. %     BACK
  455.   
  456. hzxbcb(1:iefbc,:)=dahzxbcb(1:iefbc,:).*hzxbcb(1:iefbc,:)-...
  457.   dbhzxbcb(1:iefbc,:).*(eybcb(2:ibfbc,:)-eybcb(1:iefbc,:));
  458.   
  459. %     LEFT
  460.   
  461. hzxbcl(1:iebc-1,:)=dahzxbcl(1:iebc-1,:).*hzxbcl(1:iebc-1,:)-...
  462.   dbhzxbcl(1:iebc-1,:).*(eybcl(2:iebc,:)-eybcl(1:iebc-1,:));
  463. hzxbcl(iebc,:)=dahzxbcl(iebc,:).*hzxbcl(iebc,:)-...
  464.   dbhzxbcl(iebc,:).*(ey(1,:)-eybcl(iebc,:));
  465.   
  466. %     RIGHT
  467.   
  468. hzxbcr(2:iebc,:)=dahzxbcr(2:iebc,:).*hzxbcr(2:iebc,:)-...
  469.   dbhzxbcr(2:iebc,:).*(eybcr(3:ibbc,:)-eybcr(2:iebc,:));
  470. hzxbcr(1,:)=dahzxbcr(1,:).*hzxbcr(1,:)-...
  471.   dbhzxbcr(1,:).*(eybcr(2,:)-ey(ib,:));
  472.   
  473. %***********************************************************************
  474. %     Update HZY in PML regions
  475. %***********************************************************************

  476. %     FRONT
  477.   
  478. hzybcf(:,1:jebc-1)=dahzybcf(:,1:jebc-1).*hzybcf(:,1:jebc-1)-...
  479.   dbhzybcf(:,1:jebc-1).*(exbcf(:,1:jebc-1)-exbcf(:,2:jebc));
  480. hzybcf(1:iebc,jebc)=dahzybcf(1:iebc,jebc).*hzybcf(1:iebc,jebc)-...
  481.   dbhzybcf(1:iebc,jebc).*(exbcf(1:iebc,jebc)-exbcl(1:iebc,1));
  482. hzybcf(iebc+1:iebc+ie,jebc)=...
  483.   dahzybcf(iebc+1:iebc+ie,jebc).*hzybcf(iebc+1:iebc+ie,jebc)-...
  484.   dbhzybcf(iebc+1:iebc+ie,jebc).*(exbcf(iebc+1:iebc+ie,jebc)-...
  485.                                   ex(1:ie,1));
  486. hzybcf(iebc+ie+1:iefbc,jebc)=...
  487.   dahzybcf(iebc+ie+1:iefbc,jebc).*hzybcf(iebc+ie+1:iefbc,jebc)-...
  488.   dbhzybcf(iebc+ie+1:iefbc,jebc).*(exbcf(iebc+ie+1:iefbc,jebc)-...
  489.                                    exbcr(1:iebc,1));

  490. %     BACK
  491.   
  492. hzybcb(1:iefbc,2:jebc)=dahzybcb(1:iefbc,2:jebc).*hzybcb(1:iefbc,2:jebc)-...
  493.   dbhzybcb(1:iefbc,2:jebc).*(exbcb(1:iefbc,2:jebc)-exbcb(1:iefbc,3:jbbc));
  494. hzybcb(1:iebc,1)=dahzybcb(1:iebc,1).*hzybcb(1:iebc,1)-...
  495.   dbhzybcb(1:iebc,1).*(exbcl(1:iebc,jb)-exbcb(1:iebc,2));
  496. hzybcb(iebc+1:iebc+ie,1)=...
  497.   dahzybcb(iebc+1:iebc+ie,1).*hzybcb(iebc+1:iebc+ie,1)-...
  498.   dbhzybcb(iebc+1:iebc+ie,1).*(ex(1:ie,jb)-exbcb(iebc+1:iebc+ie,2));
  499. hzybcb(iebc+ie+1:iefbc,1)=...
  500.   dahzybcb(iebc+ie+1:iefbc,1).*hzybcb(iebc+ie+1:iefbc,1)-...
  501.   dbhzybcb(iebc+ie+1:iefbc,1).*(exbcr(1:iebc,jb)-...
  502.                                 exbcb(iebc+ie+1:iefbc,2));
  503.   
  504. %     LEFT
  505.   
  506. hzybcl(:,1:je)=dahzybcl(:,1:je).*hzybcl(:,1:je)-...
  507.   dbhzybcl(:,1:je).*(exbcl(:,1:je)-exbcl(:,2:jb));
  508.   
  509. %     RIGHT
  510.   
  511. hzybcr(:,1:je)=dahzybcr(:,1:je).*hzybcr(:,1:je)-...
  512.   dbhzybcr(:,1:je).*(exbcr(:,1:je)-exbcr(:,2:jb));

  513. %***********************************************************************
  514. %     Visualize fields
  515. %***********************************************************************

  516. if mod(n,4)==0;

  517. timestep=int2str(n);

  518. subplot(3,1,1),pcolor(ex');
  519. shading flat;
  520. caxis([-80.0 80.0]);
  521. axis([1 ie 1 jb]);
  522. colorbar;
  523. axis image;
  524. axis off;
  525. title(['Ex at time step = ',timestep]);

  526. subplot(3,1,2),pcolor(ey');
  527. shading flat;
  528. caxis([-80.0 80.0]);
  529. axis([1 ib 1 je]);
  530. colorbar;
  531. axis image;
  532. axis off;
  533. title(['Ey at time step = ',timestep]);

  534. subplot(3,1,3),pcolor(hz');
  535. shading flat;
  536. caxis([-0.2 0.2]);
  537. axis([1 ie 1 je]);
  538. colorbar;
  539. axis image;
  540. axis off;
  541. title(['Hz at time step = ',timestep]);

  542. nn=n/4;
  543. M(:,nn)=getframe(gcf,rect);

  544. end;

  545. %***********************************************************************
  546. %     END TIME-STEPPING LOOP
  547. %***********************************************************************

  548. end

  549. movie(gcf,M,0,10,rect);
复制代码
发表于 2016-3-21 15:39 | 显示全部楼层
下面是二阶声波方程的PML边界处理程序,Visual C++的

acoustic-wave-with-PML.zip

2.38 MB, 下载次数: 6

 楼主| 发表于 2016-3-22 18:05 | 显示全部楼层
风花雪月 发表于 2016-3-21 15:39
下面是二阶声波方程的PML边界处理程序,Visual C++的

多谢大神,不知道是否有基于有限元的没啊
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-11-17 07:26 , Processed in 0.088465 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表