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, fix_acc, beam_calculator, wtf, design_space_factory, fault_idx, comp_idx=None, info_other_sol=None, objective_factory_class=None, **kwargs)[source]

Bases: list[Fault]

A class to hold all fault related data.

Parameters:
__init__(ref_acc, fix_acc, beam_calculator, wtf, design_space_factory, fault_idx, comp_idx=None, info_other_sol=None, objective_factory_class=None, **kwargs)[source]

Create the FaultScenario and the Fault objects.

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.

  • wtf (dict[str, Any]) – What To Fit dictionary. Holds information on the fixing method.

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

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

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

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

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

Return type:

None

skip_optimization

If optimization should be skipped for these Faults.

_create_faults(*cavities)[source]

Create the Fault objects.

Parameters:

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

Return type:

list[Fault]

property _reference_simulation_output: SimulationOutput

Determine wich SimulationOutput is the reference.

fix_all()[source]

Fix all the Fault objects in self.

Return type:

None

_wrap_fix(fault, simulation_output)[source]

Fix the fault and recompute propagation with new settings.

Orchestrates:
Parameters:
  • fault (Fault) – The fault to fix.

  • simulation_output (SimulationOutput) – The most recent simulation, that includes the compensation settings of all Fault upstream of fault.

Return type:

SimulationOutput

Returns:

Most recent simulation, that includes the compensation settings of upstream Fault as well as of this one.

_prepare_fix_objects(fault, simulation_output)[source]

Create objects to instantiate the OptimisationAlgorithm.

In particular: - build DesignSpace - build ObjectiveFactory - extract ListOfElements corresponding to zone to recompute

  • this subset_elts is kept as a Fault attribute.

Parameters:
  • fault (Fault) – The fault to fix.

  • simulation_output (SimulationOutput) – The most recent simulation, that includes the compensation settings of all Fault upstream of fault.

Return type:

OptimisationAlgorithm

Returns:

Object that will fix the failure.

_evaluate_fit_quality(save=True, id_solver_ref=None, id_solver_fix=None)[source]

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

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

  • id_solver_ref (str | None, default: None) – 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, default: None) – 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.

Return type:

None

_set_evaluation_elements(additional_elt=None)[source]

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

Parameters:

additional_elt (list[Element] | None, default: None)

Return type:

list[Element]

_simulations_that_should_be_compared(id_solver_ref, id_solver_fix)[source]

Get proper SimulationOutput for comparison.

Parameters:
Return type:

tuple[SimulationOutput, SimulationOutput]

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

classmethod from_pickle(pickler, path)[source]

Instantiate object from previously pickled file.

Parameters:
Return type:

Self

_mark_cavities_to_rephase()[source]

Change the status of cavities after first failure.

Only cavities with a reference phase different from "phi_0_abs" are altered.

Todo

Could probably be simpler.

Return type:

None

_ensure_phi0_is_preserved()[source]

Transfer the reference 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 BeamCalculator.reference_phase_policy is set to "phi_0_abs".

If the linac was already fixed, we simply skip this step.

Return type:

None

property _reference_phase_policy: Literal['phi_0_abs', 'phi_0_rel', 'phi_s', 'as_in_original_dat']

Give reference phase policy of BeamCalculator.

_resolve_reference_phase(reference_cavity)[source]

Get the reference phase matching the reference phase policy.

According to the value of BeamCalculator.reference_phase_policy:

  • "phi_0_abs", "phi_0_rel", "phi_s": take this reference.

  • `"as_in_original_dat": take reference from reference_cavity.

Parameters:

reference_cavity (FieldMap)

Return type:

Literal['phi_0_abs', 'phi_0_rel', 'phi_s']

class FaultScenarioFactory(accelerators, beam_calc, design_space, objective_factory_class=None)[source]

Bases: object

This objects consistently create FaultScenario.

Parameters:
__init__(accelerators, beam_calc, design_space, objective_factory_class=None)[source]

Init solver parameters for each non-unpickled Accelerator.

Parameters:
  • accelerators (dict[int, list[Accelerator]]) – Dictionary where keys are FaultScenario indexes, and values are lists of corresponding Accelerator. First index corresponds to reference accelerator (no failure).

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

  • design_space_kw – The design space table from the TOML configuration file.

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

  • design_space (DesignSpaceKw)

Returns:

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

Return type:

None

create(failed, compensating_manual=None, **wtf)[source]
Overloads:
  • self, failed (NESTED_ELEMENTS_ID), compensating_manual (None), wtflist[FaultScenario]

  • self, failed (list[NESTED_ELEMENTS_ID]), compensating_manual (list[NESTED_ELEMENTS_ID]), wtflist[FaultScenario]

Parameters:
Return type:

list[FaultScenario]

Instantiate fault scenarios.

Parameters:
Return type:

list[FaultScenario]

fault_scenario_factory(accelerators, beam_calc, wtf, design_space, objective_factory_class=None, **kwargs)[source]

Create the FaultScenario objects (factory template).

Deprecated since version 0.14.1: Prefer the more flexible:

factory = FaultScenarioFactory(
    accelerators=accelerators,
    beam_calc=beam_calc,
    design_space=design_space,
    objective_factory_class=objective_factory_class,
)
fault_scenarios = factory.create(**wtf)
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 – The design space table from the TOML configuration file.

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

  • design_space (dict[str, Any])

Return type:

list[FaultScenario]

Returns:

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

_force_element_to_index_method_creation(accelerator, beam_calculator)[source]

Run a first simulation to link Element with their index.

Note

To initalize a Fault, you need a sub|LOE|. 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.

Parameters:
Return type:

None