-
Notifications
You must be signed in to change notification settings - Fork 784
Expand file tree
/
Copy pathtest_diffracted_planewave.py
More file actions
283 lines (242 loc) · 9.79 KB
/
Copy pathtest_diffracted_planewave.py
File metadata and controls
283 lines (242 loc) · 9.79 KB
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
import cmath
import math
import unittest
import numpy as np
import meep as mp
class TestDiffractedPlanewave(unittest.TestCase):
@classmethod
def setUp(cls):
cls.resolution = 50 # pixels/um
cls.dpml = 1.0 # PML thickness
cls.dsub = 3.0 # substrate thickness
cls.dpad = 3.0 # length of padding between grating and PML
cls.wvl = 0.5 # center wavelength
cls.fcen = 1 / cls.wvl # center frequency
cls.ng = 1.5
cls.glass = mp.Medium(index=cls.ng)
cls.pml_layers = [mp.PML(thickness=cls.dpml, direction=mp.X)]
def run_binary_grating_diffraction(self, gp, gh, gdc, theta_pw):
"""Computes the mode coefficient of the transmitted orders of
a binary grating given an incident planewave and verifies
that the results are the same when using either a band number
or `DiffractedPlanewave` object in `get_eigenmode_coefficients`.
Args:
gp: grating periodicity (μm).
gh: grating height (μm).
gdc: grating duty cycle (dimensionless).
theta: angle (in degrees) of incident planewave rotated counter
clockwise (CCW) about z axis. 0 degrees along +x axis.
"""
sx = self.dpml + self.dsub + gh + self.dpad + self.dpml
sy = gp
cell_size = mp.Vector3(sx, sy, 0)
eig_parity = mp.ODD_Z # S polarization
if theta_pw == 0:
k = mp.Vector3()
eig_parity += mp.EVEN_Y
symmetries = [mp.Mirror(direction=mp.Y)]
else:
# k (in source medium) with correct length (plane of incidence: xy)
k = mp.Vector3(self.fcen * self.ng).rotate(
mp.Vector3(z=1), math.radians(theta_pw)
)
symmetries = []
def pw_amp(k, x0):
def _pw_amp(x):
return cmath.exp(1j * 2 * math.pi * k.dot(x + x0))
return _pw_amp
src_pt = mp.Vector3(-0.5 * sx + self.dpml, 0, 0)
sources = [
mp.Source(
mp.GaussianSource(self.fcen, fwidth=0.1 * self.fcen),
component=mp.Ez,
center=src_pt,
size=mp.Vector3(0, sy, 0),
amp_func=pw_amp(k, src_pt),
)
]
geometry = [
mp.Block(
material=self.glass,
size=mp.Vector3(self.dpml + self.dsub, mp.inf, mp.inf),
center=mp.Vector3(-0.5 * sx + 0.5 * (self.dpml + self.dsub), 0, 0),
),
mp.Block(
material=self.glass,
size=mp.Vector3(gh, gdc * gp, mp.inf),
center=mp.Vector3(-0.5 * sx + self.dpml + self.dsub + 0.5 * gh, 0, 0),
),
]
sim = mp.Simulation(
resolution=self.resolution,
cell_size=cell_size,
boundary_layers=self.pml_layers,
geometry=geometry,
k_point=k,
sources=sources,
symmetries=symmetries,
)
tran_pt = mp.Vector3(0.5 * sx - self.dpml, 0, 0)
tran_flux = sim.add_mode_monitor(
self.fcen, 0, 1, mp.FluxRegion(center=tran_pt, size=mp.Vector3(0, sy, 0))
)
sim.run(
until_after_sources=mp.stop_when_fields_decayed(20, mp.Ez, src_pt, 1e-6)
)
m_plus = int(np.floor((self.fcen - k.y) * gp))
m_minus = int(np.ceil((-self.fcen - k.y) * gp))
if theta_pw == 0:
orders = range(m_plus + 1)
else:
ms = range(m_minus, m_plus + 1)
kx = lambda m: np.power(self.fcen, 2) - np.power(k.y + m / gp, 2)
kxs = [kx(m) for m in ms]
# Ordering of the modes computed by MPB is according to *decreasing*
# values of kx (i.e., closest to propagation direction of 0° or +x)
ids = np.flip(np.argsort(kxs))
orders = [ms[d] for d in ids]
for band, order in enumerate(orders):
res = sim.get_eigenmode_coefficients(
tran_flux, [band + 1], eig_parity=eig_parity
)
tran_ref = abs(res.alpha[0, 0, 0]) ** 2
# For a planewave at normal incidence, the ±m diffracted orders
# contain identical power which means that to obtain the power in
# each order requires halving the total power. This applies
# to all orders except m=0.
if theta_pw == 0 and band != 0:
tran_ref = 0.5 * tran_ref
vg_ref = res.vgrp[0]
kdom_ref = res.kdom[0]
res = sim.get_eigenmode_coefficients(
tran_flux,
mp.DiffractedPlanewave([0, order, 0], mp.Vector3(0, 1, 0), 1, 0),
)
tran_dp = abs(res.alpha[0, 0, 0]) ** 2
vg_dp = res.vgrp[0]
kdom_dp = res.kdom[0]
err = abs(tran_ref - tran_dp) / tran_ref
print(
"tran:, {:2d} (band), {:2d} (order), "
"{:10.8f} (band num.), {:10.8f} (diff. pw.), "
"{:10.8f} (error)".format(band, order, tran_ref, tran_dp, err)
)
self.assertAlmostEqual(vg_ref, vg_dp, places=4)
self.assertAlmostEqual(tran_ref, tran_dp, places=4)
if theta_pw == 0:
self.assertAlmostEqual(abs(kdom_ref.x), kdom_dp.x, places=5)
self.assertAlmostEqual(abs(kdom_ref.y), kdom_dp.y, places=5)
self.assertAlmostEqual(abs(kdom_ref.z), kdom_dp.z, places=5)
else:
self.assertAlmostEqual(kdom_ref.x, kdom_dp.x, places=5)
self.assertAlmostEqual(kdom_ref.y, kdom_dp.y, places=5)
self.assertAlmostEqual(kdom_ref.z, kdom_dp.z, places=5)
print("PASSED.")
def test_diffracted_planewave(self):
self.run_binary_grating_diffraction(2.6, 0.4, 0.6, 0)
self.run_binary_grating_diffraction(2.6, 0.4, 0.6, 13.4)
# self.run_binary_grating_diffraction(10.0,0.5,0.5,0)
# self.run_binary_grating_diffraction(10.0,0.5,0.5,10.7)
def run_mode_source(self, gp, gh, gdc, m, use_diffpw):
"""Computes the flux of a transmitted order of a binary grating
given an incident planewave. The incident planewave is defined
using a mode source specified using a band number and parity or
`DiffractedPlanewave` object.
Args:
gp: grating periodicity (μm).
gh: grating height (μm).
gdc: grating duty cycle (dimensionless).
m: diffraction order in y direction (integer).
use_diffpw: use a `DiffractedPlanewave` (True) or band number
and parity (False).
"""
sx = self.dpml + self.dsub + gh + self.dpad + self.dpml
sy = gp
cell_size = mp.Vector3(sx, sy, 0)
eig_parity = mp.ODD_Z # S polarization
if m == 0:
k = mp.Vector3()
eig_parity += mp.EVEN_Y
symmetries = [mp.Mirror(direction=mp.Y)]
else:
ky = m / gp
theta_pw = math.asin(ky / (self.fcen * self.ng))
# k (in source medium) with correct length (plane of incidence: xy)
k = mp.Vector3(self.fcen * self.ng).rotate(mp.Vector3(z=1), theta_pw)
symmetries = []
if use_diffpw:
# The *zeroth* diffraction order specifies a planewave with a
# wavevector equal to the `k_point` of the `Simulation` object.
eigsrc_args = {
"eig_band": mp.DiffractedPlanewave(
[0, 0, 0],
mp.Vector3(0, 1, 0),
1,
0,
)
}
else:
eigsrc_args = {
"eig_band": 1,
"eig_parity": eig_parity,
"direction": mp.NO_DIRECTION,
"eig_kpoint": k,
}
src_pt = mp.Vector3(-0.5 * sx + self.dpml, 0, 0)
sources = [
mp.EigenModeSource(
mp.GaussianSource(self.fcen, fwidth=0.1 * self.fcen),
center=src_pt,
size=mp.Vector3(0, sy, 0),
**eigsrc_args,
)
]
geometry = [
mp.Block(
material=self.glass,
size=mp.Vector3(self.dpml + self.dsub, mp.inf, mp.inf),
center=mp.Vector3(-0.5 * sx + 0.5 * (self.dpml + self.dsub), 0, 0),
),
mp.Block(
material=self.glass,
size=mp.Vector3(gh, gdc * gp, mp.inf),
center=mp.Vector3(-0.5 * sx + self.dpml + self.dsub + 0.5 * gh, 0, 0),
),
]
sim = mp.Simulation(
resolution=self.resolution,
cell_size=cell_size,
boundary_layers=self.pml_layers,
k_point=k,
geometry=geometry,
sources=sources,
symmetries=symmetries,
)
tran_pt = mp.Vector3(0.5 * sx - self.dpml, 0, 0)
tran_flux = sim.add_flux(
self.fcen, 0, 1, mp.FluxRegion(center=tran_pt, size=mp.Vector3(0, sy, 0))
)
sim.run(
until_after_sources=mp.stop_when_fields_decayed(20, mp.Ez, src_pt, 1e-6)
)
tran = mp.get_fluxes(tran_flux)[0]
# force garbage collection
sim.reset_meep()
return tran
def test_mode_source(self):
gp = 1.5
gh = 0.5
gdc = 0.3
m = 2
tran_bn = self.run_mode_source(gp, gh, gdc, m, False)
tran_dp = self.run_mode_source(gp, gh, gdc, m, True)
print(
f"mode-source:, "
f"{tran_bn:.5f} (band number), "
f"{tran_dp:.5f} (diffraction order)"
)
self.assertAlmostEqual(
tran_bn, tran_dp, places=3 if mp.is_single_precision() else 4
)
if __name__ == "__main__":
unittest.main()