failures.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)

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

_break_cavities() None

Break the cavities.

..todo ::

At this step I do not save the .dat anymore. Was it useful for something?

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

_get_reference() SimulationOutput

Determine wich SimulationOutput is the reference.

property _reference_phase: Literal['phi_0_abs', 'phi_0_rel', 'phi_s']

phi_0_rel or ‘phi_0_abs’.

Type:

Give the reference phase

_set_cavities_to_rephase() None

Change the status of cavities after first failure.

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

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

_set_faults(reference_simulation_output: SimulationOutput, list_of_elements_factory: ListOfElementsFactory, design_space_factory: DesignSpaceFactory, *cavities: Sequence[Sequence[FieldMap]]) 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.

Return type:

list[Fault]

_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]

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

Get proper SimulationOutput for comparison.

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

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

_wrap_single_fix(fault: Fault, optimisation_algorithm: OptimisationAlgorithm, ref_simulation_output: SimulationOutput) tuple[bool, dict]

Fix a fault and recompute propagation with new settings.

fix_all() None

Fix all the Fault objects in self.

Todo

make this more readable

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

Instantiate object from previously pickled file.

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.

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

fault_scenario_factory(accelerators: list[Accelerator], beam_calculator: BeamCalculator, wtf: dict[str, Any], design_space_kw: dict[str, str | bool | float]) 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_calculator (BeamCalculator) – The solver that will be called during the optimisation process.

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

  • design_space_kw (dict[str, str | bool | float]) – The [design_space] entries from the .ini file.

Returns:

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

Return type:

list[FaultScenario]