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:
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.profile(): the outline, as aProfilewalked 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. Readself.parametersinsideprofile(): the tuner changes them while it works, so the outline must follow. ReturnNonefor a set of dimensions that does not make a valid shape, and the analysis reports it instead of meshing it.rebuild(parameters, beampipe=None): a new cavity of your class, built from a dictionary with the same keys asself.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 = Trueand implementhalf_cells()andset_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_valueandset_tune_valueto name its parts ('p2_r', for example); seeSplineCavity.A
.geofile. A shape that cannot be written as aProfilecan overridecreate()to write a gmsh.geofile and pointself.geo_filepathat it. Such a cavity cannot carry dielectric regions.