forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verlet.rs
64 lines (51 loc) · 1.57 KB
/
verlet.rs
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
fn verlet(mut pos: f64, acc: f64, dt: f64) -> f64 {
let mut prev_pos = pos;
let mut time = 0.0;
while pos > 0.0 {
time += dt;
let temp_pos = pos;
pos = pos * 2.0 - prev_pos + acc * dt * dt;
prev_pos = temp_pos;
}
time
}
fn stormer_verlet(mut pos: f64, acc: f64, dt: f64) -> (f64, f64) {
let mut prev_pos = pos;
let mut time = 0.0;
let mut vel = 0.0;
while pos > 0.0 {
time += dt;
let temp_pos = pos;
pos = pos * 2.0 - prev_pos + acc * dt * dt;
prev_pos = temp_pos;
// Because acceleration is constant, velocity is
// straightforward
vel += acc * dt;
}
(time, vel)
}
fn velocity_verlet(mut pos: f64, acc: f64, dt: f64) -> (f64, f64) {
let mut time = 0.0;
let mut vel = 0.0;
while pos > 0.0 {
time += dt;
pos += vel * dt + 0.5 * acc * dt * dt;
vel += acc * dt;
}
(time, vel)
}
fn main() {
let time_v = verlet(5.0, -10.0, 0.01);
let (time_sv, vel_sv) = stormer_verlet(5.0, -10.0, 0.01);
let (time_vv, vel_vv) = velocity_verlet(5.0, -10.0, 0.01);
println!("[#]\nTime for Verlet integration is:");
println!("{}", time_v);
println!("[#]\nTime for Stormer Verlet integration is:");
println!("{}", time_sv);
println!("[#]\nVelocity for Stormer Verlet integration is:");
println!("{}", vel_sv);
println!("[#]\nTime for velocity Verlet integration is:");
println!("{}", time_vv);
println!("[#]\nVelocity for velocity Verlet integration is:");
println!("{}", vel_vv);
}