Metadata-Version: 2.4
Name: cavsim2d
Version: 0.2.0
Summary: Quick 2D axisymmetric RF cavity analysis: eigenmode, tuning, wakefield, UQ and optimisation.
Author-email: Sosoho-Abasi Udongwo <numurho@gmail.com>
License-Expression: MIT
Project-URL: Homepage, https://github.com/Dark-Elektron/cavsim2d
Project-URL: Documentation, https://dark-elektron.github.io/cavsim2d/
Project-URL: Repository, https://github.com/Dark-Elektron/cavsim2d
Project-URL: Changelog, https://github.com/Dark-Elektron/cavsim2d/blob/master/CHANGELOG.md
Keywords: rf-cavity,accelerator,eigenmode,wakefield,finite-element,ngsolve
Classifier: Programming Language :: Python :: 3
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Topic :: Scientific/Engineering :: Physics
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.26.4
Requires-Dist: scipy>=1.13.1
Requires-Dist: pandas>=2.2.2
Requires-Dist: matplotlib>=3.8.4
Requires-Dist: ngsolve>=6.2.2402
Requires-Dist: gmsh>=4.13
Requires-Dist: paretoset>=1.2.4
Requires-Dist: termcolor>=2.1.0
Requires-Dist: tqdm>=4.66.4
Requires-Dist: openpyxl
Requires-Dist: jinja2
Requires-Dist: joblib>=1.3
Requires-Dist: scikit-learn>=1.6
Requires-Dist: SALib
Requires-Dist: ipywidgets>=8.1.3
Requires-Dist: anywidget>=0.9
Requires-Dist: webgui_jupyter_widgets>=0.2.18
Provides-Extra: jupyter
Requires-Dist: ipykernel; extra == "jupyter"
Requires-Dist: jupyterlab; extra == "jupyter"
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Requires-Dist: flake8; extra == "dev"
Provides-Extra: docs
Requires-Dist: sphinx-immaterial>=0.11; extra == "docs"
Requires-Dist: sphinxcontrib-bibtex; extra == "docs"
Requires-Dist: myst-nb; extra == "docs"
Dynamic: license-file

# cavsim2d

