Solver settings: time against accuracy

The mesh decides how close the discretised cavity is to the real one; the previous two examples measure that. The eigensolver’s settings decide how close the answer is to the discretised cavity, and how long it takes to get there. This example measures what each setting costs and buys, so you can trade accuracy for speed knowingly.

cav.eigenmode.benchmark does the measuring. Every point is a complete simulation (mesh, solve and every figure of merit), repeated five times, and scored against a solve of the same mesh converged far beyond any setting being compared. The errors here are therefore the solver’s alone. Times are the mean and standard deviation over the repeats. Errors vary over orders of magnitude from run to run, so each is the mean of its logarithm, drawn with a spread of \(10^{\mu\pm\sigma}\).

The settings compared:

setting

what it does

pinvit_tol

the eigensolver stops once every checked mode’s relative residual is below it (default 1e-8)

pinvit_converge_modes

how many of the lowest modes must converge (default: all n_modes)

pinvit_padding

extra vectors the eigensolver iterates on beyond n_modes (default: twice n_modes, at most 30)

preconditioner

'direct' (default), an exact factorisation, or 'bddc', an approximate one

direct_solver

the sparse direct solver for every factorisation (default 'sparsecholesky')

The timings are from the machine that built these pages. Run the notebook on yours to get your own; it is also the way to check a new setting, which is one more entry in variants.

import os
import tempfile

import matplotlib.pyplot as plt

from cavsim2d import EllipticalCavity
from cavsim2d.utils.printing import set_verbose
from cavsim2d.utils.style import apply_style

set_verbose(False)
apply_style()
WORK = tempfile.mkdtemp()

MID = [42, 42, 12, 19, 35, 57.652, 103.3536]      # TESLA mid-cell: A, B, a, b, Ri, L, Req [mm]
CONFIG = {'boundary_conditions': 'mm', 'mesh_config': {'h': 20, 'p': 3}}   # the defaults
TOLERANCES = [1e-4, 1e-6, 1e-8, 1e-10]


def tesla(n_cells):
    cav = EllipticalCavity(n_cells, MID, MID, MID, beampipe='both', name=f'tesla_{n_cells}cell')
    cav.set_workspace(os.path.join(WORK, cav.name))
    return cav


def show(table):
    '''Round a benchmark table for reading.'''
    fmt = {c: '{:.1e}' for c in table.columns}
    fmt.update({'time per simulation [s]': '{:.2f}', 'std [s]': '{:.2f}',
                'solve [s]': '{:.2f}', 'PINVIT iterations': '{:.0f}'})
    return table.style.format(fmt).format_index(
        lambda v: f'{v:.0e}' if isinstance(v, float) else v)

1. Tolerance and preconditioner on a single cell

A TESLA mid-cell with beam pipes, on the default mesh. The direct and BDDC preconditioners are each run at four tolerances.

cell = tesla(1)
cell.eigenmode.benchmark(variants={'direct': {}, 'bddc': {'preconditioner': 'bddc'}},
                         sweep={'pinvit_tol': TOLERANCES}, n_repeats=5,
                         eigenmode_config=CONFIG)
show(cell.eigenmode.benchmark_table())
    time per simulation [s] std [s] solve [s] PINVIT iterations freq (mode of interest) freq (passband) freq (all modes) R/Q Epk/Eacc Bpk/Eacc G
variant pinvit_tol                      
direct 1e-04 0.26 0.04 0.19 7 2.4e-15 2.4e-15 1.5e-09 5.0e-05 3.4e-05 2.7e-05 1.4e-05
1e-06 0.25 0.03 0.18 11 3.4e-15 3.4e-15 1.9e-13 5.8e-07 1.6e-07 1.9e-07 1.1e-07
1e-08 0.27 0.03 0.20 14 2.2e-15 2.2e-15 1.2e-13 1.6e-09 3.4e-10 8.9e-10 3.2e-10
1e-10 0.28 0.02 0.21 17 3.8e-15 3.8e-15 9.9e-14 3.1e-12 1.5e-11 2.5e-11 3.3e-11
bddc 1e-04 0.41 0.07 0.34 40 4.8e-12 4.8e-12 3.4e-09 6.6e-05 3.5e-05 7.8e-05 9.6e-06
1e-06 0.56 0.12 0.49 63 1.0e-14 1.0e-14 6.2e-13 1.4e-06 2.0e-07 3.1e-07 1.3e-07
1e-08 0.57 0.07 0.50 86 1.0e-14 1.0e-14 4.1e-13 5.3e-09 3.4e-09 3.1e-09 9.2e-10
1e-10 0.70 0.08 0.63 108 1.0e-14 1.0e-14 4.0e-13 9.3e-11 6.6e-11 5.5e-11 3.3e-11

