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