forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverlet.c
65 lines (53 loc) · 1.54 KB
/
verlet.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
void verlet(double *time, double pos, double acc, double dt) {
double prev_pos, temp_pos;
prev_pos = pos;
*time = 0.0;
while (pos > 0) {
*time += dt;
temp_pos = pos;
pos = pos * 2 - prev_pos + acc * dt * dt;
prev_pos = temp_pos;
}
}
void stormer_verlet(double *time, double *vel,
double pos, double acc, double dt) {
double prev_pos, temp_pos;
prev_pos = pos;
*vel = 0.0;
*time = 0.0;
while (pos > 0) {
*time += dt;
temp_pos = pos;
pos = pos * 2 - prev_pos + acc * dt * dt;
prev_pos = temp_pos;
*vel += acc * dt;
}
}
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;
}
}
int main() {
double time, vel;
verlet(&time, 5.0, -10, 0.01);
printf("[#]\nTime for Verlet integration is:\n");
printf("%lf\n", time);
stormer_verlet(&time, &vel, 5.0, -10, 0.01);
printf("[#]\nTime for Stormer Verlet integration is:\n");
printf("%lf\n", time);
printf("[#]\nVelocity for Stormer Verlet integration is:\n");
printf("%lf\n", vel);
velocity_verlet(&time, &vel, 5.0, -10, 0.01);
printf("[#]\nTime for velocity Verlet integration is:\n");
printf("%lf\n", time);
printf("[#]\nVelocity for Stormer Verlet integration is:\n");
printf("%lf\n", vel);
return 0;
}