forked from henrynester/bender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbender.py
235 lines (197 loc) · 8.97 KB
/
bender.py
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
import track, tline
import numpy as np, matplotlib.pyplot as plt
import ezdxf
from ezdxf import units
import ezdxf.path
import progress.bar
import json, sys
class Bender: # uh oh
def __init__(self, track_sequence, floquet_unit_cell, fillet_radius=None):
self.track_sequence = track_sequence
self.floquet_unit_cell = floquet_unit_cell
self.fillet_radius = fillet_radius
self.create_dxf()
def construct_tline(self):
self.unit_cell_vertices_count = \
self.floquet_unit_cell.vertices().shape[1]
unit_cell_length = self.floquet_unit_cell.cell_length()
arclength = trackseq.total_arclength()
self.n_floquet_cells = int(arclength // unit_cell_length)
bar = progress.bar.Bar('', max=self.n_floquet_cells)
tline_vertices = np.array([[], []])
offset = np.array([[0.0], [0.0]])
for i in range(self.n_floquet_cells):
tline_vertices = np.append(tline_vertices,
floquet.vertices() + offset, axis=1)
offset += np.array([[unit_cell_length], [0.0]])
bar.next()
bar.finish()
self.tline_vertices = tline_vertices
def bend_tline(self, plot=False):
arclengths, heights = self.tline_vertices
xs, ys, us, vs = self.track_sequence.vertices_normals(arclengths)
self.bent_xs = xs + us * heights
self.bent_ys = ys + vs * heights
if plot:
fig,ax=plt.subplots()
ax.set_aspect('equal')
ax.plot(self.bent_xs, self.bent_ys, marker='.', color='black')
plt.show()
def mirror_tline(self):
self.tline_vertices[1,:] *= -1
def create_dxf(self):
self.doc = ezdxf.new()
self.msp = self.doc.modelspace()
self.doc.units = units.M
def write_bent_tline(self):
bar = progress.bar.Bar('', max=self.n_floquet_cells)
for n in range(self.n_floquet_cells):
first = self.unit_cell_vertices_count*n
last = first + self.unit_cell_vertices_count
polygon = [(self.bent_xs[i], self.bent_ys[i])
for i in range(first,last)]
if self.fillet_radius is not None:
# create path object, fillet path at control vertices (noncollinear points)
# then convert to an lwpolyline dxf file entry and add to the file
p = ezdxf.path.from_vertices(polygon)
p.close()
pp = ezdxf.path.fillet(p.control_vertices(), radius=self.fillet_radius)
out = ezdxf.path.to_lwpolylines((pp,))
self.msp.add_lwpolyline(next(out), close=True)
else:
# no fillets: polygon point list goes straight into the dxf file
self.msp.add_lwpolyline(polygon, close=True)
bar.next()
bar.finish()
def write_dxf(self,filename):
# Save the DXF file
self.doc.saveas(filename)
if __name__ == '__main__':
if len(sys.argv) <= 1:
print('usage: python bender.py /path/to/config.json')
raise ValueError()
cfgdict=None
dxf_filename=None
with open(sys.argv[1]) as cfgfile:
cfgdict = json.load(cfgfile)
dxf_filename = cfgfile.name.split('.json')[0] + '.dxf'
print(cfgfile.name, cfgdict)
cfg_tline, cfg_track = cfgdict['tline'], cfgdict['track']
floquet = tline.FloquetUnitCell()
for w_loaded_line, n_fishbone_cell in zip(cfg_tline['w_loaded_lines'],
cfg_tline['n_fishbone_cells']):
fishbone = tline.FishboneUnitCell(
cfg_tline['fishbone_cell_length'],
cfg_tline['w_center_line'],
cfg_tline['w_fishbone_line'],
w_loaded_line,
cfg_tline['gnd_spacing'],
cfg_tline['gnd_interdigitate'])
floquet.append_fishbones(fishbone, n_fishbone_cell)
#Alec 10GHz
# floquet.append_fishbones(fishboneA, 19)
# floquet.append_fishbones(fishboneB, 8)
# floquet.append_fishbones(fishboneA, 38)
# floquet.append_fishbones(fishboneB, 8)
# floquet.append_fishbones(fishboneA, 37)
# floquet.append_fishbones(fishboneB, 10)
# floquet.append_fishbones(fishboneA, 18)
#Jordan 5GHz
# floquet.append_fishbones(fishboneA, 39)
# floquet.append_fishbones(fishboneB, 13)
# floquet.append_fishbones(fishboneA, 79)
# floquet.append_fishbones(fishboneB, 13)
# floquet.append_fishbones(fishboneA, 78)
# floquet.append_fishbones(fishboneB, 14)
# floquet.append_fishbones(fishboneA, 39)
trackseq = track.TrackSequence()
if cfg_track['style'] == 'fermat':
min_spacing = cfg_track['line_spacing_factor']*floquet.cell_min_spacing()
turns = cfg_track['n_turns']
final_angle = turns * 2 * np.pi
tline_compact_length = cfg_track['compact_length']
fermat1 = track.FermatSpiralTrack(turns, min_spacing, True)
fermat2 = track.FermatSpiralTrack(turns, min_spacing, False)
arc2 = fermat2.construct_suitable_ArcTrack()
arc1 = fermat1.construct_suitable_ArcTrack()
straight2 = arc2.construct_suitable_StraightTrack(tline_compact_length)
straight1 = arc1.construct_suitable_StraightTrack(tline_compact_length)
trackseq.append_track(straight2)
trackseq.append_track(arc2)
trackseq.append_track(fermat2)
trackseq.append_track(fermat1)
trackseq.append_track(arc1)
trackseq.append_track(straight1)
elif cfg_track['style'] == 'singletary':
# filleted square wave shape
# math and variable assignment
compact_width = cfg_track['compact_width']
compact_height = cfg_track['compact_height']
r_launch = cfg_track['radius_launch']
launch_len = cfg_track['launch_len']
n_half_periods = cfg_track['n_half_periods']
T = cfg_track.get('half_period_length',
(compact_width - (2 * r_launch) - (2 * launch_len))
/n_half_periods)
# semi-circle radius 'R'
R = T / 2
# amplitude of straight-line section
A = (compact_height / 2) - R
# Building track
x_start = launch_len + r_launch
x_end = x_start + n_half_periods * T
launchstraight1 = track.StraightTrack(np.array((0,0)),
np.array((launch_len,0)))
launchcurve1 = track.ArcTrack(np.array((launch_len, r_launch)),
r_launch, 3*np.pi/2, 2*np.pi, False)
launchstraight2 = track.StraightTrack(
np.array((launch_len+r_launch, r_launch)),
np.array((launch_len+r_launch, A)))
delaunchstraight1 = track.StraightTrack(np.array((x_end, -A)),
np.array((x_end, -r_launch)))
delaunchcurve1 = track.ArcTrack(np.array((x_end+r_launch, -r_launch)),
r_launch, -np.pi, -np.pi/2, True)
delaunchstraight2 = track.StraightTrack(
np.array((x_end + r_launch, 0)),
np.array((x_end + r_launch + launch_len, 0)))
trackseq.append_track(launchstraight1)
trackseq.append_track(launchcurve1)
trackseq.append_track(launchstraight2)
if n_half_periods % 2 != 0:
print('n_half_periods must be even')
ValueError()
for i in range(n_half_periods):
curve, straight = None, None
if i % 2 == 0:
curve = track.ArcTrack(np.array((x_start+(0.5 + i) * T, A)),
R, -np.pi, 0, True)
straight = track.StraightTrack(np.array((x_start+(i+1)*T,A)),
np.array((x_start+(i+1)*T,-A)))
else:
curve = track.ArcTrack(np.array((x_start+(0.5 + i) * T, -A)),
R, np.pi, 2*np.pi, False)
straight = track.StraightTrack(
np.array((x_start+(i + 1) * T, -A)),
np.array((x_start+(i + 1) * T, A)))
trackseq.append_track(curve)
if i < n_half_periods-1:
trackseq.append_track(straight)
trackseq.append_track(delaunchstraight1)
trackseq.append_track(delaunchcurve1)
trackseq.append_track(delaunchstraight2)
bender = Bender(trackseq, floquet, cfg_tline['fillet_radius'])
print('construct_tline')
bender.construct_tline()
print('bend_tline')
bender.bend_tline(plot=False)
print('write_bent_tline')
bender.write_bent_tline()
print('mirror_tline')
bender.mirror_tline()
print('bend_tline')
bender.bend_tline(plot=False)
print('write_bent_tline')
bender.write_bent_tline()
print(f'{floquet.cell_length()*1e6:.1f}um unit cell * {bender.n_floquet_cells:d} unit cells should = {trackseq.total_arclength()*1e6:.0f}um track length')
print('write_dxf')
bender.write_dxf(dxf_filename)