Skip to content

Commit 0f434d1

Browse files
authored
Merge pull request #1 from freddieknets/master
From frederik
2 parents ee92453 + 58036fe commit 0f434d1

14 files changed

+1481
-0
lines changed

BetaBeating.madx

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
! Calculate beta beating at the BPMs
2+
! Values are outputted in the main output
3+
! Todo: retrieve this output with SixDB
4+
!
5+
! This file should be called twice:
6+
! once before the assignment of errors (with crossing angle still on), and
7+
! once at the end of the mask (just before sending to SixTrack)
8+
!
9+
! F.F. Van der Veken, 19 March 2019
10+
11+
if (second_run_beta_beating==0){
12+
13+
second_run_beta_beating=1;
14+
15+
! unperturbed twiss at the bpms
16+
select, flag=twiss, clear;
17+
select, flag=twiss, pattern=BPM, column=name,s,betx,bety;
18+
twiss,file="twiss_bpm_unperturbed.tfs";
19+
20+
} else {
21+
22+
second_run_beta_beating=0;
23+
24+
! perturbed twiss at the bpms
25+
select, flag=twiss, clear;
26+
select, flag=twiss, pattern=BPM, column=name,s,betx,bety;
27+
twiss,file="twiss_bpm_perturbed.tfs";
28+
29+
assign, echo="betabeat_tempcode.py",truncate;
30+
31+
print,text="import csv";
32+
print,text="import os";
33+
print,text="";
34+
print,text="if (os.path.isfile('twiss_bpm_unperturbed.tfs') and os.path.isfile('twiss_bpm_perturbed.tfs')):";
35+
print,text=" with open('twiss_bpm_unperturbed.tfs') as f:";
36+
print,text=" reader = csv.reader(f, delimiter=' ')";
37+
print,text=" unperturbed = list(reader)";
38+
print,text=" with open('twiss_bpm_perturbed.tfs') as f:";
39+
print,text=" reader = csv.reader(f, delimiter=' ')";
40+
print,text=" perturbed = list(reader)";
41+
print,text=" ";
42+
print,text=" unperturbed[:] = list(map(lambda line: [x for x in line if x != ''], [x for x in unperturbed if x[0] == ''] ))";
43+
print,text=" unperturbed[:] = list(map(lambda line: [line[0],float(line[1]),float(line[2]),float(line[3])], unperturbed))";
44+
print,text=" unperturbed.sort(key=lambda x: x[1])";
45+
print,text=" unperturbed_elements = [x[0] for x in unperturbed]";
46+
print,text=" ";
47+
print,text=" perturbed[:] = list(map(lambda line: [x for x in line if x != ''], [x for x in perturbed if x[0] == ''] ))";
48+
print,text=" perturbed[:] = list(map(lambda line: [line[0],float(line[1]),float(line[2]),float(line[3])], perturbed))";
49+
print,text=" perturbed.sort(key=lambda x: x[1])";
50+
print,text=" perturbed_elements = [x[0] for x in perturbed]";
51+
print,text=" ";
52+
print,text=" if (unperturbed_elements == perturbed_elements):";
53+
print,text=" print('')";
54+
print,text=" print('Beta beating calculated at the BPMs:')";
55+
print,text=" beating=list(map(lambda x, y: [ x[0], str((x[1]+y[1])/2), str( (y[2]-x[2])/x[2]), str( (y[3]-x[3])/x[3]) ], unperturbed, perturbed))";
56+
print,text=" beating.insert(0,['NAME','s','betax_beat','betay_beat'])";
57+
print,text=" col_width = max(len(elem) for line in beating for elem in line) + 2 # padding";
58+
print,text=" for line in beating:";
59+
print,text=" print(line[0].ljust(col_width)+line[1].ljust(col_width)+line[2].ljust(col_width)+line[3].ljust(col_width))";
60+
print,text=" print('')";
61+
print,text=" else:";
62+
print,text=" print('Warning: twiss_bpm_unperturbed.tfs and twiss_bpm_perturbed.tfs have different elements!\nAborted calculation of beta beating.\n')";
63+
print,text="else:";
64+
print,text=" print('Warning: twiss_bpm_unperturbed.tfs and/or twiss_bpm_perturbed.tfs is missing!\nAborted calculation of beta beating.\n')";
65+
assign;
66+
67+
System,"python betabeat_tempcode.py";
68+
System,"rm betabeat_tempcode.py";
69+
System,"rm twiss_bpm_unperturbed.tfs";
70+
System,"rm twiss_bpm_perturbed.tfs";
71+
72+
};