[![Documentation](https://img.shields.io/badge/docs-online-blue)](https://dark-elektron.github.io/cavsim2d/)
[![PyPI](https://img.shields.io/pypi/v/cavsim2d)](https://pypi.org/project/cavsim2d/)
[![Python](https://img.shields.io/badge/python-3.10%2B-blue)](https://pypi.org/project/cavsim2d/)
[![License: MIT](https://img.shields.io/badge/license-MIT-blue)](https://github.com/Dark-Elektron/cavsim2d/blob/master/LICENSE)
![Status: beta](https://img.shields.io/badge/status-beta-orange)

Documentation: [dark-elektron.github.io/cavsim2d](https://dark-elektron.github.io/cavsim2d/),
with installation, a quickstart, step-by-step tutorials, a guide to every analysis and
the API reference.

cavsim2d is a Python toolkit for designing and analysing axisymmetric RF cavities,
such as the elliptical cavities of superconducting linacs, pillboxes, RF guns or a
shape of your own. Because the structures are axisymmetric, it works on their 2D
cross-section, so it runs on an ordinary computer: an eigenmode solve of a cell
takes seconds.

## What you can do with it

- [Eigenmodes](https://dark-elektron.github.io/cavsim2d/eigenmode.html): resonant
  frequencies, fields and figures of merit (R/Q, G, Q, Epk/Eacc, Bpk/Eacc, field
  flatness, cell-to-cell coupling) of the accelerating mode and of the dipole,
  quadrupole and higher-order modes, with open beam pipes for the external Q and
  dielectric or lossy inserts.
- [Tuning](https://dark-elektron.github.io/cavsim2d/tuning.html): adjust a dimension
  until the cavity resonates at a target frequency, tune the end cells of a multicell
  cavity for a flat field, or tune to a frequency and a figure of merit together.
- [Wakefields and impedance](https://dark-elektron.github.io/cavsim2d/wakefield.html):
  the wake potentials, loss and kick factors and impedance spectra a bunch leaves
  behind, computed with the ABCI code.
- [Frequency domain](https://dark-elektron.github.io/cavsim2d/frequency_domain.html):
  impedance, admittance and scattering matrices and the beam coupling impedance, with
  reduced models for fast sweeps.
- [Multipacting](https://dark-elektron.github.io/cavsim2d/multipacting.html): track
  electrons at the wall to find the field levels where an electron avalanche can
  build up.
- [Shape optimisation](https://dark-elektron.github.io/cavsim2d/optimisation.html):
  search for the best trade-offs between objectives such as the peak surface fields
  and R/Q.
- [Uncertainty quantification](https://dark-elektron.github.io/cavsim2d/uq.html):
  propagate manufacturing tolerances through any of the above.
- [Beam lines](https://dark-elektron.github.io/cavsim2d/beamlines.html):
  join pipes, tapers, bellows, absorbers and cavities into one structure with `+`.

## Cavity shapes

| Model | What it is | Parameters |
| --- | --- | --- |
| `EllipticalCavity` | Elliptical cells, single or multicell | `[A, B, a, b, Ri, L, Req]` per half-cell, in mm |
| `EllipticalCavityFlatTop` | Elliptical cells with a flat equator | the same, plus the flat length `l` |
| `Pillbox` | Cylindrical cells | `[L, Req, Ri, S, L_bp]`, in mm |
| `SplineCavity` | A free-form wall through Bézier control points | `p0` to `p5`, each `[z, r]` in mm |
| `RFGun` | A photoinjector or VHF gun | named segment lengths, radii and angles, in metres and radians |
| `Beampipe`, `Taper`, `Bellows`, `BLA` | Beam-line elements, joined with `+` | in mm |

Every model runs every analysis. To add a shape of your own, subclass `Cavity` and
describe its wall once; see [Writing a new cavity
model](https://dark-elektron.github.io/cavsim2d/extending.html). The parameters are
explained, with figures, in the [geometry
guide](https://dark-elektron.github.io/cavsim2d/geometry.html).

## Installation

```bash
pip install cavsim2d
```

cavsim2d needs Python 3.10 or newer. NGSolve, the finite-element library it solves
with, and gmsh are installed with it. To run the example notebooks, install the
Jupyter extra as well:

```bash
pip install "cavsim2d[jupyter]"
```

Wakefield analyses use ABCI, a free code from KEK that cavsim2d may not
redistribute: download it from the [ABCI page](https://abci.kek.jp/abci.htm) and set
the `CAVSIM2D_ABCI` environment variable to the executable, as the [installation
guide](https://dark-elektron.github.io/cavsim2d/installation.html) describes.
Everything else works without it.

## A first simulation

Build one elliptical cell and solve for its modes. The examples are written for a
Jupyter notebook, where the plots appear inline.

```python
from cavsim2d import EllipticalCavity

# One elliptical cell: [A, B, a, b, Ri, L, Req] in mm
cell = [42, 42, 12, 19, 35, 57.7, 100.0]
cav = EllipticalCavity(1, cell, beampipe='none', name='my_cell')

cav.plot('geometry')                  # the cell's half cross-section

cav.eigenmode.run()                   # solve its monopole modes
qois = cav.eigenmode.qois             # figures of merit of the accelerating mode
print(f"{qois['freq [MHz]']:.1f} MHz, R/Q = {qois['R/Q [Ohm]']:.1f} Ohm, "
      f"G = {qois['G [Ohm]']:.1f} Ohm, Epk/Eacc = {qois['Epk/Eacc []']:.2f}")
```

```text
1349.6 MHz, R/Q = 113.5 Ohm, G = 279.0 Ohm, Epk/Eacc = 1.99
```

The cell resonates above 1300 MHz. Tune its equator radius `Req` to bring it onto
1300 MHz:

```python
cav.tune.run({'freqs': 1300, 'cell_type': {'mid-cell': 'Req'}})

tuned = cav.tuned                     # a new cavity, saved beside the original
tuned.eigenmode.run()
print(f"Req = {tuned.parameters['Req_m']:.2f} mm -> "
      f"{tuned.eigenmode.qois['freq [MHz]']:.1f} MHz")
```

```text
Req = 103.37 mm -> 1300.0 MHz
```

The results are saved in `./my_cell/`. In a later session, build the same cavity
and call `cav.load('my_cell')` to reopen them without solving again. In a notebook,
`cav.show_fields()` shows the field of the accelerating mode.

## Where to go next

- [Tutorials](https://dark-elektron.github.io/cavsim2d/tutorials/index.html): twelve
  notebooks that teach cavsim2d step by step, from a first cavity to optimisation,
  uncertainty quantification, beam lines and shapes of your own.
- The guide to each analysis linked above, each ending with how-to guides for specific
  tasks.
- [Concepts](https://dark-elektron.github.io/cavsim2d/concepts.html): how studies,
  project folders, configurations and results fit together.
- [Configuration reference](https://dark-elektron.github.io/cavsim2d/configuration.html):
  every setting of every analysis, with its default and unit.
- [API reference](https://dark-elektron.github.io/cavsim2d/api.html) and
  [glossary](https://dark-elektron.github.io/cavsim2d/glossary.html).
- [Runnable scripts](https://github.com/Dark-Elektron/cavsim2d/tree/master/examples):
  one script per analysis, writing results and plots to disk.

## Citing cavsim2d

If you use cavsim2d in published work, please cite it. The details are in
[CITATION.cff](https://github.com/Dark-Elektron/cavsim2d/blob/master/CITATION.cff),
and the "Cite this repository" button on the GitHub page gives them as BibTeX or
APA.

## Contributing

Bug reports and suggestions are welcome as [GitHub
issues](https://github.com/Dark-Elektron/cavsim2d/issues). To work on the code,
install it from a clone with the test tools and run the tests:

```bash
git clone https://github.com/Dark-Elektron/cavsim2d.git
cd cavsim2d
pip install -e ".[dev]"
pytest tests/ -q
```

Wakefield tests run only where ABCI is available. In a clone, a copy at
`cavsim2d/solvers/ABCI/ABCI.exe` is used when `CAVSIM2D_ABCI` is unset; that path is
ignored by git and never packaged. Changes are listed in the
[changelog](https://github.com/Dark-Elektron/cavsim2d/blob/master/CHANGELOG.md).

## License

MIT; see [LICENSE](https://github.com/Dark-Elektron/cavsim2d/blob/master/LICENSE).
