Exemple #1
0
def test_area():
    c = gdstk.Cell("c_area")
    c.add(gdstk.rectangle((0, 0), (1, 1), layer=0))
    c.add(gdstk.rectangle((0, 0), (1, 1), layer=1))
    c.add(gdstk.rectangle((1, 1), (2, 2), layer=1))
    c.add(gdstk.rectangle((1, 1), (2, 2), datatype=2))
    assert c.area() == 4.0
    assert c.area(True) == {(0, 0): 1.0, (1, 0): 2.0, (0, 2): 1}
Exemple #2
0
def test_notempty():
    name = "ca_notempty"
    c = gdstk.Cell(name)
    ref = gdstk.Reference(c, (1, -1), numpy.pi / 2, 2, True, 2, 3, (3, 2))
    ref.origin = (0, 0)
    c.add(gdstk.rectangle((0, 0), (1, 2), 2, 3))
    assert_close(ref.bounding_box(), ((0, 0), (8, 5)))
    assert_same_shape(
        [gdstk.rectangle((0, 0), (8, 2)), gdstk.rectangle((0, 3), (8, 5))],
        gdstk.Cell("TMP").add(ref).flatten().polygons,
    )
Exemple #3
0
def grating(period,
            fill_frac=0.5,
            length=20,
            width=25,
            layer=0,
            datatype=0,
            cell_name="Grating"):
    """
    Straight grating:

    Args:
        period: Grating period.
        fill_frac: Filling fraction of the teeth (wrt period).
        length: Length of the grating.
        width: Width of the grating.
        layer: GDSII layer number
        datatype: GDSII data type number

    Return:
        gdstk.Cell
    """
    result = gdstk.Cell(cell_name)
    x = width / 2
    w = period * fill_frac
    result.add(
        gdstk.rectangle((-x, y * period), (x, y * period + w),
                        layer=layer,
                        datatype=datatype)
        for y in range(int(length / period)))
    return result
Exemple #4
0
def test_copy_transform(proof_cells):
    ref_cell1 = gdstk.Cell("Reference 1")
    ref_cell1.add(*gdstk.text("F.", 10, (0, 0)))
    ref_cell1.add(gdstk.Label("LaBeL", (2.4, 8.7), "s"))
    ref_cell1.add(
        gdstk.FlexPath(8 + 4j, 1, simple_path=True,
                       layer=3).arc(2, 0, numpy.pi / 2))
    ref_cell1.add(
        gdstk.RobustPath(7.5 + 7j, 1, simple_path=True,
                         layer=4).bezier([-2 + 1j, -2 + 3j, 4j, 6j, -3 + 6j],
                                         relative=True))

    ref_cell2 = gdstk.Cell("Reference 2")
    ref_cell2.add(*gdstk.text("^", 10, (0, 5), layer=1))
    ref_cell2.add(gdstk.Reference(ref_cell1))

    cell = gdstk.Cell("Original cell")
    cell.add(gdstk.rectangle((-1, -0.5), (1, 0.5), layer=2))
    cell.add(gdstk.Reference(ref_cell2))
    cell.add(gdstk.Reference(ref_cell1, (10, 7), numpy.pi / 4, 0.5, True))
    cell.add(
        gdstk.Reference(ref_cell1, (-7, 15), -numpy.pi / 3, 0.5, True, 3, 2,
                        (5, 4)))
    cell.add(
        gdstk.Reference(ref_cell2, (-7, 23), numpy.pi / 3, 0.5, True, 3, 2,
                        (5, 8)))
    cell_copy = cell.copy("Cell.copy", (-10, -10), numpy.pi / 2, 2,
                          True).flatten()
    for path in cell_copy.paths:
        cell_copy.add(*path.to_polygons())
    assert_same_shape(proof_cells["Cell.copy"].polygons, cell_copy.polygons)
