forked from kitchenknif/PyTMM
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tmmnlay.pyx
489 lines (397 loc) · 15.2 KB
/
tmmnlay.pyx
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# -*- coding: UTF-8 -*-
#
# Copyright (C) 2018 Ovidio Peña Rodríguez <[email protected]>
#
# This file is part of tmmnlay.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import numpy as np
(TE, TM) = range(2)
class MultiLayer(object):
"""
MultiLayer TMM
Problem geometry:
| 1 | | | | | | | | | | | | T |
| | = | I_01 | | L1 | | I_12 | ... | Ln | | I_n(n+1) | = | |
| R | | | | | | | | | | | | 0 |
In addition
"""
def __init__(self, n, d, wvl, aoi=0.0):
"""
Initialize the MultiLayer class.
Description:
This class contains the functions needed to solve the wave
propagation in a multilayer structure.
Inputs:
n -- A 1D or 2D array of possibly complex values for the index of
refraction of the layers. The first dimension is for the
wavelengths and the second for the layers. If the array is
1D then assume that it is constantand repeat the values
for all wavelengths
d -- A 1D array of thickness for all layers. d[0] and d[-1]
(corresponding to the outer semi-infinite layers) will be
set to zero, regardless of whatever value they have before you
pass the array to this class. If you don't want this done to
your array, make a copy first. (units must match that of the
wavelength)
wvl -- A 1D array of wavelength (units must match that of the
thickness)
aoi -- Angle of incidence (in degrees)
"""
self.d = d
self.wvl = wvl
self.n = n
self.aoi = aoi
# Make sure that the problem is well defined
assert self.num_layers >= 2, "We need at least two layers (%i found)" % (self.num_layers)
assert self.num_lambda_n == self.num_lambda,\
"The number of refractive index values (%i) does not match the number of wavelengths (%i)"\
% (self.num_lambda_n, self.num_lambda)
assert self.num_layers_n == self.num_layers,\
"The number of refractive index values (%i) does not match the number of layers (%i)"\
% (self.num_layers_n, self.num_layers)
def reset(self):
"""
Reset the calculations
"""
self._im = self._lm = None
self._matrix_TE = self._matrix_TM = None
self._coeffs_TE = self._coeffs_TM = None
def get_array(self, value):
"""
Force the input value to be a NumPy array
"""
if type(value) is np.ndarray:
return value
elif type(value) in (int, float, complex):
return np.array([value])
else:
return np.array(value)
@property
def num_layers(self):
"""
Returns the number of layers
"""
return self._d.shape[0]
@property
def num_lambda(self):
"""
Returns the number of wavelength values
"""
return self._wvl.shape[0]
@property
def num_layers_n(self):
"""
Returns the number of layers
"""
return self._n.shape[1]
@property
def num_lambda_n(self):
"""
Returns the number of wavelength values
"""
return self._n.shape[0]
@property
def n(self):
"""
Returns the refractive index values
"""
return self._n
@n.setter
def n(self, value):
"""
Updates the refractive index values
"""
self._n = self.get_array(value)
# If n is an 1D array we repeat the values for all wavelengths
if len(self._n.shape) == 1:
ln = self._n.shape[0]
lw = self._wvl.shape[0]
self._n = np.tile(self._n, lw).reshape((lw, ln))
self.reset()
@property
def d(self):
"""
Returns the thickness values
"""
return self._d
@d.setter
def d(self, value):
"""
Updates the thickness values
"""
self._d = self.get_array(value)
# Enforce this requirement for the outer layers
self._d[0] = self._d[-1] = 0.0
self.reset()
@property
def wvl(self):
"""
Returns the wavelength values
"""
return self._wvl
@wvl.setter
def wvl(self, value):
"""
Updates wavelength values
"""
self._wvl = self.get_array(value)
self.reset()
@property
def aoi(self):
"""
Returns the angle of incidence (degrees)
"""
return self._aoi
@aoi.setter
def aoi(self, value):
"""
Updates the angle of incidence (degrees)
"""
self._aoi = value
self.reset()
@property
def interface_matrices(self):
"""
Calculates (only if necessary) and returns the interface matrices.
To save memory it only saves the rjk and tjk coefficients corresponding
to the TE (s) and TM (p) polarizations.
"""
# Make sure that the problem is well defined
assert self.num_layers >= 2, "We need at least two layers (%i found)" % (self.num_layers)
assert self.num_lambda_n == self.num_lambda,\
"The number of refractive index values (%i) does not match the number of wavelengths (%i)"\
% (self.num_lambda_n, self.num_lambda)
assert self.num_layers_n == self.num_layers,\
"The number of refractive index values (%i) does not match the number of layers (%i)"\
% (self.num_layers_n, self.num_layers)
if self._im is None:
n2 = self.n**2.0
nj = self.n[:, :-1]
nk = self.n[:, 1:]
s2 = (n2[:, 0]*np.sin(self.aoi*np.pi/180.0)**2.0)[:, None]/n2
c = np.sqrt(1.0 - s2)
cj = c[:, :-1]
ck = c[:, 1:]
num1 = nj*cj + nk*ck
num2 = nj*ck + nk*cj
self._im = np.zeros((self.num_lambda, self.num_layers - 1, 4), dtype=complex)
# rjk for the TE (s) polarization
self._im[:, :, 0] = (nj*cj - nk*ck)/num1
# tjk for the TE (s) polarization
self._im[:, :, 1] = 2.0*nj*cj/num1
# rjk for the TM (p) polarization
self._im[:, :, 2] = (nj*ck - nk*cj)/num2
# tjk for the TM (p) polarization
self._im[:, :, 3] = 2.0*nj*ck/num2
return self._im
@property
def layer_matrices(self):
"""
Calculates (only if necessary) and returns the layer matrices.
To save memory it only saves the values of exp(-i*beta_j) and
exp(i*beta_j).
"""
# Make sure that the problem is well defined
assert self.num_layers >= 2, "We need at least two layers (%i found)" % (self.num_layers)
assert self.num_lambda_n == self.num_lambda,\
"The number of refractive index values (%i) does not match the number of wavelengths (%i)"\
% (self.num_lambda_n, self.num_lambda)
assert self.num_layers_n == self.num_layers,\
"The number of refractive index values (%i) does not match the number of layers (%i)"\
% (self.num_layers_n, self.num_layers)
if (self._lm is None) and (self.num_layers > 2):
s2 = np.sin(self.aoi*np.pi/180.0)**2.0
n2 = self.n**2.0
kj = np.sqrt(n2[:, 1:-1] - (n2[:, 0]*s2)[:, None])*self.d[1:-1]
Bj = 2.0j*np.pi*kj/self.wvl[:, None]
self._lm = np.zeros((self.num_lambda, self.num_layers - 2, 2), dtype=complex)
# exp(-i*beta_j)
self._lm[:, :, 0] = np.exp(-Bj)
# exp(i*beta_j)
self._lm[:, :, 1] = np.exp(Bj)
return self._lm
def matrix(self, pol=TE):
"""
Calculate total matrix for the multilayer.
"""
im = self.interface_matrices
S = np.zeros((self.num_lambda, 2, 2), dtype=complex)
S[:, 0, 0] = S[:, 1, 1] = 1.0/im[:, 0, 2*pol + 1]
S[:, 0, 1] = S[:, 1, 0] = im[:, 0, 2*pol]/im[:, 0, 2*pol + 1]
if self.num_layers > 2:
lm = self.layer_matrices
B11 = lm[:, :, 0]/im[:, 1:, 2*pol + 1]
B12 = lm[:, :, 0]*im[:, 1:, 2*pol]/im[:, 1:, 2*pol + 1]
B21 = lm[:, :, 1]*im[:, 1:, 2*pol]/im[:, 1:, 2*pol + 1]
B22 = lm[:, :, 1]/im[:, 1:, 2*pol + 1]
for j in range(self.num_layers - 2):
C11 = S[:, 0, 0]*B11[:, j] + S[:, 0, 1]*B21[:, j]
C12 = S[:, 0, 0]*B12[:, j] + S[:, 0, 1]*B22[:, j]
C21 = S[:, 1, 0]*B11[:, j] + S[:, 1, 1]*B21[:, j]
C22 = S[:, 1, 0]*B12[:, j] + S[:, 1, 1]*B22[:, j]
S[:, 0, 0] = C11
S[:, 0, 1] = C12
S[:, 1, 0] = C21
S[:, 1, 1] = C22
return S
@property
def matrix_TE(self):
"""
Calculate total TE matrix for the multilayer.
"""
if self._matrix_TE is None:
self._matrix_TE = self.matrix(pol=TE)
return self._matrix_TE
@property
def matrix_TM(self):
"""
Calculate total TM matrix for the multilayer.
"""
if self._matrix_TM is None:
self._matrix_TM = self.matrix(pol=TM)
return self._matrix_TM
def rt(self, pol=TE):
"""
Calculate total Fresnel reflection and transmission coefficients for the multilayer
"""
S = self.matrix(pol=pol)
r = S[:, 1, 0]/S[:, 0, 0]
t = 1.0/S[:, 0, 0]
return r, t
@property
def rt_TE(self):
"""
Calculate TE Fresnel reflection and transmission coefficients for the multilayer
"""
r, t = self.rt(pol=TE)
#Apply correction for total internal reflection
r[np.isnan(r)] = 1.0
t[np.isnan(t)] = 2.0
return r, t
@property
def rt_TM(self):
"""
Calculate TM Fresnel reflection and transmission coefficients for the multilayer
"""
r, t = self.rt(pol=TM)
#Apply correction for total internal reflection
r[np.isnan(r)] = -1.0
t[np.isnan(t)] = 0.0
return r, t
def coeffs_matrix(self, layer, pol=TE):
im = self.interface_matrices
lm = self.layer_matrices
Sl = np.zeros((self.num_lambda, 2, 2), dtype=complex)
Sr = np.zeros((self.num_lambda, 2, 2), dtype=complex)
Sl[:, 0, 0] = Sl[:, 1, 1] = 1.0/im[:, 0, 2*pol + 1]
Sl[:, 0, 1] = Sl[:, 1, 0] = im[:, 0, 2*pol]/im[:, 0, 2*pol + 1]
Sr[:, 0, 0] = Sr[:, 1, 1] = 1.0/im[:, layer + 1, 2*pol + 1]
Sr[:, 0, 1] = Sr[:, 1, 0] = im[:, layer + 1, 2*pol]/im[:, layer + 1, 2*pol + 1]
B11 = lm[:, :, 0]/im[:, 1:, 2*pol + 1]
B12 = lm[:, :, 0]*im[:, 1:, 2*pol]/im[:, 1:, 2*pol + 1]
B21 = lm[:, :, 1]*im[:, 1:, 2*pol]/im[:, 1:, 2*pol + 1]
B22 = lm[:, :, 1]/im[:, 1:, 2*pol + 1]
for j in range(layer):
C11 = Sl[:, 0, 0]*B11[:, j] + Sl[:, 0, 1]*B21[:, j]
C12 = Sl[:, 0, 0]*B12[:, j] + Sl[:, 0, 1]*B22[:, j]
C21 = Sl[:, 1, 0]*B11[:, j] + Sl[:, 1, 1]*B21[:, j]
C22 = Sl[:, 1, 0]*B12[:, j] + Sl[:, 1, 1]*B22[:, j]
Sl[:, 0, 0] = C11
Sl[:, 0, 1] = C12
Sl[:, 1, 0] = C21
Sl[:, 1, 1] = C22
for j in range(layer + 1, self.num_layers - 2):
C11 = Sr[:, 0, 0]*B11[:, j] + Sr[:, 0, 1]*B21[:, j]
C12 = Sr[:, 0, 0]*B12[:, j] + Sr[:, 0, 1]*B22[:, j]
C21 = Sr[:, 1, 0]*B11[:, j] + Sr[:, 1, 1]*B21[:, j]
C22 = Sr[:, 1, 0]*B12[:, j] + Sr[:, 1, 1]*B22[:, j]
Sr[:, 0, 0] = C11
Sr[:, 0, 1] = C12
Sr[:, 1, 0] = C21
Sr[:, 1, 1] = C22
return Sl, Sr
def coefficients(self, pol=TE):
"""
Calculate the field coefficients
"""
s2 = np.sin(self.aoi*np.pi/180.0)**2.0
n2 = self.n**2.0
qj = np.sqrt(n2[:, 1:-1] - (n2[:, 0]*s2)[:, None])
Bj = 4.0j*np.pi*qj*self.d[1:-1]/self.wvl[:, None]
coeffs = np.zeros((self.num_lambda, self.num_layers, 2), dtype=complex)
for j in range(self.num_layers - 2):
Sp, Spp = self.coeffs_matrix(layer=j, pol=pol)
coeffs[:, j + 1, 0] = 1.0/(Sp[:, 0, 0] + Sp[:, 0, 1]*Spp[:, 1, 0]*np.exp(Bj[:, j])/Spp[:, 0, 0])
coeffs[:, j + 1, 1] = coeffs[:, j + 1, 0]*Spp[:, 1, 0]*np.exp(Bj[:, j])/Spp[:, 0, 0]
#Define coefficients in the outer layers
r, t = self.rt(pol=pol)
coeffs[:, 0, 0] = 1.0 + 0.0j
coeffs[:, 0, 1] = r
coeffs[:, -1, 0] = t
coeffs[:, -1, 1] = 0.0 + 0.0j
return coeffs
@property
def coeffs_TE(self):
"""
Calculate the field coefficients for the TE polarization
"""
if self._coeffs_TE is None:
self._coeffs_TE = self.coefficients(pol=TE)
return self._coeffs_TE
@property
def coeffs_TM(self):
"""
Calculate the field coefficients for the TM polarization
"""
if self._coeffs_TM is None:
self._coeffs_TM = self.coefficients(pol=TM)
return self._coeffs_TM
def field(self, x, coeffs):
"""
Returns the electric field at specified values of x.
Inputs:
coeffs -- Array of field coefficients
x -- A 1D array of any length specifying the x values for which the field
should be returned. It must be sorted in increasing order.
Outputs: (E)
E -- an array of field values for the given x (complex-valued)
"""
xl = np.cumsum(self.d)
xl -= xl[0] # Just in case self.d[0] was not 0.0
xl[-1] = np.inf
s2 = np.sin(self.aoi*np.pi/180.0)**2.0
n2 = self.n**2.0
Zeta = 2.0j*np.pi*np.sqrt(n2 - (n2[:, 0]*s2)[:, None])/self.wvl[:, None]
E = np.zeros((self.num_lambda, len(x)), complex)
j = 0
for i, xi in enumerate(x):
while (xi > xl[j]):
j += 1
if (j == 0):
xj = xi
else:
xj = xi - xl[j - 1]
E[:, i] = coeffs[:, j, 0]*np.exp(Zeta[:, j]*xj) + coeffs[:, j, 1]*np.exp(-Zeta[:, j]*xj)
return E
def field_TE(self, x):
"""
Calculate the electric field coefficients for the TE polarization
"""
return self.field(x, self.coeffs_TE)
def field_TM(self, x):
"""
Calculate the electric field coefficients for the TE polarization
"""
return self.field(x, self.coeffs_TM)