Skip to content

Commit 37e87fa

Browse files
authored
Merge pull request #50 from aoeftiger/feature/sc-linedensity-interpolation
Feature/sc linedensity interpolation
2 parents 6ab5d3e + a52aeb8 commit 37e87fa

File tree

15 files changed

+451
-219
lines changed

15 files changed

+451
-219
lines changed

examples/spacecharge/001_prepare_spacecharge_lattice_and_particles.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717

1818
if particle_type == "protons":
1919

20-
# For bunched
20+
# Space charge:
2121
number_of_particles = 2e11
22+
23+
# For bunched
2224
bunchlength_rms = 0.22
2325

2426
# For coasting
25-
line_density = 2e11 / 0.5
27+
circumference = 0.5
2628

2729
mass = pysixtrack.Particles.pmass
2830
p0c = 25.92e9
@@ -37,20 +39,22 @@
3739

3840
if particle_type == "ions":
3941

40-
# For bunched
42+
# Space charge:
4143
number_of_particles = 3.5e8
44+
45+
# For bunched
4246
bunchlength_rms = 0.22
4347

4448
# For coasting
45-
line_density = 3.5e8 / 0.5
49+
circumference = 0.5
4650

4751
mass = 193.7e9
4852
p0c = 1402.406299e9
4953
charge_state = 82.0
50-
neps_x = 1.63e-6
51-
neps_y = 0.86e-6
54+
neps_x = 1.63e-6
55+
neps_y = 0.86e-6
5256
delta_rms = 1.0e-3
53-
V_RF_MV = 3
57+
V_RF_MV = 3
5458
lag_RF_deg = 0.0
5559
n_SCkicks = 250
5660
length_fuzzy = 1.5
@@ -90,11 +94,11 @@
9094
# Check consistency
9195
if sc_mode == "Bunched":
9296
sc_elements, sc_names = line.get_elements_of_type(
93-
pysixtrack.elements.SpaceChargeBunched
97+
pysixtrack.elements.SCQGaussProfile
9498
)
9599
elif sc_mode == "Coasting":
96100
sc_elements, sc_names = line.get_elements_of_type(
97-
pysixtrack.elements.SpaceChargeCoasting
101+
pysixtrack.elements.SCCoasting
98102
)
99103
else:
100104
raise ValueError("mode not understood")
@@ -148,7 +152,8 @@
148152
sc_lengths,
149153
sc_twdata,
150154
betagamma,
151-
line_density,
155+
number_of_particles,
156+
circumference,
152157
delta_rms,
153158
neps_x,
154159
neps_y,
@@ -177,7 +182,7 @@
177182
with open("particle_on_CO.pkl", "rb") as fid:
178183
part_on_CO = pysixtrack.Particles.from_dict(pickle.load(fid))
179184

180-
part = part_on_CO.copy()
185+
part = part_on_CO.copy()
181186

182187
# get beta functions from twiss table
183188
with open("twiss_at_start.pkl", "rb") as fid:

pysixtrack/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.0.5"
1+
__version__ = "0.0.6"
22

33
from . import base_classes
44
from . import elements

pysixtrack/be_beamfields/qgauss.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from pysixtrack.mathlibs import MathlibDefault
2+
3+
4+
class QGauss(object):
5+
@staticmethod
6+
def calc_cq(q, mathlib=MathlibDefault, EPS=1e-6):
7+
assert q < 3
8+
cq = mathlib.sqrt(mathlib.pi)
9+
if q >= (1 + EPS):
10+
cq *= mathlib.gamma((3 - q) / (2 * q - 2))
11+
cq /= mathlib.sqrt((q - 1)) * mathlib.gamma(1 / (q - 1))
12+
elif q <= (1 - EPS):
13+
cq *= 2 * mathlib.gamma(1 / (1 - q))
14+
cq /= (
15+
(3 - q)
16+
* mathlib.sqrt(1 - q)
17+
* mathlib.gamma((3 - q) / (2 - 2 * q))
18+
)
19+
return cq
20+
21+
@staticmethod
22+
def sqrt_beta(sigma, mathlib=MathlibDefault):
23+
assert sigma > 0
24+
return 1 / (mathlib.sqrt(2) * sigma)
25+
26+
@staticmethod
27+
def exp_q(x, q, mathlib=MathlibDefault, EPS=1e-6):
28+
assert q < 3
29+
if mathlib.abs(1 - q) > EPS:
30+
u_plus = 1 + x * (1 - q)
31+
if u_plus < 0:
32+
u_plus = 0
33+
return mathlib.pow(u_plus, 1 / (1 - q))
34+
else:
35+
return mathlib.exp(x)
36+
37+
def __init__(self, q=1.0, mathlib=MathlibDefault, cq_eps=1e-6):
38+
assert q < 3
39+
self._m = mathlib
40+
self._q = q
41+
self._cq = QGauss.calc_cq(q, mathlib=mathlib, EPS=cq_eps)
42+
43+
@property
44+
def q(self):
45+
return self._q
46+
47+
@q.setter
48+
def q(self, q_value):
49+
assert q_value < 3
50+
self._q = q_value
51+
self._cq = QGauss.calc_cq(self._q, self._m)
52+
53+
@property
54+
def cq(self):
55+
return self._cq
56+
57+
def min_support(self, sqrt_beta):
58+
assert self._q < 3
59+
assert sqrt_beta > 0
60+
if self._q >= 1:
61+
return -1e10
62+
else:
63+
return -1 / self._m.sqrt(sqrt_beta * sqrt_beta * (1 - self._q))
64+
65+
def max_support(self, sqrt_beta):
66+
return -(self.min_support(sqrt_beta))
67+
68+
def eval(self, x, sqrt_beta, mu=0.0):
69+
assert self._m.abs(self._cq) > 0
70+
assert self._q < 3
71+
assert sqrt_beta > 0
72+
factor = sqrt_beta / self._cq
73+
arg = sqrt_beta * sqrt_beta
74+
arg *= (x - mu) * (x - mu)
75+
return factor * QGauss.exp_q(-arg, self._q, self._m)

0 commit comments

Comments
 (0)