1
+ #include < iomanip>
1
2
#include < iostream>
3
+ #include < utility>
4
+
5
+ typedef std::pair<double , double > vpair;
2
6
3
- // Simple function for velocity-verlet
4
7
double verlet (double pos, double acc, double dt) {
5
8
6
9
double prev_pos = pos;
@@ -16,8 +19,7 @@ double verlet(double pos, double acc, double dt) {
16
19
return time;
17
20
}
18
21
19
- // Simple function for stormer-verlet
20
- double stormer_verlet (double pos, double acc, double dt) {
22
+ std::pair<double , double > stormer_verlet (double pos, double acc, double dt) {
21
23
22
24
double prev_pos = pos;
23
25
double time = 0 ;
@@ -28,14 +30,15 @@ double stormer_verlet(double pos, double acc, double dt) {
28
30
prev_pos = pos;
29
31
pos = next_pos;
30
32
31
- // The acceleration is constant, so the velocity is straightforward
33
+ // The acceleration is constant, so the velocity is
34
+ // straightforward
32
35
vel += acc * dt;
33
36
}
34
37
35
- return time;
38
+ return std::make_pair ( time, vel) ;
36
39
}
37
40
38
- double velocity_verlet (double pos, double acc, double dt) {
41
+ std::pair< double , double > velocity_verlet (double pos, double acc, double dt) {
39
42
40
43
double time = 0 ;
41
44
double vel = 0 ;
@@ -45,18 +48,41 @@ double velocity_verlet(double pos, double acc, double dt) {
45
48
vel += acc * dt;
46
49
}
47
50
48
- return time;
51
+ return std::make_pair ( time, vel) ;
49
52
}
50
53
51
54
int main () {
55
+ double time, vel;
56
+ vpair time_vel_pair;
57
+
58
+ std::cout << std::fixed << std::setprecision (8 );
59
+
60
+ // Note that depending on the simulation, you might want to have the
61
+ // Verlet loop outside.
62
+
63
+ // For example, if your acceleration chages as a function of time,
64
+ // you might need to also change the acceleration to be read into
65
+ // each of these functions.
66
+
67
+ time = verlet (5.0 , -10 , 0.01 );
68
+ std::cout << " Time for Verlet integration is: " \
69
+ << time << std::endl;
70
+
71
+ time_vel_pair = stormer_verlet (5.0 , -10 , 0.01 );
72
+ time = time_vel_pair.first ;
73
+ vel = time_vel_pair.second ;
74
+ std::cout << " Time for Stormer Verlet integration is: " \
75
+ << time << std::endl;
76
+ std::cout << " Velocity for Stormer Verlet integration is: " \
77
+ << vel << std::endl;
52
78
53
- // Note that depending on the simulation, you might want to have the verlet
54
- // loop outside.
79
+ time_vel_pair = velocity_verlet (5.0 , -10 , 0.01 );
80
+ time = time_vel_pair.first ;
81
+ vel = time_vel_pair.second ;
82
+ std::cout << " Time for velocity Verlet integration is: " \
83
+ << time << std::endl;
84
+ std::cout << " Velocity for velocity Verlet integration is: " \
85
+ << vel << std::endl;
55
86
56
- // For example, if your acceleration chages as a function of time, you might
57
- // need to also change the acceleration to be read into each of these
58
- // functions
59
- std::cout << verlet (5.0 , -10 , 0.01 ) << std::endl;
60
- std::cout << stormer_verlet (5.0 , -10 , 0.01 ) << std::endl;
61
- std::cout << velocity_verlet (5.0 , -10 , 0.01 ) << std::endl;
87
+ return 0 ;
62
88
}
0 commit comments