3. Is the mesh fine enough?¶
Every number the solver reports is computed on a mesh, so it carries a mesh error. Before you quote a figure of merit, check that a finer mesh no longer changes it. Here you do that for a 2-cell cavity, and find which settings each figure of merit needs.
In this tutorial you will:
Solve the same cavity on meshes of increasing accuracy.
Watch each figure of merit settle.
Put a number on how much each one still changes.
Let the solver refine the mesh by itself.
Before you start: work through An elliptical cavity. Running the whole notebook takes about a minute.
1. The two mesh settings¶
mesh_config controls the mesh with two numbers:
h, the largest element size, in millimetres. Smaller elements follow the field more closely, and there are more of them to solve for.p, the polynomial order of the elements. A higher order fits the field better inside each element, at a higher cost per element.
The defaults are h = 20 and p = 3. The cost of a solve is measured by its number
of unknowns, the degrees of freedom (DOFs).
The cavity is a 2-cell version of the TESLA cavity, so that the cell coupling kcc
and the field flatness ff exist.
import pandas as pd
from cavsim2d import EllipticalCavity
mid_cell = [42, 42, 12, 19, 35, 57.7, 103.353] # A, B, a, b, Ri, L, Req [mm]
end_cell_left = [40.34, 40.34, 10, 13.5, 39, 55.716, 103.353]
end_cell_right = [42, 42, 9, 12.8, 39, 56.815, 103.353]
FOMS = ['freq [MHz]', 'R/Q [Ohm]', 'G [Ohm]', 'Epk/Eacc []',
'Bpk/Eacc [mT/MV/m]', 'kcc [%]', 'ff [%]']
2. Solve on meshes of increasing order¶
Keep h = 16 mm and raise p from 2 to 6. Each mesh gets its own cavity, and so
its own results folder, so the runs do not overwrite one another.
rows = {}
for p in [2, 3, 4, 5, 6]:
cav = EllipticalCavity(2, mid_cell, end_cell_left, end_cell_right,
beampipe='both', name=f'two_cell_p{p}')
cav.eigenmode.run({'mesh_config': {'h': 16, 'p': p}})
q = cav.eigenmode.qois
rows[p] = {'DOFs': q['No of DOFs'], **{key: q[key] for key in FOMS}}
table = pd.DataFrame(rows).T
table.index.name = 'p'
table.round(4)
| DOFs | freq [MHz] | R/Q [Ohm] | G [Ohm] | Epk/Eacc [] | Bpk/Eacc [mT/MV/m] | kcc [%] | ff [%] | |
|---|---|---|---|---|---|---|---|---|
| p | ||||||||
| 2 | 2640.0 | 1300.0880 | 218.9206 | 270.4541 | 2.0823 | 4.5030 | 0.9491 | 99.8414 |
| 3 | 4860.0 | 1300.0854 | 218.8798 | 272.4965 | 2.0227 | 4.2569 | 0.9491 | 99.8368 |
| 4 | 7750.0 | 1300.0857 | 218.8807 | 272.5233 | 2.0224 | 4.2568 | 0.9491 | 99.8360 |
| 5 | 11310.0 | 1300.0857 | 218.8807 | 272.5223 | 2.0224 | 4.2564 | 0.9491 | 99.8358 |
| 6 | 15540.0 | 1300.0857 | 218.8807 | 272.5220 | 2.0223 | 4.2563 | 0.9491 | 99.8358 |
Read down each column. The frequency, R/Q and kcc barely move from one order to
the next. G and the two peak-field ratios move the most: at p = 2, Epk/Eacc is
2.082 and Bpk/Eacc 4.503, against 2.022 and 4.256 from p = 3 on. The cost rises
with the order, from 2640 DOFs at p = 2 to 15540 at p = 6.
3. How much is still changing?¶
The change from one order to the next, as a percentage, puts a number on it. A figure of merit has settled when the change is far below the accuracy you need.
change = table[FOMS].pct_change().abs() * 100 # percent change from the row above
change.iloc[1:].map(lambda x: f'{x:.1e}')
| freq [MHz] | R/Q [Ohm] | G [Ohm] | Epk/Eacc [] | Bpk/Eacc [mT/MV/m] | kcc [%] | ff [%] | |
|---|---|---|---|---|---|---|---|
| p | |||||||
| 3 | 2.0e-04 | 1.9e-02 | 7.6e-01 | 2.9e+00 | 5.5e+00 | 1.3e-03 | 4.6e-03 |
| 4 | 2.4e-05 | 4.1e-04 | 9.8e-03 | 1.2e-02 | 2.8e-03 | 2.3e-04 | 8.2e-04 |
| 5 | 2.7e-06 | 8.6e-06 | 3.5e-04 | 3.1e-03 | 1.0e-02 | 1.2e-05 | 1.5e-04 |
| 6 | 3.4e-07 | 9.5e-06 | 1.3e-04 | 9.5e-04 | 1.7e-03 | 1.3e-07 | 2.5e-05 |
Each row is the change from the order above it. The large steps all come between
p = 2 and p = 3: 2.9 % for Epk/Eacc, 5.5 % for Bpk/Eacc and 0.76 % for G.
From p = 3 to p = 4 nothing changes by more than 0.012 %, so at this element
size p = 3 is already within about 0.01 % of the answer on every figure of merit.
The peak fields settle less smoothly than the rest. Bpk/Eacc changes by 0.003 %
from p = 3 to p = 4, then by 0.010 % from p = 4 to p = 5. A peak field is
read at one point on the wall, and a new mesh moves that point slightly, so two
meshes that happen to agree are weak evidence. Look for a run of meshes that agree.
4. Let the solver refine the mesh¶
'adaptive': True makes the solver estimate its own error on every element and
refine where the error is largest, until the error is small or the mesh is as big as
allowed. The result is reported on the finest mesh, like any other run.
cav = EllipticalCavity(2, mid_cell, end_cell_left, end_cell_right,
beampipe='both', name='two_cell_adaptive')
cav.eigenmode.run({'mesh_config': {'h': 20, 'p': 3, 'adaptive': True}})
cav.eigenmode.plot_convergence();
The left panel is the solver’s own error estimate for each of the four modes it reports, which falls steadily as it refines. The right panel shows their frequencies, which barely move: on this cavity the first mesh already had them right. The refinement stopped after 8 meshes, when the mesh reached the default size limit, with the error of the two modes of the accelerating passband, 0 and 1, near \(10^{-10}\).
q = cav.eigenmode.qois
print(len(cav.eigenmode.adaptive_history()), 'meshes')
pd.DataFrame({'adaptive': {key: q[key] for key in FOMS},
'p = 6': table.loc[6, FOMS]}).round(4)
8 meshes
| adaptive | p = 6 | |
|---|---|---|
| freq [MHz] | 1300.0857 | 1300.0857 |
| R/Q [Ohm] | 218.8806 | 218.8807 |
| G [Ohm] | 272.5206 | 272.5220 |
| Epk/Eacc [] | 2.0224 | 2.0223 |
| Bpk/Eacc [mT/MV/m] | 4.2563 | 4.2563 |
| kcc [%] | 0.9491 | 0.9491 |
| ff [%] | 99.8362 | 99.8358 |
The adaptive solve agrees with the p = 6 solve to within 0.005 % on every figure
of merit. Both routes give the same answer; the adaptive one does not need you to
choose the mesh.
On a cavity of your own, solve at two or three orders, or adaptively, and quote a figure of merit only once it has stopped changing by more than you can accept. The convergence study of every figure of merit takes this further, for the dipole modes too.
What you learned¶
A figure of merit can be trusted once a finer mesh no longer changes it; check the quantity you are going to report, not only the frequency.
mesh_configsets the element sizehand the polynomial orderp; on this cavity,p = 3ath = 16mm was already within about 0.01 % of the finest solve.The peak-field ratios are the slowest to settle.
'adaptive': Truerefines the mesh where the solver’s own error estimate is largest.
Next¶
Tune a cell to a target frequency: change a dimension until the cavity resonates where you need it.