pyaccelerator.lattice

Accelerator lattice

Module Contents

Classes

Lattice

A lattice of accelerator elements.

Plotter

Lattice plotter.

class pyaccelerator.lattice.Lattice(*args)[source]

A lattice of accelerator elements.

Looks like a list, smells like a list and tastes like a list. Is in fact an accelerator lattice.

Examples

Create a simple lattice.

>>> Lattice([Drift(1), QuadrupoleThin(0.8)])
Lattice([Drift(l=1, name="drift_0"), QuadrupoleThin(f=0.8, name="quadrupole_thin_0")])
classmethod load(cls, path: os.PathLike) → ’Lattice’[source]

Load a lattice from a file.

Parameters

path – File path.

Returns

Loaded Lattice instance.

Examples

Save and load a lattice:

>>> lat = Lattice([Drift(1)])
>>> lat.save("drift.json")
>>> lat_loaded = Lattice.load("drift.json")
property m(self)[source]
closed_orbit(self, dp: float, **solver_kwargs) → TransportedPhasespace[source]

Compute the closed orbit for a given dp/p.

Parameters
  • dp – dp/p for which to compute the closed orbit.

  • **solver_kwargs – passed to scipy.root.

Returns

Closed orbit solution transported through the lattice.

closed_orbit_solution(self, dp: float, **solver_kwargs) → np.ndarray[source]

Compute the closed orbit solution for a given dp/p.

Parameters
  • dp – dp/p for which to compute the closed orbit.

  • **solver_kwargs – passed to scipy.root.

Returns

Closed orbit solution.

dispersion(self, **solver_kwargs) → TransportedPhasespace[source]

Compute the dispersion, i.e. the closed orbit for a particle with dp/p = 1.

Parameters

**solver_kwargs – passed to scipy.root.

Returns

Dispersion solution transported through the lattice.

dispersion_solution(self, **solver_kwargs)[source]

Compute the dispersion solution.

Parameters

**solver_kwargs – passed to scipy.root.

Returns

Dispersion solution.

twiss(self, plane='h') → TransportedTwiss[source]

Compute the twiss parameters through the lattice for a given plane.

Parameters

plane – plane of interest, either “h” or “v”.

Returns

Twiss parameters through the lattice.

twiss_solution(self, plane: str = 'h') → np.ndarray[source]

Compute the twiss periodic solution.

Parameters

plane – plane of interest, either “h” or “v”.

Returns

Twiss periodic solution.

tune(self, plane: str = 'h', n_turns: int = 1024, dp: float = 0, tol=0.0001) → float[source]

Compute the fractional part of the tune.

Note: the whole tune value would be Q = n + q or Q = n + (1 - q) with q the fractional part of the tune returned by this method and n an integer.

Parameters
  • plane – plane of interest, either “h” or “v”.

  • n_turns – number of turns for which to track the particle, higher values lead to more precise values at the expense of computation time.

  • dp – dp/p value of the tracked particle.

  • tol – numerical tolerance for DC component.

Returns

The fractional part of the tune.

chromaticity(self, plane: str = 'h', delta_dp=0.001, **kwargs) → float[source]

Compute the chromaticity. Tracks 2 particles with different dp/p and computes the chromaticity from the tune change.

Parameters
  • plane – plane of interest, either “h” of “v”.

  • delta_dp – dp/p difference between the 2 particles.

  • **kwargs – passed to the compute tune method.

Returns

Chromaticity value.

slice(self, element_type: Type[‘BaseElement’], n_element: int) → ’Lattice’[source]

Slice the element_type elements of the lattice into n_element.

Parameters
  • element_type – Element class to slice.

  • n_element – Slice element_type into n_element smaller elements.

Returns

Sliced Lattice.

Examples

Slice the Drift elements into 2:

>>> lat = Lattice([Drift(1), QuadrupoleThin(0.8)])
>>> lat.slice(Drift, 2)
Lattice([Drift(l=0.5, name="drift_0_slice_0"),
         Drift(l=0.5, name="drift_0_slice_1"),
         Quadrupole(f=0.8, name="quadrupole_thin_0")])
