accelerator module

Define Accelerator, the highest-level class of LightWin.

It holds, well… an accelerator. This accelerator has a ListOfElements. For each BeamCalculator defined, it has a SimulationOutput stored in Accelerator.simulation_outputs. Additionally, it has a ParticleInitialState, which describes energy, phase, etc of the beam at the entry of its ListOfElements.

class Accelerator(name, status, dat_file, accelerator_path, list_of_elements_factory, e_mev, sigma, pickle_path=None, index=0, **kwargs)[source]

Bases: object

Class holding a ListOfElements.

Parameters:
__init__(name, status, dat_file, accelerator_path, list_of_elements_factory, e_mev, sigma, pickle_path=None, index=0, **kwargs)[source]

Create object.

Parameters:
  • name (str) – Name of the accelerator, used in plots.

  • status (Literal['reference', 'broken', 'fix']) – Current status design.

  • dat_file (Path) – Absolute path to the linac DAT file.

  • accelerator_path (Path) – Absolute path where results for each BeamCalculator will be stored.

  • list_of_elements_factory (ListOfElementsFactory) – A factory to create the list of elements.

  • e_mev (float) – Initial beam energy in \(\mathrm{MeV}\).

  • sigma (ndarray[tuple[Any, ...], dtype[double]]) – Initial beam \(\sigma\) matrix in \(\mathrm{m}\) and \(\mathrm{rad}\).

  • pickle_path (Path | None, default: None) – Where to pickle object. Used in keep().

  • index (int, default: 0) – Corresponding FaultScenario index. A null index is reserved for reference accelerator.

Return type:

None

name

Name for the object. The default will be "Reference" or "Solution". If object is unpickled, the name will be taken from the TOML configuration dictionary.

index

Corresponding FaultScenario index. A null index is reserved for reference accelerator.

simulation_outputs: dict[str, SimulationOutput]

Every SimulationOutput instance, associated with the name of the BeamCalculator that created it. This dictionary is filled by keep().

accelerator_path

Absolute path where results for each BeamCalculator will be stored. Typically, this is a 000001/-like folder.

elts: ListOfElements

The list of elements contained in the accelerator.

_is_unpickled: bool

Internal variable telling if this instance was create by unpickling a PKL file.

_pickle_path: Path | None

If set, keep() will also pickle current instance

__str__()[source]

Return str(self).

Return type:

str

property id: str

Produce a unique identifier for current object.

It is the concatenation of index and name:

000000_Reference        # Default for reference
000001_Solution         # Default for broken/fixed of first failure
000001_AnotherSolution  # Can be created by unpickling
property l_cav

Shortcut to easily get list of cavities.

property is_unpickled: bool

Tell if current object was created by unpickling a PKL file.

If this flag is True, the main consequence is that the BeamCalculator instances will not re-create new SimulationOutput, and instead return the ones that should already be in simulation_outputs.

has(key)[source]

Tell if the required attribute is in this class.

Parameters:

key (str)

Return type:

bool

get(*keys, to_numpy=True, none_to_nan=False, elt=None, pos=None, **kwargs)[source]

Get attributes from this instance or its attributes.

Note

Simulation-related quantities (e.g., beam parameters, transfer matrices) are stored in the simulation_outputs dictionary, where each key is the name of a BeamCalculator solver (e.g., "CyEnvelope1D_0", "TraceWin_1"), and each value is a corresponding SimulationOutput object.

If simulations have been performed using multiple solvers, Accelerator.get() becomes ambiguous and should be avoided for solver-dependent data. In that case, prefer calling accelerator.simulation_outputs[solver_name].get(...) directly.