Each column is a worst relative error over the modes reported: the frequency of the mode of interest (the accelerating mode), of the passband (the lowest n_cells modes) and of every reported mode, then R/Q, Epk/Eacc and Bpk/Eacc over the modes that carry a voltage, and G over every mode.

cell.eigenmode.plot_benchmark()
plt.show()
../../_images/f612536c32621094336ad17f5602e2ccbe51cd6f73366ad3ff927de30464b6d6.png

On a single cell the direct preconditioner is the faster of the two at every tolerance: 0.25 to 0.28 s per simulation against 0.41 to 0.70 s for BDDC. BDDC needs six times as many iterations (40 to 108 against 7 to 17), and each one is not correspondingly cheaper. Direct iterations are so cheap here that tightening the tolerance from \(10^{-4}\) to \(10^{-10}\) costs almost nothing: meshing and the figures of merit take most of the time.

The tolerance buys accuracy in the fields, close to one decade for one decade. At the default 1e-8 every figure of merit is within \(2\times10^{-9}\) of the reference; at 1e-4, R/Q is within \(5\times10^{-5}\). The frequencies converge far faster than the fields: at 1e-4 the accelerating mode’s frequency is already exact to round-off.

2. Nine cells: a passband, and the modes above it

A 9-cell reports n_cells + 2 = 11 modes by default: the nine of the accelerating passband and the lowest two of the next band. The eigensolver converges a group of closely spaced modes more slowly, and those two, at the bottom of a dense band, slowest of all.

nine = tesla(9)
nine.eigenmode.benchmark(variants={'direct': {}, 'bddc': {'preconditioner': 'bddc'}},
                         sweep={'pinvit_tol': TOLERANCES}, n_repeats=5,
                         eigenmode_config=CONFIG)
show(nine.eigenmode.benchmark_table())
    time per simulation [s] std [s] solve [s] PINVIT iterations freq (mode of interest) freq (passband) freq (all modes) R/Q Epk/Eacc Bpk/Eacc G ff
variant pinvit_tol                        
direct 1e-04 3.03 0.21 1.88 15 5.2e-14 1.2e-13 7.4e-09 1.1e-10 8.3e-11 8.3e-11 3.7e-06 3.3e-11
1e-06 3.60 0.16 2.45 24 5.8e-14 1.1e-13 7.7e-13 1.1e-10 8.3e-11 8.4e-11 7.1e-08 4.0e-11
1e-08 4.29 0.27 3.14 31 6.0e-14 1.2e-13 1.2e-13 9.7e-11 7.8e-11 7.9e-11 3.1e-10 4.7e-11
1e-10 5.17 0.33 4.03 40 4.5e-14 1.0e-13 1.0e-13 1.0e-10 8.0e-11 8.0e-11 3.4e-12 3.6e-11
bddc 1e-04 9.12 1.35 7.99 89 3.4e-14 1.4e-13 8.8e-09 1.1e-10 7.8e-11 7.6e-11 4.2e-06 4.7e-11
1e-06 13.66 0.63 12.54 129 1.8e-14 1.4e-13 7.9e-13 1.2e-10 9.2e-11 9.2e-11 5.7e-08 2.9e-11
1e-08 16.23 0.43 15.14 156 2.3e-14 1.6e-13 1.6e-13 1.1e-10 8.5e-11 8.5e-11 9.2e-10 4.2e-11
1e-10 19.11 0.63 17.98 185 1.3e-14 1.5e-13 1.5e-13 1.1e-10 8.4e-11 8.4e-11 6.8e-12 3.9e-11
nine.eigenmode.plot_benchmark(errors=['freq (passband)', 'freq (all modes)', 'R/Q', 'G'])
plt.show()
../../_images/df27947a02a1a5301e37b52ba799a9c416c74a2cf4bc72c58437594862eb6458.png

The frequency error of every mode, for the direct preconditioner at each tolerance: modes 1 to 9 are the passband, 10 and 11 the next band.

bench = nine.eigenmode.benchmark_df
nine.eigenmode.plot_benchmark(bench[bench['variant'] == 'direct'], kind='modes')
plt.show()
../../_images/c290c995fcc6fae5da8bd7f97edc4def30f089d07f67f5f3d9af6bf5f16b6e1f.png

On nine cells the solve takes most of the time, and both settings show. Direct takes 3.0 s at 1e-4 and 4.3 s at the default 1e-8. BDDC takes three to four times as long at every tolerance, 9.1 to 19.1 s, with five times the iterations.

