-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmacros.cmdx
261 lines (241 loc) · 10.1 KB
/
macros.cmdx
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
! A.Mereghetti, 2021-01-19
! a file collecting MADX macros
! general aperture outside magnets
aper1=0.0; ! [m]
aper2=0.0; ! [m]
aper3=0.0; ! [m]
aper4=0.0; ! [m]
CNAO_APE: MARKER, APERTYPE=RECTANGLE, APERTURE={aper1,aper2,aper3,aper4};
! longitudinal shift of added apertures
tmpD=10.0E-06; ! [m]
! add aperture markers upstream/downstream of a given element
! NB: aperType and aper1,aper2,aper3,aper4 and offx,offy should be defined before calling the macro;
addAperture(eleName,apeType,mySeq,myGenApe): macro = {
eleName, APERTYPE=apeType, APERTURE={aper1,aper2,aper3,aper4}, APER_OFFSET={offx,offy};
eleName_EN: MARKER, APERTYPE=apeType, APERTURE={aper1,aper2,aper3,aper4}, APER_OFFSET={offx,offy};
eleName_EX: MARKER, APERTYPE=apeType, APERTURE={aper1,aper2,aper3,aper4}, APER_OFFSET={offx,offy};
SEQEDIT, sequence=mySeq;
Install, element=eleName_EN, at=-0.5*eleName->L- tmpD, from=eleName;
Install, element=myGenApe , at=-0.5*eleName->L-2*tmpD, from=eleName;
Install, element=myGenApe , at= 0.5*eleName->L+2*tmpD, from=eleName;
Install, element=eleName_EX, at= 0.5*eleName->L+ tmpD, from=eleName;
ENDEDIT;
};
! add aperture markers at beginning/end of lattice sequence
contourSeqAperture(mySeq,myGenApe): macro = {
SEQEDIT, sequence=mySeq;
Install, element=myGenApe, at=0.0;
Install, element=myGenApe, at=mySeq_L;
ENDEDIT;
};
! save a .tfs file with linear optics functions in a specific column order
! please specify if the sequence is in thin or thick lens (is_thin<>0).
! in case of an open line (e.g. a transfer line, the gantry, etc...), use
! initial conditions to linear optics functions (flagged as initial).
saveOpticsTFS(fileName): macro = {
select,flag=twiss,clear;
select,flag=twiss,column=name,keyword,l,s,betx,alfx,bety,alfy,x,px,y,py,dx,dpx,dy,dpy,mux,muy;
if (is_open<>0) {
! a transfer line: we need the initial conditions
if (is_thin>0) {
twiss, rmatrix=true, file=fileName_optics_thin.tfs, beta0=initial;
} else {
twiss, rmatrix=true, file=fileName_optics.tfs, beta0=initial;
};
} else {
! a ring: optics is computed as self-consisten solution
if (is_thin>0) {
twiss, rmatrix=true, file=fileName_optics_thin.tfs;
} else {
twiss, rmatrix=true, file=fileName_optics.tfs;
};
};
};
! save a .tfs file with geometry infos (strengths, aperture, etc...) in a specific column order
! please specify if the sequence is in thin or thick lens (is_thin<>0).
! in case of an open line (e.g. a transfer line, the gantry, etc...), use
! initial conditions to linear optics functions (flagged as initial).
saveGeometryTFS(fileName): macro = {
select,flag=twiss,clear;
select,flag=twiss,column=name,keyword,l,s,kick,hkick,vkick,angle,k0l,k1l,k2l,apertype,aper_1,aper_2,aper_3,aper_4,apoff_1,apoff_2;
if (is_open<>0) {
! a transfer line: we need the initial conditions
if (is_thin>0) {
twiss, rmatrix=true, file=fileName_geometry_thin.tfs, beta0=initial;
} else {
twiss, rmatrix=true, file=fileName_geometry.tfs, beta0=initial;
};
} else {
! a ring: optics is computed as self-consisten solution
if (is_thin>0) {
twiss, rmatrix=true, file=fileName_geometry_thin.tfs;
} else {
twiss, rmatrix=true, file=fileName_geometry.tfs;
};
};
};
! save a .tfs file with main elements of R matrix, at centre of thick elements!
! please specify if the sequence is in thin or thick lens (is_thin<>0).
! in case of an open line (e.g. a transfer line, the gantry, etc...), use
! initial conditions to linear optics functions (flagged as initial).
saveRmatrixTFS(fileName): macro = {
select,flag=twiss,clear;
select,flag=twiss,column=name,keyword,l,s,re11,re12,re21,re22,re16,re26,re33,re34,re43,re44,re36,re46,re51,re52,re55,re56,re66;
if (is_open<>0) {
! a transfer line: we need the initial conditions
if (is_thin>0) {
twiss, rmatrix=true, centre=true, file=fileName_Rmatrix_thin.tfs, beta0=initial;
} else {
twiss, rmatrix=true, centre=true, file=fileName_Rmatrix.tfs, beta0=initial;
};
} else {
! a ring: optics is computed as self-consisten solution
if (is_thin>0) {
twiss, rmatrix=true, centre=true, file=fileName_Rmatrix_thin.tfs;
} else {
twiss, rmatrix=true, centre=true, file=fileName_Rmatrix.tfs;
};
};
};
! save an apic.txt file
! please specify if the sequence is in thin or thick lens (is_thin<>0).
! in case of an open line (e.g. a transfer line, the gantry, etc...), use
! initial conditions to linear optics functions (flagged as initial).
saveAPIC(): macro = {
select,flag=twiss,clear;
select,flag=twiss,COLUMN=NAME, KEYWORD, L, TILT, ANGLE, E1, E2, K1L, K2L, K3L, KS;
if (is_open<>0) {
! a transfer line: we need the initial conditions
twiss, rmatrix=true, file=apic.txt, beta0=initial;
} else {
! a ring: optics is computed as self-consisten solution
twiss, rmatrix=true, file=apic.txt;
};
};
! insert a thin kicker up/dostream of a specific element
surroundKICKER(MagName,SeqName): macro={
MagName->HKICK=0.0; MagName->VKICK=0.0;
MagNamePRIMA: KICKER, L= 0.0, HKICK := MagName_HKICK/2, VKICK := MagName_VKICK/2;
MagNameDOPO: KICKER, L= 0.0, HKICK := MagName_HKICK/2, VKICK := MagName_VKICK/2;
SEQEDIT, sequence=SeqName;
Install, element=MagNamePRIMA, at=-0.5*MagName->L, from=MagName;
Install, element=MagNameDOPO , at= 0.5*MagName->L, from=MagName;
ENDEDIT;
};
surroundMBEND(MagName,SeqName): macro={
MagName->ANGLE=0.0; MagName->K0=0.0;
MagNamePRIMA: KICKER, L= 0.0, HKICK := MagName_ANGLE/2;
MagNameDOPO: KICKER, L= 0.0, HKICK := MagName_ANGLE/2;
SEQEDIT, sequence=SeqName;
Install, element=MagNamePRIMA, at=-0.5*MagName->L, from=MagName;
Install, element=MagNameDOPO , at= 0.5*MagName->L, from=MagName;
ENDEDIT;
};
! set beam rigidity and other realtivistic quantities based on beam energy
setRigidity(): macro = {
! input:
! - Ek [MeV/n]: kinetic energy per nucleon;
! - A0,Z0 []: nuclear mass, atomic number;
! - m0 [MeV]: rest mass;
gammaRel=(Ek*A0)/m0+1;
betaGammaRel=sqrt(gammaRel^2-1);
betaRel=betaGammaRel/gammaRel;
Brho=betaGammaRel*m0/(CLIGHT*1E-6*Z0);
! value, Ek, A0, m0, gammaRel, betaGammaRel, betaRel, Brho;
};
setBeam(mySequence): macro = {
! AM: set crazy energies, otherwise dispersion will be divided by beta_rel...
beam, particle=proton, energy=1000, sequence=mySequence;
if ( is_carbon == 0 ) {
! properties of protons
A0=1; ! []
Z0=1; ! []
m0=PMASS*1E3; ! [MeV]
! m0=938.255; ! rampGen [MeV]
} else {
! protperties of carbon ions
A0=12; ! []
Z0=6; ! []
m0=A0*NMASS*1E3; ! [MeV]
};
};
! save Rmatrix at specific element
! NB: please remember to use and twiss your sequence beforehand, e.g.:
! use, sequence=gantry;
! twiss, table=twiss, rmatrix=true, beta0=initial;
save_RMatrix(eleName): macro = {
REMOVEFILE, FILE="Rmatrix.txt";
assign, echo="Rmatrix.txt";
PRINTF, TEXT="# | % 23.0f % 23.0f % 23.0f % 23.0f % 23.0f % 23.0f", VALUE=1,2,3,4,5,6;
PRINTF, TEXT="#--|------------------------------------------------------------------------------------------------------------------------------------------------", VALUE=0;
n=1;
WHILE (n<=6) {
exec, save_RMatrix_iLine(eleName,$n);
n=n+1;
};
assign, echo=terminal;
};
save_RMatrix_iLine(eleName,iLine): macro = {
PRINTF, TEXT="%2.0f | % 23.16E % 23.16E % 23.16E % 23.16E % 23.16E % 23.16E",
VALUE=iLine,table(twiss,eleName,reiLine1),table(twiss,eleName,reiLine2),table(twiss,eleName,reiLine3),
table(twiss,eleName,reiLine4),table(twiss,eleName,reiLine5),table(twiss,eleName,reiLine6);
};
show_kIVals_element(eleName): macro = {
tmpK0=eleName->ANGLE/eleName->L;
PRINTF, TEXT=eleName % 24.16E % 24.16E % 24.16E % 24.16E, VALUE=tmpK0,tmpK0*Brho,eleName->K1,eleName->K1*Brho;
};
save_strengths(fileName,beamLine): macro = {
! beamLine: MEBT, synchro, HEBT;
REMOVEFILE, FILE=fileName.str;
assign, echo=filename.str;
print, TEXT="! general infos:";
print, TEXT="! - kinetick energy [MeV/n]";
print, TEXT="! - Range [mm]";
value, Ek;
value, Bp;
exec, save_strengths_beamLine();
assign, echo=terminal;
};
insertMarkerUpstreamOf(MyMarkerName,UpEleName,MySeqName): macro = {
SEQEDIT, SEQUENCE=MySeqName;
INSTALL, ELEMENT=MyMarkerName, CLASS=MARKER, AT=-0.5*UpEleName->L, FROM=UpEleName;
ENDEDIT;
};
! convert current into K1, using a linear dependence
I2K_QUs_lin(MagName,biro) : macro {
! the macro expects IMagName, KMagName and FMagName to be defined
! eg. H2_012A_QUE -> I_H2_012A_QUE, K1_H2_012A_QUE, F_H2_012A_QUE
! ...as well as coefficients of the polynomial
! eg. H2_012A_QUE -> a0_LIN_H2_012A_QUE and a1_LIN_H2_012A_QUE
K1_MagName = ( a0_LIN_MagName +a1_LIN_MagName*abs(I_MagName) ) /biro *abs(F_MagName);
if (I_MagName<0){ K1_MagName=-K1_MagName; };
};
! convert current into K1, using a fifth order polynomial
I2K_QUs_fifth(MagName,biro) : macro {
! the macro expects IMagName, KMagName and FMagName to be defined
! eg. H2_012A_QUE -> I_H2_012A_QUE, K1_H2_012A_QUE, F_H2_012A_QUE
! ...as well as coefficients of the polynomial
! eg. H2_012A_QUE -> a0_FIF_H2_012A_QUE, a1_FIF_H2_012A_QUE, ... and a5_FIF_H2_012A_QUE
K1_MagName = ( a0_FIF_MagName
+a1_FIF_MagName*abs(I_MagName)
+a2_FIF_MagName*abs(I_MagName)^2
+a3_FIF_MagName*abs(I_MagName)^3
+a4_FIF_MagName*abs(I_MagName)^4
+a5_FIF_MagName*abs(I_MagName)^5) /biro *abs(F_MagName);
if (I_MagName<0){ K1_MagName=-K1_MagName; };
};
! duplicate an existing sequence
duplicateSequence(SeqInName,SeqOutName): macro {
SeqOutName_L=SeqInName_L;
SeqOutName: sequence, l=SeqOutName_L, refer=entry;
SeqInName, at=0;
endsequence;
};
! concatenate two existing sequences
concatenate2Sequences(SeqInName1,SeqInName2,SeqOutName): macro {
SeqOutName_L=SeqInName1_L+SeqInName2_L;
SeqOutName: sequence, l=SeqOutName_L, refer=entry;
SeqInName1, at=0;
SeqInName2, at=SeqInName1_L;
endsequence;
};