1
1
#include < iomanip>
2
2
#include < iostream>
3
- #include < utility>
4
3
5
- using vpair = std::pair<double , double >;
4
+ struct timestep {
5
+ double time;
6
+ double vel;
7
+ };
6
8
7
9
double verlet (double pos, double acc, double dt) {
8
10
@@ -19,7 +21,7 @@ double verlet(double pos, double acc, double dt) {
19
21
return time ;
20
22
}
21
23
22
- vpair stormer_verlet (double pos, double acc, double dt) {
24
+ timestep stormer_verlet (double pos, double acc, double dt) {
23
25
24
26
double prev_pos = pos;
25
27
double time = 0 ;
@@ -35,10 +37,10 @@ vpair stormer_verlet(double pos, double acc, double dt) {
35
37
vel += acc * dt;
36
38
}
37
39
38
- return std::make_pair ( time , vel) ;
40
+ return timestep { time , vel } ;
39
41
}
40
42
41
- vpair velocity_verlet (double pos, double acc, double dt) {
43
+ timestep velocity_verlet (double pos, double acc, double dt) {
42
44
43
45
double time = 0 ;
44
46
double vel = 0 ;
@@ -48,12 +50,10 @@ vpair velocity_verlet(double pos, double acc, double dt) {
48
50
vel += acc * dt;
49
51
}
50
52
51
- return std::make_pair ( time , vel) ;
53
+ return timestep { time , vel } ;
52
54
}
53
55
54
56
int main () {
55
- double time , vel;
56
-
57
57
std::cout << std::fixed << std::setprecision (8 );
58
58
59
59
// Note that depending on the simulation, you might want to have the
@@ -63,21 +63,21 @@ int main() {
63
63
// you might need to also change the acceleration to be read into
64
64
// each of these functions.
65
65
66
- time = verlet (5.0 , -10 , 0.01 );
66
+ double time = verlet (5.0 , -10 , 0.01 );
67
67
std::cout << " Time for Verlet integration is: " \
68
68
<< time << std::endl;
69
69
70
- std::tie ( time , vel) = stormer_verlet (5.0 , -10 , 0.01 );
70
+ timestep timestep_sv = stormer_verlet (5.0 , -10 , 0.01 );
71
71
std::cout << " Time for Stormer Verlet integration is: " \
72
- << time << std::endl;
72
+ << timestep_sv. time << std::endl;
73
73
std::cout << " Velocity for Stormer Verlet integration is: " \
74
- << vel << std::endl;
74
+ << timestep_sv. vel << std::endl;
75
75
76
- std::tie ( time , vel) = velocity_verlet (5.0 , -10 , 0.01 );
76
+ timestep timestep_vv = velocity_verlet (5.0 , -10 , 0.01 );
77
77
std::cout << " Time for velocity Verlet integration is: " \
78
- << time << std::endl;
78
+ << timestep_vv. time << std::endl;
79
79
std::cout << " Velocity for velocity Verlet integration is: " \
80
- << vel << std::endl;
80
+ << timestep_vv. vel << std::endl;
81
81
82
82
return 0 ;
83
83
0 commit comments