Most of those iterations go to the two modes above the passband. The per-mode plot shows it: the nine passband modes are converged to round-off at every tolerance, 1e-4 included, while modes 10 and 11 follow the tolerance. So the passband’s figures of merit are converged to about \(10^{-10}\) whatever the tolerance, and only the all-modes frequency and G, which is scored over every reported mode, respond to it.

3. Converging the passband only

If only the passband will be used, pinvit_converge_modes=9 stops the solve once those nine have converged. The two modes above are still reported, but not converged.

nine.eigenmode.benchmark(variants={'every mode (default)': {},
                                   'passband only': {'pinvit_converge_modes': 9}},
                         sweep={'pinvit_tol': [1e-6, 1e-8]}, n_repeats=5,
                         eigenmode_config=CONFIG)
show(nine.eigenmode.benchmark_table())
    time per simulation [s] std [s] solve [s] PINVIT iterations freq (mode of interest) freq (passband) freq (all modes) R/Q Epk/Eacc Bpk/Eacc G ff
variant pinvit_tol                        
every mode (default) 1e-06 3.95 0.13 2.84 23 2.0e-14 6.9e-14 5.6e-13 1.9e-11 1.3e-11 1.3e-11 3.1e-08 3.2e-11
1e-08 5.03 0.14 3.91 32 2.8e-14 7.3e-14 7.3e-14 2.4e-11 1.4e-11 1.4e-11 2.9e-10 3.2e-11
passband only 1e-06 2.24 0.11 1.13 7 5.1e-14 7.4e-14 8.0e-05 9.7e-07 4.9e-07 4.6e-07 3.2e-04 4.1e-07
1e-08 2.30 0.11 1.19 9 2.8e-14 5.8e-14 5.0e-06 1.1e-08 5.6e-09 6.0e-09 8.6e-05 5.2e-09

Converging the passband alone takes 7 to 9 iterations and 2.3 s per simulation, instead of 4.0 to 5.0 s: the solve itself is about three times faster, and the passband is converged to the tolerance asked for (R/Q to \(10^{-6}\) at 1e-6, \(10^{-8}\) at 1e-8). The price is the two modes above. Nothing converged them, so their frequencies are up to \(10^{-4}\) off and their G up to \(3\times10^{-4}\). Use it when those modes will not be read. Setting n_modes=n_cells does the same without reporting them at all.

4. Padding

The eigensolver iterates on more vectors than it reports. The extra ones, pinvit_padding, are never returned, but they decide how fast the highest requested modes converge: those converge at a rate set by the gap to the first eigenvalue outside the block of vectors. The default is twice n_modes, at most 30: 6 for the single cell and 22 for nine cells.

for cav in (cell, nine):
    cav.eigenmode.benchmark(sweep={'pinvit_padding': [2, 6, 11, 22, 30]}, n_repeats=5,
                            eigenmode_config=CONFIG)
    display(show(cav.eigenmode.benchmark_table()))
    cav.eigenmode.plot_benchmark(kind='time')
    plt.show()
    time per simulation [s] std [s] solve [s] PINVIT iterations freq (mode of interest) freq (passband) freq (all modes) R/Q Epk/Eacc Bpk/Eacc G
variant pinvit_padding                      
default 2 0.27 0.06 0.19 18 5.5e-15 5.5e-15 1.5e-13 8.4e-10 5.0e-10 1.8e-10 7.4e-10
6 0.28 0.02 0.20 14 7.5e-15 7.5e-15 1.4e-13 1.3e-09 7.2e-10 6.1e-10 9.4e-10
11 0.34 0.04 0.26 11 8.9e-15 8.9e-15 7.1e-14 1.1e-09 1.5e-09 1.8e-09 8.3e-10
22 0.38 0.07 0.31 9 9.4e-15 9.4e-15 2.3e-13 4.0e-10 4.5e-10 3.1e-10 1.0e-09
30 0.50 0.03 0.42 8 1.2e-14 1.2e-14 1.5e-13 9.3e-10 3.7e-10 6.6e-10 1.1e-09
../../_images/743986cf2b426bf93a7629d13839fb17d3eb503919afc44e2941e5a5e1413b6f.png
    time per simulation [s] std [s] solve [s] PINVIT iterations freq (mode of interest) freq (passband) freq (all modes) R/Q Epk/Eacc Bpk/Eacc G ff
