Skip to content

Commit ccbad87

Browse files
committed
keep results in a struct rather than a pair
1 parent fc192e2 commit ccbad87

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

contents/verlet_integration/code/c++/verlet.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#include <iomanip>
22
#include <iostream>
3-
#include <utility>
43

5-
using vpair = std::pair<double, double>;
4+
struct timestep {
5+
double time;
6+
double vel;
7+
};
68

79
double verlet(double pos, double acc, double dt) {
810

@@ -19,7 +21,7 @@ double verlet(double pos, double acc, double dt) {
1921
return time;
2022
}
2123

22-
vpair stormer_verlet(double pos, double acc, double dt) {
24+
timestep stormer_verlet(double pos, double acc, double dt) {
2325

2426
double prev_pos = pos;
2527
double time = 0;
@@ -35,10 +37,10 @@ vpair stormer_verlet(double pos, double acc, double dt) {
3537
vel += acc * dt;
3638
}
3739

38-
return std::make_pair(time, vel);
40+
return timestep { time, vel };
3941
}
4042

41-
vpair velocity_verlet(double pos, double acc, double dt) {
43+
timestep velocity_verlet(double pos, double acc, double dt) {
4244

4345
double time = 0;
4446
double vel = 0;
@@ -48,12 +50,10 @@ vpair velocity_verlet(double pos, double acc, double dt) {
4850
vel += acc * dt;
4951
}
5052

51-
return std::make_pair(time, vel);
53+
return timestep { time, vel };
5254
}
5355

5456
int main() {
55-
double time, vel;
56-
5757
std::cout << std::fixed << std::setprecision(8);
5858

5959
// Note that depending on the simulation, you might want to have the
@@ -63,21 +63,21 @@ int main() {
6363
// you might need to also change the acceleration to be read into
6464
// each of these functions.
6565

66-
time = verlet(5.0, -10, 0.01);
66+
double time = verlet(5.0, -10, 0.01);
6767
std::cout << "Time for Verlet integration is: " \
6868
<< time << std::endl;
6969

70-
std::tie(time, vel) = stormer_verlet(5.0, -10, 0.01);
70+
timestep timestep_sv = stormer_verlet(5.0, -10, 0.01);
7171
std::cout << "Time for Stormer Verlet integration is: " \
72-
<< time << std::endl;
72+
<< timestep_sv.time << std::endl;
7373
std::cout << "Velocity for Stormer Verlet integration is: " \
74-
<< vel << std::endl;
74+
<< timestep_sv.vel << std::endl;
7575

76-
std::tie(time, vel) = velocity_verlet(5.0, -10, 0.01);
76+
timestep timestep_vv = velocity_verlet(5.0, -10, 0.01);
7777
std::cout << "Time for velocity Verlet integration is: " \
78-
<< time << std::endl;
78+
<< timestep_vv.time << std::endl;
7979
std::cout << "Velocity for velocity Verlet integration is: " \
80-
<< vel << std::endl;
80+
<< timestep_vv.vel << std::endl;
8181

8282
return 0;
8383

0 commit comments

Comments
 (0)