File size: 4,270 Bytes
34292e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3
"""klayout_drc.py: each check flags the shapes that break it, and only those.

Runs on the klayout Python module, and is skipped without it.
"""

import os
import sys
import tempfile
import unittest

sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "../util"))

try:
    import klayout.db as db
except ImportError:
    db = None

import klayout_drc
from klayout_drc import nm, projection, um


def write_gds(path, boxes):
    """boxes: (layer, datatype, x1, y1, x2, y2) in microns, in a hierarchy of
    a top cell and one child cell placed twice."""
    layout = db.Layout()
    layout.dbu = 0.001
    top = layout.create_cell("TOP")
    child = layout.create_cell("CHILD")
    for layer, datatype, x1, y1, x2, y2 in boxes:
        li = layout.layer(layer, datatype)
        top.shapes(li).insert(db.DBox(x1, y1, x2, y2))
    li = layout.layer(1, 0)
    child.shapes(li).insert(db.DBox(0, 0, 0.05, 1))  # narrow: one violation
    top.insert(db.DCellInstArray(child.cell_index(), db.DTrans(db.DVector(100, 0))))
    top.insert(db.DCellInstArray(child.cell_index(), db.DTrans(db.DVector(200, 0))))
    layout.write(path)


@unittest.skipIf(db is None, "the klayout Python module is not installed")
class TestKlayoutDrc(unittest.TestCase):
    def setUp(self):
        self.tmp = tempfile.TemporaryDirectory()
        self.gds = os.path.join(self.tmp.name, "in.gds")
        self.rdb = os.path.join(self.tmp.name, "out.lyrdb")
        write_gds(
            self.gds,
            [
                (1, 0, 0, 0, 0.08, 1),  # width 80 nm
                (1, 0, 1, 0, 1.2, 1),  # clean
                (1, 0, 1.25, 0, 1.45, 1),  # 50 nm from the one above
                (2, 0, 3, 0, 3.1, 0.1),  # via
                (1, 0, 2.99, -0.01, 3.11, 0.11),  # 10 nm enclosure
                (2, 0, 4, 0, 4.1, 0.1),  # via
                (1, 0, 3.9, -0.1, 4.2, 0.2),  # 100 nm enclosure
            ],
        )

    def tearDown(self):
        self.tmp.cleanup()

    def run_deck(self, body, mode="deep"):
        deck = klayout_drc.Deck(self.gds, self.rdb, "test", mode=mode)
        body(deck)
        deck.write()
        counts = {}
        for name, cat in deck.categories.items():
            counts[name] = cat.num_items()
        return counts

    def test_width_space_enclosing(self):
        def body(deck):
            m1 = deck.input(1, 0)
            via = deck.input(2, 0)
            m1.width(nm(100), projection).output("W", "width")
            m1.space(nm(100), projection).output("S", "space")
            m1.enclosing(via, nm(50), projection).output("E", "enclosure")

        counts = self.run_deck(body)
        # The child cell's narrow shape is reported once, in the child.
        self.assertEqual(counts, {"W": 2, "S": 1, "E": 4})

    def test_flat_reports_each_instance(self):
        def body(deck):
            deck.input(1, 0).width(nm(100), projection).output("W")

        self.assertEqual(self.run_deck(body, mode="flat"), {"W": 3})

    def test_universal_not_equal(self):
        def body(deck):
            m1 = deck.input(1, 0)
            m1.drc(klayout_drc.width(projection) != nm(200)).with_angle(90).output("X")

        # Every edge of a shape that is not 200 nm wide, both sides: the
        # 80 nm, 50 nm (twice in the child) and 300 nm wide shapes.
        counts = self.run_deck(body)
        self.assertGreater(counts["X"], 0)

    def test_area_and_units(self):
        def body(deck):
            self.assertEqual(deck.v(nm(80)), 80)
            self.assertEqual(deck.v(um(1)), 1000)
            self.assertEqual(deck.area(0.5), 500000)
            deck.input(1, 0).with_area(0, 0.1).output("A")

        # 0.08, 0.0144 and 0.09 um2 in the top cell, 0.05 um2 once in the child
        self.assertEqual(self.run_deck(body), {"A": 4})

    def test_corners_default_all(self):
        def body(deck):
            via = deck.input(2, 0)
            via.corners().output("ALL")
            via.corners((-180, 180)).output("RANGE")
            via.corners(-180).output("ONE")

        # Two vias in the top cell, four corners each; none of them -180.
        self.assertEqual(self.run_deck(body), {"ALL": 8, "RANGE": 8, "ONE": 0})


if __name__ == "__main__":
    unittest.main()