Skip to content

Update C implementations for Verlet algorithms #298

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 2 commits into from
Jul 24, 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
60 changes: 33 additions & 27 deletions contents/verlet_integration/code/c/verlet.c
Original file line number Diff line number Diff line change
@@ -1,55 +1,61 @@
#include <stdio.h>

void verlet(double pos, double acc, double dt) {
double prev_pos, temp_pos, time;
void verlet(double *time, double pos, double acc, double dt) {
double prev_pos, temp_pos;
prev_pos = pos;
time = 0;
*time = 0.0;

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

printf("%f\n", time);
}

void stormer_verlet(double pos, double acc, double dt) {
double prev_pos, temp_pos, time, vel;
void stormer_verlet(double *time, double *vel,
double pos, double acc, double dt) {
double prev_pos, temp_pos;
prev_pos = pos;
vel = 0;
time = 0;
*vel = 0.0;
*time = 0.0;

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

vel += acc * dt;
*vel += acc * dt;
}

printf("%f\n", time);
}

void velocity_verlet(double pos, double acc, double dt) {
double time, vel;
vel = 0;
time = 0;
void velocity_verlet(double *time, double *vel,
double pos, double acc, double dt) {
*vel = 0.0;
*time = 0.0;

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

printf("%f\n", time);
}

int main() {
verlet(5.0, -10, 0.01);
stormer_verlet(5.0, -10, 0.01);
velocity_verlet(5.0, -10, 0.01);
double time, vel;

verlet(&time, 5.0, -10, 0.01);
printf("Time for Verlet integration is: %lf\n",
time);

stormer_verlet(&time, &vel, 5.0, -10, 0.01);
printf("Time and velocity for Stormer Verlet integration is: %lf, %lf\n",
time, vel);

velocity_verlet(&time, &vel, 5.0, -10, 0.01);
printf("Time and velocity for velocity Verlet integration is: %lf, %lf\n",
time, vel);

return 0;
}

7 changes: 3 additions & 4 deletions contents/verlet_integration/verlet_integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Here is what it looks like in code:
{% sample lang="cpp" %}
[import:4-17, lang:"c_cpp"](code/c++/verlet.cpp)
{% sample lang="c" %}
[import:3-16, lang:"c_cpp"](code/c/verlet.c)
[import:3-14, lang:"c_cpp"](code/c/verlet.c)
{% sample lang="java" %}
[import:2-18, lang:"java"](code/java/verlet.java)
{% sample lang="py" %}
Expand Down Expand Up @@ -81,7 +81,7 @@ Here's what it looks like in code:
{% sample lang="cpp" %}
[import:20-36, lang:"c_cpp"](code/c++/verlet.cpp)
{% sample lang="c" %}
[import:18-33, lang:"c_cpp"](code/c/verlet.c)
[import:16-31, lang:"c_cpp"](code/c/verlet.c)
{% sample lang="java" %}
[import:21-40, lang:"java"](code/java/verlet.java)
{% sample lang="py" %}
Expand Down Expand Up @@ -138,7 +138,7 @@ Here is the velocity Verlet method in code:
{% sample lang="cpp" %}
[import:38-49, lang:"c_cpp"](code/c++/verlet.cpp)
{% sample lang="c" %}
[import:35-46, lang:"c_cpp"](code/c/verlet.c)
[import:33-43, lang:"c_cpp"](code/c/verlet.c)
{% sample lang="java" %}
[import:43-57, lang:"java"](code/java/verlet.java)
{% sample lang="py" %}
Expand Down Expand Up @@ -223,4 +223,3 @@ $$
\newcommand{\bfomega}{\boldsymbol{\omega}}
\newcommand{\bftau}{\boldsymbol{\tau}}
$$