Python package to query and build LHC optics.
Installation:
git clone https://github.com/rdemaria/lhcoptics.git
cd lhcoptics
pip install -e .
Usage:
from lhcoptics import LHC
inj=LHC().y2025.pp.ramp[0].plot()
The package consists of the following main components:
LHC: Manages the acc-models-lhc LHC optics repository, allowing access to different LHC optics configurations.LHCRepo: Manages a branch or tag of the LHC optics repository.LHCCycle: Manages a LHC cycle, including the LHC optics and the LHC beam.LHCProcess: Manages a LHC beam process: a list of LHC optics and a settings parametrized by time.LHCOptics: An optics configuration consisting of strengths, knobs and parameters.
Example: plot the IR1 optics in the first step the ramp process of the 2055 proton-proton cycle
from lhcoptics import LHC
LHC().y2025.pp.ramp[0].ir1.plot()
The LHC class is responsible for managing the LHC optics repository. It provides functionality to extract branches or tags from the repository and store local copies of the optics. The class also includes mechanisms to regularly check branches for updates.
The class stores data locally, by default in Python user site, or using the environment variable LHCOPTICS_BASEDIR. The Git repository URL can also be customized by LHCOPTICS_GIT_URL.
Usage:
from lhcoptics import LHC
# initialize LHC instance repository
# create a directory if it doesn't exist
# check gitlab for newer versions
lhc = LHC()
# for a check gitlab for updates
lhc.check_local_branches()
# list of branches
lhc.branches
# list of tags
lhc.tags
# extract 2025 branch
lhc.y2025
An LHCOptics contains data for specifying a full LHC Optics. It contains global params and knobs and a list of LHC sections (ir1, ir2, ... , ir8, a12, a23, ..., a78) that contains strengths, local params and local knobs. The optics can contain a model that allows to compute twiss, plot and rematch the optics.
Methods:
-
LHCOptics.set_repository(branch): Create a link or a directoryacc-models-lhcin the local directory fromgitlab.cern.ch/acc-models/acc-models-lhcif it does not exists. Branch are2024,hl16etc. If$HOME/local/acc-models-lhc/branchexists, a symlink to this directory is created, else the a full git clone is done. -
LHCOptics.from_json(filename): Create optics from a json file -
LHCOptics.from_dict(dct): Create optics from a dict containing strengths, paramns and knobs, irs and arcs dict -
LHCOptics.from_madx(filename,model): Create optics from a madx istance by extracting strengths, params and knobs from variables. If model is 'madx' or 'xsuite' it will attach the model to the optics -
LHCOptics.from_madxfile(madxfile): As before, but creating a madx instance from a madx script
An LHCSection is indentified by name, start, end, strengths, params and knobs. A LHCSection is specialzed in LHC Arcs and LHCIRs.
Methods:
-
cls.from_json(filename): Create section from a json file -
cls.from_dict(dct): Create section from a dict containing strengths, paramns and knobs -
cls.from_madxfile(filename): Create section from a madx file -
cls.from_madx(madx): Create section from a madx instance -
to_json(filename): Save section to a json file -
to_dict(): Save section to a dict -
to_madx(filename,dst): Save section to dst which could bestr, filename, a madx instance, an open file. -
update_model(): transfer strengths and knobs data toparent.model -
update_strength(src): update existing strengths fromsrc.strengths, orsrcorparent.modelif src is None -
update_knobs(src): update existing knobs fromsrc.knobs, orsrcorparent.modelif src is None -
update_params(src): update existing params fromsrc.params, orsrcorparent.modelif src is None -
update(src): combine update_strengths, knobs and params. -
get_params(): measure hardcoded parameters from model -
get_params_from_twiss(): measure hardcoded parameters from a twiss table -
set_params(): add or replace hardcoded parameters from model -
twiss(beam,method): get the twiss table table: beam in (1,2), method see specific sections -
plot(beam,method): plot the twiss table: beam in (1,2), method see specific sections
twiss_from_init(beam): get twiss table from boundary conditionstwiss_full(beam): get the twiss table from the full opticstwiss_from_params(beam): get the twiss table with init from paramsset_init(): set the initial conditions
twiss_init(beam): get twiss init at boundaries of the arc from periodic solutiontwiss_init_cell(beam): get twiss init at boundaries of celltwiss_cell(beam): get periodic solution of cell
Class definitions, camel case alphabetically sorted:
- class variables lower snake case alphabetically sorted
- static methods
- class methods
- init
- other special double underscore methods
- methods starting with verb lower snake case alphabetically sorted
Repository objects have the following methods
__init__: create object from path, read data from file, populate attributesrefresh: re-read data from the file or from dict()read_data: read the from fileto_dict: return a dictionary of data to be saved from attributuressave_data: save data from attributes to file
See examples/repo/job_add_cycles.py.
- add extra 2025 MD cycles
- LHCOptics for hl optics
- for the knobs use class approach based on some euristic in from_madx
- missing knobs specialization
- octupole knobs
- dpp knmob
- documentation