Components with hierarchy

Components with hierarchy#

You can define component parametric cells (waveguides, bends, couplers) as functions with basic input parameters (width, length, radius …) and use them as arguments for composing more complex functions.

from functools import partial

import toolz
import gdsfactory as gf
from gdsfactory.typings import ComponentSpec, CrossSectionSpec

Problem

When using hierarchical cells where you pass N subcells with M parameters you can end up with N*M parameters. This is make code hard to read.

@gf.cell
def bend_with_straight_with_too_many_input_parameters(
    bend=gf.components.bend_euler,
    straight=gf.components.straight,
    length: float = 3,
    angle: float = 90.0,
    p: float = 0.5,
    with_arc_floorplan: bool = True,
    npoints: int | None = None,
    cross_section: CrossSectionSpec = "strip",
) -> gf.Component:
    """As hierarchical cells become more complex, the number of input parameters can increase significantly."""
    c = gf.Component()
    b = bend(
        angle=angle,
        p=p,
        with_arc_floorplan=with_arc_floorplan,
        npoints=npoints,
        cross_section=cross_section,
    )
    s = straight(length=length, cross_section=cross_section)

    bref = c << b
    sref = c << s

    sref.connect("o2", bref.ports["o2"])
    c.info["length"] = b.info["length"] + s.info["length"]
    return c


c = bend_with_straight_with_too_many_input_parameters()
c.plot()
../_images/24fe0cf353e082d0a9a0689eade4aaa5ae36a3aedb10f418b51aaafac1654627.png

Solution

You can use a ComponentSpec parameter for every subcell. The ComponentSpec can be a dictionary with arbitrary number of settings, a string, or a function.

ComponentSpec#

When defining a Parametric cell you can use other ComponentSpec as an arguments. It can be a:

  1. string: function name of a cell registered on the active PDK. "bend_circular"

  2. dict: dict(component='bend_circular', settings=dict(radius=20))

  3. function: Using functools.partial you can customize the default parameters of a function.

@gf.cell
def bend_with_straight(
    bend: ComponentSpec = gf.components.bend_euler,
    straight: ComponentSpec = gf.components.straight,
) -> gf.Component:
    """Much simpler version.

    Args:
        bend: input bend.
        straight: output straight.
    """
    c = gf.Component()
    b = gf.get_component(bend)
    s = gf.get_component(straight)

    bref = c << b
    sref = c << s

    sref.connect("o2", bref.ports["o2"])
    c.info["length"] = b.info["length"] + s.info["length"]
    return c


c = bend_with_straight()
c.plot()
../_images/bde014307cd08ab692a8e59c6f794767d40ff0e8d883b028bd1f492e2dba32df.png

1. string#

You can use any string registered in the Pdk. Go to the PDK tutorial to learn how to register cells in a PDK.

c = bend_with_straight(bend="bend_circular")
c.plot()
../_images/19cf7fbe0751b0df1abf005a4296435d07391995cc8dcc1c67f55384c94bab0a.png

2. dict#

You can pass a dict of settings.

bend = gf.get_component("bend_circular", radius=20)

c = bend_with_straight(bend=bend)
c.plot()
../_images/4ca77e65cbfb6f76d2af7ba824c34e5cb9ff1d1e4366b437ccb9fb637d5b9b84.png

3. function#

You can pass a function of a function with customized default input parameters from functools import partial

Partial lets you define different default parameters for a function, so you can modify the default settings for each child cell.

c = bend_with_straight(bend=partial(gf.components.bend_circular, radius=30))
c.plot()
../_images/e2ee29ed6286a2ba71c3282f18a843d464f91296ba5cc1bda2b352431cda214d.png
bend20 = partial(gf.components.bend_circular, radius=20)
b = bend20()
b.plot()
../_images/431086c08facf452212058bd3262afc2edd3892dff439ca1b399534818a6bb76.png
type(bend20)
functools.partial
bend20.func.__name__
'bend_circular'
bend20.keywords
{'radius': 20}
b = bend_with_straight(bend=bend20)
print(b.info.length)
b.plot()
41.416
../_images/4ca77e65cbfb6f76d2af7ba824c34e5cb9ff1d1e4366b437ccb9fb637d5b9b84.png
# You can still modify the bend to have any bend radius
b3 = bend20(radius=10)
b3.plot()
../_images/374eb714fe9d1b02ac084adf3c0c06bb04a34540b79e8c6fa4a8e5b0882ae370.png

Composing functions#

You can combine more complex functions out of smaller functions.

Lets say that we want to add tapers and grating couplers to a wide waveguide.

c1 = gf.components.straight()
c1.plot()
../_images/4d11a6d9b667440ec51e33c443fa7ced22b997854113f4f48dcb18738f3ac298.png
straight_wide = partial(gf.components.straight, width=3)
c3 = straight_wide()
c3.plot()
../_images/5b755dcd818a164f92f91327e766774e7ad16ac8e34d01845fd1bcd32ccac5b0.png
c1 = gf.components.straight()
c1.plot()
../_images/4d11a6d9b667440ec51e33c443fa7ced22b997854113f4f48dcb18738f3ac298.png
c2 = gf.c.extend_ports(c1, length=5)
c2
../_images/7f684e1c290fdeccea08de820f802254747916ca822e866ff685589d2dea37b3.png
c3 = gf.routing.add_fiber_array(c2, with_loopback=False)
c3.plot()
../_images/ba5ba84b9b5bd8d662f6f58590012a9ee7b5c416b3f9a74bb5e2a8b4cd9137c1.png

Lets do it with a single step thanks to toolz.pipe

add_fiber_array = partial(gf.routing.add_fiber_array, with_loopback=False)

c1 = gf.c.straight(width=5)
taper = gf.components.taper(length=10, width1=5, width2=0.5)
add_tapers = partial(gf.c.extend_ports, extension=taper)

# pipe is more readable than the equivalent add_fiber_array(add_tapers(c1))
c3 = toolz.pipe(c1, add_tapers, add_fiber_array)
c3.plot()
../_images/0d10bbbde4ff5e579748a99c8d53959f730fdc981990a0dc0764da3f20d79b3e.png

we can even combine add_tapers and add_fiber_array thanks to toolz.compose or toolz.compose

For example:

add_tapers_fiber_array = toolz.compose_left(add_tapers, add_fiber_array)
c4 = add_tapers_fiber_array(c1)
c4.plot()
../_images/0d10bbbde4ff5e579748a99c8d53959f730fdc981990a0dc0764da3f20d79b3e.png

is equivalent to

c5 = add_fiber_array(add_tapers(c1))
c5.plot()
../_images/0d10bbbde4ff5e579748a99c8d53959f730fdc981990a0dc0764da3f20d79b3e.png

as well as equivalent to

add_tapers_fiber_array = toolz.compose(add_fiber_array, add_tapers)
c6 = add_tapers_fiber_array(c1)
c6.plot()
../_images/0d10bbbde4ff5e579748a99c8d53959f730fdc981990a0dc0764da3f20d79b3e.png

or

c7 = toolz.pipe(c1, add_tapers, add_fiber_array)
c7.plot()
../_images/0d10bbbde4ff5e579748a99c8d53959f730fdc981990a0dc0764da3f20d79b3e.png