Skip to content

Commit 900f609

Browse files
committed
Adapt examples
1 parent 38c2f66 commit 900f609

15 files changed

+36
-165
lines changed

examples/particles_generation/001a_build_particles_set.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@
99
# Build a reference particle
1010
p0 = xp.Particles(mass0=xp.PROTON_MASS_EV, q0=1, p0c=7e12, x=1, y=3)
1111

12-
# Choose a context
13-
ctx = xo.ContextCpu()
14-
1512
# Built a set of three particles with different y coordinates
16-
particles = xp.build_particles(_context=ctx, particle_ref=p0, y=[1,2,3])
13+
particles = xp.build_particles(particle_ref=p0, y=[1,2,3])
1714

1815
# Inspect
1916
print(particles.p0c[1]) # gives 7e12

examples/particles_generation/001at_build_particles_set_with_tracker.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,16 @@
22
# This file is part of the Xpart Package. #
33
# Copyright (c) CERN, 2021. #
44
# ######################################### #
5-
import json
65

76
import xpart as xp
87
import xobjects as xo
98
import xtrack as xt
109

11-
ctx = xo.ContextCpu() # choose a context
12-
13-
# Load machine model and built a line
14-
filename = ('../../../xtrack/test_data/lhc_no_bb/line_and_particle.json')
15-
with open(filename, 'r') as fid:
16-
line = xt.Line.from_dict(json.load(fid)['line'])
17-
line.build_tracker(_context=ctx)
10+
# Get a line
11+
line = xt.load('../../../xtrack/test_data/lhc_no_bb/line_and_particle.json')
1812

1913
# Attach a reference particle to the line
20-
line.particle_ref = xp.Particles(p0c=7e12, mass0=xp.PROTON_MASS_EV, q0=1, x =1 , y=3)
14+
line.set_particle_ref('proton', p0c=7e12, x=1, y=3)
2115

2216
# Built a set of three particles with different y coordinates
2317
# (context and particle_ref are taken from the line)

examples/particles_generation/001b_build_particles_shift.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@
99
# Build a reference particle
1010
p0 = xp.Particles(mass0=xp.PROTON_MASS_EV, q0=1, p0c=7e12, x=1, y=3)
1111

12-
# Choose a context
13-
ctx = xo.ContextCpu()
14-
1512
# Built a set of three particles with different y coordinates
16-
particles = xp.build_particles(mode='shift', particle_ref=p0, y=[1,2,3],
17-
_context=ctx)
13+
particles = xp.build_particles(mode='shift', particle_ref=p0, y=[1,2,3])
1814

1915
# Inspect
2016
print(particles.p0c[1]) # gives 7e12

examples/particles_generation/001bt_build_particles_shift_with_tracker.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,16 @@
22
# This file is part of the Xpart Package. #
33
# Copyright (c) CERN, 2021. #
44
# ######################################### #
5-
import json
65

76
import xpart as xp
87
import xobjects as xo
98
import xtrack as xt
109

11-
ctx = xo.ContextCpu() # choose a context
12-
13-
# Load machine model and built a line
14-
filename = ('../../../xtrack/test_data/lhc_no_bb/line_and_particle.json')
15-
with open(filename, 'r') as fid:
16-
line = xt.Line.from_dict(json.load(fid)['line'])
17-
line.build_tracker(_context=ctx)
10+
# Load machine model
11+
line = xt.load('../../../xtrack/test_data/lhc_no_bb/line_and_particle.json')
1812

1913
# Attach a reference particle to the line
20-
line.particle_ref = xp.Particles(p0c=7e12, mass0=xp.PROTON_MASS_EV, q0=1, x=1 , y=3)
14+
line.set_particle_ref('proton', p0c=7e12, x=1, y=3)
2115

2216
# Built a set of three particles with different y coordinates
2317
# (context and particle_ref are taken from the line)

examples/particles_generation/001c_build_particles_normalized.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,14 @@
33
# Copyright (c) CERN, 2021. #
44
# ######################################### #
55

6-
import json
7-
86
import xobjects as xo
97
import xpart as xp
108
import xtrack as xt
119

12-
# Choose a context
13-
ctx = xo.ContextCpu()
14-
15-
# Load machine model (from pymask)
16-
filename = ('../../../xtrack/test_data/lhc_no_bb/line_and_particle.json')
17-
with open(filename, 'r') as fid:
18-
dct = json.load(fid)
19-
line = xt.Line.from_dict(dct['line'])
20-
line.build_tracker(_context=ctx)
10+
line = xt.load('../../../xtrack/test_data/lhc_no_bb/line_and_particle.json')
2111

2212
# Attach a reference particle to the line
23-
line.particle_ref = xp.Particles(mass0=xp.PROTON_MASS_EV, q0=1, p0c=7e12, x=1, y=3)
13+
line.set_particle_ref('proton', p0c=7e12, x=1, y=3)
2414

