Skip to main content

pymadng-utils

codecov Coverage

Alpha (0.0.1). The API may change without notice.

pymadng-utils is a small Python library for accelerator-model workflows that cross the MAD-X/MAD-NG boundary. It provides:

  • LHC, PSB and FCC-ee machine descriptors;
  • a managed MAD-NG session that loads a saved MAD-X sequence and configures its beam;
  • observation, TWISS, tune matching, AC-dipole, AC-kicker, orbit-correction, magnet-strength, and perturbation helpers;
  • conversion of MAD-NG TFS tables to MAD-X/OMC3 naming conventions;
  • export of reusable sequences from existing OMC3 model directories; and
  • end-to-end LHC and PSB model-creation workflows and FCC-ee lattice preparation.

The package is workflow infrastructure, not a replacement for MAD-NG, MAD-X, or OMC3. Most operations require a working accelerator software environment and real sequence/model files.

Installation

Python 3.11 or newer is required.

Install the core package:

python -m pip install .

Install an editable checkout with the dependencies used by model creation and tests:

python -m pip install -e '.[test]'

The extras are:

Extra Adds Intended use
model cpymad, omc3 sequence export and model creation
test pytest, pytest-cov, cpymad, omc3 repository test suite
dev pre-commit, ruff linting and pre-commit hooks
docs Sphinx and the Furo theme local documentation build

MAD-NG itself must be usable by pymadng; cpymad additionally needs a working MAD-X installation. OMC3 model creation may also require access to CERN accelerator-model repositories, depending on its fetch configuration.

Quick start: load a sequence and run TWISS

An accelerator descriptor holds the machine-specific contract: sequence name, particle and kinetic energy, BPM selection pattern, tune knobs, integer tunes, and AC-dipole element name. Constructing AcceleratorMadInterface immediately starts MAD-NG, loads the sequence, and attaches the beam.

from pathlib import Path

from pymadng_utils.accelerators import LHC
from pymadng_utils.mad import AcceleratorMadInterface

accelerator = LHC(
    beam=1,
    sequence_file=Path("lhcb1_saved.seq"),
    kinetic_energy=6800.0,  # GeV; this is kinetic, not total energy
)

with AcceleratorMadInterface(accelerator) as interface:
    interface.observe()  # uses the LHC BPM pattern
    twiss = interface.run_twiss(coupling=True)
    print(twiss.headers["q1"], twiss.headers["q2"])

run_twiss() returns the pymadng table converted to a DataFrame, indexed by element name when the result contains a name column. Its headers are augmented with particle and total energy in GeV.

Use the interface as a context manager, or call close() explicitly. The MAD-X sequence is translated on every load; load_sequence(cache_translation=True) writes a sibling .mad cache, which makes that directory writable-required and is unsafe for parallel loaders of the same sequence.

Off-momentum coordinates

MAD-NG uses pt, while many accelerator workflows use relative momentum deviation dp/p. The descriptor and interface both expose beam-aware conversions:

with AcceleratorMadInterface(accelerator) as interface:
    pt = interface.dp2pt(1e-3)
    twiss = interface.run_twiss(pt=pt, coupling=True)

The convenience pt= argument is mutually exclusive with explicit X0= and deltap= arguments. A deltap= argument (without an explicit X0=) is normalised through the same path: it is converted to pt and seeded as the sixth X0 coordinate, so run_twiss(deltap=dp), run_twiss(pt=interface.dp2pt(dp)), and MAD-NG's own twiss{deltap=dp} are all bit-identical.

interface.dp2pt/interface.pt2dp run MAD.gphys.dp2pt/pt2dp inside MAD-NG on the loaded sequence's beam.beta, with the value sent and received over the pipe as a double, so nothing is lost to string formatting or to a second implementation of the same formula. Note these are not mutual inverses to the last bit (~1e-13 relative), and the closed-orbit search amplifies that to ~1e-9 on the orbit over a full ring, so convert once and stay in one coordinate rather than round-tripping. Standalone conversion functions are available from pymadng_utils.physics when the reference beta is already known and no MAD-NG session is at hand.

What else is here

The usage guide documents the rest in full: the LHC and PSB descriptors, the four MAD-NG interface classes and their operations, saved-sequence export from an OMC3 model directory (LHC, PSB), model-table regeneration, the packaged end-to-end LHC creator, and the TFS/knob conversion helpers. The API reference documents each object from its docstrings.

Development

Install the git hooks once with pre-commit install (ruff, whitespace checks, and a commit-message check).

Run the test suite:

pytest

Some tests start MAD-NG or MAD-X and use committed accelerator fixtures; they are integration tests, not pure unit tests.

Build the Sphinx documentation:

python -m pip install -e '.[docs]'
make -C docs html

The generated site starts at docs/_build/html/index.html.

Repository layout

src/pymadng_utils/
├── accelerators/   LHC/PSB/FCC-ee descriptors
├── io/             knob-file I/O
├── mad/            MAD-NG session interfaces
├── mad_scripts/    packaged MAD-NG scripts (output suppression, tune matching)
├── madx/           sequence export and TFS conversion
├── model_creator/  higher-level model workflows
└── physics.py      particle masses and dp/p ↔ pt conversion

The project is distributed under the terms in LICENSE.

Metadata

Release files for pymadng-utils 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pymadng-utils 0.0.1
File Size Uploaded Provenance Actions
pymadng_utils-0.0.1.tar.gz 43.0 kB With provenance Details for pymadng_utils-0.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pymadng-utils 0.0.1
File Interpreter ABI Platform Actions
pymadng_utils-0.0.1-py3-none-any.whl Python 3 none any Details for pymadng_utils-0.0.1-py3-none-any.whl

Total release size: 91.9 kB

Release files / pymadng_utils-0.0.1.tar.gz

Download URL pymadng_utils-0.0.1.tar.gz
Size 43.0 kB
Tags Source
SHA-256 checksum
How to use checksums
f00c7c6fc872fe9b9dd70a3d3ad4fbb64b288b51facd7f3043ea78e6fa016068
BLAKE2b-256 checksum
How to use checksums
fd580b52b24aaeca983f7ca803e98bd14ceb7412f2bf08e83aebdbafcad236e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.

Transparency log

Release files / pymadng_utils-0.0.1-py3-none-any.whl

Download URL pymadng_utils-0.0.1-py3-none-any.whl
Size 48.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
73d6b1bf0b6cc8f0f228d0ca40a0c63d97786b0c3c0d47e07d542920a778ff86
BLAKE2b-256 checksum
How to use checksums
fd67331b3b69eb503bbbecd1a73af6886e0e4175cebd5deafaf8c4454f5a6f5d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.

Transparency log

Security for pymadng-utils 0.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pymadng_utils-0.0.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page