Skip to content

Commit 99ec260

Browse files
committed
TimeLag and PhaseLag
1 parent e1c4ae6 commit 99ec260

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

atintegrators/CrabCavityPass.c

+24-12
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ struct elem
1414
double Vy;
1515
double Frequency;
1616
double Energy;
17-
double Phase;
17+
double TimeLag;
18+
double PhaseLag;
1819
double SigPhi;
1920
double SigVV;
2021
};
@@ -37,7 +38,7 @@ static void CrabCavityPass(double *r_in, double le, double nvx, double nvy,
3738
double k = TWOPI*freq/C0;
3839

3940
if (sigphi > 0.0)
40-
phi = phi + atrandn_r(rng, 0.0, sigphi);
41+
phi += atrandn_r(rng, 0.0, sigphi);
4142
if (sigvv > 0.0) {
4243
nvx *= atrandn_r(rng, 1.0, sigvv);
4344
nvy *= atrandn_r(rng, 1.0, sigvv);
@@ -75,15 +76,17 @@ ExportMode struct elem *trackFunction(const atElem *ElemData,struct elem *Elem,
7576
double *r_in, int num_particles, struct parameters *Param)
7677
{
7778
double energy;
79+
double lag, t0f, phiref;
7880

7981
if (!Elem) {
80-
double Length, *Voltages, Energy, Frequency, Phase, SigPhi, SigVV;
82+
double Length, *Voltages, Energy, Frequency, TimeLag, PhaseLag, SigPhi, SigVV;
8183
Length = atGetDouble(ElemData,"Length"); check_error();
8284
Voltages = atGetDoubleArray(ElemData,"Voltages"); check_error();
8385
Frequency = atGetDouble(ElemData,"Frequency"); check_error();
8486
/* Optional fields */
8587
Energy = atGetOptionalDouble(ElemData,"Energy",Param->energy); check_error();
86-
Phase = atGetOptionalDouble(ElemData,"Phase",0.0); check_error();
88+
TimeLag=atGetOptionalDouble(ElemData,"TimeLag",0.0); check_error();
89+
PhaseLag=atGetOptionalDouble(ElemData,"PhaseLag",0.0); check_error();
8790
SigPhi = atGetOptionalDouble(ElemData,"SigPhi",0.0); check_error();
8891
SigVV = atGetOptionalDouble(ElemData,"SigVV",0.0); check_error();
8992

@@ -93,13 +96,17 @@ ExportMode struct elem *trackFunction(const atElem *ElemData,struct elem *Elem,
9396
Elem->Vy=Voltages[1];
9497
Elem->Energy=Energy;
9598
Elem->Frequency=Frequency;
96-
Elem->Phase=Phase;
99+
Elem->TimeLag=TimeLag;
100+
Elem->PhaseLag=PhaseLag;
97101
Elem->SigPhi=SigPhi;
98102
Elem->SigVV=SigVV;
99103
}
100104
energy = atEnergy(Param->energy, Elem->Energy);
105+
t0f = Elem->Frequency * Param->T0;
106+
lag = TWOPI*Elem->Frequency*Elem->TimeLag/C0 + Elem->PhaseLag;
107+
phiref = TWOPI * (t0f - round(t0f)) * Param->nturn - lag;
101108
CrabCavityPass(r_in, Elem->Length, Elem->Vx/energy, Elem->Vy/energy,
102-
Elem->Frequency, Elem->Phase, Elem->SigPhi, Elem->SigVV, Param->common_rng, num_particles);
109+
Elem->Frequency, phiref, Elem->SigPhi, Elem->SigVV, Param->common_rng, num_particles);
103110
return Elem;
104111
}
105112

@@ -111,6 +118,7 @@ MODULE_DEF(CrabCavityPass) /* Dummy module initialisation */
111118
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
112119
{
113120
if (nrhs >= 2) {
121+
double lag, phiref;
114122
double *r_in;
115123
double rest_energy = 0.0;
116124
double charge = -1.0;
@@ -121,7 +129,8 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
121129
double Frequency = atGetDouble(ElemData,"Frequency"); check_error();
122130
/* Optional fields */
123131
double Energy = atGetOptionalDouble(ElemData,"Energy",0.0); check_error();
124-
double Phase = atGetOptionalDouble(ElemData,"Phase",0.0); check_error();
132+
double TimeLag = atGetOptionalDouble(ElemData,"TimeLag",0.0); check_error();
133+
double PhaseLag = atGetOptionalDouble(ElemData,"PhaseLag",0.0); check_error();
125134
double SigPhi = atGetOptionalDouble(ElemData,"SigPhi",0.0); check_error();
126135
double SigVV = atGetOptionalDouble(ElemData,"SigVV",0.0); check_error();
127136
if (nrhs > 2) atProperties(prhs[2], &Energy, &rest_energy, &charge);
@@ -130,20 +139,23 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
130139
/* ALLOCATE memory for the output array of the same size as the input */
131140
plhs[0] = mxDuplicateArray(prhs[1]);
132141
r_in = mxGetDoubles(plhs[0]);
142+
lag = TWOPI*Frequency*TimeLag/C0 + PhaseLag;
143+
phiref = -lag;
133144
CrabCavityPass(r_in, Length, Voltages[0]/Energy, Voltages[1]/Energy,
134-
Frequency, Phase, SigPhi, SigVV, &pcg32_global, num_particles);
145+
Frequency, phiref, SigPhi, SigVV, &pcg32_global, num_particles);
135146
}
136147
else if (nrhs == 0) {
137148
plhs[0] = mxCreateCellMatrix(3,1);
138149
mxSetCell(plhs[0],0,mxCreateString("Length"));
139150
mxSetCell(plhs[0],1,mxCreateString("Voltages"));
140151
mxSetCell(plhs[0],2,mxCreateString("Frequency"));
141152
if (nlhs > 1) { /* optional fields */
142-
plhs[1] = mxCreateCellMatrix(4,1);
153+
plhs[1] = mxCreateCellMatrix(5,1);
143154
mxSetCell(plhs[0],0,mxCreateString("Energy"));
144-
mxSetCell(plhs[1],1,mxCreateString("Phase"));
145-
mxSetCell(plhs[1],2,mxCreateString("SigPhi"));
146-
mxSetCell(plhs[1],3,mxCreateString("SigVV"));
155+
mxSetCell(plhs[1],1,mxCreateString("TimeLag"));
156+
mxSetCell(plhs[1],2,mxCreateString("PhaseLag"));
157+
mxSetCell(plhs[1],3,mxCreateString("SigPhi"));
158+
mxSetCell(plhs[1],4,mxCreateString("SigVV"));
147159
}
148160
}
149161
else {

0 commit comments

Comments
 (0)