Model Creator

pymadng_utils.model_creator builds higher-level workflows on top of the lower-level MAD-NG and MAD-X utilities.

Typical workflow

  1. Create or fetch a base model directory with OMC3.

  2. Generate a saved MAD-X sequence with pymadng_utils.madx.make_sequence.make_madx_sequence.

  3. Re-open that sequence in MAD-NG and export TWISS tables with update_model_with_madng.

Notes

  • create_lhc_model and create_psb_model are the packaged end-to-end workflows.

  • create_fcc_model only rewrites the FCC-ee MAD-X text; it needs neither MAD-X nor OMC3.

  • create_models imports omc3 and needs the model extra.

  • 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:

  1. Creates model instance using omc3

  2. Exports the MAD-X sequence lhcb<beam>_saved.seq (for beam 2 this is the forward beam-4 tracking sequence, so seq_dir=1 is used for the MAD-NG LHC descriptor)

  3. 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 fetch method; path is 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]; None skips the AC dipole.

  • energy (float) – Total beam energy in GeV (omc3 and MAD-X beam, energy= convention); the MAD-NG LHC descriptor gets energy - PROTON_MASS_GEV as 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:

  1. Creates model instance using omc3

  2. Generates MAD-X sequence files

  3. Updates model with MAD-NG (tune matching and twiss computation)

update_with_madng=False stops 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 with convert_to_madx=False for 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 fetch method; path is 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]. None skips 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.

Parameters:

name (str)

Return type:

str

pymadng_utils.model_creator.create_fcc_model.bpms_to_monitors(madx_text)[source]

Declare the bpm_* markers as monitor (what the MAD-NG fitter expects of a BPM).

Parameters:

madx_text (str)

Return type:

str

pymadng_utils.model_creator.create_fcc_model.slice_sextupoles(madx_text, nslice=3)[source]

Give every sextupole nslice MAD-NG slices (a single slice is too coarse for the FCC-ee sextupoles).

Parameters:
  • madx_text (str)

  • nslice (int)

Return type:

str

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.

Parameters:

madx_text (str)

Return type:

tuple[str, dict[str, str]]

pymadng_utils.model_creator.create_fcc_model.create_fcc_model(lattice_dir, output_file, *, madx_name='fccee_z.madx')[source]

Write output_file: lattice_dir/madx_name with deferred corrector kicks, monitor BPMs and 3-slice sextupoles.

Returns {corrector name: kick variable}.

Parameters:
Return type:

dict[str, str]

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:

  1. Initializes the MAD-NG model

  2. Matches tunes to target values

  3. Computes and exports twiss tables (elements, natural, AC dipole)

  4. 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.dat header)

  • 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.