helper module
Define some helper functions to filter list of elements.
Todo
Filtering consistency.
- filter_out(elts, to_exclude)[source]
Filter out types while keeping the input list structure.
Note
Function not used anymore. Keeping it just in case.
- filter_elts(elts, type_to_check)[source]
Filter elements according to their type.
Note
Used only for
filter_cav(), may be simpler?
- filter_cav(elts, *, type_to_check=<class 'lightwin.core.elements.field_maps.field_map.FieldMap'>)
Filter elements according to their type.
Note
Used only for
filter_cav(), may be simpler?
- elt_at_this_s_idx(elts, s_idx, show_info=False)[source]
Give the element where the given index is.
- Parameters:
- Return type:
- Returns:
Element where the mesh index
s_idxis inelts.
- equivalent_elt_idx(elts, elt)[source]
Return the index of element from
eltscorresponding toelt.Important
This routine uses the name of the element and not its adress. So it will not complain if the
Elementobject that you asked for is not in this list of elements. In the contrary, it was meant to find equivalent cavities between different lists of elements.See also
- Parameters:
elts (
TypeVar(ListOfElements) |list[Element]) – List of elements where you want the position.elt (
str|Element|Literal['first','last']) – Element of which you want the position. If you give a str, it should be the name of an element. If it is anElement, we take its name in the routine. Magic keywords'first','last'are also accepted.
- Return type:
- Returns:
Index of equivalent element.
- equivalent_elt(elts, elt)[source]
- Overloads:
elts (ListOfElements | list[Element] | list[FieldMap]), elt (FieldMap) → FieldMap
elts (ListOfElements | list[Element] | list[FieldMap]), elt (Element | str | GET_ELT_ARG_T) → Element
- Parameters:
- Return type:
Return the element from
eltscorresponding toelt.Important
This routine uses the name of the element and not its adress. So it will not complain if the
Elementobject that you asked for is not in this list of elements. In the contrary, it was meant to find equivalent cavities between different lists of elements.- Parameters:
elts (
TypeVar(ListOfElements) |list[Element] |list[FieldMap]) – List of elements where you want the position.elt (
Element|str|FieldMap|Literal['first','last']) – Element of which you want the position. If you give a str, it should be the name of an element. If it is anElement, we take its name in the routine. Magic keywords'first','last'are also accepted.
- Returns:
Equivalent element.
- Return type:
- indiv_to_cumul_transf_mat(tm_cumul_in, r_zz_elt, n_steps)[source]
Compute cumulated transfer matrix.
- Parameters:
- Return type:
- Returns:
Cumulated transfer matrices.
- group_elements_by_section_and_lattice(by_section)[source]
Regroup Elements by Section and then by Lattice.
- _get_first_key_of_idx_dict_higher_than(elts, *, index_name, first_or_last, higher_than=-1, n_to_check=10)[source]
Take first valid idx in
n_to_checkfirst/last elements ofelts.Typical usage is getting the number of sections or lattice by taking the last element with a section/lattice index higher than -1.
- Parameters:
index_name (
str) – Name of the index to get. Must be a key of in theidxattribute ofElement.first_or_last (
Literal['first','last']) – If we want to check then_to_checkfirst or last elements.higher_than (
int, default:-1) – The index under which the value is invalid. The default is -1, which is the initialisation index for all the values of theidxdictionary.n_to_check (
int, default:10) – Number of elements in which we will look for the index.
- Return type:
- Returns:
The first valid index that is found.
- first(iterable, default=None, condition=<function <lambda> at 0x7318faa86ac0>)[source]
Return the first item in
iterablesatisfyingcondition.If the condition is not given, returns the first item of the iterable.
If the
defaultargument is given and the iterable is empty, or if it has no items matching the condition, thedefaultargument is returned if it matches the condition.The
defaultargument being None is the same as it not being given.Raises
StopIterationif no item satisfying the condition is found and default is not given or doesn’t satisfy the condition.>>> first((1, 2, 3), condition=lambda x: x % 2 == 0) 2 >>> first(range(3, 100)) 3 >>> first(()) Traceback (most recent call last): ... StopIteration >>> first([], default=1) 1 >>> first([], default=1, condition=lambda x: x % 2 == 0) Traceback (most recent call last): ... StopIteration >>> first([1, 3, 5], default=1, condition=lambda x: x % 2 == 0) Traceback (most recent call last): ... StopIteration