Skip to content

Commit

Permalink
Merge pull request #2304 from gdsfactory/simpler_add_grating_couplers
Browse files Browse the repository at this point in the history
simpler_add_grating_couplers
  • Loading branch information
joamatab committed Nov 12, 2023
2 parents 5d6e69e + 1148e18 commit f3edb98
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 152 deletions.
115 changes: 2 additions & 113 deletions gdsfactory/components/add_grating_couplers.py
Expand Up @@ -7,14 +7,12 @@
import numpy as np

import gdsfactory as gf
from gdsfactory.add_labels import get_input_label_text_loopback
from gdsfactory.component import Component
from gdsfactory.components.bend_euler import bend_euler
from gdsfactory.components.grating_coupler_elliptical_trenches import grating_coupler_te
from gdsfactory.components.straight import straight
from gdsfactory.cross_section import strip
from gdsfactory.port import select_ports_optical
from gdsfactory.routing.get_input_labels import get_input_labels
from gdsfactory.routing.manhattan import round_corners
from gdsfactory.routing.utils import (
check_ports_have_equal_spacing,
Expand All @@ -23,8 +21,6 @@
from gdsfactory.typings import (
ComponentSpec,
CrossSectionSpec,
LabelListFactory,
LayerSpec,
PortsDict,
)

Expand All @@ -33,28 +29,20 @@
def add_grating_couplers(
component: ComponentSpec = straight,
grating_coupler: ComponentSpec = grating_coupler_te,
layer_label: LayerSpec | None = None,
gc_port_name: str = "o1",
get_input_labels_function: LabelListFactory | None = get_input_labels,
select_ports: Callable[..., PortsDict] = select_ports_optical,
component_name: str | None = None,
) -> Component:
"""Returns new component with grating couplers and labels.
Args:
component: to add grating_couplers.
grating_coupler: grating_coupler spec.
layer_label: for label.
gc_port_name: where to add label.
get_input_labels_function: function to get label.
select_ports: for selecting optical_ports.
component_name: optional component name.
"""
c = Component()
component = gf.get_component(component)

c.component = component
component_name = component_name or component.metadata_child.get("name")
c.add_ref(component)
grating_coupler = gf.get_component(grating_coupler)

Expand All @@ -71,15 +59,6 @@ def add_grating_couplers(
io_gratings.append(gc_ref)
c.add(gc_ref)

if layer_label and get_input_labels_function:
labels = get_input_labels_function(
io_gratings,
list(component.ports.values()),
component_name=component_name,
layer_label=layer_label,
gc_port_name=gc_port_name,
)
c.add(labels)
c.copy_child_info(component)
return c

Expand All @@ -88,14 +67,10 @@ def add_grating_couplers(
def add_grating_couplers_with_loopback_fiber_single(
component: ComponentSpec = "spiral_inner_io_fiber_single",
grating_coupler: ComponentSpec = grating_coupler_te,
layer_label: tuple[int, int] | None = None,
gc_port_name: str = "o1",
get_input_labels_function: LabelListFactory | None = get_input_labels,
get_input_label_text_loopback_function: Callable = get_input_label_text_loopback,
select_ports: Callable[..., PortsDict] = select_ports_optical,
with_loopback: bool = True,
cross_section: CrossSectionSpec = strip,
component_name: str | None = None,
loopback_xspacing: float = 5.0,
rotation: int = 90,
) -> Component:
Expand All @@ -104,27 +79,21 @@ def add_grating_couplers_with_loopback_fiber_single(
Args:
component: to add grating_couplers.
grating_coupler: grating_coupler spec function, string or dict.
layer_label: optional layer_label for the ports.
gc_port_name: grating_coupler port name.
get_input_labels_function: function to get grating_coupler labels.
get_input_label_text_loopback_function:
select_ports: function to select ports.
with_loopback: adds a reference loopback.
cross_section: for routing.
component_name: optional component name.
loopback_xspacing: in um.
rotation: in degrees, 90 for North South devices, 0 for East-West.
"""
c = Component()
component = gf.get_component(component)

c.component = component
c.add_ref(component)
grating_coupler = gf.get_component(grating_coupler)

c.info["polarization"] = grating_coupler.info["polarization"]
c.info["wavelength"] = grating_coupler.info["wavelength"]
component_name = component_name or component.metadata_child.get("name")

io_gratings = []
optical_ports = select_ports(component.ports)
Expand All @@ -138,16 +107,6 @@ def add_grating_couplers_with_loopback_fiber_single(
c.add(gc_ref)
c.add_port(name=port.name, port=port)

if layer_label and get_input_labels_function:
labels = get_input_labels_function(
io_gratings,
optical_ports,
component_name=component_name,
layer_label=layer_label,
gc_port_name=gc_port_name,
)
c.add(labels)

p2 = optical_ports[0]
p1 = optical_ports[-1]

Expand Down Expand Up @@ -175,30 +134,6 @@ def add_grating_couplers_with_loopback_fiber_single(
p1.port_type = "loopback"
p2.port_type = "loopback"

if layer_label and get_input_labels_function:
port = wg.ports["o2"]
text = get_input_label_text_loopback_function(
port=port, gc=grating_coupler, gc_index=0, component_name=component_name
)

c.add_label(
text=text,
position=port.center,
anchor="o",
layer=layer_label,
)

port = wg.ports["o1"]
text = get_input_label_text_loopback_function(
port=port, gc=grating_coupler, gc_index=1, component_name=component_name
)
c.add_label(
text=text,
position=port.center,
anchor="o",
layer=layer_label,
)

c.copy_child_info(component)
return c

Expand All @@ -216,12 +151,7 @@ def add_grating_couplers_with_loopback_fiber_array(
gc_rotation: int = -90,
straight_separation: float = 5.0,
bend: ComponentSpec = bend_euler,
layer_label: LayerSpec | None = None,
layer_label_loopback: LayerSpec | None = None,
component_name: str | None = None,
with_loopback: bool = True,
nlabels_loopback: int = 2,
get_input_labels_function: LabelListFactory | None = get_input_labels,
cross_section: CrossSectionSpec = "xs_sc",
select_ports: Callable = select_ports_optical,
loopback_yspacing: float = 4.0,
Expand All @@ -237,13 +167,7 @@ def add_grating_couplers_with_loopback_fiber_array(
gc_rotation: grating coupler rotation in degrees.
straight_separation: in um.
bend: bend spec.
layer_label: for testing label.
layer_label_loopback: for testing label alignment loopback.
component_name: optional component name.
with_loopback: If True, add compact loopback alignment ports.
nlabels_loopback: number of ports to label \
(0: no labels, 1: first port, 2: both ports).
get_input_labels_function: for getting test labels.
cross_section: CrossSectionSpec.
select_ports: function to select ports.
loopback_yspacing: in um.
Expand All @@ -255,7 +179,6 @@ def add_grating_couplers_with_loopback_fiber_array(
gc = gf.get_component(grating_coupler)

direction = "S"
component_name = component_name or component.metadata_child.get("name")

c = Component()
c.copy_child_info(component)
Expand Down Expand Up @@ -290,16 +213,6 @@ def add_grating_couplers_with_loopback_fiber_array(
gc_ref.connect(gc.ports[gc_port_name].name, port)
references += [gc_ref]

if layer_label and get_input_labels_function:
labels = get_input_labels_function(
io_gratings=references,
ordered_ports=optical_ports,
component_name=component_name,
layer_label=layer_label,
gc_port_name=gc_port_name,
)
c.add(labels)

if with_loopback:
y0 = references[0].ports[gc_port_name].y - loopback_yspacing
xs = [p.x for p in optical_ports]
Expand Down Expand Up @@ -341,32 +254,8 @@ def add_grating_couplers_with_loopback_fiber_array(
c.add([gca1, gca2])
c.add(loopback_route.references)
c.add_port(name="loopback_1", port=port0)
c.add_port(name=f"loopback_{len(component.ports)+2}", port=port1)

component_name_loopback = f"loopback_{component_name}"
if nlabels_loopback == 1:
io_gratings_loopback = [gca1]
ordered_ports_loopback = [port0]
elif nlabels_loopback == 2:
io_gratings_loopback = [gca1, gca2]
ordered_ports_loopback = [port0, port1]
if nlabels_loopback == 0 or layer_label is None:
pass
elif 0 < nlabels_loopback <= 2 and get_input_labels_function:
c.add(
get_input_labels_function(
io_gratings=io_gratings_loopback,
ordered_ports=ordered_ports_loopback,
component_name=component_name_loopback,
layer_label=layer_label_loopback or layer_label,
gc_port_name=gc_port_name,
)
)
else:
raise ValueError(
f"Invalid nlabels_loopback = {nlabels_loopback}, "
"valid (0: no labels, 1: first port, 2: both ports2)"
)
c.add_port(name="loopback_2", port=port1)

ports = [p.flip() for p in component.ports.values()]
c.add_ports(ports)
c.copy_child_info(component)
Expand Down
7 changes: 6 additions & 1 deletion gdsfactory/samples/01_component_pcell_with_pins.py
Expand Up @@ -29,7 +29,12 @@ def straight_narrow(


if __name__ == "__main__":
wg = straight_narrow(decorator=gf.add_pins.add_pins)
from functools import partial

c = partial(
gf.c.spiral_inner_io,
decorator=gf.c.add_grating_couplers_with_loopback_fiber_array,
)
wg = c()
# By default show adds pins, so you don't need it to show_ports
wg.show(show_ports=False)
8 changes: 0 additions & 8 deletions test-data-regression/test_settings_add_grating_couplers_.yml
Expand Up @@ -6,33 +6,25 @@ settings:
default:
component:
function: straight
component_name: null
gc_port_name: o1
get_input_labels_function:
function: get_input_labels
grating_coupler:
function: grating_coupler_elliptical_trenches
settings:
polarization: te
taper_angle: 35
layer_label: null
select_ports:
function: select_ports
settings:
port_type: optical
full:
component:
function: straight
component_name: null
gc_port_name: o1
get_input_labels_function:
function: get_input_labels
grating_coupler:
function: grating_coupler_elliptical_trenches
settings:
polarization: te
taper_angle: 35
layer_label: null
select_ports:
function: select_ports
settings:
Expand Down
Expand Up @@ -35,23 +35,17 @@ settings:
component: spiral_inner_io
settings:
length: 20000.0
component_name: null
cross_section: xs_sc
excluded_ports: null
gc_port_name: o1
gc_rotation: -90
get_input_labels_function:
function: get_input_labels
grating_coupler:
function: grating_coupler_elliptical_trenches
settings:
polarization: te
taper_angle: 35
grating_separation: 127.0
layer_label: null
layer_label_loopback: null
loopback_yspacing: 4.0
nlabels_loopback: 2
select_ports:
function: select_ports
settings:
Expand All @@ -65,23 +59,17 @@ settings:
component: spiral_inner_io
settings:
length: 20000.0
component_name: null
cross_section: xs_sc
excluded_ports: null
gc_port_name: o1
gc_rotation: -90
get_input_labels_function:
function: get_input_labels
grating_coupler:
function: grating_coupler_elliptical_trenches
settings:
polarization: te
taper_angle: 35
grating_separation: 127.0
layer_label: null
layer_label_loopback: null
loopback_yspacing: 4.0
nlabels_loopback: 2
select_ports:
function: select_ports
settings:
Expand Down
Expand Up @@ -53,24 +53,16 @@ settings:
child: null
default:
component: spiral_inner_io_fiber_single
component_name: null
cross_section:
function: cross_section
settings:
add_pins_function_name: add_pins_inside1nm
gc_port_name: o1
get_input_label_text_loopback_function:
function: get_input_label_text
settings:
prefix: loopback_
get_input_labels_function:
function: get_input_labels
grating_coupler:
function: grating_coupler_elliptical_trenches
settings:
polarization: te
taper_angle: 35
layer_label: null
loopback_xspacing: 5.0
rotation: 90
select_ports:
Expand All @@ -80,24 +72,16 @@ settings:
with_loopback: true
full:
component: spiral_inner_io_fiber_single
component_name: null
cross_section:
function: cross_section
settings:
add_pins_function_name: add_pins_inside1nm
gc_port_name: o1
get_input_label_text_loopback_function:
function: get_input_label_text
settings:
prefix: loopback_
get_input_labels_function:
function: get_input_labels
grating_coupler:
function: grating_coupler_elliptical_trenches
settings:
polarization: te
taper_angle: 35
layer_label: null
loopback_xspacing: 5.0
rotation: 90
select_ports:
Expand Down

0 comments on commit f3edb98

Please sign in to comment.