-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyCOBRA_base.py
129 lines (89 loc) · 4.51 KB
/
PyCOBRA_base.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
from abc import ABCMeta, abstractmethod
import numpy as np
from scipy.constants import e, m_p
def gaussian_generator(eps_geo, phase_space_tuple=('x', 'xp'), alpha=0, beta=1):
sigma = np.sqrt(eps_geo)
def generate(bunch):
n_macroparticles = bunch.n_macroparticles
x = np.random.normal(scale=sigma, size=n_macroparticles)
xp = np.random.normal(scale=sigma, size=n_macroparticles)
M = np.array([[np.sqrt(beta), 0],
[-alpha/np.sqrt(beta), 1./np.sqrt(beta)]])
x, xp = M[0,0]*x + M[0,1]*xp, M[1,0]*x + M[1,1]*xp
setattr(bunch, phase_space_tuple[0], x)
setattr(bunch, phase_space_tuple[1], xp)
return generate
class Bunch(object):
def __init__(self, n_macroparticles,
weight=1, charge=e, mass=m_p, gamma=1,
*phase_space_generators):
self.n_macroparticles = n_macroparticles
self.weight = weight
self.charge = charge
self.mass = mass
self.gamma = gamma
[generate(self) for generate in phase_space_generators]
def emittance_normalised(self, x, xp):
return np.sqrt(self.gamma**2 - 1) * \
np.sqrt( np.std(x**2)*np.std(xp**2) - np.std(x*xp)**2 )
def epsn_x(self):
return emittance_normalised(self.x, self.xp)
def epsn_y(self):
return emittance_normalised(self.y, self.yp)
def epsn_z(self):
return emittance_normalised(self.z, self.dp)
class Beam(object):
def __init__(self, bunches_list):
self.n_macroparticles = sum([b.n_macroparticles for b in bunches_list])
self.weight = np.concatenate(b.weight for b in bunches_list)
self.charge = np.concatenate(b.charge for b in bunches_list)
self.mass = np.concatenate(b.mass for b in bunches_list)
self.gamma = np.concatenate(b.gamma for b in bunches_list)
self.x = np.concatenate(b.x for b in bunches_list)
self.xp = np.concatenate(b.xp for b in bunches_list)
self.y = np.concatenate(b.y for b in bunches_list)
self.yp = np.concatenate(b.yp for b in bunches_list)
self.z = np.concatenate(b.z for b in bunches_list)
self.dp = np.concatenate(b.dp for b in bunches_list)
class MachineElement(object):
__metaclass__ = ABCMeta
@abstractmethod
def kick(self, beam):
pass
class TwissMap(MachineElement):
def __init__(self, plane='x',
alpha_0=0, beta_0=100, alpha_1=0, beta_1=100, dmu=0,
*detuners):
B = np.array([[1./np.sqrt(beta_0), 0],
[alpha_0/np.sqrt(beta_0), np.sqrt(beta_0)]])
R = np.array([[np.cos(dmu), np.sin(dmu)],
[-np.sin(dmu), np.cos(dmu)]])
B_inv = np.array([[np.sqrt(beta_1), 0],
[-alpha_1/np.sqrt(beta_1), 1./np.sqrt(beta_1)]])
I = np.array([[1, 0],
[0, 1]])
S = np.array([[0, 1],
[-1, 0]])
self.dmu = dmu
self.M = np.dot(B_inv, np.dot(R, B))
self.C = np.dot(B_inv, np.dot(I, B))
self.S = np.dot(B_inv, np.dot(S, B))
def kick(self, beam):
if self.plane=='x':
beam.x, beam.xp = (self.C[0,0]*np.cos(self.dmu) + self.S[0,0]*np.sin(self.dmu)) * self.x \
+ (self.C[0,1]*np.cos(self.dmu) + self.S[0,1]*np.sin(self.dmu)) * self.xp, \
(self.C[1,0]*np.cos(self.dmu) + self.S[1,0]*np.sin(self.dmu)) * self.x \
+ (self.C[1,1]*np.cos(self.dmu) + self.S[1,1]*np.sin(self.dmu)) * self.xp
if self.plane=='y':
beam.y, beam.yp = (self.C[0,0]*np.cos(self.dmu) + self.S[0,0]*np.sin(self.dmu)) * self.y \
+ (self.C[0,1]*np.cos(self.dmu) + self.S[0,1]*np.sin(self.dmu)) *self.yp, \
+ (self.C[1,0]*np.cos(self.dmu) + self.S[1,0]*np.sin(self.dmu)) * self.y \
+ (self.C[1,1]*np.cos(self.dmu) + self.S[1,1]*np.sin(self.dmu))* self.yp
if self.plane=='z':
beam.z, beam.dp = (self.C[0,0]*np.cos(self.dmu) + self.S[0,0]*np.sin(self.dmu)) * self.z \
+ (self.C[0,1]*np.cos(self.dmu) + self.S[0,1]*np.sin(self.dmu)) *self.dp, \
+ (self.C[1,0]*np.cos(self.dmu) + self.S[1,0]*np.sin(self.dmu)) * self.z \
+ (self.C[1,1]*np.cos(self.dmu) + self.S[1,1]*np.sin(self.dmu))* self.dp
class RFMap(MachineElement):
def __init__(self, V, h, dphi):
pass