Exemple #5
0
def test_filter():
    polys = [
        gdstk.rectangle((0, 0), (1, 1), layer=l, datatype=t)
        for t in range(3)
        for l in range(3)
    ]
    labels = [
        gdstk.Label("FILTER", (1, 1), layer=l, texttype=t)
        for t in range(3)
        for l in range(3)
    ]
    paths = [
        gdstk.FlexPath([0j, 1j], [0.1, 0.1, 0.1], 0.5, layer=[0, 1, 2], datatype=t)
        for t in range(3)
    ] + [
        gdstk.RobustPath(0j, [0.1, 0.1], 0.5, layer=[1, 2], datatype=t)
        for t in range(3)
    ]
    layers = [1, 2]
    types = [0]
    for op, test in [
        ("and", lambda a, b: a and b),
        ("or", lambda a, b: a or b),
        ("xor", lambda a, b: (a and not b) or (b and not a)),
        ("nand", lambda a, b: not (a and b)),
        ("nor", lambda a, b: not (a or b)),
        ("nxor", lambda a, b: not ((a and not b) or (b and not a))),
    ]:
        path_results = [
            [test(a in layers, b in types) for a, b in zip(path.layers, path.datatypes)]
            for path in paths
        ]
        cell = gdstk.Cell(op)
        cell.add(*polys, *labels, *paths)
        cell.filter(layers, types, op)

        cell_polys = cell.polygons
        for poly in polys:
            if test(poly.layer in layers, poly.datatype in types):
                assert poly not in cell_polys
            else:
                assert poly in cell_polys
        cell_labels = cell.labels
        for label in labels:
            if test(label.layer in layers, label.texttype in types):
                assert label not in cell_labels
            else:
                assert label in cell_labels
        cell_paths = cell.paths
        for path, results in zip(paths, path_results):
            if all(results):
                assert path not in cell_paths
            else:
                assert path in cell_paths
                assert len(path.layers) == len(results) - sum(results)
                assert all(
                    not test(a in layers, b in types)
                    for a, b in zip(path.layers, path.datatypes)
                )
Exemple #6
0
def bench_gdstk():
    cell = gdstk.Cell("MAIN")
    for i in range(10000):
        cell.add(gdstk.rectangle((i, 0), (i + 1, 1)))
    name = pathlib.Path(tempfile.gettempdir()) / "gsdtk.gds"
    lib = gdstk.Library()
    lib.add(cell)
    lib.write_gds(name)
Exemple #7
0
def inside_example():
    rect = gdstk.rectangle((0, 0), (1, 1))
    print(gdstk.inside([(0.5, 0.5), (2, 2)], rect))
    print(gdstk.inside([(0.5, 0.5), (2, 2)], rect, "any"))
    print(gdstk.inside([(0.5, 0.5), (2, 2)], rect, "all"))
    # Point groups
    print(
        gdstk.inside([[(0.5, 0.5), (2, 2)], [(0, 0), (1, 1)], [(2, 2),
                                                               (3, 3)]], rect))
Exemple #8
0
def inside_example():
    rect = gdstk.rectangle((0, 0), (1, 1))
    assert gdstk.inside([(0.5, 0.5), (2, 2)], rect) == (True, False)
    assert gdstk.inside([(0.5, 0.5), (2, 2)], rect, "any") == (True, )
    assert gdstk.inside([(0.5, 0.5), (2, 2)], rect, "all") == (False, )
    # Point groups
    assert gdstk.inside([[(0.5, 0.5), (2, 2)], [(0, 0),
                                                (1, 1)], [(2, 2), (3, 3)]],
                        rect) == (True, True, False)
Exemple #9
0
def bench_gdstk():
    r = gdstk.rectangle((0, 0), (20, 10))
    pts = [[(1, 1), (-1, -1)], [(2, 2), (-2, 2), (2, -2)], [(5, 5), (10, 5)],
           [(-1, -1), (-2, -2)], [(2, 3)]]
    assert gdstk.inside(pts[0], r) == (True, False)
    assert gdstk.inside(pts[1], r) == (True, False, False)
    assert gdstk.inside(pts[1], r, "any") == (True, )
    assert gdstk.inside(pts[1], r, "all") == (False, )
    assert gdstk.inside(pts[4], r) == (True, )
    assert gdstk.inside(pts, r, "any") == (True, True, True, False, True)
    assert gdstk.inside(pts, r, "all") == (False, False, True, False, True)
Exemple #10
0
def make_first_lib(filename):
    lib = gdstk.Library("First")
    main = lib.new_cell("Main")
    main.add(*gdstk.text("First library", 10, (0, 0)))
    ref1 = lib.new_cell("Square")
    ref1.add(gdstk.rectangle((-15, 0), (-5, 10)))
    main.add(gdstk.Reference(ref1))
    ref2 = lib.new_cell("Circle")
    ref2.add(gdstk.ellipse((0, 0), 4))
    ref1.add(gdstk.Reference(ref2, (-10, 5)))
    lib.write_gds(filename)
