def __init__(self, eps=1., x_edges=[0.], y_edges=[0.]): """Create a polygon shape Parameters ---------- eps : float Permittivity value x_edges : List or np.ndarray x-coordinates of polygon vertices y_edges : List or np.ndarray y-coordinates of polygon vertices Note ---- The polygon vertices must be supplied in counter-clockwise order. """ # Make extra sure that the last point of the polygon is the same as the # first point self.x_edges = bd.hstack((bd.array(x_edges), x_edges[0])) self.y_edges = bd.hstack((bd.array(y_edges), y_edges[0])) super().__init__(eps) if self.compute_ft([[0.], [0.]]) < 0: raise ValueError("The edges defined by x_edges and y_edges must be" " specified in counter-clockwise order")
def AB_matrices(omega, g, eps_array, d_array, chi_array=None, pol='TE'): """ Function to calculate A,B coeff Output: array of shape [M+1,2] """ assert len(d_array)==len(eps_array)-2, \ 'd_array should have length = num_layers' if chi_array is None: chi_array = chi(omega, g, eps_array) if pol.lower()=='te': S_matrices, T_matrices = \ S_T_matrices_TE(omega, g, eps_array, d_array) elif pol.lower()=='tm': S_matrices, T_matrices = \ S_T_matrices_TM(omega, g, eps_array, d_array) else: raise Exception("Polarization should be 'TE' or 'TM'.") A0 = 0 B0 = 1 AB0 = bd.array([A0, B0]).reshape(-1,1) # A, B coeff for each layer ABs = [AB0, bd.dot(T_matrices[0], bd.dot(S_matrices[0], AB0))] for i,S in enumerate(S_matrices[1:]): term = bd.dot(S_matrices[i+1], bd.dot(T_matrices[i], ABs[-1])) if i < len(S_matrices)-2: term = bd.dot(T_matrices[i+1], term) ABs.append(term) return bd.array(ABs)
def __init__(self, *args): """ Initialize a Bravais lattice. If a single argument is passed, then - 'square': initializes a square lattice. - 'hexagonal': initializes a hexagonal lattice. with lattice constant a = 1 in both cases. If two arguments are passed, they should each be 2-element arrays defining the elementary vectors of the lattice. """ # Primitive vectors cell definition (a1, a2) = self._parse_input(*args) self.a1 = a1[0:2] self.a2 = a2[0:2] ec_area = bd.norm(bd.cross(a1, a2)) a3 = bd.array([0, 0, 1]) # Reciprocal lattice basis vectors b1 = 2*np.pi*bd.cross(a2, a3)/bd.dot(a1, bd.cross(a2, a3)) b2 = 2*np.pi*bd.cross(a3, a1)/bd.dot(a2, bd.cross(a3, a1)) bz_area = bd.norm(bd.cross(b1, b2)) self.b1 = b1[0:2] self.b2 = b2[0:2] self.ec_area = ec_area # Elementary cell area self.bz_area = bz_area # Brillouin zone area
def __init__(self, lattice, z_min: float = 0, z_max: float = 0, eps_b: float = 1.): """Initialize a ShapesLayer. Parameters ---------- lattice : Lattice A lattice defining the 2D periodicity. z_min : float, optional z-coordinate of the bottom of the layer. z_max : float, optional z-coordinate of the top of the layer. eps_b : float, optional Layer background permittivity. """ super().__init__(lattice, z_min, z_max) # Define background permittivity self.eps_b = eps_b # Initialize average permittivity - needed for guided-mode computation self.eps_avg = bd.array(eps_b) # Initialize an empty list of shapes self.layer_type = 'shapes' self.shapes = []
def rotate(self, angle): """Rotate the polygon around its center of mass by `angle` radians """ rotmat = bd.array([[bd.cos(angle), -bd.sin(angle)], \ [bd.sin(angle), bd.cos(angle)]]) (xj, yj) = (bd.array(self.x_edges), bd.array(self.y_edges)) com_x = bd.sum((xj + bd.roll(xj, -1)) * (xj * bd.roll(yj, -1) - \ bd.roll(xj, -1) * yj))/6/self.area com_y = bd.sum((yj + bd.roll(yj, -1)) * (xj * bd.roll(yj, -1) - \ bd.roll(xj, -1) * yj))/6/self.area new_coords = bd.dot(rotmat, bd.vstack((xj-com_x, yj-com_y))) self.x_edges = new_coords[0, :] + com_x self.y_edges = new_coords[1, :] + com_y return self
def run(self, kpoints=np.array([[0], [0]]), pol='te', numeig=10): """ Run the simulation. The computed eigen-frequencies are stored in :attr:`PlaneWaveExp.freqs`, and the corresponding eigenvectors - in :attr:`PlaneWaveExp.eigvecs`. Parameters ---------- kpoints : np.ndarray, optional Numpy array of shape (2, Nk) with the [kx, ky] coordinates of the k-vectors over which the simulation is run. pol : {'te', 'tm'}, optional Polarization of the modes. numeig : int, optional Number of eigen-frequencies to be stored (starting from lowest). """ self._kpoints = kpoints self.pol = pol.lower() # Change this if switching to a solver that allows for variable numeig self.numeig = numeig self._compute_ft() self._compute_eps_inv() freqs = [] self._eigvecs = [] for ik, k in enumerate(kpoints.T): # Construct the matrix for diagonalization if self.pol == 'te': mat = bd.dot(bd.transpose(k[:, bd.newaxis] + self.gvec), (k[:, bd.newaxis] + self.gvec)) mat = mat * self.eps_inv_mat elif self.pol == 'tm': Gk = bd.sqrt(bd.square(k[0] + self.gvec[0, :]) + \ bd.square(k[1] + self.gvec[1, :])) mat = bd.outer(Gk, Gk) mat = mat * self.eps_inv_mat else: raise ValueError("Polarization should be 'TE' or 'TM'") # Diagonalize using numpy.linalg.eigh() for now; should maybe switch # to scipy.sparse.linalg.eigsh() in the future # NB: we shift the matrix by np.eye to avoid problems at the zero- # frequency mode at Gamma (freq2, evecs) = bd.eigh(mat + bd.eye(mat.shape[0])) freq1 = bd.sqrt(bd.abs(freq2 - bd.ones(mat.shape[0])))/2/np.pi i_sort = bd.argsort(freq1)[0:self.numeig] freq = freq1[i_sort] evec = evecs[:, i_sort] freqs.append(freq) self._eigvecs.append(evec) # Store the eigenfrequencies taking the standard reduced frequency # convention for the units (2pi a/c) self._freqs = bd.array(freqs) self.mat = mat
def chis_3layer(omega, g, eps_array): """ """ (eps1, eps2, eps3) = [e for e in eps_array] chis1 = 1j*bd.sqrt(g**2 - eps1*omega**2) chis2 = bd.array(bd.sqrt(-g**2 + eps2*omega**2), dtype=bd.complex) chis3 = 1j*bd.sqrt(g**2 - eps3*omega**2) return (chis1, chis2, chis3)
def S_T_matrices_TE(omega, g, eps_array, d_array): """ Function to get a list of S and T matrices for D22 calculation """ assert len(d_array)==len(eps_array)-2, \ 'd_array should have length = num_layers' chi_array = chi(omega, g, eps_array) S11 = (chi_array[:-1] + chi_array[1:]) S12 = -chi_array[:-1] + chi_array[1:] S22 = S11 S21 = S12 S_matrices = 0.5 / chi_array[1:].reshape(-1,1,1) * \ bd.array([[S11,S12],[S21,S22]]).transpose([2,0,1]) T11 = bd.exp(1j*chi_array[1:-1]*d_array/2) T22 = bd.exp(-1j*chi_array[1:-1]*d_array/2) T_matrices = bd.array([[T11,bd.zeros(T11.shape)], [bd.zeros(T11.shape),T22]]).transpose([2,0,1]) return S_matrices, T_matrices
def __init__(self, eps=1, x_cent=0, y_cent=0, a=0): """Create a square shape Parameters ---------- eps : float Permittivity value x_cent : float x-coordinate of square center y_cent : float y-coordinate of square center a : float square edge length """ self.x_cent = x_cent self.y_cent = y_cent self.a = a x_edges = x_cent + bd.array([-a/2, a/2, a/2, -a/2]) y_edges = y_cent + bd.array([-a/2, -a/2, a/2, a/2]) super().__init__(eps, x_edges, y_edges)
def __init__(self, eps=1, x_cent=0, y_cent=0, a=0): """Create a hexagon shape Parameters ---------- eps : float Permittivity value x_cent : float x-coordinate of hexagon center y_cent : float y-coordinate of hexagon center a : float hexagon edge length """ self.x_cent = x_cent self.y_cent = y_cent self.a = a x_edges = x_cent + bd.array([a, a/2, -a/2, -a, -a/2, a/2, a]) y_edges = y_cent + bd.array([0, np.sqrt(3)/2*a, np.sqrt(3)/2*a, 0, \ -np.sqrt(3)/2*a, -np.sqrt(3)/2*a, 0]) super().__init__(eps, x_edges, y_edges)
def chi(omega, g, eps): """ Function to compute chi_j, the z-direction wave-vector in each layer j Either omega is an array and eps is a number, or vice versa Input omega : frequency * 2π , in units of light speed/unit length eps : slab permittivity array g : wave vector along propagation direction Output chi : array of chi_j for all layers j including claddings """ sqarg = bd.array(eps * omega**2 - g**2, dtype=bd.complex) return bd.where(bd.real(sqarg) >= 0, bd.sqrt(sqarg), 1j * bd.sqrt(-sqarg))
def _parse_input(self, *args): if len(args) == 1: if args[0] == 'square': self.type = 'square' a1 = bd.array([1, 0, 0]) a2 = bd.array([0, 1, 0]) elif args[0] == 'hexagonal': self.type = 'hexagonal' a1 = bd.array([0.5, bd.sqrt(3)/2, 0]) a2 = bd.array([0.5, -bd.sqrt(3)/2, 0]) else: raise ValueError("Lattice can be 'square' or 'hexagonal, " "or defined through two primitive vectors.") elif len(args) == 2: a1 = bd.hstack((bd.array(args[0]), 0)) a2 = bd.hstack((bd.array(args[1]), 0)) if np.inner(a1, a2) == 0: self.type = 'rectangular' else: self.type = 'custom' return (a1, a2)
def __init__(self, eps=1.): """Create a shape """ self.eps = eps self.area = bd.real(self.compute_ft(bd.array([[0.], [0.]])))
def rad_modes(omega: float, g_array: np.ndarray, eps_array: np.ndarray, d_array: np.ndarray, pol: str='TE', clad: int=0): """ Function to compute the radiative modes of a multi-layer structure Input g_array : numpy array of wave vector amplitudes eps_array : numpy array of slab permittivities, starting with lower cladding and ending with upper cladding d_array : thicknesses of each layer omega : frequency of the radiative mode pol : polarization, 'te' or 'tm' clad : radiating into cladding index, 0 (lower) or 1 (upper) Output Xs, Ys : X, Y coefficients of the modes in every layer """ Xs, Ys = [], [] for ig, g in enumerate(g_array): g_val = max([g, 1e-10]) # Get the scattering and transfer matrices if pol.lower()=='te' and clad==0: S_mat, T_mat = S_T_matrices_TE(omega, g_val, eps_array[::-1], d_array[::-1]) elif pol.lower()=='te' and clad==1: S_mat, T_mat = S_T_matrices_TE(omega, g_val, eps_array, d_array) elif pol.lower()=='tm' and clad==0: S_mat, T_mat = S_T_matrices_TM(omega, g_val, eps_array[::-1], d_array[::-1]) elif pol.lower()=='tm' and clad==1: S_mat, T_mat = S_T_matrices_TM(omega, g_val, eps_array, d_array) # Compute the transfer matrix coefficients coeffs = [bd.array([0, 1])] coeffs.append(bd.dot(T_mat[0], bd.dot(S_mat[0], coeffs[0]))) for i, S in enumerate(S_mat[1:-1]): T2 = T_mat[i+1] T1 = T_mat[i] coeffs.append(bd.dot(T2, bd.dot(S, bd.dot(T1, coeffs[-1])))) coeffs.append(bd.dot(S_mat[-1], bd.dot(T_mat[-1], coeffs[-1]))) coeffs = bd.array(coeffs, dtype=bd.complex).transpose() # Normalize coeffs = coeffs / coeffs[1, -1] if pol=='te': c_ind = [0, -1] coeffs = coeffs/bd.sqrt(eps_array[c_ind[clad]])/omega # Assign correctly based on which cladding the modes radiate to if clad == 0: Xs.append(coeffs[0, ::-1].ravel()) Ys.append(coeffs[1, ::-1].ravel()) elif clad == 1: Xs.append(coeffs[1, :].ravel()) Ys.append(coeffs[0, :].ravel()) Xs = bd.array(Xs, dtype=bd.complex).transpose() Ys = bd.array(Ys, dtype=bd.complex).transpose() # Fix the dimension if g_array is an empty list if len(g_array)==0: Xs = bd.ones((eps_array.size, 1))*Xs Ys = bd.ones((eps_array.size, 1))*Ys """ (Xs, Ys) corresponds to the X, W coefficients for TE radiative modes in Andreani and Gerace PRB (2006), and to the Z, Y coefficients for TM modes Note that there's an error in the manuscript; within our definitions, the correct statement should be: X3 = 0 for states out-going in the lower cladding; normalize through W1; and W1 = 0 for states out-going in the upper cladding; normalize through X3. """ return (Xs, Ys)