Magnet errors¶
Seeded magnet-error injection for xsuite lines.
These helpers add reproducible imperfections to an xtrack.Line so that
tracking studies can exercise a distorted closed orbit. They are deliberately
minimal and side-effecting (they mutate the line in place) and return the applied
values so the same imperfection can be mirrored onto a matching model (e.g. a
MAD-NG twiss model) when required.
- xtrack_tools.errors.apply_relative_bend_field_errors(line, *, rms, seed, name_prefix=None)[source]¶
Apply a seeded relative dipole field error to every powered bend in place.
A field error scales the dipole field
k0by(1 + rel_error)while leaving the reference geometryh = angle / lengthunchanged — the physical meaning of, say, a 0.08% bend error. Powered bends are those with a non-zeroangle; the geometry is read fromangle/lengthand the nativek0field is set to(angle / length) * (1 + rel_error).Perturbing the native
k0(rather than adding a separateknl[0]multipole) is what makes an xsuite line and a matching MAD-NG model agree to the codes’ floor (~1e-8): a stand-alone dipole multipole inside a curved element is transported differently by the two codes (~0.1% orbit disagreement), whereas the native dipole field is handled identically.- Parameters:
- Returns:
{name: k0_after}for every perturbed bend, so the identical absolutek0values can be written onto a matching model.- Return type:
- xtrack_tools.errors.apply_vertical_quad_misalignment(line, *, rms, seed, name_prefix=None)[source]¶
Apply a seeded vertical misalignment to every quadrupole in place.
Each
xtrack.Quadrupoleis shifted vertically by an independent draw fromN(0, rms)via itsshift_yattribute, distorting the vertical closed orbit.- Parameters:
line (Line) – Line to modify in place.
rms (float) – Standard deviation of the vertical offset in metres (e.g.
2e-4for 0.2 mm).seed (int) – Seed for the
numpyrandom generator (reproducible).name_prefix (str | None) – If given, only elements whose name starts with this prefix (case-insensitive) are considered.
- Returns:
{name: shift_y}for every misaligned quadrupole.- Return type:
- xtrack_tools.errors.apply_quad_tilt(line, *, rms, seed, name_prefix=None)[source]¶
Apply a seeded roll about the
saxis to every quadrupole in place.Each
xtrack.Quadrupoleis rolled by an independent draw fromN(0, rms)via itsrot_s_radattribute. A rolled quadrupole carries a skew component~ 2 * theta * k1l, which couples the horizontal dispersion into the vertical plane. This is the cheapest physical way to give a tracking line a genuine vertical dispersion that a nominal (untilted) model does not know about – unlikeapply_vertical_quad_misalignment(), which kicks the vertical orbit but leavesDyessentially untouched.- Parameters:
line (Line) – Line to modify in place.
rms (float) – Standard deviation of the roll angle in radians (e.g.
5e-4for 0.5 mrad).seed (int) – Seed for the
numpyrandom generator (reproducible).name_prefix (str | None) – If given, only elements whose name starts with this prefix (case-insensitive) are considered.
- Returns:
{name: rot_s_rad}for every tilted quadrupole.- Return type:
- xtrack_tools.errors.correct_closed_orbit(line, *, monitor_prefix, corrector_prefix_x, corrector_prefix_y, n_singular_values=None)[source]¶
Steer the closed orbit flat with the machine’s own orbit correctors.
Real error lattices are steered by operators before measurement, leaving a few mm of residual orbit. Note the correctors are dipoles, so the corrected line carries their dispersion even though its closed orbit is flat.
- Parameters:
line (Line) – Line to correct in place.
monitor_prefix (str) – Case-insensitive name prefix selecting the BPMs.
corrector_prefix_x (str) – Case-insensitive name prefix selecting the horizontal correctors.
corrector_prefix_y (str) – As above, vertical.
n_singular_values (int | None) – Truncate the correction SVD to this rank.
Noneuses every mode.
- Returns:
{"x_before": .., "x_after": .., "y_before": .., "y_after": ..}closed orbit RMS at the BPMs in metres.- Return type: