accelmo 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. accelmo-0.0.1/LICENSE +21 -0
  2. accelmo-0.0.1/PKG-INFO +208 -0
  3. accelmo-0.0.1/README.md +166 -0
  4. accelmo-0.0.1/pyproject.toml +187 -0
  5. accelmo-0.0.1/setup.cfg +4 -0
  6. accelmo-0.0.1/src/accelmo/__init__.py +8 -0
  7. accelmo-0.0.1/src/accelmo/analysis/__init__.py +21 -0
  8. accelmo-0.0.1/src/accelmo/analysis/degeneracy_checker.py +308 -0
  9. accelmo-0.0.1/src/accelmo/backend/__init__.py +26 -0
  10. accelmo-0.0.1/src/accelmo/backend/madng/__init__.py +5 -0
  11. accelmo-0.0.1/src/accelmo/backend/madng/accelerator_hooks.py +171 -0
  12. accelmo-0.0.1/src/accelmo/backend/madng/engines.py +535 -0
  13. accelmo-0.0.1/src/accelmo/backend/madng/interface.py +600 -0
  14. accelmo-0.0.1/src/accelmo/backend/madng/machine_state.py +62 -0
  15. accelmo-0.0.1/src/accelmo/backend/madng/mad_scripts/run_closed_twiss_init.mad +399 -0
  16. accelmo-0.0.1/src/accelmo/backend/madng/scripts.py +367 -0
  17. accelmo-0.0.1/src/accelmo/backend/protocol.py +274 -0
  18. accelmo-0.0.1/src/accelmo/backend/xtrack/__init__.py +1 -0
  19. accelmo-0.0.1/src/accelmo/backend/xtrack/engines.py +366 -0
  20. accelmo-0.0.1/src/accelmo/backend/xtrack/model.py +292 -0
  21. accelmo-0.0.1/src/accelmo/config.py +169 -0
  22. accelmo-0.0.1/src/accelmo/fitting/__init__.py +4 -0
  23. accelmo-0.0.1/src/accelmo/fitting/bpm_gains.py +54 -0
  24. accelmo-0.0.1/src/accelmo/fitting/config.py +44 -0
  25. accelmo-0.0.1/src/accelmo/fitting/lifecycle.py +40 -0
  26. accelmo-0.0.1/src/accelmo/fitting/lm_loop.py +212 -0
  27. accelmo-0.0.1/src/accelmo/fitting/pool.py +75 -0
  28. accelmo-0.0.1/src/accelmo/fitting/priors.py +58 -0
  29. accelmo-0.0.1/src/accelmo/fitting/protocol.py +265 -0
  30. accelmo-0.0.1/src/accelmo/fitting/reduction.py +65 -0
  31. accelmo-0.0.1/src/accelmo/fitting/results.py +36 -0
  32. accelmo-0.0.1/src/accelmo/fitting/setup.py +239 -0
  33. accelmo-0.0.1/src/accelmo/fitting/shared_reference.py +102 -0
  34. accelmo-0.0.1/src/accelmo/fitting/uncertainty.py +115 -0
  35. accelmo-0.0.1/src/accelmo/fitting/weights.py +13 -0
  36. accelmo-0.0.1/src/accelmo/fitting/worker.py +250 -0
  37. accelmo-0.0.1/src/accelmo/machine/__init__.py +1 -0
  38. accelmo-0.0.1/src/accelmo/machine/accelerators/__init__.py +27 -0
  39. accelmo-0.0.1/src/accelmo/machine/accelerators/base.py +276 -0
  40. accelmo-0.0.1/src/accelmo/machine/accelerators/fcc.py +82 -0
  41. accelmo-0.0.1/src/accelmo/machine/accelerators/lhc.py +188 -0
  42. accelmo-0.0.1/src/accelmo/machine/accelerators/magnet_grouping.py +149 -0
  43. accelmo-0.0.1/src/accelmo/machine/accelerators/psb.py +202 -0
  44. accelmo-0.0.1/src/accelmo/machine/accelerators/selection.py +109 -0
  45. accelmo-0.0.1/src/accelmo/machine/accelerators/sps.py +125 -0
  46. accelmo-0.0.1/src/accelmo/machine/knob_names.py +14 -0
  47. accelmo-0.0.1/src/accelmo/machine/machine_state.py +65 -0
  48. accelmo-0.0.1/src/accelmo/machine/patterns.py +96 -0
  49. accelmo-0.0.1/src/accelmo/momentum_reference.py +271 -0
  50. accelmo-0.0.1/src/accelmo/monitor/__init__.py +21 -0
  51. accelmo-0.0.1/src/accelmo/monitor/__main__.py +52 -0
  52. accelmo-0.0.1/src/accelmo/monitor/app.py +338 -0
  53. accelmo-0.0.1/src/accelmo/monitor/cache.py +240 -0
  54. accelmo-0.0.1/src/accelmo/monitor/panels.py +261 -0
  55. accelmo-0.0.1/src/accelmo/monitor/source.py +52 -0
  56. accelmo-0.0.1/src/accelmo/monitor/view.py +39 -0
  57. accelmo-0.0.1/src/accelmo/optimisers/__init__.py +18 -0
  58. accelmo-0.0.1/src/accelmo/optimisers/adam.py +136 -0
  59. accelmo-0.0.1/src/accelmo/optimisers/base.py +84 -0
  60. accelmo-0.0.1/src/accelmo/optimisers/lbfgs.py +208 -0
  61. accelmo-0.0.1/src/accelmo/optimisers/levenberg_marquardt.py +253 -0
  62. accelmo-0.0.1/src/accelmo/poco/__init__.py +34 -0
  63. accelmo-0.0.1/src/accelmo/poco/calibrated.py +328 -0
  64. accelmo-0.0.1/src/accelmo/poco/calibration.py +234 -0
  65. accelmo-0.0.1/src/accelmo/poco/closed_orbit.py +335 -0
  66. accelmo-0.0.1/src/accelmo/poco/fitter.py +410 -0
  67. accelmo-0.0.1/src/accelmo/poco/workers/__init__.py +1 -0
  68. accelmo-0.0.1/src/accelmo/poco/workers/calibrated.py +200 -0
  69. accelmo-0.0.1/src/accelmo/poco/workers/closed_orbit.py +501 -0
  70. accelmo-0.0.1/src/accelmo/poco/workers/closed_twiss.py +174 -0
  71. accelmo-0.0.1/src/accelmo/runlog/__init__.py +25 -0
  72. accelmo-0.0.1/src/accelmo/runlog/reader.py +127 -0
  73. accelmo-0.0.1/src/accelmo/runlog/writer.py +150 -0
  74. accelmo-0.0.1/src/accelmo/tracking/__init__.py +59 -0
  75. accelmo-0.0.1/src/accelmo/tracking/config/__init__.py +29 -0
  76. accelmo-0.0.1/src/accelmo/tracking/config/helpers.py +43 -0
  77. accelmo-0.0.1/src/accelmo/tracking/config/models.py +66 -0
  78. accelmo-0.0.1/src/accelmo/tracking/config/tracking.py +535 -0
  79. accelmo-0.0.1/src/accelmo/tracking/data_manager.py +476 -0
  80. accelmo-0.0.1/src/accelmo/tracking/dispatch/__init__.py +1 -0
  81. accelmo-0.0.1/src/accelmo/tracking/dispatch/payloads.py +210 -0
  82. accelmo-0.0.1/src/accelmo/tracking/dispatch/setup.py +212 -0
  83. accelmo-0.0.1/src/accelmo/tracking/dispatch/turn_planner.py +196 -0
  84. accelmo-0.0.1/src/accelmo/tracking/fitter.py +698 -0
  85. accelmo-0.0.1/src/accelmo/tracking/ic_iteration.py +110 -0
  86. accelmo-0.0.1/src/accelmo/tracking/lm_problem.py +252 -0
  87. accelmo-0.0.1/src/accelmo/tracking/ranges.py +112 -0
  88. accelmo-0.0.1/src/accelmo/tracking/session.py +301 -0
  89. accelmo-0.0.1/src/accelmo/tracking/sgd/__init__.py +1 -0
  90. accelmo-0.0.1/src/accelmo/tracking/sgd/checkpointing.py +229 -0
  91. accelmo-0.0.1/src/accelmo/tracking/sgd/loop.py +327 -0
  92. accelmo-0.0.1/src/accelmo/tracking/sgd/scheduler.py +70 -0
  93. accelmo-0.0.1/src/accelmo/tracking/uncertainty.py +160 -0
  94. accelmo-0.0.1/src/accelmo/tracking/worker.py +661 -0
  95. accelmo-0.0.1/src/accelmo.egg-info/PKG-INFO +208 -0
  96. accelmo-0.0.1/src/accelmo.egg-info/SOURCES.txt +100 -0
  97. accelmo-0.0.1/src/accelmo.egg-info/dependency_links.txt +1 -0
  98. accelmo-0.0.1/src/accelmo.egg-info/entry_points.txt +2 -0
  99. accelmo-0.0.1/src/accelmo.egg-info/requires.txt +36 -0
  100. accelmo-0.0.1/src/accelmo.egg-info/top_level.txt +1 -0
  101. accelmo-0.0.1/tests/test_calibration.py +198 -0
  102. accelmo-0.0.1/tests/test_package_layers.py +108 -0
accelmo-0.0.1/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Joshua Gray
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
accelmo-0.0.1/PKG-INFO ADDED
@@ -0,0 +1,208 @@
1
+ Metadata-Version: 2.4
2
+ Name: accelmo
3
+ Version: 0.0.1
4
+ Summary: Accelerator Effective Lattice Modelling and Optimisation
5
+ Author: Joshua Gray
6
+ License: MIT
7
+ Project-URL: Repository, https://github.com/jgray-19/accelmo
8
+ Requires-Python: >=3.11
9
+ Description-Content-Type: text/markdown
10
+ License-File: LICENSE
11
+ Requires-Dist: numpy
12
+ Requires-Dist: threadpoolctl
13
+ Requires-Dist: pandas
14
+ Requires-Dist: tfs-pandas
15
+ Requires-Dist: pymadng>=0.10.0
16
+ Requires-Dist: pymadng-utils>=0.0.2
17
+ Requires-Dist: matplotlib
18
+ Requires-Dist: uncertainties
19
+ Requires-Dist: scipy>=1.17.1
20
+ Provides-Extra: test
21
+ Requires-Dist: pytest; extra == "test"
22
+ Requires-Dist: pytest-cov; extra == "test"
23
+ Requires-Dist: pytest-xdist; extra == "test"
24
+ Provides-Extra: docs
25
+ Requires-Dist: sphinx>=7.0; extra == "docs"
26
+ Requires-Dist: sphinx-autodoc-typehints>=2.0; extra == "docs"
27
+ Requires-Dist: myst-parser>=2.0; extra == "docs"
28
+ Requires-Dist: furo>=2025.12.19; extra == "docs"
29
+ Provides-Extra: tracking
30
+ Requires-Dist: omc3>0.26.0; extra == "tracking"
31
+ Requires-Dist: cpymad; extra == "tracking"
32
+ Requires-Dist: pyarrow; extra == "tracking"
33
+ Requires-Dist: psutil; extra == "tracking"
34
+ Provides-Extra: monitor
35
+ Requires-Dist: panel>=1.5; extra == "monitor"
36
+ Provides-Extra: dev
37
+ Requires-Dist: pre-commit; extra == "dev"
38
+ Requires-Dist: ruff; extra == "dev"
39
+ Provides-Extra: all
40
+ Requires-Dist: accelmo[dev,docs,monitor,test,tracking]; extra == "all"
41
+ Dynamic: license-file
42
+
43
+ # accelmo
44
+
45
+ [![Coverage Status](https://github.com/jgray-19/accelmo/actions/workflows/coverage.yml/badge.svg)](https://github.com/jgray-19/accelmo/actions/workflows/coverage.yml)
46
+ [![codecov](https://codecov.io/github/jgray-19/accelmo/graph/badge.svg?token=Y1KZACDFPL)](https://codecov.io/github/jgray-19/accelmo)
47
+
48
+ Estimation of accelerator magnet errors (strengths, misalignments and tilts) from
49
+ beam measurements, using gradient-based optimisation of MAD-NG models.
50
+
51
+ Two families of fit are provided:
52
+
53
+ | Fit | Data | Entry points |
54
+ |---|---|---|
55
+ | Tracking | Turn-by-turn BPM data, tracked through the model | `ArcByArcFitter`, `ACDMarkerFitter`, `KickerFitter` |
56
+ | Closed orbit | Closed orbit and phase advance, at one or more machine states | `ClosedOrbitFitter`, `CalibratedClosedOrbitFitter` |
57
+ | Closed twiss | Closed orbit, phase advance, beta and dispersion | `ClosedOrbitFitter` with `closed_twiss_series` |
58
+
59
+ Supported machines: LHC, PSB, SPS and FCC.
60
+
61
+ Full documentation: <https://jgray-19.github.io/accelmo/>
62
+
63
+ ## Installation
64
+
65
+ Requires Python 3.11 or later.
66
+
67
+ ```bash
68
+ git clone https://github.com/jgray-19/accelmo.git
69
+ cd accelmo
70
+ uv sync
71
+ ```
72
+
73
+ Optional dependency groups:
74
+
75
+ | Extra | Contents |
76
+ |---|---|
77
+ | `tracking` | omc3, cpymad, pyarrow, psutil (required by the tracking workflows) |
78
+ | `monitor` | Panel, for the live fit monitor (`python -m accelmo.monitor`) |
79
+ | `test` | pytest, pytest-cov, pytest-xdist |
80
+ | `docs` | Sphinx and theme |
81
+ | `dev` | ruff, pre-commit |
82
+
83
+ ```bash
84
+ uv sync --extra test --extra docs --extra tracking
85
+ ```
86
+
87
+ ### Companion packages
88
+
89
+ Companion packages:
90
+
91
+ | Package | Purpose |
92
+ |---|---|
93
+ | [pymadng-utils](https://pypi.org/project/pymadng-utils/) | Shared accelerator abstractions, dp/p and pt conversion, knob-file I/O |
94
+ | [xtrack_tools](https://github.com/jgray-19/xtrack_tools) | Tracking helpers and dataframe conversion (tests) |
95
+
96
+ `pymadng-utils` is a normal dependency. `xtrack_tools` is pinned in the `dev` dependency
97
+ group, so `uv sync` installs it.
98
+
99
+ ## Usage
100
+
101
+ A tracking fit is configured with an accelerator and four configuration objects,
102
+ then run with `fitter.run()`. It returns a `FitResult`: the fitted knob values,
103
+ their 1-sigma uncertainties under the same knob names, and `diagnostics` saying why
104
+ and after how many epochs the fit stopped. Optional settings (starting knobs, true
105
+ strengths for diagnostics, output and checkpoint settings, callbacks) go in a
106
+ `FitterOptions`.
107
+
108
+ ```python
109
+ from pathlib import Path
110
+
111
+ from accelmo.config import OptimiserConfig, TrackingDataConfig, WorkerRuntime
112
+ from accelmo.fitting.config import OutputConfig, SequenceConfig
113
+ from accelmo.machine.accelerators import LHC
114
+ from accelmo.tracking import (
115
+ ArcByArcFitter,
116
+ FitterOptions,
117
+ MeasurementConfig,
118
+ MeasurementDetails,
119
+ )
120
+
121
+ accelerator = LHC(beam=1, sequence_file="lhcb1.seq", errors={"quad": {"k1"}})
122
+
123
+ fitter = ArcByArcFitter(
124
+ accelerator=accelerator,
125
+ solver=OptimiserConfig(
126
+ max_epochs=200,
127
+ warmup_epochs=10,
128
+ warmup_lr_start=1e-6,
129
+ max_lr=1e-4,
130
+ min_lr=1e-6,
131
+ ),
132
+ runtime=WorkerRuntime(num_workers=8),
133
+ tracking_data=TrackingDataConfig(num_batches=4),
134
+ sequence_config=SequenceConfig(magnet_range="$start/$end"),
135
+ measurement_config=MeasurementConfig({Path("measurement.parquet"): MeasurementDetails()}),
136
+ bpm_start_points=["BPM.12R1.B1"],
137
+ bpm_end_points=["BPM.20R1.B1"],
138
+ options=FitterOptions(output_config=OutputConfig(write_run_log=False)),
139
+ )
140
+ result = fitter.run()
141
+ print(result.knobs, result.uncertainties, result.diagnostics.reason)
142
+ ```
143
+
144
+ Further examples are in `tests/training/`, which exercise each fitter end to end.
145
+
146
+ ### Tracking modes
147
+
148
+ | Class | Initial conditions | Tracking |
149
+ |---|---|---|
150
+ | `ArcByArcFitter` | BPM at the start of each range | Forward and backward over the configured BPM ranges |
151
+ | `ACDMarkerFitter` | AC-dipole `before`/`after` markers | Bidirectional, whole ring observed |
152
+ | `KickerFitter` | Kicker marker | Single worker, forward only, `turns_per_track` turns |
153
+
154
+ `KickerFitter` additionally takes the `kicker_name` and `turns_per_track`.
155
+ The measurement data must contain `x`, `px`, `y`, `py` at the kicker marker, and the
156
+ sequence must include the kicker element.
157
+
158
+ ### Closed-twiss fits
159
+
160
+ The closed-twiss and closed-orbit fitters live in `accelmo.poco` (Parametric
161
+ Optimisation of Closed Orbits) and return the same `FitResult` as the tracking fitters.
162
+
163
+ `ClosedOrbitFitter(series=closed_twiss_series(measurements, observables))` fits knobs so that
164
+ the model's periodic optics match measured closed orbit, beta, phase and dispersion
165
+ simultaneously, using a single parametric MAD-NG `twiss`. `measurements` maps the measurement
166
+ momentum `pt` to a measured optics dataframe; each becomes one absolute series. The solver is
167
+ Levenberg-Marquardt, configured by `LevenbergMarquardtConfig`.
168
+
169
+ ### Closed-orbit fits
170
+
171
+ `ClosedOrbitFitter` fits knobs to one or more `ClosedOrbitSeries`. Each series
172
+ carries a `machine_state`: the MAD-X globals (quadrupole strengths, corrector kicks,
173
+ tune knobs) at which its orbits were measured. These are fixed inputs and may differ
174
+ between series. Varying the quadrupole strengths between series makes quadrupole
175
+ misalignments observable.
176
+
177
+ Measured orbits are always absolute. A plane in `absolute_planes` is fitted as is; every
178
+ other plane is fitted as the change from the fitter's own `machine_state` to the series'
179
+ state, the fitter subtracting each measurement's `reference` orbit. `machine_state` accepts a
180
+ dictionary, a knobs file or a TFS corrector table; `accelmo.machine.machine_state.merge_machine_states`
181
+ combines several. Accepted iterations are recorded in `fitter.history`, and
182
+ `fitter.close()` shuts down the MAD-NG process.
183
+
184
+ See `tests/training/test_closed_orbit_machine_state.py`.
185
+
186
+ ## Tests
187
+
188
+ ```bash
189
+ uv run pytest -m "not slow" # fast suite
190
+ uv run pytest -m slow # convergence and end-to-end tests
191
+ uv run pytest --cov=accelmo
192
+ ```
193
+
194
+ Markers are listed in `pyproject.toml` and `tests/README.md`.
195
+
196
+ ## Documentation
197
+
198
+ ```bash
199
+ uv sync --extra docs
200
+ cd docs && uv run make html
201
+ ```
202
+
203
+ The output is written to `docs/_build/html/index.html`.
204
+
205
+ ## Related repositories
206
+
207
+ The measurement and campaign workflows built on this package are maintained
208
+ separately: `lhc_measurements`, `psb_md`, `psb_loco`, `lhc_loco` and `fcc_loco`.
@@ -0,0 +1,166 @@
1
+ # accelmo
2
+
3
+ [![Coverage Status](https://github.com/jgray-19/accelmo/actions/workflows/coverage.yml/badge.svg)](https://github.com/jgray-19/accelmo/actions/workflows/coverage.yml)
4
+ [![codecov](https://codecov.io/github/jgray-19/accelmo/graph/badge.svg?token=Y1KZACDFPL)](https://codecov.io/github/jgray-19/accelmo)
5
+
6
+ Estimation of accelerator magnet errors (strengths, misalignments and tilts) from
7
+ beam measurements, using gradient-based optimisation of MAD-NG models.
8
+
9
+ Two families of fit are provided:
10
+
11
+ | Fit | Data | Entry points |
12
+ |---|---|---|
13
+ | Tracking | Turn-by-turn BPM data, tracked through the model | `ArcByArcFitter`, `ACDMarkerFitter`, `KickerFitter` |
14
+ | Closed orbit | Closed orbit and phase advance, at one or more machine states | `ClosedOrbitFitter`, `CalibratedClosedOrbitFitter` |
15
+ | Closed twiss | Closed orbit, phase advance, beta and dispersion | `ClosedOrbitFitter` with `closed_twiss_series` |
16
+
17
+ Supported machines: LHC, PSB, SPS and FCC.
18
+
19
+ Full documentation: <https://jgray-19.github.io/accelmo/>
20
+
21
+ ## Installation
22
+
23
+ Requires Python 3.11 or later.
24
+
25
+ ```bash
26
+ git clone https://github.com/jgray-19/accelmo.git
27
+ cd accelmo
28
+ uv sync
29
+ ```
30
+
31
+ Optional dependency groups:
32
+
33
+ | Extra | Contents |
34
+ |---|---|
35
+ | `tracking` | omc3, cpymad, pyarrow, psutil (required by the tracking workflows) |
36
+ | `monitor` | Panel, for the live fit monitor (`python -m accelmo.monitor`) |
37
+ | `test` | pytest, pytest-cov, pytest-xdist |
38
+ | `docs` | Sphinx and theme |
39
+ | `dev` | ruff, pre-commit |
40
+
41
+ ```bash
42
+ uv sync --extra test --extra docs --extra tracking
43
+ ```
44
+
45
+ ### Companion packages
46
+
47
+ Companion packages:
48
+
49
+ | Package | Purpose |
50
+ |---|---|
51
+ | [pymadng-utils](https://pypi.org/project/pymadng-utils/) | Shared accelerator abstractions, dp/p and pt conversion, knob-file I/O |
52
+ | [xtrack_tools](https://github.com/jgray-19/xtrack_tools) | Tracking helpers and dataframe conversion (tests) |
53
+
54
+ `pymadng-utils` is a normal dependency. `xtrack_tools` is pinned in the `dev` dependency
55
+ group, so `uv sync` installs it.
56
+
57
+ ## Usage
58
+
59
+ A tracking fit is configured with an accelerator and four configuration objects,
60
+ then run with `fitter.run()`. It returns a `FitResult`: the fitted knob values,
61
+ their 1-sigma uncertainties under the same knob names, and `diagnostics` saying why
62
+ and after how many epochs the fit stopped. Optional settings (starting knobs, true
63
+ strengths for diagnostics, output and checkpoint settings, callbacks) go in a
64
+ `FitterOptions`.
65
+
66
+ ```python
67
+ from pathlib import Path
68
+
69
+ from accelmo.config import OptimiserConfig, TrackingDataConfig, WorkerRuntime
70
+ from accelmo.fitting.config import OutputConfig, SequenceConfig
71
+ from accelmo.machine.accelerators import LHC
72
+ from accelmo.tracking import (
73
+ ArcByArcFitter,
74
+ FitterOptions,
75
+ MeasurementConfig,
76
+ MeasurementDetails,
77
+ )
78
+
79
+ accelerator = LHC(beam=1, sequence_file="lhcb1.seq", errors={"quad": {"k1"}})
80
+
81
+ fitter = ArcByArcFitter(
82
+ accelerator=accelerator,
83
+ solver=OptimiserConfig(
84
+ max_epochs=200,
85
+ warmup_epochs=10,
86
+ warmup_lr_start=1e-6,
87
+ max_lr=1e-4,
88
+ min_lr=1e-6,
89
+ ),
90
+ runtime=WorkerRuntime(num_workers=8),
91
+ tracking_data=TrackingDataConfig(num_batches=4),
92
+ sequence_config=SequenceConfig(magnet_range="$start/$end"),
93
+ measurement_config=MeasurementConfig({Path("measurement.parquet"): MeasurementDetails()}),
94
+ bpm_start_points=["BPM.12R1.B1"],
95
+ bpm_end_points=["BPM.20R1.B1"],
96
+ options=FitterOptions(output_config=OutputConfig(write_run_log=False)),
97
+ )
98
+ result = fitter.run()
99
+ print(result.knobs, result.uncertainties, result.diagnostics.reason)
100
+ ```
101
+
102
+ Further examples are in `tests/training/`, which exercise each fitter end to end.
103
+
104
+ ### Tracking modes
105
+
106
+ | Class | Initial conditions | Tracking |
107
+ |---|---|---|
108
+ | `ArcByArcFitter` | BPM at the start of each range | Forward and backward over the configured BPM ranges |
109
+ | `ACDMarkerFitter` | AC-dipole `before`/`after` markers | Bidirectional, whole ring observed |
110
+ | `KickerFitter` | Kicker marker | Single worker, forward only, `turns_per_track` turns |
111
+
112
+ `KickerFitter` additionally takes the `kicker_name` and `turns_per_track`.
113
+ The measurement data must contain `x`, `px`, `y`, `py` at the kicker marker, and the
114
+ sequence must include the kicker element.
115
+
116
+ ### Closed-twiss fits
117
+
118
+ The closed-twiss and closed-orbit fitters live in `accelmo.poco` (Parametric
119
+ Optimisation of Closed Orbits) and return the same `FitResult` as the tracking fitters.
120
+
121
+ `ClosedOrbitFitter(series=closed_twiss_series(measurements, observables))` fits knobs so that
122
+ the model's periodic optics match measured closed orbit, beta, phase and dispersion
123
+ simultaneously, using a single parametric MAD-NG `twiss`. `measurements` maps the measurement
124
+ momentum `pt` to a measured optics dataframe; each becomes one absolute series. The solver is
125
+ Levenberg-Marquardt, configured by `LevenbergMarquardtConfig`.
126
+
127
+ ### Closed-orbit fits
128
+
129
+ `ClosedOrbitFitter` fits knobs to one or more `ClosedOrbitSeries`. Each series
130
+ carries a `machine_state`: the MAD-X globals (quadrupole strengths, corrector kicks,
131
+ tune knobs) at which its orbits were measured. These are fixed inputs and may differ
132
+ between series. Varying the quadrupole strengths between series makes quadrupole
133
+ misalignments observable.
134
+
135
+ Measured orbits are always absolute. A plane in `absolute_planes` is fitted as is; every
136
+ other plane is fitted as the change from the fitter's own `machine_state` to the series'
137
+ state, the fitter subtracting each measurement's `reference` orbit. `machine_state` accepts a
138
+ dictionary, a knobs file or a TFS corrector table; `accelmo.machine.machine_state.merge_machine_states`
139
+ combines several. Accepted iterations are recorded in `fitter.history`, and
140
+ `fitter.close()` shuts down the MAD-NG process.
141
+
142
+ See `tests/training/test_closed_orbit_machine_state.py`.
143
+
144
+ ## Tests
145
+
146
+ ```bash
147
+ uv run pytest -m "not slow" # fast suite
148
+ uv run pytest -m slow # convergence and end-to-end tests
149
+ uv run pytest --cov=accelmo
150
+ ```
151
+
152
+ Markers are listed in `pyproject.toml` and `tests/README.md`.
153
+
154
+ ## Documentation
155
+
156
+ ```bash
157
+ uv sync --extra docs
158
+ cd docs && uv run make html
159
+ ```
160
+
161
+ The output is written to `docs/_build/html/index.html`.
162
+
163
+ ## Related repositories
164
+
165
+ The measurement and campaign workflows built on this package are maintained
166
+ separately: `lhc_measurements`, `psb_md`, `psb_loco`, `lhc_loco` and `fcc_loco`.
@@ -0,0 +1,187 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "accelmo"
7
+ version = "0.0.1"
8
+ description = "Accelerator Effective Lattice Modelling and Optimisation"
9
+ authors = [
10
+ { name = "Joshua Gray" },
11
+ ]
12
+ readme = "README.md"
13
+ license = { text = "MIT" }
14
+ requires-python = ">=3.11"
15
+ dependencies = [
16
+ "numpy",
17
+ "threadpoolctl",
18
+ "pandas",
19
+ "tfs-pandas",
20
+ "pymadng>=0.10.0",
21
+ "pymadng-utils>=0.0.2",
22
+ "matplotlib",
23
+ "uncertainties",
24
+ "scipy>=1.17.1",
25
+ ]
26
+
27
+ [project.urls]
28
+ Repository = "https://github.com/jgray-19/accelmo"
29
+
30
+ [project.scripts]
31
+ accelmo-monitor = "accelmo.monitor.__main__:main"
32
+
33
+ [project.optional-dependencies]
34
+ test = [
35
+ "pytest",
36
+ "pytest-cov",
37
+ "pytest-xdist",
38
+ ]
39
+ docs = [
40
+ "sphinx>=7.0",
41
+ "sphinx-autodoc-typehints>=2.0",
42
+ "myst-parser>=2.0",
43
+ "furo>=2025.12.19",
44
+ ]
45
+ tracking = [
46
+ "omc3 > 0.26.0",
47
+ "cpymad",
48
+ "pyarrow",
49
+ "psutil",
50
+ ]
51
+ monitor = [
52
+ "panel>=1.5",
53
+ ]
54
+ dev = [
55
+ "pre-commit",
56
+ "ruff",
57
+ ]
58
+ all = ["accelmo[test,docs,tracking,monitor,dev]"]
59
+
60
+ [dependency-groups]
61
+ dev = [
62
+ "xtrack-tools",
63
+ ]
64
+
65
+ [tool.uv.sources]
66
+ xtrack-tools = { git = "https://github.com/jgray-19/xtrack_tools", rev = "0e6779d35546db12182a99d25c1cba60d395c90e" }
67
+
68
+
69
+ [tool.setuptools]
70
+ package-dir = {"" = "src"}
71
+
72
+ [tool.setuptools.packages.find]
73
+ where = ["src"]
74
+ include = ["accelmo*"]
75
+
76
+ [tool.setuptools.package-data]
77
+ "accelmo.backend.madng" = ["mad_scripts/*.mad"]
78
+
79
+ [tool.coverage.run]
80
+ # Use the importable package name so coverage can map executed files correctly
81
+ # in CI even when the project is installed into site-packages.
82
+ source = ["accelmo"]
83
+ relative_files = true
84
+
85
+ [tool.pytest.ini_options]
86
+ testpaths = ["tests"]
87
+ norecursedirs = ["tests/data"]
88
+ markers = [
89
+ "slow: computationally expensive tests",
90
+ "regression: fast behavioural regression contracts",
91
+ "integration: multi-component integration tests",
92
+ "convergence: full optimiser convergence tests",
93
+ "lhc: LHC-specific tests",
94
+ "sps: SPS-specific tests",
95
+ "psb: PSB-specific tests",
96
+ "serial: test must not run in parallel",
97
+ ]
98
+ log_cli = true
99
+ log_cli_level = "INFO"
100
+ log_cli_format = "%(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)"
101
+ log_cli_date_format = "%Y-%m-%d %H:%M:%S"
102
+
103
+ # ----- Dev Tools Configuration ----- #
104
+
105
+ [tool.ruff]
106
+ exclude = [
107
+ ".eggs",
108
+ ".git",
109
+ ".mypy_cache",
110
+ ".venv",
111
+ "_build",
112
+ "build",
113
+ "dist",
114
+ ]
115
+
116
+ target-version = "py311"
117
+
118
+ line-length = 100
119
+ indent-width = 4
120
+
121
+ [tool.ruff.lint]
122
+ # Allow unused variables when underscore-prefixed.
123
+ dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
124
+ ignore = [
125
+ "E501", # line too long
126
+ "FBT001", # boolean-type-hint-positional-argument
127
+ "FBT002", # boolean-default-value-positional-argument
128
+ "PT019", # pytest-fixture-param-without-value (but suggested solution fails)
129
+ "TRY004", # config validation raises ValueError by design
130
+ "RUF012", # class-level family tables are constants
131
+ "UP042", # str-mixin enums keep their string behaviour
132
+ ]
133
+ extend-select = [
134
+ "F", # Pyflakes rules
135
+ "W", # PyCodeStyle warnings
136
+ "E", # PyCodeStyle errors
137
+ "I", # Sort imports properly
138
+ "A", # Detect shadowed builtins
139
+ "N", # enforce naming conventions, e.g. ClassName vs function_name
140
+ "UP", # Warn if certain things can change due to newer Python versions
141
+ "C4", # Catch incorrect use of comprehensions, dict, list, etc
142
+ "FA", # Enforce from __future__ import annotations
143
+ "FBT", # detect boolean traps
144
+ "ISC", # Good use of string concatenation
145
+ "BLE", # disallow catch-all exceptions
146
+ "ICN", # Use common import conventions
147
+ "RET", # Good return practices
148
+ "SIM", # Common simplification rules
149
+ "TID", # Some good import practices
150
+ "TC", # Enforce importing certain types in a TYPE_CHECKING block
151
+ "PTH", # Use pathlib instead of os.path
152
+ "NPY", # Some numpy-specific things
153
+ "D101", # public class without docstring
154
+ "D102", # public method without docstring
155
+ "D103", # public function without docstring
156
+ ]
157
+ # Allow fix for all enabled rules (when `--fix`) is provided.
158
+ fixable = ["ALL"]
159
+ unfixable = []
160
+
161
+ [tool.ruff.lint.per-file-ignores]
162
+ # Docstrings are required in the package only.
163
+ "!src/**" = ["D101", "D102", "D103"]
164
+
165
+
166
+ [tool.ty.src]
167
+ exclude = [
168
+ "**/acc-models-lhc/",
169
+ "*_analysis*/",
170
+ "*_results/",
171
+ "analysis*/",
172
+ "b1_flat_60_18cm/",
173
+ "betabeat/",
174
+ "data/",
175
+ "docs/api/",
176
+ "/mad_scripts/",
177
+ "models/**",
178
+ "plots/",
179
+ "*.out",
180
+ "*.png",
181
+ "*.txt",
182
+ "*.parquet",
183
+ "*.mad",
184
+ "temp*/",
185
+ "*.out",
186
+ "*.json",
187
+ ]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,8 @@
1
+ """Public interface for the accelmo package.
2
+
3
+ The package bundles configuration helpers, optimiser implementations, and
4
+ simulation orchestrators for magnet knob tuning studies.
5
+ """
6
+
7
+ # The top-level package intentionally does not re-export a large symbol surface.
8
+ __all__ = []
@@ -0,0 +1,21 @@
1
+ """Analysis utilities that inspect a fit without changing the optimisation.
2
+
3
+ Currently this exposes the degeneracy checker, which examines the Gauss-Newton
4
+ normal matrix ``A = JᵀWJ`` to reveal knob combinations the measurement cannot
5
+ constrain - the flat directions that make an optimiser wander instead of
6
+ converging to a unique minimum.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ from accelmo.analysis.degeneracy_checker import (
12
+ DegeneracyReport,
13
+ DegenerateDirection,
14
+ analyse_degeneracy,
15
+ )
16
+
17
+ __all__ = [
18
+ "DegeneracyReport",
19
+ "DegenerateDirection",
20
+ "analyse_degeneracy",
21
+ ]