forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverlet.nim
62 lines (50 loc) · 1.33 KB
/
verlet.nim
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
func verlet(pos_in, acc, dt: float): float =
var
pos: float = pos_in
prevPos: float = pos
time: float = 0.0
tempPos: float
while pos > 0.0:
time += dt
tempPos = pos
pos = pos * 2 - prevPos + acc * dt * dt
prevPos = tempPos
time
func stormerVerlet(pos_in, acc, dt: float): (float, float) =
var
pos: float = pos_in
prevPos: float = pos
time: float = 0.0
vel: float = 0.0
tempPos: float
while pos > 0.0:
time += dt
tempPos = pos
pos = pos * 2 - prevPos + acc * dt * dt
prevPos = tempPos
vel += acc * dt
(time, vel)
func velocityVerlet(pos_in, acc, dt: float): (float, float) =
var
pos: float = pos_in
time: float = 0.0
vel: float = 0.0
while pos > 0.0:
time += dt
pos += vel * dt + 0.5 * acc * dt * dt
vel += acc * dt
(time, vel)
when isMainModule:
let timeV = verlet(5.0, -10.0, 0.01)
echo "[#]\nTime for Verlet integration is:"
echo timeV
let (timeSV, velSV) = stormerVerlet(5.0, -10.0, 0.01)
echo "[#]\nTime for Stormer Verlet integration is:"
echo timeSV
echo "[#]\nVelocity for Stormer Verlet integration is:"
echo velSV
let (timeVV, velVV) = velocityVerlet(5.0, -10.0, 0.01)
echo "[#]\nTime for velocity Verlet integration is:"
echo timeVV
echo "[#]\nVelocity for velocity Verlet integration is:"
echo velVV