forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verlet.go
60 lines (47 loc) · 1.13 KB
/
verlet.go
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
package main
import "fmt"
func verlet(pos, acc, dt float64) (time float64) {
prevPos := pos
time = 0
for pos > 0 {
time += dt
nextPos := pos*2 - prevPos + acc*dt*dt
prevPos, pos = pos, nextPos
}
return
}
func stormerVerlet(pos, acc, dt float64) (time, vel float64) {
prevPos := pos
time, vel = 0, 0
for pos > 0 {
time += dt
vel += acc * dt
nextPos := pos*2 - prevPos + acc*dt*dt
prevPos, pos = pos, nextPos
}
return
}
func velocityVerlet(pos, acc, dt float64) (time, vel float64) {
time, vel = 0, 0
for pos > 0 {
time += dt
pos += vel*dt + .5*acc*dt*dt
vel += acc * dt
}
return
}
func main() {
time := verlet(5., -10., .01)
fmt.Println("[#]\nTime for Verlet integration is:")
fmt.Println(time)
time, vel := stormerVerlet(5., -10., .01)
fmt.Println("[#]\nTime for Stormer Verlet integration is:")
fmt.Println(time)
fmt.Println("[#]\nVelocity for Stormer Verlet integration is:")
fmt.Println(vel)
time, vel = velocityVerlet(5., -10., .01)
fmt.Println("[#]\nTime for velocity Verlet integration is:")
fmt.Println(time)
fmt.Println("[#]\nVelocity for velocity Verlet integration is:")
fmt.Println(vel)
}