Skip to content

Commit

Permalink
Merge branch 'release/v0.67.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
giadarol committed Sep 17, 2024
2 parents 90678d3 + fbbf97b commit 7b6900d
Show file tree
Hide file tree
Showing 29 changed files with 3,091 additions and 298 deletions.
45 changes: 31 additions & 14 deletions examples/coasting/001_frev_meas.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,21 @@

from scipy.constants import c as clight

delta0 = 1e-2
delta0 = 0 #-1e-2
delta_range = 0
num_turns=100
num_particles = 100_000

# delta0 = 1e-2
# delta_range = 0
# num_turns=20
# num_particles = 5_000_000

# # To see the different number of turns
# delta0 = 0e-2
# delta_range = 10e-3
# num_turns=5000
# num_particles = 5000

line = xt.Line.from_json(
'../../test_data/psb_injection/line_and_particle.json')
Expand Down Expand Up @@ -42,15 +56,15 @@
zeta_min0 = -circumference/2*tw.beta0/beta1
zeta_max0 = circumference/2*tw.beta0/beta1

num_particles = 50000

p = line.build_particles(
delta=delta0 + 0 * np.random.uniform(-1, 1, num_particles),
delta=delta0 + delta_range * np.random.uniform(-1, 1, num_particles),
x_norm=0, y_norm=0
)

# Need to take beta of actual particles to convert the distribution along the
# circumference to a distribution in time
p.zeta = (np.random.uniform(0, circumference, num_particles) / p.rvv
p.zeta = (np.random.uniform(0, circumference, num_particles) / p.rvv * 0.999
+ (zeta_max0 - circumference) / p.rvv)

st.prepare_particles_for_sync_time(p, line)
Expand Down Expand Up @@ -95,13 +109,13 @@ def y_mean_hist(line, particles):


line.enable_time_dependent_vars = True
num_turns=200

line.track(p, num_turns=num_turns, log=xt.Log(intensity=intensity,
long_density=long_density,
y_mean_hist=y_mean_hist,
z_range=z_range,
particles=particles
), with_progress=10)
), with_progress=2)

inten = line.log_last_track['intensity']

Expand Down Expand Up @@ -165,8 +179,8 @@ def y_mean_hist(line, particles):

plt.figure(2)
plt.plot(p.delta, p.at_turn, '.')
plt.ylabel('Number of turns')
plt.xlabel(r'$\delta$')
plt.ylabel('Number of revolutions')
plt.xlabel(r'$\Delta P / P_0$')

plt.figure(3)
plt.plot([zz[1]-zz[0] for zz in line.log_last_track['z_range']])
Expand Down Expand Up @@ -201,18 +215,21 @@ def y_mean_hist(line, particles):
plt.xlabel('z [m]')
plt.ylabel('x [m]')

plt.figure(8)
f8 = plt.figure(8)
ax1 = plt.subplot(2, 1, 1)
plt.plot(t_unwrapped*1e6, y_vs_t, '-')
plt.ylabel('y mean [m]')
plt.grid()
ax2 = plt.subplot(2, 1, 2, sharex=ax1)
plt.plot(t_unwrapped*1e6, intensity_vs_t, '-')
plt.ylabel('intensity')
plt.plot(t_unwrapped*1e6, intensity_vs_t/np.mean(intensity_vs_t), '-')
plt.ylabel('Beam line density [a.u.]')
plt.xlabel('t [us]')
plt.ylim(bottom=0)
for tt in t_range_size * np.arange(0, hist_y.shape[0]):
ax1.axvline(x=tt*1e6, color='red', linestyle='--', alpha=0.5)
for tt in np.arange(0, t_unwrapped[-1], 1/f_nominal):
for ax in [ax1, ax2]:
ax.axvline(x=tt*1e6, color='red', linestyle='--', alpha=0.5)

# zoom
ax1.set_xlim(0, 15)


plt.show()
49 changes: 49 additions & 0 deletions examples/coasting/e000_illustration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import numpy as np
import matplotlib.pyplot as plt

v = 1.
l_ref = 1

t_ref = l_ref/v

l = np.array([0.95, 1, 1.05])

t_max = 80
t = np.linspace(0, t_max, 10000)

s = np.zeros((len(l), len(t)))
for il, ll in enumerate(l):
s[il, :] = np.mod(0.62*l_ref + v * t * ll/l_ref, l_ref)

passing = np.diff(s, prepend=0) < 0

import matplotlib.pyplot as plt
plt.close('all')

