User Guide¶
Tracking fits¶
A tracking fit recovers knob strengths by tracking measured initial conditions
through the MAD-NG model and comparing the result with the measured turn-by-turn
data. A fit is defined by an accelerator, four configuration objects and an optional
FitterOptions. fitter.run() returns a
FitResult: the fitted knobs, their 1-sigma
uncertainties under the same knob names, and diagnostics (why and after how many
epochs the fit stopped).
Object |
Role |
|---|---|
|
Machine definition: sequence file, kinetic energy, BPM pattern and the knob
families to fit ( |
|
|
Worker count, worker log level and BLAS threads. |
|
Batch count, data and validation fractions, momenta in the loss, track length. |
|
Magnet range exposed to MAD-NG and BPMs to exclude. |
|
Measurement files, each with a |
|
|
Starting knobs, true strengths, output and checkpoint settings, callbacks, and the
|
Fitters¶
Fitter |
Initial conditions |
Tracking |
|---|---|---|
BPM at the start of each range |
Forward and backward over the |
|
AC-dipole |
Bidirectional; the whole ring is observed. |
|
Kicker marker |
One worker, forward only, |
|
Before-marker, every |
Back-to-back |
All fit families, the knob families of each machine and the solvers are listed in Fit Families.
KickerFitter also takes the kicker_name and turns_per_track.
The measurement data must contain x, px, y and py at the kicker
marker, and the sequence must contain the kicker element.
Example¶
from pathlib import Path
from accelmo.config import OptimiserConfig, TrackingDataConfig, WorkerRuntime
from accelmo.fitting.config import OutputConfig, SequenceConfig
from accelmo.machine.accelerators import LHC
from accelmo.tracking import (
ArcByArcFitter,
FitterOptions,
MeasurementConfig,
MeasurementDetails,
)
accelerator = LHC(beam=1, sequence_file="lhcb1.seq", errors={"quad": {"k1"}})
fitter = ArcByArcFitter(
accelerator=accelerator,
solver=OptimiserConfig(
max_epochs=200,
warmup_epochs=10,
warmup_lr_start=1e-6,
max_lr=1e-4,
min_lr=1e-6,
),
runtime=WorkerRuntime(num_workers=8),
tracking_data=TrackingDataConfig(num_batches=4),
sequence_config=SequenceConfig(magnet_range="$start/$end"),
measurement_config=MeasurementConfig(
{Path("measurement.parquet"): MeasurementDetails()}
),
bpm_start_points=["BPM.12R1.B1"],
bpm_end_points=["BPM.20R1.B1"],
options=FitterOptions(output_config=OutputConfig(write_run_log=False)),
)
result = fitter.run()
print(result.knobs, result.uncertainties, result.diagnostics.reason)
FitterOptions¶
initial_knob_strengthsStarting values, in optimisation space. Knobs left out keep the model value.
true_strengthsReference values (file or dictionary) used to report the error of the fit.
debugRun MAD-NG in debug mode.
output_configOutputConfig: run log, TensorBoard logging, uncertainty estimation and log files.checkpoint_configCheckpointConfig: periodic checkpoints and restart.initial_conditions_callbackEpoch-end hook that returns refreshed start coordinates for every worker, or
Noneto keep them.loss_callbackCalled each epoch with the loss values.
Validation¶
TrackingDataConfig.validation_fraction (default 0.1) holds out a disjoint set of turns
per file. These are never used for training, so the validation loss is out-of-sample.
data_fraction is applied to the remaining training turns.
Closed-twiss fits¶
The closed-twiss and closed-orbit fitters live in accelmo.poco and return the
same FitResult as the tracking fitters.
ClosedOrbitFitter with
series=closed_twiss_series(measurements, observables)
(closed_twiss_series()) fits knobs so that the periodic model
optics match a measured closed twiss. Closed orbit, beta, phase and
dispersion are all obtained from one parametric MAD-NG twiss, so they are fitted
simultaneously and no starting point is taken from the measurement. The solver is
Levenberg-Marquardt, configured by
LevenbergMarquardtConfig.
measurements maps each measurement’s pt to a measured optics dataframe; each is one
absolute series. Parameters are
weighted by the inverse measurement variance; use_errors=False normalises every
observable family identically instead. knob_sigmas adds a Gaussian prior of absolute widths.
Closed-orbit fits¶
ClosedOrbitFitter fits knobs to one or
more ClosedOrbitSeries.
machine_stateMAD-X globals (quadrupole strengths, corrector kicks, tune knobs) at which a series was measured. They are fixed inputs and may differ between series. Varying the quadrupole strengths between series makes quadrupole misalignments observable. Accepted forms are a dictionary, a knobs file or a TFS corrector table;
accelmo.machine.machine_state.merge_machine_states()combines several. Settingmachine_stateon the fitter provides a default inherited by every series.absolute_planesPlanes fitted as absolute orbits. The remaining planes are fitted as the change from the fitter’s own
machine_stateto the series’ state: the fitter subtracts each measurement’sreferenceorbit (errors added in quadrature), which is required for those planes. Measured orbits are always passed in absolute.observablesFitted quantities:
x,y, and the phase advancesmu1,mu2(mu1/mu2are set per series).
Accepted iterations are stored in fitter.history. Call fitter.close() to shut
down the MAD-NG process once the fit is complete.
CalibratedClosedOrbitFitter extends this with BPM gains and corrector kick
calibrations as additional parameters, with Gaussian priors sigma_bpm and
sigma_corrector. Every k_<corrector> kick a series’ machine_state sets gets a gain; a
series may set several or none (an off-momentum orbit). fit_corrector_gains=False fits the BPM
gains alone, which is the mode for absolute orbits.
Degeneracy analysis¶
accelmo.analysis.degeneracy_checker evaluates the eigenspectrum of the
Gauss-Newton normal matrix at the initial knobs. Near-zero eigenvalues identify knob
combinations that the data cannot constrain, so the problem can be regularised or the
knob set reduced before running the fit. Use
check_degeneracy() on a tracking fitter.
Terminal dashboard and logs¶
A tracking fit draws a live terminal dashboard (FitDashboard):
a strip of stages (data, model, workers, fit, uncertainty), progress with ETA, the loss
history, the best epoch so far and the last warnings. It does nothing when the output is
not a terminal; the run log has the numbers then.
Call accelmo.logs.configure_logging() once at the top of a script to print WARNING
and above to the terminal and, with file=, write INFO and above to a file. The
dashboard hides the console handler while it is on screen and shows warnings in its own
strip.
Each worker process writes every record at WorkerRuntime.logging_level (default
DEBUG) to workers/python_worker_<id>.log in the run log directory. Only WARNING
and above are relayed to the main process, tagged with the worker name, so they reach the
dashboard and the main log. A worker with its own python_logfile keeps it.
Note
Workers are forked processes, and the warning relay reaches them only by fork (Linux, the
default start method up to Python 3.13). The spawn and forkserver start methods, the
default on macOS, Windows and Python 3.14, are not supported: a spawned worker would write its
own log file but its warnings would not reach the main process or the dashboard.
Monitoring a fit¶
Every fit writes a run log under OutputConfig.run_log_root (switch off with
OutputConfig(write_run_log=False)). Serve live plots of running and finished fits
with:
uv sync --extra monitor
python -m accelmo.monitor RUN_LOG_ROOT --spec my_package.monitor:SPEC
accelmo-monitor is the same command as python -m accelmo.monitor. Then open
http://localhost:5006. On a remote machine, forward the port with
ssh -L 5006:localhost:5006 host. Without --spec only the generic panels are
shown. See accelmo.monitor and accelmo.runlog.
Testing¶
pytest -m "not slow" # fast suite
pytest -m slow # convergence and end-to-end tests
pytest --cov=accelmo
Markers are listed in tests/README.md. Tests marked serial must not run in
parallel (do not combine with -n). The tests under tests/training/ double as
worked examples.