-
Notifications
You must be signed in to change notification settings - Fork 0
/
UTILITY_setLattice.py
343 lines (262 loc) · 12.5 KB
/
UTILITY_setLattice.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
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
335
336
337
338
339
340
341
342
343
import yaml
from UTILITY_linacPhaseAndAmplitude import getLinacMatchStrings, setLinacPhase, setLinacGradientAuto
#This is the all-in-one function that should make any and all changes to the lattice
#This way there's a single place where default values need to be kept
def setLattice(
tao,
defaultsFile=None,
verbose = False,
**overrides
):
if not defaultsFile:
defaultsFile = 'setLattice_defaults.yml'
if verbose:
print(f"No defaults file provided to setLattice(). Using {defaultsFile}")
with open(defaultsFile, 'r') as file:
defaults = yaml.safe_load(file)
# Combine defaults and overrides, with overrides taking precedence
latticeSettings = {**defaults, **overrides}
#Prevent recalculation until changes are made
tao.cmd("set global lattice_calc_on = F")
#Pre-2024-09-30 version, can delete
# setLinacsHelper(tao,
# latticeSettings["L1PhaseSet"],
# latticeSettings["L2PhaseSet"],
# latticeSettings["L2EnergyOffset"],
# latticeSettings["L3EnergyOffset"]
# )
setLinacsHelper(
tao,
latticeSettings["L0BPhaseSet"],
latticeSettings["L0BEnergyOffset"],
latticeSettings["L1PhaseSet"],
latticeSettings["L1EnergyOffset"],
latticeSettings["L2PhaseSet"],
latticeSettings["L2EnergyOffset"],
latticeSettings["L3PhaseSet"],
latticeSettings["L3EnergyOffset"]
)
setAllInjectorQuads(tao,
latticeSettings["QA10361kG"],
latticeSettings["QA10371kG"],
latticeSettings["QE10425kG"],
latticeSettings["QE10441kG"],
latticeSettings["QE10511kG"],
latticeSettings["QE10525kG"]
)
setAllFinalFocusQuads(tao,
latticeSettings["Q5FFkG"],
latticeSettings["Q4FFkG"],
latticeSettings["Q3FFkG"],
latticeSettings["Q2FFkG"],
latticeSettings["Q1FFkG"],
latticeSettings["Q0FFkG"],
latticeSettings["Q0DkG"],
latticeSettings["Q1DkG"],
latticeSettings["Q2DkG"]
)
setAllWChicaneBends(tao,
latticeSettings["B1EkG"],
latticeSettings["B2EkG"],
latticeSettings["B3EkG"]
)
setAllWChicaneQuads(tao,
latticeSettings["Q1EkG"],
latticeSettings["Q2EkG"],
latticeSettings["Q3EkG"],
latticeSettings["Q4EkG"],
latticeSettings["Q5EkG"],
latticeSettings["Q6EkG"]
)
if latticeSettings["symmetricSextupoleStrengths"]:
#Everything set symmetrically to "L" values
setAllWChicaneSextupoles(tao,
latticeSettings["S1ELkG"],
latticeSettings["S2ELkG"],
latticeSettings["S3ELkG"],
latticeSettings["S3ELkG"],
latticeSettings["S2ELkG"],
latticeSettings["S1ELkG"]
)
else:
setAllWChicaneSextupoles(tao,
latticeSettings["S1ELkG"],
latticeSettings["S2ELkG"],
latticeSettings["S3ELkG"],
latticeSettings["S3ERkG"],
latticeSettings["S2ERkG"],
latticeSettings["S1ERkG"]
)
setAllWChicaneMovers(tao,
latticeSettings["S1EL_xOffset"],
latticeSettings["S1EL_yOffset"],
latticeSettings["S2EL_xOffset"],
latticeSettings["S2EL_yOffset"],
latticeSettings["S2ER_xOffset"],
latticeSettings["S2ER_yOffset"],
latticeSettings["S1ER_xOffset"],
latticeSettings["S1ER_yOffset"]
)
setAllFinalFocusKickers(tao,
latticeSettings["XC1FFkG"],
latticeSettings["XC3FFkG"],
latticeSettings["YC1FFkG"],
latticeSettings["YC2FFkG"]
)
#Reenable lattice calculations
tao.cmd("set global lattice_calc_on = T")
return
def setLinacsHelper(tao, L0BPhaseSet, L0BEnergyOffset, L1PhaseSet, L1EnergyOffset, L2PhaseSet, L2EnergyOffset, L3PhaseSet, L3EnergyOffset):
[L1MatchStrings, L2MatchStrings, L3MatchStrings, selectMarkers] = getLinacMatchStrings(tao)
# === Make changes to base lattice ===
tao.cmd(f'set ele L0BF PHI0 = {L0BPhaseSet/360.0}') #DNT. "loadNominalValues_2Bunch.m" had this set to zero
tao.cmd(f'set ele L0BF VOLTAGE = {5.95e7 + L0BEnergyOffset}') #DNT. Base value was 7.1067641E+07, new value (5.95e7) set to change output energy to 125 MeV (down from 136.5 MeV)
#L1AssertPHI0 = -19 #DNT. "loadNominalValues_2Bunch.m" had this set to -19 degrees
setLinacPhase( tao, L1MatchStrings, L1PhaseSet )
setLinacGradientAuto( tao, L1MatchStrings, L1EnergyOffset + 0.335e9 - 0.125e9 )
#L2AssertPHI0 = -37 #DNT. "loadNominalValues_2Bunch.m" had this set to -37 degrees
setLinacPhase( tao, L2MatchStrings, L2PhaseSet )
setLinacGradientAuto( tao, L2MatchStrings, L2EnergyOffset + 4.5e9 - 0.335e9 )
setLinacPhase( tao, L3MatchStrings, L3PhaseSet )
setLinacGradientAuto( tao, L3MatchStrings, L3EnergyOffset + 10.0e9 - 4.5e9 )
def setLinacsHelperLEGACY(tao, L1PhaseSet, L2PhaseSet, L2EnergyOffset, L3EnergyOffset):
"""Pre 2024-09-30 version. Can be deleted"""
[L1MatchStrings, L2MatchStrings, L3MatchStrings, selectMarkers] = getLinacMatchStrings(tao)
# === Make changes to base lattice ===
tao.cmd('set ele L0BF PHI0 = 0') #DNT. "loadNominalValues_2Bunch.m" had this set to zero
tao.cmd('set ele L0BF VOLTAGE = 5.95e7') #DNT. Base value was 7.1067641E+07, new value set to change output energy to 125 MeV (down from 136.5 MeV)
#L1AssertPHI0 = -19 #DNT. "loadNominalValues_2Bunch.m" had this set to -19 degrees
setLinacPhase( tao, L1MatchStrings, L1PhaseSet )
setLinacGradientAuto( tao, L1MatchStrings, 0.335e9 - 0.125e9 )
#L2AssertPHI0 = -37 #DNT. "loadNominalValues_2Bunch.m" had this set to -37 degrees
setLinacPhase( tao, L2MatchStrings, L2PhaseSet )
setLinacGradientAuto( tao, L2MatchStrings, L2EnergyOffset + 4.5e9 - 0.335e9 )
setLinacPhase( tao, L3MatchStrings, 0 )
setLinacGradientAuto( tao, L3MatchStrings, L3EnergyOffset + 10.0e9 - 4.5e9 )
def setBendkG(tao, bendName, integratedFieldkG):
"""Set bend based on EPICS-style integrated field. This involves a sign flip!"""
bendLength = tao.ele_gen_attribs(bendName)["L"]
desiredGradientkG = integratedFieldkG / bendLength
#Bmad uses Tesla and opposite sign!
tao.cmd(f"set ele {bendName} B_FIELD = {-1 * desiredGradientkG/10}")
return
def getBendkG(tao, bendName):
"""Get bend's present EPICS-style integrated field. This involves a sign flip!"""
bendLength = tao.ele_gen_attribs(bendName)["L"]
bendGradientTm = tao.ele_gen_attribs(bendName)["B_FIELD"]
bendGradientkGm = bendGradientTm*10
bendIntegratedFieldkG = bendGradientkGm * bendLength
#Bmad uses opposite sign!
return -1 * bendIntegratedFieldkG
def setQuadkG(tao, quadName, integratedFieldkG):
"""Set quad based on EPICS-style integrated field. This involves a sign flip!"""
quadLength = tao.ele_gen_attribs(quadName)["L"]
desiredGradientkG = integratedFieldkG / quadLength
#Bmad uses Tesla and opposite sign!
tao.cmd(f"set ele {quadName} B1_GRADIENT = {-1 * desiredGradientkG/10}")
return
def getQuadkG(tao, quadName):
"""Get quad's present EPICS-style integrated field. This involves a sign flip!"""
quadLength = tao.ele_gen_attribs(quadName)["L"]
quadGradientTm = tao.ele_gen_attribs(quadName)["B1_GRADIENT"]
quadGradientkGm = quadGradientTm*10
quadIntegratedFieldkG = quadGradientkGm * quadLength
#Bmad uses opposite sign!
return -1 * quadIntegratedFieldkG
def setSextkG(tao, sextName, integratedFieldkG):
"""Set sextupole based on EPICS-style integrated field. This involves a sign flip!"""
sextLength = tao.ele_gen_attribs(sextName)["L"]
desiredGradientkG = integratedFieldkG / sextLength
#Bmad uses Tesla and opposite sign!
tao.cmd(f"set ele {sextName} B2_GRADIENT = {-1 * desiredGradientkG/10}")
return
def getSextkG(tao, sextName):
"""Get sextupoles's present EPICS-style integrated field. This involves a sign flip!"""
sextLength = tao.ele_gen_attribs(sextName)["L"]
sextGradientTm = tao.ele_gen_attribs(sextName)["B2_GRADIENT"]
sextGradientkGm = sextGradientTm*10
sextIntegratedFieldkG = sextGradientkGm * sextLength
#Bmad uses opposite sign!
return -1 * sextIntegratedFieldkG
def setXOffset(tao, eleName, offset):
tao.cmd(f"set ele {eleName} X_OFFSET = {offset}")
return
def setYOffset(tao, eleName, offset):
tao.cmd(f"set ele {eleName} Y_OFFSET = {offset}")
return
def setKickerkG(tao, kickerName, integratedFieldkG):
"""Set HKICKER or VKICKER based on EPICS-style integrated field"""
tao.cmd(f"set ele {kickerName} BL_KICK = {integratedFieldkG/10}")
return
def setAllInjectorQuads(tao, QA10361kG, QA10371kG, QE10425kG, QE10441kG, QE10511kG, QE10525kG):
setQuadkG(tao, "QA10361", QA10361kG)
setQuadkG(tao, "QA10371", QA10371kG)
setQuadkG(tao, "QE10425", QE10425kG)
setQuadkG(tao, "QE10441", QE10441kG)
setQuadkG(tao, "QE10511", QE10511kG)
setQuadkG(tao, "QE10525", QE10525kG)
return
def setAllFinalFocusQuads(tao, Q5FFkG, Q4FFkG, Q3FFkG, Q2FFkG, Q1FFkG, Q0FFkG, Q0DkG, Q1DkG, Q2DkG):
setQuadkG(tao, "Q5FF", Q5FFkG)
setQuadkG(tao, "Q4FF", Q4FFkG)
setQuadkG(tao, "Q3FF", Q3FFkG)
setQuadkG(tao, "Q2FF", Q2FFkG)
setQuadkG(tao, "Q1FF", Q1FFkG)
setQuadkG(tao, "Q0FF", Q0FFkG)
setQuadkG(tao, "Q0D", Q0DkG)
setQuadkG(tao, "Q1D", Q1DkG)
setQuadkG(tao, "Q2D", Q2DkG)
return
def setAllWChicaneBends(tao, B1EkG, B2EkG, B3EkG):
setBendkG(tao, "B1LE", B1EkG)
setBendkG(tao, "B2LE", B2EkG)
setBendkG(tao, "B3LE", B3EkG)
setBendkG(tao, "B3RE", B3EkG)
setBendkG(tao, "B2RE", B2EkG)
setBendkG(tao, "B1RE", B1EkG)
return
def setAllWChicaneQuads(tao, Q1EkG, Q2EkG, Q3EkG, Q4EkG, Q5EkG, Q6EkG):
setQuadkG(tao, "Q1EL", Q1EkG)
setQuadkG(tao, "Q2EL", Q2EkG)
setQuadkG(tao, "Q3EL_1", Q3EkG)
setQuadkG(tao, "Q3EL_2", Q3EkG)
setQuadkG(tao, "Q4EL_1", Q4EkG)
setQuadkG(tao, "Q4EL_2", Q4EkG)
setQuadkG(tao, "Q4EL_3", Q4EkG)
setQuadkG(tao, "Q5EL", Q5EkG)
setQuadkG(tao, "Q6E", Q6EkG)
setQuadkG(tao, "Q5ER", Q5EkG)
setQuadkG(tao, "Q4ER_1", Q4EkG)
setQuadkG(tao, "Q4ER_2", Q4EkG)
setQuadkG(tao, "Q4ER_3", Q4EkG)
setQuadkG(tao, "Q3ER_1", Q3EkG)
setQuadkG(tao, "Q3ER_2", Q3EkG)
setQuadkG(tao, "Q2ER", Q2EkG)
setQuadkG(tao, "Q1ER", Q1EkG)
return
def setAllWChicaneSextupoles(tao, S1ELkG, S2ELkG, S3ELkG, S3ERkG, S2ERkG, S1ERkG):
setSextkG(tao, "S1EL", S1ELkG)
setSextkG(tao, "S2EL", S2ELkG)
setSextkG(tao, "S3EL_1", S3ELkG)
setSextkG(tao, "S3EL_2", S3ELkG)
setSextkG(tao, "S3ER_1", S3ERkG)
setSextkG(tao, "S3ER_2", S3ERkG)
setSextkG(tao, "S2ER", S2ERkG)
setSextkG(tao, "S1ER", S1ERkG)
return
def setAllWChicaneMovers(tao, S1EL_xOffset, S1EL_yOffset, S2EL_xOffset, S2EL_yOffset, S2ER_xOffset, S2ER_yOffset, S1ER_xOffset, S1ER_yOffset):
setXOffset(tao, "S1EL", S1EL_xOffset)
setYOffset(tao, "S1EL", S1EL_yOffset)
setXOffset(tao, "S2EL", S2EL_xOffset)
setYOffset(tao, "S2EL", S2EL_yOffset)
setXOffset(tao, "S2ER", S2ER_xOffset)
setYOffset(tao, "S2ER", S2ER_yOffset)
setXOffset(tao, "S1ER", S1ER_xOffset)
setYOffset(tao, "S1ER", S1ER_yOffset)
return
def setAllFinalFocusKickers(tao, XC1FFkG, XC3FFkG, YC1FFkG, YC2FFkG):
setKickerkG(tao, "XC1FF", XC1FFkG)
setKickerkG(tao, "XC3FF", XC3FFkG)
setKickerkG(tao, "YC1FF", YC1FFkG)
setKickerkG(tao, "YC2FF", YC2FFkG)
return