forked from pylhc/Python_Classes4MAD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metaclass.py
491 lines (427 loc) · 18.7 KB
/
metaclass.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
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
# version 1 unknown/unknown
# version 2 20120905 (tbach):
# - major refactoring
# - cleared indentation
# - moved help section in comments to top and docstring
# - removed all exec statements (replaced with setattr) (still not good)
# - added class attributes for all unknown members
# - declared constants for PI, E, I
# - cleaned up imports
# - removed all trailing ";"
# - tested it for getllm/lhc, produces exactly same results
# (getllm called only Cmatrix and chiterms functions))
#
"""
Read the twiss class from the twiss file
x=twiss('twiss')
use it as:
print x.Q1, x.Q2, x.BETX[0]
run beaMatrix for example:
x.beatMatrix()
print x.RM[0]
BETA-BEAT CORRECTION
first compute the response matrix by:
x.beatMatrix()
Define targetbeat as an array containing the desired changed in Dbeta/beta (x,y)
targetbeat=Dbeta/beta
dkl gives the required integrated strengths by:
dkl=matrixmultiply(generalized_inverse(x.RM,0.003),targetbeat)
Want to explore the singular values?:
svd=singular_value_decomposition(x.RM)
Computing SEXTUPOLAR RESONANCE TERMS:
x.fterms()
The fterms are arrays evaluated at all the elements:
print x.f3000 , x.f2100 , x.f1020, x.f1002
COUPLING
Compute the Cmatrix, gamma, f1001 and f1010 from the Twiss file at all elements
x.Cmatrix()
print x.C[0] (four components of C at the first elements)
print x.f1001
...
"""
import numpy
from numpy import dot as matrixmultiply
from numpy.linalg import inv as inverse
from numpy.linalg import det as determinant
import sys
I = complex(0, 1)
E = numpy.e
PI = numpy.pi
class twiss:
"""Twiss parameters from madx output (with free choice of select items)"""
def forknames(self, dictionary):
NAME = getattr(self, "NAME")
for n in dictionary:
if n in NAME:
for m in dictionary[n]:
self.indx[m] = self.indx[n]
self.indx[m.upper()] = self.indx[n]
else:
print "skipped value from dictionary because not in NAME. value: ", n
def __init__(self, filename, dictionary=None):
if dictionary is None:
dictionary = {}
self.filename = filename # Added to see which file it is during debugging (vimaier)
self.__has_parsed_a_table_row = False
self.indx = {}
self.keys = []
alllabels = []
if filename.endswith(".gz"):
import gzip
f = gzip.open(filename, 'rb')
else:
f = open(filename, 'r')
for line in f:
is_line_parsed = False # Check if line was parsed otherwise print info (vimaier)
if line.startswith("#"): # comment line
continue
if ("@ " not in line and "@" in line):
line = line.replace("@" , "@ ")
split_line = line.split()
if ("@ " in line and "%" in line and "s" not in split_line[2]):
# Float-Descriptor-line
label = split_line[1]
try:
setattr(self, label, float(split_line[3].replace("\"", "")))
except:
print "Problem parsing:", line,
print "Going to be parsed as string"
try:
setattr(self, label, split_line[3].replace("\"", ""))
except:
print "Problem persists, let's ignore it!"
is_line_parsed = True
elif ("@ " in line and "s" in split_line[2]):
# String-Descriptor-line
label = split_line[1].replace(":", "")
setattr(self, label, split_line[3].replace("\"", ""))
is_line_parsed = True
if ("* " in line or "*\t" in line):
# Columns-names-line
alllabels = split_line
for alllabels_item in alllabels[1:]:
setattr(self, alllabels_item, [])
self.keys.append(alllabels_item)
is_line_parsed = True
if ("$ " in line or "$\t" in line):
# Columns-datatypes-line
alltypes = split_line
is_line_parsed = True
if ("@" not in line and "*" not in line and "$" not in line and "#" not in line):
# Table-entry-line
values = split_line
for j in range(0,len(values)):
if ("%hd" in alltypes[j + 1] or "%d" in alltypes[j + 1] ):
getattr(self, alllabels[j + 1]).append(int(values[j]))
if ("%le" in alltypes[j + 1]):
getattr(self, alllabels[j + 1]).append(float(values[j]))
if ("s" in alltypes[j+1]):
getattr(self, alllabels[j + 1]).append(values[j].replace("\"", ""))
if "NAME" == alllabels[j + 1]:
NAME = getattr(self, "NAME")
self.indx[values[j].replace("\"", "")] = len(NAME) - 1
self.indx[values[j].replace("\"", "").upper()] = len(NAME) - 1
self.indx[values[j].replace("\"", "").lower()] = len(NAME) - 1
self.__has_parsed_a_table_row = True
is_line_parsed = True
if not is_line_parsed:
print >> sys.stderr,"Did not parse line ("," ".join(split_line),") in ",filename
f.close()
try:
alltypes
except:
print >> sys.stderr, "From Metaclass: Bad format or empty file ", filename
raise ValueError
for j in range(1, len(alllabels)):
if (("%le" in alltypes[j]) | ("%hd" in alltypes[j])):
setattr(self, alllabels[j], numpy.array(getattr(self, alllabels[j])))
if len(dictionary) > 0:
self.forknames(dictionary)
def has_bpm_data(self):
return self.__has_parsed_a_table_row
def has_no_bpm_data(self):
return not self.has_bpm_data()
def chrombeat(self):
'''
Add dbx/dby to the twiss table
'''
self.dbx = []
self.dby = []
S = getattr(self, "S")
WX = getattr(self, "WX")
WY = getattr(self, "WY")
PHIX = getattr(self, "PHIX")
PHIY = getattr(self, "PHIY")
for i in range(0, len(S)):
ax = WX[i] * numpy.cos(PHIX[i] * 2 * PI)
ay = WY[i] * numpy.cos(PHIY[i] * 2 * PI)
self.dbx.append(ax)
self.dby.append(ay)
def fterms(self):
'''
Add f terms to the twiss table
'''
self.f3000 = []
self.f2100 = []
self.f1020 = []
self.f1002 = []
self.f20001 = []
self.f1011 = []
self.f4000 = []
self.f2000 = []
S = getattr(self, "S")
MUX = getattr(self, "MUX")
MUY = getattr(self, "MUY")
Q1 = getattr(self, "Q1")
Q2 = getattr(self, "Q2")
K1L = getattr(self, "K1L")
K2L = getattr(self, "K2L")
K3L = getattr(self, "K3L")
BETX = getattr(self, "BETX")
BETY = getattr(self, "BETY")
DX = getattr(self, "DX")
for i in range(0, len(S)):
phix = MUX - MUX[i]
phiy = MUY - MUY[i]
for j in range(0, i):
phix[j] += Q1
phiy[j] += Q2
dumm = -sum(K2L * BETX ** 1.5 * E ** (3 * I * 2 * PI * phix)) / 48.
self.f3000.append(dumm / (1. - E ** (3 * I * 2 * PI * Q1)))
dumm = -sum(K2L * BETX ** 1.5 * E ** (I * 2 * PI * phix)) / 16.
self.f2100.append(dumm / (1. - E ** (I * 2 * PI * Q1)))
dumm = sum(K2L * BETX ** 0.5 * BETY * E ** (I * 2 * PI * (phix + 2 * phiy))) / 8.
self.f1020.append(dumm / (1. - E ** (I * 2 * PI * (Q1 + 2 * Q2))))
dumm = sum(K2L * BETX ** 0.5 * BETY * E ** (I * 2 * PI * (phix - 2 * phiy))) / 8.
self.f1002.append(dumm / (1. - E ** (I * 2 * PI * (Q1 - 2 * Q2))))
dumm = sum((K1L - 2 * K2L * DX) * BETX * E ** (2 * I * 2 * PI * phix)) / 8.
self.f20001.append(dumm / (1. - E ** (2 * I * 2 * PI * Q1)))
dumm = sum(K2L * BETX ** 0.5 * BETY * E ** (I * 2 * PI * (phix))) / 4.
self.f1011.append(dumm / (1. - E ** (I * 2 * PI * Q1)))
dumm = -sum(K3L * BETX ** 2 * E ** (4 * I * 2 * PI * (phix))) / 384.
self.f4000.append(dumm / (1. - E ** (4 * I * 2 * PI * Q1)))
dumm = -sum(K1L * BETX ** 1 * E ** (2 * I * 2 * PI * phix)) / 32.
self.f2000.append(dumm / (1. - E ** (2 * I * 2 * PI * Q1)))
self.f3000 = numpy.array(self.f3000)
self.f2100 = numpy.array(self.f2100)
self.f1020 = numpy.array(self.f1020)
self.f1002 = numpy.array(self.f1002)
self.f1011 = numpy.array(self.f1011)
self.f0120 = numpy.conjugate(self.f1002)
self.f0111 = numpy.conjugate(self.f1011)
self.f1200 = numpy.conjugate(self.f2100)
self.fRS3 = 3 * self.f3000 - self.f2100
self.fRS2 = self.f1020 - self.f0120
self.fRS1 = 2 * self.f1020 - self.f1011
self.fRS1 = 2 * self.f0120 - self.f0111
def chiterms(self, ListOfBPMS=None):
'''
Add chi terms to the twiss table
'''
if ListOfBPMS is None:
ListOfBPMS = []
factMADtoSix = 0.0005
self.chi3000 = []
self.chi4000 = []
self.chi2000 = []
NAME = getattr(self, "NAME")
S = getattr(self, "S")
MUX = getattr(self, "MUX")
K1L = getattr(self, "K1L")
K2L = getattr(self, "K2L")
K3L = getattr(self, "K3L")
BETX = getattr(self, "BETX")
if len(ListOfBPMS) == 0:
print "Assuming that BPM elements are named as BP and H"
for el in NAME:
if "BP" in el and "H" in el:
ListOfBPMS.append(el)
print "Found ", len(ListOfBPMS), "BPMs for chiterms computation"
if len(ListOfBPMS) < 3:
print "Error, not enough H BPMs in ListOfBPMs"
sys.exit(1)
self.chi = []
self.chiBPMs = []
self.chiS = []
for i in range(len(ListOfBPMS) - 2):
name = ListOfBPMS[i]
name1 = ListOfBPMS[i + 1]
name2 = ListOfBPMS[i + 2]
self.chiBPMs.append([name, name1, name2])
indx = self.indx[name]
indx1 = self.indx[name1]
indx2 = self.indx[name2]
bphmii = MUX[indx]
bphmii1 = MUX[indx1]
bphmii2 = MUX[indx2]
bphs = S[indx]
bphs1 = S[indx1]
bphs2 = S[indx2]
self.chiS.append([bphs, bphs1, bphs2])
d1 = (bphmii1 - bphmii) * 2 * PI - PI / 2
d2 = (bphmii2 - bphmii1) * 2 * PI - PI / 2
f1 = numpy.sqrt(1 + (numpy.sin(d1) / numpy.cos(d1)) ** 2)
f2 = numpy.sqrt(1 + (numpy.sin(d2) / numpy.cos(d2)) ** 2)
quadr = 0
quadi = 0
sexr = 0
sexi = 0
octr = 0
octi = 0
for j in range(len(NAME)):
k1l = K1L[j]
k2l = K2L[j]
k3l = K3L[j]
bx = BETX[j]
m = MUX[j]
if S[j] > bphs and S[j] < bphs1 and k2l ** 2 > 0:
quadr += numpy.cos(-1 * (m - bphmii) * 2 * PI) * numpy.sin((m - bphmii) * 2 * PI) * k1l * bx ** 1 * f1
quadi += numpy.sin(-1 * (m - bphmii) * 2 * PI) * numpy.sin((m - bphmii) * 2 * PI) * k1l * bx ** 1 * f1
sexr += numpy.cos(-2 * (m - bphmii) * 2 * PI) * numpy.sin((m - bphmii) * 2 * PI) * k2l * bx ** 1.5 * f1
sexi += numpy.sin(-2 * (m - bphmii) * 2 * PI) * numpy.sin((m - bphmii) * 2 * PI) * k2l * bx ** 1.5 * f1
octr += numpy.cos(-3 * (m - bphmii) * 2 * PI) * numpy.sin((m - bphmii) * 2 * PI) * k3l * bx ** 2 * f1
octi += numpy.sin(-3 * (m - bphmii) * 2 * PI) * numpy.sin((m - bphmii) * 2 * PI) * k3l * bx ** 2 * f1
if S[j] > bphs1 and S[j] < bphs2 and k2l ** 2 > 0:
quadr += numpy.cos(-1 * (m - bphmii) * 2 * PI) * numpy.sin((m - bphmii) * 2 * PI - d1 - d2) * k1l * bx ** 1 * f2
quadi += numpy.sin(-1 * (m - bphmii) * 2 * PI) * numpy.sin((m - bphmii) * 2 * PI - d1 - d2) * k1l * bx ** 1 * f2
sexr += numpy.cos(-2 * (m - bphmii) * 2 * PI) * numpy.sin((m - bphmii) * 2 * PI - d1 - d2) * k2l * bx ** 1.5 * f2
sexi += numpy.sin(-2 * (m - bphmii) * 2 * PI) * numpy.sin((m - bphmii) * 2 * PI - d1 - d2) * k2l * bx ** 1.5 * f2
octr += numpy.cos(-3 * (m - bphmii) * 2 * PI) * numpy.sin((m - bphmii) * 2 * PI - d1 - d2) * k3l * bx ** 2 * f2
octi += numpy.sin(-3 * (m - bphmii) * 2 * PI) * numpy.sin((m - bphmii) * 2 * PI - d1 - d2) * k3l * bx ** 2 * f2
if S[j] > bphs2:
break
self.chi.append(complex(sexr, sexi) / 4 * factMADtoSix)
self.chi4000.append(complex(octr, octi) / 4 * factMADtoSix)
self.chi2000.append(complex(quadr, quadi) / 4 * factMADtoSix)
def Cmatrix(self):
'''
Calculate the C matrix
'''
self.C = []
self.gamma = []
self.f1001 = []
self.f1010 = []
S = getattr(self, "S")
R11 = getattr(self, "R11")
R12 = getattr(self, "R12")
R21 = getattr(self, "R21")
R22 = getattr(self, "R22")
BETX = getattr(self, "BETX")
BETY = getattr(self, "BETY")
ALFX = getattr(self, "ALFX")
ALFY = getattr(self, "ALFY")
J = numpy.reshape(numpy.array([0, 1, -1, 0]), (2, 2))
for j in range(0, len(S)):
R = numpy.array([[R11[j], R12[j]], [R21[j], R22[j]]])
C = matrixmultiply(-J, matrixmultiply(numpy.transpose(R), J))
C = (1 / numpy.sqrt(1 + determinant(R))) * C
g11 = 1 / numpy.sqrt(BETX[j])
g12 = 0
g21 = ALFX[j] / numpy.sqrt(BETX[j])
g22 = numpy.sqrt(BETX[j])
Ga = numpy.reshape(numpy.array([g11, g12, g21, g22]), (2, 2))
g11 = 1 / numpy.sqrt(BETY[j])
g12 = 0
g21 = ALFY[j] / numpy.sqrt(BETY[j])
g22 = numpy.sqrt(BETY[j])
Gb = numpy.reshape(numpy.array([g11, g12, g21, g22]), (2, 2))
C = matrixmultiply(Ga, matrixmultiply(C, inverse(Gb)))
gamma = 1 - determinant(C)
self.gamma.append(gamma)
C = numpy.ravel(C)
self.C.append(C)
self.f1001.append(((C[0] + C[3]) * 1j + (C[1] - C[2])) / 4 / gamma)
self.f1010.append(((C[0] - C[3]) * 1j + (-C[1] - C[2])) / 4 / gamma)
self.F1001R = numpy.array(self.f1001).real
self.F1001I = numpy.array(self.f1001).imag
self.F1010R = numpy.array(self.f1010).real
self.F1010I = numpy.array(self.f1010).imag
self.F1001W = numpy.sqrt(self.F1001R ** 2 + self.F1001I ** 2)
self.F1010W = numpy.sqrt(self.F1010R ** 2 + self.F1010I ** 2)
def beatMatrix(self):
'''
Add RM to the twiss table
'''
self.RM = []
S = getattr(self, "S")
MUX = getattr(self, "MUX")
MUY = getattr(self, "MUY")
Q1 = getattr(self, "Q1")
Q2 = getattr(self, "Q2")
BETX = getattr(self, "BETX")
BETY = getattr(self, "BETY")
for j in range(0, len(S)):
self.RM.append(-BETX * numpy.cos(2 * PI * (Q1 - 2 * abs(MUX[j] - MUX))) / numpy.sin(2 * PI * Q1))
for j in range(0, len(S)):
self.RM.append(-BETY * numpy.cos(2 * PI * (Q2 - 2 * abs(MUY[j] - MUY))) / numpy.sin(2 * PI * Q2))
self.RM = numpy. array(self.RM)
def abh(self, bet1, alf1, KL, K):
""" bet1 and alf1 at the end of the element """
gamma1 = (1. + alf1 ** 2) / bet1
KL2 = 2.*KL
sinhc = numpy.sinh(KL2) / KL2
res = 0.5 * bet1 * (1. + sinhc) + alf1 * numpy.sinh(KL) ** 2. / KL / K + (sinhc - 1.) / (2.*K ** 2.) * gamma1
return res
def ab(self, bet1, alf1, KL, K):
""" bet1 and alf1 at the end of the element """
gamma1 = (1. + alf1 ** 2) / bet1
KL2 = 2.*KL
sinc = numpy.sin(KL2) / KL2
res = 0.5 * bet1 * (1. + sinc) + alf1 * numpy.sin(KL) ** 2. / KL / K + (1. - sinc) / (2.*K ** 2.) * gamma1
return res
def AveBetas(self):
totx = 0
toty = 0
totl = 0
S = getattr(self, "S")
L = getattr(self, "L")
K1L = getattr(self, "K1L")
BETX = getattr(self, "BETX")
BETY = getattr(self, "BETY")
ALFX = getattr(self, "ALFX")
ALFY = getattr(self, "ALFY")
NAME = getattr(self, "NAME")
for i in range(len(S)):
if L[i] > 0:
k = numpy.sqrt(abs(K1L[i]) / L[i])
kL = k * L[i]
if K1L[i] == 0:
bxs = BETX[i] * L[i] + ALFX[i] * L[i] ** 2 + (1 + ALFX[i] ** 2) / BETX[i] * L[i] ** 3 / 3.
bys = BETY[i] * L[i] + ALFY[i] * L[i] ** 2 + (1 + ALFY[i] ** 2) / BETY[i] * L[i] ** 3 / 3.
totx = totx + bxs
toty = toty + bys
totl = totl + L[i]
print NAME[i], S[i], L[i], bxs, bys
if K1L[i] > 0.0:
bxs = self.ab(BETX[i], ALFX[i], kL, k) * L[i]
totx = totx + bxs
bys = self.abh(BETY[i], ALFY[i], kL, k) * L[i]
toty = toty + bys
totl = totl + L[i]
abx=self.ab(BETX[i], ALFX[i], kL, k)
aby= self.abh(BETY[i], ALFY[i], kL, k)
print NAME[i], S[i], L[i], abx, aby, abx*K1L[i],aby*K1L[i]
if K1L[i] < 0:
bxs = self.abh(BETX[i], ALFX[i], kL, k) * L[i]
totx = totx + bxs
bys = self.ab(BETY[i], ALFY[i], kL, k) * L[i]
toty = toty + bys
totl = totl + L[i]
abx=self.abh(BETX[i], ALFX[i], kL, k)
aby=self.ab(BETY[i], ALFY[i], kL, k)
print NAME[i], S[i], L[i], abx, aby, abx*K1L[i],aby*K1L[i]
else:
print NAME[i], S[i], L[i], BETX[i], BETY[i]
print "TOTAL", S[i], totl, totx, toty
def I5(self):
H = 0
NAME = getattr(self, "NAME")
DX = getattr(self, "DX")
DPX = getattr(self, "DPX")
BETX = getattr(self, "BETX")
ALFX = getattr(self, "ALFX")
ANGLE = getattr(self, "ANGLE")
L = getattr(self, "L")
for i in range(len(NAME)):
H = H + (DX[i] ** 2 + (DPX[i] * BETX[i] + DX[i] * ALFX[i]) ** 2) / BETX[i] * (abs(ANGLE[i])) ** 3 / L[i] ** 2
return H