@@ -14,6 +14,7 @@ struct elem
1414 int nturnsw ;
1515 int blmode ;
1616 int cavitymode ;
17+ int buffersize ;
1718 double normfact ;
1819 double phasegain ;
1920 double voltgain ;
@@ -34,7 +35,16 @@ struct elem
3435 double * vbeam ;
3536 double * vcav ;
3637 double * vgen ;
38+ double * vgen_buffer ;
39+ double * vbeam_buffer ;
40+ double * vbunch_buffer ;
3741};
42+
43+
44+ void write_buffer (double * data , double * buffer , int datasize , int buffersize ){
45+ memmove (buffer , buffer + datasize , datasize * buffersize * sizeof (double ));
46+ memcpy (buffer + datasize * (buffersize - 1 ), data , datasize * sizeof (double ));
47+ }
3848
3949
4050void BeamLoadingCavityPass (double * r_in ,int num_particles ,int nbunch ,
@@ -48,6 +58,7 @@ void BeamLoadingCavityPass(double *r_in,int num_particles,int nbunch,
4858 long nslice = Elem -> nslice ;
4959 long nturnsw = Elem -> nturnsw ;
5060 long blmode = Elem -> blmode ;
61+ long buffersize = Elem -> buffersize ;
5162 double normfact = Elem -> normfact ;
5263 double le = Elem -> Length ;
5364 double energy = Elem -> Energy ;
@@ -58,6 +69,9 @@ void BeamLoadingCavityPass(double *r_in,int num_particles,int nbunch,
5869 double rshunt = Elem -> Rshunt ;
5970 double beta = Elem -> Beta ;
6071 double * turnhistory = Elem -> turnhistory ;
72+ double * vgen_buffer = Elem -> vgen_buffer ;
73+ double * vbeam_buffer = Elem -> vbeam_buffer ;
74+ double * vbunch_buffer = Elem -> vbunch_buffer ;
6175 double * z_cuts = Elem -> z_cuts ;
6276 double * vbunch = Elem -> vbunch ;
6377 double phasegain = Elem -> phasegain ;
@@ -89,7 +103,6 @@ void BeamLoadingCavityPass(double *r_in,int num_particles,int nbunch,
89103 /*Track RF cavity is always done. */
90104 trackRFCavity (r_in ,le ,vgen /energy ,rffreq ,harmn ,tlag ,- psi ,nturn ,circumference /C0 ,num_particles );
91105
92-
93106 /*Only allocate memory if current is > 0*/
94107 if (tot_current > 0 ){
95108 void * buffer = atMalloc (sz );
@@ -126,6 +139,11 @@ void BeamLoadingCavityPass(double *r_in,int num_particles,int nbunch,
126139 if (cavitymode == 1 ){
127140 update_vgen (vbeamk ,vcavk ,vgenk ,phasegain ,voltgain );
128141 }
142+ if (buffersize > 0 ){
143+ write_buffer (vbeamk , vbeam_buffer , 2 , buffersize );
144+ write_buffer (vgenk , vgen_buffer , 2 , buffersize );
145+ write_buffer (vbunch , vbunch_buffer , 2 * nbunch , buffersize );
146+ }
129147 atFree (buffer );
130148 }
131149}
@@ -138,10 +156,13 @@ ExportMode struct elem *trackFunction(const atElem *ElemData,struct elem *Elem,
138156 double rl = Param -> RingLength ;
139157 int nturn = Param -> nturn ;
140158 if (!Elem ) {
141- long nslice ,nturns ,blmode ,cavitymode ;
159+ long nslice ,nturns ,blmode ,cavitymode , buffersize ;
142160 double wakefact ;
143161 double normfact , phasegain , voltgain ;
144162 double * turnhistory ;
163+ double * vgen_buffer ;
164+ double * vbeam_buffer ;
165+ double * vbunch_buffer ;
145166 double * z_cuts ;
146167 double Energy , Frequency , TimeLag , Length ;
147168 double qfactor ,rshunt ,beta ;
@@ -160,6 +181,7 @@ ExportMode struct elem *trackFunction(const atElem *ElemData,struct elem *Elem,
160181 nslice = atGetLong (ElemData ,"_nslice" ); check_error ();
161182 nturns = atGetLong (ElemData ,"_nturns" ); check_error ();
162183 blmode = atGetLong (ElemData ,"_blmode" ); check_error ();
184+ buffersize = atGetLong (ElemData ,"_buffersize" ); check_error ();
163185 cavitymode = atGetLong (ElemData ,"_cavitymode" ); check_error ();
164186 wakefact = atGetDouble (ElemData ,"_wakefact" ); check_error ();
165187 qfactor = atGetDouble (ElemData ,"Qfactor" ); check_error ();
@@ -174,6 +196,9 @@ ExportMode struct elem *trackFunction(const atElem *ElemData,struct elem *Elem,
174196 vcav = atGetDoubleArray (ElemData ,"_vcav" ); check_error ();
175197 vgen = atGetDoubleArray (ElemData ,"_vgen" ); check_error ();
176198 vbeam_phasor = atGetDoubleArray (ElemData ,"_vbeam_phasor" ); check_error ();
199+ vgen_buffer = atGetDoubleArray (ElemData ,"_vgen_buffer" ); check_error ();
200+ vbeam_buffer = atGetDoubleArray (ElemData ,"_vbeam_buffer" ); check_error ();
201+ vbunch_buffer = atGetDoubleArray (ElemData ,"_vbunch_buffer" ); check_error ();
177202 /*optional attributes*/
178203
179204 z_cuts = atGetOptionalDoubleArray (ElemData ,"ZCuts" ); check_error ();
@@ -207,6 +232,10 @@ ExportMode struct elem *trackFunction(const atElem *ElemData,struct elem *Elem,
207232 Elem -> voltgain = voltgain ;
208233 Elem -> vbeam_phasor = vbeam_phasor ;
209234 Elem -> cavitymode = cavitymode ;
235+ Elem -> buffersize = buffersize ;
236+ Elem -> vgen_buffer = vgen_buffer ;
237+ Elem -> vbeam_buffer = vbeam_buffer ;
238+ Elem -> vbunch_buffer = vbunch_buffer ;
210239 }
211240 if (num_particles < Param -> nbunch ){
212241 atError ("Number of particles has to be greater or equal to the number of bunches." );
@@ -245,7 +274,7 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
245274 int num_particles = mxGetN (prhs [1 ]);
246275 struct elem El , * Elem = & El ;
247276
248- long nslice ,nturns ,blmode ,cavitymode ;
277+ long nslice ,nturns ,blmode ,cavitymode , buffersize ;
249278 double wakefact ;
250279 double normfact , phasegain , voltgain ;
251280 double * turnhistory ;
@@ -257,6 +286,9 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
257286 double * vbeam ;
258287 double * vgen ;
259288 double * vcav ;
289+ double * vgen_buffer ;
290+ double * vbeam_buffer ;
291+ double * vbunch_buffer ;
260292 /*attributes for RF cavity*/
261293 Length = atGetDouble (ElemData ,"Length" ); check_error ();
262294 Energy = atGetDouble (ElemData ,"Energy" ); check_error ();
@@ -266,6 +298,7 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
266298 nslice = atGetLong (ElemData ,"_nslice" ); check_error ();
267299 nturns = atGetLong (ElemData ,"_nturns" ); check_error ();
268300 blmode = atGetLong (ElemData ,"_blmode" ); check_error ();
301+ buffersize = atGetLong (ElemData ,"_buffersize" ); check_error ();
269302 cavitymode = atGetLong (ElemData ,"_cavitymode" ); check_error ();
270303 wakefact = atGetDouble (ElemData ,"_wakefact" ); check_error ();
271304 qfactor = atGetDouble (ElemData ,"Qfactor" ); check_error ();
@@ -280,6 +313,9 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
280313 vcav = atGetDoubleArray (ElemData ,"_vcav" ); check_error ();
281314 vgen = atGetDoubleArray (ElemData ,"_vgen" ); check_error ();
282315 vbeam_phasor = atGetDoubleArray (ElemData ,"_vbeam_phasor" ); check_error ();
316+ vgen_buffer = atGetDoubleArray (ElemData ,"_vgen_buffer" ); check_error ();
317+ vbeam_buffer = atGetDoubleArray (ElemData ,"_vbeam_buffer" ); check_error ();
318+ vbunch_buffer = atGetDoubleArray (ElemData ,"_vbunch_buffer" ); check_error ();
283319 /*optional attributes*/
284320 z_cuts = atGetOptionalDoubleArray (ElemData ,"ZCuts" ); check_error ();
285321
@@ -306,6 +342,10 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
306342 Elem -> phasegain = phasegain ;
307343 Elem -> voltgain = voltgain ;
308344 Elem -> vbeam_phasor = vbeam_phasor ;
345+ Elem -> buffersize = buffersize ;
346+ Elem -> vgen_buffer = vgen_buffer ;
347+ Elem -> vbeam_buffer = vbeam_buffer ;
348+ Elem -> vbunch_buffer = vbunch_buffer ;
309349
310350 if (mxGetM (prhs [1 ]) != 6 ) mexErrMsgIdAndTxt ("AT:WrongArg" ,"Second argument must be a 6 x N matrix" );
311351 /* ALLOCATE memory for the output array of the same size as the input */
@@ -318,7 +358,7 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
318358 }
319359 else if (nrhs == 0 )
320360 { /* return list of required fields */
321- plhs [0 ] = mxCreateCellMatrix (19 ,1 );
361+ plhs [0 ] = mxCreateCellMatrix (24 ,1 );
322362 mxSetCell (plhs [0 ],0 ,mxCreateString ("Length" ));
323363 mxSetCell (plhs [0 ],1 ,mxCreateString ("Energy" ));
324364 mxSetCell (plhs [0 ],2 ,mxCreateString ("Frequency" ));
@@ -339,6 +379,10 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
339379 mxSetCell (plhs [0 ],17 ,mxCreateString ("_vcav" ));
340380 mxSetCell (plhs [0 ],18 ,mxCreateString ("_vgen" ));
341381 mxSetCell (plhs [0 ],19 ,mxCreateString ("_vbeam_phasor" ));
382+ mxSetCell (plhs [0 ],20 ,mxCreateString ("_vgen_buffer" ));
383+ mxSetCell (plhs [0 ],21 ,mxCreateString ("_vbeam_buffer" ));
384+ mxSetCell (plhs [0 ],22 ,mxCreateString ("_vbunch_buffer" ));
385+ mxSetCell (plhs [0 ],23 ,mxCreateString ("_buffersize" ));
342386 if (nlhs > 1 ) /* optional fields */
343387 {
344388 plhs [1 ] = mxCreateCellMatrix (2 ,1 );
0 commit comments