Parameters:
  • *keys (Literal['accelerator_path', 'elts', 'name', 'simulation_outputs'] | Literal['accelerator_path', 'dat_file', 'dat_filecontent', 'elts_n_cmds', 'files', 'input_beam', 'input_particle', 'tm_cumul_in'] | Literal['aperture_flag', 'field_map_filename', 'field_map_folder', 'geometry'] | Literal['dat_idx', 'elt_idx', 'idx', 'idx_in_lattice', 'lattice', 'length_m', 'name', 'nature', 'section'] | Literal['abs_mesh', 'd_z', 'n_steps', 'rel_mesh', 's_in', 's_out', 'transf_mat_function'] | Literal['acceptance_energy', 'acceptance_phi', 'field', 'freq_cavity_mhz', 'k_e', 'omega_0_rf', 'phi_ref', 'phi_rf', 'phi_s', 'reference', 'rf_field', 'status', 'v_cav_mv'] | Literal['phi_0_abs', 'phi_0_rel', 'phi_s'] | Literal['beta', 'gamma', 'phi_abs', 'synchronous', 'w_kin', 'z_in'] | Literal['e_mev', 'e_rest_mev', 'f_bunch_mhz', 'i_milli_a', 'q_adim', 'sigma', 'inv_e_rest_mev', 'gamma_init', 'omega_0_bunch', 'lambda_bunch', 'q_over_m', 'm_over_q'] | Literal['alpha_phiw', 'beta_phiw', 'envelope_energy_phiw', 'envelope_pos_phiw', 'eps_phiw', 'eps_no_normalization_phiw', 'eps_normalized_phiw', 'gamma_phiw', 'sigma_phiw', 'twiss_phiw', 'alpha_phiw99', 'beta_phiw99', 'envelope_energy_phiw99', 'envelope_pos_phiw99', 'eps_phiw99', 'eps_no_normalization_phiw99', 'eps_normalized_phiw99', 'gamma_phiw99', 'sigma_phiw99', 'twiss_phiw99', 'alpha_t', 'beta_t', 'envelope_energy_t', 'envelope_pos_t', 'eps_t', 'eps_no_normalization_t', 'eps_normalized_t', 'gamma_t', 'sigma_t', 'twiss_t', 'alpha_x', 'beta_x', 'envelope_energy_x', 'envelope_pos_x', 'eps_x', 'eps_no_normalization_x', 'eps_normalized_x', 'gamma_x', 'sigma_x', 'twiss_x', 'alpha_x99', 'beta_x99', 'envelope_energy_x99', 'envelope_pos_x99', 'eps_x99', 'eps_no_normalization_x99', 'eps_normalized_x99', 'gamma_x99', 'sigma_x99', 'twiss_x99', 'alpha_y', 'beta_y', 'envelope_energy_y', 'envelope_pos_y', 'eps_y', 'eps_no_normalization_y', 'eps_normalized_y', 'gamma_y', 'sigma_y', 'twiss_y', 'alpha_y99', 'beta_y99', 'envelope_energy_y99', 'envelope_pos_y99', 'eps_y99', 'eps_no_normalization_y99', 'eps_normalized_y99', 'gamma_y99', 'sigma_y99', 'twiss_y99', 'alpha_z', 'beta_z', 'envelope_energy_z', 'envelope_pos_z', 'eps_z', 'eps_no_normalization_z', 'eps_normalized_z', 'gamma_z', 'sigma_z', 'twiss_z', 'alpha_zdelta', 'beta_zdelta', 'envelope_energy_zdelta', 'envelope_pos_zdelta', 'eps_zdelta', 'eps_no_normalization_zdelta', 'eps_normalized_zdelta', 'gamma_zdelta', 'sigma_zdelta', 'twiss_zdelta'] | Literal['alpha', 'beta', 'beta_kin', 'envelope_energy', 'envelope_pos', 'eps', 'eps_no_normalization', 'eps_normalized', 'gamma', 'gamma_kin', 'sigma', 'twiss', 'z_abs'] | Literal['phiw', 'phiw99', 't', 'x', 'x99', 'y', 'y99', 'z', 'zdelta']) – Names of the desired attributes.

  • to_numpy (bool, default: True) – Convert list outputs to NumPy arrays.

  • none_to_nan (bool, default: False) – Replace None values with np.nan.

  • elt (str | Element | None, default: None) – Target element name or instance, passed to recursive_getter.

  • pos (Literal['in', 'out'] | None, default: None) – Position key for slicing data arrays.

  • **kwargs (Any) – Additional arguments for recursive_getter.

Returns:

A single value or tuple of values.

Return type:

Any

is_computed(solver_id=None)[source]

Determine if propagation was calculated.

Parameters:

solver_id (str | None, default: None) – If given, we check if this key was set in simulation_outputs.

Return type:

bool

Returns:

  • True if propagation was calculated with solver_id (or with any

  • solver if solver_id was not given).

_create_special_getters()[source]

Create a dict of aliases that can be accessed w/ the get method.

Return type:

dict[str, Callable]

keep(simulation_output, exported_phase, beam_calculator_id, skip_pickle=False)[source]

Save simulation and settings.

In particular:
Parameters:
  • simulation_output (SimulationOutput) – The instance to keep.

  • exported_phase (Literal['phi_0_abs', 'phi_0_rel', 'phi_s'] | Literal['as_in_original_dat'] | Literal['as_in_settings']) – The reference phase in the output DAT file.

  • beam_calculator_id (str) – Unique ID for the BeamCalculator that created simulation_output. Will be the key to access simulation_output in simulation_outputs.

  • skip_pickle (bool, default: False) – Use this during failure compensation to avoid pickling after every Fault compensation.

  • simulation_output – The instance to keep.

  • exported_phase – The reference phase in the output DAT file.

  • beam_calculator_id – Unique ID for the BeamCalculator that created simulation_output. Will be the key to access simulation_output in simulation_outputs.

  • skip_pickle – Use this during failure compensation to avoid pickling after every Fault compensation.

Return type:

None

elt_at_this_s_idx(s_idx, show_info=False)[source]

Give the element where the given index is.

Parameters:
  • s_idx (int)

  • show_info (bool, default: False)

Return type:

Element | None

equivalent_elt(elt)[source]

Return element from self.elts with the same name as elt.

Parameters:

