ODEIntegrator.cc
Go to the documentation of this file.
1 
8 #include <iostream>
9 #include <iomanip>
10 #include <cmath>
11 #include <boost/program_options.hpp>
12 
13 #include <utl/RK4ODEIntegrator.h>
14 #include <utl/RK5ODEIntegrator.h>
15 
16 #include "HarmonicOscillator.h"
17 
18 using namespace std;
19 namespace po = boost::program_options;
20 using namespace utl;
21 
22 
23 int
24 main(int argc, char* argv[])
25 {
26  double dx;
27  double accuracy;
28  double x1;
29 
30  po::options_description desc("Allowed options");
31  desc.add_options()
32  ("help,h", "produce help message")
33  ("dx,d", po::value<double>(&dx)->default_value(0.5), "step size")
34  ("x1,1", po::value<double>(&x1)->default_value(1000), "end point")
35  ("accuracy,a", po::value<double>(&accuracy)->default_value(1e-3),
36  "variable step accuracy")
37  ("variable,v", "run variable step")
38  ("fixed,f", "run variable step with fixed try")
39  ;
40 
41  po::variables_map vm;
42  po::store(po::parse_command_line(argc, argv, desc), vm);
43  po::notify(vm);
44 
45  if (vm.count("help")) {
46  cout << desc << '\n';
47  return EXIT_SUCCESS;
48  }
49 
50  typedef double Vector[2];
51 
52  const double m = 1;
53  const double k = 1;
54  HarmonicOscillator ho(m, k);
55  const double omega = sqrt(k / m);
56 
58 
59  const double x0 = 0;
60  const Vector y0 = { 1, 0 };
61 
62  cout << setprecision(16);
63 
64  if (!vm.count("variable")) {
65 
67 
70 
71  for ( ; it4.GetX() <= x1; it4 += dx, it5 += dx) {
72  const double x = it4.GetX();
73  cout << x << ' ' << it5.GetX() << ' '
74  << it4.GetY()[0] << ' ' << it5.GetY()[0] << ' '
75  << it4.GetY()[1] << ' ' << it5.GetY()[1] << ' '
76  << y0[0] * cos(omega * x) << ' '
77  << -y0[0] * sin(omega * x) << '\n';
78  }
79 
80  } else {
81 
83  rk5.AdaptiveBegin(x0, dx, y0, accuracy);
84 
85  const bool fixed = vm.count("fixed");
86 
87  double x = it.GetX();
88 
89  while (x <= x1) {
90  cout << x << ' '
91  << it.GetY()[0] << ' '
92  << it.GetY()[1] << ' '
93  << y0[0] * cos(omega * x) << ' '
94  << -y0[0] * sin(omega * x) << ' ';
95  if (fixed)
96  it += dx;
97  else
98  ++it;
99  const double newx = it.GetX();
100  cout << newx - x << '\n';
101  x = newx;
102  }
103 
104  }
105 
106  return EXIT_SUCCESS;
107 }
RK5Iterator< DerivativeFunctor, VectorType > Begin(const double x, const VectorType &y)
RK4Iterator< DerivativeFunctor, VectorType > Begin(const double x, const VectorType &y)
double GetX() const
int main(int argc, char *argv[])
Definition: DBSync.cc:58
VectorType & GetY()
AdaptiveRK5Iterator< DerivativeFunctor, VectorType > AdaptiveBegin(const double x, const double dx, const VectorType &y, const double accuracy=1e-5)
double GetY(const CoordinateSystemPtr &coordinateSystem) const
Definition: BasicVector.h:209
Vector object.
Definition: Vector.h:30
constexpr double m
Definition: AugerUnits.h:121

, generated on Tue Sep 26 2023.