transport(self, initial: Sequence[Union[float, np.ndarray]]) → TransportedPhasespace[source]

Transport phase space coordinates or twiss parameters along the lattice.

Parameters

initial – phase space coords to transport through the lattice.

Returns

Transported phase space coords through the lattice.

Examples

Transport phase space coords through a Drift:

>>> lat = Lattice([Drift(1)])
>>> lat.transport(phasespace=[1, 1, 0, 0, 0])
TransportedPhasespace(s=array([0, 1], x=array([1., 2.]), x_prime=array([1., 1.]), y=array([0, 0]), y_prime=array([0, 0]), dp=array([0., 0.]))

Transport a distribution of phase space coordinates through the lattice:

>>> beam = Beam()
>>> lat = Lattice([Drift(1)])
>>> transported = lat.transport(beam.match([1, 0, 1]))
>>> plt.plot(tranported.s, transported.x)
...

Transport a phase space ellipse’s coordinates through the lattice:

>>> beam = Beam()
>>> lat = Lattice([Drift(1)])
>>> transported = lat.transport(beam.ellipse([1, 0, 1]))
>>> plt.plot(transported.x, transported.x_prime)
...
transport_twiss(self, twiss: Sequence[float], plane: str = 'h') → TransportedTwiss[source]

Transport the given twiss parameters along the lattice.

Parameters
  • twiss – list of twiss parameters, beta[m], alpha[rad], and gamma[m^-1], one twiss parameter can be None.

  • plane – plane of interest, either “h” or “v”.

Returns

Named tuple containing the twiss parameters along the lattice the

coordinates along the ring.

search(self, pattern: str, *args, **kwargs) → List[int][source]

Search the lattice for elements with name matching the pattern.

Parameters
  • pattern – RegEx pattern.

  • *args – Passed to re.search.

  • **kwargs – Passed to re.search.

Raises

ValueError – If not elements match the provided pattern.

Returns

List of indexes in the lattice where the element’s name matches the pattern.

append(self, *args, **kwargs)[source]

L.append(object) -> None – append object to end

clear(self, *args, **kwargs)[source]

L.clear() -> None – remove all items from L

extend(self, *args, **kwargs)[source]

L.extend(iterable) -> None – extend list by appending elements from the iterable

insert(self, *args, **kwargs)[source]

L.insert(index, object) – insert object before index

pop(self, *args, **kwargs)[source]

L.pop([index]) -> item – remove and return item at index (default last). Raises IndexError if list is empty or index is out of range.

remove(self, *args, **kwargs)[source]

L.remove(value) -> None – remove first occurrence of value. Raises ValueError if the value is not present.

reverse(self, *args, **kwargs)[source]

L.reverse() – reverse IN PLACE

sort(self, *args, **kwargs)[source]

DISABLED.

save(self, path: os.PathLike)[source]

Save a lattice to file.

Parameters

path – File path.

Examples

Save a lattice:

>>> lat = Lattice([Drift(1)])
>>> lat.save('drift.json')
copy(self, deep=True) → ’Lattice’[source]

Create a copy of the lattice.

Parameters

deep – If True create copies of the elements themselves.

Returns

A copy of the lattice.

class pyaccelerator.lattice.Plotter(lattice: Lattice)[source]

Lattice plotter.

Parameters

LatticeLattice instance.

Examples

Plot a lattice:

>>> lat = Lattice([QuadrupoleThin(-0.6), Drift(1), QuadrupoleThin(0.6)])
>>> lat.plot.layout()  # or lat.plot("layout")
...

Plot the top down view of the lattice:

>>> lat = Lattice([Drift(1), Dipole(1, np.pi/2)])
>>> lat.plot.top_down()  # or lat.plot("top_down")
...
top_down(self, n_s_per_element: int = int(1000.0)) → Tuple[plt.Figure, plt.Axes][source]

Plot the s coordinate in the horizontal plane of the lattice.

Parameters

n_s_per_element – Number of steps along the s coordinate for each element in the lattice.

Returns

Plotted plt.Figure and plt.Axes.

layout(self) → Tuple[plt.Figure, plt.Axes][source]

Plot the lattice.

Returns

Plotted plt.Figure and plt.Axes.