Skip to content

Commit 10ebadf

Browse files
committed
Adjust C++ code for Verlet according to #257
1 parent f5060a3 commit 10ebadf

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed
Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
#include <iomanip>
12
#include <iostream>
3+
#include <utility>
4+
5+
typedef std::pair<double, double> vpair;
26

3-
// Simple function for velocity-verlet
47
double verlet(double pos, double acc, double dt) {
58

69
double prev_pos = pos;
@@ -16,8 +19,7 @@ double verlet(double pos, double acc, double dt) {
1619
return time;
1720
}
1821

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) {
2123

2224
double prev_pos = pos;
2325
double time = 0;
@@ -28,14 +30,15 @@ double stormer_verlet(double pos, double acc, double dt) {
2830
prev_pos = pos;
2931
pos = next_pos;
3032

31-
// The acceleration is constant, so the velocity is straightforward
33+
// The acceleration is constant, so the velocity is
34+
// straightforward
3235
vel += acc * dt;
3336
}
3437

35-
return time;
38+
return std::make_pair(time, vel);
3639
}
3740

38-
double velocity_verlet(double pos, double acc, double dt) {
41+
std::pair<double, double> velocity_verlet(double pos, double acc, double dt) {
3942

4043
double time = 0;
4144
double vel = 0;
@@ -45,18 +48,41 @@ double velocity_verlet(double pos, double acc, double dt) {
4548
vel += acc * dt;
4649
}
4750

48-
return time;
51+
return std::make_pair(time, vel);
4952
}
5053

5154
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;
5278

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;
5586

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;
6288
}

contents/verlet_integration/verlet_integration.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Here is what it looks like in code:
3333
{% sample lang="jl" %}
3434
[import:1-13, lang:"julia"](code/julia/verlet.jl)
3535
{% sample lang="cpp" %}
36-
[import:4-17, lang:"c_cpp"](code/c++/verlet.cpp)
36+
[import:7-20, lang:"c_cpp"](code/c++/verlet.cpp)
3737
{% sample lang="c" %}
3838
[import:3-14, lang:"c_cpp"](code/c/verlet.c)
3939
{% sample lang="java" %}
@@ -79,7 +79,7 @@ Here's what it looks like in code:
7979
{% sample lang="jl" %}
8080
[import:15-31, lang:"julia"](code/julia/verlet.jl)
8181
{% sample lang="cpp" %}
82-
[import:20-36, lang:"c_cpp"](code/c++/verlet.cpp)
82+
[import:22-39, lang:"c_cpp"](code/c++/verlet.cpp)
8383
{% sample lang="c" %}
8484
[import:16-31, lang:"c_cpp"](code/c/verlet.c)
8585
{% sample lang="java" %}
@@ -136,7 +136,7 @@ Here is the velocity Verlet method in code:
136136
{% sample lang="jl" %}
137137
[import:33-45, lang:"julia"](code/julia/verlet.jl)
138138
{% sample lang="cpp" %}
139-
[import:38-49, lang:"c_cpp"](code/c++/verlet.cpp)
139+
[import:41-52, lang:"c_cpp"](code/c++/verlet.cpp)
140140
{% sample lang="c" %}
141141
[import:33-43, lang:"c_cpp"](code/c/verlet.c)
142142
{% sample lang="java" %}

0 commit comments

Comments
 (0)