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, first_cumulated_transfer_matrix, element_to_index, individual=None, cumulated=None)[source]

Bases: object

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

Note

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

Parameters:
__init__(is_3d, first_cumulated_transfer_matrix, element_to_index, individual=None, cumulated=None)[source]

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 (ndarray[tuple[Any, ...], dtype[double]]) – First transfer matrix.

  • individual (ndarray[tuple[Any, ...], dtype[double]] | None, default: None) – Individual transfer matrices. The default is None, in which case the cumulated transfer matrix must be provided directly.

  • cumulated (ndarray[tuple[Any, ...], dtype[double]] | None, default: None) – Cumulated transfer matrices. The default is None, in which case the individual transfer matrices must be given.

  • element_to_index (ELEMENT_TO_INDEX_T) – Takes an Element, its name, 'first' or 'last' as argument, and returns corresponding index. Index should be the same in all the arrays attributes of this class: z_abs, beam_parameters attributes, etc. Used to easily get the desired properties at the proper position.

Return type:

None

_element_to_index

Takes an Element, its name, 'first' or 'last' as argument, and returns corresponding index. Index should be the same in all the arrays attributes of this class: z_abs, beam_parameters attributes, etc. Used to easily get the desired properties at the proper position.

has(key)[source]

Check if object has attribute named key.

Parameters:

key (str)

Return type:

bool

get(*keys, elt=None, pos=None, to_numpy=True, none_to_nan=False, handle_missing_elt=False, **kwargs)[source]

Get attributes from this class.

Optionally, at a specific element/position.

Parameters:
  • *keys (Literal['cumulated', 'individual', 'n_points', 'r_xx', 'r_yy', 'r_zdelta', 'r_zz', 'r_zdelta_11', 'r_zdelta_12', 'r_zdelta_21', 'r_zdelta_22']) – Names of the desired attributes.

  • elt (Element | str | None, default: None) – Element or its name where the value should be extracted.

  • pos (Literal['in', 'out'] | None, default: None) – Position in the element.

  • to_numpy (bool, default: True) – Convert lists to NumPy arrays.

  • none_to_nan (bool, default: False) – Replace None values with np.nan.

  • handle_missing_elt (bool, default: False) – Look for an equivalent element when elt is not in TransferMatrix._element_to_index ‘s _elts.

  • **kwargs (Any) – Ignored here, but accepted for compatibility.

Return type:

Any

Returns:

Attribute(s) at the requested location.

_init_from_individual(individual, first_cumulated_transfer_matrix)[source]

Compute cumulated transfer matrix from individual.

Parameters:
  • individual (ndarray[tuple[Any, ...], dtype[double]]) – Individual transfer matrices along the linac.

  • first_cumulated_transfer_matrix (ndarray[tuple[Any, ...], dtype[double]] | 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.

Return type:

tuple[int, ndarray[tuple[Any, ...], dtype[double]]]

Returns:

  • n_points – Number of mesh points along the linac.

  • cumulated – Cumulated transfer matrices.

_init_from_cumulated(cumulated, first_cumulated_transfer_matrix, tol=1e-08)[source]

Check that the given cumulated matrix is valid.

Parameters:
  • cumulated (ndarray[tuple[Any, ...], dtype[double]] | None) – Cumulated transfer matrices along the linac.

  • first_cumulated_transfer_matrix (ndarray[tuple[Any, ...], dtype[double]]) – The first of the cumulated transfer matrices.

  • tol (float, default: 1e-08) – The max allowed difference between cumulated and first_cumulated_transfer_matrix when determining if they are the same or not.

Return type:

tuple[int, ndarray[tuple[Any, ...], dtype[double]]]

Returns:

  • n_points – Number of mesh points along the linac.

  • cumulated – Cumulated transfer matrices.

_compute_cumulated(first_cumulated_transfer_matrix, shape, is_3d, n_points)[source]

Compute cumulated transfer matrix from individual.

Parameters:
  • first_cumulated_transfer_matrix (ndarray[tuple[Any, ...], dtype[double]]) – 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.

Return type:

ndarray[tuple[Any, ...], dtype[double]]

Returns:

  • Cumulated transfer matrix.

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

property r_xx: ndarray[tuple[Any, ...], dtype[float64]]

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

property r_yy: ndarray[tuple[Any, ...], dtype[float64]]

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

property r_zz: ndarray[tuple[Any, ...], dtype[float64]]

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.

property r_zdelta: ndarray[tuple[Any, ...], dtype[float64]]

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

property r_zdelta_11: ndarray[tuple[Any, ...], dtype[float64]]

Return first component of transfer matrix in \([z-\delta]\).

property r_zdelta_12: ndarray[tuple[Any, ...], dtype[float64]]

Return 2nd component of transfer matrix in \([z-\delta]\).

property r_zdelta_21: ndarray[tuple[Any, ...], dtype[float64]]

Return 3rd component of transfer matrix in \([z-\delta]\).

property r_zdelta_22: ndarray[tuple[Any, ...], dtype[float64]]

Return 4td component of transfer matrix in \([z-\delta]\).