forked from SchusterLab/maskLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicrowaveLib.py
More file actions
1802 lines (1530 loc) · 76 KB
/
Copy pathmicrowaveLib.py
File metadata and controls
1802 lines (1530 loc) · 76 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 4 17:29:02 2019
@author: Sasha
Library for drawing standard microwave components (CPW parts, inductors, capacitors etc)
Only standard composite components (inductors, launchers) are included here- complicated / application specific composites
go in sub-libraries
"""
import maskLib.MaskLib as m
from dxfwrite import DXFEngine as dxf
from dxfwrite import const
from dxfwrite.entities import Polyline
from dxfwrite.vector2d import vadd, midpoint ,vsub, vector2angle, magnitude, distance
from dxfwrite.algebra import rotate_2d
from maskLib.Entities import SolidPline, SkewRect, CurveRect, RoundRect, InsideCurve, DogBone
from maskLib.utilities import kwargStrip, curveAB
from copy import deepcopy
from matplotlib.path import Path
from matplotlib.transforms import Bbox
import math
from copy import copy
import numpy as np
# ===============================================================================
# perforate the ground plane with a grid of squares, which avoid any polylines
# ===============================================================================
#TODO move to MaskLib
def waffle(chip, grid_x, grid_y=None,width=10,height=None,exclude=None,padx=0,pady=None,bleedRadius=1,layer='0'):
radius = max(int(bleedRadius),0)
if exclude is None:
exclude = ['FRAME']
else:
exclude.append('FRAME')
if grid_y is None:
grid_y = grid_x
if height is None:
height = width
if pady is None:
pady=padx
nx, ny = list(map(int, [(chip.width) / grid_x, (chip.height) / grid_y]))
occupied = [[False]*ny for i in range(nx)]
for i in range(nx):
occupied[i][0] = True
occupied[i][-1] = True
for i in range(ny):
occupied[0][i] = True
occupied[-1][i] = True
for e in chip.chipBlock.get_data():
if isinstance(e.__dxftags__()[0], Polyline):
if e.layer not in exclude:
o_x_list = []
o_y_list = []
plinePts = [v.__getitem__('location').__getitem__('xy') for v in e.__dxftags__()[0].get_data()]
plinePts.append(plinePts[0])
for p in plinePts:
o_x, o_y = list(map(int, (p[0] / grid_x, p[1] / grid_y)))
if 0 <= o_x < nx and 0 <= o_y < ny:
o_x_list.append(o_x)
o_y_list.append(o_y)
#this will however ignore a rectangle with corners outside the chip...
if o_x_list:
path = Path([[pt[0]/grid_x,pt[1]/grid_y] for pt in plinePts],closed=True)
for x in range(min(o_x_list)-1, max(o_x_list)+2):
for y in range(min(o_y_list)-1, max(o_y_list)+2):
try:
if path.contains_point([x+.5,y+.5]):
occupied[x][y]=True
elif path.intersects_bbox(Bbox.from_bounds(x,y,1.,1.),filled=True):
occupied[x][y]=True
except IndexError:
pass
second_pass = deepcopy(occupied)
for r in range(radius):
for i in range(nx):
for j in range(ny):
if occupied[i][j]:
for ip, jp in [(i+1,j), (i-1,j), (i,j+1), (i,j-1)]:
try:
second_pass[ip][jp] = True
except IndexError:
pass
occupied = deepcopy(second_pass)
for i in range(int(padx/grid_x),nx-int(padx/grid_x)):
for j in range(int(pady/grid_y),ny-int(pady/grid_y)):
if not second_pass[i][j]:
pos = i*grid_x + grid_x/2., j*grid_y + grid_y/2.
chip.add(dxf.rectangle(pos,width,height,bgcolor=chip.wafer.bg(layer),halign=const.CENTER,valign=const.MIDDLE,layer=layer) )
return chip
# ===============================================================================
# basic POSITIVE microstrip function definitions
# ===============================================================================
def Strip_straight(chip,structure,length,w=None,bgcolor=None,**kwargs): #note: uses CPW conventions
def struct():
if isinstance(structure,m.Structure):
return structure
elif isinstance(structure,tuple):
return m.Structure(chip,structure)
else:
return chip.structure(structure)
if bgcolor is None:
bgcolor = chip.wafer.bg()
if w is None:
try:
w = struct().defaults['w']
except KeyError:
print('\x1b[33ms not defined in ',chip.chipID,'!\x1b[0m')
chip.add(dxf.rectangle(struct().start,length,w,valign=const.MIDDLE,rotation=struct().direction,bgcolor=bgcolor,**kwargStrip(kwargs)),structure=structure,length=length)
def Strip_taper(chip,structure,length=None,w0=None,w1=None,bgcolor=None,offset=(0,0),**kwargs): #note: uses CPW conventions
def struct():
if isinstance(structure,m.Structure):
return structure
elif isinstance(structure,tuple):
return m.Structure(chip,structure)
else:
return chip.structure(structure)
if bgcolor is None:
bgcolor = chip.wafer.bg()
if w0 is None:
try:
w0 = struct().defaults['w']
except KeyError:
print('\x1b[33ms not defined in ',chip.chipID,'!\x1b[0m')
if w1 is None:
try:
w1 = struct().defaults['w']
except KeyError:
print('\x1b[33ms not defined in ',chip.chipID,'!\x1b[0m')
#if undefined, make outer angle 30 degrees
if length is None:
length = math.sqrt(3)*abs(w0/2-w1/2)
chip.add(SkewRect(struct().start,length,w0,offset,w1,rotation=struct().direction,valign=const.MIDDLE,edgeAlign=const.MIDDLE,bgcolor=bgcolor,**kwargs),structure=structure,offsetVector=vadd((length,0),offset))
def Strip_bend(chip,structure,angle=90,CCW=True,w=None,radius=None,ptDensity=120,bgcolor=None,**kwargs):
def struct():
if isinstance(structure,m.Structure):
return structure
elif isinstance(structure,tuple):
return m.Structure(chip,structure)
else:
return chip.structure(structure)
if w is None:
try:
w = struct().defaults['w']
except KeyError:
print('\x1b[33mw not defined in ',chip.chipID)
if radius is None:
try:
radius = struct().defaults['radius']
except KeyError:
print('\x1b[33mradius not defined in ',chip.chipID,'!\x1b[0m')
return
if bgcolor is None:
bgcolor = chip.wafer.bg()
while angle < 0:
angle = angle + 360
angle = angle%360
chip.add(CurveRect(struct().start,w,radius,angle=angle,ptDensity=ptDensity,ralign=const.MIDDLE,valign=const.MIDDLE,rotation=struct().direction,vflip=not CCW,bgcolor=bgcolor,**kwargs))
struct().updatePos(newStart=struct().getPos((radius*math.sin(math.radians(angle)),(CCW and 1 or -1)*radius*(math.cos(math.radians(angle))-1))),angle=CCW and -angle or angle)
def Strip_stub_open(chip,structure,flipped=False,curve_out=True,r_out=None,w=None,allow_oversize=True,length=None,bgcolor=None,**kwargs):
def struct():
if isinstance(structure,m.Structure):
return structure
elif isinstance(structure,tuple):
return m.Structure(chip,structure)
else:
return chip.structure(structure)
if w is None:
try:
w = struct().defaults['w']
except KeyError:
print('\x1b[33ms not defined in ',chip.chipID,'!\x1b[0m')
if r_out is None:
try:
if allow_oversize:
r_out = struct().defaults['r_out']
else:
r_out = min(struct().defaults['r_out'],w/2)
except KeyError:
print('r_out not defined in ',chip.chipID,'!\x1b[0m')
r_out=0
if bgcolor is None:
bgcolor = chip.wafer.bg()
if r_out > 0:
dx = 0.
if flipped:
if allow_oversize:
dx = max(length,r_out)
else:
dx = min(w/2,r_out)
if allow_oversize:
l=r_out
else:
l=min(w/2,r_out)
if length is None: length=0
chip.add(RoundRect(struct().getPos((dx,0)),max(length,l),w,l,roundCorners=[0,curve_out,curve_out,0],hflip=flipped,valign=const.MIDDLE,rotation=struct().direction,bgcolor=bgcolor,**kwargs),structure=structure,length=max(l,length))
else:
if length is not None:
if allow_oversize:
l=length
else:
l=min(w/2,length)
else:
l=w/2
Strip_straight(chip,structure,l,w=w,bgcolor=bgcolor,**kwargs)
def Strip_stub_short(chip,structure,r_ins=None,w=None,flipped=False,extra_straight_section=False,bgcolor=None,**kwargs):
def struct():
if isinstance(structure,m.Structure):
return structure
elif isinstance(structure,tuple):
return m.Structure(chip,structure)
else:
return chip.structure(structure)
if w is None:
try:
w = struct().defaults['w']
except KeyError:
print('\x1b[33mw not defined in ',chip.chipID,'!\x1b[0m')
if r_ins is None:
try:
r_ins = struct().defaults['r_ins']
except KeyError:
#print('r_ins not defined in ',chip.chipID,'!\x1b[0m')
r_ins=0
if bgcolor is None:
bgcolor = chip.wafer.bg()
if r_ins > 0:
if extra_straight_section and not flipped:
Strip_straight(chip, struct(), r_ins, w=w,rotation=struct().direction,bgcolor=bgcolor,**kwargs)
chip.add(InsideCurve(struct().getPos((0,-w/2)),r_ins,rotation=struct().direction,hflip=flipped,bgcolor=bgcolor,**kwargs))
chip.add(InsideCurve(struct().getPos((0,w/2)),r_ins,rotation=struct().direction,hflip=flipped,vflip=True,bgcolor=bgcolor,**kwargs))
if extra_straight_section and flipped:
Strip_straight(chip, struct(), r_ins, w=w,rotation=struct().direction,bgcolor=bgcolor,**kwargs)
def Strip_pad(chip,structure,length,r_out=None,w=None,bgcolor=None,**kwargs):
'''
Draw a pad with all rounded corners (similar to strip_stub_open + strip_straight + strip_stub_open but only one shape)
'''
def struct():
if isinstance(structure,m.Structure):
return structure
elif isinstance(structure,tuple):
return m.Structure(chip,structure)
else:
return chip.structure(structure)
if w is None:
try:
w = struct().defaults['w']
except KeyError:
print('\x1b[33ms not defined in ',chip.chipID,'!\x1b[0m')
if r_out is None:
try:
r_out = min(struct().defaults['r_out'],w/2)
except KeyError:
print('r_out not defined in ',chip.chipID,'!\x1b[0m')
r_out=0
if bgcolor is None:
bgcolor = chip.wafer.bg()
if r_out > 0:
chip.add(RoundRect(struct().getPos((0,0)),length,w,r_out,roundCorners=[1,1,1,1],valign=const.MIDDLE,rotation=struct().direction,bgcolor=bgcolor,**kwargs),structure=structure,length=length)
else:
Strip_straight(chip,structure,length,w=w,bgcolor=bgcolor,**kwargs)
# ===============================================================================
# basic NEGATIVE CPW function definitions
# ===============================================================================
def CPW_straight(chip,structure,length,w=None,s=None,bondwires=False,bond_pitch=70,incl_end_bond=True,bgcolor=None,**kwargs): #note: uses CPW conventions
def struct():
if isinstance(structure,m.Structure):
return structure
elif isinstance(structure,tuple):
return m.Structure(chip,structure)
else:
return chip.structure(structure)
if bgcolor is None:
bgcolor = chip.wafer.bg()
if w is None:
try:
w = struct().defaults['w']
except KeyError:
print('\x1b[33ms not defined in ',chip.chipID,'!\x1b[0m')
if s is None:
try:
s = struct().defaults['s']
except KeyError:
print('\x1b[33ms not defined in ',chip.chipID,'!\x1b[0m')
if bondwires: # bond parameters patched through kwargs
num_bonds = int(length/bond_pitch)
this_struct = struct().clone()
this_struct.shiftPos(bond_pitch)
if not incl_end_bond: num_bonds -= 1
for i in range(num_bonds):
Airbridge(chip, this_struct, **kwargs)
this_struct.shiftPos(bond_pitch)
chip.add(dxf.rectangle(struct().getPos((0,-w/2)),length,-s,rotation=struct().direction,bgcolor=bgcolor,**kwargStrip(kwargs)))
chip.add(dxf.rectangle(struct().getPos((0,w/2)),length,s,rotation=struct().direction,bgcolor=bgcolor,**kwargStrip(kwargs)),structure=structure,length=length)
return struct().getPos()
def CPW_taper(chip,structure,length=None,w0=None,s0=None,w1=None,s1=None,bgcolor=None,offset=(0,0),**kwargs): #note: uses CPW conventions
def struct():
if isinstance(structure,m.Structure):
return structure
elif isinstance(structure,tuple):
return m.Structure(chip,structure)
else:
return chip.structure(structure)
if bgcolor is None:
bgcolor = chip.wafer.bg()
if w0 is None:
try:
w0 = struct().defaults['w']
except KeyError:
print('\x1b[33ms not defined in ',chip.chipID,'!\x1b[0m')
if s0 is None:
try:
s0 = struct().defaults['s']
except KeyError:
print('\x1b[33ms not defined in ',chip.chipID,'!\x1b[0m')
if w1 is None:
try:
w1 = struct().defaults['w']
except KeyError:
print('\x1b[33ms not defined in ',chip.chipID,'!\x1b[0m')
if s1 is None:
try:
s1 = struct().defaults['s']
except KeyError:
print('\x1b[33ms not defined in ',chip.chipID,'!\x1b[0m')
#if undefined, make outer angle 30 degrees
if length is None:
length = math.sqrt(3)*abs(w0/2+s0-w1/2-s1)
chip.add(SkewRect(struct().getPos((0,-w0/2)),length,s0,(offset[0],w0/2-w1/2+offset[1]),s1,rotation=struct().direction,valign=const.TOP,edgeAlign=const.TOP,bgcolor=bgcolor,**kwargs))
chip.add(SkewRect(struct().getPos((0,w0/2)),length,s0,(offset[0],w1/2-w0/2+offset[1]),s1,rotation=struct().direction,valign=const.BOTTOM,edgeAlign=const.BOTTOM,bgcolor=bgcolor,**kwargs),structure=structure,offsetVector=(length+offset[0],offset[1]))
def CPW_stub_short(chip,structure,flipped=False,curve_ins=True,curve_out=True,r_out=None,w=None,s=None,length=None,bgcolor=None,**kwargs):
allow_oversize = (curve_ins != curve_out)
def struct():
if isinstance(structure,m.Structure):
return structure
elif isinstance(structure,tuple):
return m.Structure(chip,structure)
else:
return chip.structure(structure)
if w is None:
try:
w = struct().defaults['w']
except KeyError:
print('\x1b[33ms not defined in ',chip.chipID,'!\x1b[0m')
if s is None:
try:
s = struct().defaults['s']
except KeyError:
print('\x1b[33ms not defined in ',chip.chipID,'!\x1b[0m')
if r_out is None:
try:
if allow_oversize:
r_out = struct().defaults['r_out']
else:
r_out = min(struct().defaults['r_out'],s/2)
except KeyError:
print('r_out not defined in ',chip.chipID,'!\x1b[0m')
r_out=0
if bgcolor is None:
bgcolor = chip.wafer.bg()
if r_out > 0:
dx = 0.
if flipped:
if allow_oversize:
dx = r_out
else:
dx = min(s/2,r_out)
if allow_oversize:
l=r_out
else:
l=min(s/2,r_out)
chip.add(RoundRect(struct().getPos((dx,w/2)),l,s,l,roundCorners=[0,curve_ins,curve_out,0],hflip=flipped,valign=const.BOTTOM,rotation=struct().direction,bgcolor=bgcolor,**kwargs))
chip.add(RoundRect(struct().getPos((dx,-w/2)),l,s,l,roundCorners=[0,curve_out,curve_ins,0],hflip=flipped,valign=const.TOP,rotation=struct().direction,bgcolor=bgcolor,**kwargs),structure=structure,length=l)
else:
if length is not None:
if allow_oversize:
l=length
else:
l=min(s/2,length)
else:
l=s/2
CPW_straight(chip,structure,l,w=w,s=s,bgcolor=bgcolor,**kwargs)
def CPW_stub_open(chip,structure,length=0,r_out=None,r_ins=None,w=None,s=None,flipped=False,extra_straight_section=False,bgcolor=None, polygon_overlap=False, **kwargs):
def struct():
if isinstance(structure,m.Structure):
return structure
elif isinstance(structure,tuple):
return m.Structure(chip,structure)
else:
return chip.structure(structure)
if w is None:
try:
w = struct().defaults['w']
except KeyError:
print('\x1b[33mw not defined in ',chip.chipID,'!\x1b[0m')
if s is None:
try:
s = struct().defaults['s']
except KeyError:
print('\x1b[33ms not defined in ',chip.chipID,'!\x1b[0m')
if length==0:
length = max(length,s)
if r_out is None:
try:
r_out = struct().defaults['r_out']
except KeyError:
print('\x1b[33mr_out not defined in ',chip.chipID,'!\x1b[0m')
r_out=0
if r_ins is None:
try:
r_ins = struct().defaults['r_ins']
except KeyError:
#print('r_ins not defined in ',chip.chipID,'!\x1b[0m')
r_ins=0
if bgcolor is None:
bgcolor = chip.wafer.bg()
dx = 0.
if flipped:
dx = length
if r_ins > 0:
if extra_straight_section and not flipped:
CPW_straight(chip, struct(), r_ins, w=w,s=s,rotation=struct().direction,bgcolor=bgcolor,**kwargs)
d_angle = 0
if polygon_overlap: d_angle = 0.03
chip.add(InsideCurve(struct().getPos((dx,w/2)),r_ins, angle=90+d_angle, rotation=struct().direction - d_angle/2,hflip=flipped,bgcolor=bgcolor,**kwargs))
chip.add(InsideCurve(struct().getPos((dx,-w/2)),r_ins, angle=90+d_angle, rotation=struct().direction + d_angle/2,hflip=flipped,vflip=True,bgcolor=bgcolor,**kwargs))
chip.add(RoundRect(struct().getPos((dx,0)),length,w+2*s,min(r_out,length),roundCorners=[0,1,1,0],hflip=flipped,valign=const.MIDDLE,rotation=struct().direction,bgcolor=bgcolor,**kwargs),structure=structure,length=length)
if extra_straight_section and flipped:
CPW_straight(chip, struct(), r_ins, w=w,s=s,rotation=struct().direction,bgcolor=bgcolor,**kwargs)
def CPW_cap(chip,structure,gap,r_ins=None,w=None,s=None,bgcolor=None,angle=90,**kwargs):
def struct():
if isinstance(structure,m.Structure):
return structure
elif isinstance(structure,tuple):
return m.Structure(chip,structure)
else:
return chip.structure(structure)
if w is None:
try:
w = struct().defaults['w']
except KeyError:
print('\x1b[33mw not defined in ',chip.chipID,'!\x1b[0m')
if s is None:
try:
s = struct().defaults['s']
except KeyError:
print('\x1b[33ms not defined in ',chip.chipID,'!\x1b[0m')
if r_ins is None:
try:
r_ins = struct().defaults['r_ins']
except KeyError:
#print('r_ins not defined in ',chip.chipID,'!\x1b[0m')
r_ins=0
if bgcolor is None:
bgcolor = chip.wafer.bg()
if r_ins > 0:
chip.add(InsideCurve(struct().getPos((0,w/2)),r_ins,rotation=struct().direction + 90,vflip=True,angle=angle,bgcolor=bgcolor,**kwargs))
chip.add(InsideCurve(struct().getPos((0,-w/2)),r_ins,rotation=struct().direction - 90,angle=angle,bgcolor=bgcolor,**kwargs))
chip.add(InsideCurve(struct().getPos((gap,w/2)),r_ins,rotation=struct().direction + 90,angle=angle,bgcolor=bgcolor,**kwargs))
chip.add(InsideCurve(struct().getPos((gap,-w/2)),r_ins,rotation=struct().direction - 90,vflip=True,angle=angle,bgcolor=bgcolor,**kwargs))
chip.add(dxf.rectangle(struct().start,gap,w+2*s,valign=const.MIDDLE,rotation=struct().direction,bgcolor=bgcolor,**kwargStrip(kwargs)),structure=structure,length=gap)
def CPW_stub_round(chip,structure,w=None,s=None,round_left=True,round_right=True,flipped=False,bgcolor=None,**kwargs):
#same as stub_open, but preserves gap width along turn (so radii are nominally defined by w, s)
def struct():
if isinstance(structure,m.Structure):
return structure
elif isinstance(structure,tuple):
return m.Structure(chip,structure)
else:
return chip.structure(structure)
if w is None:
try:
w = struct().defaults['w']
except KeyError:
print('\x1b[33mw not defined in ',chip.chipID)
if s is None:
try:
s = struct().defaults['s']
except KeyError:
print('\x1b[33ms not defined in ',chip.chipID)
if bgcolor is None:
bgcolor = chip.wafer.bg()
dx = 0.
if flipped:
dx = s+w/2
if False:#round_left and round_right:
chip.add(CurveRect(struct().getPos((dx,w/2)),s,w/2,angle=180,ralign=const.BOTTOM,rotation=struct().direction,hflip=flipped,bgcolor=bgcolor,**kwargs),structure=structure,length=s+w/2)
else:
if round_left:
chip.add(CurveRect(struct().getPos((dx,w/2)),s,w/2,angle=90,ralign=const.BOTTOM,rotation=struct().direction,hflip=flipped,bgcolor=bgcolor,**kwargs))
else:
chip.add(dxf.rectangle(struct().getPos((0,w/2)),s+w/2,s,rotation=struct().direction,bgcolor=bgcolor,**kwargStrip(kwargs)))
chip.add(InsideCurve(struct().getPos((flipped and s or w/2,w/2)),w/2,rotation=struct().direction,hflip=flipped,bgcolor=bgcolor,**kwargs))
chip.add(dxf.rectangle(struct().getPos((s+w/2-dx,w/2)),-s,-w/2,rotation=struct().direction,halign = flipped and const.RIGHT or const.LEFT, bgcolor=bgcolor,**kwargStrip(kwargs)))
if round_right:
chip.add(CurveRect(struct().getPos((dx,-w/2)),s,w/2,angle=90,ralign=const.BOTTOM,rotation=struct().direction,hflip=flipped,vflip=True,bgcolor=bgcolor,**kwargs),structure=structure,length=s+w/2)
else:
chip.add(dxf.rectangle(struct().getPos((0,-w/2)),s+w/2,-s,rotation=struct().direction,bgcolor=bgcolor,**kwargStrip(kwargs)))
chip.add(InsideCurve(struct().getPos((flipped and s or w/2,-w/2)),w/2,rotation=struct().direction,hflip=flipped,vflip=True,bgcolor=bgcolor,**kwargs))
chip.add(dxf.rectangle(struct().getPos((s+w/2-dx,-w/2)),-s,w/2,rotation=struct().direction,halign = flipped and const.RIGHT or const.LEFT, bgcolor=bgcolor,**kwargStrip(kwargs)),structure=structure,length=s+w/2)
def CPW_bend(chip,structure,angle=90,CCW=True,w=None,s=None,radius=None,ptDensity=120,bondwires=False,incl_end_bond=True,bond_pitch=70,bgcolor=None,**kwargs):
def struct():
if isinstance(structure,m.Structure):
return structure
elif isinstance(structure,tuple):
return m.Structure(chip,structure)
else:
return chip.structure(structure)
if w is None:
try:
w = struct().defaults['w']
except KeyError:
print('\x1b[33mw not defined in ',chip.chipID)
if s is None:
try:
s = struct().defaults['s']
except KeyError:
print('\x1b[33ms not defined in ',chip.chipID)
if radius is None:
try:
radius = struct().defaults['radius']
except KeyError:
print('\x1b[33mradius not defined in ',chip.chipID,'!\x1b[0m')
return
if bgcolor is None:
bgcolor = chip.wafer.bg()
while angle < 0:
angle = angle + 360
angle = angle%360
angleRadians = math.radians(angle)
startstruct = struct().clone()
chip.add(CurveRect(struct().start,s,radius,angle=angle,ptDensity=ptDensity,roffset=w/2,ralign=const.BOTTOM,rotation=struct().direction,vflip=not CCW,bgcolor=bgcolor,**kwargs))
chip.add(CurveRect(struct().start,s,radius,angle=angle,ptDensity=ptDensity,roffset=-w/2,ralign=const.TOP,valign=const.TOP,rotation=struct().direction,vflip=not CCW,bgcolor=bgcolor,**kwargs))
struct().updatePos(newStart=struct().getPos((radius*math.sin(angleRadians),(CCW and 1 or -1)*radius*(math.cos(angleRadians)-1))),angle=CCW and -angle or angle)
if bondwires: # bond parameters patched through kwargs
bond_angle_density = 8
if 'lincolnLabs' in kwargs and kwargs['lincolnLabs']: bond_angle_density = int((2*math.pi*radius)/bond_pitch)
clockwise = 1 if CCW else -1
bond_points = curveAB(startstruct.start, struct().start, clockwise=clockwise, angleDeg=angle, ptDensity=bond_angle_density)
if not incl_end_bond: bond_points = bond_points[:-1]
for i, bond_point in enumerate(bond_points[1:], start=1):
this_struct = m.Structure(chip, start=bond_point, direction=startstruct.direction-clockwise*i*360/bond_angle_density)
Airbridge(chip, this_struct, br_radius=radius, clockwise=clockwise, **kwargs)
def CPW_tee(chip,structure,w=None,s=None,radius=None,r_ins=None,w1=None,s1=None,bgcolor=None,hflip=False,branch_off=const.CENTER, polygon_overlap=False, **kwargs):
def struct():
if isinstance(structure,m.Structure):
return structure
elif isinstance(structure,tuple):
return m.Structure(chip,structure)
else:
return chip.structure(structure)
if w is None:
try:
w = struct().defaults['w']
except KeyError:
print('\x1b[33mw not defined in ',chip.chipID)
if s is None:
try:
s = struct().defaults['s']
except KeyError:
print('\x1b[33ms not defined in ',chip.chipID)
if radius is None:
try:
radius = 2*struct().defaults['s']
except KeyError:
print('\x1b[33mradius not defined in ',chip.chipID,'!\x1b[0m')
return
if r_ins is None: #check if r_ins is defined in the defaults
try:
r_ins = struct().defaults['r_ins']
except KeyError: # quiet catch
r_ins = None
#default to left and right branches identical to original structure
if w1 is None:
w1 = w
if s1 is None:
s1 = s
#clone structure defaults
defaults1 = copy(struct().defaults)
#update new defaults if defined
defaults1.update({'w':w1})
defaults1.update({'s':s1})
if bgcolor is None:
bgcolor = chip.wafer.bg()
#long curves not allowed if gaps differ
if s!=s1:
radius = min(abs(radius),min(s,s1))
#assign a inside curve radius if not defined
if r_ins is None:
r_ins = radius
s_rad = max(radius,s1)
#figure out if tee is centered, or offset
if branch_off == const.LEFT:
struct().translatePos((s_rad+w1/2 - 2*hflip*(s_rad+w1/2),w/2+max(radius,s)),angle=-90)
elif branch_off == const.RIGHT:
struct().translatePos((s_rad+w1/2 - 2*hflip*(s_rad+w1/2),-w/2-max(radius,s)),angle=90)
chip.add(dxf.rectangle(struct().getPos((s_rad+w1 - 2*hflip*(s_rad+w1),0)),hflip and -s1 or s1,2*max(radius,s)+w,valign=const.MIDDLE,rotation=struct().direction,bgcolor=bgcolor,**kwargStrip(kwargs)))
if s==s1:
chip.add(CurveRect(struct().getPos((0,-w/2-s)),s,radius,hflip=hflip,ralign=const.TOP,rotation=struct().direction,bgcolor=bgcolor,**kwargs))
chip.add(CurveRect(struct().getPos((0,w/2+s)),s,radius,hflip=hflip,vflip=True,ralign=const.TOP,rotation=struct().direction,bgcolor=bgcolor,**kwargs))
else:
if s1>s:
chip.add(dxf.rectangle(struct().getPos((0,-w/2)),hflip and s-s1 or s1-s,-s,rotation=struct().direction,bgcolor=bgcolor,**kwargStrip(kwargs)))
chip.add(dxf.rectangle(struct().getPos((0,w/2)),hflip and s-s1 or s1-s,s,rotation=struct().direction,bgcolor=bgcolor,**kwargStrip(kwargs)))
chip.add(CurveRect(struct().getPos((hflip and s-s1 or s1-s,-w/2-s)),radius,radius,hflip=hflip,ralign=const.TOP,rotation=struct().direction,bgcolor=bgcolor,**kwargs))
chip.add(CurveRect(struct().getPos((hflip and s-s1 or s1-s,w/2+s)),radius,radius,hflip=hflip,vflip=True,ralign=const.TOP,rotation=struct().direction,bgcolor=bgcolor,**kwargs))
else:#s1<s
chip.add(CurveRect(struct().getPos((0,-w/2-radius)),radius,radius,hflip=hflip,ralign=const.TOP,rotation=struct().direction,bgcolor=bgcolor,**kwargs))
chip.add(CurveRect(struct().getPos((0,w/2+radius)),radius,radius,hflip=hflip,vflip=True,ralign=const.TOP,rotation=struct().direction,bgcolor=bgcolor,**kwargs))
chip.add(dxf.rectangle(struct().getPos((0,-w/2-radius)),hflip and -radius or radius,-(s-s1),rotation=struct().direction,bgcolor=bgcolor,**kwargStrip(kwargs)))
chip.add(dxf.rectangle(struct().getPos((0,w/2+radius)),hflip and -radius or radius,(s-s1),rotation=struct().direction,bgcolor=bgcolor,**kwargStrip(kwargs)))
if radius <= min(s,s1) and r_ins > 0:
#inside edges are square
d_angle = 0
if polygon_overlap: d_angle = 0.03
chip.add(InsideCurve(struct().getPos((0,w/2+s)),r_ins,hflip=hflip,vflip=True,ralign=const.TOP, angle=90+d_angle, rotation=struct().direction + d_angle/2, bgcolor=bgcolor,**kwargs))
chip.add(InsideCurve(struct().getPos((0,-w/2-s)),r_ins,hflip=hflip,vflip=False,ralign=const.TOP, angle=90+d_angle, rotation=struct().direction - d_angle/2, bgcolor=bgcolor,**kwargs))
if branch_off == const.CENTER:
s_l = struct().cloneAlong((s_rad+w1/2 - 2*hflip*(s_rad+w1/2),w/2+max(radius,s)),newDirection=90,defaults=defaults1)
s_r = struct().cloneAlong((s_rad+w1/2 - 2*hflip*(s_rad+w1/2),-w/2-max(radius,s)),newDirection=-90,defaults=defaults1)
return s_l,s_r
elif branch_off == const.LEFT:
s_l = struct().cloneAlong((0,0),newDirection=180)
struct().translatePos((w/2+max(radius,s),s_rad+w1/2 - 2*hflip*(s_rad+w1/2)),angle=90)
return s_l
elif branch_off == const.RIGHT:
s_r = struct().cloneAlong((0,0),newDirection=180)
struct().translatePos((w/2+max(radius,s),-s_rad-w1/2 + 2*hflip*(s_rad+w1/2)),angle=-90)
return s_r
# ===============================================================================
# basic NEGATIVE TwoPinCPW function definitions
# ===============================================================================
def TwoPinCPW_straight(chip,structure,length,w=None,s_ins=None,s_out=None,Width=None,s=None,bgcolor=None,**kwargs): #note: uses CPW conventions
def struct():
if isinstance(structure,m.Structure):
return structure
elif isinstance(structure,tuple):
return m.Structure(chip,structure)
else:
return chip.structure(structure)
if bgcolor is None:
bgcolor = chip.wafer.bg()
if w is None:
try:
w = struct().defaults['w']
except KeyError:
print('\x1b[33ms not defined in ',chip.chipID,'!\x1b[0m')
if s is not None:
#s overridden somewhere above
if s_ins is None:
s_ins = s
if s_out is None:
s_out = s
if s_ins is None:
try:
s_ins = struct().defaults['s']
except KeyError:
print('\x1b[33ms not defined in ',chip.chipID,'!\x1b[0m')
if s_out is None:
if Width is not None:
s_out = Width - w - s_ins/2
else:
try:
s_out = struct().defaults['s']
except KeyError:
print('\x1b[33ms not defined in ',chip.chipID,'!\x1b[0m')
chip.add(dxf.rectangle(struct().getPos((0,-s_ins/2-w)),length,-s_out,rotation=struct().direction,bgcolor=bgcolor,**kwargStrip(kwargs)))
chip.add(dxf.rectangle(struct().getPos((0,-s_ins/2)),length,s_ins,rotation=struct().direction,bgcolor=bgcolor,**kwargStrip(kwargs)))
chip.add(dxf.rectangle(struct().getPos((0,s_ins/2+w)),length,s_out,rotation=struct().direction,bgcolor=bgcolor,**kwargStrip(kwargs)),structure=structure,length=length)
# ===============================================================================
# NEGATIVE wire/stripline function definitions
# ===============================================================================
def Wire_bend(chip,structure,angle=90,CCW=True,w=None,radius=None,bgcolor=None,**kwargs):
#only defined for 90 degree bends
if angle%90 != 0:
return
def struct():
if isinstance(structure,m.Structure):
return structure
elif isinstance(structure,tuple):
return m.Structure(chip,structure)
else:
return chip.structure(structure)
if w is None:
try:
w = struct().defaults['w']
except KeyError:
print('\x1b[33mw not defined in ',chip.chipID)
if radius is None:
try:
radius = struct().defaults['radius']
except KeyError:
print('\x1b[33mradius not defined in ',chip.chipID,'!\x1b[0m')
return
if bgcolor is None:
bgcolor = chip.wafer.bg()
while angle < 0:
angle = angle + 360
angle = angle%360
if radius-w/2 > 0:
chip.add(CurveRect(struct().start,radius-w/2,radius,angle=angle,roffset=-w/2,ralign=const.TOP,valign=const.TOP,rotation=struct().direction,vflip=not CCW,bgcolor=bgcolor,**kwargs))
for i in range(angle//90):
chip.add(InsideCurve(struct().getPos(vadd(rotate_2d((radius+w/2,(CCW and 1 or -1)*(radius+w/2)),(CCW and -1 or 1)*math.radians(i*90)),(0,CCW and -radius or radius))),radius+w/2,rotation=struct().direction+(CCW and -1 or 1)*i*90,bgcolor=bgcolor,vflip=not CCW,**kwargs))
struct().updatePos(newStart=struct().getPos((radius*math.sin(math.radians(angle)),(CCW and 1 or -1)*radius*(math.cos(math.radians(angle))-1))),angle=CCW and -angle or angle)
# ===============================================================================
# composite CPW function definitions
# ===============================================================================
def CPW_pad(chip,struct,l_pad=0,l_gap=0,padw=300,pads=50,l_lead=None,w=None,s=None,r_ins=None,r_out=None,bgcolor=None,**kwargs):
if w is None:
try:
w = struct().defaults['w']
except KeyError:
w=0
print('\x1b[33mw not defined in ',chip.chipID)
CPW_stub_open(chip,struct,length=max(l_gap,pads),r_out=r_out,r_ins=r_ins,w=padw,s=pads,flipped=True,**kwargs)
CPW_straight(chip,struct,max(l_pad,padw),w=padw,s=pads,**kwargs)
if l_lead is None:
l_lead = max(l_gap,pads)
CPW_stub_short(chip,struct,length=l_lead,r_out=r_out,r_ins=r_ins,w=w,s=pads+padw/2-w/2,flipped=False,curve_ins=False,**kwargs)
def CPW_launcher(chip,struct,l_taper=None,l_pad=0,l_gap=0,padw=300,pads=160,w=None,s=None,r_ins=0,r_out=0,bgcolor=None,**kwargs):
CPW_stub_open(chip,struct,length=max(l_gap,pads),r_out=r_out,r_ins=r_ins,w=padw,s=pads,flipped=True,**kwargs)
CPW_straight(chip,struct,max(l_pad,padw),w=padw,s=pads,**kwargs)
CPW_taper(chip,struct,length=l_taper,w0=padw,s0=pads,**kwargs)
def CPW_taper_cap(chip,structure,gap,width,l_straight=0,l_taper=None,s1=None,**kwargs):
def struct():
if isinstance(structure,m.Structure):
return structure
elif isinstance(structure,tuple):
return m.Structure(chip,structure)
else:
return chip.structure(structure)
if s1 is None:
try:
s = struct().defaults['s']
w = struct().defaults['w']
s1 = width*s/w
except KeyError:
print('\x1b[33mw not defined in ',chip.chipID)
print('\x1b[33ms not defined in ',chip.chipID)
if l_taper is None:
l_taper = 3*width
if l_straight<=0:
try:
tap_angle = math.degrees(math.atan(2*l_taper/(width-struct().defaults['w'])))
except KeyError:
print('\x1b[33mw not defined in ',chip.chipID)
tap_angle = 90
else:
tap_angle = 90
CPW_taper(chip,structure,length=l_taper,w1=width,s1=s1,**kwargs)
if l_straight > 0 :
CPW_straight(chip,structure,length=l_straight,w=width,s=s1,**kwargs)
CPW_cap(chip, structure, gap, w=width, s=s1, angle=tap_angle, **kwargs)
if l_straight > 0 :
CPW_straight(chip,structure,length=l_straight,w=width,s=s1,**kwargs)
CPW_taper(chip,structure,length=l_taper,w0=width,s0=s1,**kwargs)
def CPW_directTo(chip,from_structure,to_structure,to_flipped=True,w=None,s=None,radius=None,CW1_override=None,CW2_override=None,flip_angle=False,debug=False,**kwargs):
def struct1():
if isinstance(from_structure,m.Structure):
return from_structure
else:
return chip.structure(from_structure)
if radius is None:
try:
radius = struct1().defaults['radius']
except KeyError:
print('\x1b[33mradius not defined in ',chip.chipID,'!\x1b[0m')
return
#struct2 is only a local copy
struct2 = isinstance(to_structure,m.Structure) and to_structure or chip.structure(to_structure)
if to_flipped:
struct2.direction=(struct2.direction+180.)%360
CW1 = vector2angle(struct1().getGlobalPos(struct2.start)) < 0
CW2 = vector2angle(struct2.getGlobalPos(struct1().start)) < 0
target1 = struct1().getPos((0,CW1 and -2*radius or 2*radius))
target2 = struct2.getPos((0,CW2 and -2*radius or 2*radius))
#reevaluate based on center positions
CW1 = vector2angle(struct1().getGlobalPos(target2)) < 0
CW2 = vector2angle(struct2.getGlobalPos(target1)) < 0
if CW1_override is not None:
CW1 = CW1_override
if CW2_override is not None:
CW2 = CW2_override
center1 = struct1().getPos((0,CW1 and -radius or radius))
center2 = struct2.getPos((0,CW2 and -radius or radius))
if debug:
chip.add(dxf.line(struct1().getPos((-3000,0)),struct1().getPos((3000,0)),layer='FRAME'))
chip.add(dxf.line(struct2.getPos((-3000,0)),struct2.getPos((3000,0)),layer='FRAME'))
chip.add(dxf.circle(center=center1,radius=radius,layer='FRAME'))
chip.add(dxf.circle(center=center2,radius=radius,layer='FRAME'))
correction_angle=math.asin(abs(2*radius*(CW1 - CW2)/distance(center2,center1)))
angle1 = abs(struct1().direction - math.degrees(vector2angle(vsub(center2,center1)))) + math.degrees(correction_angle)
if flip_angle:
angle1 = 360-abs(struct1().direction - math.degrees(vector2angle(vsub(center2,center1)))) + math.degrees(correction_angle)
if debug:
print(CW1,CW2,angle1,math.degrees(correction_angle))
if angle1 > 270:
if debug:
print('adjusting to shorter angle')
angle1 = min(angle1,abs(360-angle1))
'''
if CW1 - CW2 == 0 and abs(angle1)>100:
if abs((struct1().getGlobalPos(struct2.start))[1]) < 2*radius:
print('adjusting angle')
angle1 = angle1 + math.degrees(math.asin(abs(2*radius/distance(center2,center1))))
'''
CPW_bend(chip,from_structure,angle=angle1,w=w,s=s,radius=radius, CCW=CW1,**kwargs)
CPW_straight(chip,from_structure,distance(center2,center1)*math.cos(correction_angle),w=w,s=s,**kwargs)
angle2 = abs(struct1().direction-struct2.direction)
if angle2 > 270:
angle2 = min(angle2,abs(360-angle2))
CPW_bend(chip,from_structure,angle=angle2,w=w,s=s,radius=radius,CCW=CW2,**kwargs)
#Various wiggles (meander) definitions
def wiggle_calc(chip,structure,length=None,nTurns=None,maxWidth=None,Width=None,start_bend = True,stop_bend=True,w=None,s=None,radius=None,debug=False,**kwargs):
#figure out
def struct():
if isinstance(structure,m.Structure):
return structure
elif isinstance(structure,tuple):
return m.Structure(chip,structure)
else:
return chip.structure(structure)
if radius is None:
try:
radius = struct().defaults['radius']
except KeyError:
print('\x1b[33mradius not defined in ',chip.chipID,'!\x1b[0m')
return
if w is None:
try:
w = struct().defaults['w']
except KeyError:
print('\x1b[33mw not defined in ',chip.chipID,'!\x1b[0m')
if s is None:
try:
s = struct().defaults['s']
except KeyError:
s = 0
#prevent dumb entries
if nTurns is None:
nTurns = 1
elif nTurns < 1:
nTurns = 1
#debug
if debug:
print('w=',w,' s=',s,' nTurns=',nTurns,' length=',length,' Width=',Width,' maxWidth=',maxWidth)
#is length constrained?
if length is not None:
if nTurns is None:
nTurns = 1
h = (length - nTurns*2*math.pi*radius - (start_bend+stop_bend)*(math.pi/2-1)*radius)/(4*nTurns)
#is width constrained?
if Width is not None or maxWidth is not None:
#maxWidth corresponds to the wiggle width, while Width corresponds to the total width filled
if maxWidth is not None:
if Width is None:
Width = maxWidth
else:
maxWidth = min(maxWidth,Width)
else:
maxWidth = Width
while h+radius+w/2+s/2>maxWidth:
nTurns = nTurns+1
h = (length - nTurns*2*math.pi*radius - (start_bend+stop_bend)*(math.pi/2-1)*radius)/(4*nTurns)
else: #length is not contrained
h= maxWidth-radius-w/2-s
h = max(h,radius)
return {'nTurns':nTurns,'h':h,'length':length,'maxWidth':maxWidth,'Width':Width}
def CPW_wiggles(chip,structure,length=None,nTurns=None,maxWidth=None,CCW=True,start_bend = True,stop_bend=True,w=None,s=None,radius=None,bgcolor=None,debug=False,**kwargs):
def struct():
if isinstance(structure,m.Structure):
return structure
elif isinstance(structure,tuple):
return m.Structure(chip,structure)
else:
return chip.structure(structure)
if radius is None:
try:
radius = struct().defaults['radius']
except KeyError:
print('\x1b[33mradius not defined in ',chip.chipID,'!\x1b[0m')
return
params = wiggle_calc(chip,structure,length,nTurns,maxWidth,None,start_bend,stop_bend,w,s,radius,**kwargs)
[nTurns,h,length,maxWidth]=[params[key] for key in ['nTurns','h','length','maxWidth']]
if (length is None) or (h is None) or (nTurns is None):
print('not enough params specified for CPW_wiggles!')
return