forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverlet.nim
56 lines (44 loc) · 1.27 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
proc 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
return time
proc 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
return (time, vel)
proc 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
return (time, vel)
let timeV = verlet(5.0, -10.0, 0.01)
echo "Time for Verlet integration is: ", timeV
let (timeSV, velSV) = stormerVerlet(5.0, -10.0, 0.01)
echo "Time for Stormer Verlet integration is: ", timeSV
echo "Velocity for Stormer Verlet integration is: ", velSV
let (timeVV, velVV) = velocityVerlet(5.0, -10.0, 0.01)
echo "Time for velocity Verlet integration is: ", timeVV
echo "Velocity for velocity Verlet integration is: ", velVV