forked from pals-project/pals-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_serialization.py
More file actions
334 lines (272 loc) · 10.8 KB
/
test_serialization.py
File metadata and controls
334 lines (272 loc) · 10.8 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import os
import pals
def test_yaml():
# Create one base element
element1 = pals.Marker(name="element1")
# Create one thick element
element2 = pals.Drift(name="element2", length=2.0)
# Create line with both elements
line = pals.BeamLine(name="line", line=[element1, element2])
# Serialize the BeamLine object to YAML
test_file = "line.pals.yaml"
line.to_file(test_file)
# Read the YAML data from the test file
loaded_line = pals.BeamLine.from_file(test_file)
# Remove the test file
os.remove(test_file)
# Validate loaded BeamLine object
assert line == loaded_line
def test_json():
# Create one base element
element1 = pals.Marker(name="element1")
# Create one thick element
element2 = pals.Drift(name="element2", length=2.0)
# Create line with both elements
line = pals.BeamLine(name="line", line=[element1, element2])
# Serialize the BeamLine object to JSON
test_file = "line.pals.json"
line.to_file(test_file)
# Read the JSON data from the test file
loaded_line = pals.BeamLine.from_file(test_file)
# Remove the test file
os.remove(test_file)
# Validate loaded BeamLine object
assert line == loaded_line
def test_comprehensive_lattice():
"""Test a comprehensive lattice using every PALS element at least once"""
# Create elements in alphabetical order for easy maintenance
# ACKicker
ackicker = pals.ACKicker(name="ackicker1", length=0.1)
# BeamBeam
beambeam = pals.BeamBeam(name="beambeam1", BeamBeamP=pals.BeamBeamParameters())
# BeginningEle
beginning = pals.BeginningEle(name="beginning1")
# Converter
converter = pals.Converter(name="converter1")
# CrabCavity
crabcavity = pals.CrabCavity(name="crabcavity1", length=0.2)
# Drift
drift = pals.Drift(name="drift1", length=0.5)
# EGun
egun = pals.EGun(name="egun1", length=0.15)
# Feedback
feedback = pals.Feedback(name="feedback1")
# Fiducial
fiducial = pals.Fiducial(name="fiducial1")
# FloorShift
floorshift = pals.FloorShift(
name="floorshift1", FloorShiftP=pals.FloorShiftParameters(x_offset=0.1)
)
# Foil
foil = pals.Foil(name="foil1")
# Fork
fork = pals.Fork(name="fork1", ForkP=pals.ForkParameters(to_line="line1"))
# Girder
girder = pals.Girder(name="girder1")
# Instrument
instrument = pals.Instrument(name="instrument1", length=0.05)
# Kicker
kicker = pals.Kicker(name="kicker1", length=0.1)
# Marker
marker = pals.Marker(name="marker1")
# Mask
mask = pals.Mask(name="mask1", length=0.02)
# Match
match = pals.Match(name="match1")
# Multipole
multipole = pals.Multipole(name="multipole1", length=0.3)
# NullEle
nullele = pals.NullEle(name="nullele1")
# Octupole
octupole = pals.Octupole(
name="octupole1",
length=0.25,
ElectricMultipoleP=pals.ElectricMultipoleParameters(En3=0.5),
MetaP=pals.MetaParameters(alias="octupole_test"),
)
# Patch
patch = pals.Patch(
name="patch1", length=0.4, PatchP=pals.PatchParameters(x_offset=0.05)
)
# Quadrupole
quadrupole = pals.Quadrupole(
name="quadrupole1",
length=0.8,
MagneticMultipoleP=pals.MagneticMultipoleParameters(Bn1=1.0),
)
# RBend
rbend = pals.RBend(
name="rbend1",
length=1.0,
BendP=pals.BendParameters(rho_ref=2.0),
ApertureP=pals.ApertureParameters(x_limits=[-0.2, 0.2]),
)
# RFCavity
rfcavity = pals.RFCavity(
name="rfcavity1",
length=0.3,
RFP=pals.RFParameters(frequency=1e9),
SolenoidP=pals.SolenoidParameters(Ksol=0.05),
)
# SBend
sbend = pals.SBend(
name="sbend1", length=1.2, BendP=pals.BendParameters(rho_ref=1.5)
)
# Sextupole
sextupole = pals.Sextupole(
name="sextupole1",
length=0.2,
MagneticMultipoleP=pals.MagneticMultipoleParameters(Bn2=1.0),
ApertureP=pals.ApertureParameters(x_limits=[-0.1, 0.1]),
)
# Solenoid
solenoid = pals.Solenoid(
name="solenoid1", length=0.6, SolenoidP=pals.SolenoidParameters(Ksol=0.1)
)
# Taylor
taylor = pals.Taylor(name="taylor1")
# UnionEle - with nested elements
union_marker = pals.Marker(name="union_marker")
union_drift = pals.Drift(name="union_drift", length=0.1)
unionele = pals.UnionEle(name="unionele1", elements=[union_marker, union_drift])
# Wiggler
wiggler = pals.Wiggler(name="wiggler1", length=2.0)
# Create comprehensive lattice
lattice = pals.Lattice(
name="comprehensive_lattice",
branches=[
pals.BeamLine(
name="comprehensive_beamline",
line=[
beginning, # Start with beginning element
fiducial, # Global coordinate reference
marker, # Mark position
drift, # Field-free region
quadrupole, # Focusing element
sextupole, # Chromatic correction
octupole, # Higher order correction
multipole, # General multipole
rbend, # Rectangular bend
sbend, # Sector bend
solenoid, # Longitudinal focusing
rfcavity, # RF acceleration
crabcavity, # RF crab cavity
kicker, # Transverse kick
ackicker, # AC kicker
patch, # Coordinate transformation
floorshift, # Global coordinate shift
instrument, # Measurement device
mask, # Collimation
match, # Matching element
egun, # Electron source
converter, # Species conversion
foil, # Electron stripping
beambeam, # Colliding beams
feedback, # Feedback system
girder, # Support structure
fork, # Branch connection
taylor, # Taylor map
unionele, # Overlapping elements
wiggler, # Undulator
nullele, # Placeholder
],
)
],
)
# Write to temporary file
yaml_file = "comprehensive_lattice.pals.yaml"
lattice.to_file(yaml_file)
# Read back from file
with open(yaml_file, "r") as file:
print(f"\nComprehensive lattice YAML:\n{file.read()}")
# Deserialize back to Python object using Pydantic model logic
loaded_lattice = pals.Lattice.from_file(yaml_file)
# Verify the loaded lattice has the correct structure and parameter groups
assert len(loaded_lattice.branches) == 1 # Should have 1 branch
assert (
len(loaded_lattice.branches[0].line) == 31
) # Should have 31 elements in the branch
# Verify specific elements with parameter groups are correctly loaded
sextupole_loaded = None
octupole_loaded = None
rbend_loaded = None
rfcavity_loaded = None
unionele_loaded = None
for elem in loaded_lattice.branches[0].line:
if elem.name == "sextupole1":
sextupole_loaded = elem
elif elem.name == "octupole1":
octupole_loaded = elem
elif elem.name == "rbend1":
rbend_loaded = elem
elif elem.name == "rfcavity1":
rfcavity_loaded = elem
elif elem.name == "unionele1":
unionele_loaded = elem
# Test that parameter groups are correctly deserialized
assert sextupole_loaded.MagneticMultipoleP.Bn2 == 1.0
assert sextupole_loaded.ApertureP.x_limits == [-0.1, 0.1]
assert octupole_loaded.ElectricMultipoleP.En3 == 0.5
assert octupole_loaded.MetaP.alias == "octupole_test"
assert rbend_loaded.BendP.rho_ref == 2.0
assert rbend_loaded.ApertureP.x_limits == [-0.2, 0.2]
assert rfcavity_loaded.RFP.frequency == 1e9
assert rfcavity_loaded.SolenoidP.Ksol == 0.05
# Test that UnionEle elements are correctly deserialized
assert unionele_loaded is not None
assert len(unionele_loaded.elements) == 2
assert unionele_loaded.elements[0].name == "union_marker"
assert unionele_loaded.elements[0].kind == "Marker"
assert unionele_loaded.elements[1].name == "union_drift"
assert unionele_loaded.elements[1].kind == "Drift"
assert unionele_loaded.elements[1].length == 0.1
# Write to temporary file
json_file = "comprehensive_lattice.pals.json"
lattice.to_file(json_file)
# Read back from file
with open(json_file, "r") as file:
print(f"\nComprehensive lattice JSON:\n{file.read()}")
# Deserialize back to Python object using Pydantic model logic
loaded_lattice_json = pals.Lattice.from_file(json_file)
# Verify the loaded lattice has the correct structure and parameter groups
assert len(loaded_lattice_json.branches) == 1 # Should have 1 branch
assert (
len(loaded_lattice_json.branches[0].line) == 31
) # Should have 31 elements in the branch
# Verify specific elements with parameter groups are correctly loaded
sextupole_loaded_json = None
octupole_loaded_json = None
rbend_loaded_json = None
rfcavity_loaded_json = None
unionele_loaded_json = None
for elem in loaded_lattice_json.branches[0].line:
if elem.name == "sextupole1":
sextupole_loaded_json = elem
elif elem.name == "octupole1":
octupole_loaded_json = elem
elif elem.name == "rbend1":
rbend_loaded_json = elem
elif elem.name == "rfcavity1":
rfcavity_loaded_json = elem
elif elem.name == "unionele1":
unionele_loaded_json = elem
# Test that parameter groups are correctly deserialized
assert sextupole_loaded_json.MagneticMultipoleP.Bn2 == 1.0
assert sextupole_loaded_json.ApertureP.x_limits == [-0.1, 0.1]
assert octupole_loaded_json.ElectricMultipoleP.En3 == 0.5
assert octupole_loaded_json.MetaP.alias == "octupole_test"
assert rbend_loaded_json.BendP.rho_ref == 2.0
assert rbend_loaded_json.ApertureP.x_limits == [-0.2, 0.2]
assert rfcavity_loaded_json.RFP.frequency == 1e9
assert rfcavity_loaded_json.SolenoidP.Ksol == 0.05
# Test that UnionEle elements are correctly deserialized from JSON
assert unionele_loaded_json is not None
assert len(unionele_loaded_json.elements) == 2
assert unionele_loaded_json.elements[0].name == "union_marker"
assert unionele_loaded_json.elements[0].kind == "Marker"
assert unionele_loaded_json.elements[1].name == "union_drift"
assert unionele_loaded_json.elements[1].kind == "Drift"
assert unionele_loaded_json.elements[1].length == 0.1
# Clean up temporary files
os.remove(yaml_file)
os.remove(json_file)