Metadata-Version: 2.1
Name: simplestoragering
Version: 0.0.1
Summary: simple storage ring for single particle dynamics simulation.
Author-email: Bingfeng WEI <wbf2016@mail.ustc.edu.cn>
License: MIT License
        
        Copyright (c) 2021 Wei, Bingfeng
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/wei0852/simplestoragering
Project-URL: Repository, https://github.com/wei0852/simplestoragering.git
Project-URL: Issues, https://github.com/wei0852/simplestoragering/issues
Classifier: Development Status :: 4 - Beta
Classifier: Operating System :: OS Independent
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Cython
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy >1.18.0
Requires-Dist: matplotlib >3.4.0

# SimpleStorageRing
SimpleStorageRing is a python project to simulate simple storage ring and calculate lattice data.

You can sort the components to generate a ring, and then calculate the data of storage ring by Courant-Snyder method. 

----------------------
The code is written in Cython, must be compiled first:
```command
python setup.py build_ext --inplace
```
reference: http://docs.cython.org/en/stable/src/quickstart/build.html

## reference particle 
set reference particle (electron) energy first:

```
set_ref_energy(energy_MeV: float)
```
or
```
set_ref_gamma(gamma: float)
```
## components
A storage ring consists of many magnets to guide electron beam,

* `Mark(name: str)`
* `Drift(name: str, length: float, Ax: float = 10, Ay: float = 10)`
* `HBend(name: str, length: float, theta: float, theta_in: float, theta_out: float, k1: float = 0, gap: float = 0, fint_in: float = 0.5, fint_out: float = 0.5, Ax: float = 10, Ay: float = 10))`, horizontal bend.
* `Quadrupole(name: str, length: float, k1: float, Ax: float = 10, Ay: float = 10)`
* `Sextupole(name: str, length: float, k2: float = 0, n_slices: int = 4, Ax: float = 10, Ay: float = 10)`
* `Octupole(name: str = None, length: float = 0, k3: float = 0, n_slices: int = 1, Ax: float = 10, Ay: float = 10)`

$k_n = \dfrac{q}{P_0} \dfrac{\partial^n B_y}{\partial x^n}$. All the components are child classes of the Element class. 

Attributes:

* magnet data
    * name: str, 
    * length: float, 
    * h: float, curvature of the reference trajectory.
    * theta_in, theta_out: float, edge angle of bend.
    * k1, k2, k3: float, multipole strength.
    * Ax, Ay: float, aperture.
    * n_slices: int, slice magnets for RDTs calculation and particle tracking.
    * matrix: 6x6 np.ndarray, transport matrix.
* data about beam and lattice.
    * s: float, location in a line or ring.
    * closed_orbit: list[6]
    * betax, alphax, gammax, psix, betay, alphay, gammay, psiy, etax, etaxp, etay, etayp: float, twiss parameters and dispersion.
    * nux, nuy: float, psix / 2$\pi$, psiy / 2$\pi$
    * curl_H: float, dispersion H-function (curly-H function) $\mathcal{H}_x=\gamma_x \eta_x^2 + 2\alpha_x \eta_x \eta_x' + \beta_x\eta_x'^2$.

and methods:

* copy(), return a same component without data about beam or lattice. 
* slice(n_slices: int) -> list[Elements]
* linear_optics() -> np.array([i1, i2, i3, i4, i5, xix, xiy]), 
                           np.array([betax, alphax, gammax, betay, alphay, gammay, etax, etaxp, etay, etayp, psix, psiy])

## Courant-Snyder lattice
`CSLattice(ele_list: list[Element], n_periods: int, coupling: float=0.0)`, lattice solved by cs method.

Attributes:
* length: float
* n_periods: int, number of periods
* angle, abs_angle: float
* elements: list of Elements
* mark: Dictionary of Mark in lattice. The key is the name of Mark, and the value is a list of Mark with the same name.
* initialize twiss
    * twiss_x0, twiss_y0: np.ndarray, [$\beta$, $\alpha$, $\gamma$]
    * eta_x0, eta_y0: np.ndarray, [$\eta$, $\eta'$]. eta_y0=[0,0] because the coupled motion has not be considered yet.
* nux, nuy: Tunes
* xi_x, xi_y: Chromaticities (Calculated with linear model. use track_chromaticity for more accurate results.).
* natural_xi_x, natural_xi_y: Natural chromaticities
* I1, I2, I3, I4, I5: radiation integrals.
* Jx, Jy, Js: horizontal / vertical / longitudinal damping partition number
* sigma_e: natural energy spread
* emittance: natural emittance
* U0: energy loss [MeV]
* f_c: frequency
* tau_s, tau_x, tau_y: longitudinal / horizontal / vertical damping time
* alpha: Momentum compaction
* etap: phase slip factor

Methods:
* set_initial_twiss(betax, alphax, betay, alphay, etax, etaxp, etay, etayp)
  
    If periodic solutions are not used ( linear_optics(periodicity=False) ).
* linear_optics(periodicity=True, line_mode=False)
* driving_terms(verbose=True)
* adts(verbose=True)
* track_chromaticity(order=2, verbose=True, delta=None, matrix_precision=1e-9, resdl_limit=1e-12)
  
  compute chromaticities using the tunes of 4d off-momentum closed orbit.
* slice_elements(drift_maxlength=10.0, bend_maxlength=10.0, quad_maxlength=10.0, sext_maxlength=10.0)
* output_twiss(file_name: str = u'twiss_data.txt')

## Functions

`symplectic_track(particle, lattice, n_turns: int, record = True)`

`track_4d_closed_orbit(lattice: CSLattice, delta, resdl_limit=1e-16, matrix_precision=1e-9)`

`output_opa_file(lattice: CSLattice, file_name=None)`

`output_elegant_file(lattice: CSLattice, filename=None, new_version=True)`

`chromaticity_correction(lattice: CSLattice, sextupole_name_list: list, target: list=None, initial_k2=None, update_sext=True, verbose=True)`

## DA & LMA

* XYGrid()
* XDeltaGrid()
* NLine()
* LocalMomentumAperture()

and some functions:

`plot_layout_in_ax(ele_list: list, ax: Axes, ratio=0.03)`

`plot_resonance_line_in_ax(ax: Axes, order: int = 3, refnux: float=None, refnuy: float=None)`

`plot_lattice(ele_list, parameter: str or list[str], with_layout=True)`

or use `get_col(ele_list, parameter)` to get a column of the parameter along the element list.

