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 k0 by (1 + rel_error) while leaving the reference geometry h = angle / length unchanged — the physical meaning of, say, a 0.08% bend error. Powered bends are those with a non-zero angle; the geometry is read from angle / length and the native k0 field is set to (angle / length) * (1 + rel_error).

Perturbing the native k0 (rather than adding a separate knl[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:
  • line (Line) – Line to modify in place.

  • rms (float) – Standard deviation of the relative field error (e.g. 8e-4).

  • seed (int) – Seed for the numpy random generator (reproducible).

  • name_prefix (str | None) – If given, only elements whose name starts with this prefix (case-insensitive) are considered.

Returns:

{name: k0_after} for every perturbed bend, so the identical absolute k0 values can be written onto a matching model.

Return type:

dict[str, float]

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.Quadrupole is shifted vertically by an independent draw from N(0, rms) via its shift_y attribute, 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-4 for 0.2 mm).

  • seed (int) – Seed for the numpy random 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:

dict[str, float]

xtrack_tools.errors.apply_quad_tilt(line, *, rms, seed, name_prefix=None)[source]

Apply a seeded roll about the s axis to every quadrupole in place.

Each xtrack.Quadrupole is rolled by an independent draw from N(0, rms) via its rot_s_rad attribute. 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 – unlike apply_vertical_quad_misalignment(), which kicks the vertical orbit but leaves Dy essentially untouched.

Parameters:
  • line (Line) – Line to modify in place.

  • rms (float) – Standard deviation of the roll angle in radians (e.g. 5e-4 for 0.5 mrad).

  • seed (int) – Seed for the numpy random 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:

dict[str, float]

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. None uses every mode.

Returns:

{"x_before": .., "x_after": .., "y_before": .., "y_after": ..} closed orbit RMS at the BPMs in metres.

Return type:

dict[str, float]