Exemple #11
0
def test_frozen_gds_with_cell_array_has_constant_hash(tmpdir):
    fn1 = str(tmpdir.join("freezea1.gds"))
    fn2 = str(tmpdir.join("freezea2.gds"))
    frozen_date = datetime(1988, 8, 28)
    lib = gdstk.Library(name="Elsa")
    cell = gdstk.Cell(name="Anna")
    cell.add(gdstk.rectangle((0, 0), (100, 1000)))
    cell2 = gdstk.Cell(name="Olaf")
    cell2.add(gdstk.rectangle((0, 0), (50, 100)))
    cell_array = gdstk.Reference(cell2,
                                 columns=5,
                                 rows=2,
                                 spacing=(60, 120),
                                 origin=(1000, 0))
    cell.add(cell_array)
    lib.add(cell)
    lib.write_gds(fn1, timestamp=frozen_date)
    hash1 = hash_file(fn1)
    lib.write_gds(fn2, timestamp=frozen_date)
    hash2 = hash_file(fn2)
    assert hash1 == hash2
Exemple #12
0
def test_frozen_gds_with_cell_has_constant_hash(tmpdir):
    fn1 = str(tmpdir.join("freezec1.gds"))
    fn2 = str(tmpdir.join("freezec2.gds"))
    frozen_date = datetime(1988, 8, 28)
    lib = gdstk.Library(name="Elsa")
    cell = gdstk.Cell(name="Anna")
    cell.add(gdstk.rectangle((0, 0), (100, 1000)))
    lib.add(cell)
    lib.write_gds(fn1, timestamp=frozen_date)
    hash1 = hash_file(fn1)
    lib.write_gds(fn2, timestamp=frozen_date)
    hash2 = hash_file(fn2)
    assert hash1 == hash2
Exemple #13
0
def bounding_box_image():
    polygons = gdstk.text("F", 10, (0, 0))
    f_cell = gdstk.Cell("F_CELL")
    f_cell.add(*polygons)
    array_ref = gdstk.Reference(f_cell,
                                rotation=numpy.pi / 4,
                                columns=3,
                                rows=2,
                                spacing=(8, 10))
    bbox = array_ref.bounding_box()
    # print(bbox)
    polygon_bb = gdstk.rectangle(*bbox, datatype=1)
    return gdstk.Cell("bounding_box").add(array_ref, polygon_bb)
Exemple #14
0
def read_rawcells_example():
    cell1 = gdstk.Cell("CELL_1")
    cell1.add(gdstk.rectangle((0, 0), (2, 1)))
    cell2 = gdstk.Cell("CELL_2")
    cell2.add(gdstk.Reference(cell1, (-1, 0)))
    library = gdstk.Library()
    library.add(cell1, cell2)
    library.write_gds("test.gds")
    raw_cells = gdstk.read_rawcells("test.gds")
    print(raw_cells.keys())
    print(len(raw_cells["CELL_1"].dependencies(True)))
    print(len(raw_cells["CELL_2"].dependencies(True)))
    deps = raw_cells["CELL_2"].dependencies(True)
    print(deps[0] is raw_cells["CELL_1"])
Exemple #15
0
def read_rawcells_example():
    cell1 = gdstk.Cell("CELL_1")
    cell1.add(gdstk.rectangle((0, 0), (2, 1)))
    cell2 = gdstk.Cell("CELL_2")
    cell2.add(gdstk.Reference(cell1, (-1, 0)))
    library = gdstk.Library()
    library.add(cell1, cell2)
    library.write_gds("test.gds")
    raw_cells = gdstk.read_rawcells("test.gds")
    assert tuple(sorted(raw_cells.keys())) == ("CELL_1", "CELL_2")
    assert len(raw_cells["CELL_1"].dependencies(True)) == 0
    assert len(raw_cells["CELL_2"].dependencies(True)) == 1
    deps = raw_cells["CELL_2"].dependencies(True)
    assert deps[0] is raw_cells["CELL_1"]