plt.figure(1, figsize=(6.4*1.8*0.8, 4.8*0.8))
sp1 = plt.subplot(311)
sp2 = plt.subplot(312, sharex=sp1)
sp3 = plt.subplot(313, sharex=sp1)

sp1.stem(t, passing[1, :], markerfmt=' ', basefmt='C0', linefmt='C0')
sp2.stem(t, passing[0, :], markerfmt=' ', basefmt='C1', linefmt='C1')
sp3.stem(t, passing[2, :], markerfmt=' ', basefmt='C2', linefmt='C2')

t_turn = np.arange(0, t_max, t_ref)
for tt in t_turn:
for sp in [sp1, sp2, sp3]:
sp.axvline(tt, color='k', linestyle='--', alpha=0.4)

sp3.set_xlim(0, t_max/t_ref)
sp3.set_xlabel(r'$t~/~T_0$')

for sp in [sp1, sp2, sp3]:
sp.set_ylim(0, 1.1)

plt.subplots_adjust(bottom=.14, top=.95, hspace=0.3)

# For zoom
# plt.subplots_adjust(right=.6)

plt.show()

230 changes: 230 additions & 0 deletions examples/lattice_design_shortcuts/000_dev.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
import xtrack as xt
import numpy as np

env = xt.Environment()
env.particle_ref = xt.Particles(p0c=2e9)

n_bends_per_cell = 6
n_cells_par_arc = 3
n_arcs = 3

n_bends = n_bends_per_cell * n_cells_par_arc * n_arcs

env.vars({
'l.mq': 0.5,
'kqf': 0.027,
'kqd': -0.0271,
'l.mb': 10,
'l.ms': 0.3,
'k2sf': 0.001,
'k2sd': -0.001,
'angle.mb': 2 * np.pi / n_bends,
'k0.mb': 'angle.mb / l.mb',
'k0l.corrector': 0,
'k1sl.corrector': 0,
'l.halfcell': 38,
})

env.new('mb', xt.Bend, length='l.mb', k0='k0.mb', h='k0.mb')
env.new('mq', xt.Quadrupole, length='l.mq')
env.new('ms', xt.Sextupole, length='l.ms')
env.new('corrector', xt.Multipole, knl=[0], ksl=[0])

env.new('mq.f', 'mq', k1='kqf')
env.new('mq.d', 'mq', k1='kqd')

halfcell = env.new_line(components=[

# End of the half cell (will be mid of the cell)
env.new('mid', xt.Marker, at='l.halfcell'),

# Bends
env.new('mb.2', 'mb', at='l.halfcell / 2'),
env.new('mb.1', 'mb', at='-l.mb - 1', from_='mb.2'),
env.new('mb.3', 'mb', at='l.mb + 1', from_='mb.2'),

# Quads
env.place('mq.d', at = '0.5 + l.mq / 2'),
env.place('mq.f', at = 'l.halfcell - l.mq / 2 - 0.5'),

# Sextupoles
env.new('ms.d', 'ms', k2='k2sf', at=1.2, from_='mq.d'),
env.new('ms.f', 'ms', k2='k2sd', at=-1.2, from_='mq.f'),

# Dipole correctors
env.new('corrector.v', 'corrector', at=0.75, from_='mq.d'),
env.new('corrector.h', 'corrector', at=-0.75, from_='mq.f')

])

hcell_left = halfcell.replicate(name='l', mirror=True)
hcell_right = halfcell.replicate(name='r')

cell = env.new_line(components=[
env.new('start', xt.Marker),
hcell_left,
hcell_right,
env.new('end', xt.Marker),
])

opt = cell.match(
method='4d',
vary=xt.VaryList(['kqf', 'kqd'], step=1e-5),
targets=xt.TargetSet(
qx=0.333333,
qy=0.333333,
))
tw_cell = cell.twiss4d()


env.vars({
'kqf.ss': 0.027 / 2,
'kqd.ss': -0.0271 / 2,
})

halfcell_ss = env.new_line(components=[

env.new('mid', xt.Marker, at='l.halfcell'),

env.new('mq.ss.d', 'mq', k1='kqd.ss', at = '0.5 + l.mq / 2'),
env.new('mq.ss.f', 'mq', k1='kqf.ss', at = 'l.halfcell - l.mq / 2 - 0.5'),

env.new('corrector.ss.v', 'corrector', at=0.75, from_='mq.ss.d'),
env.new('corrector.ss.h', 'corrector', at=-0.75, from_='mq.ss.f')
])

hcell_left_ss = halfcell_ss.replicate(name='l', mirror=True)
hcell_right_ss = halfcell_ss.replicate(name='r')
cell_ss = env.new_line(components=[
env.new('start.ss', xt.Marker),
hcell_left_ss,
hcell_right_ss,
env.new('end.ss', xt.Marker),
])

