Skip to content

Commit 93a0d8a

Browse files
committed
Merge branch 'release_merge' of https://github.com/like2000/PyHEADTAIL
Conflicts: cobra_functions/__init__.py cobra_functions/stats.pyx impedances/__init__.py monitors/__init__.py solvers/__init__.py trackers/__init__.py
2 parents 5be1610 + 759a38e commit 93a0d8a

File tree

113 files changed

+12240
-372251
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+12240
-372251
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ scripts/
1010
#*
1111
.project
1212
.pydevproject
13+
*.h5
14+
*.h5part
15+
.ipynb_checkpoints

__init__.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from ._version import __version__
2+
from general.element import Element, Printing
3+
from particles.slicing import clean_slices
4+
# print ' '
5+
# print ' ;Cfttttt11111111tttt1f0f. '
6+
# print ' ,GttttfG0GGG00000GGG0G0G0G0G0G0Ct1t1fG: '
7+
# print ' ,GtttLG000GGGGGGGGGGG00GGGGGGGGGGGGGG0GGG0GCtttLt '
8+
# print ' .CtttC0G00GGGGGGGGGGG00t1t1GGGGGGGGGGGGGGGGGG0GG0G00Gtttt '
9+
# print ' G1tf000GGGGGGGGGG000811tGG0GL0GGGGGGGGGGGGGGGGGGGGGGGGGGGGG11C '
10+
# print ' GtttGG0G00GGGGGGGGG08t1GGG0;;0G000GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGtt: '
11+
# print ' 01tGG00GGGGGGGGGGGG0tt000ft::::GGG00GGGGGGGGGGGGGGGGGGG0GGG0G0000000GftG '
12+
# print ' L1tLf. 8ttGGGGGGGGGGGGGG00GtGG8fft:::::;0GG@0GGGGGGGGGGGGG00G088888888888GttfLLLt1C '
13+
# print ' CtG0G0GG8888t.G1tGGGGGGGGGGGGGGG0fGG0ffff;:::::::0G08GGGGGGGGGGGG888888888888f '
14+
# print ' .tt0GGGGG0888Ctf0GGGGGGGGGGGG0G0000GGGG0Gt;:;:::;0GG88GGGGG8808888888888C '
15+
# print ' C1GGGGGG0G01t00GGGGGGGGGGGGG00000000GGGGGGGG;::;GGG80GGGGGGGG0Lt1t@@i '
16+
# print ' 1tGGGGG0811G00GGGGGGGGGGGGGGGG0880G0GGGGGG0G0;L0G08GGGGGGGGGGG0GGG1ttf '
17+
# print ' t1t0GG08t1G0GGGGGGGGGGGGG0GtLGGGG0GLtGGGGGGGGG8G08GGGGGGGGGGGGGGGGG00tttf '
18+
# print ' 0tCGG8tt00GGGGGGGGG0G0CtG0GG0GGG000GGt00GGGG000GGGGGGGGGGGGGGGGGGGGGG0G1tG '
19+
# print ' 0G0G81G0GGGGGGGGGG0Gf00GG8888@8@80GGG0CGGG0GGGGGGGGGGGGGGGGGGGGGGGGGG000ttL '
20+
# print ' C11GG080GGGGGGGG00t0GG08@111: .i1t80GG08GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGt1C '
21+
# print ' f0@tGGGGGGGGGGG00C0GG88i1 1GG0G0GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG0GGttC '
22+
# print ' G88ffG0GGGGGG0G0GGG0811. .0GG08GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG0Gt1L '
23+
# print ' C .C .0G1800GGGGGG0G00f0811 .0G00GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG0G11i '
24+
# print ' L 1 Lt 11GGGGGGG000ft81: .8; 0G0GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG0GG001t0 '
25+
# print ' L: .C 1 f t 8CG00GGGG0G181, 0G00 00G0GG0GGGGGGGGGGGGGGGGGGGGGGGG0GGGGGGGGGGGGtt8 '
26+
# print ' f. . .;iC .. C .tGLG0GGGGt01. 0, Gi 0GGGGGGGGGGGGGGGGGGG0GGGGGGGGGGGGGGGGGGGGGGGGtt8 '
27+
# print ' CL0t .G1111 : G 1 08t8L0000t11: 00@8L t0GGGGGGGGGGGGGGGGGGG80GGGGGGGGGGGGGGGGGGGGGGGG11L '
28+
# print ' 1 C1 i f , :,0C10G0Ltf: :C8@1 :00GGGGGGGGGGGGGGGGGGGGG8888880000GGGGGGGGGGGGGGCtt. '
29+
# print ' ,1G:C111. .1 i i i 0@80ttt10i G111 fGGG0GGGGGGGGGGGGGGGGGGGGG8888888888GGG0GGGGGGGGGGtt0 '
30+
# print ' ; tL1 01 ; Gf C 00. LG1 f f;;:::;;:8GGGGGGGGGGGGGGG0GGGt888888888880GGGGGGGG00Gtt. '
31+
# print ' i1i C1 i111 . C8@G i0,G;::;C1 .t;:;:;::::::0GGGG00GGGGGGGGGGG0G11 .G8@8888880GGGGGGGGGL1G '
32+
# print ' L111tt1111. 1; 1111. . @888@@888:;:;;:;;;0. :t::::;: :::::8GG88GGGGG0GGGGGGGGGC1, 8888@8800GGGGGG1t '
33+
# print ' 8 C1 f L11i ;. 1. ;.:;:::::::::::::::::::::;; ;;:::10G880GGGGGGGGGGGGGG0GL1 ,88888G0GGG0f1. '
34+
# print ' G .i, .1 G ,1110f L.::::::::::::::::::::;. i.L;;:;LG88888G0GGGGGGGGGGGGGG0tf ,88880GGGGt; '
35+
# print ' t111111; :11 f11L G .1.::::::::::::;CC;:;:;:::;;C88888888880GGGGGGGGGGGGG0G1G 0@8000G1t '
36+
# print ' L1Cf011. ;11 .11G t1.L ,L,.::::::;:::::;:::C@888888888888888G000GGGGGGGGGGt1 f880GtL '
37+
# print ' .G111111111. .1t. 111.. .:LG8888@88088@0tfL@G t88888888880GGGGGGGGG0GCti CC0tL '
38+
# print ' ;L111111110. .1C i :..::;ft888888888ffffffftt8. 08888880GGGGGGGGGGGtG tti '
39+
# print ' G11i,,,,. 0 :L..;:::::;ffC88888888fffi::::::..C; C88888GGG0GGGG0GtC f. '
40+
# print ' .Gf11111. :Cff.C ,tftL.,;:::::::::;f888888888881:;:::::::..i. 888800GGGGGG01L '
41+
# print ' i1111111111ffft;:.....::ft..:::::::::::::0G000GGGG8888880f;;;::::.. 88880GGGGGGtf '
42+
# print ' .. iffff;:::::::;ttC.::::::::::::::0GGGG0GC888888800t G::::;.G @88GGGGGGtf '
43+
# print ' . G::::::;1: L.;:;:::::::::::GG0G0Gf888888 Gi10::::;:. .;@8GGGGGtC '
44+
# print ' ,.:::::::::::::;0GG1 L888 1L fG:;:::::. 880GGG1C '
45+
# print ' .,:::::::::::;80i...... 1G tf;:::.; 880001. '
46+
# print ' :.:::::::::::t. . GftftC,G G0Gtt '
47+
# print ' :.:;::::::;i , Ct11G LC1i '
48+
# print ' 1:::::::1001 C . tf '
49+
# print ' '
50+
print ('PyHEADTAIL v' + __version__ + '\n\n')

