-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsad2xs_knobs.py
175 lines (143 loc) · 7.21 KB
/
sad2xs_knobs.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
"""
Knob Generation for SAD to XSuite Conversion
=============================================
Author(s): John P T Salvesen, Giovanni Iadarola
Email: [email protected]
Date: 20-11-2024
"""
################################################################################
# Required Packages
################################################################################
import numpy as np
################################################################################
# Helper Functions
################################################################################
def get_element_class(element_name):
"""
Get the base string of an element name
Base strings used to define powering groups
############################################################################
Parameters:
############################################################################
element_name: str
The name of the element
############################################################################
Outputs
############################################################################
element_class: string
The base string of the element name
"""
# Default to the element name
element_class = element_name
# Split the element name by the last period
substrings = element_name.rsplit('.', 1)
# If the element name has a period, the base string is the first substring
if len(substrings) == 2 and substrings[1].isdigit():
element_class = substrings[0]
# If the name starts with a minus sign, remove it
if element_class.startswith('-'):
element_class = element_class[1:]
# If there is a minus sign elsewhere, raise an error
if '-' in element_class:
raise ValueError(f'Element name {element_name} contains a minus sign')
return element_class
################################################################################
# Conversion Function
################################################################################
def sad2xsuite_knobs(line):
"""
Generate Xsuite Line variables for an imported lattice
Done according to naming of elements
############################################################################
Parameters:
############################################################################
line: xtrack.Line
Xsuite Line object representing the lattice
############################################################################
Outputs
############################################################################
line: xtrack.Line
Xsuite Line object representing the lattice
"""
############################################################################
# Build Line Table
############################################################################
line.build_tracker()
line_table = line.get_table(attr=True)
line.discard_tracker()
############################################################################
# Bend Knobs
############################################################################
bend_names = line_table.rows[line_table['element_type'] == 'Bend'].name
for bend_name in bend_names:
# Get the root name ignoring any numbering
bend_class = get_element_class(bend_name)
# If the bend has a k0, create a variable
if line[bend_name].k0 != 0:
line.vars[f'k0_{bend_class}'] = line[bend_name].k0
# Assign the variable to the bend
line.element_refs[bend_name].k0 = line.vars[f'k0_{bend_class}']
# If the bend has a h value, and it's close to k0
# Assign to the same variable
if line[bend_name].h != 0:
if np.isclose(line[bend_name].h, line[bend_name].k0, rtol=1E-3):
line.element_refs[bend_name].h = line.vars[f'k0_{bend_class}']
# No such thing as k0s in xtrack, it is controlled by s_rotation
# If the bend has a k1, create a variable
if line[bend_name].k1 != 0:
line.vars[f'k1_{bend_class}'] = line[bend_name].k1
# Assign the variable to the bend
line.element_refs[bend_name].k1 = line.vars[f'k1_{bend_class}']
############################################################################
# Quadrupole Knobs
############################################################################
quad_names = line_table.rows[line_table['element_type'] == 'Quadrupole'].name
for quad_name in quad_names:
# Get the root name ignoring any numbering
quad_class = get_element_class(quad_name)
# If the quad has a k1, create a variable
if line[quad_name].k1 != 0:
line.vars[f'k1_{quad_class}'] = line[quad_name].k1
# Assign the variable to the bend
line.element_refs[quad_name].k1 = line.vars[f'k1_{quad_class}']
# If the quad has a k1s, create a variable
if line[quad_name].k1s != 0:
line.vars[f'k1s_{quad_class}'] = line[quad_name].k1s
# Assign the variable to the bend
line.element_refs[quad_name].k1s = line.vars[f'k1s_{quad_class}']
############################################################################
# Sextupole Knobs
############################################################################
sext_names = line_table.rows[line_table['element_type'] == 'Sextupole'].name
for sext_name in sext_names:
# Get the root name ignoring any numbering
sext_class = get_element_class(sext_name)
# If the quad has a k1, create a variable
if line[sext_name].k2 != 0:
line.vars[f'k2_{sext_class}'] = line[sext_name].k2
# Assign the variable to the bend
line.element_refs[sext_name].k2 = line.vars[f'k2_{sext_class}']
# If the quad has a k2s, create a variable
if line[sext_name].k2s != 0:
line.vars[f'k2s_{sext_class}'] = line[sext_name].k2s
# Assign the variable to the bend
line.element_refs[sext_name].k2s = line.vars[f'k2s_{sext_class}']
############################################################################
# RF Cavities
############################################################################
cavi_names = line_table.rows[line_table['element_type'] == 'Cavity'].name
for cavi_name in cavi_names:
# Get the root name ignoring any numbering
cavi_class = get_element_class(cavi_name)
# Create cavity variables
line.vars[f'volt_{cavi_class}'] = line[cavi_name].voltage
line.vars[f'freq_{cavi_class}'] = line[cavi_name].frequency
line.vars[f'lag_{cavi_class}'] = line[cavi_name].lag
# Assign the variables to the cavity
line.element_refs[cavi_name].voltage = line.vars[f'volt_{cavi_class}']
line.element_refs[cavi_name].frequency = line.vars[f'freq_{cavi_class}']
line.element_refs[cavi_name].lag = line.vars[f'lag_{cavi_class}']
############################################################################
# Return Line
############################################################################
return line