-
Notifications
You must be signed in to change notification settings - Fork 8
/
mode.py
632 lines (527 loc) · 19.8 KB
/
mode.py
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
import numpy as np
from matplotlib import pyplot as plt
import pickle
import random
from copy import deepcopy
from typing import Callable
class EigenMode(object):
"""Virtual class representing an eigenmode"""
def __init__(self):
return NotImplementedError()
def _inner_product(self):
return NotImplementedError()
def plot(self):
return NotImplementedError()
def get_confined_power(self):
return NotImplementedError()
def spurious_value(self):
return NotImplementedError()
def zero_phase(self):
return NotImplementedError()
def plot_material(self):
return NotImplementedError()
def save(self, path=("./ModeObject_" + str(random.random()) + ".pk")):
"""Serializes the mode into a pickle file"""
pickle.dump(self, open(path, "wb+"))
def get_fields(self):
"""Returns an array [self.Hx, self.Hy, self.Hz, self.Ex, self.Ey, self.Ez]."""
return [self.Hx, self.Hy, self.Hz, self.Ex, self.Ey, self.Ez]
def get_H(self):
"""Returns an array [self.Hx, self.Hy, self.Hz]."""
return [self.Hx, self.Hy, self.Hz]
def get_E(self):
"""Returns an array [self.Ex, self.Ey, self.Ez]."""
return [self.Ex, self.Ey, self.Ez]
def get_neff(self):
"""Returns the effective index as a complex number."""
return self.neff
def get_Hx(self):
return self.Hx
def get_Hy(self):
return self.Hy
def get_Hz(self):
return self.Hz
def get_Ex(self):
return self.Ex
def get_Ey(self):
return self.Ey
def get_Ez(self):
return self.Ez
def get_wavelength(self):
"""Returns the wavelength."""
return self.wl
def __str__(self):
return "Mode Object with effective index of " + str(self.neff)
def change_fields(self, start, other, func):
for comp in ["Hx", "Hy", "Hz", "Ex", "Ey", "Ez"]:
cur = getattr(start, comp)
new = getattr(other, comp) if isinstance(other, EigenMode) else other
setattr(start, comp, func(cur, new))
return start
def __mul__(self, other):
return self.change_fields(deepcopy(self), other, lambda a, b: a * b)
def __add__(self, other):
return self.change_fields(deepcopy(self), other, lambda a, b: a + b)
def __truediv__(self, other):
return self.change_fields(deepcopy(self), other, lambda a, b: a / b)
def __sub__(self, other):
return self.change_fields(deepcopy(self), other, lambda a, b: a - b)
def __imul__(self, other):
return self.change_fields(self, other, lambda a, b: a * b)
def __iadd__(self, other):
return self.change_fields(self, other, lambda a, b: a + b)
def __itruediv__(self, other):
return self.change_fields(self, other, lambda a, b: a / b)
def __isub__(self, other):
return self.change_fields(self, other, lambda a, b: a - b)
def inner_product(self, mode2) -> float:
"""Takes the inner product between self and the provided Mode
Parameters
----------
mode2 : EigenMode
second eigenmode in the operation
Returns
-------
number
the inner product between the modes
"""
# return self.overlap(mode2)
return self._inner_product(self, mode2)
def check_spurious(
self, spurious_threshold: float = 0.9
) -> bool:
"""Takes in a mode and determine whether the mode is likely spurious based on the ratio of confined to not confined power
Parameters
----------
spurious_threshold: float
if the calculated spurious value is higher than this threshold, the mode is considered spurious
Returns
-------
boolean
True if likely spurious
"""
return self.spurious_value() > spurious_threshold
def normalize(self) -> None:
"""Normalizes the Mode to power 1."""
self.zero_phase()
factor = self.inner_product(self)
self /= np.sqrt(factor)
class Mode1D(EigenMode):
"""Object that holds the field profiles and effective index for a 1D eigenmode"""
def __init__(
self,
x: "np.ndarray" = None,
wl: float = None,
neff: float = None,
Hx: "np.ndarray" = None,
Hy: "np.ndarray" = None,
Hz: "np.ndarray" = None,
Ex: "np.ndarray" = None,
Ey: "np.ndarray" = None,
Ez: "np.ndarray" = None,
n: "np.ndarray" = None,
) -> None:
"""Constructor for Mode1D Object (one dimensional eigenmode)
Parameters
----------
x : (ndarray float)
array of grid points in x direction (propogation in z)
wl : (float)
wavelength (meters)
neff : (float)
effective index
Hx : (ndarray float)
Hx field profile
Hy : (ndarray float)
Hy field profile
Hz : (ndarray float)
Hz field profile
Ex : (ndarray float)
Ex field profile
Ey : (ndarray float)
Ey field profile
Ez : (ndarray float)
Ez field profile
n : (ndarray float)
refractive index profile
"""
self.x = x
self.wl = wl
self.neff = neff
self.Hx = Hx if Hx is not None else np.zeros(10)
self.Hy = Hy if Hy is not None else self.Hx * 0
self.Hz = Hz if Hz is not None else self.Hx * 0
self.Ex = Ex if Ex is not None else self.Hx * 0
self.Ey = Ey if Ey is not None else self.Hx * 0
self.Ez = Ez if Ez is not None else self.Hx * 0
self.n = n
self.H = np.sqrt(
np.abs(self.Hx) ** 2 + np.abs(self.Hy) ** 2 + np.abs(self.Hz) ** 2
)
self.E = np.sqrt(
np.abs(self.Ex) ** 2 + np.abs(self.Ey) ** 2 + np.abs(self.Ez) ** 2
)
def plot(self, operation: str = "Real", normalize: bool = True) -> None:
"""Plots the fields in the mode using pyplot. Should call plt.figure() before and plt.show() or plt.savefig() after
Parameters
----------
operation : string or function
the operation to perform on the fields from ("Real", "Imaginary", "Abs", "Abs^2") (default:"Real") or a function such as np.abs
normalize : bool
if true, will normalize biggest field to 1
"""
temp = (
self
/ max(
[
np.abs(np.real(np.amax(i)))
for i in [self.Ex, self.Ey, self.Ez, self.Hx, self.Hy, self.Hz]
]
)
if normalize
else self / 1
)
# Parse operation
op_name = (
operation.__name__ if hasattr(operation, "__name__") else str(operation)
)
if operation == "Imaginary":
operation = lambda a: np.imag(a)
elif operation == "Abs":
operation = lambda a: np.abs(a)
elif operation == "Abs^2":
operation = lambda a: np.abs(a) ** 2
elif operation == "Real":
operation = lambda a: np.real(a)
try:
t = self.change_fields(
deepcopy(temp), deepcopy(temp), lambda a, b: operation(b)
)
Hx, Hy, Hz, Ex, Ey, Ez = [t.Hx, t.Hy, t.Hz, t.Ex, t.Ey, t.Ez]
except Exception as e:
print(e)
raise Exception(
"Invalid operation provided. Please choose from ('Imaginary', 'Abs', 'Abs^2', 'Real') or provide a function"
)
# Plot fields
fields = ["Hx", "Hy", "Hz", "Ex", "Ey", "Ez"]
for i, field in enumerate([Hx, Hy, Hz, Ex, Ey, Ez]):
plt.subplot(2, 3, i + 1)
plt.plot(self.x, field)
plt.xlabel("x µm")
plt.ylabel("{}({})".format(op_name, fields[i]))
plt.tight_layout()
def _inner_product(
self, mode1: EigenMode, mode2: EigenMode, mask: "np.ndarray" = None
) -> float:
"""Helper function that takes the inner product between Modes mode1 and mode2
Parameters
----------
mode1 : Mode
first eigenmode in the operation
mode2 : Mode
second eigenmode in the operation
mask : np.ndarray
a mask to multiply on the field profiles before conducting the inner product
Returns
-------
number
the inner product between the two input modes
"""
mask = 1 if mask is None else mask
Ex = mode1.Ex * mask
Hy = np.conj(mode2.Hy) * mask
Ey = mode1.Ey * mask
Hx = np.conj(mode2.Hx) * mask
cross = Ex * Hy - Ey * Hx
return np.trapz(cross, np.real(mode1.x))
def get_confined_power(self, num_pixels: int = None) -> float:
"""Takes in a mode and returns the percentage of power confined in the core
Parameters
----------
num_pixels : int
number of pixels outside of the core to expand the mask to capture power just outside the core
Returns
-------
float
Percentage of confined power
"""
# Increase core by 5% to capture slight leaks
if num_pixels is None:
num_pixels = int(len(self.x) * 0.05)
mask = np.where(self.n > np.mean(self.n), 1, 0)
kernel = np.ones(num_pixels + 1)
mask = np.convolve(mask, kernel, "same")
mask = np.where(mask > 0, 1, 0)
ratio = self._inner_product(self, self, mask=mask) / self._inner_product(
self, self, mask=None
)
return ratio
def zero_phase(self) -> None:
"""Changes the phase such that the z components are all imaginary and the xy components are all real."""
index = int(self.Hy.shape[0] / 2)
phase = np.angle(np.array(self.Hy))[index]
self *= np.exp(-1j * phase)
if (np.sum(np.real(self.Hy))) < 0:
self *= -1
def plot_material(
self, operation: Callable[["np.ndarray"], "np.ndarray"] = np.real
) -> None:
"""Plots the index of refraction profile"""
plt.plot(self.x, operation(self.n))
plt.title("Index of Refraction")
plt.xlabel("x (µm)")
plt.ylabel("y (µm)")
class Mode(EigenMode):
"""Object that holds the field profiles and effective index for a 2D eigenmode"""
def __init__(
self,
x: "np.ndarray" = None,
y: "np.ndarray" = None,
wl: float = None,
neff: float = None,
Hx: "np.ndarray" = None,
Hy: "np.ndarray" = None,
Hz: "np.ndarray" = None,
Ex: "np.ndarray" = None,
Ey: "np.ndarray" = None,
Ez: "np.ndarray" = None,
n: "np.ndarray" = None,
) -> None:
"""Constructor for Mode Object
Parameters
----------
x : (ndarray float)
array of grid points in x direction (propogation in z)
y : (ndarray float)
array of grid points in y direction (propogation in z)
wl : (float)
wavelength (meters)
neff : (float)
effective index
Hx : (ndarray float)
Hx field profile
Hy : (ndarray float)
Hy field profile
Hz : (ndarray float)
Hz field profile
Ex : (ndarray float)
Ex field profile
Ey : (ndarray float)
Ey field profile
Ez : (ndarray float)
Ez field profile
n : (ndarray float)
refractive index profile
"""
self.x = x
self.y = y
self.wl = wl
self.neff = neff
self.Hx = Hx if Hx is not None else np.zeros((10, 10))
self.Hy = Hy if Hy is not None else self.Hx * 0
self.Hz = Hz if Hz is not None else self.Hx * 0
self.Ex = Ex if Ex is not None else self.Hx * 0
self.Ey = Ey if Ey is not None else self.Hx * 0
self.Ez = Ez if Ez is not None else self.Hx * 0
self.n = n
self.H = np.sqrt(
np.abs(self.Hx) ** 2 + np.abs(self.Hy) ** 2 + np.abs(self.Hz) ** 2
)
self.E = np.sqrt(
np.abs(self.Ex) ** 2 + np.abs(self.Ey) ** 2 + np.abs(self.Ez) ** 2
)
def plot(
self, operation: str = "Real", colorbar: bool = True, normalize: bool = True
) -> None:
"""Plots the fields in the mode using pyplot. Should call plt.figure() before and plt.show() or plt.savefig() after
Parameters
----------
operation : string or function
the operation to perform on the fields from ("Real", "Imaginary", "Abs", "Abs^2") (default:"Real") or a function such as np.abs
colorbar : bool
if true, will show a colorbar for each field
normalize : bool
if true, will normalize biggest field to 1
"""
temp = (
self
/ max(
[
np.abs(np.real(np.amax(i)))
for i in [self.Ex, self.Ey, self.Ez, self.Hx, self.Hy, self.Hz]
]
)
if normalize
else self / 1
)
# Parse operation
op_name = (
operation.__name__ if hasattr(operation, "__name__") else str(operation)
)
if operation == "Imaginary":
operation = lambda a: np.imag(a)
elif operation == "Abs":
operation = lambda a: np.abs(a)
elif operation == "Abs^2":
operation = lambda a: np.abs(a) ** 2
elif operation == "Real":
operation = lambda a: np.real(a)
try:
t = self.change_fields(
deepcopy(temp), deepcopy(temp), lambda a, b: operation(b)
)
Hx, Hy, Hz, Ex, Ey, Ez = [t.Hx, t.Hy, t.Hz, t.Ex, t.Ey, t.Ez]
except Exception as e:
print(e)
raise Exception(
"Invalid operation provided. Please choose from ('Imaginary', 'Abs', 'Abs^2', 'Real') or provide a function"
)
# Plot fields
fields = ["Hx", "Hy", "Hz", "Ex", "Ey", "Ez"]
for i, field in enumerate([Hx, Hy, Hz, Ex, Ey, Ez]):
plt.subplot(
2, 3, i + 1, adjustable="box", aspect=field.shape[0] / field.shape[1]
)
v = max(abs(field.min()), abs(field.max()))
plt.imshow(
np.rot90(field),
cmap="RdBu",
vmin=-v,
vmax=v,
extent=[
temp.x[0],
temp.x[-1],
temp.y[0],
temp.y[-1],
],
interpolation="none",
)
plt.title("{}({})".format(op_name, fields[i]))
if colorbar:
plt.colorbar(fraction=0.046, pad=0.04)
plt.xlabel("x µm")
plt.ylabel("y µm")
plt.tight_layout()
def _inner_product(
self, mode1: EigenMode, mode2: EigenMode, mask: "np.ndarray" = None
) -> float:
"""Helper function that takes the inner product between Modes mode1 and mode2
Parameters
----------
mode1 : EigenMode
first eigenmode in the operation
mode2 : EigenMode
second eigenmode in the operation
mask : np.ndarray
a mask to multiply on the field profiles before conducting the inner product
Returns
-------
number
the inner product between the two input modes
"""
mask = 1 if mask is None else mask
Ex = mode1.Ex * mask
Hy = np.conj(mode2.Hy) * mask
Ey = mode1.Ey * mask
Hx = np.conj(mode2.Hx) * mask
cross = Ex * Hy - Ey * Hx
return np.trapz(np.trapz(cross, np.real(mode1.x)), np.real(mode1.y))
def get_confined_power(self, num_pixels: int = None) -> float:
"""Takes in a mode and returns the percentage of power confined in the core
Parameters
----------
num_pixels : int
number of pixels outside of the core to expand the mask to capture power just outside the core (mask dilation)
Returns
-------
float
Percentage of confined power
"""
# Increase core by 5% to capture slight leaks
if num_pixels is None:
num_pixels = int(len(self.x) * 0.05)
mask = np.where(self.n > np.mean(self.n), 1, 0)
kernel = np.ones(num_pixels + 1)
for row in range(mask.shape[0]): # This could be vectorized with scipy or just a 2D kernal, but numpy won't let this happen
mask[row] = np.convolve(mask[row], kernel, "same")
for col in range(mask.shape[1]):
mask[:, col] = np.convolve(mask[:, col], kernel, "same")
mask = np.where(mask > 0, 1, 0)
ratio = np.abs(self._inner_product(self, self, mask=mask)) / np.abs(self._inner_product(
self, self, mask=None
))
return ratio
def zero_phase(self) -> None:
"""Changes the phase such that the z components are all imaginary and the xy components are all real."""
index = int(self.Hy.shape[0] / 2)
phase = np.angle(np.array(self.Hy))[index][index]
self *= np.exp(-1j * phase)
if (np.sum(np.real(self.Hy))) < 0:
self *= -1
def plot_material(self) -> None:
"""Plots the index of refraction profile"""
plt.figure()
plt.imshow(
np.real(np.rot90(self.n)),
extent=[
self.x[0],
self.x[-1],
self.y[0],
self.y[-1],
],
cmap="Greys",
interpolation="none",
)
plt.colorbar()
plt.title("Index of Refraction")
plt.xlabel("x (µm)")
plt.ylabel("y (µm)")
def plot_power(self) -> None:
"""Plots the power profile"""
Pz = self.Ex * self.Hy - self.Ey * self.Hx
plt.figure()
plt.imshow(
np.real(np.rot90(Pz)),
extent=[
self.x[0],
self.x[-1],
self.y[0],
self.y[-1],
],
cmap="Blues",
interpolation="none",
)
plt.colorbar()
plt.title("Power")
plt.xlabel("x (µm)")
plt.ylabel("y (µm)")
def integrate(self, field):
return np.trapz(np.trapz(field, np.real(self.x)), np.real(self.y))
def TE_polarization_fraction(self):
"""Returns the fraction of power in the TE polarization"""
Ex = np.abs(self.Ex) ** 2
Ey = np.abs(self.Ey) ** 2
return self.integrate(Ex) / self.integrate(Ex+Ey)
def TM_polarization_fraction(self):
"""Returns the fraction of power in the TE polarization"""
Ex = np.abs(self.Ex) ** 2
Ey = np.abs(self.Ey) ** 2
return self.integrate(Ey) / self.integrate(Ex+Ey)
def effective_area(self):
"""Returns the effective area of the mode"""
E2 = np.abs(self.Ex) ** 2 + np.abs(self.Ey) ** 2 + np.abs(self.Ez) ** 2
return self.integrate(E2) ** 2 / self.integrate(E2 ** 2)
def effective_area_ratio(self):
"""Returns the ratio of the effective area to the cross-sectional area"""
return self.effective_area() / self.integrate(np.ones(self.Ex.shape))
def spurious_value(self):
"""Returns the spurious value of the mode"""
return 1 - (self.get_confined_power() * (1-self.effective_area_ratio()))
def overlap(self, m2:EigenMode):
"""Returns the overlap of the mode with another mode"""
E1H2 = self.integrate(self.Ex * m2.Hy.conj() - self.Ey * m2.Hx.conj())
E2H1 = self.integrate(m2.Ex * self.Hy.conj() - m2.Ey * self.Hx.conj())
E1H1 = self.integrate(self.Ex * self.Hy.conj() - self.Ey * self.Hx.conj())
E2H2 = self.integrate(m2.Ex * m2.Hy.conj() - m2.Ey * m2.Hx.conj())
return np.abs(np.real(E1H2 * E2H1 / E1H1) / np.real(E2H2))