Skip to content

Improve readability of examples in verlet.cpp #243

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 1 commit into from
Jul 15, 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
49 changes: 22 additions & 27 deletions chapters/algorithms/verlet_integration/code/c++/verlet.cpp
Original file line number Diff line number Diff line change
@@ -1,56 +1,51 @@
#include <iostream>

// Simple function for velocity-verlet
void verlet(double pos, double acc, double dt) {
double verlet(double pos, double acc, double dt) {

// Note that we are using a temp variable for the previous position
double prev_pos, temp_pos, time;
prev_pos = pos;
time = 0;
double prev_pos = pos;
double time = 0;

while (pos > 0) {
time += dt;
temp_pos = pos;
pos = pos * 2 - prev_pos + acc * dt * dt;
prev_pos = temp_pos;
double next_pos = pos * 2 - prev_pos + acc * dt * dt;
prev_pos = pos;
pos = next_pos;
}

std::cout << time << '\n';
return time;
}

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

// Note that we are using a temp variable for the previous position
double prev_pos, temp_pos, time, vel;
prev_pos = pos;
vel = 0;
time = 0;
double prev_pos = pos;
double time = 0;
double vel = 0;
while (pos > 0) {
time += dt;
temp_pos = pos;
pos = pos * 2 - prev_pos + acc * dt * dt;
prev_pos = temp_pos;
double next_pos = pos * 2 - prev_pos + acc * dt * dt;
prev_pos = pos;
pos = next_pos;

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

std::cout << time << '\n';
return time;
}

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

double time, vel;
vel = 0;
time = 0;
double time = 0;
double vel = 0;
while (pos > 0) {
time += dt;
pos += vel * dt + 0.5 * acc * dt * dt;
vel += acc * dt;
}

std::cout << time << '\n';
return time;
}

int main() {
Expand All @@ -61,7 +56,7 @@ int main() {
// 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
verlet(5.0, -10, 0.01);
stormer_verlet(5.0, -10, 0.01);
velocity_verlet(5.0, -10, 0.01);
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 chapters/algorithms/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-19, lang:"c_cpp"](code/c++/verlet.cpp)
[import:4-17, lang:"c_cpp"](code/c++/verlet.cpp)
{% sample lang="c" %}
[import:3-16, 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:22-40, lang:"c_cpp"](code/c++/verlet.cpp)
[import:20-36, lang:"c_cpp"](code/c++/verlet.cpp)
{% sample lang="c" %}
[import:18-33, 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:42-54, lang:"c_cpp"](code/c++/verlet.cpp)
[import:38-49, lang:"c_cpp"](code/c++/verlet.cpp)
{% sample lang="c" %}
[import:35-46, lang:"c_cpp"](code/c/verlet.c)
{% sample lang="java" %}
Expand Down