variant pinvit_padding                        
default 2 9.89 1.27 8.79 209 4.0e-15 1.7e-13 3.7e-12 1.9e-11 8.2e-12 8.6e-12 2.8e-10 2.3e-11
6 6.83 0.60 5.73 101 2.2e-14 1.8e-13 5.2e-12 4.1e-11 2.1e-11 2.5e-11 3.3e-10 3.9e-11
11 6.96 0.52 5.86 78 1.5e-14 1.8e-13 5.6e-12 2.8e-11 2.0e-11 1.5e-11 4.8e-10 3.3e-11
22 4.72 0.46 3.61 32 2.3e-14 1.8e-13 5.7e-12 2.2e-11 1.7e-11 1.4e-11 5.1e-10 3.2e-11
30 3.88 0.12 2.75 16 1.9e-14 1.8e-13 5.7e-12 1.5e-11 7.8e-12 1.0e-11 8.3e-10 2.5e-11
../../_images/9a865203e4d88a30b7256d946c7cff206ea905e2c6cb2e3d0ca412783700a1c9.png

On the single cell padding changes little: 0.27 to 0.28 s up to 6 extra vectors, rising to 0.5 s at 30 as every iteration carries more vectors. On nine cells it is decisive: 209 iterations and 9.9 s with 2 extra vectors, 32 and 4.7 s at the default 22, 16 and 3.9 s at 30. The two modes above the passband open the next band. With few extra vectors the first eigenvalue outside the block sits inside that band, so the gap that sets their convergence is small; with 22 or more the block reaches past it. The default is close to the best on a multicell cavity and nearly free on a single cell. pinvit_padding sets it directly.

5. The sparse direct solver

Which sparse direct solvers are available depends on how NGSolve was built: sparsecholesky always is, pardiso and umfpack depend on the build. A variant asking for one this build lacks is skipped with a warning.

backends = {'sparsecholesky': {}, 'pardiso': {'direct_solver': 'pardiso'},
            'umfpack': {'direct_solver': 'umfpack'}}
for cav in (cell, nine):
    cav.eigenmode.benchmark(variants=backends, n_repeats=3, eigenmode_config=CONFIG)
    display(show(cav.eigenmode.benchmark_table()))
ipykernel_39804\2965386525.py:4: UserWarning: benchmark: skipping 'umfpack', this NGSolve build has no 'umfpack' direct solver.
  cav.eigenmode.benchmark(variants=backends, n_repeats=3, eigenmode_config=CONFIG)
  time per simulation [s] std [s] solve [s] PINVIT iterations freq (mode of interest) freq (passband) freq (all modes) R/Q Epk/Eacc Bpk/Eacc G
variant                      
sparsecholesky 0.30 0.03 0.22 14 2.0e-15 2.0e-15 1.1e-13 1.1e-09 1.2e-09 2.4e-09 4.6e-10
pardiso 4.10 0.03 4.03 14 4.7e-15 4.7e-15 7.9e-14 1.8e-09 1.8e-09 5.6e-09 1.4e-09
ipykernel_39804\2965386525.py:4: UserWarning: benchmark: skipping 'umfpack', this NGSolve build has no 'umfpack' direct solver.
  cav.eigenmode.benchmark(variants=backends, n_repeats=3, eigenmode_config=CONFIG)
  time per simulation [s] std [s] solve [s] PINVIT iterations freq (mode of interest) freq (passband) freq (all modes) R/Q Epk/Eacc Bpk/Eacc G ff
variant                        
sparsecholesky 4.50 0.43 3.36 32 2.3e-14 7.3e-14 7.3e-14 3.8e-11 2.6e-11 2.8e-11 3.4e-10 4.4e-11
pardiso 36.94 1.21 35.88 31 1.1e-15 7.7e-14 8.1e-14 2.9e-11 1.8e-11 1.5e-11 4.0e-10 3.9e-11

Both backends give the same answer, and sparsecholesky is the faster: 14 times on the single cell (0.30 s against 4.1 s) and 8 times on nine cells (4.5 s against 37 s). This build has no umfpack, so that variant was skipped. On a build that has it, the same cell compares all three.

6. What to use

  • The defaults (the direct preconditioner, sparsecholesky, pinvit_tol=1e-8, padding of twice n_modes) keep the solver’s error far below any practical mesh’s: figures of merit to about \(10^{-9}\), frequencies to round-off.

  • BDDC did not win anywhere here. It was 1.6 to 3.8 times slower, on both cavities and at every tolerance.

  • The tolerance is a free knob on small problems and a real one on large ones. On nine cells 1e-4 takes 3.0 s against the default’s 4.3 s and still leaves the passband at round-off, with G at \(4\times10^{-6}\) and the modes above the passband at \(10^{-8}\).

  • On a multicell cavity the largest saving is to converge only the modes you will use: pinvit_converge_modes=n_cells, or n_modes=n_cells.

  • Keep direct_solver='sparsecholesky' unless a benchmark on your own machine says otherwise.