Skip to content

Update C++ implementations for Verlet algorithms #299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 37 additions & 15 deletions contents/verlet_integration/code/c++/verlet.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#include <iomanip>
#include <iostream>

// Simple function for velocity-verlet
struct timestep {
double time;
double vel;
};

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

double prev_pos = pos;
Expand All @@ -16,8 +21,7 @@ double verlet(double pos, double acc, double dt) {
return time;
}

// Simple function for stormer-verlet
double stormer_verlet(double pos, double acc, double dt) {
timestep stormer_verlet(double pos, double acc, double dt) {

double prev_pos = pos;
double time = 0;
Expand All @@ -28,14 +32,15 @@ double stormer_verlet(double pos, double acc, double dt) {
prev_pos = pos;
pos = next_pos;

// The acceleration is constant, so the velocity is straightforward
// The acceleration is constant, so the velocity is
// straightforward
vel += acc * dt;
}

return time;
return timestep { time, vel };
}

double velocity_verlet(double pos, double acc, double dt) {
timestep velocity_verlet(double pos, double acc, double dt) {

double time = 0;
double vel = 0;
Expand All @@ -45,18 +50,35 @@ double velocity_verlet(double pos, double acc, double dt) {
vel += acc * dt;
}

return time;
return timestep { time, vel };
}

int main() {
std::cout << std::fixed << std::setprecision(8);

// Note that depending on the simulation, you might want to have the
// Verlet loop outside.

// For example, if your acceleration chages as a function of time,
// you might need to also change the acceleration to be read into
// each of these functions.

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

timestep timestep_sv = stormer_verlet(5.0, -10, 0.01);
std::cout << "Time for Stormer Verlet integration is: " \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there a backslash?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to use an explicit line continuation indicator even when not required.

<< timestep_sv.time << std::endl;
std::cout << "Velocity for Stormer Verlet integration is: " \
<< timestep_sv.vel << std::endl;

timestep timestep_vv = velocity_verlet(5.0, -10, 0.01);
std::cout << "Time for velocity Verlet integration is: " \
<< timestep_vv.time << std::endl;
std::cout << "Velocity for velocity Verlet integration is: " \
<< timestep_vv.vel << std::endl;

// Note that depending on the simulation, you might want to have the verlet
// loop outside.
return 0;

// For example, if your acceleration chages as a function of time, you might
// need to also change the acceleration to be read into each of these
// functions
std::cout << verlet(5.0, -10, 0.01) << std::endl;
std::cout << stormer_verlet(5.0, -10, 0.01) << std::endl;
std::cout << velocity_verlet(5.0, -10, 0.01) << std::endl;
}
6 changes: 3 additions & 3 deletions contents/verlet_integration/verlet_integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Here is what it looks like in code:
{% sample lang="jl" %}
[import:1-13, lang:"julia"](code/julia/verlet.jl)
{% sample lang="cpp" %}
[import:4-17, lang:"c_cpp"](code/c++/verlet.cpp)
[import:9-22, lang:"c_cpp"](code/c++/verlet.cpp)
{% sample lang="c" %}
[import:3-14, lang:"c_cpp"](code/c/verlet.c)
{% sample lang="java" %}
Expand Down Expand Up @@ -79,7 +79,7 @@ Here's what it looks like in code:
{% sample lang="jl" %}
[import:15-31, lang:"julia"](code/julia/verlet.jl)
{% sample lang="cpp" %}
[import:20-36, lang:"c_cpp"](code/c++/verlet.cpp)
[import:24-41, lang:"c_cpp"](code/c++/verlet.cpp)
{% sample lang="c" %}
[import:16-31, lang:"c_cpp"](code/c/verlet.c)
{% sample lang="java" %}
Expand Down Expand Up @@ -136,7 +136,7 @@ Here is the velocity Verlet method in code:
{% sample lang="jl" %}
[import:33-45, lang:"julia"](code/julia/verlet.jl)
{% sample lang="cpp" %}
[import:38-49, lang:"c_cpp"](code/c++/verlet.cpp)
[import:43-54, lang:"c_cpp"](code/c++/verlet.cpp)
{% sample lang="c" %}
[import:33-43, lang:"c_cpp"](code/c/verlet.c)
{% sample lang="java" %}
Expand Down