"""Cookbook: a tight bundle fanned out to a wider port pitch.

A 4-channel bundle entering at 10 µm pitch and leaving at 50 µm. The
auto-fanout absorbs the pitch difference into a U-shape on each side;
the spine in between stays parallel and tight.
"""
import lumicron as lm
import lumicron.pdks.elyon_demo.all as pdk


@lm.pcell
def FanoutBus(
    n: int = 4,
    pitch_in: float = 10.0,
    pitch_out: float = 50.0,
    span: float = 600.0,
):
    c = lm.CELL("FanoutBus")

    for i in range(n):
        c.add(lm.PORT(
            f"in{i}", position=(0, i * pitch_in), direction=0,
            route_profile=pdk.RP.silc_strip,
        ))
        c.add(lm.PORT(
            f"out{i}", position=(span, i * pitch_out), direction=180,
            route_profile=pdk.RP.silc_strip,
        ))

    a = [c.ports[f"in{i}"] for i in range(n)]
    b = [c.ports[f"out{i}"] for i in range(n)]
    c.Route(a, spacing=pitch_in, radius=15).to(b)
    return c


if __name__ == "__main__":
    FanoutBus().to_gds("fanout_bus.gds")
