ChargeInConstantMagneticField.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <cmath>
3 #include <cstdlib>
4 
5 #include <utl/RK5ODEIntegrator.h>
6 
8 
9 using namespace std;
10 using namespace utl;
11 using namespace galactic;
12 
13 
14 typedef double Vector3[3];
15 typedef double Vector6[6];
16 
17 
19 public:
21  { fB0[0] = b0[0]; fB0[1] = b0[1]; fB0[2] = b0[2]; }
22 
23  void
24  operator()(const Vector6& /*position*/, Vector3& b)
25  {
26  // ignore position
27  b[0] = fB0[0];
28  b[1] = fB0[1];
29  b[2] = fB0[2];
30  }
31 
32 private:
34 };
35 
36 
37 int
39 {
40  typedef double Vector6[6];
41 
42  typedef ConstantMagneticField MFType;
43  const Vector3 b0 = { 1, -1, 2 };
44  MFType b(b0);
45 
47  ODE chargeMotionODE(1, 1, b);
48 
49  RK5ODEIntegrator<ODE> helix(chargeMotionODE);
50 
51  const double x0 = 0;
52  const double dxFirst = 0.1;
53  // first 3 elements are position, last 3 are (unit) velocity
54  const Vector6 x0u0 = { 0, 0, 0, 1, 0, 0 };
55  const double accuracy = 1e-5;
57  helix.AdaptiveBegin(x0, dxFirst, x0u0, accuracy);
58 
59  for (int i = 0; i < 30; ++i) {
60 
61  // normalize velocity vector to prevent numerical dissipation
62  // this is optional but should be used for low accuracy runs
63  Vector6& xu = it.GetY();
64  const double uNorm =
65  sqrt(xu[3]*xu[3] + xu[4]*xu[4] + xu[5]*xu[5]);
66  xu[3] /= uNorm;
67  xu[4] /= uNorm;
68  xu[5] /= uNorm;
69 
70  // output current time, position and velocity
71  cout << it.GetX();
72  for (int j = 0; j < 6; ++j)
73  cout << ' ' << xu[j];
74  cout << '\n';
75 
76  // advance with optimal step for this accuracy
77  ++it;
78  }
79 
80  return EXIT_SUCCESS;
81 }
void operator()(const Vector6 &, Vector3 &b)
int main(int argc, char *argv[])
Definition: DBSync.cc:58
double Vector6[6]
AdaptiveRK5Iterator< DerivativeFunctor, VectorType > AdaptiveBegin(const double x, const double dx, const VectorType &y, const double accuracy=1e-5)
double Vector3[3]

, generated on Tue Sep 26 2023.