|
1 | 1 | #include <iostream>
|
2 | 2 |
|
3 | 3 | // Simple function for velocity-verlet
|
4 |
| -void verlet(double pos, double acc, double dt){ |
| 4 | +void verlet(double pos, double acc, double dt) { |
5 | 5 |
|
6 |
| - // Note that we are using a temp variable for the previous position |
7 |
| - double prev_pos, temp_pos, time; |
8 |
| - prev_pos = pos; |
9 |
| - time = 0; |
| 6 | + // Note that we are using a temp variable for the previous position |
| 7 | + double prev_pos, temp_pos, time; |
| 8 | + prev_pos = pos; |
| 9 | + time = 0; |
10 | 10 |
|
11 |
| - while (pos > 0){ |
12 |
| - time += dt; |
13 |
| - temp_pos = pos; |
14 |
| - pos = pos*2 - prev_pos + acc * dt * dt; |
15 |
| - prev_pos = temp_pos; |
16 |
| - } |
17 |
| - |
18 |
| - std::cout << time << '\n'; |
| 11 | + while (pos > 0) { |
| 12 | + time += dt; |
| 13 | + temp_pos = pos; |
| 14 | + pos = pos * 2 - prev_pos + acc * dt * dt; |
| 15 | + prev_pos = temp_pos; |
| 16 | + } |
19 | 17 |
|
| 18 | + std::cout << time << '\n'; |
20 | 19 | }
|
21 | 20 |
|
22 | 21 | // Simple function for stormer-verlet
|
23 |
| -void stormer_verlet(double pos, double acc, double dt){ |
24 |
| - |
25 |
| - // Note that we are using a temp variable for the previous position |
26 |
| - double prev_pos, temp_pos, time, vel; |
27 |
| - prev_pos = pos; |
28 |
| - vel = 0; |
29 |
| - time = 0; |
30 |
| - while (pos > 0){ |
31 |
| - time += dt; |
32 |
| - temp_pos = pos; |
33 |
| - pos = pos*2 - prev_pos + acc * dt * dt; |
34 |
| - prev_pos = temp_pos; |
35 |
| - |
36 |
| - // The acceleration is constant, so the velocity is straightforward |
37 |
| - vel += acc*dt; |
38 |
| - } |
39 |
| - |
40 |
| - std::cout << time << '\n'; |
41 |
| - |
| 22 | +void stormer_verlet(double pos, double acc, double dt) { |
| 23 | + |
| 24 | + // Note that we are using a temp variable for the previous position |
| 25 | + double prev_pos, temp_pos, time, vel; |
| 26 | + prev_pos = pos; |
| 27 | + vel = 0; |
| 28 | + time = 0; |
| 29 | + while (pos > 0) { |
| 30 | + time += dt; |
| 31 | + temp_pos = pos; |
| 32 | + pos = pos * 2 - prev_pos + acc * dt * dt; |
| 33 | + prev_pos = temp_pos; |
| 34 | + |
| 35 | + // The acceleration is constant, so the velocity is straightforward |
| 36 | + vel += acc * dt; |
| 37 | + } |
| 38 | + |
| 39 | + std::cout << time << '\n'; |
42 | 40 | }
|
43 | 41 |
|
44 |
| -void velocity_verlet(double pos, double acc, double dt){ |
45 |
| - |
46 |
| - double time, vel; |
47 |
| - vel = 0; |
48 |
| - time = 0; |
49 |
| - while (pos > 0){ |
50 |
| - time += dt; |
51 |
| - pos += vel*dt + 0.5*acc * dt * dt; |
52 |
| - vel += acc*dt; |
53 |
| - } |
| 42 | +void velocity_verlet(double pos, double acc, double dt) { |
54 | 43 |
|
55 |
| - std::cout << time << '\n'; |
| 44 | + double time, vel; |
| 45 | + vel = 0; |
| 46 | + time = 0; |
| 47 | + while (pos > 0) { |
| 48 | + time += dt; |
| 49 | + pos += vel * dt + 0.5 * acc * dt * dt; |
| 50 | + vel += acc * dt; |
| 51 | + } |
56 | 52 |
|
| 53 | + std::cout << time << '\n'; |
57 | 54 | }
|
58 | 55 |
|
59 |
| -int main(){ |
| 56 | +int main() { |
60 | 57 |
|
61 |
| - // Note that depending on the simulation, you might want to have the verlet |
62 |
| - // loop outside. |
63 |
| - |
64 |
| - // For example, if your acceleration chages as a function of time, you might |
65 |
| - // need to also change the acceleration to be read into each of these |
66 |
| - // functions |
67 |
| - verlet(5.0, -10, 0.01); |
68 |
| - stormer_verlet(5.0, -10, 0.01); |
69 |
| - velocity_verlet(5.0, -10, 0.01); |
| 58 | + // Note that depending on the simulation, you might want to have the verlet |
| 59 | + // loop outside. |
70 | 60 |
|
| 61 | + // For example, if your acceleration chages as a function of time, you might |
| 62 | + // need to also change the acceleration to be read into each of these |
| 63 | + // functions |
| 64 | + verlet(5.0, -10, 0.01); |
| 65 | + stormer_verlet(5.0, -10, 0.01); |
| 66 | + velocity_verlet(5.0, -10, 0.01); |
71 | 67 | }
|
72 |
| - |
0 commit comments