fault_scenario module

Define a list-based class holding all the Fault to fix.

We also define fault_scenario_factory(), a factory function creating all the required FaultScenario objects.

class FaultScenario(ref_acc: Accelerator, fix_acc: Accelerator, beam_calculator: BeamCalculator, wtf: dict[str, Any], design_space_factory: DesignSpaceFactory, fault_idx: list[int] | list[list[int]], comp_idx: list[list[int]] | None = None, info_other_sol: list[dict] | None = None, objective_factory_class: type[ObjectiveFactory] | None = None, **kwargs)

Bases: list

A class to hold all fault related data.

__init__(ref_acc: Accelerator, fix_acc: Accelerator, beam_calculator: BeamCalculator, wtf: dict[str, Any], design_space_factory: DesignSpaceFactory, fault_idx: list[int] | list[list[int]], comp_idx: list[list[int]] | None = None, info_other_sol: list[dict] | None = None, objective_factory_class: type[ObjectiveFactory] | None = None, **kwargs) None

Create the FaultScenario and the Fault objects.

Todo

Could be cleaner.

Parameters:
  • ref_acc (Accelerator) – The reference linac (nominal or baseline).

  • fix_acc (Accelerator) – The broken linac to be fixed.

  • beam_calculator (BeamCalculator) – The solver that will be called during the optimisation process.

  • initial_beam_parameters_factory (InitialBeamParametersFactory) – An object to create beam parameters at the entrance of the linac portion.

  • wtf (dict[str, str | int | bool | list[str] | list[float]]) – What To Fit dictionary. Holds information on the fixing method.

  • design_space_factory (DesignSpaceFactory) – An object to easily create the proper DesignSpace.

  • fault_idx (list[int | list[int]]) – List containing the position of the errors. If strategy is manual, it is a list of lists (faults already gathered).

  • comp_idx (list[list[int]], optional) – List containing the position of the compensating cavities. If strategy is manual, it must be provided. The default is None.

  • info_other_sol (list[dict], optional) – Contains information on another fit, for comparison purposes. The default is None.

  • objective_factory_class (type[ObjectiveFactory] | None, optional) – If provided, will override the objective_preset. Used to let user define it’s own ObjectiveFactory without altering the source code.

_set_faults(reference_simulation_output: SimulationOutput, list_of_elements_factory: ListOfElementsFactory, design_space_factory: DesignSpaceFactory, *cavities: Sequence[Sequence[FieldMap]], objective_factory_class: type[ObjectiveFactory] | None = None) list[Fault]

Create the Fault objects.

Parameters:
  • reference_simulation_output (SimulationOutput) – The simulation of the nominal linac we’ll try to match.

  • list_of_elements_factory (ListOfElementsFactory) – An object that can create ListOfElements.

  • design_space_factory (DesignSpaceFactory) – An object that can create DesignSpace.

  • *cavities (Sequence[Sequence[FieldMap]]) – First if the list of gathered failed cavities. Second is the list of corresponding compensating cavities.

  • objective_factory_class (type[ObjectiveFactory] | None, optional) – If provided, will override the objective_preset. Used to let user define it’s own ObjectiveFactory without altering the source code.

Return type:

list[Fault]

_get_reference() SimulationOutput

Determine wich SimulationOutput is the reference.

_set_cavities_to_rephase() None

Change the status of cavities after first failure.

_set_optimisation_algorithms() list[OptimisationAlgorithm]

Set each fault’s optimisation algorithm.

Returns:

optimisation_algorithms – The optimisation algorithm for each fault in self.

Return type:

list[OptimisationAlgorithm]

_break_cavities() None

Break the cavities.

fix_all() None

Fix all the Fault objects in self.

Todo

make this more readable

_wrap_single_fix(fault: Fault, optimisation_algorithm: OptimisationAlgorithm, ref_simulation_output: SimulationOutput) OptiSol

Fix a fault and recompute propagation with new settings.

_update_rephased_cavities_status(fault: Fault) None

Modify the status of the cavities that were already rephased.

Change the cavities with status “rephased (in progress)” to “rephased (ok)” between fault and the next one.

_transfer_phi0_from_ref_to_broken() None

Transfer the entry phases from reference linac to broken.

If the absolute initial phases are not kept between reference and broken linac, it comes down to rephasing the linac. This is what we want to avoid when beam_calculator.flag_phi_abs == True.

property _reference_phase: 'phi_0_abs', 'phi_0_rel', 'phi_s'

Give the reference phase "phi_0_rel" or "phi_0_abs".

_evaluate_fit_quality(save: bool = True, id_solver_ref: str | None = None, id_solver_fix: str | None = None) None

Compute some quantities on the whole linac to see if fit is good.

Parameters:
  • save (bool, optional) – To tell if you want to save the evaluation. The default is True.

  • id_solver_ref (str | None, optional) – Id of the solver from which you want reference results. The default is None. In this case, the first solver is taken (beam_calc_param).

  • id_solver_fix (str | None, optional) – Id of the solver from which you want fixed results. The default is None. In this case, the solver is the same as for reference.

_set_evaluation_elements(additional_elt: list[Element] | None = None) dict[str, Element]

Set a the proper list of where to check the fit quality.

_simulations_that_should_be_compared(id_solver_ref: str | None, id_solver_fix: str | None) tuple[SimulationOutput, SimulationOutput]

Get proper SimulationOutput for comparison.

pickle(pickler: MyPickler, path: Path | str | None = None) Path

Pickle (save) the object.

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

classmethod from_pickle(pickler: MyPickler, path: Path | str) Self

Instantiate object from previously pickled file.

fault_scenario_factory(accelerators: list[Accelerator], beam_calc: BeamCalculator, wtf: dict[str, Any], design_space: dict[str, Any], objective_factory_class: type[ObjectiveFactory] | None = None, **kwargs) list[FaultScenario]

Create the FaultScenario objects (factory template).

Parameters:
  • accelerators (list[Accelerator]) – Holds all the linacs. The first one must be the reference linac, while all the others will be to be fixed.

  • beam_calc (BeamCalculator) – The solver that will be called during the optimisation process.

  • wtf (dict[str, Any]) – The WhatToFit table of the TOML configuration file.

  • design_space_kw (dict[str, Any]) – The design space table from the TOML configuration file.

  • objective_factory_class (type[ObjectiveFactory] | None, optional) – If provided, will override the objective_preset. Used to let user define it’s own ObjectiveFactory without altering the source code.

Returns:

fault_scenarios – Holds all the initialized FaultScenario objects, holding their already initialied Fault objects.

Return type:

list[FaultScenario]

_force_element_to_index_method_creation(accelerator: Accelerator, beam_calculator: BeamCalculator) None

Run a first simulation to link Element with their index.

Note

To initalize a Fault, you need a sub:class:.ListOfElements. To create the latter, you need a _element_to_index method. It can only be created if you know the number of steps in every Element. So, for TraceWin, we run a first simulation.