Exemple #16
0
def bench_gdstk(output=None):
    c1 = gdstk.Cell("REF")
    c1.add(gdstk.rectangle((0, 0), (10, 10)))
    c1.add(
        gdstk.FlexPath([(0, 0), (10, 0), (10, 10), (0, 10)], [0.1, 0.1],
                       0.3,
                       layer=1))
    c1.add(gdstk.Label("Label", (5, 5), anchor='o'))
    c2 = gdstk.Cell("MAIN")
    c2.add(gdstk.Reference(c1, columns=3, rows=2, spacing=(20, 20)))
    c2.flatten()
    c1.remove(*c1.polygons, *c1.paths, *c1.labels)
    if output:
        c2.write_svg(output, 10)
Exemple #17
0
def test_inside():
    ring = gdstk.ellipse((0, 0), 1, inner_radius=0.5, tolerance=1e-3)
    circle = gdstk.ellipse((0, 0), 0.5, tolerance=1e-3)

    points = [(0, 0), (0.2, 0), (-0.1, -0.8), (0.9, 0.7), (-0.4, 0.4)]
    truth_ring = tuple([0.25 <= p[0]**2 + p[1]**2 <= 1 for p in points])
    truth_circle = tuple([p[0]**2 + p[1]**2 <= 0.25 for p in points])

    assert ring.contain(*points) == truth_ring
    assert ring.contain_all(*points) == all(truth_ring)
    assert ring.contain_any(*points) == any(truth_ring)

    assert circle.contain(*points) == truth_circle
    assert circle.contain_all(*points) == all(truth_circle)
    assert circle.contain_any(*points) == any(truth_circle)

    assert gdstk.inside(points, [ring, circle]) == tuple(
        [r or c for r, c in zip(truth_ring, truth_circle)])
    assert gdstk.all_inside(points, [ring, circle]) == all(
        [r or c for r, c in zip(truth_ring, truth_circle)])
    assert gdstk.any_inside(points, [ring, circle]) == any(
        [r or c for r, c in zip(truth_ring, truth_circle)])

    polys = [
        gdstk.rectangle((0, 0), (10, 10)),
        gdstk.rectangle((10, 0), (20, 10))
    ]
    for pts, _any, _all in (
        ([(1, 1), (-1, -1)], True, False),
        ([(2, 2), (-2, 2), (2, -2)], True, False),
        ([(5, 5), (10, 5)], True, True),
        ([(-1, -1), (-2, -2)], False, False),
        ([(2, 3)], True, True),
    ):
        assert gdstk.any_inside(pts, polys) == _any
        assert gdstk.all_inside(pts, polys) == _all
Exemple #18
0
def bench_gdstk(output=None):
    p = gdstk.Polygon([(0, 0), (1, 0), (0, 1)])
    fp = gdstk.FlexPath([(0, 0), (1, 0), (0.5, -0.5)], 0.1, ends="round")
    c1 = gdstk.Cell("REF")
    c1.add(p, fp)
    r = gdstk.Reference(c1,
                        columns=3,
                        rows=2,
                        spacing=(2, 2),
                        rotation=30 * numpy.pi / 180)
    c2 = gdstk.Cell("MAIN")
    c2.add(r)
    bb = c2.bounding_box()
    if output:
        c2.add(gdstk.rectangle(*bb, layer=1))
        c2.write_svg(output, 100)
Exemple #19
0
def bounding_box_image():
    polygons = gdstk.text("F", 10, (0, 0))
    f_cell = gdstk.Cell("F_CELL")
    f_cell.add(*polygons)
    array_ref = gdstk.Reference(f_cell,
                                rotation=numpy.pi / 4,
                                columns=3,
                                rows=2,
                                spacing=(8, 10))
    bbox = array_ref.bounding_box()
    assert bbox == (
        (-12.816310409006173, 1.7677669529663689),
        (11.313708498984761, 27.66555281392367),
    )
    polygon_bb = gdstk.rectangle(*bbox, datatype=1)
    return gdstk.Cell("bounding_box").add(array_ref, polygon_bb)
Exemple #20
0
def bounding_box_image():
    polygons = gdstk.text("F", 10, (0, 0))
    f_cell = gdstk.Cell("F_CELL")
    f_cell.add(*polygons)
    array_ref = gdstk.Reference(f_cell,
                                rotation=numpy.pi / 4,
                                columns=3,
                                rows=2,
                                spacing=(8, 10))
    path = gdstk.FlexPath([(-5, 0), (0, -5), (5, 0)], 1, gdsii_path=True)
    main_cell = gdstk.Cell("MAIN")
    main_cell.add(array_ref, path)
    bbox = main_cell.bounding_box()
    # print(bbox)
    polygon_bb = gdstk.rectangle(*bbox, datatype=1)
    main_cell.name = "bounding_box"
    return main_cell.add(polygon_bb)
