12. Your own cavity shape

Every cavity shape in cavsim2d is a class built on one base class, Cavity. A shape of your own needs three things: its dimensions, a description of its wall, and a way to rebuild itself with new dimensions. Every analysis then works on it as it does on the built-in shapes.

In this tutorial you will:

  1. Describe a cone-shaped cell as a new class.

  2. Build it and look at it.

  3. Solve its modes.

  4. Tune it to a target frequency.

Before you start: work through Tune a cell to a target frequency. Running the whole notebook takes under a minute.

1. Describe the shape

The cell here is a cone: its wall runs straight from the iris up to the equator, and back down. The class supplies three things:

  • self.parameters: the dimensions, in millimetres. Every entry that is a single number can be tuned, varied in uncertainty quantification and optimised, by its name.

  • profile(): the wall, as a Profile, walked from the axis in metres. Each segment carries a label: 'PEC' for the metal wall, 'PMC' for an open plane such as the opening at an iris. close('AXI') closes the outline along the axis.

  • rebuild(parameters): a new cavity of this class with other dimensions. The tuner, sweeps, uncertainty quantification and optimisation all make their variants with it.

from cavsim2d.geometry import Profile
from cavsim2d.models.base import Cavity


class ConeCavity(Cavity):
    '''One cell whose wall runs straight from the iris to the equator and back.
    dims = [L, Req, Ri]: length, equator radius and iris radius, in mm.'''

    def __init__(self, dims, name='cone'):
        super().__init__(name=name)
        L, Req, Ri = dims
        self.parameters = {'L': float(L), 'Req': float(Req), 'Ri': float(Ri)}

    def profile(self):
        L, Req, Ri = (self.parameters[k] * 1e-3 for k in ('L', 'Req', 'Ri'))   # m
        p = Profile('cone')
        p.start(-L / 2, 0.0)              # on the axis
        p.line_to(-L / 2, Ri, 'PMC')      # up the left iris plane
        p.line_to(0.0, Req, 'PEC')        # the metal wall, up to the equator
        p.line_to(L / 2, Ri, 'PEC')       # and down to the right iris
        p.line_to(L / 2, 0.0, 'PMC')      # down the right iris plane
        p.close('AXI')                    # back along the axis
        return p

    def rebuild(self, parameters, beampipe=None):
        return ConeCavity([parameters[k] for k in ('L', 'Req', 'Ri')], name=self.name)

2. Build it and look at it

cone = ConeCavity([100, 100, 20])        # L = 100 mm, Req = 100 mm, Ri = 20 mm
cone.plot('geometry');
../_images/113b70aebdbdd9a94f19d8a5164e1238126289856ba1c3bf866c1f1598cf375d.png

3. Solve its modes

The solver meshes profile() directly and reads the 'PEC' and 'PMC' labels as the boundary conditions, so nothing in the analysis is specific to the cone.

cone.eigenmode.run()
q = cone.eigenmode.qois
for key in ['freq [MHz]', 'R/Q [Ohm]', 'G [Ohm]']:
    print(f'{key:12s} {q[key]:8.3f}')
freq [MHz]   1598.802
R/Q [Ohm]     117.111
G [Ohm]       197.952

The cone resonates at 1598.8 MHz, with an R/Q of 117.1 Ohm and a geometry factor of 198.0 Ohm. Its peak-field ratios are left out on purpose: the cone’s irises are sharp edges, where the field keeps growing as the mesh is refined (Tutorial 3), so their values would describe the mesh rather than the cone.

4. Tune it

The tuner changes Req through rebuild(), exactly as it does for the built-in shapes. A cone has no separate inner and end cells, so 'mid-cell' simply names the cell.

cone.tune.run({'freqs': 1500, 'cell_type': {'mid-cell': 'Req'}});

cone.tuned.eigenmode.run()
print(f"Req: 100.000 -> {cone.tuned.parameters['Req']:.3f} mm, "
      f"frequency {cone.tuned.eigenmode.qois['freq [MHz]']:.3f} MHz")
Req: 100.000 -> 107.136 mm, frequency 1500.000 MHz

To bring the cone down to 1500 MHz, the tuner widened its equator radius from 100 to 107.136 mm, and the tuned cone resonates at 1500.000 MHz.

Wakefields, multipacting, uncertainty quantification and optimisation work on the cone in the same way. The guide to writing a cavity model covers the rest of the Cavity interface, and the curved custom cavity builds a wall from an arc.

What you learned

  • A new shape is a subclass of Cavity with parameters, profile() and rebuild().

  • profile() walks the wall in metres and labels each segment 'PEC' (metal) or 'PMC' (open).

  • Every scalar in parameters can be tuned, swept, varied and optimised by name.

Next

That completes the tutorials. The user guide covers each analysis in depth, with how-to guides for specific tasks, and the configuration reference lists every setting.