comment out some eme notes

This commit is contained in:
Jan Petykiewicz 2024-03-18 10:54:51 -07:00
parent 99461dc129
commit ce3e47daa9

View File

@ -1,169 +1,173 @@
import scipy
from simphony.elements import Model import numpy
from simphony.netlist import Subcircuit from numpy.typing import ArrayLike, NDarray
from simphony.simulation import SweepSimulation
from matplotlib import pyplot as plt
class PeriodicLayer(Model): #from simphony.elements import Model
def __init__(self, left_modes, right_modes, s_params): #from simphony.netlist import Subcircuit
self.left_modes = left_modes #from simphony.simulation import SweepSimulation
self.right_modes = right_modes #
self.left_ports = len(self.left_modes) #from matplotlib import pyplot as plt
self.right_ports = len(self.right_modes) #
self.normalize_fields() #
self.s_params = s_params #class PeriodicLayer(Model):
# def __init__(self, left_modes, right_modes, s_params):
def normalize_fields(self): # self.left_modes = left_modes
for mode in range(len(self.left_modes)): # self.right_modes = right_modes
self.left_modes[mode].normalize() # self.left_ports = len(self.left_modes)
for mode in range(len(self.right_modes)): # self.right_ports = len(self.right_modes)
self.right_modes[mode].normalize() # self.normalize_fields()
# self.s_params = s_params
#
class PeriodicEME: # def normalize_fields(self):
def __init__(self, layers=[], num_periods=1): # for mode in range(len(self.left_modes)):
self.layers = layers # self.left_modes[mode].normalize()
self.num_periods = num_periods # for mode in range(len(self.right_modes)):
self.wavelength = wavelength # self.right_modes[mode].normalize()
#
def propagate(self): #
wl = self.wavelength #class PeriodicEME:
if not len(self.layers): # def __init__(self, layers=[], num_periods=1):
raise Exception("Must place layers before propagating") # self.layers = layers
# self.num_periods = num_periods
num_modes = max([l.num_modes for l in self.layers]) # self.wavelength = wavelength
iface = InterfaceSingleMode if num_modes == 1 else InterfaceMultiMode #
# def propagate(self):
eme = EME(layers=self.layers) # wl = self.wavelength
left, right = eme.propagate() # if not len(self.layers):
self.single_period = eme.s_matrix # raise Exception("Must place layers before propagating")
#
period_layer = PeriodicLayer(left.modes, right.modes, self.single_period) # num_modes = max([l.num_modes for l in self.layers])
current_layer = PeriodicLayer(left.modes, right.modes, self.single_period) # iface = InterfaceSingleMode if num_modes == 1 else InterfaceMultiMode
interface = iface(right, left) #
# eme = EME(layers=self.layers)
for _ in range(self.num_periods - 1): # left, right = eme.propagate()
current_layer.s_params = cascade(current_layer, interface, wl) # self.single_period = eme.s_matrix
current_layer.s_params = cascade(current_layer, period_layer, wl) #
# period_layer = PeriodicLayer(left.modes, right.modes, self.single_period)
self.s_params = current_layer.s_params # current_layer = PeriodicLayer(left.modes, right.modes, self.single_period)
# interface = iface(right, left)
#
class EME: # for _ in range(self.num_periods - 1):
def __init__(self, layers=[]): # current_layer.s_params = cascade(current_layer, interface, wl)
self.layers = layers # current_layer.s_params = cascade(current_layer, period_layer, wl)
self.wavelength = None #
# self.s_params = current_layer.s_params
def propagate(self): #
layers = self.layers #
wl = layers[0].wavelength if self.wavelength is None else self.wavelength #class EME:
if not len(layers): # def __init__(self, layers=[]):
raise Exception("Must place layers before propagating") # self.layers = layers
# self.wavelength = None
num_modes = max([l.num_modes for l in layers]) #
iface = InterfaceSingleMode if num_modes == 1 else InterfaceMultiMode # def propagate(self):
# layers = self.layers
first_layer = layers[0] # wl = layers[0].wavelength if self.wavelength is None else self.wavelength
current = Current(wl, first_layer) # if not len(layers):
interface = iface(first_layer, layers[1]) # raise Exception("Must place layers before propagating")
#
current.s = cascade(current, interface, wl) # num_modes = max([l.num_modes for l in layers])
current.right_pins = interface.right_pins # iface = InterfaceSingleMode if num_modes == 1 else InterfaceMultiMode
#
for index in range(1, len(layers) - 1): # first_layer = layers[0]
layer1 = layers[index] # current = Current(wl, first_layer)
layer2 = layers[index + 1] # interface = iface(first_layer, layers[1])
interface = iface(layer1, layer2) #
# current.s = cascade(current, interface, wl)
current.s = cascade(current, layer1, wl) # current.right_pins = interface.right_pins
current.right_pins = layer1.right_pins #
# for index in range(1, len(layers) - 1):
current.s = cascade(current, interface, wl) # layer1 = layers[index]
current.right_pins = interface.right_pins # layer2 = layers[index + 1]
# interface = iface(layer1, layer2)
last_layer = layers[-1] #
current.s = cascade(current, last_layer, wl) # current.s = cascade(current, layer1, wl)
current.right_pins = last_layer.right_pins # current.right_pins = layer1.right_pins
#
self.s_matrix = current.s # current.s = cascade(current, interface, wl)
return first_layer, last_layer # current.right_pins = interface.right_pins
#
# last_layer = layers[-1]
def stack(sa, sb): # current.s = cascade(current, last_layer, wl)
qab = numpy.eye() - sa.r11 @ sb.r11 # current.right_pins = last_layer.right_pins
qba = numpy.eye() - sa.r11 @ sb.r11 #
#s.t12 = sa.t12 @ numpy.pinv(qab) @ sb.t12 # self.s_matrix = current.s
#s.r21 = sa.t12 @ numpy.pinv(qab) @ sb.r22 @ sa.t21 + sa.r22 # return first_layer, last_layer
#s.r12 = sb.t21 @ numpy.pinv(qba) @ sa.r11 @ sb.t12 + sb.r11 #
#s.t21 = sb.t21 @ numpy.pinv(qba) @ sa.t21 #
s.t12 = sa.t12 @ numpy.linalg.solve(qab, sb.t12) #def stack(sa, sb):
s.r21 = sa.t12 @ numpy.linalg.solve(qab, sb.r22 @ sa.t21) + sa.r22 # qab = numpy.eye() - sa.r11 @ sb.r11
s.r12 = sb.t21 @ numpy.linalg.solve(qba, sa.r11 @ sb.t12) + sb.r11 # qba = numpy.eye() - sa.r11 @ sb.r11
s.t21 = sb.t21 @ numpy.linalg.solve(qba, sa.t21) # #s.t12 = sa.t12 @ numpy.pinv(qab) @ sb.t12
return s # #s.r21 = sa.t12 @ numpy.pinv(qab) @ sb.r22 @ sa.t21 + sa.r22
# #s.r12 = sb.t21 @ numpy.pinv(qba) @ sa.r11 @ sb.t12 + sb.r11
# #s.t21 = sb.t21 @ numpy.pinv(qba) @ sa.t21
def cascade(first, second, wavelength): # s.t12 = sa.t12 @ numpy.linalg.solve(qab, sb.t12)
circuit = Subcircuit("Device") # s.r21 = sa.t12 @ numpy.linalg.solve(qab, sb.r22 @ sa.t21) + sa.r22
# s.r12 = sb.t21 @ numpy.linalg.solve(qba, sa.r11 @ sb.t12) + sb.r11
circuit.add([(first, "first"), (second, "second")]) # s.t21 = sb.t21 @ numpy.linalg.solve(qba, sa.t21)
for port in range(first.right_ports): # return s
circuit.connect("first", "right" + str(port), "second", "left" + str(port)) #
#
simulation = SweepSimulation(circuit, wavelength, wavelength, num=1) #def cascade(first, second, wavelength):
result = simulation.simulate() # circuit = Subcircuit("Device")
return result.s #
# circuit.add([(first, "first"), (second, "second")])
# for port in range(first.right_ports):
class InterfaceSingleMode(Model): # circuit.connect("first", "right" + str(port), "second", "left" + str(port))
def __init__(self, layer1, layer2, num_modes=1): #
self.num_modes = num_modes # simulation = SweepSimulation(circuit, wavelength, wavelength, num=1)
self.num_ports = 2 * num_modes # result = simulation.simulate()
self.s = self.solve(layer1, layer2, num_modes) # return result.s
#
def solve(self, layer1, layer2, num_modes): #
nm = num_modes #class InterfaceSingleMode(Model):
s = numpy.zeros((2 * nm, 2 * nm), dtype=complex) # def __init__(self, layer1, layer2, num_modes=1):
# self.num_modes = num_modes
for ii, left_mode in enumerate(layer1.modes): # self.num_ports = 2 * num_modes
for oo, right_mode in enumerate(layer2.modes): # self.s = self.solve(layer1, layer2, num_modes)
r, t = get_rt(left_mode, right_mode) #
s[ oo, ii] = r # def solve(self, layer1, layer2, num_modes):
s[nm + oo, ii] = t # nm = num_modes
# s = numpy.zeros((2 * nm, 2 * nm), dtype=complex)
for ii, right_mode in enumerate(layer2.modes): #
for oo, left_mode in enumerate(layer1.modes): # for ii, left_mode in enumerate(layer1.modes):
r, t = get_rt(right_mode, left_mode) # for oo, right_mode in enumerate(layer2.modes):
s[ oo, nm + ii] = t # r, t = get_rt(left_mode, right_mode)
s[nm + oo, nm + ii] = r # s[ oo, ii] = r
return s # s[nm + oo, ii] = t
#
# for ii, right_mode in enumerate(layer2.modes):
class InterfaceMultiMode(Model): # for oo, left_mode in enumerate(layer1.modes):
def __init__(self, layer1, layer2): # r, t = get_rt(right_mode, left_mode)
self.s = self.solve(layer1, layer2) # s[ oo, nm + ii] = t
# s[nm + oo, nm + ii] = r
def solve(self, layer1, layer2): # return s
n1p = layer1.num_modes #
n2p = layer2.num_modes #
num_ports = n1p + n2p #class InterfaceMultiMode(Model):
s = numpy.zeros((num_ports, num_ports), dtype=complex) # def __init__(self, layer1, layer2):
# self.s = self.solve(layer1, layer2)
for l1p in range(n1p): #
ts = get_t(l1p, layer1, layer2) # def solve(self, layer1, layer2):
rs = get_r(l1p, layer1, layer2, ts) # n1p = layer1.num_modes
s[n1p:, l1p] = ts # n2p = layer2.num_modes
s[:n1p, l1p] = rs # num_ports = n1p + n2p
# s = numpy.zeros((num_ports, num_ports), dtype=complex)
for l2p in range(n2p): #
ts = get_t(l2p, layer2, layer1) # for l1p in range(n1p):
rs = get_r(l2p, layer2, layer1, ts) # ts = get_t(l1p, layer1, layer2)
s[:n1p, n1p + l2p] = ts # rs = get_r(l1p, layer1, layer2, ts)
s[n1p:, n1p + l2p] = rs # s[n1p:, l1p] = ts
# s[:n1p, l1p] = rs
return s #
# for l2p in range(n2p):
# ts = get_t(l2p, layer2, layer1)
# rs = get_r(l2p, layer2, layer1, ts)
# s[:n1p, n1p + l2p] = ts
# s[n1p:, n1p + l2p] = rs
#
# return s
def get_t(p, left, right): def get_t(p, left, right):