Model Creator¶
pymadng_utils.model_creator builds higher-level workflows on top of the lower-level MAD-NG and MAD-X utilities.
Typical workflow¶
Create or fetch a base model directory with OMC3.
Generate a saved MAD-X sequence with
pymadng_utils.madx.make_sequence.make_madx_sequence.Re-open that sequence in MAD-NG and export TWISS tables with
update_model_with_madng.
Notes¶
create_lhc_modelandcreate_psb_modelare the packaged end-to-end workflows.create_fcc_modelonly rewrites the FCC-ee MAD-X text; it needs neither MAD-X nor OMC3.create_modelsimportsomc3and needs themodelextra.Sequence generation intentionally expects a real nominal OMC3 job file and now raises on ambiguous layouts instead of guessing from directory names.
Create models¶
Script to create LHC model directories for beam 1 and beam 2 at 18cm optics using omc3.
This script orchestrates the complete model creation workflow: 1. Creates nominal model using omc3 2. Generates MAD-X sequences 3. Updates model with MAD-NG (tune matching, twiss computation) 4. Exports TFS files in MAD-X format
- pymadng_utils.model_creator.create_models.create_lhc_model(beam, output_dir, year, *, fetch='afs', path=None, nat_tunes=(0.28, 0.31), drv_tunes=(0.27, 0.322), energy=6800.0, modifiers=None)[source]¶
Create a complete LHC model for the specified beam.
This function performs the full workflow:
Creates model instance using omc3
Exports the MAD-X sequence
lhcb<beam>_saved.seq(for beam 2 this is the forward beam-4 tracking sequence, soseq_dir=1is used for the MAD-NGLHCdescriptor)Updates model with MAD-NG (tune matching and twiss computation)
- Parameters:
beam (int) – Beam number (1 or 2).
output_dir (pathlib.Path) – Directory where model files will be created.
year (str) – LHC optics year.
fetch (str) – omc3
fetchmethod;pathis required unless it is"afs".path (str | None) – omc3 accelerator-model path for non-AFS fetch methods.
nat_tunes (tuple[float, float]) – Natural fractional tunes [Q1, Q2].
drv_tunes (tuple[float, float] | None) – Driven fractional tunes [Q1, Q2];
Noneskips the AC dipole.energy (float) – Total beam energy in GeV (omc3 and MAD-X
beam, energy=convention); the MAD-NGLHCdescriptor getsenergy - PROTON_MASS_GEVas kinetic energy.modifiers (str | list[str] | None) – omc3 optics modifier files.
- Raises:
ValueError – If beam is not 1 or 2.
- Return type:
None
- pymadng_utils.model_creator.create_models.create_psb_model(ring, output_dir, year, *, fetch='afs', path=None, nat_tunes=(0.17, 0.225), drv_tunes=None, kinetic_energy=0.16, dpp=0.0, cycle_point='1_flat_bottom', str_file='psb_fb_lhcindiv.str', scenario='lhc_indiv', update_with_madng=True)[source]¶
Create a complete PSB model for the specified ring.
This function performs the full workflow:
Creates model instance using omc3
Generates MAD-X sequence files
Updates model with MAD-NG (tune matching and twiss computation)
update_with_madng=Falsestops after step 2, leaving the plain omc3 model in place. PSB callers that seed the MAD-NG update with extra magnet strengths, or that need it re-run withconvert_to_madx=Falsefor in-process tracking, run their own MAD-NG step against this base model instead of the one this function would otherwise run – so this flag lets them skip the redundant first pass rather than computing (and discarding) a Twiss twice.- Parameters:
ring (int) – PSB ring number (1, 2, 3, or 4).
output_dir (pathlib.Path) – Directory where model files will be created.
year (str) – Scenario year/era.
fetch (str) – omc3
fetchmethod;pathis required unless it is"afs".path (str | None) – omc3 accelerator-model path for non-AFS fetch methods.
nat_tunes (tuple[float, float]) – Natural fractional tunes [Q1, Q2].
drv_tunes (tuple[float, float] | None) – Driven fractional tunes [Q1, Q2].
Noneskips the AC dipole excitation.kinetic_energy (float) – Kinetic energy in GeV.
dpp (float) – Relative momentum deviation the model is built/twissed at.
cycle_point (str) – PSB cycle point.
str_file (str) – Strength file name.
scenario (str) – omc3 scenario name.
update_with_madng (bool) – Run the MAD-NG update (step 3); see above.
- Raises:
ValueError – If ring is not 1, 2, 3, or 4.
- Return type:
None
FCC-ee lattice preparation¶
Prepare the FCC-ee LCC MAD-X lattice for LOCO studies.
- pymadng_utils.model_creator.create_fcc_model.kick_variable(name)[source]¶
Return the variable that drives the kick of corrector
name.
- pymadng_utils.model_creator.create_fcc_model.bpms_to_monitors(madx_text)[source]¶
Declare the
bpm_*markers asmonitor(what the MAD-NG fitter expects of a BPM).
- pymadng_utils.model_creator.create_fcc_model.slice_sextupoles(madx_text, nslice=3)[source]¶
Give every sextupole
nsliceMAD-NG slices (a single slice is too coarse for the FCC-ee sextupoles).
- pymadng_utils.model_creator.create_fcc_model.defer_correctors(madx_text)[source]¶
Rewrite every
hcor_*/vcor_*kicker so its kick is a MAD-X variable.Returns the new text and
{corrector name: variable}. The variables are declared (at zero) just before the sequence statement; no other line is touched.
MAD-NG update helpers¶
MAD-NG utility functions for LHC model creation and tune matching.
- pymadng_utils.model_creator.madng_utils.update_model_with_madng(accelerator, model_dir, *, tunes=None, drv_tunes=None, deltap=0.0, convert_to_madx=True)[source]¶
Update model using MAD-NG with tune matching and twiss computation.
This is the main workflow function that:
Initializes the MAD-NG model
Matches tunes to target values
Computes and exports twiss tables (elements, natural, AC dipole)
Converts TFS files to MAD-X format
- Parameters:
accelerator (Accelerator) – Accelerator configuration object
model_dir (Path) – Directory where model files are located and will be updated
tunes (list[float] | None) – Target fractional tunes [Q1, Q2] to match in the model (default: the fractional tunes in the existing
twiss.datheader)drv_tunes (list[float] | None) – Optional target driven tunes [Q1, Q2] for tracking mode
deltap (float) – Relative momentum deviation used for tune matching and exported twiss tables
convert_to_madx (bool) – Whether to convert TFS files to MAD-X format after export
- Return type:
None
Sequence generation helpers¶
Sequence generation now lives in pymadng_utils.madx.make_sequence and is documented in the MAD-X section.