OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
EnvelopeBunch.h
Go to the documentation of this file.
1 #ifndef _ENVELOPE_BUNCH_H
2 #define _ENVELOPE_BUNCH_H
3 
7 #include "Algorithms/PartBunch.h"
8 #include <Physics/Physics.h>
9 
10 #include <assert.h>
11 #include <vector>
12 
13 #include <memory>
14 
18  sp_z,
19  sp_I,
29 };
30 
32  sv_fixedStep = 0x0001,
33  sv_fieldOutside = 0x0002,
34  sv_radial = 0x0004,
35  sv_offaxis = 0x0008,
36  sv_lwakes = 0x0010,
37  sv_twakes = 0x0020,
38  sv_s_path = 0x0100
39 };
40 
41 enum DataStatus {
47  ds_spaceCharge = 0x001c
48 };
49 
53 };
54 
55 
60 class EnvelopeBunch : public PartBunch {
61 
62 public:
64  EnvelopeBunch(const PartData *ref);
65 
67  EnvelopeBunch(const std::vector<OpalParticle> &,
68  const PartData *ref);
69 
71  EnvelopeBunch(const EnvelopeBunch &) = delete;
72 
73  virtual ~EnvelopeBunch();
74 
76  void createBunch();
77 
79  void distributeSlices(int nSlice = 101);
80 
81  bool isValid_m;
82 
84  std::unique_ptr<Profile> currentProfile_m;
85 
88 
91  void initialize(int sli, double charge, double energy, double width, double te, double frac,
92  double current, double center, double bX, double bY, double mX, double mY, double Bz, int nbin);
93 
95  bool isRadial() { return solver_m & sv_radial; }
96 
98  bool isOffaxis() { return solver_m & sv_offaxis; }
99 
101  void calcBeamParameters();
102 
103  void computeSpaceCharge();
104 
112  void timeStep(double tStep, double zCat = 0.0);
113 
121  void derivs(double tc, double Y[], double dYdt[]);
122 
124  double Eavg();
126  double zAvg();
128  double zTail();
130  double zHead();
132  double time() { return t_m; }
133 
135  void setEx(double emi) { emtnx0_m = emi; }
137  void setEy(double emi) { emtny0_m = emi; }
139  void setSolverParameter(int s) { solver_m = s; }
140  // set particle energy of bunch in [eV] and optional the correlated energy spread [eV/m]
141  void setEnergy(double, double = 0.0);
142 
143  void setExternalFields(int i, const Vector_t &EF, const Vector_t &BF, const Vector_t &KR, const Vector_t &KT) {
144  this->EF[i] = EF;
145  this->BF[i] = BF;
146  this->KR[i] = KR;
147  this->KT[i] = KT;
148  }
149 
151  EF = this->EF[i];
152  BF = this->BF[i];
153  KR = this->KR[i];
154  KT = this->KT[i];
155  }
156 
158  double get_sPos();
160  double AvBField();
162  double AvEField();
164  int getLocalNum() { return numMySlices_m; }
166  int getTotalNum() { return numSlices_m; }
168  double getT() { return t_m; }
170  double get_meanKineticEnergy() { return Eavg(); }
172  double get_dEdt() { return dEdt_m; }
174  Vector_t sigmax() { return sigmax_m; }
176  Vector_t sigmap() { return sigmap_m; }
178  Vector_t emtn() { return emtn_m; }
182  Vector_t maxX() { return maxX_m; }
184  Vector_t minX() { return minX_m; }
186  Vector_t maxP() { return maxP_m; }
188  Vector_t minP() { return minP_m; }
190  double getChargePerParticle() { return Q_m / numSlices_m; }
192  Vector_t get_rrms() { return sigmax_m; }
194  Vector_t get_prms() { return sigmap_m; }
195 
198  size_t numMySlices() { return numMySlices_m; }
199 
200  Inform &slprint(Inform &os);
201 
203  void setCharge(double _Q) {
204  sign_m = _Q < 0.0 ? -1 : 1;
205  Q_m = std::abs(_Q);
206  }
207 
209  double getGamma(int i) {
210  assert(i < numMySlices_m);
211  return slices_m[i]->computeGamma();
212  }
213 
215  double getBeta(int i) {
216  assert(i < numMySlices_m);
217  return slices_m[i]->p[SLI_beta];
218  }
219  void setBeta(int i, double val) {
220  assert(i < numMySlices_m);
221  slices_m[i]->p[SLI_beta] = val;
222  }
223 
224  void setR(int i, const Vector_t &R) {
225  assert(i < numMySlices_m);
226  slices_m[i]->p[SLI_x] = R[0];
227  slices_m[i]->p[SLI_y] = R[1];
228  slices_m[i]->p[SLI_z] = R[2];
229  }
230 
231  Vector_t getR(int i) {
232  assert(i < numMySlices_m);
233  Vector_t R;
234  R[0] = slices_m[i]->p[SLI_x];
235  R[1] = slices_m[i]->p[SLI_y];
236  R[2] = slices_m[i]->p[SLI_z];
237 
238  return R;
239  }
240 
241  void setP(int i, const Vector_t &P) {
242  assert(i < numMySlices_m);
243  slices_m[i]->p[SLI_px] = P[0];
244  slices_m[i]->p[SLI_py] = P[1];
245  slices_m[i]->p[SLI_beta] = P[2] / sqrt(P[2]*P[2] + 1.0);
246  }
247 
248  Vector_t getP(int i) {
249  assert(i < numMySlices_m);
250  Vector_t P;
251  P[0] = slices_m[i]->p[SLI_px];
252  P[1] = slices_m[i]->p[SLI_py];
253  P[2] = slices_m[i]->p[SLI_beta] * slices_m[i]->computeGamma();
254 
255  return P;
256  }
257 
259  void setZ(int i, double coo) {
260  assert(i < numMySlices_m);
261  slices_m[i]->p[SLI_z] = coo;
262  }
263 
265  double getZ(int i) {
266  assert(i < numMySlices_m);
267  return slices_m[i]->p[SLI_z];
268  }
269 
271  double getX(int i) {
272  assert(i < numMySlices_m);
273  return slices_m[i]->p[SLI_x];
274  }
275  void setX(int i, double val) {
276  assert(i < numMySlices_m);
277  slices_m[i]->p[SLI_x] = val;
278  }
279 
281  double getY(int i) {
282  assert(i < numMySlices_m);
283  return slices_m[i]->p[SLI_y];
284  }
285  void setY(int i, double val) {
286  assert(i < numMySlices_m);
287  slices_m[i]->p[SLI_y] = val;
288  }
289 
291  double getX0(int i) {
292  assert(i < numMySlices_m);
293  return slices_m[i]->p[SLI_x0];
294  }
295  void setX0(int i, double val) {
296  assert(i < numMySlices_m);
297  slices_m[i]->p[SLI_x0] = val;
298  }
299 
301  double getY0(int i) {
302  assert(i < numMySlices_m);
303  return slices_m[i]->p[SLI_y0];
304  }
305  void setY0(int i, double val) {
306  assert(i < numMySlices_m);
307  slices_m[i]->p[SLI_y0] = val;
308  }
309 
311  double getPx(int i) {
312  assert(i < numMySlices_m);
313  return slices_m[i]->p[SLI_px];
314  }
315  void setPx(int i, double val) {
316  assert(i < numMySlices_m);
317  slices_m[i]->p[SLI_px] = val;
318  }
319 
321  double getPy(int i) {
322  assert(i < numMySlices_m);
323  return slices_m[i]->p[SLI_py];
324  }
325  void setPy(int i, double val) {
326  assert(i < numMySlices_m);
327  slices_m[i]->p[SLI_py] = val;
328  }
329 
331  double getPz(int i) {
332  assert(i < numMySlices_m);
333  return slices_m[i]->p[SLI_beta] * Physics::m_e * slices_m[i]->computeGamma();
334  }
335 
337  double getPx0(int i) {
338  assert(i < numMySlices_m);
339  return slices_m[i]->p[SLI_px0];
340  }
341  void setPx0(int i, double val) {
342  assert(i < numMySlices_m);
343  slices_m[i]->p[SLI_px0] = val;
344  }
345 
347  double getPy0(int i) {
348  assert(i < numMySlices_m);
349  return slices_m[i]->p[SLI_py0];
350  }
351  void setPy0(int i, double val) {
352  assert(i < numMySlices_m);
353  slices_m[i]->p[SLI_py0] = val;
354  }
355 
358  min[0] = 0.0;
359  min[1] = 0.0;
360  min[2] = this->zTail();
361  max[0] = 0.0;
362  max[1] = 0.0;
363  max[2] = this->zHead();
364  }
365 
366 
367 private:
377  std::vector<double> z_m;
379  std::vector<double> b_m;
381  std::vector< std::vector<int> > bins_m;
383  double hbin_m;
385  int nebin_m;
390 
392  int solver_m;
394  int dStat_m;
396  double t_m;
398  double t_offset_m;
404  double dx0_m, dy0_m;
406  double dfi_x_m, dfi_y_m;
408  double Bz0_m;
410  double Q_m;
412  double I0avg_m;
422  std::unique_ptr<Vector_t[]> KR;
424  std::unique_ptr<Vector_t[]> KT;
426  std::unique_ptr<Vector_t[]> EF;
428  std::unique_ptr<Vector_t[]> BF;
430  std::vector< std::shared_ptr<EnvelopeSlice> > slices_m;
431  unsigned int activeSlice_m;
433  int sign_m;
437  double zCat_m;
439  std::vector<double> Exw_m;
441  std::vector<double> Eyw_m;
443  std::vector<double> Ezw_m;
445  std::vector<double> Esct_m;
447  std::vector<double> G_m;
448 
449  int nValid_m;
450  double z0_m;
453  double E_m;
454  double dEdt_m;
455  double Einc_m;
456  double tau_m;
457  double I_m;
458  double Irms_m;
459  double Rx_m;
460  double Ry_m;
461  double RxMax_m;
462  double RyMax_m;
463  double RxMin_m;
464  double RyMin_m;
465  double Px_m;
466  double Py_m;
467  double x0_m;
468  double y0_m;
469  double x0Max_m;
470  double y0Max_m;
471  double x0Min_m;
472  double y0Min_m;
473  // double dx02_m;
474  // double dy02_m;
475  // double dfi_x_m;
476  // double dfi_y_m;
477  double Ez_m;
478  double Bz_m;
487 
488  // used in derivs() to remove call to zHead/Tail() containing MPI
489  // collectives causing problems if slices are unevenly distributed across
490  // processors.
491  double curZHead_m;
492  double curZTail_m;
493 
498  void synchronizeSlices();
499 
510  void runStats(EnvelopeBunchParameter sp, double *xAvg, double *xMax, double *xMin, double *rms, int *nValid);
511 
521  void calcEmittance(double *emtnx, double *emtny, double *emtx, double *emty, int *nValid);
522 
531  void calcEnergyChirp(double *g0, double *dgdt, double *gInc, int *nValid);
532 
541  void setBinnedLShape(EnvelopeBunchShape shape, double z0, double w, double frac);
542 
552  void setTShape(double enx, double eny, double rx, double ry, double b0);
553 
562  void setTOffset(double x0, double px0, double y0, double py0);
563 
565  double moveZ0(double zC);
566 
568  void backup() {
569  for (auto & slice : slices_m)
570  slice->backup();
571  }
572 
573 
576  double tReset(double dt = 0.0);
577 
583  void cSpaceCharge();
584 
586  void calcI();
587 };
588 
590  return p.slprint(os);
591 }
592 #endif
std::unique_ptr< Vector_t[]> EF
external E fields
size_t mySliceEndOffset_m
last global slice on this processor
double zCat_m
cathode position
pX0 angular deflection centriod x
Definition: EnvelopeSlice.h:22
Definition: rbendmap.h:8
ParticleAttrib< Vector_t > P
void setR(int i, const Vector_t &R)
std::ostream & operator<<(std::ostream &os, const Attribute &attr)
Definition: Attribute.cpp:167
std::vector< double > Exw_m
transverse wake field x
void setP(int i, const Vector_t &P)
void setBeta(int i, double val)
void setPy(int i, double val)
void setPy0(int i, double val)
double get_dEdt()
returns the energy spread
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
Vector_t minX()
returns vector with the min spatial extends of the bunch
double get_meanKineticEnergy()
returns the mean energy
void calcEmittance(double *emtnx, double *emtny, double *emtx, double *emty, int *nValid)
calculate bunch emittance
Vector_t get_rrms()
returns RMS x,y,z
double emtnx0_m
intrinsic normalized emittance of slice [m rad]
double getT()
returns the current time of the bunch
void backup()
backup slice values
wakes and space-charge fields calculated
Definition: EnvelopeBunch.h:45
double t_m
local time in bunch [s]
std::vector< double > b_m
synchronized betas for parallel tracker
y beam size y (rms) [m]
Definition: EnvelopeSlice.h:16
core of the envelope tracker based on Rene Bakkers BET implementation
Definition: EnvelopeBunch.h:60
Vector_t maxX()
returns vector with the max spatial extends of the bunch
Vector_t get_norm_emit()
returns vector with normalized emittance
double dfi_x_m
rotation of coordinate system when tracking along the s-axis [rad]
std::unique_ptr< Vector_t[]> KR
define value of radial kick for each slice
unsigned int activeSlice_m
void setCharge(double _Q)
set the charge of the bunch
IpplTimings::TimerRef calcITimer_m
Definition: EnvelopeBunch.h:86
void synchronizeSlices()
synchronize z position and betas of all slices (needed in calcI and space charge calculation) ...
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:123
double getZ(int i)
returns Z coordinate of slice i
Vector_t KTsl_m
transverse kick of beam
Vector_t maxP_m
EnvelopeBunchParameter
Definition: EnvelopeBunch.h:15
SolverParameters
Definition: EnvelopeBunch.h:31
z slice position [m]
Definition: EnvelopeSlice.h:8
int numMySlices_m
number of my slices in bunch
virtual ~EnvelopeBunch()
void initialize(int sli, double charge, double energy, double width, double te, double frac, double current, double center, double bX, double bY, double mX, double mY, double Bz, int nbin)
Particle reference data.
Definition: PartData.h:38
void setZ(int i, double coo)
set Z coordinate of slice i
Vector_t sigmax_m
void createBunch()
create and initialize local num slices
slice data synchronized with other MPI nodes
Definition: EnvelopeBunch.h:44
beta normalized velocity (total) [-]
Definition: EnvelopeSlice.h:10
double getGamma(int i)
returns gamma of slice i
double zAvg()
calculate &lt;z&gt; [m]
bool isOffaxis()
check if solver includes off-axis tracking
Definition: EnvelopeBunch.h:98
void setTOffset(double x0, double px0, double y0, double py0)
set transverse offset of bunch
std::vector< std::vector< int > > bins_m
bins for emission
void setX(int i, double val)
int sign_m
gives the sign of charge Q
double getX0(int i)
returns X coordinate of the centroid of slice i
double hbin_m
emission bin width
double zHead()
calculate the head of the bunch [m]
double emtbx0_m
intrinsic normalized emittance Bush effect [m rad]
void getExternalFields(int i, Vector_t &EF, Vector_t &BF, Vector_t &KR, Vector_t &KT) const
transverse wakes calculated
Definition: EnvelopeBunch.h:47
Vector_t sigmax()
returns vector with rms position
constexpr double m_e
The electron rest mass in GeV.
Definition: Physics.h:85
double AvBField()
returns average magnetic field
angular deflection centriod x
Definition: EnvelopeBunch.h:28
pY0 angular deflection centriod y
Definition: EnvelopeSlice.h:26
EnvelopeBunch(const PartData *ref)
Default constructor.
int solver_m
see enum SolverParameters
double Bz0_m
magnetic field on cathode [T]
beam size y [m]
Definition: EnvelopeBunch.h:22
Vector_t minP_m
int dStat_m
see enum DataStatus
void setEx(double emi)
set emittance X
py beam divergence y [rad]
Definition: EnvelopeSlice.h:18
void setTShape(double enx, double eny, double rx, double ry, double b0)
set transverse shape of bunch (initial distribution)
Vector_t maxP()
returns vector with the max momentum of the bunch
Vector_t emtn()
returns vector with emittance
double getPy(int i)
returns Y momenta of slice i
double zTail()
calculate tail of bunch [m]
std::unique_ptr< Vector_t[]> BF
external B fields
double getY0(int i)
returns Y coordinate of the centroid of slice i
int numSlices_m
number of total slices in bunch
std::unique_ptr< Profile > currentProfile_m
current profile of bunch (fit)
Definition: EnvelopeBunch.h:84
int currentSlice_m
current Slice set in run() &amp; cSpaceCharge() and used in derivs() &amp; zcsI()
double get_sPos()
return reference position
include radial movement in DV
Definition: EnvelopeBunch.h:35
void setExternalFields(int i, const Vector_t &EF, const Vector_t &BF, const Vector_t &KR, const Vector_t &KT)
double getPx0(int i)
returns angular deflection centroid in x of slice i
bool isRadial()
check if solver includes radial
Definition: EnvelopeBunch.h:95
Vector_t Esl_m
electric field
size_t mySliceEndOffset()
double I0avg_m
average current on creation of bunch (see setLshape)
size_t lastEmittedBin_m
longitudinal wakes calculated
Definition: EnvelopeBunch.h:46
size_t numMySlices()
fields synchronized with other MPI nodes
Definition: EnvelopeBunch.h:43
X0 position centroid x [m].
Definition: EnvelopeSlice.h:20
int getLocalNum()
returns the number of local slices
slice position [m]
Definition: EnvelopeBunch.h:19
void setX0(int i, double val)
std::vector< double > Esct_m
Longitudinal Space-charge field.
double getPy0(int i)
returns angular deflection centroid in y of slice i
int activeSlices_m
number of active slices
void calcI()
calculates the current current distribution
double Eavg()
calculate the average energy of the bunch
std::vector< std::shared_ptr< EnvelopeSlice > > slices_m
array of slices
double emission_time_step_m
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
ParticleAttrib< double > dt
beam divergence y
Definition: EnvelopeBunch.h:24
Vector_t getP(int i)
Lorenz factor.
Definition: EnvelopeBunch.h:18
std::vector< double > Ezw_m
longitudinal wake field
EnvelopeBunchShape
Definition: EnvelopeBunch.h:50
void setY(int i, double val)
void initialize(FieldLayout_t *fLayout)
Definition: PartBunch.cpp:71
void derivs(double tc, double Y[], double dYdt[])
helper function to calculate derivatives need in RH equation
double getY(int i)
returns Y coordinate of slice i
Inform & slprint(Inform &os)
void computeSpaceCharge()
longitudinal wakes
Definition: EnvelopeBunch.h:37
double AvEField()
returns average electric field
Vector_t norm_emtn_m
normalized velocity (total) [-]
Definition: EnvelopeBunch.h:17
std::vector< double > z_m
synchronized z positions for parallel tracker
double t_offset_m
accumulated time offset by tReset function
void setY0(int i, double val)
void distributeSlices(int nSlice=101)
distributes nSlice amongst processors and initializes slices
void setBinnedLShape(EnvelopeBunchShape shape, double z0, double w, double frac)
set longitudinal shape of bunch (initial distribution)
Particle Bunch.
Definition: PartBunch.h:30
double getChargePerParticle()
returns charge per slice
beam size x [m]
Definition: EnvelopeBunch.h:21
DataStatus
Definition: EnvelopeBunch.h:41
double getPz(int i)
returns Z momenta of slice i
Y0 position centroid y [m].
Definition: EnvelopeSlice.h:24
void setSolverParameter(int s)
set the DE solver flag
Vector_t getR(int i)
x beam size x (rms) [m]
Definition: EnvelopeSlice.h:12
beam divergence x
Definition: EnvelopeBunch.h:23
double getBeta(int i)
returns beta of slice i
double Q_m
total bunch charge [C]
void setPx0(int i, double val)
Vector_t KRsl_m
radial focussing term beam
Vector_t maxX_m
Vector_t sigmap()
returns vector with rms momenta
void setEy(double emi)
set emittance Y
double tReset(double dt=0.0)
void timeStep(double tStep, double zCat=0.0)
performs a time-step for all active slices (also handles emission)
px beam divergence x [rad]
Definition: EnvelopeSlice.h:14
double getPx(int i)
returns X momenta of slice i
void get_bounds(Vector_t &min, Vector_t &max)
returns bounds of envelope bunch
position centroid y [m]
Definition: EnvelopeBunch.h:27
Timing::TimerRef TimerRef
Definition: IpplTimings.h:176
Vector_t emtn_m
std::vector< double > Eyw_m
transverse wake field y
int nebin_m
number of bins for emission
include off-axis movement in DV
Definition: EnvelopeBunch.h:36
beam divergence z
Definition: EnvelopeBunch.h:25
int getTotalNum()
returns the total number of slices
double moveZ0(double zC)
move the complete bunch forward such that the head of the bunch matches the cahtode position ...
size_t mySliceStartOffset()
current [A]
Definition: EnvelopeBunch.h:20
Definition: Inform.h:41
void setEnergy(double, double=0.0)
Vector_t Bsl_m
magnetic field
transverse wakes
Definition: EnvelopeBunch.h:38
void runStats(EnvelopeBunchParameter sp, double *xAvg, double *xMax, double *xMin, double *rms, int *nValid)
run statistics on slices
Vector_t minX_m
void calcBeamParameters()
calculates envelope statistics
T::PETE_Expr_t::PETE_Return_t min(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:95
void setPx(int i, double val)
Vector_t sigmap_m
int firstBinWithValue_m
first bin on processor containing slices
double time()
read time-stamp of bunch
void calcEnergyChirp(double *g0, double *dgdt, double *gInc, int *nValid)
calculate the energy chirp and uncorrelated energy spread
IpplTimings::TimerRef spaceChargeTimer_m
Definition: EnvelopeBunch.h:87
std::unique_ptr< Vector_t[]> KT
define value of transversal kick for each slice
position centroid x [m]
Definition: EnvelopeBunch.h:26
Vector_t minP()
returns vector with the min momentum of the bunch
size_t mySliceStartOffset_m
first global slice on this processor
double dx0_m
offset of the coordinate system when tracking along the s-axis [m]
solve DV with fixed time-step
Definition: EnvelopeBunch.h:33
Vector_t get_prms()
returns RMSP x,y,z
solve field outside of DV
Definition: EnvelopeBunch.h:34
std::vector< double > G_m
Transverse Space-charge term: Eq.(9)
double getX(int i)
returns X coordinate of slice i