2515
# Built a set of three particles with different x coordinates
2616
particles = line.build_particles(

examples/particles_generation/002_halo.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# Copyright (c) CERN, 2021. #
44
# ######################################### #
55

6-
import json
76
import numpy as np
87

98
import xpart as xp
@@ -13,13 +12,9 @@
1312
nemitt_x = 2.5e-6
1413
nemitt_y = 3e-6
1514

16-
# Load machine model (from pymask)
17-
filename = ('../../../xtrack/test_data/lhc_no_bb/line_and_particle.json')
18-
with open(filename, 'r') as fid:
19-
input_data = json.load(fid)
20-
line = xt.Line.from_dict(input_data['line'])
21-
line.particle_ref = xp.Particles.from_dict(input_data['particle'])
22-
line.build_tracker()
15+
line = xt.load('../../../xtrack/test_data/lhc_no_bb/line_and_particle.json')
16+
17+
line.set_particle_ref('proton', p0c=7e12)
2318

2419
# Horizontal plane: generate cut halo distribution
2520
(x_in_sigmas, px_in_sigmas, r_points, theta_points

examples/particles_generation/003_pencil.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# Copyright (c) CERN, 2021. #
44
# ######################################### #
55

6-
import json
76
import numpy as np
87

98
import xpart as xp
@@ -13,13 +12,9 @@
1312
nemitt_x = 2.5e-6
1413
nemitt_y = 3e-6
1514

16-
# Load machine model (from pymask)
17-
filename = ('../../../xtrack/test_data/lhc_no_bb/line_and_particle.json')
18-
with open(filename, 'r') as fid:
19-
input_data = json.load(fid)
20-
line=xt.Line.from_dict(input_data['line'])
21-
line.particle_ref = xp.Particles.from_dict(input_data['particle'])
22-
line.build_tracker()
15+
line = xt.load('../../../xtrack/test_data/lhc_no_bb/line_and_particle.json')
16+
17+
line.set_particle_ref('proton', p0c=7e12)
2318

2419
# Horizontal plane: generate gaussian distribution in normalized coordinates
2520
x_in_sigmas, px_in_sigmas = xp.generate_2D_gaussian(num_particles)

examples/particles_generation/003a_pencil_on_collimator.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# Copyright (c) CERN, 2021. #
44
# ######################################### #
55

6-
import json
76
import numpy as np
87

98
import xpart as xp
@@ -13,17 +12,13 @@
1312
nemitt_x = 2.5e-6
1413
nemitt_y = 3e-6
1514

16-
# Load machine model
17-
filename = ('../../../xtrack/test_data/hllhc15_noerrors_nobb/line_and_particle.json')
18-
with open(filename, 'r') as fid:
19-
input_data = json.load(fid)
20-
line=xt.Line.from_dict(input_data['line'])
21-
line.particle_ref = xp.Particles.from_dict(input_data['particle'])
22-
line.build_tracker()
15+
line = xt.load('../../../xtrack/test_data/lhc_no_bb/line_and_particle.json')
16+
17+
line.set_particle_ref('proton', p0c=7e12)
2318

2419
# Location of the collimator
2520
at_element = 'tcp.d6l7.b1'
26-
at_s = line.line.get_s_position(at_element) + 1.
21+
at_s = line.get_s_position(at_element) + 1.
2722
y_cut = 3e-3 # position of the jaw
2823
pencil_dr_sigmas = 3 # width of the pencil
2924
side = '+' # side of the pencil

examples/particles_generation/003b_pencil_on_collimator_dispersion.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# Copyright (c) CERN, 2021. #
44
# ######################################### #
55

6-
import json
76
import numpy as np
87

98
import xpart as xp
@@ -13,14 +12,9 @@
1312
nemitt_x = 2.5e-6
1413
nemitt_y = 3e-6
1514

16-
# Load machine model
17-
filename = ('../../../xtrack/test_data/hllhc15_noerrors_nobb/line_and_particle.json')
18-
line = xt.Line.from_json(filename)
19-
with open(filename, 'r') as fid:
20-
input_data = json.load(fid)
21-
line = xt.Line.from_dict(input_data['line'])
22-
line.particle_ref = xp.Particles.from_dict(input_data['particle'])
23-
line.build_tracker()
15+
line = xt.load('../../../xtrack/test_data/lhc_no_bb/line_and_particle.json')
16+
17+
line.set_particle_ref('proton', p0c=7e12)
2418

2519
# Location of the collimator
2620
at_element = 'tcp.6l3.b1' # High dispersion
@@ -54,7 +48,7 @@
5448
at_element=at_element, match_at_s=at_s)
5549

5650
# Drift to at_s position for checking
57-
drift_to_at_s = xt.Drift(length=at_s-line.line.get_s_position(at_element))
51+
drift_to_at_s = xt.Drift(length=at_s - line.get_s_position(at_element))
5852
drift_to_at_s.track(particles)
5953

6054
# Checks and plots

examples/particles_generation/004_generate_gaussian.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
# Copyright (c) CERN, 2021. #
44
# ######################################### #
55

6-
import json
7-
86
import numpy as np
97
from scipy.constants import e as qe
108
from scipy.constants import m_p
@@ -18,14 +16,8 @@
1816
nemitt_x = 2e-6
1917
nemitt_y = 2.5e-6
2018

21-
filename = ('../../../xtrack/test_data/sps_w_spacecharge'
22-
'/line_no_spacecharge_and_particle.json')
23-
with open(filename, 'r') as fid:
24-
ddd = json.load(fid)
25-
line = xt.Line.from_dict(ddd['line'])
26-
line.particle_ref = xp.Particles.from_dict(ddd['particle'])
27-
28-
line.build_tracker()
19+
line = xt.load('../../../xtrack/test_data/sps_w_spacecharge/line_no_spacecharge_and_particle.json')
20+
line.set_particle_ref('proton', p0c=26e9)
2921

3022
particles = xp.generate_matched_gaussian_bunch(
3123
num_particles=n_part, total_intensity_particles=bunch_intensity,

0 commit comments

Comments
 (0)