Robust tuning

An ordinary tune brings the nominal cavity to the target frequency. A real cavity differs from its drawing, so its frequency is spread around the nominal one. A robust tune brings the mean of that spread to the target instead. It is an ordinary tune with a uq_config: at every step the tuner solves the perturbed shapes the uq_config describes and tunes their mean frequency.

This guide tunes a flat-top elliptical cell to 790 MHz, first as usual and then robustly.

import os
import tempfile
import json

import pandas as pd

from cavsim2d import Study, EllipticalCavityFlatTop
from cavsim2d.utils.style import apply_style

apply_style()

1. An ordinary tune

A flat-top cell’s dimensions are [A, B, a, b, Ri, L, Req, l] in mm, l being the length of the flat section at the equator. Tune the equator radius Req to 790 MHz.

ft = [62.22, 66.13, 30.22, 23.11, 80, 93.5, 171.20, 20]
cavs = Study(os.path.join(tempfile.mkdtemp(), 'ft_tune'))
cavs.add_cavity([EllipticalCavityFlatTop(1, ft, ft, ft, beampipe='none')], ['FT'])

cavs.run_tune({
    'freqs': 790.0,
    'cell_type': {'mid-cell': 'Req'},
    'processes': 1,
    'eigenmode_config': {'boundary_conditions': 'mm', 'mesh_config': {'h': 8, 'p': 2}},
})
res = cavs.cavities_list[0].tune.qois['mid-cell']
print(f"tuned Req = {res['parameters']['Req_m']:.3f} mm at {res['FREQ']:.3f} MHz")
tuned Req = 172.377 mm at 790.000 MHz

2. A robust tune

Add a uq_config: the equator half-axes A and B vary by up to 0.5 mm either side of their nominal values. The call is otherwise the same.

cavs2 = Study(os.path.join(tempfile.mkdtemp(), 'ft_rtune'))
cav = EllipticalCavityFlatTop(1, ft, ft, ft, beampipe='none')
cavs2.add_cavity([cav], ['FT'])

cavs2.run_tune({
    'freqs': 790.0,
    'cell_type': {'mid-cell': 'Req'},
    'eigenmode_config': {'boundary_conditions': 'mm', 'mesh_config': {'h': 8, 'p': 2}},
    'uq_config': {
        'variables': ['A', 'B'],
        'delta': [0.5, 0.5],                           # mm
        'method': ['Quadrature', 'Stroud3'],
        'objectives': ['monopole:freq [MHz]'],
    },
});

3. The tuned frequency and its spread

The tuned Req is in cav.tune.qois, as for any tune. The statistics of the frequency over the perturbed shapes are saved in uq_tune_results.json in the tune’s folder, cav.tune.folder.

result = cav.tune.qois['mid-cell']
print(f"robust tune: Req = {result['parameters']['Req_m']:.3f} mm")

with open(os.path.join(cav.tune.folder, 'uq_tune_results.json')) as f:
    stats = next(iter(json.load(f).values()))
pd.DataFrame({q.split(':', 1)[-1]: {'mean': v['expe'][0], 'std': v['stdDev'][0]}
             for q, v in stats.items()}).T.round(4)
robust tune: Req = 172.376 mm
mean std
freq [MHz] 790.0 0.6532

The robust tune ends at Req = 172.376 mm, about 0.001 mm from the ordinary tune’s 172.377 mm. The mean frequency of the perturbed shapes is on target, 790.0 MHz, and their spread is 0.6532 MHz, one standard deviation. The two tunes agree this closely because over +/- 0.5 mm the frequency changes almost linearly with A and B, so the shapes above and below the nominal one balance out. A robust tune makes a difference where the response curves over the tolerance. The spread is what the tolerances leave: no choice of Req removes it.

See also: the tuning guide, the uncertainty quantification guide, and robust optimisation.