-
Notifications
You must be signed in to change notification settings - Fork 0
/
StrMPoleSymplectic4FringeHFullPass.c
388 lines (307 loc) · 11 KB
/
StrMPoleSymplectic4FringeHFullPass.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
#include "mex.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
void strthinkick(double* r, double* A, double* B, double L, int max_order)
/*****************************************************************************
Calculate and apply a multipole kick to a 6-dimentional
phase space vector in a straight element ( quadrupole)
IMPORTANT !!!
The reference coordinate system is straight but the field expansion may still
contain dipole terms: PolynomA(1), PolynomB(1) - in MATLAB notation,
A[0], B[0] - C,C++ notation
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)d
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;
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;
}
r[1] -= L*ReSum;
r[3] += L*ImSum;
}
void StrMPoleSymplectic4Pass(double *r, double le, double *A, double *B,
int max_order, int num_int_steps,
double *T1, double *T2,
double *R1, double *R2, int num_particles)
{ int c,m;
double norm, NormL1, NormL2;
double *r6;
bool useT1, useT2, useR1, useR2;
double SL, L1, L2, K1, K2;
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;
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);
/* integrator */
if(B[1]!=0) QuadFringe(r6,B[1],1.0); /* Lee-Whiting limit for the hard edge quadrupole fringe field*/
for(m=0; m < num_int_steps; m++) /* Loop over slices */
{ r6 = r+c*6;
AT_H_Full_Drift(r6, L1);
strthinkick(r6, A, B, K1, max_order);
AT_H_Full_Drift(r6, L2);
strthinkick(r6, A, B, K2, max_order);
AT_H_Full_Drift(r6, L2);
strthinkick(r6, A, B, K1, max_order);
AT_H_Full_Drift(r6, L1);
}
if(B[1]!=0) QuadFringe(r6,B[1],-1.0); /* Lee-Whiting limit for the hard edge quadrupole fringe field*/
/* 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 9
{ int fnum;
double *A , *B;
double *pr1, *pr2, *pt1, *pt2;
int max_order, num_int_steps;
double le;
int *returnptr;
int *NewFieldNumbers;
switch(mode)
{ case NO_LOCAL_COPY: /* NOT used in AT1.3 Get fields by names from MATLAB workspace */
{
} break;
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 futurereference
*/
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,"R1");
NewFieldNumbers[5] = fnum;
if(fnum<0)
pr1 = NULL;
else
pr1 = mxGetPr(mxGetFieldByNumber(ElemData,0,fnum));
fnum = mxGetFieldNumber(ElemData,"R2");
NewFieldNumbers[6] = fnum;
if(fnum<0)
pr2 = NULL;
else
pr2 = mxGetPr(mxGetFieldByNumber(ElemData,0,fnum));
fnum = mxGetFieldNumber(ElemData,"T1");
NewFieldNumbers[7] = fnum;
if(fnum<0)
pt1 = NULL;
else
pt1 = mxGetPr(mxGetFieldByNumber(ElemData,0,fnum));
fnum = mxGetFieldNumber(ElemData,"T2");
NewFieldNumbers[8] = 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]));
/* Optional fields */
if(FieldNumbers[5]<0)
pr1 = NULL;
else
pr1 = mxGetPr(mxGetFieldByNumber(ElemData,0,FieldNumbers[5]));
if(FieldNumbers[6]<0)
pr2 = NULL;
else
pr2 = mxGetPr(mxGetFieldByNumber(ElemData,0,FieldNumbers[6]));
if(FieldNumbers[7]<0)
pt1 = NULL;
else
pt1 = mxGetPr(mxGetFieldByNumber(ElemData,0,FieldNumbers[7]));
if(FieldNumbers[8]<0)
pt2 = NULL;
else
pt2 = mxGetPr(mxGetFieldByNumber(ElemData,0,FieldNumbers[8]));
returnptr = FieldNumbers;
} break;
default:
{ mexErrMsgTxt("No match for calling mode in function StrMPoleSymplectic4Pass\n");
}
}
StrMPoleSymplectic4Pass(r_in, le, A, B, max_order, num_int_steps,
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, *A, *B, *pr1, *pr2, *pt1, *pt2;
int max_order, num_int_steps;
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");
/* Optionnal arguments */
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;
plhs[0] = mxDuplicateArray(prhs[1]);
r_in = mxGetPr(plhs[0]);
StrMPoleSymplectic4Pass(r_in, le, A, B, max_order, num_int_steps,
pt1, pt2, pr1, pr2, n);
}
else
{ /* return list of required fields */
plhs[0] = mxCreateCellMatrix(5,1);
mxSetCell(plhs[0],0,mxCreateString("Length"));
mxSetCell(plhs[0],1,mxCreateString("PolynomA"));
mxSetCell(plhs[0],2,mxCreateString("PolynomB"));
mxSetCell(plhs[0],3,mxCreateString("MaxOrder"));
mxSetCell(plhs[0],4,mxCreateString("NumIntSteps"));
if(nlhs>1) /* Required and optional fields */
{ plhs[1] = mxCreateCellMatrix(4,1);
mxSetCell(plhs[1],0,mxCreateString("T1"));
mxSetCell(plhs[1],1,mxCreateString("T2"));
mxSetCell(plhs[1],2,mxCreateString("R1"));
mxSetCell(plhs[1],3,mxCreateString("R2"));
}
}
}