BetaBeatingAll.madx

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
! Calculate beta beating at the BPMs
2+
! Values are outputted in the main output
3+
! Todo: retrieve this output with SixDB
4+
!
5+
! This file should be called twice:
6+
! once before the assignment of errors (with crossing angle still on), and
7+
! once at the end of the mask (just before sending to SixTrack)
8+
!
9+
! F.F. Van der Veken, 19 March 2019
10+
11+
if (second_run_beta_beating==0){
12+
13+
second_run_beta_beating=1;
14+
15+
! unperturbed twiss at the bpms
16+
select, flag=twiss, clear;
17+
select, flag=twiss, pattern="^[^D].*$", column=name,s,betx,bety;
18+
twiss,file="twiss_all_unperturbed.tfs";
19+
20+
} else {
21+
22+
second_run_beta_beating=0;
23+
24+
! perturbed twiss at the bpms
25+
select, flag=twiss, clear;
26+
select, flag=twiss, pattern="^[^D].*$", column=name,s,betx,bety;
27+
twiss,file="twiss_all_perturbed.tfs";
28+
29+
assign, echo="betabeat_tempcode.py",truncate;
30+
31+
print,text="import csv";
32+
print,text="import os";
33+
print,text="";
34+
print,text="if (os.path.isfile('twiss_all_unperturbed.tfs') and os.path.isfile('twiss_all_perturbed.tfs')):";
35+
print,text=" with open('twiss_all_unperturbed.tfs') as f:";
36+
print,text=" reader = csv.reader(f, delimiter=' ')";
37+
print,text=" unperturbed = list(reader)";
38+
print,text=" with open('twiss_all_perturbed.tfs') as f:";
39+
print,text=" reader = csv.reader(f, delimiter=' ')";
40+
print,text=" perturbed = list(reader)";
41+
print,text=" ";
42+
print,text=" unperturbed[:] = list(map(lambda line: [x for x in line if x != ''], [x for x in unperturbed if x[0] == ''] ))";
43+
print,text=" unperturbed[:] = list(map(lambda line: [line[0],float(line[1]),float(line[2]),float(line[3])], unperturbed))";
44+
print,text=" unperturbed.sort(key=lambda x: x[1])";
45+
print,text=" unperturbed_elements = [x[0] for x in unperturbed]";
46+
print,text=" ";
47+
print,text=" perturbed[:] = list(map(lambda line: [x for x in line if x != ''], [x for x in perturbed if x[0] == ''] ))";
48+
print,text=" perturbed[:] = list(map(lambda line: [line[0],float(line[1]),float(line[2]),float(line[3])], perturbed))";
49+
print,text=" perturbed.sort(key=lambda x: x[1])";
50+
print,text=" perturbed_elements = [x[0] for x in perturbed]";
51+
print,text=" ";
52+
print,text=" if (unperturbed_elements == perturbed_elements):";
53+
print,text=" print('')";
54+
print,text=" print('Beta beating calculated at every element:')";
55+
print,text=" beating=list(map(lambda x, y: [ x[0], str((x[1]+y[1])/2), str( (y[2]-x[2])/x[2]), str( (y[3]-x[3])/x[3]) ], unperturbed, perturbed))";
56+
print,text=" beating.insert(0,['NAME','s','betax_beat','betay_beat'])";
57+
print,text=" col_width = max(len(elem) for line in beating for elem in line) + 2 # padding";
58+
print,text=" for line in beating:";
59+
print,text=" print(line[0].ljust(col_width)+line[1].ljust(col_width)+line[2].ljust(col_width)+line[3].ljust(col_width))";
60+
print,text=" print('')";
61+
print,text=" else:";
62+
print,text=" print('Warning: twiss_all_unperturbed.tfs and twiss_all_perturbed.tfs have different elements!\nAborted calculation of beta beating.\n')";
63+
print,text="else:";
64+
print,text=" print('Warning: twiss_all_unperturbed.tfs and/or twiss_all_perturbed.tfs is missing!\nAborted calculation of beta beating.\n')";
65+
assign;
66+
67+
System,"python betabeat_tempcode.py";
68+
System,"rm betabeat_tempcode.py";
69+
System,"rm twiss_all_unperturbed.tfs";
70+
System,"rm twiss_all_perturbed.tfs";
71+
72+
};