Exemple #21
0
def test_contain():
    r = gdstk.rectangle((0, 0), (20, 10))
    pts = [
        [(1, 1), (-1, -1)],
        [(2, 2), (-2, 2), (2, -2)],
        [(5, 5), (10, 5)],
        [(-1, -1), (-2, -2)],
    ]
    assert r.contain(pts[0][0]) == True
    assert r.contain(pts[0][1]) == False
    assert r.contain(*pts[0]) == (True, False)
    assert r.contain(*pts[1]) == (True, False, False)
    assert r.contain_any(*pts[1]) == True
    assert r.contain_all(*pts[1]) == False
    assert r.contain_any(*pts[2]) == True
    assert r.contain_all(*pts[2]) == True
    assert r.contain_any(*pts[3]) == False
    assert r.contain_all(*pts[3]) == False
Exemple #22
0
def bounding_box_image():
    polygons = gdstk.text("F", 10, (0, 0))
    f_cell = gdstk.Cell("F_CELL")
    f_cell.add(*polygons)
    array_ref = gdstk.Reference(
        f_cell, rotation=numpy.pi / 4, columns=3, rows=2, spacing=(8, 10)
    )
    path = gdstk.FlexPath([(-5, 0), (0, -5), (5, 0)], 1, simple_path=True)
    main_cell = gdstk.Cell("MAIN")
    main_cell.add(array_ref, path)
    bbox = main_cell.bounding_box()
    assert bbox == (
        (-12.816310409006173, -5.707106781186548),
        (11.313708498984761, 27.66555281392367),
    )
    polygon_bb = gdstk.rectangle(*bbox, datatype=1)
    main_cell.name = "bounding_box"
    return main_cell.add(polygon_bb)
Exemple #23
0
def test_bb_polygon_repetition():
    pol = gdstk.rectangle((0, 0), (1, 1))
    pol.repetition = gdstk.Repetition(x_offsets=(1, 3, -2))
    c_pol = gdstk.Cell("C")
    c_pol.add(pol)
    assert_close(c_pol.bounding_box(), ((-2, 0), (4, 1)))
    ref = gdstk.Reference(c_pol)
    ref.repetition = gdstk.Repetition(y_offsets=(-1, 2, -4))
    c_ref = gdstk.Cell("D")
    c_ref.add(ref)
    assert_close(c_ref.bounding_box(), ((-2, -4), (4, 3)))
    ref.rotation = numpy.pi / 4
    a = (-2 + 1j) * numpy.exp(0.25j * numpy.pi)
    b = (-2 + 0j) * numpy.exp(0.25j * numpy.pi)
    c = (4 + 0j) * numpy.exp(0.25j * numpy.pi)
    d = (4 + 1j) * numpy.exp(0.25j * numpy.pi)
    assert_close(c_ref.bounding_box(),
                 ((a.real, b.imag - 4), (c.real, d.imag + 2)))
Exemple #24
0
def test_read_gds_missing_refs(tmpdir):
    c1 = gdstk.Cell("c1")
    c1.add(gdstk.rectangle((0, -1), (1, 2), 2, 4))

    r1 = gdstk.Reference(c1, magnification=2)
    c2 = gdstk.Cell("c2")
    c2.add(r1)

    lib = gdstk.Library()
    lib.add(c2)

    fname = str(tmpdir.join("test_missing_refs.gds"))
    lib.write_gds(fname)

    with pytest.warns(RuntimeWarning):
        lib2 = gdstk.read_gds(fname)

    assert len(lib2.cells) == 1
    assert lib2.cells[0].name == "c2"
