-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaster_lattice.py
371 lines (306 loc) · 10.7 KB
/
master_lattice.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
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
import numpy as np
import math
########### INPUT ###########
#Design of the machine#
arc_number = 8 #number of arcs in the ring
arc_cells = 28 #number of cells per arc
straight_sections = 8 #number of straight sections in the ring
straight_cells = 4 #number of cells per straight section
total_cells = (arc_number*arc_cells)+(straight_sections*straight_cells) #total number of cells in the ring
print 'total number of cells in the ring = '
print total_cells
print '\n'
cell_length = 100
circumference = total_cells*cell_length
print 'circumference = '
print circumference
print '\n'
m = 60.1 #phase advance per cell. Choose close to 60, but not exactly 60! (otherwise resonances...)
tune = (m*total_cells)/360
cell_dipoles = 2 #number of dipoles per cell
no_dipoles = (straight_cells + 2)*straight_sections*cell_dipoles #number of cells with no dipoles
total_dipoles = (total_cells*cell_dipoles) - no_dipoles #number of dipoles in the ring
#sextupole strength
ksf =3.17441e-04
ksd =-4.07306e-04
#tune
Qpx = 43.0
Qpy = 43.0
frac_q_x = 0.11 #Give slightly different fractional numbers to avoid coupling
frac_q_y = 0.21
Qx_total = Qpx+frac_q_x
Qy_total = Qpy+frac_q_y
#number of turns for tracking
TURNS=1000
#Define chromaticities. Usual values are up to 40 (maximum!)
Qx = []
Qy = []
f = open('chromaticities.txt','r')
for line in f:
chromaticity=line.split()
Qx.append(float(chromaticity[0]))
Qy.append(float(chromaticity[1]))
DQ1 = Qx[0] #give values from 0-9. These correspond to chromaticities = 0,1,5,10,15,20,25,30,35,40.
DQ2 = Qy[0]
#Define dipoles, quadrupoles and sextupoles as multipoles
elements = """
MBS:multipole, knl:={d_angle};
qfType:multipole, knl:={0,kf};
qdType:multipole, knl:={0,kd};
sfType:multipole, knl:={0,0,ksf};
sdType:multipole, knl:={0,0,ksd};
CAV: RFCAVITY, L := 0.;
"""
fodo = """
fodo:SEQUENCE, REFER=entry, L=circumference;
"""
con = """
beam, particle=proton, sequence=fodo, energy=6500;
use, sequence=fodo;
"""
matching_begin= """
match, sequence = fodo;
"""
matching_end = """
vary, name = kf, step=0.00001;
vary, name = kd, step=0.00001;
vary, name = ksf, step=0.00001;
vary, name = ksd, step=0.00001;
lmdif, calls=50, tolerance=1e-6; //method adopted
ENDMATCH;
"""
twiss = """
twiss,save,centre,file=twiss_master.out;
"""
plots = """
survey,file=survey_master.cas;
"""
#Tracking here!
track = """
start, x= 2e-3, px=0, y= 2e-3, py=0, t= 0, pt = 1e-6;
start, x= 3e-3, px=0, y= 3e-3, py=0, t= 0, pt = 3e-5;
start, x= 4e-3, px=0, y= 4e-3, py=0, t= 0, pt = 6e-5;
start, x= 5e-3, px=0, y= 5e-3, py=0, t= 0, pt = 8e-5;
start, x= 6e-3, px=0, y= 6e-3, py=0, t= 0, pt = 1e-4;
start, x= 7e-3, px=0, y= 7e-3, py=0, t= 0, pt = 1.3e-4;
start, x= 8e-3, px=0, y= 8e-3, py=0, t= 0, pt = 1.6e-4;
start, x= 9e-3, px=0, y= 9e-3, py=0, t= 0, pt = 1.8e-4;
start, x= 10e-3, px=0, y= 10e-3, py=0, t= 0, pt = 2.1e-4;
start, x= 11e-3, px=0, y= 11e-3, py=0, t= 0, pt = 3e-4;
"""
#Generate MADX file
fname = 'madx_master_lattice.txt'
#WRITE IN MADX FILE
with open(fname, 'w') as fid:
fid.write('arc_number = %f;\n'%(float(arc_number)))
fid.write('arc_cells = %d;\n'%(int(arc_cells)))
fid.write('straight_sections = %f;\n'%(float(straight_sections)))
fid.write('straight_cells = %d;\n'%(int(straight_cells)))
fid.write('total_cells = (arc_number*arc_cells)+(straight_sections*straight_cells);\n')
fid.write('cell_length = %f;\n'%(float(cell_length)))
fid.write('circumference = total_cells*cell_length;\n')
fid.write('\n')
fid.write('m = %f;\n'%(float(m)))
fid.write('tune = %f;\n'%(float(tune)))
fid.write('cell_dipoles = %f;\n'%(float(cell_dipoles)))
fid.write('no_dipoles = %f;\n'%(float(no_dipoles)))
fid.write('total_dipoles = %f;\n'%(float(total_dipoles)))
fid.write('d_angle := 2*pi/total_dipoles;\n')
fid.write('\n')
fid.write('kf := (4*sin(m/2))/cell_length;\n')
fid.write('kd := -(4*sin(m/2))/cell_length;\n')
fid.write('\n')
fid.write('ksf := %f;\n'%(float(ksf)))
fid.write('ksd := %f;\n'%(float(ksd)))
fid.write(elements)
fid.write(fodo)
#build lattice
loc=0.0 #position of the 1st element
dip=1 #dipole counter
drift = float(cell_dipoles + 2)*total_cells #number of drift spaces
print 'Number of drift spaces = '
print drift
print '\n'
space = float(circumference/drift)
print 'space = '
print space
print '\n'
full_cells = abs((arc_cells*arc_number/straight_sections)-4) #full cells in a row
print 'full cells in a row = '
print full_cells
print '\n'
half_dipoles = (cell_dipoles/2)
straight_drift = float(half_dipoles+1) #in straight sections space between F and D is different due to the missing dipoles. N dipoles result in N+1 drifts
print 'straight drifts = '
print straight_drift
print '\n'
fid.write('CAV1:CAV, at=0.;\n')
offset = 1
#lattice begins in the middle of a straight section
for jj in xrange (straight_sections):
########################################################################################################################################
print 'Last half straight section'
print '\n'
for rr in xrange (straight_cells/2):
fid.write('/*cell%d*/\n'%(int(rr+offset)))
#focus
fid.write('qf%d:qfType, at=%f;\n'%(int(rr+offset), float(loc)))
#defocus
loc=loc+(straight_drift*space)
fid.write('qd%d:qdType, at=%f;\n'%(int(rr+offset), float(loc)))
loc=loc+(straight_drift*space)
fid.write('\n')
offset = offset+rr+1
print 'offset rr = %d= '%int(rr)
print offset
print '\n'
########################################################################################################################################
######## Dispersion suppressor ########
print 'Begin dispersion suppressor'
print '\n'
fid.write('/*cell%d*/\n'%(int(offset)))
#focus
fid.write('qf%d:qfType, at=%f;\n'%(int(offset), float(loc)))
for kk in xrange (int(half_dipoles)):
#bend
loc=loc+space
fid.write('DIP%d:MBS, at=%f;\n'%(int(dip), float(loc)))
dip=dip+1
#defocus
loc=loc+space
fid.write('qd%d:qdType, at=%f;\n'%(int(offset), float(loc)))
for kk in xrange (int(half_dipoles)):
#bend
loc=loc+space
fid.write('DIP%d:MBS, at=%f;\n'%(int(dip), float(loc)))
dip=dip+1
loc=loc+space
fid.write('\n')
###############################################################
offset = offset+1
print 'offset kk = %d= '%int(kk)
print offset
print '\n'
fid.write('/*cell%d*/\n'%(int(offset)))
#focus
fid.write('qf%d:qfType, at=%f;\n'%(int(offset), float(loc)))
#defocus
loc=loc+(straight_drift*space)
fid.write('qd%d:qdType, at=%f;\n'%(int(offset), float(loc)))
loc=loc+(straight_drift*space)
offset = offset+1
fid.write('\n')
print 'End dispersion suppressor'
print '\n'
######## End of dispersion suppressor ########
########################################################################################################################################
for ii in xrange(int(full_cells)):
fid.write('/*cell%d*/\n'%(int(ii+offset)))
#focus
fid.write('qf%d:qfType, at=%f;\n'%(int(ii+offset), float(loc)))
#add sextupole next to quadrupole
fid.write('sf%d:sfType, at=%f;\n'%(int(ii+offset), float(loc)))
for kk in xrange (int(half_dipoles)):
#bend
loc=loc+space
fid.write('DIP%d:MBS, at=%f;\n'%(int(dip), float(loc)))
dip=dip+1
#defocus
loc=loc+space
fid.write('qd%d:qdType, at=%f;\n'%(int(ii+offset), float(loc)))
#add sextupole next to quadrupole
fid.write('sd%d:sdType, at=%f;\n'%(int(ii+offset), float(loc)))
for kk in xrange (int(half_dipoles)):
#bend
loc=loc+space
fid.write('DIP%d:MBS, at=%f;\n'%(int(dip), float(loc)))
dip=dip+1
loc=loc+space
fid.write('\n')
print 'offset jj = %d ii = %d= '%(int(jj),int(ii))
print offset
print '\n'
########################################################################################################################################
######## Dispersion suppressor ########
print 'Begin dispersion suppressor'
print '\n'
offset = offset+ii+1
print 'offset jj = %d ii = %d= '%(int(jj),int(ii))
print offset
print '\n'
fid.write('/*cell%d*/\n'%(int(offset)))
#focus
fid.write('qf%d:qfType, at=%f;\n'%(int(offset), float(loc)))
#defocus
loc=loc+(straight_drift*space)
fid.write('qd%d:qdType, at=%f;\n'%(int(offset), float(loc)))
loc=loc+(straight_drift*space)
fid.write('\n')
###############################################################
offset = offset+1
print 'offset jj = %d ii = %d= '%(int(jj),int(ii))
print offset
print '\n'
fid.write('/*cell%d*/\n'%(int(offset)))
#focus
fid.write('qf%d:qfType, at=%f;\n'%(int(offset), float(loc)))
for kk in xrange (int(half_dipoles)):
#bend
loc=loc+space
fid.write('DIP%d:MBS, at=%f;\n'%(int(dip), float(loc)))
dip=dip+1
#defocus
loc=loc+space
fid.write('qd%d:qdType, at=%f;\n'%(int(offset), float(loc)))
for kk in xrange (int(half_dipoles)):
#bend
loc=loc+space
fid.write('DIP%d:MBS, at=%f;\n'%(int(dip), float(loc)))
dip=dip+1
loc=loc+space
fid.write('\n')
print 'End dispersion suppressor'
print '\n'
######## End of dispersion suppressor ########
########################################################################################################################################
print 'Begin half straight section '
print '\n'
#first half of the drift space
offset = offset+1
print 'offset jj = %d ii = %d= '%(int(jj),int(ii))
print offset
print '\n'
for rr in xrange(straight_cells/2):
fid.write('/*cell%d*/\n'%(int(rr+offset)))
#focus
fid.write('qf%d:qfType, at=%f;\n'%(int(rr+offset), float(loc)))
#defocus
loc=loc+(straight_drift*space)
fid.write('qd%d:qdType, at=%f;\n'%(int(rr+offset), float(loc)))
loc=loc+(straight_drift*space)
fid.write('\n')
offset = offset+rr+1
print 'offset rr = %d= '%int(rr)
print offset
print '\n'
########################################################################################################################################
fid.write('ENDSEQUENCE;\n')
#Set RF cavities
fid.write('CAV1, volt=0.1, lag=0.5, harmon=35000;\n')
fid.write(con)
fid.write(matching_begin)
fid.write('global, sequence = fodo, DQ1 =%f;\n'%(float(DQ1)))
fid.write('global, sequence = fodo, DQ2 =%f;\n'%(float(DQ2)))
fid.write('global, sequence = fodo, Q1 =%f;\n'%(float(Qx_total)))
fid.write('global, sequence = fodo, Q2 =%f;'%(float(Qy_total)))
fid.write(matching_end)
fid.write(plots)
fid.write(twiss)
#change voltage!
fid.write('CAV1, volt=10, lag=0.5, harmon=35000;\n')
#tracking
fid.write('track,file=track_master%d.out,dump;'%(DQ1))
fid.write(track)
fid.write('run,turns=%d;\n'%(TURNS))
fid.write('endtrack;\n')
fid.write('stop;\n')