Skip to content

Commit 87cabb1

Browse files
authored
Merge pull request #299 from berquist/verlet-cpp
Update C++ implementations for Verlet algorithms
2 parents b285d9f + 35365e8 commit 87cabb1

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed
Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
#include <iomanip>
12
#include <iostream>
23

3-
// Simple function for velocity-verlet
4+
struct timestep {
5+
double time;
6+
double vel;
7+
};
8+
49
double verlet(double pos, double acc, double dt) {
510

611
double prev_pos = pos;
@@ -16,8 +21,7 @@ double verlet(double pos, double acc, double dt) {
1621
return time;
1722
}
1823

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

2226
double prev_pos = pos;
2327
double time = 0;
@@ -28,14 +32,15 @@ double stormer_verlet(double pos, double acc, double dt) {
2832
prev_pos = pos;
2933
pos = next_pos;
3034

31-
// The acceleration is constant, so the velocity is straightforward
35+
// The acceleration is constant, so the velocity is
36+
// straightforward
3237
vel += acc * dt;
3338
}
3439

35-
return time;
40+
return timestep { time, vel };
3641
}
3742

38-
double velocity_verlet(double pos, double acc, double dt) {
43+
timestep velocity_verlet(double pos, double acc, double dt) {
3944

4045
double time = 0;
4146
double vel = 0;
@@ -45,18 +50,35 @@ double velocity_verlet(double pos, double acc, double dt) {
4550
vel += acc * dt;
4651
}
4752

48-
return time;
53+
return timestep { time, vel };
4954
}
5055

5156
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;
5281

53-
// Note that depending on the simulation, you might want to have the verlet
54-
// loop outside.
82+
return 0;
5583

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

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:9-22, 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" %}
@@ -78,7 +78,7 @@ However, the error for this is $$\mathcal{O}(\Delta t)$$, which is quite poor, b
7878
{% sample lang="jl" %}
7979
[import:15-31, lang:"julia"](code/julia/verlet.jl)
8080
{% sample lang="cpp" %}
81-
[import:20-36, lang:"c_cpp"](code/c++/verlet.cpp)
81+
[import:24-41, lang:"c_cpp"](code/c++/verlet.cpp)
8282
{% sample lang="c" %}
8383
[import:16-31, lang:"c_cpp"](code/c/verlet.c)
8484
{% sample lang="java" %}
@@ -137,7 +137,7 @@ Here is the velocity Verlet method in code:
137137
{% sample lang="jl" %}
138138
[import:33-45, lang:"julia"](code/julia/verlet.jl)
139139
{% sample lang="cpp" %}
140-
[import:38-49, lang:"c_cpp"](code/c++/verlet.cpp)
140+
[import:43-54, lang:"c_cpp"](code/c++/verlet.cpp)
141141
{% sample lang="c" %}
142142
[import:33-43, lang:"c_cpp"](code/c/verlet.c)
143143
{% sample lang="java" %}

0 commit comments

Comments
 (0)