forked from zhangt58/felscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreaddpa.cpp
272 lines (243 loc) · 8.41 KB
/
readdpa.cpp
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
/*********************************************************************\
* Program: readdpa and readdpa_lite *
* Purpose: simulating the phase-shifting when e- pass phase-shifter *
* Copyright (C) 2012 Tong Zhang *
* *
* This program is free software: you can distribute it and/or modify*
* it under the terms of GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>*
\*********************************************************************/
/**************************************************************************\
This program is written for read .dpa file(binary) from genesis output file.
Author: Tong ZHANG
e-mail: [email protected]
Created Time: Nov. 10th, 2011
Modified: Sep. 11th, 2012
Usage: readdpa dpafile ascfile
\**************************************************************************/
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <cmath>
using namespace std;
const double pi = 3.141592653589793;
const double c0 = 299792458.0;
template <class T>
T fmod(T f1, T f2)
{
return f1 - (int)(f1/f2)*f2;
}
double mean(vector<double> a, long param, long powernum)
{
double sum = 0;
long npart = a.size()/6, inc_idx = 0, inc_idx1, inc_idx2;
switch(param)
{
case 1: //gamma
inc_idx = 0;
break;
case 2: //theta
inc_idx = npart;
break;
case 3: //x
inc_idx = npart * 2;
break;
case 4: //y
inc_idx = npart * 3;
break;
case 5: //xp
inc_idx = npart * 4;
break;
case 6: //yp
inc_idx = npart * 5;
break;
case 35: //xxp
inc_idx1 = npart * 2;
inc_idx2 = npart * 4;
for (int j = 0; j < npart ; j ++ )
sum += a[j+inc_idx1]*a[j+inc_idx2]/a[j];
return sum/(double)npart;
break;
case 46: //yyp
inc_idx1 = npart * 3;
inc_idx2 = npart * 5;
for (int j = 0; j < npart ; j ++ )
sum += a[j+inc_idx1]*a[j+inc_idx2]/a[j];
return sum/(double)npart;
break;
}
if(powernum == 1)
{
if(param == 5 || param == 6)
for (int j = 0; j < npart ; j ++ )sum += a[j+inc_idx]/a[j];
else
for (int j = 0; j < npart ; j ++ )sum += a[j+inc_idx];
}
else
{
if(param == 5 || param == 6)
for (int j = 0; j < npart ; j ++ )sum += a[j+inc_idx]*a[j+inc_idx]/a[j]/a[j];
else
for (int j = 0; j < npart ; j ++ )sum += a[j+inc_idx]*a[j+inc_idx];
}
return sum/(double)npart;
}
void give_usage(char **argv)
{
cout << "Usage: " << argv[0] << " dpafile ascfile multitimes format\n\n";
cout << " " << "This program will generate multitimes dpafile to ascfile.\n";
cout << " " << "e.g. if dpafile contain [0,2pi], then ascfile will range from [0,2pi*N].\n\n";
cout << " " << "3rd param: data format, elegant or genesis\n\n";
cout << " " << "Column-name conventions:\n";
cout << " " << " elegant format:|--t--|gamma|--x--|--y--|betax|betay|\n";
cout << " " << " genesis format:|gamma|theta|--x--|--y--|--xp-|--yp-|\n";
cout << " " << " where xp=gamma*betax, yp=gamma*betay, respectively.\n" << endl;
}
int main(int argc, char**argv)
{
ifstream infile(argv[1], ios::in | ios::binary); // open dpa file, i.e. input file
ofstream outfile(argv[2]); // open file for writting
if (argc == 1)
{
give_usage(argv);
exit(1);
}
if (argc < 5)
{
cout << "Error! Not enough parameters!\n";
cout << "Try '"<< argv[0] << "' for more detailed help.\n";
exit(1);
}
if (!infile)
{
cout << "Error! Cannot open " << argv[1] << "!\n";
cout << "Try '"<< argv[0] << "' for more detailed help.\n";
exit(1);
}
if (!outfile)
{
cout << "Error! Cannot open " << argv[2] << "!\n";
cout << "Try '"<< argv[0] << "' for more detailed help.\n";
exit(1);
}
double multitimes = atof(argv[3]);
string dumformat = argv[4];
double temp;
int i = 0;
vector <double> a;
infile.read((char*)&temp,sizeof(double));
do
{
a.push_back(temp);
infile.read((char*)&temp,sizeof(double));
i++;
}while(!infile.eof());
infile.close();
cout << i/6 << " lines read.\n";
outfile << scientific;
outfile.precision(18);
//elegant outfile format: t, gamma, x, y, betax, betay
//genesis dpafile format: gamma, theta, x,y,xp(gamma*betax),yp(gamma*betay)
if(dumformat == "genesis")
{
for( int npart = i/6, j = 0; j < npart; j ++ )
{
for ( int multin = 0; multin < multitimes; multin ++ )
{ outfile << a[j] << " " // gamma (Energy, gamma)
<< fmod(a[j+npart],2*pi)+2*pi*multin << " " // theta (particle phase, theta)
<< a[j+2*npart] << " " // x (x position, x)
<< a[j+3*npart] << " " // y (y position, y)
<< a[j+4*npart] << " " // xp (x momenta, normalized to mc, i.e. xp = gamma*betax => betax = xp/gamma)
<< a[j+5*npart] << "\n";// yp (y momenta, normalized to mc, i.e. yp = gamma*betay => betay = yp/gamma)
}
}
}
else
{
if (argc < 6){cout << "Please give xlamds.\n";exit(1);}
double xlamds = atof(argv[5]);
for( int npart = i/6, j = 0; j < npart; j ++ )
{
for ( int multin = 0; multin < multitimes; multin ++ )
{ outfile << (fmod(a[j+npart],2*pi)+2*pi*multin)*xlamds/2/pi/c0 << " " // t (time coordinator, t = theta/k/c)
<< a[j] << " " // gamma (Energy, gamma)
<< a[j+2*npart] << " " // x (x position, x)
<< a[j+3*npart] << " " // y (y position, y)
<< a[j+4*npart]/a[j] << " " // betax (x momenta, normalized to mc, i.e. xp = gamma*betax => betax = xp/gamma)
<< a[j+5*npart]/a[j] << "\n"; // betay (y momenta, normalized to mc, i.e. yp = gamma*betay => betay = yp/gamma)
}
}
}
outfile.close();
////////////////////////////////////////////////////////////////////////////
//parameters can be calculated from imported data(a[i])
double emitx, sigmax, sigmaxp, //betax, alphax, gammax,
emity, sigmay, sigmayp, //betay, alphay, gammay,
avggam, avgx2, avgy2, avgxp2, avgyp2, avgxxp, avgyyp;
avggam = mean(a,1,1);
avgx2 = mean(a,3,2);
avgxp2 = mean(a,5,2);
avgy2 = mean(a,4,2);
avgyp2 = mean(a,6,2);
avgxxp = mean(a,35,1);
avgyyp = mean(a,46,1);
emitx = sqrt(avgx2*avgxp2-avgxxp*avgxxp);
emity = sqrt(avgy2*avgyp2-avgyyp*avgyyp);
sigmax = sqrt(avgx2);
sigmaxp = sqrt(avgxp2);
sigmay = sqrt(avgy2);
sigmayp = sqrt(avgyp2);
int nharm = 1000;
ofstream bunch_file("bun.tmp");
double *b = new double [nharm];
for ( int n = 1; n <= nharm ; n ++ )
{
double sumcos = 0, sumsin = 0;
long npart = a.size()/6;
for( int j = 0; j < npart; j++)
{
sumcos += cos(n*a[j+npart]);
sumsin += sin(-n*a[j+npart]);
}
b[n-1] = sqrt(sumcos*sumcos + sumsin*sumsin)/(double)npart;
bunch_file << n << " " << b[n-1] << "\n";
}
cout << scientific << left;
cout.precision(6);
cout << "------------------------------\n"
<< "Normalized emittance: \n"
<< " Emitnx = " << emitx * avggam << " m.rad\n"
<< " Emitny = " << emity * avggam << " m.rad\n"
<< "------------------------------\n"
<< "RMS Beam Size: " << "\n"
<< " sigmax = " << sigmax << "\n"
<< " sigmax' = " << sigmaxp << "\n"
<< " sigmay = " << sigmay << "\n"
<< " sigmay' = " << sigmayp << "\n"
<< "------------------------------\n"
<< "Bunching factor: \n"
<< " b1 | " << b[0] << " | " << " b5 | " << b[4] << " |\n"
<< " b2 | " << b[1] << " | " << " b6 | " << b[5] << " |\n"
<< " b3 | " << b[2] << " | " << " b7 | " << b[6] << " |\n"
<< " b4 | " << b[3] << " | " << " b8 | " << b[7] << " |\n"
<< "See more detailed bunching factor in the file bun.tmp\n"
<< "------------------------------\n"
<< "Twiss Parameters: \n"
<< " betax = " << sigmax*sigmax/emitx << "\n"
<< " gammax = " << sigmaxp*sigmaxp/emitx << "\n"
<< " alphax = " << -avgxxp/emitx << "\n"
<< " betay = " << sigmay*sigmay/emity << "\n"
<< " gammay = " << sigmayp*sigmayp/emity << "\n"
<< " alphay = " << -avgyyp/emity << endl;
return 0;
}