accelmo.backend.protocol

What the workers and the parent need from a model backend.

A backend module (accelmo.backend.<name>) provides TrackingEngine and ClosedOrbitEngine implementations plus the parent-side open_model() and closed_orbit_table(); accelmo.backend.get_backend() picks it from Accelerator.backend. Only automatic differentiation computes derivatives. MAD-NG is the production backend, xtrack the reference for other codes; the contract and the acceptance tests are in docs/backends.rst.

class accelmo.backend.protocol.TrackGeometry(start_bpm, end_bpm, initial_condition_marker=None, sdir=1, cycle_marker=None, anchor_mode=None, anchor_sources=(), n_turns=1)[source]

Bases: object

The model segment one tracking worker observes and tracks, fixed when its engine is built.

Parameters:
  • start_bpm (str)

  • end_bpm (str)

  • initial_condition_marker (str | None)

  • sdir (int)

  • cycle_marker (str | None)

  • anchor_mode (str | None)

  • anchor_sources (tuple[str, ...])

  • n_turns (int)

start_bpm: str
end_bpm: str
initial_condition_marker: str | None = None

Marker supplying the start coordinates (kicker, AC dipole); the full ring is tracked.

sdir: int = 1
cycle_marker: str | None = None
anchor_mode: str | None = None
anchor_sources: tuple[str, ...] = ()
n_turns: int = 1

Turns tracked from each start.

property init_point: str

the marker, else the first BPM in tracking order.

Type:

Where the start coordinates are read

property bpm_range: str

Observed BPM range first/last.

property tracking_range: str | None

Tracked range in tracking order; None tracks the full cycled sequence.

A marker start tracks the whole ring every turn, since a named range would treat elements outside it as drifts.

property cycle_target: str

Element the sequence is cycled to before tracking.

The initial-condition marker or where the measured turn increment starts: the tracking end for a backward range, where the start coordinates are taken. Validation workers cycle identically or they track a different path.

class accelmo.backend.protocol.TrackRequest(geometry, observables, batch_size, num_batches, validation, file_index, turn_offset)[source]

Bases: object

What one tracking worker tracks.

turn_offset is the start turn minus the file’s first turn (AC-kicker phase) and file_index the measurement file of every particle.

Parameters:
  • geometry (TrackGeometry)

  • observables (tuple[str, ...])

  • batch_size (int)

  • num_batches (int)

  • validation (bool)

  • file_index (int)

  • turn_offset (int)

geometry: TrackGeometry
observables: tuple[str, ...]
batch_size: int
num_batches: int
validation: bool
file_index: int
turn_offset: int
class accelmo.backend.protocol.SecondOrderBlocks(knob_knob, knob_start, start_start)[source]

Bases: object

Σ_points wr ∂²m of one batch, with wr the weighted residuals sent in.

knob_knob is (K, K) over the engine’s knobs (pt last when fitted), summed over the particles; knob_start is (P, K, 4) and start_start (P, 4, 4) per particle.

Parameters:
  • knob_knob (np.ndarray)

  • knob_start (np.ndarray)

  • start_start (np.ndarray)

knob_knob: ndarray
knob_start: ndarray
start_start: ndarray
class accelmo.backend.protocol.TrackResult(obs, d_dk=None, d_dc0=None, n_lost=0, n_total=0)[source]

Bases: object

One tracked batch.

obs[o] is (P, N); d_dk[o] is (P, K, N) with K the knobs (then pt when the energy is fitted); d_dc0[o] is (P, 4, N), the derivatives by the start x, px, y, py. Validation engines fill obs only.

Parameters:
  • obs (dict[str, np.ndarray])

  • d_dk (dict[str, np.ndarray] | None)

  • d_dc0 (dict[str, np.ndarray] | None)

  • n_lost (int)

  • n_total (int)

obs: dict[str, ndarray]
d_dk: dict[str, ndarray] | None = None
d_dc0: dict[str, ndarray] | None = None
n_lost: int = 0
n_total: int = 0
class accelmo.backend.protocol.ClosedSolution(names, values, jacobian)[source]

Bases: object

Closed orbit (and optics) at the BPMs in model order.

values is (n_observables, n_bpms) and jacobian (n_observables, n_bpms, n_knobs) (None from a jacobian=False solve), rows in the requested observable order.

Parameters:
  • names (list[str])

  • values (np.ndarray)

  • jacobian (np.ndarray | None)

names: list[str]
values: ndarray
jacobian: ndarray | None
class accelmo.backend.protocol.TrackingEngine(config, plan, init_knobs, *, worker_id, logfile=None)[source]

Bases: Protocol

Tracks batches of particles with knob (and start-coordinate) derivatives.

knob_names are the fitted knobs, pt last when the energy is fitted.

Parameters:
knob_names: list[str]
nbpms: int
set_knobs(knobs)[source]

Set the fitted knobs (and pt when the energy is fitted) to knobs.

Return type:

None

Parameters:

knobs (Mapping[str, float])

load_particles(init_coords, init_pts)[source]

Set the start coordinates and pt offsets, both indexed [batch][particle].

Return type:

None

