├── README.md ├── Lecture2e1_SlabTrmRfc.m └── Lecture2e2_SiliconReflection.m /README.md: -------------------------------------------------------------------------------- 1 | # FDTD 2 | Learn 1D to 2D FDTD from online courses 3 | https://empossible.net/academics/emp5304/ 4 | -------------------------------------------------------------------------------- /Lecture2e1_SlabTrmRfc.m: -------------------------------------------------------------------------------- 1 | clc; 2 | close all; 3 | clear; 4 | %% 5 | %Question:1 foot materials=304.8mm;mu=2; epsilon=6; 6 | %To get the reflection and transmission 7 | % 8 | 9 | %% 10 | %Initialize Simulation 11 | % Define units 12 | % Define constants 13 | c_0=299792458;%units:m/s 14 | 15 | %% 16 | %%Define Simulation Parameters 17 | % ? Device parameters 18 | mu_r=2; 19 | epsilon_r=6; 20 | n_max=sqrt(mu_r*epsilon_r);%refractive index of slab 21 | n_bc=1;%refractive index of boundary 22 | thickness=304.8*10^-3;%unit:m 23 | % ? Frequency range (fmax) 24 | f_max=10^9; 25 | lambda_min=c_0/f_max/n_max; 26 | 27 | % ? Grid parameters (NRES, etc.) 28 | N_lambda=20; 29 | 30 | %% Compute Grid Resolution 31 | Dlambda=lambda_min/N_lambda; 32 | Dd=thickness/4; 33 | DzPrim=min(Dlambda,Dd);%Minimum requirement about Z 34 | N_slab=ceil(thickness/DzPrim);%Steps in slab 35 | Dz=thickness/N_slab;%Z step 36 | 37 | %% Buidl grid 38 | N_total=N_slab+2*10+3;%2*10 for spaces;3 for TF/SF and reflection/transmission; 39 | % INITIALIZE Field on Grid 40 | Ey = zeros(1,N_total); 41 | Hx = zeros(1,N_total); 42 | % INITIALIZE MATERIALS TO FREE SPACE 43 | ER = ones(1,N_total); 44 | UR = ones(1,N_total); 45 | %%Build Device on Grid 46 | Nz1=13;Nz2=83;%position of slab 47 | UR(Nz1:Nz2) = ones(1,(Nz2-Nz1+1))*mu_r; 48 | ER(Nz1:Nz2) = ones(1,(Nz2-Nz1+1))*epsilon_r; 49 | %%Compute the Time Step 50 | Dt=n_bc*Dz/2/c_0; 51 | 52 | %% Compute Source Parameters 53 | tau=1/2/f_max;% 54 | t_0=6*tau; 55 | 56 | %% Compute Number of Time Steps 57 | t_prop=n_max*N_total*Dz/c_0; 58 | T=12*tau+5*t_prop; 59 | STEPS=ceil(T/Dt); 60 | 61 | %% Compute the Source Functions 62 | t=[0:(STEPS-1)]*Dt;%time axis 63 | deltat=n_bc*Dz/2/c_0+Dt/2;%total delay between E and H 64 | A=-sqrt(1/1);%amplitude of H field 65 | Esrc=exp(-((t-t_0)/tau).^2);%amplitude of H field 66 | Hsrc=A*exp(-((t-t_0+deltat)/tau).^2);%amplitude of H field 67 | nzsrc=2;%position of source injection 68 | 69 | %% Initialize the Fourier Transforms 70 | N_freq = 100;%Frequency numbers 71 | FREQ = linspace(0,1*10^9,N_freq);%Frequency 72 | K = exp(-1i*2*pi*Dt.*FREQ);%Kernerl 73 | REF = zeros(1,N_freq); 74 | TRN = zeros(1,N_freq); 75 | SRC = zeros(1,N_freq); 76 | 77 | %% Initialize Detection parameters 78 | EyR = zeros(1,N_freq); 79 | EyT = zeros(1,N_freq); 80 | 81 | 82 | %% FDTD main loop 83 | figure;%for E/H field 84 | 85 | % COMPUTE UPDATE COEFFICIENTS 86 | mEy = (c_0*Dt) ./ ER; 87 | mHx = (c_0*Dt) ./ UR; 88 | % Initialize boundary 89 | H2=0; H1=0; 90 | E2=0; E1=0; 91 | 92 | % FDTD main loop 93 | 94 | for T = 1 : STEPS 95 | 96 | % Record H-Field at Boundary 97 | H2=H1; H1=Hx(1); 98 | 99 | % Update H from E 100 | for nz = 1 : (N_total-1) 101 | Hx(nz) = Hx(nz) + mHx(nz)*(Ey(nz+1) - Ey(nz))/Dz; 102 | end 103 | Hx(N_total) = Hx(N_total) + mHx(N_total)*(E2 - Ey(N_total))/Dz; 104 | 105 | % Handle H Source 106 | Hx(1)=Hx(1)-mHx(1)/Dz*Esrc(T); 107 | 108 | % Record E-Field Boundary 109 | E2=E1; E1=Ey(N_total); 110 | 111 | % Update E from H 112 | Ey(1) = Ey(1) + mEy(1)*(Hx(1) - H2)/Dz; 113 | for nz = 2 : N_total 114 | Ey(nz) = Ey(nz) + mEy(nz)*(Hx(nz) - Hx(nz-1))/Dz; 115 | end 116 | 117 | % Handle E Source 118 | Ey(2) = Ey(2)-mEy(2)/Dz*Hsrc(T); 119 | 120 | % Inject E Source 121 | % Ey(nzsrc) = Ey(nzsrc) + Esrc(T); 122 | % Ey(nzsrc) = Esrc(T); 123 | % Hx(nzsrc-1) = Hx(nzsrc-1) + Hsrc(T); 124 | 125 | 126 | subplot(2,1,1); 127 | % Visualize 128 | plot([1:N_total]*Dz,Ey,'b');hold on; 129 | plot([1:N_total]*Dz,Hx,'r');hold off 130 | ylim([-2 2]); 131 | xlim([0 0.4035]); 132 | title(['FIELD STEP ',num2str(T),' of ',num2str(STEPS)]); 133 | 134 | 135 | % Update Fourier Transforms 136 | for nf = 1 : N_freq 137 | % Integrate REF(nf), TRN(nf), and SRC(nf) 138 | EyR(nf) = EyR(nf) + (K(nf)^T)*Ey(1); 139 | EyT(nf) = EyT(nf) + (K(nf)^T)*Ey(N_total); 140 | SRC(nf) = SRC(nf) + (K(nf)^T)*Esrc(T); 141 | end 142 | % FINISH FOURIER TRANSFORMS 143 | % EyR = EyR*Dt; 144 | % EyT = EyT*Dt; 145 | % SRC = SRC*Dt; 146 | 147 | % COMPUTE REFLECTANCE 148 | % AND TRANSMITTANCE 149 | REF = abs(EyR./SRC).^2; 150 | TRN = abs(EyT./SRC).^2; 151 | CON = REF + TRN; 152 | 153 | 154 | subplot(2,1,2); 155 | plot(REF,'r'); 156 | hold on; 157 | plot(TRN,'b'); 158 | plot(CON,'k--') 159 | hold off 160 | ylim([0 2]); 161 | pause(0.00010); 162 | 163 | if ((T-447)^2<0.01) 164 | a=1 165 | elseif ((T-837)^2<0.01) 166 | a=2 167 | elseif (T-1383)^2<0.01 168 | a=3 169 | end 170 | end 171 | 172 | 173 | %% Post-Process the Data 174 | 175 | 176 | 177 | 178 | 179 | 180 | -------------------------------------------------------------------------------- /Lecture2e2_SiliconReflection.m: -------------------------------------------------------------------------------- 1 | clc; 2 | close all; 3 | clear; 4 | %% 5 | %Question:1 foot materials=304.8mm 6 | 7 | %% 8 | %Initialize Simulation 9 | % Define units 10 | % Define constants 11 | c_0=299792458;%units:m/s 12 | 13 | %% 14 | %%Define Simulation Parameters 15 | % Device parameters 16 | mu_r=1; 17 | epsilon_r=3.5^2; 18 | n_si=3.5;%refractive index of slab 19 | n_air=1;%refractive index of boundary 20 | thickness=9.3e-06;%unit:m 21 | % Frequency range (fmax) 22 | lambda=(1500:1:1600)*10^(-9);%wavelength from 1500 nm to 1600 nm; 23 | f_air=c_0./lambda/n_air; 24 | f_si=c_0./lambda/n_si; 25 | f_max=max([max(f_air),max(f_si)]); 26 | lambda_min=min(lambda)/n_si; 27 | 28 | 29 | %% Compute Grid Resolution 30 | N_lambda=20; % Grid parameters (NRES, etc.) 31 | Dlambda=lambda_min/N_lambda; 32 | Dd=thickness/4; 33 | DzPrim=min(Dlambda,Dd);%Minimum requirement about Z 34 | N_si=ceil(thickness/DzPrim);%Steps in slab 35 | Dz=thickness/N_si;%Z step 36 | 37 | %% Buidl grid 38 | N_total=100+1+100+N_si+100;%100 for spaces;1 for TF/SF; 100 for spaces;The rest is Silicon;100 for spaces; 39 | % INITIALIZE Field on Grid 40 | Ey = zeros(1,N_total); 41 | Hx = zeros(1,N_total); 42 | % INITIALIZE MATERIALS TO FREE SPACE 43 | ER = ones(1,N_total); 44 | UR = ones(1,N_total); 45 | %%Build Device on Grid 46 | Nz1=202;Nz2=635;%position of slab 47 | UR(Nz1:Nz2) = ones(1,(Nz2-Nz1+1))*mu_r; 48 | ER(Nz1:Nz2) = ones(1,(Nz2-Nz1+1))*epsilon_r; 49 | %%Compute the Time Step 50 | Dt=n_air*Dz/2/c_0; 51 | 52 | %% Compute Source Parameters 53 | tau=1/2/f_max;% 54 | t_0=6*tau; 55 | 56 | %% Compute Number of Time Steps 57 | t_prop=n_si*N_total*Dz/c_0; 58 | T=6*tau+0.5*t_prop; 59 | STEPS=ceil(T/Dt)+1000; 60 | 61 | %% Compute the Source Functions 62 | t=[0:(STEPS-1)]*Dt;%time axis 63 | deltat=n_air*Dz/2/c_0+Dt/2;%total delay between E and H 64 | A=-sqrt(1/1);%amplitude of H field 65 | Esrc=exp(-((t-t_0)/tau).^2);%amplitude of H field 66 | Hsrc=A*exp(-((t-t_0+deltat)/tau).^2);%amplitude of H field 67 | nzsrc=101;%position of source injection 68 | 69 | %% Initialize the Fourier Transforms 70 | N_freq = length(f_air);%Frequency numbers 71 | FREQ = f_air;%Frequency 72 | K = exp(-1i*2*pi*Dt.*FREQ);%Kernerl 73 | REF = zeros(1,N_freq); 74 | TRN = zeros(1,N_freq); 75 | SRC = zeros(1,N_freq); 76 | 77 | %% Initialize Detection parameters 78 | EyR = zeros(1,N_freq); 79 | EyT = zeros(1,N_freq); 80 | 81 | 82 | %% FDTD main loop 83 | figure;%for E/H field 84 | 85 | % COMPUTE UPDATE COEFFICIENTS 86 | mEy = (c_0*Dt) ./ ER; 87 | mHx = (c_0*Dt) ./ UR; 88 | % Initialize boundary 89 | H2=0; H1=0; 90 | E2=0; E1=0; 91 | 92 | % FDTD main loop 93 | 94 | for T = 1 : STEPS 95 | 96 | % Record H-Field at Boundary 97 | H2=H1; H1=Hx(1); 98 | 99 | % Update H from E 100 | for nz = 1 : (N_total-1) 101 | Hx(nz) = Hx(nz) + mHx(nz)*(Ey(nz+1) - Ey(nz))/Dz; 102 | end 103 | Hx(N_total) = Hx(N_total) + mHx(N_total)*(E2 - Ey(N_total))/Dz; 104 | 105 | % Handle H Source 106 | Hx(nzsrc-1)=Hx(nzsrc-1)-mHx(nzsrc-1)/Dz*Esrc(T); 107 | 108 | % Record E-Field Boundary 109 | E2=E1; E1=Ey(N_total); 110 | 111 | % Update E from H 112 | Ey(1) = Ey(1) + mEy(1)*(Hx(1) - H2)/Dz; 113 | for nz = 2 : N_total 114 | Ey(nz) = Ey(nz) + mEy(nz)*(Hx(nz) - Hx(nz-1))/Dz; 115 | end 116 | 117 | % Handle E Source 118 | Ey(nzsrc) = Ey(nzsrc)-mEy(nzsrc)/Dz*Hsrc(T); 119 | 120 | % Inject E Source 121 | % Ey(nzsrc) = Ey(nzsrc) + Esrc(T); 122 | % Ey(nzsrc) = Esrc(T); 123 | % Hx(nzsrc-1) = Hx(nzsrc-1) + Hsrc(T); 124 | 125 | 126 | subplot(2,1,1); 127 | % Visualize 128 | plot([1:N_total]*Dz,Ey,'b');hold on; 129 | plot([1:N_total]*Dz,Hx,'r');hold off 130 | ylim([-2 2]); 131 | % xlim([0 0.4035]); 132 | title(['FIELD STEP ',num2str(T),' of ',num2str(STEPS)]); 133 | 134 | 135 | % Update Fourier Transforms 136 | for nf = 1 : N_freq 137 | % Integrate REF(nf), TRN(nf), and SRC(nf) 138 | EyR(nf) = EyR(nf) + (K(nf)^T)*Ey(51); 139 | EyT(nf) = EyT(nf) + (K(nf)^T)*Ey(291); 140 | SRC(nf) = SRC(nf) + (K(nf)^T)*Esrc(T); 141 | end 142 | % FINISH FOURIER TRANSFORMS 143 | % EyR = EyR*Dt; 144 | % EyT = EyT*Dt; 145 | % SRC = SRC*Dt; 146 | 147 | % COMPUTE REFLECTANCE 148 | % AND TRANSMITTANCE 149 | REF = abs(EyR./SRC).^2; 150 | TRN = abs(EyT./SRC).^2*3.5; 151 | CON = REF + TRN; 152 | 153 | 154 | subplot(2,1,2); 155 | plot(REF,'r'); 156 | hold on; 157 | plot(TRN,'b'); 158 | plot(CON,'k--') 159 | hold off 160 | ylim([0 2]); 161 | 162 | if mod(T,T/500)<0.01 163 | pause(0.00010); 164 | end 165 | 166 | end 167 | 168 | 169 | %% Post-Process the Data 170 | 171 | 172 | 173 | 174 | 175 | 176 | --------------------------------------------------------------------------------