-
-
Notifications
You must be signed in to change notification settings - Fork 359
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,60 @@ | ||
#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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line is too long, move the |
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line is also too long. |
||
*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_v; | ||
double time_sv, vel_sv; | ||
double time_vv, vel_vv; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not have one time and velocity variable since you set it to zero at the start of every function. |
||
|
||
verlet(&time_v, 5.0, -10, 0.01); | ||
stormer_verlet(&time_sv, &vel_sv, 5.0, -10, 0.01); | ||
velocity_verlet(&time_vv, &vel_vv, 5.0, -10, 0.01); | ||
|
||
printf("Time for Verlet integration is: %lf\n", | ||
time_v); | ||
printf("Time and velocity for Stormer Verlet integration is: %lf, %lf\n", | ||
time_sv, vel_sv); | ||
printf("Time and velocity for velocity Verlet integration is: %lf, %lf\n", | ||
time_vv, vel_vv); | ||
|
||
return 0; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need the brackets there.