This repository has been archived by the owner on Feb 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mapclassold.py
374 lines (328 loc) · 12.9 KB
/
mapclassold.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
import sys
from string import split
from operator import *
from numpy import identity, matrix
from definitions import *
import metaclass2
from transport import *
from pytpsa import pol, polmap
from math import *
from multiprocessing import Process
import time
################
def gammln(xx):
###############
g = [0.57236494292474305, 0.0, -0.12078223763524987, -4.4408920985006262e-16, 0.28468287047291829, 0.69314718055994429, 1.2009736023470738, 1.7917594692280547, 2.4537365708424441, 3.1780538303479453, 3.9578139676187165, 4.787491742782044, 5.6625620598571462, 6.5792512120101181, 7.5343642367587762, 8.5251613610654982, 9.5492672573011443, 10.604602902745485, 11.689333420797617, 12.801827480081961, 13.940625219404433, 15.104412573076393, 16.292000476568372, 17.502307845875293, 18.734347511938164, 19.987214495663956, 21.260076156247152, 22.552163853126299, 23.862765841692411, 25.191221182742492, 26.536914491119941, 27.899271383845765, 29.277754515046258, 30.671860106086712, 32.081114895954009, 33.505073450144195, 34.943315776884795, 36.395445208041721, 37.861086508970466, 39.339884187209584]
return g[int(xx / 0.5 - 1)]
#################
def gammlnGOOD(xx):
#################
cof = [76.18009172947146, -86.50532032941677, 24.01409824083091, -1.231739572450155, 0.1208650973866179e-2, -0.5395239384953e-5]
y = x = xx
tmp = x + 5.5
tmp -= (x + 0.5) * log(tmp)
ser = 1.000000000190015
for c in cof:
y = y + 1
ser += c / y
return - tmp + log(2.5066282746310005 * ser / x)
#########################
class Map2(polmap, dct):
#########################
'''
MAP2 coefficients from madx-PTC output
:param int order: calculate map up to this order
:param string filename: input filename
:param twiss2 t: the twiss2 object
:param twiss2 terr: the twiss object that contains the errors assigned to elements from MADX (e.g. misaligments).
Either filename is used or twiss object to construct a Map2
'''
def __init__(self, *args, **kwargs):
if len(args) == 1 and isinstance(args[0], metaclass2.twiss2):
self.fromTwiss(args[0], **kwargs)
else:
self.fromFort(*args, **kwargs)
## Twiss
def fromTwiss(self, t, terr=None, order=6):
EQnL = computeEQ(4, order, X , Y)
EQnLR, EQnLI = separateComplexList(EQnL)
R = generateDefaultMap(order=order)
U = generateDefaultMatrix(order=order)
for i in xrange(len(t.elems)):
e = t.elems[i]
try:
start = time.time()
mtr = metaclass2.matrixForElement(e, order)
if mtr == None:
mp = metaclass2.mapForElement(e, order, EQnLR, EQnLI)
else:
M = mtr * U
mp = metaclass2.matrixToMap(M, XYZD)
end = time.time() - start
print e.KEYWORD, "Generating map", end
# Apply misalignments here if any
if terr is not None:
if isinstance(terr, metaclass2.twiss2):
dx = terr.elems[i].DX
dy = terr.elems[i].DY
if dx != 0:
mp = mp(x=X+dx)
if dy != 0:
mp = mp(y=Y+dy)
else:
raise TypeError("The 'align' attribute has to be of the type 'twiss2'.")
# Combine the map with everything else
tstart = time.time()
R = mp * R
tend = time.time() - tstart
print e.KEYWORD, "Composition" , tend
# print "R", "%.20f" % time()
except Exception:
#print "No implementation for element: ", e.NAME, e.KEYWORD
pass
for k in XYZD:
self[k] = R[k]
# Reorder the variables so that they are always in the same order
# This is important for comparision operations but also for all
# the other methods
self.reorder(XYZD)
for k in XYZD:
print k, "=", self[k]
## fort.18
def fromFort(self, order=6, filename='fort.18'):
ietall = -1
dct = {}
fdct = {}
for line in open(filename):
sline = split(line)
l = len(sline)
if l == 8 or l == 9:
coef = float(sline[1])
if l == 8:
a = [int(sline[3]), int(sline[4]), int(sline[5]), int(sline[6]), int(sline[7])]
else:
a = [int(sline[3]), int(sline[4]), int(sline[5]), int(sline[6]), int(sline[7]), int(sline[8])]
if sum(a) <= order:
dct[tuple(a)] = coef
if "etall" in line:
if ietall >= 0:
p = pol(order=order)
p.fromdict(dct, XYZD)
fdct[XYZD[ietall]] = p
dct = {}
ietall += 1
p = pol()
p.fromdict(dct, XYZD)
fdct[XYZD[ietall]] = p
self.update(fdct)
def offset(self, xory, sig, gaussianDelta=False):
'''
Calculate the beam offset
:param string xory: Which dimension to calculate for (x,y,px, or py)
:param list sig: Initial size of beam (sigma) for [x,px,y,py,d,s]
:param boolean gaussianDelta: Use gaussian energy delta or not
:return: the offset in the desired dimension
'''
sx = 0
if gaussianDelta:
if 'x' in xory:
xory = XYZD[0]
else:
xory = XYZD[2]
for ind, coeff in self[xory].iteritems():
if all(n % 2 == 0 for n in ind):
sigmaprod = self.__sigma(ind, sig, gaussianDelta, dv=2)
if sigmaprod > 0:
Gammasumln = self.__gamma(ind, gaussianDelta)
factor = self.__factor(ind, gaussianDelta)
sx += coeff * factor * exp(Gammasumln) * sigmaprod
return sx
def sigma(self, xory, sig, gaussianDelta=False):
'''
Calculate the beam size in sigma.
:param string xory: Which coordinate to calculate for (x,y,px, or py)
:param list sig: Initial size of beam (sigma) for [x,px,y,py,d,s]
:param boolean gaussianDelta: Use gaussian energy delta or not
:return: sigma for xory dimension
'''
sx = 0
for ind1, coeff1 in self[xory].iteritems():
for ind2, coeff2 in self[xory].iteritems():
if ind1 >= ind2:
countfactor = 2.0
if ind1 == ind2:
countfactor = 1.0
ind = [sum(a) for a in zip(ind1, ind2)]
if all(n % 2 == 0 for n in ind):
sigmaprod = self.__sigma(ind, sig, gaussianDelta)
if sigmaprod > 0:
Gammasumln = self.__gamma(ind, gaussianDelta)
factor = countfactor * self.__factor(ind, gaussianDelta)
sx += coeff1 * coeff2 * factor * exp(Gammasumln) * sigmaprod
return sx
def comp(self, m, v=None):
'''
Compares two maps and returns the chi2. This comparison is only made on the elements for
which d is 0. It takes two parameters: m for the second map to compare it with and v which
is a list of the dimensions to be compared, it defaults to the values in XYZD (see definitions.py).
:param Map2 m: another map
:param list str v: list of variables used to compare the maps by default it's XYZD
:return: chi2
'''
chi2 = 0
if v == None:
v = XYZD[:-2]
for f in v:
p1 = self[f]
p2 = m[f]
if len(p1.keys()[0]) == len(p2.keys()[0]) and\
p1.order == p2.order and\
p1.vars == p2.vars:
for k, v in p1.iteritems():
if k[4] == 0:
chi2 += (v - p2.get(k, 0)) ** 2
else:
print "These maps are not comparable."
return -1
return chi2
def compc(self, m, v=None):
'''
Compares two maps and returns the chi2. It takes two parameters: m for the second map to
compare it with and v which is a list of the dimensions to be compared, it defaults to the
values in XYZD (see definitions.py).
:param Map2 m: another map
:param list str v: list of variables used to compare the maps by default it's XYZD
:return: chi2
'''
chi2 = 0
if v == None:
v = XYZD[-2]
for f in v:
p1 = self[f]
p2 = m[f]
if len(p1.keys()[0]) == len(p2.keys()[0]) and\
p1.order == p2.order and\
p1.vars == p2.vars:
for k, v in p1.iteritems():
chi2 += (v - p2.get(k, 0)) ** 2
else:
print "These maps are not comparable."
return -1
return chi2
#Correlation from mapclass.py originally
def correlation(self, v1, v2, sig, gaussianDelta=False):
'''
It calculates the correlation between two dimensions for some given initial sigmas (parameter
i). Alternatively it can be set to assume a gaussian distribution of the particles.
:param str v1: name of the first dimension to correlate
:param str v2: name of the second dimension to correlate
:param list sig: Initial size of beam (sigma) for [x,px,y,py,d,s]
:param boolean gaussianDelta: Use gaussian energy delta or not
:return: correlation
'''
sx = 0
for ind1, coeff1 in self[v1].iteritems():
for ind2, coeff2 in self[v2].iteritems():
if ind1 >= ind2 or gaussianDelta:
countfactor = 2.0
if ind1 == ind2 or gaussianDelta:
countfactor = 1.0
ind = [sum(a) for a in zip(ind1, ind2)]
if all(n % 2 == 0 for n in ind):
sigmaprod = self.__sigma(ind, sig, gaussianDelta)
if sigmaprod > 0:
Gammasumln = self.__gamma(ind, gaussianDelta)
factor = countfactor * self.__factor(ind, gaussianDelta)
sx += coeff1 * coeff2 * factor * exp(Gammasumln) * sigmaprod
return sx
#Correlation3 from mapclass.GaussianDelta.py
def correlation3(self, v1, v2, v3, sig, gaussianDelta=False):
'''
It calculates the correlation between two dimensions for some given initial sigmas (parameter
i). Alternatively it can be set to assume a gaussian distribution of the particles.
:param str v1: name of the first dimension to correlate
:param str v2: name of the second dimension to correlate
:param str v3: name of the third dimension to correlate
:param list sig: Initial size of beam (sigma) for [x,px,y,py,d,s]
:param boolean gaussianDelta: Use gaussian energy delta or not
:return: correlation
'''
sx = 0
for ind1, coeff1 in self[v1].iteritems():
for ind2, coeff2 in self[v2].iteritems():
for ind3, coeff3 in self[v3].iteritems():
countfactor = 1.0
ind = [sum(a) for a in zip(ind1, ind2, ind3)]
if all(n % 2 == 0 for n in ind):
sigmaprod = self.__sigma(ind, sig, gaussianDelta)
if sigmaprod > 0:
Gammasumln = self.__gamma(ind, gaussianDelta)
factor = countfactor * self.__factor(ind, gaussianDelta)
sx += coeff1 * coeff2 * coeff3 * factor * exp(Gammasumln) * sigmaprod
return sx
def generatelist(self, xory, sig, gaussianDelta=False):
'''
Provides a list of the largest contributions to the sigmas assuming a Gaussian distribution
or a uniform one.
:param str xory: name of the dimension
:param list sig: Initial size of beam (sigma) for [x,px,y,py,d,s]
:param boolean gaussianDelta: Use gaussian energy delta or not
:return: list of contributions
'''
sx = 0
l = []
if gaussianDelta:
if 'x' in xory:
xory = XYZD[0]
else:
xory = XYZD[2]
for ind1, coeff1 in self[xory].iteritems():
for ind2, coeff2 in self[xory].iteritems():
if ind1 >= ind2:
countfactor = 2.0
if ind1 == ind2:
countfactor = 1.0
ind = [sum(a) for a in zip(ind1, ind2)]
if all(n % 2 == 0 for n in ind):
sigmaprod = self.__sigma(ind, sig, gaussianDelta)
if sigmaprod > 0:
Gammasumln = self.__gamma(ind, gaussianDelta)
factor = countfactor * self.__factor(ind, gaussianDelta)
sxt = coeff1 * coeff2 * factor * exp(Gammasumln) * sigmaprod
l.append([-abs(sxt), sxt] + list(ind1) + list(ind2))
l.sort()
return l
#Auxiliary functions (private)
def __sigma(self, ind, i, gaussianDelta, dv=1):
if gaussianDelta:
# sigmaprod = pow(i[0], ind[0])*pow(i[1], ind[1])*pow(i[2], ind[2])*pow(i[3], ind[3])*pow(i[4], ind[4])
sigmaprod = reduce(mul, map(pow, i, ind))
else:
# sigmaprod = pow(i[0], ind[0])*pow(i[1], ind[1])*pow(i[2], ind[2])*pow(i[3], ind[3])*pow(i[4]/2., ind[4])
qq = 1
if len(ind) > 5:
qq = pow(i[5] / dv, ind[5])
sigmaprod = reduce(mul, map(pow, i[:4], ind[:4])) * pow(i[4] / 2., ind[4]) * qq
return sigmaprod
def __gamma(self, ind, gaussianDelta):
if gaussianDelta:
# Gammasumln = gammln(0.5+ind[0]/2.)+gammln(0.5+ind[1]/2.)+gammln(0.5+ind[2]/2.)+gammln(0.5+ind[3]/2.)+gammln(0.5+ind[4]/2.)
Gammasumln = reduce(add, map(lambda x: gammln(0.5 + x / 2.), ind))
else:
indl = list(ind)
del indl[4]
Gammasumln = reduce(add, map(lambda x: gammln(0.5 + x / 2.), indl))
return Gammasumln
def __factor(self, ind, gaussianDelta, poten=2.):
l = len(ind)
if gaussianDelta:
if l == 5:
poten = 2.5
if l == 6:
poten = 3.0
factor = pow(2, sum(ind) / 2.) / pow(pi, poten)
else:
if l > 5:
poten = 2.5
factor = pow(2, sum(ind[:4] + ind[5:]) / 2) / pow(pi, poten) / (ind[4] + 1.0)
return factor