Exemple #25
0
def bench_gdstk(output=None):
    p = gdstk.Polygon([(0, 0), (1, 0), (0, 1)])
    fp = gdstk.FlexPath([(-1, 0.5), (1, 0), (0.5, -0.5)], [0.1, 0.1], 0.3, ends="round")
    rp = gdstk.RobustPath((0, 0), [0.1, 0.1], 0.3).interpolation(
        [(2, 0.5), (2, 1), (-1, 4)]
    )
    c1 = gdstk.Cell("REF")
    c1.add(p, fp, rp)
    r1 = gdstk.Reference(
        c1, columns=3, rows=2, spacing=(2, 2), rotation=30 * numpy.pi / 180
    )
    r2 = gdstk.Reference(c1, origin=(8, 0), columns=5, rows=4, spacing=(2, 2))
    r3 = gdstk.Reference(c1, origin=(9, 1), columns=4, rows=3, spacing=(2, 2))
    c2 = gdstk.Cell("MAIN")
    c2.add(r1, r2, r3)
    bb = c2.bounding_box()
    if output:
        c2.add(gdstk.rectangle(*bb, layer=1))
        c2.write_svg(output, 100)
Exemple #26
0
def test_copy():
    p = gdstk.Polygon(((0, 0), (1, 0), (0, 1)))
    lbl = gdstk.Label("label", (0, 0))
    cref = gdstk.Cell("ref").add(gdstk.rectangle((-1, -1), (-2, -2)))
    ref = gdstk.Reference(cref)
    cell = gdstk.Cell("original")
    cell.add(p, lbl, ref)
    shallow_copy = cell.copy("copy_0", deep_copy=False)
    assert len(shallow_copy.polygons) == len(cell.polygons)
    assert p in shallow_copy.polygons
    assert len(shallow_copy.labels) == len(cell.labels)
    assert lbl in shallow_copy.labels
    assert len(shallow_copy.references) == len(cell.references)
    assert ref in shallow_copy.references
    deep_copy = cell.copy("copy_1")
    assert len(deep_copy.polygons) == len(cell.polygons)
    assert p not in deep_copy.polygons
    assert len(deep_copy.labels) == len(cell.labels)
    assert lbl not in deep_copy.labels
    assert len(deep_copy.references) == len(cell.references)
    assert ref not in deep_copy.references
    assert deep_copy.references[0].cell is ref.cell
Exemple #27
0
def init_image():
    frame = gdstk.rectangle((-2, -1), (2, 1), datatype=1)
    label_o = gdstk.Label("Center", (0, 0), rotation=numpy.pi / 6)
    label_n = gdstk.Label("North", (0, 1), "n")
    label_s = gdstk.Label("South", (0, -1), "s")
    label_e = gdstk.Label("East", (2, 0), "e")
    label_w = gdstk.Label("West", (-2, 0), "w")
    label_ne = gdstk.Label("Northeast", (2, 1), "ne")
    label_se = gdstk.Label("Southeast", (2, -1), "se")
    label_nw = gdstk.Label("Northwest", (-2, 1), "nw")
    label_sw = gdstk.Label("Southwest", (-2, -1), "sw")
    return gdstk.Cell("init").add(
        frame,
        label_o,
        label_n,
        label_s,
        label_e,
        label_w,
        label_ne,
        label_se,
        label_nw,
        label_sw,
    )
Exemple #28
0
def sample_library():
    lib = gdstk.Library("lib", unit=2e-3, precision=1e-5)
    c1 = gdstk.Cell("gl_rw_gds_1")
    c1.add(gdstk.rectangle((0, -1), (1, 2), 2, 4))
    c1.add(gdstk.Label("label", (1, -1), "w", 10, 1.5, True, 5, 6))
    c2 = gdstk.Cell("gl_rw_gds_2")
    c2.add(gdstk.ellipse((0, 0), 1))
    c3 = gdstk.Cell("gl_rw_gds_3")
    c3.add(gdstk.Reference(c1, (0, 1), -90, 2, True))
    c4 = gdstk.Cell("gl_rw_gds_4")
    c4.add(
        gdstk.Reference(
            c2,
            (-1, -2),
            columns=2,
            rows=3,
            spacing=(1, 4),
            rotation=numpy.pi,
            magnification=0.5,
            x_reflection=True,
        ))
    lib.add(c1, c2, c3, c4)
    return lib
