Skip to content

Commit 4f8728f

Browse files
sklanberquist
authored andcommitted
Added Verlet integration in Kotlin (#535)
1 parent 1bb82d9 commit 4f8728f

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
data class VerletValues(val time: Double, val vel: Double)
2+
3+
fun verlet(_pos: Double, acc: Double, dt: Double): Double {
4+
var pos = _pos // Since function parameter are val and can't be modified
5+
var prevPos = pos
6+
var time = 0.0
7+
8+
while (pos > 0) {
9+
time += dt
10+
val nextPos = pos * 2 - prevPos + acc * dt * dt
11+
prevPos = pos
12+
pos = nextPos
13+
}
14+
return time
15+
}
16+
17+
fun stormerVerlet(_pos: Double, acc: Double, dt: Double): VerletValues {
18+
var pos = _pos
19+
var prevPos = pos
20+
var time = 0.0
21+
var vel = 0.0
22+
while (pos > 0) {
23+
time += dt
24+
val nextPos = pos * 2 - prevPos + acc * dt * dt
25+
prevPos = pos
26+
pos = nextPos
27+
vel += acc * dt
28+
}
29+
return VerletValues(time, vel)
30+
}
31+
32+
fun velocityVerlet(_pos: Double, acc: Double, dt: Double): VerletValues {
33+
var pos = _pos
34+
var time = 0.0
35+
var vel = 0.0
36+
while (pos > 0) {
37+
time += dt
38+
pos += vel * dt + 0.5 * acc * dt * dt
39+
vel += acc * dt
40+
}
41+
return VerletValues(time, vel)
42+
}
43+
44+
fun main(args: Array<String>) {
45+
val verletTime = verlet(5.0, -10.0, 0.01)
46+
println("Time for Verlet integration is: $verletTime")
47+
48+
val stormerVerlet = stormerVerlet(5.0, -10.0, 0.01)
49+
println("Time for Stormer Verlet integration is: $stormerVerlet.time")
50+
println("Velocity for Stormer Verlet integration is: $stormerVerlet.vel")
51+
52+
val velocityVerlet = velocityVerlet(5.0, -10.0, 0.01)
53+
println("Time for Velocity Verlet integration is: $velocityVerlet.time")
54+
println("Velocity for Velocity Verlet integration is: $velocityVerlet.vel")
55+
}

contents/verlet_integration/verlet_integration.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia cod
6565
[import:5-16, lang:"go"](code/golang/verlet.go)
6666
{% sample lang="asm-x64" %}
6767
[import:18-42, lang:"asm-x64"](code/asm-x64/verlet.s)
68+
{% sample lang="kotlin" %}
69+
[import:3-15, lang:"kotlin"](code/kotlin/verlet.kt)
6870
{% endmethod %}
6971

7072
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
@@ -117,6 +119,8 @@ Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia cod
117119
[import:18-30, lang:"go"](code/golang/verlet.go)
118120
{% sample lang="asm-x64" %}
119121
[import:44-71, lang:"asm-x64"](code/asm-x64/verlet.s)
122+
{% sample lang="kotlin" %}
123+
[import:17-30, lang:"kotlin"](code/kotlin/verlet.kt)
120124
{% endmethod %}
121125

122126

@@ -183,6 +187,8 @@ Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia cod
183187
[import:32-42, lang:"go"](code/golang/verlet.go)
184188
{% sample lang="asm-x64" %}
185189
[import:73-101, lang:"asm-x64"](code/asm-x64/verlet.s)
190+
{% sample lang="kotlin" %}
191+
[import:32-42, lang:"kotlin"](code/kotlin/verlet.kt)
186192
{% endmethod %}
187193

188194
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))$$.
@@ -231,6 +237,8 @@ Submitted by P. Mekhail
231237
[import, lang:"go"](code/golang/verlet.go)
232238
{% sample lang="asm-x64" %}
233239
[import, lang:"asm-x64"](code/asm-x64/verlet.s)
240+
{% sample lang="kotlin" %}
241+
[import, lang:"kotlin"](code/kotlin/verlet.kt)
234242
{% endmethod %}
235243

236244

0 commit comments

Comments
 (0)