-
Notifications
You must be signed in to change notification settings - Fork 0
/
BndMPoleSymplectic4HFullPass.c
593 lines (428 loc) · 16 KB
/
BndMPoleSymplectic4HFullPass.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#include "mex.h"
#include<math.h>
#include "elempass.h"
#include "atlalib.c"
#include "atphyslib.c"
#define DRIFT1 0.6756035959798286638
#define DRIFT2 -0.1756035959798286639
#define KICK1 1.351207191959657328
#define KICK2 -1.702414383919314656
#define SQR(X) ((X)*(X))
void bndthinkick(double* r, double* A, double* B, double L, double irho, int max_order)
/*****************************************************************************
Calculate multipole kick in a curved elemrnt (bending magnet)
The reference coordinate system has the curvature given by the inverse
(design) radius irho.
IMPORTANT !!!
The magnetic field Bo that provides this curvature MUST NOT be included in the dipole term
PolynomB[1](MATLAB notation)(C: B[0] in this function) of the By field expansion
The kick is given by
e L L delta L x
theta = - --- B + ------- - ----- ,
x p y rho 2
0 rho
e L
theta = --- B
y p x
0
Note: in the US convention the transverse multipole field is written as:
max_order+1
----
\ n-1
(B + iB )/ B rho = > (ia + b ) (x + iy)
y x / n n
----
n=1
is a polynomial in (x,y) with the highest order = MaxOrder
Using different index notation
max_order
----
\ n
(B + iB )/ B rho = > (iA + B ) (x + iy)
y x / n n
----
n=0
A,B: i=0 ... max_order
[0] - dipole, [1] - quadrupole, [2] - sextupole ...
units for A,B[i] = 1/[m]^(i+1)
Coeficients are stroed in the PolynomA, PolynomB field of the element
structure in MATLAB
A[i] (C++,C) = PolynomA(i+1) (MATLAB)
B[i] (C++,C) = PolynomB(i+1) (MATLAB)
i = 0 .. MaxOrder
******************************************************************************/
{ int i;
double ReSum = B[max_order];
double ImSum = A[max_order];
double ReSumTemp;
/* recursively calculate the local transvrese magnetic field
* Bx = ReSum, By = ImSum
*/
for(i=max_order-1;i>0;i--)
{ ReSumTemp = ReSum*r[0] - ImSum*r[2] + B[i];
ImSum = ImSum*r[0] + ReSum*r[2] + A[i];
ReSum = ReSumTemp;
}
ReSumTemp = ReSum*r[0] - ImSum*r[2];
ImSum = ImSum*r[0] + ReSum*r[2] + A[0];
ReSum = ReSumTemp;
r[1] -= L*ReSum*(1+r[0]*irho);
r[3] += L*ImSum*(1+r[0]*irho);
}
void BndMPoleSymplectic4Pass(double *r, double le, double irho, double *A, double *B,
int max_order, int num_int_steps,
double entrance_angle, double exit_angle,
double fint1, double fint2, double gap,
double *T1, double *T2,
double *R1, double *R2, int num_particles)
{ int c,m;
double *r6;
double SL, L1, L2, K1, K2;
bool useT1, useT2, useR1, useR2, useFringe1, useFringe2;
SL = le/num_int_steps;
L1 = SL*DRIFT1;
L2 = SL*DRIFT2;
K1 = SL*KICK1;
K2 = SL*KICK2;
if(T1==NULL)
useT1=false;
else
useT1=true;
if(T2==NULL)
useT2=false;
else
useT2=true;
if(R1==NULL)
useR1=false;
else
useR1=true;
if(R2==NULL)
useR2=false;
else
useR2=true;
/* if either is 0 - do not calculate fringe effects */
if( fint1==0 || gap==0)
useFringe1 = false;
else
useFringe1=true;
if( fint2==0 || gap==0)
useFringe2 = false;
else
useFringe2=true;
for(c = 0;c<num_particles;c++) /* Loop over particles */
{ r6 = r+c*6;
if(!mxIsNaN(r6[0]))
{
/* misalignment at entrance */
if(useT1)
ATaddvv(r6,T1);
if(useR1)
ATmultmv(r6,R1);
/* edge focus */
if(useFringe1)
edge_fringe(r6, irho, entrance_angle,fint1,gap);
else
edge(r6, irho, entrance_angle);
/* integrator */
for(m=0; m < num_int_steps; m++) /* Loop over slices*/
{ r6 = r+c*6;
AT_H_Full_Bend_y_Drift(r6,L1,irho);
bndthinkick(r6, A, B, K1,irho, max_order);
AT_H_Full_Bend_y_Drift(r6,L2,irho);
bndthinkick(r6, A, B, K2,irho, max_order);
AT_H_Full_Bend_y_Drift(r6,L2,irho);
bndthinkick(r6, A, B, K1,irho, max_order);
AT_H_Full_Bend_y_Drift(r6,L1,irho);
}
/* edge focus */
if(useFringe2)
edge_fringe(r6, irho, exit_angle,fint2,gap);
else
edge(r6, irho, exit_angle);
/* Misalignment at exit */
if(useR2)
ATmultmv(r6,R2);
if(useT2)
ATaddvv(r6,T2);
}
}
}
ExportMode int* passFunction(const mxArray *ElemData, int *FieldNumbers,
double *r_in, int num_particles, int mode)
#define NUM_FIELDS_2_REMEMBER 15
{ double *A , *B;
double *pr1, *pr2, *pt1, *pt2, fint1, fint2, gap;
double entrance_angle, exit_angle;
int max_order, num_int_steps;
double le,ba,irho;
int *returnptr;
int *NewFieldNumbers, fnum;
switch(mode)
{ case MAKE_LOCAL_COPY: /* Find field numbers first
Save a list of field number in an array
and make returnptr point to that array
*/
{
/* Allocate memory for integer array of
field numbers for faster future reference
*/
NewFieldNumbers = (int*)mxCalloc(NUM_FIELDS_2_REMEMBER,sizeof(int));
/* Populate */
fnum = mxGetFieldNumber(ElemData,"PolynomA");
if(fnum<0)
mexErrMsgTxt("Required field 'PolynomA' was not found in the element data structure");
NewFieldNumbers[0] = fnum;
A = mxGetPr(mxGetFieldByNumber(ElemData,0,fnum));
fnum = mxGetFieldNumber(ElemData,"PolynomB");
if(fnum<0)
mexErrMsgTxt("Required field 'PolynomB' was not found in the element data structure");
NewFieldNumbers[1] = fnum;
B = mxGetPr(mxGetFieldByNumber(ElemData,0,fnum));
fnum = mxGetFieldNumber(ElemData,"MaxOrder");
if(fnum<0)
mexErrMsgTxt("Required field 'MaxOrder' was not found in the element data structure");
NewFieldNumbers[2] = fnum;
max_order = (int)mxGetScalar(mxGetFieldByNumber(ElemData,0,fnum));
fnum = mxGetFieldNumber(ElemData,"NumIntSteps");
if(fnum<0)
mexErrMsgTxt("Required field 'NumIntSteps' was not found in the element data structure");
NewFieldNumbers[3] = fnum;
num_int_steps = (int)mxGetScalar(mxGetFieldByNumber(ElemData,0,fnum));
fnum = mxGetFieldNumber(ElemData,"Length");
if(fnum<0)
mexErrMsgTxt("Required field 'Length' was not found in the element data structure");
NewFieldNumbers[4] = fnum;
le = mxGetScalar(mxGetFieldByNumber(ElemData,0,fnum));
fnum = mxGetFieldNumber(ElemData,"BendingAngle");
if(fnum<0)
mexErrMsgTxt("Required field 'BendingAngle' was not found in the element data structure");
NewFieldNumbers[5] = fnum;
ba = mxGetScalar(mxGetFieldByNumber(ElemData,0,fnum));
fnum = mxGetFieldNumber(ElemData,"EntranceAngle");
if(fnum<0)
mexErrMsgTxt("Required field 'EntranceAngle' was not found in the element data structure");
NewFieldNumbers[6] = fnum;
entrance_angle = mxGetScalar(mxGetFieldByNumber(ElemData,0,fnum));
fnum = mxGetFieldNumber(ElemData,"ExitAngle");
if(fnum<0)
mexErrMsgTxt("Required field 'ExitAngle' was not found in the element data structure");
NewFieldNumbers[7] = fnum;
exit_angle = mxGetScalar(mxGetFieldByNumber(ElemData,0,fnum));
fnum = mxGetFieldNumber(ElemData,"FringeInt1");/* Optional field FringeInt */
NewFieldNumbers[8] = fnum;
if(fnum<0)
fint1 = 0;
else
fint1 = mxGetScalar(mxGetFieldByNumber(ElemData,0,fnum));
fnum = mxGetFieldNumber(ElemData,"FringeInt2");/* Optional field FringeInt */
NewFieldNumbers[9] = fnum;
if(fnum<0)
fint2 = 0;
else
fint2 = mxGetScalar(mxGetFieldByNumber(ElemData,0,fnum));
fnum = mxGetFieldNumber(ElemData,"FullGap");
NewFieldNumbers[10] = fnum;
if(fnum<0)
gap = 0;
else
gap = mxGetScalar(mxGetFieldByNumber(ElemData,0,fnum));
fnum = mxGetFieldNumber(ElemData,"R1");
NewFieldNumbers[11] = fnum;
if(fnum<0)
pr1 = NULL;
else
pr1 = mxGetPr(mxGetFieldByNumber(ElemData,0,fnum));
fnum = mxGetFieldNumber(ElemData,"R2");
NewFieldNumbers[12] = fnum;
if(fnum<0)
pr2 = NULL;
else
pr2 = mxGetPr(mxGetFieldByNumber(ElemData,0,fnum));
fnum = mxGetFieldNumber(ElemData,"T1");
NewFieldNumbers[13] = fnum;
if(fnum<0)
pt1 = NULL;
else
pt1 = mxGetPr(mxGetFieldByNumber(ElemData,0,fnum));
fnum = mxGetFieldNumber(ElemData,"T2");
NewFieldNumbers[14] = fnum;
if(fnum<0)
pt2 = NULL;
else
pt2 = mxGetPr(mxGetFieldByNumber(ElemData,0,fnum));
returnptr = NewFieldNumbers;
} break;
case USE_LOCAL_COPY: /* Get fields from MATLAB using field numbers
The second argument ponter to the array of field
numbers is previously created with
QuadLinPass( ..., MAKE_LOCAL_COPY)
*/
{ A = mxGetPr(mxGetFieldByNumber(ElemData,0,FieldNumbers[0]));
B = mxGetPr(mxGetFieldByNumber(ElemData,0,FieldNumbers[1]));
max_order = (int)mxGetScalar(mxGetFieldByNumber(ElemData,0,FieldNumbers[2]));
num_int_steps = (int)mxGetScalar(mxGetFieldByNumber(ElemData,0,FieldNumbers[3]));
le = mxGetScalar(mxGetFieldByNumber(ElemData,0,FieldNumbers[4]));
ba = mxGetScalar(mxGetFieldByNumber(ElemData,0,FieldNumbers[5]));
entrance_angle = mxGetScalar(mxGetFieldByNumber(ElemData,0,FieldNumbers[6]));
exit_angle = mxGetScalar(mxGetFieldByNumber(ElemData,0,FieldNumbers[7]));
/* Optional fields */
if(FieldNumbers[8]<0)
fint1 = 0;
else
fint1 = mxGetScalar(mxGetFieldByNumber(ElemData,0,FieldNumbers[8]));
if(FieldNumbers[9]<0)
fint2 = 0;
else
fint2 = mxGetScalar(mxGetFieldByNumber(ElemData,0,FieldNumbers[9]));
if(FieldNumbers[10]<0)
gap = 0;
else
gap = mxGetScalar(mxGetFieldByNumber(ElemData,0,FieldNumbers[10]));
/* Optional fields */
if(FieldNumbers[11]<0)
pr1 = NULL;
else
pr1 = mxGetPr(mxGetFieldByNumber(ElemData,0,FieldNumbers[11]));
if(FieldNumbers[12]<0)
pr2 = NULL;
else
pr2 = mxGetPr(mxGetFieldByNumber(ElemData,0,FieldNumbers[12]));
if(FieldNumbers[13]<0)
pt1 = NULL;
else
pt1 = mxGetPr(mxGetFieldByNumber(ElemData,0,FieldNumbers[13]));
if(FieldNumbers[14]<0)
pt2 = NULL;
else
pt2 = mxGetPr(mxGetFieldByNumber(ElemData,0,FieldNumbers[14]));
returnptr = FieldNumbers;
} break;
default:
{ mexErrMsgTxt("No match for calling mode in function BndMPoleSymplectic4Pass\n");
}
}
irho = ba/le;
BndMPoleSymplectic4Pass(r_in, le, irho, A, B, max_order, num_int_steps,
entrance_angle, exit_angle, fint1, fint2, gap, pt1, pt2, pr1, pr2, num_particles);
return(returnptr);
}
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{ int m,n;
double *r_in;
double le, ba, *A, *B;
double irho;
int max_order, num_int_steps;
double entrance_angle, exit_angle ;
double *pr1, *pr2, *pt1, *pt2, fint1, fint2, gap;
mxArray *tmpmxptr;
if(nrhs)
{
/* ALLOCATE memory for the output array of the same size as the input */
m = mxGetM(prhs[1]);
n = mxGetN(prhs[1]);
if(m!=6)
mexErrMsgTxt("Second argument must be a 6 x N matrix");
tmpmxptr =mxGetField(prhs[0],0,"PolynomA");
if(tmpmxptr)
A = mxGetPr(tmpmxptr);
else
mexErrMsgTxt("Required field 'PolynomA' was not found in the element data structure");
tmpmxptr =mxGetField(prhs[0],0,"PolynomB");
if(tmpmxptr)
B = mxGetPr(tmpmxptr);
else
mexErrMsgTxt("Required field 'PolynomB' was not found in the element data structure");
tmpmxptr = mxGetField(prhs[0],0,"MaxOrder");
if(tmpmxptr)
max_order = (int)mxGetScalar(tmpmxptr);
else
mexErrMsgTxt("Required field 'MaxOrder' was not found in the element data structure");
tmpmxptr = mxGetField(prhs[0],0,"NumIntSteps");
if(tmpmxptr)
num_int_steps = (int)mxGetScalar(tmpmxptr);
else
mexErrMsgTxt("Required field 'NumIntSteps' was not found in the element data structure");
tmpmxptr = mxGetField(prhs[0],0,"Length");
if(tmpmxptr)
le = mxGetScalar(tmpmxptr);
else
mexErrMsgTxt("Required field 'Length' was not found in the element data structure");
tmpmxptr = mxGetField(prhs[0],0,"BendingAngle");
if(tmpmxptr)
ba = mxGetScalar(tmpmxptr);
else
mexErrMsgTxt("Required field 'BendingAngle' was not found in the element data structure");
tmpmxptr = mxGetField(prhs[0],0,"EntranceAngle");
if(tmpmxptr)
entrance_angle = mxGetScalar(tmpmxptr);
else
mexErrMsgTxt("Required field 'EntranceAngle' was not found in the element data structure");
tmpmxptr = mxGetField(prhs[0],0,"ExitAngle");
if(tmpmxptr)
exit_angle = mxGetScalar(tmpmxptr);
else
mexErrMsgTxt("Required field 'ExitAngle' was not found in the element data structure");
tmpmxptr = mxGetField(prhs[0],0,"FringeInt1");
if(tmpmxptr)
fint1 = mxGetScalar(tmpmxptr);
else
fint1 = 0;
tmpmxptr = mxGetField(prhs[0],0,"FringeInt2");
if(tmpmxptr)
fint2 = mxGetScalar(tmpmxptr);
else
fint2 = 0;
tmpmxptr = mxGetField(prhs[0],0,"FullGap");
if(tmpmxptr)
gap = mxGetScalar(tmpmxptr);
else
gap = 0;
tmpmxptr = mxGetField(prhs[0],0,"R1");
if(tmpmxptr)
pr1 = mxGetPr(tmpmxptr);
else
pr1=NULL;
tmpmxptr = mxGetField(prhs[0],0,"R2");
if(tmpmxptr)
pr2 = mxGetPr(tmpmxptr);
else
pr2=NULL;
tmpmxptr = mxGetField(prhs[0],0,"T1");
if(tmpmxptr)
pt1=mxGetPr(tmpmxptr);
else
pt1=NULL;
tmpmxptr = mxGetField(prhs[0],0,"T2");
if(tmpmxptr)
pt2=mxGetPr(tmpmxptr);
else
pt2=NULL;
irho = ba/le;
plhs[0] = mxDuplicateArray(prhs[1]);
r_in = mxGetPr(plhs[0]);
BndMPoleSymplectic4Pass(r_in, le, irho, A, B, max_order, num_int_steps,
entrance_angle, exit_angle, fint1, fint2, gap, pt1, pt2, pr1, pr2, n);
}
else
{ /* return list of required fields */
plhs[0] = mxCreateCellMatrix(8,1);
mxSetCell(plhs[0],0,mxCreateString("Length"));
mxSetCell(plhs[0],1,mxCreateString("BendingAngle"));
mxSetCell(plhs[0],2,mxCreateString("EntranceAngle"));
mxSetCell(plhs[0],3,mxCreateString("ExitAngle"));
mxSetCell(plhs[0],4,mxCreateString("PolynomA"));
mxSetCell(plhs[0],5,mxCreateString("PolynomB"));
mxSetCell(plhs[0],6,mxCreateString("MaxOrder"));
mxSetCell(plhs[0],7,mxCreateString("NumIntSteps"));
if(nlhs>1) /* Required and optional fields */
{ plhs[1] = mxCreateCellMatrix(7,1);
mxSetCell(plhs[1],0,mxCreateString("FullGap"));
mxSetCell(plhs[1],1,mxCreateString("FringeInt1"));
mxSetCell(plhs[1],2,mxCreateString("FringeInt2"));
mxSetCell(plhs[1],3,mxCreateString("T1"));
mxSetCell(plhs[1],4,mxCreateString("T2"));
mxSetCell(plhs[1],5,mxCreateString("R1"));
mxSetCell(plhs[1],6,mxCreateString("R2"));
}
}
}