Aperture modelling
An aperture model describes the physical chamber of a machine. It combines transverse profiles into pipes, installs those pipes along the survey, and can be used to compare the resulting cross-sections with a beam envelope, as well as to install limit elements in the line for loss tracking.
Two objects manage the model:
env.aperture(xtrack.EnvAperture) defines named profiles and pipes, shared by the lines in that environment.line.aperture(xtrack.LineAperture) installs pipes on a particular line, queries geometry, computes beam clearance, and plots the results.
The example below uses one FODO cell, a straight pipe, and a slightly curved pipe. A pipe can span several lattice elements; its profiles do not have to sit at element boundaries. All lengths, offsets, and mechanical tolerances in this example are in metres.
Build a FODO cell
Create a focusing quadrupole, a drift, a defocusing quadrupole, and a weak bend.
import matplotlib.pyplot as plt
import numpy as np
import xtrack as xt
bend_length = 2.0
bend_angle = 0.02
env = xt.Environment()
env.new('qf', 'Quadrupole', length=1.0, k1=0.12)
env.new('qd', 'Quadrupole', length=1.0, k1=-0.12)
env.new('drift', 'Drift', length=2.0)
env.new('bend', 'Bend', length=bend_length, angle=bend_angle)
line = env.new_line(name='fodo', components=['qf', 'drift', 'qd', 'bend'])
line.set_particle_ref('proton', p0c=3e9)
line.twiss_default['method'] = '4d'
twiss = line.twiss()
twiss.cols['s betx bety'].show(digits=5)
# => name s betx bety
# => qf 0 21.219 16.751
# => drift 1 21.217 16.751
# => qd 3 16.651 21.343
# => bend 4 16.653 21.343
# => _end_point 6 21.219 16.751
Define profiles and pipes
A profile defines a transverse shape. Here we use a circle, an ellipse, and a rectangle.
Each profile.place(at=...) creates a profile position within a pipe.
The straight pipe stays circular through the focusing quadrupole, narrows to
an ellipse through the drift, and stays elliptical through the defocusing quadrupole.
The curved pipe transitions from that same ellipse to a rectangle. Its curvature
is set independently to that of the bend. The pipe therefore bends more gently than the
reference trajectory, progressively displacing the chamber relative to the beam.
Visualisation of the aperture section we are creating.
round_profile = env.aperture.new_profile('round', 'Circle', radius=0.030)
narrow_profile = env.aperture.new_profile('narrow', 'Ellipse', a=0.022, b=0.016)
wide_profile = env.aperture.new_profile('wide', 'Rectangle', max_x=0.026, max_y=0.022)
straight_pipe = env.aperture.new_pipe('straight_pipe', positions=[
round_profile.place(at=0.0, tol_r=0.0002),
round_profile.place(at=1.0, tol_r=0.0002),
narrow_profile.place(at=3.0, tol_r=0.0004),
narrow_profile.place(at=4.0, tol_r=0.0004),
])
pipe_curvature = 0.004 # 1/m, independently of the bend's 0.01 1/m curvature.
pipe_length = 1.99 # Keep the tilted end profile just inside the bend's exit.
bend_pipe = env.aperture.new_pipe(
'bend_pipe', curvature=pipe_curvature,
positions=[
narrow_profile.place(at=0.0, tol_r=0.0004),
wide_profile.place(at=pipe_length, tol_r=0.0002),
],
)
Each profile can also be plotted on its own:
fig_profiles, axs = plt.subplots(1, 3, figsize=(10, 3.5))
for profile, ax in zip([round_profile, narrow_profile, wide_profile], axs):
profile.plot(ax=ax)
ax.set_xlabel('x [m]')
ax.set_ylabel('y [m]')
fig_profiles.tight_layout()
The three profile shapes used by the two pipes.
Tolerances belong to profile positions and installed pipes. The profile shapes
themselves contain only geometry. Here, tol_r is a radial mechanical tolerance;
tol_x and tol_y specify horizontal and vertical tolerances.
Install the pipes
Install the straight pipe at the start of qf and the curved pipe at the start of bend.
Both use the same elliptical profile at their shared boundary, with matching position
and orientation, so the chamber is continuous. Beyond the join, the pipe axis and
the survey gradually separate because their curvatures differ.
aperture = line.aperture
aperture.place_pipe('upstream', straight_pipe, at='qf', tol_x=0.0003)
aperture.place_pipe('downstream', bend_pipe, at='bend', tol_x=0.0003)
aperture.build_model()
build_model() captures the line’s survey and pre-computes quantities necessary for
further aperture computations (such as caching polygons and their longitudinal bounds).
While here we call it explicitly, the function will also be called implicitly when needed.
In a more complex model than ours, the same pipe definition can be installed several times
under different placement names.
Explore the model
Named views let you inspect profiles, pipes, and installed pipe positions.
print(list(env.aperture.profiles.keys()))
# => ['round', 'narrow', 'wide']
print(env.aperture.profiles['narrow'])
# => <ProfileView 'narrow': Ellipse(a=0.022, b=0.016)>
for position in env.aperture.pipes['straight_pipe']:
print(position.profile.name, position.shift_s, position.tol_r)
# => round 0.0 0.0002
# => round 1.0 0.0002
# => narrow 3.0 0.0004
# => narrow 4.0 0.0004
print(aperture.pipe_positions['downstream'].tol_x)
# => 0.0003
get_pipe_table() reports each installed pipe’s interval, while
get_bounds_table() reports the individual installed profiles and their footprints
along the survey. The two profiles at 4 m belong to different installed pipes but
describe the same cross-section at the join.
The final profile sits near 5.99 m, just before the bend’s exit. Its longitudinal
footprint has a nonzero width because the profile plane is tilted relative to the survey.
pipes = aperture.get_pipe_table()
pipes.cols['name pipe_name s_start s_end length'].show(digits=5)
# => name pipe_name s_start s_end length
# => upstream straight_pipe 0 4 4
# => downstream bend_pipe 4 5.99 1.99
bounds = aperture.get_bounds_table()
bounds.cols['name profile_name s s_start s_end'].show(digits=5)
# => name profile_name s s_start s_end
# => upstream::0 round 0 0 0
# => upstream::1 round 1 1 1
# => upstream::2 narrow 3 3 3
# => upstream::3 narrow 4 4 4
# => downstream::0 narrow 4 4 4
# => downstream::1 wide 5.99 5.9896 5.9902
get_cross_sections_at_s returns interpolated polygons in the local survey frame.
With extents=True, it also returns intersections with the horizontal and vertical
axes. Geometry and tolerances are returned separately: the tolerances do not shrink
the returned aperture polygon or its axis intersections.
sections = aperture.get_cross_sections_at_s([0.0, 2.0, 4.0, 5.0, 5.95], extents=True)
sections.cols['s min_x max_x min_y max_y'].show(digits=5, max_col_width=12)
# => index s min_x max_x min_y max_y
# => 0 0 -0.029991 0.03 -0.029993 0.029993
# => 1 2 -0.025991 0.026 -0.022995 0.022995
# => 2 4 -0.02199 0.022 -0.015997 0.015997
# => 3 5 -0.021005 0.02701 -0.018969 0.018971
# => 4 5.95 -0.014513 0.03733 -0.021862 0.021862
sections.cols['s tol_x tol_y tol_r'].show(digits=5, max_col_width=12)
# => index s tol_x tol_y tol_r
# => 0 0 0.0003 0 0.0002
# => 1 2 0.0003 0 0.0003
# => 2 4 0.0003 0 0.0004
# => 3 5 0.0003 0 0.0002995
# => 4 5.95 0.0003 0 0.00020402
polygon = sections.cross_section[1] # (x, y) vertices at s = 2 m
Within a pipe, profile-position tolerances interpolate between neighbouring profile
centres, and the installed pipe’s tolerances are added component by component.
For example, at the start of the upstream pipe, tol_r is 0.2 mm and tol_x
is 0.3 mm. At a pipe boundary, aperture geometry can interpolate between pipes,
but tolerances are taken from the active pipe rather than interpolated across the boundary.
Compute beam clearance
halo_params supplies the normalised emittances and beam uncertainty margins.
For this example, equal halo parameters define a circle in normalised coordinates.
We also allow a 0.5 mm closed-orbit uncertainty. The bend creates dispersion, so
the momentum spread also contributes to the beam envelope.
aperture.halo_params.update(
nemitt_x=2.5e-6,
nemitt_y=2.5e-6,
tol_co=0.0005,
delta_rms=1e-4,
halo_x=1.0,
halo_y=1.0,
halo_r=1.0,
halo_primary=1.0,
)
get_aperture_sigmas_at_s computes n1, the largest beam scale in sigma units
that fits inside the aperture, including the configured uncertainty margins and
mechanical tolerances. It returns a results table and the Twiss table sampled at
the requested positions. The minimum n1 identifies the limiting sampled location.
get_envelope_at_s instead returns the envelope at a chosen sigma level.
Its cross_section column contains the envelope polygon for each position.
# Stay inside the tilted end profile rather than sampling the pipe's end cap.
s_sample = np.arange(0.0, line.get_length(), 0.05)
clearance, sampled_twiss = aperture.get_aperture_sigmas_at_s(
s_sample, method='rays', num_rays=64, with_progress=False,
)
limiting_index = np.argmin(clearance.n1)
print(f'Minimum n1: {clearance.n1[limiting_index]:.3f} '
f'at s = {clearance.s[limiting_index]:.2f} m')
# => Minimum n1: 3.296 at s = 5.95 m
envelopes, _ = aperture.get_envelope_at_s(
s_sample, sigmas=3.0, include_aper_tols=True, extents=True, with_progress=False,
)
envelopes.rows[[0, 40, 80, 100, 119]].cols['s min_x max_x min_y max_y'].show(
digits=5, max_col_width=12,
)
# => index s min_x max_x min_y max_y
# => 0 0 -0.013344 0.013344 -0.011557 0.011557
# => 40 2 -0.012723 0.012723 -0.01234 0.01234
# => 80 4 -0.012136 0.012136 -0.013155 0.013155
# => 100 5 -0.012723 0.012723 -0.01234 0.01234
# => 119 5.95 -0.013311 0.013311 -0.011594 0.011594
Beam clearance along the cell, sampled at 5 cm intervals.
The rays method samples directions and is a useful starting point,
but methods exact and bisection exist as well (see the physics
manual or method docstrings for more details on these).
If you already have a sampled Twiss, use get_aperture_sigmas_for_twiss
or get_envelope_for_twiss to reuse them.
Visualise the model
Individual pipes
pipe.plot() shows a pipe definition in its own frame, before installation on a line.
Use plane='zx' for a horizontal projection of its physical geometry, or
plane='sx' for a view in which the curved axis is straightened.
The vertical projections are available as 'zy' and 'sy'.
Here we plot both pipe definitions and enlarge the transverse scale for visibility:
fig_pipes, axs = plt.subplots(1, 2, figsize=(10, 3))
for pipe, ax in zip([straight_pipe, bend_pipe], axs):
pipe.plot(plane='zx', ax=ax, max_curve_angle_rad=0.001)
ax.set_aspect('auto') # Enlarge the transverse scale to see the pipe shape.
fig_pipes.tight_layout()
Horizontal projections of the straight and curved pipe definitions, with transverse dimensions enlarged to show their shapes.
pipe.plot_3d() draws the pipe surface and the profiles defining it.
frame='curved' preserves the physical curvature; frame='straight'
displays the pipe in straightened coordinates. Both plots below use the curved frame,
with transverse dimensions enlarged for visibility.
fig_pipes_3d = plt.figure(figsize=(12, 4))
for index, pipe in enumerate([straight_pipe, bend_pipe]):
ax = fig_pipes_3d.add_subplot(1, 2, index + 1, projection='3d')
pipe.plot_3d(frame='curved', ax=ax, max_curve_angle_rad=0.001)
ax.set_xlim(0, pipe.length)
ax.set_ylim(-0.065, 0.04)
ax.set_zlim(-0.035, 0.035)
ax.set_box_aspect((4, 1, 1)) # Enlarge transverse dimensions for visibility.
fig_pipes_3d.tight_layout()
Pipe surfaces and defining profiles in three dimensions. The downstream pipe’s axis bends while its cross-section transitions from an ellipse to a rectangle.
Floor projection
plot_floor_projection draws the installed pipes and the reference trajectory
in the global Z-X plane. This plot is interactive, and clicking on pipes gives
more metadata about their properties and locations.
fig_floor, ax = plt.subplots(figsize=(10, 3))
aperture.plot_floor_projection(ax=ax, max_curve_angle_rad=0.001)
fig_floor.tight_layout()
A continuous chamber with a straight upstream pipe and a curved downstream pipe.
Data behind the plots
The plotting helpers have corresponding data queries. get_extents_at_s supplies
the aperture limits for plot_extents. get_envelope_at_s supplies the envelope
polygons and extents, as shown above. get_aperture_sigmas_at_s supplies the n1
values.
extents = aperture.get_extents_at_s(s_sample)
extents.rows[[0, 40, 80, 100, 119]].cols['s min_x max_x tol_x tol_r'].show(
digits=5, max_col_width=12,
)
# => index s min_x max_x tol_x tol_r
# => 0 0 -0.029991 0.03 0.0003 0.0002
# => 40 2 -0.025991 0.026 0.0003 0.0003
# => 80 4 -0.02199 0.022 0.0003 0.0004
# => 100 5 -0.021005 0.02701 0.0003 0.0002995
# => 119 5.95 -0.014513 0.03733 0.0003 0.00020402
beam_limits, _ = aperture.get_envelope_at_s(
s_sample, sigmas=3.0, include_aper_tols=False, polygons=False, extents=True,
with_progress=False,
)
beam_limits.rows[[0, 40, 80, 100, 119]].cols['s min_x max_x'].show(digits=5, max_col_width=12)
# => index s min_x max_x
# => 0 0 -0.012844 0.012844
# => 40 2 -0.012123 0.012123
# => 80 4 -0.011436 0.011436
# => 100 5 -0.012123 0.012123
# => 119 5.95 -0.012807 0.012807
clearance.rows[[0, 40, 80, 100, 119]].cols['s n1'].show(digits=5, max_col_width=12)
# => index s n1
# => 0 0 7.087
# => 40 2 5.766
# => 80 4 3.6955
# => 100 5 4.72
# => 119 5.95 3.2958
Longitudinal limits and beam envelope
plot_extents plots the aperture’s horizontal and vertical axis intersections
together with a beam envelope. Its horizontal axis is survey arc length s;
the curved pipe is therefore straightened in this view. The parameter sigmas
is optional: when not given, the n1 along the requested sample points will
be computed and used.
fig_extents, _ = aperture.plot_extents(
s_sample, sigmas=3.0, with_progress=False,
)
fig_extents.set_size_inches(10, 6)
fig_extents.suptitle('Aperture limits and three-sigma beam envelope')
fig_extents.tight_layout()
Solid curves show geometric aperture limits. Dashed curves include mechanical tolerance margins. The filled beam envelope is at three sigma; by default this plot leaves mechanical tolerances out of the beam envelope because they are already shown on the aperture limits.
Transverse cross-sections
plot_transverse compares cross-sections and beam envelopes at selected positions,
each expressed in its local survey frame. The data come from
get_cross_sections_at_s and get_envelope_at_s.
fig_transverse, ax = plt.subplots(figsize=(6, 5))
aperture.plot_transverse(
s_positions=[2.0, 5.0], sigmas=3.0, ax=ax, with_progress=False,
)
ax.set_title('Cross-sections at 2 m and 5 m, with three-sigma envelopes')
ax.set_xlabel('x [m]')
ax.set_ylabel('y [m]')
fig_transverse.tight_layout()
Cross-sections and three-sigma beam envelopes in the straight pipe at 2 m and in the curved pipe at 5 m.
Plot the computed clearance
clearance is the table returned by get_aperture_sigmas_at_s. Its s and
n1 columns can be plotted directly with Matplotlib.
fig_clearance, ax = plt.subplots(figsize=(10, 3))
ax.plot(clearance.s, clearance.n1)
ax.set_xlabel('s [m]')
ax.set_ylabel(r'$n_1$')
ax.set_title('Maximum beam size that fits in the aperture')
fig_clearance.tight_layout()
Beam clearance along the cell, sampled at 5 cm intervals.
Modify or extend a model
Named views remain valid across model builds and discards. Existing shape parameters,
offsets, and tolerances can be edited through these views. After geometry changes,
call line.aperture.build_model() to refresh its polygons and bounds.
To add profiles or pipe definitions, first call env.aperture.discard_model();
this also discards dependent line aperture models. To add pipe placements or capture
an updated lattice survey, call line.aperture.discard_model() before rebuilding.
Discarding preserves the current definitions and placements.
This geometric model is used for aperture calculations. To use its cross-sections
as loss checks during tracking, obtain limit elements with
line.aperture.get_limit_elements_at_s(...) and insert them into the line.
Save and reload the model
The aperture model can be serialised with env.to_json() together with the lattice.
Pass include_aperture=True to include the profile and pipe definitions,
pipe placements, and halo parameters.
Alternatively, only the aperture model can be exported with env.aperture.to_json().
It can then be loaded into a compatible environment (without an already-present aperture
model) with env.aperture.from_json().
Further reference
Physics guide for the physics manual’s aperture modelling chapter
Aperture model tutorial for a more detailed example using a toy ring
Run the downloaded script to display the plots. To save them as well, use
python aperture_fodo.py --output-dir figures.