at.lattice.utils#
Helper functions for working with AT lattices.
A lattice as understood by pyAT is a sequence of elements. These functions are useful for working with these sequences.
Selecting elements in a lattice:
The refpts argument allows functions to select locations in the lattice. The location is defined as the entrance of the selected element. refpts may be:
an integer in the range [-len(lattice), len(lattice)-1] selecting an element according to the python indexing rules. As a special case, len(lattice) is allowed and refers to the end of the last element,
an ordered list of such integers without duplicates,
a numpy array of booleans of maximum length len(lattice)+1, where selected elements are
True
,None
, meaning an empty selection,All
, meaning “all possible reference points”: the entrance of all elements plus the end of the last element,End
, selecting the end of the last element,an element type, selecting all the elements of that type in the lattice, e.g.
at.Sextupole
,a string, selecting all the elements whose FamName attribute matches it. Unix shell-style wildcards are accepted, e.g. “Q[FD]*”,
a callable
filtfunc
such thatfiltfunc(elem)
isTrue
for selected elements.
Module Attributes
Functions
|
Return a tuple containing for each input argument the requested information |
|
Deprecated decorator for optics functions (see |
|
Decorator for optics functions |
|
Deprecated decorator for optics functions (see |
|
Decorator for optics functions |
|
Decorator for optics functions |
|
Return a |
|
Returns a |
|
Returns an integer array of element indices, selecting ring elements. |
|
Returns a bool array of element indices, selecting ring elements. |
|
Checks the presence or the value of an attribute |
|
Checks the type of an element |
|
Checks the name of an element |
|
Returns a list of elements selected by key. |
|
Returns the locations of selected elements |
|
Returns the number of reference points |
|
Return an iterator over selected elements in a lattice |
|
Sets the translations of a list of elements. |
|
Sets the tilts of a list of elements. |
|
Sets the tilts of a list of elements. |
|
Set the tilt angle \(\theta\) of an |
|
Sets the transverse displacement of an |
|
Set the tilt, pitch and yaw angle of an |
|
Extracts attribute values from selected |
|
Set the values of an attribute of an array of elements based on their refpts |
|
Compute the 2D ring geometry in cartesian coordinates |
|
Return a callable object which sets the value of item index of attribute attrname of its 1st argument to it 2nd orgument. |
|
Return a callable object which fetches item index of attribute attrname of its operand. |
Exceptions
- exception AtWarning[source]#
Bases:
UserWarning
- axis_descr(axis, [axis, ]key=None)[source]#
Return a tuple containing for each input argument the requested information
- Parameters:
axis (Union[int, str]) – either an index in 0:6 or a string in [‘x’, ‘xp’, ‘y’, ‘yp’, ‘dp’, ‘ct’]
key –
key in the coordinate description dictionary, selecting the desired information. One of :
- ’index’
index in the standard AT coordinate vector
- ’label’
label for plot annotation
- ’unit’
coordinate unit
None
entire description dictionary
- Returns:
descr (Tuple) – requested information for each input argument.
Examples
>>> axis_descr('x','dp', key='index') (0, 4)
returns the indices in the standard coordinate vector
>>> dplabel, = axis_descr('dp', key='label') >>> print(dplabel) $\delta$
returns the coordinate label for plot annotation
>>> axis_descr('x','dp') ({'index': 0, 'label': 'x', 'unit': ' [m]'}, {'index': 4, 'label': '$\\delta$', 'unit': ''})
returns the entire description directories
- bool_refpts(refpts, n_elements, endpoint=True, types='None, All, End, int, bool')[source]#
Returns a
bool
array of element indices, selecting ring elements.- Parameters:
- Returns:
bool_refs (BoolRefpts) – A bool numpy array used for indexing
Element
s in a lattice.
- check_6d(is_6d)[source]#
Decorator for optics functions
Wraps a function like
func(ring, *args, **kwargs)
wherering
is aLattice
object. RaiseAtError
ifring.is_6d
is not the desired is_6d state. No test is performed ifring
is not aLattice
.Example
>>> @check_6d(False) ... def find_orbit4(ring, dct=None, guess=None, **kwargs): ...
Raises
AtError
ifring.is_6d
isTrue
See also
- checkattr(attrname, attrvalue=None)[source]#
Checks the presence or the value of an attribute
Returns a function to be used as an
Element
filter, which checks the presence or the value of an attribute of the providedElement
. This function can be used to extract from a ring all elements having a given attribute.- Parameters:
- Returns:
checkfun (ElementFilter) – Element filter function
Examples
>>> cavs = filter(checkattr('Frequency'), ring)
Returns an iterator over all elements in ring that have a
Frequency
attribute>>> elts = filter(checkattr('K', 0.0), ring)
Returns an iterator over all elements in ring that have a
K
attribute equal to 0.0
- checkname(pattern, regex=False)[source]#
Checks the name of an element
Returns a function to be used as an
Element
filter, which checks the name of the providedElement
. This function can be used to extract from a ring all elements having a given name.- Parameters:
pattern (str) – Desired
Element
name. Unix shell-style wildcards are supported (seefnmatch.fnmatch()
)regex (bool) – Use regular expression for refpts string matching instead of Unix shell-style wildcards.
- Returns:
checkfun (ElementFilter) – Element filter function
Examples
>>> qps = filter(checkname('QF*'), ring)
Returns an iterator over all with name starting with
QF
.
- checktype(eltype)[source]#
Checks the type of an element
Returns a function to be used as an
Element
filter, which checks the type of the providedElement
. This function can be used to extract from a ring all elements having a given type.- Parameters:
- Returns:
checkfun (ElementFilter) – Element filter function
Examples
>>> qps = filter(checktype(at.Quadrupole), ring)
Returns an iterator over all quadrupoles in ring
- get_bool_index(ring, refpts, endpoint=True, regex=False)[source]#
Returns a bool array of element indices, selecting ring elements.
- Parameters:
refpts (Type[Element] | Element | Callable[[Element], bool] | str | None | int | Sequence[int] | bool | Sequence[bool] | RefptsCode) – Element selection key. See “Selecting elements in a lattice”
endpoint (bool) – if
True
, allow len(ring) as a special index, referring to the end of the last element.regex (bool) – Use regular expression for refpts string matching instead of Unix shell-style wildcards.
- Returns:
bool_refs (BoolRefpts) – A bool numpy array used for indexing
Element
s in a lattice.
Examples
>>> refpts = get_bool_index(ring, at.Quadrupole)
Returns a numpy array of booleans where all
Quadrupole
areTrue
>>> refpts = get_bool_index(ring, "Q[FD]*")
Returns a numpy array of booleans where all elements whose FamName matches “Q[FD]*” are
True
>>> refpts = get_bool_index(ring, at.checkattr('K', 0.0))
Returns a numpy array of booleans where all elements whose K attribute is 0.0 are
True
>>> refpts = get_bool_index(ring, None)
Returns a numpy array of len(ring)+1
False
values
- get_elements(ring, refpts, regex=False)[source]#
Returns a list of elements selected by key.
Deprecated:
get_elements(ring, refpts)
isring[refpts]
- Parameters:
- Returns:
elem_list (list) – list of
Element
s matching key
- get_geometry(ring, refpts=RefptsCode.All, start_coordinates=(0, 0, 0), centered=False, regex=False)[source]#
Compute the 2D ring geometry in cartesian coordinates
- Parameters:
refpts (Type[Element] | Element | Callable[[Element], bool] | str | None | int | Sequence[int] | bool | Sequence[bool] | RefptsCode) – Element selection key. See “Selecting elements in a lattice”
start_coordinates (Tuple[float, float, float]) – x, y, angle at starting point. x and y are ignored if centered is
True
.centered (bool) – if
True
the coordinates origin is the center of the ring.regex (bool) – Use regular expression for refpts string matching instead of Unix shell-style wildcards.
- Returns:
geomdata – recarray containing, x, y, angle.
radius – machine radius at the beginning of the lattice.
Attention
This radius is different from the radius usually defined as \(C/2\pi\)
Example
>>> geomdata, radius = get_geometry(ring)
- get_s_pos(ring, refpts=RefptsCode.All, regex=False)[source]#
Returns the locations of selected elements
- Parameters:
- Returns:
s_pos – Array of locations of the elements selected by refpts
Example
>>> get_s_pos(ring, at.End) array([26.37428795])
Position at the end of the last element: length of the lattice
- get_uint32_index(ring, refpts, endpoint=True, regex=False)[source]#
Returns an integer array of element indices, selecting ring elements.
- Parameters:
refpts (Type[Element] | Element | Callable[[Element], bool] | str | None | int | Sequence[int] | bool | Sequence[bool] | RefptsCode) – Element selection key. See “Selecting elements in a lattice”
endpoint (bool) – if
True
, allow len(ring) as a special index, referring to the end of the last element.regex (bool) – Use regular expression for refpts string matching instead of Unix shell-style wildcards.
- Returns:
uint32_ref (Uint32Refpts) – uint32 numpy array used for indexing
Element
s in a lattice.
Examples
>>> get_uint32_index(ring, at.Sextupole) array([ 21, 27, 35, 89, 97, 103], dtype=uint32)
numpy array of indices of all
Sextupole
s>>> get_uint32_index(ring, at.End) array([121], dtype=uint32)
numpy array([
len(ring)+1
])>>> get_uint32_index(ring, at.checkattr('Frequency')) array([0], dtype=uint32)
numpy array of indices of all elements having a ‘Frequency’ attribute
- get_value_refpts(ring, refpts, attrname, index=None, regex=False)[source]#
- Extracts attribute values from selected
lattice
Element
s.
- Parameters:
refpts (Type[Element] | Element | Callable[[Element], bool] | str | None | int | Sequence[int] | bool | Sequence[bool] | RefptsCode) – Element selection key. See “Selecting elements in a lattice”
attrname (str) – Attribute name
index (int | None) – index of the value to retrieve if attrname is an array. If
None
the full array is retrievedregex (bool) – Use regular expression for refpts string matching instead of Unix shell-style wildcards.
- Returns:
attrvalues – numpy Array of attribute values.
- getval(attrname, index=None)[source]#
Return a callable object which fetches item index of attribute attrname of its operand. Examples:
After
f = getval('Length')
,f(elem)
returnselem.Length
After
f = getval('PolynomB, index=1)
,f(elem)
returnselem.PolynomB[1]
- make_copy(copy)[source]#
Decorator for optics functions
Wraps a function like
func(ring, refpts, *args, **kwargs)
wherering
is aLattice
object.If copy is
False
, the function is not modified,If copy is
True
, a shallow copy ofring
is done, then the elements selected by refpts are deep-copied, thenfunc
is applied to the copy, and the newring
is returned.
- refpts_count(refpts, n_elements, endpoint=True, types='None, All, End, int, bool')[source]#
Returns the number of reference points
- refpts_iterator(ring, refpts, regex=False)[source]#
Return an iterator over selected elements in a lattice
- Parameters:
- Returns:
elem_iter (Iterator[Element]) – Iterator over the elements in ring selected by refpts.
- rotate_elem(elem, tilt=0.0, pitch=0.0, yaw=0.0, relative=False)[source]#
Set the tilt, pitch and yaw angle of an
Element
. The tilt is a rotation around the s-axis, the pitch is a rotation around the x-axis and the yaw is a rotation around the y-axis.A positive angle represents a clockwise rotation when looking in the direction of the rotation axis.
The transformations are not all commmutative, the pitch and yaw are applied first and the tilt is always the last transformation applied. The element is rotated around its mid-point.
If relative is
True
, the previous angle and shifts are rebuilt form the R and T matrix and incremented by the input arguments.The shift is always conserved regardless of the value of relative.
The transformations are applied by changing the particle coordinates at the entrance of the element and restoring them at the end. Following the small angles approximation the longitudinal shift of the particle coordinates is neglected and the element length is unchanged.
- set_6d(is_6d)[source]#
Decorator for optics functions
Wraps a function like
func(ring, *args, **kwargs)
wherering
is aLattice
object. Ifring.is_6d
is not the desired is_6d state,func()
will be called with a modified copy ofring
satisfying the is_6d state.- Parameters:
is_6d (bool) – Desired 6D state
Example
>>> @set_6d(True) ... def compute(ring) ...
is roughly equivalent to:
>>> if ring.is_6d: ... compute(ring) ... else: ... compute(ring.enable_6d(copy=True))
- See Also:
- set_rotation(ring, tilts=0.0, pitches=0.0, yaws=0.0, relative=False)[source]#
Sets the tilts of a list of elements.
- Parameters:
tilts – Sequence of tilt values as long as ring or scalar tilt value applied to all elements, default=0
pitches – Sequence of pitch values as long as ring or scalar tilt value applied to all elements, default=0
yaws – Sequence of yaw values as long as ring or scalar tilt value applied to all elements, default=0
relative – If
True
, the rotations are added to the existing ones
- set_shift(ring, dxs, dzs, relative=False)[source]#
Sets the translations of a list of elements.
- Parameters:
dxs – Sequence of horizontal displacements values as long as ring or scalar value applied to all elements [m]
dzs – Sequence of vertical displacements values as long as ring or scalar value applied to all elements [m]
relative – If
True
, the displacement is added to the existing one
- set_value_refpts(ring, refpts, attrname, attrvalues, index=None, increment=False, copy=False, regex=False)[source]#
Set the values of an attribute of an array of elements based on their refpts
- Parameters:
refpts (Type[Element] | Element | Callable[[Element], bool] | str | None | int | Sequence[int] | bool | Sequence[bool] | RefptsCode) – Element selection key. See “Selecting elements in a lattice”
attrname (str) – Attribute name
attrvalues – Attribute values
index (int | None) – index of the value to set if attrname is an array. if
None
, the full array is replaced by attrvalueincrement (bool) – If
True
, attrvalues are added to the initial values.regex (bool) – Use regular expression for refpts string matching instead of Unix shell-style wildcards.
copy (bool) –
If
False
, the modification is done in-place,If
True
, return a shallow copy of the lattice. Only the modified elements are copied.Caution
a shallow copy means that all non-modified elements are shared with the original lattice. Any further modification will affect both lattices.
- setval(attrname, index=None)[source]#
Return a callable object which sets the value of item index of attribute attrname of its 1st argument to it 2nd orgument.
After
f = setval('Length')
,f(elem, value)
is equivalent toelem.Length = value
After
f = setval('PolynomB, index=1)
,f(elem, value)
is equivalent toelem.PolynomB[1] = value
- shift_elem(elem, deltax=0.0, deltaz=0.0, relative=False)[source]#
Sets the transverse displacement of an
Element
The translation vectors are stored in the
T1
andT2
attributes.
- tilt_elem(elem, rots, relative=False)[source]#
Set the tilt angle \(\theta\) of an
Element
The rotation matrices are stored in the
R1
andR2
attributes.\(R_1=\begin{pmatrix} cos\theta & sin\theta \\ -sin\theta & cos\theta \end{pmatrix}\), \(R_2=\begin{pmatrix} cos\theta & -sin\theta \\ sin\theta & cos\theta \end{pmatrix}\)
- uint32_refpts(refpts, n_elements, endpoint=True, types='None, All, End, int, bool')[source]#
Return a
uint32
array of element indices selecting ring elements.- Parameters:
- Returns:
uint32_ref (Uint32Refpts) –
uint32
numpy array used for indexingElement
s in a lattice.