Reuse parametric building blocks
How to avoid “duplicate cell name” warnings utilizing the @hashme decorator
A parametric cell is obtained by creating a Cell inside a Python function which returns the variable the Cell has been assigned to.
# example created by Bright Photonics def parametric(a=1, b=2): with nd.Cell(name='new_cell') as C: # stuff cells are made off # use parameters a and/or b where needed return C parametric(a=5).put(0)
If this function is called more than once, and a fixed cell name is used to create the cell, Nazca will issue a “Duplicate cell name” warning and the cell name obtains an integer number suffix (cell counter) to avoid cell name conflicts; The renaming looks for example like “new_cell” and “new_cell_145”. After all, in GDS the cell name is its unique identifier and never should two cells with the same name be created in one design. The GDS that Nazca exports will be correct due to the renaming, but this warning can and should be fixed as best practice.
The Duplicate cell name warning can for one be fixed by sending a unique cell name to the function. However, this is not ideal in case the function is called twice with the same set of parameters (but distinct cell names), because a new cell is generated whose content is exactly the same as an existing cell. The GDS design is still correct, but larger than needed. Better is to reuse the existing identical copy and make use of cell instantiation. In Nazca this cell reuse is achieved by applying the @hashme decorator to the parametric cell. Hashme does introspection on the full function call and compresses the call in a hash. The hash is added as a suffix to the cell name. If hashme finds that a function call is a copy of an earlier one it returns the existing cell.
The hashme decorator is defined in the bb_util module and, therefor, accessed as bb_util.hashme. The first parameter in hashme is the cell basename and is mandatory. An exception will be thrown if it is omitted. The second and subsequent parameters are optional, and contain zero or more keyword names, as strings, of the function call that is being decorated. If a keyword is provided, its value will be added to the cell name with an underscore, like ‘_10’.
The below examples show a case without hashme (def BB1) and two cases with hashme. The last one (def BB3) includes a keyword. Check out the resulting cell names in the exported GDS. The names have been added as well as comments in the code.
# example created by Bright Photonics import nazca as nd # No hashme def BB1(L=10): with nd.Cell('cell1') as C: nd.strt(length=L).put() return C # use hashme @nd.bb_util.hashme('cell2') def BB2(L=10): with nd.Cell(hashme=True) as C: nd.strt(length=L).put() return C # use hashme and add parameter values to the cell name @nd.bb_util.hashme('cell3', 'L') def BB3(L=10): with nd.Cell(hashme=True) as C: nd.strt(length=L).put() return C BB1(10).put(0, 0) BB1(10).put(0, 5) BB1(20).put(0, 10) # cell names out: # 1. cell1 # 2. cell1_4 # 3. cell1_6 BB2(10).put(30, 0) BB2(10).put(30, 5) BB2(20).put(30, 10) # cell names out: # 1 .cell2_$2efc (reused) # 2. cell2_$f771 BB3(10).put(60, 0) BB3(10).put(60, 5) BB3(20).put(60, 10) # cell names out: # 1. cell3_10_$2efc (reused) # 2. cell3_20_$f771 nd.export_gds()