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:
objectThe 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;
Nonetracks 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:
objectWhat one tracking worker tracks.
turn_offsetis the start turn minus the file’s first turn (AC-kicker phase) andfile_indexthe 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 ∂²mof one batch, withwrthe weighted residuals sent in.knob_knobis(K, K)over the engine’s knobs (ptlast when fitted), summed over the particles;knob_startis(P, K, 4)andstart_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:
objectOne tracked batch.
obs[o]is(P, N);d_dk[o]is(P, K, N)withKthe knobs (thenptwhen the energy is fitted);d_dc0[o]is(P, 4, N), the derivatives by the start x, px, y, py. Validation engines fillobsonly.- 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:
objectClosed orbit (and optics) at the BPMs in model order.
valuesis(n_observables, n_bpms)andjacobian(n_observables, n_bpms, n_knobs)(Nonefrom ajacobian=Falsesolve), 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:
ProtocolTracks batches of particles with knob (and start-coordinate) derivatives.
knob_namesare the fitted knobs,ptlast when the energy is fitted.- Parameters:
config (WorkerConfig)
plan (TrackRequest)
init_knobs (Mapping[str, float])
worker_id (int)
logfile (Path | None)
- knob_names: list[str]¶
- nbpms: int¶
- set_knobs(knobs)[source]¶
Set the fitted knobs (and
ptwhen the energy is fitted) toknobs.- Return type:
None- Parameters:
knobs (Mapping[str, float])
- load_particles(init_coords, init_pts)[source]¶
Set the start coordinates and
ptoffsets, 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
ptof every particle in batchesfirst..last(0-based) toptplus 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, pyof every particle, in batch order;ptis 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_derivativesalso fillsd_dc0.- Return type:
- Parameters:
batch (int)
start_derivatives (bool)
- begin_second_order()[source]¶
Switch to second-order derivatives, keeping the knobs, starts and
pt;trackstill works.- Return type:
None
- class accelmo.backend.protocol.ClosedOrbitEngine(config, init_knobs, *, optics_names, orbit_coords, worker_id, logfile=None, extra_knobs=None)[source]¶
Bases:
ProtocolSolves 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:
ptis 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
ptof 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]¶
Nonewhen the closed orbit is lost.jacobian=False(loss-only evaluation, orbit observables only) leavesClosedSolution.jacobianNoneand may skip the derivatives entirely.- Return type:
ClosedSolution|None- Parameters:
observables (Sequence[str])
jacobian (bool)
- class accelmo.backend.protocol.Model(*args, **kwargs)[source]¶
Bases:
ProtocolParent-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
markerthe first element of the sequence.- Return type:
None- Parameters:
marker (str)
- class accelmo.backend.protocol.Backend(*args, **kwargs)[source]¶
Bases:
ProtocolThe 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 overmagnet_rangeand return the model.- Return type:
- 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
columnsat the BPMs for these strengths,ptand state.- Return type:
DataFrame- Parameters:
accelerator (Accelerator)
magnet_strengths (Mapping[str, float] | None)
pt (float)
machine_state (MachineState | None)
columns (Sequence[str])