Skip to content

Commit d2ae305

Browse files
c252berquist
authored andcommitted
Verlet Integration in Nim (algorithm-archivists#598)
1 parent ba1fecb commit d2ae305

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
proc verlet(pos_in, acc, dt: float): float =
2+
var
3+
pos: float = pos_in
4+
prevPos: float = pos
5+
time: float = 0.0
6+
tempPos: float
7+
8+
while pos > 0.0:
9+
time += dt
10+
tempPos = pos
11+
pos = pos * 2 - prevPos + acc * dt * dt
12+
prevPos = tempPos
13+
14+
return time
15+
16+
proc stormerVerlet(pos_in, acc, dt: float): (float, float) =
17+
var
18+
pos: float = pos_in
19+
prevPos: float = pos
20+
time: float = 0.0
21+
vel: float = 0.0
22+
tempPos: float
23+
24+
while pos > 0.0:
25+
time += dt
26+
tempPos = pos
27+
pos = pos * 2 - prevPos + acc * dt * dt
28+
prevPos = tempPos
29+
30+
vel += acc * dt
31+
32+
return (time, vel)
33+
34+
proc velocityVerlet(pos_in, acc, dt: float): (float, float) =
35+
var
36+
pos: float = pos_in
37+
time: float = 0.0
38+
vel: float = 0.0
39+
40+
while pos > 0.0:
41+
time += dt
42+
pos += vel * dt + 0.5 * acc * dt * dt
43+
vel += acc * dt
44+
45+
return (time, vel)
46+
47+
let timeV = verlet(5.0, -10.0, 0.01)
48+
echo "Time for Verlet integration is: ", timeV
49+
50+
let (timeSV, velSV) = stormerVerlet(5.0, -10.0, 0.01)
51+
echo "Time for Stormer Verlet integration is: ", timeSV
52+
echo "Velocity for Stormer Verlet integration is: ", velSV
53+
54+
let (timeVV, velVV) = velocityVerlet(5.0, -10.0, 0.01)
55+
echo "Time for velocity Verlet integration is: ", timeVV
56+
echo "Velocity for velocity Verlet integration is: ", velVV

contents/verlet_integration/verlet_integration.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia cod
6767
[import:18-42, lang:"asm-x64"](code/asm-x64/verlet.s)
6868
{% sample lang="kotlin" %}
6969
[import:3-15, lang:"kotlin"](code/kotlin/verlet.kt)
70+
{% sample lang="nim" %}
71+
[import:1-14, lang:"nim"](code/nim/verlet.nim)
7072
{% endmethod %}
7173

7274
Now, obviously this poses a problem; what if we want to calculate a term that requires velocity, like the kinetic energy, $$\frac{1}{2}mv^2$$? In this case, we certainly cannot get rid of the velocity! Well, we can find the velocity to $$\mathcal{O}(\Delta t^2)$$ accuracy by using the Stormer-Verlet method, which is the same as before, but we calculate velocity like so
@@ -121,6 +123,8 @@ Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia cod
121123
[import:44-71, lang:"asm-x64"](code/asm-x64/verlet.s)
122124
{% sample lang="kotlin" %}
123125
[import:17-30, lang:"kotlin"](code/kotlin/verlet.kt)
126+
{% sample lang="nim" %}
127+
[import:16-32, lang:"nim"](code/nim/verlet.nim)
124128
{% endmethod %}
125129

126130

@@ -189,6 +193,8 @@ Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia cod
189193
[import:73-101, lang:"asm-x64"](code/asm-x64/verlet.s)
190194
{% sample lang="kotlin" %}
191195
[import:32-42, lang:"kotlin"](code/kotlin/verlet.kt)
196+
{% sample lang="nim" %}
197+
[import:34-45, lang:"nim"](code/nim/verlet.nim)
192198
{% endmethod %}
193199

194200
Even though this method is more widely used than the simple Verlet method mentioned above, it unforunately has an error term of $$\mathcal{O}(\Delta t^2)$$, which is two orders of magnitude worse. That said, if you want to have a simulaton with many objects that depend on one another --- like a gravity simulation --- the Velocity Verlet algorithm is a handy choice; however, you may have to play further tricks to allow everything to scale appropriately. These types of simulatons are sometimes called *n-body* simulations and one such trick is the Barnes-Hut algorithm, which cuts the complexity of n-body simulations from $$\sim \mathcal{O}(n^2)$$ to $$\sim \mathcal{O}(n\log(n))$$.
@@ -248,6 +254,8 @@ Submitted by P. Mekhail
248254
[import, lang:"asm-x64"](code/asm-x64/verlet.s)
249255
{% sample lang="kotlin" %}
250256
[import, lang:"kotlin"](code/kotlin/verlet.kt)
257+
{% sample lang="nim" %}
258+
[import, lang="nim"](code/nim/verlet.nim)
251259
{% endmethod %}
252260

253261

0 commit comments

Comments
 (0)