It looks like you're new here. If you want to get involved, click one of these buttons!
Hello,
Since I'm new to Klayout, I'm learning from YouTube. I made multiple attempts to create a rectangle array with a single column and linearly variable spacing. I can create a column of rectangles with identical spacing by using an array function. I need guidance from someone to create single column rectangles with linearly variable spacing.
Thanks in advance
Krish
Comments
Hi @Krishp
Try this out, it's a PCell that provides a variable that allows you to define custom spaced/rotated/mirrored/visibility for array elements, if such thing is still qualified as a array.
Usecase
Parameters
This provides a interface that allows you to use python syntax to define it's geo location.
WIDTHandHEIGHTprovides an access to current size settingROWandCOLas variable to aquire current element sequenceROWSandCOLSprovides an access to row counts and column countsXpos, provides an access to current element X locationYpos, provides an access to current element Y locationROT, provides an access to current element Rotation angleMIR, provides an access to current element Rotation angle, it should provide good enough degrees of freedom to play with.
The parameters are calculated in a order from top to bottom, so each field does not have access to it self and parameters with lower priority.
(Cannot do recursive calls, so invalid paramaters will be trimmed automatically when decleared)
but since this PCELL uses
evalsto achieve this function, dangerous things can happen if you tried to access or assigning variables that is not ment to be changed.Now variable evaluation is done using ASTEVAL to parse the command, reduce the chance of uintentional system distructive code from running in the cell.
Installation
To enable this you need to unzip the attached file and place the content
Lib_ArrayMagicinto you..\KLayout\pymacrofolder, for instance mine on a Win system is at :C:\Users\RawrRanger\KLayout\pymacrosAfter placing the file into
pymacrosfolder please restart you application and you should able to see there' a new Library installed and can be accessed from :ArrayMagicFunctionArraylayout examples will becoms a PCELL array once the library is installed correctly
For some special use cases and examples:
Dounat pattern filling
Width10Height10Row counts50Col counts50X position functionCOL * 20 - (COLS/2) * 20 + 10Y position functionROW * 20 - (ROWS/2) * 20 + 10Visibility function200 < (Xpos ** 2 + Ypos ** 2)** (0.5) < 480Hex type array
Width10Height10Row counts10Col counts10X position functionCOL * 20 + ( 10 if (ROW %2) == 0 else 0)Y position functionROW * 15Circular array
Width10Height10Row counts10Col counts1X position function50 * math.cos(2 * math.pi / ROWS * ROW)Y position function50 * math.sin(2 * math.pi / ROWS * ROW)Rotation function360 / ROWS * ROWRing type array
Width3Height1Row counts100Col counts4X position function(COL+1) * 25 * math.cos(2 * math.pi / ROWS * ROW + math.pi/3)Y position function(COL+1) * 20 * math.sin(2 * math.pi / ROWS * ROW)Rotation functionmath.atan(Ypos/Xpos) / math.pi * 180Visibility function(ROW % (COLS - COL)) ==0Polynomial array
Width30Height30~~~~Row counts1Col counts65X position functionCOL * 15 - (COLS/2) * 15Y position function(1e-5) * Xpos **3 + 0.005 * Xpos ** 2 + 0.1 * Xpos + 5Mirror functionXpos > 03x3 grouped Array
Width20Height10Row counts9Col counts9X position functionWIDTH * COL + (15 * int(COL/3))Y position functionHEIGHT * ROW + (15 * int(ROW/3))Mirror functionROW % 2 == 0Crossed box array
Width10Height10Row counts9Col counts9X position functionCOL * 20Y position functionROW * 20Visibility function(COL in [0, COLS-1]) or (ROW in [0, ROWS-1] or ROW==COL or (ROWS-1-ROW)==COL)The array is actually not drawn individually, instead is done by placing
PlaceHoldercells accroading to the scripted location.This approach allows you to do a cell replace of the
PlaceHoldercell with something useful and then don't forgot to useConvert to static cellfunction to make the cell replacement becomes a permanent change.If
Conver to static cellis not applied, the change will be revert everytimePCELLrefreshes it self, to find out more about this limitation please check these disscussion.PCell can't add instances from it's parent layout
https://www.klayout.de/forum/discussion/1976/placing-a-cell-using-a-pcell
PCell have no idea about it's location:
https://www.klayout.de/forum/discussion/660/how-to-get-set-the-instance-coords-within-a-pcell
Hey RawrRanger,
This is very cool, thanks for sharing!
Cheers,
Tomas
Hi @RawrRanger,
yes, this is definitely cool!
I will not need matplotlib anymore
A brief warning is in order I hope: If you use eval on a user string, the user string basically can do something malicious such as
os.system(...). So eventually this is a way to sneak in malicious code into a GDS file. This is not the only way to do that and one should always be careful with files from external sources, but I just wanted to point this out.KLayout provides a simple expression parser internally, which maybe is easier to use:
One advantage is that Expressions cannot access variables of your script.
However, it is not much safer as currently it has access to all Qt classes, including "QProcess" for example:
Just imagine what you can do with that ...
I think it is possible to create some kind of "safe mode", but there is always a tradeoff between security and functionality.
The expression syntax is described here: https://www.klayout.de/doc-qt5/about/expressions.html
There are some small differences. For example, "A**2" is written as "pow(A,2)".
Execution times are better with KLayout Expressions:
This takes 5.6s on my machine:
While the same with Expressions takes 2.3s:
Best regards,
Matthias
Hi @RawrRanger, Thank you very much for taking the time to help! Your examples were incredibly clear and made all the difference in solving my problem. I greatly appreciate your help and knowledge.
Hi Matthias
Thanks for the suggestion,
Indeed having code running through eval directy could cause bad things, i've tried Expression and generic builtoin function AST but alot of functions and syntactic syrup that we love becoms not avaliable.
Too keep the agility, I've switched to use ASTEVAL package, which seems to be able prevent most of the dangerous code from running through eval but also allows most of the basic math/list comprehension stuff.
The down side is the execution time of ASTEVAL is around 5 times slower than the fastest while doing a a 1000000 cycle sum test :