core.transfer_matrix.transfer_matrix module

Hold the transfer matrix along the linac.

Todo

Check if it can be more efficient. Maybe store R_xx, R_yy, R_zz separately?

Todo

Maybe transfer matrices should always be (6, 6)??

Todo

_init_from methods in factory???

Todo

The SimulationOutput.get method with transfer matrix components fails with TraceWin solver.

class TransferMatrix(is_3d: bool, first_cumulated_transfer_matrix: ndarray, element_to_index: Callable[[str | Element, str | None], int | slice], individual: ndarray | None = None, cumulated: ndarray | None = None)

Bases: object

Hold the (n, 6, 6) transfer matrix along the linac.

Note

When the simulation is in 1D only, the values corresponding to the transverse planes are filled with np.nan.

individual

Individual transfer matrices along the linac. Not defined if not provided at initialisation.

Type:

np.ndarray

cumulated

Cumulated transfer matrices along the linac.

Type:

np.ndarray

__init__(is_3d: bool, first_cumulated_transfer_matrix: ndarray, element_to_index: Callable[[str | Element, str | None], int | slice], individual: ndarray | None = None, cumulated: ndarray | None = None) None

Create the object and compute the cumulated transfer matrix.

Parameters:
  • is_3d (bool) – If the simulation is in 3d or not.

  • first_cumulated_transfer_matrix (np.ndarray) – First transfer matrix.

  • individual (np.ndarray | list[np.ndarray] | None, optional) – Individual transfer matrices. The default is None, in which case the cumulated transfer matrix must be provided directly.

  • cumulated (np.ndarray | None, optional) – Cumulated transfer matrices. The default is None, in which case the individual transfer matrices must be given.

  • element_to_index – to doc

_compute_cumulated(first_cumulated_transfer_matrix: ndarray, shape: tuple[int, int, int], is_3d: bool, n_points: int) ndarray

Compute cumulated transfer matrix from individual.

Parameters:
  • first_cumulated_transfer_matrix (np.ndarray) – First transfer matrix. It should be eye matrix if we study a linac from the start (z_pos == 0.), and should be the cumulated transfer matrix of the previous linac portion otherwise.

  • shape (tuple[int, int, int]) – Shape of the output cumulated array.

  • is_3d (bool) – If the simulation is in 3D or not.

  • n_points (int) – Number of mesh points along the linac.

Returns:

  • cumulated (np.ndarray) – Cumulated transfer matrix.

  • .. todo:: – I think the 3D/1D handling may be smarter?

_init_from_cumulated(cumulated: ndarray | None, first_cumulated_transfer_matrix: ndarray, tol: float = 1e-08) tuple[int, ndarray]

Check that the given cumulated matrix is valid.

Parameters:
  • cumulated (np.ndarray) – Cumulated transfer matrices along the linac.

  • first_cumulated_transfer_matrix (np.ndarray) – The first of the cumulated transfer matrices.

  • tol (float, optional) – The max allowed difference between cumulated and first_cumulated_transfer_matrix when determining if they are the same or not.

Returns:

  • n_points (int) – Number of mesh points along the linac.

  • cumulated (np.ndarray) – Cumulated transfer matrices.

_init_from_individual(individual: ndarray, first_cumulated_transfer_matrix: ndarray | None) tuple[int, ndarray]

Compute cumulated transfer matrix from individual.

Parameters:
  • individual (np.ndarray) – Individual transfer matrices along the linac.

  • first_cumulated_transfer_matrix (np.ndarray | None) – First transfer matrix. It should be None if we study a linac from the start (z_pos == 0.), and should be the cumulated transfer matrix of the previous linac portion otherwise.

Returns:

  • n_points (int) – Number of mesh points along the linac.

  • cumulated (np.ndarray) – Cumulated transfer matrices.

get(*keys: str, elt: Element | None = None, pos: str | None = None, **kwargs: Any) tuple[ndarray | float, ...]

Shorthand to get attributes from this class or its attributes.

Parameters:
  • *keys (str) – Name of the desired attributes.

  • to_numpy (bool, optional) – If you want the list output to be converted to a np.ndarray. The default is True.

  • none_to_nan (bool, optional) – To convert None to np.nan. The default is True.

  • elt (Element | None, optional) – If provided, return the attributes only at the considered Element.

  • pos ('in' | 'out' | None) – If you want the attribute at the entry, exit, or in the whole Element.

  • **kwargs (Any) – Other arguments passed to recursive getter.

Returns:

out – Attribute(s) value(s). Will be floats if only one value is returned (elt is given, pos is in ('in', 'out')).

Return type:

tuple[np.ndarray | float, …]

has(key: str) bool

Check if object has attribute named key.

property r_xx: ndarray

Return the transfer matrix of \([x-x']\) plane.

property r_yy: ndarray

Return the transfer matrix of \([y-y']\) plane.

property r_zdelta: ndarray

Return the transfer matrix of \([z-\delta]\) plane.

property r_zz: ndarray

Return the transfer matrix of \([z-\delta]\) plane.

Deprecated since version v3.2.2.3: Use r_zdelta instead. Although it is called r_zz in the TraceWin doc, it is a transfer matrix in the \([z-\delta]\) plane.