FineCouplingCorrection.madx

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
! Fine tuning of coupling correction
2+
! S.Fartoukh March 2009
3+
! 2011/11/21 From SLHCV3.0 R. De Maria
4+
! 2016/04/27 R. De Maria & F.F Van der Veken
5+
! work around mad-x bug on integer part of the tune in presence of coupling for ctap calculation
6+
7+
if(mylhcbeam==1)
8+
{kqtf=kqtf.b1;kqtf0=kqtf.b1;
9+
kqtd=kqtd.b1;kqtd0=kqtd.b1;
10+
kqtf.b1:=kqtf;
11+
kqtd.b1:=kqtd;};
12+
13+
if(mylhcbeam > 1)
14+
{kqtf=kqtf.b2;kqtf0=kqtf.b2;
15+
kqtd=kqtd.b2;kqtd0=kqtd.b2;
16+
kqtf.b2:=kqtf;
17+
kqtd.b2:=kqtd;};
18+
19+
20+
! closest tune
21+
qmid=(qx0-qx00+qy0-qy00)*0.5;
22+
23+
match;
24+
global, q1=qx00+qmid,q2=qy00+qmid;
25+
vary, name=kqtf, step=1.E-9;
26+
vary, name=kqtd, step=1.E-9;
27+
lmdif, calls=50, tolerance=1.E-10;
28+
endmatch;
29+
30+
31+
32+
! Quick minimization based on linear machine
33+
cmrskew0 = cmrskew;
34+
cmiskew0 = cmiskew;
35+
twiss; qx=table(summ,q1); qy=table(summ,q2);
36+
cta0 = abs(2*(qx-qy)-round(2*(qx-qy)))/2;
37+
closest0 = cta0;
38+
!value,closest0;
39+
40+
cmrskew = cmrskew0+cta0/2.;
41+
twiss; qx=table(summ,q1); qy=table(summ,q2);
42+
ctap = abs(2*(qx-qy)-round(2*(qx-qy)))/2;
43+
!value,ctap;
44+
45+
cmrskew = cmrskew0-cta0/2.;
46+
twiss; qx=table(summ,q1); qy=table(summ,q2);
47+
ctam = abs(2*(qx-qy)-round(2*(qx-qy)))/2;
48+
!value,ctam;
49+
50+
cmrskew = cmrskew0+(ctam^2-ctap^2)/2./cta0;
51+
twiss; qx=table(summ,q1); qy=table(summ,q2);
52+
cta0 = abs(2*(qx-qy)-round(2*(qx-qy)))/2;
53+
!value,cta0;
54+
55+
cmiskew = cmiskew0+cta0/2.;
56+
twiss; qx=table(summ,q1); qy=table(summ,q2);
57+
ctap = abs(2*(qx-qy)-round(2*(qx-qy)))/2;
58+
!value,ctap;
59+
60+
cmiskew = cmiskew0-cta0/2.;
61+
twiss; qx=table(summ,q1); qy=table(summ,q2);
62+
ctam = abs(2*(qx-qy)-round(2*(qx-qy)))/2;
63+
!value,ctam;
64+
65+
cmiskew = cmiskew0+(ctam^2-ctap^2)/2./cta0;
66+
twiss; qx=table(summ,q1); qy=table(summ,q2);
67+
68+
closest1 =abs(2*(qx-qy)-round(2*(qx-qy)))/2;
69+
!value,closest1;
70+
71+
!Empirical minimisation
72+
match;
73+
global, q1=qx00+qmid, q2=qy00+qmid;
74+
vary, name=kqtf, step=1.E-9;
75+
vary, name=kqtd, step=1.E-9;
76+
lmdif, calls=100,tolerance=1.E-6;
77+
endmatch;
78+
79+
match;
80+
global, q1=qx00+qmid, q2=qy00+qmid;
81+
vary, name=cmrskew, step=1.E-9;
82+
vary, name=cmiskew, step=1.E-9;
83+
lmdif, calls=150, tolerance=2.E-6;
84+
endmatch;
85+
86+
match;
87+
global, q1=qx00+qmid, q2=qy00+qmid;
88+
vary, name=kqtf, step=1.E-9;
89+
vary, name=kqtd, step=1.E-9;
90+
lmdif, calls=100, tolerance=1.E-7;
91+
endmatch;
92+
93+
match;
94+
global, q1=qx00+qmid, q2=qy00+qmid;
95+
vary, name=cmrskew, step=1.E-9;
96+
vary, name=cmiskew, step=1.E-9;
97+
lmdif, calls=150, tolerance=2.E-7;
98+
endmatch;
99+
100+
match;
101+
global, q1=qx00+qmid, q2=qy00+qmid;
102+
vary, name=kqtf, step=1.E-9;
103+
vary, name=kqtd, step=1.E-9;
104+
lmdif, calls=100, tolerance=1.E-7;
105+
endmatch;
106+
107+
twiss; qx=table(summ,q1); qy=table(summ,q2);
108+
closest2=abs(2*(qx-qy)-round(2*(qx-qy)))/2;
109+
value, closest0, closest1, closest2, cmrskew, cmiskew;
110+
111+
if(mylhcbeam==1)
112+
{kqtf.b1=kqtf0;
113+
kqtd.b1=kqtd0;};
114+
115+
if(mylhcbeam > 1)
116+
{kqtf.b2=kqtf0;
117+
kqtd.b2=kqtd0;};
118+
119+
return;

