SDDSlib
Loading...
Searching...
No Matches
elegant2genesis.c
1/*************************************************************************\
2 * Copyright (c) 2002 The University of Chicago, as Operator of Argonne
3 * National Laboratory.
4 * Copyright (c) 2002 The Regents of the University of California, as
5 * Operator of Los Alamos National Laboratory.
6 * This file is distributed subject to a Software License Agreement found
7 * in the file LICENSE that is included with this distribution.
8\*************************************************************************/
9
10/*
11 $Log: not supported by cvs2svn $
12 Revision 1.21 2006/10/19 17:55:39 soliday
13 Updated to work with linux-x86_64.
14
15 Revision 1.20 2003/09/02 19:16:02 soliday
16 Cleaned up code for Linux.
17
18 Revision 1.19 2002/08/14 17:12:36 soliday
19 Added Open License
20
21 Revision 1.18 2002/07/01 18:35:08 borland
22 Added option to reverse order of rows in file. Added 't' column.
23
24 Revision 1.17 2001/11/21 16:30:29 borland
25 No longer emits NaN values when the number of particles in a slice is zero.
26
27 Revision 1.16 2001/08/08 19:20:22 borland
28 Added ability to output the filtered beam data using the -removePTails option.
29
30 Revision 1.15 2001/08/08 00:58:48 borland
31 Added -removePTails option, which permits removing momentum tails from
32 the beam.
33
34 Revision 1.14 2001/07/11 15:37:51 borland
35 Added -pipe option.
36
37 Revision 1.13 2001/05/03 20:53:53 soliday
38 Fixed a problem related to the last version that was merged with a previous
39 version.
40
41 Revision 1.12 2001/04/03 18:23:16 borland
42 Added -chargeParameter option, which permits taking the beam charge from
43 a parameter in the SDDS input file.
44
45 Revision 1.11 2001/01/23 19:14:55 soliday
46 Standardized usage message.
47
48 Revision 1.10 2000/06/02 20:01:01 soliday
49 To be consistant with GENESIS, where dgamma is the rms value of gamma,
50 dgamma is calculated the same way it was before revision 1.5.
51
52 Revision 1.9 2000/06/01 21:42:55 borland
53 SDDS output is now the default. Added -steer option. Improved usage message.
54
55 Revision 1.8 2000/05/31 20:31:17 soliday
56 Fixed small bug from last change.
57
58 Revision 1.7 2000/05/31 20:20:14 soliday
59 Added alphax and alphay in ascii output.
60
61 Revision 1.6 2000/05/31 19:28:19 borland
62 Added gammaStDev column.
63
64 Revision 1.5 2000/05/31 17:48:48 borland
65 Fixed bug in computation of delta gamma (was computing delta s).
66 Simplified computation of alphax and alphay, and used geometric emittance
67 in place of normalized emittance.
68
69 Revision 1.4 2000/05/31 15:20:55 soliday
70 Added correct computation for alphax and alphay.
71
72 Revision 1.3 2000/01/06 22:18:15 soliday
73 Multiplied the previous value of Emittance by gamma to get the correct emittance.
74 Divided the previous value of Current by the wavelength to get the correct value.
75
76 Revision 1.2 2000/01/06 21:29:19 soliday
77 Fixed a bug when calculating the emittance
78
79 Revision 1.1 2000/01/06 18:02:25 soliday
80 This program is used to convert the output file from elegant into the beamline
81 file used by genesis.
82
83*/
84#include "mdb.h"
85#include "SDDS.h"
86#include "scan.h"
87
88#define SET_WAVELENGTH 0
89#define SET_SLICES 1
90#define SET_TOTALCHARGE 2
91#define SET_TEXTOUTPUT 3
92#define SET_STEER 4
93#define SET_CHARGEPARAMETER 5
94#define SET_PIPE 6
95#define SET_REMPTAILS 7
96#define SET_REVERSEORDER 8
97#define SET_LOCAL_FIT 9
98#define N_OPTIONS 10
99
100char *option[N_OPTIONS] = {"wavelength", "slices", "totalcharge", "textoutput", "steer",
101 "chargeparameter", "pipe", "removePTails",
102 "reverseorder", "localfit"};
103
104char *USAGE = "elegant2genesis [<input>] [<output>] [-pipe=[in][,out]] [-textOutput]\n\
105[-totalCharge=<charge-in-Coulombs> | -chargeParameter=<name>]\n\
106[-wavelength=<meters> | -slices=<integer>]\n\
107[-steer] [-removePTails=deltaLimit=<value>[,fit][,beamOutput=<filename>]]\n\
108[-reverseOrder] [-localFit]\n\n\
109textOutput Make the output file a text file instead of an SDDS file.\n\
110totalCharge Specify the total charge in Coulombs.\n\
111chargeParameter Specify the name of a parameter in the input file that gives\n\
112 the total charge in Coulombs.\n\
113wavelength Specify the wavelength of light in meters.\n\
114slices Specify the number of slices to cut the beam into.\n\
115steer Force the x, x', y, and y' centroids for the whole beam to zero.\n\
116 Slices may still have nonzero centroids.\n\
117removePTail Removes the momentum tails from the beam. deltaLimit is the maximum\n\
118 |p-<p>|/<p> that will be kept. If 'fit' is given, then a linear fit to\n\
119 (t, p) is done and removal is based on residuals from this fit.\n\
120 If 'beamOutput' is given, the filtered beam is written to the named\n\
121 file for review.\n\
122reverseOrder By default, the data for the head of the beam comes first. This\n\
123 option causes elegant to put the data for the tail of the beam \n\
124 first.\n\
125localFit If given, then for each slice the program performs a local linear\n\
126 fit of momentum vs time, and subtracts it from the momentum data.\n\
127 This removes a contribution to the energy spread, but may not be\n\
128 appropriate if slices are longer than the cooperation length.\n\n\
129Program by Robert Soliday and Michael Borland. (" __DATE__ " " __TIME__ ", SVN revision: " SVN_VERSION ")\n";
130
131#define PTAILS_DELTALIMIT 0x0001U
132#define PTAILS_FITMODE 0x0002U
133#define PTAILS_OUTPUT 0X0004U
134int64_t removeMomentumTails(double *x, double *xp, double *y, double *yp, double *s, double *p, double *t, int64_t rows, double limit, unsigned long flags);
135void removeLocalFit(double *p, double *s, short *selected, double pAverage, int64_t rows);
136
137/* ********** */
138
139int main(int argc, char **argv) {
140 SDDS_DATASET SDDS_input, SDDS_output, SDDS_pTails;
141 FILE *fileID = NULL;
142 SCANNED_ARG *s_arg;
143 long i_arg;
144 char *input, *output;
145 unsigned long pipeFlags = 0;
146 long noWarnings = 0, tmpfile_used = 0;
147 long retval, sddsOutput = 1, steer = 0, firstOne, sliceOffset = 0;
148 int64_t i, j, rows, origRows;
149 long reverseOrder = 0;
150 char *chargeParameter;
151 unsigned long removePTailsFlags = 0;
152 double pTailsDeltaLimit, term;
153 char *pTailsOutputFile = NULL;
154 long localFit = 0;
155 short *selected;
156
157 char column[7][15] = {"x", "xp", "y", "yp", "t", "p", "particleID"};
158 long columnIndex[7];
159
160 double *tValues, *sValues, *xValues, *xpValues, *yValues, *ypValues, *pValues;
161 double sAverage, pAverage, pStDev;
162 double xAverage, xpAverage, xRMS, x2Average, xp2Average, xxpAverage, xEmittance;
163 double yAverage, ypAverage, yRMS, y2Average, yp2Average, yypAverage, yEmittance;
164 double sMin = 1, sMax = -1, s1, s2;
165 /*
166 double pMin, pMax;
167 */
168 double alphax, alphay;
169
170 double wavelength = 0.0001;
171 double totalCharge = 0;
172 double current;
173 long slices = 4;
174 long tmp = 0;
175 long N = 0;
176 double Ne = 0;
177
178 input = output = NULL;
179 sValues = NULL;
180 chargeParameter = NULL;
181 selected = NULL;
182
184 argc = scanargs(&s_arg, argc, argv);
185 if (argc < 3)
186 bomb(NULL, USAGE);
187
188 /* parse the command line */
189 for (i_arg = 1; i_arg < argc; i_arg++) {
190 if (s_arg[i_arg].arg_type == OPTION) {
191 switch (match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
192 case SET_TOTALCHARGE:
193 if (s_arg[i_arg].n_items != 2) {
194 fprintf(stderr, "error: invalid -totalCharge syntax\n");
195 exit(1);
196 }
197 if (sscanf(s_arg[i_arg].list[1], "%lf", &totalCharge) != 1) {
198 fprintf(stderr, "error: invalid -totalCharge syntax or value\n");
199 exit(1);
200 }
201 break;
202 case SET_WAVELENGTH:
203 if (tmp == 2) {
204 fprintf(stderr, "error: -wavelength and -slices can not be used together\n");
205 exit(1);
206 }
207 if (s_arg[i_arg].n_items != 2) {
208 fprintf(stderr, "error: invalid -wavelength syntax\n");
209 exit(1);
210 }
211 if (sscanf(s_arg[i_arg].list[1], "%lf", &wavelength) != 1 || wavelength <= 0) {
212 fprintf(stderr, "error: invalid -wavelength syntax or value\n");
213 exit(1);
214 }
215 tmp = 1;
216 break;
217 case SET_SLICES:
218 if (tmp == 1) {
219 fprintf(stderr, "error: -wavelength and -slices can not be used together\n");
220 exit(1);
221 }
222 if (s_arg[i_arg].n_items != 2) {
223 fprintf(stderr, "error: invalid -slices syntax\n");
224 exit(1);
225 }
226 if (sscanf(s_arg[i_arg].list[1], "%ld", &slices) != 1 || slices <= 0) {
227 fprintf(stderr, "error: invalid -slices syntax or value\n");
228 exit(1);
229 }
230 tmp = 2;
231 break;
232 case SET_TEXTOUTPUT:
233 sddsOutput = 0;
234 break;
235 case SET_STEER:
236 steer = 1;
237 break;
238 case SET_CHARGEPARAMETER:
239 if (s_arg[i_arg].n_items != 2) {
240 fprintf(stderr, "error: invalid -chargeParameter syntax\n");
241 exit(1);
242 }
243 chargeParameter = s_arg[i_arg].list[1];
244 break;
245 case SET_PIPE:
246 if (!processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipeFlags))
247 SDDS_Bomb("invalid -pipe syntax");
248 break;
249 case SET_REMPTAILS:
250 if (s_arg[i_arg].n_items -= 1) {
251 if (!scanItemList(&removePTailsFlags, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
252 "deltalimit", SDDS_DOUBLE, &pTailsDeltaLimit, 1, PTAILS_DELTALIMIT, "fit", -1, NULL, 0, PTAILS_FITMODE, "beamoutput", SDDS_STRING, &pTailsOutputFile, 1, PTAILS_OUTPUT, NULL) ||
253 !(removePTailsFlags & PTAILS_DELTALIMIT) || pTailsDeltaLimit <= 0)
254 SDDS_Bomb("invalid -removePTails syntax or values");
255 }
256 break;
257 case SET_REVERSEORDER:
258 reverseOrder = 1;
259 break;
260 case SET_LOCAL_FIT:
261 localFit = 1;
262 break;
263 default:
264 fprintf(stderr, "error: unknown switch: %s\n", s_arg[i_arg].list[0]);
265 exit(1);
266 }
267 } else {
268 if (input == NULL) {
269 input = s_arg[i_arg].list[0];
270 } else if (output == NULL) {
271 output = s_arg[i_arg].list[0];
272 } else {
273 fprintf(stderr, "too many filenames\n");
274 exit(1);
275 }
276 }
277 }
278
279 processFilenames("elegant2genesis", &input, &output, pipeFlags, noWarnings, &tmpfile_used);
280
281 /* open the input file */
282 if (!SDDS_InitializeInput(&SDDS_input, input)) {
283 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
284 exit(1);
285 }
286
287 /* check for needed columns */
288 for (i = 0; i < 7; i++) {
289 if ((columnIndex[i] = SDDS_GetColumnIndex(&SDDS_input, column[i])) < 0) {
290 fprintf(stderr, "error: column %s does not exist\n", column[i]);
291 exit(1);
292 }
293 }
294
295 if (removePTailsFlags & PTAILS_OUTPUT) {
296 if (!SDDS_InitializeOutput(&SDDS_pTails, SDDS_BINARY, 0, NULL, NULL, pTailsOutputFile) ||
297 !SDDS_DefineSimpleColumn(&SDDS_pTails, "t", "s", SDDS_DOUBLE) ||
298 !SDDS_DefineSimpleColumn(&SDDS_pTails, "p", NULL, SDDS_DOUBLE) ||
299 !SDDS_DefineSimpleColumn(&SDDS_pTails, "x", "m", SDDS_DOUBLE) ||
300 !SDDS_DefineSimpleColumn(&SDDS_pTails, "xp", NULL, SDDS_DOUBLE) || !SDDS_DefineSimpleColumn(&SDDS_pTails, "y", "m", SDDS_DOUBLE) || !SDDS_DefineSimpleColumn(&SDDS_pTails, "yp", NULL, SDDS_DOUBLE) || !SDDS_WriteLayout(&SDDS_pTails)) {
301 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
302 exit(1);
303 }
304 }
305
306 retval = -1;
307 firstOne = 1;
308 while ((retval = SDDS_ReadPage(&SDDS_input)) >= 1) {
309 rows = origRows = SDDS_RowCount(&SDDS_input);
310 if (rows < 1) {
311 fprintf(stderr, "error: no rows found for page %ld\n", retval);
312 exit(1);
313 }
314 /* get all the needed column data */
315 if (!(tValues = SDDS_GetNumericColumn(&SDDS_input, "t", SDDS_DOUBLE)) ||
316 !(xValues = SDDS_GetNumericColumn(&SDDS_input, "x", SDDS_DOUBLE)) ||
317 !(xpValues = SDDS_GetNumericColumn(&SDDS_input, "xp", SDDS_DOUBLE)) || !(yValues = SDDS_GetNumericColumn(&SDDS_input, "y", SDDS_DOUBLE)) || !(ypValues = SDDS_GetNumericColumn(&SDDS_input, "yp", SDDS_DOUBLE)) || !(pValues = SDDS_GetNumericColumn(&SDDS_input, "p", SDDS_DOUBLE))) {
318 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
319 exit(1);
320 }
321
322 if (chargeParameter && !SDDS_GetParameterAsDouble(&SDDS_input, chargeParameter, &totalCharge))
323 SDDS_Bomb("unable to read charge from file");
324 if ((!(tValues)) || (!(xValues)) || (!(xpValues)) || (!(yValues)) || (!(ypValues)) || (!(pValues))) {
325 fprintf(stderr, "error: invalid data for page %ld\n", retval);
326 exit(1);
327 }
328 selected = realloc(selected, sizeof(*selected) * rows);
329 sValues = realloc(sValues, sizeof(*sValues) * rows);
330 sAverage = xAverage = xpAverage = yAverage = ypAverage = 0;
331 for (i = 0; i < rows; i++) { /* calculate s values and sAverage */
332 sValues[i] = tValues[i] * 2.99792458e8;
333 sAverage += tValues[i];
334 xAverage += xValues[i];
335 xpAverage += xpValues[i];
336 yAverage += yValues[i];
337 ypAverage += ypValues[i];
338 }
339 sAverage = sAverage * 2.99792458e8 / rows;
340 xAverage /= rows;
341 xpAverage /= rows;
342 yAverage /= rows;
343 ypAverage /= rows;
344
345 for (i = 0; i < rows; i++)
346 /* move origin of s values to center at sAverage and flip the sign */
347 sValues[i] = sAverage - sValues[i];
348
349 /* remove the momentum tails, if asked */
350 if (removePTailsFlags) {
351 if (!(rows = removeMomentumTails(xValues, xpValues, yValues, ypValues, sValues, pValues, tValues, rows, pTailsDeltaLimit, removePTailsFlags)))
352 SDDS_Bomb("all data removed by P-tails filter");
353 if (removePTailsFlags & PTAILS_OUTPUT) {
354 if (!SDDS_StartPage(&SDDS_pTails, rows) ||
355 !SDDS_SetColumn(&SDDS_pTails, SDDS_SET_BY_NAME, xValues, rows, "x") ||
356 !SDDS_SetColumn(&SDDS_pTails, SDDS_SET_BY_NAME, xpValues, rows, "xp") ||
357 !SDDS_SetColumn(&SDDS_pTails, SDDS_SET_BY_NAME, yValues, rows, "y") ||
358 !SDDS_SetColumn(&SDDS_pTails, SDDS_SET_BY_NAME, ypValues, rows, "yp") ||
359 !SDDS_SetColumn(&SDDS_pTails, SDDS_SET_BY_NAME, tValues, rows, "t") ||
360 !SDDS_SetColumn(&SDDS_pTails, SDDS_SET_BY_NAME, pValues, rows, "p") ||
361 !SDDS_WritePage(&SDDS_pTails)) {
362 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
363 exit(1);
364 }
365 }
366 }
367
368 if (steer) {
369 /* set the centroids for the whole beam to zero */
370 for (i = 0; i < rows; i++) {
371 xValues[i] -= xAverage;
372 xpValues[i] -= xpAverage;
373 yValues[i] -= yAverage;
374 ypValues[i] -= ypAverage;
375 }
376 }
377
378 /* find limits of distribution in s */
379 find_min_max(&sMin, &sMax, sValues, rows);
380 if ((tmp == 1) || (tmp == 0)) { /* calculate the # of slices from the wavelength */
381 slices = (sMax - sMin) / wavelength + 1;
382 }
383 if (tmp == 2) { /* calculate the wavelength from the # of slices */
384 wavelength = (sMax - sMin) / slices;
385 }
386
387 if (firstOne) {
388 /* open the output file */
389 if (sddsOutput) {
390 if (SDDS_InitializeOutput(&SDDS_output, SDDS_ASCII, 1, NULL, NULL, output) != 1) {
391 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
392 exit(1);
393 }
394 if ((SDDS_DefineColumn(&SDDS_output, "s", "Location", "m", NULL, NULL, SDDS_DOUBLE, 0) == -1) ||
395 (SDDS_DefineColumn(&SDDS_output, "t", "Time position", "s", NULL, NULL, SDDS_DOUBLE, 0) == -1) ||
396 (SDDS_DefineColumn(&SDDS_output, "gamma", NULL, NULL, NULL, NULL, SDDS_DOUBLE, 0) == -1) ||
397 (SDDS_DefineColumn(&SDDS_output, "dgamma", NULL, NULL, NULL, NULL, SDDS_DOUBLE, 0) == -1) ||
398 (SDDS_DefineColumn(&SDDS_output, "Sdelta", NULL, NULL, NULL, NULL, SDDS_DOUBLE, 0) == -1) ||
399 (SDDS_DefineColumn(&SDDS_output, "xemit", "NormalizedEmittance-x", "m", NULL, NULL, SDDS_DOUBLE, 0) == -1) ||
400 (SDDS_DefineColumn(&SDDS_output, "yemit", "NormalizedEmittance-y", "m", NULL, NULL, SDDS_DOUBLE, 0) == -1) ||
401 (SDDS_DefineColumn(&SDDS_output, "xrms", "Beam Size-x", "m", NULL, NULL, SDDS_DOUBLE, 0) == -1) ||
402 (SDDS_DefineColumn(&SDDS_output, "yrms", "Beam Size-y", "m", NULL, NULL, SDDS_DOUBLE, 0) == -1) ||
403 (SDDS_DefineColumn(&SDDS_output, "xavg", "Position-x", "m", NULL, NULL, SDDS_DOUBLE, 0) == -1) ||
404 (SDDS_DefineColumn(&SDDS_output, "yavg", "Position-y", "m", NULL, NULL, SDDS_DOUBLE, 0) == -1) ||
405 (SDDS_DefineColumn(&SDDS_output, "pxavg", "Average x'", "rad", NULL, NULL, SDDS_DOUBLE, 0) == -1) ||
406 (SDDS_DefineColumn(&SDDS_output, "pyavg", "Average y'", "rad", NULL, NULL, SDDS_DOUBLE, 0) == -1) ||
407 (SDDS_DefineColumn(&SDDS_output, "alphax", "Alpha-x", NULL, NULL, NULL, SDDS_DOUBLE, 0) == -1) ||
408 (SDDS_DefineColumn(&SDDS_output, "alphay", "Alpha-y", NULL, NULL, NULL, SDDS_DOUBLE, 0) == -1) ||
409 (SDDS_DefineColumn(&SDDS_output, "current", "Current", "Amp", NULL, NULL, SDDS_DOUBLE, 0) == -1) ||
410 (SDDS_DefineColumn(&SDDS_output, "wakez", "Wake Loss", "eV/m", NULL, NULL, SDDS_DOUBLE, 0) == -1) ||
411 (SDDS_DefineColumn(&SDDS_output, "N", "Number of macroparticles", NULL, NULL, NULL, SDDS_LONG, 0) == -1) || (SDDS_DefineColumn(&SDDS_output, "Ne", "Number of electrons", NULL, NULL, NULL, SDDS_DOUBLE, 0) == -1)) {
412 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
413 exit(1);
414 }
415 if ((SDDS_WriteLayout(&SDDS_output) != 1) || (SDDS_StartPage(&SDDS_output, slices) != 1)) {
416 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
417 exit(1);
418 }
419 } else {
420 fileID = fopen(output, "w");
421 if (fileID == NULL) {
422 fprintf(stderr, "unable to open output file for writing\n");
423 exit(1);
424 }
425 fprintf(fileID, "%ld", slices);
426 }
427 firstOne = 0;
428 } else {
429 if (sddsOutput && !SDDS_LengthenTable(&SDDS_output, slices)) {
430 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
431 exit(1);
432 }
433 sliceOffset += slices;
434 }
435
436 /* for each slice calculate all the needed values */
437 for (i = 0; i < slices; i++) {
438 N = 0;
439 xAverage = xpAverage = xRMS = x2Average = xp2Average = xxpAverage = 0;
440 yAverage = ypAverage = yRMS = y2Average = yp2Average = yypAverage = 0;
441 pAverage = alphax = alphay = 0;
442 if (!reverseOrder) {
443 s1 = sMin + (wavelength * i);
444 s2 = sMin + (wavelength * (i + 1));
445 } else {
446 s1 = sMin + (wavelength * (slices - i - 1));
447 s2 = sMin + (wavelength * (slices - i));
448 }
449 for (j = 0; j < rows; j++) {
450 selected[j] = 0;
451 if ((s1 <= sValues[j]) && (s2 > sValues[j])) {
452 selected[j] = 1;
453 N++;
454 xAverage += xValues[j];
455 xpAverage += xpValues[j];
456 yAverage += yValues[j];
457 ypAverage += ypValues[j];
458 pAverage += pValues[j];
459 }
460 }
461 if (N > 2) {
462 current = N * 2.99792458e8 * (totalCharge / (origRows * wavelength));
463 Ne = totalCharge * N / (e_mks * origRows);
464 xAverage /= N;
465 yAverage /= N;
466 xpAverage /= N;
467 ypAverage /= N;
468 pAverage /= N;
469 /*
470 pMin = pMax = pAverage;
471 */
472 pStDev = 0;
473 if (localFit)
474 removeLocalFit(pValues, sValues, selected, pAverage, rows);
475 for (j = 0; j < rows; j++) {
476 if (selected[j]) {
477 pStDev += sqr(pValues[j] - pAverage);
478 x2Average += sqr(xValues[j] - xAverage);
479 y2Average += sqr(yValues[j] - yAverage);
480 xp2Average += sqr(xpValues[j] - xpAverage);
481 yp2Average += sqr(ypValues[j] - ypAverage);
482 xxpAverage += (xValues[j] - xAverage) * (xpValues[j] - xpAverage);
483 yypAverage += (yValues[j] - yAverage) * (ypValues[j] - ypAverage);
484 /*
485 if (pValues[j] < pMin)
486 pMin = pValues[j];
487 if (pValues[j] > pMax)
488 pMax = pValues[j];
489 */
490 }
491 }
492 pStDev = sqrt(pStDev / (N - 1.0));
493 x2Average /= N;
494 y2Average /= N;
495 xp2Average /= N;
496 yp2Average /= N;
497 xxpAverage /= N;
498 yypAverage /= N;
499 xRMS = sqrt(x2Average);
500 yRMS = sqrt(y2Average);
501 if ((term = (x2Average * xp2Average) - sqr(xxpAverage)) > 0)
502 xEmittance = sqrt(term) * pAverage;
503 else
504 xEmittance = 0;
505 if ((term = (y2Average * yp2Average) - sqr(yypAverage)) > 0)
506 yEmittance = sqrt(term) * pAverage;
507 else
508 yEmittance = 0;
509
510 alphax = -xxpAverage / (xEmittance / pAverage);
511 alphay = -yypAverage / (yEmittance / pAverage);
512 } else {
513 pAverage = pStDev = xEmittance = yEmittance = xRMS = yRMS = xAverage = yAverage = xpAverage = ypAverage = alphax = alphay = current = Ne = 0;
514 }
515
516 if (sddsOutput) {
517 if (SDDS_SetRowValues(&SDDS_output, SDDS_SET_BY_NAME | SDDS_PASS_BY_VALUE, i + sliceOffset,
518 "s", s1,
519 "t", -s1 / c_mks,
520 "gamma", pAverage,
521 "dgamma", pStDev,
522 "Sdelta", pAverage > 0 ? pStDev / pAverage : 0,
523 "xemit", xEmittance, "yemit", yEmittance, "xrms", xRMS, "yrms", yRMS, "xavg", xAverage, "yavg", yAverage, "pxavg", xpAverage, "pyavg", ypAverage, "alphax", alphax, "alphay", alphay, "current", current, "wakez", 0.0, "N", N, "Ne", Ne, NULL) != 1) {
524 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
525 exit(1);
526 }
527 } else {
528 fprintf(fileID, "\n % .6E % .6E % .6E % .6E % .6E % .6E % .6E % .6E % .6E % .6E % .6E % .6E % .6E % .6E 0.000000E+00", s1, pAverage, pStDev, xEmittance, yEmittance, xRMS, yRMS, xAverage, yAverage, xpAverage, ypAverage, alphax, alphay, current);
529 }
530 }
531 free(tValues);
532 free(xValues);
533 free(xpValues);
534 free(yValues);
535 free(ypValues);
536 free(pValues);
537 }
538 free(sValues);
539
540 /* close the output file */
541 if (sddsOutput) {
542 if (!SDDS_WritePage(&SDDS_output)) {
543 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
544 exit(1);
545 }
546 if (!SDDS_Terminate(&SDDS_output)) {
547 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
548 exit(1);
549 }
550 } else {
551 fclose(fileID);
552 }
553
554 /* close the input file */
555 if (!SDDS_Terminate(&SDDS_input)) {
556 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
557 exit(1);
558 }
559 if (removePTailsFlags & PTAILS_OUTPUT && !SDDS_Terminate(&SDDS_pTails)) {
560 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
561 exit(1);
562 }
563
564 return (0);
565}
566
567int64_t removeMomentumTails(double *x, double *xp, double *y, double *yp, double *s, double *p, double *t, int64_t rows, double limit, unsigned long flags)
568/* t is equivalent to s. It is filtered here for convenience in output
569 * to an elegant-type file
570 */
571{
572 double *delta, pAve;
573 int64_t ip, jp;
574#if defined(DEBUG)
575 static FILE *fp = NULL;
576#endif
577
578 if (!rows)
579 return rows;
580 if (!(delta = malloc(sizeof(*delta) * rows)))
581 SDDS_Bomb("memory allocation failure");
582
583 for (ip = pAve = 0; ip < rows; ip++)
584 pAve += p[ip];
585 pAve /= rows;
586 for (ip = 0; ip < rows; ip++)
587 delta[ip] = (p[ip] - pAve) / pAve;
588
589 if (flags & PTAILS_FITMODE) {
590 double slope, intercept, variance;
591 if (!unweightedLinearFit(s, delta, rows, &slope, &intercept, &variance))
592 SDDS_Bomb("fit failed in momentum tail removal");
593 for (ip = 0; ip < rows; ip++)
594 delta[ip] -= s[ip] * slope + intercept;
595 }
596
597 jp = rows - 1;
598 for (ip = 0; ip < rows; ip++) {
599 if (fabs(delta[ip]) > limit) {
600 if (jp > ip) {
601 x[ip] = x[jp];
602 xp[ip] = xp[jp];
603 y[ip] = y[jp];
604 yp[ip] = yp[jp];
605 s[ip] = s[jp];
606 p[ip] = p[jp];
607 t[ip] = t[jp];
608 delta[ip] = delta[jp];
609 jp--;
610 ip--;
611 }
612 rows--;
613 }
614 }
615 free(delta);
616
617#if defined(DEBUG)
618 if (!fp) {
619 fp = fopen("e2g.sdds", "w");
620 fprintf(fp, "SDDS1\n&column name=x type=double &end\n");
621 fprintf(fp, "&column name=xp type=double &end\n");
622 fprintf(fp, "&column name=y type=double &end\n");
623 fprintf(fp, "&column name=yp type=double &end\n");
624 fprintf(fp, "&column name=s type=double &end\n");
625 fprintf(fp, "&column name=p type=double &end\n");
626 fprintf(fp, "&data mode=ascii &end\n");
627 fprintf(fp, "%ld\n", rows);
628 }
629 for (ip = 0; ip < rows; ip++)
630 fprintf(fp, "%le %le %le %le %le %le\n", x[ip], xp[ip], y[ip], yp[ip], s[ip], p[ip]);
631#endif
632
633 return rows;
634}
635
636void removeLocalFit(double *p, double *s, short *selected, double pAverage, int64_t rows) {
637 double slope, intercept, variance;
638 int64_t i;
639
640 if (unweightedLinearFitSelect(s, p, selected, rows, &slope, &intercept, &variance)) {
641 for (i = 0; i < rows; i++)
642 if (selected[i])
643 p[i] -= intercept + slope * s[i] - pAverage;
644 }
645}
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_LengthenTable(SDDS_DATASET *SDDS_dataset, int64_t n_additional_rows)
int32_t SDDS_SetRowValues(SDDS_DATASET *SDDS_dataset, int32_t mode, int64_t row,...)
int32_t SDDS_StartPage(SDDS_DATASET *SDDS_dataset, int64_t expected_n_rows)
int32_t SDDS_SetColumn(SDDS_DATASET *SDDS_dataset, int32_t mode, void *data, int64_t rows,...)
Sets the values for one data column in the current data table of an SDDS dataset.
void * SDDS_GetNumericColumn(SDDS_DATASET *SDDS_dataset, char *column_name, int32_t desiredType)
Retrieves the data of a specified numerical column as an array of a desired numerical type,...
double * SDDS_GetParameterAsDouble(SDDS_DATASET *SDDS_dataset, char *parameter_name, double *memory)
Retrieves the value of a specified parameter as a double from the current data table of an SDDS datas...
int32_t SDDS_InitializeInput(SDDS_DATASET *SDDS_dataset, char *filename)
Definition SDDS_input.c:49
int32_t SDDS_Terminate(SDDS_DATASET *SDDS_dataset)
int32_t SDDS_ReadPage(SDDS_DATASET *SDDS_dataset)
int32_t SDDS_InitializeOutput(SDDS_DATASET *SDDS_dataset, int32_t data_mode, int32_t lines_per_row, const char *description, const char *contents, const char *filename)
Initializes the SDDS output dataset.
int32_t SDDS_DefineSimpleColumn(SDDS_DATASET *SDDS_dataset, const char *name, const char *unit, int32_t type)
Defines a simple data column within the SDDS dataset.
int32_t SDDS_WritePage(SDDS_DATASET *SDDS_dataset)
Writes the current data table to the output file.
int32_t SDDS_DefineColumn(SDDS_DATASET *SDDS_dataset, const char *name, const char *symbol, const char *units, const char *description, const char *format_string, int32_t type, int32_t field_length)
Defines a data column within the SDDS dataset.
int32_t SDDS_WriteLayout(SDDS_DATASET *SDDS_dataset)
Writes the SDDS layout header to the output file.
int32_t SDDS_GetColumnIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named column in the SDDS dataset.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
Definition SDDS_utils.c:432
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
Definition SDDS_utils.c:288
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
Definition SDDS_utils.c:342
#define SDDS_STRING
Identifier for the string data type.
Definition SDDStypes.h:85
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
Definition SDDStypes.h:61
#define SDDS_DOUBLE
Identifier for the double data type.
Definition SDDStypes.h:37
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
Definition bomb.c:26
int find_min_max(double *min, double *max, double *list, int64_t n)
Finds the minimum and maximum values in a list of doubles.
Definition findMinMax.c:33
long unweightedLinearFitSelect(double *xData, double *yData, short *select, long nData, double *slope, double *intercept, double *variance)
Performs an unweighted linear fit on the provided data with optional data point selection.
Definition linfit.c:60
long unweightedLinearFit(double *xData, double *yData, long nData, double *slope, double *intercept, double *variance)
Performs an unweighted linear fit on the provided data.
Definition linfit.c:37
long match_string(char *string, char **option, long n_options, long mode)
Matches a given string against an array of option strings based on specified modes.
int scanargs(SCANNED_ARG **scanned, int argc, char **argv)
Definition scanargs.c:36
long processPipeOption(char **item, long items, unsigned long *flags)
Definition scanargs.c:356
void processFilenames(char *programName, char **input, char **output, unsigned long pipeFlags, long noWarnings, long *tmpOutputUsed)
Definition scanargs.c:390
long scanItemList(unsigned long *flags, char **item, long *items, unsigned long mode,...)
Scans a list of items and assigns values based on provided keywords and types.