Writing a new cavity model

A shape of your own is a subclass of Cavity, and every analysis (eigenmode, tuning, wakefield, multipacting, uncertainty quantification and optimisation) works on it with no changes anywhere else. Your own cavity shape builds one step by step; this page is the reference.

The idea is the one from Geometry: the model describes the cavity’s outline once, as a labelled chain of wall segments, and every analysis reads that outline. The settings of each analysis (mesh size, bunch length, launch phases, …) live in its configuration, so a model never needs to know which analysis will use it.

What a model provides

Three things:

  1. self.parameters: a dictionary of the shape’s dimensions, in millimetres. Every entry that is a single number is automatically a variable you can tune, vary in uncertainty quantification and optimise, by its name.

  2. profile(): the outline, as a Profile walked from the axis, in metres. Each segment carries a label: 'PEC' for the metal wall, 'PMC' for an opening or a symmetry plane, and 'AXI' for the axis. Read self.parameters inside profile(): the tuner changes them while it works, so the outline must follow. Return None for a set of dimensions that does not make a valid shape, and the analysis reports it instead of meshing it.

  3. rebuild(parameters, beampipe=None): a new cavity of your class, built from a dictionary with the same keys as self.parameters. Tuning, sweeps, uncertainty quantification and optimisation all make their variants through it.

The base class does the rest: it creates the cavity’s folders when the cavity joins a study or gets a workspace, records the target frequency during a tune, and reads and writes the variables through tune_variables(), get_tune_value() and set_tune_value().

A complete example

A one-cell cavity whose wall runs straight from the opening up to the equator and back down, with three dimensions:

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)

That is the whole model, and it behaves like any built-in shape:

from cavsim2d import Study

cone = ConeCavity([100, 100, 20])         # L = Req = 100 mm, Ri = 20 mm
study = Study('cones')
study.add_cavity(cone, names='cone')

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

Because the outline labels its wall 'PEC' and its openings 'PMC', the cone also runs through the wakefield and multipacting analyses unchanged.

The Profile API

A Profile is built by walking the outline from the axis, in metres:

p = Profile('my-cavity')
p.start(z0, 0.0)                                       # begin on the axis
p.line_to(z, r, 'PMC')                                 # a straight segment
p.arc_to(z, r, through, 'PEC')                         # a circular arc through a point
p.circle_arc_to(z, r, center, 'PEC')                   # a circular arc about a centre
p.ellipse_arc_to(z, r, center, semi_z, semi_r, 'PEC')  # an elliptical arc
p.spline_to(poles, 'PEC', kind='bspline')              # a spline shaped by control points
p.close('AXI')                                         # back along the axis

Each call draws from the current point to (z, r). Curved segments are kept exact in the mesh, rather than broken into straight pieces.

Optional extras

  • Cells. Set uses_cell_suffixes = True and implement half_cells() and set_half_cells() only if your shape is built from cells whose dimensions can differ, as the elliptical cavities are; this enables multicell tuning and multicell uncertainty quantification. Most shapes do not need it.

  • Dimensions that are not single numbers. If a dimension is a coordinate pair, as the spline’s control points are, override expand_variable, get_tune_value and set_tune_value to name its parts ('p2_r', for example); see SplineCavity.

  • A .geo file. A shape that cannot be written as a Profile can override create() to write a gmsh .geo file and point self.geo_filepath at it. Such a cavity cannot carry dielectric regions.

How-to guides