Exemple #29
0
def make_proof_lib():
    lib = gdstk.Library("Test Library", unit=1e-6, precision=1e-12)

    cell = lib.new_cell("Polygon.fillet")
    p1 = gdstk.Polygon([(0, 0), (1.2, 0), (1.2, 0.3), (1, 0.3), (1.5, 1),
                        (0, 1.5)])
    p2 = p1.copy().translate(2, 0)
    cell.add(p1.fillet(0.3, tolerance=1e-3))
    cell.add(p2.fillet([0.3, 0, 0.1, 0, 0.5, 10], tolerance=1e-3))

    for scale_width in [True, False]:
        cell = lib.new_cell(f"FlexPath: scale_width {scale_width}")
        path0 = gdstk.FlexPath(
            (0j, 1j, 0.5 + 1j),
            [0.1, 0.2],
            0.3,
            tolerance=1e-4,
            scale_width=scale_width,
        )
        path0.turn(0.4, -numpy.pi, [0.2, 0.1]).segment((-0.2, 0),
                                                       relative=True)
        path1 = path0.copy().mirror((1.5, 0))
        path1.set_layers(1, 1)
        path2 = path0.copy().mirror((1.5, 0), (1.5, 1))
        path2.set_layers(2, 2)
        path3 = path0.copy().scale(2, (3, 0))
        path3.set_layers(3, 3)
        path4 = path0.copy().scale(-2, (-1, 0))
        path4.set_layers(4, 4)
        path5 = path0.copy().rotate(numpy.pi / 2, (2, 1)).translate(0.2, -0.3)
        path5.set_layers(5, 5)
        cell.add(path0, path1, path2, path3, path4, path5)

    for scale_width in [True, False]:
        cell = lib.new_cell(f"RobustPath: scale_width {scale_width}")
        path0 = gdstk.RobustPath(
            0j,
            [0.1, 0.2],
            0.3,
            tolerance=1e-4,
            scale_width=scale_width,
        )
        path0.vertical(1).horizontal(0.5).turn(0.4, -numpy.pi,
                                               [0.2, 0.1]).segment(
                                                   (-0.2, 0), relative=True)
        path1 = path0.copy().mirror((1.5, 0))
        path1.set_layers(1, 1)
        path2 = path0.copy().mirror((1.5, 0), (1.5, 1))
        path2.set_layers(2, 2)
        path3 = path0.copy().scale(2, (3, 0))
        path3.set_layers(3, 3)
        path4 = path0.copy().scale(-2, (-1, 0))
        path4.set_layers(4, 4)
        path5 = path0.copy().rotate(numpy.pi / 2, (2, 1)).translate(0.2, -0.3)
        path5.set_layers(5, 5)
        cell.add(path0, path1, path2, path3, path4, path5)

    ref_cell1 = gdstk.Cell("Reference 1")
    ref_cell1.add(*gdstk.text("F.", 10, (0, 0)))
    ref_cell1.add(gdstk.Label("LaBeL", (2.4, 8.7), "s"))
    ref_cell1.add(gdstk.FlexPath(8 + 4j, 1, layer=3).arc(2, 0, numpy.pi / 2))
    ref_cell1.add(
        gdstk.RobustPath(7.5 + 7j, 1,
                         layer=4).bezier([-2 + 1j, -2 + 3j, 4j, 6j, -3 + 6j],
                                         relative=True))

    ref_cell2 = gdstk.Cell("Reference 2")
    ref_cell2.add(*gdstk.text("^", 10, (0, 5), layer=1))
    ref_cell2.add(gdstk.Reference(ref_cell1))

    cell = gdstk.Cell("Original cell")
    cell.add(gdstk.rectangle((-1, -0.5), (1, 0.5), layer=2))
    cell.add(gdstk.Reference(ref_cell2))
    cell.add(gdstk.Reference(ref_cell1, (10, 7), numpy.pi / 4, 0.5, True))
    cell.add(
        gdstk.Reference(ref_cell1, (-7, 15), -numpy.pi / 3, 0.5, True, 3, 2,
                        (5, 4)))
    cell.add(
        gdstk.Reference(ref_cell2, (-7, 23), numpy.pi / 3, 0.5, True, 3, 2,
                        (5, 8)))

    cell_copy = cell.copy("Cell.copy", (-10, -10), numpy.pi / 2, 2,
                          True).flatten()
    lib.add(cell_copy)

    gds_outfile = pathlib.Path(__file__).parent / "proof_lib.gds"
    if gds_outfile.exists():
        print(f"Test library {str(gds_outfile)} already exists.")
    else:
        lib.write_gds(gds_outfile)
        print(f"Test library saved as {str(gds_outfile)}.")

    oas_outfile = pathlib.Path(__file__).parent / "proof_lib.oas"
    if oas_outfile.exists():
        print(f"Test library {str(oas_outfile)} already exists.")
    else:
        lib.write_oas(oas_outfile)
        print(f"Test library saved as {str(oas_outfile)}.")
