OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
RK4.hpp
Go to the documentation of this file.
1 template <typename FieldFunction, typename ... Arguments>
3  const size_t& i,
4  const double& t,
5  const double dt,
6  Arguments& ... args) const
7 {
8  // Fourth order Runge-Kutta integrator
9  // arguments:
10  // x Current value of dependent variable
11  // t Independent variable (usually time)
12  // dt Step size (usually time step)
13  // i index of particle
14 
15  double x[6];
16 
17  this->copyTo(bunch->R[i], bunch->P[i], &x[0]);
18 
19  double deriv1[6];
20  double deriv2[6];
21  double deriv3[6];
22  double deriv4[6];
23  double xtemp[6];
24 
25  // Evaluate f1 = f(x,t).
26 
27  bool outOfBound = derivate_m(bunch, x, t, deriv1 , i, args ...);
28  if (outOfBound)
29  return false;
30 
31  // Evaluate f2 = f( x+dt*f1/2, t+dt/2 ).
32  const double half_dt = 0.5 * dt;
33  const double t_half = t + half_dt;
34 
35  for(int j = 0; j < 6; ++j)
36  xtemp[j] = x[j] + half_dt * deriv1[j];
37 
38  outOfBound = derivate_m(bunch, xtemp, t_half, deriv2 , i, args ...);
39  if (outOfBound)
40  return false;
41 
42  // Evaluate f3 = f( x+dt*f2/2, t+dt/2 ).
43  for(int j = 0; j < 6; ++j)
44  xtemp[j] = x[j] + half_dt * deriv2[j];
45 
46  outOfBound = derivate_m(bunch, xtemp, t_half, deriv3 , i, args ...);
47  if (outOfBound)
48  return false;
49 
50  // Evaluate f4 = f( x+dt*f3, t+dt ).
51  double t_full = t + dt;
52  for(int j = 0; j < 6; ++j)
53  xtemp[j] = x[j] + dt * deriv3[j];
54 
55  outOfBound = derivate_m(bunch, xtemp, t_full, deriv4 , i, args ...);
56  if (outOfBound)
57  return false;
58 
59  // Return x(t+dt) computed from fourth-order R-K.
60  for(int j = 0; j < 6; ++j)
61  x[j] += dt / 6.*(deriv1[j] + deriv4[j] + 2.*(deriv2[j] + deriv3[j]));
62 
63  this->copyFrom(bunch->R[i], bunch->P[i], &x[0]);
64 
65  return true;
66 }
67 
68 
69 template <typename FieldFunction, typename ... Arguments>
71  double* y,
72  const double& t,
73  double* yp,
74  const size_t& i,
75  Arguments& ... args) const
76 {
77  // New for OPAL 2.0: Changing variables to m, T, s
78  // Currently: m, ns, kG
79 
80  Vector_t externalE, externalB, tempR;
81 
82  externalB = Vector_t(0.0, 0.0, 0.0);
83  externalE = Vector_t(0.0, 0.0, 0.0);
84 
85  for(int j = 0; j < 3; ++j)
86  tempR(j) = y[j];
87 
88  bunch->R[i] = tempR;
89 
90  bool outOfBound = this->fieldfunc_m(t, i, externalE, externalB, args ...);
91 
92  double qtom = bunch->Q[i] / (bunch->M[i] * mass_coeff); // m^2/s^2/GV
93 
94  double tempgamma = sqrt(1 + (y[3] * y[3] + y[4] * y[4] + y[5] * y[5]));
95 
96  yp[0] = c_mtns / tempgamma * y[3]; // [m/ns]
97  yp[1] = c_mtns / tempgamma * y[4]; // [m/ns]
98  yp[2] = c_mtns / tempgamma * y[5]; // [m/ns]
99 
100 /*
101  yp[0] = c_mmtns / tempgamma * y[3]; // [mm/ns]
102  yp[1] = c_mmtns / tempgamma * y[4]; // [mm/ns]
103  yp[2] = c_mmtns / tempgamma * y[5]; // [mm/ns]
104 */
105 
106  yp[3] = (externalE(0) / Physics::c + (externalB(2) * y[4] - externalB(1) * y[5]) / tempgamma) * qtom; // [1/ns]
107  yp[4] = (externalE(1) / Physics::c - (externalB(2) * y[3] - externalB(0) * y[5]) / tempgamma) * qtom; // [1/ns];
108  yp[5] = (externalE(2) / Physics::c + (externalB(1) * y[3] - externalB(0) * y[4]) / tempgamma) * qtom; // [1/ns];
109 
110  return outOfBound;
111 }
112 
113 
114 template <typename FieldFunction, typename ... Arguments>
116  const Vector_t& P,
117  double* x) const
118 {
119  for (int j = 0; j < 3; j++) {
120  x[j] = R(j); // [x,y,z] (mm)
121  x[j+3] = P(j); // [px,py,pz] (beta*gamma)
122  }
123 }
124 
125 
126 template <typename FieldFunction, typename ... Arguments>
128  Vector_t& P,
129  double* x) const
130 {
131  for(int j = 0; j < 3; j++) {
132  R(j) = x[j]; // [x,y,z] (mm)
133  P(j) = x[j+3]; // [px,py,pz] (beta*gamma)
134  }
135 }
ParticleAttrib< Vector_t > P
ParticleAttrib< double > Q
bool derivate_m(PartBunchBase< double, 3 > *bunch, double *y, const double &t, double *yp, const size_t &i, Arguments &...args) const
Definition: RK4.hpp:70
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:52
void copyTo(const Vector_t &R, const Vector_t &P, double *x) const
Definition: RK4.hpp:115
Vektor< double, 3 > Vector_t
Definition: Vektor.h:6
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
bool doAdvance_m(PartBunchBase< double, 3 > *bunch, const size_t &i, const double &t, const double dt, Arguments &...args) const
Definition: RK4.hpp:2
void copyFrom(Vector_t &R, Vector_t &P, double *x) const
Definition: RK4.hpp:127
ParticleAttrib< double > M
ParticlePos_t & R