# NCKU 波導光學(Guided wave optics) 作業2
contributed by <[`tintinjian12999`](https://github.com/tintinjian12999)>
完整的程式碼在[這裡](https://github.com/tintinjian12999/guided-wave-optics-NCKU/blob/master/HW2/HW2.py)
## 1. Consider a symmetric slab waveguide with `n1=1.516`, `ns=n0=1.0`, and `2a=8μm`, at a wavelength of `1.55μm`, develop your own mode solver to answer the following questions.
### (a) Develop your own mode solver using the finite difference scheme outlined in the lecture. Solve for the waveguide modes and their corresponding propagation constants using your mode solver.
基本上要解的方程式是

然後透過 finite difference 的轉換可以得到




基本上要解的就是這組特徵值和特徵向量,可以用 numpy 的套件輕易地完成。
解的上下限先設定在 0 ~ 4a,分 12000 個點,目前計算 substrate 長度為 a , core 為 2a , cladding 為 a。
```python
a = 4E-6
upper_bound =4 * a # Substrate to waveguide: a, waveguide: 2a, waveguide to cladding: 2a
n1 = 1.516
ns = 1
n0 = 1
N = 12000 # It's better to be multiplies of 4
points = np.linspace(0, upper_bound, N)
```
接著算

```python
step = upper_bound / N # Delta_x
n1 = 1.516
ns = 1
n0 = 1
k = 2 * np.pi / lambda1
# Define the refractive index in each layer
#O(n)
refractive_index = []
for i in range(int( N / 4 )):
refractive_index.append(ns)
for i in range(int(N /4 ) ,int( 3 * N/4 )):
refractive_index.append(n1)
for i in range(int(3 * N / 4) ,int(N)):
refractive_index.append(n0)
refractive_index = np.array(refractive_index)
mu_n = (k ** 2) * (refractive_index ** 2) * (step ** 2) - 2
```
建立一個陣列用來儲存不同區域的折射率並算出對應的 mu 值。
然後就可以開始建立我們的矩陣了!
```python
array = np.zeros((N,N))
#O(N)
for i in range(N):
if (i == 0):
array[0][0] = mu_n[0] / (step ** 2)
array[0][1] = 1 / (step ** 2)
elif (i == (N - 1)):
array[i][i] = mu_n[i] / (step ** 2)
array[i][i - 1] = 1 / (step ** 2)
else:
array[i][i - 1] = 1 / (step ** 2)
array[i][i] = mu_n[i] / (step ** 2)
array[i][i + 1] = 1 / (step ** 2)
```
先初始化為零再用一層 for 迴圈就行了。
接著透過 numpy 內建的 linalg.eig 這個方法,把陣列丟進去他就會自己算特徵值跟特徵向量了
```python
#O(N ^ 3) !!
eigenvalues, eigenvectors = np.linalg.eig(array)
filter = eigenvalues >= 0
eigenvalues = eigenvalues[filter]
eigenvectors = eigenvectors[filter]
effective_refractive_index = np.sqrt(eigenvalues)
filter = effective_refractive_index <= n1 * k
eigenvectors = eigenvectors[filter]
effective_refractive_index = effective_refractive_index[filter]
filter = effective_refractive_index >= ns * k
eigenvectors = eigenvectors[filter]
effective_refractive_index = effective_refractive_index[filter]
beta = [round(number , 3) for number in effective_refractive_index]
beta = np.sort(beta)
```
同時把滿足 cutoff condtion 的模態抓出來。

得到的結果為
```
[4292820.488, 4620144.247, 4910326.745, 5163146.893,
5381570.972, 5568511.264 , 5726394.981, 5857156.893,
5962305.022, 6042987.7, 6100047.369, 6134059.84]
```
基本上與 HW1 所算出來的結果相差無幾,主要的效能瓶頸在 python 解 eigenvalue 的方法時間複雜度在 O(n ^ 3),所以如果增加步數的話運算需要的時間就會大幅增加。
## (b) How many TE modes does the waveguide support?
滿足 cutoff condition 的有 12 個
## ( c ) Find the propagation constant β corresponding to each TE mode. Compare your results with those from [homework#1](https://hackmd.io/@tintinjian12999/guided-wave-optics-HW1).
HW1 算出來的 β (1/m)為
```
[4292835.986, 4620155.096, 4910334.806, 5163152.666,
5381574.964, 5568513.894, 5726396.617, 5857157.827,
5962305.482, 6042987.884, 6100047.414, 6134059.842]
```
對比 HW2 的 (1/m)
```
[4292820.488, 4620144.247, 4910326.745, 5163146.893,
5381570.972, 5568511.264 , 5726394.981, 5857156.893,
5962305.022, 6042987.7, 6100047.369, 6134059.84]
```
基本上是一致的。
## (d) How does the step size in discretization affect the mode solver results?
把點數從 12000 調成 120 (也就是把 step size 增加一百倍),得到的結果如下 (1/m)
```
[4350714.028 4662568.824 4939755.224 5182896.732 5394395.681 5576528.963
5731181.843 5859853.664 5963711.249 6043641.809 6100295.312 6134115.477]
```
可以看到和解析解(HW1)的誤差變大了,若進一步調成 12
```
[5968963.019 5995359.729 6033095.934 6074436.884 6111214.814 6136415.144]
```
可以看到除了數值不一樣,連 mode 的數目都變了。