1
1
import numpy
2
- from numpy .testing import assert_allclose , assert_equal
3
2
import pytest
3
+ from numpy .testing import assert_allclose , assert_equal
4
+
4
5
from at import elements
5
6
from at .lattice import Lattice , AtWarning , AtError
6
7
7
8
8
9
def test_lattice_creation_gets_attributes_from_arguments ():
9
- lat = Lattice (name = ' lattice' , energy = 3.e+6 , periodicity = 32 , an_attr = 12 )
10
+ lat = Lattice (name = " lattice" , energy = 3.0e6 , periodicity = 32 , an_attr = 12 )
10
11
assert len (lat ) == 0
11
- assert lat .name == ' lattice'
12
- assert lat .energy == 3.e+6
12
+ assert lat .name == " lattice"
13
+ assert lat .energy == 3.0e6
13
14
assert lat .periodicity == 32
14
15
assert lat .radiation is False
15
- assert lat .particle .name == ' relativistic'
16
+ assert lat .particle .name == " relativistic"
16
17
assert lat .an_attr == 12
17
18
18
19
19
20
def test_lattice_energy_radiation_periodicity ():
20
- d = elements .Dipole ('d1' , 1 , BendingAngle = numpy .pi / 16 , Energy = 5.e+6 ,
21
- PassMethod = 'BndMPoleSymplectic4RadPass' )
22
- lat = Lattice ([d ], name = 'lattice' , energy = 3.e+6 )
23
- assert lat .energy == 3.e+6
21
+ d = elements .Dipole (
22
+ "d1" ,
23
+ 1 ,
24
+ BendingAngle = numpy .pi / 16 ,
25
+ Energy = 5.0e6 ,
26
+ PassMethod = "BndMPoleSymplectic4RadPass" ,
27
+ )
28
+ lat = Lattice ([d ], name = "lattice" , energy = 3.0e6 )
29
+ assert lat .energy == 3.0e6
24
30
assert lat .periodicity == 32
25
31
assert lat .radiation is True
26
32
assert lat .is_6d is True
27
33
28
34
29
35
def test_lattice_voltage_harmonic_number ():
30
- rf = elements .RFCavity ('rf' , 0 , 0.2e6 , 0.5e9 , 5 , 3.e6 )
31
- d = elements .Dipole ('d1' , 2.99792458 , BendingAngle = numpy .pi / 5 )
32
- lat = Lattice ([rf , d ], name = ' lattice' )
33
- assert lat .energy == 3.e+6
36
+ rf = elements .RFCavity ("rf" , 0 , 0.2e6 , 0.5e9 , 5 , 3.0e6 )
37
+ d = elements .Dipole ("d1" , 2.99792458 , BendingAngle = numpy .pi / 5 )
38
+ lat = Lattice ([rf , d ], name = " lattice" )
39
+ assert lat .energy == 3.0e6
34
40
assert lat .periodicity == 10
35
41
assert lat .rf_voltage == 2e6
36
- assert lat .revolution_frequency == 10.e6
42
+ assert lat .revolution_frequency == 10.0e6
37
43
assert lat .harmonic_number == 50
38
44
assert lat .radiation is True
39
45
40
46
41
47
def test_lattice_creation_from_lattice_inherits_attributes ():
42
- d = elements .Dipole ('d1' , 1 , BendingAngle = numpy .pi , Energy = 5.e+6 ,
43
- PassMethod = 'BndMPoleSymplectic4RadPass' )
44
- lat1 = Lattice ([d ], name = 'lattice' , energy = 3.e+6 , periodicity = 32 ,
45
- an_attr = 12 )
48
+ d = elements .Dipole (
49
+ "d1" ,
50
+ 1 ,
51
+ BendingAngle = numpy .pi ,
52
+ Energy = 5.0e6 ,
53
+ PassMethod = "BndMPoleSymplectic4RadPass" ,
54
+ )
55
+ lat1 = Lattice ([d ], name = "lattice" , energy = 3.0e6 , periodicity = 32 , an_attr = 12 )
46
56
lat2 = Lattice (lat1 , another_attr = 5 )
47
57
assert id (lat1 ) != id (lat2 )
48
58
assert len (lat2 ) == 1
49
- assert lat2 .name == ' lattice'
50
- assert lat2 .energy == 3.e+6
59
+ assert lat2 .name == " lattice"
60
+ assert lat2 .energy == 3.0e6
51
61
assert lat2 .periodicity == 32
52
62
assert lat2 .is_6d is True
53
63
assert lat2 .another_attr == 5
@@ -56,30 +66,39 @@ def test_lattice_creation_from_lattice_inherits_attributes():
56
66
57
67
58
68
def test_lattice_energy_is_not_defined_raises_AtError ():
59
- d = elements .Dipole ('d1' , 1 , BendingAngle = numpy .pi )
69
+ d = elements .Dipole ("d1" , 1 , BendingAngle = numpy .pi )
60
70
with pytest .raises (AtError ):
61
71
Lattice ([d ])
62
72
63
73
64
74
def test_item_is_not_an_AT_element_warns_correctly ():
65
75
with pytest .warns (AtWarning ):
66
- Lattice (['a' ], energy = 0 , periodicity = 1 )
76
+ Lattice (["a" ], energy = 0 , periodicity = 1 )
67
77
68
78
69
79
def test_lattice_string_ordering ():
70
- lat = Lattice ([elements .Drift ('D0' , 1.0 , attr1 = numpy .array (0 ))],
71
- name = 'lat' , energy = 5 , periodicity = 1 , attr2 = 3 )
80
+ lat = Lattice (
81
+ [elements .Drift ("D0" , 1.0 , attr1 = numpy .array (0 ))],
82
+ name = "lat" ,
83
+ energy = 5 ,
84
+ periodicity = 1 ,
85
+ attr2 = 3 ,
86
+ )
72
87
latstr = str (lat )
73
- assert latstr .startswith ("Lattice(<1 elements>, name='lat', "
74
- "energy=5, particle=Particle('relativistic'), "
75
- "periodicity=1, beam_current=0.0, nbunch=1" )
88
+ assert latstr .startswith (
89
+ "Lattice(<1 elements>, name='lat', "
90
+ "energy=5, particle=Particle('relativistic'), "
91
+ "periodicity=1, beam_current=0.0, nbunch=1"
92
+ )
76
93
assert latstr .endswith ("attr2=3)" )
77
94
78
95
latrepr = repr (lat )
79
- assert latrepr .startswith ("Lattice([Drift('D0', 1.0, attr1=array(0))], "
80
- "name='lat', "
81
- "energy=5, particle=Particle('relativistic'), "
82
- "periodicity=1, beam_current=0.0, nbunch=1" )
96
+ assert latrepr .startswith (
97
+ "Lattice([Drift('D0', 1.0, attr1=array(0))], "
98
+ "name='lat', "
99
+ "energy=5, particle=Particle('relativistic'), "
100
+ "periodicity=1, beam_current=0.0, nbunch=1"
101
+ )
83
102
assert latrepr .endswith ("attr2=3)" )
84
103
85
104
@@ -90,7 +109,7 @@ def test_getitem(simple_lattice, simple_ring):
90
109
91
110
92
111
def test_setitem (simple_lattice ):
93
- new = elements .Monitor ('M2' )
112
+ new = elements .Monitor ("M2" )
94
113
old = simple_lattice [5 ]
95
114
simple_lattice [5 ] = new
96
115
assert simple_lattice [5 ] != old
@@ -102,7 +121,7 @@ def test_setitem(simple_lattice):
102
121
103
122
104
123
def test_delitem (simple_lattice , simple_ring ):
105
- mon = elements .Monitor ('M2' )
124
+ mon = elements .Monitor ("M2" )
106
125
simple_lattice .append (mon )
107
126
assert len (simple_lattice ) == 7
108
127
del simple_lattice [- 1 ]
@@ -128,34 +147,32 @@ def test_property_values_against_known(hmba_lattice):
128
147
assert hmba_lattice .rf_voltage == 6000000
129
148
assert hmba_lattice .harmonic_number == 992
130
149
assert hmba_lattice .radiation is False
131
- numpy .testing .assert_almost_equal (hmba_lattice .energy_loss ,
132
- 2526188.658758993 , decimal = 1 )
150
+ numpy .testing .assert_almost_equal (
151
+ hmba_lattice .energy_loss , 2526188.658758993 , decimal = 1
152
+ )
133
153
134
154
135
155
def test_radiation_change (hmba_lattice ):
136
- rfs = [elem for elem in hmba_lattice if isinstance (elem ,
137
- elements .RFCavity )]
138
- dipoles = [elem for elem in hmba_lattice if isinstance (elem ,
139
- elements .Dipole )]
140
- quads = [elem for elem in hmba_lattice if isinstance (elem ,
141
- elements .Quadrupole )]
142
- hmba_lattice .radiation_on (None , 'pass2' , 'auto' )
156
+ rfs = [elem for elem in hmba_lattice if isinstance (elem , elements .RFCavity )]
157
+ dipoles = [elem for elem in hmba_lattice if isinstance (elem , elements .Dipole )]
158
+ quads = [elem for elem in hmba_lattice if isinstance (elem , elements .Quadrupole )]
159
+ hmba_lattice .radiation_on (None , "pass2" , "auto" )
143
160
assert hmba_lattice .radiation is True
144
161
assert hmba_lattice .has_cavity is False
145
162
for elem in rfs :
146
- assert elem .PassMethod == ' IdentityPass'
163
+ assert elem .PassMethod == " IdentityPass"
147
164
for elem in dipoles :
148
- assert elem .PassMethod == ' pass2'
165
+ assert elem .PassMethod == " pass2"
149
166
for elem in quads :
150
- assert elem .PassMethod == ' StrMPoleSymplectic4RadPass'
151
- hmba_lattice .radiation_off (None , ' BndMPoleSymplectic4Pass' , ' auto' )
167
+ assert elem .PassMethod == " StrMPoleSymplectic4RadPass"
168
+ hmba_lattice .radiation_off (None , " BndMPoleSymplectic4Pass" , " auto" )
152
169
assert hmba_lattice .radiation is False
153
170
for elem in rfs :
154
- assert elem .PassMethod == ' IdentityPass'
171
+ assert elem .PassMethod == " IdentityPass"
155
172
for elem in dipoles :
156
- assert elem .PassMethod == ' BndMPoleSymplectic4Pass'
173
+ assert elem .PassMethod == " BndMPoleSymplectic4Pass"
157
174
for elem in quads :
158
- assert elem .PassMethod == ' StrMPoleSymplectic4Pass'
175
+ assert elem .PassMethod == " StrMPoleSymplectic4Pass"
159
176
160
177
161
178
def test_radiation_state_errors (hmba_lattice ):
@@ -173,35 +190,38 @@ def test_radiation_state_errors(hmba_lattice):
173
190
hmba_lattice .disable_6d ()
174
191
hmba_lattice .get_mcf ()
175
192
176
- @pytest .mark .parametrize ('ring' ,
177
- [pytest .lazy_fixture ('hmba_lattice' )])
178
- def test_develop (ring ):
193
+
194
+ @pytest .mark .parametrize ("ring" , ["hmba_lattice" ])
195
+ def test_develop (request , ring ):
196
+ ring = request .getfixturevalue (ring )
179
197
newring = ring .develop ()
180
198
assert_allclose (ring .circumference , newring .circumference )
181
199
assert_allclose (ring .rf_voltage , newring .rf_voltage )
182
- assert_allclose (ring .revolution_frequency * ring .harmonic_number ,
183
- newring .revolution_frequency * newring .harmonic_number )
200
+ assert_allclose (
201
+ ring .revolution_frequency * ring .harmonic_number ,
202
+ newring .revolution_frequency * newring .harmonic_number ,
203
+ )
184
204
rp1 = ring .radiation_parameters ()
185
205
rp2 = newring .radiation_parameters ()
186
206
assert_allclose (rp1 .fulltunes , rp2 .fulltunes )
187
207
assert_allclose (rp1 .U0 , rp2 .U0 )
188
208
189
- @pytest .mark .parametrize ('ring' ,
190
- [pytest .lazy_fixture ('hmba_lattice' )])
191
- def test_operators (ring ):
209
+
210
+ @pytest .mark .parametrize ("ring" , ["hmba_lattice" ])
211
+ def test_operators (request , ring ):
212
+ ring = request .getfixturevalue (ring )
192
213
newring1 = ring + ring
193
214
newring2 = ring .concatenate (ring , copy = True )
194
- assert_equal (len (newring1 ), len (ring )* 2 )
215
+ assert_equal (len (newring1 ), len (ring ) * 2 )
195
216
assert_equal (len (newring2 ), len (ring ) * 2 )
196
217
newring1 += ring
197
218
newring2 .concatenate (ring , copy = False )
198
- assert_equal (len (newring1 ), len (ring )* 3 )
199
- assert_equal (len (newring2 ), len (ring )* 3 )
219
+ assert_equal (len (newring1 ), len (ring ) * 3 )
220
+ assert_equal (len (newring2 ), len (ring ) * 3 )
200
221
newring1 = ring * 2
201
222
newring2 = ring .repeat (2 )
202
- assert_equal (len (newring1 ), len (ring )* 2 )
223
+ assert_equal (len (newring1 ), len (ring ) * 2 )
203
224
assert_equal (len (newring2 ), len (ring ) * 2 )
204
225
assert_equal (newring1 .harmonic_number , ring .harmonic_number )
205
226
newring1 = ring .reverse (copy = True )
206
227
assert_equal (newring1 [- 1 ].FamName , ring [0 ].FamName )
207
-
0 commit comments