FineCouplingCorrectionSimplex.madx

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
! Fine tuning of coupling correction
2+
! S.Fartoukh March 2009
3+
! 2011/11/21 From SLHCV3.0 R. De Maria
4+
! 2016/04/27 R. De Maria & F.F Van der Veken
5+
! work around mad-x bug on integer part of the tune in presence of coupling for ctap calculation
6+
7+
if(mylhcbeam==1)
8+
{kqtf=kqtf.b1;kqtf0=kqtf.b1;
9+
kqtd=kqtd.b1;kqtd0=kqtd.b1;
10+
kqtf.b1:=kqtf;
11+
kqtd.b1:=kqtd;};
12+
13+
if(mylhcbeam > 1)
14+
{kqtf=kqtf.b2;kqtf0=kqtf.b2;
15+
kqtd=kqtd.b2;kqtd0=kqtd.b2;
16+
kqtf.b2:=kqtf;
17+
kqtd.b2:=kqtd;};
18+
19+
20+
! closest tune
21+
qmid=(qx0-qx00+qy0-qy00)*0.5;
22+
23+
match;
24+
global, q1=qx00+qmid,q2=qy00+qmid;
25+
vary, name=kqtf, step=1.E-9;
26+
vary, name=kqtd, step=1.E-9;
27+
lmdif, calls=50, tolerance=1.E-10;
28+
endmatch;
29+
30+
31+
32+
! Quick minimization based on linear machine
33+
cmrskew0 = cmrskew;
34+
cmiskew0 = cmiskew;
35+
twiss; qx=table(summ,q1); qy=table(summ,q2);
36+
cta0 = abs(2*(qx-qy)-round(2*(qx-qy)))/2;
37+
closest0 = cta0;
38+
!value,closest0;
39+
40+
cmrskew = cmrskew0+cta0/2.;
41+
twiss; qx=table(summ,q1); qy=table(summ,q2);
42+
ctap = abs(2*(qx-qy)-round(2*(qx-qy)))/2;
43+
!value,ctap;
44+
45+
cmrskew = cmrskew0-cta0/2.;
46+
twiss; qx=table(summ,q1); qy=table(summ,q2);
47+
ctam = abs(2*(qx-qy)-round(2*(qx-qy)))/2;
48+
!value,ctam;
49+
50+
cmrskew = cmrskew0+(ctam^2-ctap^2)/2./cta0;
51+
twiss; qx=table(summ,q1); qy=table(summ,q2);
52+
cta0 = abs(2*(qx-qy)-round(2*(qx-qy)))/2;
53+
!value,cta0;
54+
55+
cmiskew = cmiskew0+cta0/2.;
56+
twiss; qx=table(summ,q1); qy=table(summ,q2);
57+
ctap = abs(2*(qx-qy)-round(2*(qx-qy)))/2;
58+
!value,ctap;
59+
60+
cmiskew = cmiskew0-cta0/2.;
61+
twiss; qx=table(summ,q1); qy=table(summ,q2);
62+
ctam = abs(2*(qx-qy)-round(2*(qx-qy)))/2;
63+
!value,ctam;
64+
65+
cmiskew = cmiskew0+(ctam^2-ctap^2)/2./cta0;
66+
twiss; qx=table(summ,q1); qy=table(summ,q2);
67+
68+
closest1 =abs(2*(qx-qy)-round(2*(qx-qy)))/2;
69+
!value,closest1;
70+
71+
!Empirical minimisation
72+
match;
73+
global, q1=qx00+qmid, q2=qy00+qmid;
74+
vary, name=kqtf, step=1.E-9;
75+
vary, name=kqtd, step=1.E-9;
76+
lmdif, calls=200,tolerance=1.E-6;
77+
endmatch;
78+
79+
match;
80+
global, q1=qx00+qmid, q2=qy00+qmid;
81+
vary, name=cmrskew, step=1.E-9;
82+
vary, name=cmiskew, step=1.E-9;
83+
simplex, calls=300, tolerance=2.E-6;
84+
endmatch;
85+
86+
match;
87+
global, q1=qx00+qmid, q2=qy00+qmid;
88+
vary, name=kqtf, step=1.E-9;
89+
vary, name=kqtd, step=1.E-9;
90+
lmdif, calls=200, tolerance=1.E-7;
91+
endmatch;
92+
93+
match;
94+
global, q1=qx00+qmid, q2=qy00+qmid;
95+
vary, name=cmrskew, step=1.E-9;
96+
vary, name=cmiskew, step=1.E-9;
97+
simplex, calls=300, tolerance=2.E-7;
98+
endmatch;
99+
100+
match;
101+
global, q1=qx00+qmid, q2=qy00+qmid;
102+
vary, name=kqtf, step=1.E-9;
103+
vary, name=kqtd, step=1.E-9;
104+
lmdif, calls=200, tolerance=1.E-7;
105+
endmatch;
106+
107+
twiss; qx=table(summ,q1); qy=table(summ,q2);
108+
closest2=abs(2*(qx-qy)-round(2*(qx-qy)))/2;
109+
value, closest0, closest1, closest2, cmrskew, cmiskew;
110+
111+
if(mylhcbeam==1)
112+
{kqtf.b1=kqtf0;
113+
kqtd.b1=kqtd0;};
114+
115+
if(mylhcbeam > 1)
116+
{kqtf.b2=kqtf0;
117+
kqtd.b2=kqtd0;};
118+
119+
return;

0 commit comments

Comments
 (0)