elt (Element | str)

Return type:

Element

pickle(pickler, path=None)[source]

Pickle (save) the object.

This is useful for debug and temporary saves; do not use it for long time saving.

Parameters:
Return type:

Path | None

plot(key, to_deg=True, grid=True, x=None, legend_entry=None, ax=None, **kwargs)[source]

Plot key for every stored SimulationOutput.

This method does not use the default plotting module, but pandas dataframe plotting method.

Parameters:
  • key (Literal['acceptance_energy', 'acceptance_phi', 'beam_parameters', 'element_to_index', 'elt_idx', 'mismatch_factor_zdelta', 'phi_s', 'set_of_cavity_settings', 'synch_trajectory', 'v_cav_mv', 'z_abs'] | Literal['alpha_phiw', 'beta_phiw', 'envelope_energy_phiw', 'envelope_pos_phiw', 'eps_phiw', 'eps_no_normalization_phiw', 'eps_normalized_phiw', 'gamma_phiw', 'sigma_phiw', 'twiss_phiw', 'alpha_phiw99', 'beta_phiw99', 'envelope_energy_phiw99', 'envelope_pos_phiw99', 'eps_phiw99', 'eps_no_normalization_phiw99', 'eps_normalized_phiw99', 'gamma_phiw99', 'sigma_phiw99', 'twiss_phiw99', 'alpha_t', 'beta_t', 'envelope_energy_t', 'envelope_pos_t', 'eps_t', 'eps_no_normalization_t', 'eps_normalized_t', 'gamma_t', 'sigma_t', 'twiss_t', 'alpha_x', 'beta_x', 'envelope_energy_x', 'envelope_pos_x', 'eps_x', 'eps_no_normalization_x', 'eps_normalized_x', 'gamma_x', 'sigma_x', 'twiss_x', 'alpha_x99', 'beta_x99', 'envelope_energy_x99', 'envelope_pos_x99', 'eps_x99', 'eps_no_normalization_x99', 'eps_normalized_x99', 'gamma_x99', 'sigma_x99', 'twiss_x99', 'alpha_y', 'beta_y', 'envelope_energy_y', 'envelope_pos_y', 'eps_y', 'eps_no_normalization_y', 'eps_normalized_y', 'gamma_y', 'sigma_y', 'twiss_y', 'alpha_y99', 'beta_y99', 'envelope_energy_y99', 'envelope_pos_y99', 'eps_y99', 'eps_no_normalization_y99', 'eps_normalized_y99', 'gamma_y99', 'sigma_y99', 'twiss_y99', 'alpha_z', 'beta_z', 'envelope_energy_z', 'envelope_pos_z', 'eps_z', 'eps_no_normalization_z', 'eps_normalized_z', 'gamma_z', 'sigma_z', 'twiss_z', 'alpha_zdelta', 'beta_zdelta', 'envelope_energy_zdelta', 'envelope_pos_zdelta', 'eps_zdelta', 'eps_no_normalization_zdelta', 'eps_normalized_zdelta', 'gamma_zdelta', 'sigma_zdelta', 'twiss_zdelta'] | Literal['alpha', 'beta', 'beta_kin', 'envelope_energy', 'envelope_pos', 'eps', 'eps_no_normalization', 'eps_normalized', 'gamma', 'gamma_kin', 'sigma', 'twiss', 'z_abs'] | Literal['phiw', 'phiw99', 't', 'x', 'x99', 'y', 'y99', 'z', 'zdelta'] | Literal['beta', 'gamma', 'phi_abs', 'synchronous', 'w_kin', 'z_in'] | Literal['e_mev', 'e_rest_mev', 'f_bunch_mhz', 'i_milli_a', 'q_adim', 'sigma', 'inv_e_rest_mev', 'gamma_init', 'omega_0_bunch', 'lambda_bunch', 'q_over_m', 'm_over_q'] | Literal['cumulated', 'individual', 'n_points', 'r_xx', 'r_yy', 'r_zdelta', 'r_zz', 'r_zdelta_11', 'r_zdelta_12', 'r_zdelta_21', 'r_zdelta_22'] | Literal['eps_t', 'eps_x', 'eps_y', 'mismatch_factor_t', 'mismatch_factor_x', 'mismatch_factor_y'] | Literal['eps_phiw99', 'eps_x99', 'eps_y99', 'pow_lost'])

  • to_deg (bool, default: True)

  • grid (bool, default: True)

  • x (Literal['z_abs', 'elt_idx'] | None, default: None)

  • legend_entry (str | None, default: None)

  • ax (Axes | None, default: None)

Return type:

Axes | None

classmethod from_pickle(pickler, path=None, name=None, accelerator_id=None, index=0)[source]

Instantiate object from previously pickled file.

Note

Also sets the “private” attribute Accelerator._is_unpickled to True.

Todo

The GUI may also ask for the new Accelerator name? I think it would be too much intricated because I have no GUI specific module. Maybe one day…

Parameters:
Return type:

Self