-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtest_transverse_deflecting_cavity.py
135 lines (113 loc) · 4.39 KB
/
test_transverse_deflecting_cavity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import pytest
import torch
import cheetah
@pytest.mark.parametrize("dtype", [torch.float32, torch.float64])
def test_transverse_deflecting_cavity_bmadx_tracking(dtype):
"""
Test that the results of tracking through a TDC with the `"bmadx"` tracking method
match the results from Bmad-X.
"""
incoming_beam = torch.load(
"tests/resources/bmadx/incoming.pt", weights_only=False
).to(dtype)
tdc = cheetah.TransverseDeflectingCavity(
length=torch.tensor([1.0]),
voltage=torch.tensor([1e7]),
phase=torch.tensor([0.2], dtype=dtype),
frequency=torch.tensor([1e9]),
tracking_method="bmadx",
dtype=dtype,
)
# Run tracking
outgoing_beam = tdc.track(incoming_beam)
# Load reference result computed with Bmad-X
outgoing_bmadx = torch.load(
"tests/resources/bmadx/outgoing_transverse_deflecting_cavity.pt",
weights_only=False,
)
assert torch.allclose(
outgoing_beam.particles,
outgoing_bmadx.to(dtype),
atol=1e-14 if dtype == torch.float64 else 0.00001,
rtol=1e-14 if dtype == torch.float64 else 1e-6,
)
def test_transverse_deflecting_cavity_energy_length_vectorization():
"""
Test that vectorised tracking through a TDC throws now exception and outputs the
correct shape, when the input beam's energy and the TDC's length are vectorised.
"""
incoming_beam = cheetah.ParticleBeam.from_parameters(
num_particles=torch.tensor(10_000),
sigma_px=torch.tensor(2e-7),
sigma_py=torch.tensor(2e-7),
energy=torch.tensor([50e6, 60e6]),
)
tdc = cheetah.TransverseDeflectingCavity(
length=torch.tensor(1.0),
voltage=torch.tensor([[1e7], [2e7], [3e7]]),
phase=torch.tensor(0.4),
frequency=torch.tensor(1e9),
tracking_method="bmadx",
)
outgoing_beam = tdc.track(incoming_beam)
assert outgoing_beam.particles.shape[:-2] == torch.Size([3, 2])
def test_transverse_deflecting_cavity_energy_phase_vectorization():
"""
Test that vectorised tracking through a TDC throws now exception and outputs the
correct shape, when the input beam's energy and the TDC's phase are vectorised.
"""
incoming_beam = cheetah.ParticleBeam.from_parameters(
num_particles=torch.tensor(10_000),
sigma_px=torch.tensor(2e-7),
sigma_py=torch.tensor(2e-7),
energy=torch.tensor([50e6, 60e6]),
)
tdc = cheetah.TransverseDeflectingCavity(
length=torch.tensor(1.0),
voltage=torch.tensor(1e7),
phase=torch.tensor([[0.6], [0.5], [0.4]]),
frequency=torch.tensor(1e9),
tracking_method="bmadx",
)
outgoing_beam = tdc.track(incoming_beam)
assert outgoing_beam.particles.shape[:-2] == torch.Size([3, 2])
def test_transverse_deflecting_cavity_energy_frequency_vectorization():
"""
Test that vectorised tracking through a TDC throws now exception and outputs the
correct shape, when the input beam's energy and the TDC's frequency are vectorised.
"""
incoming_beam = cheetah.ParticleBeam.from_parameters(
num_particles=torch.tensor(10_000),
sigma_px=torch.tensor(2e-7),
sigma_py=torch.tensor(2e-7),
energy=torch.tensor([50e6, 60e6]),
)
tdc3 = cheetah.TransverseDeflectingCavity(
length=torch.tensor(1.0),
voltage=torch.tensor(1e7),
phase=torch.tensor(0.4),
frequency=torch.tensor([[1e9], [2e9], [3e9]]),
tracking_method="bmadx",
)
_ = tdc3.track(incoming_beam)
assert _.particles.shape[:-2] == torch.Size([3, 2])
def test_transverse_deflecting_cavity_all_parameters_vectorization():
"""
Test that vectorised tracking through a TDC throws now exception and outputs the
correct shape, when all parameters are vectorised.
"""
incoming_beam = cheetah.ParticleBeam.from_parameters(
num_particles=torch.tensor(10_000),
sigma_px=torch.tensor(2e-7),
sigma_py=torch.tensor(2e-7),
energy=torch.tensor([50e6, 60e6]),
)
tdc = cheetah.TransverseDeflectingCavity(
length=torch.tensor(1.0),
voltage=torch.ones([4, 1, 1, 1]) * 1e7,
phase=torch.ones([1, 3, 1, 1]) * 0.4,
frequency=torch.ones([1, 1, 2, 1]) * 1e9,
tracking_method="bmadx",
)
outgoing_beam = tdc.track(incoming_beam)
assert outgoing_beam.particles.shape[:-2] == torch.Size([4, 3, 2, 2])