"""A periodic route profile — distributed Bragg grating.

`p.tooth(cell, period=...)` tiles a small PCell along the route. Use it
for grating teeth, periodic claddings, photonic-crystal slabs, or
periodic electrical contacts. The cell's local +x aligns with the path
tangent, so a single 1-period unit cell is enough.
"""
import lumicron as lm
import lumicron_pdk as lpdk

LAYER = lpdk.LayerTable(
    SILC = lpdk.Layer(9, 0, color="#008000", min_width=0.2, min_radius=5.0),
)


@lm.pcell
def BraggTooth(width: float = 1.5, length: float = 0.16):
    """One tooth of a distributed Bragg grating — a wider rectangle."""
    c = lm.CELL("BraggTooth")
    rect = lm.Rectangle(x_dim=length, y_dim=width, layer=LAYER.SILC)
    h = c.add(rect)
    c.Place(h).at((0, 0))
    return c


@lpdk.route_profile(port_width=0.5, radius=20.0, radius_min=10.0)
def silc_bragg(p):
    """Silicon strip with a 320 nm-period Bragg grating overlaid."""
    p.layer(LAYER.SILC, width=p.port_width)
    # Period = 320 nm; tile only on straight segments (span_bends=False)
    # since the wide teeth would distort an arc.
    p.tooth(BraggTooth(width=1.5, length=0.16),
            period=0.32, span_bends=False)


@lm.pcell
def BraggDemo():
    c = lm.CELL("BraggDemo")
    c.add(lm.PORT("in", position=(0, 0), direction=0,
                  route_profile=silc_bragg))
    c.add(lm.PORT("out", position=(200, 0), direction=180,
                  route_profile=silc_bragg))
    c.Route(c.ports["in"], c.ports["out"])
    return c


if __name__ == "__main__":
    BraggDemo().to_gds("bragg_demo.gds")
