at.load.madx#

Using MAD-X files with PyAT#

PyAT can read lattice descriptions in Mad-X format (.seq files), and can export lattices in MAD-X format.

However, because of intrinsic differences between PyAT and MAD-X, some incompatibilities must be taken into account.

1. Translation losses#

6-D motion#

While AT allows setting 6-D motion and synchrotron radiation on individual elements, MAD has a global setting for the whole lattice. When reading a MAD-X lattice without radiation, 6-D motion in the resulting AT lattice is turned off, including in RF cavities. If the MAD-X lattice has radiation on, 6-D motion is activated on the AT lattice according to default settings (RF cavities active, radiation in dipoles, quadrupoles and wigglers).

Combined function magnets#

MAD has little support for combined function magnets. When exporting a lattice in MAD format, the main field component for each magnet class in kept but other components disappear, except in the few cases handled by MAD (quadrupole and sextupole components in SBEND or RBEND, skew quad component in QUADRUPOLE, see the MAD user’s reference manual for more).

Multipoles#

MAD has no thick multipoles. Multipoles in MAD format (MULTIPOLE element) are interpreted as ThinMultipole elements. In the other direction, an AT Multipole is converted to a thin MULTIPOLE surrounded by two drift spaces.

MAD elements absent from AT#

Many MAD elements have no equivalent in AT. They are replaced by Marker or Drift elements, depending on their length.

Incompatible attributes#

Some AT element attributes have no MAD equivalent, and vice versa.

When exporting to a MAD-X file:

  • NumIntSteps, MaxOrder are discarded,

  • FringeBendEntrance, FringeBendExit, FringeQuadEntrance, FringeQuadExit are discarded,

  • R1, R2, T1, T2 are discarded,

  • RApertures, EApertures are discarded.

When reading a MAD-X file:

  • TILT is interpreted and converted to R1 and R2 attributes,

  • KTAP is interpreted and converted to FieldScaling.

2. Reading MAD-X files#

Short way#

The load_madx() function created a Lattice from a LINE or SEQUENCE in a MAD-X file.

>>> ring = at.load_madx("lattice.seq", use="PS")
>>> print(ring)
Lattice(<1295 elements>, name='PS', energy=1000000000.0, particle=Particle('positron'),
periodicity=1, harmonic_number=0, beam_current=0.0, nbunch=1, use='PS')

Detailed way#

MadxParser creates an empty database which can be populated with the elements of a MAD-X file.

>>> parser = at.MadxParser()

The MadxParser.parse_files() method reads one or several MAD-X files and populates the parser

>>> parser.parse_files("lattice.seq", use="PS")

The parser can be examined and modified using the standard python syntax:

>>> parser["pr_bht91_f"]
sbend(name=PR.BHT91.F, l=2.1975925, angle='angle.prbhf', k1='k1prbhf+k1prpfwf-k1prf8l')
>>> parser["angle.prbhf"]
0.03135884818
>>> parser["angle.prbhf"] = 0.032

All MAD parameters can be interactively modified and their last value will be taken into account when generating a PyAT lattice.

The MadxParser.lattice() method creates a Lattice from a LINE or SEQUENCE of the parser:

>>> ring = parser.lattice(use="ps")
>>> print(ring)
Lattice(<1295 elements>, name='PS', energy=1000000000.0, particle=Particle('positron'),
periodicity=1, harmonic_number=0, beam_current=0.0, nbunch=1, use='PS')

3. Exporting to MAD-X files#

Exporting a PyAT lattice to a MAD-X files produces a single MAD SEQUENCE of LINE.

See save_madx() for usage.

4. Functions and classes#

Functions

load_madx(*files[, use, strict, verbose])

Create a Lattice from MAD-X files

save_madx(ring[, filename])

Save a Lattice as a MAD-X file

Classes

MadParameter(parser, expr)

MAD parameter

MadxParser(**kwargs)

MAD-X specific parser

class MadParameter(parser, expr)[source]#

Bases: str

MAD parameter

A MAD parameter is an expression which can be evaluated n the context of a MAD parser

Parameters:
  • parser – MadParser instance defining the context for evaluation

  • expr – expression to be evaluated

The expression may contain MAD parameter names, arithmetic operators and mathematical functions known by MAD

evaluate()[source]#
class MadxParser(**kwargs)[source]#

Bases: _MadParser

MAD-X specific parser

