-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedabrt.c
345 lines (340 loc) · 15 KB
/
edabrt.c
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/****************************************************************************
* edabrt: Electrostatic Deflector Aberrations *
* E. Valetov & M. Berz *
* Created 25-Jan-2018 *
* Email: [email protected] *
* *
* This program computes the first and second order aberrations of an *
* electrostatic deflector in the x-a plane using exact analytic formulas. *
* Please refer to the following report for a derivation of these formulas: *
* E. Valetov and M. Berz *
* Derivation of Analytic Formulas for Electrostatic Deflector Aberrations, *
* and Comparison with the Code COSY INFINITY *
* MSUHEP-180212, Michigan State University (2018) *
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
double calculate_aberration(double radius, double ang, double n1, double n2,
int index1, int index2) {
double s = radius * (M_PI / 180) * ang; // arc length
double h = 1 / radius; // curvature
if (n1 < 3) {
double s3n1 = h * sqrt(3 - n1);
switch (index1) {
case 1:
switch (index2) {
case 10:
return cos(s3n1 * s);
case 01:
return sin(s3n1 * s) / s3n1;
case 20:
return -4 * h * (9 * n1 + 2 * n2 - 15 +
(6 * n1 + n2 - 12) * cos(s3n1 * s)) *
pow(sin(s3n1 * s / 2), 2) / (3 * (n1 - 3));
case 11:
return -2 * pow(h, 3) * (3 - 3 * n1 - n2 +
(6 * n1 + n2 - 12) *
cos(s3n1 * s)) *
sin(s3n1 * s) /
(3 * pow(h * h * (3 - n1), 1.5));
case 02:
return -4 * (3 - 3 * n1 - n2 +
(6 * n1 + n2 - 12) * cos(s3n1 * s)) *
pow(sin(s3n1 * s / 2), 2) /
(3 * h * pow((n1 - 3), 2));
default:
return 0;
}
case 2:
switch (index2) {
case 10:
return -s3n1 * sin(s3n1 * s);
case 01:
return cos(s3n1 * s);
case 20:
return 2 * pow(h, 3) * (3 * n1 + n2 - 3) *
(sin(s3n1 * s) + sin(2 * s3n1 * s)) /
(3 * s3n1);
case 11:
return -4 * h * (3 * n1 + n2 - 3) *
(1 + 2 * cos(s3n1 * s)) *
pow(sin(s3n1 * s / 2), 2) /
(3 * (n1 - 3));
case 02:
return -2 * pow(h, 3) * (15 - 9 * n1 - 2 * n2 +
2 * (3 * n1 + n2 - 3) *
cos(s3n1 * s)) *
sin(s3n1 * s) /
(3 * pow(h * h * (3 - n1), 1.5));
default:
return 0;
}
default:
return 0;
}
} else if (n1 > 3) {
double s3n1 = h * sqrt(n1 - 3);
switch (index1) {
case 1:
switch (index2) {
case 10:
return cosh(s3n1 * s);
case 01:
return -sinh(s3n1 * s) / s3n1;
case 20:
return h * (4 * (3 * n1 + n2 - 3) * cosh(s3n1 * s) -
3 * (4 * n1 + n2 - 6) *
(2 * cosh(2 * s3n1 * s) - 1) -
(6 + n2) * cosh(4 * s3n1 * s)) /
(6 * (n1 - 3));
case 11:
return (4 * (3 * n1 + n2 - 3) * sinh(s3n1 * s) -
(6 + n2) * sinh(4 * s3n1 * s)) /
(6 * pow(n1 - 3, 1.5));
case 02:
return -(4 * (9 * n1 + 2 * n2 - 15) * cosh(s3n1 * s) -
3 * (4 * n1 + n2 - 6) *
(2 * cosh(2 * s3n1 * s) + 1) +
(6 + n2) * cosh(4 * s3n1 * s)) /
(6 * h * pow(n1 - 3, 2));
default:
return 0;
}
case 2:
switch (index2) {
case 10:
return s3n1 * sinh(s3n1 * s);
case 01:
return cosh(s3n1 * s);
case 20:
return 2 * h * h * h * (3 * n1 + n2 - 3) *
(sinh(s3n1 * s) + sinh(2 * s3n1 * s)) /
(3 * s3n1);
case 11:
return 2 * h * (3 * n1 + n2 - 3) *
(cosh(2 * s3n1 * s) - cosh(s3n1 * s)) /
(3 * (n1 - 3));
case 02:
return 2 * (15 - 9 * n1 - 2 * n2 +
2 * (3 * n1 + n2 - 3) *
cosh(s3n1 * s)) *
sinh(s3n1 * s) /
(3 * pow(n1 - 3, 1.5));
default:
return 0;
}
default:
return 0;
}
} else {
switch (index1) {
case 1:
switch (index2) {
case 10:
return 1;
case 01:
return s;
case 20:
return h * h * h * (6 + n2) * s * s;
case 11:
return 2 * h * s +
h * h * h * (6 + n2) * s * s * s / 3;
case 02:
return h * s * s * (6 + h * h * (6 + n2) * s * s) / 6;
default:
return 0;
}
case 2:
switch (index2) {
case 10:
return 0;
case 01:
return 1;
case 20:
return 2 * h * h * h * (6 + n2) * s;
case 11:
return h * h * h * (6 + n2) * s * s;
case 02:
return 2 * h * s * (h * h * (6 + n2) * s * s - 3) / 3;
default:
return 0;
}
default:
return 0;
}
}
}
int print_aberrations(double radius, double ang, double n1, double n2,
int index1) {
double aberration;
int max_order = 2;
int number_of_variables = 2;
int first_non_zero = 0;
char index_string[20];
int counter = 1;
for (int order = 1; order <= max_order; order++) {
int index_array[number_of_variables + 1];
for (int j = 0; j <= number_of_variables; j++)
index_array[j] = 0;
index_array[0] = order;
while (index_array[number_of_variables] == 0) {
int index2 = 0;
for (int j = 0; j <= number_of_variables - 1; j++)
index2 = index2 * 10 + index_array[j];
aberration = calculate_aberration(radius, ang, n1, n2, index1,
index2);
sprintf(index_string, "%i", index_array[0]);
if (number_of_variables > 1)
for (int j = 1; j <= number_of_variables - 1; j++) {
snprintf(index_string, sizeof index_string, "%s %i",
index_string, index_array[j]);
}
if (aberration != 0) {
if (counter == 1)
printf(" I COEFFICIENT ORDER EXPONENTS\n");
printf(" %i % 16.15le %i %s\n", counter, aberration,
order, index_string);
counter++;
}
for (int j = 0; j <= number_of_variables - 1; j++) {
if (index_array[j] != 0) {
first_non_zero = j;
break;
}
}
if (index_array[first_non_zero] == order) {
index_array[0] = order - 1;
index_array[first_non_zero + 1] = 1;
} else {
index_array[first_non_zero]--;
index_array[first_non_zero + 1]++;
}
}
}
if (counter == 1)
printf(" ALL COMPONENTS ZERO\n");
printf(" --------------------------------------\n");
}
int invalid_option_exit(char *option) {
printf("edabrt: invalid option -- %s\n", option);
printf("Try 'edabrt --help' for more information.\n");
exit(-1);
}
int main(int argc, char **argv) {
double r; // reference radius
double ang; // central angle
double n1, n2; // first and second order inhomogeneity coefficients
double dummy;
int read_code;
printf("----------------------------------------------------------\n");
printf(" edabrt: Electrostatic Deflector Aberrations \n");
printf(" E. Valetov & M. Berz \n");
printf(" Created 25-Jan-2018 \n");
printf(" Email: [email protected] \n");
printf("----------------------------------------------------------\n");
if (argc >= 2)
if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "/h") == 0 ||
strcmp(argv[1], "--help") == 0) {
printf("\nThis program computers the first and second order ");
printf("aberrations of an electrostatic deflector\n");
printf("in the horizontal x-a plane using exact analytic ");
printf("formulas.\n");
printf("\nINTERACTIVE MODE\n");
printf("Run the program and follow the prompts to specify the ");
printf("electrostatic deflector parameters.\n");
printf("\nCOMMAND-LINE ARGUMENTS\n");
printf("Electrostatic deflector parameters may be optionally ");
printf("supplied using the command line:\n");
printf("edabrt [r ang n1 n2] [--help]\n");
printf(" r Reference orbit radius in meters\n");
printf(" ang Central angle spanning the deflector ");
printf("in degrees\n");
printf(" n1 First order electrostatic field ");
printf("inhomogeneity coefficient\n");
printf(" n2 Second order electrostatic field ");
printf("inhomogeneity coefficient\n");
printf(" --help This information\n");
exit(0);
}
switch (argc) {
case 5:
if (sscanf(argv[1], "%lf", &r) != 1) invalid_option_exit(argv[1]);
if (r <= 0) {
printf("\nedabrt: supplied radius r is not positive\n");
exit(-1);
}
if (sscanf(argv[2], "%lf", &ang) != 1)
invalid_option_exit(argv[2]);
if (sscanf(argv[3], "%lf", &n1) != 1)
invalid_option_exit(argv[3]);
if (sscanf(argv[4], "%lf", &n2) != 1)
invalid_option_exit(argv[4]);
printf("\nReference radius r = % 16.15le m\n", r);
printf("Central angle ang = % 16.15le°\n", ang);
printf("1st order inhomogeneity coefficient n1 = % 16.15le\n",
n1);
printf("2nd order inhomogeneity coefficient n2 = % 16.15le\n",
n2);
break;
case 1:
printf("\n");
do {
fseek(stdin, 0, SEEK_END);
printf("Please enter the reference orbit radius r ");
printf("in [m].\n> ");
stdin = freopen(NULL, "r", stdin);
read_code = scanf("%lf", &r);
if (read_code != 1)
printf("Not a numerical value.\n");
else if (r <= 0) {
printf("The radius must be positive.\n");
read_code = 0;
}
} while (read_code != 1);
do {
printf("Please enter the central angle ang spanning the ");
printf("deflector in [°].\n> ");
stdin = freopen(NULL, "r", stdin);
read_code = scanf("%lf", &ang);
if (read_code != 1)
printf("Not a numerical value.\n");
} while (read_code != 1);
do {
printf("Please enter the first order inhomogeneity ");
printf("coefficient n1.\n> ");
stdin = freopen(NULL, "r", stdin);
read_code = scanf("%lf", &n1);
if (read_code != 1)
printf("Not a numerical value.\n");
} while (read_code != 1);
do {
printf("Please enter the first order inhomogeneity ");
printf("coefficient n2.\n> ");
stdin = freopen(NULL, "r", stdin);
read_code = scanf("%lf", &n2);
if (read_code != 1)
printf("Not a numerical value.\n");
} while (read_code != 1);
break;
default:
printf("\n");
for (int j = 1; j < argc; j++) {
if (sscanf(argv[j], "%lf", &dummy) != 1) {
printf("edabrt: invalid option -- %s\n", argv[j]);
printf("Try 'edabrt --help' for more information.\n");
exit(-1);
}
}
printf("edabrt: 4 numerical arguments expected, %i supplied\n",
argc - 1);
printf("Try 'edabrt --help' for more information.\n");
exit(-1);
}
printf("\nFirst and second order aberrations in the x-a plane:\n\n");
printf("(x|...)\n");
print_aberrations(r, ang, n1, n2, 1);
printf("(a|...)\n");
print_aberrations(r, ang, n1, n2, 2);
}