masque/examples/tutorial/devices.py

341 lines
12 KiB
Python
Raw Normal View History

2023-01-24 23:25:10 -08:00
# TODO update tutorials
2023-02-23 13:15:32 -08:00
from typing import Sequence, Mapping
2022-02-27 21:21:44 -08:00
import numpy
from numpy import pi
2023-01-24 23:25:10 -08:00
from masque import (
layer_t, Pattern, Ref, Label, Builder, Port, Polygon,
2023-04-07 18:08:42 -07:00
Library, ILibraryView,
2023-01-24 23:25:10 -08:00
)
from masque.utils import ports2data
2023-01-24 23:25:10 -08:00
from masque.file.gdsii import writefile, check_valid_names
2022-02-27 21:21:44 -08:00
import pcgen
import basic_shapes
from basic_shapes import GDS_OPTS
LATTICE_CONSTANT = 512
RADIUS = LATTICE_CONSTANT / 2 * 0.75
def ports_to_data(pat: Pattern) -> Pattern:
"""
2023-01-24 23:25:10 -08:00
Bake port information into the pattern.
This places a label at each port location on layer (3, 0) with text content
'name:ptype angle_deg'
"""
return ports2data.ports_to_data(pat, layer=(3, 0))
def data_to_ports(lib: Mapping[str, Pattern], name: str, pat: Pattern) -> Pattern:
"""
Scans the Pattern to determine port locations. Same port format as `ports_to_data`
"""
return ports2data.data_to_ports(layers=[(3, 0)], library=lib, pattern=pat, name=name)
2022-02-27 21:21:44 -08:00
def perturbed_l3(
lattice_constant: float,
2023-01-24 23:25:10 -08:00
hole: str,
hole_lib: Mapping[str, Pattern],
2022-02-27 21:21:44 -08:00
trench_layer: layer_t = (1, 0),
shifts_a: Sequence[float] = (0.15, 0, 0.075),
shifts_r: Sequence[float] = (1.0, 1.0, 1.0),
2023-02-23 13:15:32 -08:00
xy_size: tuple[int, int] = (10, 10),
2022-02-27 21:21:44 -08:00
perturbed_radius: float = 1.1,
trench_width: float = 1200,
2023-01-24 23:25:10 -08:00
) -> Pattern:
2022-02-27 21:21:44 -08:00
"""
2023-01-24 23:25:10 -08:00
Generate a `Pattern` representing a perturbed L3 cavity.
2022-02-27 21:21:44 -08:00
Args:
lattice_constant: Distance between nearest neighbor holes
2023-01-24 23:25:10 -08:00
hole: name of a `Pattern` containing a single hole
hole_lib: Library which contains the `Pattern` object for hole.
Necessary because we need to know how big it is...
2022-02-27 21:21:44 -08:00
trench_layer: Layer for the trenches, default `(1, 0)`.
shifts_a: passed to `pcgen.l3_shift`; specifies lattice constant
(1 - multiplicative factor) for shifting holes adjacent to
the defect (same row). Default `(0.15, 0, 0.075)` for first,
second, third holes.
shifts_r: passed to `pcgen.l3_shift`; specifies radius for perturbing
holes adjacent to the defect (same row). Default 1.0 for all holes.
Provided sequence should have same length as `shifts_a`.
xy_size: `(x, y)` number of mirror periods in each direction; total size is
`2 * n + 1` holes in each direction. Default (10, 10).
perturbed_radius: radius of holes perturbed to form an upwards-driected beam
(multiplicative factor). Default 1.1.
trench width: Width of the undercut trenches. Default 1200.
Returns:
2023-01-24 23:25:10 -08:00
`Pattern` object representing the L3 design.
2022-02-27 21:21:44 -08:00
"""
2023-01-24 23:25:10 -08:00
print('Generating perturbed L3...')
2022-02-27 21:21:44 -08:00
# Get hole positions and radii
xyr = pcgen.l3_shift_perturbed_defect(mirror_dims=xy_size,
perturbed_radius=perturbed_radius,
shifts_a=shifts_a,
shifts_r=shifts_r)
# Build L3 cavity, using references to the provided hole pattern
2023-01-24 23:25:10 -08:00
pat = Pattern()
pat.refs += [
Ref(hole, scale=r, offset=(lattice_constant * x,
lattice_constant * y))
2022-02-27 21:21:44 -08:00
for x, y, r in xyr]
# Add rectangular undercut aids
2023-01-24 23:25:10 -08:00
min_xy, max_xy = pat.get_bounds_nonempty(hole_lib)
2022-02-27 21:21:44 -08:00
trench_dx = max_xy[0] - min_xy[0]
pat.shapes += [
Polygon.rect(ymin=max_xy[1], xmin=min_xy[0], lx=trench_dx, ly=trench_width, layer=trench_layer),
Polygon.rect(ymax=min_xy[1], xmin=min_xy[0], lx=trench_dx, ly=trench_width, layer=trench_layer),
2022-02-27 21:21:44 -08:00
]
# Ports are at outer extents of the device (with y=0)
extent = lattice_constant * xy_size[0]
2023-01-24 23:25:10 -08:00
pat.ports = dict(
input=Port((-extent, 0), rotation=0, ptype='pcwg'),
output=Port((extent, 0), rotation=pi, ptype='pcwg'),
)
2022-02-27 21:21:44 -08:00
ports_to_data(pat)
2023-01-24 23:25:10 -08:00
return pat
2022-02-27 21:21:44 -08:00
def waveguide(
lattice_constant: float,
2023-01-24 23:25:10 -08:00
hole: str,
2022-02-27 21:21:44 -08:00
length: int,
mirror_periods: int,
2023-01-24 23:25:10 -08:00
) -> Pattern:
2022-02-27 21:21:44 -08:00
"""
2023-01-24 23:25:10 -08:00
Generate a `Pattern` representing a photonic crystal line-defect waveguide.
2022-02-27 21:21:44 -08:00
Args:
lattice_constant: Distance between nearest neighbor holes
2023-01-24 23:25:10 -08:00
hole: name of a `Pattern` containing a single hole
2022-02-27 21:21:44 -08:00
length: Distance (number of mirror periods) between the input and output ports.
Ports are placed at lattice sites.
mirror_periods: Number of hole rows on each side of the line defect
Returns:
2023-01-24 23:25:10 -08:00
`Pattern` object representing the waveguide.
2022-02-27 21:21:44 -08:00
"""
# Generate hole locations
xy = pcgen.waveguide(length=length, num_mirror=mirror_periods)
# Build the pattern
2023-01-24 23:25:10 -08:00
pat = Pattern()
pat.refs += [
Ref(hole, offset=(lattice_constant * x,
lattice_constant * y))
for x, y in xy]
2022-02-27 21:21:44 -08:00
# Ports are at outer edges, with y=0
extent = lattice_constant * length / 2
2023-01-24 23:25:10 -08:00
pat.ports = dict(
left=Port((-extent, 0), rotation=0, ptype='pcwg'),
right=Port((extent, 0), rotation=pi, ptype='pcwg'),
)
ports_to_data(pat)
2023-01-24 23:25:10 -08:00
return pat
2022-02-27 21:21:44 -08:00
def bend(
lattice_constant: float,
2023-01-24 23:25:10 -08:00
hole: str,
2022-02-27 21:21:44 -08:00
mirror_periods: int,
2023-01-24 23:25:10 -08:00
) -> Pattern:
2022-02-27 21:21:44 -08:00
"""
2023-01-24 23:25:10 -08:00
Generate a `Pattern` representing a 60-degree counterclockwise bend in a photonic crystal
2022-02-27 21:21:44 -08:00
line-defect waveguide.
Args:
lattice_constant: Distance between nearest neighbor holes
2023-01-24 23:25:10 -08:00
hole: name of a `Pattern` containing a single hole
2022-02-27 21:21:44 -08:00
mirror_periods: Minimum number of mirror periods on each side of the line defect.
Returns:
2023-01-24 23:25:10 -08:00
`Pattern` object representing the waveguide bend.
2022-02-27 21:21:44 -08:00
Ports are named 'left' (input) and 'right' (output).
"""
# Generate hole locations
xy = pcgen.wgbend(num_mirror=mirror_periods)
# Build the pattern
2023-01-24 23:25:10 -08:00
pat= Pattern()
pat.refs += [
Ref(hole, offset=(lattice_constant * x,
lattice_constant * y))
2022-02-27 21:21:44 -08:00
for x, y in xy]
# Figure out port locations.
extent = lattice_constant * mirror_periods
2023-01-24 23:25:10 -08:00
pat.ports = dict(
left=Port((-extent, 0), rotation=0, ptype='pcwg'),
right=Port((extent / 2,
extent * numpy.sqrt(3) / 2),
rotation=pi * 4 / 3, ptype='pcwg'),
)
ports_to_data(pat)
2023-01-24 23:25:10 -08:00
return pat
2022-02-27 21:21:44 -08:00
def y_splitter(
lattice_constant: float,
2023-01-24 23:25:10 -08:00
hole: str,
2022-02-27 21:21:44 -08:00
mirror_periods: int,
2023-01-24 23:25:10 -08:00
) -> Pattern:
2022-02-27 21:21:44 -08:00
"""
2023-01-24 23:25:10 -08:00
Generate a `Pattern` representing a photonic crystal line-defect waveguide y-splitter.
2022-02-27 21:21:44 -08:00
Args:
lattice_constant: Distance between nearest neighbor holes
2023-01-24 23:25:10 -08:00
hole: name of a `Pattern` containing a single hole
2022-02-27 21:21:44 -08:00
mirror_periods: Minimum number of mirror periods on each side of the line defect.
Returns:
2023-01-24 23:25:10 -08:00
`Pattern` object representing the y-splitter.
2022-02-27 21:21:44 -08:00
Ports are named 'in', 'top', and 'bottom'.
"""
# Generate hole locations
xy = pcgen.y_splitter(num_mirror=mirror_periods)
# Build pattern
2023-01-24 23:25:10 -08:00
pat = Pattern()
pat.refs += [
Ref(hole, offset=(lattice_constant * x,
lattice_constant * y))
2022-02-27 21:21:44 -08:00
for x, y in xy]
# Determine port locations
extent = lattice_constant * mirror_periods
2023-01-24 23:25:10 -08:00
pat.ports = {
2022-02-27 21:21:44 -08:00
'in': Port((-extent, 0), rotation=0, ptype='pcwg'),
'top': Port((extent / 2, extent * numpy.sqrt(3) / 2), rotation=pi * 4 / 3, ptype='pcwg'),
'bot': Port((extent / 2, -extent * numpy.sqrt(3) / 2), rotation=pi * 2 / 3, ptype='pcwg'),
}
ports_to_data(pat)
2023-01-24 23:25:10 -08:00
return pat
2022-02-27 21:21:44 -08:00
2023-01-24 23:25:10 -08:00
def main(interactive: bool = True) -> None:
2022-02-27 21:21:44 -08:00
# Generate some basic hole patterns
2023-01-24 23:25:10 -08:00
shape_lib = {
'smile': basic_shapes.smile(RADIUS),
'hole': basic_shapes.hole(RADIUS),
}
2022-02-27 21:21:44 -08:00
# Build some devices
a = LATTICE_CONSTANT
2023-01-24 23:25:10 -08:00
devices = {}
devices['wg05'] = waveguide(lattice_constant=a, hole='hole', length=5, mirror_periods=5)
devices['wg10'] = waveguide(lattice_constant=a, hole='hole', length=10, mirror_periods=5)
devices['wg28'] = waveguide(lattice_constant=a, hole='hole', length=28, mirror_periods=5)
devices['wg90'] = waveguide(lattice_constant=a, hole='hole', length=90, mirror_periods=5)
devices['bend0'] = bend(lattice_constant=a, hole='hole', mirror_periods=5)
devices['ysplit'] = y_splitter(lattice_constant=a, hole='hole', mirror_periods=5)
devices['l3cav'] = perturbed_l3(lattice_constant=a, hole='smile', hole_lib=shape_lib, xy_size=(4, 10)) # uses smile :)
# Turn our dict of devices into a Library -- useful for getting abstracts
2023-04-07 18:08:42 -07:00
lib = Library(devices)
2023-01-24 23:25:10 -08:00
abv = lib.abstract_view() # lets us use abv[cell] instead of lib.abstract(cell)
2022-02-27 21:21:44 -08:00
#
# Build a circuit
#
2023-01-24 23:25:10 -08:00
circ = Builder(library=lib)
2022-02-27 21:21:44 -08:00
# Start by placing a waveguide. Call its ports "in" and "signal".
2023-01-24 23:25:10 -08:00
circ.place(abv['wg10'], offset=(0, 0), port_map={'left': 'in', 'right': 'signal'})
2022-02-27 21:21:44 -08:00
# Extend the signal path by attaching the "left" port of a waveguide.
# Since there is only one other port ("right") on the waveguide we
# are attaching (wg10), it automatically inherits the name "signal".
2023-01-24 23:25:10 -08:00
circ.plug(abv['wg10'], {'signal': 'left'})
2022-02-27 21:21:44 -08:00
# Attach a y-splitter to the signal path.
# Since the y-splitter has 3 ports total, we can't auto-inherit the
# port name, so we have to specify what we want to name the unattached
# ports. We can call them "signal1" and "signal2".
2023-01-24 23:25:10 -08:00
circ.plug(abv['ysplit'], {'signal': 'in'}, {'top': 'signal1', 'bot': 'signal2'})
2022-02-27 21:21:44 -08:00
# Add a waveguide to both signal ports, inheriting their names.
2023-01-24 23:25:10 -08:00
circ.plug(abv['wg05'], {'signal1': 'left'})
circ.plug(abv['wg05'], {'signal2': 'left'})
2022-02-27 21:21:44 -08:00
# Add a bend to both ports.
# Our bend's ports "left" and "right" refer to the original counterclockwise
# orientation. We want the bends to turn in opposite directions, so we attach
# the "right" port to "signal1" to bend clockwise, and the "left" port
# to "signal2" to bend counterclockwise.
# We could also use `mirrored=(True, False)` to mirror one of the devices
# and then use same device port on both paths.
2023-01-24 23:25:10 -08:00
circ.plug(abv['bend0'], {'signal1': 'right'})
circ.plug(abv['bend0'], {'signal2': 'left'})
2022-02-27 21:21:44 -08:00
# We add some waveguides and a cavity to "signal1".
2023-01-24 23:25:10 -08:00
circ.plug(abv['wg10'], {'signal1': 'left'})
circ.plug(abv['l3cav'], {'signal1': 'input'})
circ.plug(abv['wg10'], {'signal1': 'left'})
2022-02-27 21:21:44 -08:00
# "signal2" just gets a single of equivalent length
2023-01-24 23:25:10 -08:00
circ.plug(abv['wg28'], {'signal2': 'left'})
2022-02-27 21:21:44 -08:00
# Now we bend both waveguides back towards each other
2023-01-24 23:25:10 -08:00
circ.plug(abv['bend0'], {'signal1': 'right'})
circ.plug(abv['bend0'], {'signal2': 'left'})
circ.plug(abv['wg05'], {'signal1': 'left'})
circ.plug(abv['wg05'], {'signal2': 'left'})
2022-02-27 21:21:44 -08:00
# To join the waveguides, we attach a second y-junction.
# We plug "signal1" into the "bot" port, and "signal2" into the "top" port.
# The remaining port gets named "signal_out".
# This operation would raise an exception if the ports did not line up
# correctly (i.e. they required different rotations or translations of the
# y-junction device).
2023-01-24 23:25:10 -08:00
circ.plug(abv['ysplit'], {'signal1': 'bot', 'signal2': 'top'}, {'in': 'signal_out'})
2022-02-27 21:21:44 -08:00
# Finally, add some more waveguide to "signal_out".
2023-01-24 23:25:10 -08:00
circ.plug(abv['wg10'], {'signal_out': 'left'})
2022-02-27 21:21:44 -08:00
# We can also add text labels for our circuit's ports.
# They will appear at the uppermost hierarchy level, while the individual
# device ports will appear further down, in their respective cells.
ports_to_data(circ.pattern)
2023-01-24 23:25:10 -08:00
# Add the pattern into our library
lib['my_circuit'] = circ.pattern
# Check if we forgot to include any patterns... ooops!
if dangling := lib.dangling_refs():
print('Warning: The following patterns are referenced, but not present in the'
f' library! {dangling}')
2023-01-24 23:25:10 -08:00
print('We\'ll solve this by merging in shape_lib, which contains those shapes...')
lib.add(shape_lib)
assert not lib.dangling_refs()
# We can visualize the design. Usually it's easier to just view the GDS.
if interactive:
print('Visualizing... this step may be slow')
circ.pattern.visualize(lib)
2022-02-27 21:21:44 -08:00
2023-01-24 23:25:10 -08:00
#Write out to GDS, only keeping patterns referenced by our circuit (including itself)
subtree = lib.subtree('my_circuit') # don't include wg90, which we don't use
check_valid_names(subtree.keys())
writefile(subtree, 'circuit.gds', **GDS_OPTS)
2022-02-27 21:21:44 -08:00
if __name__ == '__main__':
main()