MAD-X Utilities

pymadng_utils.madx groups the helpers that interact with MAD-X directly or post-process MAD-NG outputs into MAD-X-style TFS files.

The main entry point in this package area is make_madx_sequence(), which exports a saved sequence from an OMC3 model directory. The function is intentionally strict:

  • it expects job.create_model_nominal.madx to exist

  • it requires explicit sequence selection statements in that job file

  • it raises on unsupported or ambiguous layouts instead of silently guessing

That behaviour is deliberate and matches the committed test fixtures and supported workflows in this repository.

pymadng_utils.madx exports only the TFS conversion helpers, so it imports with the core install. make_sequence imports cpymad and omc3 (the model extra) and is imported explicitly: from pymadng_utils.madx.make_sequence import make_madx_sequence.

Package exports

MAD-X helpers. make_sequence needs the model extra and is imported explicitly.

pymadng_utils.madx.convert_multiple_tfs_files(tfs_files)[source]

Convert multiple TFS files to MAD-X format.

Parameters:

tfs_files (list[Path]) – List of TFS file paths to convert.

Return type:

None

pymadng_utils.madx.convert_tfs_to_madx(tfs_df, remove_drifts=True)[source]

Convert TFS DataFrame from MAD-NG format to MAD-X format.

This function performs the following transformations:

  • Drops every column whose name is not lowercase (MAD-NG’s extra columns)

  • Converts the remaining column names and all header keys to uppercase

  • Renames phase advance columns: ‘MU1’ → ‘MUX’, ‘MU2’ → ‘MUY’ (keeps existing MUX/MUY)

  • If remove_drifts, renames the elements of kind drift whose name starts with “drift” to consecutive DRIFT_0, DRIFT_1, …

  • Sets ‘NAME’ as index and removes special rows (‘$start’, ‘$end’)

Parameters:
  • tfs_df (pandas.DataFrame) – Input TFS DataFrame in MAD-NG format.

  • remove_drifts – Renumber the drift elements as described above.

Returns:

Converted TFS DataFrame in MAD-X format.

Return type:

pandas.DataFrame

pymadng_utils.madx.export_tfs_to_madx(tfs_file)[source]

Read a TFS file, convert to MAD-X format, and overwrite the original.

This is a convenience function that reads a TFS file, applies the MAD-NG to MAD-X conversion, and writes it back.

Parameters:

tfs_file (Path) – Path to the TFS file to convert.

Raises:

FileNotFoundError – If the TFS file doesn’t exist.

Return type:

None

Sequence export

MAD-X utility functions for model sequence creation.

pymadng_utils.madx.make_sequence.make_madx_sequence(model_dir, *, seq_outdir=None, beam4=False, post_optics_madx_files=None)[source]

Generate and save a matched MAD-X sequence file from a model folder.

  • Supports LHC and PSB model directories

  • Uses the existing PSB nominal job file and stops after tune matching

  • Optionally injects post-optics modifiers for creator-backed accelerators

  • Saves the resulting sequence to seq_outdir or the model directory

Parameters:
  • model_dir (Path | str)

  • seq_outdir (Path | None)

  • beam4 (bool)

  • post_optics_madx_files (Sequence[Path | str] | None)

Return type:

Path

TFS conversion

TFS file format conversion utilities.

pymadng_utils.madx.tfs_utils.convert_tfs_to_madx(tfs_df, remove_drifts=True)[source]

Convert TFS DataFrame from MAD-NG format to MAD-X format.

This function performs the following transformations:

  • Drops every column whose name is not lowercase (MAD-NG’s extra columns)

  • Converts the remaining column names and all header keys to uppercase

  • Renames phase advance columns: ‘MU1’ → ‘MUX’, ‘MU2’ → ‘MUY’ (keeps existing MUX/MUY)

  • If remove_drifts, renames the elements of kind drift whose name starts with “drift” to consecutive DRIFT_0, DRIFT_1, …

  • Sets ‘NAME’ as index and removes special rows (‘$start’, ‘$end’)

Parameters:
  • tfs_df (pandas.DataFrame) – Input TFS DataFrame in MAD-NG format.

  • remove_drifts – Renumber the drift elements as described above.

Returns:

Converted TFS DataFrame in MAD-X format.

Return type:

pandas.DataFrame

pymadng_utils.madx.tfs_utils.export_tfs_to_madx(tfs_file)[source]

Read a TFS file, convert to MAD-X format, and overwrite the original.

This is a convenience function that reads a TFS file, applies the MAD-NG to MAD-X conversion, and writes it back.

Parameters:

tfs_file (Path) – Path to the TFS file to convert.

Raises:

FileNotFoundError – If the TFS file doesn’t exist.

Return type:

None

pymadng_utils.madx.tfs_utils.convert_multiple_tfs_files(tfs_files)[source]

Convert multiple TFS files to MAD-X format.

Parameters:

tfs_files (list[Path]) – List of TFS file paths to convert.

Return type:

None