cavity_settings module
Store cavity settings that can change during an optimisation.
Todo
Similar to synchronous phase, allow for V_cav to be “master” instead of k_e.
See also
- TRANSF_MAT_FUNC_WRAPPER_T
A function that takes in the kinetic energy, the relative entry phase, the cavity settings, other kwargs, and returns a dict containing propagation info in the element.
alias of
Callable[[float,float, CavitySettings,dict[str,Any]],dict[str,Any]]
- class CavityVars(k_e, phi, status, reference)[source]
Bases:
NamedTupleRegroup main cavity settings variables.
- Parameters:
- status: Literal['compensate (in progress)', 'compensate (not ok)', 'compensate (ok)', 'failed', 'nominal', 'rephased (in progress)', 'rephased (ok)']
Alias for field number 2
- _asdict()
Return a new dict which maps field names to their values.
- _field_defaults = {}
- _fields = ('k_e', 'phi', 'status', 'reference')
- classmethod _make(iterable)
Make a new CavityVars object from a sequence or iterable
- _replace(**kwds)
Return a new CavityVars object replacing specified fields with new values
- exception MissingAttributeError[source]
Bases:
RuntimeErrorRaised when a phase cannot be calculated because of missing info.
- class CavitySettings(k_e, phi, reference, status, freq_bunch_mhz, freq_cavity_mhz=None, transf_mat_func_wrappers=None, phi_s_funcs=None, field=None)[source]
Bases:
objectHold the cavity parameters that can vary during optimisation.
Todo
Which syntax for when I want to compute the value of a property but not return it? Maybe a
_ = self.phi_0_abs? Maybe this case should not appear here, appart for when I debug.Note
In this routine, all phases are defined in radian and are rf phases.
Todo
Determine if status should be kept here or in the field map.
Todo
For TraceWin solver, I will also need the field map index.
- Parameters:
k_e (
float)phi (
float)reference (
Literal['phi_0_abs','phi_0_rel','phi_s'])status (
Literal['compensate (in progress)','compensate (not ok)','compensate (ok)','failed','nominal','rephased (in progress)','rephased (ok)'])freq_bunch_mhz (
float)transf_mat_func_wrappers (
dict[str,Callable[[float,float,CavitySettings,dict[str,Any]],dict[str,Any]]] |None, default:None)phi_s_funcs (
dict[str,Callable[[Any],tuple[float,float]]] |None, default:None)
- __init__(k_e, phi, reference, status, freq_bunch_mhz, freq_cavity_mhz=None, transf_mat_func_wrappers=None, phi_s_funcs=None, field=None)[source]
Instantiate the object.
- Parameters:
k_e (
float) – Amplitude of the electric field.phi (
float) – Input phase in radians. Must be absolute or relative entry phase, or synchronous phase.reference (
Literal['phi_0_abs','phi_0_rel','phi_s']) – Name of the phase used for reference. When a particle enters the cavity, this is the phase that is not recomputed.status (
Literal['compensate (in progress)','compensate (not ok)','compensate (ok)','failed','nominal','rephased (in progress)','rephased (ok)']) – Cavity status.freq_bunch_mhz (
float) – Bunch frequency in \(\mathrm{MHz}\).freq_cavity_mhz (
float|None, default:None) – Frequency of the cavity in \(\mathrm{MHz}\). The default is None, which happens when theListOfElementsis under creation and we did not process theFREQcommands yet. transf_mat_func_wrappers : A dictionary which keys are the differentBeamCalculatorids, and values are corresponding functions to compute propagation of the beam.phi_s_funcs (
dict[str,Callable[[Any],tuple[float,float]]] |None, default:None) – A dictionary which keys are the differentBeamCalculatorids, and values are corresponding functions to compute synchronous phase and accelerating voltage from the ouput of correspondingtransf_mat_func_wrapper.field (
Field|None, default:None) – Holds the parameters that are geometry-specific, such as interpolated field maps.transf_mat_func_wrappers (
dict[str,Callable[[float,float,CavitySettings,dict[str,Any]],dict[str,Any]]] |None, default:None)
- Return type:
None
- _transf_mat_func_wrappers: dict[str, TRANSF_MAT_FUNC_WRAPPER_T]
All functions that can be used to compute beam propagation in current field map
- _phi_s_funcs: dict[str, PHI_S_FUNC_T]
All functions that can be used to compute synchronous phase and accelerating field in current field map
- _transf_mat_func_wrapper: TRANSF_MAT_FUNC_WRAPPER_T
The function to use with current solver to compute beam propagation
- _phi_s_func: PHI_S_FUNC_T
The function to use with current solver to compute synchronous phase and accelerating field
- classmethod copy(base, cavity_vars=None)[source]
Create cavity settings, based on
base.- Parameters:
base (
Self) – The referenceCavitySettings. A priori, this is the nominal settings.cavity_vars (
CavityVars|None, default:None) – Amplitude, phase, status and reference to override the ones inbase. Provided during optimization process.
- Returns:
A new
CavitySettingswith modified amplitude and phase.- Return type:
Self
- __str__()[source]
Print out the different phases/k_e, and which one is the reference.
Note
Nonemeans that the phase was not calculated.- Return type:
- get(*keys, to_deg=False, **kwargs)[source]
Get attributes from this class or its nested members.
- Parameters:
*keys (
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']) – Name of the desired attributes.to_deg (
bool, default:False) – Wether keys with"phi"in their name should be multiplied by \(360 / 2\pi\).**kwargs (
Any) – Other arguments passed to recursive getter.
- Return type:
- Returns:
Attribute(s) value(s).
- _check_consistency_of_status_and_reference()[source]
Perform some tests on
statusandreference.We check that if the cavity is rephased, its reference phase is not \(phi_{0,\,\mathrm{abs}}\)
If the cavity is broken, we check that its reference phase is not synchronous because it is not defined.
- Return type:
- set_bunch_to_rf_freq_func(freq_cavity_mhz=None)[source]
Set the rf frequency, and methods to switch between freq definitions.
This method is called a first time at the instantiation of
self; it will be called once again if aFreqcommand is found.
- property reference: Literal['phi_0_abs', 'phi_0_rel', 'phi_s']
Say what is the reference phase.
Equivalents of referencein TraceWin’sFIELD_MAPLightWin’s
referenceTraceWin
'phi_0_rel'P = 0'phi_0_abs'P = 1'phi_s'SET_SYNC_PHASE
- set_reference(reference, phi_ref=None, ensure_can_be_calculated=True)[source]
Change the reference phase.
- Parameters:
reference (
Literal['phi_0_abs','phi_0_rel','phi_s']) – The name of the new reference.phi_ref (
float|None, default:None) – The new value for the reference phase in \(\mathrm{rad}\). Remember that when the value of the reference phase is updated, all other phases are invalidated.ensure_can_be_calculated (
bool, default:True) – To check that the new reference phase is already set or can be calculated.
- Raises:
MissingAttributeError – When
ensure_can_be_calculated = Trueand the new reference phase cannot be calculated.- Return type:
- _delete_non_reference_phases()[source]
Reset the phases that are not the reference to None.
- Return type:
- property status: Literal['compensate (in progress)', 'compensate (not ok)', 'compensate (ok)', 'failed', 'nominal', 'rephased (in progress)', 'rephased (ok)']
Give the status of the cavity under study.
- property phi_s: float
Get the synchronous phase, and compute it if necessary.
Note
It is mandatory for the calculation of this quantity to compute propagation of the particle in the cavity.
See also
- set_cavity_parameters_methods(solver_id, transf_mat_function_wrapper, phi_s_func=None)[source]
Set the generic methods to compute beam propagation, cavity params.
This function is called within two contexts.
When initializing the
BeamCalculatorspecific parameters (ElementBeamCalculatorParameters).When re-initalizing the
ElementBeamCalculatorParametersbecause thestatusof the cavity changed, and in particular when it switches to'failed'. In this case, the_phi_s_funcis not altered.
- Parameters:
solver_id (
str) – The name of the solver for which functions must be changed.transf_mat_function_wrapper (
Callable) – A function that compute the propagation of the beam.phi_s_func (
Callable[[Any],tuple[float,float]] |None, default:None) – A function that takes in the output oftransf_mat_function_wrapperand returns the accelerating voltage in \(\mathrm{MV}\) and the synchronous phase in \(rad\). The default is None, which happens when we break the cavity and only thetransf_mat_function_wrapperneeds to be updated. In this case, the synchronous phase function is left unchanged.
- Return type:
None
See also
- set_cavity_parameters_arguments(solver_id, w_kin, **kwargs)[source]
Adapt the cavity parameters methods to beam with
w_kin.This function must be called:
When the kinetic energy at the entrance of the cavity is changed (like this occurs during optimisation process)
When the synchronous phase must be calculated with another solver.
- Parameters:
solver_id (
str) – Name of the solver that will compute \(V_\mathrm{cav}\) and \(\phi_s\).w_kin (
float) – Kinetic energy of the synchronous particle at the entry of the cavity.kwargs – Other keyword arguments that will be passed to the function that will compute propagation of the beam in the
FieldMap. Note that you should check thatphi_0_relkey is removed in yourBeamCalculator, to avoid a clash in the_phi_0_rel_to_cavity_parametersfunction.
- Return type:
None
See also
- _phi_0_rel_to_cavity_parameters(phi_0_rel)[source]
Compute cavity parameters based on relative entry phase.
- Parameters:
phi_0_rel (
float) – Relative entry phase in radians.- Return type:
- Returns:
A tuple containing (V_cav, phi_s).
- Raises:
MissingAttributeError – If the transfer matrix function or phi_s function is not set.
- _residual_func(phi_0_rel, phi_s)[source]
Calculate the squared difference between target and computed phi_s.
- _phi_s_to_phi_0_rel(phi_s)[source]
Find the relative entry phase that yields the target sync phase.
- Parameters:
phi_s (
float) – Target synchronous phase in radians.- Return type:
- Returns:
Relative entry phase in radians that achieves the target phi_s.
- Raises:
RuntimeError – If the optimization fails to find a solution.
- property v_cav_mv: float | None
Get the accelerating voltage, and compute it if necessary.
Note
It is mandatory for the calculation of this quantity to compute propagation of the particle in the cavity.
See also
- shift_phi_bunch(delta_phi_bunch, check_positive=False)[source]
Shift the synchronous particle entry phase by
delta_phi_bunch.This is mandatory when the reference phase is changed. In particular, it is the case when studying a sub-list of elements with
TraceWin. With this solver, the entry phase in the first element of the sub-ListOfElementsis always 0.0, even if is not the first element of the linac.- Parameters:
- Return type:
Examples
>>> phi_in_1st_element = 0.0 >>> phi_in_20th_element = 55.0 >>> 25th_element: FieldMap >>> 25th_element.cavity_settings.shift_phi_bunch( >>> ... phi_in_20th_element - phi_in_1st_element >>> ) # now phi_0_abs and phi_0_rel are properly understood