Exemple #30
0
def memory_benchmark():
    proc = psutil.Process()
    total = 100000
    print(f"\nMemory usage per object for {total} objects:\n")

    def print_row(*vals, hsep=False):
        columns = [20, 16, 16, 9]
        print(
            "|",
            vals[0].ljust(columns[0]),
            "|",
            " | ".join(v.center(c) for v, c in zip(vals[1:], columns[1:])),
            "|",
        )
        if hsep:
            print(
                "|",
                ":" + "-" * (columns[0] - 1),
                "|",
                " | ".join(":" + "-" * (c - 2) + ":" for c in columns[1:]),
                "|",
            )

    print_row(
        "Object",
        "Gdspy " + gdspy.__version__,
        "Gdstk " + gdstk.__version__,
        "Reduction",
        hsep=True,
    )

    def mem_test(func):
        start_mem = proc.memory_info()
        r = [func(i) for i in range(total)]
        end_mem = proc.memory_info()
        return (end_mem.vms - start_mem.vms) / total, r

    data = []
    gdspy_cell = gdspy.Cell("TEMP", exclude_from_current=True)
    gdstk_cell = gdstk.Cell("TEMP")
    for obj, gdspy_func, gdstk_func in [
        (
            "Rectangle",
            lambda i: gdspy.Rectangle((i, i), (i + 1, i + 1)),
            lambda i: gdstk.rectangle((i, i), (i + 1, i + 1)),
        ),
        (
            "Circle (r = 10)",
            lambda i: gdspy.Round((0, 10 * i), 10),
            lambda i: gdstk.ellipse((0, 10 * i), 10),
        ),
        (
            "FlexPath segment",
            lambda i: gdspy.FlexPath([(i, i + 1), (i + 1, i)], 0.1),
            lambda i: gdstk.FlexPath([(i, i + 1), (i + 1, i)], 0.1),
        ),
        (
            "FlexPath arc",
            lambda i: gdspy.FlexPath([(10 * i, 0)], 0.1).arc(10, 0, numpy.pi),
            lambda i: gdstk.FlexPath([(10 * i, 0)], 0.1).arc(10, 0, numpy.pi),
        ),
        (
            "RobustPath segment",
            lambda i: gdspy.RobustPath((i, i + 1), 0.1).segment((i + 1, i)),
            lambda i: gdstk.RobustPath((i, i + 1), 0.1).segment((i + 1, i)),
        ),
        (
            "RobustPath arc",
            lambda i: gdspy.RobustPath((10 * i, 0), 0.1).arc(10, 0, numpy.pi),
            lambda i: gdstk.RobustPath((10 * i, 0), 0.1).arc(10, 0, numpy.pi),
        ),
        (
            "Label",
            lambda i: gdspy.Label(str(i), (i, i)),
            lambda i: gdstk.Label(str(i), (i, i)),
        ),
        (
            "Reference",
            lambda i: gdspy.CellReference(gdspy_cell, (0, 0)),
            lambda i: gdstk.Reference(gdstk_cell, (0, 0)),
        ),
        (
            "Reference (array)",
            lambda i: gdspy.CellArray(gdspy_cell, (0, 0), 1, 1, (0, 0)),
            lambda i: gdstk.Reference(gdstk_cell, (0, 0), rows=1, columns=1),
        ),
        (
            "Cell",
            lambda i: gdspy.Cell(str(i), exclude_from_current=True),
            lambda i: gdstk.Cell(str(i)),
        ),
    ]:
        gdspy_mem, gdspy_data = mem_test(gdspy_func)
        data.append(gdspy_data)
        gdstk_mem, gdstk_data = mem_test(gdstk_func)
        data.append(gdstk_data)
        print_row(
            obj,
            prefix_format(gdspy_mem, unit="B", base="bin"),
            prefix_format(gdstk_mem, unit="B", base="bin"),
            f"{100 - 100 * gdstk_mem / gdspy_mem:.0f}%",
        )