Parameters:
  • init_coords (list[list[list[float]]])

  • init_pts (list[list[float]])

set_pts(pt, first_batch, last_batch)[source]

Set pt of every particle in batches first..last (0-based) to pt plus its own offset.

Return type:

None

Parameters:
  • pt (float)

  • first_batch (int)

  • last_batch (int)

set_start_coords(x, px, y, py)[source]

Replace the start x, px, y, py of every particle, in batch order; pt is kept.

Return type:

None

Parameters:
  • x (np.ndarray)

  • px (np.ndarray)

  • y (np.ndarray)

  • py (np.ndarray)

preflight()[source]

One-particle dry run: {"observed", "expected", "lost"}.

Return type:

dict[str, int | bool]

track(batch, *, start_derivatives)[source]

Track batch batch (0-based); start_derivatives also fills d_dc0.

Return type:

TrackResult

Parameters:
  • batch (int)

  • start_derivatives (bool)

begin_second_order()[source]

Switch to second-order derivatives, keeping the knobs, starts and pt; track still works.

Return type:

None

second_order(batch, weighted_residuals)[source]

Contract weighted_residuals[o] (P, N) with the second derivatives of o (after begin_second_order).

Return type:

SecondOrderBlocks

Parameters:
  • batch (int)

  • weighted_residuals (dict[str, np.ndarray])

close()[source]

Release the model.

Return type:

None

class accelmo.backend.protocol.ClosedOrbitEngine(config, init_knobs, *, optics_names, orbit_coords, worker_id, logfile=None, extra_knobs=None)[source]

Bases: Protocol

Solves the closed orbit (and twiss) with knob derivatives.

Parameters:
  • config (WorkerConfig)

  • init_knobs (Mapping[str, float])

  • optics_names (Sequence[str])

  • orbit_coords (Sequence[str])

  • worker_id (int)

  • logfile (Path | None)

  • extra_knobs (Sequence[str] | None)

knob_names: list[str]
fit_pt: bool

its derivative is the Jacobian column after the knobs.

Type:

pt is fitted too

set_knobs(knobs)[source]

Set the fitted knobs to knobs.

Return type:

None

Parameters:

knobs (Mapping[str, float])

get_vars(names)[source]

Current values of the global variables names (machine state, e.g. corrector kicks).

Return type:

dict[str, float]

Parameters:

names (Sequence[str])

set_vars(values)[source]

Set global variables or element attributes (a machine state).

Return type:

None

Parameters:

values (Mapping[str, float])

set_pt(pt)[source]

Set the momentum offset pt of the closed-orbit search.

Return type:

None

Parameters:

pt (float)

set_warm_start(key)[source]

Start the next closed-orbit search from the last orbit solved under key.

Return type:

None

Parameters:

key (str)

solve(observables, *, jacobian=True)[source]

None when the closed orbit is lost.

jacobian=False (loss-only evaluation, orbit observables only) leaves ClosedSolution.jacobian None and may skip the derivatives entirely.

Return type:

ClosedSolution | None

Parameters:
  • observables (Sequence[str])

  • jacobian (bool)

close()[source]

Release the model.

Return type:

None

class accelmo.backend.protocol.Model(*args, **kwargs)[source]

Bases: Protocol

Parent-side model: knob discovery and BPM lists (no derivatives).

knob_names: list[str]
all_bpms: list[str]
bpms_in_range: list[str]
cycle_to_start(marker)[source]

Make marker the first element of the sequence.

Return type:

None

Parameters:

marker (str)

recount_bpms()[source]

Refresh all_bpms and bpms_in_range after a cycle.

Return type:

None

apply_initial_model_values(values)[source]

Set knobs and fixed strengths to values before the fit starts.

Return type:

None

Parameters:

values (Mapping[str, float])

knob_values()[source]

Current values of knob_names, in that order.

Return type:

ndarray

close()[source]

Release the model.

Return type:

None

class accelmo.backend.protocol.Backend(*args, **kwargs)[source]

Bases: Protocol

The module accelmo.backend.<name>.

TrackingEngine: type[TrackingEngine]
ClosedOrbitEngine: type[ClosedOrbitEngine]
open_model(accelerator, *, magnet_range, machine_state=None, b2_errors=None, bad_bpms=None, debug=False, mad_logfile=None, anchor_mode=None, anchor_markers=())[source]

Load accelerator, create its knobs over magnet_range and return the model.

Return type:

Model

Parameters:
  • accelerator (Accelerator)

  • magnet_range (str)

  • machine_state (MachineState | None)

  • b2_errors (Path | None)

  • bad_bpms (list[str] | None)

  • debug (bool)

  • mad_logfile (Path | None)

  • anchor_mode (str | None)

  • anchor_markers (Sequence[str])

closed_orbit_table(accelerator, magnet_strengths, pt, machine_state, columns)[source]

Closed-orbit columns at the BPMs for these strengths, pt and state.

Return type:

DataFrame

Parameters:
  • accelerator (Accelerator)

  • magnet_strengths (Mapping[str, float] | None)

  • pt (float)

  • machine_state (MachineState | None)

  • columns (Sequence[str])