-
Notifications
You must be signed in to change notification settings - Fork 2
/
WC2D_TM_Ez_PWE_f.m
184 lines (134 loc) · 5.69 KB
/
WC2D_TM_Ez_PWE_f.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
function[Ez,f0_z]=WC2D_TM_Ez_PWE_f(x,y,eps0,nmodes,f0_guess,f0_min,f0_max,Nx,Ny,NGx,NGy);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Constants %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
c=2.99792458e8;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%% Interpolation on a grid that have 2^N points %%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
NGx = 2*floor(NGx/2); %% round to lower even number
NGy = 2*floor(NGy/2); %% round to lower even number
[X,Y] = meshgrid(x,y);
xx=linspace(x(1),x(end),Nx);
yy=linspace(y(1),y(end),Ny);
[XX,YY] = meshgrid(xx,yy);
eps=interp2(X,Y,eps0,XX,YY);
Gamma=1./eps;
dxx=xx(2)-xx(1);
dyy=yy(2)-yy(1);
Ltotx=xx(end)-xx(1);
Ltoty=yy(end)-yy(1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%% Building Epsilon in Fourier space %%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Gammak=fftshift(fft2(Gamma))*dxx*dyy/Ltotx/Ltoty;
Gammak =Gammak(Ny/2-NGy+1:Ny/2+NGy+1 , Nx/2-NGx+1:Nx/2+NGx+1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%% Reciprocal lattice vectors %%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Gx = (-NGx/2:NGx/2)'*2*pi/Ltotx;
Gy = (-NGy/2:NGy/2)'*2*pi/Ltoty;
NGx=length(Gx);
NGy=length(Gy);
NG=NGx*NGy;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Building Hamiltonien %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%idx_x = repmat((1:NGx), [NGy 1 ]);
%idx_x = idx_x(:);
%
%idx_y = repmat((1:NGy)', [1 NGx]);
%idx_y = idx_y(:);
%
%%idx_X = (idx_x-idx_x') + NGx; %% work only in Octave
%%idx_Y = (idx_y-idx_y') + NGy; %% work only in Octave
%idx_X = (repmat(idx_x,[1 NG])-repmat(idx_x',[NG 1])) + NGx; %% work in Octave and Matlab
%idx_Y = (repmat(idx_y,[1 NG])-repmat(idx_y',[NG 1])) + NGy; %% work in Octave and Matlab
%
%idx = sub2ind(size(Gammak), idx_Y(:), idx_X(:));
%idx = reshape(idx, [NG NG]);
%
%GX = diag(Gx(idx_x));
%GY = diag(Gy(idx_y));
%
%D2 = GX.^2 + GY.^2 ;
%
%%Hz = D2*Gammak(idx) ;
%Hz = Gammak(idx)*D2 ;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
HHH=zeros(NGy,NGx,NGy,NGx);
for ix=1:NGx
for jx=1:NGx
for iy=1:NGy
for jy=1:NGy
HHH(iy,ix,jy,jx) = Gammak(iy-jy+NGy,ix-jx+NGx );
end
end
end
end
[GXX,GYY]=meshgrid(Gx,Gy);
GXX=GXX(:);
GYY=GYY(:);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% TE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%GkXX = ( GXX )*( GXX )';
%GkYY = ( GYY )*( GYY )';
%Gk=GkXX+GkYY;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% TM %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Gk1 = sqrt( ( GXX ) .^2 + ( GYY ) .^2 ) ;
Gk2 = sqrt( ( GXX )'.^2 + ( GYY )'.^2 );
Gk=Gk1*Gk2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
HH=reshape(Gk,[NGy,NGx,NGy,NGx]);
H=HH.*HHH;
Hz=reshape(H,NG,NG);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%% Solving Hamiltonien %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[psik,k0_z] = eig(Hz); %% eigen values are ordered
f0_z=sqrt(diag(k0_z)) * c /2/pi;
lambda_z= 2*pi ./ sqrt(diag(k0_z)) * 1e6;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%% Filtering and reshaping the Wavefunction %%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
idx1=real(f0_z)>f0_min;
idx2=real(f0_z)<f0_max;
idx3=imag(f0_z)==0;
%idx=logical( idx1.*idx2.*idx3);
idx=logical( idx1.*idx2);
f0_z=f0_z(idx);
psik=psik(:,idx);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% here is a small patch due to differences between Octave and Matlab
% Matlab order the eigen values while Octave reverse it
if real(f0_z(end))<real(f0_z(1))
psik=psik(:,end:-1:1);
f0_z=f0_z(end:-1:1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%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2D00));
end