_version.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
__version__ = "1.0.1"
2+

cobra_functions/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .. import __version__

cobra_functions/stats.pyx

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
"""
2+
@author Kevin Li, Michael Schenk
3+
@date 31. January 2014
4+
@brief Collection of cython functions to calculate statistics
5+
of bunch and slice_set data.
6+
@copyright CERN
7+
"""
8+
import numpy as np
9+
cimport numpy as np
10+
cimport libc.math as cmath
11+
12+
cimport cython.boundscheck
13+
cimport cython.cdivision
14+
15+
16+
@cython.boundscheck(False)
17+
cpdef double mean(double[::1] u):
18+
""" Cython function to calculate the mean value of dataset u. """
19+
cdef double mean_u = 0
20+
cdef unsigned int n = u.shape[0]
21+
22+
cdef unsigned int i
23+
for i in xrange(n):
24+
mean_u += u[i]
25+
if n:
26+
mean_u /= n
27+
28+
return mean_u
29+
30+
@cython.boundscheck(False)
31+
cpdef double std(double[::1] u):
32+
""" Cython function to calculate the standard deviation of
33+
dataset u. """
34+
cdef double mean_u = mean(u)
35+
cdef double std_u = 0
36+
cdef double du = 0
37+
38+
cdef unsigned int n = u.shape[0]
39+
cdef unsigned int i
40+
for i in xrange(n):
41+
du = u[i] - mean_u
42+
std_u += du * du
43+
if n:
44+
std_u /= n
45+
46+
return cmath.sqrt(std_u)
47+
48+
@cython.boundscheck(False)
49+
cpdef double emittance(double[::1] u, double[::1] up):
50+
""" Cython function to calculate the emittance of datasets
51+
u and up, i.e. a coordinate-momentum pair. To calculate the
52+
emittance, one needs the mean values of quantities u and
53+
up. """
54+
cdef double mean_u = mean(u)
55+
cdef double mean_up = mean(up)
56+
57+
cdef double u2 = 0
58+
cdef double up2 = 0
59+
cdef double uup = 0
60+
cdef double du = 0
61+
cdef double dup = 0
62+
63+
cdef unsigned int n = u.shape[0]
64+
cdef unsigned int i
65+
for i in xrange(n):
66+
du = u[i] - mean_u
67+
dup = up[i] - mean_up
68+
69+
u2 += du * du
70+
up2 += dup * dup
71+
uup += du * dup
72+
if n:
73+
u2 /= n
74+
up2 /= n
75+
uup /= n
76+
77+
return cmath.sqrt(u2*up2 - uup*uup)
78+
79+
80+
'''
81+
Cython statistics functions for an instance of a SliceSet class.
82+
'''
83+
84+
@cython.boundscheck(False)
85+
@cython.cdivision(True)
86+
cpdef count_macroparticles_per_slice(int[::1] slice_index_of_particle,
87+
int[::1] particles_within_cuts,
88+
int[::1] n_macroparticles):
89+
""" Cython function to count the number of macroparticles in
90+
each slice. """
91+
cdef unsigned int n_particles_within_cuts = particles_within_cuts.shape[0]
92+
cdef unsigned int s_idx, i
93+
94+
for i in xrange(n_particles_within_cuts):
95+
s_idx = slice_index_of_particle[particles_within_cuts[i]]
96+
n_macroparticles[s_idx] += 1
97+
98+
99+
@cython.boundscheck(False)
100+
@cython.cdivision(True)
101+
cpdef sort_particle_indices_by_slice(int[::1] slice_index_of_particle,
102+
int[::1] particles_within_cuts,
103+
int[::1] slice_positions,
104+
int[::1] particle_indices_by_slice):
105+
""" Iterate once through all the particles within the slicing
106+
region and assign their position in the bunch.z array to the
107+
respective slice they are in.
108+
This is to provide a method to the user that allows to see
109+
which particles are in a specific slice (see
110+
particle_indices_of_slice in SliceSet class). """
111+
cdef unsigned int n_part_in_cuts = particles_within_cuts.shape[0]
112+
cdef unsigned int n_slices = slice_positions.shape[0] - 1
113+
114+
cdef unsigned int[::1] pos_ctr = np.zeros(n_slices, dtype=np.uint32)
115+
cdef unsigned int i, p_idx, s_idx
116+
cdef unsigned int pos
117+
118+
for i in xrange(n_part_in_cuts):
119+
p_idx = particles_within_cuts[i]
120+
s_idx = slice_index_of_particle[p_idx]
121+
122+
pos = slice_positions[s_idx] + pos_ctr[s_idx]
123+
particle_indices_by_slice[pos] = p_idx
124+
pos_ctr[s_idx] += 1
125+
126+
127+
@cython.boundscheck(False)
128+
@cython.cdivision(True)
129+
cpdef mean_per_slice(int[::1] slice_index_of_particle,
130+
int[::1] particles_within_cuts,
131+
int[::1] n_macroparticles,
132+
double[::1] u, double[::1] mean_u):
133+
""" Iterate once through all the particles within the
134+
slicing region and calculate simultaneously the mean
135+
value of quantity u for each slice separately. """
136+
cdef unsigned int n_part_in_cuts = particles_within_cuts.shape[0]
137+
cdef unsigned int n_slices = mean_u.shape[0]
138+
cdef unsigned int p_idx, s_idx, i
139+
140+
for i in xrange(n_part_in_cuts):
141+
p_idx = particles_within_cuts[i]
142+
s_idx = slice_index_of_particle[p_idx]
143+
mean_u[s_idx] += u[p_idx]
144+
145+
for i in xrange(n_slices):
146+
if n_macroparticles[i]:
147+
mean_u[i] /= n_macroparticles[i]
148+
149+
150+
@cython.boundscheck(False)
151+
@cython.cdivision(True)
152+
cpdef std_per_slice(int[::1] slice_index_of_particle,
153+
int[::1] particles_within_cuts,
154+
int[::1] n_macroparticles,
155+
double[::1] u, double[::1] std_u):
156+
""" Iterate once through all the particles within the
157+
slicing region and calculate simultaneously the
158+
standard deviation of quantity u for each slice
159+
separately. """
160+
cdef unsigned int n_part_in_cuts = particles_within_cuts.shape[0]
161+
cdef unsigned int n_slices = std_u.shape[0]
162+
cdef unsigned int p_idx, s_idx, i
163+
cdef double du
164+
165+
cdef double[::1] mean_u = np.zeros(n_slices, dtype=np.double)
166+
mean_per_slice(slice_index_of_particle, particles_within_cuts,
167+
n_macroparticles, u, mean_u)
168+
169+
for i in xrange(n_part_in_cuts):
170+
p_idx = particles_within_cuts[i]
171+
s_idx = slice_index_of_particle[p_idx]
172+
173+
du = u[p_idx] - mean_u[s_idx]
174+
std_u[s_idx] += du * du
175+
176+
for i in xrange(n_slices):
177+
if n_macroparticles[i]:
178+
std_u[i] /= n_macroparticles[i]
179+
180+
std_u[i] = cmath.sqrt(std_u[i])
181+
182+
183+
@cython.boundscheck(False)
184+
@cython.cdivision(True)
185+
cpdef emittance_per_slice(int[::1] slice_index_of_particle,
186+
int[::1] particles_within_cuts,
187+
int[::1] n_macroparticles,
188+
double[::1] u, double[::1] up, double[::1] epsn_u):
189+
""" Iterate once through all the particles within the
190+
slicing region and calculate simultaneously the emittance
191+
of quantities u and up, i.e. a coordinate-momentum pair,
192+
for each slice separately. To calculate the emittance per
193+
slice, one needs the mean values of quantities u and up
194+
for each slice. """
195+
cdef unsigned int n_part_in_cuts = particles_within_cuts.shape[0]
196+
cdef unsigned int n_slices = epsn_u.shape[0]
197+
cdef unsigned int p_idx, s_idx, i
198+
199+
# Determine mean values of u and up for each slice.
200+
cdef double[::1] mean_u = np.zeros(n_slices, dtype=np.double)
201+
cdef double[::1] mean_up = np.zeros(n_slices, dtype=np.double)
202+
mean_per_slice(slice_index_of_particle, particles_within_cuts,
203+
n_macroparticles, u, mean_u)
204+
mean_per_slice(slice_index_of_particle, particles_within_cuts,
205+
n_macroparticles, up, mean_up)
206+
207+
cdef double du, dup
208+
cdef double[::1] u2 = np.zeros(n_slices, dtype=np.double)
209+
cdef double[::1] up2 = np.zeros(n_slices, dtype=np.double)
210+
cdef double[::1] uup = np.zeros(n_slices, dtype=np.double)
211+
212+
for i in xrange(n_part_in_cuts):
213+
p_idx = particles_within_cuts[i]
214+
s_idx = slice_index_of_particle[p_idx]
215+
216+
du = u[p_idx] - mean_u[s_idx]
217+
dup = up[p_idx] - mean_up[s_idx]
218+
219+
u2[s_idx] += du * du
220+
up2[s_idx] += dup * dup
221+
uup[s_idx] += du * dup
222+
223+
for i in xrange(n_slices):
224+
if n_macroparticles[i]:
225+
u2[i] /= n_macroparticles[i]
226+
up2[i] /= n_macroparticles[i]
227+
uup[i] /= n_macroparticles[i]
228+
229+
epsn_u[i] = cmath.sqrt(u2[i]*up2[i] - uup[i]*uup[i])

0 commit comments

Comments
 (0)