opt = cell_ss.match(
method='4d',
vary=xt.VaryList(['kqf.ss', 'kqd.ss'], step=1e-5),
targets=xt.TargetSet(
betx=tw_cell.betx[-1], bety=tw_cell.bety[-1], at='start.ss',
))



arc = env.new_line(components=[
cell.replicate(name='cell.1'),
cell.replicate(name='cell.2'),
cell.replicate(name='cell.3'),
])


ss = env.new_line(components=[
cell_ss.replicate('cell.1'),
cell_ss.replicate('cell.2'),
])

ring = env.new_line(components=[
arc.replicate(name='arc.1'),
ss.replicate(name='ss.1'),
arc.replicate(name='arc.2'),
ss.replicate(name='ss.2'),
arc.replicate(name='arc.3'),
ss.replicate(name='ss.3'),
])

## Insertion

env.vars({
'k1.q1': 0.025,
'k1.q2': -0.025,
'k1.q3': 0.025,
'k1.q4': -0.02,
'k1.q5': 0.025,
})

half_insertion = env.new_line(components=[

# Start-end markers
env.new('ip', xt.Marker),
env.new('e.insertion', xt.Marker, at=76),

# Quads
env.new('mq.1', xt.Quadrupole, k1='k1.q1', length='l.mq', at = 20),
env.new('mq.2', xt.Quadrupole, k1='k1.q2', length='l.mq', at = 25),
env.new('mq.3', xt.Quadrupole, k1='k1.q3', length='l.mq', at=37),
env.new('mq.4', xt.Quadrupole, k1='k1.q4', length='l.mq', at=55),
env.new('mq.5', xt.Quadrupole, k1='k1.q5', length='l.mq', at=73),

# Dipole correctors (will use h and v on the same corrector)
env.new('corrector.ss.1', 'corrector', at=0.75, from_='mq.1'),
env.new('corrector.ss.2', 'corrector', at=-0.75, from_='mq.2'),
env.new('corrector.ss.3', 'corrector', at=0.75, from_='mq.3'),
env.new('corrector.ss.4', 'corrector', at=-0.75, from_='mq.4'),
env.new('corrector.ss.5', 'corrector', at=0.75, from_='mq.5'),

])

tw_arc = arc.twiss4d()

opt = half_insertion.match(
solve=False,
betx=tw_arc.betx[0], bety=tw_arc.bety[0],
alfx=tw_arc.alfx[0], alfy=tw_arc.alfy[0],
init_at='e.insertion',
start='ip', end='e.insertion',
vary=xt.VaryList(['k1.q1', 'k1.q2', 'k1.q3', 'k1.q4'], step=1e-5),
targets=[
xt.TargetSet(alfx=0, alfy=0, at='ip'),
xt.Target(lambda tw: tw.betx[0] - tw.bety[0], 0),
xt.Target(lambda tw: tw.betx.max(), xt.LessThan(400)),
xt.Target(lambda tw: tw.bety.max(), xt.LessThan(400)),
xt.Target(lambda tw: tw.betx.min(), xt.GreaterThan(2)),
xt.Target(lambda tw: tw.bety.min(), xt.GreaterThan(2)),
]
)
opt.step(40)
opt.solve()

insertion = env.new_line([
half_insertion.replicate('l', mirror=True),
half_insertion.replicate('r')])



ring2 = env.new_line(components=[
arc.replicate(name='arcc.1'),
ss.replicate(name='sss.2'),
arc.replicate(name='arcc.2'),
insertion,
arc.replicate(name='arcc.3'),
ss.replicate(name='sss.3')
])


# # Check buffer behavior
ring2_sliced = ring2.select()
ring2_sliced.cut_at_s(np.arange(0, ring2.get_length(), 0.5))


import matplotlib.pyplot as plt
plt.close('all')
for ii, rr in enumerate([ring, ring2_sliced]):

tw = rr.twiss4d()

fig = plt.figure(ii, figsize=(6.4*1.2, 4.8))
ax1 = fig.add_subplot(2, 1, 1)
pltbet = tw.plot('betx bety', ax=ax1)
ax2 = fig.add_subplot(2, 1, 2, sharex=ax1)
pltdx = tw.plot('dx', ax=ax2)
fig.subplots_adjust(right=.85)
pltbet.move_legend(1.2,1)
pltdx.move_legend(1.2,1)

ring2.survey().plot()


plt.show()



Loading

0 comments on commit 7b6900d

Please sign in to comment.