-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path003_prepare_acceleration.py
223 lines (188 loc) · 10.1 KB
/
003_prepare_acceleration.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
import xtrack as xt
import pandas as pd
import xdeps as xd
import numpy as np
from simulation_parameters import parameters as p
print('*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~**~*~*~**~*~*~**~*~*~*')
print('003_prepare_acceleration.py')
print('*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~**~*~*~**~*~*~**~*~*~*')
if p['prepare_acceleration']==2:
print('prepare_acceleration = 2')
print('Double RF; with acceleration.')
#########################################
# Load PSB line in xsuite
#########################################
line = xt.Line.from_json('psb/psb_line_thick.json')
line.build_tracker()
#########################################
# Load momentum and RF functions
#########################################
fname = 'time_tables/Ramp_and_RF_functions.dat'
df = pd.read_csv(fname, sep='\t', skiprows=2,
names=['t_s', 'E_kin_GeV', 'V1_MV', 'phi1_rad', 'V2_MV', 'phi2_rad'])
print('Momentum and RF functions loaded from %s.'%(fname))
#########################################
# Set line energy program
#########################################
ep = xt.EnergyProgram(t_s=df['t_s'].values, kinetic_energy0=df['E_kin_GeV'].values*1e9)
line.energy_program = ep
print('Line energy program set.')
########################################
# RF frequency functions
########################################
t_s = df['t_s'].values
freq_rev = line.energy_program.get_frev_at_t_s(t_s) # Revolution frequency at selected times
#freq_rev
# Create interpolating functions and variables
line.functions['fun_freq_rev'] = xd.FunctionPieceWiseLinear(x=t_s, y=freq_rev)
line.vars['freq_rev'] = line.functions['fun_freq_rev'](line.vars['t_turn_s'])
# Create variables for first and second harmonics
line.vars['freq_h1'] = line.vars['freq_rev']
line.vars['freq_h2'] = 2 * line.vars['freq_rev']
print('RF frequency functions built and attached to line.')
########################################
# RF voltage and phase programs
########################################
V1_MV = df.V1_MV.values # comes from the file
V2_MV = df.V2_MV.values # comes from the file
# Shift phases to have the beam centered around zero
phi1_rad = df.phi1_rad.values - np.pi
phi2_rad = df.phi2_rad.values - np.pi
# Build interpolating functions and variables for voltages
line.functions['fun_volt_mv_h1'] = xd.FunctionPieceWiseLinear(x=t_s, y=V1_MV)
line.functions['fun_volt_mv_h2'] = xd.FunctionPieceWiseLinear(x=t_s, y=V2_MV)
line.vars['volt_mv_h1'] = line.functions['fun_volt_mv_h1'](line.vars['t_turn_s'])
line.vars['volt_mv_h2'] = line.functions['fun_volt_mv_h2'](line.vars['t_turn_s'])
# Build interpolating functions and variables for voltages
line.functions['fun_phi_rad_h1'] = xd.FunctionPieceWiseLinear(x=t_s, y=phi1_rad)
line.functions['fun_phi_rad_h2'] = xd.FunctionPieceWiseLinear(x=t_s, y=phi2_rad)
line.vars['phi_rad_h1'] = line.functions['fun_phi_rad_h1'](line.vars['t_turn_s'])
line.vars['phi_rad_h2'] = line.functions['fun_phi_rad_h2'](line.vars['t_turn_s'])
print('RF voltage and phase programs built and attached to line.')
########################################
# Attach variables to cavities
########################################
line.element_refs['br1.acwf5l1.1'].voltage = line.vars['volt_mv_h1'] * 1e6
line.element_refs['br1.acwf5l1.2'].voltage = line.vars['volt_mv_h2'] * 1e6
line.element_refs['br1.acwf5l1.1'].lag = line.vars['phi_rad_h1'] * 360 / 2 / np.pi
line.element_refs['br1.acwf5l1.2'].lag = line.vars['phi_rad_h2'] * 360 / 2 / np.pi
line.element_refs['br1.acwf5l1.1'].frequency = line.vars['freq_h1']
line.element_refs['br1.acwf5l1.2'].frequency = line.vars['freq_h2']
print('Voltage and phase functions attached to cavities.')
#########################################
# Save line to .json
#########################################
line.vars['t_turn_s']=0.0 # Reset time at the injection
if p['twiss_mode']=='6d':
line.twiss_default['method'] = '6d' # now with cavity
print('Twiss method set to 6d.')
print('Guessing fixed point location at zeta0=%1.2f m.'%p['zeta0'])
line.twiss(zeta0=p['zeta0']) # double RF unstable at zeta=0, guessing fixed point location at z=p['zeta0'] m
line.to_json('psb/psb_line_thick.json')
print('Line saved to psb/psb_line_thick.json')
elif p['prepare_acceleration']==3:
print('prepare_acceleration = 3')
print('Tripple RF; with acceleration.')
#########################################
# Load PSB line in xsuite
#########################################
line = xt.Line.from_json('psb/psb_line_thick.json')
line.build_tracker()
#########################################
# Load momentum and RF functions
#########################################
fname = 'time_tables/RF_TripleHarm.dat'
df = pd.read_csv(fname, sep='\t', skiprows=2,
names=['t_s', 'E_kin_GeV', 'V1_MV', 'phi1_rad', 'V2_MV', 'phi2_rad', 'V3_MV', 'phi3_rad'])
print('Momentum and RF functions loaded from %s.'%(fname))
#########################################
# Set line energy program
#########################################
ep = xt.EnergyProgram(t_s=df['t_s'].values, kinetic_energy0=df['E_kin_GeV'].values*1e9)
line.energy_program = ep
print('Line energy program set.')
########################################
# RF frequency functions
########################################
t_s = df['t_s'].values
freq_rev = line.energy_program.get_frev_at_t_s(t_s) # Revolution frequency at selected times
#freq_rev
# Create interpolating functions and variables
line.functions['fun_freq_rev'] = xd.FunctionPieceWiseLinear(x=t_s, y=freq_rev)
line.vars['freq_rev'] = line.functions['fun_freq_rev'](line.vars['t_turn_s'])
# Create variables for first and second harmonics
line.vars['freq_h1'] = line.vars['freq_rev']
line.vars['freq_h2'] = 2 * line.vars['freq_rev']
line.vars['freq_h3'] = 3 * line.vars['freq_rev']
print('RF frequency functions built and attached to line.')
########################################
# RF voltage and phase programs
########################################
V1_MV = df.V1_MV.values # comes from the file
V2_MV = df.V2_MV.values # comes from the file
V3_MV = df.V3_MV.values # comes from the file
# Shift phases to have the beam centered around zero
phi1_rad = df.phi1_rad.values - np.pi
phi2_rad = df.phi2_rad.values - np.pi
phi3_rad = df.phi3_rad.values - np.pi
# Build interpolating functions and variables for voltages
line.functions['fun_volt_mv_h1'] = xd.FunctionPieceWiseLinear(x=t_s, y=V1_MV)
line.functions['fun_volt_mv_h2'] = xd.FunctionPieceWiseLinear(x=t_s, y=V2_MV)
line.functions['fun_volt_mv_h3'] = xd.FunctionPieceWiseLinear(x=t_s, y=V3_MV)
line.vars['volt_mv_h1'] = line.functions['fun_volt_mv_h1'](line.vars['t_turn_s'])
line.vars['volt_mv_h2'] = line.functions['fun_volt_mv_h2'](line.vars['t_turn_s'])
line.vars['volt_mv_h3'] = line.functions['fun_volt_mv_h3'](line.vars['t_turn_s'])
# Build interpolating functions and variables for voltages
line.functions['fun_phi_rad_h1'] = xd.FunctionPieceWiseLinear(x=t_s, y=phi1_rad)
line.functions['fun_phi_rad_h2'] = xd.FunctionPieceWiseLinear(x=t_s, y=phi2_rad)
line.functions['fun_phi_rad_h3'] = xd.FunctionPieceWiseLinear(x=t_s, y=phi3_rad)
line.vars['phi_rad_h1'] = line.functions['fun_phi_rad_h1'](line.vars['t_turn_s'])
line.vars['phi_rad_h2'] = line.functions['fun_phi_rad_h2'](line.vars['t_turn_s'])
line.vars['phi_rad_h3'] = line.functions['fun_phi_rad_h3'](line.vars['t_turn_s'])
print('RF voltage and phase programs built and attached to line.')
########################################
# Attach variables to cavities
########################################
line.element_refs['br1.acwf5l1.1'].voltage = line.vars['volt_mv_h1'] * 1e6
line.element_refs['br1.acwf5l1.2'].voltage = line.vars['volt_mv_h2'] * 1e6
line.element_refs['br1.acwf7l1.2'].voltage = line.vars['volt_mv_h3'] * 1e6
line.element_refs['br1.acwf5l1.1'].lag = line.vars['phi_rad_h1'] * 360 / 2 / np.pi
line.element_refs['br1.acwf5l1.2'].lag = line.vars['phi_rad_h2'] * 360 / 2 / np.pi
line.element_refs['br1.acwf7l1.2'].lag = line.vars['phi_rad_h3'] * 360 / 2 / np.pi
line.element_refs['br1.acwf5l1.1'].frequency = line.vars['freq_h1']
line.element_refs['br1.acwf5l1.2'].frequency = line.vars['freq_h2']
line.element_refs['br1.acwf7l1.2'].frequency = line.vars['freq_h3']
print('Voltage and phase functions attached to cavities.')
#########################################
# Save line to .json
#########################################
line.vars['t_turn_s']=0.0 # Reset time at the injection
if p['twiss_mode']=='6d':
line.twiss_default['method'] = '6d' # now with cavity
print('Twiss method set to 6d.')
print('Guessing fixed point location at zeta0=%1.2f m.'%p['zeta0'])
line.twiss(zeta0=p['zeta0']) # double RF unstable at zeta=0, guessing fixed point location at z=p['zeta0'] m
line.to_json('psb/psb_line_thick.json')
print('Line saved to psb/psb_line_thick.json')
else:
print('prepare_acceleration = 0 or 1')
print('Single RF; no acceleration included.')
#########################################
# Load PSB line in xsuite
#########################################
line = xt.Line.from_json('psb/psb_line_thick.json')
line.build_tracker()
#########################################
# Add constant voltage to the dummy RF
#########################################
line.element_refs['br.c02'].voltage = 0.008*1e6
print('Constant voltage = 8 kV added to the dummy RF.')
#########################################
# Save line to .json
#########################################
line.vars['t_turn_s']=0.0 # Reset time at the injection
line.twiss_default['method'] = '6d' # now with cavity
print('Twiss method set to 6d.')
line.twiss()
line.to_json('psb/psb_line_thick.json')
print('Line saved to psb/psb_line_thick.json')