The parser is a subclass of dict and is database containing all the MAD-X parameters and objects.

Example

Parse a 1st file:

>>> parser = at.MadxParser()
>>> parser.parse_file("file1")

Parse another file. This adds the new definitions to the database:

>>> parser.parse_file("file2")

Look at the rf_on variable

>>> parser["rf_on"]
0

Get the list of available sequences/lines:

>>> parser.sequences
['arca',
 'arca_inj',
 'arcb_inj',
 'low_emit_ring',
 'arc_inj',
 'low_emit_ring_inj']

Generate an AT Lattice from the low_emit_ring sequence

>>> ring1 = parser.lattice(use="low_emit_ring")

Change the value of a variable:

>>> parser["rf_on"] = 1

Generate a new AT Lattice taking into account the new variables:

>>> ring2 = parser.lattice(use="low_emit_ring")

Generate an AT Lattice from another sequence:

>>> arca = parser.lattice(use="ring")
Parameters:
  • strict – If False, assign 0 to undefined variables

  • verbose – If True, print details on the processing

  • **kwargs – Initial variable definitions

clear()#

Clear the database: remove all parameters and objects

evaluate(expr)#

Evaluate the right side of an expression

Parameters:

expr (str) – expression to evaluate

Returns:

value – evaluated expression

expand(key)#

iterator over AT objects generated by a source object

lattice(use='ring', **kwargs)#

Create a lattice from the selected sequence

Parameters:

use (str) – Name of the MAD sequence or line containing the desired lattice. Default: ring

Keyword Arguments:
  • name (str) – Name of the lattice. Default: MAD sequence name.

  • particle (Particle) – Circulating particle. Default: from MAD

  • energy (float) – Energy of the lattice [eV]. Default: from MAD

  • periodicity (int) – Number of periods. Default: 1

  • * – All other keywords will be set as Lattice attributes

parse_files(*filenames, final=True, prolog=None, epilog=None, **kwargs)#

Process files and fill the database

Parameters:
  • *filenames (str) – Files to process

  • final (bool) – If True, signals that the undefined variables may be set to the default value

  • prolog (None | int | Callable[[...], None])

  • epilog (Callable[[...], None] | None)

  • **kwargs – Initial variable definitions

parse_lines(lines, *, final=True, **kwargs)#

Process input lines and fill the database

Parameters:
  • lines (Iterable[str]) – Iterable of input lines

  • final (bool) – If True, signals that the undefined variables may be set to the default value

  • **kwargs – Initial variable definitions

property ignored#

Set of ignored commands

property missing#

Set of missing definitions

property sequences#

List of available sequences or lines

load_madx(*files, use='ring', strict=True, verbose=False, **kwargs)[source]#

Create a Lattice from MAD-X files

  • The energy and particle of the generated lattice are taken from the MAD-X BEAM object, using the MAD-X default parameters: positrons at 1 Gev. These parameters are overloaded by the value given in the energy and particle keyword arguments.

  • The radiation state is given by the RADIATE flag of the BEAM object, using the AT defaults: RF cavities active, synchrotron radiation in dipoles and quadrupoles.

  • Long elements are split according to the default AT value of NumIntSteps (10).

Parameters:
  • files (str) – Names of one or several MAD-X files

  • strict (bool) – If False, assign 0 to undefined variables

  • use (str) – Name of the MADX sequence or line containing the desired lattice. Default: ring

  • verbose (bool) – If True, print details on the processing

Keyword Arguments:
  • name (str) – Name of the lattice. Default: MADX sequence name.

  • particle (Particle) – Circulating particle. Default: from MADX

  • energy (float) – Energy of the lattice [eV]. Default: from MADX

  • periodicity (int) – Number of periods. Default: 1

  • * – Other keywords will be used as initial variable definitions

Returns:

lattice (Lattice) – New Lattice object

See also

load_lattice() for a generic lattice-loading function.

save_madx(ring, filename=None, **kwargs)[source]#

Save a Lattice as a MAD-X file

Parameters:
  • ring (Lattice) – lattice

  • filename (str | None) – file to be created. If None, write to sys.stdout

Keyword Arguments:
  • use (str | None) – name of the created SEQUENCE of LINE. Default: name of the PyAT lattice

  • use_line (bool) – If True, use a MAD “LINE” format. Otherwise, use a MAD “SEQUENCE”