Eigenmodes from reduced and joined models¶
The frequency-domain solver works with the same finite element matrices as the
eigenmode solver. Its resonances with every port left open are the eigenmode
solver’s resonances with magnetic-wall pipe ends, boundary_conditions='mm'.
A reduced model keeps these resonances in a system of a few hundred unknowns,
and reduced models of separate elements can be joined through their port
modes. Each element is then solved only once, however often it repeats.
This example compares the resonances of a two-cavity module, computed four ways:
the eigenmode solver on the whole module;
a reduced model of the whole module;
the two cavities solved separately, as one element solved once, and joined;
with the pipe ends terminated, from the joined model and from the eigenmode solver with open (PML) ends, for the loaded quality factors.
import os
import tempfile
import time
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from cavsim2d import Assembly, EllipticalCavity
from cavsim2d.utils.style import apply_style
apply_style()
pd.set_option('display.max_columns', 20)
pd.set_option('display.width', 180)
workspace = tempfile.mkdtemp()
TESLA_MID = [42, 42, 12, 19, 35, 57.652, 103.3536] # A, B, a, b, Ri, L, Req [mm]
MESH = {'h': 10, 'p': 3}
BAND = (1000, 3000) # MHz
def module(name):
"""Two TESLA mid-cells, each with 30 mm of pipe on either side."""
cells = []
for label in ('cavity_a', 'cavity_b'):
cell = EllipticalCavity(1, TESLA_MID, TESLA_MID, TESLA_MID, beampipe='both',
name=label)
cell.beampipe_length = 0.03 # [m]
cells.append(cell)
mod = Assembly(cells, name=name)
mod.set_workspace(os.path.join(workspace, name))
return mod
def rq_column(df, m):
"""The R/Q column of order *m*: the linac R/Q for the monopole, the transverse
R/Q_t otherwise."""
stem = 'R/Q [' if m == 0 else 'R/Q_t'
return next(c for c in df.columns if c.startswith(stem))
1. The module¶
Two identical TESLA mid-cells, each with 30 mm of beam pipe on either side,
joined into one Assembly. The 60 mm drift between them couples the two
cavities. Every mode of a single cell therefore splits into a pair: one with
the cavities in phase and one in antiphase.
mod = module('module')
mod.plot('geometry');
2. The eigenmode solver¶
The monopole and the dipole of the whole module, with magnetic walls at the pipe ends.
t0 = time.time()
mod.eigenmode.run({'n_modes': 10, 'polarisation': [0, 1], 'boundary_conditions': 'mm',
'mesh_config': MESH})
print(f'eigenmode solver: {time.time() - t0:.0f} s')
modes = mod.eigenmode.qois_df
eig = {}
for m in (0, 1):
df = modes[modes['m'] == m].sort_values('freq [MHz]')
df = df[(df['freq [MHz]'] > BAND[0]) & (df['freq [MHz]'] < BAND[1])]
eig[m] = pd.DataFrame({'f [MHz]': df['freq [MHz]'].to_numpy(),
rq_column(df, m): df[rq_column(df, m)].to_numpy()})
eig[0]
eigenmode solver: 4 s
| f [MHz] | R/Q [Ohm] | |
|---|---|---|
| 0 | 1287.176344 | 1.199808e+02 |
| 1 | 1287.439972 | 1.274317e+02 |
| 2 | 2415.354604 | 7.649701e+01 |
| 3 | 2417.715363 | 6.989663e+00 |
| 4 | 2500.914628 | 5.225789e-21 |
| 5 | 2500.922999 | 3.670949e-21 |
| 6 | 2713.627502 | 6.059618e-01 |
| 7 | 2718.422780 | 6.095178e-01 |
eig[1]
| f [MHz] | R/Q_t [Ohm/m^(2(m-1))] | |
|---|---|---|
| 0 | 1717.148238 | 23.190344 |
| 1 | 1726.884616 | 0.125371 |
| 2 | 1841.343341 | 2.859644 |
| 3 | 1847.716734 | 46.776905 |
| 4 | 2488.254597 | 7.205938 |
| 5 | 2518.932922 | 6.181404 |
| 6 | 2553.141598 | 0.007626 |
| 7 | 2696.232143 | 18.071519 |
| 8 | 2952.459481 | 5.295567 |
3. A reduced model of the whole module¶
cav.fds.run assembles the full-order model and sweeps it. reduce builds
the reduced model from solutions at 14 frequencies of the band, 1 GHz to 3 GHz.
Its resonances come from cav.fds.eigenmodes(source='rom'), with the R/Q of
each mode computed in the eigenmode solver’s conventions: the linac R/Q for the
monopole, and the transverse R/Q_t for the dipole.
glued = module('module_fd')
t0 = time.time()
glued.fds.run(frequencies=[*BAND, 41], polarisation=[0, 1], mesh_config=MESH,
reduce={'tol': 1e-9, 'n_snapshots': 14}, progress=False)
print(f'full-order sweep and reduction: {time.time() - t0:.0f} s')
print(glued.fds.rom_of(0))
print(glued.fds.rom_of(1))
rom = {m: glued.fds.eigenmodes(source='rom', polarisation=m, fmin=BAND[0], fmax=BAND[1])
for m in (0, 1)}
rom[0]
full-order sweep and reduction: 56 s
ReducedModel('module_fd (ROM)', m=0, rank 80, band 1006.3-2993.7 MHz, 12 port modes)
ReducedModel('module_fd (ROM)', m=1, rank 78, band 1006.3-2993.7 MHz, 12 port modes)
| mode | f [MHz] | R/Q [Ohm] | source | |
|---|---|---|---|---|
| 0 | 1 | 1287.176344 | 1.199808e+02 | rom |
| 1 | 2 | 1287.439972 | 1.274317e+02 | rom |
| 2 | 3 | 2415.354604 | 7.649702e+01 | rom |
| 3 | 4 | 2417.715363 | 6.989665e+00 | rom |
| 4 | 5 | 2500.914628 | 6.857471e-26 | rom |
| 5 | 6 | 2500.922999 | 1.175285e-26 | rom |
| 6 | 7 | 2713.627502 | 6.059619e-01 | rom |
| 7 | 8 | 2718.422780 | 6.095179e-01 | rom |
4. The two cavities solved apart and joined¶
strategy='coupled' meshes and solves every element of the Assembly by
itself. Elements with the same geometry are solved once. Each element is then
reduced, and concatenate joins the element models through the pipe modes at
the cut between them.
joined = module('module_joined')
t0 = time.time()
joined.fds.run(frequencies=[*BAND, 41], polarisation=[0, 1], mesh_config=MESH,
strategy='coupled', reduce={'tol': 1e-9, 'n_snapshots': 14}, progress=False)
print(f'element sweeps, reduction and joining: {time.time() - t0:.0f} s')
elements = joined.fds.fom_of(0)
print(f'{len(elements)} elements, the same model for both: {elements[0] is elements[1]}')
print(joined.fds.concat_of(0))
concat = {m: joined.fds.eigenmodes(source='concat', polarisation=m, fmin=BAND[0],
fmax=BAND[1]) for m in (0, 1)}
concat[0]
element sweeps, reduction and joining: 53 s
2 elements, the same model for both: True
ConcatenatedSystem(2 segments, m=0, rank 142, 6 joined port modes, band 1000.0-3000.0 MHz)
| mode | f [MHz] | R/Q [Ohm] | source | |
|---|---|---|---|---|
| 0 | 1 | 1287.176348 | 1.199807e+02 | concat |
| 1 | 2 | 1287.439976 | 1.274317e+02 | concat |
| 2 | 3 | 2415.354595 | 7.649698e+01 | concat |
| 3 | 4 | 2417.715354 | 6.989679e+00 | concat |
| 4 | 5 | 2500.914617 | 3.021264e-26 | concat |
| 5 | 6 | 2500.922988 | 4.774049e-26 | concat |
| 6 | 7 | 2713.627514 | 6.059705e-01 | concat |
| 7 | 8 | 2718.422792 | 6.095319e-01 | concat |
5. Comparison¶
Frequencies and R/Q from the three, side by side, and the largest relative difference of each from the eigenmode solver.
def side_by_side(m):
ref, r, c = eig[m], rom[m], concat[m]
n = min(len(ref), len(r), len(c))
col_e, col_r = rq_column(ref, m), rq_column(r, m)
return pd.DataFrame({
'eigenmode f [MHz]': ref['f [MHz]'].to_numpy()[:n],
'ROM f [MHz]': r['f [MHz]'].to_numpy()[:n],
'joined f [MHz]': c['f [MHz]'].to_numpy()[:n],
f'eigenmode {col_e}': ref[col_e].to_numpy()[:n],
f'ROM {col_r}': r[col_r].to_numpy()[:n],
f'joined {col_r}': c[col_r].to_numpy()[:n],
})
def worst(table):
f = table.filter(like='f [MHz]').to_numpy()
rq = table.filter(like='R/Q').to_numpy()
big = rq[:, 0] > 1e-3 * rq[:, 0].max() # TE modes have R/Q ~ 0
rel = lambda a, b: np.max(np.abs(a - b) / np.abs(b))
return {'f, ROM': rel(f[:, 1], f[:, 0]), 'f, joined': rel(f[:, 2], f[:, 0]),
'R/Q, ROM': rel(rq[big, 1], rq[big, 0]), 'R/Q, joined': rel(rq[big, 2], rq[big, 0])}
cmp = {m: side_by_side(m) for m in (0, 1)}
print('counts (eigenmode, ROM, joined): monopole', [len(t[0]) for t in (eig, rom, concat)],
' dipole', [len(t[1]) for t in (eig, rom, concat)])
cmp[0]
counts (eigenmode, ROM, joined): monopole [8, 8, 8] dipole [9, 9, 9]
| eigenmode f [MHz] | ROM f [MHz] | joined f [MHz] | eigenmode R/Q [Ohm] | ROM R/Q [Ohm] | joined R/Q [Ohm] | |
|---|---|---|---|---|---|---|
| 0 | 1287.176344 | 1287.176344 | 1287.176348 | 1.199808e+02 | 1.199808e+02 | 1.199807e+02 |
| 1 | 1287.439972 | 1287.439972 | 1287.439976 | 1.274317e+02 | 1.274317e+02 | 1.274317e+02 |
| 2 | 2415.354604 | 2415.354604 | 2415.354595 | 7.649701e+01 | 7.649702e+01 | 7.649698e+01 |
| 3 | 2417.715363 | 2417.715363 | 2417.715354 | 6.989663e+00 | 6.989665e+00 | 6.989679e+00 |
| 4 | 2500.914628 | 2500.914628 | 2500.914617 | 5.225789e-21 | 6.857471e-26 | 3.021264e-26 |
| 5 | 2500.922999 | 2500.922999 | 2500.922988 | 3.670949e-21 | 1.175285e-26 | 4.774049e-26 |
| 6 | 2713.627502 | 2713.627502 | 2713.627514 | 6.059618e-01 | 6.059619e-01 | 6.059705e-01 |
| 7 | 2718.422780 | 2718.422780 | 2718.422792 | 6.095178e-01 | 6.095179e-01 | 6.095319e-01 |
cmp[1]
| eigenmode f [MHz] | ROM f [MHz] | joined f [MHz] | eigenmode R/Q_t [Ohm/m^(2(m-1))] | ROM R/Q_t [Ohm/m^0] | joined R/Q_t [Ohm/m^0] | |
|---|---|---|---|---|---|---|
| 0 | 1717.148238 | 1717.148238 | 1717.148097 | 23.190344 | 23.190326 | 23.190337 |
| 1 | 1726.884616 | 1726.884616 | 1726.884472 | 0.125371 | 0.125372 | 0.125374 |
| 2 | 1841.343341 | 1841.343341 | 1841.343276 | 2.859644 | 2.859604 | 2.859590 |
| 3 | 1847.716734 | 1847.716734 | 1847.716687 | 46.776905 | 46.776949 | 46.776975 |
| 4 | 2488.254597 | 2488.254597 | 2488.254151 | 7.205938 | 7.205939 | 7.205937 |
| 5 | 2518.932922 | 2518.932922 | 2518.932311 | 6.181404 | 6.181368 | 6.181363 |
| 6 | 2553.141598 | 2553.141598 | 2553.141279 | 0.007626 | 0.007627 | 0.007629 |
| 7 | 2696.232143 | 2696.232143 | 2696.232040 | 18.071519 | 18.071551 | 18.071661 |
| 8 | 2952.459481 | 2952.459481 | 2952.459404 | 5.295567 | 5.295561 | 5.295597 |
pd.DataFrame({('monopole' if m == 0 else 'dipole'): worst(cmp[m]) for m in (0, 1)}).map(
lambda v: f'{v:.1e}')
| monopole | dipole | |
|---|---|---|
| f, ROM | 2.5e-12 | 7.6e-13 |
| f, joined | 4.7e-09 | 2.4e-07 |
| R/Q, ROM | 2.1e-07 | 1.4e-05 |
| R/Q, joined | 2.3e-05 | 2.9e-05 |
Every mode’s R/Q against its frequency, from the three. The large circles are the eigenmode solver’s modes, the squares the reduced model’s and the small diamonds the joined model’s. The two TE modes of the monopole near 2501 MHz have no R/Q and are not drawn on the log scale.
fig, axes = plt.subplots(1, 2, figsize=(13, 4.5))
for ax, m in zip(axes, (0, 1)):
mod.eigenmode.plot_modes('R/Q', polarisation=m, fmin=BAND[0], fmax=BAND[1], ax=ax,
show=False, label='eigenmode solver', s=150)
glued.fds.plot_modes('R/Q', source='rom', polarisation=m, fmin=BAND[0], fmax=BAND[1],
ax=ax, show=False, label='reduced model', s=60)
joined.fds.plot_modes('R/Q', source='concat', polarisation=m, fmin=BAND[0],
fmax=BAND[1], ax=ax, show=False, label='joined model',
marker='D', s=18)
ax.set_title('monopole' if m == 0 else 'dipole')
plt.tight_layout()
plt.show()
All three find the same resonances between 1 GHz and 3 GHz: 8 monopole and 9 dipole modes, with no extra ones. Each single-cell mode appears as a pair; the fundamental pair at 1287.18 MHz and 1287.44 MHz is split by only 0.26 MHz, because the drift between the cavities is far below the 3278 MHz cutoff of the 35 mm pipe and couples them weakly. The two monopole modes near 2500.9 MHz are TE modes, with an R/Q of zero.
The reduced model of the module works on the eigenmode solver’s own matrices: its frequencies agree to a few \(10^{-12}\) (monopole) and \(10^{-12}\) (dipole), and its R/Q to \(2\times10^{-7}\) and \(1.4\times10^{-5}\). The joined model agrees to \(5\times10^{-9}\) and \(2\times10^{-7}\) in frequency and \(3\times10^{-5}\) in R/Q. Its two elements are meshed on their own, so its mesh is not exactly the module’s, and differences of this size are what a slightly different mesh gives.
6. Loaded resonances¶
With every pipe mode terminated in its wave impedance, the pipes behave as if
they were infinitely long. A mode above the pipe cutoff then radiates into
them, and its loaded quality factor \(Q_L\) is finite. A mode below every cutoff
stays trapped, with \(Q_L = \infty\). loaded=True gives these resonances of the
joined model. The eigenmode solver gets them with absorbing (PML) pipe ends,
boundary_conditions='oo'.
pml = module('module_pml')
pml.eigenmode.run({'n_modes': 16, 'polarisation': [0, 1], 'boundary_conditions': 'oo',
'mesh_config': MESH})
open_modes = pml.eigenmode.qois_df
rows, alone = [], []
for m in (0, 1):
loaded = joined.fds.eigenmodes(source='concat', polarisation=m, loaded=True,
fmin=BAND[0], fmax=BAND[1])
ref = open_modes[open_modes['m'] == m]
for _, row in loaded.iterrows():
j = int(np.argmin(np.abs(ref['freq [MHz]'].to_numpy() - row['f [MHz]'])))
if abs(ref['freq [MHz]'].iloc[j] / row['f [MHz]'] - 1) > 0.01:
alone.append((m, round(row['f [MHz]'], 1), round(row['Q_L []'], 1)))
continue
rows.append({'m': m, 'joined f [MHz]': row['f [MHz]'], 'joined Q_L': row['Q_L []'],
'PML f [MHz]': ref['freq [MHz]'].iloc[j], 'PML Q': ref['Q_eig []'].iloc[j]})
print('loaded resonances of the joined model without a PML mode within 1 % (m, f, Q_L):', alone)
pd.DataFrame(rows)
loaded resonances of the joined model without a PML mode within 1 % (m, f, Q_L): []
| m | joined f [MHz] | joined Q_L | PML f [MHz] | PML Q | |
|---|---|---|---|---|---|
| 0 | 0 | 1287.044492 | inf | 1287.044506 | 4.597402e+09 |
| 1 | 0 | 1287.308023 | inf | 1287.308037 | 4.595420e+09 |
| 2 | 0 | 2414.194144 | inf | 2414.194234 | 5.504828e+07 |
| 3 | 0 | 2416.529863 | inf | 2416.529955 | 5.511639e+07 |
| 4 | 0 | 2500.918803 | inf | 2500.918774 | 2.220329e+12 |
| 5 | 0 | 2500.927174 | inf | 2500.927145 | 2.221164e+12 |
| 6 | 0 | 2711.179567 | inf | 2711.179799 | 1.958969e+06 |
| 7 | 0 | 2715.963013 | inf | 2715.963228 | 1.879641e+06 |
| 8 | 1 | 1722.138686 | inf | 1722.138420 | 5.558731e+05 |
| 9 | 1 | 1732.508579 | inf | 1732.508330 | 4.698368e+05 |
| 10 | 1 | 1844.403260 | inf | 1844.403917 | 5.551208e+05 |
| 11 | 1 | 1850.180466 | inf | 1850.181052 | 6.753631e+05 |
| 12 | 1 | 2499.399679 | inf | 2501.135153 | 2.256091e+02 |
| 13 | 1 | 2522.814233 | 40.947545 | 2528.244773 | 5.175497e+01 |
| 14 | 1 | 2547.637219 | 44.807435 | 2550.396411 | 6.274981e+01 |
| 15 | 1 | 2696.213643 | 2910.225739 | 2696.202308 | 2.944634e+03 |
| 16 | 1 | 2999.406174 | 41.748395 | 3000.225674 | 4.062179e+01 |
ax = pml.eigenmode.plot_modes('Q', polarisation=1, show=False, label='eigenmode solver, PML',
s=150)
joined.fds.plot_modes('Q', source='concat', polarisation=1, fmin=BAND[0], fmax=BAND[1],
ax=ax, show=False, label='joined model, terminated pipes', s=50)
ax.axvline(2510, ls=':', color='k', lw=1.1)
ax.text(2520, 0.5, 'TE$_{11}$ cutoff', transform=ax.get_xaxis_transform(), rotation=90,
va='center')
ax.set_xlim(*BAND)
ax.set_title('dipole: loaded Q')
plt.show()
Below every pipe cutoff a mode cannot radiate. The joined model gives these modes \(Q_L = \infty\), and their frequencies agree with the PML solution to better than \(4\times10^{-7}\) (1287.044492 against 1287.044506 MHz for the fundamental). The PML’s Q of \(10^5\) to \(10^{12}\) for them is finite only because the absorbing layer takes up a little of the decaying pipe field. The same holds for the dipole mode at 2499.4 MHz, 10 MHz below the \(\mathrm{TE}_{11}\) cutoff of the 35 mm pipe (2510 MHz): the pipe field decays slowly so close to the cutoff, and the PML gives it a Q of only 226.
Above the cutoff the dipole modes radiate into the pipes. At 2696.2 MHz both methods give a Q of about 2900 (2910 against 2945, 1.2 % apart), and at 3000 MHz 41.7 against 40.6 (2.7 %). The two modes just above the cutoff, at 2522.8 MHz and 2547.6 MHz, differ more: Q of 41 and 45 against 52 and 63. Close to a cutoff both methods are presumably at their least accurate. The terminated ports use the wave impedance at the real part of the frequency, and it changes fastest there. The PML has to absorb a field that hardly decays along the pipe.
Summary¶
On the same mesh, the resonances of the reduced model are those of the eigenmode solver: frequencies to a few \(10^{-12}\), R/Q to \(1.4\times10^{-5}\). Joining two separately reduced cavities, solved once, reproduces them to \(2\times10^{-7}\) in frequency and \(3\times10^{-5}\) in R/Q, without extra resonances. The reduced and joined models of the module are built in about a minute each, both orders together, and are then evaluated at any frequency of their band without the mesh.
With the pipe ends terminated, the same joined model gives the loaded resonances: trapped modes exactly lossless, and the loaded Q of radiating modes within 1 % to 3 % of the PML eigenmode solver away from the pipe cutoff.